Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Network Working Group P. Deutsch Chris@43: Request for Comments: 1951 Aladdin Enterprises Chris@43: Category: Informational May 1996 Chris@43: Chris@43: Chris@43: DEFLATE Compressed Data Format Specification version 1.3 Chris@43: Chris@43: Status of This Memo Chris@43: Chris@43: This memo provides information for the Internet community. This memo Chris@43: does not specify an Internet standard of any kind. Distribution of Chris@43: this memo is unlimited. Chris@43: Chris@43: IESG Note: Chris@43: Chris@43: The IESG takes no position on the validity of any Intellectual Chris@43: Property Rights statements contained in this document. Chris@43: Chris@43: Notices Chris@43: Chris@43: Copyright (c) 1996 L. Peter Deutsch Chris@43: Chris@43: Permission is granted to copy and distribute this document for any Chris@43: purpose and without charge, including translations into other Chris@43: languages and incorporation into compilations, provided that the Chris@43: copyright notice and this notice are preserved, and that any Chris@43: substantive changes or deletions from the original are clearly Chris@43: marked. Chris@43: Chris@43: A pointer to the latest version of this and related documentation in Chris@43: HTML format can be found at the URL Chris@43: . Chris@43: Chris@43: Abstract Chris@43: Chris@43: This specification defines a lossless compressed data format that Chris@43: compresses data using a combination of the LZ77 algorithm and Huffman Chris@43: coding, with efficiency comparable to the best currently available Chris@43: general-purpose compression methods. The data can be produced or Chris@43: consumed, even for an arbitrarily long sequentially presented input Chris@43: data stream, using only an a priori bounded amount of intermediate Chris@43: storage. The format can be implemented readily in a manner not Chris@43: covered by patents. Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 1] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: Table of Contents Chris@43: Chris@43: 1. Introduction ................................................... 2 Chris@43: 1.1. Purpose ................................................... 2 Chris@43: 1.2. Intended audience ......................................... 3 Chris@43: 1.3. Scope ..................................................... 3 Chris@43: 1.4. Compliance ................................................ 3 Chris@43: 1.5. Definitions of terms and conventions used ................ 3 Chris@43: 1.6. Changes from previous versions ............................ 4 Chris@43: 2. Compressed representation overview ............................. 4 Chris@43: 3. Detailed specification ......................................... 5 Chris@43: 3.1. Overall conventions ....................................... 5 Chris@43: 3.1.1. Packing into bytes .................................. 5 Chris@43: 3.2. Compressed block format ................................... 6 Chris@43: 3.2.1. Synopsis of prefix and Huffman coding ............... 6 Chris@43: 3.2.2. Use of Huffman coding in the "deflate" format ....... 7 Chris@43: 3.2.3. Details of block format ............................. 9 Chris@43: 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11 Chris@43: 3.2.5. Compressed blocks (length and distance codes) ...... 11 Chris@43: 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12 Chris@43: 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13 Chris@43: 3.3. Compliance ............................................... 14 Chris@43: 4. Compression algorithm details ................................. 14 Chris@43: 5. References .................................................... 16 Chris@43: 6. Security Considerations ....................................... 16 Chris@43: 7. Source code ................................................... 16 Chris@43: 8. Acknowledgements .............................................. 16 Chris@43: 9. Author's Address .............................................. 17 Chris@43: Chris@43: 1. Introduction Chris@43: Chris@43: 1.1. Purpose Chris@43: Chris@43: The purpose of this specification is to define a lossless Chris@43: compressed data format that: Chris@43: * Is independent of CPU type, operating system, file system, Chris@43: and character set, and hence can be used for interchange; Chris@43: * Can be produced or consumed, even for an arbitrarily long Chris@43: sequentially presented input data stream, using only an a Chris@43: priori bounded amount of intermediate storage, and hence Chris@43: can be used in data communications or similar structures Chris@43: such as Unix filters; Chris@43: * Compresses data with efficiency comparable to the best Chris@43: currently available general-purpose compression methods, Chris@43: and in particular considerably better than the "compress" Chris@43: program; Chris@43: * Can be implemented readily in a manner not covered by Chris@43: patents, and hence can be practiced freely; Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 2] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: * Is compatible with the file format produced by the current Chris@43: widely used gzip utility, in that conforming decompressors Chris@43: will be able to read data produced by the existing gzip Chris@43: compressor. Chris@43: Chris@43: The data format defined by this specification does not attempt to: Chris@43: Chris@43: * Allow random access to compressed data; Chris@43: * Compress specialized data (e.g., raster graphics) as well Chris@43: as the best currently available specialized algorithms. Chris@43: Chris@43: A simple counting argument shows that no lossless compression Chris@43: algorithm can compress every possible input data set. For the Chris@43: format defined here, the worst case expansion is 5 bytes per 32K- Chris@43: byte block, i.e., a size increase of 0.015% for large data sets. Chris@43: English text usually compresses by a factor of 2.5 to 3; Chris@43: executable files usually compress somewhat less; graphical data Chris@43: such as raster images may compress much more. Chris@43: Chris@43: 1.2. Intended audience Chris@43: Chris@43: This specification is intended for use by implementors of software Chris@43: to compress data into "deflate" format and/or decompress data from Chris@43: "deflate" format. Chris@43: Chris@43: The text of the specification assumes a basic background in Chris@43: programming at the level of bits and other primitive data Chris@43: representations. Familiarity with the technique of Huffman coding Chris@43: is helpful but not required. Chris@43: Chris@43: 1.3. Scope Chris@43: Chris@43: The specification specifies a method for representing a sequence Chris@43: of bytes as a (usually shorter) sequence of bits, and a method for Chris@43: packing the latter bit sequence into bytes. Chris@43: Chris@43: 1.4. Compliance Chris@43: Chris@43: Unless otherwise indicated below, a compliant decompressor must be Chris@43: able to accept and decompress any data set that conforms to all Chris@43: the specifications presented here; a compliant compressor must Chris@43: produce data sets that conform to all the specifications presented Chris@43: here. Chris@43: Chris@43: 1.5. Definitions of terms and conventions used Chris@43: Chris@43: Byte: 8 bits stored or transmitted as a unit (same as an octet). Chris@43: For this specification, a byte is exactly 8 bits, even on machines Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 3] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: which store a character on a number of bits different from eight. Chris@43: See below, for the numbering of bits within a byte. Chris@43: Chris@43: String: a sequence of arbitrary bytes. Chris@43: Chris@43: 1.6. Changes from previous versions Chris@43: Chris@43: There have been no technical changes to the deflate format since Chris@43: version 1.1 of this specification. In version 1.2, some Chris@43: terminology was changed. Version 1.3 is a conversion of the Chris@43: specification to RFC style. Chris@43: Chris@43: 2. Compressed representation overview Chris@43: Chris@43: A compressed data set consists of a series of blocks, corresponding Chris@43: to successive blocks of input data. The block sizes are arbitrary, Chris@43: except that non-compressible blocks are limited to 65,535 bytes. Chris@43: Chris@43: Each block is compressed using a combination of the LZ77 algorithm Chris@43: and Huffman coding. The Huffman trees for each block are independent Chris@43: of those for previous or subsequent blocks; the LZ77 algorithm may Chris@43: use a reference to a duplicated string occurring in a previous block, Chris@43: up to 32K input bytes before. Chris@43: Chris@43: Each block consists of two parts: a pair of Huffman code trees that Chris@43: describe the representation of the compressed data part, and a Chris@43: compressed data part. (The Huffman trees themselves are compressed Chris@43: using Huffman encoding.) The compressed data consists of a series of Chris@43: elements of two types: literal bytes (of strings that have not been Chris@43: detected as duplicated within the previous 32K input bytes), and Chris@43: pointers to duplicated strings, where a pointer is represented as a Chris@43: pair . The representation used in the Chris@43: "deflate" format limits distances to 32K bytes and lengths to 258 Chris@43: bytes, but does not limit the size of a block, except for Chris@43: uncompressible blocks, which are limited as noted above. Chris@43: Chris@43: Each type of value (literals, distances, and lengths) in the Chris@43: compressed data is represented using a Huffman code, using one code Chris@43: tree for literals and lengths and a separate code tree for distances. Chris@43: The code trees for each block appear in a compact form just before Chris@43: the compressed data for that block. Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 4] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: 3. Detailed specification Chris@43: Chris@43: 3.1. Overall conventions In the diagrams below, a box like this: Chris@43: Chris@43: +---+ Chris@43: | | <-- the vertical bars might be missing Chris@43: +---+ Chris@43: Chris@43: represents one byte; a box like this: Chris@43: Chris@43: +==============+ Chris@43: | | Chris@43: +==============+ Chris@43: Chris@43: represents a variable number of bytes. Chris@43: Chris@43: Bytes stored within a computer do not have a "bit order", since Chris@43: they are always treated as a unit. However, a byte considered as Chris@43: an integer between 0 and 255 does have a most- and least- Chris@43: significant bit, and since we write numbers with the most- Chris@43: significant digit on the left, we also write bytes with the most- Chris@43: significant bit on the left. In the diagrams below, we number the Chris@43: bits of a byte so that bit 0 is the least-significant bit, i.e., Chris@43: the bits are numbered: Chris@43: Chris@43: +--------+ Chris@43: |76543210| Chris@43: +--------+ Chris@43: Chris@43: Within a computer, a number may occupy multiple bytes. All Chris@43: multi-byte numbers in the format described here are stored with Chris@43: the least-significant byte first (at the lower memory address). Chris@43: For example, the decimal number 520 is stored as: Chris@43: Chris@43: 0 1 Chris@43: +--------+--------+ Chris@43: |00001000|00000010| Chris@43: +--------+--------+ Chris@43: ^ ^ Chris@43: | | Chris@43: | + more significant byte = 2 x 256 Chris@43: + less significant byte = 8 Chris@43: Chris@43: 3.1.1. Packing into bytes Chris@43: Chris@43: This document does not address the issue of the order in which Chris@43: bits of a byte are transmitted on a bit-sequential medium, Chris@43: since the final data format described here is byte- rather than Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 5] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: bit-oriented. However, we describe the compressed block format Chris@43: in below, as a sequence of data elements of various bit Chris@43: lengths, not a sequence of bytes. We must therefore specify Chris@43: how to pack these data elements into bytes to form the final Chris@43: compressed byte sequence: Chris@43: Chris@43: * Data elements are packed into bytes in order of Chris@43: increasing bit number within the byte, i.e., starting Chris@43: with the least-significant bit of the byte. Chris@43: * Data elements other than Huffman codes are packed Chris@43: starting with the least-significant bit of the data Chris@43: element. Chris@43: * Huffman codes are packed starting with the most- Chris@43: significant bit of the code. Chris@43: Chris@43: In other words, if one were to print out the compressed data as Chris@43: a sequence of bytes, starting with the first byte at the Chris@43: *right* margin and proceeding to the *left*, with the most- Chris@43: significant bit of each byte on the left as usual, one would be Chris@43: able to parse the result from right to left, with fixed-width Chris@43: elements in the correct MSB-to-LSB order and Huffman codes in Chris@43: bit-reversed order (i.e., with the first bit of the code in the Chris@43: relative LSB position). Chris@43: Chris@43: 3.2. Compressed block format Chris@43: Chris@43: 3.2.1. Synopsis of prefix and Huffman coding Chris@43: Chris@43: Prefix coding represents symbols from an a priori known Chris@43: alphabet by bit sequences (codes), one code for each symbol, in Chris@43: a manner such that different symbols may be represented by bit Chris@43: sequences of different lengths, but a parser can always parse Chris@43: an encoded string unambiguously symbol-by-symbol. Chris@43: Chris@43: We define a prefix code in terms of a binary tree in which the Chris@43: two edges descending from each non-leaf node are labeled 0 and Chris@43: 1 and in which the leaf nodes correspond one-for-one with (are Chris@43: labeled with) the symbols of the alphabet; then the code for a Chris@43: symbol is the sequence of 0's and 1's on the edges leading from Chris@43: the root to the leaf labeled with that symbol. For example: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 6] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: /\ Symbol Code Chris@43: 0 1 ------ ---- Chris@43: / \ A 00 Chris@43: /\ B B 1 Chris@43: 0 1 C 011 Chris@43: / \ D 010 Chris@43: A /\ Chris@43: 0 1 Chris@43: / \ Chris@43: D C Chris@43: Chris@43: A parser can decode the next symbol from an encoded input Chris@43: stream by walking down the tree from the root, at each step Chris@43: choosing the edge corresponding to the next input bit. Chris@43: Chris@43: Given an alphabet with known symbol frequencies, the Huffman Chris@43: algorithm allows the construction of an optimal prefix code Chris@43: (one which represents strings with those symbol frequencies Chris@43: using the fewest bits of any possible prefix codes for that Chris@43: alphabet). Such a code is called a Huffman code. (See Chris@43: reference [1] in Chapter 5, references for additional Chris@43: information on Huffman codes.) Chris@43: Chris@43: Note that in the "deflate" format, the Huffman codes for the Chris@43: various alphabets must not exceed certain maximum code lengths. Chris@43: This constraint complicates the algorithm for computing code Chris@43: lengths from symbol frequencies. Again, see Chapter 5, Chris@43: references for details. Chris@43: Chris@43: 3.2.2. Use of Huffman coding in the "deflate" format Chris@43: Chris@43: The Huffman codes used for each alphabet in the "deflate" Chris@43: format have two additional rules: Chris@43: Chris@43: * All codes of a given bit length have lexicographically Chris@43: consecutive values, in the same order as the symbols Chris@43: they represent; Chris@43: Chris@43: * Shorter codes lexicographically precede longer codes. Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 7] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: We could recode the example above to follow this rule as Chris@43: follows, assuming that the order of the alphabet is ABCD: Chris@43: Chris@43: Symbol Code Chris@43: ------ ---- Chris@43: A 10 Chris@43: B 0 Chris@43: C 110 Chris@43: D 111 Chris@43: Chris@43: I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are Chris@43: lexicographically consecutive. Chris@43: Chris@43: Given this rule, we can define the Huffman code for an alphabet Chris@43: just by giving the bit lengths of the codes for each symbol of Chris@43: the alphabet in order; this is sufficient to determine the Chris@43: actual codes. In our example, the code is completely defined Chris@43: by the sequence of bit lengths (2, 1, 3, 3). The following Chris@43: algorithm generates the codes as integers, intended to be read Chris@43: from most- to least-significant bit. The code lengths are Chris@43: initially in tree[I].Len; the codes are produced in Chris@43: tree[I].Code. Chris@43: Chris@43: 1) Count the number of codes for each code length. Let Chris@43: bl_count[N] be the number of codes of length N, N >= 1. Chris@43: Chris@43: 2) Find the numerical value of the smallest code for each Chris@43: code length: Chris@43: Chris@43: code = 0; Chris@43: bl_count[0] = 0; Chris@43: for (bits = 1; bits <= MAX_BITS; bits++) { Chris@43: code = (code + bl_count[bits-1]) << 1; Chris@43: next_code[bits] = code; Chris@43: } Chris@43: Chris@43: 3) Assign numerical values to all codes, using consecutive Chris@43: values for all codes of the same length with the base Chris@43: values determined at step 2. Codes that are never used Chris@43: (which have a bit length of zero) must not be assigned a Chris@43: value. Chris@43: Chris@43: for (n = 0; n <= max_code; n++) { Chris@43: len = tree[n].Len; Chris@43: if (len != 0) { Chris@43: tree[n].Code = next_code[len]; Chris@43: next_code[len]++; Chris@43: } Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 8] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: } Chris@43: Chris@43: Example: Chris@43: Chris@43: Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, Chris@43: 3, 2, 4, 4). After step 1, we have: Chris@43: Chris@43: N bl_count[N] Chris@43: - ----------- Chris@43: 2 1 Chris@43: 3 5 Chris@43: 4 2 Chris@43: Chris@43: Step 2 computes the following next_code values: Chris@43: Chris@43: N next_code[N] Chris@43: - ------------ Chris@43: 1 0 Chris@43: 2 0 Chris@43: 3 2 Chris@43: 4 14 Chris@43: Chris@43: Step 3 produces the following code values: Chris@43: Chris@43: Symbol Length Code Chris@43: ------ ------ ---- Chris@43: A 3 010 Chris@43: B 3 011 Chris@43: C 3 100 Chris@43: D 3 101 Chris@43: E 3 110 Chris@43: F 2 00 Chris@43: G 4 1110 Chris@43: H 4 1111 Chris@43: Chris@43: 3.2.3. Details of block format Chris@43: Chris@43: Each block of compressed data begins with 3 header bits Chris@43: containing the following data: Chris@43: Chris@43: first bit BFINAL Chris@43: next 2 bits BTYPE Chris@43: Chris@43: Note that the header bits do not necessarily begin on a byte Chris@43: boundary, since a block does not necessarily occupy an integral Chris@43: number of bytes. Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 9] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: BFINAL is set if and only if this is the last block of the data Chris@43: set. Chris@43: Chris@43: BTYPE specifies how the data are compressed, as follows: Chris@43: Chris@43: 00 - no compression Chris@43: 01 - compressed with fixed Huffman codes Chris@43: 10 - compressed with dynamic Huffman codes Chris@43: 11 - reserved (error) Chris@43: Chris@43: The only difference between the two compressed cases is how the Chris@43: Huffman codes for the literal/length and distance alphabets are Chris@43: defined. Chris@43: Chris@43: In all cases, the decoding algorithm for the actual data is as Chris@43: follows: Chris@43: Chris@43: do Chris@43: read block header from input stream. Chris@43: if stored with no compression Chris@43: skip any remaining bits in current partially Chris@43: processed byte Chris@43: read LEN and NLEN (see next section) Chris@43: copy LEN bytes of data to output Chris@43: otherwise Chris@43: if compressed with dynamic Huffman codes Chris@43: read representation of code trees (see Chris@43: subsection below) Chris@43: loop (until end of block code recognized) Chris@43: decode literal/length value from input stream Chris@43: if value < 256 Chris@43: copy value (literal byte) to output stream Chris@43: otherwise Chris@43: if value = end of block (256) Chris@43: break from loop Chris@43: otherwise (value = 257..285) Chris@43: decode distance from input stream Chris@43: Chris@43: move backwards distance bytes in the output Chris@43: stream, and copy length bytes from this Chris@43: position to the output stream. Chris@43: end loop Chris@43: while not last block Chris@43: Chris@43: Note that a duplicated string reference may refer to a string Chris@43: in a previous block; i.e., the backward distance may cross one Chris@43: or more block boundaries. However a distance cannot refer past Chris@43: the beginning of the output stream. (An application using a Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 10] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: preset dictionary might discard part of the output stream; a Chris@43: distance can refer to that part of the output stream anyway) Chris@43: Note also that the referenced string may overlap the current Chris@43: position; for example, if the last 2 bytes decoded have values Chris@43: X and Y, a string reference with Chris@43: adds X,Y,X,Y,X to the output stream. Chris@43: Chris@43: We now specify each compression method in turn. Chris@43: Chris@43: 3.2.4. Non-compressed blocks (BTYPE=00) Chris@43: Chris@43: Any bits of input up to the next byte boundary are ignored. Chris@43: The rest of the block consists of the following information: Chris@43: Chris@43: 0 1 2 3 4... Chris@43: +---+---+---+---+================================+ Chris@43: | LEN | NLEN |... LEN bytes of literal data...| Chris@43: +---+---+---+---+================================+ Chris@43: Chris@43: LEN is the number of data bytes in the block. NLEN is the Chris@43: one's complement of LEN. Chris@43: Chris@43: 3.2.5. Compressed blocks (length and distance codes) Chris@43: Chris@43: As noted above, encoded data blocks in the "deflate" format Chris@43: consist of sequences of symbols drawn from three conceptually Chris@43: distinct alphabets: either literal bytes, from the alphabet of Chris@43: byte values (0..255), or pairs, Chris@43: where the length is drawn from (3..258) and the distance is Chris@43: drawn from (1..32,768). In fact, the literal and length Chris@43: alphabets are merged into a single alphabet (0..285), where Chris@43: values 0..255 represent literal bytes, the value 256 indicates Chris@43: end-of-block, and values 257..285 represent length codes Chris@43: (possibly in conjunction with extra bits following the symbol Chris@43: code) as follows: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 11] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: Extra Extra Extra Chris@43: Code Bits Length(s) Code Bits Lengths Code Bits Length(s) Chris@43: ---- ---- ------ ---- ---- ------- ---- ---- ------- Chris@43: 257 0 3 267 1 15,16 277 4 67-82 Chris@43: 258 0 4 268 1 17,18 278 4 83-98 Chris@43: 259 0 5 269 2 19-22 279 4 99-114 Chris@43: 260 0 6 270 2 23-26 280 4 115-130 Chris@43: 261 0 7 271 2 27-30 281 5 131-162 Chris@43: 262 0 8 272 2 31-34 282 5 163-194 Chris@43: 263 0 9 273 3 35-42 283 5 195-226 Chris@43: 264 0 10 274 3 43-50 284 5 227-257 Chris@43: 265 1 11,12 275 3 51-58 285 0 258 Chris@43: 266 1 13,14 276 3 59-66 Chris@43: Chris@43: The extra bits should be interpreted as a machine integer Chris@43: stored with the most-significant bit first, e.g., bits 1110 Chris@43: represent the value 14. Chris@43: Chris@43: Extra Extra Extra Chris@43: Code Bits Dist Code Bits Dist Code Bits Distance Chris@43: ---- ---- ---- ---- ---- ------ ---- ---- -------- Chris@43: 0 0 1 10 4 33-48 20 9 1025-1536 Chris@43: 1 0 2 11 4 49-64 21 9 1537-2048 Chris@43: 2 0 3 12 5 65-96 22 10 2049-3072 Chris@43: 3 0 4 13 5 97-128 23 10 3073-4096 Chris@43: 4 1 5,6 14 6 129-192 24 11 4097-6144 Chris@43: 5 1 7,8 15 6 193-256 25 11 6145-8192 Chris@43: 6 2 9-12 16 7 257-384 26 12 8193-12288 Chris@43: 7 2 13-16 17 7 385-512 27 12 12289-16384 Chris@43: 8 3 17-24 18 8 513-768 28 13 16385-24576 Chris@43: 9 3 25-32 19 8 769-1024 29 13 24577-32768 Chris@43: Chris@43: 3.2.6. Compression with fixed Huffman codes (BTYPE=01) Chris@43: Chris@43: The Huffman codes for the two alphabets are fixed, and are not Chris@43: represented explicitly in the data. The Huffman code lengths Chris@43: for the literal/length alphabet are: Chris@43: Chris@43: Lit Value Bits Codes Chris@43: --------- ---- ----- Chris@43: 0 - 143 8 00110000 through Chris@43: 10111111 Chris@43: 144 - 255 9 110010000 through Chris@43: 111111111 Chris@43: 256 - 279 7 0000000 through Chris@43: 0010111 Chris@43: 280 - 287 8 11000000 through Chris@43: 11000111 Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 12] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: The code lengths are sufficient to generate the actual codes, Chris@43: as described above; we show the codes in the table for added Chris@43: clarity. Literal/length values 286-287 will never actually Chris@43: occur in the compressed data, but participate in the code Chris@43: construction. Chris@43: Chris@43: Distance codes 0-31 are represented by (fixed-length) 5-bit Chris@43: codes, with possible additional bits as shown in the table Chris@43: shown in Paragraph 3.2.5, above. Note that distance codes 30- Chris@43: 31 will never actually occur in the compressed data. Chris@43: Chris@43: 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) Chris@43: Chris@43: The Huffman codes for the two alphabets appear in the block Chris@43: immediately after the header bits and before the actual Chris@43: compressed data, first the literal/length code and then the Chris@43: distance code. Each code is defined by a sequence of code Chris@43: lengths, as discussed in Paragraph 3.2.2, above. For even Chris@43: greater compactness, the code length sequences themselves are Chris@43: compressed using a Huffman code. The alphabet for code lengths Chris@43: is as follows: Chris@43: Chris@43: 0 - 15: Represent code lengths of 0 - 15 Chris@43: 16: Copy the previous code length 3 - 6 times. Chris@43: The next 2 bits indicate repeat length Chris@43: (0 = 3, ... , 3 = 6) Chris@43: Example: Codes 8, 16 (+2 bits 11), Chris@43: 16 (+2 bits 10) will expand to Chris@43: 12 code lengths of 8 (1 + 6 + 5) Chris@43: 17: Repeat a code length of 0 for 3 - 10 times. Chris@43: (3 bits of length) Chris@43: 18: Repeat a code length of 0 for 11 - 138 times Chris@43: (7 bits of length) Chris@43: Chris@43: A code length of 0 indicates that the corresponding symbol in Chris@43: the literal/length or distance alphabet will not occur in the Chris@43: block, and should not participate in the Huffman code Chris@43: construction algorithm given earlier. If only one distance Chris@43: code is used, it is encoded using one bit, not zero bits; in Chris@43: this case there is a single code length of one, with one unused Chris@43: code. One distance code of zero bits means that there are no Chris@43: distance codes used at all (the data is all literals). Chris@43: Chris@43: We can now define the format of the block: Chris@43: Chris@43: 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) Chris@43: 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) Chris@43: 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 13] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: (HCLEN + 4) x 3 bits: code lengths for the code length Chris@43: alphabet given just above, in the order: 16, 17, 18, Chris@43: 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 Chris@43: Chris@43: These code lengths are interpreted as 3-bit integers Chris@43: (0-7); as above, a code length of 0 means the Chris@43: corresponding symbol (literal/length or distance code Chris@43: length) is not used. Chris@43: Chris@43: HLIT + 257 code lengths for the literal/length alphabet, Chris@43: encoded using the code length Huffman code Chris@43: Chris@43: HDIST + 1 code lengths for the distance alphabet, Chris@43: encoded using the code length Huffman code Chris@43: Chris@43: The actual compressed data of the block, Chris@43: encoded using the literal/length and distance Huffman Chris@43: codes Chris@43: Chris@43: The literal/length symbol 256 (end of data), Chris@43: encoded using the literal/length Huffman code Chris@43: Chris@43: The code length repeat codes can cross from HLIT + 257 to the Chris@43: HDIST + 1 code lengths. In other words, all code lengths form Chris@43: a single sequence of HLIT + HDIST + 258 values. Chris@43: Chris@43: 3.3. Compliance Chris@43: Chris@43: A compressor may limit further the ranges of values specified in Chris@43: the previous section and still be compliant; for example, it may Chris@43: limit the range of backward pointers to some value smaller than Chris@43: 32K. Similarly, a compressor may limit the size of blocks so that Chris@43: a compressible block fits in memory. Chris@43: Chris@43: A compliant decompressor must accept the full range of possible Chris@43: values defined in the previous section, and must accept blocks of Chris@43: arbitrary size. Chris@43: Chris@43: 4. Compression algorithm details Chris@43: Chris@43: While it is the intent of this document to define the "deflate" Chris@43: compressed data format without reference to any particular Chris@43: compression algorithm, the format is related to the compressed Chris@43: formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below); Chris@43: since many variations of LZ77 are patented, it is strongly Chris@43: recommended that the implementor of a compressor follow the general Chris@43: algorithm presented here, which is known not to be patented per se. Chris@43: The material in this section is not part of the definition of the Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 14] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: specification per se, and a compressor need not follow it in order to Chris@43: be compliant. Chris@43: Chris@43: The compressor terminates a block when it determines that starting a Chris@43: new block with fresh trees would be useful, or when the block size Chris@43: fills up the compressor's block buffer. Chris@43: Chris@43: The compressor uses a chained hash table to find duplicated strings, Chris@43: using a hash function that operates on 3-byte sequences. At any Chris@43: given point during compression, let XYZ be the next 3 input bytes to Chris@43: be examined (not necessarily all different, of course). First, the Chris@43: compressor examines the hash chain for XYZ. If the chain is empty, Chris@43: the compressor simply writes out X as a literal byte and advances one Chris@43: byte in the input. If the hash chain is not empty, indicating that Chris@43: the sequence XYZ (or, if we are unlucky, some other 3 bytes with the Chris@43: same hash function value) has occurred recently, the compressor Chris@43: compares all strings on the XYZ hash chain with the actual input data Chris@43: sequence starting at the current point, and selects the longest Chris@43: match. Chris@43: Chris@43: The compressor searches the hash chains starting with the most recent Chris@43: strings, to favor small distances and thus take advantage of the Chris@43: Huffman encoding. The hash chains are singly linked. There are no Chris@43: deletions from the hash chains; the algorithm simply discards matches Chris@43: that are too old. To avoid a worst-case situation, very long hash Chris@43: chains are arbitrarily truncated at a certain length, determined by a Chris@43: run-time parameter. Chris@43: Chris@43: To improve overall compression, the compressor optionally defers the Chris@43: selection of matches ("lazy matching"): after a match of length N has Chris@43: been found, the compressor searches for a longer match starting at Chris@43: the next input byte. If it finds a longer match, it truncates the Chris@43: previous match to a length of one (thus producing a single literal Chris@43: byte) and then emits the longer match. Otherwise, it emits the Chris@43: original match, and, as described above, advances N bytes before Chris@43: continuing. Chris@43: Chris@43: Run-time parameters also control this "lazy match" procedure. If Chris@43: compression ratio is most important, the compressor attempts a Chris@43: complete second search regardless of the length of the first match. Chris@43: In the normal case, if the current match is "long enough", the Chris@43: compressor reduces the search for a longer match, thus speeding up Chris@43: the process. If speed is most important, the compressor inserts new Chris@43: strings in the hash table only when no match was found, or when the Chris@43: match is not "too long". This degrades the compression ratio but Chris@43: saves time since there are both fewer insertions and fewer searches. Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 15] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: 5. References Chris@43: Chris@43: [1] Huffman, D. A., "A Method for the Construction of Minimum Chris@43: Redundancy Codes", Proceedings of the Institute of Radio Chris@43: Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101. Chris@43: Chris@43: [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data Chris@43: Compression", IEEE Transactions on Information Theory, Vol. 23, Chris@43: No. 3, pp. 337-343. Chris@43: Chris@43: [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources, Chris@43: available in ftp://ftp.uu.net/pub/archiving/zip/doc/ Chris@43: Chris@43: [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources, Chris@43: available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/ Chris@43: Chris@43: [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix Chris@43: encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169. Chris@43: Chris@43: [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes," Chris@43: Comm. ACM, 33,4, April 1990, pp. 449-459. Chris@43: Chris@43: 6. Security Considerations Chris@43: Chris@43: Any data compression method involves the reduction of redundancy in Chris@43: the data. Consequently, any corruption of the data is likely to have Chris@43: severe effects and be difficult to correct. Uncompressed text, on Chris@43: the other hand, will probably still be readable despite the presence Chris@43: of some corrupted bytes. Chris@43: Chris@43: It is recommended that systems using this data format provide some Chris@43: means of validating the integrity of the compressed data. See Chris@43: reference [3], for example. Chris@43: Chris@43: 7. Source code Chris@43: Chris@43: Source code for a C language implementation of a "deflate" compliant Chris@43: compressor and decompressor is available within the zlib package at Chris@43: ftp://ftp.uu.net/pub/archiving/zip/zlib/. Chris@43: Chris@43: 8. Acknowledgements Chris@43: Chris@43: Trademarks cited in this document are the property of their Chris@43: respective owners. Chris@43: Chris@43: Phil Katz designed the deflate format. Jean-Loup Gailly and Mark Chris@43: Adler wrote the related software described in this specification. Chris@43: Glenn Randers-Pehrson converted this document to RFC and HTML format. Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 16] Chris@43: Chris@43: RFC 1951 DEFLATE Compressed Data Format Specification May 1996 Chris@43: Chris@43: Chris@43: 9. Author's Address Chris@43: Chris@43: L. Peter Deutsch Chris@43: Aladdin Enterprises Chris@43: 203 Santa Margarita Ave. Chris@43: Menlo Park, CA 94025 Chris@43: Chris@43: Phone: (415) 322-0103 (AM only) Chris@43: FAX: (415) 322-1734 Chris@43: EMail: Chris@43: Chris@43: Questions about the technical content of this specification can be Chris@43: sent by email to: Chris@43: Chris@43: Jean-Loup Gailly and Chris@43: Mark Adler Chris@43: Chris@43: Editorial comments on this specification can be sent by email to: Chris@43: Chris@43: L. Peter Deutsch and Chris@43: Glenn Randers-Pehrson Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Chris@43: Deutsch Informational [Page 17] Chris@43: