1 /**
  2  * Copyright (C) 2014 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global core, Uint8Array, ArrayBuffer*/
 26 
 27 (function() {
 28     "use strict";
 29 
 30 /*jslint vars:true, plusplus:true, bitwise: true, eqeq: true*/
 31 
 32     /**
 33      * @suppress {reportUnknownTypes}
 34      * @return {!{inflate:!function(!Uint8Array,!number):!Uint8Array}}
 35      */
 36     function createRawInflateSingleton() {
 37 
 38     var pako;
 39 
 40 // Imported code for Raw inflating
 41 // Source: https://github.com/nodeca/pako/blob/master/dist/pako_inflate.js
 42 // Changes done to the code:
 43 // * first line was deserialized and changed to add the result to the var "pako",
 44 //   instead of trying to register it with any existing define/require or global systems
 45 // * wrapping "!function(e){}" was turned into "(function(e){})"
 46 // * few closure markups where really needed, like @constructor
 47 /*
 48 (The MIT License)
 49 
 50 Copyright (C) 2014 by Vitaly Puzrin
 51 
 52 Permission is hereby granted, free of charge, to any person obtaining a copy
 53 of this software and associated documentation files (the "Software"), to deal
 54 in the Software without restriction, including without limitation the rights
 55 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 56 copies of the Software, and to permit persons to whom the Software is
 57 furnished to do so, subject to the following conditions:
 58 
 59 The above copyright notice and this permission notice shall be included in
 60 all copies or substantial portions of the Software.
 61 
 62 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 63 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 64 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 65 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 66 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 67 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 68 THE SOFTWARE.
 69 */
 70 //// pako_inflate.js: start
 71 /* pako 0.2.3 nodeca/pako */
 72 (function(e){
 73     pako = e();
 74 }(function(){
 75     var module,exports;
 76     return (function e(t,n,r){
 77         /**
 78          * @param {*} o
 79          * @param {*=} u
 80          * @return {undefined}
 81          */
 82         function s(o,u){
 83             if(!n[o]){
 84                 if(!t[o]){
 85                     throw new Error("Cannot find module '"+o+"'")
 86                 }
 87                 var f=n[o]={exports:{}};
 88                 t[o][0].call(f.exports,function(e){
 89                     var n=t[o][1][e];
 90                     return s(n?n:e)
 91                 },f,f.exports,e,t,n,r)
 92             }
 93             return n[o].exports
 94         }
 95         for(var o=0;o<r.length;o++)
 96             s(r[o]);
 97         return s
 98     })({1:[function(_dereq_,module,exports){
 99 'use strict';
100 
101 
102 var zlib_inflate = _dereq_('./zlib/inflate.js');
103 var utils = _dereq_('./utils/common');
104 var strings = _dereq_('./utils/strings');
105 var c = _dereq_('./zlib/constants');
106 var msg = _dereq_('./zlib/messages');
107 var zstream = _dereq_('./zlib/zstream');
108 var gzheader = _dereq_('./zlib/gzheader');
109 
110 
111 /**
112  * class Inflate
113  *
114  * Generic JS-style wrapper for zlib calls. If you don't need
115  * streaming behaviour - use more simple functions: [[inflate]]
116  * and [[inflateRaw]].
117  **/
118 
119 /* internal
120  * inflate.chunks -> Array
121  *
122  * Chunks of output data, if [[Inflate#onData]] not overriden.
123  **/
124 
125 /**
126  * Inflate.result -> Uint8Array|Array|String
127  *
128  * Uncompressed result, generated by default [[Inflate#onData]]
129  * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
130  * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
131  **/
132 
133 /**
134  * Inflate.err -> Number
135  *
136  * Error code after inflate finished. 0 (Z_OK) on success.
137  * Should be checked if broken data possible.
138  **/
139 
140 /**
141  * Inflate.msg -> String
142  *
143  * Error message, if [[Inflate.err]] != 0
144  **/
145 
146 
147 /**
148  * new Inflate(options)
149  * - options (Object): zlib inflate options.
150  *
151  * Creates new inflator instance with specified params. Throws exception
152  * on bad params. Supported options:
153  *
154  * - `windowBits`
155  *
156  * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
157  * for more information on these.
158  *
159  * Additional options, for internal needs:
160  *
161  * - `chunkSize` - size of generated data chunks (16K by default)
162  * - `raw` (Boolean) - do raw inflate
163  * - `to` (String) - if equal to 'string', then result will be converted
164  *   from utf8 to utf16 (javascript) string. When string output requested,
165  *   chunk length can differ from `chunkSize`, depending on content.
166  *
167  * By default, when no options set, autodetect deflate/gzip data format via
168  * wrapper header.
169  *
170  * ##### Example:
171  *
172  * ```javascript
173  * var pako = require('pako')
174  *   , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
175  *   , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
176  *
177  * var inflate = new pako.Inflate({ level: 3});
178  *
179  * inflate.push(chunk1, false);
180  * inflate.push(chunk2, true);  // true -> last chunk
181  *
182  * if (inflate.err) { throw new Error(inflate.err); }
183  *
184  * console.log(inflate.result);
185  * ```
186  * @constructor
187  **/
188 var Inflate = function(options) {
189 
190   this.options = utils.assign({
191     chunkSize: 16384,
192     windowBits: 0,
193     to: ''
194   }, options || {});
195 
196   var opt = this.options;
197 
198   // Force window size for `raw` data, if not set directly,
199   // because we have no header for autodetect.
200   if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
201     opt.windowBits = -opt.windowBits;
202     if (opt.windowBits === 0) { opt.windowBits = -15; }
203   }
204 
205   // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
206   if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
207       !(options && options.windowBits)) {
208     opt.windowBits += 32;
209   }
210 
211   // Gzip header has no info about windows size, we can do autodetect only
212   // for deflate. So, if window size not set, force it to max when gzip possible
213   if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
214     // bit 3 (16) -> gzipped data
215     // bit 4 (32) -> autodetect gzip/deflate
216     if ((opt.windowBits & 15) === 0) {
217       opt.windowBits |= 15;
218     }
219   }
220 
221   this.err    = 0;      // error code, if happens (0 = Z_OK)
222   this.msg    = '';     // error message
223   this.ended  = false;  // used to avoid multiple onEnd() calls
224   this.chunks = [];     // chunks of compressed data
225 
226   this.strm   = new zstream();
227   this.strm.avail_out = 0;
228 
229   var status  = zlib_inflate.inflateInit2(
230     this.strm,
231     opt.windowBits
232   );
233 
234   if (status !== c.Z_OK) {
235     throw new Error(msg[status]);
236   }
237 
238   this.header = new gzheader();
239 
240   zlib_inflate.inflateGetHeader(this.strm, this.header);
241 };
242 
243 /**
244  * Inflate#push(data[, mode]) -> Boolean
245  * - data (Uint8Array|Array|String): input data
246  * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
247  *   See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
248  *
249  * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
250  * new output chunks. Returns `true` on success. The last data block must have
251  * mode Z_FINISH (or `true`). That flush internal pending buffers and call
252  * [[Inflate#onEnd]].
253  *
254  * On fail call [[Inflate#onEnd]] with error code and return false.
255  *
256  * We strongly recommend to use `Uint8Array` on input for best speed (output
257  * format is detected automatically). Also, don't skip last param and always
258  * use the same type in your code (boolean or number). That will improve JS speed.
259  *
260  * For regular `Array`-s make sure all elements are [0..255].
261  *
262  * ##### Example
263  *
264  * ```javascript
265  * push(chunk, false); // push one of data chunks
266  * ...
267  * push(chunk, true);  // push last chunk
268  * ```
269  **/
270 Inflate.prototype.push = function(data, mode) {
271   var strm = this.strm;
272   var chunkSize = this.options.chunkSize;
273   var status, _mode;
274   var next_out_utf8, tail, utf8str;
275 
276   if (this.ended) { return false; }
277   _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
278 
279   // Convert data if needed
280   if (typeof data === 'string') {
281     // Only binary strings can be decompressed on practice
282     strm.input = strings.binstring2buf(data);
283   } else {
284     strm.input = data;
285   }
286 
287   strm.next_in = 0;
288   strm.avail_in = strm.input.length;
289 
290   do {
291     if (strm.avail_out === 0) {
292       strm.output = new utils.Buf8(chunkSize);
293       strm.next_out = 0;
294       strm.avail_out = chunkSize;
295     }
296 
297     status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH);    /* no bad return value */
298 
299     if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
300       this.onEnd(status);
301       this.ended = true;
302       return false;
303     }
304 
305     if (strm.next_out) {
306       if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) {
307 
308         if (this.options.to === 'string') {
309 
310           next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
311 
312           tail = strm.next_out - next_out_utf8;
313           utf8str = strings.buf2string(strm.output, next_out_utf8);
314 
315           // move tail
316           strm.next_out = tail;
317           strm.avail_out = chunkSize - tail;
318           if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
319 
320           this.onData(utf8str);
321 
322         } else {
323           this.onData(utils.shrinkBuf(strm.output, strm.next_out));
324         }
325       }
326     }
327   } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
328 
329   if (status === c.Z_STREAM_END) {
330     _mode = c.Z_FINISH;
331   }
332   // Finalize on the last chunk.
333   if (_mode === c.Z_FINISH) {
334     status = zlib_inflate.inflateEnd(this.strm);
335     this.onEnd(status);
336     this.ended = true;
337     return status === c.Z_OK;
338   }
339 
340   return true;
341 };
342 
343 
344 /**
345  * Inflate#onData(chunk) -> Void
346  * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
347  *   on js engine support. When string output requested, each chunk
348  *   will be string.
349  *
350  * By default, stores data blocks in `chunks[]` property and glue
351  * those in `onEnd`. Override this handler, if you need another behaviour.
352  **/
353 Inflate.prototype.onData = function(chunk) {
354   this.chunks.push(chunk);
355 };
356 
357 
358 /**
359  * Inflate#onEnd(status) -> Void
360  * - status (Number): inflate status. 0 (Z_OK) on success,
361  *   other if not.
362  *
363  * Called once after you tell inflate that input stream complete
364  * or error happenned. By default - join collected chunks,
365  * free memory and fill `results` / `err` properties.
366  **/
367 Inflate.prototype.onEnd = function(status) {
368   // On success - join
369   if (status === c.Z_OK) {
370     if (this.options.to === 'string') {
371       // Glue & convert here, until we teach pako to send
372       // utf8 alligned strings to onData
373       this.result = this.chunks.join('');
374     } else {
375       this.result = utils.flattenChunks(this.chunks);
376     }
377   }
378   this.chunks = [];
379   this.err = status;
380   this.msg = this.strm.msg;
381 };
382 
383 
384 /**
385  * inflate(data[, options]) -> Uint8Array|Array|String
386  * - data (Uint8Array|Array|String): input data to decompress.
387  * - options (Object): zlib inflate options.
388  *
389  * Decompress `data` with inflate/ungzip and `options`. Autodetect
390  * format via wrapper header by default. That's why we don't provide
391  * separate `ungzip` method.
392  *
393  * Supported options are:
394  *
395  * - windowBits
396  *
397  * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
398  * for more information.
399  *
400  * Sugar (options):
401  *
402  * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
403  *   negative windowBits implicitly.
404  * - `to` (String) - if equal to 'string', then result will be converted
405  *   from utf8 to utf16 (javascript) string. When string output requested,
406  *   chunk length can differ from `chunkSize`, depending on content.
407  *
408  *
409  * ##### Example:
410  *
411  * ```javascript
412  * var pako = require('pako')
413  *   , input = pako.deflate([1,2,3,4,5,6,7,8,9])
414  *   , output;
415  *
416  * try {
417  *   output = pako.inflate(input);
418  * } catch (err)
419  *   console.log(err);
420  * }
421  * ```
422  **/
423 function inflate(input, options) {
424   var inflator = new Inflate(options);
425 
426   inflator.push(input, true);
427 
428   // That will never happens, if you don't cheat with options :)
429   if (inflator.err) { throw inflator.msg; }
430 
431   return inflator.result;
432 }
433 
434 
435 /**
436  * inflateRaw(data[, options]) -> Uint8Array|Array|String
437  * - data (Uint8Array|Array|String): input data to decompress.
438  * - options (Object): zlib inflate options.
439  *
440  * The same as [[inflate]], but creates raw data, without wrapper
441  * (header and adler32 crc).
442  **/
443 function inflateRaw(input, options) {
444   options = options || {};
445   options.raw = true;
446   return inflate(input, options);
447 }
448 
449 
450 /**
451  * ungzip(data[, options]) -> Uint8Array|Array|String
452  * - data (Uint8Array|Array|String): input data to decompress.
453  * - options (Object): zlib inflate options.
454  *
455  * Just shortcut to [[inflate]], because it autodetects format
456  * by header.content. Done for convenience.
457  **/
458 
459 
460 exports.Inflate = Inflate;
461 exports.inflate = inflate;
462 exports.inflateRaw = inflateRaw;
463 exports.ungzip  = inflate;
464 
465 },{"./utils/common":2,"./utils/strings":3,"./zlib/constants":5,"./zlib/gzheader":7,"./zlib/inflate.js":9,"./zlib/messages":11,"./zlib/zstream":12}],2:[function(_dereq_,module,exports){
466 'use strict';
467 
468 
469 var TYPED_OK =  (typeof Uint8Array !== 'undefined') &&
470                 (typeof Uint16Array !== 'undefined') &&
471                 (typeof Int32Array !== 'undefined');
472 
473 /**
474  * @param {...*} obj
475  */
476 exports.assign = function (obj /*from1, from2, from3, ...*/) {
477   var sources = Array.prototype.slice.call(arguments, 1);
478   while (sources.length) {
479     var source = sources.shift();
480     if (!source) { continue; }
481 
482     if (typeof(source) !== 'object') {
483       throw new TypeError(source + 'must be non-object');
484     }
485 
486     for (var p in source) {
487       if (source.hasOwnProperty(p)) {
488         obj[p] = source[p];
489       }
490     }
491   }
492 
493   return obj;
494 };
495 
496 
497 // reduce buffer size, avoiding mem copy
498 exports.shrinkBuf = function (buf, size) {
499   if (buf.length === size) { return buf; }
500   if (buf.subarray) { return buf.subarray(0, size); }
501   buf.length = size;
502   return buf;
503 };
504 
505 
506 var fnTyped = {
507   arraySet: function (dest, src, src_offs, len, dest_offs) {
508     if (src.subarray && dest.subarray) {
509       dest.set(src.subarray(src_offs, src_offs+len), dest_offs);
510       return;
511     }
512     // Fallback to ordinary array
513     for(var i=0; i<len; i++) {
514       dest[dest_offs + i] = src[src_offs + i];
515     }
516   },
517   // Join array of chunks to single array.
518   flattenChunks: function(chunks) {
519     var i, l, len, pos, chunk, result;
520 
521     // calculate data length
522     len = 0;
523     for (i=0, l=chunks.length; i<l; i++) {
524       len += chunks[i].length;
525     }
526 
527     // join chunks
528     result = new Uint8Array(len);
529     pos = 0;
530     for (i=0, l=chunks.length; i<l; i++) {
531       chunk = chunks[i];
532       result.set(chunk, pos);
533       pos += chunk.length;
534     }
535 
536     return result;
537   }
538 };
539 
540 var fnUntyped = {
541   arraySet: function (dest, src, src_offs, len, dest_offs) {
542     for(var i=0; i<len; i++) {
543       dest[dest_offs + i] = src[src_offs + i];
544     }
545   },
546   // Join array of chunks to single array.
547   flattenChunks: function(chunks) {
548     return [].concat.apply([], chunks);
549   }
550 };
551 
552 
553 // Enable/Disable typed arrays use, for testing
554 //
555 exports.setTyped = function (on) {
556   if (on) {
557     exports.Buf8  = Uint8Array;
558     exports.Buf16 = Uint16Array;
559     exports.Buf32 = Int32Array;
560     exports.assign(exports, fnTyped);
561   } else {
562     exports.Buf8  = Array;
563     exports.Buf16 = Array;
564     exports.Buf32 = Array;
565     exports.assign(exports, fnUntyped);
566   }
567 };
568 
569 exports.setTyped(TYPED_OK);
570 },{}],3:[function(_dereq_,module,exports){
571 // String encode/decode helpers
572 'use strict';
573 
574 
575 var utils = _dereq_('./common');
576 
577 
578 // Quick check if we can use fast array to bin string conversion
579 //
580 // - apply(Array) can fail on Android 2.2
581 // - apply(Uint8Array) can fail on iOS 5.1 Safary
582 //
583 var STR_APPLY_OK = true;
584 var STR_APPLY_UIA_OK = true;
585 
586 try { String.fromCharCode.apply(null, [0]); } catch(__) { STR_APPLY_OK = false; }
587 try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch(__) { STR_APPLY_UIA_OK = false; }
588 
589 
590 // Table with utf8 lengths (calculated by first byte of sequence)
591 // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
592 // because max possible codepoint is 0x10ffff
593 var _utf8len = new utils.Buf8(256);
594 for (var i=0; i<256; i++) {
595   _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
596 }
597 _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
598 
599 
600 // convert string to array (typed, when possible)
601 exports.string2buf = function (str) {
602   var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
603 
604   // count binary size
605   for (m_pos = 0; m_pos < str_len; m_pos++) {
606     c = str.charCodeAt(m_pos);
607     if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
608       c2 = str.charCodeAt(m_pos+1);
609       if ((c2 & 0xfc00) === 0xdc00) {
610         c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
611         m_pos++;
612       }
613     }
614     buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
615   }
616 
617   // allocate buffer
618   buf = new utils.Buf8(buf_len);
619 
620   // convert
621   for (i=0, m_pos = 0; i < buf_len; m_pos++) {
622     c = str.charCodeAt(m_pos);
623     if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
624       c2 = str.charCodeAt(m_pos+1);
625       if ((c2 & 0xfc00) === 0xdc00) {
626         c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
627         m_pos++;
628       }
629     }
630     if (c < 0x80) {
631       /* one byte */
632       buf[i++] = c;
633     } else if (c < 0x800) {
634       /* two bytes */
635       buf[i++] = 0xC0 | (c >>> 6);
636       buf[i++] = 0x80 | (c & 0x3f);
637     } else if (c < 0x10000) {
638       /* three bytes */
639       buf[i++] = 0xE0 | (c >>> 12);
640       buf[i++] = 0x80 | (c >>> 6 & 0x3f);
641       buf[i++] = 0x80 | (c & 0x3f);
642     } else {
643       /* four bytes */
644       buf[i++] = 0xf0 | (c >>> 18);
645       buf[i++] = 0x80 | (c >>> 12 & 0x3f);
646       buf[i++] = 0x80 | (c >>> 6 & 0x3f);
647       buf[i++] = 0x80 | (c & 0x3f);
648     }
649   }
650 
651   return buf;
652 };
653 
654 // Helper (used in 2 places)
655 function buf2binstring(buf, len) {
656   // use fallback for big arrays to avoid stack overflow
657   if (len < 65537) {
658     if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
659       return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
660     }
661   }
662 
663   var result = '';
664   for(var i=0; i < len; i++) {
665     result += String.fromCharCode(buf[i]);
666   }
667   return result;
668 }
669 
670 
671 // Convert byte array to binary string
672 exports.buf2binstring = function(buf) {
673   return buf2binstring(buf, buf.length);
674 };
675 
676 
677 // Convert binary string (typed, when possible)
678 exports.binstring2buf = function(str) {
679   var buf = new utils.Buf8(str.length);
680   for(var i=0, len=buf.length; i < len; i++) {
681     buf[i] = str.charCodeAt(i);
682   }
683   return buf;
684 };
685 
686 
687 // convert array to string
688 exports.buf2string = function (buf, max) {
689   var i, out, c, c_len;
690   var len = max || buf.length;
691 
692   // Reserve max possible length (2 words per char)
693   // NB: by unknown reasons, Array is significantly faster for
694   //     String.fromCharCode.apply than Uint16Array.
695   var utf16buf = new Array(len*2);
696 
697   for (out=0, i=0; i<len;) {
698     c = buf[i++];
699     // quick process ascii
700     if (c < 0x80) { utf16buf[out++] = c; continue; }
701 
702     c_len = _utf8len[c];
703     // skip 5 & 6 byte codes
704     if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
705 
706     // apply mask on first byte
707     c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
708     // join the rest
709     while (c_len > 1 && i < len) {
710       c = (c << 6) | (buf[i++] & 0x3f);
711       c_len--;
712     }
713 
714     // terminated by end of string?
715     if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
716 
717     if (c < 0x10000) {
718       utf16buf[out++] = c;
719     } else {
720       c -= 0x10000;
721       utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
722       utf16buf[out++] = 0xdc00 | (c & 0x3ff);
723     }
724   }
725 
726   return buf2binstring(utf16buf, out);
727 };
728 
729 
730 // Calculate max possible position in utf8 buffer,
731 // that will not break sequence. If that's not possible
732 // - (very small limits) return max size as is.
733 //
734 // buf[] - utf8 bytes array
735 // max   - length limit (mandatory);
736 exports.utf8border = function(buf, max) {
737   var pos;
738 
739   max = max || buf.length;
740   if (max > buf.length) { max = buf.length; }
741 
742   // go back from last position, until start of sequence found
743   pos = max-1;
744   while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
745 
746   // Fuckup - very small and broken sequence,
747   // return max, because we should return something anyway.
748   if (pos < 0) { return max; }
749 
750   // If we came to start of buffer - that means vuffer is too small,
751   // return max too.
752   if (pos === 0) { return max; }
753 
754   return (pos + _utf8len[buf[pos]] > max) ? pos : max;
755 };
756 
757 },{"./common":2}],4:[function(_dereq_,module,exports){
758 'use strict';
759 
760 // Note: adler32 takes 12% for level 0 and 2% for level 6.
761 // It doesn't worth to make additional optimizationa as in original.
762 // Small size is preferable.
763 
764 function adler32(adler, buf, len, pos) {
765   var s1 = (adler & 0xffff) |0
766     , s2 = ((adler >>> 16) & 0xffff) |0
767     , n = 0;
768 
769   while (len !== 0) {
770     // Set limit ~ twice less than 5552, to keep
771     // s2 in 31-bits, because we force signed ints.
772     // in other case %= will fail.
773     n = len > 2000 ? 2000 : len;
774     len -= n;
775 
776     do {
777       s1 = (s1 + buf[pos++]) |0;
778       s2 = (s2 + s1) |0;
779     } while (--n);
780 
781     s1 %= 65521;
782     s2 %= 65521;
783   }
784 
785   return (s1 | (s2 << 16)) |0;
786 }
787 
788 
789 module.exports = adler32;
790 },{}],5:[function(_dereq_,module,exports){
791 module.exports = {
792 
793   /* Allowed flush values; see deflate() and inflate() below for details */
794   Z_NO_FLUSH:         0,
795   Z_PARTIAL_FLUSH:    1,
796   Z_SYNC_FLUSH:       2,
797   Z_FULL_FLUSH:       3,
798   Z_FINISH:           4,
799   Z_BLOCK:            5,
800   Z_TREES:            6,
801 
802   /* Return codes for the compression/decompression functions. Negative values
803   * are errors, positive values are used for special but normal events.
804   */
805   Z_OK:               0,
806   Z_STREAM_END:       1,
807   Z_NEED_DICT:        2,
808   Z_ERRNO:           -1,
809   Z_STREAM_ERROR:    -2,
810   Z_DATA_ERROR:      -3,
811   //Z_MEM_ERROR:     -4,
812   Z_BUF_ERROR:       -5,
813   //Z_VERSION_ERROR: -6,
814 
815   /* compression levels */
816   Z_NO_COMPRESSION:         0,
817   Z_BEST_SPEED:             1,
818   Z_BEST_COMPRESSION:       9,
819   Z_DEFAULT_COMPRESSION:   -1,
820 
821 
822   Z_FILTERED:               1,
823   Z_HUFFMAN_ONLY:           2,
824   Z_RLE:                    3,
825   Z_FIXED:                  4,
826   Z_DEFAULT_STRATEGY:       0,
827 
828   /* Possible values of the data_type field (though see inflate()) */
829   Z_BINARY:                 0,
830   Z_TEXT:                   1,
831   //Z_ASCII:                1, // = Z_TEXT (deprecated)
832   Z_UNKNOWN:                2,
833 
834   /* The deflate compression method */
835   Z_DEFLATED:               8
836   //Z_NULL:                 null // Use -1 or null inline, depending on var type
837 };
838 },{}],6:[function(_dereq_,module,exports){
839 'use strict';
840 
841 // Note: we can't get significant speed boost here.
842 // So write code to minimize size - no pregenerated tables
843 // and array tools dependencies.
844 
845 
846 // Use ordinary array, since untyped makes no boost here
847 function makeTable() {
848   var c, table = [];
849 
850   for(var n =0; n < 256; n++){
851     c = n;
852     for(var k =0; k < 8; k++){
853       c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
854     }
855     table[n] = c;
856   }
857 
858   return table;
859 }
860 
861 // Create table on load. Just 255 signed longs. Not a problem.
862 var crcTable = makeTable();
863 
864 
865 function crc32(crc, buf, len, pos) {
866   var t = crcTable
867     , end = pos + len;
868 
869   crc = crc ^ (-1);
870 
871   for (var i = pos; i < end; i++ ) {
872     crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
873   }
874 
875   return (crc ^ (-1)); // >>> 0;
876 }
877 
878 
879 module.exports = crc32;
880 },{}],7:[function(_dereq_,module,exports){
881 'use strict';
882 
883 
884 function GZheader() {
885   /* true if compressed data believed to be text */
886   this.text       = 0;
887   /* modification time */
888   this.time       = 0;
889   /* extra flags (not used when writing a gzip file) */
890   this.xflags     = 0;
891   /* operating system */
892   this.os         = 0;
893   /* pointer to extra field or Z_NULL if none */
894   this.extra      = null;
895   /* extra field length (valid if extra != Z_NULL) */
896   this.extra_len  = 0; // Actually, we don't need it in JS,
897                        // but leave for few code modifications
898 
899   //
900   // Setup limits is not necessary because in js we should not preallocate memory 
901   // for inflate use constant limit in 65536 bytes
902   //
903 
904   /* space at extra (only when reading header) */
905   // this.extra_max  = 0;
906   /* pointer to zero-terminated file name or Z_NULL */
907   this.name       = '';
908   /* space at name (only when reading header) */
909   // this.name_max   = 0;
910   /* pointer to zero-terminated comment or Z_NULL */
911   this.comment    = '';
912   /* space at comment (only when reading header) */
913   // this.comm_max   = 0;
914   /* true if there was or will be a header crc */
915   this.hcrc       = 0;
916   /* true when done reading gzip header (not used when writing a gzip file) */
917   this.done       = false;
918 }
919 
920 module.exports = GZheader;
921 },{}],8:[function(_dereq_,module,exports){
922 'use strict';
923 
924 // See state defs from inflate.js
925 var BAD = 30;       /* got a data error -- remain here until reset */
926 var TYPE = 12;      /* i: waiting for type bits, including last-flag bit */
927 
928 /*
929    Decode literal, length, and distance codes and write out the resulting
930    literal and match bytes until either not enough input or output is
931    available, an end-of-block is encountered, or a data error is encountered.
932    When large enough input and output buffers are supplied to inflate(), for
933    example, a 16K input buffer and a 64K output buffer, more than 95% of the
934    inflate execution time is spent in this routine.
935 
936    Entry assumptions:
937 
938         state.mode === LEN
939         strm.avail_in >= 6
940         strm.avail_out >= 258
941         start >= strm.avail_out
942         state.bits < 8
943 
944    On return, state.mode is one of:
945 
946         LEN -- ran out of enough output space or enough available input
947         TYPE -- reached end of block code, inflate() to interpret next block
948         BAD -- error in block data
949 
950    Notes:
951 
952     - The maximum input bits used by a length/distance pair is 15 bits for the
953       length code, 5 bits for the length extra, 15 bits for the distance code,
954       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
955       Therefore if strm.avail_in >= 6, then there is enough input to avoid
956       checking for available input while decoding.
957 
958     - The maximum bytes that a single length/distance pair can output is 258
959       bytes, which is the maximum length that can be coded.  inflate_fast()
960       requires strm.avail_out >= 258 for each loop to avoid checking for
961       output space.
962  */
963 module.exports = function inflate_fast(strm, start) {
964   var state;
965   var _in;                    /* local strm.input */
966   var last;                   /* have enough input while in < last */
967   var _out;                   /* local strm.output */
968   var beg;                    /* inflate()'s initial strm.output */
969   var end;                    /* while out < end, enough space available */
970 //#ifdef INFLATE_STRICT
971   var dmax;                   /* maximum distance from zlib header */
972 //#endif
973   var wsize;                  /* window size or zero if not using window */
974   var whave;                  /* valid bytes in the window */
975   var wnext;                  /* window write index */
976   var window;                 /* allocated sliding window, if wsize != 0 */
977   var hold;                   /* local strm.hold */
978   var bits;                   /* local strm.bits */
979   var lcode;                  /* local strm.lencode */
980   var dcode;                  /* local strm.distcode */
981   var lmask;                  /* mask for first level of length codes */
982   var dmask;                  /* mask for first level of distance codes */
983   var here;                   /* retrieved table entry */
984   var op;                     /* code bits, operation, extra bits, or */
985                               /*  window position, window bytes to copy */
986   var len;                    /* match length, unused bytes */
987   var dist;                   /* match distance */
988   var from;                   /* where to copy match from */
989   var from_source;
990 
991 
992   var input, output; // JS specific, because we have no pointers
993 
994   /* copy state to local variables */
995   state = strm.state;
996   //here = state.here;
997   _in = strm.next_in;
998   input = strm.input;
999   last = _in + (strm.avail_in - 5);
1000   _out = strm.next_out;
1001   output = strm.output;
1002   beg = _out - (start - strm.avail_out);
1003   end = _out + (strm.avail_out - 257);
1004 //#ifdef INFLATE_STRICT
1005   dmax = state.dmax;
1006 //#endif
1007   wsize = state.wsize;
1008   whave = state.whave;
1009   wnext = state.wnext;
1010   window = state.window;
1011   hold = state.hold;
1012   bits = state.bits;
1013   lcode = state.lencode;
1014   dcode = state.distcode;
1015   lmask = (1 << state.lenbits) - 1;
1016   dmask = (1 << state.distbits) - 1;
1017 
1018 
1019   /* decode literals and length/distances until end-of-block or not enough
1020      input data or output space */
1021 
1022   top:
1023   do {
1024     if (bits < 15) {
1025       hold += input[_in++] << bits;
1026       bits += 8;
1027       hold += input[_in++] << bits;
1028       bits += 8;
1029     }
1030 
1031     here = lcode[hold & lmask];
1032 
1033     dolen:
1034     for (;;) { // Goto emulation
1035       op = here >>> 24/*here.bits*/;
1036       hold >>>= op;
1037       bits -= op;
1038       op = (here >>> 16) & 0xff/*here.op*/;
1039       if (op === 0) {                          /* literal */
1040         //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1041         //        "inflate:         literal '%c'\n" :
1042         //        "inflate:         literal 0x%02x\n", here.val));
1043         output[_out++] = here & 0xffff/*here.val*/;
1044       }
1045       else if (op & 16) {                     /* length base */
1046         len = here & 0xffff/*here.val*/;
1047         op &= 15;                           /* number of extra bits */
1048         if (op) {
1049           if (bits < op) {
1050             hold += input[_in++] << bits;
1051             bits += 8;
1052           }
1053           len += hold & ((1 << op) - 1);
1054           hold >>>= op;
1055           bits -= op;
1056         }
1057         //Tracevv((stderr, "inflate:         length %u\n", len));
1058         if (bits < 15) {
1059           hold += input[_in++] << bits;
1060           bits += 8;
1061           hold += input[_in++] << bits;
1062           bits += 8;
1063         }
1064         here = dcode[hold & dmask];
1065 
1066         dodist:
1067         for (;;) { // goto emulation
1068           op = here >>> 24/*here.bits*/;
1069           hold >>>= op;
1070           bits -= op;
1071           op = (here >>> 16) & 0xff/*here.op*/;
1072 
1073           if (op & 16) {                      /* distance base */
1074             dist = here & 0xffff/*here.val*/;
1075             op &= 15;                       /* number of extra bits */
1076             if (bits < op) {
1077               hold += input[_in++] << bits;
1078               bits += 8;
1079               if (bits < op) {
1080                 hold += input[_in++] << bits;
1081                 bits += 8;
1082               }
1083             }
1084             dist += hold & ((1 << op) - 1);
1085 //#ifdef INFLATE_STRICT
1086             if (dist > dmax) {
1087               strm.msg = 'invalid distance too far back';
1088               state.mode = BAD;
1089               break top;
1090             }
1091 //#endif
1092             hold >>>= op;
1093             bits -= op;
1094             //Tracevv((stderr, "inflate:         distance %u\n", dist));
1095             op = _out - beg;                /* max distance in output */
1096             if (dist > op) {                /* see if copy from window */
1097               op = dist - op;               /* distance back in window */
1098               if (op > whave) {
1099                 if (state.sane) {
1100                   strm.msg = 'invalid distance too far back';
1101                   state.mode = BAD;
1102                   break top;
1103                 }
1104 
1105 // (!) This block is disabled in zlib defailts,
1106 // don't enable it for binary compatibility
1107 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1108 //                if (len <= op - whave) {
1109 //                  do {
1110 //                    output[_out++] = 0;
1111 //                  } while (--len);
1112 //                  continue top;
1113 //                }
1114 //                len -= op - whave;
1115 //                do {
1116 //                  output[_out++] = 0;
1117 //                } while (--op > whave);
1118 //                if (op === 0) {
1119 //                  from = _out - dist;
1120 //                  do {
1121 //                    output[_out++] = output[from++];
1122 //                  } while (--len);
1123 //                  continue top;
1124 //                }
1125 //#endif
1126               }
1127               from = 0; // window index
1128               from_source = window;
1129               if (wnext === 0) {           /* very common case */
1130                 from += wsize - op;
1131                 if (op < len) {         /* some from window */
1132                   len -= op;
1133                   do {
1134                     output[_out++] = window[from++];
1135                   } while (--op);
1136                   from = _out - dist;  /* rest from output */
1137                   from_source = output;
1138                 }
1139               }
1140               else if (wnext < op) {      /* wrap around window */
1141                 from += wsize + wnext - op;
1142                 op -= wnext;
1143                 if (op < len) {         /* some from end of window */
1144                   len -= op;
1145                   do {
1146                     output[_out++] = window[from++];
1147                   } while (--op);
1148                   from = 0;
1149                   if (wnext < len) {  /* some from start of window */
1150                     op = wnext;
1151                     len -= op;
1152                     do {
1153                       output[_out++] = window[from++];
1154                     } while (--op);
1155                     from = _out - dist;      /* rest from output */
1156                     from_source = output;
1157                   }
1158                 }
1159               }
1160               else {                      /* contiguous in window */
1161                 from += wnext - op;
1162                 if (op < len) {         /* some from window */
1163                   len -= op;
1164                   do {
1165                     output[_out++] = window[from++];
1166                   } while (--op);
1167                   from = _out - dist;  /* rest from output */
1168                   from_source = output;
1169                 }
1170               }
1171               while (len > 2) {
1172                 output[_out++] = from_source[from++];
1173                 output[_out++] = from_source[from++];
1174                 output[_out++] = from_source[from++];
1175                 len -= 3;
1176               }
1177               if (len) {
1178                 output[_out++] = from_source[from++];
1179                 if (len > 1) {
1180                   output[_out++] = from_source[from++];
1181                 }
1182               }
1183             }
1184             else {
1185               from = _out - dist;          /* copy direct from output */
1186               do {                        /* minimum length is three */
1187                 output[_out++] = output[from++];
1188                 output[_out++] = output[from++];
1189                 output[_out++] = output[from++];
1190                 len -= 3;
1191               } while (len > 2);
1192               if (len) {
1193                 output[_out++] = output[from++];
1194                 if (len > 1) {
1195                   output[_out++] = output[from++];
1196                 }
1197               }
1198             }
1199           }
1200           else if ((op & 64) === 0) {          /* 2nd level distance code */
1201             here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
1202             continue dodist;
1203           }
1204           else {
1205             strm.msg = 'invalid distance code';
1206             state.mode = BAD;
1207             break top;
1208           }
1209 
1210           break; // need to emulate goto via "continue"
1211         }
1212       }
1213       else if ((op & 64) === 0) {              /* 2nd level length code */
1214         here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
1215         continue dolen;
1216       }
1217       else if (op & 32) {                     /* end-of-block */
1218         //Tracevv((stderr, "inflate:         end of block\n"));
1219         state.mode = TYPE;
1220         break top;
1221       }
1222       else {
1223         strm.msg = 'invalid literal/length code';
1224         state.mode = BAD;
1225         break top;
1226       }
1227 
1228       break; // need to emulate goto via "continue"
1229     }
1230   } while (_in < last && _out < end);
1231 
1232   /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
1233   len = bits >> 3;
1234   _in -= len;
1235   bits -= len << 3;
1236   hold &= (1 << bits) - 1;
1237 
1238   /* update state and return */
1239   strm.next_in = _in;
1240   strm.next_out = _out;
1241   strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
1242   strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
1243   state.hold = hold;
1244   state.bits = bits;
1245   return;
1246 };
1247 
1248 },{}],9:[function(_dereq_,module,exports){
1249 'use strict';
1250 
1251 
1252 var utils = _dereq_('../utils/common');
1253 var adler32 = _dereq_('./adler32');
1254 var crc32   = _dereq_('./crc32');
1255 var inflate_fast = _dereq_('./inffast');
1256 var inflate_table = _dereq_('./inftrees');
1257 
1258 var CODES = 0;
1259 var LENS = 1;
1260 var DISTS = 2;
1261 
1262 /* Public constants ==========================================================*/
1263 /* ===========================================================================*/
1264 
1265 
1266 /* Allowed flush values; see deflate() and inflate() below for details */
1267 //var Z_NO_FLUSH      = 0;
1268 //var Z_PARTIAL_FLUSH = 1;
1269 //var Z_SYNC_FLUSH    = 2;
1270 //var Z_FULL_FLUSH    = 3;
1271 var Z_FINISH        = 4;
1272 var Z_BLOCK         = 5;
1273 var Z_TREES         = 6;
1274 
1275 
1276 /* Return codes for the compression/decompression functions. Negative values
1277  * are errors, positive values are used for special but normal events.
1278  */
1279 var Z_OK            = 0;
1280 var Z_STREAM_END    = 1;
1281 var Z_NEED_DICT     = 2;
1282 //var Z_ERRNO         = -1;
1283 var Z_STREAM_ERROR  = -2;
1284 var Z_DATA_ERROR    = -3;
1285 var Z_MEM_ERROR     = -4;
1286 var Z_BUF_ERROR     = -5;
1287 //var Z_VERSION_ERROR = -6;
1288 
1289 /* The deflate compression method */
1290 var Z_DEFLATED  = 8;
1291 
1292 
1293 /* STATES ====================================================================*/
1294 /* ===========================================================================*/
1295 
1296 
1297 var    HEAD = 1;       /* i: waiting for magic header */
1298 var    FLAGS = 2;      /* i: waiting for method and flags (gzip) */
1299 var    TIME = 3;       /* i: waiting for modification time (gzip) */
1300 var    OS = 4;         /* i: waiting for extra flags and operating system (gzip) */
1301 var    EXLEN = 5;      /* i: waiting for extra length (gzip) */
1302 var    EXTRA = 6;      /* i: waiting for extra bytes (gzip) */
1303 var    NAME = 7;       /* i: waiting for end of file name (gzip) */
1304 var    COMMENT = 8;    /* i: waiting for end of comment (gzip) */
1305 var    HCRC = 9;       /* i: waiting for header crc (gzip) */
1306 var    DICTID = 10;    /* i: waiting for dictionary check value */
1307 var    DICT = 11;      /* waiting for inflateSetDictionary() call */
1308 var        TYPE = 12;      /* i: waiting for type bits, including last-flag bit */
1309 var        TYPEDO = 13;    /* i: same, but skip check to exit inflate on new block */
1310 var        STORED = 14;    /* i: waiting for stored size (length and complement) */
1311 var        COPY_ = 15;     /* i/o: same as COPY below, but only first time in */
1312 var        COPY = 16;      /* i/o: waiting for input or output to copy stored block */
1313 var        TABLE = 17;     /* i: waiting for dynamic block table lengths */
1314 var        LENLENS = 18;   /* i: waiting for code length code lengths */
1315 var        CODELENS = 19;  /* i: waiting for length/lit and distance code lengths */
1316 var            LEN_ = 20;      /* i: same as LEN below, but only first time in */
1317 var            LEN = 21;       /* i: waiting for length/lit/eob code */
1318 var            LENEXT = 22;    /* i: waiting for length extra bits */
1319 var            DIST = 23;      /* i: waiting for distance code */
1320 var            DISTEXT = 24;   /* i: waiting for distance extra bits */
1321 var            MATCH = 25;     /* o: waiting for output space to copy string */
1322 var            LIT = 26;       /* o: waiting for output space to write literal */
1323 var    CHECK = 27;     /* i: waiting for 32-bit check value */
1324 var    LENGTH = 28;    /* i: waiting for 32-bit length (gzip) */
1325 var    DONE = 29;      /* finished check, done -- remain here until reset */
1326 var    BAD = 30;       /* got a data error -- remain here until reset */
1327 var    MEM = 31;       /* got an inflate() memory error -- remain here until reset */
1328 var    SYNC = 32;      /* looking for synchronization bytes to restart inflate() */
1329 
1330 /* ===========================================================================*/
1331 
1332 
1333 
1334 var ENOUGH_LENS = 852;
1335 var ENOUGH_DISTS = 592;
1336 //var ENOUGH =  (ENOUGH_LENS+ENOUGH_DISTS);
1337 
1338 var MAX_WBITS = 15;
1339 /* 32K LZ77 window */
1340 var DEF_WBITS = MAX_WBITS;
1341 
1342 
1343 function ZSWAP32(q) {
1344   return  (((q >>> 24) & 0xff) +
1345           ((q >>> 8) & 0xff00) +
1346           ((q & 0xff00) << 8) +
1347           ((q & 0xff) << 24));
1348 }
1349 
1350 /**
1351  * @constructor
1352  */
1353 function InflateState() {
1354   this.mode = 0;             /* current inflate mode */
1355   this.last = false;          /* true if processing last block */
1356   this.wrap = 0;              /* bit 0 true for zlib, bit 1 true for gzip */
1357   this.havedict = false;      /* true if dictionary provided */
1358   this.flags = 0;             /* gzip header method and flags (0 if zlib) */
1359   this.dmax = 0;              /* zlib header max distance (INFLATE_STRICT) */
1360   this.check = 0;             /* protected copy of check value */
1361   this.total = 0;             /* protected copy of output count */
1362   // TODO: may be {}
1363   this.head = null;           /* where to save gzip header information */
1364 
1365   /* sliding window */
1366   this.wbits = 0;             /* log base 2 of requested window size */
1367   this.wsize = 0;             /* window size or zero if not using window */
1368   this.whave = 0;             /* valid bytes in the window */
1369   this.wnext = 0;             /* window write index */
1370   this.window = null;         /* allocated sliding window, if needed */
1371 
1372   /* bit accumulator */
1373   this.hold = 0;              /* input bit accumulator */
1374   this.bits = 0;              /* number of bits in "in" */
1375 
1376   /* for string and stored block copying */
1377   this.length = 0;            /* literal or length of data to copy */
1378   this.offset = 0;            /* distance back to copy string from */
1379 
1380   /* for table and code decoding */
1381   this.extra = 0;             /* extra bits needed */
1382 
1383   /* fixed and dynamic code tables */
1384   this.lencode = null;          /* starting table for length/literal codes */
1385   this.distcode = null;         /* starting table for distance codes */
1386   this.lenbits = 0;           /* index bits for lencode */
1387   this.distbits = 0;          /* index bits for distcode */
1388 
1389   /* dynamic table building */
1390   this.ncode = 0;             /* number of code length code lengths */
1391   this.nlen = 0;              /* number of length code lengths */
1392   this.ndist = 0;             /* number of distance code lengths */
1393   this.have = 0;              /* number of code lengths in lens[] */
1394   this.next = null;              /* next available space in codes[] */
1395 
1396   this.lens = new utils.Buf16(320); /* temporary storage for code lengths */
1397   this.work = new utils.Buf16(288); /* work area for code table building */
1398 
1399   /*
1400    because we don't have pointers in js, we use lencode and distcode directly
1401    as buffers so we don't need codes
1402   */
1403   //this.codes = new utils.Buf32(ENOUGH);       /* space for code tables */
1404   this.lendyn = null;              /* dynamic table for length/literal codes (JS specific) */
1405   this.distdyn = null;             /* dynamic table for distance codes (JS specific) */
1406   this.sane = 0;                   /* if false, allow invalid distance too far */
1407   this.back = 0;                   /* bits back of last unprocessed length/lit */
1408   this.was = 0;                    /* initial length of match */
1409 }
1410 
1411 function inflateResetKeep(strm) {
1412   var state;
1413 
1414   if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1415   state = strm.state;
1416   strm.total_in = strm.total_out = state.total = 0;
1417   strm.msg = ''; /*Z_NULL*/
1418   if (state.wrap) {       /* to support ill-conceived Java test suite */
1419     strm.adler = state.wrap & 1;
1420   }
1421   state.mode = HEAD;
1422   state.last = 0;
1423   state.havedict = 0;
1424   state.dmax = 32768;
1425   state.head = null/*Z_NULL*/;
1426   state.hold = 0;
1427   state.bits = 0;
1428   //state.lencode = state.distcode = state.next = state.codes;
1429   state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS);
1430   state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS);
1431 
1432   state.sane = 1;
1433   state.back = -1;
1434   //Tracev((stderr, "inflate: reset\n"));
1435   return Z_OK;
1436 }
1437 
1438 function inflateReset(strm) {
1439   var state;
1440 
1441   if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1442   state = strm.state;
1443   state.wsize = 0;
1444   state.whave = 0;
1445   state.wnext = 0;
1446   return inflateResetKeep(strm);
1447 
1448 }
1449 
1450 function inflateReset2(strm, windowBits) {
1451   var wrap;
1452   var state;
1453 
1454   /* get the state */
1455   if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1456   state = strm.state;
1457 
1458   /* extract wrap request from windowBits parameter */
1459   if (windowBits < 0) {
1460     wrap = 0;
1461     windowBits = -windowBits;
1462   }
1463   else {
1464     wrap = (windowBits >> 4) + 1;
1465     if (windowBits < 48) {
1466       windowBits &= 15;
1467     }
1468   }
1469 
1470   /* set number of window bits, free window if different */
1471   if (windowBits && (windowBits < 8 || windowBits > 15)) {
1472     return Z_STREAM_ERROR;
1473   }
1474   if (state.window !== null && state.wbits !== windowBits) {
1475     state.window = null;
1476   }
1477 
1478   /* update state and reset the rest of it */
1479   state.wrap = wrap;
1480   state.wbits = windowBits;
1481   return inflateReset(strm);
1482 }
1483 
1484 function inflateInit2(strm, windowBits) {
1485   var ret;
1486   var state;
1487 
1488   if (!strm) { return Z_STREAM_ERROR; }
1489   //strm.msg = Z_NULL;                 /* in case we return an error */
1490 
1491   state = new InflateState();
1492 
1493   //if (state === Z_NULL) return Z_MEM_ERROR;
1494   //Tracev((stderr, "inflate: allocated\n"));
1495   strm.state = state;
1496   state.window = null/*Z_NULL*/;
1497   ret = inflateReset2(strm, windowBits);
1498   if (ret !== Z_OK) {
1499     strm.state = null/*Z_NULL*/;
1500   }
1501   return ret;
1502 }
1503 
1504 function inflateInit(strm) {
1505   return inflateInit2(strm, DEF_WBITS);
1506 }
1507 
1508 
1509 /*
1510  Return state with length and distance decoding tables and index sizes set to
1511  fixed code decoding.  Normally this returns fixed tables from inffixed.h.
1512  If BUILDFIXED is defined, then instead this routine builds the tables the
1513  first time it's called, and returns those tables the first time and
1514  thereafter.  This reduces the size of the code by about 2K bytes, in
1515  exchange for a little execution time.  However, BUILDFIXED should not be
1516  used for threaded applications, since the rewriting of the tables and virgin
1517  may not be thread-safe.
1518  */
1519 var virgin = true;
1520 
1521 var lenfix, distfix; // We have no pointers in JS, so keep tables separate
1522 
1523 function fixedtables(state) {
1524   /* build fixed huffman tables if first call (may not be thread safe) */
1525   if (virgin) {
1526     var sym;
1527 
1528     lenfix = new utils.Buf32(512);
1529     distfix = new utils.Buf32(32);
1530 
1531     /* literal/length table */
1532     sym = 0;
1533     while (sym < 144) { state.lens[sym++] = 8; }
1534     while (sym < 256) { state.lens[sym++] = 9; }
1535     while (sym < 280) { state.lens[sym++] = 7; }
1536     while (sym < 288) { state.lens[sym++] = 8; }
1537 
1538     inflate_table(LENS,  state.lens, 0, 288, lenfix,   0, state.work, {bits: 9});
1539 
1540     /* distance table */
1541     sym = 0;
1542     while (sym < 32) { state.lens[sym++] = 5; }
1543 
1544     inflate_table(DISTS, state.lens, 0, 32,   distfix, 0, state.work, {bits: 5});
1545 
1546     /* do this just once */
1547     virgin = false;
1548   }
1549 
1550   state.lencode = lenfix;
1551   state.lenbits = 9;
1552   state.distcode = distfix;
1553   state.distbits = 5;
1554 }
1555 
1556 
1557 /*
1558  Update the window with the last wsize (normally 32K) bytes written before
1559  returning.  If window does not exist yet, create it.  This is only called
1560  when a window is already in use, or when output has been written during this
1561  inflate call, but the end of the deflate stream has not been reached yet.
1562  It is also called to create a window for dictionary data when a dictionary
1563  is loaded.
1564 
1565  Providing output buffers larger than 32K to inflate() should provide a speed
1566  advantage, since only the last 32K of output is copied to the sliding window
1567  upon return from inflate(), and since all distances after the first 32K of
1568  output will fall in the output data, making match copies simpler and faster.
1569  The advantage may be dependent on the size of the processor's data caches.
1570  */
1571 function updatewindow(strm, src, end, copy) {
1572   var dist;
1573   var state = strm.state;
1574 
1575   /* if it hasn't been done already, allocate space for the window */
1576   if (state.window === null) {
1577     state.wsize = 1 << state.wbits;
1578     state.wnext = 0;
1579     state.whave = 0;
1580 
1581     state.window = new utils.Buf8(state.wsize);
1582   }
1583 
1584   /* copy state->wsize or less output bytes into the circular window */
1585   if (copy >= state.wsize) {
1586     utils.arraySet(state.window,src, end - state.wsize, state.wsize, 0);
1587     state.wnext = 0;
1588     state.whave = state.wsize;
1589   }
1590   else {
1591     dist = state.wsize - state.wnext;
1592     if (dist > copy) {
1593       dist = copy;
1594     }
1595     //zmemcpy(state->window + state->wnext, end - copy, dist);
1596     utils.arraySet(state.window,src, end - copy, dist, state.wnext);
1597     copy -= dist;
1598     if (copy) {
1599       //zmemcpy(state->window, end - copy, copy);
1600       utils.arraySet(state.window,src, end - copy, copy, 0);
1601       state.wnext = copy;
1602       state.whave = state.wsize;
1603     }
1604     else {
1605       state.wnext += dist;
1606       if (state.wnext === state.wsize) { state.wnext = 0; }
1607       if (state.whave < state.wsize) { state.whave += dist; }
1608     }
1609   }
1610   return 0;
1611 }
1612 
1613 function inflate(strm, flush) {
1614   var state;
1615   var input, output;          // input/output buffers
1616   var next;                   /* next input INDEX */
1617   var put;                    /* next output INDEX */
1618   var have, left;             /* available input and output */
1619   var hold;                   /* bit buffer */
1620   var bits;                   /* bits in bit buffer */
1621   var _in, _out;              /* save starting available input and output */
1622   var copy;                   /* number of stored or match bytes to copy */
1623   var from;                   /* where to copy match bytes from */
1624   var from_source;
1625   var here = 0;               /* current decoding table entry */
1626   var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
1627   //var last;                   /* parent table entry */
1628   var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
1629   var len;                    /* length to copy for repeats, bits to drop */
1630   var ret;                    /* return code */
1631   var hbuf = new utils.Buf8(4);    /* buffer for gzip header crc calculation */
1632   var opts;
1633 
1634   var n; // temporary var for NEED_BITS
1635 
1636   var order = /* permutation of code lengths */
1637     [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
1638 
1639 
1640   if (!strm || !strm.state || !strm.output ||
1641       (!strm.input && strm.avail_in !== 0)) {
1642     return Z_STREAM_ERROR;
1643   }
1644 
1645   state = strm.state;
1646   if (state.mode === TYPE) { state.mode = TYPEDO; }    /* skip check */
1647 
1648 
1649   //--- LOAD() ---
1650   put = strm.next_out;
1651   output = strm.output;
1652   left = strm.avail_out;
1653   next = strm.next_in;
1654   input = strm.input;
1655   have = strm.avail_in;
1656   hold = state.hold;
1657   bits = state.bits;
1658   //---
1659 
1660   _in = have;
1661   _out = left;
1662   ret = Z_OK;
1663 
1664   inf_leave: // goto emulation
1665   for (;;) {
1666     switch (state.mode) {
1667     case HEAD:
1668       if (state.wrap === 0) {
1669         state.mode = TYPEDO;
1670         break;
1671       }
1672       //=== NEEDBITS(16);
1673       while (bits < 16) {
1674         if (have === 0) { break inf_leave; }
1675         have--;
1676         hold += input[next++] << bits;
1677         bits += 8;
1678       }
1679       //===//
1680       if ((state.wrap & 2) && hold === 0x8b1f) {  /* gzip header */
1681         state.check = 0/*crc32(0L, Z_NULL, 0)*/;
1682         //=== CRC2(state.check, hold);
1683         hbuf[0] = hold & 0xff;
1684         hbuf[1] = (hold >>> 8) & 0xff;
1685         state.check = crc32(state.check, hbuf, 2, 0);
1686         //===//
1687 
1688         //=== INITBITS();
1689         hold = 0;
1690         bits = 0;
1691         //===//
1692         state.mode = FLAGS;
1693         break;
1694       }
1695       state.flags = 0;           /* expect zlib header */
1696       if (state.head) {
1697         state.head.done = false;
1698       }
1699       if (!(state.wrap & 1) ||   /* check if zlib header allowed */
1700         (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
1701         strm.msg = 'incorrect header check';
1702         state.mode = BAD;
1703         break;
1704       }
1705       if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
1706         strm.msg = 'unknown compression method';
1707         state.mode = BAD;
1708         break;
1709       }
1710       //--- DROPBITS(4) ---//
1711       hold >>>= 4;
1712       bits -= 4;
1713       //---//
1714       len = (hold & 0x0f)/*BITS(4)*/ + 8;
1715       if (state.wbits === 0) {
1716         state.wbits = len;
1717       }
1718       else if (len > state.wbits) {
1719         strm.msg = 'invalid window size';
1720         state.mode = BAD;
1721         break;
1722       }
1723       state.dmax = 1 << len;
1724       //Tracev((stderr, "inflate:   zlib header ok\n"));
1725       strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
1726       state.mode = hold & 0x200 ? DICTID : TYPE;
1727       //=== INITBITS();
1728       hold = 0;
1729       bits = 0;
1730       //===//
1731       break;
1732     case FLAGS:
1733       //=== NEEDBITS(16); */
1734       while (bits < 16) {
1735         if (have === 0) { break inf_leave; }
1736         have--;
1737         hold += input[next++] << bits;
1738         bits += 8;
1739       }
1740       //===//
1741       state.flags = hold;
1742       if ((state.flags & 0xff) !== Z_DEFLATED) {
1743         strm.msg = 'unknown compression method';
1744         state.mode = BAD;
1745         break;
1746       }
1747       if (state.flags & 0xe000) {
1748         strm.msg = 'unknown header flags set';
1749         state.mode = BAD;
1750         break;
1751       }
1752       if (state.head) {
1753         state.head.text = ((hold >> 8) & 1);
1754       }
1755       if (state.flags & 0x0200) {
1756         //=== CRC2(state.check, hold);
1757         hbuf[0] = hold & 0xff;
1758         hbuf[1] = (hold >>> 8) & 0xff;
1759         state.check = crc32(state.check, hbuf, 2, 0);
1760         //===//
1761       }
1762       //=== INITBITS();
1763       hold = 0;
1764       bits = 0;
1765       //===//
1766       state.mode = TIME;
1767       /* falls through */
1768     case TIME:
1769       //=== NEEDBITS(32); */
1770       while (bits < 32) {
1771         if (have === 0) { break inf_leave; }
1772         have--;
1773         hold += input[next++] << bits;
1774         bits += 8;
1775       }
1776       //===//
1777       if (state.head) {
1778         state.head.time = hold;
1779       }
1780       if (state.flags & 0x0200) {
1781         //=== CRC4(state.check, hold)
1782         hbuf[0] = hold & 0xff;
1783         hbuf[1] = (hold >>> 8) & 0xff;
1784         hbuf[2] = (hold >>> 16) & 0xff;
1785         hbuf[3] = (hold >>> 24) & 0xff;
1786         state.check = crc32(state.check, hbuf, 4, 0);
1787         //===
1788       }
1789       //=== INITBITS();
1790       hold = 0;
1791       bits = 0;
1792       //===//
1793       state.mode = OS;
1794       /* falls through */
1795     case OS:
1796       //=== NEEDBITS(16); */
1797       while (bits < 16) {
1798         if (have === 0) { break inf_leave; }
1799         have--;
1800         hold += input[next++] << bits;
1801         bits += 8;
1802       }
1803       //===//
1804       if (state.head) {
1805         state.head.xflags = (hold & 0xff);
1806         state.head.os = (hold >> 8);
1807       }
1808       if (state.flags & 0x0200) {
1809         //=== CRC2(state.check, hold);
1810         hbuf[0] = hold & 0xff;
1811         hbuf[1] = (hold >>> 8) & 0xff;
1812         state.check = crc32(state.check, hbuf, 2, 0);
1813         //===//
1814       }
1815       //=== INITBITS();
1816       hold = 0;
1817       bits = 0;
1818       //===//
1819       state.mode = EXLEN;
1820       /* falls through */
1821     case EXLEN:
1822       if (state.flags & 0x0400) {
1823         //=== NEEDBITS(16); */
1824         while (bits < 16) {
1825           if (have === 0) { break inf_leave; }
1826           have--;
1827           hold += input[next++] << bits;
1828           bits += 8;
1829         }
1830         //===//
1831         state.length = hold;
1832         if (state.head) {
1833           state.head.extra_len = hold;
1834         }
1835         if (state.flags & 0x0200) {
1836           //=== CRC2(state.check, hold);
1837           hbuf[0] = hold & 0xff;
1838           hbuf[1] = (hold >>> 8) & 0xff;
1839           state.check = crc32(state.check, hbuf, 2, 0);
1840           //===//
1841         }
1842         //=== INITBITS();
1843         hold = 0;
1844         bits = 0;
1845         //===//
1846       }
1847       else if (state.head) {
1848         state.head.extra = null/*Z_NULL*/;
1849       }
1850       state.mode = EXTRA;
1851       /* falls through */
1852     case EXTRA:
1853       if (state.flags & 0x0400) {
1854         copy = state.length;
1855         if (copy > have) { copy = have; }
1856         if (copy) {
1857           if (state.head) {
1858             len = state.head.extra_len - state.length;
1859             if (!state.head.extra) {
1860               // Use untyped array for more conveniend processing later
1861               state.head.extra = new Array(state.head.extra_len);
1862             }
1863             utils.arraySet(
1864               state.head.extra,
1865               input,
1866               next,
1867               // extra field is limited to 65536 bytes
1868               // - no need for additional size check
1869               copy,
1870               /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
1871               len
1872             );
1873             //zmemcpy(state.head.extra + len, next,
1874             //        len + copy > state.head.extra_max ?
1875             //        state.head.extra_max - len : copy);
1876           }
1877           if (state.flags & 0x0200) {
1878             state.check = crc32(state.check, input, copy, next);
1879           }
1880           have -= copy;
1881           next += copy;
1882           state.length -= copy;
1883         }
1884         if (state.length) { break inf_leave; }
1885       }
1886       state.length = 0;
1887       state.mode = NAME;
1888       /* falls through */
1889     case NAME:
1890       if (state.flags & 0x0800) {
1891         if (have === 0) { break inf_leave; }
1892         copy = 0;
1893         do {
1894           // TODO: 2 or 1 bytes?
1895           len = input[next + copy++];
1896           /* use constant limit because in js we should not preallocate memory */
1897           if (state.head && len &&
1898               (state.length < 65536 /*state.head.name_max*/)) {
1899             state.head.name += String.fromCharCode(len);
1900           }
1901         } while (len && copy < have);
1902 
1903         if (state.flags & 0x0200) {
1904           state.check = crc32(state.check, input, copy, next);
1905         }
1906         have -= copy;
1907         next += copy;
1908         if (len) { break inf_leave; }
1909       }
1910       else if (state.head) {
1911         state.head.name = null;
1912       }
1913       state.length = 0;
1914       state.mode = COMMENT;
1915       /* falls through */
1916     case COMMENT:
1917       if (state.flags & 0x1000) {
1918         if (have === 0) { break inf_leave; }
1919         copy = 0;
1920         do {
1921           len = input[next + copy++];
1922           /* use constant limit because in js we should not preallocate memory */
1923           if (state.head && len &&
1924               (state.length < 65536 /*state.head.comm_max*/)) {
1925             state.head.comment += String.fromCharCode(len);
1926           }
1927         } while (len && copy < have);
1928         if (state.flags & 0x0200) {
1929           state.check = crc32(state.check, input, copy, next);
1930         }
1931         have -= copy;
1932         next += copy;
1933         if (len) { break inf_leave; }
1934       }
1935       else if (state.head) {
1936         state.head.comment = null;
1937       }
1938       state.mode = HCRC;
1939       /* falls through */
1940     case HCRC:
1941       if (state.flags & 0x0200) {
1942         //=== NEEDBITS(16); */
1943         while (bits < 16) {
1944           if (have === 0) { break inf_leave; }
1945           have--;
1946           hold += input[next++] << bits;
1947           bits += 8;
1948         }
1949         //===//
1950         if (hold !== (state.check & 0xffff)) {
1951           strm.msg = 'header crc mismatch';
1952           state.mode = BAD;
1953           break;
1954         }
1955         //=== INITBITS();
1956         hold = 0;
1957         bits = 0;
1958         //===//
1959       }
1960       if (state.head) {
1961         state.head.hcrc = ((state.flags >> 9) & 1);
1962         state.head.done = true;
1963       }
1964       strm.adler = state.check = 0 /*crc32(0L, Z_NULL, 0)*/;
1965       state.mode = TYPE;
1966       break;
1967     case DICTID:
1968       //=== NEEDBITS(32); */
1969       while (bits < 32) {
1970         if (have === 0) { break inf_leave; }
1971         have--;
1972         hold += input[next++] << bits;
1973         bits += 8;
1974       }
1975       //===//
1976       strm.adler = state.check = ZSWAP32(hold);
1977       //=== INITBITS();
1978       hold = 0;
1979       bits = 0;
1980       //===//
1981       state.mode = DICT;
1982       /* falls through */
1983     case DICT:
1984       if (state.havedict === 0) {
1985         //--- RESTORE() ---
1986         strm.next_out = put;
1987         strm.avail_out = left;
1988         strm.next_in = next;
1989         strm.avail_in = have;
1990         state.hold = hold;
1991         state.bits = bits;
1992         //---
1993         return Z_NEED_DICT;
1994       }
1995       strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
1996       state.mode = TYPE;
1997       /* falls through */
1998     case TYPE:
1999       if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
2000       /* falls through */
2001     case TYPEDO:
2002       if (state.last) {
2003         //--- BYTEBITS() ---//
2004         hold >>>= bits & 7;
2005         bits -= bits & 7;
2006         //---//
2007         state.mode = CHECK;
2008         break;
2009       }
2010       //=== NEEDBITS(3); */
2011       while (bits < 3) {
2012         if (have === 0) { break inf_leave; }
2013         have--;
2014         hold += input[next++] << bits;
2015         bits += 8;
2016       }
2017       //===//
2018       state.last = (hold & 0x01)/*BITS(1)*/;
2019       //--- DROPBITS(1) ---//
2020       hold >>>= 1;
2021       bits -= 1;
2022       //---//
2023 
2024       switch ((hold & 0x03)/*BITS(2)*/) {
2025       case 0:                             /* stored block */
2026         //Tracev((stderr, "inflate:     stored block%s\n",
2027         //        state.last ? " (last)" : ""));
2028         state.mode = STORED;
2029         break;
2030       case 1:                             /* fixed block */
2031         fixedtables(state);
2032         //Tracev((stderr, "inflate:     fixed codes block%s\n",
2033         //        state.last ? " (last)" : ""));
2034         state.mode = LEN_;             /* decode codes */
2035         if (flush === Z_TREES) {
2036           //--- DROPBITS(2) ---//
2037           hold >>>= 2;
2038           bits -= 2;
2039           //---//
2040           break inf_leave;
2041         }
2042         break;
2043       case 2:                             /* dynamic block */
2044         //Tracev((stderr, "inflate:     dynamic codes block%s\n",
2045         //        state.last ? " (last)" : ""));
2046         state.mode = TABLE;
2047         break;
2048       case 3:
2049         strm.msg = 'invalid block type';
2050         state.mode = BAD;
2051       }
2052       //--- DROPBITS(2) ---//
2053       hold >>>= 2;
2054       bits -= 2;
2055       //---//
2056       break;
2057     case STORED:
2058       //--- BYTEBITS() ---// /* go to byte boundary */
2059       hold >>>= bits & 7;
2060       bits -= bits & 7;
2061       //---//
2062       //=== NEEDBITS(32); */
2063       while (bits < 32) {
2064         if (have === 0) { break inf_leave; }
2065         have--;
2066         hold += input[next++] << bits;
2067         bits += 8;
2068       }
2069       //===//
2070       if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
2071         strm.msg = 'invalid stored block lengths';
2072         state.mode = BAD;
2073         break;
2074       }
2075       state.length = hold & 0xffff;
2076       //Tracev((stderr, "inflate:       stored length %u\n",
2077       //        state.length));
2078       //=== INITBITS();
2079       hold = 0;
2080       bits = 0;
2081       //===//
2082       state.mode = COPY_;
2083       if (flush === Z_TREES) { break inf_leave; }
2084       /* falls through */
2085     case COPY_:
2086       state.mode = COPY;
2087       /* falls through */
2088     case COPY:
2089       copy = state.length;
2090       if (copy) {
2091         if (copy > have) { copy = have; }
2092         if (copy > left) { copy = left; }
2093         if (copy === 0) { break inf_leave; }
2094         //--- zmemcpy(put, next, copy); ---
2095         utils.arraySet(output, input, next, copy, put);
2096         //---//
2097         have -= copy;
2098         next += copy;
2099         left -= copy;
2100         put += copy;
2101         state.length -= copy;
2102         break;
2103       }
2104       //Tracev((stderr, "inflate:       stored end\n"));
2105       state.mode = TYPE;
2106       break;
2107     case TABLE:
2108       //=== NEEDBITS(14); */
2109       while (bits < 14) {
2110         if (have === 0) { break inf_leave; }
2111         have--;
2112         hold += input[next++] << bits;
2113         bits += 8;
2114       }
2115       //===//
2116       state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
2117       //--- DROPBITS(5) ---//
2118       hold >>>= 5;
2119       bits -= 5;
2120       //---//
2121       state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
2122       //--- DROPBITS(5) ---//
2123       hold >>>= 5;
2124       bits -= 5;
2125       //---//
2126       state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
2127       //--- DROPBITS(4) ---//
2128       hold >>>= 4;
2129       bits -= 4;
2130       //---//
2131 //#ifndef PKZIP_BUG_WORKAROUND
2132       if (state.nlen > 286 || state.ndist > 30) {
2133         strm.msg = 'too many length or distance symbols';
2134         state.mode = BAD;
2135         break;
2136       }
2137 //#endif
2138       //Tracev((stderr, "inflate:       table sizes ok\n"));
2139       state.have = 0;
2140       state.mode = LENLENS;
2141       /* falls through */
2142     case LENLENS:
2143       while (state.have < state.ncode) {
2144         //=== NEEDBITS(3);
2145         while (bits < 3) {
2146           if (have === 0) { break inf_leave; }
2147           have--;
2148           hold += input[next++] << bits;
2149           bits += 8;
2150         }
2151         //===//
2152         state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
2153         //--- DROPBITS(3) ---//
2154         hold >>>= 3;
2155         bits -= 3;
2156         //---//
2157       }
2158       while (state.have < 19) {
2159         state.lens[order[state.have++]] = 0;
2160       }
2161       // We have separate tables & no pointers. 2 commented lines below not needed.
2162       //state.next = state.codes;
2163       //state.lencode = state.next;
2164       // Switch to use dynamic table
2165       state.lencode = state.lendyn;
2166       state.lenbits = 7;
2167 
2168       opts = {bits: state.lenbits};
2169       ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
2170       state.lenbits = opts.bits;
2171 
2172       if (ret) {
2173         strm.msg = 'invalid code lengths set';
2174         state.mode = BAD;
2175         break;
2176       }
2177       //Tracev((stderr, "inflate:       code lengths ok\n"));
2178       state.have = 0;
2179       state.mode = CODELENS;
2180       /* falls through */
2181     case CODELENS:
2182       while (state.have < state.nlen + state.ndist) {
2183         for (;;) {
2184           here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
2185           here_bits = here >>> 24;
2186           here_op = (here >>> 16) & 0xff;
2187           here_val = here & 0xffff;
2188 
2189           if ((here_bits) <= bits) { break; }
2190           //--- PULLBYTE() ---//
2191           if (have === 0) { break inf_leave; }
2192           have--;
2193           hold += input[next++] << bits;
2194           bits += 8;
2195           //---//
2196         }
2197         if (here_val < 16) {
2198           //--- DROPBITS(here.bits) ---//
2199           hold >>>= here_bits;
2200           bits -= here_bits;
2201           //---//
2202           state.lens[state.have++] = here_val;
2203         }
2204         else {
2205           if (here_val === 16) {
2206             //=== NEEDBITS(here.bits + 2);
2207             n = here_bits + 2;
2208             while (bits < n) {
2209               if (have === 0) { break inf_leave; }
2210               have--;
2211               hold += input[next++] << bits;
2212               bits += 8;
2213             }
2214             //===//
2215             //--- DROPBITS(here.bits) ---//
2216             hold >>>= here_bits;
2217             bits -= here_bits;
2218             //---//
2219             if (state.have === 0) {
2220               strm.msg = 'invalid bit length repeat';
2221               state.mode = BAD;
2222               break;
2223             }
2224             len = state.lens[state.have - 1];
2225             copy = 3 + (hold & 0x03);//BITS(2);
2226             //--- DROPBITS(2) ---//
2227             hold >>>= 2;
2228             bits -= 2;
2229             //---//
2230           }
2231           else if (here_val === 17) {
2232             //=== NEEDBITS(here.bits + 3);
2233             n = here_bits + 3;
2234             while (bits < n) {
2235               if (have === 0) { break inf_leave; }
2236               have--;
2237               hold += input[next++] << bits;
2238               bits += 8;
2239             }
2240             //===//
2241             //--- DROPBITS(here.bits) ---//
2242             hold >>>= here_bits;
2243             bits -= here_bits;
2244             //---//
2245             len = 0;
2246             copy = 3 + (hold & 0x07);//BITS(3);
2247             //--- DROPBITS(3) ---//
2248             hold >>>= 3;
2249             bits -= 3;
2250             //---//
2251           }
2252           else {
2253             //=== NEEDBITS(here.bits + 7);
2254             n = here_bits + 7;
2255             while (bits < n) {
2256               if (have === 0) { break inf_leave; }
2257               have--;
2258               hold += input[next++] << bits;
2259               bits += 8;
2260             }
2261             //===//
2262             //--- DROPBITS(here.bits) ---//
2263             hold >>>= here_bits;
2264             bits -= here_bits;
2265             //---//
2266             len = 0;
2267             copy = 11 + (hold & 0x7f);//BITS(7);
2268             //--- DROPBITS(7) ---//
2269             hold >>>= 7;
2270             bits -= 7;
2271             //---//
2272           }
2273           if (state.have + copy > state.nlen + state.ndist) {
2274             strm.msg = 'invalid bit length repeat';
2275             state.mode = BAD;
2276             break;
2277           }
2278           while (copy--) {
2279             state.lens[state.have++] = len;
2280           }
2281         }
2282       }
2283 
2284       /* handle error breaks in while */
2285       if (state.mode === BAD) { break; }
2286 
2287       /* check for end-of-block code (better have one) */
2288       if (state.lens[256] === 0) {
2289         strm.msg = 'invalid code -- missing end-of-block';
2290         state.mode = BAD;
2291         break;
2292       }
2293 
2294       /* build code tables -- note: do not change the lenbits or distbits
2295          values here (9 and 6) without reading the comments in inftrees.h
2296          concerning the ENOUGH constants, which depend on those values */
2297       state.lenbits = 9;
2298 
2299       opts = {bits: state.lenbits};
2300       ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
2301       // We have separate tables & no pointers. 2 commented lines below not needed.
2302       // state.next_index = opts.table_index;
2303       state.lenbits = opts.bits;
2304       // state.lencode = state.next;
2305 
2306       if (ret) {
2307         strm.msg = 'invalid literal/lengths set';
2308         state.mode = BAD;
2309         break;
2310       }
2311 
2312       state.distbits = 6;
2313       //state.distcode.copy(state.codes);
2314       // Switch to use dynamic table
2315       state.distcode = state.distdyn;
2316       opts = {bits: state.distbits};
2317       ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
2318       // We have separate tables & no pointers. 2 commented lines below not needed.
2319       // state.next_index = opts.table_index;
2320       state.distbits = opts.bits;
2321       // state.distcode = state.next;
2322 
2323       if (ret) {
2324         strm.msg = 'invalid distances set';
2325         state.mode = BAD;
2326         break;
2327       }
2328       //Tracev((stderr, 'inflate:       codes ok\n'));
2329       state.mode = LEN_;
2330       if (flush === Z_TREES) { break inf_leave; }
2331       /* falls through */
2332     case LEN_:
2333       state.mode = LEN;
2334       /* falls through */
2335     case LEN:
2336       if (have >= 6 && left >= 258) {
2337         //--- RESTORE() ---
2338         strm.next_out = put;
2339         strm.avail_out = left;
2340         strm.next_in = next;
2341         strm.avail_in = have;
2342         state.hold = hold;
2343         state.bits = bits;
2344         //---
2345         inflate_fast(strm, _out);
2346         //--- LOAD() ---
2347         put = strm.next_out;
2348         output = strm.output;
2349         left = strm.avail_out;
2350         next = strm.next_in;
2351         input = strm.input;
2352         have = strm.avail_in;
2353         hold = state.hold;
2354         bits = state.bits;
2355         //---
2356 
2357         if (state.mode === TYPE) {
2358           state.back = -1;
2359         }
2360         break;
2361       }
2362       state.back = 0;
2363       for (;;) {
2364         here = state.lencode[hold & ((1 << state.lenbits) -1)];  /*BITS(state.lenbits)*/
2365         here_bits = here >>> 24;
2366         here_op = (here >>> 16) & 0xff;
2367         here_val = here & 0xffff;
2368 
2369         if (here_bits <= bits) { break; }
2370         //--- PULLBYTE() ---//
2371         if (have === 0) { break inf_leave; }
2372         have--;
2373         hold += input[next++] << bits;
2374         bits += 8;
2375         //---//
2376       }
2377       if (here_op && (here_op & 0xf0) === 0) {
2378         last_bits = here_bits;
2379         last_op = here_op;
2380         last_val = here_val;
2381         for (;;) {
2382           here = state.lencode[last_val +
2383                   ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
2384           here_bits = here >>> 24;
2385           here_op = (here >>> 16) & 0xff;
2386           here_val = here & 0xffff;
2387 
2388           if ((last_bits + here_bits) <= bits) { break; }
2389           //--- PULLBYTE() ---//
2390           if (have === 0) { break inf_leave; }
2391           have--;
2392           hold += input[next++] << bits;
2393           bits += 8;
2394           //---//
2395         }
2396         //--- DROPBITS(last.bits) ---//
2397         hold >>>= last_bits;
2398         bits -= last_bits;
2399         //---//
2400         state.back += last_bits;
2401       }
2402       //--- DROPBITS(here.bits) ---//
2403       hold >>>= here_bits;
2404       bits -= here_bits;
2405       //---//
2406       state.back += here_bits;
2407       state.length = here_val;
2408       if (here_op === 0) {
2409         //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
2410         //        "inflate:         literal '%c'\n" :
2411         //        "inflate:         literal 0x%02x\n", here.val));
2412         state.mode = LIT;
2413         break;
2414       }
2415       if (here_op & 32) {
2416         //Tracevv((stderr, "inflate:         end of block\n"));
2417         state.back = -1;
2418         state.mode = TYPE;
2419         break;
2420       }
2421       if (here_op & 64) {
2422         strm.msg = 'invalid literal/length code';
2423         state.mode = BAD;
2424         break;
2425       }
2426       state.extra = here_op & 15;
2427       state.mode = LENEXT;
2428       /* falls through */
2429     case LENEXT:
2430       if (state.extra) {
2431         //=== NEEDBITS(state.extra);
2432         n = state.extra;
2433         while (bits < n) {
2434           if (have === 0) { break inf_leave; }
2435           have--;
2436           hold += input[next++] << bits;
2437           bits += 8;
2438         }
2439         //===//
2440         state.length += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
2441         //--- DROPBITS(state.extra) ---//
2442         hold >>>= state.extra;
2443         bits -= state.extra;
2444         //---//
2445         state.back += state.extra;
2446       }
2447       //Tracevv((stderr, "inflate:         length %u\n", state.length));
2448       state.was = state.length;
2449       state.mode = DIST;
2450       /* falls through */
2451     case DIST:
2452       for (;;) {
2453         here = state.distcode[hold & ((1 << state.distbits) -1)];/*BITS(state.distbits)*/
2454         here_bits = here >>> 24;
2455         here_op = (here >>> 16) & 0xff;
2456         here_val = here & 0xffff;
2457 
2458         if ((here_bits) <= bits) { break; }
2459         //--- PULLBYTE() ---//
2460         if (have === 0) { break inf_leave; }
2461         have--;
2462         hold += input[next++] << bits;
2463         bits += 8;
2464         //---//
2465       }
2466       if ((here_op & 0xf0) === 0) {
2467         last_bits = here_bits;
2468         last_op = here_op;
2469         last_val = here_val;
2470         for (;;) {
2471           here = state.distcode[last_val +
2472                   ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
2473           here_bits = here >>> 24;
2474           here_op = (here >>> 16) & 0xff;
2475           here_val = here & 0xffff;
2476 
2477           if ((last_bits + here_bits) <= bits) { break; }
2478           //--- PULLBYTE() ---//
2479           if (have === 0) { break inf_leave; }
2480           have--;
2481           hold += input[next++] << bits;
2482           bits += 8;
2483           //---//
2484         }
2485         //--- DROPBITS(last.bits) ---//
2486         hold >>>= last_bits;
2487         bits -= last_bits;
2488         //---//
2489         state.back += last_bits;
2490       }
2491       //--- DROPBITS(here.bits) ---//
2492       hold >>>= here_bits;
2493       bits -= here_bits;
2494       //---//
2495       state.back += here_bits;
2496       if (here_op & 64) {
2497         strm.msg = 'invalid distance code';
2498         state.mode = BAD;
2499         break;
2500       }
2501       state.offset = here_val;
2502       state.extra = (here_op) & 15;
2503       state.mode = DISTEXT;
2504       /* falls through */
2505     case DISTEXT:
2506       if (state.extra) {
2507         //=== NEEDBITS(state.extra);
2508         n = state.extra;
2509         while (bits < n) {
2510           if (have === 0) { break inf_leave; }
2511           have--;
2512           hold += input[next++] << bits;
2513           bits += 8;
2514         }
2515         //===//
2516         state.offset += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
2517         //--- DROPBITS(state.extra) ---//
2518         hold >>>= state.extra;
2519         bits -= state.extra;
2520         //---//
2521         state.back += state.extra;
2522       }
2523 //#ifdef INFLATE_STRICT
2524       if (state.offset > state.dmax) {
2525         strm.msg = 'invalid distance too far back';
2526         state.mode = BAD;
2527         break;
2528       }
2529 //#endif
2530       //Tracevv((stderr, "inflate:         distance %u\n", state.offset));
2531       state.mode = MATCH;
2532       /* falls through */
2533     case MATCH:
2534       if (left === 0) { break inf_leave; }
2535       copy = _out - left;
2536       if (state.offset > copy) {         /* copy from window */
2537         copy = state.offset - copy;
2538         if (copy > state.whave) {
2539           if (state.sane) {
2540             strm.msg = 'invalid distance too far back';
2541             state.mode = BAD;
2542             break;
2543           }
2544 // (!) This block is disabled in zlib defailts,
2545 // don't enable it for binary compatibility
2546 //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
2547 //          Trace((stderr, "inflate.c too far\n"));
2548 //          copy -= state.whave;
2549 //          if (copy > state.length) { copy = state.length; }
2550 //          if (copy > left) { copy = left; }
2551 //          left -= copy;
2552 //          state.length -= copy;
2553 //          do {
2554 //            output[put++] = 0;
2555 //          } while (--copy);
2556 //          if (state.length === 0) { state.mode = LEN; }
2557 //          break;
2558 //#endif
2559         }
2560         if (copy > state.wnext) {
2561           copy -= state.wnext;
2562           from = state.wsize - copy;
2563         }
2564         else {
2565           from = state.wnext - copy;
2566         }
2567         if (copy > state.length) { copy = state.length; }
2568         from_source = state.window;
2569       }
2570       else {                              /* copy from output */
2571         from_source = output;
2572         from = put - state.offset;
2573         copy = state.length;
2574       }
2575       if (copy > left) { copy = left; }
2576       left -= copy;
2577       state.length -= copy;
2578       do {
2579         output[put++] = from_source[from++];
2580       } while (--copy);
2581       if (state.length === 0) { state.mode = LEN; }
2582       break;
2583     case LIT:
2584       if (left === 0) { break inf_leave; }
2585       output[put++] = state.length;
2586       left--;
2587       state.mode = LEN;
2588       break;
2589     case CHECK:
2590       if (state.wrap) {
2591         //=== NEEDBITS(32);
2592         while (bits < 32) {
2593           if (have === 0) { break inf_leave; }
2594           have--;
2595           // Use '|' insdead of '+' to make sure that result is signed
2596           hold |= input[next++] << bits;
2597           bits += 8;
2598         }
2599         //===//
2600         _out -= left;
2601         strm.total_out += _out;
2602         state.total += _out;
2603         if (_out) {
2604           strm.adler = state.check =
2605               /*UPDATE(state.check, put - _out, _out);*/
2606               (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
2607 
2608         }
2609         _out = left;
2610         // NB: crc32 stored as signed 32-bit int, ZSWAP32 returns signed too
2611         if ((state.flags ? hold : ZSWAP32(hold)) !== state.check) {
2612           strm.msg = 'incorrect data check';
2613           state.mode = BAD;
2614           break;
2615         }
2616         //=== INITBITS();
2617         hold = 0;
2618         bits = 0;
2619         //===//
2620         //Tracev((stderr, "inflate:   check matches trailer\n"));
2621       }
2622       state.mode = LENGTH;
2623       /* falls through */
2624     case LENGTH:
2625       if (state.wrap && state.flags) {
2626         //=== NEEDBITS(32);
2627         while (bits < 32) {
2628           if (have === 0) { break inf_leave; }
2629           have--;
2630           hold += input[next++] << bits;
2631           bits += 8;
2632         }
2633         //===//
2634         if (hold !== (state.total & 0xffffffff)) {
2635           strm.msg = 'incorrect length check';
2636           state.mode = BAD;
2637           break;
2638         }
2639         //=== INITBITS();
2640         hold = 0;
2641         bits = 0;
2642         //===//
2643         //Tracev((stderr, "inflate:   length matches trailer\n"));
2644       }
2645       state.mode = DONE;
2646       /* falls through */
2647     case DONE:
2648       ret = Z_STREAM_END;
2649       break inf_leave;
2650     case BAD:
2651       ret = Z_DATA_ERROR;
2652       break inf_leave;
2653     case MEM:
2654       return Z_MEM_ERROR;
2655     case SYNC:
2656       /* falls through */
2657     default:
2658       return Z_STREAM_ERROR;
2659     }
2660   }
2661 
2662   // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
2663 
2664   /*
2665      Return from inflate(), updating the total counts and the check value.
2666      If there was no progress during the inflate() call, return a buffer
2667      error.  Call updatewindow() to create and/or update the window state.
2668      Note: a memory error from inflate() is non-recoverable.
2669    */
2670 
2671   //--- RESTORE() ---
2672   strm.next_out = put;
2673   strm.avail_out = left;
2674   strm.next_in = next;
2675   strm.avail_in = have;
2676   state.hold = hold;
2677   state.bits = bits;
2678   //---
2679 
2680   if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
2681                       (state.mode < CHECK || flush !== Z_FINISH))) {
2682     if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
2683       state.mode = MEM;
2684       return Z_MEM_ERROR;
2685     }
2686   }
2687   _in -= strm.avail_in;
2688   _out -= strm.avail_out;
2689   strm.total_in += _in;
2690   strm.total_out += _out;
2691   state.total += _out;
2692   if (state.wrap && _out) {
2693     strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
2694       (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
2695   }
2696   strm.data_type = state.bits + (state.last ? 64 : 0) +
2697                     (state.mode === TYPE ? 128 : 0) +
2698                     (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
2699   if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
2700     ret = Z_BUF_ERROR;
2701   }
2702   return ret;
2703 }
2704 
2705 function inflateEnd(strm) {
2706 
2707   if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
2708     return Z_STREAM_ERROR;
2709   }
2710 
2711   var state = strm.state;
2712   if (state.window) {
2713     state.window = null;
2714   }
2715   strm.state = null;
2716   return Z_OK;
2717 }
2718 
2719 function inflateGetHeader(strm, head) {
2720   var state;
2721 
2722   /* check state */
2723   if (!strm || !strm.state) { return Z_STREAM_ERROR; }
2724   state = strm.state;
2725   if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
2726 
2727   /* save header structure */
2728   state.head = head;
2729   head.done = false;
2730   return Z_OK;
2731 }
2732 
2733 
2734 exports.inflateReset = inflateReset;
2735 exports.inflateReset2 = inflateReset2;
2736 exports.inflateResetKeep = inflateResetKeep;
2737 exports.inflateInit = inflateInit;
2738 exports.inflateInit2 = inflateInit2;
2739 exports.inflate = inflate;
2740 exports.inflateEnd = inflateEnd;
2741 exports.inflateGetHeader = inflateGetHeader;
2742 exports.inflateInfo = 'pako inflate (from Nodeca project)';
2743 
2744 /* Not implemented
2745 exports.inflateCopy = inflateCopy;
2746 exports.inflateGetDictionary = inflateGetDictionary;
2747 exports.inflateMark = inflateMark;
2748 exports.inflatePrime = inflatePrime;
2749 exports.inflateSetDictionary = inflateSetDictionary;
2750 exports.inflateSync = inflateSync;
2751 exports.inflateSyncPoint = inflateSyncPoint;
2752 exports.inflateUndermine = inflateUndermine;
2753 */
2754 },{"../utils/common":2,"./adler32":4,"./crc32":6,"./inffast":8,"./inftrees":10}],10:[function(_dereq_,module,exports){
2755 'use strict';
2756 
2757 
2758 var utils = _dereq_('../utils/common');
2759 
2760 var MAXBITS = 15;
2761 var ENOUGH_LENS = 852;
2762 var ENOUGH_DISTS = 592;
2763 //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
2764 
2765 var CODES = 0;
2766 var LENS = 1;
2767 var DISTS = 2;
2768 
2769 var lbase = [ /* Length codes 257..285 base */
2770   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2771   35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
2772 ];
2773 
2774 var lext = [ /* Length codes 257..285 extra */
2775   16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
2776   19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
2777 ];
2778 
2779 var dbase = [ /* Distance codes 0..29 base */
2780   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2781   257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2782   8193, 12289, 16385, 24577, 0, 0
2783 ];
2784 
2785 var dext = [ /* Distance codes 0..29 extra */
2786   16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
2787   23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
2788   28, 28, 29, 29, 64, 64
2789 ];
2790 
2791 module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
2792 {
2793   var bits = opts.bits;
2794       //here = opts.here; /* table entry for duplication */
2795 
2796   var len = 0;               /* a code's length in bits */
2797   var sym = 0;               /* index of code symbols */
2798   var min = 0, max = 0;          /* minimum and maximum code lengths */
2799   var root = 0;              /* number of index bits for root table */
2800   var curr = 0;              /* number of index bits for current table */
2801   var drop = 0;              /* code bits to drop for sub-table */
2802   var left = 0;                   /* number of prefix codes available */
2803   var used = 0;              /* code entries in table used */
2804   var huff = 0;              /* Huffman code */
2805   var incr;              /* for incrementing code, index */
2806   var fill;              /* index for replicating entries */
2807   var low;               /* low bits for current root entry */
2808   var mask;              /* mask for low root bits */
2809   var next;             /* next available space in table */
2810   var base = null;     /* base value table to use */
2811   var base_index = 0;
2812 //  var shoextra;    /* extra bits table to use */
2813   var end;                    /* use base and extra for symbol > end */
2814   var count = new utils.Buf16(MAXBITS+1); //[MAXBITS+1];    /* number of codes of each length */
2815   var offs = new utils.Buf16(MAXBITS+1); //[MAXBITS+1];     /* offsets in table for each length */
2816   var extra = null;
2817   var extra_index = 0;
2818 
2819   var here_bits, here_op, here_val;
2820 
2821   /*
2822    Process a set of code lengths to create a canonical Huffman code.  The
2823    code lengths are lens[0..codes-1].  Each length corresponds to the
2824    symbols 0..codes-1.  The Huffman code is generated by first sorting the
2825    symbols by length from short to long, and retaining the symbol order
2826    for codes with equal lengths.  Then the code starts with all zero bits
2827    for the first code of the shortest length, and the codes are integer
2828    increments for the same length, and zeros are appended as the length
2829    increases.  For the deflate format, these bits are stored backwards
2830    from their more natural integer increment ordering, and so when the
2831    decoding tables are built in the large loop below, the integer codes
2832    are incremented backwards.
2833 
2834    This routine assumes, but does not check, that all of the entries in
2835    lens[] are in the range 0..MAXBITS.  The caller must assure this.
2836    1..MAXBITS is interpreted as that code length.  zero means that that
2837    symbol does not occur in this code.
2838 
2839    The codes are sorted by computing a count of codes for each length,
2840    creating from that a table of starting indices for each length in the
2841    sorted table, and then entering the symbols in order in the sorted
2842    table.  The sorted table is work[], with that space being provided by
2843    the caller.
2844 
2845    The length counts are used for other purposes as well, i.e. finding
2846    the minimum and maximum length codes, determining if there are any
2847    codes at all, checking for a valid set of lengths, and looking ahead
2848    at length counts to determine sub-table sizes when building the
2849    decoding tables.
2850    */
2851 
2852   /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
2853   for (len = 0; len <= MAXBITS; len++) {
2854     count[len] = 0;
2855   }
2856   for (sym = 0; sym < codes; sym++) {
2857     count[lens[lens_index + sym]]++;
2858   }
2859 
2860   /* bound code lengths, force root to be within code lengths */
2861   root = bits;
2862   for (max = MAXBITS; max >= 1; max--) {
2863     if (count[max] !== 0) { break; }
2864   }
2865   if (root > max) {
2866     root = max;
2867   }
2868   if (max === 0) {                     /* no symbols to code at all */
2869     //table.op[opts.table_index] = 64;  //here.op = (var char)64;    /* invalid code marker */
2870     //table.bits[opts.table_index] = 1;   //here.bits = (var char)1;
2871     //table.val[opts.table_index++] = 0;   //here.val = (var short)0;
2872     table[table_index++] = (1 << 24) | (64 << 16) | 0;
2873 
2874 
2875     //table.op[opts.table_index] = 64;
2876     //table.bits[opts.table_index] = 1;
2877     //table.val[opts.table_index++] = 0;
2878     table[table_index++] = (1 << 24) | (64 << 16) | 0;
2879 
2880     opts.bits = 1;
2881     return 0;     /* no symbols, but wait for decoding to report error */
2882   }
2883   for (min = 1; min < max; min++) {
2884     if (count[min] !== 0) { break; }
2885   }
2886   if (root < min) {
2887     root = min;
2888   }
2889 
2890   /* check for an over-subscribed or incomplete set of lengths */
2891   left = 1;
2892   for (len = 1; len <= MAXBITS; len++) {
2893     left <<= 1;
2894     left -= count[len];
2895     if (left < 0) {
2896       return -1;
2897     }        /* over-subscribed */
2898   }
2899   if (left > 0 && (type === CODES || max !== 1)) {
2900     return -1;                      /* incomplete set */
2901   }
2902 
2903   /* generate offsets into symbol table for each length for sorting */
2904   offs[1] = 0;
2905   for (len = 1; len < MAXBITS; len++) {
2906     offs[len + 1] = offs[len] + count[len];
2907   }
2908 
2909   /* sort symbols by length, by symbol order within each length */
2910   for (sym = 0; sym < codes; sym++) {
2911     if (lens[lens_index + sym] !== 0) {
2912       work[offs[lens[lens_index + sym]]++] = sym;
2913     }
2914   }
2915 
2916   /*
2917    Create and fill in decoding tables.  In this loop, the table being
2918    filled is at next and has curr index bits.  The code being used is huff
2919    with length len.  That code is converted to an index by dropping drop
2920    bits off of the bottom.  For codes where len is less than drop + curr,
2921    those top drop + curr - len bits are incremented through all values to
2922    fill the table with replicated entries.
2923 
2924    root is the number of index bits for the root table.  When len exceeds
2925    root, sub-tables are created pointed to by the root entry with an index
2926    of the low root bits of huff.  This is saved in low to check for when a
2927    new sub-table should be started.  drop is zero when the root table is
2928    being filled, and drop is root when sub-tables are being filled.
2929 
2930    When a new sub-table is needed, it is necessary to look ahead in the
2931    code lengths to determine what size sub-table is needed.  The length
2932    counts are used for this, and so count[] is decremented as codes are
2933    entered in the tables.
2934 
2935    used keeps track of how many table entries have been allocated from the
2936    provided *table space.  It is checked for LENS and DIST tables against
2937    the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
2938    the initial root table size constants.  See the comments in inftrees.h
2939    for more information.
2940 
2941    sym increments through all symbols, and the loop terminates when
2942    all codes of length max, i.e. all codes, have been processed.  This
2943    routine permits incomplete codes, so another loop after this one fills
2944    in the rest of the decoding tables with invalid code markers.
2945    */
2946 
2947   /* set up for code type */
2948   switch (type) {
2949     case CODES:
2950       base = extra = work;    /* dummy value--not used */
2951       end = 19;
2952       break;
2953     case LENS:
2954       base = lbase;
2955       base_index -= 257;
2956       extra = lext;
2957       extra_index -= 257;
2958       end = 256;
2959       break;
2960     default:            /* DISTS */
2961       base = dbase;
2962       extra = dext;
2963       end = -1;
2964   }
2965 
2966   /* initialize opts for loop */
2967   huff = 0;                   /* starting code */
2968   sym = 0;                    /* starting code symbol */
2969   len = min;                  /* starting code length */
2970   next = table_index;              /* current table to fill in */
2971   curr = root;                /* current table index bits */
2972   drop = 0;                   /* current bits to drop from code for index */
2973   low = -1;                   /* trigger new sub-table when len > root */
2974   used = 1 << root;          /* use root table entries */
2975   mask = used - 1;            /* mask for comparing low */
2976 
2977   /* check available table space */
2978   if ((type === LENS && used > ENOUGH_LENS) ||
2979     (type === DISTS && used > ENOUGH_DISTS)) {
2980     return 1;
2981   }
2982 
2983   var i=0;
2984   /* process all codes and make table entries */
2985   for (;;) {
2986     i++;
2987     /* create table entry */
2988     here_bits = len - drop;
2989     if (work[sym] < end) {
2990       here_op = 0;
2991       here_val = work[sym];
2992     }
2993     else if (work[sym] > end) {
2994       here_op = extra[extra_index + work[sym]];
2995       here_val = base[base_index + work[sym]];
2996     }
2997     else {
2998       here_op = 32 + 64;         /* end of block */
2999       here_val = 0;
3000     }
3001 
3002     /* replicate for those indices with low len bits equal to huff */
3003     incr = 1 << (len - drop);
3004     fill = 1 << curr;
3005     min = fill;                 /* save offset to next table */
3006     do {
3007       fill -= incr;
3008       table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
3009     } while (fill !== 0);
3010 
3011     /* backwards increment the len-bit code huff */
3012     incr = 1 << (len - 1);
3013     while (huff & incr) {
3014       incr >>= 1;
3015     }
3016     if (incr !== 0) {
3017       huff &= incr - 1;
3018       huff += incr;
3019     } else {
3020       huff = 0;
3021     }
3022 
3023     /* go to next symbol, update count, len */
3024     sym++;
3025     if (--(count[len]) === 0) {
3026       if (len === max) { break; }
3027       len = lens[lens_index + work[sym]];
3028     }
3029 
3030     /* create new sub-table if needed */
3031     if (len > root && (huff & mask) !== low) {
3032       /* if first time, transition to sub-tables */
3033       if (drop === 0) {
3034         drop = root;
3035       }
3036 
3037       /* increment past last table */
3038       next += min;            /* here min is 1 << curr */
3039 
3040       /* determine length of next table */
3041       curr = len - drop;
3042       left = 1 << curr;
3043       while (curr + drop < max) {
3044         left -= count[curr + drop];
3045         if (left <= 0) { break; }
3046         curr++;
3047         left <<= 1;
3048       }
3049 
3050       /* check for enough space */
3051       used += 1 << curr;
3052       if ((type === LENS && used > ENOUGH_LENS) ||
3053         (type === DISTS && used > ENOUGH_DISTS)) {
3054         return 1;
3055       }
3056 
3057       /* point entry in root table to sub-table */
3058       low = huff & mask;
3059       /*table.op[low] = curr;
3060       table.bits[low] = root;
3061       table.val[low] = next - opts.table_index;*/
3062       table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
3063     }
3064   }
3065 
3066   /* fill in remaining table entry if code is incomplete (guaranteed to have
3067    at most one remaining entry, since if the code is incomplete, the
3068    maximum code length that was allowed to get this far is one bit) */
3069   if (huff !== 0) {
3070     //table.op[next + huff] = 64;            /* invalid code marker */
3071     //table.bits[next + huff] = len - drop;
3072     //table.val[next + huff] = 0;
3073     table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
3074   }
3075 
3076   /* set return parameters */
3077   //opts.table_index += used;
3078   opts.bits = root;
3079   return 0;
3080 };
3081 },{"../utils/common":2}],11:[function(_dereq_,module,exports){
3082 'use strict';
3083 
3084 module.exports = {
3085   '2':    'need dictionary',     /* Z_NEED_DICT       2  */
3086   '1':    'stream end',          /* Z_STREAM_END      1  */
3087   '0':    '',                    /* Z_OK              0  */
3088   '-1':   'file error',          /* Z_ERRNO         (-1) */
3089   '-2':   'stream error',        /* Z_STREAM_ERROR  (-2) */
3090   '-3':   'data error',          /* Z_DATA_ERROR    (-3) */
3091   '-4':   'insufficient memory', /* Z_MEM_ERROR     (-4) */
3092   '-5':   'buffer error',        /* Z_BUF_ERROR     (-5) */
3093   '-6':   'incompatible version' /* Z_VERSION_ERROR (-6) */
3094 };
3095 },{}],12:[function(_dereq_,module,exports){
3096 'use strict';
3097 
3098 
3099 function ZStream() {
3100   /* next input byte */
3101   this.input = null; // JS specific, because we have no pointers
3102   this.next_in = 0;
3103   /* number of bytes available at input */
3104   this.avail_in = 0;
3105   /* total number of input bytes read so far */
3106   this.total_in = 0;
3107   /* next output byte should be put there */
3108   this.output = null; // JS specific, because we have no pointers
3109   this.next_out = 0;
3110   /* remaining free space at output */
3111   this.avail_out = 0;
3112   /* total number of bytes output so far */
3113   this.total_out = 0;
3114   /* last error message, NULL if no error */
3115   this.msg = ''/*Z_NULL*/;
3116   /* not visible by applications */
3117   this.state = null;
3118   /* best guess about the data type: binary or text */
3119   this.data_type = 2/*Z_UNKNOWN*/;
3120   /* adler32 value of the uncompressed data */
3121   this.adler = 0;
3122 }
3123 
3124 module.exports = ZStream;
3125 },{}]},{},[1])
3126 (1)
3127 }));
3128 //// pako_inflate.js: end
3129 
3130     /**
3131      * @param {!Uint8Array} data
3132      * @param {!number} size
3133      * @return {!Uint8Array}
3134      */
3135     function zip_inflate(data, size) {
3136         return pako.inflateRaw(data);
3137     }
3138 
3139     return {
3140         inflate: zip_inflate
3141     };
3142 }
3143 
3144     /**
3145      * Wrapper for RawInflate functions
3146      * @const
3147      * @type {!{inflate:!function(!Uint8Array,!number):!Uint8Array}}
3148      */
3149     core.RawInflate = createRawInflateSingleton();
3150 }());
3151