]>
git.proxmox.com Git - mirror_novnc.git/blob - include/inflator.js
1 (function(f
){if(typeof exports
==="object"&&typeof module
!=="undefined"){module
.exports
=f()}else if(typeof define
==="function"&&define
.amd
){define([],f
)}else{var g
;if(typeof window
!=="undefined"){g
=window
}else if(typeof global
!=="undefined"){g
=global
}else if(typeof self
!=="undefined"){g
=self
}else{g
=this}g
.inflator
= f()}})(function(){var define
,module
,exports
;return (function e(t
,n
,r
){function s(o
,u
){if(!n
[o
]){if(!t
[o
]){var a
=typeof require
=="function"&&require
;if(!u
&&a
)return a(o
,!0);if(i
)return i(o
,!0);var f
=new Error("Cannot find module '"+o
+"'");throw f
.code
="MODULE_NOT_FOUND",f
}var l
=n
[o
]={exports
:{}};t
[o
][0].call(l
.exports
,function(e
){var n
=t
[o
][1][e
];return s(n
?n
:e
)},l
,l
.exports
,e
,t
,n
,r
)}return n
[o
].exports
}var i
=typeof require
=="function"&&require
;for(var o
=0;o
<r
.length
;o
++)s(r
[o
]);return s
})({1:[function(require
,module
,exports
){
5 var TYPED_OK
= (typeof Uint8Array
!== 'undefined') &&
6 (typeof Uint16Array
!== 'undefined') &&
7 (typeof Int32Array
!== 'undefined');
10 exports
.assign = function (obj
/*from1, from2, from3, ...*/) {
11 var sources
= Array
.prototype.slice
.call(arguments
, 1);
12 while (sources
.length
) {
13 var source
= sources
.shift();
14 if (!source
) { continue; }
16 if (typeof source
!== 'object') {
17 throw new TypeError(source
+ 'must be non-object');
20 for (var p
in source
) {
21 if (source
.hasOwnProperty(p
)) {
31 // reduce buffer size, avoiding mem copy
32 exports
.shrinkBuf = function (buf
, size
) {
33 if (buf
.length
=== size
) { return buf
; }
34 if (buf
.subarray
) { return buf
.subarray(0, size
); }
41 arraySet: function (dest
, src
, src_offs
, len
, dest_offs
) {
42 if (src
.subarray
&& dest
.subarray
) {
43 dest
.set(src
.subarray(src_offs
, src_offs
+len
), dest_offs
);
46 // Fallback to ordinary array
47 for (var i
=0; i
<len
; i
++) {
48 dest
[dest_offs
+ i
] = src
[src_offs
+ i
];
51 // Join array of chunks to single array.
52 flattenChunks: function(chunks
) {
53 var i
, l
, len
, pos
, chunk
, result
;
55 // calculate data length
57 for (i
=0, l
=chunks
.length
; i
<l
; i
++) {
58 len
+= chunks
[i
].length
;
62 result
= new Uint8Array(len
);
64 for (i
=0, l
=chunks
.length
; i
<l
; i
++) {
66 result
.set(chunk
, pos
);
75 arraySet: function (dest
, src
, src_offs
, len
, dest_offs
) {
76 for (var i
=0; i
<len
; i
++) {
77 dest
[dest_offs
+ i
] = src
[src_offs
+ i
];
80 // Join array of chunks to single array.
81 flattenChunks: function(chunks
) {
82 return [].concat
.apply([], chunks
);
87 // Enable/Disable typed arrays use, for testing
89 exports
.setTyped = function (on
) {
91 exports
.Buf8
= Uint8Array
;
92 exports
.Buf16
= Uint16Array
;
93 exports
.Buf32
= Int32Array
;
94 exports
.assign(exports
, fnTyped
);
97 exports
.Buf16
= Array
;
98 exports
.Buf32
= Array
;
99 exports
.assign(exports
, fnUntyped
);
103 exports
.setTyped(TYPED_OK
);
105 },{}],2:[function(require
,module
,exports
){
108 // Note: adler32 takes 12% for level 0 and 2% for level 6.
109 // It doesn't worth to make additional optimizationa as in original.
110 // Small size is preferable.
112 function adler32(adler
, buf
, len
, pos
) {
113 var s1
= (adler
& 0xffff) |0,
114 s2
= ((adler
>>> 16) & 0xffff) |0,
118 // Set limit ~ twice less than 5552, to keep
119 // s2 in 31-bits, because we force signed ints.
120 // in other case %= will fail.
121 n
= len
> 2000 ? 2000 : len
;
125 s1
= (s1
+ buf
[pos
++]) |0;
133 return (s1
| (s2
<< 16)) |0;
137 module
.exports
= adler32
;
139 },{}],3:[function(require
,module
,exports
){
142 // Note: we can't get significant speed boost here.
143 // So write code to minimize size - no pregenerated tables
144 // and array tools dependencies.
147 // Use ordinary array, since untyped makes no boost here
148 function makeTable() {
151 for (var n
=0; n
< 256; n
++) {
153 for (var k
=0; k
< 8; k
++) {
154 c
= ((c
&1) ? (0xEDB88320 ^ (c
>>> 1)) : (c
>>> 1));
162 // Create table on load. Just 255 signed longs. Not a problem.
163 var crcTable
= makeTable();
166 function crc32(crc
, buf
, len
, pos
) {
172 for (var i
= pos
; i
< end
; i
++) {
173 crc
= (crc
>>> 8) ^ t
[(crc
^ buf
[i
]) & 0xFF];
176 return (crc
^ (-1)); // >>> 0;
180 module
.exports
= crc32
;
182 },{}],4:[function(require
,module
,exports
){
185 // See state defs from inflate.js
186 var BAD
= 30; /* got a data error -- remain here until reset */
187 var TYPE
= 12; /* i: waiting for type bits, including last-flag bit */
190 Decode literal, length, and distance codes and write out the resulting
191 literal and match bytes until either not enough input or output is
192 available, an end-of-block is encountered, or a data error is encountered.
193 When large enough input and output buffers are supplied to inflate(), for
194 example, a 16K input buffer and a 64K output buffer, more than 95% of the
195 inflate execution time is spent in this routine.
201 strm.avail_out >= 258
202 start >= strm.avail_out
205 On return, state.mode is one of:
207 LEN -- ran out of enough output space or enough available input
208 TYPE -- reached end of block code, inflate() to interpret next block
209 BAD -- error in block data
213 - The maximum input bits used by a length/distance pair is 15 bits for the
214 length code, 5 bits for the length extra, 15 bits for the distance code,
215 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
216 Therefore if strm.avail_in >= 6, then there is enough input to avoid
217 checking for available input while decoding.
219 - The maximum bytes that a single length/distance pair can output is 258
220 bytes, which is the maximum length that can be coded. inflate_fast()
221 requires strm.avail_out >= 258 for each loop to avoid checking for
224 module
.exports
= function inflate_fast(strm
, start
) {
226 var _in
; /* local strm.input */
227 var last
; /* have enough input while in < last */
228 var _out
; /* local strm.output */
229 var beg
; /* inflate()'s initial strm.output */
230 var end
; /* while out < end, enough space available */
231 //#ifdef INFLATE_STRICT
232 var dmax
; /* maximum distance from zlib header */
234 var wsize
; /* window size or zero if not using window */
235 var whave
; /* valid bytes in the window */
236 var wnext
; /* window write index */
237 var window
; /* allocated sliding window, if wsize != 0 */
238 var hold
; /* local strm.hold */
239 var bits
; /* local strm.bits */
240 var lcode
; /* local strm.lencode */
241 var dcode
; /* local strm.distcode */
242 var lmask
; /* mask for first level of length codes */
243 var dmask
; /* mask for first level of distance codes */
244 var here
; /* retrieved table entry */
245 var op
; /* code bits, operation, extra bits, or */
246 /* window position, window bytes to copy */
247 var len
; /* match length, unused bytes */
248 var dist
; /* match distance */
249 var from; /* where to copy match from */
253 var input
, output
; // JS specific, because we have no pointers
255 /* copy state to local variables */
260 last
= _in
+ (strm
.avail_in
- 5);
261 _out
= strm
.next_out
;
262 output
= strm
.output
;
263 beg
= _out
- (start
- strm
.avail_out
);
264 end
= _out
+ (strm
.avail_out
- 257);
265 //#ifdef INFLATE_STRICT
271 window
= state
.window
;
274 lcode
= state
.lencode
;
275 dcode
= state
.distcode
;
276 lmask
= (1 << state
.lenbits
) - 1;
277 dmask
= (1 << state
.distbits
) - 1;
280 /* decode literals and length/distances until end-of-block or not enough
281 input data or output space */
286 hold
+= input
[_in
++] << bits
;
288 hold
+= input
[_in
++] << bits
;
292 here
= lcode
[hold
& lmask
];
295 for (;;) { // Goto emulation
296 op
= here
>>> 24/*here.bits*/;
299 op
= (here
>>> 16) & 0xff/*here.op*/;
300 if (op
=== 0) { /* literal */
301 //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
302 // "inflate: literal '%c'\n" :
303 // "inflate: literal 0x%02x\n", here.val));
304 output
[_out
++] = here
& 0xffff/*here.val*/;
306 else if (op
& 16) { /* length base */
307 len
= here
& 0xffff/*here.val*/;
308 op
&= 15; /* number of extra bits */
311 hold
+= input
[_in
++] << bits
;
314 len
+= hold
& ((1 << op
) - 1);
318 //Tracevv((stderr, "inflate: length %u\n", len));
320 hold
+= input
[_in
++] << bits
;
322 hold
+= input
[_in
++] << bits
;
325 here
= dcode
[hold
& dmask
];
328 for (;;) { // goto emulation
329 op
= here
>>> 24/*here.bits*/;
332 op
= (here
>>> 16) & 0xff/*here.op*/;
334 if (op
& 16) { /* distance base */
335 dist
= here
& 0xffff/*here.val*/;
336 op
&= 15; /* number of extra bits */
338 hold
+= input
[_in
++] << bits
;
341 hold
+= input
[_in
++] << bits
;
345 dist
+= hold
& ((1 << op
) - 1);
346 //#ifdef INFLATE_STRICT
348 strm
.msg
= 'invalid distance too far back';
355 //Tracevv((stderr, "inflate: distance %u\n", dist));
356 op
= _out
- beg
; /* max distance in output */
357 if (dist
> op
) { /* see if copy from window */
358 op
= dist
- op
; /* distance back in window */
361 strm
.msg
= 'invalid distance too far back';
366 // (!) This block is disabled in zlib defailts,
367 // don't enable it for binary compatibility
368 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
369 // if (len <= op - whave) {
371 // output[_out++] = 0;
375 // len -= op - whave;
377 // output[_out++] = 0;
378 // } while (--op > whave);
380 // from = _out - dist;
382 // output[_out++] = output[from++];
388 from = 0; // window index
389 from_source
= window
;
390 if (wnext
=== 0) { /* very common case */
392 if (op
< len
) { /* some from window */
395 output
[_out
++] = window
[from++];
397 from = _out
- dist
; /* rest from output */
398 from_source
= output
;
401 else if (wnext
< op
) { /* wrap around window */
402 from += wsize
+ wnext
- op
;
404 if (op
< len
) { /* some from end of window */
407 output
[_out
++] = window
[from++];
410 if (wnext
< len
) { /* some from start of window */
414 output
[_out
++] = window
[from++];
416 from = _out
- dist
; /* rest from output */
417 from_source
= output
;
421 else { /* contiguous in window */
423 if (op
< len
) { /* some from window */
426 output
[_out
++] = window
[from++];
428 from = _out
- dist
; /* rest from output */
429 from_source
= output
;
433 output
[_out
++] = from_source
[from++];
434 output
[_out
++] = from_source
[from++];
435 output
[_out
++] = from_source
[from++];
439 output
[_out
++] = from_source
[from++];
441 output
[_out
++] = from_source
[from++];
446 from = _out
- dist
; /* copy direct from output */
447 do { /* minimum length is three */
448 output
[_out
++] = output
[from++];
449 output
[_out
++] = output
[from++];
450 output
[_out
++] = output
[from++];
454 output
[_out
++] = output
[from++];
456 output
[_out
++] = output
[from++];
461 else if ((op
& 64) === 0) { /* 2nd level distance code */
462 here
= dcode
[(here
& 0xffff)/*here.val*/ + (hold
& ((1 << op
) - 1))];
466 strm
.msg
= 'invalid distance code';
471 break; // need to emulate goto via "continue"
474 else if ((op
& 64) === 0) { /* 2nd level length code */
475 here
= lcode
[(here
& 0xffff)/*here.val*/ + (hold
& ((1 << op
) - 1))];
478 else if (op
& 32) { /* end-of-block */
479 //Tracevv((stderr, "inflate: end of block\n"));
484 strm
.msg
= 'invalid literal/length code';
489 break; // need to emulate goto via "continue"
491 } while (_in
< last
&& _out
< end
);
493 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
497 hold
&= (1 << bits
) - 1;
499 /* update state and return */
501 strm
.next_out
= _out
;
502 strm
.avail_in
= (_in
< last
? 5 + (last
- _in
) : 5 - (_in
- last
));
503 strm
.avail_out
= (_out
< end
? 257 + (end
- _out
) : 257 - (_out
- end
));
509 },{}],5:[function(require
,module
,exports
){
513 var utils
= require('../utils/common');
514 var adler32
= require('./adler32');
515 var crc32
= require('./crc32');
516 var inflate_fast
= require('./inffast');
517 var inflate_table
= require('./inftrees');
523 /* Public constants ==========================================================*/
524 /* ===========================================================================*/
527 /* Allowed flush values; see deflate() and inflate() below for details */
528 //var Z_NO_FLUSH = 0;
529 //var Z_PARTIAL_FLUSH = 1;
530 //var Z_SYNC_FLUSH = 2;
531 //var Z_FULL_FLUSH = 3;
537 /* Return codes for the compression/decompression functions. Negative values
538 * are errors, positive values are used for special but normal events.
541 var Z_STREAM_END
= 1;
544 var Z_STREAM_ERROR
= -2;
545 var Z_DATA_ERROR
= -3;
546 var Z_MEM_ERROR
= -4;
547 var Z_BUF_ERROR
= -5;
548 //var Z_VERSION_ERROR = -6;
550 /* The deflate compression method */
554 /* STATES ====================================================================*/
555 /* ===========================================================================*/
558 var HEAD
= 1; /* i: waiting for magic header */
559 var FLAGS
= 2; /* i: waiting for method and flags (gzip) */
560 var TIME
= 3; /* i: waiting for modification time (gzip) */
561 var OS
= 4; /* i: waiting for extra flags and operating system (gzip) */
562 var EXLEN
= 5; /* i: waiting for extra length (gzip) */
563 var EXTRA
= 6; /* i: waiting for extra bytes (gzip) */
564 var NAME
= 7; /* i: waiting for end of file name (gzip) */
565 var COMMENT
= 8; /* i: waiting for end of comment (gzip) */
566 var HCRC
= 9; /* i: waiting for header crc (gzip) */
567 var DICTID
= 10; /* i: waiting for dictionary check value */
568 var DICT
= 11; /* waiting for inflateSetDictionary() call */
569 var TYPE
= 12; /* i: waiting for type bits, including last-flag bit */
570 var TYPEDO
= 13; /* i: same, but skip check to exit inflate on new block */
571 var STORED
= 14; /* i: waiting for stored size (length and complement) */
572 var COPY_
= 15; /* i/o: same as COPY below, but only first time in */
573 var COPY = 16; /* i/o: waiting for input or output to copy stored block */
574 var TABLE = 17; /* i: waiting for dynamic block table lengths */
575 var LENLENS
= 18; /* i: waiting for code length code lengths */
576 var CODELENS
= 19; /* i: waiting for length/lit and distance code lengths */
577 var LEN_ = 20; /* i: same as LEN below, but only first time in */
578 var LEN
= 21; /* i: waiting for length/lit/eob code */
579 var LENEXT
= 22; /* i: waiting for length extra bits */
580 var DIST
= 23; /* i: waiting for distance code */
581 var DISTEXT
= 24; /* i: waiting for distance extra bits */
582 var MATCH
= 25; /* o: waiting for output space to copy string */
583 var LIT
= 26; /* o: waiting for output space to write literal */
584 var CHECK
= 27; /* i: waiting for 32-bit check value */
585 var LENGTH
= 28; /* i: waiting for 32-bit length (gzip) */
586 var DONE
= 29; /* finished check, done -- remain here until reset */
587 var BAD
= 30; /* got a data error -- remain here until reset */
588 var MEM
= 31; /* got an inflate() memory error -- remain here until reset */
589 var SYNC
= 32; /* looking for synchronization bytes to restart inflate() */
591 /* ===========================================================================*/
595 var ENOUGH_LENS
= 852;
596 var ENOUGH_DISTS
= 592;
597 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
600 /* 32K LZ77 window */
601 var DEF_WBITS
= MAX_WBITS
;
604 function ZSWAP32(q
) {
605 return (((q
>>> 24) & 0xff) +
606 ((q
>>> 8) & 0xff00) +
607 ((q
& 0xff00) << 8) +
612 function InflateState() {
613 this.mode
= 0; /* current inflate mode */
614 this.last
= false; /* true if processing last block */
615 this.wrap
= 0; /* bit 0 true for zlib, bit 1 true for gzip */
616 this.havedict
= false; /* true if dictionary provided */
617 this.flags
= 0; /* gzip header method and flags (0 if zlib) */
618 this.dmax
= 0; /* zlib header max distance (INFLATE_STRICT) */
619 this.check
= 0; /* protected copy of check value */
620 this.total
= 0; /* protected copy of output count */
622 this.head
= null; /* where to save gzip header information */
625 this.wbits
= 0; /* log base 2 of requested window size */
626 this.wsize
= 0; /* window size or zero if not using window */
627 this.whave
= 0; /* valid bytes in the window */
628 this.wnext
= 0; /* window write index */
629 this.window
= null; /* allocated sliding window, if needed */
631 /* bit accumulator */
632 this.hold
= 0; /* input bit accumulator */
633 this.bits
= 0; /* number of bits in "in" */
635 /* for string and stored block copying */
636 this.length
= 0; /* literal or length of data to copy */
637 this.offset
= 0; /* distance back to copy string from */
639 /* for table and code decoding */
640 this.extra
= 0; /* extra bits needed */
642 /* fixed and dynamic code tables */
643 this.lencode
= null; /* starting table for length/literal codes */
644 this.distcode = null; /* starting table for distance codes */
645 this.lenbits
= 0; /* index bits for lencode */
646 this.distbits
= 0; /* index bits for distcode */
648 /* dynamic table building */
649 this.ncode
= 0; /* number of code length code lengths */
650 this.nlen
= 0; /* number of length code lengths */
651 this.ndist
= 0; /* number of distance code lengths */
652 this.have
= 0; /* number of code lengths in lens[] */
653 this.next
= null; /* next available space in codes[] */
655 this.lens
= new utils
.Buf16(320); /* temporary storage for code lengths */
656 this.work
= new utils
.Buf16(288); /* work area for code table building */
659 because we don't have pointers in js, we use lencode and distcode directly
660 as buffers so we don't need codes
662 //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
663 this.lendyn
= null; /* dynamic table for length/literal codes (JS specific) */
664 this.distdyn = null; /* dynamic table for distance codes (JS specific) */
665 this.sane
= 0; /* if false, allow invalid distance too far */
666 this.back
= 0; /* bits back of last unprocessed length/lit */
667 this.was = 0; /* initial length of match */
670 function inflateResetKeep(strm
) {
673 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
675 strm
.total_in
= strm
.total_out
= state
.total
= 0;
676 strm
.msg
= ''; /*Z_NULL*/
677 if (state
.wrap
) { /* to support ill-conceived Java test suite */
678 strm
.adler
= state
.wrap
& 1;
684 state
.head
= null/*Z_NULL*/;
687 //state.lencode = state.distcode = state.next = state.codes;
688 state
.lencode
= state
.lendyn
= new utils
.Buf32(ENOUGH_LENS
);
689 state
.distcode
= state
.distdyn
= new utils
.Buf32(ENOUGH_DISTS
);
693 //Tracev((stderr, "inflate: reset\n"));
697 function inflateReset(strm
) {
700 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
705 return inflateResetKeep(strm
);
709 function inflateReset2(strm
, windowBits
) {
714 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
717 /* extract wrap request from windowBits parameter */
718 if (windowBits
< 0) {
720 windowBits
= -windowBits
;
723 wrap
= (windowBits
>> 4) + 1;
724 if (windowBits
< 48) {
729 /* set number of window bits, free window if different */
730 if (windowBits
&& (windowBits
< 8 || windowBits
> 15)) {
731 return Z_STREAM_ERROR
;
733 if (state
.window
!== null && state
.wbits
!== windowBits
) {
737 /* update state and reset the rest of it */
739 state
.wbits
= windowBits
;
740 return inflateReset(strm
);
743 function inflateInit2(strm
, windowBits
) {
747 if (!strm
) { return Z_STREAM_ERROR
; }
748 //strm.msg = Z_NULL; /* in case we return an error */
750 state
= new InflateState();
752 //if (state === Z_NULL) return Z_MEM_ERROR;
753 //Tracev((stderr, "inflate: allocated\n"));
755 state
.window
= null/*Z_NULL*/;
756 ret
= inflateReset2(strm
, windowBits
);
758 strm
.state
= null/*Z_NULL*/;
763 function inflateInit(strm
) {
764 return inflateInit2(strm
, DEF_WBITS
);
769 Return state with length and distance decoding tables and index sizes set to
770 fixed code decoding. Normally this returns fixed tables from inffixed.h.
771 If BUILDFIXED is defined, then instead this routine builds the tables the
772 first time it's called, and returns those tables the first time and
773 thereafter. This reduces the size of the code by about 2K bytes, in
774 exchange for a little execution time. However, BUILDFIXED should not be
775 used for threaded applications, since the rewriting of the tables and virgin
776 may not be thread-safe.
780 var lenfix
, distfix
; // We have no pointers in JS, so keep tables separate
782 function fixedtables(state
) {
783 /* build fixed huffman tables if first call (may not be thread safe) */
787 lenfix
= new utils
.Buf32(512);
788 distfix
= new utils
.Buf32(32);
790 /* literal/length table */
792 while (sym < 144) { state.lens[sym++] = 8; }
793 while (sym < 256) { state.lens[sym++] = 9; }
794 while (sym < 280) { state.lens[sym++] = 7; }
795 while (sym < 288) { state.lens[sym++] = 8; }
797 inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, {bits: 9});
801 while (sym
< 32) { state
.lens
[sym
++] = 5; }
803 inflate_table(DISTS
, state
.lens
, 0, 32, distfix
, 0, state
.work
, {bits
: 5});
805 /* do this just once */
809 state
.lencode
= lenfix
;
811 state
.distcode
= distfix
;
817 Update the window with the last wsize (normally 32K) bytes written before
818 returning. If window does not exist yet, create it. This is only called
819 when a window is already in use, or when output has been written during this
820 inflate call, but the end of the deflate stream has not been reached yet.
821 It is also called to create a window for dictionary data when a dictionary
824 Providing output buffers larger than 32K to inflate() should provide a speed
825 advantage, since only the last 32K of output is copied to the sliding window
826 upon return from inflate(), and since all distances after the first 32K of
827 output will fall in the output data, making match copies simpler and faster.
828 The advantage may be dependent on the size of the processor's data caches.
830 function updatewindow(strm
, src
, end
, copy
) {
832 var state
= strm
.state
;
834 /* if it hasn't been done already, allocate space for the window */
835 if (state
.window
=== null) {
836 state
.wsize
= 1 << state
.wbits
;
840 state
.window
= new utils
.Buf8(state
.wsize
);
843 /* copy state->wsize or less output bytes into the circular window */
844 if (copy
>= state
.wsize
) {
845 utils
.arraySet(state
.window
,src
, end
- state
.wsize
, state
.wsize
, 0);
847 state
.whave
= state
.wsize
;
850 dist
= state
.wsize
- state
.wnext
;
854 //zmemcpy(state->window + state->wnext, end - copy, dist);
855 utils
.arraySet(state
.window
,src
, end
- copy
, dist
, state
.wnext
);
858 //zmemcpy(state->window, end - copy, copy);
859 utils
.arraySet(state
.window
,src
, end
- copy
, copy
, 0);
861 state
.whave
= state
.wsize
;
865 if (state
.wnext
=== state
.wsize
) { state
.wnext
= 0; }
866 if (state
.whave
< state
.wsize
) { state
.whave
+= dist
; }
872 function inflate(strm
, flush
) {
874 var input
, output
; // input/output buffers
875 var next
; /* next input INDEX */
876 var put
; /* next output INDEX */
877 var have
, left
; /* available input and output */
878 var hold
; /* bit buffer */
879 var bits
; /* bits in bit buffer */
880 var _in
, _out
; /* save starting available input and output */
881 var copy
; /* number of stored or match bytes to copy */
882 var from; /* where to copy match bytes from */
884 var here
= 0; /* current decoding table entry */
885 var here_bits
, here_op
, here_val
; // paked "here" denormalized (JS specific)
886 //var last; /* parent table entry */
887 var last_bits
, last_op
, last_val
; // paked "last" denormalized (JS specific)
888 var len
; /* length to copy for repeats, bits to drop */
889 var ret
; /* return code */
890 var hbuf
= new utils
.Buf8(4); /* buffer for gzip header crc calculation */
893 var n
; // temporary var for NEED_BITS
895 var order
= /* permutation of code lengths */
896 [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
899 if (!strm
|| !strm
.state
|| !strm
.output
||
900 (!strm
.input
&& strm
.avail_in
!== 0)) {
901 return Z_STREAM_ERROR
;
905 if (state
.mode
=== TYPE
) { state
.mode
= TYPEDO
; } /* skip check */
910 output
= strm
.output
;
911 left
= strm
.avail_out
;
914 have
= strm
.avail_in
;
923 inf_leave
: // goto emulation
925 switch (state
.mode
) {
927 if (state
.wrap
=== 0) {
933 if (have
=== 0) { break inf_leave
; }
935 hold
+= input
[next
++] << bits
;
939 if ((state
.wrap
& 2) && hold
=== 0x8b1f) { /* gzip header */
940 state
.check
= 0/*crc32(0L, Z_NULL, 0)*/;
941 //=== CRC2(state.check, hold);
942 hbuf
[0] = hold
& 0xff;
943 hbuf
[1] = (hold
>>> 8) & 0xff;
944 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
954 state
.flags
= 0; /* expect zlib header */
956 state
.head
.done
= false;
958 if (!(state
.wrap
& 1) || /* check if zlib header allowed */
959 (((hold
& 0xff)/*BITS(8)*/ << 8) + (hold
>> 8)) % 31) {
960 strm
.msg
= 'incorrect header check';
964 if ((hold
& 0x0f)/*BITS(4)*/ !== Z_DEFLATED
) {
965 strm
.msg
= 'unknown compression method';
969 //--- DROPBITS(4) ---//
973 len
= (hold
& 0x0f)/*BITS(4)*/ + 8;
974 if (state
.wbits
=== 0) {
977 else if (len
> state
.wbits
) {
978 strm
.msg
= 'invalid window size';
982 state
.dmax
= 1 << len
;
983 //Tracev((stderr, "inflate: zlib header ok\n"));
984 strm
.adler
= state
.check
= 1/*adler32(0L, Z_NULL, 0)*/;
985 state
.mode
= hold
& 0x200 ? DICTID
: TYPE
;
992 //=== NEEDBITS(16); */
994 if (have
=== 0) { break inf_leave
; }
996 hold
+= input
[next
++] << bits
;
1001 if ((state
.flags
& 0xff) !== Z_DEFLATED
) {
1002 strm
.msg
= 'unknown compression method';
1006 if (state
.flags
& 0xe000) {
1007 strm
.msg
= 'unknown header flags set';
1012 state
.head
.text
= ((hold
>> 8) & 1);
1014 if (state
.flags
& 0x0200) {
1015 //=== CRC2(state.check, hold);
1016 hbuf
[0] = hold
& 0xff;
1017 hbuf
[1] = (hold
>>> 8) & 0xff;
1018 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1028 //=== NEEDBITS(32); */
1030 if (have
=== 0) { break inf_leave
; }
1032 hold
+= input
[next
++] << bits
;
1037 state
.head
.time
= hold
;
1039 if (state
.flags
& 0x0200) {
1040 //=== CRC4(state.check, hold)
1041 hbuf
[0] = hold
& 0xff;
1042 hbuf
[1] = (hold
>>> 8) & 0xff;
1043 hbuf
[2] = (hold
>>> 16) & 0xff;
1044 hbuf
[3] = (hold
>>> 24) & 0xff;
1045 state
.check
= crc32(state
.check
, hbuf
, 4, 0);
1055 //=== NEEDBITS(16); */
1057 if (have
=== 0) { break inf_leave
; }
1059 hold
+= input
[next
++] << bits
;
1064 state
.head
.xflags
= (hold
& 0xff);
1065 state
.head
.os
= (hold
>> 8);
1067 if (state
.flags
& 0x0200) {
1068 //=== CRC2(state.check, hold);
1069 hbuf
[0] = hold
& 0xff;
1070 hbuf
[1] = (hold
>>> 8) & 0xff;
1071 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1081 if (state
.flags
& 0x0400) {
1082 //=== NEEDBITS(16); */
1084 if (have
=== 0) { break inf_leave
; }
1086 hold
+= input
[next
++] << bits
;
1090 state
.length
= hold
;
1092 state
.head
.extra_len
= hold
;
1094 if (state
.flags
& 0x0200) {
1095 //=== CRC2(state.check, hold);
1096 hbuf
[0] = hold
& 0xff;
1097 hbuf
[1] = (hold
>>> 8) & 0xff;
1098 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1106 else if (state
.head
) {
1107 state
.head
.extra
= null/*Z_NULL*/;
1112 if (state
.flags
& 0x0400) {
1113 copy
= state
.length
;
1114 if (copy
> have
) { copy
= have
; }
1117 len
= state
.head
.extra_len
- state
.length
;
1118 if (!state
.head
.extra
) {
1119 // Use untyped array for more conveniend processing later
1120 state
.head
.extra
= new Array(state
.head
.extra_len
);
1126 // extra field is limited to 65536 bytes
1127 // - no need for additional size check
1129 /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
1132 //zmemcpy(state.head.extra + len, next,
1133 // len + copy > state.head.extra_max ?
1134 // state.head.extra_max - len : copy);
1136 if (state
.flags
& 0x0200) {
1137 state
.check
= crc32(state
.check
, input
, copy
, next
);
1141 state
.length
-= copy
;
1143 if (state
.length
) { break inf_leave
; }
1149 if (state
.flags
& 0x0800) {
1150 if (have
=== 0) { break inf_leave
; }
1153 // TODO: 2 or 1 bytes?
1154 len
= input
[next
+ copy
++];
1155 /* use constant limit because in js we should not preallocate memory */
1156 if (state
.head
&& len
&&
1157 (state
.length
< 65536 /*state.head.name_max*/)) {
1158 state
.head
.name
+= String
.fromCharCode(len
);
1160 } while (len
&& copy
< have
);
1162 if (state
.flags
& 0x0200) {
1163 state
.check
= crc32(state
.check
, input
, copy
, next
);
1167 if (len
) { break inf_leave
; }
1169 else if (state
.head
) {
1170 state
.head
.name
= null;
1173 state
.mode
= COMMENT
;
1176 if (state
.flags
& 0x1000) {
1177 if (have
=== 0) { break inf_leave
; }
1180 len
= input
[next
+ copy
++];
1181 /* use constant limit because in js we should not preallocate memory */
1182 if (state
.head
&& len
&&
1183 (state
.length
< 65536 /*state.head.comm_max*/)) {
1184 state
.head
.comment
+= String
.fromCharCode(len
);
1186 } while (len
&& copy
< have
);
1187 if (state
.flags
& 0x0200) {
1188 state
.check
= crc32(state
.check
, input
, copy
, next
);
1192 if (len
) { break inf_leave
; }
1194 else if (state
.head
) {
1195 state
.head
.comment
= null;
1200 if (state
.flags
& 0x0200) {
1201 //=== NEEDBITS(16); */
1203 if (have
=== 0) { break inf_leave
; }
1205 hold
+= input
[next
++] << bits
;
1209 if (hold
!== (state
.check
& 0xffff)) {
1210 strm
.msg
= 'header crc mismatch';
1220 state
.head
.hcrc
= ((state
.flags
>> 9) & 1);
1221 state
.head
.done
= true;
1223 strm
.adler
= state
.check
= 0 /*crc32(0L, Z_NULL, 0)*/;
1227 //=== NEEDBITS(32); */
1229 if (have
=== 0) { break inf_leave
; }
1231 hold
+= input
[next
++] << bits
;
1235 strm
.adler
= state
.check
= ZSWAP32(hold
);
1243 if (state
.havedict
=== 0) {
1245 strm
.next_out
= put
;
1246 strm
.avail_out
= left
;
1247 strm
.next_in
= next
;
1248 strm
.avail_in
= have
;
1254 strm
.adler
= state
.check
= 1/*adler32(0L, Z_NULL, 0)*/;
1258 if (flush
=== Z_BLOCK
|| flush
=== Z_TREES
) { break inf_leave
; }
1262 //--- BYTEBITS() ---//
1269 //=== NEEDBITS(3); */
1271 if (have
=== 0) { break inf_leave
; }
1273 hold
+= input
[next
++] << bits
;
1277 state
.last
= (hold
& 0x01)/*BITS(1)*/;
1278 //--- DROPBITS(1) ---//
1283 switch ((hold
& 0x03)/*BITS(2)*/) {
1284 case 0: /* stored block */
1285 //Tracev((stderr, "inflate: stored block%s\n",
1286 // state.last ? " (last)" : ""));
1287 state
.mode
= STORED
;
1289 case 1: /* fixed block */
1291 //Tracev((stderr, "inflate: fixed codes block%s\n",
1292 // state.last ? " (last)" : ""));
1293 state
.mode
= LEN_
; /* decode codes */
1294 if (flush
=== Z_TREES
) {
1295 //--- DROPBITS(2) ---//
1302 case 2: /* dynamic block */
1303 //Tracev((stderr, "inflate: dynamic codes block%s\n",
1304 // state.last ? " (last)" : ""));
1308 strm
.msg
= 'invalid block type';
1311 //--- DROPBITS(2) ---//
1317 //--- BYTEBITS() ---// /* go to byte boundary */
1321 //=== NEEDBITS(32); */
1323 if (have
=== 0) { break inf_leave
; }
1325 hold
+= input
[next
++] << bits
;
1329 if ((hold
& 0xffff) !== ((hold
>>> 16) ^ 0xffff)) {
1330 strm
.msg
= 'invalid stored block lengths';
1334 state
.length
= hold
& 0xffff;
1335 //Tracev((stderr, "inflate: stored length %u\n",
1342 if (flush
=== Z_TREES
) { break inf_leave
; }
1348 copy
= state
.length
;
1350 if (copy
> have
) { copy
= have
; }
1351 if (copy
> left
) { copy
= left
; }
1352 if (copy
=== 0) { break inf_leave
; }
1353 //--- zmemcpy(put, next, copy); ---
1354 utils
.arraySet(output
, input
, next
, copy
, put
);
1360 state
.length
-= copy
;
1363 //Tracev((stderr, "inflate: stored end\n"));
1367 //=== NEEDBITS(14); */
1369 if (have
=== 0) { break inf_leave
; }
1371 hold
+= input
[next
++] << bits
;
1375 state
.nlen
= (hold
& 0x1f)/*BITS(5)*/ + 257;
1376 //--- DROPBITS(5) ---//
1380 state
.ndist
= (hold
& 0x1f)/*BITS(5)*/ + 1;
1381 //--- DROPBITS(5) ---//
1385 state
.ncode
= (hold
& 0x0f)/*BITS(4)*/ + 4;
1386 //--- DROPBITS(4) ---//
1390 //#ifndef PKZIP_BUG_WORKAROUND
1391 if (state
.nlen
> 286 || state
.ndist
> 30) {
1392 strm
.msg
= 'too many length or distance symbols';
1397 //Tracev((stderr, "inflate: table sizes ok\n"));
1399 state
.mode
= LENLENS
;
1402 while (state
.have
< state
.ncode
) {
1405 if (have
=== 0) { break inf_leave
; }
1407 hold
+= input
[next
++] << bits
;
1411 state
.lens
[order
[state
.have
++]] = (hold
& 0x07);//BITS(3);
1412 //--- DROPBITS(3) ---//
1417 while (state
.have
< 19) {
1418 state
.lens
[order
[state
.have
++]] = 0;
1420 // We have separate tables & no pointers. 2 commented lines below not needed.
1421 //state.next = state.codes;
1422 //state.lencode = state.next;
1423 // Switch to use dynamic table
1424 state
.lencode
= state
.lendyn
;
1427 opts
= {bits
: state
.lenbits
};
1428 ret
= inflate_table(CODES
, state
.lens
, 0, 19, state
.lencode
, 0, state
.work
, opts
);
1429 state
.lenbits
= opts
.bits
;
1432 strm
.msg
= 'invalid code lengths set';
1436 //Tracev((stderr, "inflate: code lengths ok\n"));
1438 state
.mode
= CODELENS
;
1441 while (state
.have
< state
.nlen
+ state
.ndist
) {
1443 here
= state
.lencode
[hold
& ((1 << state
.lenbits
) - 1)];/*BITS(state.lenbits)*/
1444 here_bits
= here
>>> 24;
1445 here_op
= (here
>>> 16) & 0xff;
1446 here_val
= here
& 0xffff;
1448 if ((here_bits
) <= bits
) { break; }
1449 //--- PULLBYTE() ---//
1450 if (have
=== 0) { break inf_leave
; }
1452 hold
+= input
[next
++] << bits
;
1456 if (here_val
< 16) {
1457 //--- DROPBITS(here.bits) ---//
1458 hold
>>>= here_bits
;
1461 state
.lens
[state
.have
++] = here_val
;
1464 if (here_val
=== 16) {
1465 //=== NEEDBITS(here.bits + 2);
1468 if (have
=== 0) { break inf_leave
; }
1470 hold
+= input
[next
++] << bits
;
1474 //--- DROPBITS(here.bits) ---//
1475 hold
>>>= here_bits
;
1478 if (state
.have
=== 0) {
1479 strm
.msg
= 'invalid bit length repeat';
1483 len
= state
.lens
[state
.have
- 1];
1484 copy
= 3 + (hold
& 0x03);//BITS(2);
1485 //--- DROPBITS(2) ---//
1490 else if (here_val
=== 17) {
1491 //=== NEEDBITS(here.bits + 3);
1494 if (have
=== 0) { break inf_leave
; }
1496 hold
+= input
[next
++] << bits
;
1500 //--- DROPBITS(here.bits) ---//
1501 hold
>>>= here_bits
;
1505 copy
= 3 + (hold
& 0x07);//BITS(3);
1506 //--- DROPBITS(3) ---//
1512 //=== NEEDBITS(here.bits + 7);
1515 if (have
=== 0) { break inf_leave
; }
1517 hold
+= input
[next
++] << bits
;
1521 //--- DROPBITS(here.bits) ---//
1522 hold
>>>= here_bits
;
1526 copy
= 11 + (hold
& 0x7f);//BITS(7);
1527 //--- DROPBITS(7) ---//
1532 if (state
.have
+ copy
> state
.nlen
+ state
.ndist
) {
1533 strm
.msg
= 'invalid bit length repeat';
1538 state
.lens
[state
.have
++] = len
;
1543 /* handle error breaks in while */
1544 if (state
.mode
=== BAD
) { break; }
1546 /* check for end-of-block code (better have one) */
1547 if (state
.lens
[256] === 0) {
1548 strm
.msg
= 'invalid code -- missing end-of-block';
1553 /* build code tables -- note: do not change the lenbits or distbits
1554 values here (9 and 6) without reading the comments in inftrees.h
1555 concerning the ENOUGH constants, which depend on those values */
1558 opts
= {bits
: state
.lenbits
};
1559 ret
= inflate_table(LENS
, state
.lens
, 0, state
.nlen
, state
.lencode
, 0, state
.work
, opts
);
1560 // We have separate tables & no pointers. 2 commented lines below not needed.
1561 // state.next_index = opts.table_index;
1562 state
.lenbits
= opts
.bits
;
1563 // state.lencode = state.next;
1566 strm
.msg
= 'invalid literal/lengths set';
1572 //state.distcode.copy(state.codes);
1573 // Switch to use dynamic table
1574 state
.distcode
= state
.distdyn
;
1575 opts
= {bits
: state
.distbits
};
1576 ret
= inflate_table(DISTS
, state
.lens
, state
.nlen
, state
.ndist
, state
.distcode
, 0, state
.work
, opts
);
1577 // We have separate tables & no pointers. 2 commented lines below not needed.
1578 // state.next_index = opts.table_index;
1579 state
.distbits
= opts
.bits
;
1580 // state.distcode = state.next;
1583 strm
.msg
= 'invalid distances set';
1587 //Tracev((stderr, 'inflate: codes ok\n'));
1589 if (flush
=== Z_TREES
) { break inf_leave
; }
1595 if (have
>= 6 && left
>= 258) {
1597 strm
.next_out
= put
;
1598 strm
.avail_out
= left
;
1599 strm
.next_in
= next
;
1600 strm
.avail_in
= have
;
1604 inflate_fast(strm
, _out
);
1606 put
= strm
.next_out
;
1607 output
= strm
.output
;
1608 left
= strm
.avail_out
;
1609 next
= strm
.next_in
;
1611 have
= strm
.avail_in
;
1616 if (state
.mode
=== TYPE
) {
1623 here
= state
.lencode
[hold
& ((1 << state
.lenbits
) -1)]; /*BITS(state.lenbits)*/
1624 here_bits
= here
>>> 24;
1625 here_op
= (here
>>> 16) & 0xff;
1626 here_val
= here
& 0xffff;
1628 if (here_bits
<= bits
) { break; }
1629 //--- PULLBYTE() ---//
1630 if (have
=== 0) { break inf_leave
; }
1632 hold
+= input
[next
++] << bits
;
1636 if (here_op
&& (here_op
& 0xf0) === 0) {
1637 last_bits
= here_bits
;
1639 last_val
= here_val
;
1641 here
= state
.lencode
[last_val
+
1642 ((hold
& ((1 << (last_bits
+ last_op
)) -1))/*BITS(last.bits + last.op)*/ >> last_bits
)];
1643 here_bits
= here
>>> 24;
1644 here_op
= (here
>>> 16) & 0xff;
1645 here_val
= here
& 0xffff;
1647 if ((last_bits
+ here_bits
) <= bits
) { break; }
1648 //--- PULLBYTE() ---//
1649 if (have
=== 0) { break inf_leave
; }
1651 hold
+= input
[next
++] << bits
;
1655 //--- DROPBITS(last.bits) ---//
1656 hold
>>>= last_bits
;
1659 state
.back
+= last_bits
;
1661 //--- DROPBITS(here.bits) ---//
1662 hold
>>>= here_bits
;
1665 state
.back
+= here_bits
;
1666 state
.length
= here_val
;
1667 if (here_op
=== 0) {
1668 //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1669 // "inflate: literal '%c'\n" :
1670 // "inflate: literal 0x%02x\n", here.val));
1675 //Tracevv((stderr, "inflate: end of block\n"));
1681 strm
.msg
= 'invalid literal/length code';
1685 state
.extra
= here_op
& 15;
1686 state
.mode
= LENEXT
;
1690 //=== NEEDBITS(state.extra);
1693 if (have
=== 0) { break inf_leave
; }
1695 hold
+= input
[next
++] << bits
;
1699 state
.length
+= hold
& ((1 << state
.extra
) -1)/*BITS(state.extra)*/;
1700 //--- DROPBITS(state.extra) ---//
1701 hold
>>>= state
.extra
;
1702 bits
-= state
.extra
;
1704 state
.back
+= state
.extra
;
1706 //Tracevv((stderr, "inflate: length %u\n", state.length));
1707 state
.was
= state
.length
;
1712 here
= state
.distcode
[hold
& ((1 << state
.distbits
) -1)];/*BITS(state.distbits)*/
1713 here_bits
= here
>>> 24;
1714 here_op
= (here
>>> 16) & 0xff;
1715 here_val
= here
& 0xffff;
1717 if ((here_bits
) <= bits
) { break; }
1718 //--- PULLBYTE() ---//
1719 if (have
=== 0) { break inf_leave
; }
1721 hold
+= input
[next
++] << bits
;
1725 if ((here_op
& 0xf0) === 0) {
1726 last_bits
= here_bits
;
1728 last_val
= here_val
;
1730 here
= state
.distcode
[last_val
+
1731 ((hold
& ((1 << (last_bits
+ last_op
)) -1))/*BITS(last.bits + last.op)*/ >> last_bits
)];
1732 here_bits
= here
>>> 24;
1733 here_op
= (here
>>> 16) & 0xff;
1734 here_val
= here
& 0xffff;
1736 if ((last_bits
+ here_bits
) <= bits
) { break; }
1737 //--- PULLBYTE() ---//
1738 if (have
=== 0) { break inf_leave
; }
1740 hold
+= input
[next
++] << bits
;
1744 //--- DROPBITS(last.bits) ---//
1745 hold
>>>= last_bits
;
1748 state
.back
+= last_bits
;
1750 //--- DROPBITS(here.bits) ---//
1751 hold
>>>= here_bits
;
1754 state
.back
+= here_bits
;
1756 strm
.msg
= 'invalid distance code';
1760 state
.offset
= here_val
;
1761 state
.extra
= (here_op
) & 15;
1762 state
.mode
= DISTEXT
;
1766 //=== NEEDBITS(state.extra);
1769 if (have
=== 0) { break inf_leave
; }
1771 hold
+= input
[next
++] << bits
;
1775 state
.offset
+= hold
& ((1 << state
.extra
) -1)/*BITS(state.extra)*/;
1776 //--- DROPBITS(state.extra) ---//
1777 hold
>>>= state
.extra
;
1778 bits
-= state
.extra
;
1780 state
.back
+= state
.extra
;
1782 //#ifdef INFLATE_STRICT
1783 if (state
.offset
> state
.dmax
) {
1784 strm
.msg
= 'invalid distance too far back';
1789 //Tracevv((stderr, "inflate: distance %u\n", state.offset));
1793 if (left
=== 0) { break inf_leave
; }
1795 if (state
.offset
> copy
) { /* copy from window */
1796 copy
= state
.offset
- copy
;
1797 if (copy
> state
.whave
) {
1799 strm
.msg
= 'invalid distance too far back';
1803 // (!) This block is disabled in zlib defailts,
1804 // don't enable it for binary compatibility
1805 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1806 // Trace((stderr, "inflate.c too far\n"));
1807 // copy -= state.whave;
1808 // if (copy > state.length) { copy = state.length; }
1809 // if (copy > left) { copy = left; }
1811 // state.length -= copy;
1813 // output[put++] = 0;
1814 // } while (--copy);
1815 // if (state.length === 0) { state.mode = LEN; }
1819 if (copy
> state
.wnext
) {
1820 copy
-= state
.wnext
;
1821 from = state
.wsize
- copy
;
1824 from = state
.wnext
- copy
;
1826 if (copy
> state
.length
) { copy
= state
.length
; }
1827 from_source
= state
.window
;
1829 else { /* copy from output */
1830 from_source
= output
;
1831 from = put
- state
.offset
;
1832 copy
= state
.length
;
1834 if (copy
> left
) { copy
= left
; }
1836 state
.length
-= copy
;
1838 output
[put
++] = from_source
[from++];
1840 if (state
.length
=== 0) { state
.mode
= LEN
; }
1843 if (left
=== 0) { break inf_leave
; }
1844 output
[put
++] = state
.length
;
1852 if (have
=== 0) { break inf_leave
; }
1854 // Use '|' insdead of '+' to make sure that result is signed
1855 hold
|= input
[next
++] << bits
;
1860 strm
.total_out
+= _out
;
1861 state
.total
+= _out
;
1863 strm
.adler
= state
.check
=
1864 /*UPDATE(state.check, put - _out, _out);*/
1865 (state
.flags
? crc32(state
.check
, output
, _out
, put
- _out
) : adler32(state
.check
, output
, _out
, put
- _out
));
1869 // NB: crc32 stored as signed 32-bit int, ZSWAP32 returns signed too
1870 if ((state
.flags
? hold
: ZSWAP32(hold
)) !== state
.check
) {
1871 strm
.msg
= 'incorrect data check';
1879 //Tracev((stderr, "inflate: check matches trailer\n"));
1881 state
.mode
= LENGTH
;
1884 if (state
.wrap
&& state
.flags
) {
1887 if (have
=== 0) { break inf_leave
; }
1889 hold
+= input
[next
++] << bits
;
1893 if (hold
!== (state
.total
& 0xffffffff)) {
1894 strm
.msg
= 'incorrect length check';
1902 //Tracev((stderr, "inflate: length matches trailer\n"));
1917 return Z_STREAM_ERROR
;
1921 // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
1924 Return from inflate(), updating the total counts and the check value.
1925 If there was no progress during the inflate() call, return a buffer
1926 error. Call updatewindow() to create and/or update the window state.
1927 Note: a memory error from inflate() is non-recoverable.
1931 strm
.next_out
= put
;
1932 strm
.avail_out
= left
;
1933 strm
.next_in
= next
;
1934 strm
.avail_in
= have
;
1939 if (state
.wsize
|| (_out
!== strm
.avail_out
&& state
.mode
< BAD
&&
1940 (state
.mode
< CHECK
|| flush
!== Z_FINISH
))) {
1941 if (updatewindow(strm
, strm
.output
, strm
.next_out
, _out
- strm
.avail_out
)) {
1946 _in
-= strm
.avail_in
;
1947 _out
-= strm
.avail_out
;
1948 strm
.total_in
+= _in
;
1949 strm
.total_out
+= _out
;
1950 state
.total
+= _out
;
1951 if (state
.wrap
&& _out
) {
1952 strm
.adler
= state
.check
= /*UPDATE(state.check, strm.next_out - _out, _out);*/
1953 (state
.flags
? crc32(state
.check
, output
, _out
, strm
.next_out
- _out
) : adler32(state
.check
, output
, _out
, strm
.next_out
- _out
));
1955 strm
.data_type
= state
.bits
+ (state
.last
? 64 : 0) +
1956 (state
.mode
=== TYPE
? 128 : 0) +
1957 (state
.mode
=== LEN_
|| state
.mode
=== COPY_
? 256 : 0);
1958 if (((_in
=== 0 && _out
=== 0) || flush
=== Z_FINISH
) && ret
=== Z_OK
) {
1964 function inflateEnd(strm
) {
1966 if (!strm
|| !strm
.state
/*|| strm->zfree == (free_func)0*/) {
1967 return Z_STREAM_ERROR
;
1970 var state
= strm
.state
;
1972 state
.window
= null;
1978 function inflateGetHeader(strm
, head
) {
1982 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
1984 if ((state
.wrap
& 2) === 0) { return Z_STREAM_ERROR
; }
1986 /* save header structure */
1993 exports
.inflateReset
= inflateReset
;
1994 exports
.inflateReset2
= inflateReset2
;
1995 exports
.inflateResetKeep
= inflateResetKeep
;
1996 exports
.inflateInit
= inflateInit
;
1997 exports
.inflateInit2
= inflateInit2
;
1998 exports
.inflate
= inflate
;
1999 exports
.inflateEnd
= inflateEnd
;
2000 exports
.inflateGetHeader
= inflateGetHeader
;
2001 exports
.inflateInfo
= 'pako inflate (from Nodeca project)';
2004 exports.inflateCopy = inflateCopy;
2005 exports.inflateGetDictionary = inflateGetDictionary;
2006 exports.inflateMark = inflateMark;
2007 exports.inflatePrime = inflatePrime;
2008 exports.inflateSetDictionary = inflateSetDictionary;
2009 exports.inflateSync = inflateSync;
2010 exports.inflateSyncPoint = inflateSyncPoint;
2011 exports.inflateUndermine = inflateUndermine;
2014 },{"../utils/common":1,"./adler32":2,"./crc32":3,"./inffast":4,"./inftrees":6}],6:[function(require
,module
,exports
){
2018 var utils
= require('../utils/common');
2021 var ENOUGH_LENS
= 852;
2022 var ENOUGH_DISTS
= 592;
2023 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
2029 var lbase
= [ /* Length codes 257..285 base */
2030 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2031 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
2034 var lext
= [ /* Length codes 257..285 extra */
2035 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
2036 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
2039 var dbase
= [ /* Distance codes 0..29 base */
2040 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2041 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2042 8193, 12289, 16385, 24577, 0, 0
2045 var dext
= [ /* Distance codes 0..29 extra */
2046 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
2047 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
2048 28, 28, 29, 29, 64, 64
2051 module
.exports
= function inflate_table(type
, lens
, lens_index
, codes
, table
, table_index
, work
, opts
)
2053 var bits
= opts
.bits
;
2054 //here = opts.here; /* table entry for duplication */
2056 var len
= 0; /* a code's length in bits */
2057 var sym
= 0; /* index of code symbols */
2058 var min
= 0, max
= 0; /* minimum and maximum code lengths */
2059 var root
= 0; /* number of index bits for root table */
2060 var curr
= 0; /* number of index bits for current table */
2061 var drop
= 0; /* code bits to drop for sub-table */
2062 var left
= 0; /* number of prefix codes available */
2063 var used
= 0; /* code entries in table used */
2064 var huff
= 0; /* Huffman code */
2065 var incr
; /* for incrementing code, index */
2066 var fill
; /* index for replicating entries */
2067 var low
; /* low bits for current root entry */
2068 var mask
; /* mask for low root bits */
2069 var next
; /* next available space in table */
2070 var base
= null; /* base value table to use */
2072 // var shoextra; /* extra bits table to use */
2073 var end
; /* use base and extra for symbol > end */
2074 var count
= new utils
.Buf16(MAXBITS
+1); //[MAXBITS+1]; /* number of codes of each length */
2075 var offs
= new utils
.Buf16(MAXBITS
+1); //[MAXBITS+1]; /* offsets in table for each length */
2077 var extra_index
= 0;
2079 var here_bits
, here_op
, here_val
;
2082 Process a set of code lengths to create a canonical Huffman code. The
2083 code lengths are lens[0..codes-1]. Each length corresponds to the
2084 symbols 0..codes-1. The Huffman code is generated by first sorting the
2085 symbols by length from short to long, and retaining the symbol order
2086 for codes with equal lengths. Then the code starts with all zero bits
2087 for the first code of the shortest length, and the codes are integer
2088 increments for the same length, and zeros are appended as the length
2089 increases. For the deflate format, these bits are stored backwards
2090 from their more natural integer increment ordering, and so when the
2091 decoding tables are built in the large loop below, the integer codes
2092 are incremented backwards.
2094 This routine assumes, but does not check, that all of the entries in
2095 lens[] are in the range 0..MAXBITS. The caller must assure this.
2096 1..MAXBITS is interpreted as that code length. zero means that that
2097 symbol does not occur in this code.
2099 The codes are sorted by computing a count of codes for each length,
2100 creating from that a table of starting indices for each length in the
2101 sorted table, and then entering the symbols in order in the sorted
2102 table. The sorted table is work[], with that space being provided by
2105 The length counts are used for other purposes as well, i.e. finding
2106 the minimum and maximum length codes, determining if there are any
2107 codes at all, checking for a valid set of lengths, and looking ahead
2108 at length counts to determine sub-table sizes when building the
2112 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
2113 for (len
= 0; len
<= MAXBITS
; len
++) {
2116 for (sym
= 0; sym
< codes
; sym
++) {
2117 count
[lens
[lens_index
+ sym
]]++;
2120 /* bound code lengths, force root to be within code lengths */
2122 for (max
= MAXBITS
; max
>= 1; max
--) {
2123 if (count
[max
] !== 0) { break; }
2128 if (max
=== 0) { /* no symbols to code at all */
2129 //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
2130 //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
2131 //table.val[opts.table_index++] = 0; //here.val = (var short)0;
2132 table
[table_index
++] = (1 << 24) | (64 << 16) | 0;
2135 //table.op[opts.table_index] = 64;
2136 //table.bits[opts.table_index] = 1;
2137 //table.val[opts.table_index++] = 0;
2138 table
[table_index
++] = (1 << 24) | (64 << 16) | 0;
2141 return 0; /* no symbols, but wait for decoding to report error */
2143 for (min
= 1; min
< max
; min
++) {
2144 if (count
[min
] !== 0) { break; }
2150 /* check for an over-subscribed or incomplete set of lengths */
2152 for (len
= 1; len
<= MAXBITS
; len
++) {
2157 } /* over-subscribed */
2159 if (left
> 0 && (type
=== CODES
|| max
!== 1)) {
2160 return -1; /* incomplete set */
2163 /* generate offsets into symbol table for each length for sorting */
2165 for (len
= 1; len
< MAXBITS
; len
++) {
2166 offs
[len
+ 1] = offs
[len
] + count
[len
];
2169 /* sort symbols by length, by symbol order within each length */
2170 for (sym
= 0; sym
< codes
; sym
++) {
2171 if (lens
[lens_index
+ sym
] !== 0) {
2172 work
[offs
[lens
[lens_index
+ sym
]]++] = sym
;
2177 Create and fill in decoding tables. In this loop, the table being
2178 filled is at next and has curr index bits. The code being used is huff
2179 with length len. That code is converted to an index by dropping drop
2180 bits off of the bottom. For codes where len is less than drop + curr,
2181 those top drop + curr - len bits are incremented through all values to
2182 fill the table with replicated entries.
2184 root is the number of index bits for the root table. When len exceeds
2185 root, sub-tables are created pointed to by the root entry with an index
2186 of the low root bits of huff. This is saved in low to check for when a
2187 new sub-table should be started. drop is zero when the root table is
2188 being filled, and drop is root when sub-tables are being filled.
2190 When a new sub-table is needed, it is necessary to look ahead in the
2191 code lengths to determine what size sub-table is needed. The length
2192 counts are used for this, and so count[] is decremented as codes are
2193 entered in the tables.
2195 used keeps track of how many table entries have been allocated from the
2196 provided *table space. It is checked for LENS and DIST tables against
2197 the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
2198 the initial root table size constants. See the comments in inftrees.h
2199 for more information.
2201 sym increments through all symbols, and the loop terminates when
2202 all codes of length max, i.e. all codes, have been processed. This
2203 routine permits incomplete codes, so another loop after this one fills
2204 in the rest of the decoding tables with invalid code markers.
2207 /* set up for code type */
2208 // poor man optimization - use if-else instead of switch,
2209 // to avoid deopts in old v8
2210 if (type
=== CODES
) {
2211 base
= extra
= work
; /* dummy value--not used */
2214 } else if (type
=== LENS
) {
2221 } else { /* DISTS */
2227 /* initialize opts for loop */
2228 huff
= 0; /* starting code */
2229 sym
= 0; /* starting code symbol */
2230 len
= min
; /* starting code length */
2231 next
= table_index
; /* current table to fill in */
2232 curr
= root
; /* current table index bits */
2233 drop
= 0; /* current bits to drop from code for index */
2234 low
= -1; /* trigger new sub-table when len > root */
2235 used
= 1 << root
; /* use root table entries */
2236 mask
= used
- 1; /* mask for comparing low */
2238 /* check available table space */
2239 if ((type
=== LENS
&& used
> ENOUGH_LENS
) ||
2240 (type
=== DISTS
&& used
> ENOUGH_DISTS
)) {
2245 /* process all codes and make table entries */
2248 /* create table entry */
2249 here_bits
= len
- drop
;
2250 if (work
[sym
] < end
) {
2252 here_val
= work
[sym
];
2254 else if (work
[sym
] > end
) {
2255 here_op
= extra
[extra_index
+ work
[sym
]];
2256 here_val
= base
[base_index
+ work
[sym
]];
2259 here_op
= 32 + 64; /* end of block */
2263 /* replicate for those indices with low len bits equal to huff */
2264 incr
= 1 << (len
- drop
);
2266 min
= fill
; /* save offset to next table */
2269 table
[next
+ (huff
>> drop
) + fill
] = (here_bits
<< 24) | (here_op
<< 16) | here_val
|0;
2270 } while (fill
!== 0);
2272 /* backwards increment the len-bit code huff */
2273 incr
= 1 << (len
- 1);
2274 while (huff
& incr
) {
2284 /* go to next symbol, update count, len */
2286 if (--count
[len
] === 0) {
2287 if (len
=== max
) { break; }
2288 len
= lens
[lens_index
+ work
[sym
]];
2291 /* create new sub-table if needed */
2292 if (len
> root
&& (huff
& mask
) !== low
) {
2293 /* if first time, transition to sub-tables */
2298 /* increment past last table */
2299 next
+= min
; /* here min is 1 << curr */
2301 /* determine length of next table */
2304 while (curr
+ drop
< max
) {
2305 left
-= count
[curr
+ drop
];
2306 if (left
<= 0) { break; }
2311 /* check for enough space */
2313 if ((type
=== LENS
&& used
> ENOUGH_LENS
) ||
2314 (type
=== DISTS
&& used
> ENOUGH_DISTS
)) {
2318 /* point entry in root table to sub-table */
2320 /*table.op[low] = curr;
2321 table.bits[low] = root;
2322 table.val[low] = next - opts.table_index;*/
2323 table
[low
] = (root
<< 24) | (curr
<< 16) | (next
- table_index
) |0;
2327 /* fill in remaining table entry if code is incomplete (guaranteed to have
2328 at most one remaining entry, since if the code is incomplete, the
2329 maximum code length that was allowed to get this far is one bit) */
2331 //table.op[next + huff] = 64; /* invalid code marker */
2332 //table.bits[next + huff] = len - drop;
2333 //table.val[next + huff] = 0;
2334 table
[next
+ huff
] = ((len
- drop
) << 24) | (64 << 16) |0;
2337 /* set return parameters */
2338 //opts.table_index += used;
2343 },{"../utils/common":1}],7:[function(require
,module
,exports
){
2347 function ZStream() {
2348 /* next input byte */
2349 this.input
= null; // JS specific, because we have no pointers
2351 /* number of bytes available at input */
2353 /* total number of input bytes read so far */
2355 /* next output byte should be put there */
2356 this.output
= null; // JS specific, because we have no pointers
2358 /* remaining free space at output */
2360 /* total number of bytes output so far */
2362 /* last error message, NULL if no error */
2363 this.msg
= ''/*Z_NULL*/;
2364 /* not visible by applications */
2366 /* best guess about the data type: binary or text */
2367 this.data_type
= 2/*Z_UNKNOWN*/;
2368 /* adler32 value of the uncompressed data */
2372 module
.exports
= ZStream
;
2374 },{}],"/partial_inflator.js":[function(require
,module
,exports
){
2375 var zlib
= require('./lib/zlib/inflate.js');
2376 var ZStream
= require('./lib/zlib/zstream.js');
2378 var Inflate = function () {
2379 this.strm
= new ZStream();
2380 this.chunkSize
= 1024 * 10 * 10;
2381 this.strm
.output
= new Uint8Array(this.chunkSize
);
2382 this.windowBits
= 5;
2384 zlib
.inflateInit(this.strm
, this.windowBits
);
2387 Inflate
.prototype = {
2388 inflate: function (data
, flush
) {
2389 this.strm
.input
= new Uint8Array(data
);
2390 this.strm
.avail_in
= this.strm
.input
.length
;
2391 this.strm
.next_in
= 0;
2392 this.strm
.next_out
= 0;
2394 this.strm
.avail_out
= this.chunkSize
;
2396 zlib
.inflate(this.strm
, flush
);
2398 return new Uint8Array(this.strm
.output
.buffer
, 0, this.strm
.next_out
);
2401 reset: function () {
2402 zlib
.inflateReset(this.strm
);
2406 module
.exports
= {Inflate
: Inflate
};
2408 },{"./lib/zlib/inflate.js":5,"./lib/zlib/zstream.js":7}]},{},[])("/partial_inflator.js")