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