"""Walk chunks; auto-detect preamble length by finding first 10 NN.""" import sys sys.path.insert(0, ".") from analysis.load_bundle import load_bundle def walk_chunks(body, start, max_NN=0x80): chunks = [] i = start while i + 1 < len(body): if body[i] != 0x10: break NN = body[i + 1] if NN == 0 or NN > max_NN or NN % 4 != 0: break chunk_len = NN // 2 + 4 if i + chunk_len > len(body): break data = bytes(body[i + 2 : i + 2 + NN // 2]) trailer = bytes(body[i + 2 + NN // 2 : i + chunk_len]) chunks.append((i, NN, data, trailer)) i += chunk_len return chunks, i def find_first_chunk_start(body): """Locate first byte that begins a `10 NN` chunk (NN ∈ multiples of 4, 4..0x7C).""" for i in range(20): if body[i] == 0x10 and body[i + 1] % 4 == 0 and 0 < body[i + 1] <= 0x7C: return i return -1 def main(): for name in ("event-c", "event-d", "event-a", "event-b"): b = load_bundle(name) body = b.body start = find_first_chunk_start(body) chunks, end = walk_chunks(body, start) print(f"\n=== {name} === body={len(body)} N_samples={len(b.samples['Tran'])} start={start}") print(f" chunks parsed: {len(chunks)}, walk ended at {end}") if chunks: print(f" first 5 chunks: {[(c[0], c[1], c[3].hex()) for c in chunks[:5]]}") print(f" last 5 chunks: {[(c[0], c[1], c[3].hex()) for c in chunks[-5:]]}") print(f" bytes around end of walk: {body[end-4:end+12].hex(' ')}") else: print(f" bytes at start: {body[start:start+16].hex(' ')}") if __name__ == "__main__": main()