MCPcopy
hub / github.com/Gallopsled/pwntools / normalize_writes

Function normalize_writes

pwnlib/fmtstr.py:126–154  ·  view source on GitHub ↗

r""" This function converts user-specified writes to a dict ``{ address1: data1, address2: data2, ... }`` such that all values are raw bytes and consecutive writes are merged to a single key. Examples: >>> context.clear(endian="little", bits=32) >>> normalize_writes({0x

(writes)

Source from the content-addressed store, hash-verified

124}
125
126def normalize_writes(writes):
127 r"""
128 This function converts user-specified writes to a dict ``{ address1: data1, address2: data2, ... }``
129 such that all values are raw bytes and consecutive writes are merged to a single key.
130
131 Examples:
132
133 >>> context.clear(endian="little", bits=32)
134 >>> normalize_writes({0x0: [p32(0xdeadbeef)], 0x4: p32(0xf00dface), 0x10: 0x41414141})
135 [(0, b'\xef\xbe\xad\xde\xce\xfa\r\xf0'), (16, b'AAAA')]
136 """
137 # make all writes flat
138 writes = { address: flat(data) for address, data in writes.items() }
139
140 # merge adjacent writes (and detect overlaps)
141 merged = []
142 prev_end = -1
143 for address, data in sorted(writes.items(), key=itemgetter(0)):
144 if address < prev_end:
145 raise ValueError("normalize_writes(): data at offset %d overlaps with previous data which ends at offset %d" % (address, prev_end))
146
147 if address == prev_end and merged:
148 merged[-1] = (merged[-1][0], merged[-1][1] + data)
149 else:
150 merged.append((address, data))
151
152 prev_end = address + len(data)
153
154 return merged
155
156# optimization examples (with bytes_written=0)
157#

Callers 1

make_atomsFunction · 0.85

Calls 3

flatFunction · 0.85
itemsMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected