mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:18:37 +00:00
rpf9_carve and four indexers: under --limit, leave out a stream that runs past the window
rpf9_carve.py, anim_index.py, rbf0_index.py, restofworld_index.py and bvh_index.py inflated up to the end of the archive even with --limit, so a stream that started inside the window and ended after it was counted at its full stored size as readable while the gap before it counted as unread: readable + zero + unread exceeded the span. On a 1.0.2.1 copy's common.rpf with --limit 0x2000 all five reported 114.2% (one stream for 2,568 bytes that end past the window). They now inflate only inside the window, as story_index (#712) and navmesh_index (#766) do: a stream that doesn't end inside it is left out, and a Bink video must fit in it. Without --limit the window is the whole archive, so nothing changes: common.rpf still gives 954 streams and 100% in all five. bvh_index passed the window's end already but its read slices ignored it; they are now cut at that end too. Tests: test_rpf9_carve's LimitWindowTest and the new test_limit_window.py (anim/rbf0/restofworld/ bvh_index) put the limit inside the first of two incompressible streams; all five fail on main and pass now (108 tests across the touched files). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 518343b7850361a5d8046bd456e80e261a9f1e1d)
This commit is contained in:
76
tests/test_limit_window.py
Normal file
76
tests/test_limit_window.py
Normal file
@ -0,0 +1,76 @@
|
||||
"""--limit ending inside a stream: the index tools leave that stream out instead of counting its whole stored size.
|
||||
|
||||
story_index (#712) and navmesh_index (#766) already inflate only inside the scan window; anim_index, rbf0_index,
|
||||
restofworld_index and bvh_index inflated up to the end of the archive, so a stream that started in the window but
|
||||
ended after it was counted as readable and readable + zero + unread exceeded the span. rpf9_carve.py has its own
|
||||
test in test_rpf9_carve.py.
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import zlib
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
||||
|
||||
import anim_index # noqa: E402
|
||||
import bvh_index # noqa: E402
|
||||
import rbf0_index # noqa: E402
|
||||
import restofworld_index # noqa: E402
|
||||
|
||||
GAP = 0x100
|
||||
|
||||
|
||||
def noisy(n, seed):
|
||||
"""Bytes zlib can't shrink, so a stream's stored size is about n."""
|
||||
out = bytearray()
|
||||
while len(out) < n:
|
||||
out += hashlib.sha256(bytes([seed]) + len(out).to_bytes(4, "little")).digest()
|
||||
return bytes(out[:n])
|
||||
|
||||
|
||||
def summary_of(result):
|
||||
"""The tools return (rows, summary), (rows, records, summary) or (rows, [summary])."""
|
||||
last = result[-1]
|
||||
return last[0] if isinstance(last, list) else last
|
||||
|
||||
|
||||
def first_of(summary, *keys):
|
||||
for key in keys:
|
||||
if key in summary:
|
||||
return summary[key]
|
||||
raise KeyError(keys)
|
||||
|
||||
|
||||
class LimitWindowTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.first = zlib.compress(noisy(4000, 1))
|
||||
data = bytearray(0x40 + GAP) + self.first + zlib.compress(noisy(4000, 2))
|
||||
data[0:4] = b"9FPR"
|
||||
struct.pack_into("<I", data, 4, 0x40)
|
||||
self.path = os.path.join(self.tmp.name, "window.rpf")
|
||||
with open(self.path, "wb") as f:
|
||||
f.write(data)
|
||||
self.limit = GAP + len(self.first) // 2 # ends inside the first stream
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_a_stream_crossing_the_limit_is_left_out(self):
|
||||
for tool in (anim_index, rbf0_index, restofworld_index, bvh_index):
|
||||
with self.subTest(tool=tool.__name__):
|
||||
summary = summary_of(tool.scan_rpf(self.path, limit=self.limit))
|
||||
stored = first_of(summary, "compressed_bytes", "stored")
|
||||
unread = first_of(summary, "unread_bytes", "unread")
|
||||
self.assertEqual(summary["span"], self.limit)
|
||||
self.assertEqual(stored, 0)
|
||||
self.assertEqual(stored + summary["zero_padding"] + unread, summary["span"])
|
||||
self.assertLessEqual(summary["readable_fraction"], 1.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@ -144,5 +144,40 @@ class CarveEdgeTest(unittest.TestCase):
|
||||
self.assertEqual(json.load(f)["summary"]["span"], 0x40)
|
||||
|
||||
|
||||
|
||||
def noisy(n, seed):
|
||||
"""Bytes zlib can't shrink, so a stream's stored size is about n."""
|
||||
import hashlib
|
||||
out = bytearray()
|
||||
while len(out) < n:
|
||||
out += hashlib.sha256(bytes([seed]) + len(out).to_bytes(4, "little")).digest()
|
||||
return bytes(out[:n])
|
||||
|
||||
|
||||
class LimitWindowTest(unittest.TestCase):
|
||||
def test_a_stream_crossing_the_limit_is_left_out(self):
|
||||
"""--limit ending inside a stream: the stream doesn't decode inside the window, so it isn't counted as
|
||||
readable, and readable + zero + unread add up to the span (as story_index does since #712)."""
|
||||
first = zlib.compress(noisy(4000, 1))
|
||||
second = zlib.compress(noisy(4000, 2))
|
||||
data = bytearray(0x40 + 0x100) + first + second
|
||||
data[0:4] = b"9FPR"
|
||||
struct.pack_into("<I", data, 4, 0x40)
|
||||
limit = 0x100 + len(first) // 2
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
arc = os.path.join(tmp, "x.rpf")
|
||||
out = os.path.join(tmp, "out")
|
||||
with open(arc, "wb") as f:
|
||||
f.write(data)
|
||||
p = subprocess.run([sys.executable, "-I", TOOL, arc, out, "--index-only", "--limit", str(limit)],
|
||||
capture_output=True, text=True)
|
||||
self.assertEqual(p.returncode, 0, p.stderr)
|
||||
with open(os.path.join(out, "index.json"), encoding="utf-8") as f:
|
||||
summary = json.load(f)["summary"]
|
||||
self.assertEqual(summary["span"], limit)
|
||||
self.assertEqual(summary["files"], 0)
|
||||
self.assertEqual(summary["compressed_bytes"] + summary["zero_padding"] + summary["unread_bytes"],
|
||||
summary["span"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -350,7 +350,7 @@ def scan_rpf(path, start=None, limit=None, progress=None):
|
||||
if m[s:s + 3] == b"KB2":
|
||||
blen = (_u32(m, s + 4) or 0) + 8
|
||||
w, h = _u32(m, s + 0x14), _u32(m, s + 0x18)
|
||||
if not (64 <= blen <= size - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
if not (64 <= blen <= end - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
pos = s + 1
|
||||
continue
|
||||
z, u = _gap(m[last:s])
|
||||
@ -365,8 +365,8 @@ def scan_rpf(path, start=None, limit=None, progress=None):
|
||||
d = zlib.decompressobj()
|
||||
chunks, p = [], s
|
||||
try:
|
||||
while not d.eof and p < size:
|
||||
block = m[p:p + (1 << 20)]
|
||||
while not d.eof and p < end: # inside the --limit window, as in story_index (#712)
|
||||
block = m[p:min(p + (1 << 20), end)]
|
||||
chunks.append(d.decompress(block))
|
||||
p += len(block)
|
||||
if d.unused_data or d.eof:
|
||||
|
||||
@ -195,7 +195,7 @@ def inflate_stream(m, s, size, max_inflate):
|
||||
|
||||
try:
|
||||
while not d.eof and p < size:
|
||||
chunk = m[p:p + (1 << 20)]
|
||||
chunk = m[p:min(p + (1 << 20), size)] # size is the scan window's end
|
||||
if not chunk:
|
||||
break
|
||||
take(d.decompress(chunk))
|
||||
@ -218,7 +218,7 @@ def _count_stream(m, s, size):
|
||||
d = zlib.decompressobj()
|
||||
total, p = 0, s
|
||||
while not d.eof and p < size:
|
||||
chunk = m[p:p + (1 << 20)]
|
||||
chunk = m[p:min(p + (1 << 20), size)]
|
||||
if not chunk:
|
||||
break
|
||||
total += len(d.decompress(chunk))
|
||||
@ -254,7 +254,9 @@ def scan_rpf(path, start=None, limit=None, max_inflate=1 << 28):
|
||||
if not mt:
|
||||
break
|
||||
s = mt.start()
|
||||
got = inflate_stream(m, s, size, max_inflate)
|
||||
# Bounded by the scan window (the archive without --limit): a stream that runs past it is left out,
|
||||
# so readable + zero + unread add up to the span, as in story_index (#712).
|
||||
got = inflate_stream(m, s, end, max_inflate)
|
||||
if got is None:
|
||||
pos = s + 1
|
||||
continue
|
||||
|
||||
@ -383,7 +383,7 @@ def scan_rpf(path, start=None, limit=None, progress=None, string_limit=16):
|
||||
if mm[s:s + 3] == b"KB2":
|
||||
blen = (_u32(mm, s + 4) or 0) + 8
|
||||
w, h = _u32(mm, s + 0x14), _u32(mm, s + 0x18)
|
||||
if not (64 <= blen <= size - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
if not (64 <= blen <= end - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
pos = s + 1
|
||||
continue
|
||||
gap = mm[last:s]
|
||||
@ -396,8 +396,8 @@ def scan_rpf(path, start=None, limit=None, progress=None, string_limit=16):
|
||||
d = zlib.decompressobj()
|
||||
chunks, p = [], s
|
||||
try:
|
||||
while not d.eof and p < size:
|
||||
block = mm[p:p + (1 << 20)]
|
||||
while not d.eof and p < end: # inside the --limit window, as in story_index (#712)
|
||||
block = mm[p:min(p + (1 << 20), end)]
|
||||
chunks.append(d.decompress(block))
|
||||
p += len(block)
|
||||
if d.unused_data or d.eof:
|
||||
|
||||
@ -211,7 +211,7 @@ def scan_rpf(path, start=None, limit=None, max_inflate=MAX_INFLATE):
|
||||
if m[s:s + 3] == b"KB2":
|
||||
blen = (_u32(m, s + 4) or 0) + 8
|
||||
w, h = _u32(m, s + 0x14), _u32(m, s + 0x18)
|
||||
if not (64 <= blen <= size - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
if not (64 <= blen <= end - s and w and h and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
pos = s + 1
|
||||
continue
|
||||
z, u = _gap(m[last:s])
|
||||
@ -224,8 +224,8 @@ def scan_rpf(path, start=None, limit=None, max_inflate=MAX_INFLATE):
|
||||
p, total, head = s, 0, b""
|
||||
too_big = False
|
||||
try:
|
||||
while not d.eof and p < size:
|
||||
block = m[p:p + (1 << 20)]
|
||||
while not d.eof and p < end: # inside the --limit window, as in story_index (#712)
|
||||
block = m[p:min(p + (1 << 20), end)]
|
||||
if not block:
|
||||
break
|
||||
p += len(block)
|
||||
|
||||
@ -94,12 +94,12 @@ def main() -> int:
|
||||
break
|
||||
s = mt.start()
|
||||
if m[s:s + 3] == b"KB2":
|
||||
if s + 0x1C > size: # header cut off by the end of the file (a truncated copy): not a video
|
||||
if s + 0x1C > end: # header cut off by the end of the file or of --limit's window: not a video
|
||||
pos = s + 1
|
||||
continue
|
||||
blen = struct.unpack_from("<I", m, s + 4)[0] + 8
|
||||
w, h = struct.unpack_from("<II", m, s + 0x14)
|
||||
if not (64 <= blen <= size - s and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
if not (64 <= blen <= end - s and 16 <= w <= 8192 and 16 <= h <= 8192):
|
||||
pos = s + 1
|
||||
continue
|
||||
gap = m[last:s]
|
||||
@ -117,8 +117,10 @@ def main() -> int:
|
||||
d = zlib.decompressobj()
|
||||
try:
|
||||
chunks, p = [], s
|
||||
while not d.eof and p < size:
|
||||
chunk = m[p:p + (1 << 20)]
|
||||
# Inflate only inside the scan window (all of the file without --limit): a stream that runs past
|
||||
# its end is left out, so readable + zero + unread add up to the span, as in story_index (#712).
|
||||
while not d.eof and p < end:
|
||||
chunk = m[p:min(p + (1 << 20), end)]
|
||||
if not chunk:
|
||||
break
|
||||
chunks.append(d.decompress(chunk))
|
||||
|
||||
Reference in New Issue
Block a user