diff --git a/tools/nup.py b/tools/nup.py index fe2c944c..b26e5867 100644 --- a/tools/nup.py +++ b/tools/nup.py @@ -43,9 +43,20 @@ class Nup: toc = f.read(toc_size) if len(toc) != toc_size or hashlib.sha256(toc).digest() != toc_sha: raise NupError('the table of contents is damaged (its sha256 does not match)') + # The sha256 only says the table is what the file's writer wrote: counts and offsets that do not fit it end in + # a NupError, which the callers report, not in struct.error or IndexError + try: + self._parse(toc) + except (struct.error, IndexError, ValueError, OverflowError, MemoryError) as e: + raise NupError(f'the table of contents is malformed ({type(e).__name__}: {e})') from None + + def _parse(self, toc): tmagic, npkg, ndir, nfile, pool_off, pool_len, _ = struct.unpack_from(' len(toc) + or pool_off + pool_len > len(toc)): + raise NupError('the table of contents is malformed (its counts or name pool run past its end)') at = 0x30 self.packages = [] for i in range(npkg): @@ -56,26 +67,40 @@ class Nup: at += ndir * DIRECTORY.size self.files = [FILE.unpack_from(toc, at + i * FILE.size) for i in range(nfile)] at += nfile * FILE.size + if pool_off < at: + raise NupError('the table of contents is malformed (its name pool overlaps its entries)') self.children = struct.unpack_from(f'<{(pool_off - at) // 4}I', toc, at) self.pool = toc[pool_off:pool_off + pool_len] self.ndir = ndir def _name(self, off, n): - return self.pool[off:off + n].decode('utf-8') + try: + return self.pool[off:off + n].decode('utf-8') + except UnicodeDecodeError: + raise NupError(f'a name in the table of contents is not UTF-8 (at {off:#x})') from None def tree(self, dir_index): - """(path relative to the directory, file index) for every file below it.""" + """(path relative to the directory, file index) for every file below it. A directory met twice (one listed + as its own child or ancestor) is a NupError, where it went round for good.""" stack = [(dir_index, '')] + seen = set() while stack: d, prefix = stack.pop() - _no, _nl, first, count, _ = self.dirs[d] - for c in self.children[first:first + count]: - if c < self.ndir: - no, nl = self.dirs[c][0], self.dirs[c][1] - stack.append((c, prefix + self._name(no, nl) + '/')) - else: - fi = c - self.ndir - yield prefix + self._name(self.files[fi][0], self.files[fi][1]), fi + if d in seen: + raise NupError(f'directory {d} of the table of contents is listed below itself') + seen.add(d) + try: + _no, _nl, first, count, _ = self.dirs[d] + entries = self.children[first:first + count] + for c in entries: + if c < self.ndir: + no, nl = self.dirs[c][0], self.dirs[c][1] + stack.append((c, prefix + self._name(no, nl) + '/')) + else: + fi = c - self.ndir + yield prefix + self._name(self.files[fi][0], self.files[fi][1]), fi + except IndexError: + raise NupError(f'directory {d} of the table of contents names an entry it does not have') from None def size(self, fi): return self.files[fi][2] @@ -112,7 +137,10 @@ class Nup: if not b: break left -= len(b) - out = d.decompress(b) + try: + out = d.decompress(b) + except zstandard.ZstdError as e: + raise NupError(f'file {fi}: damaged ({e})') from None if out: got += len(out) h.update(out)