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)
| 124 | } |
| 125 | |
| 126 | def 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 | # |
no test coverage detected