Merge branch 'pr1351-head' into task/pr1351

This commit is contained in:
Valiantsin Dzerakh committed 2026-11-22 16:04:59 +03:00
commit 516b1d2ab5
2 files changed
+71 -2

No files matched your search

+61
View File
@@ -66,6 +66,35 @@ def ncz_file(nca, body, blocks=False):
return out + b''.join(stored)
class BlocksTest(unittest.TestCase):
def read(self, data, plain=1 << 14):
header = b'NCZBLOCK' + bytes([2, 1, 0, 14]) + struct.pack('<IQI', 1, plain, len(data))
return ncz.blocks(io.BytesIO(header + data), 0, len(header) + len(data), plain)
def test_oversized_compressed_block_is_rejected_before_yielding(self):
data = zstd.compress(bytes(1 << 20)) # a small fixture, 64 times the declared output
with self.assertRaisesRegex(ValueError, 'NCZ block decompressed size'):
next(self.read(data))
def test_short_compressed_block_is_rejected_before_yielding(self):
with self.assertRaisesRegex(ValueError, 'NCZ block decompressed size'):
next(self.read(zstd.compress(bytes(100))))
def test_compressed_block_can_contain_multiple_frames(self):
plain = bytes(1 << 14)
data = zstd.compress(plain[:100]) + zstd.compress(plain[100:])
self.assertEqual(b''.join(self.read(data)), plain)
def test_compressed_final_block_uses_its_actual_plain_size(self):
plain = bytes(123)
self.assertEqual(b''.join(self.read(zstd.compress(plain), len(plain))), plain)
def test_compressed_block_must_reach_its_end_marker(self):
data = zstd.compress(bytes(1 << 14), options={zstd.CompressionParameter.checksum_flag: True})
with self.assertRaises((zstd.ZstdError, EOFError)):
next(self.read(data[:-1]))
class UnpackTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
@@ -88,6 +117,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 +150,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())
+10 -2
View File
@@ -126,9 +126,17 @@ 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
if length < plain:
# Read at most one byte beyond the declared size: a damaged frame must not allocate its whole output.
# ZstdFile also preserves support for concatenated frames within a block.
with zstd.ZstdFile(io.BytesIO(data)) as z:
data = z.read(plain + 1)
require(len(data) == plain, 'NCZ block decompressed size does not match its header')
yield data
def stream(f, pos, end, total):