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