mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 21:15:57 +00:00
Skip stored padding in raw NCZBLOCK blocks
This commit is contained in:
1 parent
afae80c908
commit
4ef0e2bd4a
2 files changed
+35
-1
No files matched your search
@@ -88,6 +88,25 @@ class UnpackTest(unittest.TestCase):
|
||||
def expected(self):
|
||||
return pfs0([(self.name + '.nca', self.nca), ('a.tik', self.tik)])
|
||||
|
||||
def padded_nsz(self, indices, truncate=False):
|
||||
"""Store selected blocks raw, followed by padding included in the stored length, not the NCA body."""
|
||||
def pad(data):
|
||||
head = ncz.HEADER + 16 + 0x40 * len(SECTIONS)
|
||||
exponent = data[head + 11]
|
||||
count = struct.unpack_from('<I', data, head + 12)[0]
|
||||
for index in indices:
|
||||
index %= count
|
||||
lengths = struct.unpack_from(f'<{count}I', data, head + 24)
|
||||
start = head + 24 + 4 * count + sum(lengths[:index])
|
||||
plain = self.body[index << exponent:(index + 1) << exponent]
|
||||
padding = bytes(max(16, (1 << exponent) - len(plain)))
|
||||
stored = plain + padding
|
||||
data[start:start + lengths[index]] = stored
|
||||
struct.pack_into('<I', data, head + 24 + 4 * index, len(stored))
|
||||
return data[:-1] if truncate else data
|
||||
|
||||
return self.nsz(blocks=True, damage=pad)
|
||||
|
||||
def test_stream_comes_back_byte_for_byte(self):
|
||||
src = self.nsz()
|
||||
self.assertEqual(ncz.compressed(src)[0], 'nsz')
|
||||
@@ -102,6 +121,19 @@ class UnpackTest(unittest.TestCase):
|
||||
ncz.unpack(self.nsz(blocks=True), self.dir / 'out.nsp')
|
||||
self.assertEqual((self.dir / 'out.nsp').read_bytes(), self.expected())
|
||||
|
||||
def test_raw_block_padding_is_not_written_into_the_nca(self):
|
||||
for indices in ((0,), (-1,), (0, -1)):
|
||||
with self.subTest(indices=indices):
|
||||
seen = []
|
||||
ncz.unpack(self.padded_nsz(indices), self.dir / 'out.nsp', lambda d, t: seen.append((d, t)))
|
||||
self.assertEqual((self.dir / 'out.nsp').read_bytes(), self.expected())
|
||||
self.assertEqual(seen[-1], (len(self.expected()), len(self.expected())))
|
||||
|
||||
def test_a_raw_block_cannot_claim_padding_outside_its_entry(self):
|
||||
with self.assertRaisesRegex(ValueError, 'Truncated NCZ block'):
|
||||
ncz.unpack(self.padded_nsz((-1,), truncate=True), self.dir / 'out.nsp')
|
||||
self.assertFalse((self.dir / 'out.nsp').exists())
|
||||
|
||||
def test_plain_nsp_is_not_compressed(self):
|
||||
path = self.dir / 'plain.nsp'
|
||||
path.write_bytes(self.expected())
|
||||
|
||||
+3
-1
@@ -126,7 +126,9 @@ def blocks(f, pos, end, total):
|
||||
for i, length in enumerate(stored):
|
||||
plain = min(1 << exponent, size - (i << exponent))
|
||||
require(pos + length <= end, 'Truncated NCZ block')
|
||||
data = read_at(f, pos, length)
|
||||
# nsz's BlockDecompressorReader.py __decompressBlock skips padding in a raw block: only plain bytes belong
|
||||
# to the NCA, while the full stored length locates the next block.
|
||||
data = read_at(f, pos, min(length, plain))
|
||||
pos += length
|
||||
yield zstd.decompress(data) if length < plain else data
|
||||
|
||||
|
||||
Reference in new issue
Block a user