]> git.proxmox.com Git - mirror_novnc.git/blame - core/inflator.js
Uncomment ES6 module syntax
[mirror_novnc.git] / core / inflator.js
CommitLineData
ae510306
SR
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){
2var zlib = require('pako/lib/zlib/inflate.js');
3var ZStream = require('pako/lib/zlib/zstream.js');
4
5function Inflate() {
6 this.strm = new ZStream();
7 this.chunkSize = 1024 * 10 * 10;
8 this.strm.output = new Uint8Array(this.chunkSize);
9 this.windowBits = 5;
10
11 zlib.inflateInit(this.strm, this.windowBits);
12};
13
14Inflate.prototype = {
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;
20
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);
27 }
28
29 this.strm.avail_out = this.chunkSize;
30
31 zlib.inflate(this.strm, flush);
32
33 return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
34 },
35
36 reset: function () {
37 zlib.inflateReset(this.strm);
38 }
39};
40
41module.exports = { Inflate: Inflate };
42
43},{"pako/lib/zlib/inflate.js":6,"pako/lib/zlib/zstream.js":8}],2:[function(require,module,exports){
6940936f
SR
44'use strict';
45
46
47var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
48 (typeof Uint16Array !== 'undefined') &&
49 (typeof Int32Array !== 'undefined');
50
51
52exports.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; }
57
58 if (typeof source !== 'object') {
59 throw new TypeError(source + 'must be non-object');
60 }
61
62 for (var p in source) {
63 if (source.hasOwnProperty(p)) {
64 obj[p] = source[p];
65 }
66 }
67 }
68
69 return obj;
70};
71
72
73// reduce buffer size, avoiding mem copy
74exports.shrinkBuf = function (buf, size) {
75 if (buf.length === size) { return buf; }
76 if (buf.subarray) { return buf.subarray(0, size); }
77 buf.length = size;
78 return buf;
79};
80
81
82var fnTyped = {
83 arraySet: function (dest, src, src_offs, len, dest_offs) {
84 if (src.subarray && dest.subarray) {
ae510306 85 dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
6940936f
SR
86 return;
87 }
88 // Fallback to ordinary array
ae510306 89 for (var i = 0; i < len; i++) {
6940936f
SR
90 dest[dest_offs + i] = src[src_offs + i];
91 }
92 },
93 // Join array of chunks to single array.
ae510306 94 flattenChunks: function (chunks) {
6940936f
SR
95 var i, l, len, pos, chunk, result;
96
97 // calculate data length
98 len = 0;
ae510306 99 for (i = 0, l = chunks.length; i < l; i++) {
6940936f
SR
100 len += chunks[i].length;
101 }
102
103 // join chunks
104 result = new Uint8Array(len);
105 pos = 0;
ae510306 106 for (i = 0, l = chunks.length; i < l; i++) {
6940936f
SR
107 chunk = chunks[i];
108 result.set(chunk, pos);
109 pos += chunk.length;
110 }
111
112 return result;
113 }
114};
115
116var fnUntyped = {
117 arraySet: function (dest, src, src_offs, len, dest_offs) {
ae510306 118 for (var i = 0; i < len; i++) {
6940936f
SR
119 dest[dest_offs + i] = src[src_offs + i];
120 }
121 },
122 // Join array of chunks to single array.
ae510306 123 flattenChunks: function (chunks) {
6940936f
SR
124 return [].concat.apply([], chunks);
125 }
126};
127
128
129// Enable/Disable typed arrays use, for testing
130//
131exports.setTyped = function (on) {
132 if (on) {
133 exports.Buf8 = Uint8Array;
134 exports.Buf16 = Uint16Array;
135 exports.Buf32 = Int32Array;
136 exports.assign(exports, fnTyped);
137 } else {
138 exports.Buf8 = Array;
139 exports.Buf16 = Array;
140 exports.Buf32 = Array;
141 exports.assign(exports, fnUntyped);
142 }
143};
144
145exports.setTyped(TYPED_OK);
146
ae510306 147},{}],3:[function(require,module,exports){
6940936f
SR
148'use strict';
149
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.
153
154function adler32(adler, buf, len, pos) {
155 var s1 = (adler & 0xffff) |0,
156 s2 = ((adler >>> 16) & 0xffff) |0,
157 n = 0;
158
159 while (len !== 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;
164 len -= n;
165
166 do {
167 s1 = (s1 + buf[pos++]) |0;
168 s2 = (s2 + s1) |0;
169 } while (--n);
170
171 s1 %= 65521;
172 s2 %= 65521;
173 }
174
175 return (s1 | (s2 << 16)) |0;
176}
177
178
179module.exports = adler32;
180
ae510306 181},{}],4:[function(require,module,exports){
6940936f
SR
182'use strict';
183
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.
187
188
189// Use ordinary array, since untyped makes no boost here
190function makeTable() {
191 var c, table = [];
192
ae510306 193 for (var n = 0; n < 256; n++) {
6940936f 194 c = n;
ae510306
SR
195 for (var k = 0; k < 8; k++) {
196 c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
6940936f
SR
197 }
198 table[n] = c;
199 }
200
201 return table;
202}
203
204// Create table on load. Just 255 signed longs. Not a problem.
205var crcTable = makeTable();
206
207
208function crc32(crc, buf, len, pos) {
209 var t = crcTable,
210 end = pos + len;
211
ae510306 212 crc ^= -1;
6940936f
SR
213
214 for (var i = pos; i < end; i++) {
215 crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
216 }
217
218 return (crc ^ (-1)); // >>> 0;
219}
220
221
222module.exports = crc32;
223
ae510306 224},{}],5:[function(require,module,exports){
6940936f
SR
225'use strict';
226
227// See state defs from inflate.js
228var BAD = 30; /* got a data error -- remain here until reset */
229var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
230
231/*
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.
238
239 Entry assumptions:
240
241 state.mode === LEN
242 strm.avail_in >= 6
243 strm.avail_out >= 258
244 start >= strm.avail_out
245 state.bits < 8
246
247 On return, state.mode is one of:
248
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
252
253 Notes:
254
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.
260
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
264 output space.
265 */
266module.exports = function inflate_fast(strm, start) {
267 var state;
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 */
275//#endif
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 */
c802d931
SR
279 // Use `s_window` instead `window`, avoid conflict with instrumentation tools
280 var s_window; /* allocated sliding window, if wsize != 0 */
6940936f
SR
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 */
293 var from_source;
294
295
296 var input, output; // JS specific, because we have no pointers
297
298 /* copy state to local variables */
299 state = strm.state;
300 //here = state.here;
301 _in = strm.next_in;
302 input = strm.input;
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
309 dmax = state.dmax;
310//#endif
311 wsize = state.wsize;
312 whave = state.whave;
313 wnext = state.wnext;
c802d931 314 s_window = state.window;
6940936f
SR
315 hold = state.hold;
316 bits = state.bits;
317 lcode = state.lencode;
318 dcode = state.distcode;
319 lmask = (1 << state.lenbits) - 1;
320 dmask = (1 << state.distbits) - 1;
321
322
323 /* decode literals and length/distances until end-of-block or not enough
324 input data or output space */
325
326 top:
327 do {
328 if (bits < 15) {
329 hold += input[_in++] << bits;
330 bits += 8;
331 hold += input[_in++] << bits;
332 bits += 8;
333 }
334
335 here = lcode[hold & lmask];
336
337 dolen:
338 for (;;) { // Goto emulation
339 op = here >>> 24/*here.bits*/;
340 hold >>>= op;
341 bits -= op;
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*/;
348 }
349 else if (op & 16) { /* length base */
350 len = here & 0xffff/*here.val*/;
351 op &= 15; /* number of extra bits */
352 if (op) {
353 if (bits < op) {
354 hold += input[_in++] << bits;
355 bits += 8;
356 }
357 len += hold & ((1 << op) - 1);
358 hold >>>= op;
359 bits -= op;
360 }
361 //Tracevv((stderr, "inflate: length %u\n", len));
362 if (bits < 15) {
363 hold += input[_in++] << bits;
364 bits += 8;
365 hold += input[_in++] << bits;
366 bits += 8;
367 }
368 here = dcode[hold & dmask];
369
370 dodist:
371 for (;;) { // goto emulation
372 op = here >>> 24/*here.bits*/;
373 hold >>>= op;
374 bits -= op;
375 op = (here >>> 16) & 0xff/*here.op*/;
376
377 if (op & 16) { /* distance base */
378 dist = here & 0xffff/*here.val*/;
379 op &= 15; /* number of extra bits */
380 if (bits < op) {
381 hold += input[_in++] << bits;
382 bits += 8;
383 if (bits < op) {
384 hold += input[_in++] << bits;
385 bits += 8;
386 }
387 }
388 dist += hold & ((1 << op) - 1);
389//#ifdef INFLATE_STRICT
390 if (dist > dmax) {
391 strm.msg = 'invalid distance too far back';
392 state.mode = BAD;
393 break top;
394 }
395//#endif
396 hold >>>= op;
397 bits -= op;
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 */
402 if (op > whave) {
403 if (state.sane) {
404 strm.msg = 'invalid distance too far back';
405 state.mode = BAD;
406 break top;
407 }
408
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) {
413// do {
414// output[_out++] = 0;
415// } while (--len);
416// continue top;
417// }
418// len -= op - whave;
419// do {
420// output[_out++] = 0;
421// } while (--op > whave);
422// if (op === 0) {
423// from = _out - dist;
424// do {
425// output[_out++] = output[from++];
426// } while (--len);
427// continue top;
428// }
429//#endif
430 }
431 from = 0; // window index
c802d931 432 from_source = s_window;
6940936f
SR
433 if (wnext === 0) { /* very common case */
434 from += wsize - op;
435 if (op < len) { /* some from window */
436 len -= op;
437 do {
c802d931 438 output[_out++] = s_window[from++];
6940936f
SR
439 } while (--op);
440 from = _out - dist; /* rest from output */
441 from_source = output;
442 }
443 }
444 else if (wnext < op) { /* wrap around window */
445 from += wsize + wnext - op;
446 op -= wnext;
447 if (op < len) { /* some from end of window */
448 len -= op;
449 do {
c802d931 450 output[_out++] = s_window[from++];
6940936f
SR
451 } while (--op);
452 from = 0;
453 if (wnext < len) { /* some from start of window */
454 op = wnext;
455 len -= op;
456 do {
c802d931 457 output[_out++] = s_window[from++];
6940936f
SR
458 } while (--op);
459 from = _out - dist; /* rest from output */
460 from_source = output;
461 }
462 }
463 }
464 else { /* contiguous in window */
465 from += wnext - op;
466 if (op < len) { /* some from window */
467 len -= op;
468 do {
c802d931 469 output[_out++] = s_window[from++];
6940936f
SR
470 } while (--op);
471 from = _out - dist; /* rest from output */
472 from_source = output;
473 }
474 }
475 while (len > 2) {
476 output[_out++] = from_source[from++];
477 output[_out++] = from_source[from++];
478 output[_out++] = from_source[from++];
479 len -= 3;
480 }
481 if (len) {
482 output[_out++] = from_source[from++];
483 if (len > 1) {
484 output[_out++] = from_source[from++];
485 }
486 }
487 }
488 else {
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++];
494 len -= 3;
495 } while (len > 2);
496 if (len) {
497 output[_out++] = output[from++];
498 if (len > 1) {
499 output[_out++] = output[from++];
500 }
501 }
502 }
503 }
504 else if ((op & 64) === 0) { /* 2nd level distance code */
505 here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
506 continue dodist;
507 }
508 else {
509 strm.msg = 'invalid distance code';
510 state.mode = BAD;
511 break top;
512 }
513
514 break; // need to emulate goto via "continue"
515 }
516 }
517 else if ((op & 64) === 0) { /* 2nd level length code */
518 here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
519 continue dolen;
520 }
521 else if (op & 32) { /* end-of-block */
522 //Tracevv((stderr, "inflate: end of block\n"));
523 state.mode = TYPE;
524 break top;
525 }
526 else {
527 strm.msg = 'invalid literal/length code';
528 state.mode = BAD;
529 break top;
530 }
531
532 break; // need to emulate goto via "continue"
533 }
534 } while (_in < last && _out < end);
535
536 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
537 len = bits >> 3;
538 _in -= len;
539 bits -= len << 3;
540 hold &= (1 << bits) - 1;
541
542 /* update state and return */
543 strm.next_in = _in;
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));
547 state.hold = hold;
548 state.bits = bits;
549 return;
550};
551
ae510306 552},{}],6:[function(require,module,exports){
6940936f
SR
553'use strict';
554
555
ae510306
SR
556var utils = require('../utils/common');
557var adler32 = require('./adler32');
558var crc32 = require('./crc32');
559var inflate_fast = require('./inffast');
6940936f
SR
560var inflate_table = require('./inftrees');
561
562var CODES = 0;
563var LENS = 1;
564var DISTS = 2;
565
566/* Public constants ==========================================================*/
567/* ===========================================================================*/
568
569
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;
575var Z_FINISH = 4;
576var Z_BLOCK = 5;
577var Z_TREES = 6;
578
579
580/* Return codes for the compression/decompression functions. Negative values
581 * are errors, positive values are used for special but normal events.
582 */
583var Z_OK = 0;
584var Z_STREAM_END = 1;
585var Z_NEED_DICT = 2;
586//var Z_ERRNO = -1;
587var Z_STREAM_ERROR = -2;
588var Z_DATA_ERROR = -3;
589var Z_MEM_ERROR = -4;
590var Z_BUF_ERROR = -5;
591//var Z_VERSION_ERROR = -6;
592
593/* The deflate compression method */
594var Z_DEFLATED = 8;
595
596
597/* STATES ====================================================================*/
598/* ===========================================================================*/
599
600
601var HEAD = 1; /* i: waiting for magic header */
602var FLAGS = 2; /* i: waiting for method and flags (gzip) */
603var TIME = 3; /* i: waiting for modification time (gzip) */
604var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
605var EXLEN = 5; /* i: waiting for extra length (gzip) */
606var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
607var NAME = 7; /* i: waiting for end of file name (gzip) */
608var COMMENT = 8; /* i: waiting for end of comment (gzip) */
609var HCRC = 9; /* i: waiting for header crc (gzip) */
610var DICTID = 10; /* i: waiting for dictionary check value */
611var DICT = 11; /* waiting for inflateSetDictionary() call */
612var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
613var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
614var STORED = 14; /* i: waiting for stored size (length and complement) */
615var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
616var COPY = 16; /* i/o: waiting for input or output to copy stored block */
617var TABLE = 17; /* i: waiting for dynamic block table lengths */
618var LENLENS = 18; /* i: waiting for code length code lengths */
619var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
620var LEN_ = 20; /* i: same as LEN below, but only first time in */
621var LEN = 21; /* i: waiting for length/lit/eob code */
622var LENEXT = 22; /* i: waiting for length extra bits */
623var DIST = 23; /* i: waiting for distance code */
624var DISTEXT = 24; /* i: waiting for distance extra bits */
625var MATCH = 25; /* o: waiting for output space to copy string */
626var LIT = 26; /* o: waiting for output space to write literal */
627var CHECK = 27; /* i: waiting for 32-bit check value */
628var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
629var DONE = 29; /* finished check, done -- remain here until reset */
630var BAD = 30; /* got a data error -- remain here until reset */
631var MEM = 31; /* got an inflate() memory error -- remain here until reset */
632var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
633
634/* ===========================================================================*/
635
636
637
638var ENOUGH_LENS = 852;
639var ENOUGH_DISTS = 592;
640//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
641
642var MAX_WBITS = 15;
643/* 32K LZ77 window */
644var DEF_WBITS = MAX_WBITS;
645
646
ae510306 647function zswap32(q) {
6940936f
SR
648 return (((q >>> 24) & 0xff) +
649 ((q >>> 8) & 0xff00) +
650 ((q & 0xff00) << 8) +
651 ((q & 0xff) << 24));
652}
653
654
655function 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 */
664 // TODO: may be {}
665 this.head = null; /* where to save gzip header information */
666
667 /* sliding window */
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 */
673
674 /* bit accumulator */
675 this.hold = 0; /* input bit accumulator */
676 this.bits = 0; /* number of bits in "in" */
677
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 */
681
682 /* for table and code decoding */
683 this.extra = 0; /* extra bits needed */
684
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 */
690
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[] */
697
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 */
700
701 /*
702 because we don't have pointers in js, we use lencode and distcode directly
703 as buffers so we don't need codes
704 */
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 */
711}
712
713function inflateResetKeep(strm) {
714 var state;
715
716 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
717 state = strm.state;
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;
722 }
723 state.mode = HEAD;
724 state.last = 0;
725 state.havedict = 0;
726 state.dmax = 32768;
727 state.head = null/*Z_NULL*/;
728 state.hold = 0;
729 state.bits = 0;
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);
733
734 state.sane = 1;
735 state.back = -1;
736 //Tracev((stderr, "inflate: reset\n"));
737 return Z_OK;
738}
739
740function inflateReset(strm) {
741 var state;
742
743 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
744 state = strm.state;
745 state.wsize = 0;
746 state.whave = 0;
747 state.wnext = 0;
748 return inflateResetKeep(strm);
749
750}
751
752function inflateReset2(strm, windowBits) {
753 var wrap;
754 var state;
755
756 /* get the state */
757 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
758 state = strm.state;
759
760 /* extract wrap request from windowBits parameter */
761 if (windowBits < 0) {
762 wrap = 0;
763 windowBits = -windowBits;
764 }
765 else {
766 wrap = (windowBits >> 4) + 1;
767 if (windowBits < 48) {
768 windowBits &= 15;
769 }
770 }
771
772 /* set number of window bits, free window if different */
773 if (windowBits && (windowBits < 8 || windowBits > 15)) {
774 return Z_STREAM_ERROR;
775 }
776 if (state.window !== null && state.wbits !== windowBits) {
777 state.window = null;
778 }
779
780 /* update state and reset the rest of it */
781 state.wrap = wrap;
782 state.wbits = windowBits;
783 return inflateReset(strm);
784}
785
786function inflateInit2(strm, windowBits) {
787 var ret;
788 var state;
789
790 if (!strm) { return Z_STREAM_ERROR; }
791 //strm.msg = Z_NULL; /* in case we return an error */
792
793 state = new InflateState();
794
795 //if (state === Z_NULL) return Z_MEM_ERROR;
796 //Tracev((stderr, "inflate: allocated\n"));
797 strm.state = state;
798 state.window = null/*Z_NULL*/;
799 ret = inflateReset2(strm, windowBits);
800 if (ret !== Z_OK) {
801 strm.state = null/*Z_NULL*/;
802 }
803 return ret;
804}
805
806function inflateInit(strm) {
807 return inflateInit2(strm, DEF_WBITS);
808}
809
810
811/*
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.
820 */
821var virgin = true;
822
823var lenfix, distfix; // We have no pointers in JS, so keep tables separate
824
825function fixedtables(state) {
826 /* build fixed huffman tables if first call (may not be thread safe) */
827 if (virgin) {
828 var sym;
829
830 lenfix = new utils.Buf32(512);
831 distfix = new utils.Buf32(32);
832
833 /* literal/length table */
834 sym = 0;
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; }
839
ae510306 840 inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
6940936f
SR
841
842 /* distance table */
843 sym = 0;
844 while (sym < 32) { state.lens[sym++] = 5; }
845
ae510306 846 inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
6940936f
SR
847
848 /* do this just once */
849 virgin = false;
850 }
851
852 state.lencode = lenfix;
853 state.lenbits = 9;
854 state.distcode = distfix;
855 state.distbits = 5;
856}
857
858
859/*
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
865 is loaded.
866
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.
872 */
873function updatewindow(strm, src, end, copy) {
874 var dist;
875 var state = strm.state;
876
877 /* if it hasn't been done already, allocate space for the window */
878 if (state.window === null) {
879 state.wsize = 1 << state.wbits;
880 state.wnext = 0;
881 state.whave = 0;
882
883 state.window = new utils.Buf8(state.wsize);
884 }
885
886 /* copy state->wsize or less output bytes into the circular window */
887 if (copy >= state.wsize) {
ae510306 888 utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0);
6940936f
SR
889 state.wnext = 0;
890 state.whave = state.wsize;
891 }
892 else {
893 dist = state.wsize - state.wnext;
894 if (dist > copy) {
895 dist = copy;
896 }
897 //zmemcpy(state->window + state->wnext, end - copy, dist);
ae510306 898 utils.arraySet(state.window, src, end - copy, dist, state.wnext);
6940936f
SR
899 copy -= dist;
900 if (copy) {
901 //zmemcpy(state->window, end - copy, copy);
ae510306 902 utils.arraySet(state.window, src, end - copy, copy, 0);
6940936f
SR
903 state.wnext = copy;
904 state.whave = state.wsize;
905 }
906 else {
907 state.wnext += dist;
908 if (state.wnext === state.wsize) { state.wnext = 0; }
909 if (state.whave < state.wsize) { state.whave += dist; }
910 }
911 }
912 return 0;
913}
914
915function inflate(strm, flush) {
916 var state;
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 */
926 var from_source;
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 */
934 var opts;
935
936 var n; // temporary var for NEED_BITS
937
938 var order = /* permutation of code lengths */
ae510306 939 [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
6940936f
SR
940
941
942 if (!strm || !strm.state || !strm.output ||
943 (!strm.input && strm.avail_in !== 0)) {
944 return Z_STREAM_ERROR;
945 }
946
947 state = strm.state;
948 if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
949
950
951 //--- LOAD() ---
952 put = strm.next_out;
953 output = strm.output;
954 left = strm.avail_out;
955 next = strm.next_in;
956 input = strm.input;
957 have = strm.avail_in;
958 hold = state.hold;
959 bits = state.bits;
960 //---
961
962 _in = have;
963 _out = left;
964 ret = Z_OK;
965
966 inf_leave: // goto emulation
967 for (;;) {
968 switch (state.mode) {
969 case HEAD:
970 if (state.wrap === 0) {
971 state.mode = TYPEDO;
972 break;
973 }
974 //=== NEEDBITS(16);
975 while (bits < 16) {
976 if (have === 0) { break inf_leave; }
977 have--;
978 hold += input[next++] << bits;
979 bits += 8;
980 }
981 //===//
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);
988 //===//
989
990 //=== INITBITS();
991 hold = 0;
992 bits = 0;
993 //===//
994 state.mode = FLAGS;
995 break;
996 }
997 state.flags = 0; /* expect zlib header */
998 if (state.head) {
999 state.head.done = false;
1000 }
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';
1004 state.mode = BAD;
1005 break;
1006 }
1007 if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
1008 strm.msg = 'unknown compression method';
1009 state.mode = BAD;
1010 break;
1011 }
1012 //--- DROPBITS(4) ---//
1013 hold >>>= 4;
1014 bits -= 4;
1015 //---//
1016 len = (hold & 0x0f)/*BITS(4)*/ + 8;
1017 if (state.wbits === 0) {
1018 state.wbits = len;
1019 }
1020 else if (len > state.wbits) {
1021 strm.msg = 'invalid window size';
1022 state.mode = BAD;
1023 break;
1024 }
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;
1029 //=== INITBITS();
1030 hold = 0;
1031 bits = 0;
1032 //===//
1033 break;
1034 case FLAGS:
1035 //=== NEEDBITS(16); */
1036 while (bits < 16) {
1037 if (have === 0) { break inf_leave; }
1038 have--;
1039 hold += input[next++] << bits;
1040 bits += 8;
1041 }
1042 //===//
1043 state.flags = hold;
1044 if ((state.flags & 0xff) !== Z_DEFLATED) {
1045 strm.msg = 'unknown compression method';
1046 state.mode = BAD;
1047 break;
1048 }
1049 if (state.flags & 0xe000) {
1050 strm.msg = 'unknown header flags set';
1051 state.mode = BAD;
1052 break;
1053 }
1054 if (state.head) {
1055 state.head.text = ((hold >> 8) & 1);
1056 }
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);
1062 //===//
1063 }
1064 //=== INITBITS();
1065 hold = 0;
1066 bits = 0;
1067 //===//
1068 state.mode = TIME;
1069 /* falls through */
1070 case TIME:
1071 //=== NEEDBITS(32); */
1072 while (bits < 32) {
1073 if (have === 0) { break inf_leave; }
1074 have--;
1075 hold += input[next++] << bits;
1076 bits += 8;
1077 }
1078 //===//
1079 if (state.head) {
1080 state.head.time = hold;
1081 }
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);
1089 //===
1090 }
1091 //=== INITBITS();
1092 hold = 0;
1093 bits = 0;
1094 //===//
1095 state.mode = OS;
1096 /* falls through */
1097 case OS:
1098 //=== NEEDBITS(16); */
1099 while (bits < 16) {
1100 if (have === 0) { break inf_leave; }
1101 have--;
1102 hold += input[next++] << bits;
1103 bits += 8;
1104 }
1105 //===//
1106 if (state.head) {
1107 state.head.xflags = (hold & 0xff);
1108 state.head.os = (hold >> 8);
1109 }
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);
1115 //===//
1116 }
1117 //=== INITBITS();
1118 hold = 0;
1119 bits = 0;
1120 //===//
1121 state.mode = EXLEN;
1122 /* falls through */
1123 case EXLEN:
1124 if (state.flags & 0x0400) {
1125 //=== NEEDBITS(16); */
1126 while (bits < 16) {
1127 if (have === 0) { break inf_leave; }
1128 have--;
1129 hold += input[next++] << bits;
1130 bits += 8;
1131 }
1132 //===//
1133 state.length = hold;
1134 if (state.head) {
1135 state.head.extra_len = hold;
1136 }
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);
1142 //===//
1143 }
1144 //=== INITBITS();
1145 hold = 0;
1146 bits = 0;
1147 //===//
1148 }
1149 else if (state.head) {
1150 state.head.extra = null/*Z_NULL*/;
1151 }
1152 state.mode = EXTRA;
1153 /* falls through */
1154 case EXTRA:
1155 if (state.flags & 0x0400) {
1156 copy = state.length;
1157 if (copy > have) { copy = have; }
1158 if (copy) {
1159 if (state.head) {
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);
1164 }
1165 utils.arraySet(
1166 state.head.extra,
1167 input,
1168 next,
1169 // extra field is limited to 65536 bytes
1170 // - no need for additional size check
1171 copy,
1172 /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
1173 len
1174 );
1175 //zmemcpy(state.head.extra + len, next,
1176 // len + copy > state.head.extra_max ?
1177 // state.head.extra_max - len : copy);
1178 }
1179 if (state.flags & 0x0200) {
1180 state.check = crc32(state.check, input, copy, next);
1181 }
1182 have -= copy;
1183 next += copy;
1184 state.length -= copy;
1185 }
1186 if (state.length) { break inf_leave; }
1187 }
1188 state.length = 0;
1189 state.mode = NAME;
1190 /* falls through */
1191 case NAME:
1192 if (state.flags & 0x0800) {
1193 if (have === 0) { break inf_leave; }
1194 copy = 0;
1195 do {
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);
1202 }
1203 } while (len && copy < have);
1204
1205 if (state.flags & 0x0200) {
1206 state.check = crc32(state.check, input, copy, next);
1207 }
1208 have -= copy;
1209 next += copy;
1210 if (len) { break inf_leave; }
1211 }
1212 else if (state.head) {
1213 state.head.name = null;
1214 }
1215 state.length = 0;
1216 state.mode = COMMENT;
1217 /* falls through */
1218 case COMMENT:
1219 if (state.flags & 0x1000) {
1220 if (have === 0) { break inf_leave; }
1221 copy = 0;
1222 do {
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);
1228 }
1229 } while (len && copy < have);
1230 if (state.flags & 0x0200) {
1231 state.check = crc32(state.check, input, copy, next);
1232 }
1233 have -= copy;
1234 next += copy;
1235 if (len) { break inf_leave; }
1236 }
1237 else if (state.head) {
1238 state.head.comment = null;
1239 }
1240 state.mode = HCRC;
1241 /* falls through */
1242 case HCRC:
1243 if (state.flags & 0x0200) {
1244 //=== NEEDBITS(16); */
1245 while (bits < 16) {
1246 if (have === 0) { break inf_leave; }
1247 have--;
1248 hold += input[next++] << bits;
1249 bits += 8;
1250 }
1251 //===//
1252 if (hold !== (state.check & 0xffff)) {
1253 strm.msg = 'header crc mismatch';
1254 state.mode = BAD;
1255 break;
1256 }
1257 //=== INITBITS();
1258 hold = 0;
1259 bits = 0;
1260 //===//
1261 }
1262 if (state.head) {
1263 state.head.hcrc = ((state.flags >> 9) & 1);
1264 state.head.done = true;
1265 }
ae510306 1266 strm.adler = state.check = 0;
6940936f
SR
1267 state.mode = TYPE;
1268 break;
1269 case DICTID:
1270 //=== NEEDBITS(32); */
1271 while (bits < 32) {
1272 if (have === 0) { break inf_leave; }
1273 have--;
1274 hold += input[next++] << bits;
1275 bits += 8;
1276 }
1277 //===//
ae510306 1278 strm.adler = state.check = zswap32(hold);
6940936f
SR
1279 //=== INITBITS();
1280 hold = 0;
1281 bits = 0;
1282 //===//
1283 state.mode = DICT;
1284 /* falls through */
1285 case DICT:
1286 if (state.havedict === 0) {
1287 //--- RESTORE() ---
1288 strm.next_out = put;
1289 strm.avail_out = left;
1290 strm.next_in = next;
1291 strm.avail_in = have;
1292 state.hold = hold;
1293 state.bits = bits;
1294 //---
1295 return Z_NEED_DICT;
1296 }
1297 strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
1298 state.mode = TYPE;
1299 /* falls through */
1300 case TYPE:
1301 if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
1302 /* falls through */
1303 case TYPEDO:
1304 if (state.last) {
1305 //--- BYTEBITS() ---//
1306 hold >>>= bits & 7;
1307 bits -= bits & 7;
1308 //---//
1309 state.mode = CHECK;
1310 break;
1311 }
1312 //=== NEEDBITS(3); */
1313 while (bits < 3) {
1314 if (have === 0) { break inf_leave; }
1315 have--;
1316 hold += input[next++] << bits;
1317 bits += 8;
1318 }
1319 //===//
1320 state.last = (hold & 0x01)/*BITS(1)*/;
1321 //--- DROPBITS(1) ---//
1322 hold >>>= 1;
1323 bits -= 1;
1324 //---//
1325
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;
1331 break;
1332 case 1: /* fixed block */
1333 fixedtables(state);
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) ---//
1339 hold >>>= 2;
1340 bits -= 2;
1341 //---//
1342 break inf_leave;
1343 }
1344 break;
1345 case 2: /* dynamic block */
1346 //Tracev((stderr, "inflate: dynamic codes block%s\n",
1347 // state.last ? " (last)" : ""));
1348 state.mode = TABLE;
1349 break;
1350 case 3:
1351 strm.msg = 'invalid block type';
1352 state.mode = BAD;
1353 }
1354 //--- DROPBITS(2) ---//
1355 hold >>>= 2;
1356 bits -= 2;
1357 //---//
1358 break;
1359 case STORED:
1360 //--- BYTEBITS() ---// /* go to byte boundary */
1361 hold >>>= bits & 7;
1362 bits -= bits & 7;
1363 //---//
1364 //=== NEEDBITS(32); */
1365 while (bits < 32) {
1366 if (have === 0) { break inf_leave; }
1367 have--;
1368 hold += input[next++] << bits;
1369 bits += 8;
1370 }
1371 //===//
1372 if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
1373 strm.msg = 'invalid stored block lengths';
1374 state.mode = BAD;
1375 break;
1376 }
1377 state.length = hold & 0xffff;
1378 //Tracev((stderr, "inflate: stored length %u\n",
1379 // state.length));
1380 //=== INITBITS();
1381 hold = 0;
1382 bits = 0;
1383 //===//
1384 state.mode = COPY_;
1385 if (flush === Z_TREES) { break inf_leave; }
1386 /* falls through */
1387 case COPY_:
1388 state.mode = COPY;
1389 /* falls through */
1390 case COPY:
1391 copy = state.length;
1392 if (copy) {
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);
1398 //---//
1399 have -= copy;
1400 next += copy;
1401 left -= copy;
1402 put += copy;
1403 state.length -= copy;
1404 break;
1405 }
1406 //Tracev((stderr, "inflate: stored end\n"));
1407 state.mode = TYPE;
1408 break;
1409 case TABLE:
1410 //=== NEEDBITS(14); */
1411 while (bits < 14) {
1412 if (have === 0) { break inf_leave; }
1413 have--;
1414 hold += input[next++] << bits;
1415 bits += 8;
1416 }
1417 //===//
1418 state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
1419 //--- DROPBITS(5) ---//
1420 hold >>>= 5;
1421 bits -= 5;
1422 //---//
1423 state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
1424 //--- DROPBITS(5) ---//
1425 hold >>>= 5;
1426 bits -= 5;
1427 //---//
1428 state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
1429 //--- DROPBITS(4) ---//
1430 hold >>>= 4;
1431 bits -= 4;
1432 //---//
1433//#ifndef PKZIP_BUG_WORKAROUND
1434 if (state.nlen > 286 || state.ndist > 30) {
1435 strm.msg = 'too many length or distance symbols';
1436 state.mode = BAD;
1437 break;
1438 }
1439//#endif
1440 //Tracev((stderr, "inflate: table sizes ok\n"));
1441 state.have = 0;
1442 state.mode = LENLENS;
1443 /* falls through */
1444 case LENLENS:
1445 while (state.have < state.ncode) {
1446 //=== NEEDBITS(3);
1447 while (bits < 3) {
1448 if (have === 0) { break inf_leave; }
1449 have--;
1450 hold += input[next++] << bits;
1451 bits += 8;
1452 }
1453 //===//
1454 state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
1455 //--- DROPBITS(3) ---//
1456 hold >>>= 3;
1457 bits -= 3;
1458 //---//
1459 }
1460 while (state.have < 19) {
1461 state.lens[order[state.have++]] = 0;
1462 }
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;
1468 state.lenbits = 7;
1469
ae510306 1470 opts = { bits: state.lenbits };
6940936f
SR
1471 ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
1472 state.lenbits = opts.bits;
1473
1474 if (ret) {
1475 strm.msg = 'invalid code lengths set';
1476 state.mode = BAD;
1477 break;
1478 }
1479 //Tracev((stderr, "inflate: code lengths ok\n"));
1480 state.have = 0;
1481 state.mode = CODELENS;
1482 /* falls through */
1483 case CODELENS:
1484 while (state.have < state.nlen + state.ndist) {
1485 for (;;) {
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;
1490
1491 if ((here_bits) <= bits) { break; }
1492 //--- PULLBYTE() ---//
1493 if (have === 0) { break inf_leave; }
1494 have--;
1495 hold += input[next++] << bits;
1496 bits += 8;
1497 //---//
1498 }
1499 if (here_val < 16) {
1500 //--- DROPBITS(here.bits) ---//
1501 hold >>>= here_bits;
1502 bits -= here_bits;
1503 //---//
1504 state.lens[state.have++] = here_val;
1505 }
1506 else {
1507 if (here_val === 16) {
1508 //=== NEEDBITS(here.bits + 2);
1509 n = here_bits + 2;
1510 while (bits < n) {
1511 if (have === 0) { break inf_leave; }
1512 have--;
1513 hold += input[next++] << bits;
1514 bits += 8;
1515 }
1516 //===//
1517 //--- DROPBITS(here.bits) ---//
1518 hold >>>= here_bits;
1519 bits -= here_bits;
1520 //---//
1521 if (state.have === 0) {
1522 strm.msg = 'invalid bit length repeat';
1523 state.mode = BAD;
1524 break;
1525 }
1526 len = state.lens[state.have - 1];
1527 copy = 3 + (hold & 0x03);//BITS(2);
1528 //--- DROPBITS(2) ---//
1529 hold >>>= 2;
1530 bits -= 2;
1531 //---//
1532 }
1533 else if (here_val === 17) {
1534 //=== NEEDBITS(here.bits + 3);
1535 n = here_bits + 3;
1536 while (bits < n) {
1537 if (have === 0) { break inf_leave; }
1538 have--;
1539 hold += input[next++] << bits;
1540 bits += 8;
1541 }
1542 //===//
1543 //--- DROPBITS(here.bits) ---//
1544 hold >>>= here_bits;
1545 bits -= here_bits;
1546 //---//
1547 len = 0;
1548 copy = 3 + (hold & 0x07);//BITS(3);
1549 //--- DROPBITS(3) ---//
1550 hold >>>= 3;
1551 bits -= 3;
1552 //---//
1553 }
1554 else {
1555 //=== NEEDBITS(here.bits + 7);
1556 n = here_bits + 7;
1557 while (bits < n) {
1558 if (have === 0) { break inf_leave; }
1559 have--;
1560 hold += input[next++] << bits;
1561 bits += 8;
1562 }
1563 //===//
1564 //--- DROPBITS(here.bits) ---//
1565 hold >>>= here_bits;
1566 bits -= here_bits;
1567 //---//
1568 len = 0;
1569 copy = 11 + (hold & 0x7f);//BITS(7);
1570 //--- DROPBITS(7) ---//
1571 hold >>>= 7;
1572 bits -= 7;
1573 //---//
1574 }
1575 if (state.have + copy > state.nlen + state.ndist) {
1576 strm.msg = 'invalid bit length repeat';
1577 state.mode = BAD;
1578 break;
1579 }
1580 while (copy--) {
1581 state.lens[state.have++] = len;
1582 }
1583 }
1584 }
1585
1586 /* handle error breaks in while */
1587 if (state.mode === BAD) { break; }
1588
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';
1592 state.mode = BAD;
1593 break;
1594 }
1595
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 */
1599 state.lenbits = 9;
1600
ae510306 1601 opts = { bits: state.lenbits };
6940936f
SR
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;
1607
1608 if (ret) {
1609 strm.msg = 'invalid literal/lengths set';
1610 state.mode = BAD;
1611 break;
1612 }
1613
1614 state.distbits = 6;
1615 //state.distcode.copy(state.codes);
1616 // Switch to use dynamic table
1617 state.distcode = state.distdyn;
ae510306 1618 opts = { bits: state.distbits };
6940936f
SR
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;
1624
1625 if (ret) {
1626 strm.msg = 'invalid distances set';
1627 state.mode = BAD;
1628 break;
1629 }
1630 //Tracev((stderr, 'inflate: codes ok\n'));
1631 state.mode = LEN_;
1632 if (flush === Z_TREES) { break inf_leave; }
1633 /* falls through */
1634 case LEN_:
1635 state.mode = LEN;
1636 /* falls through */
1637 case LEN:
1638 if (have >= 6 && left >= 258) {
1639 //--- RESTORE() ---
1640 strm.next_out = put;
1641 strm.avail_out = left;
1642 strm.next_in = next;
1643 strm.avail_in = have;
1644 state.hold = hold;
1645 state.bits = bits;
1646 //---
1647 inflate_fast(strm, _out);
1648 //--- LOAD() ---
1649 put = strm.next_out;
1650 output = strm.output;
1651 left = strm.avail_out;
1652 next = strm.next_in;
1653 input = strm.input;
1654 have = strm.avail_in;
1655 hold = state.hold;
1656 bits = state.bits;
1657 //---
1658
1659 if (state.mode === TYPE) {
1660 state.back = -1;
1661 }
1662 break;
1663 }
1664 state.back = 0;
1665 for (;;) {
ae510306 1666 here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
6940936f
SR
1667 here_bits = here >>> 24;
1668 here_op = (here >>> 16) & 0xff;
1669 here_val = here & 0xffff;
1670
1671 if (here_bits <= bits) { break; }
1672 //--- PULLBYTE() ---//
1673 if (have === 0) { break inf_leave; }
1674 have--;
1675 hold += input[next++] << bits;
1676 bits += 8;
1677 //---//
1678 }
1679 if (here_op && (here_op & 0xf0) === 0) {
1680 last_bits = here_bits;
1681 last_op = here_op;
1682 last_val = here_val;
1683 for (;;) {
1684 here = state.lencode[last_val +
ae510306 1685 ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
6940936f
SR
1686 here_bits = here >>> 24;
1687 here_op = (here >>> 16) & 0xff;
1688 here_val = here & 0xffff;
1689
1690 if ((last_bits + here_bits) <= bits) { break; }
1691 //--- PULLBYTE() ---//
1692 if (have === 0) { break inf_leave; }
1693 have--;
1694 hold += input[next++] << bits;
1695 bits += 8;
1696 //---//
1697 }
1698 //--- DROPBITS(last.bits) ---//
1699 hold >>>= last_bits;
1700 bits -= last_bits;
1701 //---//
1702 state.back += last_bits;
1703 }
1704 //--- DROPBITS(here.bits) ---//
1705 hold >>>= here_bits;
1706 bits -= here_bits;
1707 //---//
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));
1714 state.mode = LIT;
1715 break;
1716 }
1717 if (here_op & 32) {
1718 //Tracevv((stderr, "inflate: end of block\n"));
1719 state.back = -1;
1720 state.mode = TYPE;
1721 break;
1722 }
1723 if (here_op & 64) {
1724 strm.msg = 'invalid literal/length code';
1725 state.mode = BAD;
1726 break;
1727 }
1728 state.extra = here_op & 15;
1729 state.mode = LENEXT;
1730 /* falls through */
1731 case LENEXT:
1732 if (state.extra) {
1733 //=== NEEDBITS(state.extra);
1734 n = state.extra;
1735 while (bits < n) {
1736 if (have === 0) { break inf_leave; }
1737 have--;
1738 hold += input[next++] << bits;
1739 bits += 8;
1740 }
1741 //===//
ae510306 1742 state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
6940936f
SR
1743 //--- DROPBITS(state.extra) ---//
1744 hold >>>= state.extra;
1745 bits -= state.extra;
1746 //---//
1747 state.back += state.extra;
1748 }
1749 //Tracevv((stderr, "inflate: length %u\n", state.length));
1750 state.was = state.length;
1751 state.mode = DIST;
1752 /* falls through */
1753 case DIST:
1754 for (;;) {
ae510306 1755 here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
6940936f
SR
1756 here_bits = here >>> 24;
1757 here_op = (here >>> 16) & 0xff;
1758 here_val = here & 0xffff;
1759
1760 if ((here_bits) <= bits) { break; }
1761 //--- PULLBYTE() ---//
1762 if (have === 0) { break inf_leave; }
1763 have--;
1764 hold += input[next++] << bits;
1765 bits += 8;
1766 //---//
1767 }
1768 if ((here_op & 0xf0) === 0) {
1769 last_bits = here_bits;
1770 last_op = here_op;
1771 last_val = here_val;
1772 for (;;) {
1773 here = state.distcode[last_val +
ae510306 1774 ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
6940936f
SR
1775 here_bits = here >>> 24;
1776 here_op = (here >>> 16) & 0xff;
1777 here_val = here & 0xffff;
1778
1779 if ((last_bits + here_bits) <= bits) { break; }
1780 //--- PULLBYTE() ---//
1781 if (have === 0) { break inf_leave; }
1782 have--;
1783 hold += input[next++] << bits;
1784 bits += 8;
1785 //---//
1786 }
1787 //--- DROPBITS(last.bits) ---//
1788 hold >>>= last_bits;
1789 bits -= last_bits;
1790 //---//
1791 state.back += last_bits;
1792 }
1793 //--- DROPBITS(here.bits) ---//
1794 hold >>>= here_bits;
1795 bits -= here_bits;
1796 //---//
1797 state.back += here_bits;
1798 if (here_op & 64) {
1799 strm.msg = 'invalid distance code';
1800 state.mode = BAD;
1801 break;
1802 }
1803 state.offset = here_val;
1804 state.extra = (here_op) & 15;
1805 state.mode = DISTEXT;
1806 /* falls through */
1807 case DISTEXT:
1808 if (state.extra) {
1809 //=== NEEDBITS(state.extra);
1810 n = state.extra;
1811 while (bits < n) {
1812 if (have === 0) { break inf_leave; }
1813 have--;
1814 hold += input[next++] << bits;
1815 bits += 8;
1816 }
1817 //===//
ae510306 1818 state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
6940936f
SR
1819 //--- DROPBITS(state.extra) ---//
1820 hold >>>= state.extra;
1821 bits -= state.extra;
1822 //---//
1823 state.back += state.extra;
1824 }
1825//#ifdef INFLATE_STRICT
1826 if (state.offset > state.dmax) {
1827 strm.msg = 'invalid distance too far back';
1828 state.mode = BAD;
1829 break;
1830 }
1831//#endif
1832 //Tracevv((stderr, "inflate: distance %u\n", state.offset));
1833 state.mode = MATCH;
1834 /* falls through */
1835 case MATCH:
1836 if (left === 0) { break inf_leave; }
1837 copy = _out - left;
1838 if (state.offset > copy) { /* copy from window */
1839 copy = state.offset - copy;
1840 if (copy > state.whave) {
1841 if (state.sane) {
1842 strm.msg = 'invalid distance too far back';
1843 state.mode = BAD;
1844 break;
1845 }
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; }
1853// left -= copy;
1854// state.length -= copy;
1855// do {
1856// output[put++] = 0;
1857// } while (--copy);
1858// if (state.length === 0) { state.mode = LEN; }
1859// break;
1860//#endif
1861 }
1862 if (copy > state.wnext) {
1863 copy -= state.wnext;
1864 from = state.wsize - copy;
1865 }
1866 else {
1867 from = state.wnext - copy;
1868 }
1869 if (copy > state.length) { copy = state.length; }
1870 from_source = state.window;
1871 }
1872 else { /* copy from output */
1873 from_source = output;
1874 from = put - state.offset;
1875 copy = state.length;
1876 }
1877 if (copy > left) { copy = left; }
1878 left -= copy;
1879 state.length -= copy;
1880 do {
1881 output[put++] = from_source[from++];
1882 } while (--copy);
1883 if (state.length === 0) { state.mode = LEN; }
1884 break;
1885 case LIT:
1886 if (left === 0) { break inf_leave; }
1887 output[put++] = state.length;
1888 left--;
1889 state.mode = LEN;
1890 break;
1891 case CHECK:
1892 if (state.wrap) {
1893 //=== NEEDBITS(32);
1894 while (bits < 32) {
1895 if (have === 0) { break inf_leave; }
1896 have--;
1897 // Use '|' insdead of '+' to make sure that result is signed
1898 hold |= input[next++] << bits;
1899 bits += 8;
1900 }
1901 //===//
1902 _out -= left;
1903 strm.total_out += _out;
1904 state.total += _out;
1905 if (_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));
1909
1910 }
1911 _out = left;
ae510306
SR
1912 // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
1913 if ((state.flags ? hold : zswap32(hold)) !== state.check) {
6940936f
SR
1914 strm.msg = 'incorrect data check';
1915 state.mode = BAD;
1916 break;
1917 }
1918 //=== INITBITS();
1919 hold = 0;
1920 bits = 0;
1921 //===//
1922 //Tracev((stderr, "inflate: check matches trailer\n"));
1923 }
1924 state.mode = LENGTH;
1925 /* falls through */
1926 case LENGTH:
1927 if (state.wrap && state.flags) {
1928 //=== NEEDBITS(32);
1929 while (bits < 32) {
1930 if (have === 0) { break inf_leave; }
1931 have--;
1932 hold += input[next++] << bits;
1933 bits += 8;
1934 }
1935 //===//
1936 if (hold !== (state.total & 0xffffffff)) {
1937 strm.msg = 'incorrect length check';
1938 state.mode = BAD;
1939 break;
1940 }
1941 //=== INITBITS();
1942 hold = 0;
1943 bits = 0;
1944 //===//
1945 //Tracev((stderr, "inflate: length matches trailer\n"));
1946 }
1947 state.mode = DONE;
1948 /* falls through */
1949 case DONE:
1950 ret = Z_STREAM_END;
1951 break inf_leave;
1952 case BAD:
1953 ret = Z_DATA_ERROR;
1954 break inf_leave;
1955 case MEM:
1956 return Z_MEM_ERROR;
1957 case SYNC:
1958 /* falls through */
1959 default:
1960 return Z_STREAM_ERROR;
1961 }
1962 }
1963
1964 // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
1965
1966 /*
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.
1971 */
1972
1973 //--- RESTORE() ---
1974 strm.next_out = put;
1975 strm.avail_out = left;
1976 strm.next_in = next;
1977 strm.avail_in = have;
1978 state.hold = hold;
1979 state.bits = bits;
1980 //---
1981
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)) {
1985 state.mode = MEM;
1986 return Z_MEM_ERROR;
1987 }
1988 }
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));
1997 }
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) {
2002 ret = Z_BUF_ERROR;
2003 }
2004 return ret;
2005}
2006
2007function inflateEnd(strm) {
2008
2009 if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
2010 return Z_STREAM_ERROR;
2011 }
2012
2013 var state = strm.state;
2014 if (state.window) {
2015 state.window = null;
2016 }
2017 strm.state = null;
2018 return Z_OK;
2019}
2020
2021function inflateGetHeader(strm, head) {
2022 var state;
2023
2024 /* check state */
2025 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
2026 state = strm.state;
2027 if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
2028
2029 /* save header structure */
2030 state.head = head;
2031 head.done = false;
2032 return Z_OK;
2033}
2034
ae510306
SR
2035function inflateSetDictionary(strm, dictionary) {
2036 var dictLength = dictionary.length;
2037
2038 var state;
2039 var dictid;
2040 var ret;
2041
2042 /* check state */
2043 if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; }
2044 state = strm.state;
2045
2046 if (state.wrap !== 0 && state.mode !== DICT) {
2047 return Z_STREAM_ERROR;
2048 }
2049
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;
2057 }
2058 }
2059 /* copy dictionary to window using updatewindow(), which will amend the
2060 existing dictionary if appropriate */
2061 ret = updatewindow(strm, dictionary, dictLength, dictLength);
2062 if (ret) {
2063 state.mode = MEM;
2064 return Z_MEM_ERROR;
2065 }
2066 state.havedict = 1;
2067 // Tracev((stderr, "inflate: dictionary set\n"));
2068 return Z_OK;
2069}
6940936f
SR
2070
2071exports.inflateReset = inflateReset;
2072exports.inflateReset2 = inflateReset2;
2073exports.inflateResetKeep = inflateResetKeep;
2074exports.inflateInit = inflateInit;
2075exports.inflateInit2 = inflateInit2;
2076exports.inflate = inflate;
2077exports.inflateEnd = inflateEnd;
2078exports.inflateGetHeader = inflateGetHeader;
ae510306 2079exports.inflateSetDictionary = inflateSetDictionary;
6940936f
SR
2080exports.inflateInfo = 'pako inflate (from Nodeca project)';
2081
2082/* Not implemented
2083exports.inflateCopy = inflateCopy;
2084exports.inflateGetDictionary = inflateGetDictionary;
2085exports.inflateMark = inflateMark;
2086exports.inflatePrime = inflatePrime;
6940936f
SR
2087exports.inflateSync = inflateSync;
2088exports.inflateSyncPoint = inflateSyncPoint;
2089exports.inflateUndermine = inflateUndermine;
2090*/
2091
ae510306 2092},{"../utils/common":2,"./adler32":3,"./crc32":4,"./inffast":5,"./inftrees":7}],7:[function(require,module,exports){
6940936f
SR
2093'use strict';
2094
2095
2096var utils = require('../utils/common');
2097
2098var MAXBITS = 15;
2099var ENOUGH_LENS = 852;
2100var ENOUGH_DISTS = 592;
2101//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
2102
2103var CODES = 0;
2104var LENS = 1;
2105var DISTS = 2;
2106
2107var 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
2110];
2111
2112var 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
2115];
2116
2117var 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
2121];
2122
2123var 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
2127];
2128
2129module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
2130{
2131 var bits = opts.bits;
2132 //here = opts.here; /* table entry for duplication */
2133
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 */
2149 var base_index = 0;
2150// var shoextra; /* extra bits table to use */
2151 var end; /* use base and extra for symbol > end */
ae510306
SR
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 */
6940936f
SR
2154 var extra = null;
2155 var extra_index = 0;
2156
2157 var here_bits, here_op, here_val;
2158
2159 /*
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.
2171
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.
2176
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
2181 the caller.
2182
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
2187 decoding tables.
2188 */
2189
2190 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
2191 for (len = 0; len <= MAXBITS; len++) {
2192 count[len] = 0;
2193 }
2194 for (sym = 0; sym < codes; sym++) {
2195 count[lens[lens_index + sym]]++;
2196 }
2197
2198 /* bound code lengths, force root to be within code lengths */
2199 root = bits;
2200 for (max = MAXBITS; max >= 1; max--) {
2201 if (count[max] !== 0) { break; }
2202 }
2203 if (root > max) {
2204 root = max;
2205 }
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;
2211
2212
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;
2217
2218 opts.bits = 1;
2219 return 0; /* no symbols, but wait for decoding to report error */
2220 }
2221 for (min = 1; min < max; min++) {
2222 if (count[min] !== 0) { break; }
2223 }
2224 if (root < min) {
2225 root = min;
2226 }
2227
2228 /* check for an over-subscribed or incomplete set of lengths */
2229 left = 1;
2230 for (len = 1; len <= MAXBITS; len++) {
2231 left <<= 1;
2232 left -= count[len];
2233 if (left < 0) {
2234 return -1;
2235 } /* over-subscribed */
2236 }
2237 if (left > 0 && (type === CODES || max !== 1)) {
2238 return -1; /* incomplete set */
2239 }
2240
2241 /* generate offsets into symbol table for each length for sorting */
2242 offs[1] = 0;
2243 for (len = 1; len < MAXBITS; len++) {
2244 offs[len + 1] = offs[len] + count[len];
2245 }
2246
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;
2251 }
2252 }
2253
2254 /*
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.
2261
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.
2267
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.
2272
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.
2278
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.
2283 */
2284
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 */
2290 end = 19;
2291
2292 } else if (type === LENS) {
2293 base = lbase;
2294 base_index -= 257;
2295 extra = lext;
2296 extra_index -= 257;
2297 end = 256;
2298
2299 } else { /* DISTS */
2300 base = dbase;
2301 extra = dext;
2302 end = -1;
2303 }
2304
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 */
2315
2316 /* check available table space */
2317 if ((type === LENS && used > ENOUGH_LENS) ||
2318 (type === DISTS && used > ENOUGH_DISTS)) {
2319 return 1;
2320 }
2321
ae510306 2322 var i = 0;
6940936f
SR
2323 /* process all codes and make table entries */
2324 for (;;) {
2325 i++;
2326 /* create table entry */
2327 here_bits = len - drop;
2328 if (work[sym] < end) {
2329 here_op = 0;
2330 here_val = work[sym];
2331 }
2332 else if (work[sym] > end) {
2333 here_op = extra[extra_index + work[sym]];
2334 here_val = base[base_index + work[sym]];
2335 }
2336 else {
2337 here_op = 32 + 64; /* end of block */
2338 here_val = 0;
2339 }
2340
2341 /* replicate for those indices with low len bits equal to huff */
2342 incr = 1 << (len - drop);
2343 fill = 1 << curr;
2344 min = fill; /* save offset to next table */
2345 do {
2346 fill -= incr;
2347 table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
2348 } while (fill !== 0);
2349
2350 /* backwards increment the len-bit code huff */
2351 incr = 1 << (len - 1);
2352 while (huff & incr) {
2353 incr >>= 1;
2354 }
2355 if (incr !== 0) {
2356 huff &= incr - 1;
2357 huff += incr;
2358 } else {
2359 huff = 0;
2360 }
2361
2362 /* go to next symbol, update count, len */
2363 sym++;
2364 if (--count[len] === 0) {
2365 if (len === max) { break; }
2366 len = lens[lens_index + work[sym]];
2367 }
2368
2369 /* create new sub-table if needed */
2370 if (len > root && (huff & mask) !== low) {
2371 /* if first time, transition to sub-tables */
2372 if (drop === 0) {
2373 drop = root;
2374 }
2375
2376 /* increment past last table */
2377 next += min; /* here min is 1 << curr */
2378
2379 /* determine length of next table */
2380 curr = len - drop;
2381 left = 1 << curr;
2382 while (curr + drop < max) {
2383 left -= count[curr + drop];
2384 if (left <= 0) { break; }
2385 curr++;
2386 left <<= 1;
2387 }
2388
2389 /* check for enough space */
2390 used += 1 << curr;
2391 if ((type === LENS && used > ENOUGH_LENS) ||
2392 (type === DISTS && used > ENOUGH_DISTS)) {
2393 return 1;
2394 }
2395
2396 /* point entry in root table to sub-table */
2397 low = huff & mask;
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;
2402 }
2403 }
2404
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) */
2408 if (huff !== 0) {
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;
2413 }
2414
2415 /* set return parameters */
2416 //opts.table_index += used;
2417 opts.bits = root;
2418 return 0;
2419};
2420
ae510306 2421},{"../utils/common":2}],8:[function(require,module,exports){
6940936f
SR
2422'use strict';
2423
2424
2425function ZStream() {
2426 /* next input byte */
2427 this.input = null; // JS specific, because we have no pointers
2428 this.next_in = 0;
2429 /* number of bytes available at input */
2430 this.avail_in = 0;
2431 /* total number of input bytes read so far */
2432 this.total_in = 0;
2433 /* next output byte should be put there */
2434 this.output = null; // JS specific, because we have no pointers
2435 this.next_out = 0;
2436 /* remaining free space at output */
2437 this.avail_out = 0;
2438 /* total number of bytes output so far */
2439 this.total_out = 0;
2440 /* last error message, NULL if no error */
2441 this.msg = ''/*Z_NULL*/;
2442 /* not visible by applications */
2443 this.state = null;
2444 /* best guess about the data type: binary or text */
2445 this.data_type = 2/*Z_UNKNOWN*/;
2446 /* adler32 value of the uncompressed data */
2447 this.adler = 0;
2448}
2449
2450module.exports = ZStream;
2451
ae510306 2452},{}]},{},[1])(1)
c802d931 2453});