Debian Codegraph @

src/lib/inflate_tree.c

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