A Brotli stream is a sequence of bits, packed into bytes from the least significant bit up, as in Deflate. Bit patterns below are written as in RFC 7932: read from right to left. This page follows RFC 7932; the commands inside a meta-block are on commands and distances.
Packing bits into bytes
Elements are packed in order of increasing bit number inside each byte, starting with
the least significant bit. Integer values (every element that is not a prefix code) are
packed starting with their least significant bit and are unsigned. Prefix codes are
packed starting with the most significant bit of the code, so printed with the first
byte at the right margin, fixed-width elements read in the usual order and prefix codes
appear bit-reversed. The RFC’s example: the 3-bit value 6, the 4-bit value 2, prefix
code 110, prefix code 10 and the 12-bit value 3628 pack into
the three bytes (byte 2 first) 11100010 11000101 10010110.
A stream is a header and a series of meta-blocks. Each decompresses to 0 to 16,777,216 bytes and the output is their concatenation. The decoder must keep at least the window before the current position. A string reference may reach into earlier meta-blocks (up to the window), but never past the start of the output or the window: any such distance is a static dictionary reference. A reference may overlap the current position (the last two bytes X, Y and a reference of length 5, distance 2 add X Y X Y X). If the stream ends before the last meta-block is complete it is invalid.
Window size
The stream starts with WBITS, 1 to 7 bits. The window is
(1 << WBITS) − 16 bytes.
| WBITS | Bits | WBITS | Bits |
|---|---|---|---|
| 10 | 0100001 | 18 | 0011 |
| 11 | 0110001 | 19 | 0101 |
| 12 | 1000001 | 20 | 0111 |
| 13 | 1010001 | 21 | 1001 |
| 14 | 1100001 | 22 | 1011 |
| 15 | 1110001 | 23 | 1101 |
| 16 | 0 | 24 | 1111 |
| 17 | 0000001 |
0010001 is invalid in RFC 7932. Google’s large-window extension uses it, below.
Large-window extension
Not part of RFC 7932; defined by the google/brotli sources
(c/dec/decode.c, c/common/constants.h, read 2026-11-13). A decoder
accepts it only when asked to (BROTLI_DECODER_PARAM_LARGE_WINDOW); a decoder
without that setting rejects the header as an invalid window size. The stream header then has
14 bits instead of 1 to 7:
- The seven bits
0010001, the entry the table above marks invalid (read in stream order: a 1 bit, a three-bit value 0, then a three-bit value 1). - One reserved bit, which must be 0; 1 is invalid.
- A 6-bit WBITS (least significant bit first), which must be 10 to 30. The window is
(1 << WBITS) − 16bytes, as in RFC 7932. The format allows up to 62 bits in theory; the library’s encoder and decoder support 30 at most (1 GiB less 16 bytes).
The rest of the stream differs in the distance alphabet only:
- Distance codes use
NDIRECT,NPOSTFIXand the extra-bit formulas of the commands page unchanged, but the largest distance code has 62 extra-bit groups instead of 24. The alphabet size that fixes the width of a simple prefix code is16 + NDIRECT + (62 << (NPOSTFIX + 1)), not16 + NDIRECT + (24 << (NPOSTFIX + 1)). The library states that the 62 matters to the file format specifically through the simple prefix code for distances (RFC 7932 section 3.4). - Symbols are restricted to complete groups of
1 << NPOSTFIXcodes whose distances stay at or below0x7FFFFFFC((1 << 31) − 4). The decoder computes the first forbidden symbol fromNPOSTFIXandNDIRECT(the library’sBrotliCalculateDistanceCodeLimit): a distance symbol at or above that limit is invalid and complex prefix codes list code lengths only for the symbols below it. ForNPOSTFIX = 0andNDIRECT = 0the limit is 74 symbols (the last permitted distance is exactly0x7FFFFFFC, computed here from that function’s steps). - A distance that exceeds the position and the window is still a static dictionary reference, as
before; the maximum backward distance is
(1 << WBITS) − 16.
Meta-block header
| Field | Bits | Meaning |
|---|---|---|
ISLAST | 1 | 1 for the last meta-block. |
ISLASTEMPTY | 1 | Only if ISLAST: 1 means the meta-block is empty and the stream ends here; the remaining bits of the last byte must be 0 or the stream is invalid. |
MNIBBLES | 2 | Fixed-length code: 00 → 4, 01 → 5, 10 → 6, 11 → 0 (an empty meta-block that carries metadata, below). |
MLEN − 1 | MNIBBLES × 4 | Uncompressed length of the meta-block, 1 to 16,777,216. With more than 4 nibbles, the last nibble must not be 0. |
ISUNCOMPRESSED | 1 | Only if not ISLAST: 1 means the data is stored. The reader skips to the next byte boundary (the skipped bits must be 0), and MLEN bytes follow. |
Metadata blocks
With MNIBBLES 0: a reserved bit (0), MSKIPBYTES (2 bits,
0 to 3), then MSKIPLEN − 1 in MSKIPBYTES × 8 bits (present
only when MSKIPBYTES is positive, otherwise MSKIPLEN is 0;
with MSKIPBYTES above 1, a last byte of all zeros makes the stream
invalid). Then 0 to 7 fill bits up to a byte boundary, which must be zero, and
MSKIPLEN bytes of metadata. Metadata is not part of the output or of the
sliding window. The reserved bit must be zero.
Compressed meta-block header
Otherwise, after the header comes, in order:
- For each of the three categories, in the order literals (L), insert-and-copy
lengths (I) and distances (D):
NBLTYPES(1 to 256; it must equal the largest block type plus one), and if more than 1, a prefix code for block types (alphabetNBLTYPES + 2), a prefix code for block counts (alphabet 26), and the first block count (code plus extra bits). With one block type, the type is 0 and the block count is taken as 16,777,216. The first block type is always 0; the previous and current block types start as 1 and 0. NPOSTFIX, 2 bits (0 to 3), and the four most significant bits ofNDIRECT(0 to 120): its value is that 4-bit number shifted left byNPOSTFIX. These are the distance code parameters.- A context mode, 2 bits, for each literal block type.
NTREESL(1 to 256) and, if more than 1, the literal context map (64 entries per literal block type).NTREESD(1 to 256) and, if more than 1, the distance context map (4 entries per distance block type).NTREESLprefix codes for literals (256 symbols),NBLTYPESIfor insert-and-copy lengths (704 symbols) andNTREESDfor distances (16 +NDIRECT+ 48 <<NPOSTFIXsymbols).- The commands, until
MLENbytes have been produced.
A command is: a block type and count for the next insert-and-copy block if
NBLTYPESI ≥ 2 and the count has run out; the insert-and-copy length symbol;
the insert length of literals (each preceded by a literal block switch when that count
has run out, and coded with the prefix code CMAPL[64 × block type + context
ID]); then, unless the distance is implicit, a distance block switch when due and
the distance code (prefix code CMAPD[4 × block type + context ID]). The
meta-block ends when the bytes produced equal MLEN; if that is reached after
the insert part of a command, the command’s copy length is ignored and may be any value,
and no distance is read. Exceeding MLEN in any other way is invalid. Block
counts need not reach zero at the end. If the last command does not end on a byte
boundary, the unused bits must be zero.
Counts from 1 to 256
| Value | Bits |
|---|---|
| 1 | 0 |
| 2 | 0001 |
| 3 to 4 | x0011 |
| 5 to 8 | xx0101 |
| 9 to 16 | xxx0111 |
| 17 to 32 | xxxx1001 |
| 33 to 64 | xxxxx1011 |
| 65 to 128 | xxxxxx1101 |
| 129 to 256 | xxxxxxx1111 |
Used for NBLTYPESL, NBLTYPESI, NBLTYPESD,
NTREESL and NTREESD.
Prefix codes
Every Huffman (prefix) code starts with 2 bits, HSKIP.
- Simple code (
HSKIP= 1):NSYM − 1in 2 bits, thenNSYMsymbols (up to four), each inALPHABET_BITSbits, the smallest width that can hold every symbol of the alphabet (8 for literals). A value at or above the alphabet size, or equal to an earlier one, makes the stream invalid. The symbols need not be sorted, but codes of equal length are assigned in sorted symbol order. Code lengths, in the order the symbols appear: 1 symbol, length 0 (no bits are written or read); 2 symbols, 1 and 1; 3 symbols, 1, 2, 2; 4 symbols, a tree-select bit chooses 2, 2, 2, 2 (0) or 1, 2, 3, 3 (1). - Complex code (
HSKIP0, 2 or 3): code lengths of the code length alphabet, in the order 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, skipping the firstHSKIP. Each is written with a fixed code: 0 =00, 1 =0111, 2 =011, 3 =10, 4 =01, 5 =1111. Then the symbol code lengths, coded with that code: 0 to 15 are lengths; 16 repeats the previous non-zero length 3 to 6 times (2 extra bits; if there is no previous non-zero length, 8 is repeated), 17 repeats zero 3 to 10 times (3 extra bits). The previous length starts as 8. A 16 straight after a 16 changes the earlier repeat count: new count = 4 × (old count − 2) + (3 to 6 from the new extra bits); for 17 after 17 it is 8 × (old count − 2) + (3 to 10). Only the final count is used, and a 16 does not modify a 17 count or the reverse. The RFC’s example: lengths 7, 16 (+11), 16 (+10) expand to 22 lengths of 7. A repeat that would exceed the alphabet size is invalid.
Rules for complex codes: the code lengths of the code length alphabet are 0 to 5 (2 to
4 bits each); a trailing zero is omitted once at least two are non-zero, and their
sum of 32 >> length must be 32. The symbol lengths’ sum of
32768 >> length (repeats included) must be 32768; trailing 0 or 17
symbols are omitted, so the last symbol is 1 to 16. A complex code needs at least two
non-zero lengths. If only one code length symbol is non-zero (for example a literal code
of all-equal length 8, written with just symbol 16), that symbol is returned with no
bits. A length of 0 means the symbol is not used.
Canonical codes and alphabet sizes
All prefix codes are canonical: codes of one length are consecutive in symbol order and
shorter codes come first. From the lengths: count the codes of each length
(bl_count[N]); code = 0; for bits = 1..MAX_BITS: code = (code +
bl_count[bits − 1]) << 1; next_code[bits] = code; then each symbol with
non-zero length takes next_code[len]++. The RFC’s example: lengths
3, 3, 3, 3, 3, 2, 4, 4 give A = 010, B = 011, C = 100, D = 101, E = 110, F = 00, G = 1110,
H = 1111.
| Prefix code | Alphabet size |
|---|---|
| Literal | 256 |
| Insert-and-copy length | 704 |
| Distance | 16 + NDIRECT + (48 << NPOSTFIX) |
| Block count | 26 |
| Block type | NBLTYPESx + 2 (x is I, L or D) |
| Context map | NTREESx + RLEMAXx (x is L or D) |
Context modes
A literal’s prefix code is chosen by a context ID (0 to 63) worked out from the two bytes before it, by the mode of its block type:
| Mode | Name | Context from p1 (last byte) and p2 |
|---|---|---|
0 | LSB6 | The low 6 bits of p1. |
1 | MSB6 | p1 >> 2 (the high 6 bits of p1). |
2 | UTF8 | Lut0[p1] | Lut1[p2], a function of p1 and p2 optimised for text. |
3 | Signed | (Lut2[p1] << 3) | Lut2[p2], optimised for sequences of signed integers. |
LSB6 is p1 & 0x3F. p1 and p2 are the last two bytes of the output
(p1 the latest), however they were produced (stored blocks, copies, dictionary words or
literals), and both are zero at the start of the stream. The mode of each literal block
type is stored as 2 bits, in one run of NBLTYPESL entries. A distance’s
context is its copy length: 2 → 0, 3 → 1, 4 → 2, more than 4 → 3.
Lookup tables
Each table has 256 entries, indexed by the byte, in rows of 16. Lengths and CRC-32 of
the bytes: Lut0 256 bytes 0x8E91EFB7, Lut1 0xD01A32F4, Lut2
0x0DD7A0D6 (RFC 7932 section 7.1 and Appendix C).
Lut0
0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,
44, 44, 44, 44, 44, 44,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 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7
Context maps
A context map maps (block type, context ID) to a prefix code index. It is written
as: RLEMAX flag (1 bit; if 1, 4 bits of RLEMAX − 1), a prefix
code over NTREES + RLEMAX symbols, the entries (symbol 0 is a 0; 1 to
RLEMAX are runs of zeros of 2n plus n extra bits; higher
symbols are the value minus RLEMAX), then an IMTF bit: if 1,
undo a move-to-front transform on the entries.
Details from RFC 7932 section 7.3. The map has 64 × NBLTYPESL entries
(literals) or 4 × NBLTYPESD (distances), each 0 to 255, indexed
CMAPL[64 × BTYPE_L + CID_L] and CMAPD[4 × BTYPE_D + CID_D].
NTREES is the highest value plus one, and the values present must be exactly
0 to NTREES − 1. RLEMAX is written in 1 to 5 bits: 0 as a single
0 bit, 1 to 16 as xxxx1 (so 01001 is 5). The alphabet is: 0 =
value 0; k (1 to RLEMAX) = repeat zero 2k to
2k+1 − 1 times with k extra bits; RLEMAX + 1 = value 1,
up to RLEMAX + NTREES − 1 = value NTREES − 1. With
RLEMAX = 0 there is no run-length coding. RLEMAX may be larger
than needed. A run that overshoots the map size is invalid. When NTREES is 1
the map is not stored and holds only zeros. The inverse move-to-front transform:
void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
uint8_t mtf[256]; int i;
for (i = 0; i < 256; ++i) mtf[i] = (uint8_t)i;
for (i = 0; i < v_len; ++i) {
uint8_t index = v[i];
uint8_t value = mtf[index];
v[i] = value;
for (; index; --index) mtf[index] = mtf[index - 1];
mtf[0] = value;
}
}