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