|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +"""XCode extractor |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import sys |
| 11 | +import struct |
| 12 | +import zlib |
| 13 | +import xml.etree.ElementTree as ET |
| 14 | +import lzma |
| 15 | + |
| 16 | +XAR_MAGIC = b'\x78\x61\x72\x21' |
| 17 | +PBZX_MAGIC = b'\x70\x62\x7a\x78' |
| 18 | +LZMA_MAGIC = b'\xfd\x37\x7a\x58\x5a\x00' |
| 19 | + |
| 20 | +class io_wrapper(object): |
| 21 | + """Helper for stdin/stdout binary weirdness""" |
| 22 | + def __init__(self, filename, mode): |
| 23 | + self.filename = filename |
| 24 | + self.mode = mode |
| 25 | + def __enter__(self): |
| 26 | + if self.filename == '-': |
| 27 | + if self.mode is None or self.mode == '' or 'r' in self.mode: |
| 28 | + self.fh = sys.stdin |
| 29 | + else: |
| 30 | + self.fh = sys.stdout |
| 31 | + else: |
| 32 | + self.fh = open(self.filename, self.mode) |
| 33 | + return self |
| 34 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 35 | + if self.filename != '-': |
| 36 | + self.fh.close() |
| 37 | + def write(self, bytes): |
| 38 | + if self.filename != '-': |
| 39 | + return self.fh.write(bytes) |
| 40 | + return self.fh.buffer.write(bytes) |
| 41 | + def read(self, size): |
| 42 | + if self.filename != '-': |
| 43 | + return self.fh.read(size) |
| 44 | + return self.fh.buffer.read(size) |
| 45 | + def seek(self, size): |
| 46 | + return self.fh.seek(size) |
| 47 | + |
| 48 | +def run(): |
| 49 | + parser = argparse.ArgumentParser( |
| 50 | + description=__doc__, formatter_class=argparse.RawTextHelpFormatter) |
| 51 | + |
| 52 | + parser.add_argument("-f", '--file', nargs='?', default="-") |
| 53 | + parser.add_argument('outfile', nargs='?', default="-") |
| 54 | + |
| 55 | + args = parser.parse_args() |
| 56 | + |
| 57 | + with io_wrapper(args.file, "rb") as infile, io_wrapper(args.outfile, "wb") as outfile: |
| 58 | + |
| 59 | + start_offset = 0 |
| 60 | + magic = infile.read(4) |
| 61 | + if magic != XAR_MAGIC: |
| 62 | + print("bad xar magic", file=sys.stderr) |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + bytes = infile.read(24) |
| 66 | + header_size, xar_version, toc_compressed, toc_uncompressed, checksum_type = struct.unpack('>HHQQI', bytes) |
| 67 | + start_offset += header_size |
| 68 | + start_offset += toc_compressed |
| 69 | + bytes = infile.read(toc_compressed) |
| 70 | + |
| 71 | + xml_toc = zlib.decompress(bytes).decode("utf-8") |
| 72 | + |
| 73 | + root = ET.fromstring(xml_toc) |
| 74 | + |
| 75 | + content_offset = 0 |
| 76 | + content_length = 0 |
| 77 | + content_encoding = "" |
| 78 | + |
| 79 | + toc = root.find("toc") |
| 80 | + for elem in toc.findall("file"): |
| 81 | + name = elem.find("name").text |
| 82 | + if name == "Content": |
| 83 | + data = elem.find("data") |
| 84 | + content_offset = int(data.find("offset").text) |
| 85 | + content_length = int(data.find("length").text) |
| 86 | + content_encoding = data.find("encoding").get("style") |
| 87 | + content_uncompressed_length = int(data.find("size").text) |
| 88 | + found_content = True |
| 89 | + break |
| 90 | + |
| 91 | + if (content_length == 0): |
| 92 | + print("No \"Content\" file to extract.", file=sys.stderr) |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + infile.seek(content_offset + start_offset) |
| 96 | + |
| 97 | + content_read_size = 0 |
| 98 | + |
| 99 | + magic = infile.read(4) |
| 100 | + content_read_size += 4 |
| 101 | + if magic != PBZX_MAGIC: |
| 102 | + print("bad pbzx magic", file=sys.stderr) |
| 103 | + sys.exit(2) |
| 104 | + |
| 105 | + bytes = infile.read(8) |
| 106 | + content_read_size += 8 |
| 107 | + flags, = struct.unpack('>Q', bytes) |
| 108 | + bytes = infile.read(16) |
| 109 | + content_read_size += 16 |
| 110 | + while (flags & 1 << 24): |
| 111 | + flags, size = struct.unpack('>QQ', bytes) |
| 112 | + bytes = infile.read(size) |
| 113 | + content_read_size += size |
| 114 | + compressed = size != 1 << 24 |
| 115 | + if compressed: |
| 116 | + if bytes[0:6] != LZMA_MAGIC: |
| 117 | + print("bad lzma magic: ", file=sys.stderr) |
| 118 | + sys.exit(3) |
| 119 | + outfile.write(lzma.decompress(bytes)) |
| 120 | + else: |
| 121 | + outfile.write(bytes) |
| 122 | + |
| 123 | + if content_read_size == content_length: |
| 124 | + break |
| 125 | + |
| 126 | + bytes = infile.read(16) |
| 127 | + content_read_size += 16 |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + run() |
0 commit comments