The data of a compressed meta-block (see stream and meta-blocks) is a series of commands. Each command is an insert-and-copy length code with its extra bits, that many literals, then (unless the meta-block is complete) a distance code with its extra bits. Block switch commands for each category may come before any symbol of that category. This page follows RFC 7932, sections 4 to 6 and 8.
Insert-and-copy length codes
The 704 symbols are arranged in cells of 64. Within a cell, the insert length code
is the cell’s first insert code + (symbol >> 3) & 7, and the copy
length code is its first copy code + symbol & 7.
| Symbols | Insert codes | Copy codes | Distance |
|---|---|---|---|
| 0 to 63 | 0 to 7 | 0 to 7 | Implied code 0 (the last distance) |
| 64 to 127 | 0 to 7 | 8 to 15 | Implied code 0 |
| 128 to 191 | 0 to 7 | 0 to 7 | Coded |
| 192 to 255 | 0 to 7 | 8 to 15 | Coded |
| 256 to 319 | 8 to 15 | 0 to 7 | Coded |
| 320 to 383 | 8 to 15 | 8 to 15 | Coded |
| 384 to 447 | 0 to 7 | 16 to 23 | Coded |
| 448 to 511 | 16 to 23 | 0 to 7 | Coded |
| 512 to 575 | 8 to 15 | 16 to 23 | Coded |
| 576 to 639 | 16 to 23 | 8 to 15 | Coded |
| 640 to 703 | 16 to 23 | 16 to 23 | Coded |
Insert and copy lengths
| Code | Insert: extra bits, lengths | Copy: extra bits, lengths |
|---|---|---|
| 0 | 0, 0 | 0, 2 |
| 1 | 0, 1 | 0, 3 |
| 2 | 0, 2 | 0, 4 |
| 3 | 0, 3 | 0, 5 |
| 4 | 0, 4 | 0, 6 |
| 5 | 0, 5 | 0, 7 |
| 6 | 1, 6–7 | 0, 8 |
| 7 | 1, 8–9 | 0, 9 |
| 8 | 2, 10–13 | 1, 10–11 |
| 9 | 2, 14–17 | 1, 12–13 |
| 10 | 3, 18–25 | 2, 14–17 |
| 11 | 3, 26–33 | 2, 18–21 |
| 12 | 4, 34–49 | 3, 22–29 |
| 13 | 4, 50–65 | 3, 30–37 |
| 14 | 5, 66–97 | 4, 38–53 |
| 15 | 5, 98–129 | 4, 54–69 |
| 16 | 6, 130–193 | 5, 70–101 |
| 17 | 7, 194–321 | 5, 102–133 |
| 18 | 8, 322–577 | 6, 134–197 |
| 19 | 9, 578–1089 | 7, 198–325 |
| 20 | 10, 1090–2113 | 8, 326–581 |
| 21 | 12, 2114–6209 | 9, 582–1093 |
| 22 | 14, 6210–22593 | 10, 1094–2117 |
| 23 | 24, 22594–16799809 | 24, 2118–16779333 |
The insert extra bits come before the copy extra bits.
Block switches and block counts
When a category has more than one block type, a block switch is a block type code and a block count. Block type code 0 means the type before the current one, 1 means current + 1 (wrapping to 0 after the highest type), and n ≥ 2 means type n − 2. The previous and current types start as 1 and 0 at the end of the meta-block header, the first block’s type is 0 and not coded, and a switch is read right before the next element when the count has run out (not after the last element of the previous block). Block counts:
| Code | Extra bits | Range | Code | Extra bits | Range |
|---|---|---|---|---|---|
| 0 | 2 | 1–4 | 13 | 5 | 145–176 |
| 1 | 2 | 5–8 | 14 | 5 | 177–208 |
| 2 | 2 | 9–12 | 15 | 5 | 209–240 |
| 3 | 2 | 13–16 | 16 | 6 | 241–304 |
| 4 | 3 | 17–24 | 17 | 6 | 305–368 |
| 5 | 3 | 25–32 | 18 | 7 | 369–496 |
| 6 | 3 | 33–40 | 19 | 8 | 497–752 |
| 7 | 3 | 41–48 | 20 | 9 | 753–1264 |
| 8 | 4 | 49–64 | 21 | 10 | 1265–2288 |
| 9 | 4 | 65–80 | 22 | 11 | 2289–4336 |
| 10 | 4 | 81–96 | 23 | 12 | 4337–8432 |
| 11 | 4 | 97–112 | 24 | 13 | 8433–16624 |
| 12 | 5 | 113–144 | 25 | 24 | 16625–16793840 |
Distances
The last four distances are kept in a ring buffer, set to 16, 15, 11 and 4 at the start of the stream (4 is the last). Codes 0 to 15 refer to it:
| Code | Distance | Code | Distance |
|---|---|---|---|
| 0 | Last | 8 | Last − 3 |
| 1 | Second to last | 9 | Last + 3 |
| 2 | Third to last | 10 | Second to last − 1 |
| 3 | Fourth to last | 11 | Second to last + 1 |
| 4 | Last − 1 | 12 | Second to last − 2 |
| 5 | Last + 1 | 13 | Second to last + 2 |
| 6 | Last − 2 | 14 | Second to last − 3 |
| 7 | Last + 2 | 15 | Second to last + 3 |
Codes 16 to 15 + NDIRECT are the distances 1 to NDIRECT.
Higher codes have extra bits; with d = code − NDIRECT − 16:
ndistbits = 1 + (d >> (NPOSTFIX + 1))
hcode = d >> NPOSTFIX
lcode = d & ((1 << NPOSTFIX) − 1)
offset = ((2 + (hcode & 1)) << ndistbits) − 4
distance = ((offset + extra) << NPOSTFIX) + lcode + NDIRECT + 1
Distance code 0 does not change the ring buffer; every other distance that is not a
dictionary reference is pushed into it. So “second to last” means the second to last
distance that was not coded as 0. The ring buffer starts at the beginning of the stream
and is not reset at meta-block boundaries. A special code that resolves to zero or a
negative value makes the stream invalid. The special codes and the direct codes carry no
extra bits; the maximum number of extra bits is 24, which gives the alphabet size
16 + NDIRECT + (48 << NPOSTFIX). NPOSTFIX is 0 to 3 and
NDIRECT 0 to 120.
Static dictionary references
A distance larger than the maximum allowed (the smaller of the window size and the bytes produced so far) refers to Brotli’s built-in dictionary of 122,784 bytes: words of 4 to 24 bytes, grouped by length. With copy length len:
word_id = distance − (max allowed distance + 1)
index = word_id & ((1 << NDBITS[len]) − 1)
transform_id = word_id >> NDBITS[len]
The word is transformed by one of 121 transforms (adding a prefix and suffix, changing case, cutting letters from the start or end) and copied to the output. A transform ID of 121 or more is invalid. The transforms are listed below.
| Length | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| NDBITS | 10 | 10 | 11 | 11 | 10 | 10 | 10 | 10 | 10 | 9 | 9 |
| Length | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|---|---|---|---|---|---|---|---|---|---|---|
| NDBITS | 8 | 7 | 7 | 8 | 7 | 7 | 6 | 6 | 5 | 5 |
The static dictionary has three parts: DICT (the bytes),
DOFFSET[0..24] and NDBITS[0..24] (zero for lengths below 4).
NWORDS[len] = 1 << NDBITS[len] for len ≥ 4, else 0;
DOFFSET[0] = 0; DOFFSET[len + 1] = DOFFSET[len] + len ×
NWORDS[len]; DICTSIZE = DOFFSET[24] + 24 × NWORDS[24] (122,784). A
word starts at DOFFSET[len] + index × len. A copy length below 4 or above 24
for a dictionary reference, or a transform ID above 120, is invalid. A dictionary
reference is not pushed to the ring buffer of last distances. The dictionary’s bytes are
listed in hexadecimal in RFC 7932 Appendix A. This site serves them as the raw file
brotli-dictionary.bin:
122,784 bytes, CRC-32 5136CB04, checked byte for byte against the hexadecimal
of Appendix A. The file is the DICT array itself, so word i of length
len is the len bytes at DOFFSET[len] + i × len.
Word transforms
Each transform is prefix + T(word) + suffix, where T is one of
21 elementary transforms: Identity, FermentFirst, FermentAll, OmitFirst1 to 9 (the last
length − k bytes, or empty if the word is shorter than k) and
OmitLast1 to 9 (the first length − k bytes, or empty). OmitFirst8 is not
used in the list. Ferment, as the RFC defines it (pos is the position
examined):
int Ferment(uint8_t* word, int word_len, int pos) {
if (word[pos] < 192) {
if (word[pos] >= 97 && word[pos] <= 122) word[pos] = word[pos] ^ 32;
return 1;
} else if (word[pos] < 224) {
if (pos + 1 < word_len) word[pos + 1] = word[pos + 1] ^ 32;
return 2;
} else {
if (pos + 2 < word_len) word[pos + 2] = word[pos + 2] ^ 5;
return 3;
}
}
FermentFirst: if (word_len > 0) Ferment(word, word_len, 0);
FermentAll: i = 0; while (i < word_len) i += Ferment(word, word_len, i);
A transform adds at most 13 bytes to a word, so a 38-byte buffer holds any result with a
terminating zero. Strings below are in C notation (\", \n,
\t, \xc2\xa0 a no-break space in UTF-8). For the RFC’s check
value, each transform becomes prefix, a zero byte, one byte for the transform (0 Identity,
1 FermentFirst, 2 FermentAll, 3 to 11 OmitFirst1 to 9, 12 to 20 OmitLast1 to 9), suffix and
a zero byte; the 121 sequences joined are 648 bytes with CRC-32 3D965F81.
| ID | Prefix | Transform | Suffix | ID | Prefix | Transform | Suffix |
|---|---|---|---|---|---|---|---|
| 0 | "" | Identity | "" | 61 | "" | Identity | "ly " |
| 1 | "" | Identity | " " | 62 | " the " | Identity | " of " |
| 2 | " " | Identity | " " | 63 | "" | OmitLast5 | "" |
| 3 | "" | OmitFirst1 | "" | 64 | "" | OmitLast9 | "" |
| 4 | "" | FermentFirst | " " | 65 | " " | FermentFirst | ", " |
| 5 | "" | Identity | " the " | 66 | "" | FermentFirst | "\"" |
| 6 | " " | Identity | "" | 67 | "." | Identity | "(" |
| 7 | "s " | Identity | " " | 68 | "" | FermentAll | " " |
| 8 | "" | Identity | " of " | 69 | "" | FermentFirst | "\">" |
| 9 | "" | FermentFirst | "" | 70 | "" | Identity | "=\"" |
| 10 | "" | Identity | " and " | 71 | " " | Identity | "." |
| 11 | "" | OmitFirst2 | "" | 72 | ".com/" | Identity | "" |
| 12 | "" | OmitLast1 | "" | 73 | " the " | Identity | " of the " |
| 13 | ", " | Identity | " " | 74 | "" | FermentFirst | "'" |
| 14 | "" | Identity | ", " | 75 | "" | Identity | ". This " |
| 15 | " " | FermentFirst | " " | 76 | "" | Identity | "," |
| 16 | "" | Identity | " in " | 77 | "." | Identity | " " |
| 17 | "" | Identity | " to " | 78 | "" | FermentFirst | "(" |
| 18 | "e " | Identity | " " | 79 | "" | FermentFirst | "." |
| 19 | "" | Identity | "\"" | 80 | "" | Identity | " not " |
| 20 | "" | Identity | "." | 81 | " " | Identity | "=\"" |
| 21 | "" | Identity | "\">" | 82 | "" | Identity | "er " |
| 22 | "" | Identity | "\n" | 83 | " " | FermentAll | " " |
| 23 | "" | OmitLast3 | "" | 84 | "" | Identity | "al " |
| 24 | "" | Identity | "]" | 85 | " " | FermentAll | "" |
| 25 | "" | Identity | " for " | 86 | "" | Identity | "='" |
| 26 | "" | OmitFirst3 | "" | 87 | "" | FermentAll | "\"" |
| 27 | "" | OmitLast2 | "" | 88 | "" | FermentFirst | ". " |
| 28 | "" | Identity | " a " | 89 | " " | Identity | "(" |
| 29 | "" | Identity | " that " | 90 | "" | Identity | "ful " |
| 30 | " " | FermentFirst | "" | 91 | " " | FermentFirst | ". " |
| 31 | "" | Identity | ". " | 92 | "" | Identity | "ive " |
| 32 | "." | Identity | "" | 93 | "" | Identity | "less " |
| 33 | " " | Identity | ", " | 94 | "" | FermentAll | "'" |
| 34 | "" | OmitFirst4 | "" | 95 | "" | Identity | "est " |
| 35 | "" | Identity | " with " | 96 | " " | FermentFirst | "." |
| 36 | "" | Identity | "'" | 97 | "" | FermentAll | "\">" |
| 37 | "" | Identity | " from " | 98 | " " | Identity | "='" |
| 38 | "" | Identity | " by " | 99 | "" | FermentFirst | "," |
| 39 | "" | OmitFirst5 | "" | 100 | "" | Identity | "ize " |
| 40 | "" | OmitFirst6 | "" | 101 | "" | FermentAll | "." |
| 41 | " the " | Identity | "" | 102 | "\xc2\xa0" | Identity | "" |
| 42 | "" | OmitLast4 | "" | 103 | " " | Identity | "," |
| 43 | "" | Identity | ". The " | 104 | "" | FermentFirst | "=\"" |
| 44 | "" | FermentAll | "" | 105 | "" | FermentAll | "=\"" |
| 45 | "" | Identity | " on " | 106 | "" | Identity | "ous " |
| 46 | "" | Identity | " as " | 107 | "" | FermentAll | ", " |
| 47 | "" | Identity | " is " | 108 | "" | FermentFirst | "='" |
| 48 | "" | OmitLast7 | "" | 109 | " " | FermentFirst | "," |
| 49 | "" | OmitLast1 | "ing " | 110 | " " | FermentAll | "=\"" |
| 50 | "" | Identity | "\n\t" | 111 | " " | FermentAll | ", " |
| 51 | "" | Identity | ":" | 112 | "" | FermentAll | "," |
| 52 | " " | Identity | ". " | 113 | "" | FermentAll | "(" |
| 53 | "" | Identity | "ed " | 114 | "" | FermentAll | ". " |
| 54 | "" | OmitFirst9 | "" | 115 | " " | FermentAll | "." |
| 55 | "" | OmitFirst7 | "" | 116 | "" | FermentAll | "='" |
| 56 | "" | OmitLast6 | "" | 117 | " " | FermentAll | ". " |
| 57 | "" | Identity | "(" | 118 | " " | FermentFirst | "=\"" |
| 58 | "" | FermentFirst | ", " | 119 | " " | FermentAll | "='" |
| 59 | "" | OmitLast8 | "" | 120 | " " | FermentFirst | "='" |
| 60 | "" | Identity | " at " |