blob: df7f1703ef3b71834a7610db376df3e7698d4c42 [file] [log] [blame]
Kelvin Zhange2399442021-08-12 10:36:37 -07001#!/usr/bin/env python3
2
3import zipfile
4import struct
5
6
7def 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
22def 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
36if __name__ == '__main__':
37 import sys
38 sys.exit(main(sys.argv))