]>
Commit | Line | Data |
---|---|---|
fba220c6 SR |
1 | import * as utils from "../utils/common.js"; |
2 | import adler32 from "./adler32.js"; | |
3 | import crc32 from "./crc32.js"; | |
4 | import inflate_fast from "./inffast.js"; | |
5 | import inflate_table from "./inftrees.js"; | |
6 | ||
7 | var CODES = 0; | |
8 | var LENS = 1; | |
9 | var DISTS = 2; | |
10 | ||
11 | /* Public constants ==========================================================*/ | |
12 | /* ===========================================================================*/ | |
13 | ||
14 | ||
15 | /* Allowed flush values; see deflate() and inflate() below for details */ | |
9a31083a NL |
16 | //export const Z_NO_FLUSH = 0; |
17 | //export const Z_PARTIAL_FLUSH = 1; | |
18 | //export const Z_SYNC_FLUSH = 2; | |
19 | //export const Z_FULL_FLUSH = 3; | |
20 | export const Z_FINISH = 4; | |
21 | export const Z_BLOCK = 5; | |
22 | export const Z_TREES = 6; | |
fba220c6 SR |
23 | |
24 | ||
25 | /* Return codes for the compression/decompression functions. Negative values | |
26 | * are errors, positive values are used for special but normal events. | |
27 | */ | |
9a31083a NL |
28 | export const Z_OK = 0; |
29 | export const Z_STREAM_END = 1; | |
30 | export const Z_NEED_DICT = 2; | |
31 | //export const Z_ERRNO = -1; | |
32 | export const Z_STREAM_ERROR = -2; | |
33 | export const Z_DATA_ERROR = -3; | |
34 | export const Z_MEM_ERROR = -4; | |
35 | export const Z_BUF_ERROR = -5; | |
36 | //export const Z_VERSION_ERROR = -6; | |
fba220c6 SR |
37 | |
38 | /* The deflate compression method */ | |
9a31083a | 39 | export const Z_DEFLATED = 8; |
fba220c6 SR |
40 | |
41 | ||
42 | /* STATES ====================================================================*/ | |
43 | /* ===========================================================================*/ | |
44 | ||
45 | ||
46 | var HEAD = 1; /* i: waiting for magic header */ | |
47 | var FLAGS = 2; /* i: waiting for method and flags (gzip) */ | |
48 | var TIME = 3; /* i: waiting for modification time (gzip) */ | |
49 | var OS = 4; /* i: waiting for extra flags and operating system (gzip) */ | |
50 | var EXLEN = 5; /* i: waiting for extra length (gzip) */ | |
51 | var EXTRA = 6; /* i: waiting for extra bytes (gzip) */ | |
52 | var NAME = 7; /* i: waiting for end of file name (gzip) */ | |
53 | var COMMENT = 8; /* i: waiting for end of comment (gzip) */ | |
54 | var HCRC = 9; /* i: waiting for header crc (gzip) */ | |
55 | var DICTID = 10; /* i: waiting for dictionary check value */ | |
56 | var DICT = 11; /* waiting for inflateSetDictionary() call */ | |
57 | var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ | |
58 | var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */ | |
59 | var STORED = 14; /* i: waiting for stored size (length and complement) */ | |
60 | var COPY_ = 15; /* i/o: same as COPY below, but only first time in */ | |
61 | var COPY = 16; /* i/o: waiting for input or output to copy stored block */ | |
62 | var TABLE = 17; /* i: waiting for dynamic block table lengths */ | |
63 | var LENLENS = 18; /* i: waiting for code length code lengths */ | |
64 | var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */ | |
65 | var LEN_ = 20; /* i: same as LEN below, but only first time in */ | |
66 | var LEN = 21; /* i: waiting for length/lit/eob code */ | |
67 | var LENEXT = 22; /* i: waiting for length extra bits */ | |
68 | var DIST = 23; /* i: waiting for distance code */ | |
69 | var DISTEXT = 24; /* i: waiting for distance extra bits */ | |
70 | var MATCH = 25; /* o: waiting for output space to copy string */ | |
71 | var LIT = 26; /* o: waiting for output space to write literal */ | |
72 | var CHECK = 27; /* i: waiting for 32-bit check value */ | |
73 | var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */ | |
74 | var DONE = 29; /* finished check, done -- remain here until reset */ | |
75 | var BAD = 30; /* got a data error -- remain here until reset */ | |
76 | var MEM = 31; /* got an inflate() memory error -- remain here until reset */ | |
77 | var SYNC = 32; /* looking for synchronization bytes to restart inflate() */ | |
78 | ||
79 | /* ===========================================================================*/ | |
80 | ||
81 | ||
82 | ||
83 | var ENOUGH_LENS = 852; | |
84 | var ENOUGH_DISTS = 592; | |
85 | //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); | |
86 | ||
87 | var MAX_WBITS = 15; | |
88 | /* 32K LZ77 window */ | |
89 | var DEF_WBITS = MAX_WBITS; | |
90 | ||
91 | ||
92 | function zswap32(q) { | |
93 | return (((q >>> 24) & 0xff) + | |
94 | ((q >>> 8) & 0xff00) + | |
95 | ((q & 0xff00) << 8) + | |
96 | ((q & 0xff) << 24)); | |
97 | } | |
98 | ||
99 | ||
100 | function InflateState() { | |
101 | this.mode = 0; /* current inflate mode */ | |
102 | this.last = false; /* true if processing last block */ | |
103 | this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ | |
104 | this.havedict = false; /* true if dictionary provided */ | |
105 | this.flags = 0; /* gzip header method and flags (0 if zlib) */ | |
106 | this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ | |
107 | this.check = 0; /* protected copy of check value */ | |
108 | this.total = 0; /* protected copy of output count */ | |
109 | // TODO: may be {} | |
110 | this.head = null; /* where to save gzip header information */ | |
111 | ||
112 | /* sliding window */ | |
113 | this.wbits = 0; /* log base 2 of requested window size */ | |
114 | this.wsize = 0; /* window size or zero if not using window */ | |
115 | this.whave = 0; /* valid bytes in the window */ | |
116 | this.wnext = 0; /* window write index */ | |
117 | this.window = null; /* allocated sliding window, if needed */ | |
118 | ||
119 | /* bit accumulator */ | |
120 | this.hold = 0; /* input bit accumulator */ | |
121 | this.bits = 0; /* number of bits in "in" */ | |
122 | ||
123 | /* for string and stored block copying */ | |
124 | this.length = 0; /* literal or length of data to copy */ | |
125 | this.offset = 0; /* distance back to copy string from */ | |
126 | ||
127 | /* for table and code decoding */ | |
128 | this.extra = 0; /* extra bits needed */ | |
129 | ||
130 | /* fixed and dynamic code tables */ | |
131 | this.lencode = null; /* starting table for length/literal codes */ | |
132 | this.distcode = null; /* starting table for distance codes */ | |
133 | this.lenbits = 0; /* index bits for lencode */ | |
134 | this.distbits = 0; /* index bits for distcode */ | |
135 | ||
136 | /* dynamic table building */ | |
137 | this.ncode = 0; /* number of code length code lengths */ | |
138 | this.nlen = 0; /* number of length code lengths */ | |
139 | this.ndist = 0; /* number of distance code lengths */ | |
140 | this.have = 0; /* number of code lengths in lens[] */ | |
141 | this.next = null; /* next available space in codes[] */ | |
142 | ||
143 | this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ | |
144 | this.work = new utils.Buf16(288); /* work area for code table building */ | |
145 | ||
146 | /* | |
147 | because we don't have pointers in js, we use lencode and distcode directly | |
148 | as buffers so we don't need codes | |
149 | */ | |
150 | //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ | |
151 | this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ | |
152 | this.distdyn = null; /* dynamic table for distance codes (JS specific) */ | |
153 | this.sane = 0; /* if false, allow invalid distance too far */ | |
154 | this.back = 0; /* bits back of last unprocessed length/lit */ | |
155 | this.was = 0; /* initial length of match */ | |
156 | } | |
157 | ||
158 | function inflateResetKeep(strm) { | |
159 | var state; | |
160 | ||
161 | if (!strm || !strm.state) { return Z_STREAM_ERROR; } | |
162 | state = strm.state; | |
163 | strm.total_in = strm.total_out = state.total = 0; | |
164 | strm.msg = ''; /*Z_NULL*/ | |
165 | if (state.wrap) { /* to support ill-conceived Java test suite */ | |
166 | strm.adler = state.wrap & 1; | |
167 | } | |
168 | state.mode = HEAD; | |
169 | state.last = 0; | |
170 | state.havedict = 0; | |
171 | state.dmax = 32768; | |
172 | state.head = null/*Z_NULL*/; | |
173 | state.hold = 0; | |
174 | state.bits = 0; | |
175 | //state.lencode = state.distcode = state.next = state.codes; | |
176 | state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); | |
177 | state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); | |
178 | ||
179 | state.sane = 1; | |
180 | state.back = -1; | |
181 | //Tracev((stderr, "inflate: reset\n")); | |
182 | return Z_OK; | |
183 | } | |
184 | ||
185 | function inflateReset(strm) { | |
186 | var state; | |
187 | ||
188 | if (!strm || !strm.state) { return Z_STREAM_ERROR; } | |
189 | state = strm.state; | |
190 | state.wsize = 0; | |
191 | state.whave = 0; | |
192 | state.wnext = 0; | |
193 | return inflateResetKeep(strm); | |
194 | ||
195 | } | |
196 | ||
197 | function inflateReset2(strm, windowBits) { | |
198 | var wrap; | |
199 | var state; | |
200 | ||
201 | /* get the state */ | |
202 | if (!strm || !strm.state) { return Z_STREAM_ERROR; } | |
203 | state = strm.state; | |
204 | ||
205 | /* extract wrap request from windowBits parameter */ | |
206 | if (windowBits < 0) { | |
207 | wrap = 0; | |
208 | windowBits = -windowBits; | |
209 | } | |
210 | else { | |
211 | wrap = (windowBits >> 4) + 1; | |
212 | if (windowBits < 48) { | |
213 | windowBits &= 15; | |
214 | } | |
215 | } | |
216 | ||
217 | /* set number of window bits, free window if different */ | |
218 | if (windowBits && (windowBits < 8 || windowBits > 15)) { | |
219 | return Z_STREAM_ERROR; | |
220 | } | |
221 | if (state.window !== null && state.wbits !== windowBits) { | |
222 | state.window = null; | |
223 | } | |
224 | ||
225 | /* update state and reset the rest of it */ | |
226 | state.wrap = wrap; | |
227 | state.wbits = windowBits; | |
228 | return inflateReset(strm); | |
229 | } | |
230 | ||
231 | function inflateInit2(strm, windowBits) { | |
232 | var ret; | |
233 | var state; | |
234 | ||
235 | if (!strm) { return Z_STREAM_ERROR; } | |
236 | //strm.msg = Z_NULL; /* in case we return an error */ | |
237 | ||
238 | state = new InflateState(); | |
239 | ||
240 | //if (state === Z_NULL) return Z_MEM_ERROR; | |
241 | //Tracev((stderr, "inflate: allocated\n")); | |
242 | strm.state = state; | |
243 | state.window = null/*Z_NULL*/; | |
244 | ret = inflateReset2(strm, windowBits); | |
245 | if (ret !== Z_OK) { | |
246 | strm.state = null/*Z_NULL*/; | |
247 | } | |
248 | return ret; | |
249 | } | |
250 | ||
251 | function inflateInit(strm) { | |
252 | return inflateInit2(strm, DEF_WBITS); | |
253 | } | |
254 | ||
255 | ||
256 | /* | |
257 | Return state with length and distance decoding tables and index sizes set to | |
258 | fixed code decoding. Normally this returns fixed tables from inffixed.h. | |
259 | If BUILDFIXED is defined, then instead this routine builds the tables the | |
260 | first time it's called, and returns those tables the first time and | |
261 | thereafter. This reduces the size of the code by about 2K bytes, in | |
262 | exchange for a little execution time. However, BUILDFIXED should not be | |
263 | used for threaded applications, since the rewriting of the tables and virgin | |
264 | may not be thread-safe. | |
265 | */ | |
266 | var virgin = true; | |
267 | ||
268 | var lenfix, distfix; // We have no pointers in JS, so keep tables separate | |
269 | ||
270 | function fixedtables(state) { | |
271 | /* build fixed huffman tables if first call (may not be thread safe) */ | |
272 | if (virgin) { | |
273 | var sym; | |
274 | ||
275 | lenfix = new utils.Buf32(512); | |
276 | distfix = new utils.Buf32(32); | |
277 | ||
278 | /* literal/length table */ | |
279 | sym = 0; | |
280 | while (sym < 144) { state.lens[sym++] = 8; } | |
281 | while (sym < 256) { state.lens[sym++] = 9; } | |
282 | while (sym < 280) { state.lens[sym++] = 7; } | |
283 | while (sym < 288) { state.lens[sym++] = 8; } | |
284 | ||
285 | inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 }); | |
286 | ||
287 | /* distance table */ | |
288 | sym = 0; | |
289 | while (sym < 32) { state.lens[sym++] = 5; } | |
290 | ||
291 | inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 }); | |
292 | ||
293 | /* do this just once */ | |
294 | virgin = false; | |
295 | } | |
296 | ||
297 | state.lencode = lenfix; | |
298 | state.lenbits = 9; | |
299 | state.distcode = distfix; | |
300 | state.distbits = 5; | |
301 | } | |
302 | ||
303 | ||
304 | /* | |
305 | Update the window with the last wsize (normally 32K) bytes written before | |
306 | returning. If window does not exist yet, create it. This is only called | |
307 | when a window is already in use, or when output has been written during this | |
308 | inflate call, but the end of the deflate stream has not been reached yet. | |
309 | It is also called to create a window for dictionary data when a dictionary | |
310 | is loaded. | |
311 | ||
312 | Providing output buffers larger than 32K to inflate() should provide a speed | |
313 | advantage, since only the last 32K of output is copied to the sliding window | |
314 | upon return from inflate(), and since all distances after the first 32K of | |
315 | output will fall in the output data, making match copies simpler and faster. | |
316 | The advantage may be dependent on the size of the processor's data caches. | |
317 | */ | |
318 | function updatewindow(strm, src, end, copy) { | |
319 | var dist; | |
320 | var state = strm.state; | |
321 | ||
322 | /* if it hasn't been done already, allocate space for the window */ | |
323 | if (state.window === null) { | |
324 | state.wsize = 1 << state.wbits; | |
325 | state.wnext = 0; | |
326 | state.whave = 0; | |
327 | ||
328 | state.window = new utils.Buf8(state.wsize); | |
329 | } | |
330 | ||
331 | /* copy state->wsize or less output bytes into the circular window */ | |
332 | if (copy >= state.wsize) { | |
333 | utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); | |
334 | state.wnext = 0; | |
335 | state.whave = state.wsize; | |
336 | } | |
337 | else { | |
338 | dist = state.wsize - state.wnext; | |
339 | if (dist > copy) { | |
340 | dist = copy; | |
341 | } | |
342 | //zmemcpy(state->window + state->wnext, end - copy, dist); | |
343 | utils.arraySet(state.window, src, end - copy, dist, state.wnext); | |
344 | copy -= dist; | |
345 | if (copy) { | |
346 | //zmemcpy(state->window, end - copy, copy); | |
347 | utils.arraySet(state.window, src, end - copy, copy, 0); | |
348 | state.wnext = copy; | |
349 | state.whave = state.wsize; | |
350 | } | |
351 | else { | |
352 | state.wnext += dist; | |
353 | if (state.wnext === state.wsize) { state.wnext = 0; } | |
354 | if (state.whave < state.wsize) { state.whave += dist; } | |
355 | } | |
356 | } | |
357 | return 0; | |
358 | } | |
359 | ||
360 | function inflate(strm, flush) { | |
361 | var state; | |
362 | var input, output; // input/output buffers | |
363 | var next; /* next input INDEX */ | |
364 | var put; /* next output INDEX */ | |
365 | var have, left; /* available input and output */ | |
366 | var hold; /* bit buffer */ | |
367 | var bits; /* bits in bit buffer */ | |
368 | var _in, _out; /* save starting available input and output */ | |
369 | var copy; /* number of stored or match bytes to copy */ | |
370 | var from; /* where to copy match bytes from */ | |
371 | var from_source; | |
372 | var here = 0; /* current decoding table entry */ | |
373 | var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) | |
374 | //var last; /* parent table entry */ | |
375 | var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) | |
376 | var len; /* length to copy for repeats, bits to drop */ | |
377 | var ret; /* return code */ | |
378 | var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ | |
379 | var opts; | |
380 | ||
381 | var n; // temporary var for NEED_BITS | |
382 | ||
383 | var order = /* permutation of code lengths */ | |
384 | [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; | |
385 | ||
386 | ||
387 | if (!strm || !strm.state || !strm.output || | |
388 | (!strm.input && strm.avail_in !== 0)) { | |
389 | return Z_STREAM_ERROR; | |
390 | } | |
391 | ||
392 | state = strm.state; | |
393 | if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */ | |
394 | ||
395 | ||
396 | //--- LOAD() --- | |
397 | put = strm.next_out; | |
398 | output = strm.output; | |
399 | left = strm.avail_out; | |
400 | next = strm.next_in; | |
401 | input = strm.input; | |
402 | have = strm.avail_in; | |
403 | hold = state.hold; | |
404 | bits = state.bits; | |
405 | //--- | |
406 | ||
407 | _in = have; | |
408 | _out = left; | |
409 | ret = Z_OK; | |
410 | ||
411 | inf_leave: // goto emulation | |
412 | for (;;) { | |
413 | switch (state.mode) { | |
414 | case HEAD: | |
415 | if (state.wrap === 0) { | |
416 | state.mode = TYPEDO; | |
417 | break; | |
418 | } | |
419 | //=== NEEDBITS(16); | |
420 | while (bits < 16) { | |
421 | if (have === 0) { break inf_leave; } | |
422 | have--; | |
423 | hold += input[next++] << bits; | |
424 | bits += 8; | |
425 | } | |
426 | //===// | |
427 | if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */ | |
428 | state.check = 0/*crc32(0L, Z_NULL, 0)*/; | |
429 | //=== CRC2(state.check, hold); | |
430 | hbuf[0] = hold & 0xff; | |
431 | hbuf[1] = (hold >>> 8) & 0xff; | |
432 | state.check = crc32(state.check, hbuf, 2, 0); | |
433 | //===// | |
434 | ||
435 | //=== INITBITS(); | |
436 | hold = 0; | |
437 | bits = 0; | |
438 | //===// | |
439 | state.mode = FLAGS; | |
440 | break; | |
441 | } | |
442 | state.flags = 0; /* expect zlib header */ | |
443 | if (state.head) { | |
444 | state.head.done = false; | |
445 | } | |
446 | if (!(state.wrap & 1) || /* check if zlib header allowed */ | |
447 | (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) { | |
448 | strm.msg = 'incorrect header check'; | |
449 | state.mode = BAD; | |
450 | break; | |
451 | } | |
452 | if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) { | |
453 | strm.msg = 'unknown compression method'; | |
454 | state.mode = BAD; | |
455 | break; | |
456 | } | |
457 | //--- DROPBITS(4) ---// | |
458 | hold >>>= 4; | |
459 | bits -= 4; | |
460 | //---// | |
461 | len = (hold & 0x0f)/*BITS(4)*/ + 8; | |
462 | if (state.wbits === 0) { | |
463 | state.wbits = len; | |
464 | } | |
465 | else if (len > state.wbits) { | |
466 | strm.msg = 'invalid window size'; | |
467 | state.mode = BAD; | |
468 | break; | |
469 | } | |
470 | state.dmax = 1 << len; | |
471 | //Tracev((stderr, "inflate: zlib header ok\n")); | |
472 | strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; | |
473 | state.mode = hold & 0x200 ? DICTID : TYPE; | |
474 | //=== INITBITS(); | |
475 | hold = 0; | |
476 | bits = 0; | |
477 | //===// | |
478 | break; | |
479 | case FLAGS: | |
480 | //=== NEEDBITS(16); */ | |
481 | while (bits < 16) { | |
482 | if (have === 0) { break inf_leave; } | |
483 | have--; | |
484 | hold += input[next++] << bits; | |
485 | bits += 8; | |
486 | } | |
487 | //===// | |
488 | state.flags = hold; | |
489 | if ((state.flags & 0xff) !== Z_DEFLATED) { | |
490 | strm.msg = 'unknown compression method'; | |
491 | state.mode = BAD; | |
492 | break; | |
493 | } | |
494 | if (state.flags & 0xe000) { | |
495 | strm.msg = 'unknown header flags set'; | |
496 | state.mode = BAD; | |
497 | break; | |
498 | } | |
499 | if (state.head) { | |
500 | state.head.text = ((hold >> 8) & 1); | |
501 | } | |
502 | if (state.flags & 0x0200) { | |
503 | //=== CRC2(state.check, hold); | |
504 | hbuf[0] = hold & 0xff; | |
505 | hbuf[1] = (hold >>> 8) & 0xff; | |
506 | state.check = crc32(state.check, hbuf, 2, 0); | |
507 | //===// | |
508 | } | |
509 | //=== INITBITS(); | |
510 | hold = 0; | |
511 | bits = 0; | |
512 | //===// | |
513 | state.mode = TIME; | |
514 | /* falls through */ | |
515 | case TIME: | |
516 | //=== NEEDBITS(32); */ | |
517 | while (bits < 32) { | |
518 | if (have === 0) { break inf_leave; } | |
519 | have--; | |
520 | hold += input[next++] << bits; | |
521 | bits += 8; | |
522 | } | |
523 | //===// | |
524 | if (state.head) { | |
525 | state.head.time = hold; | |
526 | } | |
527 | if (state.flags & 0x0200) { | |
528 | //=== CRC4(state.check, hold) | |
529 | hbuf[0] = hold & 0xff; | |
530 | hbuf[1] = (hold >>> 8) & 0xff; | |
531 | hbuf[2] = (hold >>> 16) & 0xff; | |
532 | hbuf[3] = (hold >>> 24) & 0xff; | |
533 | state.check = crc32(state.check, hbuf, 4, 0); | |
534 | //=== | |
535 | } | |
536 | //=== INITBITS(); | |
537 | hold = 0; | |
538 | bits = 0; | |
539 | //===// | |
540 | state.mode = OS; | |
541 | /* falls through */ | |
542 | case OS: | |
543 | //=== NEEDBITS(16); */ | |
544 | while (bits < 16) { | |
545 | if (have === 0) { break inf_leave; } | |
546 | have--; | |
547 | hold += input[next++] << bits; | |
548 | bits += 8; | |
549 | } | |
550 | //===// | |
551 | if (state.head) { | |
552 | state.head.xflags = (hold & 0xff); | |
553 | state.head.os = (hold >> 8); | |
554 | } | |
555 | if (state.flags & 0x0200) { | |
556 | //=== CRC2(state.check, hold); | |
557 | hbuf[0] = hold & 0xff; | |
558 | hbuf[1] = (hold >>> 8) & 0xff; | |
559 | state.check = crc32(state.check, hbuf, 2, 0); | |
560 | //===// | |
561 | } | |
562 | //=== INITBITS(); | |
563 | hold = 0; | |
564 | bits = 0; | |
565 | //===// | |
566 | state.mode = EXLEN; | |
567 | /* falls through */ | |
568 | case EXLEN: | |
569 | if (state.flags & 0x0400) { | |
570 | //=== NEEDBITS(16); */ | |
571 | while (bits < 16) { | |
572 | if (have === 0) { break inf_leave; } | |
573 | have--; | |
574 | hold += input[next++] << bits; | |
575 | bits += 8; | |
576 | } | |
577 | //===// | |
578 | state.length = hold; | |
579 | if (state.head) { | |
580 | state.head.extra_len = hold; | |
581 | } | |
582 | if (state.flags & 0x0200) { | |
583 | //=== CRC2(state.check, hold); | |
584 | hbuf[0] = hold & 0xff; | |
585 | hbuf[1] = (hold >>> 8) & 0xff; | |
586 | state.check = crc32(state.check, hbuf, 2, 0); | |
587 | //===// | |
588 | } | |
589 | //=== INITBITS(); | |
590 | hold = 0; | |
591 | bits = 0; | |
592 | //===// | |
593 | } | |
594 | else if (state.head) { | |
595 | state.head.extra = null/*Z_NULL*/; | |
596 | } | |
597 | state.mode = EXTRA; | |
598 | /* falls through */ | |
599 | case EXTRA: | |
600 | if (state.flags & 0x0400) { | |
601 | copy = state.length; | |
602 | if (copy > have) { copy = have; } | |
603 | if (copy) { | |
604 | if (state.head) { | |
605 | len = state.head.extra_len - state.length; | |
606 | if (!state.head.extra) { | |
607 | // Use untyped array for more conveniend processing later | |
608 | state.head.extra = new Array(state.head.extra_len); | |
609 | } | |
610 | utils.arraySet( | |
611 | state.head.extra, | |
612 | input, | |
613 | next, | |
614 | // extra field is limited to 65536 bytes | |
615 | // - no need for additional size check | |
616 | copy, | |
617 | /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ | |
618 | len | |
619 | ); | |
620 | //zmemcpy(state.head.extra + len, next, | |
621 | // len + copy > state.head.extra_max ? | |
622 | // state.head.extra_max - len : copy); | |
623 | } | |
624 | if (state.flags & 0x0200) { | |
625 | state.check = crc32(state.check, input, copy, next); | |
626 | } | |
627 | have -= copy; | |
628 | next += copy; | |
629 | state.length -= copy; | |
630 | } | |
631 | if (state.length) { break inf_leave; } | |
632 | } | |
633 | state.length = 0; | |
634 | state.mode = NAME; | |
635 | /* falls through */ | |
636 | case NAME: | |
637 | if (state.flags & 0x0800) { | |
638 | if (have === 0) { break inf_leave; } | |
639 | copy = 0; | |
640 | do { | |
641 | // TODO: 2 or 1 bytes? | |
642 | len = input[next + copy++]; | |
643 | /* use constant limit because in js we should not preallocate memory */ | |
644 | if (state.head && len && | |
645 | (state.length < 65536 /*state.head.name_max*/)) { | |
646 | state.head.name += String.fromCharCode(len); | |
647 | } | |
648 | } while (len && copy < have); | |
649 | ||
650 | if (state.flags & 0x0200) { | |
651 | state.check = crc32(state.check, input, copy, next); | |
652 | } | |
653 | have -= copy; | |
654 | next += copy; | |
655 | if (len) { break inf_leave; } | |
656 | } | |
657 | else if (state.head) { | |
658 | state.head.name = null; | |
659 | } | |
660 | state.length = 0; | |
661 | state.mode = COMMENT; | |
662 | /* falls through */ | |
663 | case COMMENT: | |
664 | if (state.flags & 0x1000) { | |
665 | if (have === 0) { break inf_leave; } | |
666 | copy = 0; | |
667 | do { | |
668 | len = input[next + copy++]; | |
669 | /* use constant limit because in js we should not preallocate memory */ | |
670 | if (state.head && len && | |
671 | (state.length < 65536 /*state.head.comm_max*/)) { | |
672 | state.head.comment += String.fromCharCode(len); | |
673 | } | |
674 | } while (len && copy < have); | |
675 | if (state.flags & 0x0200) { | |
676 | state.check = crc32(state.check, input, copy, next); | |
677 | } | |
678 | have -= copy; | |
679 | next += copy; | |
680 | if (len) { break inf_leave; } | |
681 | } | |
682 | else if (state.head) { | |
683 | state.head.comment = null; | |
684 | } | |
685 | state.mode = HCRC; | |
686 | /* falls through */ | |
687 | case HCRC: | |
688 | if (state.flags & 0x0200) { | |
689 | //=== NEEDBITS(16); */ | |
690 | while (bits < 16) { | |
691 | if (have === 0) { break inf_leave; } | |
692 | have--; | |
693 | hold += input[next++] << bits; | |
694 | bits += 8; | |
695 | } | |
696 | //===// | |
697 | if (hold !== (state.check & 0xffff)) { | |
698 | strm.msg = 'header crc mismatch'; | |
699 | state.mode = BAD; | |
700 | break; | |
701 | } | |
702 | //=== INITBITS(); | |
703 | hold = 0; | |
704 | bits = 0; | |
705 | //===// | |
706 | } | |
707 | if (state.head) { | |
708 | state.head.hcrc = ((state.flags >> 9) & 1); | |
709 | state.head.done = true; | |
710 | } | |
711 | strm.adler = state.check = 0; | |
712 | state.mode = TYPE; | |
713 | break; | |
714 | case DICTID: | |
715 | //=== NEEDBITS(32); */ | |
716 | while (bits < 32) { | |
717 | if (have === 0) { break inf_leave; } | |
718 | have--; | |
719 | hold += input[next++] << bits; | |
720 | bits += 8; | |
721 | } | |
722 | //===// | |
723 | strm.adler = state.check = zswap32(hold); | |
724 | //=== INITBITS(); | |
725 | hold = 0; | |
726 | bits = 0; | |
727 | //===// | |
728 | state.mode = DICT; | |
729 | /* falls through */ | |
730 | case DICT: | |
731 | if (state.havedict === 0) { | |
732 | //--- RESTORE() --- | |
733 | strm.next_out = put; | |
734 | strm.avail_out = left; | |
735 | strm.next_in = next; | |
736 | strm.avail_in = have; | |
737 | state.hold = hold; | |
738 | state.bits = bits; | |
739 | //--- | |
740 | return Z_NEED_DICT; | |
741 | } | |
742 | strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; | |
743 | state.mode = TYPE; | |
744 | /* falls through */ | |
745 | case TYPE: | |
746 | if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; } | |
747 | /* falls through */ | |
748 | case TYPEDO: | |
749 | if (state.last) { | |
750 | //--- BYTEBITS() ---// | |
751 | hold >>>= bits & 7; | |
752 | bits -= bits & 7; | |
753 | //---// | |
754 | state.mode = CHECK; | |
755 | break; | |
756 | } | |
757 | //=== NEEDBITS(3); */ | |
758 | while (bits < 3) { | |
759 | if (have === 0) { break inf_leave; } | |
760 | have--; | |
761 | hold += input[next++] << bits; | |
762 | bits += 8; | |
763 | } | |
764 | //===// | |
765 | state.last = (hold & 0x01)/*BITS(1)*/; | |
766 | //--- DROPBITS(1) ---// | |
767 | hold >>>= 1; | |
768 | bits -= 1; | |
769 | //---// | |
770 | ||
771 | switch ((hold & 0x03)/*BITS(2)*/) { | |
772 | case 0: /* stored block */ | |
773 | //Tracev((stderr, "inflate: stored block%s\n", | |
774 | // state.last ? " (last)" : "")); | |
775 | state.mode = STORED; | |
776 | break; | |
777 | case 1: /* fixed block */ | |
778 | fixedtables(state); | |
779 | //Tracev((stderr, "inflate: fixed codes block%s\n", | |
780 | // state.last ? " (last)" : "")); | |
781 | state.mode = LEN_; /* decode codes */ | |
782 | if (flush === Z_TREES) { | |
783 | //--- DROPBITS(2) ---// | |
784 | hold >>>= 2; | |
785 | bits -= 2; | |
786 | //---// | |
787 | break inf_leave; | |
788 | } | |
789 | break; | |
790 | case 2: /* dynamic block */ | |
791 | //Tracev((stderr, "inflate: dynamic codes block%s\n", | |
792 | // state.last ? " (last)" : "")); | |
793 | state.mode = TABLE; | |
794 | break; | |
795 | case 3: | |
796 | strm.msg = 'invalid block type'; | |
797 | state.mode = BAD; | |
798 | } | |
799 | //--- DROPBITS(2) ---// | |
800 | hold >>>= 2; | |
801 | bits -= 2; | |
802 | //---// | |
803 | break; | |
804 | case STORED: | |
805 | //--- BYTEBITS() ---// /* go to byte boundary */ | |
806 | hold >>>= bits & 7; | |
807 | bits -= bits & 7; | |
808 | //---// | |
809 | //=== NEEDBITS(32); */ | |
810 | while (bits < 32) { | |
811 | if (have === 0) { break inf_leave; } | |
812 | have--; | |
813 | hold += input[next++] << bits; | |
814 | bits += 8; | |
815 | } | |
816 | //===// | |
817 | if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) { | |
818 | strm.msg = 'invalid stored block lengths'; | |
819 | state.mode = BAD; | |
820 | break; | |
821 | } | |
822 | state.length = hold & 0xffff; | |
823 | //Tracev((stderr, "inflate: stored length %u\n", | |
824 | // state.length)); | |
825 | //=== INITBITS(); | |
826 | hold = 0; | |
827 | bits = 0; | |
828 | //===// | |
829 | state.mode = COPY_; | |
830 | if (flush === Z_TREES) { break inf_leave; } | |
831 | /* falls through */ | |
832 | case COPY_: | |
833 | state.mode = COPY; | |
834 | /* falls through */ | |
835 | case COPY: | |
836 | copy = state.length; | |
837 | if (copy) { | |
838 | if (copy > have) { copy = have; } | |
839 | if (copy > left) { copy = left; } | |
840 | if (copy === 0) { break inf_leave; } | |
841 | //--- zmemcpy(put, next, copy); --- | |
842 | utils.arraySet(output, input, next, copy, put); | |
843 | //---// | |
844 | have -= copy; | |
845 | next += copy; | |
846 | left -= copy; | |
847 | put += copy; | |
848 | state.length -= copy; | |
849 | break; | |
850 | } | |
851 | //Tracev((stderr, "inflate: stored end\n")); | |
852 | state.mode = TYPE; | |
853 | break; | |
854 | case TABLE: | |
855 | //=== NEEDBITS(14); */ | |
856 | while (bits < 14) { | |
857 | if (have === 0) { break inf_leave; } | |
858 | have--; | |
859 | hold += input[next++] << bits; | |
860 | bits += 8; | |
861 | } | |
862 | //===// | |
863 | state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257; | |
864 | //--- DROPBITS(5) ---// | |
865 | hold >>>= 5; | |
866 | bits -= 5; | |
867 | //---// | |
868 | state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1; | |
869 | //--- DROPBITS(5) ---// | |
870 | hold >>>= 5; | |
871 | bits -= 5; | |
872 | //---// | |
873 | state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4; | |
874 | //--- DROPBITS(4) ---// | |
875 | hold >>>= 4; | |
876 | bits -= 4; | |
877 | //---// | |
878 | //#ifndef PKZIP_BUG_WORKAROUND | |
879 | if (state.nlen > 286 || state.ndist > 30) { | |
880 | strm.msg = 'too many length or distance symbols'; | |
881 | state.mode = BAD; | |
882 | break; | |
883 | } | |
884 | //#endif | |
885 | //Tracev((stderr, "inflate: table sizes ok\n")); | |
886 | state.have = 0; | |
887 | state.mode = LENLENS; | |
888 | /* falls through */ | |
889 | case LENLENS: | |
890 | while (state.have < state.ncode) { | |
891 | //=== NEEDBITS(3); | |
892 | while (bits < 3) { | |
893 | if (have === 0) { break inf_leave; } | |
894 | have--; | |
895 | hold += input[next++] << bits; | |
896 | bits += 8; | |
897 | } | |
898 | //===// | |
899 | state.lens[order[state.have++]] = (hold & 0x07);//BITS(3); | |
900 | //--- DROPBITS(3) ---// | |
901 | hold >>>= 3; | |
902 | bits -= 3; | |
903 | //---// | |
904 | } | |
905 | while (state.have < 19) { | |
906 | state.lens[order[state.have++]] = 0; | |
907 | } | |
908 | // We have separate tables & no pointers. 2 commented lines below not needed. | |
909 | //state.next = state.codes; | |
910 | //state.lencode = state.next; | |
911 | // Switch to use dynamic table | |
912 | state.lencode = state.lendyn; | |
913 | state.lenbits = 7; | |
914 | ||
915 | opts = { bits: state.lenbits }; | |
916 | ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); | |
917 | state.lenbits = opts.bits; | |
918 | ||
919 | if (ret) { | |
920 | strm.msg = 'invalid code lengths set'; | |
921 | state.mode = BAD; | |
922 | break; | |
923 | } | |
924 | //Tracev((stderr, "inflate: code lengths ok\n")); | |
925 | state.have = 0; | |
926 | state.mode = CODELENS; | |
927 | /* falls through */ | |
928 | case CODELENS: | |
929 | while (state.have < state.nlen + state.ndist) { | |
930 | for (;;) { | |
931 | here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/ | |
932 | here_bits = here >>> 24; | |
933 | here_op = (here >>> 16) & 0xff; | |
934 | here_val = here & 0xffff; | |
935 | ||
936 | if ((here_bits) <= bits) { break; } | |
937 | //--- PULLBYTE() ---// | |
938 | if (have === 0) { break inf_leave; } | |
939 | have--; | |
940 | hold += input[next++] << bits; | |
941 | bits += 8; | |
942 | //---// | |
943 | } | |
944 | if (here_val < 16) { | |
945 | //--- DROPBITS(here.bits) ---// | |
946 | hold >>>= here_bits; | |
947 | bits -= here_bits; | |
948 | //---// | |
949 | state.lens[state.have++] = here_val; | |
950 | } | |
951 | else { | |
952 | if (here_val === 16) { | |
953 | //=== NEEDBITS(here.bits + 2); | |
954 | n = here_bits + 2; | |
955 | while (bits < n) { | |
956 | if (have === 0) { break inf_leave; } | |
957 | have--; | |
958 | hold += input[next++] << bits; | |
959 | bits += 8; | |
960 | } | |
961 | //===// | |
962 | //--- DROPBITS(here.bits) ---// | |
963 | hold >>>= here_bits; | |
964 | bits -= here_bits; | |
965 | //---// | |
966 | if (state.have === 0) { | |
967 | strm.msg = 'invalid bit length repeat'; | |
968 | state.mode = BAD; | |
969 | break; | |
970 | } | |
971 | len = state.lens[state.have - 1]; | |
972 | copy = 3 + (hold & 0x03);//BITS(2); | |
973 | //--- DROPBITS(2) ---// | |
974 | hold >>>= 2; | |
975 | bits -= 2; | |
976 | //---// | |
977 | } | |
978 | else if (here_val === 17) { | |
979 | //=== NEEDBITS(here.bits + 3); | |
980 | n = here_bits + 3; | |
981 | while (bits < n) { | |
982 | if (have === 0) { break inf_leave; } | |
983 | have--; | |
984 | hold += input[next++] << bits; | |
985 | bits += 8; | |
986 | } | |
987 | //===// | |
988 | //--- DROPBITS(here.bits) ---// | |
989 | hold >>>= here_bits; | |
990 | bits -= here_bits; | |
991 | //---// | |
992 | len = 0; | |
993 | copy = 3 + (hold & 0x07);//BITS(3); | |
994 | //--- DROPBITS(3) ---// | |
995 | hold >>>= 3; | |
996 | bits -= 3; | |
997 | //---// | |
998 | } | |
999 | else { | |
1000 | //=== NEEDBITS(here.bits + 7); | |
1001 | n = here_bits + 7; | |
1002 | while (bits < n) { | |
1003 | if (have === 0) { break inf_leave; } | |
1004 | have--; | |
1005 | hold += input[next++] << bits; | |
1006 | bits += 8; | |
1007 | } | |
1008 | //===// | |
1009 | //--- DROPBITS(here.bits) ---// | |
1010 | hold >>>= here_bits; | |
1011 | bits -= here_bits; | |
1012 | //---// | |
1013 | len = 0; | |
1014 | copy = 11 + (hold & 0x7f);//BITS(7); | |
1015 | //--- DROPBITS(7) ---// | |
1016 | hold >>>= 7; | |
1017 | bits -= 7; | |
1018 | //---// | |
1019 | } | |
1020 | if (state.have + copy > state.nlen + state.ndist) { | |
1021 | strm.msg = 'invalid bit length repeat'; | |
1022 | state.mode = BAD; | |
1023 | break; | |
1024 | } | |
1025 | while (copy--) { | |
1026 | state.lens[state.have++] = len; | |
1027 | } | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | /* handle error breaks in while */ | |
1032 | if (state.mode === BAD) { break; } | |
1033 | ||
1034 | /* check for end-of-block code (better have one) */ | |
1035 | if (state.lens[256] === 0) { | |
1036 | strm.msg = 'invalid code -- missing end-of-block'; | |
1037 | state.mode = BAD; | |
1038 | break; | |
1039 | } | |
1040 | ||
1041 | /* build code tables -- note: do not change the lenbits or distbits | |
1042 | values here (9 and 6) without reading the comments in inftrees.h | |
1043 | concerning the ENOUGH constants, which depend on those values */ | |
1044 | state.lenbits = 9; | |
1045 | ||
1046 | opts = { bits: state.lenbits }; | |
1047 | ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); | |
1048 | // We have separate tables & no pointers. 2 commented lines below not needed. | |
1049 | // state.next_index = opts.table_index; | |
1050 | state.lenbits = opts.bits; | |
1051 | // state.lencode = state.next; | |
1052 | ||
1053 | if (ret) { | |
1054 | strm.msg = 'invalid literal/lengths set'; | |
1055 | state.mode = BAD; | |
1056 | break; | |
1057 | } | |
1058 | ||
1059 | state.distbits = 6; | |
1060 | //state.distcode.copy(state.codes); | |
1061 | // Switch to use dynamic table | |
1062 | state.distcode = state.distdyn; | |
1063 | opts = { bits: state.distbits }; | |
1064 | ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); | |
1065 | // We have separate tables & no pointers. 2 commented lines below not needed. | |
1066 | // state.next_index = opts.table_index; | |
1067 | state.distbits = opts.bits; | |
1068 | // state.distcode = state.next; | |
1069 | ||
1070 | if (ret) { | |
1071 | strm.msg = 'invalid distances set'; | |
1072 | state.mode = BAD; | |
1073 | break; | |
1074 | } | |
1075 | //Tracev((stderr, 'inflate: codes ok\n')); | |
1076 | state.mode = LEN_; | |
1077 | if (flush === Z_TREES) { break inf_leave; } | |
1078 | /* falls through */ | |
1079 | case LEN_: | |
1080 | state.mode = LEN; | |
1081 | /* falls through */ | |
1082 | case LEN: | |
1083 | if (have >= 6 && left >= 258) { | |
1084 | //--- RESTORE() --- | |
1085 | strm.next_out = put; | |
1086 | strm.avail_out = left; | |
1087 | strm.next_in = next; | |
1088 | strm.avail_in = have; | |
1089 | state.hold = hold; | |
1090 | state.bits = bits; | |
1091 | //--- | |
1092 | inflate_fast(strm, _out); | |
1093 | //--- LOAD() --- | |
1094 | put = strm.next_out; | |
1095 | output = strm.output; | |
1096 | left = strm.avail_out; | |
1097 | next = strm.next_in; | |
1098 | input = strm.input; | |
1099 | have = strm.avail_in; | |
1100 | hold = state.hold; | |
1101 | bits = state.bits; | |
1102 | //--- | |
1103 | ||
1104 | if (state.mode === TYPE) { | |
1105 | state.back = -1; | |
1106 | } | |
1107 | break; | |
1108 | } | |
1109 | state.back = 0; | |
1110 | for (;;) { | |
1111 | here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ | |
1112 | here_bits = here >>> 24; | |
1113 | here_op = (here >>> 16) & 0xff; | |
1114 | here_val = here & 0xffff; | |
1115 | ||
1116 | if (here_bits <= bits) { break; } | |
1117 | //--- PULLBYTE() ---// | |
1118 | if (have === 0) { break inf_leave; } | |
1119 | have--; | |
1120 | hold += input[next++] << bits; | |
1121 | bits += 8; | |
1122 | //---// | |
1123 | } | |
1124 | if (here_op && (here_op & 0xf0) === 0) { | |
1125 | last_bits = here_bits; | |
1126 | last_op = here_op; | |
1127 | last_val = here_val; | |
1128 | for (;;) { | |
1129 | here = state.lencode[last_val + | |
1130 | ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; | |
1131 | here_bits = here >>> 24; | |
1132 | here_op = (here >>> 16) & 0xff; | |
1133 | here_val = here & 0xffff; | |
1134 | ||
1135 | if ((last_bits + here_bits) <= bits) { break; } | |
1136 | //--- PULLBYTE() ---// | |
1137 | if (have === 0) { break inf_leave; } | |
1138 | have--; | |
1139 | hold += input[next++] << bits; | |
1140 | bits += 8; | |
1141 | //---// | |
1142 | } | |
1143 | //--- DROPBITS(last.bits) ---// | |
1144 | hold >>>= last_bits; | |
1145 | bits -= last_bits; | |
1146 | //---// | |
1147 | state.back += last_bits; | |
1148 | } | |
1149 | //--- DROPBITS(here.bits) ---// | |
1150 | hold >>>= here_bits; | |
1151 | bits -= here_bits; | |
1152 | //---// | |
1153 | state.back += here_bits; | |
1154 | state.length = here_val; | |
1155 | if (here_op === 0) { | |
1156 | //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? | |
1157 | // "inflate: literal '%c'\n" : | |
1158 | // "inflate: literal 0x%02x\n", here.val)); | |
1159 | state.mode = LIT; | |
1160 | break; | |
1161 | } | |
1162 | if (here_op & 32) { | |
1163 | //Tracevv((stderr, "inflate: end of block\n")); | |
1164 | state.back = -1; | |
1165 | state.mode = TYPE; | |
1166 | break; | |
1167 | } | |
1168 | if (here_op & 64) { | |
1169 | strm.msg = 'invalid literal/length code'; | |
1170 | state.mode = BAD; | |
1171 | break; | |
1172 | } | |
1173 | state.extra = here_op & 15; | |
1174 | state.mode = LENEXT; | |
1175 | /* falls through */ | |
1176 | case LENEXT: | |
1177 | if (state.extra) { | |
1178 | //=== NEEDBITS(state.extra); | |
1179 | n = state.extra; | |
1180 | while (bits < n) { | |
1181 | if (have === 0) { break inf_leave; } | |
1182 | have--; | |
1183 | hold += input[next++] << bits; | |
1184 | bits += 8; | |
1185 | } | |
1186 | //===// | |
1187 | state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; | |
1188 | //--- DROPBITS(state.extra) ---// | |
1189 | hold >>>= state.extra; | |
1190 | bits -= state.extra; | |
1191 | //---// | |
1192 | state.back += state.extra; | |
1193 | } | |
1194 | //Tracevv((stderr, "inflate: length %u\n", state.length)); | |
1195 | state.was = state.length; | |
1196 | state.mode = DIST; | |
1197 | /* falls through */ | |
1198 | case DIST: | |
1199 | for (;;) { | |
1200 | here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/ | |
1201 | here_bits = here >>> 24; | |
1202 | here_op = (here >>> 16) & 0xff; | |
1203 | here_val = here & 0xffff; | |
1204 | ||
1205 | if ((here_bits) <= bits) { break; } | |
1206 | //--- PULLBYTE() ---// | |
1207 | if (have === 0) { break inf_leave; } | |
1208 | have--; | |
1209 | hold += input[next++] << bits; | |
1210 | bits += 8; | |
1211 | //---// | |
1212 | } | |
1213 | if ((here_op & 0xf0) === 0) { | |
1214 | last_bits = here_bits; | |
1215 | last_op = here_op; | |
1216 | last_val = here_val; | |
1217 | for (;;) { | |
1218 | here = state.distcode[last_val + | |
1219 | ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; | |
1220 | here_bits = here >>> 24; | |
1221 | here_op = (here >>> 16) & 0xff; | |
1222 | here_val = here & 0xffff; | |
1223 | ||
1224 | if ((last_bits + here_bits) <= bits) { break; } | |
1225 | //--- PULLBYTE() ---// | |
1226 | if (have === 0) { break inf_leave; } | |
1227 | have--; | |
1228 | hold += input[next++] << bits; | |
1229 | bits += 8; | |
1230 | //---// | |
1231 | } | |
1232 | //--- DROPBITS(last.bits) ---// | |
1233 | hold >>>= last_bits; | |
1234 | bits -= last_bits; | |
1235 | //---// | |
1236 | state.back += last_bits; | |
1237 | } | |
1238 | //--- DROPBITS(here.bits) ---// | |
1239 | hold >>>= here_bits; | |
1240 | bits -= here_bits; | |
1241 | //---// | |
1242 | state.back += here_bits; | |
1243 | if (here_op & 64) { | |
1244 | strm.msg = 'invalid distance code'; | |
1245 | state.mode = BAD; | |
1246 | break; | |
1247 | } | |
1248 | state.offset = here_val; | |
1249 | state.extra = (here_op) & 15; | |
1250 | state.mode = DISTEXT; | |
1251 | /* falls through */ | |
1252 | case DISTEXT: | |
1253 | if (state.extra) { | |
1254 | //=== NEEDBITS(state.extra); | |
1255 | n = state.extra; | |
1256 | while (bits < n) { | |
1257 | if (have === 0) { break inf_leave; } | |
1258 | have--; | |
1259 | hold += input[next++] << bits; | |
1260 | bits += 8; | |
1261 | } | |
1262 | //===// | |
1263 | state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; | |
1264 | //--- DROPBITS(state.extra) ---// | |
1265 | hold >>>= state.extra; | |
1266 | bits -= state.extra; | |
1267 | //---// | |
1268 | state.back += state.extra; | |
1269 | } | |
1270 | //#ifdef INFLATE_STRICT | |
1271 | if (state.offset > state.dmax) { | |
1272 | strm.msg = 'invalid distance too far back'; | |
1273 | state.mode = BAD; | |
1274 | break; | |
1275 | } | |
1276 | //#endif | |
1277 | //Tracevv((stderr, "inflate: distance %u\n", state.offset)); | |
1278 | state.mode = MATCH; | |
1279 | /* falls through */ | |
1280 | case MATCH: | |
1281 | if (left === 0) { break inf_leave; } | |
1282 | copy = _out - left; | |
1283 | if (state.offset > copy) { /* copy from window */ | |
1284 | copy = state.offset - copy; | |
1285 | if (copy > state.whave) { | |
1286 | if (state.sane) { | |
1287 | strm.msg = 'invalid distance too far back'; | |
1288 | state.mode = BAD; | |
1289 | break; | |
1290 | } | |
1291 | // (!) This block is disabled in zlib defailts, | |
1292 | // don't enable it for binary compatibility | |
1293 | //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR | |
1294 | // Trace((stderr, "inflate.c too far\n")); | |
1295 | // copy -= state.whave; | |
1296 | // if (copy > state.length) { copy = state.length; } | |
1297 | // if (copy > left) { copy = left; } | |
1298 | // left -= copy; | |
1299 | // state.length -= copy; | |
1300 | // do { | |
1301 | // output[put++] = 0; | |
1302 | // } while (--copy); | |
1303 | // if (state.length === 0) { state.mode = LEN; } | |
1304 | // break; | |
1305 | //#endif | |
1306 | } | |
1307 | if (copy > state.wnext) { | |
1308 | copy -= state.wnext; | |
1309 | from = state.wsize - copy; | |
1310 | } | |
1311 | else { | |
1312 | from = state.wnext - copy; | |
1313 | } | |
1314 | if (copy > state.length) { copy = state.length; } | |
1315 | from_source = state.window; | |
1316 | } | |
1317 | else { /* copy from output */ | |
1318 | from_source = output; | |
1319 | from = put - state.offset; | |
1320 | copy = state.length; | |
1321 | } | |
1322 | if (copy > left) { copy = left; } | |
1323 | left -= copy; | |
1324 | state.length -= copy; | |
1325 | do { | |
1326 | output[put++] = from_source[from++]; | |
1327 | } while (--copy); | |
1328 | if (state.length === 0) { state.mode = LEN; } | |
1329 | break; | |
1330 | case LIT: | |
1331 | if (left === 0) { break inf_leave; } | |
1332 | output[put++] = state.length; | |
1333 | left--; | |
1334 | state.mode = LEN; | |
1335 | break; | |
1336 | case CHECK: | |
1337 | if (state.wrap) { | |
1338 | //=== NEEDBITS(32); | |
1339 | while (bits < 32) { | |
1340 | if (have === 0) { break inf_leave; } | |
1341 | have--; | |
1342 | // Use '|' insdead of '+' to make sure that result is signed | |
1343 | hold |= input[next++] << bits; | |
1344 | bits += 8; | |
1345 | } | |
1346 | //===// | |
1347 | _out -= left; | |
1348 | strm.total_out += _out; | |
1349 | state.total += _out; | |
1350 | if (_out) { | |
1351 | strm.adler = state.check = | |
1352 | /*UPDATE(state.check, put - _out, _out);*/ | |
1353 | (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out)); | |
1354 | ||
1355 | } | |
1356 | _out = left; | |
1357 | // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too | |
1358 | if ((state.flags ? hold : zswap32(hold)) !== state.check) { | |
1359 | strm.msg = 'incorrect data check'; | |
1360 | state.mode = BAD; | |
1361 | break; | |
1362 | } | |
1363 | //=== INITBITS(); | |
1364 | hold = 0; | |
1365 | bits = 0; | |
1366 | //===// | |
1367 | //Tracev((stderr, "inflate: check matches trailer\n")); | |
1368 | } | |
1369 | state.mode = LENGTH; | |
1370 | /* falls through */ | |
1371 | case LENGTH: | |
1372 | if (state.wrap && state.flags) { | |
1373 | //=== NEEDBITS(32); | |
1374 | while (bits < 32) { | |
1375 | if (have === 0) { break inf_leave; } | |
1376 | have--; | |
1377 | hold += input[next++] << bits; | |
1378 | bits += 8; | |
1379 | } | |
1380 | //===// | |
1381 | if (hold !== (state.total & 0xffffffff)) { | |
1382 | strm.msg = 'incorrect length check'; | |
1383 | state.mode = BAD; | |
1384 | break; | |
1385 | } | |
1386 | //=== INITBITS(); | |
1387 | hold = 0; | |
1388 | bits = 0; | |
1389 | //===// | |
1390 | //Tracev((stderr, "inflate: length matches trailer\n")); | |
1391 | } | |
1392 | state.mode = DONE; | |
1393 | /* falls through */ | |
1394 | case DONE: | |
1395 | ret = Z_STREAM_END; | |
1396 | break inf_leave; | |
1397 | case BAD: | |
1398 | ret = Z_DATA_ERROR; | |
1399 | break inf_leave; | |
1400 | case MEM: | |
1401 | return Z_MEM_ERROR; | |
1402 | case SYNC: | |
1403 | /* falls through */ | |
1404 | default: | |
1405 | return Z_STREAM_ERROR; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" | |
1410 | ||
1411 | /* | |
1412 | Return from inflate(), updating the total counts and the check value. | |
1413 | If there was no progress during the inflate() call, return a buffer | |
1414 | error. Call updatewindow() to create and/or update the window state. | |
1415 | Note: a memory error from inflate() is non-recoverable. | |
1416 | */ | |
1417 | ||
1418 | //--- RESTORE() --- | |
1419 | strm.next_out = put; | |
1420 | strm.avail_out = left; | |
1421 | strm.next_in = next; | |
1422 | strm.avail_in = have; | |
1423 | state.hold = hold; | |
1424 | state.bits = bits; | |
1425 | //--- | |
1426 | ||
1427 | if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && | |
1428 | (state.mode < CHECK || flush !== Z_FINISH))) { | |
1429 | if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) { | |
1430 | state.mode = MEM; | |
1431 | return Z_MEM_ERROR; | |
1432 | } | |
1433 | } | |
1434 | _in -= strm.avail_in; | |
1435 | _out -= strm.avail_out; | |
1436 | strm.total_in += _in; | |
1437 | strm.total_out += _out; | |
1438 | state.total += _out; | |
1439 | if (state.wrap && _out) { | |
1440 | strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/ | |
1441 | (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out)); | |
1442 | } | |
1443 | strm.data_type = state.bits + (state.last ? 64 : 0) + | |
1444 | (state.mode === TYPE ? 128 : 0) + | |
1445 | (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); | |
1446 | if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { | |
1447 | ret = Z_BUF_ERROR; | |
1448 | } | |
1449 | return ret; | |
1450 | } | |
1451 | ||
1452 | function inflateEnd(strm) { | |
1453 | ||
1454 | if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) { | |
1455 | return Z_STREAM_ERROR; | |
1456 | } | |
1457 | ||
1458 | var state = strm.state; | |
1459 | if (state.window) { | |
1460 | state.window = null; | |
1461 | } | |
1462 | strm.state = null; | |
1463 | return Z_OK; | |
1464 | } | |
1465 | ||
1466 | function inflateGetHeader(strm, head) { | |
1467 | var state; | |
1468 | ||
1469 | /* check state */ | |
1470 | if (!strm || !strm.state) { return Z_STREAM_ERROR; } | |
1471 | state = strm.state; | |
1472 | if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; } | |
1473 | ||
1474 | /* save header structure */ | |
1475 | state.head = head; | |
1476 | head.done = false; | |
1477 | return Z_OK; | |
1478 | } | |
1479 | ||
1480 | function inflateSetDictionary(strm, dictionary) { | |
1481 | var dictLength = dictionary.length; | |
1482 | ||
1483 | var state; | |
1484 | var dictid; | |
1485 | var ret; | |
1486 | ||
1487 | /* check state */ | |
1488 | if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; } | |
1489 | state = strm.state; | |
1490 | ||
1491 | if (state.wrap !== 0 && state.mode !== DICT) { | |
1492 | return Z_STREAM_ERROR; | |
1493 | } | |
1494 | ||
1495 | /* check for correct dictionary identifier */ | |
1496 | if (state.mode === DICT) { | |
1497 | dictid = 1; /* adler32(0, null, 0)*/ | |
1498 | /* dictid = adler32(dictid, dictionary, dictLength); */ | |
1499 | dictid = adler32(dictid, dictionary, dictLength, 0); | |
1500 | if (dictid !== state.check) { | |
1501 | return Z_DATA_ERROR; | |
1502 | } | |
1503 | } | |
1504 | /* copy dictionary to window using updatewindow(), which will amend the | |
1505 | existing dictionary if appropriate */ | |
1506 | ret = updatewindow(strm, dictionary, dictLength, dictLength); | |
1507 | if (ret) { | |
1508 | state.mode = MEM; | |
1509 | return Z_MEM_ERROR; | |
1510 | } | |
1511 | state.havedict = 1; | |
1512 | // Tracev((stderr, "inflate: dictionary set\n")); | |
1513 | return Z_OK; | |
1514 | } | |
1515 | ||
1516 | export { inflateReset, inflateReset2, inflateResetKeep, inflateInit, inflateInit2, inflate, inflateEnd, inflateGetHeader, inflateSetDictionary }; | |
858ea4a7 | 1517 | export var inflateInfo = 'pako inflate (from Nodeca project)'; |
fba220c6 SR |
1518 | |
1519 | /* Not implemented | |
1520 | exports.inflateCopy = inflateCopy; | |
1521 | exports.inflateGetDictionary = inflateGetDictionary; | |
1522 | exports.inflateMark = inflateMark; | |
1523 | exports.inflatePrime = inflatePrime; | |
1524 | exports.inflateSync = inflateSync; | |
1525 | exports.inflateSyncPoint = inflateSyncPoint; | |
1526 | exports.inflateUndermine = inflateUndermine; | |
1527 | */ |