A compressed block (block type 2, see blocks) is a literals section followed by a sequences section. The literals are the bytes not found in earlier data; each sequence says how many literals to copy, then how long a match to copy and from how far back. This page follows RFC 8878, sections 3.1.1.3 to 3.1.1.5 and 4 (read for this page).
Literals section
The header’s first byte: bits 0 and 1 are Literals_Block_Type, bits 2
and 3 Size_Format; the sizes follow in the remaining bits, little-endian.
| Type | Name | Content |
|---|---|---|
0 | Raw | The literals as they are. |
1 | RLE | One byte, repeated Regenerated_Size times. |
2 | Compressed | A Huffman tree description, then 1 or 4 Huffman-coded streams. |
3 | Treeless | Huffman-coded streams that reuse the previous block’s tree. |
| Types | Size_Format | Header | Sizes |
|---|---|---|---|
| Raw, RLE | 00 or 10 (1 bit) | 1 byte | Regenerated size: 5 bits |
| Raw, RLE | 01 | 2 bytes | Regenerated size: 12 bits |
| Raw, RLE | 11 | 3 bytes | Regenerated size: 20 bits |
| Compressed, Treeless | 00 | 3 bytes | 1 stream; regenerated and compressed sizes 10 bits each |
| Compressed, Treeless | 01 | 3 bytes | 4 streams; 10 bits each |
| Compressed, Treeless | 10 | 4 bytes | 4 streams; 14 bits each |
| Compressed, Treeless | 11 | 5 bytes | 4 streams; 18 bits each |
The compressed size includes the tree description. With four streams, a 6-byte jump
table (three 2-byte sizes of streams 1 to 3) comes first; stream 4 takes the rest.
Each of the first three streams decodes to (Regenerated_Size + 3) / 4
bytes, and the fourth to what remains.
Huffman tree description
The tree is sent as a weight for each symbol from 0 up to the last one before the
last present symbol; the last present symbol’s weight is not sent but worked out by
completing the sum of 2(weight − 1) (zero weights excluded) to the next power
of two, whose exponent is Max_Number_of_Bits, the depth of the tree.
Symbols above the last present one have weight 0. A weight w > 0 means a
code of Max_Number_of_Bits + 1 − w bits; 0 means the byte does not occur.
Codes are at most 11 bits.
- Header byte below 128: it is the size in bytes of the FSE-compressed weights that follow. One bitstream, two interleaved states sharing one distribution table (maximum accuracy log 6): state 1 gives the even-numbered symbols and state 2 the odd-numbered ones; state 1 is initialised first, then they alternate. The count of weights is found by the end of the stream: if updating a state would need more bits than remain, the missing bits are taken as zero, and the symbols of the two final states are then also decoded. At most 255 weights.
- Header byte 128 or more:
header − 127weights follow directly, 4 bits each, two to a byte, the first in the top 4 bits, occupyingceil(n / 2)bytes. At most 128 weights, so a literal above 128 forces the FSE form.
Prefix codes. Symbols with weight 0 are removed; the rest are sorted by
weight and, within one weight, by symbol value; codes are then handed out in sequence
starting from the lowest weight. The RFC’s example: weights 0:4, 1:3, 2:2, 3:0, 4:1, 5:1
give symbol 4 = 0000, 5 = 0001, 2 = 001, 1 =
01, 0 = 1.
Huffman streams are read backwards. The last byte is never 0: after
the last information bit the writer adds a single 1 bit and fills the byte with 0 bits.
The reader skips those zero bits and that first 1 bit, then reads codes in forward
order from the end, little-endian, taking the highest Max_Number_of_Bits
bits, looking up the symbol and dropping its code length. A stream not consumed exactly
to its first bit is faulty. The RFC’s example: literals “0145” with the code above are
stored as 00010000 00001101.
A Treeless block with no earlier Huffman table in the frame (or dictionary) is corrupt. Block header sizes may use a longer form than needed. The 4-stream sizes are the compressed sizes in the jump table, and if the three sizes exceed the total the data is corrupt.
Sequences section
| Field | Description |
|---|---|
Number_of_Sequences | First byte b0: 0 means no sequences, and the section ends here; below 128, b0; below 255, ((b0 − 128) << 8) + b1; 255, b1 + (b2 << 8) + 7F00. |
Symbol_Compression_Modes | One byte: bits 7–6 literal lengths mode, 5–4 offsets mode, 3–2 match lengths mode, 1–0 reserved (0). |
| Tables | For each of the three, in that order, what its mode needs. |
| Bitstream | The sequences, read backwards like the Huffman streams. |
| Mode | Name | Table |
|---|---|---|
0 | Predefined_Mode | The default distribution; nothing stored. |
1 | RLE_Mode | One byte: the symbol used for all sequences. |
2 | FSE_Compressed_Mode | An FSE table description. Must not be used when only one symbol is present (use RLE_Mode). |
3 | Repeat_Mode | Reuse the table from the previous compressed block with Number_of_Sequences > 0, or from the dictionary for the first block; this includes RLE and Predefined tables. With nothing to repeat it is corrupt data. |
Decoding: the bitstream is read backwards (start at the last byte of the block, skip
the zero padding and the first 1 bit). It begins with the initial states, each
Accuracy_Log bits: literal lengths, then offsets, then match lengths. Each
sequence is then decoded, first to last: the codes come from the states; read the
offset extra bits, then the match length extra bits, then the literal length extra bits;
then, unless it was the last sequence, update the states in the order literal lengths,
match lengths, offsets. At the end the stream must be exactly consumed or it is corrupt.
The highest accuracy logs allowed are 9 for literal and match lengths and 8 for offsets.
If Number_of_Sequences is 0 the block’s content is only the literals, and
the tables kept for Repeat_Mode are not updated. Literals left after the last sequence
are added at the end of the block. The sequences section size is the block size minus
the literals section header and content.
FSE tables
FSE (Finite State Entropy, based on ANS) decodes with a table whose size is a power of
two (1 << Accuracy_Log); each cell holds Symbol,
Num_Bits and Baseline. The initial state is
Accuracy_Log bits read little-endian; the next state is
Baseline + Num_Bits bits read. FSE streams are read from the end to the
beginning; the bit order inside is not reversed.
Table description
Read forward, little-endian, byte-padded (spare bits of the last byte are unused).
Accuracy_Log = (low 4 bits of the first byte) + 5. Then one value per
symbol from 0 to the last present symbol, with at least two symbols of nonzero
probability. Each value’s width depends on the probability points still unallocated
(R): the field can take 0 to R + 1, so it needs
floor(log2(R + 1)) + 1 bits, and small values use one bit fewer.
In the RFC’s example (accuracy log 8, 100 points used, values 0 to 157 possible) the 8-bit
raw value 0 to 97 decodes to itself in 7 bits, 98 to 127 in 8 bits as themselves, 128 to
225 as 0 to 97 (7 bits), and 226 to 255 as 128 to 157 (8 bits). The probability is the
decoded value − 1, so 0 means −1, “less than 1”, which counts as 1 point.
A symbol with probability 0 is followed by a 2-bit repeat flag giving how many further
zero-probability symbols follow (0 to 3); a 3 is followed by another flag, and so on.
Reading stops when the total reaches 1 << Accuracy_Log; going over
it is corrupt. The number of symbols must equal what the context expects (never more
than 256).
Building the decoding table
- Symbols with probability −1 each take one cell, from the end of the table backwards,
in symbol order. They do a full state reset, reading
Accuracy_Logbits. - The other symbols, from symbol 0 and position 0, take as many cells as their
probability, spread:
position += (tableSize >> 1) + (tableSize >> 3) + 3; position &= tableSize − 1;skipping positions already taken by −1 symbols. The position does not reset between symbols. - For each symbol, sort its states in natural order. Round its number of states up to the next power of two; the lowest states get double width and need one more bit. Baselines are assigned starting from the higher states (which use fewer bits), then resuming at the first state, each taking its width from the baseline.
The RFC’s example: a symbol with 5 states at accuracy log 7 has widths 32, 32, 32, 16, 16, bits 5, 5, 5, 4, 4, and baselines 32, 64, 96, 0, 16 (ranges 32–63, 64–95, 96–127, 0–15, 16–31). The RFC’s Appendix A lists the resulting tables for the default distributions; they are not reproduced here.
Literal length codes
Codes run 0 to 35 and give lengths 0 to 131,071. Codes 0 to 15 are the length itself, with no extra bits. Above that, length = baseline + the extra bits (read little-endian):
| Code | Baseline | Extra bits | Code | Baseline | Extra bits |
|---|---|---|---|---|---|
| 16 | 16 | 1 | 26 | 128 | 7 |
| 17 | 18 | 1 | 27 | 256 | 8 |
| 18 | 20 | 1 | 28 | 512 | 9 |
| 19 | 22 | 1 | 29 | 1024 | 10 |
| 20 | 24 | 2 | 30 | 2048 | 11 |
| 21 | 28 | 2 | 31 | 4096 | 12 |
| 22 | 32 | 3 | 32 | 8192 | 13 |
| 23 | 40 | 3 | 33 | 16384 | 14 |
| 24 | 48 | 4 | 34 | 32768 | 15 |
| 25 | 64 | 6 | 35 | 65536 | 16 |
Match length codes
Codes run 0 to 52 and give lengths 3 to 131,074. Codes 0 to 31 mean lengths 3 to 34 (code + 3), with no extra bits. Above that:
| Code | Baseline | Extra bits | Code | Baseline | Extra bits |
|---|---|---|---|---|---|
| 32 | 35 | 1 | 43 | 131 | 7 |
| 33 | 37 | 1 | 44 | 259 | 8 |
| 34 | 39 | 1 | 45 | 515 | 9 |
| 35 | 41 | 1 | 46 | 1027 | 10 |
| 36 | 43 | 2 | 47 | 2051 | 11 |
| 37 | 47 | 2 | 48 | 4099 | 12 |
| 38 | 51 | 3 | 49 | 8195 | 13 |
| 39 | 59 | 3 | 50 | 16387 | 14 |
| 40 | 67 | 4 | 51 | 32771 | 15 |
| 41 | 83 | 4 | 52 | 65539 | 16 |
| 42 | 99 | 5 |
Offset codes and repeat offsets
Offset code N gives Offset_Value = (1 << N) + N
extra bits (little-endian). A decoder may limit its maximum code; the RFC recommends at
least 22, and the reference decoder supports 31. For a maximum code N the
largest Offset_Value is 2N+1 − 1, a distance of up to
2N+1 − 4, further limited by the window (RFC section 3.1.1.3.2.1.1). Values above 3 are a new offset of
Offset_Value − 3, limited by the window. Values 1 to 3 pick a repeat
offset:
| Offset_Value | Literal length > 0 | Literal length = 0 |
|---|---|---|
| 1 | Repeat offset 1 | Repeat offset 2 |
| 2 | Repeat offset 2 | Repeat offset 3 |
| 3 | Repeat offset 3 | Repeat offset 1 − 1 |
The three repeat offsets start as 1, 4 and 8 in each frame (or come from the dictionary); each block starts with the offsets left by the most recent compressed block (raw and RLE blocks are skipped). After each sequence they are updated: if the offset value did not name a repeat offset (above 3, or 3 with literal length 0), all shift back one and offset 1 takes the offset just used; otherwise (1 or 2, or 3 with a literal length above 0) the used one moves to the front and those before it move back one place.
The RFC’s worked example (table 18), starting from 1, 4, 8:
| Offset_Value | Literal length | Offset 1 | Offset 2 | Offset 3 | Comment |
|---|---|---|---|---|---|
| 1114 | 11 | 1111 | 1 | 4 | not a repeat |
| 1 | 22 | 1111 | 1 | 4 | repeat 1; no change |
| 2225 | 22 | 2222 | 1111 | 1 | not a repeat |
| 1114 | 111 | 1111 | 2222 | 1111 | not a repeat |
| 3336 | 33 | 3333 | 1111 | 2222 | not a repeat |
| 2 | 22 | 1111 | 3333 | 2222 | repeat 2; swap 1 and 2 |
| 3 | 33 | 2222 | 1111 | 3333 | repeat 3; rotate 3 to 1 |
| 1 | 0 | 2221 | 2222 | 1111 | the RFC’s comment is “insert resolved offset”; the result, 2222 − 1, is what value 3 gives with a literal length of 0, so the value 1 printed here looks like an error in the RFC |
| 1 | 0 | 2222 | 2221 | 3333 | repeat 2 |
Sequence execution: copy literals_length bytes from the literals to the
output, then match_length bytes from offset bytes back,
counted from the position after the literals (an offset of 6 and length 3 copy 3 bytes
from 6 back).
Default distributions
Normalised counts used by Predefined_Mode, where −1 marks a “less than 1” probability:
- Literal lengths, accuracy log 6: 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, −1, −1, −1, −1.
- Match lengths, accuracy log 6: 1, 4, 3, 2, 2, 2, 2, 2, 2, then 37 ones, then seven −1.
- Offsets, accuracy log 5 (29 entries, so code 28 at most, offset values up to 536,870,908; a larger offset cannot use this table): 1, 1, 1, 1, 1, 1, 2, 2, 2, then 15 ones, then five −1.