"""Decoder for the write-flow capture at artifacts/reverse-engineering/instrumented-wcf-writemessage-writes/ bothmessage-write-capture-latest.ndjson. Lists the 46-record write-flow inventory and prints the EnsT2(analog) CTagMetadata payload byte-by-byte for layout decoding. Output stays commit-safe — the only string values printed are the sandbox tag name, description, and engineering unit, all of which were chosen for the sandbox itself (not customer data). """ import base64 import json import re import sys from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent CAPTURE = REPO_ROOT / "artifacts" / "reverse-engineering" / "instrumented-wcf-writemessage-writes" / "bothmessage-write-capture-latest.ndjson" ACTION_RE = re.compile(rb"aa/(?:Hist|Retr|Trx|Stat|Stor)/[A-Za-z0-9]+(?:Response)?") def main() -> int: if not CAPTURE.exists(): print(f"Capture file not found: {CAPTURE}") return 1 with CAPTURE.open(encoding="utf-8-sig") as fh: records = [json.loads(line) for line in fh if line.strip()] print(f"Total records: {len(records)}") print() print(f"{'#':>3} {'Phase':<6} {'Length':>6} {'Action':<48}") print("-" * 70) for idx, rec in enumerate(records): body = base64.b64decode(rec["Base64"]) match = ACTION_RE.search(body) action = match.group(0).decode() if match else "" phase = "Write" if rec["Phase"] == "WCF.WriteMessage.Body" else "Read" print(f"{idx:>3} {phase:<6} {rec['Length']:>6} {action:<48}") # Find the EnsT2 outgoing record and dump its InBuff payload. print() print("== EnsT2(Float) CTagMetadata payload ==") for idx, rec in enumerate(records): if rec["Phase"] != "WCF.WriteMessage.Body": continue body = base64.b64decode(rec["Base64"]) if b"aa/Hist/EnsT2" not in body: continue i = body.find(b"InBuff") marker = body[i + 6] if marker == 0x9F: length = int.from_bytes(body[i + 7:i + 9], "little") payload = body[i + 9:i + 9 + length] elif marker == 0x9E: length = body[i + 7] payload = body[i + 8:i + 8 + length] elif marker == 0xA0: length = int.from_bytes(body[i + 7:i + 9], "little") payload = body[i + 9:i + 9 + length + 1] else: print(f" Unknown marker 0x{marker:02X}; cannot extract") return 2 print(f" Record {idx}: {len(payload)} bytes") for off in range(0, len(payload), 16): chunk = payload[off:off + 16] hp = " ".join(f"{c:02X}" for c in chunk) ap = "".join(chr(c) if 32 <= c < 127 else "." for c in chunk) print(f" {off:04X} {hp:<48} |{ap}|") return 0 print(" (No EnsT2 record found.)") return 0 if __name__ == "__main__": sys.exit(main())