"""Outer layer of a .vromfs.bin file. Layout: header 16 bytes: magic (VRFs | VRFx), platform, unpacked size, packed word ext header 8 bytes, only for VRFx: size (=8), flags, version (stored reversed) payload compressed (zstd, obfuscated) or plain image bytes digest 16-byte MD5 of the unpacked image, absent for the "no check" pack type signature 0 or 256 trailing bytes The packed word is little-endian: top 6 bits are the pack type, low 26 bits the packed size. """ from __future__ import annotations import hashlib import struct from dataclasses import dataclass from enum import Enum import zstandard from . import obfuscation from .errors import VromfsFormatError SIGNATURE_SIZE = 0x100 _HEADER = struct.Struct("<4s4sII") _EXT_HEADER = struct.Struct(" bool: return self is not PackType.PLAIN @property def checked(self) -> bool: return self is not PackType.ZSTD_NO_CHECK @dataclass(frozen=True) class ContainerInfo: """Everything about the container except the image itself; reused when writing back.""" magic: Magic platform: bytes pack_type: PackType ext_flags: int | None version: tuple[int, int, int, int] | None signature: bytes def parse_container(data: bytes) -> tuple[ContainerInfo, bytes]: """Return the container description and the unpacked inner image.""" if len(data) < _HEADER.size: raise VromfsFormatError("file too short for a container header") magic_raw, platform, unpacked_size, packed_word = _HEADER.unpack_from(data, 0) try: magic = Magic(magic_raw) pack_type = PackType(packed_word >> 26) except ValueError as error: raise VromfsFormatError(f"unknown container header: {error}") from error packed_size = packed_word & 0x03FF_FFFF cursor = _HEADER.size ext_flags = version = None if magic is Magic.VRFX: ext_size, ext_flags, version_raw = _EXT_HEADER.unpack_from(data, cursor) if ext_size != _EXT_HEADER.size: raise VromfsFormatError(f"unexpected extended header size {ext_size}") version = tuple(reversed(version_raw)) cursor += _EXT_HEADER.size stored_size = packed_size if pack_type.compressed else unpacked_size stored = data[cursor:cursor + stored_size] cursor += stored_size image = _unpack_payload(stored, pack_type, unpacked_size) if pack_type.checked: digest = data[cursor:cursor + 16] cursor += 16 if hashlib.md5(image).digest() != digest: raise VromfsFormatError("image MD5 does not match the container digest") signature = data[cursor:] if len(signature) not in (0, SIGNATURE_SIZE): raise VromfsFormatError(f"unexpected trailing data of {len(signature)} bytes") info = ContainerInfo(magic, platform, pack_type, ext_flags, version, signature) return info, image def build_container(info: ContainerInfo, image: bytes) -> bytes: """Pack an inner image using the same container settings it was read with.""" stored = _pack_payload(image, info.pack_type) packed_size = len(stored) if info.pack_type.compressed else 0 if packed_size > 0x03FF_FFFF: raise VromfsFormatError("compressed payload too large for the 26-bit size field") parts = [_HEADER.pack(info.magic.value, info.platform, len(image), (info.pack_type.value << 26) | packed_size)] if info.magic is Magic.VRFX: parts.append(_EXT_HEADER.pack(_EXT_HEADER.size, info.ext_flags or 0, bytes(reversed(info.version)))) parts.append(stored) if info.pack_type.checked: parts.append(hashlib.md5(image).digest()) parts.append(info.signature) return b"".join(parts) def _unpack_payload(stored: bytes, pack_type: PackType, unpacked_size: int) -> bytes: if not pack_type.compressed: return stored compressed = obfuscation.toggle(stored) image = zstandard.ZstdDecompressor().decompress(compressed, max_output_size=unpacked_size) if len(image) != unpacked_size: raise VromfsFormatError(f"image is {len(image)} bytes, header says {unpacked_size}") return image def _pack_payload(image: bytes, pack_type: PackType) -> bytes: if not pack_type.compressed: return image compressed = zstandard.ZstdCompressor(level=9, write_content_size=True).compress(image) return obfuscation.toggle(compressed)