1import * as utils from "../utils/common.js";
2import adler32 from "./adler32.js";
3import crc32 from "./crc32.js";
4import inflate_fast from "./inffast.js";
5import inflate_table from "./inftrees.js";
11/* Public constants ==========================================================*/
12/* ===========================================================================*/
15/* Allowed flush values; see deflate() and inflate() below for details */
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;
20export const Z_FINISH = 4;
21export const Z_BLOCK = 5;
22export const Z_TREES = 6;
25/* Return codes for the compression/decompression functions. Negative values
26 * are errors, positive values are used for special but normal events.
29export const Z_STREAM_END = 1;
30export const Z_NEED_DICT = 2;
31//export const Z_ERRNO = -1;
32export const Z_STREAM_ERROR = -2;
33export const Z_DATA_ERROR = -3;
34export const Z_MEM_ERROR = -4;
35export const Z_BUF_ERROR = -5;
36//export const Z_VERSION_ERROR = -6;
38/* The deflate compression method */
39export const Z_DEFLATED = 8;
42/* STATES ====================================================================*/
43/* ===========================================================================*/
46var HEAD = 1; /* i: waiting for magic header */
47var FLAGS = 2; /* i: waiting for method and flags (gzip) */
48var TIME = 3; /* i: waiting for modification time (gzip) */
49var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
50var EXLEN = 5; /* i: waiting for extra length (gzip) */
51var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
52var NAME = 7; /* i: waiting for end of file name (gzip) */
53var COMMENT = 8; /* i: waiting for end of comment (gzip) */
54var HCRC = 9; /* i: waiting for header crc (gzip) */
55var DICTID = 10; /* i: waiting for dictionary check value */
56var DICT = 11; /* waiting for inflateSetDictionary() call */
57var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
58var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
59var STORED = 14; /* i: waiting for stored size (length and complement) */
60var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
61var COPY = 16; /* i/o: waiting for input or output to copy stored block */
62var TABLE = 17; /* i: waiting for dynamic block table lengths */
63var LENLENS = 18; /* i: waiting for code length code lengths */
64var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
65var LEN_ = 20; /* i: same as LEN below, but only first time in */
66var LEN = 21; /* i: waiting for length/lit/eob code */
67var LENEXT = 22; /* i: waiting for length extra bits */
68var DIST = 23; /* i: waiting for distance code */
69var DISTEXT = 24; /* i: waiting for distance extra bits */
70var MATCH = 25; /* o: waiting for output space to copy string */
71var LIT = 26; /* o: waiting for output space to write literal */
72var CHECK = 27; /* i: waiting for 32-bit check value */
73var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
74var DONE = 29; /* finished check, done -- remain here until reset */
75var BAD = 30; /* got a data error -- remain here until reset */
76var MEM = 31; /* got an inflate() memory error -- remain here until reset */
77var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
79/* ===========================================================================*/
84var ENOUGH_DISTS = 592;
85//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
89var DEF_WBITS = MAX_WBITS;
93 return (((q >>> 24) & 0xff) +
94 ((q >>> 8) & 0xff00) +
100function 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 */
110 this.head = null; /* where to save gzip header information */
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 */
119 /* bit accumulator */
120 this.hold = 0; /* input bit accumulator */
121 this.bits = 0; /* number of bits in "in" */
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 */
127 /* for table and code decoding */
128 this.extra = 0; /* extra bits needed */
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 */
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[] */
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 */
147 because we don't have pointers in js, we use lencode and distcode directly
148 as buffers so we don't need codes
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 */
158function inflateResetKeep(strm) {
161 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
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;
172 state.head = null/*Z_NULL*/;
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);
181 //Tracev((stderr, "inflate: reset\n"));
185function inflateReset(strm) {
188 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
193 return inflateResetKeep(strm);
197function inflateReset2(strm, windowBits) {
202 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
205 /* extract wrap request from windowBits parameter */
206 if (windowBits < 0) {
208 windowBits = -windowBits;
211 wrap = (windowBits >> 4) + 1;
212 if (windowBits < 48) {
217 /* set number of window bits, free window if different */
218 if (windowBits && (windowBits < 8 || windowBits > 15)) {
219 return Z_STREAM_ERROR;
221 if (state.window !== null && state.wbits !== windowBits) {
225 /* update state and reset the rest of it */
227 state.wbits = windowBits;
228 return inflateReset(strm);
231function inflateInit2(strm, windowBits) {
235 if (!strm) { return Z_STREAM_ERROR; }
236 //strm.msg = Z_NULL; /* in case we return an error */
238 state = new InflateState();
240 //if (state === Z_NULL) return Z_MEM_ERROR;
241 //Tracev((stderr, "inflate: allocated\n"));
243 state.window = null/*Z_NULL*/;
244 ret = inflateReset2(strm, windowBits);
246 strm.state = null/*Z_NULL*/;
251function inflateInit(strm) {
252 return inflateInit2(strm, DEF_WBITS);
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.
268var lenfix, distfix; // We have no pointers in JS, so keep tables separate
270function fixedtables(state) {
271 /* build fixed huffman tables if first call (may not be thread safe) */
275 lenfix = new utils.Buf32(512);
276 distfix = new utils.Buf32(32);
278 /* literal/length table */
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; }
285 inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
289 while (sym < 32) { state.lens[sym++] = 5; }
291 inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
293 /* do this just once */
297 state.lencode = lenfix;
299 state.distcode = distfix;
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
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.
318function updatewindow(strm, src, end, copy) {
320 var state = strm.state;
322 /* if it hasn't been done already, allocate space for the window */
323 if (state.window === null) {
324 state.wsize = 1 << state.wbits;
328 state.window = new utils.Buf8(state.wsize);
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);
335 state.whave = state.wsize;
338 dist = state.wsize - state.wnext;
342 //zmemcpy(state->window + state->wnext, end - copy, dist);
343 utils.arraySet(state.window, src, end - copy, dist, state.wnext);
346 //zmemcpy(state->window, end - copy, copy);
347 utils.arraySet(state.window, src, end - copy, copy, 0);
349 state.whave = state.wsize;
353 if (state.wnext === state.wsize) { state.wnext = 0; }
354 if (state.whave < state.wsize) { state.whave += dist; }
360function inflate(strm, flush) {
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 */
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 */
381 var n; // temporary var for NEED_BITS
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 ];
387 if (!strm || !strm.state || !strm.output ||
388 (!strm.input && strm.avail_in !== 0)) {
389 return Z_STREAM_ERROR;
393 if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
398 output = strm.output;
399 left = strm.avail_out;
402 have = strm.avail_in;
411 inf_leave: // goto emulation
413 switch (state.mode) {
415 if (state.wrap === 0) {
421 if (have === 0) { break inf_leave; }
423 hold += input[next++] << bits;
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);
442 state.flags = 0; /* expect zlib header */
444 state.head.done = false;
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';
452 if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
453 strm.msg = 'unknown compression method';
457 //--- DROPBITS(4) ---//
461 len = (hold & 0x0f)/*BITS(4)*/ + 8;
462 if (state.wbits === 0) {
465 else if (len > state.wbits) {
466 strm.msg = 'invalid window size';
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;
480 //=== NEEDBITS(16); */
482 if (have === 0) { break inf_leave; }
484 hold += input[next++] << bits;
489 if ((state.flags & 0xff) !== Z_DEFLATED) {
490 strm.msg = 'unknown compression method';
494 if (state.flags & 0xe000) {
495 strm.msg = 'unknown header flags set';
500 state.head.text = ((hold >> 8) & 1);
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);
516 //=== NEEDBITS(32); */
518 if (have === 0) { break inf_leave; }
520 hold += input[next++] << bits;
525 state.head.time = hold;
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);
543 //=== NEEDBITS(16); */
545 if (have === 0) { break inf_leave; }
547 hold += input[next++] << bits;
552 state.head.xflags = (hold & 0xff);
553 state.head.os = (hold >> 8);
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);
569 if (state.flags & 0x0400) {
570 //=== NEEDBITS(16); */
572 if (have === 0) { break inf_leave; }
574 hold += input[next++] << bits;
580 state.head.extra_len = hold;
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);
594 else if (state.head) {
595 state.head.extra = null/*Z_NULL*/;
600 if (state.flags & 0x0400) {
602 if (copy > have) { copy = have; }
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);
614 // extra field is limited to 65536 bytes
615 // - no need for additional size check
617 /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
620 //zmemcpy(state.head.extra + len, next,
621 // len + copy > state.head.extra_max ?
622 // state.head.extra_max - len : copy);
624 if (state.flags & 0x0200) {
625 state.check = crc32(state.check, input, copy, next);
629 state.length -= copy;
631 if (state.length) { break inf_leave; }
637 if (state.flags & 0x0800) {
638 if (have === 0) { break inf_leave; }
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);
648 } while (len && copy < have);
650 if (state.flags & 0x0200) {
651 state.check = crc32(state.check, input, copy, next);
655 if (len) { break inf_leave; }
657 else if (state.head) {
658 state.head.name = null;
661 state.mode = COMMENT;
664 if (state.flags & 0x1000) {
665 if (have === 0) { break inf_leave; }
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);
674 } while (len && copy < have);
675 if (state.flags & 0x0200) {
676 state.check = crc32(state.check, input, copy, next);
680 if (len) { break inf_leave; }
682 else if (state.head) {
683 state.head.comment = null;
688 if (state.flags & 0x0200) {
689 //=== NEEDBITS(16); */
691 if (have === 0) { break inf_leave; }
693 hold += input[next++] << bits;
697 if (hold !== (state.check & 0xffff)) {
698 strm.msg = 'header crc mismatch';
708 state.head.hcrc = ((state.flags >> 9) & 1);
709 state.head.done = true;
711 strm.adler = state.check = 0;
715 //=== NEEDBITS(32); */
717 if (have === 0) { break inf_leave; }
719 hold += input[next++] << bits;
723 strm.adler = state.check = zswap32(hold);
731 if (state.havedict === 0) {
734 strm.avail_out = left;
736 strm.avail_in = have;
742 strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
746 if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
750 //--- BYTEBITS() ---//
757 //=== NEEDBITS(3); */
759 if (have === 0) { break inf_leave; }
761 hold += input[next++] << bits;
765 state.last = (hold & 0x01)/*BITS(1)*/;
766 //--- DROPBITS(1) ---//
771 switch ((hold & 0x03)/*BITS(2)*/) {
772 case 0: /* stored block */
773 //Tracev((stderr, "inflate: stored block%s\n",
774 // state.last ? " (last)" : ""));
777 case 1: /* fixed block */
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) ---//
790 case 2: /* dynamic block */
791 //Tracev((stderr, "inflate: dynamic codes block%s\n",
792 // state.last ? " (last)" : ""));
796 strm.msg = 'invalid block type';
799 //--- DROPBITS(2) ---//
805 //--- BYTEBITS() ---// /* go to byte boundary */
809 //=== NEEDBITS(32); */
811 if (have === 0) { break inf_leave; }
813 hold += input[next++] << bits;
817 if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
818 strm.msg = 'invalid stored block lengths';
822 state.length = hold & 0xffff;
823 //Tracev((stderr, "inflate: stored length %u\n",
830 if (flush === Z_TREES) { break inf_leave; }
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);
848 state.length -= copy;
851 //Tracev((stderr, "inflate: stored end\n"));
855 //=== NEEDBITS(14); */
857 if (have === 0) { break inf_leave; }
859 hold += input[next++] << bits;
863 state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
864 //--- DROPBITS(5) ---//
868 state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
869 //--- DROPBITS(5) ---//
873 state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
874 //--- DROPBITS(4) ---//
878//#ifndef PKZIP_BUG_WORKAROUND
879 if (state.nlen > 286 || state.ndist > 30) {
880 strm.msg = 'too many length or distance symbols';
885 //Tracev((stderr, "inflate: table sizes ok\n"));
887 state.mode = LENLENS;
890 while (state.have < state.ncode) {
893 if (have === 0) { break inf_leave; }
895 hold += input[next++] << bits;
899 state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
900 //--- DROPBITS(3) ---//
905 while (state.have < 19) {
906 state.lens[order[state.have++]] = 0;
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;
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;
920 strm.msg = 'invalid code lengths set';
924 //Tracev((stderr, "inflate: code lengths ok\n"));
926 state.mode = CODELENS;
929 while (state.have < state.nlen + state.ndist) {
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;
936 if ((here_bits) <= bits) { break; }
937 //--- PULLBYTE() ---//
938 if (have === 0) { break inf_leave; }
940 hold += input[next++] << bits;
945 //--- DROPBITS(here.bits) ---//
949 state.lens[state.have++] = here_val;
952 if (here_val === 16) {
953 //=== NEEDBITS(here.bits + 2);
956 if (have === 0) { break inf_leave; }
958 hold += input[next++] << bits;
962 //--- DROPBITS(here.bits) ---//
966 if (state.have === 0) {
967 strm.msg = 'invalid bit length repeat';
971 len = state.lens[state.have - 1];
972 copy = 3 + (hold & 0x03);//BITS(2);
973 //--- DROPBITS(2) ---//
978 else if (here_val === 17) {
979 //=== NEEDBITS(here.bits + 3);
982 if (have === 0) { break inf_leave; }
984 hold += input[next++] << bits;
988 //--- DROPBITS(here.bits) ---//
993 copy = 3 + (hold & 0x07);//BITS(3);
994 //--- DROPBITS(3) ---//
1000 //=== NEEDBITS(here.bits + 7);
1003 if (have === 0) { break inf_leave; }
1005 hold += input[next++] << bits;
1009 //--- DROPBITS(here.bits) ---//
1010 hold >>>= here_bits;
1014 copy = 11 + (hold & 0x7f);//BITS(7);
1015 //--- DROPBITS(7) ---//
1020 if (state.have + copy > state.nlen + state.ndist) {
1021 strm.msg = 'invalid bit length repeat';
1026 state.lens[state.have++] = len;
1031 /* handle error breaks in while */
1032 if (state.mode === BAD) { break; }
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';
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 */
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;
1054 strm.msg = 'invalid literal/lengths set';
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;
1071 strm.msg = 'invalid distances set';
1075 //Tracev((stderr, 'inflate: codes ok\n'));
1077 if (flush === Z_TREES) { break inf_leave; }
1083 if (have >= 6 && left >= 258) {
1085 strm.next_out = put;
1086 strm.avail_out = left;
1087 strm.next_in = next;
1088 strm.avail_in = have;
1092 inflate_fast(strm, _out);
1094 put = strm.next_out;
1095 output = strm.output;
1096 left = strm.avail_out;
1097 next = strm.next_in;
1099 have = strm.avail_in;
1104 if (state.mode === TYPE) {
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;
1116 if (here_bits <= bits) { break; }
1117 //--- PULLBYTE() ---//
1118 if (have === 0) { break inf_leave; }
1120 hold += input[next++] << bits;
1124 if (here_op && (here_op & 0xf0) === 0) {
1125 last_bits = here_bits;
1127 last_val = here_val;
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;
1135 if ((last_bits + here_bits) <= bits) { break; }
1136 //--- PULLBYTE() ---//
1137 if (have === 0) { break inf_leave; }
1139 hold += input[next++] << bits;
1143 //--- DROPBITS(last.bits) ---//
1144 hold >>>= last_bits;
1147 state.back += last_bits;
1149 //--- DROPBITS(here.bits) ---//
1150 hold >>>= here_bits;
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));
1163 //Tracevv((stderr, "inflate: end of block\n"));
1169 strm.msg = 'invalid literal/length code';
1173 state.extra = here_op & 15;
1174 state.mode = LENEXT;
1178 //=== NEEDBITS(state.extra);
1181 if (have === 0) { break inf_leave; }
1183 hold += input[next++] << bits;
1187 state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
1188 //--- DROPBITS(state.extra) ---//
1189 hold >>>= state.extra;
1190 bits -= state.extra;
1192 state.back += state.extra;
1194 //Tracevv((stderr, "inflate: length %u\n", state.length));
1195 state.was = state.length;
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;
1205 if ((here_bits) <= bits) { break; }
1206 //--- PULLBYTE() ---//
1207 if (have === 0) { break inf_leave; }
1209 hold += input[next++] << bits;
1213 if ((here_op & 0xf0) === 0) {
1214 last_bits = here_bits;
1216 last_val = here_val;
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;
1224 if ((last_bits + here_bits) <= bits) { break; }
1225 //--- PULLBYTE() ---//
1226 if (have === 0) { break inf_leave; }
1228 hold += input[next++] << bits;
1232 //--- DROPBITS(last.bits) ---//
1233 hold >>>= last_bits;
1236 state.back += last_bits;
1238 //--- DROPBITS(here.bits) ---//
1239 hold >>>= here_bits;
1242 state.back += here_bits;
1244 strm.msg = 'invalid distance code';
1248 state.offset = here_val;
1249 state.extra = (here_op) & 15;
1250 state.mode = DISTEXT;
1254 //=== NEEDBITS(state.extra);
1257 if (have === 0) { break inf_leave; }
1259 hold += input[next++] << bits;
1263 state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
1264 //--- DROPBITS(state.extra) ---//
1265 hold >>>= state.extra;
1266 bits -= state.extra;
1268 state.back += state.extra;
1270//#ifdef INFLATE_STRICT
1271 if (state.offset > state.dmax) {
1272 strm.msg = 'invalid distance too far back';
1277 //Tracevv((stderr, "inflate: distance %u\n", state.offset));
1281 if (left === 0) { break inf_leave; }
1283 if (state.offset > copy) { /* copy from window */
1284 copy = state.offset - copy;
1285 if (copy > state.whave) {
1287 strm.msg = 'invalid distance too far back';
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; }
1299// state.length -= copy;
1301// output[put++] = 0;
1303// if (state.length === 0) { state.mode = LEN; }
1307 if (copy > state.wnext) {
1308 copy -= state.wnext;
1309 from = state.wsize - copy;
1312 from = state.wnext - copy;
1314 if (copy > state.length) { copy = state.length; }
1315 from_source = state.window;
1317 else { /* copy from output */
1318 from_source = output;
1319 from = put - state.offset;
1320 copy = state.length;
1322 if (copy > left) { copy = left; }
1324 state.length -= copy;
1326 output[put++] = from_source[from++];
1328 if (state.length === 0) { state.mode = LEN; }
1331 if (left === 0) { break inf_leave; }
1332 output[put++] = state.length;
1340 if (have === 0) { break inf_leave; }
1342 // Use '|' insdead of '+' to make sure that result is signed
1343 hold |= input[next++] << bits;
1348 strm.total_out += _out;
1349 state.total += _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));
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';
1367 //Tracev((stderr, "inflate: check matches trailer\n"));
1369 state.mode = LENGTH;
1372 if (state.wrap && state.flags) {
1375 if (have === 0) { break inf_leave; }
1377 hold += input[next++] << bits;
1381 if (hold !== (state.total & 0xffffffff)) {
1382 strm.msg = 'incorrect length check';
1390 //Tracev((stderr, "inflate: length matches trailer\n"));
1405 return Z_STREAM_ERROR;
1409 // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
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.
1419 strm.next_out = put;
1420 strm.avail_out = left;
1421 strm.next_in = next;
1422 strm.avail_in = have;
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)) {
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));
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) {
1452function inflateEnd(strm) {
1454 if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
1455 return Z_STREAM_ERROR;
1458 var state = strm.state;
1460 state.window = null;
1466function inflateGetHeader(strm, head) {
1470 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1472 if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
1474 /* save header structure */
1480function inflateSetDictionary(strm, dictionary) {
1481 var dictLength = dictionary.length;
1488 if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; }
1491 if (state.wrap !== 0 && state.mode !== DICT) {
1492 return Z_STREAM_ERROR;
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;
1504 /* copy dictionary to window using updatewindow(), which will amend the
1505 existing dictionary if appropriate */
1506 ret = updatewindow(strm, dictionary, dictLength, dictLength);
1512 // Tracev((stderr, "inflate: dictionary set\n"));
1516export { inflateReset, inflateReset2, inflateResetKeep, inflateInit, inflateInit2, inflate, inflateEnd, inflateGetHeader, inflateSetDictionary };
1517export var inflateInfo = 'pako inflate (from Nodeca project)';
1520exports.inflateCopy = inflateCopy;
1521exports.inflateGetDictionary = inflateGetDictionary;
1522exports.inflateMark = inflateMark;
1523exports.inflatePrime = inflatePrime;
1524exports.inflateSync = inflateSync;
1525exports.inflateSyncPoint = inflateSyncPoint;
1526exports.inflateUndermine = inflateUndermine;