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