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