]>
git.proxmox.com Git - mirror_novnc.git/blob - core/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
){
2 var zlib
= require('pako/lib/zlib/inflate.js');
3 var ZStream
= require('pako/lib/zlib/zstream.js');
6 this.strm
= new ZStream();
7 this.chunkSize
= 1024 * 10 * 10;
8 this.strm
.output
= new Uint8Array(this.chunkSize
);
11 zlib
.inflateInit(this.strm
, this.windowBits
);
15 inflate: function (data
, flush
, expected
) {
16 this.strm
.input
= data
;
17 this.strm
.avail_in
= this.strm
.input
.length
;
18 this.strm
.next_in
= 0;
19 this.strm
.next_out
= 0;
21 // resize our output buffer if it's too small
22 // (we could just use multiple chunks, but that would cause an extra
23 // allocation each time to flatten the chunks)
24 if (expected
> this.chunkSize
) {
25 this.chunkSize
= expected
;
26 this.strm
.output
= new Uint8Array(this.chunkSize
);
29 this.strm
.avail_out
= this.chunkSize
;
31 zlib
.inflate(this.strm
, flush
);
33 return new Uint8Array(this.strm
.output
.buffer
, 0, this.strm
.next_out
);
37 zlib
.inflateReset(this.strm
);
41 module
.exports
= { Inflate
: Inflate
};
43 },{"pako/lib/zlib/inflate.js":6,"pako/lib/zlib/zstream.js":8}],2:[function(require
,module
,exports
){
47 var TYPED_OK
= (typeof Uint8Array
!== 'undefined') &&
48 (typeof Uint16Array
!== 'undefined') &&
49 (typeof Int32Array
!== 'undefined');
52 exports
.assign = function (obj
/*from1, from2, from3, ...*/) {
53 var sources
= Array
.prototype.slice
.call(arguments
, 1);
54 while (sources
.length
) {
55 var source
= sources
.shift();
56 if (!source
) { continue; }
58 if (typeof source
!== 'object') {
59 throw new TypeError(source
+ 'must be non-object');
62 for (var p
in source
) {
63 if (source
.hasOwnProperty(p
)) {
73 // reduce buffer size, avoiding mem copy
74 exports
.shrinkBuf = function (buf
, size
) {
75 if (buf
.length
=== size
) { return buf
; }
76 if (buf
.subarray
) { return buf
.subarray(0, size
); }
83 arraySet: function (dest
, src
, src_offs
, len
, dest_offs
) {
84 if (src
.subarray
&& dest
.subarray
) {
85 dest
.set(src
.subarray(src_offs
, src_offs
+ len
), dest_offs
);
88 // Fallback to ordinary array
89 for (var i
= 0; i
< len
; i
++) {
90 dest
[dest_offs
+ i
] = src
[src_offs
+ i
];
93 // Join array of chunks to single array.
94 flattenChunks: function (chunks
) {
95 var i
, l
, len
, pos
, chunk
, result
;
97 // calculate data length
99 for (i
= 0, l
= chunks
.length
; i
< l
; i
++) {
100 len
+= chunks
[i
].length
;
104 result
= new Uint8Array(len
);
106 for (i
= 0, l
= chunks
.length
; i
< l
; i
++) {
108 result
.set(chunk
, pos
);
117 arraySet: function (dest
, src
, src_offs
, len
, dest_offs
) {
118 for (var i
= 0; i
< len
; i
++) {
119 dest
[dest_offs
+ i
] = src
[src_offs
+ i
];
122 // Join array of chunks to single array.
123 flattenChunks: function (chunks
) {
124 return [].concat
.apply([], chunks
);
129 // Enable/Disable typed arrays use, for testing
131 exports
.setTyped = function (on
) {
133 exports
.Buf8
= Uint8Array
;
134 exports
.Buf16
= Uint16Array
;
135 exports
.Buf32
= Int32Array
;
136 exports
.assign(exports
, fnTyped
);
138 exports
.Buf8
= Array
;
139 exports
.Buf16
= Array
;
140 exports
.Buf32
= Array
;
141 exports
.assign(exports
, fnUntyped
);
145 exports
.setTyped(TYPED_OK
);
147 },{}],3:[function(require
,module
,exports
){
150 // Note: adler32 takes 12% for level 0 and 2% for level 6.
151 // It doesn't worth to make additional optimizationa as in original.
152 // Small size is preferable.
154 function adler32(adler
, buf
, len
, pos
) {
155 var s1
= (adler
& 0xffff) |0,
156 s2
= ((adler
>>> 16) & 0xffff) |0,
160 // Set limit ~ twice less than 5552, to keep
161 // s2 in 31-bits, because we force signed ints.
162 // in other case %= will fail.
163 n
= len
> 2000 ? 2000 : len
;
167 s1
= (s1
+ buf
[pos
++]) |0;
175 return (s1
| (s2
<< 16)) |0;
179 module
.exports
= adler32
;
181 },{}],4:[function(require
,module
,exports
){
184 // Note: we can't get significant speed boost here.
185 // So write code to minimize size - no pregenerated tables
186 // and array tools dependencies.
189 // Use ordinary array, since untyped makes no boost here
190 function makeTable() {
193 for (var n
= 0; n
< 256; n
++) {
195 for (var k
= 0; k
< 8; k
++) {
196 c
= ((c
& 1) ? (0xEDB88320 ^ (c
>>> 1)) : (c
>>> 1));
204 // Create table on load. Just 255 signed longs. Not a problem.
205 var crcTable
= makeTable();
208 function crc32(crc
, buf
, len
, pos
) {
214 for (var i
= pos
; i
< end
; i
++) {
215 crc
= (crc
>>> 8) ^ t
[(crc
^ buf
[i
]) & 0xFF];
218 return (crc
^ (-1)); // >>> 0;
222 module
.exports
= crc32
;
224 },{}],5:[function(require
,module
,exports
){
227 // See state defs from inflate.js
228 var BAD
= 30; /* got a data error -- remain here until reset */
229 var TYPE
= 12; /* i: waiting for type bits, including last-flag bit */
232 Decode literal, length, and distance codes and write out the resulting
233 literal and match bytes until either not enough input or output is
234 available, an end-of-block is encountered, or a data error is encountered.
235 When large enough input and output buffers are supplied to inflate(), for
236 example, a 16K input buffer and a 64K output buffer, more than 95% of the
237 inflate execution time is spent in this routine.
243 strm.avail_out >= 258
244 start >= strm.avail_out
247 On return, state.mode is one of:
249 LEN -- ran out of enough output space or enough available input
250 TYPE -- reached end of block code, inflate() to interpret next block
251 BAD -- error in block data
255 - The maximum input bits used by a length/distance pair is 15 bits for the
256 length code, 5 bits for the length extra, 15 bits for the distance code,
257 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
258 Therefore if strm.avail_in >= 6, then there is enough input to avoid
259 checking for available input while decoding.
261 - The maximum bytes that a single length/distance pair can output is 258
262 bytes, which is the maximum length that can be coded. inflate_fast()
263 requires strm.avail_out >= 258 for each loop to avoid checking for
266 module
.exports
= function inflate_fast(strm
, start
) {
268 var _in
; /* local strm.input */
269 var last
; /* have enough input while in < last */
270 var _out
; /* local strm.output */
271 var beg
; /* inflate()'s initial strm.output */
272 var end
; /* while out < end, enough space available */
273 //#ifdef INFLATE_STRICT
274 var dmax
; /* maximum distance from zlib header */
276 var wsize
; /* window size or zero if not using window */
277 var whave
; /* valid bytes in the window */
278 var wnext
; /* window write index */
279 // Use `s_window` instead `window`, avoid conflict with instrumentation tools
280 var s_window
; /* allocated sliding window, if wsize != 0 */
281 var hold
; /* local strm.hold */
282 var bits
; /* local strm.bits */
283 var lcode
; /* local strm.lencode */
284 var dcode
; /* local strm.distcode */
285 var lmask
; /* mask for first level of length codes */
286 var dmask
; /* mask for first level of distance codes */
287 var here
; /* retrieved table entry */
288 var op
; /* code bits, operation, extra bits, or */
289 /* window position, window bytes to copy */
290 var len
; /* match length, unused bytes */
291 var dist
; /* match distance */
292 var from; /* where to copy match from */
296 var input
, output
; // JS specific, because we have no pointers
298 /* copy state to local variables */
303 last
= _in
+ (strm
.avail_in
- 5);
304 _out
= strm
.next_out
;
305 output
= strm
.output
;
306 beg
= _out
- (start
- strm
.avail_out
);
307 end
= _out
+ (strm
.avail_out
- 257);
308 //#ifdef INFLATE_STRICT
314 s_window
= state
.window
;
317 lcode
= state
.lencode
;
318 dcode
= state
.distcode
;
319 lmask
= (1 << state
.lenbits
) - 1;
320 dmask
= (1 << state
.distbits
) - 1;
323 /* decode literals and length/distances until end-of-block or not enough
324 input data or output space */
329 hold
+= input
[_in
++] << bits
;
331 hold
+= input
[_in
++] << bits
;
335 here
= lcode
[hold
& lmask
];
338 for (;;) { // Goto emulation
339 op
= here
>>> 24/*here.bits*/;
342 op
= (here
>>> 16) & 0xff/*here.op*/;
343 if (op
=== 0) { /* literal */
344 //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
345 // "inflate: literal '%c'\n" :
346 // "inflate: literal 0x%02x\n", here.val));
347 output
[_out
++] = here
& 0xffff/*here.val*/;
349 else if (op
& 16) { /* length base */
350 len
= here
& 0xffff/*here.val*/;
351 op
&= 15; /* number of extra bits */
354 hold
+= input
[_in
++] << bits
;
357 len
+= hold
& ((1 << op
) - 1);
361 //Tracevv((stderr, "inflate: length %u\n", len));
363 hold
+= input
[_in
++] << bits
;
365 hold
+= input
[_in
++] << bits
;
368 here
= dcode
[hold
& dmask
];
371 for (;;) { // goto emulation
372 op
= here
>>> 24/*here.bits*/;
375 op
= (here
>>> 16) & 0xff/*here.op*/;
377 if (op
& 16) { /* distance base */
378 dist
= here
& 0xffff/*here.val*/;
379 op
&= 15; /* number of extra bits */
381 hold
+= input
[_in
++] << bits
;
384 hold
+= input
[_in
++] << bits
;
388 dist
+= hold
& ((1 << op
) - 1);
389 //#ifdef INFLATE_STRICT
391 strm
.msg
= 'invalid distance too far back';
398 //Tracevv((stderr, "inflate: distance %u\n", dist));
399 op
= _out
- beg
; /* max distance in output */
400 if (dist
> op
) { /* see if copy from window */
401 op
= dist
- op
; /* distance back in window */
404 strm
.msg
= 'invalid distance too far back';
409 // (!) This block is disabled in zlib defailts,
410 // don't enable it for binary compatibility
411 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
412 // if (len <= op - whave) {
414 // output[_out++] = 0;
418 // len -= op - whave;
420 // output[_out++] = 0;
421 // } while (--op > whave);
423 // from = _out - dist;
425 // output[_out++] = output[from++];
431 from = 0; // window index
432 from_source
= s_window
;
433 if (wnext
=== 0) { /* very common case */
435 if (op
< len
) { /* some from window */
438 output
[_out
++] = s_window
[from++];
440 from = _out
- dist
; /* rest from output */
441 from_source
= output
;
444 else if (wnext
< op
) { /* wrap around window */
445 from += wsize
+ wnext
- op
;
447 if (op
< len
) { /* some from end of window */
450 output
[_out
++] = s_window
[from++];
453 if (wnext
< len
) { /* some from start of window */
457 output
[_out
++] = s_window
[from++];
459 from = _out
- dist
; /* rest from output */
460 from_source
= output
;
464 else { /* contiguous in window */
466 if (op
< len
) { /* some from window */
469 output
[_out
++] = s_window
[from++];
471 from = _out
- dist
; /* rest from output */
472 from_source
= output
;
476 output
[_out
++] = from_source
[from++];
477 output
[_out
++] = from_source
[from++];
478 output
[_out
++] = from_source
[from++];
482 output
[_out
++] = from_source
[from++];
484 output
[_out
++] = from_source
[from++];
489 from = _out
- dist
; /* copy direct from output */
490 do { /* minimum length is three */
491 output
[_out
++] = output
[from++];
492 output
[_out
++] = output
[from++];
493 output
[_out
++] = output
[from++];
497 output
[_out
++] = output
[from++];
499 output
[_out
++] = output
[from++];
504 else if ((op
& 64) === 0) { /* 2nd level distance code */
505 here
= dcode
[(here
& 0xffff)/*here.val*/ + (hold
& ((1 << op
) - 1))];
509 strm
.msg
= 'invalid distance code';
514 break; // need to emulate goto via "continue"
517 else if ((op
& 64) === 0) { /* 2nd level length code */
518 here
= lcode
[(here
& 0xffff)/*here.val*/ + (hold
& ((1 << op
) - 1))];
521 else if (op
& 32) { /* end-of-block */
522 //Tracevv((stderr, "inflate: end of block\n"));
527 strm
.msg
= 'invalid literal/length code';
532 break; // need to emulate goto via "continue"
534 } while (_in
< last
&& _out
< end
);
536 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
540 hold
&= (1 << bits
) - 1;
542 /* update state and return */
544 strm
.next_out
= _out
;
545 strm
.avail_in
= (_in
< last
? 5 + (last
- _in
) : 5 - (_in
- last
));
546 strm
.avail_out
= (_out
< end
? 257 + (end
- _out
) : 257 - (_out
- end
));
552 },{}],6:[function(require
,module
,exports
){
556 var utils
= require('../utils/common');
557 var adler32
= require('./adler32');
558 var crc32
= require('./crc32');
559 var inflate_fast
= require('./inffast');
560 var inflate_table
= require('./inftrees');
566 /* Public constants ==========================================================*/
567 /* ===========================================================================*/
570 /* Allowed flush values; see deflate() and inflate() below for details */
571 //var Z_NO_FLUSH = 0;
572 //var Z_PARTIAL_FLUSH = 1;
573 //var Z_SYNC_FLUSH = 2;
574 //var Z_FULL_FLUSH = 3;
580 /* Return codes for the compression/decompression functions. Negative values
581 * are errors, positive values are used for special but normal events.
584 var Z_STREAM_END
= 1;
587 var Z_STREAM_ERROR
= -2;
588 var Z_DATA_ERROR
= -3;
589 var Z_MEM_ERROR
= -4;
590 var Z_BUF_ERROR
= -5;
591 //var Z_VERSION_ERROR = -6;
593 /* The deflate compression method */
597 /* STATES ====================================================================*/
598 /* ===========================================================================*/
601 var HEAD
= 1; /* i: waiting for magic header */
602 var FLAGS
= 2; /* i: waiting for method and flags (gzip) */
603 var TIME
= 3; /* i: waiting for modification time (gzip) */
604 var OS
= 4; /* i: waiting for extra flags and operating system (gzip) */
605 var EXLEN
= 5; /* i: waiting for extra length (gzip) */
606 var EXTRA
= 6; /* i: waiting for extra bytes (gzip) */
607 var NAME
= 7; /* i: waiting for end of file name (gzip) */
608 var COMMENT
= 8; /* i: waiting for end of comment (gzip) */
609 var HCRC
= 9; /* i: waiting for header crc (gzip) */
610 var DICTID
= 10; /* i: waiting for dictionary check value */
611 var DICT
= 11; /* waiting for inflateSetDictionary() call */
612 var TYPE
= 12; /* i: waiting for type bits, including last-flag bit */
613 var TYPEDO
= 13; /* i: same, but skip check to exit inflate on new block */
614 var STORED
= 14; /* i: waiting for stored size (length and complement) */
615 var COPY_
= 15; /* i/o: same as COPY below, but only first time in */
616 var COPY = 16; /* i/o: waiting for input or output to copy stored block */
617 var TABLE = 17; /* i: waiting for dynamic block table lengths */
618 var LENLENS
= 18; /* i: waiting for code length code lengths */
619 var CODELENS
= 19; /* i: waiting for length/lit and distance code lengths */
620 var LEN_ = 20; /* i: same as LEN below, but only first time in */
621 var LEN
= 21; /* i: waiting for length/lit/eob code */
622 var LENEXT
= 22; /* i: waiting for length extra bits */
623 var DIST
= 23; /* i: waiting for distance code */
624 var DISTEXT
= 24; /* i: waiting for distance extra bits */
625 var MATCH
= 25; /* o: waiting for output space to copy string */
626 var LIT
= 26; /* o: waiting for output space to write literal */
627 var CHECK
= 27; /* i: waiting for 32-bit check value */
628 var LENGTH
= 28; /* i: waiting for 32-bit length (gzip) */
629 var DONE
= 29; /* finished check, done -- remain here until reset */
630 var BAD
= 30; /* got a data error -- remain here until reset */
631 var MEM
= 31; /* got an inflate() memory error -- remain here until reset */
632 var SYNC
= 32; /* looking for synchronization bytes to restart inflate() */
634 /* ===========================================================================*/
638 var ENOUGH_LENS
= 852;
639 var ENOUGH_DISTS
= 592;
640 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
643 /* 32K LZ77 window */
644 var DEF_WBITS
= MAX_WBITS
;
647 function zswap32(q
) {
648 return (((q
>>> 24) & 0xff) +
649 ((q
>>> 8) & 0xff00) +
650 ((q
& 0xff00) << 8) +
655 function InflateState() {
656 this.mode
= 0; /* current inflate mode */
657 this.last
= false; /* true if processing last block */
658 this.wrap
= 0; /* bit 0 true for zlib, bit 1 true for gzip */
659 this.havedict
= false; /* true if dictionary provided */
660 this.flags
= 0; /* gzip header method and flags (0 if zlib) */
661 this.dmax
= 0; /* zlib header max distance (INFLATE_STRICT) */
662 this.check
= 0; /* protected copy of check value */
663 this.total
= 0; /* protected copy of output count */
665 this.head
= null; /* where to save gzip header information */
668 this.wbits
= 0; /* log base 2 of requested window size */
669 this.wsize
= 0; /* window size or zero if not using window */
670 this.whave
= 0; /* valid bytes in the window */
671 this.wnext
= 0; /* window write index */
672 this.window
= null; /* allocated sliding window, if needed */
674 /* bit accumulator */
675 this.hold
= 0; /* input bit accumulator */
676 this.bits
= 0; /* number of bits in "in" */
678 /* for string and stored block copying */
679 this.length
= 0; /* literal or length of data to copy */
680 this.offset
= 0; /* distance back to copy string from */
682 /* for table and code decoding */
683 this.extra
= 0; /* extra bits needed */
685 /* fixed and dynamic code tables */
686 this.lencode
= null; /* starting table for length/literal codes */
687 this.distcode = null; /* starting table for distance codes */
688 this.lenbits
= 0; /* index bits for lencode */
689 this.distbits
= 0; /* index bits for distcode */
691 /* dynamic table building */
692 this.ncode
= 0; /* number of code length code lengths */
693 this.nlen
= 0; /* number of length code lengths */
694 this.ndist
= 0; /* number of distance code lengths */
695 this.have
= 0; /* number of code lengths in lens[] */
696 this.next
= null; /* next available space in codes[] */
698 this.lens
= new utils
.Buf16(320); /* temporary storage for code lengths */
699 this.work
= new utils
.Buf16(288); /* work area for code table building */
702 because we don't have pointers in js, we use lencode and distcode directly
703 as buffers so we don't need codes
705 //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
706 this.lendyn
= null; /* dynamic table for length/literal codes (JS specific) */
707 this.distdyn = null; /* dynamic table for distance codes (JS specific) */
708 this.sane
= 0; /* if false, allow invalid distance too far */
709 this.back
= 0; /* bits back of last unprocessed length/lit */
710 this.was = 0; /* initial length of match */
713 function inflateResetKeep(strm
) {
716 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
718 strm
.total_in
= strm
.total_out
= state
.total
= 0;
719 strm
.msg
= ''; /*Z_NULL*/
720 if (state
.wrap
) { /* to support ill-conceived Java test suite */
721 strm
.adler
= state
.wrap
& 1;
727 state
.head
= null/*Z_NULL*/;
730 //state.lencode = state.distcode = state.next = state.codes;
731 state
.lencode
= state
.lendyn
= new utils
.Buf32(ENOUGH_LENS
);
732 state
.distcode
= state
.distdyn
= new utils
.Buf32(ENOUGH_DISTS
);
736 //Tracev((stderr, "inflate: reset\n"));
740 function inflateReset(strm
) {
743 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
748 return inflateResetKeep(strm
);
752 function inflateReset2(strm
, windowBits
) {
757 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
760 /* extract wrap request from windowBits parameter */
761 if (windowBits
< 0) {
763 windowBits
= -windowBits
;
766 wrap
= (windowBits
>> 4) + 1;
767 if (windowBits
< 48) {
772 /* set number of window bits, free window if different */
773 if (windowBits
&& (windowBits
< 8 || windowBits
> 15)) {
774 return Z_STREAM_ERROR
;
776 if (state
.window
!== null && state
.wbits
!== windowBits
) {
780 /* update state and reset the rest of it */
782 state
.wbits
= windowBits
;
783 return inflateReset(strm
);
786 function inflateInit2(strm
, windowBits
) {
790 if (!strm
) { return Z_STREAM_ERROR
; }
791 //strm.msg = Z_NULL; /* in case we return an error */
793 state
= new InflateState();
795 //if (state === Z_NULL) return Z_MEM_ERROR;
796 //Tracev((stderr, "inflate: allocated\n"));
798 state
.window
= null/*Z_NULL*/;
799 ret
= inflateReset2(strm
, windowBits
);
801 strm
.state
= null/*Z_NULL*/;
806 function inflateInit(strm
) {
807 return inflateInit2(strm
, DEF_WBITS
);
812 Return state with length and distance decoding tables and index sizes set to
813 fixed code decoding. Normally this returns fixed tables from inffixed.h.
814 If BUILDFIXED is defined, then instead this routine builds the tables the
815 first time it's called, and returns those tables the first time and
816 thereafter. This reduces the size of the code by about 2K bytes, in
817 exchange for a little execution time. However, BUILDFIXED should not be
818 used for threaded applications, since the rewriting of the tables and virgin
819 may not be thread-safe.
823 var lenfix
, distfix
; // We have no pointers in JS, so keep tables separate
825 function fixedtables(state
) {
826 /* build fixed huffman tables if first call (may not be thread safe) */
830 lenfix
= new utils
.Buf32(512);
831 distfix
= new utils
.Buf32(32);
833 /* literal/length table */
835 while (sym < 144) { state.lens[sym++] = 8; }
836 while (sym < 256) { state.lens[sym++] = 9; }
837 while (sym < 280) { state.lens[sym++] = 7; }
838 while (sym < 288) { state.lens[sym++] = 8; }
840 inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
844 while (sym
< 32) { state
.lens
[sym
++] = 5; }
846 inflate_table(DISTS
, state
.lens
, 0, 32, distfix
, 0, state
.work
, { bits
: 5 });
848 /* do this just once */
852 state
.lencode
= lenfix
;
854 state
.distcode
= distfix
;
860 Update the window with the last wsize (normally 32K) bytes written before
861 returning. If window does not exist yet, create it. This is only called
862 when a window is already in use, or when output has been written during this
863 inflate call, but the end of the deflate stream has not been reached yet.
864 It is also called to create a window for dictionary data when a dictionary
867 Providing output buffers larger than 32K to inflate() should provide a speed
868 advantage, since only the last 32K of output is copied to the sliding window
869 upon return from inflate(), and since all distances after the first 32K of
870 output will fall in the output data, making match copies simpler and faster.
871 The advantage may be dependent on the size of the processor's data caches.
873 function updatewindow(strm
, src
, end
, copy
) {
875 var state
= strm
.state
;
877 /* if it hasn't been done already, allocate space for the window */
878 if (state
.window
=== null) {
879 state
.wsize
= 1 << state
.wbits
;
883 state
.window
= new utils
.Buf8(state
.wsize
);
886 /* copy state->wsize or less output bytes into the circular window */
887 if (copy
>= state
.wsize
) {
888 utils
.arraySet(state
.window
, src
, end
- state
.wsize
, state
.wsize
, 0);
890 state
.whave
= state
.wsize
;
893 dist
= state
.wsize
- state
.wnext
;
897 //zmemcpy(state->window + state->wnext, end - copy, dist);
898 utils
.arraySet(state
.window
, src
, end
- copy
, dist
, state
.wnext
);
901 //zmemcpy(state->window, end - copy, copy);
902 utils
.arraySet(state
.window
, src
, end
- copy
, copy
, 0);
904 state
.whave
= state
.wsize
;
908 if (state
.wnext
=== state
.wsize
) { state
.wnext
= 0; }
909 if (state
.whave
< state
.wsize
) { state
.whave
+= dist
; }
915 function inflate(strm
, flush
) {
917 var input
, output
; // input/output buffers
918 var next
; /* next input INDEX */
919 var put
; /* next output INDEX */
920 var have
, left
; /* available input and output */
921 var hold
; /* bit buffer */
922 var bits
; /* bits in bit buffer */
923 var _in
, _out
; /* save starting available input and output */
924 var copy
; /* number of stored or match bytes to copy */
925 var from; /* where to copy match bytes from */
927 var here
= 0; /* current decoding table entry */
928 var here_bits
, here_op
, here_val
; // paked "here" denormalized (JS specific)
929 //var last; /* parent table entry */
930 var last_bits
, last_op
, last_val
; // paked "last" denormalized (JS specific)
931 var len
; /* length to copy for repeats, bits to drop */
932 var ret
; /* return code */
933 var hbuf
= new utils
.Buf8(4); /* buffer for gzip header crc calculation */
936 var n
; // temporary var for NEED_BITS
938 var order
= /* permutation of code lengths */
939 [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
942 if (!strm
|| !strm
.state
|| !strm
.output
||
943 (!strm
.input
&& strm
.avail_in
!== 0)) {
944 return Z_STREAM_ERROR
;
948 if (state
.mode
=== TYPE
) { state
.mode
= TYPEDO
; } /* skip check */
953 output
= strm
.output
;
954 left
= strm
.avail_out
;
957 have
= strm
.avail_in
;
966 inf_leave
: // goto emulation
968 switch (state
.mode
) {
970 if (state
.wrap
=== 0) {
976 if (have
=== 0) { break inf_leave
; }
978 hold
+= input
[next
++] << bits
;
982 if ((state
.wrap
& 2) && hold
=== 0x8b1f) { /* gzip header */
983 state
.check
= 0/*crc32(0L, Z_NULL, 0)*/;
984 //=== CRC2(state.check, hold);
985 hbuf
[0] = hold
& 0xff;
986 hbuf
[1] = (hold
>>> 8) & 0xff;
987 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
997 state
.flags
= 0; /* expect zlib header */
999 state
.head
.done
= false;
1001 if (!(state
.wrap
& 1) || /* check if zlib header allowed */
1002 (((hold
& 0xff)/*BITS(8)*/ << 8) + (hold
>> 8)) % 31) {
1003 strm
.msg
= 'incorrect header check';
1007 if ((hold
& 0x0f)/*BITS(4)*/ !== Z_DEFLATED
) {
1008 strm
.msg
= 'unknown compression method';
1012 //--- DROPBITS(4) ---//
1016 len
= (hold
& 0x0f)/*BITS(4)*/ + 8;
1017 if (state
.wbits
=== 0) {
1020 else if (len
> state
.wbits
) {
1021 strm
.msg
= 'invalid window size';
1025 state
.dmax
= 1 << len
;
1026 //Tracev((stderr, "inflate: zlib header ok\n"));
1027 strm
.adler
= state
.check
= 1/*adler32(0L, Z_NULL, 0)*/;
1028 state
.mode
= hold
& 0x200 ? DICTID
: TYPE
;
1035 //=== NEEDBITS(16); */
1037 if (have
=== 0) { break inf_leave
; }
1039 hold
+= input
[next
++] << bits
;
1044 if ((state
.flags
& 0xff) !== Z_DEFLATED
) {
1045 strm
.msg
= 'unknown compression method';
1049 if (state
.flags
& 0xe000) {
1050 strm
.msg
= 'unknown header flags set';
1055 state
.head
.text
= ((hold
>> 8) & 1);
1057 if (state
.flags
& 0x0200) {
1058 //=== CRC2(state.check, hold);
1059 hbuf
[0] = hold
& 0xff;
1060 hbuf
[1] = (hold
>>> 8) & 0xff;
1061 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1071 //=== NEEDBITS(32); */
1073 if (have
=== 0) { break inf_leave
; }
1075 hold
+= input
[next
++] << bits
;
1080 state
.head
.time
= hold
;
1082 if (state
.flags
& 0x0200) {
1083 //=== CRC4(state.check, hold)
1084 hbuf
[0] = hold
& 0xff;
1085 hbuf
[1] = (hold
>>> 8) & 0xff;
1086 hbuf
[2] = (hold
>>> 16) & 0xff;
1087 hbuf
[3] = (hold
>>> 24) & 0xff;
1088 state
.check
= crc32(state
.check
, hbuf
, 4, 0);
1098 //=== NEEDBITS(16); */
1100 if (have
=== 0) { break inf_leave
; }
1102 hold
+= input
[next
++] << bits
;
1107 state
.head
.xflags
= (hold
& 0xff);
1108 state
.head
.os
= (hold
>> 8);
1110 if (state
.flags
& 0x0200) {
1111 //=== CRC2(state.check, hold);
1112 hbuf
[0] = hold
& 0xff;
1113 hbuf
[1] = (hold
>>> 8) & 0xff;
1114 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1124 if (state
.flags
& 0x0400) {
1125 //=== NEEDBITS(16); */
1127 if (have
=== 0) { break inf_leave
; }
1129 hold
+= input
[next
++] << bits
;
1133 state
.length
= hold
;
1135 state
.head
.extra_len
= hold
;
1137 if (state
.flags
& 0x0200) {
1138 //=== CRC2(state.check, hold);
1139 hbuf
[0] = hold
& 0xff;
1140 hbuf
[1] = (hold
>>> 8) & 0xff;
1141 state
.check
= crc32(state
.check
, hbuf
, 2, 0);
1149 else if (state
.head
) {
1150 state
.head
.extra
= null/*Z_NULL*/;
1155 if (state
.flags
& 0x0400) {
1156 copy
= state
.length
;
1157 if (copy
> have
) { copy
= have
; }
1160 len
= state
.head
.extra_len
- state
.length
;
1161 if (!state
.head
.extra
) {
1162 // Use untyped array for more conveniend processing later
1163 state
.head
.extra
= new Array(state
.head
.extra_len
);
1169 // extra field is limited to 65536 bytes
1170 // - no need for additional size check
1172 /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
1175 //zmemcpy(state.head.extra + len, next,
1176 // len + copy > state.head.extra_max ?
1177 // state.head.extra_max - len : copy);
1179 if (state
.flags
& 0x0200) {
1180 state
.check
= crc32(state
.check
, input
, copy
, next
);
1184 state
.length
-= copy
;
1186 if (state
.length
) { break inf_leave
; }
1192 if (state
.flags
& 0x0800) {
1193 if (have
=== 0) { break inf_leave
; }
1196 // TODO: 2 or 1 bytes?
1197 len
= input
[next
+ copy
++];
1198 /* use constant limit because in js we should not preallocate memory */
1199 if (state
.head
&& len
&&
1200 (state
.length
< 65536 /*state.head.name_max*/)) {
1201 state
.head
.name
+= String
.fromCharCode(len
);
1203 } while (len
&& copy
< have
);
1205 if (state
.flags
& 0x0200) {
1206 state
.check
= crc32(state
.check
, input
, copy
, next
);
1210 if (len
) { break inf_leave
; }
1212 else if (state
.head
) {
1213 state
.head
.name
= null;
1216 state
.mode
= COMMENT
;
1219 if (state
.flags
& 0x1000) {
1220 if (have
=== 0) { break inf_leave
; }
1223 len
= input
[next
+ copy
++];
1224 /* use constant limit because in js we should not preallocate memory */
1225 if (state
.head
&& len
&&
1226 (state
.length
< 65536 /*state.head.comm_max*/)) {
1227 state
.head
.comment
+= String
.fromCharCode(len
);
1229 } while (len
&& copy
< have
);
1230 if (state
.flags
& 0x0200) {
1231 state
.check
= crc32(state
.check
, input
, copy
, next
);
1235 if (len
) { break inf_leave
; }
1237 else if (state
.head
) {
1238 state
.head
.comment
= null;
1243 if (state
.flags
& 0x0200) {
1244 //=== NEEDBITS(16); */
1246 if (have
=== 0) { break inf_leave
; }
1248 hold
+= input
[next
++] << bits
;
1252 if (hold
!== (state
.check
& 0xffff)) {
1253 strm
.msg
= 'header crc mismatch';
1263 state
.head
.hcrc
= ((state
.flags
>> 9) & 1);
1264 state
.head
.done
= true;
1266 strm
.adler
= state
.check
= 0;
1270 //=== NEEDBITS(32); */
1272 if (have
=== 0) { break inf_leave
; }
1274 hold
+= input
[next
++] << bits
;
1278 strm
.adler
= state
.check
= zswap32(hold
);
1286 if (state
.havedict
=== 0) {
1288 strm
.next_out
= put
;
1289 strm
.avail_out
= left
;
1290 strm
.next_in
= next
;
1291 strm
.avail_in
= have
;
1297 strm
.adler
= state
.check
= 1/*adler32(0L, Z_NULL, 0)*/;
1301 if (flush
=== Z_BLOCK
|| flush
=== Z_TREES
) { break inf_leave
; }
1305 //--- BYTEBITS() ---//
1312 //=== NEEDBITS(3); */
1314 if (have
=== 0) { break inf_leave
; }
1316 hold
+= input
[next
++] << bits
;
1320 state
.last
= (hold
& 0x01)/*BITS(1)*/;
1321 //--- DROPBITS(1) ---//
1326 switch ((hold
& 0x03)/*BITS(2)*/) {
1327 case 0: /* stored block */
1328 //Tracev((stderr, "inflate: stored block%s\n",
1329 // state.last ? " (last)" : ""));
1330 state
.mode
= STORED
;
1332 case 1: /* fixed block */
1334 //Tracev((stderr, "inflate: fixed codes block%s\n",
1335 // state.last ? " (last)" : ""));
1336 state
.mode
= LEN_
; /* decode codes */
1337 if (flush
=== Z_TREES
) {
1338 //--- DROPBITS(2) ---//
1345 case 2: /* dynamic block */
1346 //Tracev((stderr, "inflate: dynamic codes block%s\n",
1347 // state.last ? " (last)" : ""));
1351 strm
.msg
= 'invalid block type';
1354 //--- DROPBITS(2) ---//
1360 //--- BYTEBITS() ---// /* go to byte boundary */
1364 //=== NEEDBITS(32); */
1366 if (have
=== 0) { break inf_leave
; }
1368 hold
+= input
[next
++] << bits
;
1372 if ((hold
& 0xffff) !== ((hold
>>> 16) ^ 0xffff)) {
1373 strm
.msg
= 'invalid stored block lengths';
1377 state
.length
= hold
& 0xffff;
1378 //Tracev((stderr, "inflate: stored length %u\n",
1385 if (flush
=== Z_TREES
) { break inf_leave
; }
1391 copy
= state
.length
;
1393 if (copy
> have
) { copy
= have
; }
1394 if (copy
> left
) { copy
= left
; }
1395 if (copy
=== 0) { break inf_leave
; }
1396 //--- zmemcpy(put, next, copy); ---
1397 utils
.arraySet(output
, input
, next
, copy
, put
);
1403 state
.length
-= copy
;
1406 //Tracev((stderr, "inflate: stored end\n"));
1410 //=== NEEDBITS(14); */
1412 if (have
=== 0) { break inf_leave
; }
1414 hold
+= input
[next
++] << bits
;
1418 state
.nlen
= (hold
& 0x1f)/*BITS(5)*/ + 257;
1419 //--- DROPBITS(5) ---//
1423 state
.ndist
= (hold
& 0x1f)/*BITS(5)*/ + 1;
1424 //--- DROPBITS(5) ---//
1428 state
.ncode
= (hold
& 0x0f)/*BITS(4)*/ + 4;
1429 //--- DROPBITS(4) ---//
1433 //#ifndef PKZIP_BUG_WORKAROUND
1434 if (state
.nlen
> 286 || state
.ndist
> 30) {
1435 strm
.msg
= 'too many length or distance symbols';
1440 //Tracev((stderr, "inflate: table sizes ok\n"));
1442 state
.mode
= LENLENS
;
1445 while (state
.have
< state
.ncode
) {
1448 if (have
=== 0) { break inf_leave
; }
1450 hold
+= input
[next
++] << bits
;
1454 state
.lens
[order
[state
.have
++]] = (hold
& 0x07);//BITS(3);
1455 //--- DROPBITS(3) ---//
1460 while (state
.have
< 19) {
1461 state
.lens
[order
[state
.have
++]] = 0;
1463 // We have separate tables & no pointers. 2 commented lines below not needed.
1464 //state.next = state.codes;
1465 //state.lencode = state.next;
1466 // Switch to use dynamic table
1467 state
.lencode
= state
.lendyn
;
1470 opts
= { bits
: state
.lenbits
};
1471 ret
= inflate_table(CODES
, state
.lens
, 0, 19, state
.lencode
, 0, state
.work
, opts
);
1472 state
.lenbits
= opts
.bits
;
1475 strm
.msg
= 'invalid code lengths set';
1479 //Tracev((stderr, "inflate: code lengths ok\n"));
1481 state
.mode
= CODELENS
;
1484 while (state
.have
< state
.nlen
+ state
.ndist
) {
1486 here
= state
.lencode
[hold
& ((1 << state
.lenbits
) - 1)];/*BITS(state.lenbits)*/
1487 here_bits
= here
>>> 24;
1488 here_op
= (here
>>> 16) & 0xff;
1489 here_val
= here
& 0xffff;
1491 if ((here_bits
) <= bits
) { break; }
1492 //--- PULLBYTE() ---//
1493 if (have
=== 0) { break inf_leave
; }
1495 hold
+= input
[next
++] << bits
;
1499 if (here_val
< 16) {
1500 //--- DROPBITS(here.bits) ---//
1501 hold
>>>= here_bits
;
1504 state
.lens
[state
.have
++] = here_val
;
1507 if (here_val
=== 16) {
1508 //=== NEEDBITS(here.bits + 2);
1511 if (have
=== 0) { break inf_leave
; }
1513 hold
+= input
[next
++] << bits
;
1517 //--- DROPBITS(here.bits) ---//
1518 hold
>>>= here_bits
;
1521 if (state
.have
=== 0) {
1522 strm
.msg
= 'invalid bit length repeat';
1526 len
= state
.lens
[state
.have
- 1];
1527 copy
= 3 + (hold
& 0x03);//BITS(2);
1528 //--- DROPBITS(2) ---//
1533 else if (here_val
=== 17) {
1534 //=== NEEDBITS(here.bits + 3);
1537 if (have
=== 0) { break inf_leave
; }
1539 hold
+= input
[next
++] << bits
;
1543 //--- DROPBITS(here.bits) ---//
1544 hold
>>>= here_bits
;
1548 copy
= 3 + (hold
& 0x07);//BITS(3);
1549 //--- DROPBITS(3) ---//
1555 //=== NEEDBITS(here.bits + 7);
1558 if (have
=== 0) { break inf_leave
; }
1560 hold
+= input
[next
++] << bits
;
1564 //--- DROPBITS(here.bits) ---//
1565 hold
>>>= here_bits
;
1569 copy
= 11 + (hold
& 0x7f);//BITS(7);
1570 //--- DROPBITS(7) ---//
1575 if (state
.have
+ copy
> state
.nlen
+ state
.ndist
) {
1576 strm
.msg
= 'invalid bit length repeat';
1581 state
.lens
[state
.have
++] = len
;
1586 /* handle error breaks in while */
1587 if (state
.mode
=== BAD
) { break; }
1589 /* check for end-of-block code (better have one) */
1590 if (state
.lens
[256] === 0) {
1591 strm
.msg
= 'invalid code -- missing end-of-block';
1596 /* build code tables -- note: do not change the lenbits or distbits
1597 values here (9 and 6) without reading the comments in inftrees.h
1598 concerning the ENOUGH constants, which depend on those values */
1601 opts
= { bits
: state
.lenbits
};
1602 ret
= inflate_table(LENS
, state
.lens
, 0, state
.nlen
, state
.lencode
, 0, state
.work
, opts
);
1603 // We have separate tables & no pointers. 2 commented lines below not needed.
1604 // state.next_index = opts.table_index;
1605 state
.lenbits
= opts
.bits
;
1606 // state.lencode = state.next;
1609 strm
.msg
= 'invalid literal/lengths set';
1615 //state.distcode.copy(state.codes);
1616 // Switch to use dynamic table
1617 state
.distcode
= state
.distdyn
;
1618 opts
= { bits
: state
.distbits
};
1619 ret
= inflate_table(DISTS
, state
.lens
, state
.nlen
, state
.ndist
, state
.distcode
, 0, state
.work
, opts
);
1620 // We have separate tables & no pointers. 2 commented lines below not needed.
1621 // state.next_index = opts.table_index;
1622 state
.distbits
= opts
.bits
;
1623 // state.distcode = state.next;
1626 strm
.msg
= 'invalid distances set';
1630 //Tracev((stderr, 'inflate: codes ok\n'));
1632 if (flush
=== Z_TREES
) { break inf_leave
; }
1638 if (have
>= 6 && left
>= 258) {
1640 strm
.next_out
= put
;
1641 strm
.avail_out
= left
;
1642 strm
.next_in
= next
;
1643 strm
.avail_in
= have
;
1647 inflate_fast(strm
, _out
);
1649 put
= strm
.next_out
;
1650 output
= strm
.output
;
1651 left
= strm
.avail_out
;
1652 next
= strm
.next_in
;
1654 have
= strm
.avail_in
;
1659 if (state
.mode
=== TYPE
) {
1666 here
= state
.lencode
[hold
& ((1 << state
.lenbits
) - 1)]; /*BITS(state.lenbits)*/
1667 here_bits
= here
>>> 24;
1668 here_op
= (here
>>> 16) & 0xff;
1669 here_val
= here
& 0xffff;
1671 if (here_bits
<= bits
) { break; }
1672 //--- PULLBYTE() ---//
1673 if (have
=== 0) { break inf_leave
; }
1675 hold
+= input
[next
++] << bits
;
1679 if (here_op
&& (here_op
& 0xf0) === 0) {
1680 last_bits
= here_bits
;
1682 last_val
= here_val
;
1684 here
= state
.lencode
[last_val
+
1685 ((hold
& ((1 << (last_bits
+ last_op
)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits
)];
1686 here_bits
= here
>>> 24;
1687 here_op
= (here
>>> 16) & 0xff;
1688 here_val
= here
& 0xffff;
1690 if ((last_bits
+ here_bits
) <= bits
) { break; }
1691 //--- PULLBYTE() ---//
1692 if (have
=== 0) { break inf_leave
; }
1694 hold
+= input
[next
++] << bits
;
1698 //--- DROPBITS(last.bits) ---//
1699 hold
>>>= last_bits
;
1702 state
.back
+= last_bits
;
1704 //--- DROPBITS(here.bits) ---//
1705 hold
>>>= here_bits
;
1708 state
.back
+= here_bits
;
1709 state
.length
= here_val
;
1710 if (here_op
=== 0) {
1711 //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1712 // "inflate: literal '%c'\n" :
1713 // "inflate: literal 0x%02x\n", here.val));
1718 //Tracevv((stderr, "inflate: end of block\n"));
1724 strm
.msg
= 'invalid literal/length code';
1728 state
.extra
= here_op
& 15;
1729 state
.mode
= LENEXT
;
1733 //=== NEEDBITS(state.extra);
1736 if (have
=== 0) { break inf_leave
; }
1738 hold
+= input
[next
++] << bits
;
1742 state
.length
+= hold
& ((1 << state
.extra
) - 1)/*BITS(state.extra)*/;
1743 //--- DROPBITS(state.extra) ---//
1744 hold
>>>= state
.extra
;
1745 bits
-= state
.extra
;
1747 state
.back
+= state
.extra
;
1749 //Tracevv((stderr, "inflate: length %u\n", state.length));
1750 state
.was
= state
.length
;
1755 here
= state
.distcode
[hold
& ((1 << state
.distbits
) - 1)];/*BITS(state.distbits)*/
1756 here_bits
= here
>>> 24;
1757 here_op
= (here
>>> 16) & 0xff;
1758 here_val
= here
& 0xffff;
1760 if ((here_bits
) <= bits
) { break; }
1761 //--- PULLBYTE() ---//
1762 if (have
=== 0) { break inf_leave
; }
1764 hold
+= input
[next
++] << bits
;
1768 if ((here_op
& 0xf0) === 0) {
1769 last_bits
= here_bits
;
1771 last_val
= here_val
;
1773 here
= state
.distcode
[last_val
+
1774 ((hold
& ((1 << (last_bits
+ last_op
)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits
)];
1775 here_bits
= here
>>> 24;
1776 here_op
= (here
>>> 16) & 0xff;
1777 here_val
= here
& 0xffff;
1779 if ((last_bits
+ here_bits
) <= bits
) { break; }
1780 //--- PULLBYTE() ---//
1781 if (have
=== 0) { break inf_leave
; }
1783 hold
+= input
[next
++] << bits
;
1787 //--- DROPBITS(last.bits) ---//
1788 hold
>>>= last_bits
;
1791 state
.back
+= last_bits
;
1793 //--- DROPBITS(here.bits) ---//
1794 hold
>>>= here_bits
;
1797 state
.back
+= here_bits
;
1799 strm
.msg
= 'invalid distance code';
1803 state
.offset
= here_val
;
1804 state
.extra
= (here_op
) & 15;
1805 state
.mode
= DISTEXT
;
1809 //=== NEEDBITS(state.extra);
1812 if (have
=== 0) { break inf_leave
; }
1814 hold
+= input
[next
++] << bits
;
1818 state
.offset
+= hold
& ((1 << state
.extra
) - 1)/*BITS(state.extra)*/;
1819 //--- DROPBITS(state.extra) ---//
1820 hold
>>>= state
.extra
;
1821 bits
-= state
.extra
;
1823 state
.back
+= state
.extra
;
1825 //#ifdef INFLATE_STRICT
1826 if (state
.offset
> state
.dmax
) {
1827 strm
.msg
= 'invalid distance too far back';
1832 //Tracevv((stderr, "inflate: distance %u\n", state.offset));
1836 if (left
=== 0) { break inf_leave
; }
1838 if (state
.offset
> copy
) { /* copy from window */
1839 copy
= state
.offset
- copy
;
1840 if (copy
> state
.whave
) {
1842 strm
.msg
= 'invalid distance too far back';
1846 // (!) This block is disabled in zlib defailts,
1847 // don't enable it for binary compatibility
1848 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1849 // Trace((stderr, "inflate.c too far\n"));
1850 // copy -= state.whave;
1851 // if (copy > state.length) { copy = state.length; }
1852 // if (copy > left) { copy = left; }
1854 // state.length -= copy;
1856 // output[put++] = 0;
1857 // } while (--copy);
1858 // if (state.length === 0) { state.mode = LEN; }
1862 if (copy
> state
.wnext
) {
1863 copy
-= state
.wnext
;
1864 from = state
.wsize
- copy
;
1867 from = state
.wnext
- copy
;
1869 if (copy
> state
.length
) { copy
= state
.length
; }
1870 from_source
= state
.window
;
1872 else { /* copy from output */
1873 from_source
= output
;
1874 from = put
- state
.offset
;
1875 copy
= state
.length
;
1877 if (copy
> left
) { copy
= left
; }
1879 state
.length
-= copy
;
1881 output
[put
++] = from_source
[from++];
1883 if (state
.length
=== 0) { state
.mode
= LEN
; }
1886 if (left
=== 0) { break inf_leave
; }
1887 output
[put
++] = state
.length
;
1895 if (have
=== 0) { break inf_leave
; }
1897 // Use '|' insdead of '+' to make sure that result is signed
1898 hold
|= input
[next
++] << bits
;
1903 strm
.total_out
+= _out
;
1904 state
.total
+= _out
;
1906 strm
.adler
= state
.check
=
1907 /*UPDATE(state.check, put - _out, _out);*/
1908 (state
.flags
? crc32(state
.check
, output
, _out
, put
- _out
) : adler32(state
.check
, output
, _out
, put
- _out
));
1912 // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
1913 if ((state
.flags
? hold
: zswap32(hold
)) !== state
.check
) {
1914 strm
.msg
= 'incorrect data check';
1922 //Tracev((stderr, "inflate: check matches trailer\n"));
1924 state
.mode
= LENGTH
;
1927 if (state
.wrap
&& state
.flags
) {
1930 if (have
=== 0) { break inf_leave
; }
1932 hold
+= input
[next
++] << bits
;
1936 if (hold
!== (state
.total
& 0xffffffff)) {
1937 strm
.msg
= 'incorrect length check';
1945 //Tracev((stderr, "inflate: length matches trailer\n"));
1960 return Z_STREAM_ERROR
;
1964 // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
1967 Return from inflate(), updating the total counts and the check value.
1968 If there was no progress during the inflate() call, return a buffer
1969 error. Call updatewindow() to create and/or update the window state.
1970 Note: a memory error from inflate() is non-recoverable.
1974 strm
.next_out
= put
;
1975 strm
.avail_out
= left
;
1976 strm
.next_in
= next
;
1977 strm
.avail_in
= have
;
1982 if (state
.wsize
|| (_out
!== strm
.avail_out
&& state
.mode
< BAD
&&
1983 (state
.mode
< CHECK
|| flush
!== Z_FINISH
))) {
1984 if (updatewindow(strm
, strm
.output
, strm
.next_out
, _out
- strm
.avail_out
)) {
1989 _in
-= strm
.avail_in
;
1990 _out
-= strm
.avail_out
;
1991 strm
.total_in
+= _in
;
1992 strm
.total_out
+= _out
;
1993 state
.total
+= _out
;
1994 if (state
.wrap
&& _out
) {
1995 strm
.adler
= state
.check
= /*UPDATE(state.check, strm.next_out - _out, _out);*/
1996 (state
.flags
? crc32(state
.check
, output
, _out
, strm
.next_out
- _out
) : adler32(state
.check
, output
, _out
, strm
.next_out
- _out
));
1998 strm
.data_type
= state
.bits
+ (state
.last
? 64 : 0) +
1999 (state
.mode
=== TYPE
? 128 : 0) +
2000 (state
.mode
=== LEN_
|| state
.mode
=== COPY_
? 256 : 0);
2001 if (((_in
=== 0 && _out
=== 0) || flush
=== Z_FINISH
) && ret
=== Z_OK
) {
2007 function inflateEnd(strm
) {
2009 if (!strm
|| !strm
.state
/*|| strm->zfree == (free_func)0*/) {
2010 return Z_STREAM_ERROR
;
2013 var state
= strm
.state
;
2015 state
.window
= null;
2021 function inflateGetHeader(strm
, head
) {
2025 if (!strm
|| !strm
.state
) { return Z_STREAM_ERROR
; }
2027 if ((state
.wrap
& 2) === 0) { return Z_STREAM_ERROR
; }
2029 /* save header structure */
2035 function inflateSetDictionary(strm
, dictionary
) {
2036 var dictLength
= dictionary
.length
;
2043 if (!strm
/* == Z_NULL */ || !strm
.state
/* == Z_NULL */) { return Z_STREAM_ERROR
; }
2046 if (state
.wrap
!== 0 && state
.mode
!== DICT
) {
2047 return Z_STREAM_ERROR
;
2050 /* check for correct dictionary identifier */
2051 if (state
.mode
=== DICT
) {
2052 dictid
= 1; /* adler32(0, null, 0)*/
2053 /* dictid = adler32(dictid, dictionary, dictLength); */
2054 dictid
= adler32(dictid
, dictionary
, dictLength
, 0);
2055 if (dictid
!== state
.check
) {
2056 return Z_DATA_ERROR
;
2059 /* copy dictionary to window using updatewindow(), which will amend the
2060 existing dictionary if appropriate */
2061 ret
= updatewindow(strm
, dictionary
, dictLength
, dictLength
);
2067 // Tracev((stderr, "inflate: dictionary set\n"));
2071 exports
.inflateReset
= inflateReset
;
2072 exports
.inflateReset2
= inflateReset2
;
2073 exports
.inflateResetKeep
= inflateResetKeep
;
2074 exports
.inflateInit
= inflateInit
;
2075 exports
.inflateInit2
= inflateInit2
;
2076 exports
.inflate
= inflate
;
2077 exports
.inflateEnd
= inflateEnd
;
2078 exports
.inflateGetHeader
= inflateGetHeader
;
2079 exports
.inflateSetDictionary
= inflateSetDictionary
;
2080 exports
.inflateInfo
= 'pako inflate (from Nodeca project)';
2083 exports.inflateCopy = inflateCopy;
2084 exports.inflateGetDictionary = inflateGetDictionary;
2085 exports.inflateMark = inflateMark;
2086 exports.inflatePrime = inflatePrime;
2087 exports.inflateSync = inflateSync;
2088 exports.inflateSyncPoint = inflateSyncPoint;
2089 exports.inflateUndermine = inflateUndermine;
2092 },{"../utils/common":2,"./adler32":3,"./crc32":4,"./inffast":5,"./inftrees":7}],7:[function(require
,module
,exports
){
2096 var utils
= require('../utils/common');
2099 var ENOUGH_LENS
= 852;
2100 var ENOUGH_DISTS
= 592;
2101 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
2107 var lbase
= [ /* Length codes 257..285 base */
2108 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2109 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
2112 var lext
= [ /* Length codes 257..285 extra */
2113 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
2114 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
2117 var dbase
= [ /* Distance codes 0..29 base */
2118 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2119 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2120 8193, 12289, 16385, 24577, 0, 0
2123 var dext
= [ /* Distance codes 0..29 extra */
2124 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
2125 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
2126 28, 28, 29, 29, 64, 64
2129 module
.exports
= function inflate_table(type
, lens
, lens_index
, codes
, table
, table_index
, work
, opts
)
2131 var bits
= opts
.bits
;
2132 //here = opts.here; /* table entry for duplication */
2134 var len
= 0; /* a code's length in bits */
2135 var sym
= 0; /* index of code symbols */
2136 var min
= 0, max
= 0; /* minimum and maximum code lengths */
2137 var root
= 0; /* number of index bits for root table */
2138 var curr
= 0; /* number of index bits for current table */
2139 var drop
= 0; /* code bits to drop for sub-table */
2140 var left
= 0; /* number of prefix codes available */
2141 var used
= 0; /* code entries in table used */
2142 var huff
= 0; /* Huffman code */
2143 var incr
; /* for incrementing code, index */
2144 var fill
; /* index for replicating entries */
2145 var low
; /* low bits for current root entry */
2146 var mask
; /* mask for low root bits */
2147 var next
; /* next available space in table */
2148 var base
= null; /* base value table to use */
2150 // var shoextra; /* extra bits table to use */
2151 var end
; /* use base and extra for symbol > end */
2152 var count
= new utils
.Buf16(MAXBITS
+ 1); //[MAXBITS+1]; /* number of codes of each length */
2153 var offs
= new utils
.Buf16(MAXBITS
+ 1); //[MAXBITS+1]; /* offsets in table for each length */
2155 var extra_index
= 0;
2157 var here_bits
, here_op
, here_val
;
2160 Process a set of code lengths to create a canonical Huffman code. The
2161 code lengths are lens[0..codes-1]. Each length corresponds to the
2162 symbols 0..codes-1. The Huffman code is generated by first sorting the
2163 symbols by length from short to long, and retaining the symbol order
2164 for codes with equal lengths. Then the code starts with all zero bits
2165 for the first code of the shortest length, and the codes are integer
2166 increments for the same length, and zeros are appended as the length
2167 increases. For the deflate format, these bits are stored backwards
2168 from their more natural integer increment ordering, and so when the
2169 decoding tables are built in the large loop below, the integer codes
2170 are incremented backwards.
2172 This routine assumes, but does not check, that all of the entries in
2173 lens[] are in the range 0..MAXBITS. The caller must assure this.
2174 1..MAXBITS is interpreted as that code length. zero means that that
2175 symbol does not occur in this code.
2177 The codes are sorted by computing a count of codes for each length,
2178 creating from that a table of starting indices for each length in the
2179 sorted table, and then entering the symbols in order in the sorted
2180 table. The sorted table is work[], with that space being provided by
2183 The length counts are used for other purposes as well, i.e. finding
2184 the minimum and maximum length codes, determining if there are any
2185 codes at all, checking for a valid set of lengths, and looking ahead
2186 at length counts to determine sub-table sizes when building the
2190 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
2191 for (len
= 0; len
<= MAXBITS
; len
++) {
2194 for (sym
= 0; sym
< codes
; sym
++) {
2195 count
[lens
[lens_index
+ sym
]]++;
2198 /* bound code lengths, force root to be within code lengths */
2200 for (max
= MAXBITS
; max
>= 1; max
--) {
2201 if (count
[max
] !== 0) { break; }
2206 if (max
=== 0) { /* no symbols to code at all */
2207 //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
2208 //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
2209 //table.val[opts.table_index++] = 0; //here.val = (var short)0;
2210 table
[table_index
++] = (1 << 24) | (64 << 16) | 0;
2213 //table.op[opts.table_index] = 64;
2214 //table.bits[opts.table_index] = 1;
2215 //table.val[opts.table_index++] = 0;
2216 table
[table_index
++] = (1 << 24) | (64 << 16) | 0;
2219 return 0; /* no symbols, but wait for decoding to report error */
2221 for (min
= 1; min
< max
; min
++) {
2222 if (count
[min
] !== 0) { break; }
2228 /* check for an over-subscribed or incomplete set of lengths */
2230 for (len
= 1; len
<= MAXBITS
; len
++) {
2235 } /* over-subscribed */
2237 if (left
> 0 && (type
=== CODES
|| max
!== 1)) {
2238 return -1; /* incomplete set */
2241 /* generate offsets into symbol table for each length for sorting */
2243 for (len
= 1; len
< MAXBITS
; len
++) {
2244 offs
[len
+ 1] = offs
[len
] + count
[len
];
2247 /* sort symbols by length, by symbol order within each length */
2248 for (sym
= 0; sym
< codes
; sym
++) {
2249 if (lens
[lens_index
+ sym
] !== 0) {
2250 work
[offs
[lens
[lens_index
+ sym
]]++] = sym
;
2255 Create and fill in decoding tables. In this loop, the table being
2256 filled is at next and has curr index bits. The code being used is huff
2257 with length len. That code is converted to an index by dropping drop
2258 bits off of the bottom. For codes where len is less than drop + curr,
2259 those top drop + curr - len bits are incremented through all values to
2260 fill the table with replicated entries.
2262 root is the number of index bits for the root table. When len exceeds
2263 root, sub-tables are created pointed to by the root entry with an index
2264 of the low root bits of huff. This is saved in low to check for when a
2265 new sub-table should be started. drop is zero when the root table is
2266 being filled, and drop is root when sub-tables are being filled.
2268 When a new sub-table is needed, it is necessary to look ahead in the
2269 code lengths to determine what size sub-table is needed. The length
2270 counts are used for this, and so count[] is decremented as codes are
2271 entered in the tables.
2273 used keeps track of how many table entries have been allocated from the
2274 provided *table space. It is checked for LENS and DIST tables against
2275 the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
2276 the initial root table size constants. See the comments in inftrees.h
2277 for more information.
2279 sym increments through all symbols, and the loop terminates when
2280 all codes of length max, i.e. all codes, have been processed. This
2281 routine permits incomplete codes, so another loop after this one fills
2282 in the rest of the decoding tables with invalid code markers.
2285 /* set up for code type */
2286 // poor man optimization - use if-else instead of switch,
2287 // to avoid deopts in old v8
2288 if (type
=== CODES
) {
2289 base
= extra
= work
; /* dummy value--not used */
2292 } else if (type
=== LENS
) {
2299 } else { /* DISTS */
2305 /* initialize opts for loop */
2306 huff
= 0; /* starting code */
2307 sym
= 0; /* starting code symbol */
2308 len
= min
; /* starting code length */
2309 next
= table_index
; /* current table to fill in */
2310 curr
= root
; /* current table index bits */
2311 drop
= 0; /* current bits to drop from code for index */
2312 low
= -1; /* trigger new sub-table when len > root */
2313 used
= 1 << root
; /* use root table entries */
2314 mask
= used
- 1; /* mask for comparing low */
2316 /* check available table space */
2317 if ((type
=== LENS
&& used
> ENOUGH_LENS
) ||
2318 (type
=== DISTS
&& used
> ENOUGH_DISTS
)) {
2323 /* process all codes and make table entries */
2326 /* create table entry */
2327 here_bits
= len
- drop
;
2328 if (work
[sym
] < end
) {
2330 here_val
= work
[sym
];
2332 else if (work
[sym
] > end
) {
2333 here_op
= extra
[extra_index
+ work
[sym
]];
2334 here_val
= base
[base_index
+ work
[sym
]];
2337 here_op
= 32 + 64; /* end of block */
2341 /* replicate for those indices with low len bits equal to huff */
2342 incr
= 1 << (len
- drop
);
2344 min
= fill
; /* save offset to next table */
2347 table
[next
+ (huff
>> drop
) + fill
] = (here_bits
<< 24) | (here_op
<< 16) | here_val
|0;
2348 } while (fill
!== 0);
2350 /* backwards increment the len-bit code huff */
2351 incr
= 1 << (len
- 1);
2352 while (huff
& incr
) {
2362 /* go to next symbol, update count, len */
2364 if (--count
[len
] === 0) {
2365 if (len
=== max
) { break; }
2366 len
= lens
[lens_index
+ work
[sym
]];
2369 /* create new sub-table if needed */
2370 if (len
> root
&& (huff
& mask
) !== low
) {
2371 /* if first time, transition to sub-tables */
2376 /* increment past last table */
2377 next
+= min
; /* here min is 1 << curr */
2379 /* determine length of next table */
2382 while (curr
+ drop
< max
) {
2383 left
-= count
[curr
+ drop
];
2384 if (left
<= 0) { break; }
2389 /* check for enough space */
2391 if ((type
=== LENS
&& used
> ENOUGH_LENS
) ||
2392 (type
=== DISTS
&& used
> ENOUGH_DISTS
)) {
2396 /* point entry in root table to sub-table */
2398 /*table.op[low] = curr;
2399 table.bits[low] = root;
2400 table.val[low] = next - opts.table_index;*/
2401 table
[low
] = (root
<< 24) | (curr
<< 16) | (next
- table_index
) |0;
2405 /* fill in remaining table entry if code is incomplete (guaranteed to have
2406 at most one remaining entry, since if the code is incomplete, the
2407 maximum code length that was allowed to get this far is one bit) */
2409 //table.op[next + huff] = 64; /* invalid code marker */
2410 //table.bits[next + huff] = len - drop;
2411 //table.val[next + huff] = 0;
2412 table
[next
+ huff
] = ((len
- drop
) << 24) | (64 << 16) |0;
2415 /* set return parameters */
2416 //opts.table_index += used;
2421 },{"../utils/common":2}],8:[function(require
,module
,exports
){
2425 function ZStream() {
2426 /* next input byte */
2427 this.input
= null; // JS specific, because we have no pointers
2429 /* number of bytes available at input */
2431 /* total number of input bytes read so far */
2433 /* next output byte should be put there */
2434 this.output
= null; // JS specific, because we have no pointers
2436 /* remaining free space at output */
2438 /* total number of bytes output so far */
2440 /* last error message, NULL if no error */
2441 this.msg
= ''/*Z_NULL*/;
2442 /* not visible by applications */
2444 /* best guess about the data type: binary or text */
2445 this.data_type
= 2/*Z_UNKNOWN*/;
2446 /* adler32 value of the uncompressed data */
2450 module
.exports
= ZStream
;