]> git.proxmox.com Git - mirror_novnc.git/blob - include/inflator.js
WIP: Switch to Pako for zlib
[mirror_novnc.git] / 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){
2 'use strict';
3
4
5 var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
6 (typeof Uint16Array !== 'undefined') &&
7 (typeof Int32Array !== 'undefined');
8
9
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; }
15
16 if (typeof source !== 'object') {
17 throw new TypeError(source + 'must be non-object');
18 }
19
20 for (var p in source) {
21 if (source.hasOwnProperty(p)) {
22 obj[p] = source[p];
23 }
24 }
25 }
26
27 return obj;
28 };
29
30
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); }
35 buf.length = size;
36 return buf;
37 };
38
39
40 var fnTyped = {
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);
44 return;
45 }
46 // Fallback to ordinary array
47 for (var i=0; i<len; i++) {
48 dest[dest_offs + i] = src[src_offs + i];
49 }
50 },
51 // Join array of chunks to single array.
52 flattenChunks: function(chunks) {
53 var i, l, len, pos, chunk, result;
54
55 // calculate data length
56 len = 0;
57 for (i=0, l=chunks.length; i<l; i++) {
58 len += chunks[i].length;
59 }
60
61 // join chunks
62 result = new Uint8Array(len);
63 pos = 0;
64 for (i=0, l=chunks.length; i<l; i++) {
65 chunk = chunks[i];
66 result.set(chunk, pos);
67 pos += chunk.length;
68 }
69
70 return result;
71 }
72 };
73
74 var fnUntyped = {
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];
78 }
79 },
80 // Join array of chunks to single array.
81 flattenChunks: function(chunks) {
82 return [].concat.apply([], chunks);
83 }
84 };
85
86
87 // Enable/Disable typed arrays use, for testing
88 //
89 exports.setTyped = function (on) {
90 if (on) {
91 exports.Buf8 = Uint8Array;
92 exports.Buf16 = Uint16Array;
93 exports.Buf32 = Int32Array;
94 exports.assign(exports, fnTyped);
95 } else {
96 exports.Buf8 = Array;
97 exports.Buf16 = Array;
98 exports.Buf32 = Array;
99 exports.assign(exports, fnUntyped);
100 }
101 };
102
103 exports.setTyped(TYPED_OK);
104
105 },{}],2:[function(require,module,exports){
106 'use strict';
107
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.
111
112 function adler32(adler, buf, len, pos) {
113 var s1 = (adler & 0xffff) |0,
114 s2 = ((adler >>> 16) & 0xffff) |0,
115 n = 0;
116
117 while (len !== 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;
122 len -= n;
123
124 do {
125 s1 = (s1 + buf[pos++]) |0;
126 s2 = (s2 + s1) |0;
127 } while (--n);
128
129 s1 %= 65521;
130 s2 %= 65521;
131 }
132
133 return (s1 | (s2 << 16)) |0;
134 }
135
136
137 module.exports = adler32;
138
139 },{}],3:[function(require,module,exports){
140 'use strict';
141
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.
145
146
147 // Use ordinary array, since untyped makes no boost here
148 function makeTable() {
149 var c, table = [];
150
151 for (var n =0; n < 256; n++) {
152 c = n;
153 for (var k =0; k < 8; k++) {
154 c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
155 }
156 table[n] = c;
157 }
158
159 return table;
160 }
161
162 // Create table on load. Just 255 signed longs. Not a problem.
163 var crcTable = makeTable();
164
165
166 function crc32(crc, buf, len, pos) {
167 var t = crcTable,
168 end = pos + len;
169
170 crc = crc ^ (-1);
171
172 for (var i = pos; i < end; i++) {
173 crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
174 }
175
176 return (crc ^ (-1)); // >>> 0;
177 }
178
179
180 module.exports = crc32;
181
182 },{}],4:[function(require,module,exports){
183 'use strict';
184
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 */
188
189 /*
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.
196
197 Entry assumptions:
198
199 state.mode === LEN
200 strm.avail_in >= 6
201 strm.avail_out >= 258
202 start >= strm.avail_out
203 state.bits < 8
204
205 On return, state.mode is one of:
206
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
210
211 Notes:
212
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.
218
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
222 output space.
223 */
224 module.exports = function inflate_fast(strm, start) {
225 var state;
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 */
233 //#endif
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 */
250 var from_source;
251
252
253 var input, output; // JS specific, because we have no pointers
254
255 /* copy state to local variables */
256 state = strm.state;
257 //here = state.here;
258 _in = strm.next_in;
259 input = strm.input;
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
266 dmax = state.dmax;
267 //#endif
268 wsize = state.wsize;
269 whave = state.whave;
270 wnext = state.wnext;
271 window = state.window;
272 hold = state.hold;
273 bits = state.bits;
274 lcode = state.lencode;
275 dcode = state.distcode;
276 lmask = (1 << state.lenbits) - 1;
277 dmask = (1 << state.distbits) - 1;
278
279
280 /* decode literals and length/distances until end-of-block or not enough
281 input data or output space */
282
283 top:
284 do {
285 if (bits < 15) {
286 hold += input[_in++] << bits;
287 bits += 8;
288 hold += input[_in++] << bits;
289 bits += 8;
290 }
291
292 here = lcode[hold & lmask];
293
294 dolen:
295 for (;;) { // Goto emulation
296 op = here >>> 24/*here.bits*/;
297 hold >>>= op;
298 bits -= op;
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*/;
305 }
306 else if (op & 16) { /* length base */
307 len = here & 0xffff/*here.val*/;
308 op &= 15; /* number of extra bits */
309 if (op) {
310 if (bits < op) {
311 hold += input[_in++] << bits;
312 bits += 8;
313 }
314 len += hold & ((1 << op) - 1);
315 hold >>>= op;
316 bits -= op;
317 }
318 //Tracevv((stderr, "inflate: length %u\n", len));
319 if (bits < 15) {
320 hold += input[_in++] << bits;
321 bits += 8;
322 hold += input[_in++] << bits;
323 bits += 8;
324 }
325 here = dcode[hold & dmask];
326
327 dodist:
328 for (;;) { // goto emulation
329 op = here >>> 24/*here.bits*/;
330 hold >>>= op;
331 bits -= op;
332 op = (here >>> 16) & 0xff/*here.op*/;
333
334 if (op & 16) { /* distance base */
335 dist = here & 0xffff/*here.val*/;
336 op &= 15; /* number of extra bits */
337 if (bits < op) {
338 hold += input[_in++] << bits;
339 bits += 8;
340 if (bits < op) {
341 hold += input[_in++] << bits;
342 bits += 8;
343 }
344 }
345 dist += hold & ((1 << op) - 1);
346 //#ifdef INFLATE_STRICT
347 if (dist > dmax) {
348 strm.msg = 'invalid distance too far back';
349 state.mode = BAD;
350 break top;
351 }
352 //#endif
353 hold >>>= op;
354 bits -= op;
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 */
359 if (op > whave) {
360 if (state.sane) {
361 strm.msg = 'invalid distance too far back';
362 state.mode = BAD;
363 break top;
364 }
365
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) {
370 // do {
371 // output[_out++] = 0;
372 // } while (--len);
373 // continue top;
374 // }
375 // len -= op - whave;
376 // do {
377 // output[_out++] = 0;
378 // } while (--op > whave);
379 // if (op === 0) {
380 // from = _out - dist;
381 // do {
382 // output[_out++] = output[from++];
383 // } while (--len);
384 // continue top;
385 // }
386 //#endif
387 }
388 from = 0; // window index
389 from_source = window;
390 if (wnext === 0) { /* very common case */
391 from += wsize - op;
392 if (op < len) { /* some from window */
393 len -= op;
394 do {
395 output[_out++] = window[from++];
396 } while (--op);
397 from = _out - dist; /* rest from output */
398 from_source = output;
399 }
400 }
401 else if (wnext < op) { /* wrap around window */
402 from += wsize + wnext - op;
403 op -= wnext;
404 if (op < len) { /* some from end of window */
405 len -= op;
406 do {
407 output[_out++] = window[from++];
408 } while (--op);
409 from = 0;
410 if (wnext < len) { /* some from start of window */
411 op = wnext;
412 len -= op;
413 do {
414 output[_out++] = window[from++];
415 } while (--op);
416 from = _out - dist; /* rest from output */
417 from_source = output;
418 }
419 }
420 }
421 else { /* contiguous in window */
422 from += wnext - op;
423 if (op < len) { /* some from window */
424 len -= op;
425 do {
426 output[_out++] = window[from++];
427 } while (--op);
428 from = _out - dist; /* rest from output */
429 from_source = output;
430 }
431 }
432 while (len > 2) {
433 output[_out++] = from_source[from++];
434 output[_out++] = from_source[from++];
435 output[_out++] = from_source[from++];
436 len -= 3;
437 }
438 if (len) {
439 output[_out++] = from_source[from++];
440 if (len > 1) {
441 output[_out++] = from_source[from++];
442 }
443 }
444 }
445 else {
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++];
451 len -= 3;
452 } while (len > 2);
453 if (len) {
454 output[_out++] = output[from++];
455 if (len > 1) {
456 output[_out++] = output[from++];
457 }
458 }
459 }
460 }
461 else if ((op & 64) === 0) { /* 2nd level distance code */
462 here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
463 continue dodist;
464 }
465 else {
466 strm.msg = 'invalid distance code';
467 state.mode = BAD;
468 break top;
469 }
470
471 break; // need to emulate goto via "continue"
472 }
473 }
474 else if ((op & 64) === 0) { /* 2nd level length code */
475 here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
476 continue dolen;
477 }
478 else if (op & 32) { /* end-of-block */
479 //Tracevv((stderr, "inflate: end of block\n"));
480 state.mode = TYPE;
481 break top;
482 }
483 else {
484 strm.msg = 'invalid literal/length code';
485 state.mode = BAD;
486 break top;
487 }
488
489 break; // need to emulate goto via "continue"
490 }
491 } while (_in < last && _out < end);
492
493 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
494 len = bits >> 3;
495 _in -= len;
496 bits -= len << 3;
497 hold &= (1 << bits) - 1;
498
499 /* update state and return */
500 strm.next_in = _in;
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));
504 state.hold = hold;
505 state.bits = bits;
506 return;
507 };
508
509 },{}],5:[function(require,module,exports){
510 'use strict';
511
512
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');
518
519 var CODES = 0;
520 var LENS = 1;
521 var DISTS = 2;
522
523 /* Public constants ==========================================================*/
524 /* ===========================================================================*/
525
526
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;
532 var Z_FINISH = 4;
533 var Z_BLOCK = 5;
534 var Z_TREES = 6;
535
536
537 /* Return codes for the compression/decompression functions. Negative values
538 * are errors, positive values are used for special but normal events.
539 */
540 var Z_OK = 0;
541 var Z_STREAM_END = 1;
542 var Z_NEED_DICT = 2;
543 //var Z_ERRNO = -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;
549
550 /* The deflate compression method */
551 var Z_DEFLATED = 8;
552
553
554 /* STATES ====================================================================*/
555 /* ===========================================================================*/
556
557
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() */
590
591 /* ===========================================================================*/
592
593
594
595 var ENOUGH_LENS = 852;
596 var ENOUGH_DISTS = 592;
597 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
598
599 var MAX_WBITS = 15;
600 /* 32K LZ77 window */
601 var DEF_WBITS = MAX_WBITS;
602
603
604 function ZSWAP32(q) {
605 return (((q >>> 24) & 0xff) +
606 ((q >>> 8) & 0xff00) +
607 ((q & 0xff00) << 8) +
608 ((q & 0xff) << 24));
609 }
610
611
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 */
621 // TODO: may be {}
622 this.head = null; /* where to save gzip header information */
623
624 /* sliding window */
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 */
630
631 /* bit accumulator */
632 this.hold = 0; /* input bit accumulator */
633 this.bits = 0; /* number of bits in "in" */
634
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 */
638
639 /* for table and code decoding */
640 this.extra = 0; /* extra bits needed */
641
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 */
647
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[] */
654
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 */
657
658 /*
659 because we don't have pointers in js, we use lencode and distcode directly
660 as buffers so we don't need codes
661 */
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 */
668 }
669
670 function inflateResetKeep(strm) {
671 var state;
672
673 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
674 state = strm.state;
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;
679 }
680 state.mode = HEAD;
681 state.last = 0;
682 state.havedict = 0;
683 state.dmax = 32768;
684 state.head = null/*Z_NULL*/;
685 state.hold = 0;
686 state.bits = 0;
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);
690
691 state.sane = 1;
692 state.back = -1;
693 //Tracev((stderr, "inflate: reset\n"));
694 return Z_OK;
695 }
696
697 function inflateReset(strm) {
698 var state;
699
700 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
701 state = strm.state;
702 state.wsize = 0;
703 state.whave = 0;
704 state.wnext = 0;
705 return inflateResetKeep(strm);
706
707 }
708
709 function inflateReset2(strm, windowBits) {
710 var wrap;
711 var state;
712
713 /* get the state */
714 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
715 state = strm.state;
716
717 /* extract wrap request from windowBits parameter */
718 if (windowBits < 0) {
719 wrap = 0;
720 windowBits = -windowBits;
721 }
722 else {
723 wrap = (windowBits >> 4) + 1;
724 if (windowBits < 48) {
725 windowBits &= 15;
726 }
727 }
728
729 /* set number of window bits, free window if different */
730 if (windowBits && (windowBits < 8 || windowBits > 15)) {
731 return Z_STREAM_ERROR;
732 }
733 if (state.window !== null && state.wbits !== windowBits) {
734 state.window = null;
735 }
736
737 /* update state and reset the rest of it */
738 state.wrap = wrap;
739 state.wbits = windowBits;
740 return inflateReset(strm);
741 }
742
743 function inflateInit2(strm, windowBits) {
744 var ret;
745 var state;
746
747 if (!strm) { return Z_STREAM_ERROR; }
748 //strm.msg = Z_NULL; /* in case we return an error */
749
750 state = new InflateState();
751
752 //if (state === Z_NULL) return Z_MEM_ERROR;
753 //Tracev((stderr, "inflate: allocated\n"));
754 strm.state = state;
755 state.window = null/*Z_NULL*/;
756 ret = inflateReset2(strm, windowBits);
757 if (ret !== Z_OK) {
758 strm.state = null/*Z_NULL*/;
759 }
760 return ret;
761 }
762
763 function inflateInit(strm) {
764 return inflateInit2(strm, DEF_WBITS);
765 }
766
767
768 /*
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.
777 */
778 var virgin = true;
779
780 var lenfix, distfix; // We have no pointers in JS, so keep tables separate
781
782 function fixedtables(state) {
783 /* build fixed huffman tables if first call (may not be thread safe) */
784 if (virgin) {
785 var sym;
786
787 lenfix = new utils.Buf32(512);
788 distfix = new utils.Buf32(32);
789
790 /* literal/length table */
791 sym = 0;
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; }
796
797 inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, {bits: 9});
798
799 /* distance table */
800 sym = 0;
801 while (sym < 32) { state.lens[sym++] = 5; }
802
803 inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, {bits: 5});
804
805 /* do this just once */
806 virgin = false;
807 }
808
809 state.lencode = lenfix;
810 state.lenbits = 9;
811 state.distcode = distfix;
812 state.distbits = 5;
813 }
814
815
816 /*
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
822 is loaded.
823
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.
829 */
830 function updatewindow(strm, src, end, copy) {
831 var dist;
832 var state = strm.state;
833
834 /* if it hasn't been done already, allocate space for the window */
835 if (state.window === null) {
836 state.wsize = 1 << state.wbits;
837 state.wnext = 0;
838 state.whave = 0;
839
840 state.window = new utils.Buf8(state.wsize);
841 }
842
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);
846 state.wnext = 0;
847 state.whave = state.wsize;
848 }
849 else {
850 dist = state.wsize - state.wnext;
851 if (dist > copy) {
852 dist = copy;
853 }
854 //zmemcpy(state->window + state->wnext, end - copy, dist);
855 utils.arraySet(state.window,src, end - copy, dist, state.wnext);
856 copy -= dist;
857 if (copy) {
858 //zmemcpy(state->window, end - copy, copy);
859 utils.arraySet(state.window,src, end - copy, copy, 0);
860 state.wnext = copy;
861 state.whave = state.wsize;
862 }
863 else {
864 state.wnext += dist;
865 if (state.wnext === state.wsize) { state.wnext = 0; }
866 if (state.whave < state.wsize) { state.whave += dist; }
867 }
868 }
869 return 0;
870 }
871
872 function inflate(strm, flush) {
873 var state;
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 */
883 var from_source;
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 */
891 var opts;
892
893 var n; // temporary var for NEED_BITS
894
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];
897
898
899 if (!strm || !strm.state || !strm.output ||
900 (!strm.input && strm.avail_in !== 0)) {
901 return Z_STREAM_ERROR;
902 }
903
904 state = strm.state;
905 if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
906
907
908 //--- LOAD() ---
909 put = strm.next_out;
910 output = strm.output;
911 left = strm.avail_out;
912 next = strm.next_in;
913 input = strm.input;
914 have = strm.avail_in;
915 hold = state.hold;
916 bits = state.bits;
917 //---
918
919 _in = have;
920 _out = left;
921 ret = Z_OK;
922
923 inf_leave: // goto emulation
924 for (;;) {
925 switch (state.mode) {
926 case HEAD:
927 if (state.wrap === 0) {
928 state.mode = TYPEDO;
929 break;
930 }
931 //=== NEEDBITS(16);
932 while (bits < 16) {
933 if (have === 0) { break inf_leave; }
934 have--;
935 hold += input[next++] << bits;
936 bits += 8;
937 }
938 //===//
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);
945 //===//
946
947 //=== INITBITS();
948 hold = 0;
949 bits = 0;
950 //===//
951 state.mode = FLAGS;
952 break;
953 }
954 state.flags = 0; /* expect zlib header */
955 if (state.head) {
956 state.head.done = false;
957 }
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';
961 state.mode = BAD;
962 break;
963 }
964 if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
965 strm.msg = 'unknown compression method';
966 state.mode = BAD;
967 break;
968 }
969 //--- DROPBITS(4) ---//
970 hold >>>= 4;
971 bits -= 4;
972 //---//
973 len = (hold & 0x0f)/*BITS(4)*/ + 8;
974 if (state.wbits === 0) {
975 state.wbits = len;
976 }
977 else if (len > state.wbits) {
978 strm.msg = 'invalid window size';
979 state.mode = BAD;
980 break;
981 }
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;
986 //=== INITBITS();
987 hold = 0;
988 bits = 0;
989 //===//
990 break;
991 case FLAGS:
992 //=== NEEDBITS(16); */
993 while (bits < 16) {
994 if (have === 0) { break inf_leave; }
995 have--;
996 hold += input[next++] << bits;
997 bits += 8;
998 }
999 //===//
1000 state.flags = hold;
1001 if ((state.flags & 0xff) !== Z_DEFLATED) {
1002 strm.msg = 'unknown compression method';
1003 state.mode = BAD;
1004 break;
1005 }
1006 if (state.flags & 0xe000) {
1007 strm.msg = 'unknown header flags set';
1008 state.mode = BAD;
1009 break;
1010 }
1011 if (state.head) {
1012 state.head.text = ((hold >> 8) & 1);
1013 }
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);
1019 //===//
1020 }
1021 //=== INITBITS();
1022 hold = 0;
1023 bits = 0;
1024 //===//
1025 state.mode = TIME;
1026 /* falls through */
1027 case TIME:
1028 //=== NEEDBITS(32); */
1029 while (bits < 32) {
1030 if (have === 0) { break inf_leave; }
1031 have--;
1032 hold += input[next++] << bits;
1033 bits += 8;
1034 }
1035 //===//
1036 if (state.head) {
1037 state.head.time = hold;
1038 }
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);
1046 //===
1047 }
1048 //=== INITBITS();
1049 hold = 0;
1050 bits = 0;
1051 //===//
1052 state.mode = OS;
1053 /* falls through */
1054 case OS:
1055 //=== NEEDBITS(16); */
1056 while (bits < 16) {
1057 if (have === 0) { break inf_leave; }
1058 have--;
1059 hold += input[next++] << bits;
1060 bits += 8;
1061 }
1062 //===//
1063 if (state.head) {
1064 state.head.xflags = (hold & 0xff);
1065 state.head.os = (hold >> 8);
1066 }
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);
1072 //===//
1073 }
1074 //=== INITBITS();
1075 hold = 0;
1076 bits = 0;
1077 //===//
1078 state.mode = EXLEN;
1079 /* falls through */
1080 case EXLEN:
1081 if (state.flags & 0x0400) {
1082 //=== NEEDBITS(16); */
1083 while (bits < 16) {
1084 if (have === 0) { break inf_leave; }
1085 have--;
1086 hold += input[next++] << bits;
1087 bits += 8;
1088 }
1089 //===//
1090 state.length = hold;
1091 if (state.head) {
1092 state.head.extra_len = hold;
1093 }
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);
1099 //===//
1100 }
1101 //=== INITBITS();
1102 hold = 0;
1103 bits = 0;
1104 //===//
1105 }
1106 else if (state.head) {
1107 state.head.extra = null/*Z_NULL*/;
1108 }
1109 state.mode = EXTRA;
1110 /* falls through */
1111 case EXTRA:
1112 if (state.flags & 0x0400) {
1113 copy = state.length;
1114 if (copy > have) { copy = have; }
1115 if (copy) {
1116 if (state.head) {
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);
1121 }
1122 utils.arraySet(
1123 state.head.extra,
1124 input,
1125 next,
1126 // extra field is limited to 65536 bytes
1127 // - no need for additional size check
1128 copy,
1129 /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
1130 len
1131 );
1132 //zmemcpy(state.head.extra + len, next,
1133 // len + copy > state.head.extra_max ?
1134 // state.head.extra_max - len : copy);
1135 }
1136 if (state.flags & 0x0200) {
1137 state.check = crc32(state.check, input, copy, next);
1138 }
1139 have -= copy;
1140 next += copy;
1141 state.length -= copy;
1142 }
1143 if (state.length) { break inf_leave; }
1144 }
1145 state.length = 0;
1146 state.mode = NAME;
1147 /* falls through */
1148 case NAME:
1149 if (state.flags & 0x0800) {
1150 if (have === 0) { break inf_leave; }
1151 copy = 0;
1152 do {
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);
1159 }
1160 } while (len && copy < have);
1161
1162 if (state.flags & 0x0200) {
1163 state.check = crc32(state.check, input, copy, next);
1164 }
1165 have -= copy;
1166 next += copy;
1167 if (len) { break inf_leave; }
1168 }
1169 else if (state.head) {
1170 state.head.name = null;
1171 }
1172 state.length = 0;
1173 state.mode = COMMENT;
1174 /* falls through */
1175 case COMMENT:
1176 if (state.flags & 0x1000) {
1177 if (have === 0) { break inf_leave; }
1178 copy = 0;
1179 do {
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);
1185 }
1186 } while (len && copy < have);
1187 if (state.flags & 0x0200) {
1188 state.check = crc32(state.check, input, copy, next);
1189 }
1190 have -= copy;
1191 next += copy;
1192 if (len) { break inf_leave; }
1193 }
1194 else if (state.head) {
1195 state.head.comment = null;
1196 }
1197 state.mode = HCRC;
1198 /* falls through */
1199 case HCRC:
1200 if (state.flags & 0x0200) {
1201 //=== NEEDBITS(16); */
1202 while (bits < 16) {
1203 if (have === 0) { break inf_leave; }
1204 have--;
1205 hold += input[next++] << bits;
1206 bits += 8;
1207 }
1208 //===//
1209 if (hold !== (state.check & 0xffff)) {
1210 strm.msg = 'header crc mismatch';
1211 state.mode = BAD;
1212 break;
1213 }
1214 //=== INITBITS();
1215 hold = 0;
1216 bits = 0;
1217 //===//
1218 }
1219 if (state.head) {
1220 state.head.hcrc = ((state.flags >> 9) & 1);
1221 state.head.done = true;
1222 }
1223 strm.adler = state.check = 0 /*crc32(0L, Z_NULL, 0)*/;
1224 state.mode = TYPE;
1225 break;
1226 case DICTID:
1227 //=== NEEDBITS(32); */
1228 while (bits < 32) {
1229 if (have === 0) { break inf_leave; }
1230 have--;
1231 hold += input[next++] << bits;
1232 bits += 8;
1233 }
1234 //===//
1235 strm.adler = state.check = ZSWAP32(hold);
1236 //=== INITBITS();
1237 hold = 0;
1238 bits = 0;
1239 //===//
1240 state.mode = DICT;
1241 /* falls through */
1242 case DICT:
1243 if (state.havedict === 0) {
1244 //--- RESTORE() ---
1245 strm.next_out = put;
1246 strm.avail_out = left;
1247 strm.next_in = next;
1248 strm.avail_in = have;
1249 state.hold = hold;
1250 state.bits = bits;
1251 //---
1252 return Z_NEED_DICT;
1253 }
1254 strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
1255 state.mode = TYPE;
1256 /* falls through */
1257 case TYPE:
1258 if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
1259 /* falls through */
1260 case TYPEDO:
1261 if (state.last) {
1262 //--- BYTEBITS() ---//
1263 hold >>>= bits & 7;
1264 bits -= bits & 7;
1265 //---//
1266 state.mode = CHECK;
1267 break;
1268 }
1269 //=== NEEDBITS(3); */
1270 while (bits < 3) {
1271 if (have === 0) { break inf_leave; }
1272 have--;
1273 hold += input[next++] << bits;
1274 bits += 8;
1275 }
1276 //===//
1277 state.last = (hold & 0x01)/*BITS(1)*/;
1278 //--- DROPBITS(1) ---//
1279 hold >>>= 1;
1280 bits -= 1;
1281 //---//
1282
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;
1288 break;
1289 case 1: /* fixed block */
1290 fixedtables(state);
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) ---//
1296 hold >>>= 2;
1297 bits -= 2;
1298 //---//
1299 break inf_leave;
1300 }
1301 break;
1302 case 2: /* dynamic block */
1303 //Tracev((stderr, "inflate: dynamic codes block%s\n",
1304 // state.last ? " (last)" : ""));
1305 state.mode = TABLE;
1306 break;
1307 case 3:
1308 strm.msg = 'invalid block type';
1309 state.mode = BAD;
1310 }
1311 //--- DROPBITS(2) ---//
1312 hold >>>= 2;
1313 bits -= 2;
1314 //---//
1315 break;
1316 case STORED:
1317 //--- BYTEBITS() ---// /* go to byte boundary */
1318 hold >>>= bits & 7;
1319 bits -= bits & 7;
1320 //---//
1321 //=== NEEDBITS(32); */
1322 while (bits < 32) {
1323 if (have === 0) { break inf_leave; }
1324 have--;
1325 hold += input[next++] << bits;
1326 bits += 8;
1327 }
1328 //===//
1329 if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
1330 strm.msg = 'invalid stored block lengths';
1331 state.mode = BAD;
1332 break;
1333 }
1334 state.length = hold & 0xffff;
1335 //Tracev((stderr, "inflate: stored length %u\n",
1336 // state.length));
1337 //=== INITBITS();
1338 hold = 0;
1339 bits = 0;
1340 //===//
1341 state.mode = COPY_;
1342 if (flush === Z_TREES) { break inf_leave; }
1343 /* falls through */
1344 case COPY_:
1345 state.mode = COPY;
1346 /* falls through */
1347 case COPY:
1348 copy = state.length;
1349 if (copy) {
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);
1355 //---//
1356 have -= copy;
1357 next += copy;
1358 left -= copy;
1359 put += copy;
1360 state.length -= copy;
1361 break;
1362 }
1363 //Tracev((stderr, "inflate: stored end\n"));
1364 state.mode = TYPE;
1365 break;
1366 case TABLE:
1367 //=== NEEDBITS(14); */
1368 while (bits < 14) {
1369 if (have === 0) { break inf_leave; }
1370 have--;
1371 hold += input[next++] << bits;
1372 bits += 8;
1373 }
1374 //===//
1375 state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
1376 //--- DROPBITS(5) ---//
1377 hold >>>= 5;
1378 bits -= 5;
1379 //---//
1380 state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
1381 //--- DROPBITS(5) ---//
1382 hold >>>= 5;
1383 bits -= 5;
1384 //---//
1385 state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
1386 //--- DROPBITS(4) ---//
1387 hold >>>= 4;
1388 bits -= 4;
1389 //---//
1390 //#ifndef PKZIP_BUG_WORKAROUND
1391 if (state.nlen > 286 || state.ndist > 30) {
1392 strm.msg = 'too many length or distance symbols';
1393 state.mode = BAD;
1394 break;
1395 }
1396 //#endif
1397 //Tracev((stderr, "inflate: table sizes ok\n"));
1398 state.have = 0;
1399 state.mode = LENLENS;
1400 /* falls through */
1401 case LENLENS:
1402 while (state.have < state.ncode) {
1403 //=== NEEDBITS(3);
1404 while (bits < 3) {
1405 if (have === 0) { break inf_leave; }
1406 have--;
1407 hold += input[next++] << bits;
1408 bits += 8;
1409 }
1410 //===//
1411 state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
1412 //--- DROPBITS(3) ---//
1413 hold >>>= 3;
1414 bits -= 3;
1415 //---//
1416 }
1417 while (state.have < 19) {
1418 state.lens[order[state.have++]] = 0;
1419 }
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;
1425 state.lenbits = 7;
1426
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;
1430
1431 if (ret) {
1432 strm.msg = 'invalid code lengths set';
1433 state.mode = BAD;
1434 break;
1435 }
1436 //Tracev((stderr, "inflate: code lengths ok\n"));
1437 state.have = 0;
1438 state.mode = CODELENS;
1439 /* falls through */
1440 case CODELENS:
1441 while (state.have < state.nlen + state.ndist) {
1442 for (;;) {
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;
1447
1448 if ((here_bits) <= bits) { break; }
1449 //--- PULLBYTE() ---//
1450 if (have === 0) { break inf_leave; }
1451 have--;
1452 hold += input[next++] << bits;
1453 bits += 8;
1454 //---//
1455 }
1456 if (here_val < 16) {
1457 //--- DROPBITS(here.bits) ---//
1458 hold >>>= here_bits;
1459 bits -= here_bits;
1460 //---//
1461 state.lens[state.have++] = here_val;
1462 }
1463 else {
1464 if (here_val === 16) {
1465 //=== NEEDBITS(here.bits + 2);
1466 n = here_bits + 2;
1467 while (bits < n) {
1468 if (have === 0) { break inf_leave; }
1469 have--;
1470 hold += input[next++] << bits;
1471 bits += 8;
1472 }
1473 //===//
1474 //--- DROPBITS(here.bits) ---//
1475 hold >>>= here_bits;
1476 bits -= here_bits;
1477 //---//
1478 if (state.have === 0) {
1479 strm.msg = 'invalid bit length repeat';
1480 state.mode = BAD;
1481 break;
1482 }
1483 len = state.lens[state.have - 1];
1484 copy = 3 + (hold & 0x03);//BITS(2);
1485 //--- DROPBITS(2) ---//
1486 hold >>>= 2;
1487 bits -= 2;
1488 //---//
1489 }
1490 else if (here_val === 17) {
1491 //=== NEEDBITS(here.bits + 3);
1492 n = here_bits + 3;
1493 while (bits < n) {
1494 if (have === 0) { break inf_leave; }
1495 have--;
1496 hold += input[next++] << bits;
1497 bits += 8;
1498 }
1499 //===//
1500 //--- DROPBITS(here.bits) ---//
1501 hold >>>= here_bits;
1502 bits -= here_bits;
1503 //---//
1504 len = 0;
1505 copy = 3 + (hold & 0x07);//BITS(3);
1506 //--- DROPBITS(3) ---//
1507 hold >>>= 3;
1508 bits -= 3;
1509 //---//
1510 }
1511 else {
1512 //=== NEEDBITS(here.bits + 7);
1513 n = here_bits + 7;
1514 while (bits < n) {
1515 if (have === 0) { break inf_leave; }
1516 have--;
1517 hold += input[next++] << bits;
1518 bits += 8;
1519 }
1520 //===//
1521 //--- DROPBITS(here.bits) ---//
1522 hold >>>= here_bits;
1523 bits -= here_bits;
1524 //---//
1525 len = 0;
1526 copy = 11 + (hold & 0x7f);//BITS(7);
1527 //--- DROPBITS(7) ---//
1528 hold >>>= 7;
1529 bits -= 7;
1530 //---//
1531 }
1532 if (state.have + copy > state.nlen + state.ndist) {
1533 strm.msg = 'invalid bit length repeat';
1534 state.mode = BAD;
1535 break;
1536 }
1537 while (copy--) {
1538 state.lens[state.have++] = len;
1539 }
1540 }
1541 }
1542
1543 /* handle error breaks in while */
1544 if (state.mode === BAD) { break; }
1545
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';
1549 state.mode = BAD;
1550 break;
1551 }
1552
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 */
1556 state.lenbits = 9;
1557
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;
1564
1565 if (ret) {
1566 strm.msg = 'invalid literal/lengths set';
1567 state.mode = BAD;
1568 break;
1569 }
1570
1571 state.distbits = 6;
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;
1581
1582 if (ret) {
1583 strm.msg = 'invalid distances set';
1584 state.mode = BAD;
1585 break;
1586 }
1587 //Tracev((stderr, 'inflate: codes ok\n'));
1588 state.mode = LEN_;
1589 if (flush === Z_TREES) { break inf_leave; }
1590 /* falls through */
1591 case LEN_:
1592 state.mode = LEN;
1593 /* falls through */
1594 case LEN:
1595 if (have >= 6 && left >= 258) {
1596 //--- RESTORE() ---
1597 strm.next_out = put;
1598 strm.avail_out = left;
1599 strm.next_in = next;
1600 strm.avail_in = have;
1601 state.hold = hold;
1602 state.bits = bits;
1603 //---
1604 inflate_fast(strm, _out);
1605 //--- LOAD() ---
1606 put = strm.next_out;
1607 output = strm.output;
1608 left = strm.avail_out;
1609 next = strm.next_in;
1610 input = strm.input;
1611 have = strm.avail_in;
1612 hold = state.hold;
1613 bits = state.bits;
1614 //---
1615
1616 if (state.mode === TYPE) {
1617 state.back = -1;
1618 }
1619 break;
1620 }
1621 state.back = 0;
1622 for (;;) {
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;
1627
1628 if (here_bits <= bits) { break; }
1629 //--- PULLBYTE() ---//
1630 if (have === 0) { break inf_leave; }
1631 have--;
1632 hold += input[next++] << bits;
1633 bits += 8;
1634 //---//
1635 }
1636 if (here_op && (here_op & 0xf0) === 0) {
1637 last_bits = here_bits;
1638 last_op = here_op;
1639 last_val = here_val;
1640 for (;;) {
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;
1646
1647 if ((last_bits + here_bits) <= bits) { break; }
1648 //--- PULLBYTE() ---//
1649 if (have === 0) { break inf_leave; }
1650 have--;
1651 hold += input[next++] << bits;
1652 bits += 8;
1653 //---//
1654 }
1655 //--- DROPBITS(last.bits) ---//
1656 hold >>>= last_bits;
1657 bits -= last_bits;
1658 //---//
1659 state.back += last_bits;
1660 }
1661 //--- DROPBITS(here.bits) ---//
1662 hold >>>= here_bits;
1663 bits -= here_bits;
1664 //---//
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));
1671 state.mode = LIT;
1672 break;
1673 }
1674 if (here_op & 32) {
1675 //Tracevv((stderr, "inflate: end of block\n"));
1676 state.back = -1;
1677 state.mode = TYPE;
1678 break;
1679 }
1680 if (here_op & 64) {
1681 strm.msg = 'invalid literal/length code';
1682 state.mode = BAD;
1683 break;
1684 }
1685 state.extra = here_op & 15;
1686 state.mode = LENEXT;
1687 /* falls through */
1688 case LENEXT:
1689 if (state.extra) {
1690 //=== NEEDBITS(state.extra);
1691 n = state.extra;
1692 while (bits < n) {
1693 if (have === 0) { break inf_leave; }
1694 have--;
1695 hold += input[next++] << bits;
1696 bits += 8;
1697 }
1698 //===//
1699 state.length += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
1700 //--- DROPBITS(state.extra) ---//
1701 hold >>>= state.extra;
1702 bits -= state.extra;
1703 //---//
1704 state.back += state.extra;
1705 }
1706 //Tracevv((stderr, "inflate: length %u\n", state.length));
1707 state.was = state.length;
1708 state.mode = DIST;
1709 /* falls through */
1710 case DIST:
1711 for (;;) {
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;
1716
1717 if ((here_bits) <= bits) { break; }
1718 //--- PULLBYTE() ---//
1719 if (have === 0) { break inf_leave; }
1720 have--;
1721 hold += input[next++] << bits;
1722 bits += 8;
1723 //---//
1724 }
1725 if ((here_op & 0xf0) === 0) {
1726 last_bits = here_bits;
1727 last_op = here_op;
1728 last_val = here_val;
1729 for (;;) {
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;
1735
1736 if ((last_bits + here_bits) <= bits) { break; }
1737 //--- PULLBYTE() ---//
1738 if (have === 0) { break inf_leave; }
1739 have--;
1740 hold += input[next++] << bits;
1741 bits += 8;
1742 //---//
1743 }
1744 //--- DROPBITS(last.bits) ---//
1745 hold >>>= last_bits;
1746 bits -= last_bits;
1747 //---//
1748 state.back += last_bits;
1749 }
1750 //--- DROPBITS(here.bits) ---//
1751 hold >>>= here_bits;
1752 bits -= here_bits;
1753 //---//
1754 state.back += here_bits;
1755 if (here_op & 64) {
1756 strm.msg = 'invalid distance code';
1757 state.mode = BAD;
1758 break;
1759 }
1760 state.offset = here_val;
1761 state.extra = (here_op) & 15;
1762 state.mode = DISTEXT;
1763 /* falls through */
1764 case DISTEXT:
1765 if (state.extra) {
1766 //=== NEEDBITS(state.extra);
1767 n = state.extra;
1768 while (bits < n) {
1769 if (have === 0) { break inf_leave; }
1770 have--;
1771 hold += input[next++] << bits;
1772 bits += 8;
1773 }
1774 //===//
1775 state.offset += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
1776 //--- DROPBITS(state.extra) ---//
1777 hold >>>= state.extra;
1778 bits -= state.extra;
1779 //---//
1780 state.back += state.extra;
1781 }
1782 //#ifdef INFLATE_STRICT
1783 if (state.offset > state.dmax) {
1784 strm.msg = 'invalid distance too far back';
1785 state.mode = BAD;
1786 break;
1787 }
1788 //#endif
1789 //Tracevv((stderr, "inflate: distance %u\n", state.offset));
1790 state.mode = MATCH;
1791 /* falls through */
1792 case MATCH:
1793 if (left === 0) { break inf_leave; }
1794 copy = _out - left;
1795 if (state.offset > copy) { /* copy from window */
1796 copy = state.offset - copy;
1797 if (copy > state.whave) {
1798 if (state.sane) {
1799 strm.msg = 'invalid distance too far back';
1800 state.mode = BAD;
1801 break;
1802 }
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; }
1810 // left -= copy;
1811 // state.length -= copy;
1812 // do {
1813 // output[put++] = 0;
1814 // } while (--copy);
1815 // if (state.length === 0) { state.mode = LEN; }
1816 // break;
1817 //#endif
1818 }
1819 if (copy > state.wnext) {
1820 copy -= state.wnext;
1821 from = state.wsize - copy;
1822 }
1823 else {
1824 from = state.wnext - copy;
1825 }
1826 if (copy > state.length) { copy = state.length; }
1827 from_source = state.window;
1828 }
1829 else { /* copy from output */
1830 from_source = output;
1831 from = put - state.offset;
1832 copy = state.length;
1833 }
1834 if (copy > left) { copy = left; }
1835 left -= copy;
1836 state.length -= copy;
1837 do {
1838 output[put++] = from_source[from++];
1839 } while (--copy);
1840 if (state.length === 0) { state.mode = LEN; }
1841 break;
1842 case LIT:
1843 if (left === 0) { break inf_leave; }
1844 output[put++] = state.length;
1845 left--;
1846 state.mode = LEN;
1847 break;
1848 case CHECK:
1849 if (state.wrap) {
1850 //=== NEEDBITS(32);
1851 while (bits < 32) {
1852 if (have === 0) { break inf_leave; }
1853 have--;
1854 // Use '|' insdead of '+' to make sure that result is signed
1855 hold |= input[next++] << bits;
1856 bits += 8;
1857 }
1858 //===//
1859 _out -= left;
1860 strm.total_out += _out;
1861 state.total += _out;
1862 if (_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));
1866
1867 }
1868 _out = left;
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';
1872 state.mode = BAD;
1873 break;
1874 }
1875 //=== INITBITS();
1876 hold = 0;
1877 bits = 0;
1878 //===//
1879 //Tracev((stderr, "inflate: check matches trailer\n"));
1880 }
1881 state.mode = LENGTH;
1882 /* falls through */
1883 case LENGTH:
1884 if (state.wrap && state.flags) {
1885 //=== NEEDBITS(32);
1886 while (bits < 32) {
1887 if (have === 0) { break inf_leave; }
1888 have--;
1889 hold += input[next++] << bits;
1890 bits += 8;
1891 }
1892 //===//
1893 if (hold !== (state.total & 0xffffffff)) {
1894 strm.msg = 'incorrect length check';
1895 state.mode = BAD;
1896 break;
1897 }
1898 //=== INITBITS();
1899 hold = 0;
1900 bits = 0;
1901 //===//
1902 //Tracev((stderr, "inflate: length matches trailer\n"));
1903 }
1904 state.mode = DONE;
1905 /* falls through */
1906 case DONE:
1907 ret = Z_STREAM_END;
1908 break inf_leave;
1909 case BAD:
1910 ret = Z_DATA_ERROR;
1911 break inf_leave;
1912 case MEM:
1913 return Z_MEM_ERROR;
1914 case SYNC:
1915 /* falls through */
1916 default:
1917 return Z_STREAM_ERROR;
1918 }
1919 }
1920
1921 // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
1922
1923 /*
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.
1928 */
1929
1930 //--- RESTORE() ---
1931 strm.next_out = put;
1932 strm.avail_out = left;
1933 strm.next_in = next;
1934 strm.avail_in = have;
1935 state.hold = hold;
1936 state.bits = bits;
1937 //---
1938
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)) {
1942 state.mode = MEM;
1943 return Z_MEM_ERROR;
1944 }
1945 }
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));
1954 }
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) {
1959 ret = Z_BUF_ERROR;
1960 }
1961 return ret;
1962 }
1963
1964 function inflateEnd(strm) {
1965
1966 if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
1967 return Z_STREAM_ERROR;
1968 }
1969
1970 var state = strm.state;
1971 if (state.window) {
1972 state.window = null;
1973 }
1974 strm.state = null;
1975 return Z_OK;
1976 }
1977
1978 function inflateGetHeader(strm, head) {
1979 var state;
1980
1981 /* check state */
1982 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1983 state = strm.state;
1984 if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
1985
1986 /* save header structure */
1987 state.head = head;
1988 head.done = false;
1989 return Z_OK;
1990 }
1991
1992
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)';
2002
2003 /* Not implemented
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;
2012 */
2013
2014 },{"../utils/common":1,"./adler32":2,"./crc32":3,"./inffast":4,"./inftrees":6}],6:[function(require,module,exports){
2015 'use strict';
2016
2017
2018 var utils = require('../utils/common');
2019
2020 var MAXBITS = 15;
2021 var ENOUGH_LENS = 852;
2022 var ENOUGH_DISTS = 592;
2023 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
2024
2025 var CODES = 0;
2026 var LENS = 1;
2027 var DISTS = 2;
2028
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
2032 ];
2033
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
2037 ];
2038
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
2043 ];
2044
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
2049 ];
2050
2051 module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
2052 {
2053 var bits = opts.bits;
2054 //here = opts.here; /* table entry for duplication */
2055
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 */
2071 var base_index = 0;
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 */
2076 var extra = null;
2077 var extra_index = 0;
2078
2079 var here_bits, here_op, here_val;
2080
2081 /*
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.
2093
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.
2098
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
2103 the caller.
2104
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
2109 decoding tables.
2110 */
2111
2112 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
2113 for (len = 0; len <= MAXBITS; len++) {
2114 count[len] = 0;
2115 }
2116 for (sym = 0; sym < codes; sym++) {
2117 count[lens[lens_index + sym]]++;
2118 }
2119
2120 /* bound code lengths, force root to be within code lengths */
2121 root = bits;
2122 for (max = MAXBITS; max >= 1; max--) {
2123 if (count[max] !== 0) { break; }
2124 }
2125 if (root > max) {
2126 root = max;
2127 }
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;
2133
2134
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;
2139
2140 opts.bits = 1;
2141 return 0; /* no symbols, but wait for decoding to report error */
2142 }
2143 for (min = 1; min < max; min++) {
2144 if (count[min] !== 0) { break; }
2145 }
2146 if (root < min) {
2147 root = min;
2148 }
2149
2150 /* check for an over-subscribed or incomplete set of lengths */
2151 left = 1;
2152 for (len = 1; len <= MAXBITS; len++) {
2153 left <<= 1;
2154 left -= count[len];
2155 if (left < 0) {
2156 return -1;
2157 } /* over-subscribed */
2158 }
2159 if (left > 0 && (type === CODES || max !== 1)) {
2160 return -1; /* incomplete set */
2161 }
2162
2163 /* generate offsets into symbol table for each length for sorting */
2164 offs[1] = 0;
2165 for (len = 1; len < MAXBITS; len++) {
2166 offs[len + 1] = offs[len] + count[len];
2167 }
2168
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;
2173 }
2174 }
2175
2176 /*
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.
2183
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.
2189
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.
2194
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.
2200
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.
2205 */
2206
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 */
2212 end = 19;
2213
2214 } else if (type === LENS) {
2215 base = lbase;
2216 base_index -= 257;
2217 extra = lext;
2218 extra_index -= 257;
2219 end = 256;
2220
2221 } else { /* DISTS */
2222 base = dbase;
2223 extra = dext;
2224 end = -1;
2225 }
2226
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 */
2237
2238 /* check available table space */
2239 if ((type === LENS && used > ENOUGH_LENS) ||
2240 (type === DISTS && used > ENOUGH_DISTS)) {
2241 return 1;
2242 }
2243
2244 var i=0;
2245 /* process all codes and make table entries */
2246 for (;;) {
2247 i++;
2248 /* create table entry */
2249 here_bits = len - drop;
2250 if (work[sym] < end) {
2251 here_op = 0;
2252 here_val = work[sym];
2253 }
2254 else if (work[sym] > end) {
2255 here_op = extra[extra_index + work[sym]];
2256 here_val = base[base_index + work[sym]];
2257 }
2258 else {
2259 here_op = 32 + 64; /* end of block */
2260 here_val = 0;
2261 }
2262
2263 /* replicate for those indices with low len bits equal to huff */
2264 incr = 1 << (len - drop);
2265 fill = 1 << curr;
2266 min = fill; /* save offset to next table */
2267 do {
2268 fill -= incr;
2269 table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
2270 } while (fill !== 0);
2271
2272 /* backwards increment the len-bit code huff */
2273 incr = 1 << (len - 1);
2274 while (huff & incr) {
2275 incr >>= 1;
2276 }
2277 if (incr !== 0) {
2278 huff &= incr - 1;
2279 huff += incr;
2280 } else {
2281 huff = 0;
2282 }
2283
2284 /* go to next symbol, update count, len */
2285 sym++;
2286 if (--count[len] === 0) {
2287 if (len === max) { break; }
2288 len = lens[lens_index + work[sym]];
2289 }
2290
2291 /* create new sub-table if needed */
2292 if (len > root && (huff & mask) !== low) {
2293 /* if first time, transition to sub-tables */
2294 if (drop === 0) {
2295 drop = root;
2296 }
2297
2298 /* increment past last table */
2299 next += min; /* here min is 1 << curr */
2300
2301 /* determine length of next table */
2302 curr = len - drop;
2303 left = 1 << curr;
2304 while (curr + drop < max) {
2305 left -= count[curr + drop];
2306 if (left <= 0) { break; }
2307 curr++;
2308 left <<= 1;
2309 }
2310
2311 /* check for enough space */
2312 used += 1 << curr;
2313 if ((type === LENS && used > ENOUGH_LENS) ||
2314 (type === DISTS && used > ENOUGH_DISTS)) {
2315 return 1;
2316 }
2317
2318 /* point entry in root table to sub-table */
2319 low = huff & mask;
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;
2324 }
2325 }
2326
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) */
2330 if (huff !== 0) {
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;
2335 }
2336
2337 /* set return parameters */
2338 //opts.table_index += used;
2339 opts.bits = root;
2340 return 0;
2341 };
2342
2343 },{"../utils/common":1}],7:[function(require,module,exports){
2344 'use strict';
2345
2346
2347 function ZStream() {
2348 /* next input byte */
2349 this.input = null; // JS specific, because we have no pointers
2350 this.next_in = 0;
2351 /* number of bytes available at input */
2352 this.avail_in = 0;
2353 /* total number of input bytes read so far */
2354 this.total_in = 0;
2355 /* next output byte should be put there */
2356 this.output = null; // JS specific, because we have no pointers
2357 this.next_out = 0;
2358 /* remaining free space at output */
2359 this.avail_out = 0;
2360 /* total number of bytes output so far */
2361 this.total_out = 0;
2362 /* last error message, NULL if no error */
2363 this.msg = ''/*Z_NULL*/;
2364 /* not visible by applications */
2365 this.state = null;
2366 /* best guess about the data type: binary or text */
2367 this.data_type = 2/*Z_UNKNOWN*/;
2368 /* adler32 value of the uncompressed data */
2369 this.adler = 0;
2370 }
2371
2372 module.exports = ZStream;
2373
2374 },{}],"/partial_inflator.js":[function(require,module,exports){
2375 var zlib = require('./lib/zlib/inflate.js');
2376 var ZStream = require('./lib/zlib/zstream.js');
2377
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;
2383
2384 zlib.inflateInit(this.strm, this.windowBits);
2385 };
2386
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;
2393
2394 this.strm.avail_out = this.chunkSize;
2395
2396 zlib.inflate(this.strm, flush);
2397
2398 return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
2399 },
2400
2401 reset: function () {
2402 zlib.inflateReset(this.strm);
2403 }
2404 };
2405
2406 module.exports = {Inflate: Inflate};
2407
2408 },{"./lib/zlib/inflate.js":5,"./lib/zlib/zstream.js":7}]},{},[])("/partial_inflator.js")
2409 });