Deflate is the compression method inside gzip and zlib (and so PNG), defined in RFC 1951 (L. Peter Deutsch, May 1996, informational; read in full this session, with RFC 1950 for the zlib wrapper; the explorer’s ZIP and PDF uses are on those formats’ pages). It combines two things. Repeated data is replaced by a copy of what came earlier (LZ77), and the symbols that result are then coded with Huffman codes, so that common symbols take few bits. The explorer shows every part below for a file dropped on this site: pick the compressed data, then Show.
Blocks
A Deflate stream is a sequence of blocks. Bits are packed starting from the lowest bit of each byte. Numbers are read the same way, lowest bit first; Huffman codes are the one exception, and go from their most significant bit first, so that a decoder can read them one bit at a time.
Each block starts with three bits:
| Bits | Name | Meaning |
|---|---|---|
| 1 | BFINAL | set if and only if this is the last block of the data set |
| 2 | BTYPE | 0 stored (“no compression”), 1 fixed Huffman codes, 2 dynamic Huffman codes, 3 reserved (an error) |
The header bits do not necessarily begin on a byte boundary, since a block need not occupy a whole number of bytes. The two compressed kinds differ only in how the Huffman codes of the literal/length and distance alphabets are defined. Packing, from RFC 1951 3.1.1: data elements go into bytes starting with the least significant bit of the byte; elements other than Huffman codes are packed starting with their own least significant bit; Huffman codes are packed starting with the code’s most significant bit.
A stored block skips the bits up to the next byte boundary (they are ignored), then holds LEN, a 16-bit count of the data bytes, NLEN, its one's complement, and LEN bytes as they are (all 2-byte numbers are little-endian, as in the packing rule). It is how Deflate avoids making incompressible data larger; at most 65,535 bytes fit in one (our reading of the 16-bit LEN).
A reference may point into an earlier block: the distance can cross block boundaries, but cannot refer past the beginning of the output (an application with a preset dictionary may discard part of the output and a distance can still refer to it). The referenced string may overlap the current position: after the last two bytes X and Y, length 5 with distance 2 adds X, Y, X, Y, X. A compressor may limit these ranges further (a shorter window, smaller blocks) and stay compliant; a compliant decompressor must accept the full ranges and blocks of any size.
Literals, lengths and distances
A compressed block is a run of tokens. The merged alphabet of literal and length symbols is 0 to 285 (the fixed code also assigns 286 and 287, which never occur in the data): 0 to 255 are literal bytes, 256 ends the block, and 257 to 285 start a copy. A copy repeats earlier output, from a distance back of up to 32,768 bytes, for a length of 3 to 258 bytes. The copy may overlap its own output, so a run of one byte is a copy from distance 1.
Length symbols and distance symbols give a range. Extra bits, sent straight after the symbol, pick the exact value within that range.
| Length symbol | Lengths | Extra bits |
|---|---|---|
| 257 to 264 | 3 to 10 | 0 |
| 265 to 268 | 11 to 18 | 1 |
| 269 to 272 | 19 to 34 | 2 |
| 273 to 276 | 35 to 66 | 3 |
| 277 to 280 | 67 to 130 | 4 |
| 281 to 284 | 131 to 257 | 5 |
| 285 | 258 | 0 |
The same table symbol by symbol (RFC 1951 3.2.5): 257: 3, 258: 4, 259: 5, 260: 6, 261: 7, 262: 8, 263: 9, 264: 10 (0 extra bits); 265: 11-12, 266: 13-14, 267: 15-16, 268: 17-18 (1 bit); 269: 19-22, 270: 23-26, 271: 27-30, 272: 31-34 (2 bits); 273: 35-42, 274: 43-50, 275: 51-58, 276: 59-66 (3 bits); 277: 67-82, 278: 83-98, 279: 99-114, 280: 115-130 (4 bits); 281: 131-162, 282: 163-194, 283: 195-226, 284: 227-257 (5 bits); 285: 258 (0 bits). The RFC says the extra bits “should be interpreted as a machine integer stored with the most-significant bit first, e.g., bits 1110 represent the value 14”.
After a length symbol comes a distance symbol, 0 to 29, with up to 13 extra bits: symbols 0 to 3 are the distances 1 to 4 with no extra bits, and from there each pair of symbols doubles the range and adds one extra bit, up to 24,577 to 32,768 for symbol 29. The full table:
| Symbol | Extra bits | Distances | Symbol | Extra bits | Distances | Symbol | Extra bits | Distances |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 10 | 4 | 33-48 | 20 | 9 | 1025-1536 |
| 1 | 0 | 2 | 11 | 4 | 49-64 | 21 | 9 | 1537-2048 |
| 2 | 0 | 3 | 12 | 5 | 65-96 | 22 | 10 | 2049-3072 |
| 3 | 0 | 4 | 13 | 5 | 97-128 | 23 | 10 | 3073-4096 |
| 4 | 1 | 5-6 | 14 | 6 | 129-192 | 24 | 11 | 4097-6144 |
| 5 | 1 | 7-8 | 15 | 6 | 193-256 | 25 | 11 | 6145-8192 |
| 6 | 2 | 9-12 | 16 | 7 | 257-384 | 26 | 12 | 8193-12288 |
| 7 | 2 | 13-16 | 17 | 7 | 385-512 | 27 | 12 | 12289-16384 |
| 8 | 3 | 17-24 | 18 | 8 | 513-768 | 28 | 13 | 16385-24576 |
| 9 | 3 | 25-32 | 19 | 8 | 769-1024 | 29 | 13 | 24577-32768 |
The decoding loop, from the RFC: read a block header; for a stored block copy LEN bytes; otherwise (reading the code trees first when dynamic) decode literal/length values until 256, copying literals, and for 257 to 285 decode a distance and copy that many bytes from that far back; repeat until the last block.
Fixed Huffman codes
A fixed block uses codes every decoder knows, so it needs no table:
| Symbols | Code length | Codes |
|---|---|---|
| 0 to 143 | 8 bits | 00110000 to 10111111 |
| 144 to 255 | 9 bits | 110010000 to 111111111 |
| 256 to 279 | 7 bits | 0000000 to 0010111 |
| 280 to 287 | 8 bits | 11000000 to 11000111 |
Distance symbols are all 5 bits in fixed blocks: codes 0 to 31, with the extra bits of the table above. Literal/length values 286 and 287 and distance codes 30 and 31 never occur in the data, but the first two take part in building the code.
Dynamic Huffman codes
The two rules for every Huffman code in Deflate: all codes of one bit length have consecutive values, in the order of the symbols they stand for, and shorter codes precede longer ones.
So the code lengths alone define the codes. The algorithm (RFC 1951 3.2.2): count the codes of each length, bl_count[N]; for bits from 1 to the maximum, code = (code + bl_count[bits-1]) << 1 (with bl_count[0] = 0) gives next_code[bits]; then give each symbol in order that has a non-zero length the value next_code[len] and add one to it. Symbols with length 0 get no code.
For example, lengths (3, 3, 3, 3, 3, 2, 4, 4) for A to H give A = 010, B = 011, C = 100, D = 101, E = 110, F = 00, G = 1110, H = 1111.
A dynamic block sends its own code lengths first. The codes themselves are canonical: given only the length of every symbol's code, the decoder assigns the codes in order, shorter codes first and, within a length, in symbol order. Only the lengths are sent, and even those are compressed, with a third, small Huffman code. The header is:
| Bits | Name | Meaning |
|---|---|---|
| 5 | HLIT | literal and length codes sent, less 257 (so 257 to 286) |
| 5 | HDIST | distance codes sent, less 1 (so 1 to 32) |
| 4 | HCLEN | code length codes sent, less 4 (so 4 to 19) |
| HCLEN + 4 fields of 3 bits | lengths of the code length code, as integers 0 to 7 (0 means the symbol is not used), in this order: 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | |
| variable | HLIT + 257 literal/length code lengths, then HDIST + 1 distance code lengths, in the code length code |
A code length symbol from 0 to 15 is that code length. Symbol 16 repeats the previous length 3 to 6 times (2 extra bits), 17 repeats zero 3 to 10 times (3 extra bits), and 18 repeats zero 11 to 138 times (7 extra bits). The repeat count is the extra bits plus 3 for 16 and 17 and plus 11 for 18 (0 = 3 for symbol 16; the RFC’s example: lengths 8, then 16 with bits 11, then 16 with bits 10, expand to 12 lengths of 8, that is 1 + 6 + 5). The repeat codes can cross from the literal/length lengths into the distance lengths: all the code lengths form one sequence of HLIT + HDIST + 258 values. A length of 0 means the symbol does not occur in the block and does not take part in building the code. If only one distance code is used it is encoded with one bit, not zero: a single code length of one, with one unused code. One distance code of zero bits means there are no distance codes at all, the data being all literals. The compressed data of the block follows, ending with symbol 256 coded in the literal/length code. Maximum code lengths are not stated as numbers in the RFC’s text sections read; the code length alphabet (0 to 15) allows lengths up to 15 for the other two codes, and the 3-bit field allows up to 7 for the code length code (our derivation).
The zlib wrapper
A zlib stream (RFC 1950, May 1996, Deutsch and Gailly) is Deflate data with a two-byte header and a checksum. The RFC names gzip and PNG as users of Deflate; gzip has its own header and trailer (see the overview) and uses raw Deflate. Data after the ADLER32 is not part of the zlib stream.
| Byte | Name | Meaning |
|---|---|---|
| 0 | CMF | bits 0 to 3 CM, the method: 8 is Deflate with a window up to 32K, 15 is reserved (it might indicate an extra field before the data in a future version); bits 4 to 7 CINFO: for CM 8, the base-2 logarithm of the LZ77 window size minus 8 (7 means 32K); values above 7 are not allowed; undefined for other methods |
| 1 | FLG | bits 0 to 4 FCHECK: make CMF × 256 + FLG (CMF first, MSB order) a multiple of 31; bit 5 FDICT: a preset dictionary is used; bits 6 and 7 FLEVEL: 0 fastest, 1 fast, 2 default, 3 maximum compression and slowest (not needed to decompress) |
| 2 to 5, if FDICT | DICTID | the Adler-32 of the preset dictionary (which is fed to the compressor with no output), so the decompressor can tell which dictionary was used |
| after the Deflate data | ADLER32 | Adler-32 of the uncompressed data (not of the dictionary): s1 is the sum of all bytes, starting at 1, s2 the sum of all s1 values, starting at 0, both modulo 65521; stored as s2 × 65536 + s1, most significant byte first |
Compliance (RFC 1950 2.3): a decompressor must check CMF, FLG and ADLER32 and report an error if any is wrong; must report an error if CM is not 8; must report an error if FDICT is set and DICTID is not a known dictionary (and, when the zlib format is part of a format that does not use preset dictionaries, must reject any stream with FDICT set); it may ignore FLEVEL.
Looking at it in the explorer
Open a gzip, ZIP, PNG or PDF file. The compressed data has a Show button, which opens the stream’s summary, its blocks, every token with its bit position, and a picture of what each output byte cost. Blocks open to show their code lengths and codes. Long streams are limited to the first 2 million tokens and 200,000 blocks, and the screen says so.