(input_filepath, file_hash, patches)
| 11 | if getenv("USBDEV", ""): SUPPORTED_CONTROLLERS.insert(0, (int(x, 16) for x in getenv("USBDEV", "").split(":"))) |
| 12 | |
| 13 | def patch(input_filepath, file_hash, patches): |
| 14 | with open(input_filepath, 'rb') as infile: data = bytearray(infile.read()) |
| 15 | |
| 16 | if_hash = hashlib.md5(data).hexdigest() |
| 17 | if if_hash != file_hash: |
| 18 | raise ValueError(f"File hash mismatch: expected {file_hash}, got {if_hash}") |
| 19 | |
| 20 | for offset, expected_bytes, new_bytes in patches: |
| 21 | if len(expected_bytes) != len(new_bytes): |
| 22 | raise ValueError("Expected bytes and new bytes must be the same length") |
| 23 | |
| 24 | if offset + len(new_bytes) > len(data): return False |
| 25 | current_bytes = data[offset:offset + len(expected_bytes)] |
| 26 | assert bytes(current_bytes) == expected_bytes, f"Expected {expected_bytes} at offset {offset:x}, but got {current_bytes}" |
| 27 | data[offset:offset + len(new_bytes)] = new_bytes |
| 28 | |
| 29 | checksum = sum(data[4:-6]) & 0xff |
| 30 | crc32 = zlib.crc32(data[4:-6]).to_bytes(4, 'little') |
| 31 | data[-5] = checksum |
| 32 | data[-4] = crc32[0] |
| 33 | data[-3] = crc32[1] |
| 34 | data[-2] = crc32[2] |
| 35 | data[-1] = crc32[3] |
| 36 | return data |
| 37 | |
| 38 | path = os.path.dirname(os.path.abspath(__file__)) |
| 39 | file_hash = "5284e618d96ef804c06f47f3b73656b7" |
searching dependent graphs…