Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80549 views
1
'use strict';
2
3
4
var utils = require('../utils/common');
5
6
var MAXBITS = 15;
7
var ENOUGH_LENS = 852;
8
var ENOUGH_DISTS = 592;
9
//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
10
11
var CODES = 0;
12
var LENS = 1;
13
var DISTS = 2;
14
15
var lbase = [ /* Length codes 257..285 base */
16
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
17
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
18
];
19
20
var lext = [ /* Length codes 257..285 extra */
21
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
22
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
23
];
24
25
var dbase = [ /* Distance codes 0..29 base */
26
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
27
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
28
8193, 12289, 16385, 24577, 0, 0
29
];
30
31
var dext = [ /* Distance codes 0..29 extra */
32
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
33
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
34
28, 28, 29, 29, 64, 64
35
];
36
37
module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
38
{
39
var bits = opts.bits;
40
//here = opts.here; /* table entry for duplication */
41
42
var len = 0; /* a code's length in bits */
43
var sym = 0; /* index of code symbols */
44
var min = 0, max = 0; /* minimum and maximum code lengths */
45
var root = 0; /* number of index bits for root table */
46
var curr = 0; /* number of index bits for current table */
47
var drop = 0; /* code bits to drop for sub-table */
48
var left = 0; /* number of prefix codes available */
49
var used = 0; /* code entries in table used */
50
var huff = 0; /* Huffman code */
51
var incr; /* for incrementing code, index */
52
var fill; /* index for replicating entries */
53
var low; /* low bits for current root entry */
54
var mask; /* mask for low root bits */
55
var next; /* next available space in table */
56
var base = null; /* base value table to use */
57
var base_index = 0;
58
// var shoextra; /* extra bits table to use */
59
var end; /* use base and extra for symbol > end */
60
var count = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* number of codes of each length */
61
var offs = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* offsets in table for each length */
62
var extra = null;
63
var extra_index = 0;
64
65
var here_bits, here_op, here_val;
66
67
/*
68
Process a set of code lengths to create a canonical Huffman code. The
69
code lengths are lens[0..codes-1]. Each length corresponds to the
70
symbols 0..codes-1. The Huffman code is generated by first sorting the
71
symbols by length from short to long, and retaining the symbol order
72
for codes with equal lengths. Then the code starts with all zero bits
73
for the first code of the shortest length, and the codes are integer
74
increments for the same length, and zeros are appended as the length
75
increases. For the deflate format, these bits are stored backwards
76
from their more natural integer increment ordering, and so when the
77
decoding tables are built in the large loop below, the integer codes
78
are incremented backwards.
79
80
This routine assumes, but does not check, that all of the entries in
81
lens[] are in the range 0..MAXBITS. The caller must assure this.
82
1..MAXBITS is interpreted as that code length. zero means that that
83
symbol does not occur in this code.
84
85
The codes are sorted by computing a count of codes for each length,
86
creating from that a table of starting indices for each length in the
87
sorted table, and then entering the symbols in order in the sorted
88
table. The sorted table is work[], with that space being provided by
89
the caller.
90
91
The length counts are used for other purposes as well, i.e. finding
92
the minimum and maximum length codes, determining if there are any
93
codes at all, checking for a valid set of lengths, and looking ahead
94
at length counts to determine sub-table sizes when building the
95
decoding tables.
96
*/
97
98
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
99
for (len = 0; len <= MAXBITS; len++) {
100
count[len] = 0;
101
}
102
for (sym = 0; sym < codes; sym++) {
103
count[lens[lens_index + sym]]++;
104
}
105
106
/* bound code lengths, force root to be within code lengths */
107
root = bits;
108
for (max = MAXBITS; max >= 1; max--) {
109
if (count[max] !== 0) { break; }
110
}
111
if (root > max) {
112
root = max;
113
}
114
if (max === 0) { /* no symbols to code at all */
115
//table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
116
//table.bits[opts.table_index] = 1; //here.bits = (var char)1;
117
//table.val[opts.table_index++] = 0; //here.val = (var short)0;
118
table[table_index++] = (1 << 24) | (64 << 16) | 0;
119
120
121
//table.op[opts.table_index] = 64;
122
//table.bits[opts.table_index] = 1;
123
//table.val[opts.table_index++] = 0;
124
table[table_index++] = (1 << 24) | (64 << 16) | 0;
125
126
opts.bits = 1;
127
return 0; /* no symbols, but wait for decoding to report error */
128
}
129
for (min = 1; min < max; min++) {
130
if (count[min] !== 0) { break; }
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
} else if (type === LENS) {
200
base = lbase;
201
base_index -= 257;
202
extra = lext;
203
extra_index -= 257;
204
end = 256;
205
} else { /* DISTS */
206
base = dbase;
207
extra = dext;
208
end = -1;
209
}
210
211
/* initialize opts for loop */
212
huff = 0; /* starting code */
213
sym = 0; /* starting code symbol */
214
len = min; /* starting code length */
215
next = table_index; /* current table to fill in */
216
curr = root; /* current table index bits */
217
drop = 0; /* current bits to drop from code for index */
218
low = -1; /* trigger new sub-table when len > root */
219
used = 1 << root; /* use root table entries */
220
mask = used - 1; /* mask for comparing low */
221
222
/* check available table space */
223
if ((type === LENS && used > ENOUGH_LENS) ||
224
(type === DISTS && used > ENOUGH_DISTS)) {
225
return 1;
226
}
227
228
var i=0;
229
/* process all codes and make table entries */
230
for (;;) {
231
i++;
232
/* create table entry */
233
here_bits = len - drop;
234
if (work[sym] < end) {
235
here_op = 0;
236
here_val = work[sym];
237
}
238
else if (work[sym] > end) {
239
here_op = extra[extra_index + work[sym]];
240
here_val = base[base_index + work[sym]];
241
}
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) { break; }
272
len = lens[lens_index + work[sym]];
273
}
274
275
/* create new sub-table if needed */
276
if (len > root && (huff & mask) !== low) {
277
/* if first time, transition to sub-tables */
278
if (drop === 0) {
279
drop = root;
280
}
281
282
/* increment past last table */
283
next += min; /* here min is 1 << curr */
284
285
/* determine length of next table */
286
curr = len - drop;
287
left = 1 << curr;
288
while (curr + drop < max) {
289
left -= count[curr + drop];
290
if (left <= 0) { break; }
291
curr++;
292
left <<= 1;
293
}
294
295
/* check for enough space */
296
used += 1 << curr;
297
if ((type === LENS && used > ENOUGH_LENS) ||
298
(type === DISTS && used > ENOUGH_DISTS)) {
299
return 1;
300
}
301
302
/* point entry in root table to sub-table */
303
low = huff & mask;
304
/*table.op[low] = curr;
305
table.bits[low] = root;
306
table.val[low] = next - opts.table_index;*/
307
table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
308
}
309
}
310
311
/* fill in remaining table entry if code is incomplete (guaranteed to have
312
at most one remaining entry, since if the code is incomplete, the
313
maximum code length that was allowed to get this far is one bit) */
314
if (huff !== 0) {
315
//table.op[next + huff] = 64; /* invalid code marker */
316
//table.bits[next + huff] = len - drop;
317
//table.val[next + huff] = 0;
318
table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
319
}
320
321
/* set return parameters */
322
//opts.table_index += used;
323
opts.bits = root;
324
return 0;
325
};
326
327