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