Read and write the digest table start as a 64-bit field in both codecs. The truncated offset caused SHA-1 mismatches in large packs such as ui/images.vromfs.bin and ui/atlases.vromfs.bin. Type checking passed and the service extracted icons and flags from the large packs.
124 lines
5.4 KiB
TypeScript
124 lines
5.4 KiB
TypeScript
/**
|
|
* Inner image of a .vromfs.bin: the file table and file data.
|
|
*
|
|
* Layout (every block aligned to 16 bytes):
|
|
* 0x00 names header: u32 offset of the name-offset table, u32 file count
|
|
* 0x10 data header: u32 offset of the data table, u32 file count
|
|
* 0x20 digest header: u64 end, u64 begin (only when names offset == 0x30; begin
|
|
* exceeds 16 bits in large packs such as ui/images.vromfs.bin)
|
|
* name offsets: count x u64, each pointing at a NUL-terminated path
|
|
* names
|
|
* data table: count x 16 bytes: u32 offset, u32 size, 8 bytes padding
|
|
* digests: count x 20-byte SHA-1 (only when digest begin != 0)
|
|
* file data
|
|
*
|
|
* The game binary-searches the full path bytes, so entries are sorted by their UTF-8 bytes,
|
|
* with the shared name map "nm" (stored as "\xff?nm") last.
|
|
*/
|
|
import { createHash } from 'node:crypto';
|
|
import { VromfsFormatError } from './errors.ts';
|
|
|
|
export const NAME_MAP_PATH = 'nm';
|
|
const NAME_MAP_STORED = Buffer.from([0xff, 0x3f, 0x6e, 0x6d]);
|
|
const ALIGN = 16;
|
|
const DIGEST_SIZE = 20;
|
|
|
|
export interface ImageLayout {
|
|
readonly hasDigestHeader: boolean;
|
|
readonly hasDigests: boolean;
|
|
}
|
|
|
|
export function parseImage(image: Uint8Array): { layout: ImageLayout; files: Map<string, Uint8Array> } {
|
|
const view = new DataView(image.buffer, image.byteOffset, image.byteLength);
|
|
const namesOffset = view.getUint32(0x00, true);
|
|
const count = view.getUint32(0x04, true);
|
|
const dataOffset = view.getUint32(0x10, true);
|
|
if (view.getUint32(0x14, true) !== count) throw new VromfsFormatError('name count differs from data count');
|
|
const hasDigestHeader = namesOffset === 0x30;
|
|
const digestsBegin = hasDigestHeader ? Number(view.getBigUint64(0x28, true)) : 0;
|
|
|
|
const files = new Map<string, Uint8Array>();
|
|
for (let index = 0; index < count; index++) {
|
|
const path = readName(image, Number(view.getBigUint64(namesOffset + index * 8, true)));
|
|
const offset = view.getUint32(dataOffset + index * ALIGN, true);
|
|
const size = view.getUint32(dataOffset + index * ALIGN + 4, true);
|
|
const content = image.slice(offset, offset + size);
|
|
if (digestsBegin) {
|
|
const expected = image.subarray(digestsBegin + index * DIGEST_SIZE, digestsBegin + (index + 1) * DIGEST_SIZE);
|
|
if (!sha1(content).equals(expected)) throw new VromfsFormatError(`SHA-1 mismatch for ${path}`);
|
|
}
|
|
files.set(path, content);
|
|
}
|
|
return { layout: { hasDigestHeader, hasDigests: digestsBegin !== 0 }, files };
|
|
}
|
|
|
|
export function buildImage(layout: ImageLayout, files: ReadonlyMap<string, Uint8Array>): Uint8Array {
|
|
if (layout.hasDigests && !layout.hasDigestHeader) throw new VromfsFormatError('digests require the digest header');
|
|
const paths = sortedPaths(files);
|
|
const count = paths.length;
|
|
const namesOffset = layout.hasDigestHeader ? 0x30 : 0x20;
|
|
const namesBlobOffset = namesOffset + aligned(8 * count);
|
|
|
|
const encodedNames = paths.map((path) => (path === NAME_MAP_PATH ? NAME_MAP_STORED : Buffer.from(path, 'utf-8')));
|
|
const nameOffsets: number[] = [];
|
|
let namesSize = 0;
|
|
for (const name of encodedNames) {
|
|
nameOffsets.push(namesBlobOffset + namesSize);
|
|
namesSize += name.length + 1;
|
|
}
|
|
const dataTableOffset = namesBlobOffset + aligned(namesSize);
|
|
const digestsOffset = dataTableOffset + ALIGN * count;
|
|
const firstDataOffset = digestsOffset + (layout.hasDigests ? aligned(DIGEST_SIZE * count) : 0);
|
|
|
|
const dataOffsets: number[] = [];
|
|
let dataSize = 0;
|
|
for (const path of paths) {
|
|
dataOffsets.push(firstDataOffset + dataSize);
|
|
dataSize += aligned(files.get(path)!.length);
|
|
}
|
|
|
|
const out = Buffer.alloc(firstDataOffset + dataSize);
|
|
out.writeUInt32LE(namesOffset, 0x00);
|
|
out.writeUInt32LE(count, 0x04);
|
|
out.writeUInt32LE(dataTableOffset, 0x10);
|
|
out.writeUInt32LE(count, 0x14);
|
|
if (layout.hasDigestHeader) {
|
|
const [begin, end] = layout.hasDigests ? [digestsOffset, digestsOffset + DIGEST_SIZE * count] : [0, firstDataOffset];
|
|
out.writeBigUInt64LE(BigInt(end), 0x20);
|
|
out.writeBigUInt64LE(BigInt(begin), 0x28);
|
|
}
|
|
paths.forEach((path, index) => {
|
|
out.writeBigUInt64LE(BigInt(nameOffsets[index]!), namesOffset + index * 8);
|
|
out.set(encodedNames[index]!, nameOffsets[index]!);
|
|
const content = files.get(path)!;
|
|
out.writeUInt32LE(dataOffsets[index]!, dataTableOffset + index * ALIGN);
|
|
out.writeUInt32LE(content.length, dataTableOffset + index * ALIGN + 4);
|
|
if (layout.hasDigests) out.set(sha1(content), digestsOffset + index * DIGEST_SIZE);
|
|
out.set(content, dataOffsets[index]!);
|
|
});
|
|
return out;
|
|
}
|
|
|
|
/** File order the game expects: byte-wise path order, name map last. */
|
|
export function sortedPaths(files: ReadonlyMap<string, Uint8Array>): string[] {
|
|
const paths = [...files.keys()].filter((path) => path !== NAME_MAP_PATH)
|
|
.sort((left, right) => Buffer.compare(Buffer.from(left, 'utf-8'), Buffer.from(right, 'utf-8')));
|
|
if (files.has(NAME_MAP_PATH)) paths.push(NAME_MAP_PATH);
|
|
return paths;
|
|
}
|
|
|
|
function readName(image: Uint8Array, offset: number): string {
|
|
const end = image.indexOf(0, offset);
|
|
if (end < 0) throw new VromfsFormatError(`file name at ${offset} is not terminated`);
|
|
const raw = Buffer.from(image.subarray(offset, end));
|
|
return raw.equals(NAME_MAP_STORED) ? NAME_MAP_PATH : raw.toString('utf-8');
|
|
}
|
|
|
|
function aligned(size: number): number {
|
|
return Math.ceil(size / ALIGN) * ALIGN;
|
|
}
|
|
|
|
function sha1(data: Uint8Array): Buffer {
|
|
return createHash('sha1').update(data).digest();
|
|
}
|