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