Debian Codegraph @

polyfills/zlib-lib/inftrees.js

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