mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:28:35 +00:00
The case needs 16 or more bytes of alignment padding after the first two streams, and its fixed split points (0x150, 0x420) only give that with stock zlib's output lengths. Python 3.14 on Windows ships zlib-ng, which compresses the parts to other lengths: 3 bytes of padding, so the test failed its own precondition and never checked the join. Search for the first split points from 0x150/0x420 that give the padding under the zlib in use, keeping the CMapData root in the third part. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
"""world_index.scan_rpf joining a resource stored as three zlib streams (synthetic bytes only).
|
|
|
|
python -I tests/test_world_index_three_streams.py
|
|
"""
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zlib
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from test_world_index import WI, make_resource, make_rpf_streams # noqa: E402
|
|
|
|
|
|
def stream_ends(parts, start=0x40):
|
|
"""(start, end) of each compressed part as make_rpf_streams lays them out."""
|
|
out, pos = [], start
|
|
for part in parts:
|
|
pos = (pos + 15) & ~15
|
|
out.append((pos, pos + len(zlib.compress(part))))
|
|
pos = out[-1][1]
|
|
return out
|
|
|
|
|
|
def padding(spans):
|
|
"""Alignment bytes between the first two streams and between the second and third."""
|
|
return sum(spans[i + 1][0] - spans[i][1] for i in range(2))
|
|
|
|
|
|
def padded_split(blob):
|
|
"""Three parts whose two alignment gaps add up to 16 bytes or more, and their spans.
|
|
|
|
How much padding a split leaves depends on the compressor's output lengths: zlib-ng (Python 3.14 on
|
|
Windows) compresses these parts to other lengths than zlib, so the split points are searched for
|
|
instead of fixed. The search starts at 0x150/0x420, which give 2 + 15 bytes with zlib, and the second
|
|
point stays at or below 0x500 so the CMapData root (at 0x500) is in the third part.
|
|
"""
|
|
for first in range(0x150, 0x200):
|
|
for second in range(0x420, 0x501):
|
|
parts = [blob[:first], blob[first:second], blob[second:]]
|
|
spans = stream_ends(parts)
|
|
if padding(spans) >= 16:
|
|
return parts, spans
|
|
return None, None
|
|
|
|
|
|
class ThreeStreamJoinTest(unittest.TestCase):
|
|
def test_third_stream_follows_the_second_streams_real_end(self):
|
|
blob = make_resource()
|
|
# With 16 or more bytes of padding after the first two streams, "first stream start + both streams'
|
|
# lengths", rounded up to 16, lands inside the second stream instead of at the third.
|
|
parts, spans = padded_split(blob)
|
|
self.assertIsNotNone(parts, "no split with 16 bytes of padding under this zlib")
|
|
archive = make_rpf_streams(parts)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = os.path.join(tmp, "three.rpf")
|
|
with open(path, "wb") as f:
|
|
f.write(archive)
|
|
rows, stats = WI.scan_rpf(path)
|
|
self.assertEqual(stats["streams"], 3)
|
|
self.assertEqual(len(rows), 1)
|
|
self.assertEqual(rows[0]["size"], len(blob))
|
|
self.assertEqual(rows[0]["map_name"], 0x11223344) # the CMapData block lives in the third stream
|
|
self.assertEqual(stats["scanned_bytes"], sum(e - s for s, e in spans))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|