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.
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):
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:
- Blocks. Stored (
BTYPE0, withLENthe ones' complement ofNLEN; its padding bits ignored), fixed Huffman (1), and dynamic Huffman (2), any number of them, the last withBFINALset.BTYPE3 is refused. - Dynamic headers.
HLITat most 286 andHDISTat most 30; a complete code-length code; a repeat code (16) only after a length, and no repeat pastHLIT + HDISTlengths (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. - 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.
- 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.
- The length. A stream that would produce more than
Rbytes is refused when it would (FORMATfor a section or stream, sinceRis its declared length); one that ends with fewer thanRis refused after it ends. - The ratio. DEFLATE spends at least 2 bits on each 258 bytes of output,
so
Rcan be at most \(1032 \cdot c\) for a stream of \(c\) bytes. A reader MUST refuse a largerRbefore 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).