Skip to content

Latest commit

 

History

History
138 lines (102 loc) · 6.02 KB

FORMAT.md

File metadata and controls

138 lines (102 loc) · 6.02 KB

DXFS File Format

This document describes the current DXF3 container and the Packed payload. DXFS writes DXF3. The reader also accepts legacy DXF1 and the separate DXF2 blocked-stream container.

DXF3 container

All multi-byte fixed-width values are little-endian. The compact header is:

Field Size Meaning
Magic 4 bytes ASCII DXF3
Version 1 byte 3
Kind 1 byte Packed = 0
Character count 1-10 bytes Unsigned base-128 varint
CRC32 4 bytes CRC-32/ISO-HDLC of codec, character count and payload
Payload Remaining bytes Packed representation

The payload length is derived from the file length. No separate 8-byte payload length field is needed. The reader validates the magic, version, kind, count varint, checksum over metadata and payload, and codec-specific payload size.

DXF1 remains readable with its 32-byte header and FNV-1a 64-bit payload checksum. DXF2 remains the independent indexed block format used by the streaming BlockReader.

Packed payload flags

The first payload byte identifies the representation.

Flag 0: decimal digits

Digits are grouped in blocks of at most 19. Each block is interpreted as a base-10 integer and stored in 8 little-endian bytes. A final short block is left-zero-padded by the decoder. The exact character count determines how many trailing digits are meaningful.

Flag 1: decimal digits with one decimal point

The characters are mapped to base-11 cells: digits 0-9 map to values 0-9 and the decimal point maps to value 10. Up to 18 cells are represented by one 64-bit little-endian integer. The exact character count preserves the point's position.

Flag 2: 7-bit ASCII

Each ASCII byte from 0 through 127 is stored as a continuous 7-bit value, least-significant bit first. Eight characters occupy seven payload bytes, apart from the final partial byte.

Flag 3: adaptive alphabet

The payload contains:

  1. Flag byte 3.
  2. One-byte alphabet size from 1 through 128.
  3. The sorted distinct ASCII symbols, one byte each.
  4. A continuous bitstream of alphabet indexes.

The index width is the smallest integer w for which 2^w is at least the alphabet size. A singleton alphabet needs no index bits. The encoder compares the complete flag-3 payload against flag-2 and selects whichever is smaller. This keeps decoding direct and avoids a statistical entropy decoder.

Exactness and limitations

Packed currently preserves ASCII bytes 0-127, including spaces, punctuation, symbols and control characters. Bytes above 127 are rejected. Unicode is not silently normalized or converted.

The file count is a byte/character count for this ASCII-only representation. The original bytes are reconstructed exactly. CRC32 detects accidental metadata or payload corruption; it is not a cryptographic authenticity mechanism.

Size accounting

A DX file's size includes the compact header, checksum, payload flag, alphabet table where used, and packed character data. Comparisons with TXT must use the complete serialized DX file, not only its payload.

Flag 4: mixed-radix alphabet blocks

For alphabets where fixed-width indexes waste many code values, the encoder also evaluates blockwise base-N storage. Each block is interpreted as one integer in base N, where N is the alphabet size. The largest block length whose full value fits in 64 bits is used. Full blocks occupy 8 bytes; the final partial block uses the minimum whole-byte width that can represent all of its possible values. The block length and final width are derived from the alphabet size and character count, so no per-block metadata is needed.

The encoder compares the complete flag-2, flag-3 and flag-4 payload sizes and selects the smallest. The decoder uses integer division and remainder only.

Flag 5: DXH adaptive canonical Huffman

DXH builds a frequency table from the actual input and derives a deterministic Huffman code for each distinct ASCII symbol. The payload layout is:

  1. Flag byte 5.
  2. One-byte alphabet size (2 through 128).
  3. Sorted symbol bytes.
  4. One code-length byte for each symbol, in the same order.
  5. The canonical Huffman bitstream, packed most-significant bit first.

The decoder reconstructs canonical codes from the symbol and code-length tables, builds a prefix tree, and restores exactly the declared character count. No original text, external dictionary, or generator descriptor is stored. DXH is useful when symbol frequencies are skewed: common characters receive shorter codes and rare characters receive longer codes. The table has a fixed per-symbol cost, so it is not expected to win on every input.

The Packed encoder compares the complete DXH payload with 7-bit ASCII, adaptive fixed-width alphabet, and mixed-radix candidates. DXH is selected only when its complete payload is strictly smaller. This is a statistical representation, not a promise to beat ASCII for every possible input.

Flag 6: DXP adaptive context arithmetic

DXP is a context-predictive representation. It maintains a small adaptive frequency model for each previous ASCII symbol, plus an initial start context. The next symbol is encoded using the frequency distribution associated with the preceding symbol. The encoder and decoder begin with identical uniform models and update the same context counts after every decoded or encoded symbol, so no model table needs to be stored in the file.

The interval coder uses integer arithmetic and emits a final bitstream after the declared character sequence. Context counts are periodically rescaled to keep arithmetic bounded. DXP is particularly effective on data with repeated transitions or predictable local sequences. It can be less effective on unpredictable data, so Packed compares its complete payload with all other representations and selects it only when smaller.

DXP is a DXFS-designed format implementation using established ideas from adaptive context modeling and arithmetic coding. Context modeling is a well-established research direction; the project-specific contribution is the deterministic model, compact flag layout, and its integration into DXF3.