1*4a5d661aSToomas Soome 2*4a5d661aSToomas Soome 3*4a5d661aSToomas Soome 4*4a5d661aSToomas Soome 5*4a5d661aSToomas Soome 6*4a5d661aSToomas Soome 7*4a5d661aSToomas SoomeNetwork Working Group P. Deutsch 8*4a5d661aSToomas SoomeRequest for Comments: 1951 Aladdin Enterprises 9*4a5d661aSToomas SoomeCategory: Informational May 1996 10*4a5d661aSToomas Soome 11*4a5d661aSToomas Soome 12*4a5d661aSToomas Soome DEFLATE Compressed Data Format Specification version 1.3 13*4a5d661aSToomas Soome 14*4a5d661aSToomas SoomeStatus of This Memo 15*4a5d661aSToomas Soome 16*4a5d661aSToomas Soome This memo provides information for the Internet community. This memo 17*4a5d661aSToomas Soome does not specify an Internet standard of any kind. Distribution of 18*4a5d661aSToomas Soome this memo is unlimited. 19*4a5d661aSToomas Soome 20*4a5d661aSToomas SoomeIESG Note: 21*4a5d661aSToomas Soome 22*4a5d661aSToomas Soome The IESG takes no position on the validity of any Intellectual 23*4a5d661aSToomas Soome Property Rights statements contained in this document. 24*4a5d661aSToomas Soome 25*4a5d661aSToomas SoomeNotices 26*4a5d661aSToomas Soome 27*4a5d661aSToomas Soome Copyright (c) 1996 L. Peter Deutsch 28*4a5d661aSToomas Soome 29*4a5d661aSToomas Soome Permission is granted to copy and distribute this document for any 30*4a5d661aSToomas Soome purpose and without charge, including translations into other 31*4a5d661aSToomas Soome languages and incorporation into compilations, provided that the 32*4a5d661aSToomas Soome copyright notice and this notice are preserved, and that any 33*4a5d661aSToomas Soome substantive changes or deletions from the original are clearly 34*4a5d661aSToomas Soome marked. 35*4a5d661aSToomas Soome 36*4a5d661aSToomas Soome A pointer to the latest version of this and related documentation in 37*4a5d661aSToomas Soome HTML format can be found at the URL 38*4a5d661aSToomas Soome <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>. 39*4a5d661aSToomas Soome 40*4a5d661aSToomas SoomeAbstract 41*4a5d661aSToomas Soome 42*4a5d661aSToomas Soome This specification defines a lossless compressed data format that 43*4a5d661aSToomas Soome compresses data using a combination of the LZ77 algorithm and Huffman 44*4a5d661aSToomas Soome coding, with efficiency comparable to the best currently available 45*4a5d661aSToomas Soome general-purpose compression methods. The data can be produced or 46*4a5d661aSToomas Soome consumed, even for an arbitrarily long sequentially presented input 47*4a5d661aSToomas Soome data stream, using only an a priori bounded amount of intermediate 48*4a5d661aSToomas Soome storage. The format can be implemented readily in a manner not 49*4a5d661aSToomas Soome covered by patents. 50*4a5d661aSToomas Soome 51*4a5d661aSToomas Soome 52*4a5d661aSToomas Soome 53*4a5d661aSToomas Soome 54*4a5d661aSToomas Soome 55*4a5d661aSToomas Soome 56*4a5d661aSToomas Soome 57*4a5d661aSToomas Soome 58*4a5d661aSToomas SoomeDeutsch Informational [Page 1] 59*4a5d661aSToomas Soome 60*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 61*4a5d661aSToomas Soome 62*4a5d661aSToomas Soome 63*4a5d661aSToomas SoomeTable of Contents 64*4a5d661aSToomas Soome 65*4a5d661aSToomas Soome 1. Introduction ................................................... 2 66*4a5d661aSToomas Soome 1.1. Purpose ................................................... 2 67*4a5d661aSToomas Soome 1.2. Intended audience ......................................... 3 68*4a5d661aSToomas Soome 1.3. Scope ..................................................... 3 69*4a5d661aSToomas Soome 1.4. Compliance ................................................ 3 70*4a5d661aSToomas Soome 1.5. Definitions of terms and conventions used ................ 3 71*4a5d661aSToomas Soome 1.6. Changes from previous versions ............................ 4 72*4a5d661aSToomas Soome 2. Compressed representation overview ............................. 4 73*4a5d661aSToomas Soome 3. Detailed specification ......................................... 5 74*4a5d661aSToomas Soome 3.1. Overall conventions ....................................... 5 75*4a5d661aSToomas Soome 3.1.1. Packing into bytes .................................. 5 76*4a5d661aSToomas Soome 3.2. Compressed block format ................................... 6 77*4a5d661aSToomas Soome 3.2.1. Synopsis of prefix and Huffman coding ............... 6 78*4a5d661aSToomas Soome 3.2.2. Use of Huffman coding in the "deflate" format ....... 7 79*4a5d661aSToomas Soome 3.2.3. Details of block format ............................. 9 80*4a5d661aSToomas Soome 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11 81*4a5d661aSToomas Soome 3.2.5. Compressed blocks (length and distance codes) ...... 11 82*4a5d661aSToomas Soome 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12 83*4a5d661aSToomas Soome 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13 84*4a5d661aSToomas Soome 3.3. Compliance ............................................... 14 85*4a5d661aSToomas Soome 4. Compression algorithm details ................................. 14 86*4a5d661aSToomas Soome 5. References .................................................... 16 87*4a5d661aSToomas Soome 6. Security Considerations ....................................... 16 88*4a5d661aSToomas Soome 7. Source code ................................................... 16 89*4a5d661aSToomas Soome 8. Acknowledgements .............................................. 16 90*4a5d661aSToomas Soome 9. Author's Address .............................................. 17 91*4a5d661aSToomas Soome 92*4a5d661aSToomas Soome1. Introduction 93*4a5d661aSToomas Soome 94*4a5d661aSToomas Soome 1.1. Purpose 95*4a5d661aSToomas Soome 96*4a5d661aSToomas Soome The purpose of this specification is to define a lossless 97*4a5d661aSToomas Soome compressed data format that: 98*4a5d661aSToomas Soome * Is independent of CPU type, operating system, file system, 99*4a5d661aSToomas Soome and character set, and hence can be used for interchange; 100*4a5d661aSToomas Soome * Can be produced or consumed, even for an arbitrarily long 101*4a5d661aSToomas Soome sequentially presented input data stream, using only an a 102*4a5d661aSToomas Soome priori bounded amount of intermediate storage, and hence 103*4a5d661aSToomas Soome can be used in data communications or similar structures 104*4a5d661aSToomas Soome such as Unix filters; 105*4a5d661aSToomas Soome * Compresses data with efficiency comparable to the best 106*4a5d661aSToomas Soome currently available general-purpose compression methods, 107*4a5d661aSToomas Soome and in particular considerably better than the "compress" 108*4a5d661aSToomas Soome program; 109*4a5d661aSToomas Soome * Can be implemented readily in a manner not covered by 110*4a5d661aSToomas Soome patents, and hence can be practiced freely; 111*4a5d661aSToomas Soome 112*4a5d661aSToomas Soome 113*4a5d661aSToomas Soome 114*4a5d661aSToomas SoomeDeutsch Informational [Page 2] 115*4a5d661aSToomas Soome 116*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 117*4a5d661aSToomas Soome 118*4a5d661aSToomas Soome 119*4a5d661aSToomas Soome * Is compatible with the file format produced by the current 120*4a5d661aSToomas Soome widely used gzip utility, in that conforming decompressors 121*4a5d661aSToomas Soome will be able to read data produced by the existing gzip 122*4a5d661aSToomas Soome compressor. 123*4a5d661aSToomas Soome 124*4a5d661aSToomas Soome The data format defined by this specification does not attempt to: 125*4a5d661aSToomas Soome 126*4a5d661aSToomas Soome * Allow random access to compressed data; 127*4a5d661aSToomas Soome * Compress specialized data (e.g., raster graphics) as well 128*4a5d661aSToomas Soome as the best currently available specialized algorithms. 129*4a5d661aSToomas Soome 130*4a5d661aSToomas Soome A simple counting argument shows that no lossless compression 131*4a5d661aSToomas Soome algorithm can compress every possible input data set. For the 132*4a5d661aSToomas Soome format defined here, the worst case expansion is 5 bytes per 32K- 133*4a5d661aSToomas Soome byte block, i.e., a size increase of 0.015% for large data sets. 134*4a5d661aSToomas Soome English text usually compresses by a factor of 2.5 to 3; 135*4a5d661aSToomas Soome executable files usually compress somewhat less; graphical data 136*4a5d661aSToomas Soome such as raster images may compress much more. 137*4a5d661aSToomas Soome 138*4a5d661aSToomas Soome 1.2. Intended audience 139*4a5d661aSToomas Soome 140*4a5d661aSToomas Soome This specification is intended for use by implementors of software 141*4a5d661aSToomas Soome to compress data into "deflate" format and/or decompress data from 142*4a5d661aSToomas Soome "deflate" format. 143*4a5d661aSToomas Soome 144*4a5d661aSToomas Soome The text of the specification assumes a basic background in 145*4a5d661aSToomas Soome programming at the level of bits and other primitive data 146*4a5d661aSToomas Soome representations. Familiarity with the technique of Huffman coding 147*4a5d661aSToomas Soome is helpful but not required. 148*4a5d661aSToomas Soome 149*4a5d661aSToomas Soome 1.3. Scope 150*4a5d661aSToomas Soome 151*4a5d661aSToomas Soome The specification specifies a method for representing a sequence 152*4a5d661aSToomas Soome of bytes as a (usually shorter) sequence of bits, and a method for 153*4a5d661aSToomas Soome packing the latter bit sequence into bytes. 154*4a5d661aSToomas Soome 155*4a5d661aSToomas Soome 1.4. Compliance 156*4a5d661aSToomas Soome 157*4a5d661aSToomas Soome Unless otherwise indicated below, a compliant decompressor must be 158*4a5d661aSToomas Soome able to accept and decompress any data set that conforms to all 159*4a5d661aSToomas Soome the specifications presented here; a compliant compressor must 160*4a5d661aSToomas Soome produce data sets that conform to all the specifications presented 161*4a5d661aSToomas Soome here. 162*4a5d661aSToomas Soome 163*4a5d661aSToomas Soome 1.5. Definitions of terms and conventions used 164*4a5d661aSToomas Soome 165*4a5d661aSToomas Soome Byte: 8 bits stored or transmitted as a unit (same as an octet). 166*4a5d661aSToomas Soome For this specification, a byte is exactly 8 bits, even on machines 167*4a5d661aSToomas Soome 168*4a5d661aSToomas Soome 169*4a5d661aSToomas Soome 170*4a5d661aSToomas SoomeDeutsch Informational [Page 3] 171*4a5d661aSToomas Soome 172*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 173*4a5d661aSToomas Soome 174*4a5d661aSToomas Soome 175*4a5d661aSToomas Soome which store a character on a number of bits different from eight. 176*4a5d661aSToomas Soome See below, for the numbering of bits within a byte. 177*4a5d661aSToomas Soome 178*4a5d661aSToomas Soome String: a sequence of arbitrary bytes. 179*4a5d661aSToomas Soome 180*4a5d661aSToomas Soome 1.6. Changes from previous versions 181*4a5d661aSToomas Soome 182*4a5d661aSToomas Soome There have been no technical changes to the deflate format since 183*4a5d661aSToomas Soome version 1.1 of this specification. In version 1.2, some 184*4a5d661aSToomas Soome terminology was changed. Version 1.3 is a conversion of the 185*4a5d661aSToomas Soome specification to RFC style. 186*4a5d661aSToomas Soome 187*4a5d661aSToomas Soome2. Compressed representation overview 188*4a5d661aSToomas Soome 189*4a5d661aSToomas Soome A compressed data set consists of a series of blocks, corresponding 190*4a5d661aSToomas Soome to successive blocks of input data. The block sizes are arbitrary, 191*4a5d661aSToomas Soome except that non-compressible blocks are limited to 65,535 bytes. 192*4a5d661aSToomas Soome 193*4a5d661aSToomas Soome Each block is compressed using a combination of the LZ77 algorithm 194*4a5d661aSToomas Soome and Huffman coding. The Huffman trees for each block are independent 195*4a5d661aSToomas Soome of those for previous or subsequent blocks; the LZ77 algorithm may 196*4a5d661aSToomas Soome use a reference to a duplicated string occurring in a previous block, 197*4a5d661aSToomas Soome up to 32K input bytes before. 198*4a5d661aSToomas Soome 199*4a5d661aSToomas Soome Each block consists of two parts: a pair of Huffman code trees that 200*4a5d661aSToomas Soome describe the representation of the compressed data part, and a 201*4a5d661aSToomas Soome compressed data part. (The Huffman trees themselves are compressed 202*4a5d661aSToomas Soome using Huffman encoding.) The compressed data consists of a series of 203*4a5d661aSToomas Soome elements of two types: literal bytes (of strings that have not been 204*4a5d661aSToomas Soome detected as duplicated within the previous 32K input bytes), and 205*4a5d661aSToomas Soome pointers to duplicated strings, where a pointer is represented as a 206*4a5d661aSToomas Soome pair <length, backward distance>. The representation used in the 207*4a5d661aSToomas Soome "deflate" format limits distances to 32K bytes and lengths to 258 208*4a5d661aSToomas Soome bytes, but does not limit the size of a block, except for 209*4a5d661aSToomas Soome uncompressible blocks, which are limited as noted above. 210*4a5d661aSToomas Soome 211*4a5d661aSToomas Soome Each type of value (literals, distances, and lengths) in the 212*4a5d661aSToomas Soome compressed data is represented using a Huffman code, using one code 213*4a5d661aSToomas Soome tree for literals and lengths and a separate code tree for distances. 214*4a5d661aSToomas Soome The code trees for each block appear in a compact form just before 215*4a5d661aSToomas Soome the compressed data for that block. 216*4a5d661aSToomas Soome 217*4a5d661aSToomas Soome 218*4a5d661aSToomas Soome 219*4a5d661aSToomas Soome 220*4a5d661aSToomas Soome 221*4a5d661aSToomas Soome 222*4a5d661aSToomas Soome 223*4a5d661aSToomas Soome 224*4a5d661aSToomas Soome 225*4a5d661aSToomas Soome 226*4a5d661aSToomas SoomeDeutsch Informational [Page 4] 227*4a5d661aSToomas Soome 228*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 229*4a5d661aSToomas Soome 230*4a5d661aSToomas Soome 231*4a5d661aSToomas Soome3. Detailed specification 232*4a5d661aSToomas Soome 233*4a5d661aSToomas Soome 3.1. Overall conventions In the diagrams below, a box like this: 234*4a5d661aSToomas Soome 235*4a5d661aSToomas Soome +---+ 236*4a5d661aSToomas Soome | | <-- the vertical bars might be missing 237*4a5d661aSToomas Soome +---+ 238*4a5d661aSToomas Soome 239*4a5d661aSToomas Soome represents one byte; a box like this: 240*4a5d661aSToomas Soome 241*4a5d661aSToomas Soome +==============+ 242*4a5d661aSToomas Soome | | 243*4a5d661aSToomas Soome +==============+ 244*4a5d661aSToomas Soome 245*4a5d661aSToomas Soome represents a variable number of bytes. 246*4a5d661aSToomas Soome 247*4a5d661aSToomas Soome Bytes stored within a computer do not have a "bit order", since 248*4a5d661aSToomas Soome they are always treated as a unit. However, a byte considered as 249*4a5d661aSToomas Soome an integer between 0 and 255 does have a most- and least- 250*4a5d661aSToomas Soome significant bit, and since we write numbers with the most- 251*4a5d661aSToomas Soome significant digit on the left, we also write bytes with the most- 252*4a5d661aSToomas Soome significant bit on the left. In the diagrams below, we number the 253*4a5d661aSToomas Soome bits of a byte so that bit 0 is the least-significant bit, i.e., 254*4a5d661aSToomas Soome the bits are numbered: 255*4a5d661aSToomas Soome 256*4a5d661aSToomas Soome +--------+ 257*4a5d661aSToomas Soome |76543210| 258*4a5d661aSToomas Soome +--------+ 259*4a5d661aSToomas Soome 260*4a5d661aSToomas Soome Within a computer, a number may occupy multiple bytes. All 261*4a5d661aSToomas Soome multi-byte numbers in the format described here are stored with 262*4a5d661aSToomas Soome the least-significant byte first (at the lower memory address). 263*4a5d661aSToomas Soome For example, the decimal number 520 is stored as: 264*4a5d661aSToomas Soome 265*4a5d661aSToomas Soome 0 1 266*4a5d661aSToomas Soome +--------+--------+ 267*4a5d661aSToomas Soome |00001000|00000010| 268*4a5d661aSToomas Soome +--------+--------+ 269*4a5d661aSToomas Soome ^ ^ 270*4a5d661aSToomas Soome | | 271*4a5d661aSToomas Soome | + more significant byte = 2 x 256 272*4a5d661aSToomas Soome + less significant byte = 8 273*4a5d661aSToomas Soome 274*4a5d661aSToomas Soome 3.1.1. Packing into bytes 275*4a5d661aSToomas Soome 276*4a5d661aSToomas Soome This document does not address the issue of the order in which 277*4a5d661aSToomas Soome bits of a byte are transmitted on a bit-sequential medium, 278*4a5d661aSToomas Soome since the final data format described here is byte- rather than 279*4a5d661aSToomas Soome 280*4a5d661aSToomas Soome 281*4a5d661aSToomas Soome 282*4a5d661aSToomas SoomeDeutsch Informational [Page 5] 283*4a5d661aSToomas Soome 284*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 285*4a5d661aSToomas Soome 286*4a5d661aSToomas Soome 287*4a5d661aSToomas Soome bit-oriented. However, we describe the compressed block format 288*4a5d661aSToomas Soome in below, as a sequence of data elements of various bit 289*4a5d661aSToomas Soome lengths, not a sequence of bytes. We must therefore specify 290*4a5d661aSToomas Soome how to pack these data elements into bytes to form the final 291*4a5d661aSToomas Soome compressed byte sequence: 292*4a5d661aSToomas Soome 293*4a5d661aSToomas Soome * Data elements are packed into bytes in order of 294*4a5d661aSToomas Soome increasing bit number within the byte, i.e., starting 295*4a5d661aSToomas Soome with the least-significant bit of the byte. 296*4a5d661aSToomas Soome * Data elements other than Huffman codes are packed 297*4a5d661aSToomas Soome starting with the least-significant bit of the data 298*4a5d661aSToomas Soome element. 299*4a5d661aSToomas Soome * Huffman codes are packed starting with the most- 300*4a5d661aSToomas Soome significant bit of the code. 301*4a5d661aSToomas Soome 302*4a5d661aSToomas Soome In other words, if one were to print out the compressed data as 303*4a5d661aSToomas Soome a sequence of bytes, starting with the first byte at the 304*4a5d661aSToomas Soome *right* margin and proceeding to the *left*, with the most- 305*4a5d661aSToomas Soome significant bit of each byte on the left as usual, one would be 306*4a5d661aSToomas Soome able to parse the result from right to left, with fixed-width 307*4a5d661aSToomas Soome elements in the correct MSB-to-LSB order and Huffman codes in 308*4a5d661aSToomas Soome bit-reversed order (i.e., with the first bit of the code in the 309*4a5d661aSToomas Soome relative LSB position). 310*4a5d661aSToomas Soome 311*4a5d661aSToomas Soome 3.2. Compressed block format 312*4a5d661aSToomas Soome 313*4a5d661aSToomas Soome 3.2.1. Synopsis of prefix and Huffman coding 314*4a5d661aSToomas Soome 315*4a5d661aSToomas Soome Prefix coding represents symbols from an a priori known 316*4a5d661aSToomas Soome alphabet by bit sequences (codes), one code for each symbol, in 317*4a5d661aSToomas Soome a manner such that different symbols may be represented by bit 318*4a5d661aSToomas Soome sequences of different lengths, but a parser can always parse 319*4a5d661aSToomas Soome an encoded string unambiguously symbol-by-symbol. 320*4a5d661aSToomas Soome 321*4a5d661aSToomas Soome We define a prefix code in terms of a binary tree in which the 322*4a5d661aSToomas Soome two edges descending from each non-leaf node are labeled 0 and 323*4a5d661aSToomas Soome 1 and in which the leaf nodes correspond one-for-one with (are 324*4a5d661aSToomas Soome labeled with) the symbols of the alphabet; then the code for a 325*4a5d661aSToomas Soome symbol is the sequence of 0's and 1's on the edges leading from 326*4a5d661aSToomas Soome the root to the leaf labeled with that symbol. For example: 327*4a5d661aSToomas Soome 328*4a5d661aSToomas Soome 329*4a5d661aSToomas Soome 330*4a5d661aSToomas Soome 331*4a5d661aSToomas Soome 332*4a5d661aSToomas Soome 333*4a5d661aSToomas Soome 334*4a5d661aSToomas Soome 335*4a5d661aSToomas Soome 336*4a5d661aSToomas Soome 337*4a5d661aSToomas Soome 338*4a5d661aSToomas SoomeDeutsch Informational [Page 6] 339*4a5d661aSToomas Soome 340*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 341*4a5d661aSToomas Soome 342*4a5d661aSToomas Soome 343*4a5d661aSToomas Soome /\ Symbol Code 344*4a5d661aSToomas Soome 0 1 ------ ---- 345*4a5d661aSToomas Soome / \ A 00 346*4a5d661aSToomas Soome /\ B B 1 347*4a5d661aSToomas Soome 0 1 C 011 348*4a5d661aSToomas Soome / \ D 010 349*4a5d661aSToomas Soome A /\ 350*4a5d661aSToomas Soome 0 1 351*4a5d661aSToomas Soome / \ 352*4a5d661aSToomas Soome D C 353*4a5d661aSToomas Soome 354*4a5d661aSToomas Soome A parser can decode the next symbol from an encoded input 355*4a5d661aSToomas Soome stream by walking down the tree from the root, at each step 356*4a5d661aSToomas Soome choosing the edge corresponding to the next input bit. 357*4a5d661aSToomas Soome 358*4a5d661aSToomas Soome Given an alphabet with known symbol frequencies, the Huffman 359*4a5d661aSToomas Soome algorithm allows the construction of an optimal prefix code 360*4a5d661aSToomas Soome (one which represents strings with those symbol frequencies 361*4a5d661aSToomas Soome using the fewest bits of any possible prefix codes for that 362*4a5d661aSToomas Soome alphabet). Such a code is called a Huffman code. (See 363*4a5d661aSToomas Soome reference [1] in Chapter 5, references for additional 364*4a5d661aSToomas Soome information on Huffman codes.) 365*4a5d661aSToomas Soome 366*4a5d661aSToomas Soome Note that in the "deflate" format, the Huffman codes for the 367*4a5d661aSToomas Soome various alphabets must not exceed certain maximum code lengths. 368*4a5d661aSToomas Soome This constraint complicates the algorithm for computing code 369*4a5d661aSToomas Soome lengths from symbol frequencies. Again, see Chapter 5, 370*4a5d661aSToomas Soome references for details. 371*4a5d661aSToomas Soome 372*4a5d661aSToomas Soome 3.2.2. Use of Huffman coding in the "deflate" format 373*4a5d661aSToomas Soome 374*4a5d661aSToomas Soome The Huffman codes used for each alphabet in the "deflate" 375*4a5d661aSToomas Soome format have two additional rules: 376*4a5d661aSToomas Soome 377*4a5d661aSToomas Soome * All codes of a given bit length have lexicographically 378*4a5d661aSToomas Soome consecutive values, in the same order as the symbols 379*4a5d661aSToomas Soome they represent; 380*4a5d661aSToomas Soome 381*4a5d661aSToomas Soome * Shorter codes lexicographically precede longer codes. 382*4a5d661aSToomas Soome 383*4a5d661aSToomas Soome 384*4a5d661aSToomas Soome 385*4a5d661aSToomas Soome 386*4a5d661aSToomas Soome 387*4a5d661aSToomas Soome 388*4a5d661aSToomas Soome 389*4a5d661aSToomas Soome 390*4a5d661aSToomas Soome 391*4a5d661aSToomas Soome 392*4a5d661aSToomas Soome 393*4a5d661aSToomas Soome 394*4a5d661aSToomas SoomeDeutsch Informational [Page 7] 395*4a5d661aSToomas Soome 396*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 397*4a5d661aSToomas Soome 398*4a5d661aSToomas Soome 399*4a5d661aSToomas Soome We could recode the example above to follow this rule as 400*4a5d661aSToomas Soome follows, assuming that the order of the alphabet is ABCD: 401*4a5d661aSToomas Soome 402*4a5d661aSToomas Soome Symbol Code 403*4a5d661aSToomas Soome ------ ---- 404*4a5d661aSToomas Soome A 10 405*4a5d661aSToomas Soome B 0 406*4a5d661aSToomas Soome C 110 407*4a5d661aSToomas Soome D 111 408*4a5d661aSToomas Soome 409*4a5d661aSToomas Soome I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are 410*4a5d661aSToomas Soome lexicographically consecutive. 411*4a5d661aSToomas Soome 412*4a5d661aSToomas Soome Given this rule, we can define the Huffman code for an alphabet 413*4a5d661aSToomas Soome just by giving the bit lengths of the codes for each symbol of 414*4a5d661aSToomas Soome the alphabet in order; this is sufficient to determine the 415*4a5d661aSToomas Soome actual codes. In our example, the code is completely defined 416*4a5d661aSToomas Soome by the sequence of bit lengths (2, 1, 3, 3). The following 417*4a5d661aSToomas Soome algorithm generates the codes as integers, intended to be read 418*4a5d661aSToomas Soome from most- to least-significant bit. The code lengths are 419*4a5d661aSToomas Soome initially in tree[I].Len; the codes are produced in 420*4a5d661aSToomas Soome tree[I].Code. 421*4a5d661aSToomas Soome 422*4a5d661aSToomas Soome 1) Count the number of codes for each code length. Let 423*4a5d661aSToomas Soome bl_count[N] be the number of codes of length N, N >= 1. 424*4a5d661aSToomas Soome 425*4a5d661aSToomas Soome 2) Find the numerical value of the smallest code for each 426*4a5d661aSToomas Soome code length: 427*4a5d661aSToomas Soome 428*4a5d661aSToomas Soome code = 0; 429*4a5d661aSToomas Soome bl_count[0] = 0; 430*4a5d661aSToomas Soome for (bits = 1; bits <= MAX_BITS; bits++) { 431*4a5d661aSToomas Soome code = (code + bl_count[bits-1]) << 1; 432*4a5d661aSToomas Soome next_code[bits] = code; 433*4a5d661aSToomas Soome } 434*4a5d661aSToomas Soome 435*4a5d661aSToomas Soome 3) Assign numerical values to all codes, using consecutive 436*4a5d661aSToomas Soome values for all codes of the same length with the base 437*4a5d661aSToomas Soome values determined at step 2. Codes that are never used 438*4a5d661aSToomas Soome (which have a bit length of zero) must not be assigned a 439*4a5d661aSToomas Soome value. 440*4a5d661aSToomas Soome 441*4a5d661aSToomas Soome for (n = 0; n <= max_code; n++) { 442*4a5d661aSToomas Soome len = tree[n].Len; 443*4a5d661aSToomas Soome if (len != 0) { 444*4a5d661aSToomas Soome tree[n].Code = next_code[len]; 445*4a5d661aSToomas Soome next_code[len]++; 446*4a5d661aSToomas Soome } 447*4a5d661aSToomas Soome 448*4a5d661aSToomas Soome 449*4a5d661aSToomas Soome 450*4a5d661aSToomas SoomeDeutsch Informational [Page 8] 451*4a5d661aSToomas Soome 452*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 453*4a5d661aSToomas Soome 454*4a5d661aSToomas Soome 455*4a5d661aSToomas Soome } 456*4a5d661aSToomas Soome 457*4a5d661aSToomas Soome Example: 458*4a5d661aSToomas Soome 459*4a5d661aSToomas Soome Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, 460*4a5d661aSToomas Soome 3, 2, 4, 4). After step 1, we have: 461*4a5d661aSToomas Soome 462*4a5d661aSToomas Soome N bl_count[N] 463*4a5d661aSToomas Soome - ----------- 464*4a5d661aSToomas Soome 2 1 465*4a5d661aSToomas Soome 3 5 466*4a5d661aSToomas Soome 4 2 467*4a5d661aSToomas Soome 468*4a5d661aSToomas Soome Step 2 computes the following next_code values: 469*4a5d661aSToomas Soome 470*4a5d661aSToomas Soome N next_code[N] 471*4a5d661aSToomas Soome - ------------ 472*4a5d661aSToomas Soome 1 0 473*4a5d661aSToomas Soome 2 0 474*4a5d661aSToomas Soome 3 2 475*4a5d661aSToomas Soome 4 14 476*4a5d661aSToomas Soome 477*4a5d661aSToomas Soome Step 3 produces the following code values: 478*4a5d661aSToomas Soome 479*4a5d661aSToomas Soome Symbol Length Code 480*4a5d661aSToomas Soome ------ ------ ---- 481*4a5d661aSToomas Soome A 3 010 482*4a5d661aSToomas Soome B 3 011 483*4a5d661aSToomas Soome C 3 100 484*4a5d661aSToomas Soome D 3 101 485*4a5d661aSToomas Soome E 3 110 486*4a5d661aSToomas Soome F 2 00 487*4a5d661aSToomas Soome G 4 1110 488*4a5d661aSToomas Soome H 4 1111 489*4a5d661aSToomas Soome 490*4a5d661aSToomas Soome 3.2.3. Details of block format 491*4a5d661aSToomas Soome 492*4a5d661aSToomas Soome Each block of compressed data begins with 3 header bits 493*4a5d661aSToomas Soome containing the following data: 494*4a5d661aSToomas Soome 495*4a5d661aSToomas Soome first bit BFINAL 496*4a5d661aSToomas Soome next 2 bits BTYPE 497*4a5d661aSToomas Soome 498*4a5d661aSToomas Soome Note that the header bits do not necessarily begin on a byte 499*4a5d661aSToomas Soome boundary, since a block does not necessarily occupy an integral 500*4a5d661aSToomas Soome number of bytes. 501*4a5d661aSToomas Soome 502*4a5d661aSToomas Soome 503*4a5d661aSToomas Soome 504*4a5d661aSToomas Soome 505*4a5d661aSToomas Soome 506*4a5d661aSToomas SoomeDeutsch Informational [Page 9] 507*4a5d661aSToomas Soome 508*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 509*4a5d661aSToomas Soome 510*4a5d661aSToomas Soome 511*4a5d661aSToomas Soome BFINAL is set if and only if this is the last block of the data 512*4a5d661aSToomas Soome set. 513*4a5d661aSToomas Soome 514*4a5d661aSToomas Soome BTYPE specifies how the data are compressed, as follows: 515*4a5d661aSToomas Soome 516*4a5d661aSToomas Soome 00 - no compression 517*4a5d661aSToomas Soome 01 - compressed with fixed Huffman codes 518*4a5d661aSToomas Soome 10 - compressed with dynamic Huffman codes 519*4a5d661aSToomas Soome 11 - reserved (error) 520*4a5d661aSToomas Soome 521*4a5d661aSToomas Soome The only difference between the two compressed cases is how the 522*4a5d661aSToomas Soome Huffman codes for the literal/length and distance alphabets are 523*4a5d661aSToomas Soome defined. 524*4a5d661aSToomas Soome 525*4a5d661aSToomas Soome In all cases, the decoding algorithm for the actual data is as 526*4a5d661aSToomas Soome follows: 527*4a5d661aSToomas Soome 528*4a5d661aSToomas Soome do 529*4a5d661aSToomas Soome read block header from input stream. 530*4a5d661aSToomas Soome if stored with no compression 531*4a5d661aSToomas Soome skip any remaining bits in current partially 532*4a5d661aSToomas Soome processed byte 533*4a5d661aSToomas Soome read LEN and NLEN (see next section) 534*4a5d661aSToomas Soome copy LEN bytes of data to output 535*4a5d661aSToomas Soome otherwise 536*4a5d661aSToomas Soome if compressed with dynamic Huffman codes 537*4a5d661aSToomas Soome read representation of code trees (see 538*4a5d661aSToomas Soome subsection below) 539*4a5d661aSToomas Soome loop (until end of block code recognized) 540*4a5d661aSToomas Soome decode literal/length value from input stream 541*4a5d661aSToomas Soome if value < 256 542*4a5d661aSToomas Soome copy value (literal byte) to output stream 543*4a5d661aSToomas Soome otherwise 544*4a5d661aSToomas Soome if value = end of block (256) 545*4a5d661aSToomas Soome break from loop 546*4a5d661aSToomas Soome otherwise (value = 257..285) 547*4a5d661aSToomas Soome decode distance from input stream 548*4a5d661aSToomas Soome 549*4a5d661aSToomas Soome move backwards distance bytes in the output 550*4a5d661aSToomas Soome stream, and copy length bytes from this 551*4a5d661aSToomas Soome position to the output stream. 552*4a5d661aSToomas Soome end loop 553*4a5d661aSToomas Soome while not last block 554*4a5d661aSToomas Soome 555*4a5d661aSToomas Soome Note that a duplicated string reference may refer to a string 556*4a5d661aSToomas Soome in a previous block; i.e., the backward distance may cross one 557*4a5d661aSToomas Soome or more block boundaries. However a distance cannot refer past 558*4a5d661aSToomas Soome the beginning of the output stream. (An application using a 559*4a5d661aSToomas Soome 560*4a5d661aSToomas Soome 561*4a5d661aSToomas Soome 562*4a5d661aSToomas SoomeDeutsch Informational [Page 10] 563*4a5d661aSToomas Soome 564*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 565*4a5d661aSToomas Soome 566*4a5d661aSToomas Soome 567*4a5d661aSToomas Soome preset dictionary might discard part of the output stream; a 568*4a5d661aSToomas Soome distance can refer to that part of the output stream anyway) 569*4a5d661aSToomas Soome Note also that the referenced string may overlap the current 570*4a5d661aSToomas Soome position; for example, if the last 2 bytes decoded have values 571*4a5d661aSToomas Soome X and Y, a string reference with <length = 5, distance = 2> 572*4a5d661aSToomas Soome adds X,Y,X,Y,X to the output stream. 573*4a5d661aSToomas Soome 574*4a5d661aSToomas Soome We now specify each compression method in turn. 575*4a5d661aSToomas Soome 576*4a5d661aSToomas Soome 3.2.4. Non-compressed blocks (BTYPE=00) 577*4a5d661aSToomas Soome 578*4a5d661aSToomas Soome Any bits of input up to the next byte boundary are ignored. 579*4a5d661aSToomas Soome The rest of the block consists of the following information: 580*4a5d661aSToomas Soome 581*4a5d661aSToomas Soome 0 1 2 3 4... 582*4a5d661aSToomas Soome +---+---+---+---+================================+ 583*4a5d661aSToomas Soome | LEN | NLEN |... LEN bytes of literal data...| 584*4a5d661aSToomas Soome +---+---+---+---+================================+ 585*4a5d661aSToomas Soome 586*4a5d661aSToomas Soome LEN is the number of data bytes in the block. NLEN is the 587*4a5d661aSToomas Soome one's complement of LEN. 588*4a5d661aSToomas Soome 589*4a5d661aSToomas Soome 3.2.5. Compressed blocks (length and distance codes) 590*4a5d661aSToomas Soome 591*4a5d661aSToomas Soome As noted above, encoded data blocks in the "deflate" format 592*4a5d661aSToomas Soome consist of sequences of symbols drawn from three conceptually 593*4a5d661aSToomas Soome distinct alphabets: either literal bytes, from the alphabet of 594*4a5d661aSToomas Soome byte values (0..255), or <length, backward distance> pairs, 595*4a5d661aSToomas Soome where the length is drawn from (3..258) and the distance is 596*4a5d661aSToomas Soome drawn from (1..32,768). In fact, the literal and length 597*4a5d661aSToomas Soome alphabets are merged into a single alphabet (0..285), where 598*4a5d661aSToomas Soome values 0..255 represent literal bytes, the value 256 indicates 599*4a5d661aSToomas Soome end-of-block, and values 257..285 represent length codes 600*4a5d661aSToomas Soome (possibly in conjunction with extra bits following the symbol 601*4a5d661aSToomas Soome code) as follows: 602*4a5d661aSToomas Soome 603*4a5d661aSToomas Soome 604*4a5d661aSToomas Soome 605*4a5d661aSToomas Soome 606*4a5d661aSToomas Soome 607*4a5d661aSToomas Soome 608*4a5d661aSToomas Soome 609*4a5d661aSToomas Soome 610*4a5d661aSToomas Soome 611*4a5d661aSToomas Soome 612*4a5d661aSToomas Soome 613*4a5d661aSToomas Soome 614*4a5d661aSToomas Soome 615*4a5d661aSToomas Soome 616*4a5d661aSToomas Soome 617*4a5d661aSToomas Soome 618*4a5d661aSToomas SoomeDeutsch Informational [Page 11] 619*4a5d661aSToomas Soome 620*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 621*4a5d661aSToomas Soome 622*4a5d661aSToomas Soome 623*4a5d661aSToomas Soome Extra Extra Extra 624*4a5d661aSToomas Soome Code Bits Length(s) Code Bits Lengths Code Bits Length(s) 625*4a5d661aSToomas Soome ---- ---- ------ ---- ---- ------- ---- ---- ------- 626*4a5d661aSToomas Soome 257 0 3 267 1 15,16 277 4 67-82 627*4a5d661aSToomas Soome 258 0 4 268 1 17,18 278 4 83-98 628*4a5d661aSToomas Soome 259 0 5 269 2 19-22 279 4 99-114 629*4a5d661aSToomas Soome 260 0 6 270 2 23-26 280 4 115-130 630*4a5d661aSToomas Soome 261 0 7 271 2 27-30 281 5 131-162 631*4a5d661aSToomas Soome 262 0 8 272 2 31-34 282 5 163-194 632*4a5d661aSToomas Soome 263 0 9 273 3 35-42 283 5 195-226 633*4a5d661aSToomas Soome 264 0 10 274 3 43-50 284 5 227-257 634*4a5d661aSToomas Soome 265 1 11,12 275 3 51-58 285 0 258 635*4a5d661aSToomas Soome 266 1 13,14 276 3 59-66 636*4a5d661aSToomas Soome 637*4a5d661aSToomas Soome The extra bits should be interpreted as a machine integer 638*4a5d661aSToomas Soome stored with the most-significant bit first, e.g., bits 1110 639*4a5d661aSToomas Soome represent the value 14. 640*4a5d661aSToomas Soome 641*4a5d661aSToomas Soome Extra Extra Extra 642*4a5d661aSToomas Soome Code Bits Dist Code Bits Dist Code Bits Distance 643*4a5d661aSToomas Soome ---- ---- ---- ---- ---- ------ ---- ---- -------- 644*4a5d661aSToomas Soome 0 0 1 10 4 33-48 20 9 1025-1536 645*4a5d661aSToomas Soome 1 0 2 11 4 49-64 21 9 1537-2048 646*4a5d661aSToomas Soome 2 0 3 12 5 65-96 22 10 2049-3072 647*4a5d661aSToomas Soome 3 0 4 13 5 97-128 23 10 3073-4096 648*4a5d661aSToomas Soome 4 1 5,6 14 6 129-192 24 11 4097-6144 649*4a5d661aSToomas Soome 5 1 7,8 15 6 193-256 25 11 6145-8192 650*4a5d661aSToomas Soome 6 2 9-12 16 7 257-384 26 12 8193-12288 651*4a5d661aSToomas Soome 7 2 13-16 17 7 385-512 27 12 12289-16384 652*4a5d661aSToomas Soome 8 3 17-24 18 8 513-768 28 13 16385-24576 653*4a5d661aSToomas Soome 9 3 25-32 19 8 769-1024 29 13 24577-32768 654*4a5d661aSToomas Soome 655*4a5d661aSToomas Soome 3.2.6. Compression with fixed Huffman codes (BTYPE=01) 656*4a5d661aSToomas Soome 657*4a5d661aSToomas Soome The Huffman codes for the two alphabets are fixed, and are not 658*4a5d661aSToomas Soome represented explicitly in the data. The Huffman code lengths 659*4a5d661aSToomas Soome for the literal/length alphabet are: 660*4a5d661aSToomas Soome 661*4a5d661aSToomas Soome Lit Value Bits Codes 662*4a5d661aSToomas Soome --------- ---- ----- 663*4a5d661aSToomas Soome 0 - 143 8 00110000 through 664*4a5d661aSToomas Soome 10111111 665*4a5d661aSToomas Soome 144 - 255 9 110010000 through 666*4a5d661aSToomas Soome 111111111 667*4a5d661aSToomas Soome 256 - 279 7 0000000 through 668*4a5d661aSToomas Soome 0010111 669*4a5d661aSToomas Soome 280 - 287 8 11000000 through 670*4a5d661aSToomas Soome 11000111 671*4a5d661aSToomas Soome 672*4a5d661aSToomas Soome 673*4a5d661aSToomas Soome 674*4a5d661aSToomas SoomeDeutsch Informational [Page 12] 675*4a5d661aSToomas Soome 676*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 677*4a5d661aSToomas Soome 678*4a5d661aSToomas Soome 679*4a5d661aSToomas Soome The code lengths are sufficient to generate the actual codes, 680*4a5d661aSToomas Soome as described above; we show the codes in the table for added 681*4a5d661aSToomas Soome clarity. Literal/length values 286-287 will never actually 682*4a5d661aSToomas Soome occur in the compressed data, but participate in the code 683*4a5d661aSToomas Soome construction. 684*4a5d661aSToomas Soome 685*4a5d661aSToomas Soome Distance codes 0-31 are represented by (fixed-length) 5-bit 686*4a5d661aSToomas Soome codes, with possible additional bits as shown in the table 687*4a5d661aSToomas Soome shown in Paragraph 3.2.5, above. Note that distance codes 30- 688*4a5d661aSToomas Soome 31 will never actually occur in the compressed data. 689*4a5d661aSToomas Soome 690*4a5d661aSToomas Soome 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) 691*4a5d661aSToomas Soome 692*4a5d661aSToomas Soome The Huffman codes for the two alphabets appear in the block 693*4a5d661aSToomas Soome immediately after the header bits and before the actual 694*4a5d661aSToomas Soome compressed data, first the literal/length code and then the 695*4a5d661aSToomas Soome distance code. Each code is defined by a sequence of code 696*4a5d661aSToomas Soome lengths, as discussed in Paragraph 3.2.2, above. For even 697*4a5d661aSToomas Soome greater compactness, the code length sequences themselves are 698*4a5d661aSToomas Soome compressed using a Huffman code. The alphabet for code lengths 699*4a5d661aSToomas Soome is as follows: 700*4a5d661aSToomas Soome 701*4a5d661aSToomas Soome 0 - 15: Represent code lengths of 0 - 15 702*4a5d661aSToomas Soome 16: Copy the previous code length 3 - 6 times. 703*4a5d661aSToomas Soome The next 2 bits indicate repeat length 704*4a5d661aSToomas Soome (0 = 3, ... , 3 = 6) 705*4a5d661aSToomas Soome Example: Codes 8, 16 (+2 bits 11), 706*4a5d661aSToomas Soome 16 (+2 bits 10) will expand to 707*4a5d661aSToomas Soome 12 code lengths of 8 (1 + 6 + 5) 708*4a5d661aSToomas Soome 17: Repeat a code length of 0 for 3 - 10 times. 709*4a5d661aSToomas Soome (3 bits of length) 710*4a5d661aSToomas Soome 18: Repeat a code length of 0 for 11 - 138 times 711*4a5d661aSToomas Soome (7 bits of length) 712*4a5d661aSToomas Soome 713*4a5d661aSToomas Soome A code length of 0 indicates that the corresponding symbol in 714*4a5d661aSToomas Soome the literal/length or distance alphabet will not occur in the 715*4a5d661aSToomas Soome block, and should not participate in the Huffman code 716*4a5d661aSToomas Soome construction algorithm given earlier. If only one distance 717*4a5d661aSToomas Soome code is used, it is encoded using one bit, not zero bits; in 718*4a5d661aSToomas Soome this case there is a single code length of one, with one unused 719*4a5d661aSToomas Soome code. One distance code of zero bits means that there are no 720*4a5d661aSToomas Soome distance codes used at all (the data is all literals). 721*4a5d661aSToomas Soome 722*4a5d661aSToomas Soome We can now define the format of the block: 723*4a5d661aSToomas Soome 724*4a5d661aSToomas Soome 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) 725*4a5d661aSToomas Soome 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) 726*4a5d661aSToomas Soome 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) 727*4a5d661aSToomas Soome 728*4a5d661aSToomas Soome 729*4a5d661aSToomas Soome 730*4a5d661aSToomas SoomeDeutsch Informational [Page 13] 731*4a5d661aSToomas Soome 732*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 733*4a5d661aSToomas Soome 734*4a5d661aSToomas Soome 735*4a5d661aSToomas Soome (HCLEN + 4) x 3 bits: code lengths for the code length 736*4a5d661aSToomas Soome alphabet given just above, in the order: 16, 17, 18, 737*4a5d661aSToomas Soome 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 738*4a5d661aSToomas Soome 739*4a5d661aSToomas Soome These code lengths are interpreted as 3-bit integers 740*4a5d661aSToomas Soome (0-7); as above, a code length of 0 means the 741*4a5d661aSToomas Soome corresponding symbol (literal/length or distance code 742*4a5d661aSToomas Soome length) is not used. 743*4a5d661aSToomas Soome 744*4a5d661aSToomas Soome HLIT + 257 code lengths for the literal/length alphabet, 745*4a5d661aSToomas Soome encoded using the code length Huffman code 746*4a5d661aSToomas Soome 747*4a5d661aSToomas Soome HDIST + 1 code lengths for the distance alphabet, 748*4a5d661aSToomas Soome encoded using the code length Huffman code 749*4a5d661aSToomas Soome 750*4a5d661aSToomas Soome The actual compressed data of the block, 751*4a5d661aSToomas Soome encoded using the literal/length and distance Huffman 752*4a5d661aSToomas Soome codes 753*4a5d661aSToomas Soome 754*4a5d661aSToomas Soome The literal/length symbol 256 (end of data), 755*4a5d661aSToomas Soome encoded using the literal/length Huffman code 756*4a5d661aSToomas Soome 757*4a5d661aSToomas Soome The code length repeat codes can cross from HLIT + 257 to the 758*4a5d661aSToomas Soome HDIST + 1 code lengths. In other words, all code lengths form 759*4a5d661aSToomas Soome a single sequence of HLIT + HDIST + 258 values. 760*4a5d661aSToomas Soome 761*4a5d661aSToomas Soome 3.3. Compliance 762*4a5d661aSToomas Soome 763*4a5d661aSToomas Soome A compressor may limit further the ranges of values specified in 764*4a5d661aSToomas Soome the previous section and still be compliant; for example, it may 765*4a5d661aSToomas Soome limit the range of backward pointers to some value smaller than 766*4a5d661aSToomas Soome 32K. Similarly, a compressor may limit the size of blocks so that 767*4a5d661aSToomas Soome a compressible block fits in memory. 768*4a5d661aSToomas Soome 769*4a5d661aSToomas Soome A compliant decompressor must accept the full range of possible 770*4a5d661aSToomas Soome values defined in the previous section, and must accept blocks of 771*4a5d661aSToomas Soome arbitrary size. 772*4a5d661aSToomas Soome 773*4a5d661aSToomas Soome4. Compression algorithm details 774*4a5d661aSToomas Soome 775*4a5d661aSToomas Soome While it is the intent of this document to define the "deflate" 776*4a5d661aSToomas Soome compressed data format without reference to any particular 777*4a5d661aSToomas Soome compression algorithm, the format is related to the compressed 778*4a5d661aSToomas Soome formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below); 779*4a5d661aSToomas Soome since many variations of LZ77 are patented, it is strongly 780*4a5d661aSToomas Soome recommended that the implementor of a compressor follow the general 781*4a5d661aSToomas Soome algorithm presented here, which is known not to be patented per se. 782*4a5d661aSToomas Soome The material in this section is not part of the definition of the 783*4a5d661aSToomas Soome 784*4a5d661aSToomas Soome 785*4a5d661aSToomas Soome 786*4a5d661aSToomas SoomeDeutsch Informational [Page 14] 787*4a5d661aSToomas Soome 788*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 789*4a5d661aSToomas Soome 790*4a5d661aSToomas Soome 791*4a5d661aSToomas Soome specification per se, and a compressor need not follow it in order to 792*4a5d661aSToomas Soome be compliant. 793*4a5d661aSToomas Soome 794*4a5d661aSToomas Soome The compressor terminates a block when it determines that starting a 795*4a5d661aSToomas Soome new block with fresh trees would be useful, or when the block size 796*4a5d661aSToomas Soome fills up the compressor's block buffer. 797*4a5d661aSToomas Soome 798*4a5d661aSToomas Soome The compressor uses a chained hash table to find duplicated strings, 799*4a5d661aSToomas Soome using a hash function that operates on 3-byte sequences. At any 800*4a5d661aSToomas Soome given point during compression, let XYZ be the next 3 input bytes to 801*4a5d661aSToomas Soome be examined (not necessarily all different, of course). First, the 802*4a5d661aSToomas Soome compressor examines the hash chain for XYZ. If the chain is empty, 803*4a5d661aSToomas Soome the compressor simply writes out X as a literal byte and advances one 804*4a5d661aSToomas Soome byte in the input. If the hash chain is not empty, indicating that 805*4a5d661aSToomas Soome the sequence XYZ (or, if we are unlucky, some other 3 bytes with the 806*4a5d661aSToomas Soome same hash function value) has occurred recently, the compressor 807*4a5d661aSToomas Soome compares all strings on the XYZ hash chain with the actual input data 808*4a5d661aSToomas Soome sequence starting at the current point, and selects the longest 809*4a5d661aSToomas Soome match. 810*4a5d661aSToomas Soome 811*4a5d661aSToomas Soome The compressor searches the hash chains starting with the most recent 812*4a5d661aSToomas Soome strings, to favor small distances and thus take advantage of the 813*4a5d661aSToomas Soome Huffman encoding. The hash chains are singly linked. There are no 814*4a5d661aSToomas Soome deletions from the hash chains; the algorithm simply discards matches 815*4a5d661aSToomas Soome that are too old. To avoid a worst-case situation, very long hash 816*4a5d661aSToomas Soome chains are arbitrarily truncated at a certain length, determined by a 817*4a5d661aSToomas Soome run-time parameter. 818*4a5d661aSToomas Soome 819*4a5d661aSToomas Soome To improve overall compression, the compressor optionally defers the 820*4a5d661aSToomas Soome selection of matches ("lazy matching"): after a match of length N has 821*4a5d661aSToomas Soome been found, the compressor searches for a longer match starting at 822*4a5d661aSToomas Soome the next input byte. If it finds a longer match, it truncates the 823*4a5d661aSToomas Soome previous match to a length of one (thus producing a single literal 824*4a5d661aSToomas Soome byte) and then emits the longer match. Otherwise, it emits the 825*4a5d661aSToomas Soome original match, and, as described above, advances N bytes before 826*4a5d661aSToomas Soome continuing. 827*4a5d661aSToomas Soome 828*4a5d661aSToomas Soome Run-time parameters also control this "lazy match" procedure. If 829*4a5d661aSToomas Soome compression ratio is most important, the compressor attempts a 830*4a5d661aSToomas Soome complete second search regardless of the length of the first match. 831*4a5d661aSToomas Soome In the normal case, if the current match is "long enough", the 832*4a5d661aSToomas Soome compressor reduces the search for a longer match, thus speeding up 833*4a5d661aSToomas Soome the process. If speed is most important, the compressor inserts new 834*4a5d661aSToomas Soome strings in the hash table only when no match was found, or when the 835*4a5d661aSToomas Soome match is not "too long". This degrades the compression ratio but 836*4a5d661aSToomas Soome saves time since there are both fewer insertions and fewer searches. 837*4a5d661aSToomas Soome 838*4a5d661aSToomas Soome 839*4a5d661aSToomas Soome 840*4a5d661aSToomas Soome 841*4a5d661aSToomas Soome 842*4a5d661aSToomas SoomeDeutsch Informational [Page 15] 843*4a5d661aSToomas Soome 844*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 845*4a5d661aSToomas Soome 846*4a5d661aSToomas Soome 847*4a5d661aSToomas Soome5. References 848*4a5d661aSToomas Soome 849*4a5d661aSToomas Soome [1] Huffman, D. A., "A Method for the Construction of Minimum 850*4a5d661aSToomas Soome Redundancy Codes", Proceedings of the Institute of Radio 851*4a5d661aSToomas Soome Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101. 852*4a5d661aSToomas Soome 853*4a5d661aSToomas Soome [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data 854*4a5d661aSToomas Soome Compression", IEEE Transactions on Information Theory, Vol. 23, 855*4a5d661aSToomas Soome No. 3, pp. 337-343. 856*4a5d661aSToomas Soome 857*4a5d661aSToomas Soome [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources, 858*4a5d661aSToomas Soome available in ftp://ftp.uu.net/pub/archiving/zip/doc/ 859*4a5d661aSToomas Soome 860*4a5d661aSToomas Soome [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources, 861*4a5d661aSToomas Soome available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/ 862*4a5d661aSToomas Soome 863*4a5d661aSToomas Soome [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix 864*4a5d661aSToomas Soome encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169. 865*4a5d661aSToomas Soome 866*4a5d661aSToomas Soome [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes," 867*4a5d661aSToomas Soome Comm. ACM, 33,4, April 1990, pp. 449-459. 868*4a5d661aSToomas Soome 869*4a5d661aSToomas Soome6. Security Considerations 870*4a5d661aSToomas Soome 871*4a5d661aSToomas Soome Any data compression method involves the reduction of redundancy in 872*4a5d661aSToomas Soome the data. Consequently, any corruption of the data is likely to have 873*4a5d661aSToomas Soome severe effects and be difficult to correct. Uncompressed text, on 874*4a5d661aSToomas Soome the other hand, will probably still be readable despite the presence 875*4a5d661aSToomas Soome of some corrupted bytes. 876*4a5d661aSToomas Soome 877*4a5d661aSToomas Soome It is recommended that systems using this data format provide some 878*4a5d661aSToomas Soome means of validating the integrity of the compressed data. See 879*4a5d661aSToomas Soome reference [3], for example. 880*4a5d661aSToomas Soome 881*4a5d661aSToomas Soome7. Source code 882*4a5d661aSToomas Soome 883*4a5d661aSToomas Soome Source code for a C language implementation of a "deflate" compliant 884*4a5d661aSToomas Soome compressor and decompressor is available within the zlib package at 885*4a5d661aSToomas Soome ftp://ftp.uu.net/pub/archiving/zip/zlib/. 886*4a5d661aSToomas Soome 887*4a5d661aSToomas Soome8. Acknowledgements 888*4a5d661aSToomas Soome 889*4a5d661aSToomas Soome Trademarks cited in this document are the property of their 890*4a5d661aSToomas Soome respective owners. 891*4a5d661aSToomas Soome 892*4a5d661aSToomas Soome Phil Katz designed the deflate format. Jean-Loup Gailly and Mark 893*4a5d661aSToomas Soome Adler wrote the related software described in this specification. 894*4a5d661aSToomas Soome Glenn Randers-Pehrson converted this document to RFC and HTML format. 895*4a5d661aSToomas Soome 896*4a5d661aSToomas Soome 897*4a5d661aSToomas Soome 898*4a5d661aSToomas SoomeDeutsch Informational [Page 16] 899*4a5d661aSToomas Soome 900*4a5d661aSToomas SoomeRFC 1951 DEFLATE Compressed Data Format Specification May 1996 901*4a5d661aSToomas Soome 902*4a5d661aSToomas Soome 903*4a5d661aSToomas Soome9. Author's Address 904*4a5d661aSToomas Soome 905*4a5d661aSToomas Soome L. Peter Deutsch 906*4a5d661aSToomas Soome Aladdin Enterprises 907*4a5d661aSToomas Soome 203 Santa Margarita Ave. 908*4a5d661aSToomas Soome Menlo Park, CA 94025 909*4a5d661aSToomas Soome 910*4a5d661aSToomas Soome Phone: (415) 322-0103 (AM only) 911*4a5d661aSToomas Soome FAX: (415) 322-1734 912*4a5d661aSToomas Soome EMail: <ghost@aladdin.com> 913*4a5d661aSToomas Soome 914*4a5d661aSToomas Soome Questions about the technical content of this specification can be 915*4a5d661aSToomas Soome sent by email to: 916*4a5d661aSToomas Soome 917*4a5d661aSToomas Soome Jean-Loup Gailly <gzip@prep.ai.mit.edu> and 918*4a5d661aSToomas Soome Mark Adler <madler@alumni.caltech.edu> 919*4a5d661aSToomas Soome 920*4a5d661aSToomas Soome Editorial comments on this specification can be sent by email to: 921*4a5d661aSToomas Soome 922*4a5d661aSToomas Soome L. Peter Deutsch <ghost@aladdin.com> and 923*4a5d661aSToomas Soome Glenn Randers-Pehrson <randeg@alumni.rpi.edu> 924*4a5d661aSToomas Soome 925*4a5d661aSToomas Soome 926*4a5d661aSToomas Soome 927*4a5d661aSToomas Soome 928*4a5d661aSToomas Soome 929*4a5d661aSToomas Soome 930*4a5d661aSToomas Soome 931*4a5d661aSToomas Soome 932*4a5d661aSToomas Soome 933*4a5d661aSToomas Soome 934*4a5d661aSToomas Soome 935*4a5d661aSToomas Soome 936*4a5d661aSToomas Soome 937*4a5d661aSToomas Soome 938*4a5d661aSToomas Soome 939*4a5d661aSToomas Soome 940*4a5d661aSToomas Soome 941*4a5d661aSToomas Soome 942*4a5d661aSToomas Soome 943*4a5d661aSToomas Soome 944*4a5d661aSToomas Soome 945*4a5d661aSToomas Soome 946*4a5d661aSToomas Soome 947*4a5d661aSToomas Soome 948*4a5d661aSToomas Soome 949*4a5d661aSToomas Soome 950*4a5d661aSToomas Soome 951*4a5d661aSToomas Soome 952*4a5d661aSToomas Soome 953*4a5d661aSToomas Soome 954*4a5d661aSToomas SoomeDeutsch Informational [Page 17] 955*4a5d661aSToomas Soome 956