feat(tools): walk SGD5 effect boundaries from stored lengths

Follow the post-pass tables through the counted string pool and next record. Check both database terminators and keep opaque payload contents out of structural matching.

Refs: #2519, #3036
This commit is contained in:
Jeremy Goldberg
2026-11-22 14:58:19 -05:00
parent 0029eba71b
commit c0439966f5
2 changed files with 131 additions and 65 deletions

View File

@ -37,7 +37,12 @@ def program(name):
return label(name) + b"\x02" + u32(stored) + packed + bytes(152) + u32(2) + b"\xaa\xbb"
def effect(name, ordinals=None, techniques=(), pool_tail=b""):
def post_pass(words=(), blocks=(b"", b""), packed=0, entries=b""):
data = u32(len(words)) + b"".join(u32(word) for word in words) + u32(0x80000000)
return data + b"".join(u32(len(block)) + block for block in blocks) + u32(packed) + entries
def effect(name, ordinals=None, techniques=(), pool_tail=b"", suffix=None):
data = bytearray(label(name) + u32(0xDEADBEEF))
for group in range(6):
entries = (ordinals or {}).get(group, ())
@ -57,7 +62,7 @@ def effect(name, ordinals=None, techniques=(), pool_tail=b""):
for row in rows:
data += bytes(row) + bytes(18)
pool += pool_tail
return data + u32(len(pool)) + pool
return data + (post_pass() if suffix is None else suffix) + u32(len(pool)) + pool
def database(groups, effects, base=0):
@ -169,13 +174,51 @@ class EffectsTest(unittest.TestCase):
for reason, item in [
("ordinal outside group", effect("Mock", {0: (1,)})),
("selector outside effect list", effect("Mock", {0: (0,)}, [("Hue", [(2, 0, 0, 0, 0, 0)])])),
("two valid pool boundaries", effect("Mock", pool_tail=u32(32) + b"\0" + b"X" * 30 + b"\0")),
("invalid pool text", effect("Mock", pool_tail=b"\xff\0")),
]:
with self.subTest(reason=reason):
data, layout = database({0: ("Glow",)}, [item])
with self.assertRaises(ValueError):
sgd5_effects.read_database(io.BytesIO(data), layout, "Absent", "Absent")
def test_counted_suffix_skips_header_and_pool_lookalikes(self):
decoy = label("Other") + u32(0) + u32(0) * 6
tags = u32(0x80000000) + b"ABCD" + u32(24) + bytes(24)
suffix = post_pass((0, 17), (decoy, b"test"), 0x14000002, tags)
inner_pool = u32(32) + b"\0" + b"X" * 30 + b"\0"
first = effect("First", pool_tail=inner_pool, suffix=suffix)
second = effect("Next")
data, layout = database({}, [first, second])
matches = sgd5_effects.read_database(io.BytesIO(data), layout, list_effects=True)["matches"]
self.assertEqual([(m["effect"], m["effect_offset"]) for m in matches],
[("First", data.index(first)), ("Next", data.index(second))])
def test_counted_suffix_refuses_invalid_framing(self):
invalid = {
"oversize word array": u32(0xffffffff),
"marker": u32(0) + u32(7),
"first block length": u32(0) + u32(0x80000000) + u32(0xffffffff),
"second block length": u32(0) + u32(0x80000000) + u32(0) + u32(0xffffffff),
"packed count": post_pass(packed=0x00000100),
"missing tag payload": post_pass(packed=1, entries=u32(0x80000000)),
"oversize variable payload": post_pass(packed=1, entries=u32(0xffffffff)),
"oversize pool": post_pass() + u32(0xffffffff),
}
for reason, suffix in invalid.items():
with self.subTest(reason=reason), self.assertRaises(ValueError):
data, layout = database({}, [effect("First", suffix=suffix)])
sgd5_effects.read_database(io.BytesIO(data), layout)
def test_declared_effect_count_must_consume_region(self):
item = effect("First")
original, layout = database({}, [item])
offset = original.index(item) - 4
for count in (0, 2):
with self.subTest(count=count), self.assertRaises(ValueError):
data = bytearray(original)
data[offset:offset + 4] = u32(count)
sgd5_effects.read_database(io.BytesIO(data), layout)
def test_bad_framing_and_truncation(self):
data, layout = database({0: ("Glow",)}, [effect("Vacant")])
record = data.index(label("Glow"))
@ -184,6 +227,7 @@ class EffectsTest(unittest.TestCase):
("magic", 0, b"MOCK"),
("stored size", record + len(label("Glow")) + 1, u32(0xffffffff)),
("end marker", end, u32(2)),
("extra marker in padding", end + 4, u32(1)),
]:
with self.subTest(reason=reason):
damaged = bytearray(data)

