From 63f23a0da87baf57cc004befbc4c8bbe28c22bb8 Mon Sep 17 00:00:00 2001 From: Rafael Date: Sun, 22 Nov 2026 02:26:42 -0300 Subject: [PATCH] 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 (cherry picked from commit 518343b7850361a5d8046bd456e80e261a9f1e1d) --- tests/test_limit_window.py | 76 ++++++++++++++++++++++++++++++++++++++ tests/test_rpf9_carve.py | 35 ++++++++++++++++++ tools/anim_index.py | 6 +-- tools/bvh_index.py | 8 ++-- tools/rbf0_index.py | 6 +-- tools/restofworld_index.py | 6 +-- tools/rpf9_carve.py | 10 +++-- 7 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 tests/test_limit_window.py diff --git a/tests/test_limit_window.py b/tests/test_limit_window.py new file mode 100644 index 000000000..3b6944a35 --- /dev/null +++ b/tests/test_limit_window.py @@ -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(" 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(" 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))