Binary library wire format

Primitive encodings

Every structure of the format is built from the encodings on this page. Two families coexist: the fixed-width little-endian fields of the container, declarations, and partition sections 1 to 4, 6, 7 and 9, and the varint fields of the manifest and of partition sections 5, 8 and 10. Each page says which one a field uses.

Fixed-width integers

Name Bytes Encoding
u8 1 an unsigned byte
u32 4 unsigned, little-endian
u64 8 unsigned, little-endian (only the container's section lengths)

There are no signed fixed-width fields. A writer MUST refuse (LIMIT) a value that does not fit its field; every count and position of the format fits a u32.

Varints

A varint is an unsigned LEB128 integer: seven bits per byte, least significant group first, the high bit set on every byte but the last.

\[ v = \sum_{i=0}^{k-1} (b_i \bmod 128) \cdot 128^{i}, \qquad b_i \ge 128 \text{ for } i < k - 1, \quad b_{k-1} < 128 \]

Every varint MUST be minimal: a multi-byte varint MUST NOT end in the byte 0x00 (so 0 is the single byte 00, 127 is 7f, 128 is 80 01, 300 is ac 02). A reader refuses a non-minimal varint with FORMAT at its first byte. The limits differ by where the varint is:

Where Longest Largest value Refusal
partition sections 5, 8 and 10, a compressed section's raw length 10 bytes \(2^{64} - 1\); the tenth byte MUST be 0 or 1 FORMAT at the first byte
the manifest 9 bytes \(2^{63} - 1\): a varint that has not ended after 9 bytes is refused FORMAT at the first byte

ST7.3 states the manifest's limit. Each field then bounds its value further (a count by the remaining bytes, a position by its table, a part number by \(2^{32} - 1\)); those checks are given with the fields.

Zigzag

The DAG blocks write signed differences as varints of their zigzag encoding (ST9.3):

\[ z(x) = \begin{cases} 2x & x \ge 0 \\ -2x - 1 & x < 0 \end{cases} \qquad z^{-1}(v) = \begin{cases} v / 2 & v \text{ even} \\ -(v + 1)/2 & v \text{ odd} \end{cases} \]

so 0, −1, 1, −2, 2 encode as 0, 1, 2, 3, 4.

Strings, lists, and options

Form Encoding Used in
string a u32 byte length, then that many bytes of UTF-8 declarations, partition sections
blob a u32 byte length, then that many bytes declarations in section 2, literal encodings
list a u32 count, then the items as above
optional a u8 0 (absent) or 1 (present), then the value if present declarations
name a varint byte length, then that many bytes of UTF-8 the manifest
flag a u8 0 or 1 the manifest

A reader MUST check that a count's items can fit in the bytes that remain, using each item's minimum size, before it allocates or iterates; the minimum sizes are given with each list. A count that cannot fit is refused with FORMAT at the count's first byte. Any other value of an optional's or a flag's first byte is refused (FORMAT).

UTF-8

Every string and name MUST be well-formed UTF-8 (Unicode table 3-7): no overlong forms, no surrogates (U+D800–U+DFFF), nothing above U+10FFFF. A reader validates and never replaces:

Lead byte Then Refusal
00–7f nothing —
c2–df one continuation
e0 a0–bf, then one continuation second byte below a0
e1–ec, ee–ef two continuations
ed 80–9f, then one continuation second byte a0 or above
f0 90–bf, then two continuations second byte below 90
f1–f3 three continuations
f4 80–8f, then two continuations second byte 90 or above
80–c1, f5–ff — invalid lead byte

A continuation is 80–bf. Errors are FORMAT at the offending byte: the lead byte for an invalid lead, a truncated sequence or a range violation of the second byte, and the continuation itself otherwise. The zero byte is a valid character; names are compared as byte strings, never as Unicode text.

Identities and hashes

An identity is 32 raw bytes, a SHA-256 digest (FIPS 180-4), stored in the digest's own byte order. Identities are ordered as byte strings: the first differing byte decides, which is the order of the digest read as an unsigned big-endian 256-bit number. Every "strictly increasing by identity" on these pages means this order.

SHA-256 is used five ways:

Use Input Page
container digest every byte of the container before the digest container
file hash every byte of a manifest or partition file directory
block digest and Merkle root 0x00 and a DAG block's bytes; 0x01 and two subtree hashes partitions
declaration and core identity a domain prefix, a zero byte, and the declaration bytes or core name identities
statement identity judgment/stmt/1, a zero byte, and the key's bytes identities

As text, an identity or a file hash is written as 64 lowercase hexadecimal digits, two per byte, most significant nibble first. CURRENT, the file names, and the canonical names judgment.stmt.<hex> use this form; a reader refuses uppercase digits there. The kernel commit of a container is different: 40 hexadecimal digits of either case, recorded verbatim.

Raw DEFLATE

Eight of a partition's ten sections and every stream of its DAG blocks are compressed with raw DEFLATE, RFC 1951: no zlib or gzip header, no checksum, no dictionary. The surrounding structure always gives the stream's raw (inflated) length R and the stream's exact extent. The full reference, with every error and its offset, is compression.

What readers accept

A reader MUST accept every stream RFC 1951 defines that ends exactly at the end of its extent and inflates to exactly R bytes, and MUST refuse all others:

  1. Blocks. Stored (BTYPE 0, with LEN the ones' complement of NLEN; its padding bits ignored), fixed Huffman (1), and dynamic Huffman (2), any number of them, the last with BFINAL set. BTYPE 3 is refused.
  2. Dynamic headers. HLIT at most 286 and HDIST at most 30; a complete code-length code; a repeat code (16) only after a length, and no repeat past HLIT + HDIST lengths (runs may cross from the literal/length lengths into the distance lengths); a code for end-of-block; a complete literal/length code; a distance code that is complete, empty, or a single one-bit code (RFC 1951, section 3.2.7). A literal/length code of a single one-bit code is refused, although zlib accepts it.
  3. Symbols. Literal/length symbols 286 and 287, distance symbols 30 and 31, the unassigned code of a single one-bit distance code, and a distance beyond the bytes produced so far are refused. Lengths are 3–258, distances 1–32,768, and references may reach into earlier blocks.
  4. The end. The stream ends with the byte holding its last bit; padding bits in that byte are ignored. Any later byte in the extent, including a second stream, is refused.
  5. The length. A stream that would produce more than R bytes is refused when it would (FORMAT for a section or stream, since R is its declared length); one that ends with fewer than R is refused after it ends.
  6. The ratio. DEFLATE spends at least 2 bits on each 258 bytes of output, so R can be at most \(1032 \cdot c\) for a stream of \(c\) bytes. A reader MUST refuse a larger R before it inflates (FORMAT), so that what it allocates stays linear in what it read. It SHOULD also allocate at most \(\min(R,\, 4c + 64)\) bytes before output arrives.

An empty stream is never written: a DAG-block stream of raw length 0 has compressed length 0 and no bytes, and a compressed section's content is never empty (its counts take at least four bytes, and section 10's at least one).

What writers emit

A conforming writer MAY emit any stream that satisfies the rules above. The reference writer emits the output of the Store's encoder at level 6, which is a pure function of the input bytes: a hash of the next three bytes, chains of earlier positions newest first, lazy matching, blocks of at most 16,383 symbols, length-limited Huffman codes by package-merge, and a choice between the exact sizes of the stored, fixed, and dynamic encodings. The rules are in compression, Determinism, and tests/store/reference/store_deflate.py implements them in Python. A writer that uses them is a reproducing writer: its files hash the same as the reference writer's (conformance).

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub