Files

30 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Unpack an .octoreport (OctoReport.exe) into a folder: report.txt + the attached files. usage: read_report.py file [outdir]"""
import subprocess
import sys
from pathlib import Path, PurePosixPath
src = Path(sys.argv[1])
out = Path(sys.argv[2] if len(sys.argv) > 2 else src.with_suffix(''))
b = src.read_bytes()
assert b.startswith(b'OCTOREPORT 1\n'), 'not an OctoReport file'
raw = subprocess.run(['zstd', '-dc', '--memory=512MB'], input=b[13:], capture_output=True, check=True).stdout
out.mkdir(parents=True, exist_ok=True)
at = 0
while at < len(raw):
nl = raw.index(b'\n', at)
head = raw[at:nl].decode('utf-8', 'replace').split(' ', 2)
n = int(head[1])
data = raw[nl + 1:nl + 1 + n]
at = nl + 1 + n
if head[0] == 'TEXT':
(out / 'report.txt').write_bytes(data)
continue
rel = PurePosixPath(head[2])
assert not rel.is_absolute() and '..' not in rel.parts, rel # untrusted names stay inside outdir
p = out / 'files' / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_bytes(data)
print(out)
print((out / 'report.txt').read_text(errors='replace'))