mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 21:15:57 +00:00
A block that decompresses to the wrong size is a damaged file again, so the installer asks for a fresh copy
The size check added in this pull request raised a plain ValueError, which the installer maps to check.unpack_failed. Before it, such a block ended in a hash or length mismatch and the player saw check.input_damaged. ncz.unpack_ncz now turns the new BlockSizeError into DamagedFile, like ZstdError and EOFError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
516b1d2ab5
commit
589bd9e08f
2 files changed
+26
-2
No files matched your search
@@ -163,6 +163,23 @@ class UnpackTest(unittest.TestCase):
|
||||
ncz.unpack(self.padded_nsz((-1,), truncate=True), self.dir / 'out.nsp')
|
||||
self.assertFalse((self.dir / 'out.nsp').exists())
|
||||
|
||||
def test_block_of_the_wrong_size_is_a_damaged_file(self):
|
||||
"""The installer turns DamagedFile into check.input_damaged, which asks for a fresh copy of the file."""
|
||||
def short(data):
|
||||
head = ncz.HEADER + 16 + 0x40 * len(SECTIONS)
|
||||
count = struct.unpack_from('<I', data, head + 12)[0]
|
||||
lengths = struct.unpack_from(f'<{count}I', data, head + 24)
|
||||
start = head + 24 + 4 * count + lengths[0]
|
||||
frame = zstd.compress(self.body[1 << 14:(2 << 14) - 100]) # block 1 is zeros, stored compressed
|
||||
data[start:start + lengths[1]] = frame
|
||||
struct.pack_into('<I', data, head + 24 + 4, len(frame))
|
||||
return data
|
||||
|
||||
with self.assertRaises(ncz.DamagedFile) as e:
|
||||
ncz.unpack(self.nsz(blocks=True, damage=short), self.dir / 'out.nsp')
|
||||
self.assertIn('decompressed size', e.exception.detail)
|
||||
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())
|
||||
|
||||
+9
-2
@@ -35,6 +35,10 @@ class DamagedFile(ValueError):
|
||||
super().__init__(f'{self.path} is damaged or incomplete ({detail})')
|
||||
|
||||
|
||||
class BlockSizeError(ValueError):
|
||||
"""An NCZBLOCK block whose data decompresses to more or fewer bytes than its header declares."""
|
||||
|
||||
|
||||
def compressed(path):
|
||||
"""('nsz' or 'xcz', entries) for a game file holding NCZ files, else None. Raises ValueError for a file that is
|
||||
neither an NSP nor an XCI."""
|
||||
@@ -135,7 +139,8 @@ def blocks(f, pos, end, total):
|
||||
# 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')
|
||||
if len(data) != plain:
|
||||
raise BlockSizeError(f'NCZ block decompressed size does not match its header (block {i})')
|
||||
yield data
|
||||
|
||||
|
||||
@@ -184,7 +189,9 @@ def unpack_ncz(f, start, end, out, name, source, progress):
|
||||
progress(n)
|
||||
if at == total:
|
||||
break
|
||||
except (zstd.ZstdError, EOFError) as e: # EOFError: the stream ends before its end-of-frame marker
|
||||
# EOFError: the stream ends before its end-of-frame marker. BlockSizeError: a block's data is damaged; like the
|
||||
# other two it is a damaged file, so the installer asks for a fresh copy (installer/steps.py, Check.unpack).
|
||||
except (zstd.ZstdError, EOFError, BlockSizeError) as e:
|
||||
raise DamagedFile(source, f'{name}: {e}') from None
|
||||
if at != total or pending:
|
||||
raise DamagedFile(source, f'{name}: {at - HEADER:,} of {total - HEADER:,} bytes')
|
||||
|
||||
Reference in new issue
Block a user