/** * 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 * 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. * Reading keeps everything except the image, so writing back reproduces the same container. */ import { createHash } from 'node:crypto'; import { constants, zstdCompressSync, zstdDecompressSync } from 'node:zlib'; import { VromfsFormatError } from './errors.ts'; import { toggleObfuscation } from './obfuscation.ts'; export const SIGNATURE_SIZE = 0x100; const HEADER_SIZE = 16; const EXT_HEADER_SIZE = 8; const SIZE_MASK = 0x03ff_ffff; const COMPRESSION_LEVEL = 9; export const PackType = { ZstdNoCheck: 0x10, Plain: 0x20, Zstd: 0x30, } as const; export type PackType = (typeof PackType)[keyof typeof PackType]; export interface ContainerInfo { readonly magic: 'VRFs' | 'VRFx'; readonly platform: Uint8Array; readonly packType: PackType; /** Extended header (VRFx only): flags and the four version bytes exactly as stored. */ readonly extended: { readonly flags: number; readonly version: Uint8Array } | null; readonly signature: Uint8Array; } export function parseContainer(data: Uint8Array): { info: ContainerInfo; image: Uint8Array } { if (data.length < HEADER_SIZE) throw new VromfsFormatError('file too short for a container header'); const view = new DataView(data.buffer, data.byteOffset, data.byteLength); const magic = Buffer.from(data.subarray(0, 4)).toString('latin1'); if (magic !== 'VRFs' && magic !== 'VRFx') throw new VromfsFormatError(`unknown container magic ${JSON.stringify(magic)}`); const platform = data.slice(4, 8); const unpackedSize = view.getUint32(8, true); const packedWord = view.getUint32(12, true); const packType = toPackType(packedWord >>> 26); let cursor = HEADER_SIZE; let extended: ContainerInfo['extended'] = null; if (magic === 'VRFx') { const extSize = view.getUint16(cursor, true); if (extSize !== EXT_HEADER_SIZE) throw new VromfsFormatError(`unexpected extended header size ${extSize}`); extended = { flags: view.getUint16(cursor + 2, true), version: data.slice(cursor + 4, cursor + 8) }; cursor += EXT_HEADER_SIZE; } const compressed = packType !== PackType.Plain; const storedSize = compressed ? packedWord & SIZE_MASK : unpackedSize; const image = unpackPayload(data.subarray(cursor, cursor + storedSize), compressed, unpackedSize); cursor += storedSize; if (packType !== PackType.ZstdNoCheck) { if (!md5(image).equals(data.subarray(cursor, cursor + 16))) throw new VromfsFormatError('image MD5 does not match the container digest'); cursor += 16; } const signature = data.slice(cursor); if (signature.length !== 0 && signature.length !== SIGNATURE_SIZE) { throw new VromfsFormatError(`unexpected trailing data of ${signature.length} bytes`); } return { info: { magic, platform, packType, extended, signature }, image }; } /** Pack an image with the container settings it was read with. The signature tail is kept as read. */ export function buildContainer(info: ContainerInfo, image: Uint8Array): Uint8Array { const compressed = info.packType !== PackType.Plain; const stored = compressed ? toggleObfuscation(zstdCompressSync(image, { params: { [constants.ZSTD_c_compressionLevel]: COMPRESSION_LEVEL } })) : image; if (compressed && stored.length > SIZE_MASK) throw new VromfsFormatError('compressed payload too large for the 26-bit size field'); const header = Buffer.alloc(HEADER_SIZE); header.write(info.magic, 0, 'latin1'); header.set(info.platform, 4); header.writeUInt32LE(image.length, 8); header.writeUInt32LE(((info.packType << 26) | (compressed ? stored.length : 0)) >>> 0, 12); const parts: Uint8Array[] = [header]; if (info.magic === 'VRFx') { const ext = Buffer.alloc(EXT_HEADER_SIZE); ext.writeUInt16LE(EXT_HEADER_SIZE, 0); ext.writeUInt16LE(info.extended?.flags ?? 0, 2); if (info.extended) ext.set(info.extended.version, 4); parts.push(ext); } parts.push(stored); if (info.packType !== PackType.ZstdNoCheck) parts.push(md5(image)); parts.push(info.signature); return Buffer.concat(parts); } function unpackPayload(stored: Uint8Array, compressed: boolean, unpackedSize: number): Uint8Array { if (!compressed) return stored; let image: Uint8Array; try { image = zstdDecompressSync(toggleObfuscation(stored), { maxOutputLength: unpackedSize }); } catch (error) { throw new VromfsFormatError(`payload decompression failed: ${(error as Error).message}`); } if (image.length !== unpackedSize) throw new VromfsFormatError(`image is ${image.length} bytes, header says ${unpackedSize}`); return image; } function toPackType(value: number): PackType { if (value === PackType.ZstdNoCheck || value === PackType.Plain || value === PackType.Zstd) return value; throw new VromfsFormatError(`unknown pack type ${value.toString(16)}`); } function md5(data: Uint8Array): Buffer { return createHash('md5').update(data).digest(); }