1c9083b85SXin LI1. Compression algorithm (deflate) 2c9083b85SXin LI 3c9083b85SXin LIThe deflation algorithm used by gzip (also zip and zlib) is a variation of 4c9083b85SXin LILZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in 5c9083b85SXin LIthe input data. The second occurrence of a string is replaced by a 6c9083b85SXin LIpointer to the previous string, in the form of a pair (distance, 7c9083b85SXin LIlength). Distances are limited to 32K bytes, and lengths are limited 8c9083b85SXin LIto 258 bytes. When a string does not occur anywhere in the previous 9c9083b85SXin LI32K bytes, it is emitted as a sequence of literal bytes. (In this 10c9083b85SXin LIdescription, `string' must be taken as an arbitrary sequence of bytes, 11c9083b85SXin LIand is not restricted to printable characters.) 12c9083b85SXin LI 13c9083b85SXin LILiterals or match lengths are compressed with one Huffman tree, and 14c9083b85SXin LImatch distances are compressed with another tree. The trees are stored 15c9083b85SXin LIin a compact form at the start of each block. The blocks can have any 16c9083b85SXin LIsize (except that the compressed data for one block must fit in 17c9083b85SXin LIavailable memory). A block is terminated when deflate() determines that 18c9083b85SXin LIit would be useful to start another block with fresh trees. (This is 19c9083b85SXin LIsomewhat similar to the behavior of LZW-based _compress_.) 20c9083b85SXin LI 21c9083b85SXin LIDuplicated strings are found using a hash table. All input strings of 22c9083b85SXin LIlength 3 are inserted in the hash table. A hash index is computed for 23c9083b85SXin LIthe next 3 bytes. If the hash chain for this index is not empty, all 24c9083b85SXin LIstrings in the chain are compared with the current input string, and 25c9083b85SXin LIthe longest match is selected. 26c9083b85SXin LI 27c9083b85SXin LIThe hash chains are searched starting with the most recent strings, to 28c9083b85SXin LIfavor small distances and thus take advantage of the Huffman encoding. 29c9083b85SXin LIThe hash chains are singly linked. There are no deletions from the 30c9083b85SXin LIhash chains, the algorithm simply discards matches that are too old. 31c9083b85SXin LI 32c9083b85SXin LITo avoid a worst-case situation, very long hash chains are arbitrarily 33c9083b85SXin LItruncated at a certain length, determined by a runtime option (level 34c9083b85SXin LIparameter of deflateInit). So deflate() does not always find the longest 35c9083b85SXin LIpossible match but generally finds a match which is long enough. 36c9083b85SXin LI 37c9083b85SXin LIdeflate() also defers the selection of matches with a lazy evaluation 38c9083b85SXin LImechanism. After a match of length N has been found, deflate() searches for 39c9083b85SXin LIa longer match at the next input byte. If a longer match is found, the 40c9083b85SXin LIprevious match is truncated to a length of one (thus producing a single 41c9083b85SXin LIliteral byte) and the process of lazy evaluation begins again. Otherwise, 42c9083b85SXin LIthe original match is kept, and the next match search is attempted only N 43c9083b85SXin LIsteps later. 44c9083b85SXin LI 45c9083b85SXin LIThe lazy match evaluation is also subject to a runtime parameter. If 46c9083b85SXin LIthe current match is long enough, deflate() reduces the search for a longer 47c9083b85SXin LImatch, thus speeding up the whole process. If compression ratio is more 48c9083b85SXin LIimportant than speed, deflate() attempts a complete second search even if 49c9083b85SXin LIthe first match is already long enough. 50c9083b85SXin LI 51c9083b85SXin LIThe lazy match evaluation is not performed for the fastest compression 52c9083b85SXin LImodes (level parameter 1 to 3). For these fast modes, new strings 53c9083b85SXin LIare inserted in the hash table only when no match was found, or 54c9083b85SXin LIwhen the match is not too long. This degrades the compression ratio 55c9083b85SXin LIbut saves time since there are both fewer insertions and fewer searches. 56c9083b85SXin LI 57c9083b85SXin LI 58c9083b85SXin LI2. Decompression algorithm (inflate) 59c9083b85SXin LI 60c9083b85SXin LI2.1 Introduction 61c9083b85SXin LI 62c9083b85SXin LIThe key question is how to represent a Huffman code (or any prefix code) so 63c9083b85SXin LIthat you can decode fast. The most important characteristic is that shorter 64c9083b85SXin LIcodes are much more common than longer codes, so pay attention to decoding the 65c9083b85SXin LIshort codes fast, and let the long codes take longer to decode. 66c9083b85SXin LI 67c9083b85SXin LIinflate() sets up a first level table that covers some number of bits of 68c9083b85SXin LIinput less than the length of longest code. It gets that many bits from the 69c9083b85SXin LIstream, and looks it up in the table. The table will tell if the next 70c9083b85SXin LIcode is that many bits or less and how many, and if it is, it will tell 71c9083b85SXin LIthe value, else it will point to the next level table for which inflate() 72c9083b85SXin LIgrabs more bits and tries to decode a longer code. 73c9083b85SXin LI 74c9083b85SXin LIHow many bits to make the first lookup is a tradeoff between the time it 75c9083b85SXin LItakes to decode and the time it takes to build the table. If building the 76c9083b85SXin LItable took no time (and if you had infinite memory), then there would only 77c9083b85SXin LIbe a first level table to cover all the way to the longest code. However, 78c9083b85SXin LIbuilding the table ends up taking a lot longer for more bits since short 79c9083b85SXin LIcodes are replicated many times in such a table. What inflate() does is 80c9083b85SXin LIsimply to make the number of bits in the first table a variable, and then 81c9083b85SXin LIto set that variable for the maximum speed. 82c9083b85SXin LI 83c9083b85SXin LIFor inflate, which has 286 possible codes for the literal/length tree, the size 84c9083b85SXin LIof the first table is nine bits. Also the distance trees have 30 possible 85c9083b85SXin LIvalues, and the size of the first table is six bits. Note that for each of 86c9083b85SXin LIthose cases, the table ended up one bit longer than the ``average'' code 87c9083b85SXin LIlength, i.e. the code length of an approximately flat code which would be a 88c9083b85SXin LIlittle more than eight bits for 286 symbols and a little less than five bits 89c9083b85SXin LIfor 30 symbols. 90c9083b85SXin LI 91c9083b85SXin LI 92c9083b85SXin LI2.2 More details on the inflate table lookup 93c9083b85SXin LI 94c9083b85SXin LIOk, you want to know what this cleverly obfuscated inflate tree actually 95c9083b85SXin LIlooks like. You are correct that it's not a Huffman tree. It is simply a 96c9083b85SXin LIlookup table for the first, let's say, nine bits of a Huffman symbol. The 97c9083b85SXin LIsymbol could be as short as one bit or as long as 15 bits. If a particular 98c9083b85SXin LIsymbol is shorter than nine bits, then that symbol's translation is duplicated 99c9083b85SXin LIin all those entries that start with that symbol's bits. For example, if the 100c9083b85SXin LIsymbol is four bits, then it's duplicated 32 times in a nine-bit table. If a 101c9083b85SXin LIsymbol is nine bits long, it appears in the table once. 102c9083b85SXin LI 103c9083b85SXin LIIf the symbol is longer than nine bits, then that entry in the table points 104c9083b85SXin LIto another similar table for the remaining bits. Again, there are duplicated 105c9083b85SXin LIentries as needed. The idea is that most of the time the symbol will be short 106c9083b85SXin LIand there will only be one table look up. (That's whole idea behind data 107c9083b85SXin LIcompression in the first place.) For the less frequent long symbols, there 108c9083b85SXin LIwill be two lookups. If you had a compression method with really long 109c9083b85SXin LIsymbols, you could have as many levels of lookups as is efficient. For 110c9083b85SXin LIinflate, two is enough. 111c9083b85SXin LI 112c9083b85SXin LISo a table entry either points to another table (in which case nine bits in 113c9083b85SXin LIthe above example are gobbled), or it contains the translation for the symbol 114c9083b85SXin LIand the number of bits to gobble. Then you start again with the next 115c9083b85SXin LIungobbled bit. 116c9083b85SXin LI 117c9083b85SXin LIYou may wonder: why not just have one lookup table for how ever many bits the 118c9083b85SXin LIlongest symbol is? The reason is that if you do that, you end up spending 119c9083b85SXin LImore time filling in duplicate symbol entries than you do actually decoding. 120c9083b85SXin LIAt least for deflate's output that generates new trees every several 10's of 121c9083b85SXin LIkbytes. You can imagine that filling in a 2^15 entry table for a 15-bit code 122c9083b85SXin LIwould take too long if you're only decoding several thousand symbols. At the 123c9083b85SXin LIother extreme, you could make a new table for every bit in the code. In fact, 124c9083b85SXin LIthat's essentially a Huffman tree. But then you spend too much time 125c9083b85SXin LItraversing the tree while decoding, even for short symbols. 126c9083b85SXin LI 127c9083b85SXin LISo the number of bits for the first lookup table is a trade of the time to 128c9083b85SXin LIfill out the table vs. the time spent looking at the second level and above of 129c9083b85SXin LIthe table. 130c9083b85SXin LI 131c9083b85SXin LIHere is an example, scaled down: 132c9083b85SXin LI 133c9083b85SXin LIThe code being decoded, with 10 symbols, from 1 to 6 bits long: 134c9083b85SXin LI 135c9083b85SXin LIA: 0 136c9083b85SXin LIB: 10 137c9083b85SXin LIC: 1100 138c9083b85SXin LID: 11010 139c9083b85SXin LIE: 11011 140c9083b85SXin LIF: 11100 141c9083b85SXin LIG: 11101 142c9083b85SXin LIH: 11110 143c9083b85SXin LII: 111110 144c9083b85SXin LIJ: 111111 145c9083b85SXin LI 146c9083b85SXin LILet's make the first table three bits long (eight entries): 147c9083b85SXin LI 148c9083b85SXin LI000: A,1 149c9083b85SXin LI001: A,1 150c9083b85SXin LI010: A,1 151c9083b85SXin LI011: A,1 152c9083b85SXin LI100: B,2 153c9083b85SXin LI101: B,2 154c9083b85SXin LI110: -> table X (gobble 3 bits) 155c9083b85SXin LI111: -> table Y (gobble 3 bits) 156c9083b85SXin LI 157c9083b85SXin LIEach entry is what the bits decode as and how many bits that is, i.e. how 158c9083b85SXin LImany bits to gobble. Or the entry points to another table, with the number of 159c9083b85SXin LIbits to gobble implicit in the size of the table. 160c9083b85SXin LI 161c9083b85SXin LITable X is two bits long since the longest code starting with 110 is five bits 162c9083b85SXin LIlong: 163c9083b85SXin LI 164c9083b85SXin LI00: C,1 165c9083b85SXin LI01: C,1 166c9083b85SXin LI10: D,2 167c9083b85SXin LI11: E,2 168c9083b85SXin LI 169c9083b85SXin LITable Y is three bits long since the longest code starting with 111 is six 170c9083b85SXin LIbits long: 171c9083b85SXin LI 172c9083b85SXin LI000: F,2 173c9083b85SXin LI001: F,2 174c9083b85SXin LI010: G,2 175c9083b85SXin LI011: G,2 176c9083b85SXin LI100: H,2 177c9083b85SXin LI101: H,2 178c9083b85SXin LI110: I,3 179c9083b85SXin LI111: J,3 180c9083b85SXin LI 181c9083b85SXin LISo what we have here are three tables with a total of 20 entries that had to 182c9083b85SXin LIbe constructed. That's compared to 64 entries for a single table. Or 183c9083b85SXin LIcompared to 16 entries for a Huffman tree (six two entry tables and one four 184c9083b85SXin LIentry table). Assuming that the code ideally represents the probability of 185c9083b85SXin LIthe symbols, it takes on the average 1.25 lookups per symbol. That's compared 186c9083b85SXin LIto one lookup for the single table, or 1.66 lookups per symbol for the 187c9083b85SXin LIHuffman tree. 188c9083b85SXin LI 189c9083b85SXin LIThere, I think that gives you a picture of what's going on. For inflate, the 190c9083b85SXin LImeaning of a particular symbol is often more than just a letter. It can be a 191c9083b85SXin LIbyte (a "literal"), or it can be either a length or a distance which 192c9083b85SXin LIindicates a base value and a number of bits to fetch after the code that is 193c9083b85SXin LIadded to the base value. Or it might be the special end-of-block code. The 194c9083b85SXin LIdata structures created in inftrees.c try to encode all that information 195c9083b85SXin LIcompactly in the tables. 196c9083b85SXin LI 197c9083b85SXin LI 198c9083b85SXin LIJean-loup Gailly Mark Adler 199c9083b85SXin LIjloup@gzip.org madler@alumni.caltech.edu 200c9083b85SXin LI 201c9083b85SXin LI 202c9083b85SXin LIReferences: 203c9083b85SXin LI 204c9083b85SXin LI[LZ77] Ziv J., Lempel A., ``A Universal Algorithm for Sequential Data 205c9083b85SXin LICompression,'' IEEE Transactions on Information Theory, Vol. 23, No. 3, 206c9083b85SXin LIpp. 337-343. 207c9083b85SXin LI 208c9083b85SXin LI``DEFLATE Compressed Data Format Specification'' available in 209c9083b85SXin LIhttp://tools.ietf.org/html/rfc1951 210