1275793eaSopenharmony_ci1. Compression algorithm (deflate)
2275793eaSopenharmony_ci
3275793eaSopenharmony_ciThe deflation algorithm used by gzip (also zip and zlib) is a variation of
4275793eaSopenharmony_ciLZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in
5275793eaSopenharmony_cithe input data.  The second occurrence of a string is replaced by a
6275793eaSopenharmony_cipointer to the previous string, in the form of a pair (distance,
7275793eaSopenharmony_cilength).  Distances are limited to 32K bytes, and lengths are limited
8275793eaSopenharmony_cito 258 bytes. When a string does not occur anywhere in the previous
9275793eaSopenharmony_ci32K bytes, it is emitted as a sequence of literal bytes.  (In this
10275793eaSopenharmony_cidescription, `string' must be taken as an arbitrary sequence of bytes,
11275793eaSopenharmony_ciand is not restricted to printable characters.)
12275793eaSopenharmony_ci
13275793eaSopenharmony_ciLiterals or match lengths are compressed with one Huffman tree, and
14275793eaSopenharmony_cimatch distances are compressed with another tree. The trees are stored
15275793eaSopenharmony_ciin a compact form at the start of each block. The blocks can have any
16275793eaSopenharmony_cisize (except that the compressed data for one block must fit in
17275793eaSopenharmony_ciavailable memory). A block is terminated when deflate() determines that
18275793eaSopenharmony_ciit would be useful to start another block with fresh trees. (This is
19275793eaSopenharmony_cisomewhat similar to the behavior of LZW-based _compress_.)
20275793eaSopenharmony_ci
21275793eaSopenharmony_ciDuplicated strings are found using a hash table. All input strings of
22275793eaSopenharmony_cilength 3 are inserted in the hash table. A hash index is computed for
23275793eaSopenharmony_cithe next 3 bytes. If the hash chain for this index is not empty, all
24275793eaSopenharmony_cistrings in the chain are compared with the current input string, and
25275793eaSopenharmony_cithe longest match is selected.
26275793eaSopenharmony_ci
27275793eaSopenharmony_ciThe hash chains are searched starting with the most recent strings, to
28275793eaSopenharmony_cifavor small distances and thus take advantage of the Huffman encoding.
29275793eaSopenharmony_ciThe hash chains are singly linked. There are no deletions from the
30275793eaSopenharmony_cihash chains, the algorithm simply discards matches that are too old.
31275793eaSopenharmony_ci
32275793eaSopenharmony_ciTo avoid a worst-case situation, very long hash chains are arbitrarily
33275793eaSopenharmony_citruncated at a certain length, determined by a runtime option (level
34275793eaSopenharmony_ciparameter of deflateInit). So deflate() does not always find the longest
35275793eaSopenharmony_cipossible match but generally finds a match which is long enough.
36275793eaSopenharmony_ci
37275793eaSopenharmony_cideflate() also defers the selection of matches with a lazy evaluation
38275793eaSopenharmony_cimechanism. After a match of length N has been found, deflate() searches for
39275793eaSopenharmony_cia longer match at the next input byte. If a longer match is found, the
40275793eaSopenharmony_ciprevious match is truncated to a length of one (thus producing a single
41275793eaSopenharmony_ciliteral byte) and the process of lazy evaluation begins again. Otherwise,
42275793eaSopenharmony_cithe original match is kept, and the next match search is attempted only N
43275793eaSopenharmony_cisteps later.
44275793eaSopenharmony_ci
45275793eaSopenharmony_ciThe lazy match evaluation is also subject to a runtime parameter. If
46275793eaSopenharmony_cithe current match is long enough, deflate() reduces the search for a longer
47275793eaSopenharmony_cimatch, thus speeding up the whole process. If compression ratio is more
48275793eaSopenharmony_ciimportant than speed, deflate() attempts a complete second search even if
49275793eaSopenharmony_cithe first match is already long enough.
50275793eaSopenharmony_ci
51275793eaSopenharmony_ciThe lazy match evaluation is not performed for the fastest compression
52275793eaSopenharmony_cimodes (level parameter 1 to 3). For these fast modes, new strings
53275793eaSopenharmony_ciare inserted in the hash table only when no match was found, or
54275793eaSopenharmony_ciwhen the match is not too long. This degrades the compression ratio
55275793eaSopenharmony_cibut saves time since there are both fewer insertions and fewer searches.
56275793eaSopenharmony_ci
57275793eaSopenharmony_ci
58275793eaSopenharmony_ci2. Decompression algorithm (inflate)
59275793eaSopenharmony_ci
60275793eaSopenharmony_ci2.1 Introduction
61275793eaSopenharmony_ci
62275793eaSopenharmony_ciThe key question is how to represent a Huffman code (or any prefix code) so
63275793eaSopenharmony_cithat you can decode fast.  The most important characteristic is that shorter
64275793eaSopenharmony_cicodes are much more common than longer codes, so pay attention to decoding the
65275793eaSopenharmony_cishort codes fast, and let the long codes take longer to decode.
66275793eaSopenharmony_ci
67275793eaSopenharmony_ciinflate() sets up a first level table that covers some number of bits of
68275793eaSopenharmony_ciinput less than the length of longest code.  It gets that many bits from the
69275793eaSopenharmony_cistream, and looks it up in the table.  The table will tell if the next
70275793eaSopenharmony_cicode is that many bits or less and how many, and if it is, it will tell
71275793eaSopenharmony_cithe value, else it will point to the next level table for which inflate()
72275793eaSopenharmony_cigrabs more bits and tries to decode a longer code.
73275793eaSopenharmony_ci
74275793eaSopenharmony_ciHow many bits to make the first lookup is a tradeoff between the time it
75275793eaSopenharmony_citakes to decode and the time it takes to build the table.  If building the
76275793eaSopenharmony_citable took no time (and if you had infinite memory), then there would only
77275793eaSopenharmony_cibe a first level table to cover all the way to the longest code.  However,
78275793eaSopenharmony_cibuilding the table ends up taking a lot longer for more bits since short
79275793eaSopenharmony_cicodes are replicated many times in such a table.  What inflate() does is
80275793eaSopenharmony_cisimply to make the number of bits in the first table a variable, and then
81275793eaSopenharmony_cito set that variable for the maximum speed.
82275793eaSopenharmony_ci
83275793eaSopenharmony_ciFor inflate, which has 286 possible codes for the literal/length tree, the size
84275793eaSopenharmony_ciof the first table is nine bits.  Also the distance trees have 30 possible
85275793eaSopenharmony_civalues, and the size of the first table is six bits.  Note that for each of
86275793eaSopenharmony_cithose cases, the table ended up one bit longer than the ``average'' code
87275793eaSopenharmony_cilength, i.e. the code length of an approximately flat code which would be a
88275793eaSopenharmony_cilittle more than eight bits for 286 symbols and a little less than five bits
89275793eaSopenharmony_cifor 30 symbols.
90275793eaSopenharmony_ci
91275793eaSopenharmony_ci
92275793eaSopenharmony_ci2.2 More details on the inflate table lookup
93275793eaSopenharmony_ci
94275793eaSopenharmony_ciOk, you want to know what this cleverly obfuscated inflate tree actually
95275793eaSopenharmony_cilooks like.  You are correct that it's not a Huffman tree.  It is simply a
96275793eaSopenharmony_cilookup table for the first, let's say, nine bits of a Huffman symbol.  The
97275793eaSopenharmony_cisymbol could be as short as one bit or as long as 15 bits.  If a particular
98275793eaSopenharmony_cisymbol is shorter than nine bits, then that symbol's translation is duplicated
99275793eaSopenharmony_ciin all those entries that start with that symbol's bits.  For example, if the
100275793eaSopenharmony_cisymbol is four bits, then it's duplicated 32 times in a nine-bit table.  If a
101275793eaSopenharmony_cisymbol is nine bits long, it appears in the table once.
102275793eaSopenharmony_ci
103275793eaSopenharmony_ciIf the symbol is longer than nine bits, then that entry in the table points
104275793eaSopenharmony_cito another similar table for the remaining bits.  Again, there are duplicated
105275793eaSopenharmony_cientries as needed.  The idea is that most of the time the symbol will be short
106275793eaSopenharmony_ciand there will only be one table look up.  (That's whole idea behind data
107275793eaSopenharmony_cicompression in the first place.)  For the less frequent long symbols, there
108275793eaSopenharmony_ciwill be two lookups.  If you had a compression method with really long
109275793eaSopenharmony_cisymbols, you could have as many levels of lookups as is efficient.  For
110275793eaSopenharmony_ciinflate, two is enough.
111275793eaSopenharmony_ci
112275793eaSopenharmony_ciSo a table entry either points to another table (in which case nine bits in
113275793eaSopenharmony_cithe above example are gobbled), or it contains the translation for the symbol
114275793eaSopenharmony_ciand the number of bits to gobble.  Then you start again with the next
115275793eaSopenharmony_ciungobbled bit.
116275793eaSopenharmony_ci
117275793eaSopenharmony_ciYou may wonder: why not just have one lookup table for how ever many bits the
118275793eaSopenharmony_cilongest symbol is?  The reason is that if you do that, you end up spending
119275793eaSopenharmony_cimore time filling in duplicate symbol entries than you do actually decoding.
120275793eaSopenharmony_ciAt least for deflate's output that generates new trees every several 10's of
121275793eaSopenharmony_cikbytes.  You can imagine that filling in a 2^15 entry table for a 15-bit code
122275793eaSopenharmony_ciwould take too long if you're only decoding several thousand symbols.  At the
123275793eaSopenharmony_ciother extreme, you could make a new table for every bit in the code.  In fact,
124275793eaSopenharmony_cithat's essentially a Huffman tree.  But then you spend too much time
125275793eaSopenharmony_citraversing the tree while decoding, even for short symbols.
126275793eaSopenharmony_ci
127275793eaSopenharmony_ciSo the number of bits for the first lookup table is a trade of the time to
128275793eaSopenharmony_cifill out the table vs. the time spent looking at the second level and above of
129275793eaSopenharmony_cithe table.
130275793eaSopenharmony_ci
131275793eaSopenharmony_ciHere is an example, scaled down:
132275793eaSopenharmony_ci
133275793eaSopenharmony_ciThe code being decoded, with 10 symbols, from 1 to 6 bits long:
134275793eaSopenharmony_ci
135275793eaSopenharmony_ciA: 0
136275793eaSopenharmony_ciB: 10
137275793eaSopenharmony_ciC: 1100
138275793eaSopenharmony_ciD: 11010
139275793eaSopenharmony_ciE: 11011
140275793eaSopenharmony_ciF: 11100
141275793eaSopenharmony_ciG: 11101
142275793eaSopenharmony_ciH: 11110
143275793eaSopenharmony_ciI: 111110
144275793eaSopenharmony_ciJ: 111111
145275793eaSopenharmony_ci
146275793eaSopenharmony_ciLet's make the first table three bits long (eight entries):
147275793eaSopenharmony_ci
148275793eaSopenharmony_ci000: A,1
149275793eaSopenharmony_ci001: A,1
150275793eaSopenharmony_ci010: A,1
151275793eaSopenharmony_ci011: A,1
152275793eaSopenharmony_ci100: B,2
153275793eaSopenharmony_ci101: B,2
154275793eaSopenharmony_ci110: -> table X (gobble 3 bits)
155275793eaSopenharmony_ci111: -> table Y (gobble 3 bits)
156275793eaSopenharmony_ci
157275793eaSopenharmony_ciEach entry is what the bits decode as and how many bits that is, i.e. how
158275793eaSopenharmony_cimany bits to gobble.  Or the entry points to another table, with the number of
159275793eaSopenharmony_cibits to gobble implicit in the size of the table.
160275793eaSopenharmony_ci
161275793eaSopenharmony_ciTable X is two bits long since the longest code starting with 110 is five bits
162275793eaSopenharmony_cilong:
163275793eaSopenharmony_ci
164275793eaSopenharmony_ci00: C,1
165275793eaSopenharmony_ci01: C,1
166275793eaSopenharmony_ci10: D,2
167275793eaSopenharmony_ci11: E,2
168275793eaSopenharmony_ci
169275793eaSopenharmony_ciTable Y is three bits long since the longest code starting with 111 is six
170275793eaSopenharmony_cibits long:
171275793eaSopenharmony_ci
172275793eaSopenharmony_ci000: F,2
173275793eaSopenharmony_ci001: F,2
174275793eaSopenharmony_ci010: G,2
175275793eaSopenharmony_ci011: G,2
176275793eaSopenharmony_ci100: H,2
177275793eaSopenharmony_ci101: H,2
178275793eaSopenharmony_ci110: I,3
179275793eaSopenharmony_ci111: J,3
180275793eaSopenharmony_ci
181275793eaSopenharmony_ciSo what we have here are three tables with a total of 20 entries that had to
182275793eaSopenharmony_cibe constructed.  That's compared to 64 entries for a single table.  Or
183275793eaSopenharmony_cicompared to 16 entries for a Huffman tree (six two entry tables and one four
184275793eaSopenharmony_cientry table).  Assuming that the code ideally represents the probability of
185275793eaSopenharmony_cithe symbols, it takes on the average 1.25 lookups per symbol.  That's compared
186275793eaSopenharmony_cito one lookup for the single table, or 1.66 lookups per symbol for the
187275793eaSopenharmony_ciHuffman tree.
188275793eaSopenharmony_ci
189275793eaSopenharmony_ciThere, I think that gives you a picture of what's going on.  For inflate, the
190275793eaSopenharmony_cimeaning of a particular symbol is often more than just a letter.  It can be a
191275793eaSopenharmony_cibyte (a "literal"), or it can be either a length or a distance which
192275793eaSopenharmony_ciindicates a base value and a number of bits to fetch after the code that is
193275793eaSopenharmony_ciadded to the base value.  Or it might be the special end-of-block code.  The
194275793eaSopenharmony_cidata structures created in inftrees.c try to encode all that information
195275793eaSopenharmony_cicompactly in the tables.
196275793eaSopenharmony_ci
197275793eaSopenharmony_ci
198275793eaSopenharmony_ciJean-loup Gailly        Mark Adler
199275793eaSopenharmony_cijloup@gzip.org          madler@alumni.caltech.edu
200275793eaSopenharmony_ci
201275793eaSopenharmony_ci
202275793eaSopenharmony_ciReferences:
203275793eaSopenharmony_ci
204275793eaSopenharmony_ci[LZ77] Ziv J., Lempel A., ``A Universal Algorithm for Sequential Data
205275793eaSopenharmony_ciCompression,'' IEEE Transactions on Information Theory, Vol. 23, No. 3,
206275793eaSopenharmony_cipp. 337-343.
207275793eaSopenharmony_ci
208275793eaSopenharmony_ci``DEFLATE Compressed Data Format Specification'' available in
209275793eaSopenharmony_cihttp://tools.ietf.org/html/rfc1951
210