From 12a8a7adeefc971e1f0a99b8ecdda10cb978cb5e Mon Sep 17 00:00:00 2001 From: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:47:16 +0100 Subject: [PATCH] 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) --- docs/psin.md | 11 ++++++----- tests/test_psin_data.py | 20 ++++++++++++++++++++ tools/psin_data.py | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/psin.md b/docs/psin.md index 6ce4d4c32..49945614e 100644 --- a/docs/psin.md +++ b/docs/psin.md @@ -399,11 +399,12 @@ python -I tools/psin_data.py [--json] [--limit N] python -I tools/psin_data.py --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 diff --git a/tests/test_psin_data.py b/tests/test_psin_data.py index 98647ab55..d3af76774 100644 --- a/tests/test_psin_data.py +++ b/tests/test_psin_data.py @@ -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") diff --git a/tools/psin_data.py b/tools/psin_data.py index 838385ef7..69fa065c6 100644 --- a/tools/psin_data.py +++ b/tools/psin_data.py @@ -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)