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), and n ≥ 2 means type n − 2. 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.
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.
| 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 dictionary and the transform list are in the RFC’s appendices; the dictionary’s
CRC-32 is 5136CB04.