|
| 1 | +import lzma |
| 2 | +import re |
| 3 | +import zlib |
| 4 | +from pathlib import Path |
| 5 | +from typing import Callable, Optional |
| 6 | + |
| 7 | +import pyzstd |
| 8 | + |
| 9 | +from unblob.file_utils import ( |
| 10 | + Endian, |
| 11 | + FileSystem, |
| 12 | + InvalidInputFormat, |
| 13 | + StructParser, |
| 14 | + iterate_file, |
| 15 | +) |
| 16 | +from unblob.models import ( |
| 17 | + Extractor, |
| 18 | + ExtractResult, |
| 19 | + File, |
| 20 | + Regex, |
| 21 | + StructHandler, |
| 22 | + ValidChunk, |
| 23 | +) |
| 24 | + |
| 25 | +# [Ref] https://github.com/freebsd/freebsd-src/tree/master/sys/geom/uzip |
| 26 | +C_DEFINITIONS = r""" |
| 27 | + typedef struct uzip_header{ |
| 28 | + char magic[16]; |
| 29 | + char format[112]; |
| 30 | + uint32_t block_size; |
| 31 | + uint32_t block_count; |
| 32 | + uint64_t toc[block_count]; |
| 33 | + } uzip_header_t; |
| 34 | +""" |
| 35 | + |
| 36 | +HEADER_STRUCT = "uzip_header_t" |
| 37 | + |
| 38 | +ZLIB_COMPRESSION = "#!/bin/sh\x0a#V2.0\x20" |
| 39 | +LZMA_COMPRESSION = "#!/bin/sh\x0a#L3.0\x0a" |
| 40 | +ZSTD_COMPRESSION = "#!/bin/sh\x0a#Z4.0\x20" |
| 41 | + |
| 42 | + |
| 43 | +class Decompressor: |
| 44 | + DECOMPRESSOR: Callable |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self._decompressor = self.DECOMPRESSOR() |
| 48 | + |
| 49 | + def decompress(self, data: bytes) -> bytes: |
| 50 | + return self._decompressor.decompress(data) |
| 51 | + |
| 52 | + def flush(self) -> bytes: |
| 53 | + return b"" |
| 54 | + |
| 55 | + |
| 56 | +class LZMADecompressor(Decompressor): |
| 57 | + DECOMPRESSOR = lzma.LZMADecompressor |
| 58 | + |
| 59 | + |
| 60 | +class ZLIBDecompressor(Decompressor): |
| 61 | + DECOMPRESSOR = zlib.decompressobj |
| 62 | + |
| 63 | + def flush(self) -> bytes: |
| 64 | + return self._decompressor.flush() |
| 65 | + |
| 66 | + |
| 67 | +class ZSTDDecompressor(Decompressor): |
| 68 | + DECOMPRESSOR = pyzstd.EndlessZstdDecompressor |
| 69 | + |
| 70 | + |
| 71 | +DECOMPRESS_METHOD: dict[bytes, type[Decompressor]] = { |
| 72 | + ZLIB_COMPRESSION.encode(): ZLIBDecompressor, |
| 73 | + LZMA_COMPRESSION.encode(): LZMADecompressor, |
| 74 | + ZSTD_COMPRESSION.encode(): ZSTDDecompressor, |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +class UZIPExtractor(Extractor): |
| 79 | + def extract(self, inpath: Path, outdir: Path): |
| 80 | + infile = File.from_path(inpath) |
| 81 | + parser = StructParser(C_DEFINITIONS) |
| 82 | + header = parser.parse(HEADER_STRUCT, infile, Endian.BIG) |
| 83 | + fs = FileSystem(outdir) |
| 84 | + outpath = Path(inpath.stem) |
| 85 | + |
| 86 | + try: |
| 87 | + decompressor_cls = DECOMPRESS_METHOD[header.magic] |
| 88 | + except LookupError: |
| 89 | + raise InvalidInputFormat("unsupported compression format") from None |
| 90 | + |
| 91 | + with fs.open(outpath, "wb+") as outfile: |
| 92 | + for current_offset, next_offset in zip(header.toc[:-1], header.toc[1:]): |
| 93 | + compressed_len = next_offset - current_offset |
| 94 | + if compressed_len == 0: |
| 95 | + continue |
| 96 | + decompressor = decompressor_cls() |
| 97 | + for chunk in iterate_file(infile, current_offset, compressed_len): |
| 98 | + outfile.write(decompressor.decompress(chunk)) |
| 99 | + outfile.write(decompressor.flush()) |
| 100 | + return ExtractResult(reports=fs.problems) |
| 101 | + |
| 102 | + |
| 103 | +class UZIPHandler(StructHandler): |
| 104 | + NAME = "uzip" |
| 105 | + PATTERNS = [ |
| 106 | + Regex(re.escape(ZLIB_COMPRESSION)), |
| 107 | + Regex(re.escape(LZMA_COMPRESSION)), |
| 108 | + Regex(re.escape(ZSTD_COMPRESSION)), |
| 109 | + ] |
| 110 | + HEADER_STRUCT = HEADER_STRUCT |
| 111 | + C_DEFINITIONS = C_DEFINITIONS |
| 112 | + EXTRACTOR = UZIPExtractor() |
| 113 | + |
| 114 | + def is_valid_header(self, header) -> bool: |
| 115 | + return ( |
| 116 | + header.block_count > 0 |
| 117 | + and header.block_size > 0 |
| 118 | + and header.block_size % 512 == 0 |
| 119 | + ) |
| 120 | + |
| 121 | + def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: |
| 122 | + header = self.parse_header(file, Endian.BIG) |
| 123 | + |
| 124 | + if not self.is_valid_header(header): |
| 125 | + raise InvalidInputFormat("Invalid uzip header.") |
| 126 | + |
| 127 | + # take the last TOC block offset, end of file is that block offset, |
| 128 | + # starting from the start offset |
| 129 | + end_offset = start_offset + header.toc[-1] |
| 130 | + return ValidChunk( |
| 131 | + start_offset=start_offset, |
| 132 | + end_offset=end_offset, |
| 133 | + ) |
0 commit comments