Kelvin Zhang | e239944 | 2021-08-12 10:36:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import zipfile |
| 4 | import struct |
| 5 | |
| 6 | |
| 7 | def readPayloadMetadata(zfp: zipfile.ZipFile, entry): |
| 8 | _MAGIC = b'CrAU' |
| 9 | # 8 bytes for version, 8 bytes for manifest length, 4 bytes for metadata signature length |
| 10 | HEADER_STRUCT = ">4sQQL" |
| 11 | HEADER_LEN = struct.calcsize(HEADER_STRUCT) |
| 12 | with zfp.open(entry) as fp: |
| 13 | header = fp.read(HEADER_LEN) |
| 14 | (magic, version, manifest_length, |
| 15 | metadata_signature_len) = struct.unpack(HEADER_STRUCT, header) |
| 16 | assert magic == _MAGIC |
| 17 | assert version == 2, "Unsupported major payload version " + str(version) |
| 18 | print(f"{manifest_length} {metadata_signature_len}") |
| 19 | return header + fp.read(manifest_length + metadata_signature_len) |
| 20 | |
| 21 | |
| 22 | def main(argv): |
| 23 | if len(argv) != 3: |
| 24 | print("Usage:", argv[0], "<input file> <output file>") |
| 25 | return 1 |
| 26 | infile = argv[1] |
| 27 | outfile = argv[2] |
| 28 | with zipfile.ZipFile(infile, "r") as inzfp, zipfile.ZipFile(outfile, "w") as outzfp: |
| 29 | for entry in inzfp.infolist(): |
| 30 | if entry.filename.startswith("META") or entry.filename.endswith(".map"): |
| 31 | outzfp.writestr(entry, inzfp.read(entry)) |
| 32 | elif entry.filename == "payload.bin": |
| 33 | outzfp.writestr(entry, readPayloadMetadata(inzfp, entry)) |
| 34 | |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | import sys |
| 38 | sys.exit(main(sys.argv)) |