View File

@ -21,6 +21,7 @@ from pathlib import Path
import re
import struct
import sys
from typing import NamedTuple
sys.path.insert(0, str(Path(__file__).resolve().parent))
from hashdict import joaat
@ -104,70 +105,96 @@ def program_lists(data, pos, groups):
return lists, pos
def effect_headers(data, groups):
"""Find counted native identifiers and validate all six arrays of database-local ordinals."""
for match in IDENTIFIER.finditer(data):
end = match.end()
# If the prefix itself looks like an identifier character, it is part
# of the regex match. The length byte decides where the label starts.
for start in range(max(0, match.start() - 1), end - 2):
if data[start] != end - start - 1 or not IDENTIFIER.fullmatch(data, start + 1, end):
continue
arrays = program_lists(data, end + 4, groups) # the word following the name is opaque
if arrays is not None:
lists, pos = arrays
yield start, data[start + 1:end - 1].decode("ascii"), lists, pos
def advance(data, pos, size):
if pos + size > len(data):
raise ValueError("effect field exceeds the database boundary")
return pos + size
def string_pool(data, pos, end):
# The length excludes its own u32, includes the final NUL, and the pool
# ends at the next effect boundary (or just before the terminal marker).
found = []
for prefix in range(pos, end - 4):
if (u32(data, prefix) == end - prefix - 4 and data[prefix + 4] == data[end - 1] == 0
and POOL.fullmatch(data, prefix + 4, end)):
found.append(prefix)
if len(found) != 1:
raise ValueError(f"expected one string pool at effect boundary {end:#x}, found {len(found)}")
return found[0]
def pool_after_passes(data, pos):
# Counted words, a marker and two length-prefixed byte blocks.
pos = advance(data, pos + 4, 4 * u32(data, pos))
if u32(data, pos) != 0x80000000:
raise ValueError("invalid post-pass marker")
pos += 4
for _ in range(2):
pos = advance(data, pos + 4, u32(data, pos))
# In 1.0.2.1 the observed entry counts fit in the low byte. Bits 8..23
# are zero, and the high byte can be nonzero. Payload meanings are unknown.
packed = u32(data, pos)
if packed & 0x00ffff00:
raise ValueError("unsupported packed post-pass count")
pos += 4
for _ in range(packed & 0xff):
tag = u32(data, pos)
pos = advance(data, pos + 4, 4 if tag == 0x80000000 else tag)
return pos
def techniques(data, lists, pos, boundary):
class Effect(NamedTuple):
start: int
name: str
lists: list
technique_rows: int
first_pass: int
pass_end: int
pool: int
end: int
def effect_records(data, groups, count):
start = 0
for _ in range(count):
if start >= len(data):
raise ValueError("missing effect record")
name_end = advance(data, start + 1, data[start])
if not IDENTIFIER.fullmatch(data, start + 1, name_end):
raise ValueError("invalid counted effect name")
name = data[start + 1:name_end - 1].decode("ascii")
arrays = program_lists(data, name_end + 4, groups) # word after the name is opaque
if arrays is None:
raise ValueError("invalid effect program lists")
lists, pos = arrays
pos = advance(data, pos, 80)
for width in (16, 24, 68, 36):
pos = advance(data, pos + 4, width * u32(data, pos))
rows = pos + 4
pass_count_at = advance(data, rows, 4 * u32(data, pos))
first = pass_count_at + 4
pass_end = advance(data, first, 24 * u32(data, pass_count_at))
pool = pool_after_passes(data, pass_end)
end = advance(data, pool + 4, u32(data, pool))
if (end == pool + 4 or data[pool + 4] != 0 or data[end - 1] != 0
or not POOL.fullmatch(data, pool + 4, end)):
raise ValueError("invalid counted string pool")
yield Effect(start, name, lists, rows, first, pass_end, pool, end)
start = end
if not 4 <= len(data) - start <= 4096 or u32(data, start) != 1 or any(data[start + 4:]):
raise ValueError("declared effects do not reach the database terminator and zero padding")
def techniques(data, effect):
"""Yield the stored name and passes, including zero-program passes."""
pool = string_pool(data, pos, boundary)
names = {m.start() - pool - 4: m.group()[:-1].decode("ascii")
for m in STRING.finditer(data, pool + 4, boundary)}
pos += 80
for width in (16, 24, 68, 36):
if pos + 4 > pool:
raise ValueError("state table crosses the string pool")
pos += 4 + width * u32(data, pos)
if pos + 4 > pool:
raise ValueError("missing technique count")
count = u32(data, pos)
pass_count_at = pos + 4 + 4 * count
if pass_count_at + 4 > pool:
raise ValueError("technique rows cross the string pool")
cursor = pass_count_at + 4
pass_end = cursor + 24 * u32(data, pass_count_at)
if pass_end > pool:
raise ValueError("pass rows cross the string pool")
for name_bits, _, length in struct.iter_unpack("<HBB", data[pos + 4:pass_count_at]):
names = {m.start() - effect.pool - 4: m.group()[:-1].decode("ascii")
for m in STRING.finditer(data, effect.pool + 4, effect.end)}
cursor = effect.first_pass
rows = memoryview(data)[effect.technique_rows:cursor - 4]
for name_bits, _, length in struct.iter_unpack("<HBB", rows):
name = names.get(name_bits & 0x7fff)
if name is None or cursor + 24 * length > pass_end:
if name is None or cursor + 24 * length > effect.pass_end:
raise ValueError("invalid technique name offset or pass count")
passes = []
for _ in range(length):
programs = []
for group, selector in enumerate(data[cursor:cursor + 6]):
if selector > len(lists[group]):
if selector > len(effect.lists[group]):
raise ValueError("pass selector is outside the effect's program list")
if selector:
programs.append((group, lists[group][selector - 1]))
programs.append((group, effect.lists[group][selector - 1]))
passes.append(programs)
cursor += 24
yield name, passes
if cursor != pass_end:
if cursor != effect.pass_end:
raise ValueError("technique pass counts do not sum to the table count")
@ -182,24 +209,19 @@ def read_database(stream, layout, effect=None, technique=None, *, list_effects=F
base = stream.tell()
if base >= end:
raise ValueError("missing effect region")
region = stream.read(end - base)
tail = len(region.rstrip(b"\x00")) - 1
if len(region) != end - base or tail < 0 or region[tail] != 1 or not 4 <= len(region) - tail <= 4096:
raise ValueError("truncated effect region or invalid terminal marker")
data = region[:tail]
headers = list(effect_headers(data, groups))
if len(headers) != count or (headers and headers[0][0] != 0) or (not headers and data):
raise ValueError(f"found {len(headers)} effect headers for {count} declared effects")
data = stream.read(end - base)
if len(data) != end - base:
raise ValueError("truncated effect region")
result = {"database": database, "groups": list(map(len, groups)), "effects": count,
"named_effects": 0, "techniques": 0, "passes": 0, "matches": []}
ends = [header[0] for header in headers[1:]] + [len(data)]
for (start, name, lists, pos), boundary in zip(headers, ends):
for record in effect_records(data, groups, count):
name = record.name
key = joaat(name)
selected = name == effect or key == shader_hash
if list_effects:
result["matches"].append({"effect": name, "shader_hash": f"{key:08X}", "effect_offset": base + start})
result["matches"].append({"effect": name, "shader_hash": f"{key:08X}", "effect_offset": base + record.start})
listing = []
for index, (stored_name, passes) in enumerate(techniques(data, lists, pos, boundary)):
for index, (stored_name, passes) in enumerate(techniques(data, record)):
result["named_effects"] += index == 0
result["techniques"] += 1
result["passes"] += len(passes)
@ -210,10 +232,10 @@ def read_database(stream, layout, effect=None, technique=None, *, list_effects=F
elif stored_name == technique:
resolved = [[program_identity(stream, g, ordinal, groups[g][ordinal]) for g, ordinal in row]
for row in passes]
result["matches"].append({"effect": name, "effect_offset": base + start,
result["matches"].append({"effect": name, "effect_offset": base + record.start,
"technique_index": index, "passes": resolved})
if selected and technique is None:
result["matches"].append({"effect": name, "effect_offset": base + start, "techniques": listing})
result["matches"].append({"effect": name, "effect_offset": base + record.start, "techniques": listing})
return result