ProvenPin Install

construct binary structs with checksums and length prefixes: working versions (tested 2026-10-18)

Declarative construct Struct for a length-prefixed, CRC32-checked file header with an Enum kind, plus round-trip build/parse and offline failure tests.

passed6 passed · 0 failed2026-10-18 03:57 UTC
Status
passed
Last passed
2026-10-18 03:57 UTC
Tests
6 / 6
Language
Python
Runtime
Python 3.13.16
pinned versions2
construct@2.10.70pytest@9.1.1
run it
uv venv -q .venv && uv pip install -q --python .venv/bin/python -r requirements.txt
.venv/bin/python -m pytest -q
test environment
Python 3.13.16
linux 6.17.0-1022-azure
x64
runner: github-actions 39095370682

The code

Exactly the files that passed. Your agent can pull them with get_recipe("construct-binary-struct-parsing") (install), or see them on GitHub.

requirements.txt
construct==2.10.70
pytest==9.1.1
header.py
import zlib

from construct import (
    Bytes,
    Checksum,
    Const,
    Enum,
    GreedyRange,
    Int8ul,
    Int16ul,
    Int32ul,
    Rebuild,
    Struct,
    len_,
    this,
)

MAGIC = b"PPAK"
Kind = Enum(Int8ul, FILE=1, DIR=2, LINK=3)


def crc32(data: bytes) -> int:
    return zlib.crc32(data) & 0xFFFFFFFF


Header = Struct(
    "magic" / Const(MAGIC),
    "version" / Int16ul,
    "kind" / Kind,
    "name_len" / Rebuild(Int8ul, len_(this.name)),
    "name" / Bytes(this.name_len),
    "data_len" / Rebuild(Int16ul, len_(this.data)),
    "data" / Bytes(this.data_len),
    "crc" / Checksum(Int32ul, crc32, this.data),
)

Archive = GreedyRange(Header)


def build_header(name: str, data: bytes, kind: str = "FILE", version: int = 1) -> bytes:
    # name_len and data_len are Rebuild fields, so callers never pass them
    return Header.build(dict(version=version, kind=kind, name=name.encode(), data=data))


def parse_header(raw: bytes):
    return Header.parse(raw)
tests/test_header.py
import zlib

import pytest
from construct import ChecksumError, ConstError, ConstructError, StreamError

from header import Archive, build_header, parse_header


def test_roundtrip_rebuilds_lengths():
    raw = build_header("readme.txt", b"hello", version=3)
    h = parse_header(raw)
    assert h.name == b"readme.txt"
    assert h.data == b"hello"
    assert (h.name_len, h.data_len) == (10, 5)
    assert h.version == 3


def test_enum_is_an_int_subclass():
    h = parse_header(build_header("a", b"", kind="DIR"))
    assert h.kind == "DIR"
    assert int(h.kind) == 2


def test_checksum_covers_only_payload():
    h = parse_header(build_header("a.bin", b"payload"))
    assert h.crc == zlib.crc32(b"payload") & 0xFFFFFFFF


def test_corrupt_payload_is_rejected():
    raw = bytearray(build_header("a.bin", b"payload"))
    raw[-5] ^= 0x01  # flip a bit inside data, checksum stays behind it
    with pytest.raises(ChecksumError):
        parse_header(bytes(raw))


def test_bad_magic_and_truncation_fail_loudly():
    assert parse_header(build_header("a", b"")).magic == b"PPAK"
    with pytest.raises(ConstError):
        parse_header(b"XXXX" + build_header("a", b"")[4:])
    with pytest.raises((StreamError, ConstructError)):
        parse_header(build_header("a", b"12345")[:-2])


def test_concatenated_records_need_greedyrange():
    blob = build_header("a", b"1") + build_header("b", b"22", kind="LINK")
    records = Archive.parse(blob)
    assert [(r.name, r.data, r.kind) for r in records] == [
        (b"a", b"1", "FILE"),
        (b"b", b"22", "LINK"),
    ]
pytest.ini
[pytest]
pythonpath = .

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.