source: trip-planner-front/node_modules/pako/lib/zlib/inflate.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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