psin_data: a lone file without the PSIN magic is refused, not reported as 0 files (EGEN-PSINDATA-NOT-PSIN) (#6841)

Squash-merged after diff review of PR #6841 - psin_data: a lone file without the PSIN magic is refused, not reported as 0 files (EGEN-PSINDATA-NOT-PSIN)
This commit is contained in:
evilclanker9000
2026-11-23 16:47:16 +01:00
committed by GitHub
parent 6de3970e15
commit 12a8a7adee
3 changed files with 40 additions and 5 deletions

View File

@ -399,11 +399,12 @@ python -I tools/psin_data.py <PSIN file or carve directory> [--json] [--limit N]
python -I tools/psin_data.py <PSIN file> --dump
```
The default output is the counts above, for one file or every `.bin` in a carve directory. `--dump` prints one
container's decoded tree (hashes as hex) on stdout, for local inspection only. It writes no file and must not be
pasted into an issue or a pull request. `tests/test_psin_data.py` builds containers from invented names and covers
every layout above, the end padding, a bad fill, pointers out of range, an unlisted enum value, a skipped type and
the CLI.
The default output is the counts above, for one file or every `.bin` in a carve directory. A single file that does
not start with the `PSIN` magic is one `ERROR:` line and exit 2; inside a directory such a file is skipped, since a
carve folder holds other streams too. `--dump` prints one container's decoded tree (hashes as hex) on stdout, for
local inspection only. It writes no file and must not be pasted into an issue or a pull request.
`tests/test_psin_data.py` builds containers from invented names and covers every layout above, the end padding, a
bad fill, pointers out of range, an unlisted enum value, a skipped type and the CLI.
### Name hashes in the data

View File

@ -381,6 +381,26 @@ class TestCli(unittest.TestCase):
self.assertEqual(r.returncode, 2)
self.assertIn("incomplete", r.stderr)
def test_a_single_file_without_the_magic_is_refused(self):
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "not-psin.bin")
with open(path, "wb") as f:
f.write(b"\x78\xda not psin")
r = self.run_tool(path, "--json")
self.assertEqual(r.returncode, 2)
self.assertEqual(r.stdout, "")
self.assertEqual(len(r.stderr.strip().splitlines()), 1)
self.assertIn("not a PSIN container", r.stderr)
def test_a_folder_still_skips_a_file_without_the_magic(self):
# the negative control: the same bytes inside a folder are skipped, not refused
with tempfile.TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "000000003000.bin"), "wb") as f:
f.write(b"\x78\xda not psin")
r = self.run_tool(tmp, "--json")
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(json.loads(r.stdout), {})
def test_dump_missing_file_returns_2(self):
with tempfile.TemporaryDirectory() as tmp:
missing = os.path.join(tmp, "missing.bin")

View File

@ -52,6 +52,9 @@ Usage:
The default output is counts only: containers, block tables, bytes reached from the root, pointers, arrays and maps,
with every check. `--dump` prints the decoded tree of one container on stdout, hashes as hex, for local inspection;
it writes no file. Unknown member types (0x5a, 0x76, 0x77, ...) are counted and skipped, never guessed.
In a carve directory, a stream that does not start with the `PSIN` magic is skipped (the folder holds other files
too). As the only argument it is a wrong path, so it is one `ERROR:` line and exit 2 rather than an empty count.
"""
import argparse
import bisect
@ -570,6 +573,17 @@ def main():
if not os.path.exists(a.src):
print(f"ERROR: {a.src}: no such file or folder", file=sys.stderr)
return 2
if os.path.isfile(a.src):
# a directory skips a stream without the magic, but a lone file named on the command line is a wrong path
try:
with open(a.src, "rb") as f:
magic = f.read(4)
except OSError as e:
print(f"ERROR: {a.src}: {e}", file=sys.stderr)
return 2
if magic != psin_index.MAGIC:
print(f"ERROR: {a.src}: not a PSIN container", file=sys.stderr)
return 2
if a.dump:
if not os.path.isfile(a.src):
print("ERROR: --dump takes one PSIN file", file=sys.stderr)