Files
WT-Offline/tools/src/wtotools/vromfs/image.py
T
N0VAN0M4LY 8d9cca9d05 fix(vromfs): use 64-bit digest table offsets
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.
2026-11-25 11:00:48 +08:00

126 lines
5.3 KiB
Python

"""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 special name-map file "nm" (stored as b"\\xff?nm") last.
"""
from __future__ import annotations
import hashlib
import struct
from dataclasses import dataclass
from .errors import VromfsFormatError
NAME_MAP_PATH = "nm"
_NAME_MAP_STORED = b"\xff?nm"
_ALIGN = 16
_PAIR = struct.Struct("<II")
_DIGEST_HEADER = struct.Struct("<QQ")
_DIGEST_SIZE = 20
@dataclass(frozen=True)
class ImageLayout:
"""Optional features of the file table, kept so a rewritten image matches the original."""
has_digest_header: bool
has_digests: bool
def parse_image(image: bytes) -> tuple[ImageLayout, dict[str, bytes]]:
names_offset, count = _PAIR.unpack_from(image, 0x00)
data_offset, data_count = _PAIR.unpack_from(image, 0x10)
if count != data_count:
raise VromfsFormatError(f"name count {count} differs from data count {data_count}")
has_digest_header = names_offset == 0x30
digests_begin = _DIGEST_HEADER.unpack_from(image, 0x20)[1] if has_digest_header else 0
paths = [_read_name(image, offset) for offset in struct.unpack_from(f"<{count}Q", image, names_offset)]
files: dict[str, bytes] = {}
for index, path in enumerate(paths):
offset, size = _PAIR.unpack_from(image, data_offset + index * _ALIGN)
content = image[offset:offset + size]
if digests_begin:
expected = image[digests_begin + index * _DIGEST_SIZE:digests_begin + (index + 1) * _DIGEST_SIZE]
if hashlib.sha1(content).digest() != expected:
raise VromfsFormatError(f"SHA-1 mismatch for {path}")
files[path] = content
return ImageLayout(has_digest_header, bool(digests_begin)), files
def build_image(layout: ImageLayout, files: dict[str, bytes]) -> bytes:
if layout.has_digests and not layout.has_digest_header:
raise VromfsFormatError("digests require the digest header")
paths = sorted_paths(files)
count = len(paths)
names_offset = 0x30 if layout.has_digest_header else 0x20
names_blob, name_offsets = _build_names(paths, names_offset + _aligned(8 * count))
data_table_offset = names_offset + _aligned(8 * count) + len(names_blob)
digests_offset = data_table_offset + _ALIGN * count
first_data_offset = digests_offset + (_aligned(_DIGEST_SIZE * count) if layout.has_digests else 0)
data_blob, data_entries = _build_data(paths, files, first_data_offset)
out = bytearray(first_data_offset)
_PAIR.pack_into(out, 0x00, names_offset, count)
_PAIR.pack_into(out, 0x10, data_table_offset, count)
if layout.has_digest_header:
begin, end = (digests_offset, digests_offset + _DIGEST_SIZE * count) if layout.has_digests else (0, first_data_offset)
_DIGEST_HEADER.pack_into(out, 0x20, end, begin)
struct.pack_into(f"<{count}Q", out, names_offset, *name_offsets)
out[names_offset + _aligned(8 * count):data_table_offset] = names_blob
for index, (offset, size) in enumerate(data_entries):
_PAIR.pack_into(out, data_table_offset + index * _ALIGN, offset, size)
if layout.has_digests:
for index, path in enumerate(paths):
out[digests_offset + index * _DIGEST_SIZE:digests_offset + (index + 1) * _DIGEST_SIZE] = hashlib.sha1(files[path]).digest()
return bytes(out) + data_blob
def sorted_paths(files: dict[str, bytes]) -> list[str]:
"""File order the game expects: byte-wise path order, name map last."""
paths = sorted((path for path in files if path != NAME_MAP_PATH), key=lambda path: path.encode("utf-8"))
if NAME_MAP_PATH in files:
paths.append(NAME_MAP_PATH)
return paths
def _read_name(image: bytes, offset: int) -> str:
raw = image[offset:image.index(b"\0", offset)]
return NAME_MAP_PATH if raw == _NAME_MAP_STORED else raw.decode("utf-8")
def _build_names(paths: list[str], base: int) -> tuple[bytes, list[int]]:
blob = bytearray()
offsets = []
for path in paths:
offsets.append(base + len(blob))
blob += (_NAME_MAP_STORED if path == NAME_MAP_PATH else path.encode("utf-8")) + b"\0"
blob += bytes(_aligned(len(blob)) - len(blob))
return bytes(blob), offsets
def _build_data(paths: list[str], files: dict[str, bytes], base: int) -> tuple[bytes, list[tuple[int, int]]]:
blob = bytearray()
entries = []
for path in paths:
content = files[path]
entries.append((base + len(blob), len(content)))
blob += content + bytes(_aligned(len(content)) - len(content))
return bytes(blob), entries
def _aligned(size: int) -> int:
return (size + _ALIGN - 1) & ~(_ALIGN - 1)