1*c9431fa1Sahl /* inftrees.c -- generate Huffman trees for efficient decoding
2*c9431fa1Sahl * Copyright (C) 1995-2005 Mark Adler
3*c9431fa1Sahl * For conditions of distribution and use, see copyright notice in zlib.h
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
77c478bd9Sstevel@tonic-gate
87c478bd9Sstevel@tonic-gate #include "zutil.h"
97c478bd9Sstevel@tonic-gate #include "inftrees.h"
107c478bd9Sstevel@tonic-gate
11*c9431fa1Sahl #define MAXBITS 15
127c478bd9Sstevel@tonic-gate
13*c9431fa1Sahl static const char inflate_copyright[] =
14*c9431fa1Sahl " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
157c478bd9Sstevel@tonic-gate /*
16*c9431fa1Sahl If you use the zlib library in a product, an acknowledgment is welcome
17*c9431fa1Sahl in the documentation of your product. If for some reason you cannot
18*c9431fa1Sahl include such an acknowledgment, I would appreciate that you keep this
19*c9431fa1Sahl copyright string in the executable of your product.
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
22*c9431fa1Sahl /*
23*c9431fa1Sahl Build a set of tables to decode the provided canonical Huffman code.
24*c9431fa1Sahl The code lengths are lens[0..codes-1]. The result starts at *table,
25*c9431fa1Sahl whose indices are 0..2^bits-1. work is a writable array of at least
26*c9431fa1Sahl lens shorts, which is used as a work area. type is the type of code
27*c9431fa1Sahl to be generated, CODES, LENS, or DISTS. On return, zero is success,
28*c9431fa1Sahl -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
29*c9431fa1Sahl on return points to the next available entry's address. bits is the
30*c9431fa1Sahl requested root table index bits, and on return it is the actual root
31*c9431fa1Sahl table index bits. It will differ if the request is greater than the
32*c9431fa1Sahl longest code or if it is less than the shortest code.
33*c9431fa1Sahl */
inflate_table(type,lens,codes,table,bits,work)34*c9431fa1Sahl int inflate_table(type, lens, codes, table, bits, work)
35*c9431fa1Sahl codetype type;
36*c9431fa1Sahl unsigned short FAR *lens;
37*c9431fa1Sahl unsigned codes;
38*c9431fa1Sahl code FAR * FAR *table;
39*c9431fa1Sahl unsigned FAR *bits;
40*c9431fa1Sahl unsigned short FAR *work;
417c478bd9Sstevel@tonic-gate {
42*c9431fa1Sahl unsigned len; /* a code's length in bits */
43*c9431fa1Sahl unsigned sym; /* index of code symbols */
44*c9431fa1Sahl unsigned min, max; /* minimum and maximum code lengths */
45*c9431fa1Sahl unsigned root; /* number of index bits for root table */
46*c9431fa1Sahl unsigned curr; /* number of index bits for current table */
47*c9431fa1Sahl unsigned drop; /* code bits to drop for sub-table */
48*c9431fa1Sahl int left; /* number of prefix codes available */
49*c9431fa1Sahl unsigned used; /* code entries in table used */
50*c9431fa1Sahl unsigned huff; /* Huffman code */
51*c9431fa1Sahl unsigned incr; /* for incrementing code, index */
52*c9431fa1Sahl unsigned fill; /* index for replicating entries */
53*c9431fa1Sahl unsigned low; /* low bits for current root entry */
54*c9431fa1Sahl unsigned mask; /* mask for low root bits */
55*c9431fa1Sahl code this; /* table entry for duplication */
56*c9431fa1Sahl code FAR *next; /* next available space in table */
57*c9431fa1Sahl const unsigned short FAR *base; /* base value table to use */
58*c9431fa1Sahl const unsigned short FAR *extra; /* extra bits table to use */
59*c9431fa1Sahl int end; /* use base and extra for symbol > end */
60*c9431fa1Sahl unsigned short count[MAXBITS+1]; /* number of codes of each length */
61*c9431fa1Sahl unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
62*c9431fa1Sahl static const unsigned short lbase[31] = { /* Length codes 257..285 base */
63*c9431fa1Sahl 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
64*c9431fa1Sahl 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
65*c9431fa1Sahl static const unsigned short lext[31] = { /* Length codes 257..285 extra */
66*c9431fa1Sahl 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
67*c9431fa1Sahl 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
68*c9431fa1Sahl static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
69*c9431fa1Sahl 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
70*c9431fa1Sahl 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
71*c9431fa1Sahl 8193, 12289, 16385, 24577, 0, 0};
72*c9431fa1Sahl static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
73*c9431fa1Sahl 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
74*c9431fa1Sahl 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
75*c9431fa1Sahl 28, 28, 29, 29, 64, 64};
767c478bd9Sstevel@tonic-gate
77*c9431fa1Sahl /*
78*c9431fa1Sahl Process a set of code lengths to create a canonical Huffman code. The
79*c9431fa1Sahl code lengths are lens[0..codes-1]. Each length corresponds to the
80*c9431fa1Sahl symbols 0..codes-1. The Huffman code is generated by first sorting the
81*c9431fa1Sahl symbols by length from short to long, and retaining the symbol order
82*c9431fa1Sahl for codes with equal lengths. Then the code starts with all zero bits
83*c9431fa1Sahl for the first code of the shortest length, and the codes are integer
84*c9431fa1Sahl increments for the same length, and zeros are appended as the length
85*c9431fa1Sahl increases. For the deflate format, these bits are stored backwards
86*c9431fa1Sahl from their more natural integer increment ordering, and so when the
87*c9431fa1Sahl decoding tables are built in the large loop below, the integer codes
88*c9431fa1Sahl are incremented backwards.
897c478bd9Sstevel@tonic-gate
90*c9431fa1Sahl This routine assumes, but does not check, that all of the entries in
91*c9431fa1Sahl lens[] are in the range 0..MAXBITS. The caller must assure this.
92*c9431fa1Sahl 1..MAXBITS is interpreted as that code length. zero means that that
93*c9431fa1Sahl symbol does not occur in this code.
947c478bd9Sstevel@tonic-gate
95*c9431fa1Sahl The codes are sorted by computing a count of codes for each length,
96*c9431fa1Sahl creating from that a table of starting indices for each length in the
97*c9431fa1Sahl sorted table, and then entering the symbols in order in the sorted
98*c9431fa1Sahl table. The sorted table is work[], with that space being provided by
99*c9431fa1Sahl the caller.
100*c9431fa1Sahl
101*c9431fa1Sahl The length counts are used for other purposes as well, i.e. finding
102*c9431fa1Sahl the minimum and maximum length codes, determining if there are any
103*c9431fa1Sahl codes at all, checking for a valid set of lengths, and looking ahead
104*c9431fa1Sahl at length counts to determine sub-table sizes when building the
105*c9431fa1Sahl decoding tables.
106*c9431fa1Sahl */
107*c9431fa1Sahl
108*c9431fa1Sahl /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
109*c9431fa1Sahl for (len = 0; len <= MAXBITS; len++)
110*c9431fa1Sahl count[len] = 0;
111*c9431fa1Sahl for (sym = 0; sym < codes; sym++)
112*c9431fa1Sahl count[lens[sym]]++;
113*c9431fa1Sahl
114*c9431fa1Sahl /* bound code lengths, force root to be within code lengths */
115*c9431fa1Sahl root = *bits;
116*c9431fa1Sahl for (max = MAXBITS; max >= 1; max--)
117*c9431fa1Sahl if (count[max] != 0) break;
118*c9431fa1Sahl if (root > max) root = max;
119*c9431fa1Sahl if (max == 0) { /* no symbols to code at all */
120*c9431fa1Sahl this.op = (unsigned char)64; /* invalid code marker */
121*c9431fa1Sahl this.bits = (unsigned char)1;
122*c9431fa1Sahl this.val = (unsigned short)0;
123*c9431fa1Sahl *(*table)++ = this; /* make a table to force an error */
124*c9431fa1Sahl *(*table)++ = this;
125*c9431fa1Sahl *bits = 1;
126*c9431fa1Sahl return 0; /* no symbols, but wait for decoding to report error */
1277c478bd9Sstevel@tonic-gate }
128*c9431fa1Sahl for (min = 1; min <= MAXBITS; min++)
129*c9431fa1Sahl if (count[min] != 0) break;
130*c9431fa1Sahl if (root < min) root = min;
1317c478bd9Sstevel@tonic-gate
132*c9431fa1Sahl /* check for an over-subscribed or incomplete set of lengths */
133*c9431fa1Sahl left = 1;
134*c9431fa1Sahl for (len = 1; len <= MAXBITS; len++) {
135*c9431fa1Sahl left <<= 1;
136*c9431fa1Sahl left -= count[len];
137*c9431fa1Sahl if (left < 0) return -1; /* over-subscribed */
138*c9431fa1Sahl }
139*c9431fa1Sahl if (left > 0 && (type == CODES || max != 1))
140*c9431fa1Sahl return -1; /* incomplete set */
1417c478bd9Sstevel@tonic-gate
142*c9431fa1Sahl /* generate offsets into symbol table for each length for sorting */
143*c9431fa1Sahl offs[1] = 0;
144*c9431fa1Sahl for (len = 1; len < MAXBITS; len++)
145*c9431fa1Sahl offs[len + 1] = offs[len] + count[len];
146*c9431fa1Sahl
147*c9431fa1Sahl /* sort symbols by length, by symbol order within each length */
148*c9431fa1Sahl for (sym = 0; sym < codes; sym++)
149*c9431fa1Sahl if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
150*c9431fa1Sahl
151*c9431fa1Sahl /*
152*c9431fa1Sahl Create and fill in decoding tables. In this loop, the table being
153*c9431fa1Sahl filled is at next and has curr index bits. The code being used is huff
154*c9431fa1Sahl with length len. That code is converted to an index by dropping drop
155*c9431fa1Sahl bits off of the bottom. For codes where len is less than drop + curr,
156*c9431fa1Sahl those top drop + curr - len bits are incremented through all values to
157*c9431fa1Sahl fill the table with replicated entries.
158*c9431fa1Sahl
159*c9431fa1Sahl root is the number of index bits for the root table. When len exceeds
160*c9431fa1Sahl root, sub-tables are created pointed to by the root entry with an index
161*c9431fa1Sahl of the low root bits of huff. This is saved in low to check for when a
162*c9431fa1Sahl new sub-table should be started. drop is zero when the root table is
163*c9431fa1Sahl being filled, and drop is root when sub-tables are being filled.
164*c9431fa1Sahl
165*c9431fa1Sahl When a new sub-table is needed, it is necessary to look ahead in the
166*c9431fa1Sahl code lengths to determine what size sub-table is needed. The length
167*c9431fa1Sahl counts are used for this, and so count[] is decremented as codes are
168*c9431fa1Sahl entered in the tables.
169*c9431fa1Sahl
170*c9431fa1Sahl used keeps track of how many table entries have been allocated from the
171*c9431fa1Sahl provided *table space. It is checked when a LENS table is being made
172*c9431fa1Sahl against the space in *table, ENOUGH, minus the maximum space needed by
173*c9431fa1Sahl the worst case distance code, MAXD. This should never happen, but the
174*c9431fa1Sahl sufficiency of ENOUGH has not been proven exhaustively, hence the check.
175*c9431fa1Sahl This assumes that when type == LENS, bits == 9.
176*c9431fa1Sahl
177*c9431fa1Sahl sym increments through all symbols, and the loop terminates when
178*c9431fa1Sahl all codes of length max, i.e. all codes, have been processed. This
179*c9431fa1Sahl routine permits incomplete codes, so another loop after this one fills
180*c9431fa1Sahl in the rest of the decoding tables with invalid code markers.
181*c9431fa1Sahl */
182*c9431fa1Sahl
183*c9431fa1Sahl /* set up for code type */
184*c9431fa1Sahl switch (type) {
185*c9431fa1Sahl case CODES:
186*c9431fa1Sahl base = extra = work; /* dummy value--not used */
187*c9431fa1Sahl end = 19;
1887c478bd9Sstevel@tonic-gate break;
189*c9431fa1Sahl case LENS:
190*c9431fa1Sahl base = lbase;
191*c9431fa1Sahl base -= 257;
192*c9431fa1Sahl extra = lext;
193*c9431fa1Sahl extra -= 257;
194*c9431fa1Sahl end = 256;
1957c478bd9Sstevel@tonic-gate break;
196*c9431fa1Sahl default: /* DISTS */
197*c9431fa1Sahl base = dbase;
198*c9431fa1Sahl extra = dext;
199*c9431fa1Sahl end = -1;
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
202*c9431fa1Sahl /* initialize state for loop */
203*c9431fa1Sahl huff = 0; /* starting code */
204*c9431fa1Sahl sym = 0; /* starting code symbol */
205*c9431fa1Sahl len = min; /* starting code length */
206*c9431fa1Sahl next = *table; /* current table to fill in */
207*c9431fa1Sahl curr = root; /* current table index bits */
208*c9431fa1Sahl drop = 0; /* current bits to drop from code for index */
209*c9431fa1Sahl low = (unsigned)(-1); /* trigger new sub-table when len > root */
210*c9431fa1Sahl used = 1U << root; /* use root table entries */
211*c9431fa1Sahl mask = used - 1; /* mask for comparing low */
2127c478bd9Sstevel@tonic-gate
213*c9431fa1Sahl /* check available table space */
214*c9431fa1Sahl if (type == LENS && used >= ENOUGH - MAXD)
215*c9431fa1Sahl return 1;
216*c9431fa1Sahl
217*c9431fa1Sahl /* process all codes and make table entries */
218*c9431fa1Sahl for (;;) {
219*c9431fa1Sahl /* create table entry */
220*c9431fa1Sahl this.bits = (unsigned char)(len - drop);
221*c9431fa1Sahl if ((int)(work[sym]) < end) {
222*c9431fa1Sahl this.op = (unsigned char)0;
223*c9431fa1Sahl this.val = work[sym];
224*c9431fa1Sahl }
225*c9431fa1Sahl else if ((int)(work[sym]) > end) {
226*c9431fa1Sahl this.op = (unsigned char)(extra[work[sym]]);
227*c9431fa1Sahl this.val = base[work[sym]];
228*c9431fa1Sahl }
229*c9431fa1Sahl else {
230*c9431fa1Sahl this.op = (unsigned char)(32 + 64); /* end of block */
231*c9431fa1Sahl this.val = 0;
232*c9431fa1Sahl }
233*c9431fa1Sahl
234*c9431fa1Sahl /* replicate for those indices with low len bits equal to huff */
235*c9431fa1Sahl incr = 1U << (len - drop);
236*c9431fa1Sahl fill = 1U << curr;
237*c9431fa1Sahl min = fill; /* save offset to next table */
2387c478bd9Sstevel@tonic-gate do {
239*c9431fa1Sahl fill -= incr;
240*c9431fa1Sahl next[(huff >> drop) + fill] = this;
241*c9431fa1Sahl } while (fill != 0);
2427c478bd9Sstevel@tonic-gate
243*c9431fa1Sahl /* backwards increment the len-bit code huff */
244*c9431fa1Sahl incr = 1U << (len - 1);
245*c9431fa1Sahl while (huff & incr)
246*c9431fa1Sahl incr >>= 1;
247*c9431fa1Sahl if (incr != 0) {
248*c9431fa1Sahl huff &= incr - 1;
249*c9431fa1Sahl huff += incr;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate else
252*c9431fa1Sahl huff = 0;
253*c9431fa1Sahl
254*c9431fa1Sahl /* go to next symbol, update count, len */
255*c9431fa1Sahl sym++;
256*c9431fa1Sahl if (--(count[len]) == 0) {
257*c9431fa1Sahl if (len == max) break;
258*c9431fa1Sahl len = lens[work[sym]];
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
261*c9431fa1Sahl /* create new sub-table if needed */
262*c9431fa1Sahl if (len > root && (huff & mask) != low) {
263*c9431fa1Sahl /* if first time, transition to sub-tables */
264*c9431fa1Sahl if (drop == 0)
265*c9431fa1Sahl drop = root;
266*c9431fa1Sahl
267*c9431fa1Sahl /* increment past last table */
268*c9431fa1Sahl next += min; /* here min is 1 << curr */
269*c9431fa1Sahl
270*c9431fa1Sahl /* determine length of next table */
271*c9431fa1Sahl curr = len - drop;
272*c9431fa1Sahl left = (int)(1 << curr);
273*c9431fa1Sahl while (curr + drop < max) {
274*c9431fa1Sahl left -= count[curr + drop];
275*c9431fa1Sahl if (left <= 0) break;
276*c9431fa1Sahl curr++;
277*c9431fa1Sahl left <<= 1;
278*c9431fa1Sahl }
279*c9431fa1Sahl
280*c9431fa1Sahl /* check for enough space */
281*c9431fa1Sahl used += 1U << curr;
282*c9431fa1Sahl if (type == LENS && used >= ENOUGH - MAXD)
283*c9431fa1Sahl return 1;
284*c9431fa1Sahl
285*c9431fa1Sahl /* point entry in root table to sub-table */
286*c9431fa1Sahl low = huff & mask;
287*c9431fa1Sahl (*table)[low].op = (unsigned char)curr;
288*c9431fa1Sahl (*table)[low].bits = (unsigned char)root;
289*c9431fa1Sahl (*table)[low].val = (unsigned short)(next - *table);
290*c9431fa1Sahl }
291*c9431fa1Sahl }
292*c9431fa1Sahl
293*c9431fa1Sahl /*
294*c9431fa1Sahl Fill in rest of table for incomplete codes. This loop is similar to the
295*c9431fa1Sahl loop above in incrementing huff for table indices. It is assumed that
296*c9431fa1Sahl len is equal to curr + drop, so there is no loop needed to increment
297*c9431fa1Sahl through high index bits. When the current sub-table is filled, the loop
298*c9431fa1Sahl drops back to the root table to fill in any remaining entries there.
299*c9431fa1Sahl */
300*c9431fa1Sahl this.op = (unsigned char)64; /* invalid code marker */
301*c9431fa1Sahl this.bits = (unsigned char)(len - drop);
302*c9431fa1Sahl this.val = (unsigned short)0;
303*c9431fa1Sahl while (huff != 0) {
304*c9431fa1Sahl /* when done with sub-table, drop back to root table */
305*c9431fa1Sahl if (drop != 0 && (huff & mask) != low) {
306*c9431fa1Sahl drop = 0;
307*c9431fa1Sahl len = root;
308*c9431fa1Sahl next = *table;
309*c9431fa1Sahl this.bits = (unsigned char)len;
310*c9431fa1Sahl }
311*c9431fa1Sahl
312*c9431fa1Sahl /* put invalid code marker in table */
313*c9431fa1Sahl next[huff >> drop] = this;
314*c9431fa1Sahl
315*c9431fa1Sahl /* backwards increment the len-bit code huff */
316*c9431fa1Sahl incr = 1U << (len - 1);
317*c9431fa1Sahl while (huff & incr)
318*c9431fa1Sahl incr >>= 1;
319*c9431fa1Sahl if (incr != 0) {
320*c9431fa1Sahl huff &= incr - 1;
321*c9431fa1Sahl huff += incr;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate else
324*c9431fa1Sahl huff = 0;
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
327*c9431fa1Sahl /* set return parameters */
328*c9431fa1Sahl *table += used;
329*c9431fa1Sahl *bits = root;
330*c9431fa1Sahl return 0;
3317c478bd9Sstevel@tonic-gate }
332