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