Milestone 6 of 10

Pack and parse the fixed-width codec payload

Write exact DTC1 fields and two-byte big-endian codes, validate configured bounds, and reject malformed lengths, magic, fields, and code bytes.

The payload gives codes a declared boundary and identity. Pack fields exactly, then reject any byte layout that does not match the declaration.

Goal

Write and parse the exact DTC1 payload with two-byte big-endian codes, configured bounds, exact lengths, and malformed-stream rejection.

Inputs

Use code streams, source length, dictionary limit, and payload configuration. The payload is:

bytes 0..3    payload magic b"DTC1"
bytes 4..7    max dictionary size, unsigned four-byte big-endian
bytes 8..15   original byte length, unsigned eight-byte big-endian
bytes 16..19  code count, unsigned four-byte big-endian
remaining     exactly code_count unsigned two-byte big-endian codes

An empty source has length zero, code count zero, and no code bytes. Valid code values are 0..65535; do not use bit packing, variable widths, host byte order, or an end marker.

Deliverables

Implement packing and parsing in src/payload.py. Produce payload read-back records with field values, exact payload length, code order, and source identity. Produce malformed results for truncated header, wrong inner magic, invalid dictionary size, excessive original length or code count, odd/short code bytes, truncated payload, trailing payload bytes, and individual codes outside range.

Checks

Check empty and non-empty payloads, one and several codes, 0, 255, 256, and 65535, big-endian order, exact expected payload length, all configured bounds, and deterministic bytes. Reject payloads before dictionary decoding when header fields or remaining lengths disagree. Do not infer code count from remaining bytes, accept odd code bodies, or treat trailing bytes as padding.

Workspace

Keep payload fields, two-byte pack/unpack, bounds, and malformed cases in src/payload.py. Write payload read-back evidence. Do not add the outer container yet.

Hints

HintCalculate the expected length
The payload header is 20 bytes; the code body is 2 * code_count. Compare that exact length with the remaining bytes before unpacking a code.
HintBig endian is explicit
Pack the high byte first and the low byte second. Never use host byte order.
HintValidate fields before decoding
A payload can have valid-looking codes but an invalid declared source length or dictionary size. Check fields and bounds before invoking the dictionary decoder.

Review

For three codes, calculate header length, body length, and total payload length. What should happen when one byte is removed from the code body?

How to check your work

Checks compare fields, bytes, code order, lengths, and malformed outcomes with the fixtures. The supplied fixture consumes exactly the declared DTC1 payload.

LLM PrimerPack and parse the fixed-width codec payloadhttps://llmprimer.com/python/projects/build-a-lossless-dictionary-text-codec/pack-and-parse-the-fixed-width-codec-payload© 2026 LLM Primer