source: trip-planner-front/node_modules/pako/dist/pako_deflate.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 125.1 KB
Line 
1/* pako 1.0.11 nodeca/pako */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pako = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2'use strict';
3
4
5var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
6 (typeof Uint16Array !== 'undefined') &&
7 (typeof Int32Array !== 'undefined');
8
9function _has(obj, key) {
10 return Object.prototype.hasOwnProperty.call(obj, key);
11}
12
13exports.assign = function (obj /*from1, from2, from3, ...*/) {
14 var sources = Array.prototype.slice.call(arguments, 1);
15 while (sources.length) {
16 var source = sources.shift();
17 if (!source) { continue; }
18
19 if (typeof source !== 'object') {
20 throw new TypeError(source + 'must be non-object');
21 }
22
23 for (var p in source) {
24 if (_has(source, p)) {
25 obj[p] = source[p];
26 }
27 }
28 }
29
30 return obj;
31};
32
33
34// reduce buffer size, avoiding mem copy
35exports.shrinkBuf = function (buf, size) {
36 if (buf.length === size) { return buf; }
37 if (buf.subarray) { return buf.subarray(0, size); }
38 buf.length = size;
39 return buf;
40};
41
42
43var fnTyped = {
44 arraySet: function (dest, src, src_offs, len, dest_offs) {
45 if (src.subarray && dest.subarray) {
46 dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
47 return;
48 }
49 // Fallback to ordinary array
50 for (var i = 0; i < len; i++) {
51 dest[dest_offs + i] = src[src_offs + i];
52 }
53 },
54 // Join array of chunks to single array.
55 flattenChunks: function (chunks) {
56 var i, l, len, pos, chunk, result;
57
58 // calculate data length
59 len = 0;
60 for (i = 0, l = chunks.length; i < l; i++) {
61 len += chunks[i].length;
62 }
63
64 // join chunks
65 result = new Uint8Array(len);
66 pos = 0;
67 for (i = 0, l = chunks.length; i < l; i++) {
68 chunk = chunks[i];
69 result.set(chunk, pos);
70 pos += chunk.length;
71 }
72
73 return result;
74 }
75};
76
77var fnUntyped = {
78 arraySet: function (dest, src, src_offs, len, dest_offs) {
79 for (var i = 0; i < len; i++) {
80 dest[dest_offs + i] = src[src_offs + i];
81 }
82 },
83 // Join array of chunks to single array.
84 flattenChunks: function (chunks) {
85 return [].concat.apply([], chunks);
86 }
87};
88
89
90// Enable/Disable typed arrays use, for testing
91//
92exports.setTyped = function (on) {
93 if (on) {
94 exports.Buf8 = Uint8Array;
95 exports.Buf16 = Uint16Array;
96 exports.Buf32 = Int32Array;
97 exports.assign(exports, fnTyped);
98 } else {
99 exports.Buf8 = Array;
100 exports.Buf16 = Array;
101 exports.Buf32 = Array;
102 exports.assign(exports, fnUntyped);
103 }
104};
105
106exports.setTyped(TYPED_OK);
107
108},{}],2:[function(require,module,exports){
109// String encode/decode helpers
110'use strict';
111
112
113var utils = require('./common');
114
115
116// Quick check if we can use fast array to bin string conversion
117//
118// - apply(Array) can fail on Android 2.2
119// - apply(Uint8Array) can fail on iOS 5.1 Safari
120//
121var STR_APPLY_OK = true;
122var STR_APPLY_UIA_OK = true;
123
124try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }
125try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
126
127
128// Table with utf8 lengths (calculated by first byte of sequence)
129// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
130// because max possible codepoint is 0x10ffff
131var _utf8len = new utils.Buf8(256);
132for (var q = 0; q < 256; q++) {
133 _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
134}
135_utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
136
137
138// convert string to array (typed, when possible)
139exports.string2buf = function (str) {
140 var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
141
142 // count binary size
143 for (m_pos = 0; m_pos < str_len; m_pos++) {
144 c = str.charCodeAt(m_pos);
145 if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
146 c2 = str.charCodeAt(m_pos + 1);
147 if ((c2 & 0xfc00) === 0xdc00) {
148 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
149 m_pos++;
150 }
151 }
152 buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
153 }
154
155 // allocate buffer
156 buf = new utils.Buf8(buf_len);
157
158 // convert
159 for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
160 c = str.charCodeAt(m_pos);
161 if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
162 c2 = str.charCodeAt(m_pos + 1);
163 if ((c2 & 0xfc00) === 0xdc00) {
164 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
165 m_pos++;
166 }
167 }
168 if (c < 0x80) {
169 /* one byte */
170 buf[i++] = c;
171 } else if (c < 0x800) {
172 /* two bytes */
173 buf[i++] = 0xC0 | (c >>> 6);
174 buf[i++] = 0x80 | (c & 0x3f);
175 } else if (c < 0x10000) {
176 /* three bytes */
177 buf[i++] = 0xE0 | (c >>> 12);
178 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
179 buf[i++] = 0x80 | (c & 0x3f);
180 } else {
181 /* four bytes */
182 buf[i++] = 0xf0 | (c >>> 18);
183 buf[i++] = 0x80 | (c >>> 12 & 0x3f);
184 buf[i++] = 0x80 | (c >>> 6 & 0x3f);
185 buf[i++] = 0x80 | (c & 0x3f);
186 }
187 }
188
189 return buf;
190};
191
192// Helper (used in 2 places)
193function buf2binstring(buf, len) {
194 // On Chrome, the arguments in a function call that are allowed is `65534`.
195 // If the length of the buffer is smaller than that, we can use this optimization,
196 // otherwise we will take a slower path.
197 if (len < 65534) {
198 if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
199 return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
200 }
201 }
202
203 var result = '';
204 for (var i = 0; i < len; i++) {
205 result += String.fromCharCode(buf[i]);
206 }
207 return result;
208}
209
210
211// Convert byte array to binary string
212exports.buf2binstring = function (buf) {
213 return buf2binstring(buf, buf.length);
214};
215
216
217// Convert binary string (typed, when possible)
218exports.binstring2buf = function (str) {
219 var buf = new utils.Buf8(str.length);
220 for (var i = 0, len = buf.length; i < len; i++) {
221 buf[i] = str.charCodeAt(i);
222 }
223 return buf;
224};
225
226
227// convert array to string
228exports.buf2string = function (buf, max) {
229 var i, out, c, c_len;
230 var len = max || buf.length;
231
232 // Reserve max possible length (2 words per char)
233 // NB: by unknown reasons, Array is significantly faster for
234 // String.fromCharCode.apply than Uint16Array.
235 var utf16buf = new Array(len * 2);
236
237 for (out = 0, i = 0; i < len;) {
238 c = buf[i++];
239 // quick process ascii
240 if (c < 0x80) { utf16buf[out++] = c; continue; }
241
242 c_len = _utf8len[c];
243 // skip 5 & 6 byte codes
244 if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
245
246 // apply mask on first byte
247 c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
248 // join the rest
249 while (c_len > 1 && i < len) {
250 c = (c << 6) | (buf[i++] & 0x3f);
251 c_len--;
252 }
253
254 // terminated by end of string?
255 if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
256
257 if (c < 0x10000) {
258 utf16buf[out++] = c;
259 } else {
260 c -= 0x10000;
261 utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
262 utf16buf[out++] = 0xdc00 | (c & 0x3ff);
263 }
264 }
265
266 return buf2binstring(utf16buf, out);
267};
268
269
270// Calculate max possible position in utf8 buffer,
271// that will not break sequence. If that's not possible
272// - (very small limits) return max size as is.
273//
274// buf[] - utf8 bytes array
275// max - length limit (mandatory);
276exports.utf8border = function (buf, max) {
277 var pos;
278
279 max = max || buf.length;
280 if (max > buf.length) { max = buf.length; }
281
282 // go back from last position, until start of sequence found
283 pos = max - 1;
284 while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
285
286 // Very small and broken sequence,
287 // return max, because we should return something anyway.
288 if (pos < 0) { return max; }
289
290 // If we came to start of buffer - that means buffer is too small,
291 // return max too.
292 if (pos === 0) { return max; }
293
294 return (pos + _utf8len[buf[pos]] > max) ? pos : max;
295};
296
297},{"./common":1}],3:[function(require,module,exports){
298'use strict';
299
300// Note: adler32 takes 12% for level 0 and 2% for level 6.
301// It isn't worth it to make additional optimizations as in original.
302// Small size is preferable.
303
304// (C) 1995-2013 Jean-loup Gailly and Mark Adler
305// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
306//
307// This software is provided 'as-is', without any express or implied
308// warranty. In no event will the authors be held liable for any damages
309// arising from the use of this software.
310//
311// Permission is granted to anyone to use this software for any purpose,
312// including commercial applications, and to alter it and redistribute it
313// freely, subject to the following restrictions:
314//
315// 1. The origin of this software must not be misrepresented; you must not
316// claim that you wrote the original software. If you use this software
317// in a product, an acknowledgment in the product documentation would be
318// appreciated but is not required.
319// 2. Altered source versions must be plainly marked as such, and must not be
320// misrepresented as being the original software.
321// 3. This notice may not be removed or altered from any source distribution.
322
323function adler32(adler, buf, len, pos) {
324 var s1 = (adler & 0xffff) |0,
325 s2 = ((adler >>> 16) & 0xffff) |0,
326 n = 0;
327
328 while (len !== 0) {
329 // Set limit ~ twice less than 5552, to keep
330 // s2 in 31-bits, because we force signed ints.
331 // in other case %= will fail.
332 n = len > 2000 ? 2000 : len;
333 len -= n;
334
335 do {
336 s1 = (s1 + buf[pos++]) |0;
337 s2 = (s2 + s1) |0;
338 } while (--n);
339
340 s1 %= 65521;
341 s2 %= 65521;
342 }
343
344 return (s1 | (s2 << 16)) |0;
345}
346
347
348module.exports = adler32;
349
350},{}],4:[function(require,module,exports){
351'use strict';
352
353// Note: we can't get significant speed boost here.
354// So write code to minimize size - no pregenerated tables
355// and array tools dependencies.
356
357// (C) 1995-2013 Jean-loup Gailly and Mark Adler
358// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
359//
360// This software is provided 'as-is', without any express or implied
361// warranty. In no event will the authors be held liable for any damages
362// arising from the use of this software.
363//
364// Permission is granted to anyone to use this software for any purpose,
365// including commercial applications, and to alter it and redistribute it
366// freely, subject to the following restrictions:
367//
368// 1. The origin of this software must not be misrepresented; you must not
369// claim that you wrote the original software. If you use this software
370// in a product, an acknowledgment in the product documentation would be
371// appreciated but is not required.
372// 2. Altered source versions must be plainly marked as such, and must not be
373// misrepresented as being the original software.
374// 3. This notice may not be removed or altered from any source distribution.
375
376// Use ordinary array, since untyped makes no boost here
377function makeTable() {
378 var c, table = [];
379
380 for (var n = 0; n < 256; n++) {
381 c = n;
382 for (var k = 0; k < 8; k++) {
383 c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
384 }
385 table[n] = c;
386 }
387
388 return table;
389}
390
391// Create table on load. Just 255 signed longs. Not a problem.
392var crcTable = makeTable();
393
394
395function crc32(crc, buf, len, pos) {
396 var t = crcTable,
397 end = pos + len;
398
399 crc ^= -1;
400
401 for (var i = pos; i < end; i++) {
402 crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
403 }
404
405 return (crc ^ (-1)); // >>> 0;
406}
407
408
409module.exports = crc32;
410
411},{}],5:[function(require,module,exports){
412'use strict';
413
414// (C) 1995-2013 Jean-loup Gailly and Mark Adler
415// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
416//
417// This software is provided 'as-is', without any express or implied
418// warranty. In no event will the authors be held liable for any damages
419// arising from the use of this software.
420//
421// Permission is granted to anyone to use this software for any purpose,
422// including commercial applications, and to alter it and redistribute it
423// freely, subject to the following restrictions:
424//
425// 1. The origin of this software must not be misrepresented; you must not
426// claim that you wrote the original software. If you use this software
427// in a product, an acknowledgment in the product documentation would be
428// appreciated but is not required.
429// 2. Altered source versions must be plainly marked as such, and must not be
430// misrepresented as being the original software.
431// 3. This notice may not be removed or altered from any source distribution.
432
433var utils = require('../utils/common');
434var trees = require('./trees');
435var adler32 = require('./adler32');
436var crc32 = require('./crc32');
437var msg = require('./messages');
438
439/* Public constants ==========================================================*/
440/* ===========================================================================*/
441
442
443/* Allowed flush values; see deflate() and inflate() below for details */
444var Z_NO_FLUSH = 0;
445var Z_PARTIAL_FLUSH = 1;
446//var Z_SYNC_FLUSH = 2;
447var Z_FULL_FLUSH = 3;
448var Z_FINISH = 4;
449var Z_BLOCK = 5;
450//var Z_TREES = 6;
451
452
453/* Return codes for the compression/decompression functions. Negative values
454 * are errors, positive values are used for special but normal events.
455 */
456var Z_OK = 0;
457var Z_STREAM_END = 1;
458//var Z_NEED_DICT = 2;
459//var Z_ERRNO = -1;
460var Z_STREAM_ERROR = -2;
461var Z_DATA_ERROR = -3;
462//var Z_MEM_ERROR = -4;
463var Z_BUF_ERROR = -5;
464//var Z_VERSION_ERROR = -6;
465
466
467/* compression levels */
468//var Z_NO_COMPRESSION = 0;
469//var Z_BEST_SPEED = 1;
470//var Z_BEST_COMPRESSION = 9;
471var Z_DEFAULT_COMPRESSION = -1;
472
473
474var Z_FILTERED = 1;
475var Z_HUFFMAN_ONLY = 2;
476var Z_RLE = 3;
477var Z_FIXED = 4;
478var Z_DEFAULT_STRATEGY = 0;
479
480/* Possible values of the data_type field (though see inflate()) */
481//var Z_BINARY = 0;
482//var Z_TEXT = 1;
483//var Z_ASCII = 1; // = Z_TEXT
484var Z_UNKNOWN = 2;
485
486
487/* The deflate compression method */
488var Z_DEFLATED = 8;
489
490/*============================================================================*/
491
492
493var MAX_MEM_LEVEL = 9;
494/* Maximum value for memLevel in deflateInit2 */
495var MAX_WBITS = 15;
496/* 32K LZ77 window */
497var DEF_MEM_LEVEL = 8;
498
499
500var LENGTH_CODES = 29;
501/* number of length codes, not counting the special END_BLOCK code */
502var LITERALS = 256;
503/* number of literal bytes 0..255 */
504var L_CODES = LITERALS + 1 + LENGTH_CODES;
505/* number of Literal or Length codes, including the END_BLOCK code */
506var D_CODES = 30;
507/* number of distance codes */
508var BL_CODES = 19;
509/* number of codes used to transfer the bit lengths */
510var HEAP_SIZE = 2 * L_CODES + 1;
511/* maximum heap size */
512var MAX_BITS = 15;
513/* All codes must not exceed MAX_BITS bits */
514
515var MIN_MATCH = 3;
516var MAX_MATCH = 258;
517var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
518
519var PRESET_DICT = 0x20;
520
521var INIT_STATE = 42;
522var EXTRA_STATE = 69;
523var NAME_STATE = 73;
524var COMMENT_STATE = 91;
525var HCRC_STATE = 103;
526var BUSY_STATE = 113;
527var FINISH_STATE = 666;
528
529var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
530var BS_BLOCK_DONE = 2; /* block flush performed */
531var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
532var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
533
534var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
535
536function err(strm, errorCode) {
537 strm.msg = msg[errorCode];
538 return errorCode;
539}
540
541function rank(f) {
542 return ((f) << 1) - ((f) > 4 ? 9 : 0);
543}
544
545function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
546
547
548/* =========================================================================
549 * Flush as much pending output as possible. All deflate() output goes
550 * through this function so some applications may wish to modify it
551 * to avoid allocating a large strm->output buffer and copying into it.
552 * (See also read_buf()).
553 */
554function flush_pending(strm) {
555 var s = strm.state;
556
557 //_tr_flush_bits(s);
558 var len = s.pending;
559 if (len > strm.avail_out) {
560 len = strm.avail_out;
561 }
562 if (len === 0) { return; }
563
564 utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
565 strm.next_out += len;
566 s.pending_out += len;
567 strm.total_out += len;
568 strm.avail_out -= len;
569 s.pending -= len;
570 if (s.pending === 0) {
571 s.pending_out = 0;
572 }
573}
574
575
576function flush_block_only(s, last) {
577 trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
578 s.block_start = s.strstart;
579 flush_pending(s.strm);
580}
581
582
583function put_byte(s, b) {
584 s.pending_buf[s.pending++] = b;
585}
586
587
588/* =========================================================================
589 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
590 * IN assertion: the stream state is correct and there is enough room in
591 * pending_buf.
592 */
593function putShortMSB(s, b) {
594// put_byte(s, (Byte)(b >> 8));
595// put_byte(s, (Byte)(b & 0xff));
596 s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
597 s.pending_buf[s.pending++] = b & 0xff;
598}
599
600
601/* ===========================================================================
602 * Read a new buffer from the current input stream, update the adler32
603 * and total number of bytes read. All deflate() input goes through
604 * this function so some applications may wish to modify it to avoid
605 * allocating a large strm->input buffer and copying from it.
606 * (See also flush_pending()).
607 */
608function read_buf(strm, buf, start, size) {
609 var len = strm.avail_in;
610
611 if (len > size) { len = size; }
612 if (len === 0) { return 0; }
613
614 strm.avail_in -= len;
615
616 // zmemcpy(buf, strm->next_in, len);
617 utils.arraySet(buf, strm.input, strm.next_in, len, start);
618 if (strm.state.wrap === 1) {
619 strm.adler = adler32(strm.adler, buf, len, start);
620 }
621
622 else if (strm.state.wrap === 2) {
623 strm.adler = crc32(strm.adler, buf, len, start);
624 }
625
626 strm.next_in += len;
627 strm.total_in += len;
628
629 return len;
630}
631
632
633/* ===========================================================================
634 * Set match_start to the longest match starting at the given string and
635 * return its length. Matches shorter or equal to prev_length are discarded,
636 * in which case the result is equal to prev_length and match_start is
637 * garbage.
638 * IN assertions: cur_match is the head of the hash chain for the current
639 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
640 * OUT assertion: the match length is not greater than s->lookahead.
641 */
642function longest_match(s, cur_match) {
643 var chain_length = s.max_chain_length; /* max hash chain length */
644 var scan = s.strstart; /* current string */
645 var match; /* matched string */
646 var len; /* length of current match */
647 var best_len = s.prev_length; /* best match length so far */
648 var nice_match = s.nice_match; /* stop if match long enough */
649 var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
650 s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
651
652 var _win = s.window; // shortcut
653
654 var wmask = s.w_mask;
655 var prev = s.prev;
656
657 /* Stop when cur_match becomes <= limit. To simplify the code,
658 * we prevent matches with the string of window index 0.
659 */
660
661 var strend = s.strstart + MAX_MATCH;
662 var scan_end1 = _win[scan + best_len - 1];
663 var scan_end = _win[scan + best_len];
664
665 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
666 * It is easy to get rid of this optimization if necessary.
667 */
668 // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
669
670 /* Do not waste too much time if we already have a good match: */
671 if (s.prev_length >= s.good_match) {
672 chain_length >>= 2;
673 }
674 /* Do not look for matches beyond the end of the input. This is necessary
675 * to make deflate deterministic.
676 */
677 if (nice_match > s.lookahead) { nice_match = s.lookahead; }
678
679 // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
680
681 do {
682 // Assert(cur_match < s->strstart, "no future");
683 match = cur_match;
684
685 /* Skip to next match if the match length cannot increase
686 * or if the match length is less than 2. Note that the checks below
687 * for insufficient lookahead only occur occasionally for performance
688 * reasons. Therefore uninitialized memory will be accessed, and
689 * conditional jumps will be made that depend on those values.
690 * However the length of the match is limited to the lookahead, so
691 * the output of deflate is not affected by the uninitialized values.
692 */
693
694 if (_win[match + best_len] !== scan_end ||
695 _win[match + best_len - 1] !== scan_end1 ||
696 _win[match] !== _win[scan] ||
697 _win[++match] !== _win[scan + 1]) {
698 continue;
699 }
700
701 /* The check at best_len-1 can be removed because it will be made
702 * again later. (This heuristic is not always a win.)
703 * It is not necessary to compare scan[2] and match[2] since they
704 * are always equal when the other bytes match, given that
705 * the hash keys are equal and that HASH_BITS >= 8.
706 */
707 scan += 2;
708 match++;
709 // Assert(*scan == *match, "match[2]?");
710
711 /* We check for insufficient lookahead only every 8th comparison;
712 * the 256th check will be made at strstart+258.
713 */
714 do {
715 /*jshint noempty:false*/
716 } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
717 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
718 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
719 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
720 scan < strend);
721
722 // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
723
724 len = MAX_MATCH - (strend - scan);
725 scan = strend - MAX_MATCH;
726
727 if (len > best_len) {
728 s.match_start = cur_match;
729 best_len = len;
730 if (len >= nice_match) {
731 break;
732 }
733 scan_end1 = _win[scan + best_len - 1];
734 scan_end = _win[scan + best_len];
735 }
736 } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
737
738 if (best_len <= s.lookahead) {
739 return best_len;
740 }
741 return s.lookahead;
742}
743
744
745/* ===========================================================================
746 * Fill the window when the lookahead becomes insufficient.
747 * Updates strstart and lookahead.
748 *
749 * IN assertion: lookahead < MIN_LOOKAHEAD
750 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
751 * At least one byte has been read, or avail_in == 0; reads are
752 * performed for at least two bytes (required for the zip translate_eol
753 * option -- not supported here).
754 */
755function fill_window(s) {
756 var _w_size = s.w_size;
757 var p, n, m, more, str;
758
759 //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
760
761 do {
762 more = s.window_size - s.lookahead - s.strstart;
763
764 // JS ints have 32 bit, block below not needed
765 /* Deal with !@#$% 64K limit: */
766 //if (sizeof(int) <= 2) {
767 // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
768 // more = wsize;
769 //
770 // } else if (more == (unsigned)(-1)) {
771 // /* Very unlikely, but possible on 16 bit machine if
772 // * strstart == 0 && lookahead == 1 (input done a byte at time)
773 // */
774 // more--;
775 // }
776 //}
777
778
779 /* If the window is almost full and there is insufficient lookahead,
780 * move the upper half to the lower one to make room in the upper half.
781 */
782 if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
783
784 utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
785 s.match_start -= _w_size;
786 s.strstart -= _w_size;
787 /* we now have strstart >= MAX_DIST */
788 s.block_start -= _w_size;
789
790 /* Slide the hash table (could be avoided with 32 bit values
791 at the expense of memory usage). We slide even when level == 0
792 to keep the hash table consistent if we switch back to level > 0
793 later. (Using level 0 permanently is not an optimal usage of
794 zlib, so we don't care about this pathological case.)
795 */
796
797 n = s.hash_size;
798 p = n;
799 do {
800 m = s.head[--p];
801 s.head[p] = (m >= _w_size ? m - _w_size : 0);
802 } while (--n);
803
804 n = _w_size;
805 p = n;
806 do {
807 m = s.prev[--p];
808 s.prev[p] = (m >= _w_size ? m - _w_size : 0);
809 /* If n is not on any hash chain, prev[n] is garbage but
810 * its value will never be used.
811 */
812 } while (--n);
813
814 more += _w_size;
815 }
816 if (s.strm.avail_in === 0) {
817 break;
818 }
819
820 /* If there was no sliding:
821 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
822 * more == window_size - lookahead - strstart
823 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
824 * => more >= window_size - 2*WSIZE + 2
825 * In the BIG_MEM or MMAP case (not yet supported),
826 * window_size == input_size + MIN_LOOKAHEAD &&
827 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
828 * Otherwise, window_size == 2*WSIZE so more >= 2.
829 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
830 */
831 //Assert(more >= 2, "more < 2");
832 n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
833 s.lookahead += n;
834
835 /* Initialize the hash value now that we have some input: */
836 if (s.lookahead + s.insert >= MIN_MATCH) {
837 str = s.strstart - s.insert;
838 s.ins_h = s.window[str];
839
840 /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
841 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
842//#if MIN_MATCH != 3
843// Call update_hash() MIN_MATCH-3 more times
844//#endif
845 while (s.insert) {
846 /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
847 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
848
849 s.prev[str & s.w_mask] = s.head[s.ins_h];
850 s.head[s.ins_h] = str;
851 str++;
852 s.insert--;
853 if (s.lookahead + s.insert < MIN_MATCH) {
854 break;
855 }
856 }
857 }
858 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
859 * but this is not important since only literal bytes will be emitted.
860 */
861
862 } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
863
864 /* If the WIN_INIT bytes after the end of the current data have never been
865 * written, then zero those bytes in order to avoid memory check reports of
866 * the use of uninitialized (or uninitialised as Julian writes) bytes by
867 * the longest match routines. Update the high water mark for the next
868 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
869 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
870 */
871// if (s.high_water < s.window_size) {
872// var curr = s.strstart + s.lookahead;
873// var init = 0;
874//
875// if (s.high_water < curr) {
876// /* Previous high water mark below current data -- zero WIN_INIT
877// * bytes or up to end of window, whichever is less.
878// */
879// init = s.window_size - curr;
880// if (init > WIN_INIT)
881// init = WIN_INIT;
882// zmemzero(s->window + curr, (unsigned)init);
883// s->high_water = curr + init;
884// }
885// else if (s->high_water < (ulg)curr + WIN_INIT) {
886// /* High water mark at or above current data, but below current data
887// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
888// * to end of window, whichever is less.
889// */
890// init = (ulg)curr + WIN_INIT - s->high_water;
891// if (init > s->window_size - s->high_water)
892// init = s->window_size - s->high_water;
893// zmemzero(s->window + s->high_water, (unsigned)init);
894// s->high_water += init;
895// }
896// }
897//
898// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
899// "not enough room for search");
900}
901
902/* ===========================================================================
903 * Copy without compression as much as possible from the input stream, return
904 * the current block state.
905 * This function does not insert new strings in the dictionary since
906 * uncompressible data is probably not useful. This function is used
907 * only for the level=0 compression option.
908 * NOTE: this function should be optimized to avoid extra copying from
909 * window to pending_buf.
910 */
911function deflate_stored(s, flush) {
912 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
913 * to pending_buf_size, and each stored block has a 5 byte header:
914 */
915 var max_block_size = 0xffff;
916
917 if (max_block_size > s.pending_buf_size - 5) {
918 max_block_size = s.pending_buf_size - 5;
919 }
920
921 /* Copy as much as possible from input to output: */
922 for (;;) {
923 /* Fill the window as much as possible: */
924 if (s.lookahead <= 1) {
925
926 //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
927 // s->block_start >= (long)s->w_size, "slide too late");
928// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
929// s.block_start >= s.w_size)) {
930// throw new Error("slide too late");
931// }
932
933 fill_window(s);
934 if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
935 return BS_NEED_MORE;
936 }
937
938 if (s.lookahead === 0) {
939 break;
940 }
941 /* flush the current block */
942 }
943 //Assert(s->block_start >= 0L, "block gone");
944// if (s.block_start < 0) throw new Error("block gone");
945
946 s.strstart += s.lookahead;
947 s.lookahead = 0;
948
949 /* Emit a stored block if pending_buf will be full: */
950 var max_start = s.block_start + max_block_size;
951
952 if (s.strstart === 0 || s.strstart >= max_start) {
953 /* strstart == 0 is possible when wraparound on 16-bit machine */
954 s.lookahead = s.strstart - max_start;
955 s.strstart = max_start;
956 /*** FLUSH_BLOCK(s, 0); ***/
957 flush_block_only(s, false);
958 if (s.strm.avail_out === 0) {
959 return BS_NEED_MORE;
960 }
961 /***/
962
963
964 }
965 /* Flush if we may have to slide, otherwise block_start may become
966 * negative and the data will be gone:
967 */
968 if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
969 /*** FLUSH_BLOCK(s, 0); ***/
970 flush_block_only(s, false);
971 if (s.strm.avail_out === 0) {
972 return BS_NEED_MORE;
973 }
974 /***/
975 }
976 }
977
978 s.insert = 0;
979
980 if (flush === Z_FINISH) {
981 /*** FLUSH_BLOCK(s, 1); ***/
982 flush_block_only(s, true);
983 if (s.strm.avail_out === 0) {
984 return BS_FINISH_STARTED;
985 }
986 /***/
987 return BS_FINISH_DONE;
988 }
989
990 if (s.strstart > s.block_start) {
991 /*** FLUSH_BLOCK(s, 0); ***/
992 flush_block_only(s, false);
993 if (s.strm.avail_out === 0) {
994 return BS_NEED_MORE;
995 }
996 /***/
997 }
998
999 return BS_NEED_MORE;
1000}
1001
1002/* ===========================================================================
1003 * Compress as much as possible from the input stream, return the current
1004 * block state.
1005 * This function does not perform lazy evaluation of matches and inserts
1006 * new strings in the dictionary only for unmatched strings or for short
1007 * matches. It is used only for the fast compression options.
1008 */
1009function deflate_fast(s, flush) {
1010 var hash_head; /* head of the hash chain */
1011 var bflush; /* set if current block must be flushed */
1012
1013 for (;;) {
1014 /* Make sure that we always have enough lookahead, except
1015 * at the end of the input file. We need MAX_MATCH bytes
1016 * for the next match, plus MIN_MATCH bytes to insert the
1017 * string following the next match.
1018 */
1019 if (s.lookahead < MIN_LOOKAHEAD) {
1020 fill_window(s);
1021 if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
1022 return BS_NEED_MORE;
1023 }
1024 if (s.lookahead === 0) {
1025 break; /* flush the current block */
1026 }
1027 }
1028
1029 /* Insert the string window[strstart .. strstart+2] in the
1030 * dictionary, and set hash_head to the head of the hash chain:
1031 */
1032 hash_head = 0/*NIL*/;
1033 if (s.lookahead >= MIN_MATCH) {
1034 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1035 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
1036 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1037 s.head[s.ins_h] = s.strstart;
1038 /***/
1039 }
1040
1041 /* Find the longest match, discarding those <= prev_length.
1042 * At this point we have always match_length < MIN_MATCH
1043 */
1044 if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
1045 /* To simplify the code, we prevent matches with the string
1046 * of window index 0 (in particular we have to avoid a match
1047 * of the string with itself at the start of the input file).
1048 */
1049 s.match_length = longest_match(s, hash_head);
1050 /* longest_match() sets match_start */
1051 }
1052 if (s.match_length >= MIN_MATCH) {
1053 // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
1054
1055 /*** _tr_tally_dist(s, s.strstart - s.match_start,
1056 s.match_length - MIN_MATCH, bflush); ***/
1057 bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
1058
1059 s.lookahead -= s.match_length;
1060
1061 /* Insert new strings in the hash table only if the match length
1062 * is not too large. This saves time but degrades compression.
1063 */
1064 if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
1065 s.match_length--; /* string at strstart already in table */
1066 do {
1067 s.strstart++;
1068 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1069 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
1070 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1071 s.head[s.ins_h] = s.strstart;
1072 /***/
1073 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1074 * always MIN_MATCH bytes ahead.
1075 */
1076 } while (--s.match_length !== 0);
1077 s.strstart++;
1078 } else
1079 {
1080 s.strstart += s.match_length;
1081 s.match_length = 0;
1082 s.ins_h = s.window[s.strstart];
1083 /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
1084 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
1085
1086//#if MIN_MATCH != 3
1087// Call UPDATE_HASH() MIN_MATCH-3 more times
1088//#endif
1089 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1090 * matter since it will be recomputed at next deflate call.
1091 */
1092 }
1093 } else {
1094 /* No match, output a literal byte */
1095 //Tracevv((stderr,"%c", s.window[s.strstart]));
1096 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
1097 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
1098
1099 s.lookahead--;
1100 s.strstart++;
1101 }
1102 if (bflush) {
1103 /*** FLUSH_BLOCK(s, 0); ***/
1104 flush_block_only(s, false);
1105 if (s.strm.avail_out === 0) {
1106 return BS_NEED_MORE;
1107 }
1108 /***/
1109 }
1110 }
1111 s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
1112 if (flush === Z_FINISH) {
1113 /*** FLUSH_BLOCK(s, 1); ***/
1114 flush_block_only(s, true);
1115 if (s.strm.avail_out === 0) {
1116 return BS_FINISH_STARTED;
1117 }
1118 /***/
1119 return BS_FINISH_DONE;
1120 }
1121 if (s.last_lit) {
1122 /*** FLUSH_BLOCK(s, 0); ***/
1123 flush_block_only(s, false);
1124 if (s.strm.avail_out === 0) {
1125 return BS_NEED_MORE;
1126 }
1127 /***/
1128 }
1129 return BS_BLOCK_DONE;
1130}
1131
1132/* ===========================================================================
1133 * Same as above, but achieves better compression. We use a lazy
1134 * evaluation for matches: a match is finally adopted only if there is
1135 * no better match at the next window position.
1136 */
1137function deflate_slow(s, flush) {
1138 var hash_head; /* head of hash chain */
1139 var bflush; /* set if current block must be flushed */
1140
1141 var max_insert;
1142
1143 /* Process the input block. */
1144 for (;;) {
1145 /* Make sure that we always have enough lookahead, except
1146 * at the end of the input file. We need MAX_MATCH bytes
1147 * for the next match, plus MIN_MATCH bytes to insert the
1148 * string following the next match.
1149 */
1150 if (s.lookahead < MIN_LOOKAHEAD) {
1151 fill_window(s);
1152 if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
1153 return BS_NEED_MORE;
1154 }
1155 if (s.lookahead === 0) { break; } /* flush the current block */
1156 }
1157
1158 /* Insert the string window[strstart .. strstart+2] in the
1159 * dictionary, and set hash_head to the head of the hash chain:
1160 */
1161 hash_head = 0/*NIL*/;
1162 if (s.lookahead >= MIN_MATCH) {
1163 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1164 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
1165 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1166 s.head[s.ins_h] = s.strstart;
1167 /***/
1168 }
1169
1170 /* Find the longest match, discarding those <= prev_length.
1171 */
1172 s.prev_length = s.match_length;
1173 s.prev_match = s.match_start;
1174 s.match_length = MIN_MATCH - 1;
1175
1176 if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
1177 s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
1178 /* To simplify the code, we prevent matches with the string
1179 * of window index 0 (in particular we have to avoid a match
1180 * of the string with itself at the start of the input file).
1181 */
1182 s.match_length = longest_match(s, hash_head);
1183 /* longest_match() sets match_start */
1184
1185 if (s.match_length <= 5 &&
1186 (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
1187
1188 /* If prev_match is also MIN_MATCH, match_start is garbage
1189 * but we will ignore the current match anyway.
1190 */
1191 s.match_length = MIN_MATCH - 1;
1192 }
1193 }
1194 /* If there was a match at the previous step and the current
1195 * match is not better, output the previous match:
1196 */
1197 if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
1198 max_insert = s.strstart + s.lookahead - MIN_MATCH;
1199 /* Do not insert strings in hash table beyond this. */
1200
1201 //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
1202
1203 /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
1204 s.prev_length - MIN_MATCH, bflush);***/
1205 bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
1206 /* Insert in hash table all strings up to the end of the match.
1207 * strstart-1 and strstart are already inserted. If there is not
1208 * enough lookahead, the last two strings are not inserted in
1209 * the hash table.
1210 */
1211 s.lookahead -= s.prev_length - 1;
1212 s.prev_length -= 2;
1213 do {
1214 if (++s.strstart <= max_insert) {
1215 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1216 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
1217 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1218 s.head[s.ins_h] = s.strstart;
1219 /***/
1220 }
1221 } while (--s.prev_length !== 0);
1222 s.match_available = 0;
1223 s.match_length = MIN_MATCH - 1;
1224 s.strstart++;
1225
1226 if (bflush) {
1227 /*** FLUSH_BLOCK(s, 0); ***/
1228 flush_block_only(s, false);
1229 if (s.strm.avail_out === 0) {
1230 return BS_NEED_MORE;
1231 }
1232 /***/
1233 }
1234
1235 } else if (s.match_available) {
1236 /* If there was no match at the previous position, output a
1237 * single literal. If there was a match but the current match
1238 * is longer, truncate the previous match to a single literal.
1239 */
1240 //Tracevv((stderr,"%c", s->window[s->strstart-1]));
1241 /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
1242 bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
1243
1244 if (bflush) {
1245 /*** FLUSH_BLOCK_ONLY(s, 0) ***/
1246 flush_block_only(s, false);
1247 /***/
1248 }
1249 s.strstart++;
1250 s.lookahead--;
1251 if (s.strm.avail_out === 0) {
1252 return BS_NEED_MORE;
1253 }
1254 } else {
1255 /* There is no previous match to compare with, wait for
1256 * the next step to decide.
1257 */
1258 s.match_available = 1;
1259 s.strstart++;
1260 s.lookahead--;
1261 }
1262 }
1263 //Assert (flush != Z_NO_FLUSH, "no flush?");
1264 if (s.match_available) {
1265 //Tracevv((stderr,"%c", s->window[s->strstart-1]));
1266 /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
1267 bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
1268
1269 s.match_available = 0;
1270 }
1271 s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
1272 if (flush === Z_FINISH) {
1273 /*** FLUSH_BLOCK(s, 1); ***/
1274 flush_block_only(s, true);
1275 if (s.strm.avail_out === 0) {
1276 return BS_FINISH_STARTED;
1277 }
1278 /***/
1279 return BS_FINISH_DONE;
1280 }
1281 if (s.last_lit) {
1282 /*** FLUSH_BLOCK(s, 0); ***/
1283 flush_block_only(s, false);
1284 if (s.strm.avail_out === 0) {
1285 return BS_NEED_MORE;
1286 }
1287 /***/
1288 }
1289
1290 return BS_BLOCK_DONE;
1291}
1292
1293
1294/* ===========================================================================
1295 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1296 * one. Do not maintain a hash table. (It will be regenerated if this run of
1297 * deflate switches away from Z_RLE.)
1298 */
1299function deflate_rle(s, flush) {
1300 var bflush; /* set if current block must be flushed */
1301 var prev; /* byte at distance one to match */
1302 var scan, strend; /* scan goes up to strend for length of run */
1303
1304 var _win = s.window;
1305
1306 for (;;) {
1307 /* Make sure that we always have enough lookahead, except
1308 * at the end of the input file. We need MAX_MATCH bytes
1309 * for the longest run, plus one for the unrolled loop.
1310 */
1311 if (s.lookahead <= MAX_MATCH) {
1312 fill_window(s);
1313 if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
1314 return BS_NEED_MORE;
1315 }
1316 if (s.lookahead === 0) { break; } /* flush the current block */
1317 }
1318
1319 /* See how many times the previous byte repeats */
1320 s.match_length = 0;
1321 if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
1322 scan = s.strstart - 1;
1323 prev = _win[scan];
1324 if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
1325 strend = s.strstart + MAX_MATCH;
1326 do {
1327 /*jshint noempty:false*/
1328 } while (prev === _win[++scan] && prev === _win[++scan] &&
1329 prev === _win[++scan] && prev === _win[++scan] &&
1330 prev === _win[++scan] && prev === _win[++scan] &&
1331 prev === _win[++scan] && prev === _win[++scan] &&
1332 scan < strend);
1333 s.match_length = MAX_MATCH - (strend - scan);
1334 if (s.match_length > s.lookahead) {
1335 s.match_length = s.lookahead;
1336 }
1337 }
1338 //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
1339 }
1340
1341 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1342 if (s.match_length >= MIN_MATCH) {
1343 //check_match(s, s.strstart, s.strstart - 1, s.match_length);
1344
1345 /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
1346 bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
1347
1348 s.lookahead -= s.match_length;
1349 s.strstart += s.match_length;
1350 s.match_length = 0;
1351 } else {
1352 /* No match, output a literal byte */
1353 //Tracevv((stderr,"%c", s->window[s->strstart]));
1354 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
1355 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
1356
1357 s.lookahead--;
1358 s.strstart++;
1359 }
1360 if (bflush) {
1361 /*** FLUSH_BLOCK(s, 0); ***/
1362 flush_block_only(s, false);
1363 if (s.strm.avail_out === 0) {
1364 return BS_NEED_MORE;
1365 }
1366 /***/
1367 }
1368 }
1369 s.insert = 0;
1370 if (flush === Z_FINISH) {
1371 /*** FLUSH_BLOCK(s, 1); ***/
1372 flush_block_only(s, true);
1373 if (s.strm.avail_out === 0) {
1374 return BS_FINISH_STARTED;
1375 }
1376 /***/
1377 return BS_FINISH_DONE;
1378 }
1379 if (s.last_lit) {
1380 /*** FLUSH_BLOCK(s, 0); ***/
1381 flush_block_only(s, false);
1382 if (s.strm.avail_out === 0) {
1383 return BS_NEED_MORE;
1384 }
1385 /***/
1386 }
1387 return BS_BLOCK_DONE;
1388}
1389
1390/* ===========================================================================
1391 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
1392 * (It will be regenerated if this run of deflate switches away from Huffman.)
1393 */
1394function deflate_huff(s, flush) {
1395 var bflush; /* set if current block must be flushed */
1396
1397 for (;;) {
1398 /* Make sure that we have a literal to write. */
1399 if (s.lookahead === 0) {
1400 fill_window(s);
1401 if (s.lookahead === 0) {
1402 if (flush === Z_NO_FLUSH) {
1403 return BS_NEED_MORE;
1404 }
1405 break; /* flush the current block */
1406 }
1407 }
1408
1409 /* Output a literal byte */
1410 s.match_length = 0;
1411 //Tracevv((stderr,"%c", s->window[s->strstart]));
1412 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
1413 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
1414 s.lookahead--;
1415 s.strstart++;
1416 if (bflush) {
1417 /*** FLUSH_BLOCK(s, 0); ***/
1418 flush_block_only(s, false);
1419 if (s.strm.avail_out === 0) {
1420 return BS_NEED_MORE;
1421 }
1422 /***/
1423 }
1424 }
1425 s.insert = 0;
1426 if (flush === Z_FINISH) {
1427 /*** FLUSH_BLOCK(s, 1); ***/
1428 flush_block_only(s, true);
1429 if (s.strm.avail_out === 0) {
1430 return BS_FINISH_STARTED;
1431 }
1432 /***/
1433 return BS_FINISH_DONE;
1434 }
1435 if (s.last_lit) {
1436 /*** FLUSH_BLOCK(s, 0); ***/
1437 flush_block_only(s, false);
1438 if (s.strm.avail_out === 0) {
1439 return BS_NEED_MORE;
1440 }
1441 /***/
1442 }
1443 return BS_BLOCK_DONE;
1444}
1445
1446/* Values for max_lazy_match, good_match and max_chain_length, depending on
1447 * the desired pack level (0..9). The values given below have been tuned to
1448 * exclude worst case performance for pathological files. Better values may be
1449 * found for specific files.
1450 */
1451function Config(good_length, max_lazy, nice_length, max_chain, func) {
1452 this.good_length = good_length;
1453 this.max_lazy = max_lazy;
1454 this.nice_length = nice_length;
1455 this.max_chain = max_chain;
1456 this.func = func;
1457}
1458
1459var configuration_table;
1460
1461configuration_table = [
1462 /* good lazy nice chain */
1463 new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
1464 new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
1465 new Config(4, 5, 16, 8, deflate_fast), /* 2 */
1466 new Config(4, 6, 32, 32, deflate_fast), /* 3 */
1467
1468 new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
1469 new Config(8, 16, 32, 32, deflate_slow), /* 5 */
1470 new Config(8, 16, 128, 128, deflate_slow), /* 6 */
1471 new Config(8, 32, 128, 256, deflate_slow), /* 7 */
1472 new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
1473 new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
1474];
1475
1476
1477/* ===========================================================================
1478 * Initialize the "longest match" routines for a new zlib stream
1479 */
1480function lm_init(s) {
1481 s.window_size = 2 * s.w_size;
1482
1483 /*** CLEAR_HASH(s); ***/
1484 zero(s.head); // Fill with NIL (= 0);
1485
1486 /* Set the default configuration parameters:
1487 */
1488 s.max_lazy_match = configuration_table[s.level].max_lazy;
1489 s.good_match = configuration_table[s.level].good_length;
1490 s.nice_match = configuration_table[s.level].nice_length;
1491 s.max_chain_length = configuration_table[s.level].max_chain;
1492
1493 s.strstart = 0;
1494 s.block_start = 0;
1495 s.lookahead = 0;
1496 s.insert = 0;
1497 s.match_length = s.prev_length = MIN_MATCH - 1;
1498 s.match_available = 0;
1499 s.ins_h = 0;
1500}
1501
1502
1503function DeflateState() {
1504 this.strm = null; /* pointer back to this zlib stream */
1505 this.status = 0; /* as the name implies */
1506 this.pending_buf = null; /* output still pending */
1507 this.pending_buf_size = 0; /* size of pending_buf */
1508 this.pending_out = 0; /* next pending byte to output to the stream */
1509 this.pending = 0; /* nb of bytes in the pending buffer */
1510 this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
1511 this.gzhead = null; /* gzip header information to write */
1512 this.gzindex = 0; /* where in extra, name, or comment */
1513 this.method = Z_DEFLATED; /* can only be DEFLATED */
1514 this.last_flush = -1; /* value of flush param for previous deflate call */
1515
1516 this.w_size = 0; /* LZ77 window size (32K by default) */
1517 this.w_bits = 0; /* log2(w_size) (8..16) */
1518 this.w_mask = 0; /* w_size - 1 */
1519
1520 this.window = null;
1521 /* Sliding window. Input bytes are read into the second half of the window,
1522 * and move to the first half later to keep a dictionary of at least wSize
1523 * bytes. With this organization, matches are limited to a distance of
1524 * wSize-MAX_MATCH bytes, but this ensures that IO is always
1525 * performed with a length multiple of the block size.
1526 */
1527
1528 this.window_size = 0;
1529 /* Actual size of window: 2*wSize, except when the user input buffer
1530 * is directly used as sliding window.
1531 */
1532
1533 this.prev = null;
1534 /* Link to older string with same hash index. To limit the size of this
1535 * array to 64K, this link is maintained only for the last 32K strings.
1536 * An index in this array is thus a window index modulo 32K.
1537 */
1538
1539 this.head = null; /* Heads of the hash chains or NIL. */
1540
1541 this.ins_h = 0; /* hash index of string to be inserted */
1542 this.hash_size = 0; /* number of elements in hash table */
1543 this.hash_bits = 0; /* log2(hash_size) */
1544 this.hash_mask = 0; /* hash_size-1 */
1545
1546 this.hash_shift = 0;
1547 /* Number of bits by which ins_h must be shifted at each input
1548 * step. It must be such that after MIN_MATCH steps, the oldest
1549 * byte no longer takes part in the hash key, that is:
1550 * hash_shift * MIN_MATCH >= hash_bits
1551 */
1552
1553 this.block_start = 0;
1554 /* Window position at the beginning of the current output block. Gets
1555 * negative when the window is moved backwards.
1556 */
1557
1558 this.match_length = 0; /* length of best match */
1559 this.prev_match = 0; /* previous match */
1560 this.match_available = 0; /* set if previous match exists */
1561 this.strstart = 0; /* start of string to insert */
1562 this.match_start = 0; /* start of matching string */
1563 this.lookahead = 0; /* number of valid bytes ahead in window */
1564
1565 this.prev_length = 0;
1566 /* Length of the best match at previous step. Matches not greater than this
1567 * are discarded. This is used in the lazy match evaluation.
1568 */
1569
1570 this.max_chain_length = 0;
1571 /* To speed up deflation, hash chains are never searched beyond this
1572 * length. A higher limit improves compression ratio but degrades the
1573 * speed.
1574 */
1575
1576 this.max_lazy_match = 0;
1577 /* Attempt to find a better match only when the current match is strictly
1578 * smaller than this value. This mechanism is used only for compression
1579 * levels >= 4.
1580 */
1581 // That's alias to max_lazy_match, don't use directly
1582 //this.max_insert_length = 0;
1583 /* Insert new strings in the hash table only if the match length is not
1584 * greater than this length. This saves time but degrades compression.
1585 * max_insert_length is used only for compression levels <= 3.
1586 */
1587
1588 this.level = 0; /* compression level (1..9) */
1589 this.strategy = 0; /* favor or force Huffman coding*/
1590
1591 this.good_match = 0;
1592 /* Use a faster search when the previous match is longer than this */
1593
1594 this.nice_match = 0; /* Stop searching when current match exceeds this */
1595
1596 /* used by trees.c: */
1597
1598 /* Didn't use ct_data typedef below to suppress compiler warning */
1599
1600 // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
1601 // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1602 // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
1603
1604 // Use flat array of DOUBLE size, with interleaved fata,
1605 // because JS does not support effective
1606 this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
1607 this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
1608 this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
1609 zero(this.dyn_ltree);
1610 zero(this.dyn_dtree);
1611 zero(this.bl_tree);
1612
1613 this.l_desc = null; /* desc. for literal tree */
1614 this.d_desc = null; /* desc. for distance tree */
1615 this.bl_desc = null; /* desc. for bit length tree */
1616
1617 //ush bl_count[MAX_BITS+1];
1618 this.bl_count = new utils.Buf16(MAX_BITS + 1);
1619 /* number of codes at each bit length for an optimal tree */
1620
1621 //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
1622 this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
1623 zero(this.heap);
1624
1625 this.heap_len = 0; /* number of elements in the heap */
1626 this.heap_max = 0; /* element of largest frequency */
1627 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1628 * The same heap array is used to build all trees.
1629 */
1630
1631 this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
1632 zero(this.depth);
1633 /* Depth of each subtree used as tie breaker for trees of equal frequency
1634 */
1635
1636 this.l_buf = 0; /* buffer index for literals or lengths */
1637
1638 this.lit_bufsize = 0;
1639 /* Size of match buffer for literals/lengths. There are 4 reasons for
1640 * limiting lit_bufsize to 64K:
1641 * - frequencies can be kept in 16 bit counters
1642 * - if compression is not successful for the first block, all input
1643 * data is still in the window so we can still emit a stored block even
1644 * when input comes from standard input. (This can also be done for
1645 * all blocks if lit_bufsize is not greater than 32K.)
1646 * - if compression is not successful for a file smaller than 64K, we can
1647 * even emit a stored file instead of a stored block (saving 5 bytes).
1648 * This is applicable only for zip (not gzip or zlib).
1649 * - creating new Huffman trees less frequently may not provide fast
1650 * adaptation to changes in the input data statistics. (Take for
1651 * example a binary file with poorly compressible code followed by
1652 * a highly compressible string table.) Smaller buffer sizes give
1653 * fast adaptation but have of course the overhead of transmitting
1654 * trees more frequently.
1655 * - I can't count above 4
1656 */
1657
1658 this.last_lit = 0; /* running index in l_buf */
1659
1660 this.d_buf = 0;
1661 /* Buffer index for distances. To simplify the code, d_buf and l_buf have
1662 * the same number of elements. To use different lengths, an extra flag
1663 * array would be necessary.
1664 */
1665
1666 this.opt_len = 0; /* bit length of current block with optimal trees */
1667 this.static_len = 0; /* bit length of current block with static trees */
1668 this.matches = 0; /* number of string matches in current block */
1669 this.insert = 0; /* bytes at end of window left to insert */
1670
1671
1672 this.bi_buf = 0;
1673 /* Output buffer. bits are inserted starting at the bottom (least
1674 * significant bits).
1675 */
1676 this.bi_valid = 0;
1677 /* Number of valid bits in bi_buf. All bits above the last valid bit
1678 * are always zero.
1679 */
1680
1681 // Used for window memory init. We safely ignore it for JS. That makes
1682 // sense only for pointers and memory check tools.
1683 //this.high_water = 0;
1684 /* High water mark offset in window for initialized bytes -- bytes above
1685 * this are set to zero in order to avoid memory check warnings when
1686 * longest match routines access bytes past the input. This is then
1687 * updated to the new high water mark.
1688 */
1689}
1690
1691
1692function deflateResetKeep(strm) {
1693 var s;
1694
1695 if (!strm || !strm.state) {
1696 return err(strm, Z_STREAM_ERROR);
1697 }
1698
1699 strm.total_in = strm.total_out = 0;
1700 strm.data_type = Z_UNKNOWN;
1701
1702 s = strm.state;
1703 s.pending = 0;
1704 s.pending_out = 0;
1705
1706 if (s.wrap < 0) {
1707 s.wrap = -s.wrap;
1708 /* was made negative by deflate(..., Z_FINISH); */
1709 }
1710 s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
1711 strm.adler = (s.wrap === 2) ?
1712 0 // crc32(0, Z_NULL, 0)
1713 :
1714 1; // adler32(0, Z_NULL, 0)
1715 s.last_flush = Z_NO_FLUSH;
1716 trees._tr_init(s);
1717 return Z_OK;
1718}
1719
1720
1721function deflateReset(strm) {
1722 var ret = deflateResetKeep(strm);
1723 if (ret === Z_OK) {
1724 lm_init(strm.state);
1725 }
1726 return ret;
1727}
1728
1729
1730function deflateSetHeader(strm, head) {
1731 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1732 if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
1733 strm.state.gzhead = head;
1734 return Z_OK;
1735}
1736
1737
1738function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
1739 if (!strm) { // === Z_NULL
1740 return Z_STREAM_ERROR;
1741 }
1742 var wrap = 1;
1743
1744 if (level === Z_DEFAULT_COMPRESSION) {
1745 level = 6;
1746 }
1747
1748 if (windowBits < 0) { /* suppress zlib wrapper */
1749 wrap = 0;
1750 windowBits = -windowBits;
1751 }
1752
1753 else if (windowBits > 15) {
1754 wrap = 2; /* write gzip wrapper instead */
1755 windowBits -= 16;
1756 }
1757
1758
1759 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
1760 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
1761 strategy < 0 || strategy > Z_FIXED) {
1762 return err(strm, Z_STREAM_ERROR);
1763 }
1764
1765
1766 if (windowBits === 8) {
1767 windowBits = 9;
1768 }
1769 /* until 256-byte window bug fixed */
1770
1771 var s = new DeflateState();
1772
1773 strm.state = s;
1774 s.strm = strm;
1775
1776 s.wrap = wrap;
1777 s.gzhead = null;
1778 s.w_bits = windowBits;
1779 s.w_size = 1 << s.w_bits;
1780 s.w_mask = s.w_size - 1;
1781
1782 s.hash_bits = memLevel + 7;
1783 s.hash_size = 1 << s.hash_bits;
1784 s.hash_mask = s.hash_size - 1;
1785 s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
1786
1787 s.window = new utils.Buf8(s.w_size * 2);
1788 s.head = new utils.Buf16(s.hash_size);
1789 s.prev = new utils.Buf16(s.w_size);
1790
1791 // Don't need mem init magic for JS.
1792 //s.high_water = 0; /* nothing written to s->window yet */
1793
1794 s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
1795
1796 s.pending_buf_size = s.lit_bufsize * 4;
1797
1798 //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
1799 //s->pending_buf = (uchf *) overlay;
1800 s.pending_buf = new utils.Buf8(s.pending_buf_size);
1801
1802 // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
1803 //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
1804 s.d_buf = 1 * s.lit_bufsize;
1805
1806 //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
1807 s.l_buf = (1 + 2) * s.lit_bufsize;
1808
1809 s.level = level;
1810 s.strategy = strategy;
1811 s.method = method;
1812
1813 return deflateReset(strm);
1814}
1815
1816function deflateInit(strm, level) {
1817 return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
1818}
1819
1820
1821function deflate(strm, flush) {
1822 var old_flush, s;
1823 var beg, val; // for gzip header write only
1824
1825 if (!strm || !strm.state ||
1826 flush > Z_BLOCK || flush < 0) {
1827 return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
1828 }
1829
1830 s = strm.state;
1831
1832 if (!strm.output ||
1833 (!strm.input && strm.avail_in !== 0) ||
1834 (s.status === FINISH_STATE && flush !== Z_FINISH)) {
1835 return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
1836 }
1837
1838 s.strm = strm; /* just in case */
1839 old_flush = s.last_flush;
1840 s.last_flush = flush;
1841
1842 /* Write the header */
1843 if (s.status === INIT_STATE) {
1844
1845 if (s.wrap === 2) { // GZIP header
1846 strm.adler = 0; //crc32(0L, Z_NULL, 0);
1847 put_byte(s, 31);
1848 put_byte(s, 139);
1849 put_byte(s, 8);
1850 if (!s.gzhead) { // s->gzhead == Z_NULL
1851 put_byte(s, 0);
1852 put_byte(s, 0);
1853 put_byte(s, 0);
1854 put_byte(s, 0);
1855 put_byte(s, 0);
1856 put_byte(s, s.level === 9 ? 2 :
1857 (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1858 4 : 0));
1859 put_byte(s, OS_CODE);
1860 s.status = BUSY_STATE;
1861 }
1862 else {
1863 put_byte(s, (s.gzhead.text ? 1 : 0) +
1864 (s.gzhead.hcrc ? 2 : 0) +
1865 (!s.gzhead.extra ? 0 : 4) +
1866 (!s.gzhead.name ? 0 : 8) +
1867 (!s.gzhead.comment ? 0 : 16)
1868 );
1869 put_byte(s, s.gzhead.time & 0xff);
1870 put_byte(s, (s.gzhead.time >> 8) & 0xff);
1871 put_byte(s, (s.gzhead.time >> 16) & 0xff);
1872 put_byte(s, (s.gzhead.time >> 24) & 0xff);
1873 put_byte(s, s.level === 9 ? 2 :
1874 (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1875 4 : 0));
1876 put_byte(s, s.gzhead.os & 0xff);
1877 if (s.gzhead.extra && s.gzhead.extra.length) {
1878 put_byte(s, s.gzhead.extra.length & 0xff);
1879 put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
1880 }
1881 if (s.gzhead.hcrc) {
1882 strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
1883 }
1884 s.gzindex = 0;
1885 s.status = EXTRA_STATE;
1886 }
1887 }
1888 else // DEFLATE header
1889 {
1890 var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
1891 var level_flags = -1;
1892
1893 if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
1894 level_flags = 0;
1895 } else if (s.level < 6) {
1896 level_flags = 1;
1897 } else if (s.level === 6) {
1898 level_flags = 2;
1899 } else {
1900 level_flags = 3;
1901 }
1902 header |= (level_flags << 6);
1903 if (s.strstart !== 0) { header |= PRESET_DICT; }
1904 header += 31 - (header % 31);
1905
1906 s.status = BUSY_STATE;
1907 putShortMSB(s, header);
1908
1909 /* Save the adler32 of the preset dictionary: */
1910 if (s.strstart !== 0) {
1911 putShortMSB(s, strm.adler >>> 16);
1912 putShortMSB(s, strm.adler & 0xffff);
1913 }
1914 strm.adler = 1; // adler32(0L, Z_NULL, 0);
1915 }
1916 }
1917
1918//#ifdef GZIP
1919 if (s.status === EXTRA_STATE) {
1920 if (s.gzhead.extra/* != Z_NULL*/) {
1921 beg = s.pending; /* start of bytes to update crc */
1922
1923 while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
1924 if (s.pending === s.pending_buf_size) {
1925 if (s.gzhead.hcrc && s.pending > beg) {
1926 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1927 }
1928 flush_pending(strm);
1929 beg = s.pending;
1930 if (s.pending === s.pending_buf_size) {
1931 break;
1932 }
1933 }
1934 put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
1935 s.gzindex++;
1936 }
1937 if (s.gzhead.hcrc && s.pending > beg) {
1938 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1939 }
1940 if (s.gzindex === s.gzhead.extra.length) {
1941 s.gzindex = 0;
1942 s.status = NAME_STATE;
1943 }
1944 }
1945 else {
1946 s.status = NAME_STATE;
1947 }
1948 }
1949 if (s.status === NAME_STATE) {
1950 if (s.gzhead.name/* != Z_NULL*/) {
1951 beg = s.pending; /* start of bytes to update crc */
1952 //int val;
1953
1954 do {
1955 if (s.pending === s.pending_buf_size) {
1956 if (s.gzhead.hcrc && s.pending > beg) {
1957 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1958 }
1959 flush_pending(strm);
1960 beg = s.pending;
1961 if (s.pending === s.pending_buf_size) {
1962 val = 1;
1963 break;
1964 }
1965 }
1966 // JS specific: little magic to add zero terminator to end of string
1967 if (s.gzindex < s.gzhead.name.length) {
1968 val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
1969 } else {
1970 val = 0;
1971 }
1972 put_byte(s, val);
1973 } while (val !== 0);
1974
1975 if (s.gzhead.hcrc && s.pending > beg) {
1976 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1977 }
1978 if (val === 0) {
1979 s.gzindex = 0;
1980 s.status = COMMENT_STATE;
1981 }
1982 }
1983 else {
1984 s.status = COMMENT_STATE;
1985 }
1986 }
1987 if (s.status === COMMENT_STATE) {
1988 if (s.gzhead.comment/* != Z_NULL*/) {
1989 beg = s.pending; /* start of bytes to update crc */
1990 //int val;
1991
1992 do {
1993 if (s.pending === s.pending_buf_size) {
1994 if (s.gzhead.hcrc && s.pending > beg) {
1995 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1996 }
1997 flush_pending(strm);
1998 beg = s.pending;
1999 if (s.pending === s.pending_buf_size) {
2000 val = 1;
2001 break;
2002 }
2003 }
2004 // JS specific: little magic to add zero terminator to end of string
2005 if (s.gzindex < s.gzhead.comment.length) {
2006 val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
2007 } else {
2008 val = 0;
2009 }
2010 put_byte(s, val);
2011 } while (val !== 0);
2012
2013 if (s.gzhead.hcrc && s.pending > beg) {
2014 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
2015 }
2016 if (val === 0) {
2017 s.status = HCRC_STATE;
2018 }
2019 }
2020 else {
2021 s.status = HCRC_STATE;
2022 }
2023 }
2024 if (s.status === HCRC_STATE) {
2025 if (s.gzhead.hcrc) {
2026 if (s.pending + 2 > s.pending_buf_size) {
2027 flush_pending(strm);
2028 }
2029 if (s.pending + 2 <= s.pending_buf_size) {
2030 put_byte(s, strm.adler & 0xff);
2031 put_byte(s, (strm.adler >> 8) & 0xff);
2032 strm.adler = 0; //crc32(0L, Z_NULL, 0);
2033 s.status = BUSY_STATE;
2034 }
2035 }
2036 else {
2037 s.status = BUSY_STATE;
2038 }
2039 }
2040//#endif
2041
2042 /* Flush as much pending output as possible */
2043 if (s.pending !== 0) {
2044 flush_pending(strm);
2045 if (strm.avail_out === 0) {
2046 /* Since avail_out is 0, deflate will be called again with
2047 * more output space, but possibly with both pending and
2048 * avail_in equal to zero. There won't be anything to do,
2049 * but this is not an error situation so make sure we
2050 * return OK instead of BUF_ERROR at next call of deflate:
2051 */
2052 s.last_flush = -1;
2053 return Z_OK;
2054 }
2055
2056 /* Make sure there is something to do and avoid duplicate consecutive
2057 * flushes. For repeated and useless calls with Z_FINISH, we keep
2058 * returning Z_STREAM_END instead of Z_BUF_ERROR.
2059 */
2060 } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
2061 flush !== Z_FINISH) {
2062 return err(strm, Z_BUF_ERROR);
2063 }
2064
2065 /* User must not provide more input after the first FINISH: */
2066 if (s.status === FINISH_STATE && strm.avail_in !== 0) {
2067 return err(strm, Z_BUF_ERROR);
2068 }
2069
2070 /* Start a new block or continue the current one.
2071 */
2072 if (strm.avail_in !== 0 || s.lookahead !== 0 ||
2073 (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
2074 var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
2075 (s.strategy === Z_RLE ? deflate_rle(s, flush) :
2076 configuration_table[s.level].func(s, flush));
2077
2078 if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
2079 s.status = FINISH_STATE;
2080 }
2081 if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
2082 if (strm.avail_out === 0) {
2083 s.last_flush = -1;
2084 /* avoid BUF_ERROR next call, see above */
2085 }
2086 return Z_OK;
2087 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
2088 * of deflate should use the same flush parameter to make sure
2089 * that the flush is complete. So we don't have to output an
2090 * empty block here, this will be done at next call. This also
2091 * ensures that for a very small output buffer, we emit at most
2092 * one empty block.
2093 */
2094 }
2095 if (bstate === BS_BLOCK_DONE) {
2096 if (flush === Z_PARTIAL_FLUSH) {
2097 trees._tr_align(s);
2098 }
2099 else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
2100
2101 trees._tr_stored_block(s, 0, 0, false);
2102 /* For a full flush, this empty block will be recognized
2103 * as a special marker by inflate_sync().
2104 */
2105 if (flush === Z_FULL_FLUSH) {
2106 /*** CLEAR_HASH(s); ***/ /* forget history */
2107 zero(s.head); // Fill with NIL (= 0);
2108
2109 if (s.lookahead === 0) {
2110 s.strstart = 0;
2111 s.block_start = 0;
2112 s.insert = 0;
2113 }
2114 }
2115 }
2116 flush_pending(strm);
2117 if (strm.avail_out === 0) {
2118 s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
2119 return Z_OK;
2120 }
2121 }
2122 }
2123 //Assert(strm->avail_out > 0, "bug2");
2124 //if (strm.avail_out <= 0) { throw new Error("bug2");}
2125
2126 if (flush !== Z_FINISH) { return Z_OK; }
2127 if (s.wrap <= 0) { return Z_STREAM_END; }
2128
2129 /* Write the trailer */
2130 if (s.wrap === 2) {
2131 put_byte(s, strm.adler & 0xff);
2132 put_byte(s, (strm.adler >> 8) & 0xff);
2133 put_byte(s, (strm.adler >> 16) & 0xff);
2134 put_byte(s, (strm.adler >> 24) & 0xff);
2135 put_byte(s, strm.total_in & 0xff);
2136 put_byte(s, (strm.total_in >> 8) & 0xff);
2137 put_byte(s, (strm.total_in >> 16) & 0xff);
2138 put_byte(s, (strm.total_in >> 24) & 0xff);
2139 }
2140 else
2141 {
2142 putShortMSB(s, strm.adler >>> 16);
2143 putShortMSB(s, strm.adler & 0xffff);
2144 }
2145
2146 flush_pending(strm);
2147 /* If avail_out is zero, the application will call deflate again
2148 * to flush the rest.
2149 */
2150 if (s.wrap > 0) { s.wrap = -s.wrap; }
2151 /* write the trailer only once! */
2152 return s.pending !== 0 ? Z_OK : Z_STREAM_END;
2153}
2154
2155function deflateEnd(strm) {
2156 var status;
2157
2158 if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
2159 return Z_STREAM_ERROR;
2160 }
2161
2162 status = strm.state.status;
2163 if (status !== INIT_STATE &&
2164 status !== EXTRA_STATE &&
2165 status !== NAME_STATE &&
2166 status !== COMMENT_STATE &&
2167 status !== HCRC_STATE &&
2168 status !== BUSY_STATE &&
2169 status !== FINISH_STATE
2170 ) {
2171 return err(strm, Z_STREAM_ERROR);
2172 }
2173
2174 strm.state = null;
2175
2176 return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
2177}
2178
2179
2180/* =========================================================================
2181 * Initializes the compression dictionary from the given byte
2182 * sequence without producing any compressed output.
2183 */
2184function deflateSetDictionary(strm, dictionary) {
2185 var dictLength = dictionary.length;
2186
2187 var s;
2188 var str, n;
2189 var wrap;
2190 var avail;
2191 var next;
2192 var input;
2193 var tmpDict;
2194
2195 if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
2196 return Z_STREAM_ERROR;
2197 }
2198
2199 s = strm.state;
2200 wrap = s.wrap;
2201
2202 if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
2203 return Z_STREAM_ERROR;
2204 }
2205
2206 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
2207 if (wrap === 1) {
2208 /* adler32(strm->adler, dictionary, dictLength); */
2209 strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
2210 }
2211
2212 s.wrap = 0; /* avoid computing Adler-32 in read_buf */
2213
2214 /* if dictionary would fill window, just replace the history */
2215 if (dictLength >= s.w_size) {
2216 if (wrap === 0) { /* already empty otherwise */
2217 /*** CLEAR_HASH(s); ***/
2218 zero(s.head); // Fill with NIL (= 0);
2219 s.strstart = 0;
2220 s.block_start = 0;
2221 s.insert = 0;
2222 }
2223 /* use the tail */
2224 // dictionary = dictionary.slice(dictLength - s.w_size);
2225 tmpDict = new utils.Buf8(s.w_size);
2226 utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
2227 dictionary = tmpDict;
2228 dictLength = s.w_size;
2229 }
2230 /* insert dictionary into window and hash */
2231 avail = strm.avail_in;
2232 next = strm.next_in;
2233 input = strm.input;
2234 strm.avail_in = dictLength;
2235 strm.next_in = 0;
2236 strm.input = dictionary;
2237 fill_window(s);
2238 while (s.lookahead >= MIN_MATCH) {
2239 str = s.strstart;
2240 n = s.lookahead - (MIN_MATCH - 1);
2241 do {
2242 /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
2243 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
2244
2245 s.prev[str & s.w_mask] = s.head[s.ins_h];
2246
2247 s.head[s.ins_h] = str;
2248 str++;
2249 } while (--n);
2250 s.strstart = str;
2251 s.lookahead = MIN_MATCH - 1;
2252 fill_window(s);
2253 }
2254 s.strstart += s.lookahead;
2255 s.block_start = s.strstart;
2256 s.insert = s.lookahead;
2257 s.lookahead = 0;
2258 s.match_length = s.prev_length = MIN_MATCH - 1;
2259 s.match_available = 0;
2260 strm.next_in = next;
2261 strm.input = input;
2262 strm.avail_in = avail;
2263 s.wrap = wrap;
2264 return Z_OK;
2265}
2266
2267
2268exports.deflateInit = deflateInit;
2269exports.deflateInit2 = deflateInit2;
2270exports.deflateReset = deflateReset;
2271exports.deflateResetKeep = deflateResetKeep;
2272exports.deflateSetHeader = deflateSetHeader;
2273exports.deflate = deflate;
2274exports.deflateEnd = deflateEnd;
2275exports.deflateSetDictionary = deflateSetDictionary;
2276exports.deflateInfo = 'pako deflate (from Nodeca project)';
2277
2278/* Not implemented
2279exports.deflateBound = deflateBound;
2280exports.deflateCopy = deflateCopy;
2281exports.deflateParams = deflateParams;
2282exports.deflatePending = deflatePending;
2283exports.deflatePrime = deflatePrime;
2284exports.deflateTune = deflateTune;
2285*/
2286
2287},{"../utils/common":1,"./adler32":3,"./crc32":4,"./messages":6,"./trees":7}],6:[function(require,module,exports){
2288'use strict';
2289
2290// (C) 1995-2013 Jean-loup Gailly and Mark Adler
2291// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
2292//
2293// This software is provided 'as-is', without any express or implied
2294// warranty. In no event will the authors be held liable for any damages
2295// arising from the use of this software.
2296//
2297// Permission is granted to anyone to use this software for any purpose,
2298// including commercial applications, and to alter it and redistribute it
2299// freely, subject to the following restrictions:
2300//
2301// 1. The origin of this software must not be misrepresented; you must not
2302// claim that you wrote the original software. If you use this software
2303// in a product, an acknowledgment in the product documentation would be
2304// appreciated but is not required.
2305// 2. Altered source versions must be plainly marked as such, and must not be
2306// misrepresented as being the original software.
2307// 3. This notice may not be removed or altered from any source distribution.
2308
2309module.exports = {
2310 2: 'need dictionary', /* Z_NEED_DICT 2 */
2311 1: 'stream end', /* Z_STREAM_END 1 */
2312 0: '', /* Z_OK 0 */
2313 '-1': 'file error', /* Z_ERRNO (-1) */
2314 '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
2315 '-3': 'data error', /* Z_DATA_ERROR (-3) */
2316 '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
2317 '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
2318 '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
2319};
2320
2321},{}],7:[function(require,module,exports){
2322'use strict';
2323
2324// (C) 1995-2013 Jean-loup Gailly and Mark Adler
2325// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
2326//
2327// This software is provided 'as-is', without any express or implied
2328// warranty. In no event will the authors be held liable for any damages
2329// arising from the use of this software.
2330//
2331// Permission is granted to anyone to use this software for any purpose,
2332// including commercial applications, and to alter it and redistribute it
2333// freely, subject to the following restrictions:
2334//
2335// 1. The origin of this software must not be misrepresented; you must not
2336// claim that you wrote the original software. If you use this software
2337// in a product, an acknowledgment in the product documentation would be
2338// appreciated but is not required.
2339// 2. Altered source versions must be plainly marked as such, and must not be
2340// misrepresented as being the original software.
2341// 3. This notice may not be removed or altered from any source distribution.
2342
2343/* eslint-disable space-unary-ops */
2344
2345var utils = require('../utils/common');
2346
2347/* Public constants ==========================================================*/
2348/* ===========================================================================*/
2349
2350
2351//var Z_FILTERED = 1;
2352//var Z_HUFFMAN_ONLY = 2;
2353//var Z_RLE = 3;
2354var Z_FIXED = 4;
2355//var Z_DEFAULT_STRATEGY = 0;
2356
2357/* Possible values of the data_type field (though see inflate()) */
2358var Z_BINARY = 0;
2359var Z_TEXT = 1;
2360//var Z_ASCII = 1; // = Z_TEXT
2361var Z_UNKNOWN = 2;
2362
2363/*============================================================================*/
2364
2365
2366function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
2367
2368// From zutil.h
2369
2370var STORED_BLOCK = 0;
2371var STATIC_TREES = 1;
2372var DYN_TREES = 2;
2373/* The three kinds of block type */
2374
2375var MIN_MATCH = 3;
2376var MAX_MATCH = 258;
2377/* The minimum and maximum match lengths */
2378
2379// From deflate.h
2380/* ===========================================================================
2381 * Internal compression state.
2382 */
2383
2384var LENGTH_CODES = 29;
2385/* number of length codes, not counting the special END_BLOCK code */
2386
2387var LITERALS = 256;
2388/* number of literal bytes 0..255 */
2389
2390var L_CODES = LITERALS + 1 + LENGTH_CODES;
2391/* number of Literal or Length codes, including the END_BLOCK code */
2392
2393var D_CODES = 30;
2394/* number of distance codes */
2395
2396var BL_CODES = 19;
2397/* number of codes used to transfer the bit lengths */
2398
2399var HEAP_SIZE = 2 * L_CODES + 1;
2400/* maximum heap size */
2401
2402var MAX_BITS = 15;
2403/* All codes must not exceed MAX_BITS bits */
2404
2405var Buf_size = 16;
2406/* size of bit buffer in bi_buf */
2407
2408
2409/* ===========================================================================
2410 * Constants
2411 */
2412
2413var MAX_BL_BITS = 7;
2414/* Bit length codes must not exceed MAX_BL_BITS bits */
2415
2416var END_BLOCK = 256;
2417/* end of block literal code */
2418
2419var REP_3_6 = 16;
2420/* repeat previous bit length 3-6 times (2 bits of repeat count) */
2421
2422var REPZ_3_10 = 17;
2423/* repeat a zero length 3-10 times (3 bits of repeat count) */
2424
2425var REPZ_11_138 = 18;
2426/* repeat a zero length 11-138 times (7 bits of repeat count) */
2427
2428/* eslint-disable comma-spacing,array-bracket-spacing */
2429var extra_lbits = /* extra bits for each length code */
2430 [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0];
2431
2432var extra_dbits = /* extra bits for each distance code */
2433 [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];
2434
2435var extra_blbits = /* extra bits for each bit length code */
2436 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
2437
2438var bl_order =
2439 [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
2440/* eslint-enable comma-spacing,array-bracket-spacing */
2441
2442/* The lengths of the bit length codes are sent in order of decreasing
2443 * probability, to avoid transmitting the lengths for unused bit length codes.
2444 */
2445
2446/* ===========================================================================
2447 * Local data. These are initialized only once.
2448 */
2449
2450// We pre-fill arrays with 0 to avoid uninitialized gaps
2451
2452var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
2453
2454// !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
2455var static_ltree = new Array((L_CODES + 2) * 2);
2456zero(static_ltree);
2457/* The static literal tree. Since the bit lengths are imposed, there is no
2458 * need for the L_CODES extra codes used during heap construction. However
2459 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
2460 * below).
2461 */
2462
2463var static_dtree = new Array(D_CODES * 2);
2464zero(static_dtree);
2465/* The static distance tree. (Actually a trivial tree since all codes use
2466 * 5 bits.)
2467 */
2468
2469var _dist_code = new Array(DIST_CODE_LEN);
2470zero(_dist_code);
2471/* Distance codes. The first 256 values correspond to the distances
2472 * 3 .. 258, the last 256 values correspond to the top 8 bits of
2473 * the 15 bit distances.
2474 */
2475
2476var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
2477zero(_length_code);
2478/* length code for each normalized match length (0 == MIN_MATCH) */
2479
2480var base_length = new Array(LENGTH_CODES);
2481zero(base_length);
2482/* First normalized length for each code (0 = MIN_MATCH) */
2483
2484var base_dist = new Array(D_CODES);
2485zero(base_dist);
2486/* First normalized distance for each code (0 = distance of 1) */
2487
2488
2489function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
2490
2491 this.static_tree = static_tree; /* static tree or NULL */
2492 this.extra_bits = extra_bits; /* extra bits for each code or NULL */
2493 this.extra_base = extra_base; /* base index for extra_bits */
2494 this.elems = elems; /* max number of elements in the tree */
2495 this.max_length = max_length; /* max bit length for the codes */
2496
2497 // show if `static_tree` has data or dummy - needed for monomorphic objects
2498 this.has_stree = static_tree && static_tree.length;
2499}
2500
2501
2502var static_l_desc;
2503var static_d_desc;
2504var static_bl_desc;
2505
2506
2507function TreeDesc(dyn_tree, stat_desc) {
2508 this.dyn_tree = dyn_tree; /* the dynamic tree */
2509 this.max_code = 0; /* largest code with non zero frequency */
2510 this.stat_desc = stat_desc; /* the corresponding static tree */
2511}
2512
2513
2514
2515function d_code(dist) {
2516 return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
2517}
2518
2519
2520/* ===========================================================================
2521 * Output a short LSB first on the stream.
2522 * IN assertion: there is enough room in pendingBuf.
2523 */
2524function put_short(s, w) {
2525// put_byte(s, (uch)((w) & 0xff));
2526// put_byte(s, (uch)((ush)(w) >> 8));
2527 s.pending_buf[s.pending++] = (w) & 0xff;
2528 s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
2529}
2530
2531
2532/* ===========================================================================
2533 * Send a value on a given number of bits.
2534 * IN assertion: length <= 16 and value fits in length bits.
2535 */
2536function send_bits(s, value, length) {
2537 if (s.bi_valid > (Buf_size - length)) {
2538 s.bi_buf |= (value << s.bi_valid) & 0xffff;
2539 put_short(s, s.bi_buf);
2540 s.bi_buf = value >> (Buf_size - s.bi_valid);
2541 s.bi_valid += length - Buf_size;
2542 } else {
2543 s.bi_buf |= (value << s.bi_valid) & 0xffff;
2544 s.bi_valid += length;
2545 }
2546}
2547
2548
2549function send_code(s, c, tree) {
2550 send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
2551}
2552
2553
2554/* ===========================================================================
2555 * Reverse the first len bits of a code, using straightforward code (a faster
2556 * method would use a table)
2557 * IN assertion: 1 <= len <= 15
2558 */
2559function bi_reverse(code, len) {
2560 var res = 0;
2561 do {
2562 res |= code & 1;
2563 code >>>= 1;
2564 res <<= 1;
2565 } while (--len > 0);
2566 return res >>> 1;
2567}
2568
2569
2570/* ===========================================================================
2571 * Flush the bit buffer, keeping at most 7 bits in it.
2572 */
2573function bi_flush(s) {
2574 if (s.bi_valid === 16) {
2575 put_short(s, s.bi_buf);
2576 s.bi_buf = 0;
2577 s.bi_valid = 0;
2578
2579 } else if (s.bi_valid >= 8) {
2580 s.pending_buf[s.pending++] = s.bi_buf & 0xff;
2581 s.bi_buf >>= 8;
2582 s.bi_valid -= 8;
2583 }
2584}
2585
2586
2587/* ===========================================================================
2588 * Compute the optimal bit lengths for a tree and update the total bit length
2589 * for the current block.
2590 * IN assertion: the fields freq and dad are set, heap[heap_max] and
2591 * above are the tree nodes sorted by increasing frequency.
2592 * OUT assertions: the field len is set to the optimal bit length, the
2593 * array bl_count contains the frequencies for each bit length.
2594 * The length opt_len is updated; static_len is also updated if stree is
2595 * not null.
2596 */
2597function gen_bitlen(s, desc)
2598// deflate_state *s;
2599// tree_desc *desc; /* the tree descriptor */
2600{
2601 var tree = desc.dyn_tree;
2602 var max_code = desc.max_code;
2603 var stree = desc.stat_desc.static_tree;
2604 var has_stree = desc.stat_desc.has_stree;
2605 var extra = desc.stat_desc.extra_bits;
2606 var base = desc.stat_desc.extra_base;
2607 var max_length = desc.stat_desc.max_length;
2608 var h; /* heap index */
2609 var n, m; /* iterate over the tree elements */
2610 var bits; /* bit length */
2611 var xbits; /* extra bits */
2612 var f; /* frequency */
2613 var overflow = 0; /* number of elements with bit length too large */
2614
2615 for (bits = 0; bits <= MAX_BITS; bits++) {
2616 s.bl_count[bits] = 0;
2617 }
2618
2619 /* In a first pass, compute the optimal bit lengths (which may
2620 * overflow in the case of the bit length tree).
2621 */
2622 tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
2623
2624 for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
2625 n = s.heap[h];
2626 bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
2627 if (bits > max_length) {
2628 bits = max_length;
2629 overflow++;
2630 }
2631 tree[n * 2 + 1]/*.Len*/ = bits;
2632 /* We overwrite tree[n].Dad which is no longer needed */
2633
2634 if (n > max_code) { continue; } /* not a leaf node */
2635
2636 s.bl_count[bits]++;
2637 xbits = 0;
2638 if (n >= base) {
2639 xbits = extra[n - base];
2640 }
2641 f = tree[n * 2]/*.Freq*/;
2642 s.opt_len += f * (bits + xbits);
2643 if (has_stree) {
2644 s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
2645 }
2646 }
2647 if (overflow === 0) { return; }
2648
2649 // Trace((stderr,"\nbit length overflow\n"));
2650 /* This happens for example on obj2 and pic of the Calgary corpus */
2651
2652 /* Find the first bit length which could increase: */
2653 do {
2654 bits = max_length - 1;
2655 while (s.bl_count[bits] === 0) { bits--; }
2656 s.bl_count[bits]--; /* move one leaf down the tree */
2657 s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
2658 s.bl_count[max_length]--;
2659 /* The brother of the overflow item also moves one step up,
2660 * but this does not affect bl_count[max_length]
2661 */
2662 overflow -= 2;
2663 } while (overflow > 0);
2664
2665 /* Now recompute all bit lengths, scanning in increasing frequency.
2666 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
2667 * lengths instead of fixing only the wrong ones. This idea is taken
2668 * from 'ar' written by Haruhiko Okumura.)
2669 */
2670 for (bits = max_length; bits !== 0; bits--) {
2671 n = s.bl_count[bits];
2672 while (n !== 0) {
2673 m = s.heap[--h];
2674 if (m > max_code) { continue; }
2675 if (tree[m * 2 + 1]/*.Len*/ !== bits) {
2676 // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
2677 s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
2678 tree[m * 2 + 1]/*.Len*/ = bits;
2679 }
2680 n--;
2681 }
2682 }
2683}
2684
2685
2686/* ===========================================================================
2687 * Generate the codes for a given tree and bit counts (which need not be
2688 * optimal).
2689 * IN assertion: the array bl_count contains the bit length statistics for
2690 * the given tree and the field len is set for all tree elements.
2691 * OUT assertion: the field code is set for all tree elements of non
2692 * zero code length.
2693 */
2694function gen_codes(tree, max_code, bl_count)
2695// ct_data *tree; /* the tree to decorate */
2696// int max_code; /* largest code with non zero frequency */
2697// ushf *bl_count; /* number of codes at each bit length */
2698{
2699 var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
2700 var code = 0; /* running code value */
2701 var bits; /* bit index */
2702 var n; /* code index */
2703
2704 /* The distribution counts are first used to generate the code values
2705 * without bit reversal.
2706 */
2707 for (bits = 1; bits <= MAX_BITS; bits++) {
2708 next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
2709 }
2710 /* Check that the bit counts in bl_count are consistent. The last code
2711 * must be all ones.
2712 */
2713 //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
2714 // "inconsistent bit counts");
2715 //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
2716
2717 for (n = 0; n <= max_code; n++) {
2718 var len = tree[n * 2 + 1]/*.Len*/;
2719 if (len === 0) { continue; }
2720 /* Now reverse the bits */
2721 tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
2722
2723 //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
2724 // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
2725 }
2726}
2727
2728
2729/* ===========================================================================
2730 * Initialize the various 'constant' tables.
2731 */
2732function tr_static_init() {
2733 var n; /* iterates over tree elements */
2734 var bits; /* bit counter */
2735 var length; /* length value */
2736 var code; /* code value */
2737 var dist; /* distance index */
2738 var bl_count = new Array(MAX_BITS + 1);
2739 /* number of codes at each bit length for an optimal tree */
2740
2741 // do check in _tr_init()
2742 //if (static_init_done) return;
2743
2744 /* For some embedded targets, global variables are not initialized: */
2745/*#ifdef NO_INIT_GLOBAL_POINTERS
2746 static_l_desc.static_tree = static_ltree;
2747 static_l_desc.extra_bits = extra_lbits;
2748 static_d_desc.static_tree = static_dtree;
2749 static_d_desc.extra_bits = extra_dbits;
2750 static_bl_desc.extra_bits = extra_blbits;
2751#endif*/
2752
2753 /* Initialize the mapping length (0..255) -> length code (0..28) */
2754 length = 0;
2755 for (code = 0; code < LENGTH_CODES - 1; code++) {
2756 base_length[code] = length;
2757 for (n = 0; n < (1 << extra_lbits[code]); n++) {
2758 _length_code[length++] = code;
2759 }
2760 }
2761 //Assert (length == 256, "tr_static_init: length != 256");
2762 /* Note that the length 255 (match length 258) can be represented
2763 * in two different ways: code 284 + 5 bits or code 285, so we
2764 * overwrite length_code[255] to use the best encoding:
2765 */
2766 _length_code[length - 1] = code;
2767
2768 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
2769 dist = 0;
2770 for (code = 0; code < 16; code++) {
2771 base_dist[code] = dist;
2772 for (n = 0; n < (1 << extra_dbits[code]); n++) {
2773 _dist_code[dist++] = code;
2774 }
2775 }
2776 //Assert (dist == 256, "tr_static_init: dist != 256");
2777 dist >>= 7; /* from now on, all distances are divided by 128 */
2778 for (; code < D_CODES; code++) {
2779 base_dist[code] = dist << 7;
2780 for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
2781 _dist_code[256 + dist++] = code;
2782 }
2783 }
2784 //Assert (dist == 256, "tr_static_init: 256+dist != 512");
2785
2786 /* Construct the codes of the static literal tree */
2787 for (bits = 0; bits <= MAX_BITS; bits++) {
2788 bl_count[bits] = 0;
2789 }
2790
2791 n = 0;
2792 while (n <= 143) {
2793 static_ltree[n * 2 + 1]/*.Len*/ = 8;
2794 n++;
2795 bl_count[8]++;
2796 }
2797 while (n <= 255) {
2798 static_ltree[n * 2 + 1]/*.Len*/ = 9;
2799 n++;
2800 bl_count[9]++;
2801 }
2802 while (n <= 279) {
2803 static_ltree[n * 2 + 1]/*.Len*/ = 7;
2804 n++;
2805 bl_count[7]++;
2806 }
2807 while (n <= 287) {
2808 static_ltree[n * 2 + 1]/*.Len*/ = 8;
2809 n++;
2810 bl_count[8]++;
2811 }
2812 /* Codes 286 and 287 do not exist, but we must include them in the
2813 * tree construction to get a canonical Huffman tree (longest code
2814 * all ones)
2815 */
2816 gen_codes(static_ltree, L_CODES + 1, bl_count);
2817
2818 /* The static distance tree is trivial: */
2819 for (n = 0; n < D_CODES; n++) {
2820 static_dtree[n * 2 + 1]/*.Len*/ = 5;
2821 static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
2822 }
2823
2824 // Now data ready and we can init static trees
2825 static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
2826 static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
2827 static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
2828
2829 //static_init_done = true;
2830}
2831
2832
2833/* ===========================================================================
2834 * Initialize a new block.
2835 */
2836function init_block(s) {
2837 var n; /* iterates over tree elements */
2838
2839 /* Initialize the trees. */
2840 for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
2841 for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
2842 for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
2843
2844 s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
2845 s.opt_len = s.static_len = 0;
2846 s.last_lit = s.matches = 0;
2847}
2848
2849
2850/* ===========================================================================
2851 * Flush the bit buffer and align the output on a byte boundary
2852 */
2853function bi_windup(s)
2854{
2855 if (s.bi_valid > 8) {
2856 put_short(s, s.bi_buf);
2857 } else if (s.bi_valid > 0) {
2858 //put_byte(s, (Byte)s->bi_buf);
2859 s.pending_buf[s.pending++] = s.bi_buf;
2860 }
2861 s.bi_buf = 0;
2862 s.bi_valid = 0;
2863}
2864
2865/* ===========================================================================
2866 * Copy a stored block, storing first the length and its
2867 * one's complement if requested.
2868 */
2869function copy_block(s, buf, len, header)
2870//DeflateState *s;
2871//charf *buf; /* the input data */
2872//unsigned len; /* its length */
2873//int header; /* true if block header must be written */
2874{
2875 bi_windup(s); /* align on byte boundary */
2876
2877 if (header) {
2878 put_short(s, len);
2879 put_short(s, ~len);
2880 }
2881// while (len--) {
2882// put_byte(s, *buf++);
2883// }
2884 utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
2885 s.pending += len;
2886}
2887
2888/* ===========================================================================
2889 * Compares to subtrees, using the tree depth as tie breaker when
2890 * the subtrees have equal frequency. This minimizes the worst case length.
2891 */
2892function smaller(tree, n, m, depth) {
2893 var _n2 = n * 2;
2894 var _m2 = m * 2;
2895 return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
2896 (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
2897}
2898
2899/* ===========================================================================
2900 * Restore the heap property by moving down the tree starting at node k,
2901 * exchanging a node with the smallest of its two sons if necessary, stopping
2902 * when the heap property is re-established (each father smaller than its
2903 * two sons).
2904 */
2905function pqdownheap(s, tree, k)
2906// deflate_state *s;
2907// ct_data *tree; /* the tree to restore */
2908// int k; /* node to move down */
2909{
2910 var v = s.heap[k];
2911 var j = k << 1; /* left son of k */
2912 while (j <= s.heap_len) {
2913 /* Set j to the smallest of the two sons: */
2914 if (j < s.heap_len &&
2915 smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
2916 j++;
2917 }
2918 /* Exit if v is smaller than both sons */
2919 if (smaller(tree, v, s.heap[j], s.depth)) { break; }
2920
2921 /* Exchange v with the smallest son */
2922 s.heap[k] = s.heap[j];
2923 k = j;
2924
2925 /* And continue down the tree, setting j to the left son of k */
2926 j <<= 1;
2927 }
2928 s.heap[k] = v;
2929}
2930
2931
2932// inlined manually
2933// var SMALLEST = 1;
2934
2935/* ===========================================================================
2936 * Send the block data compressed using the given Huffman trees
2937 */
2938function compress_block(s, ltree, dtree)
2939// deflate_state *s;
2940// const ct_data *ltree; /* literal tree */
2941// const ct_data *dtree; /* distance tree */
2942{
2943 var dist; /* distance of matched string */
2944 var lc; /* match length or unmatched char (if dist == 0) */
2945 var lx = 0; /* running index in l_buf */
2946 var code; /* the code to send */
2947 var extra; /* number of extra bits to send */
2948
2949 if (s.last_lit !== 0) {
2950 do {
2951 dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
2952 lc = s.pending_buf[s.l_buf + lx];
2953 lx++;
2954
2955 if (dist === 0) {
2956 send_code(s, lc, ltree); /* send a literal byte */
2957 //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
2958 } else {
2959 /* Here, lc is the match length - MIN_MATCH */
2960 code = _length_code[lc];
2961 send_code(s, code + LITERALS + 1, ltree); /* send the length code */
2962 extra = extra_lbits[code];
2963 if (extra !== 0) {
2964 lc -= base_length[code];
2965 send_bits(s, lc, extra); /* send the extra length bits */
2966 }
2967 dist--; /* dist is now the match distance - 1 */
2968 code = d_code(dist);
2969 //Assert (code < D_CODES, "bad d_code");
2970
2971 send_code(s, code, dtree); /* send the distance code */
2972 extra = extra_dbits[code];
2973 if (extra !== 0) {
2974 dist -= base_dist[code];
2975 send_bits(s, dist, extra); /* send the extra distance bits */
2976 }
2977 } /* literal or match pair ? */
2978
2979 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
2980 //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
2981 // "pendingBuf overflow");
2982
2983 } while (lx < s.last_lit);
2984 }
2985
2986 send_code(s, END_BLOCK, ltree);
2987}
2988
2989
2990/* ===========================================================================
2991 * Construct one Huffman tree and assigns the code bit strings and lengths.
2992 * Update the total bit length for the current block.
2993 * IN assertion: the field freq is set for all tree elements.
2994 * OUT assertions: the fields len and code are set to the optimal bit length
2995 * and corresponding code. The length opt_len is updated; static_len is
2996 * also updated if stree is not null. The field max_code is set.
2997 */
2998function build_tree(s, desc)
2999// deflate_state *s;
3000// tree_desc *desc; /* the tree descriptor */
3001{
3002 var tree = desc.dyn_tree;
3003 var stree = desc.stat_desc.static_tree;
3004 var has_stree = desc.stat_desc.has_stree;
3005 var elems = desc.stat_desc.elems;
3006 var n, m; /* iterate over heap elements */
3007 var max_code = -1; /* largest code with non zero frequency */
3008 var node; /* new node being created */
3009
3010 /* Construct the initial heap, with least frequent element in
3011 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
3012 * heap[0] is not used.
3013 */
3014 s.heap_len = 0;
3015 s.heap_max = HEAP_SIZE;
3016
3017 for (n = 0; n < elems; n++) {
3018 if (tree[n * 2]/*.Freq*/ !== 0) {
3019 s.heap[++s.heap_len] = max_code = n;
3020 s.depth[n] = 0;
3021
3022 } else {
3023 tree[n * 2 + 1]/*.Len*/ = 0;
3024 }
3025 }
3026
3027 /* The pkzip format requires that at least one distance code exists,
3028 * and that at least one bit should be sent even if there is only one
3029 * possible code. So to avoid special checks later on we force at least
3030 * two codes of non zero frequency.
3031 */
3032 while (s.heap_len < 2) {
3033 node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
3034 tree[node * 2]/*.Freq*/ = 1;
3035 s.depth[node] = 0;
3036 s.opt_len--;
3037
3038 if (has_stree) {
3039 s.static_len -= stree[node * 2 + 1]/*.Len*/;
3040 }
3041 /* node is 0 or 1 so it does not have extra bits */
3042 }
3043 desc.max_code = max_code;
3044
3045 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
3046 * establish sub-heaps of increasing lengths:
3047 */
3048 for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
3049
3050 /* Construct the Huffman tree by repeatedly combining the least two
3051 * frequent nodes.
3052 */
3053 node = elems; /* next internal node of the tree */
3054 do {
3055 //pqremove(s, tree, n); /* n = node of least frequency */
3056 /*** pqremove ***/
3057 n = s.heap[1/*SMALLEST*/];
3058 s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
3059 pqdownheap(s, tree, 1/*SMALLEST*/);
3060 /***/
3061
3062 m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
3063
3064 s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
3065 s.heap[--s.heap_max] = m;
3066
3067 /* Create a new node father of n and m */
3068 tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
3069 s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
3070 tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
3071
3072 /* and insert the new node in the heap */
3073 s.heap[1/*SMALLEST*/] = node++;
3074 pqdownheap(s, tree, 1/*SMALLEST*/);
3075
3076 } while (s.heap_len >= 2);
3077
3078 s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
3079
3080 /* At this point, the fields freq and dad are set. We can now
3081 * generate the bit lengths.
3082 */
3083 gen_bitlen(s, desc);
3084
3085 /* The field len is now set, we can generate the bit codes */
3086 gen_codes(tree, max_code, s.bl_count);
3087}
3088
3089
3090/* ===========================================================================
3091 * Scan a literal or distance tree to determine the frequencies of the codes
3092 * in the bit length tree.
3093 */
3094function scan_tree(s, tree, max_code)
3095// deflate_state *s;
3096// ct_data *tree; /* the tree to be scanned */
3097// int max_code; /* and its largest code of non zero frequency */
3098{
3099 var n; /* iterates over all tree elements */
3100 var prevlen = -1; /* last emitted length */
3101 var curlen; /* length of current code */
3102
3103 var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
3104
3105 var count = 0; /* repeat count of the current code */
3106 var max_count = 7; /* max repeat count */
3107 var min_count = 4; /* min repeat count */
3108
3109 if (nextlen === 0) {
3110 max_count = 138;
3111 min_count = 3;
3112 }
3113 tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
3114
3115 for (n = 0; n <= max_code; n++) {
3116 curlen = nextlen;
3117 nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
3118
3119 if (++count < max_count && curlen === nextlen) {
3120 continue;
3121
3122 } else if (count < min_count) {
3123 s.bl_tree[curlen * 2]/*.Freq*/ += count;
3124
3125 } else if (curlen !== 0) {
3126
3127 if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
3128 s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
3129
3130 } else if (count <= 10) {
3131 s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
3132
3133 } else {
3134 s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
3135 }
3136
3137 count = 0;
3138 prevlen = curlen;
3139
3140 if (nextlen === 0) {
3141 max_count = 138;
3142 min_count = 3;
3143
3144 } else if (curlen === nextlen) {
3145 max_count = 6;
3146 min_count = 3;
3147
3148 } else {
3149 max_count = 7;
3150 min_count = 4;
3151 }
3152 }
3153}
3154
3155
3156/* ===========================================================================
3157 * Send a literal or distance tree in compressed form, using the codes in
3158 * bl_tree.
3159 */
3160function send_tree(s, tree, max_code)
3161// deflate_state *s;
3162// ct_data *tree; /* the tree to be scanned */
3163// int max_code; /* and its largest code of non zero frequency */
3164{
3165 var n; /* iterates over all tree elements */
3166 var prevlen = -1; /* last emitted length */
3167 var curlen; /* length of current code */
3168
3169 var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
3170
3171 var count = 0; /* repeat count of the current code */
3172 var max_count = 7; /* max repeat count */
3173 var min_count = 4; /* min repeat count */
3174
3175 /* tree[max_code+1].Len = -1; */ /* guard already set */
3176 if (nextlen === 0) {
3177 max_count = 138;
3178 min_count = 3;
3179 }
3180
3181 for (n = 0; n <= max_code; n++) {
3182 curlen = nextlen;
3183 nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
3184
3185 if (++count < max_count && curlen === nextlen) {
3186 continue;
3187
3188 } else if (count < min_count) {
3189 do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
3190
3191 } else if (curlen !== 0) {
3192 if (curlen !== prevlen) {
3193 send_code(s, curlen, s.bl_tree);
3194 count--;
3195 }
3196 //Assert(count >= 3 && count <= 6, " 3_6?");
3197 send_code(s, REP_3_6, s.bl_tree);
3198 send_bits(s, count - 3, 2);
3199
3200 } else if (count <= 10) {
3201 send_code(s, REPZ_3_10, s.bl_tree);
3202 send_bits(s, count - 3, 3);
3203
3204 } else {
3205 send_code(s, REPZ_11_138, s.bl_tree);
3206 send_bits(s, count - 11, 7);
3207 }
3208
3209 count = 0;
3210 prevlen = curlen;
3211 if (nextlen === 0) {
3212 max_count = 138;
3213 min_count = 3;
3214
3215 } else if (curlen === nextlen) {
3216 max_count = 6;
3217 min_count = 3;
3218
3219 } else {
3220 max_count = 7;
3221 min_count = 4;
3222 }
3223 }
3224}
3225
3226
3227/* ===========================================================================
3228 * Construct the Huffman tree for the bit lengths and return the index in
3229 * bl_order of the last bit length code to send.
3230 */
3231function build_bl_tree(s) {
3232 var max_blindex; /* index of last bit length code of non zero freq */
3233
3234 /* Determine the bit length frequencies for literal and distance trees */
3235 scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
3236 scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
3237
3238 /* Build the bit length tree: */
3239 build_tree(s, s.bl_desc);
3240 /* opt_len now includes the length of the tree representations, except
3241 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
3242 */
3243
3244 /* Determine the number of bit length codes to send. The pkzip format
3245 * requires that at least 4 bit length codes be sent. (appnote.txt says
3246 * 3 but the actual value used is 4.)
3247 */
3248 for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
3249 if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
3250 break;
3251 }
3252 }
3253 /* Update opt_len to include the bit length tree and counts */
3254 s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
3255 //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
3256 // s->opt_len, s->static_len));
3257
3258 return max_blindex;
3259}
3260
3261
3262/* ===========================================================================
3263 * Send the header for a block using dynamic Huffman trees: the counts, the
3264 * lengths of the bit length codes, the literal tree and the distance tree.
3265 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
3266 */
3267function send_all_trees(s, lcodes, dcodes, blcodes)
3268// deflate_state *s;
3269// int lcodes, dcodes, blcodes; /* number of codes for each tree */
3270{
3271 var rank; /* index in bl_order */
3272
3273 //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
3274 //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
3275 // "too many codes");
3276 //Tracev((stderr, "\nbl counts: "));
3277 send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
3278 send_bits(s, dcodes - 1, 5);
3279 send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
3280 for (rank = 0; rank < blcodes; rank++) {
3281 //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
3282 send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
3283 }
3284 //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
3285
3286 send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
3287 //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
3288
3289 send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
3290 //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
3291}
3292
3293
3294/* ===========================================================================
3295 * Check if the data type is TEXT or BINARY, using the following algorithm:
3296 * - TEXT if the two conditions below are satisfied:
3297 * a) There are no non-portable control characters belonging to the
3298 * "black list" (0..6, 14..25, 28..31).
3299 * b) There is at least one printable character belonging to the
3300 * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
3301 * - BINARY otherwise.
3302 * - The following partially-portable control characters form a
3303 * "gray list" that is ignored in this detection algorithm:
3304 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
3305 * IN assertion: the fields Freq of dyn_ltree are set.
3306 */
3307function detect_data_type(s) {
3308 /* black_mask is the bit mask of black-listed bytes
3309 * set bits 0..6, 14..25, and 28..31
3310 * 0xf3ffc07f = binary 11110011111111111100000001111111
3311 */
3312 var black_mask = 0xf3ffc07f;
3313 var n;
3314
3315 /* Check for non-textual ("black-listed") bytes. */
3316 for (n = 0; n <= 31; n++, black_mask >>>= 1) {
3317 if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
3318 return Z_BINARY;
3319 }
3320 }
3321
3322 /* Check for textual ("white-listed") bytes. */
3323 if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
3324 s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
3325 return Z_TEXT;
3326 }
3327 for (n = 32; n < LITERALS; n++) {
3328 if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
3329 return Z_TEXT;
3330 }
3331 }
3332
3333 /* There are no "black-listed" or "white-listed" bytes:
3334 * this stream either is empty or has tolerated ("gray-listed") bytes only.
3335 */
3336 return Z_BINARY;
3337}
3338
3339
3340var static_init_done = false;
3341
3342/* ===========================================================================
3343 * Initialize the tree data structures for a new zlib stream.
3344 */
3345function _tr_init(s)
3346{
3347
3348 if (!static_init_done) {
3349 tr_static_init();
3350 static_init_done = true;
3351 }
3352
3353 s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
3354 s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
3355 s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
3356
3357 s.bi_buf = 0;
3358 s.bi_valid = 0;
3359
3360 /* Initialize the first block of the first file: */
3361 init_block(s);
3362}
3363
3364
3365/* ===========================================================================
3366 * Send a stored block
3367 */
3368function _tr_stored_block(s, buf, stored_len, last)
3369//DeflateState *s;
3370//charf *buf; /* input block */
3371//ulg stored_len; /* length of input block */
3372//int last; /* one if this is the last block for a file */
3373{
3374 send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
3375 copy_block(s, buf, stored_len, true); /* with header */
3376}
3377
3378
3379/* ===========================================================================
3380 * Send one empty static block to give enough lookahead for inflate.
3381 * This takes 10 bits, of which 7 may remain in the bit buffer.
3382 */
3383function _tr_align(s) {
3384 send_bits(s, STATIC_TREES << 1, 3);
3385 send_code(s, END_BLOCK, static_ltree);
3386 bi_flush(s);
3387}
3388
3389
3390/* ===========================================================================
3391 * Determine the best encoding for the current block: dynamic trees, static
3392 * trees or store, and output the encoded block to the zip file.
3393 */
3394function _tr_flush_block(s, buf, stored_len, last)
3395//DeflateState *s;
3396//charf *buf; /* input block, or NULL if too old */
3397//ulg stored_len; /* length of input block */
3398//int last; /* one if this is the last block for a file */
3399{
3400 var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
3401 var max_blindex = 0; /* index of last bit length code of non zero freq */
3402
3403 /* Build the Huffman trees unless a stored block is forced */
3404 if (s.level > 0) {
3405
3406 /* Check if the file is binary or text */
3407 if (s.strm.data_type === Z_UNKNOWN) {
3408 s.strm.data_type = detect_data_type(s);
3409 }
3410
3411 /* Construct the literal and distance trees */
3412 build_tree(s, s.l_desc);
3413 // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
3414 // s->static_len));
3415
3416 build_tree(s, s.d_desc);
3417 // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
3418 // s->static_len));
3419 /* At this point, opt_len and static_len are the total bit lengths of
3420 * the compressed block data, excluding the tree representations.
3421 */
3422
3423 /* Build the bit length tree for the above two trees, and get the index
3424 * in bl_order of the last bit length code to send.
3425 */
3426 max_blindex = build_bl_tree(s);
3427
3428 /* Determine the best encoding. Compute the block lengths in bytes. */
3429 opt_lenb = (s.opt_len + 3 + 7) >>> 3;
3430 static_lenb = (s.static_len + 3 + 7) >>> 3;
3431
3432 // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
3433 // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
3434 // s->last_lit));
3435
3436 if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
3437
3438 } else {
3439 // Assert(buf != (char*)0, "lost buf");
3440 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
3441 }
3442
3443 if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
3444 /* 4: two words for the lengths */
3445
3446 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
3447 * Otherwise we can't have processed more than WSIZE input bytes since
3448 * the last block flush, because compression would have been
3449 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
3450 * transform a block into a stored block.
3451 */
3452 _tr_stored_block(s, buf, stored_len, last);
3453
3454 } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
3455
3456 send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
3457 compress_block(s, static_ltree, static_dtree);
3458
3459 } else {
3460 send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
3461 send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
3462 compress_block(s, s.dyn_ltree, s.dyn_dtree);
3463 }
3464 // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
3465 /* The above check is made mod 2^32, for files larger than 512 MB
3466 * and uLong implemented on 32 bits.
3467 */
3468 init_block(s);
3469
3470 if (last) {
3471 bi_windup(s);
3472 }
3473 // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
3474 // s->compressed_len-7*last));
3475}
3476
3477/* ===========================================================================
3478 * Save the match info and tally the frequency counts. Return true if
3479 * the current block must be flushed.
3480 */
3481function _tr_tally(s, dist, lc)
3482// deflate_state *s;
3483// unsigned dist; /* distance of matched string */
3484// unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3485{
3486 //var out_length, in_length, dcode;
3487
3488 s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
3489 s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
3490
3491 s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
3492 s.last_lit++;
3493
3494 if (dist === 0) {
3495 /* lc is the unmatched char */
3496 s.dyn_ltree[lc * 2]/*.Freq*/++;
3497 } else {
3498 s.matches++;
3499 /* Here, lc is the match length - MIN_MATCH */
3500 dist--; /* dist = match distance - 1 */
3501 //Assert((ush)dist < (ush)MAX_DIST(s) &&
3502 // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
3503 // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
3504
3505 s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
3506 s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
3507 }
3508
3509// (!) This block is disabled in zlib defaults,
3510// don't enable it for binary compatibility
3511
3512//#ifdef TRUNCATE_BLOCK
3513// /* Try to guess if it is profitable to stop the current block here */
3514// if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
3515// /* Compute an upper bound for the compressed length */
3516// out_length = s.last_lit*8;
3517// in_length = s.strstart - s.block_start;
3518//
3519// for (dcode = 0; dcode < D_CODES; dcode++) {
3520// out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
3521// }
3522// out_length >>>= 3;
3523// //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
3524// // s->last_lit, in_length, out_length,
3525// // 100L - out_length*100L/in_length));
3526// if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
3527// return true;
3528// }
3529// }
3530//#endif
3531
3532 return (s.last_lit === s.lit_bufsize - 1);
3533 /* We avoid equality with lit_bufsize because of wraparound at 64K
3534 * on 16 bit machines and because stored blocks are restricted to
3535 * 64K-1 bytes.
3536 */
3537}
3538
3539exports._tr_init = _tr_init;
3540exports._tr_stored_block = _tr_stored_block;
3541exports._tr_flush_block = _tr_flush_block;
3542exports._tr_tally = _tr_tally;
3543exports._tr_align = _tr_align;
3544
3545},{"../utils/common":1}],8:[function(require,module,exports){
3546'use strict';
3547
3548// (C) 1995-2013 Jean-loup Gailly and Mark Adler
3549// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
3550//
3551// This software is provided 'as-is', without any express or implied
3552// warranty. In no event will the authors be held liable for any damages
3553// arising from the use of this software.
3554//
3555// Permission is granted to anyone to use this software for any purpose,
3556// including commercial applications, and to alter it and redistribute it
3557// freely, subject to the following restrictions:
3558//
3559// 1. The origin of this software must not be misrepresented; you must not
3560// claim that you wrote the original software. If you use this software
3561// in a product, an acknowledgment in the product documentation would be
3562// appreciated but is not required.
3563// 2. Altered source versions must be plainly marked as such, and must not be
3564// misrepresented as being the original software.
3565// 3. This notice may not be removed or altered from any source distribution.
3566
3567function ZStream() {
3568 /* next input byte */
3569 this.input = null; // JS specific, because we have no pointers
3570 this.next_in = 0;
3571 /* number of bytes available at input */
3572 this.avail_in = 0;
3573 /* total number of input bytes read so far */
3574 this.total_in = 0;
3575 /* next output byte should be put there */
3576 this.output = null; // JS specific, because we have no pointers
3577 this.next_out = 0;
3578 /* remaining free space at output */
3579 this.avail_out = 0;
3580 /* total number of bytes output so far */
3581 this.total_out = 0;
3582 /* last error message, NULL if no error */
3583 this.msg = ''/*Z_NULL*/;
3584 /* not visible by applications */
3585 this.state = null;
3586 /* best guess about the data type: binary or text */
3587 this.data_type = 2/*Z_UNKNOWN*/;
3588 /* adler32 value of the uncompressed data */
3589 this.adler = 0;
3590}
3591
3592module.exports = ZStream;
3593
3594},{}],"/lib/deflate.js":[function(require,module,exports){
3595'use strict';
3596
3597
3598var zlib_deflate = require('./zlib/deflate');
3599var utils = require('./utils/common');
3600var strings = require('./utils/strings');
3601var msg = require('./zlib/messages');
3602var ZStream = require('./zlib/zstream');
3603
3604var toString = Object.prototype.toString;
3605
3606/* Public constants ==========================================================*/
3607/* ===========================================================================*/
3608
3609var Z_NO_FLUSH = 0;
3610var Z_FINISH = 4;
3611
3612var Z_OK = 0;
3613var Z_STREAM_END = 1;
3614var Z_SYNC_FLUSH = 2;
3615
3616var Z_DEFAULT_COMPRESSION = -1;
3617
3618var Z_DEFAULT_STRATEGY = 0;
3619
3620var Z_DEFLATED = 8;
3621
3622/* ===========================================================================*/
3623
3624
3625/**
3626 * class Deflate
3627 *
3628 * Generic JS-style wrapper for zlib calls. If you don't need
3629 * streaming behaviour - use more simple functions: [[deflate]],
3630 * [[deflateRaw]] and [[gzip]].
3631 **/
3632
3633/* internal
3634 * Deflate.chunks -> Array
3635 *
3636 * Chunks of output data, if [[Deflate#onData]] not overridden.
3637 **/
3638
3639/**
3640 * Deflate.result -> Uint8Array|Array
3641 *
3642 * Compressed result, generated by default [[Deflate#onData]]
3643 * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
3644 * (call [[Deflate#push]] with `Z_FINISH` / `true` param) or if you
3645 * push a chunk with explicit flush (call [[Deflate#push]] with
3646 * `Z_SYNC_FLUSH` param).
3647 **/
3648
3649/**
3650 * Deflate.err -> Number
3651 *
3652 * Error code after deflate finished. 0 (Z_OK) on success.
3653 * You will not need it in real life, because deflate errors
3654 * are possible only on wrong options or bad `onData` / `onEnd`
3655 * custom handlers.
3656 **/
3657
3658/**
3659 * Deflate.msg -> String
3660 *
3661 * Error message, if [[Deflate.err]] != 0
3662 **/
3663
3664
3665/**
3666 * new Deflate(options)
3667 * - options (Object): zlib deflate options.
3668 *
3669 * Creates new deflator instance with specified params. Throws exception
3670 * on bad params. Supported options:
3671 *
3672 * - `level`
3673 * - `windowBits`
3674 * - `memLevel`
3675 * - `strategy`
3676 * - `dictionary`
3677 *
3678 * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
3679 * for more information on these.
3680 *
3681 * Additional options, for internal needs:
3682 *
3683 * - `chunkSize` - size of generated data chunks (16K by default)
3684 * - `raw` (Boolean) - do raw deflate
3685 * - `gzip` (Boolean) - create gzip wrapper
3686 * - `to` (String) - if equal to 'string', then result will be "binary string"
3687 * (each char code [0..255])
3688 * - `header` (Object) - custom header for gzip
3689 * - `text` (Boolean) - true if compressed data believed to be text
3690 * - `time` (Number) - modification time, unix timestamp
3691 * - `os` (Number) - operation system code
3692 * - `extra` (Array) - array of bytes with extra data (max 65536)
3693 * - `name` (String) - file name (binary string)
3694 * - `comment` (String) - comment (binary string)
3695 * - `hcrc` (Boolean) - true if header crc should be added
3696 *
3697 * ##### Example:
3698 *
3699 * ```javascript
3700 * var pako = require('pako')
3701 * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
3702 * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
3703 *
3704 * var deflate = new pako.Deflate({ level: 3});
3705 *
3706 * deflate.push(chunk1, false);
3707 * deflate.push(chunk2, true); // true -> last chunk
3708 *
3709 * if (deflate.err) { throw new Error(deflate.err); }
3710 *
3711 * console.log(deflate.result);
3712 * ```
3713 **/
3714function Deflate(options) {
3715 if (!(this instanceof Deflate)) return new Deflate(options);
3716
3717 this.options = utils.assign({
3718 level: Z_DEFAULT_COMPRESSION,
3719 method: Z_DEFLATED,
3720 chunkSize: 16384,
3721 windowBits: 15,
3722 memLevel: 8,
3723 strategy: Z_DEFAULT_STRATEGY,
3724 to: ''
3725 }, options || {});
3726
3727 var opt = this.options;
3728
3729 if (opt.raw && (opt.windowBits > 0)) {
3730 opt.windowBits = -opt.windowBits;
3731 }
3732
3733 else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
3734 opt.windowBits += 16;
3735 }
3736
3737 this.err = 0; // error code, if happens (0 = Z_OK)
3738 this.msg = ''; // error message
3739 this.ended = false; // used to avoid multiple onEnd() calls
3740 this.chunks = []; // chunks of compressed data
3741
3742 this.strm = new ZStream();
3743 this.strm.avail_out = 0;
3744
3745 var status = zlib_deflate.deflateInit2(
3746 this.strm,
3747 opt.level,
3748 opt.method,
3749 opt.windowBits,
3750 opt.memLevel,
3751 opt.strategy
3752 );
3753
3754 if (status !== Z_OK) {
3755 throw new Error(msg[status]);
3756 }
3757
3758 if (opt.header) {
3759 zlib_deflate.deflateSetHeader(this.strm, opt.header);
3760 }
3761
3762 if (opt.dictionary) {
3763 var dict;
3764 // Convert data if needed
3765 if (typeof opt.dictionary === 'string') {
3766 // If we need to compress text, change encoding to utf8.
3767 dict = strings.string2buf(opt.dictionary);
3768 } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
3769 dict = new Uint8Array(opt.dictionary);
3770 } else {
3771 dict = opt.dictionary;
3772 }
3773
3774 status = zlib_deflate.deflateSetDictionary(this.strm, dict);
3775
3776 if (status !== Z_OK) {
3777 throw new Error(msg[status]);
3778 }
3779
3780 this._dict_set = true;
3781 }
3782}
3783
3784/**
3785 * Deflate#push(data[, mode]) -> Boolean
3786 * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be
3787 * converted to utf8 byte sequence.
3788 * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
3789 * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
3790 *
3791 * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
3792 * new compressed chunks. Returns `true` on success. The last data block must have
3793 * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
3794 * [[Deflate#onEnd]]. For interim explicit flushes (without ending the stream) you
3795 * can use mode Z_SYNC_FLUSH, keeping the compression context.
3796 *
3797 * On fail call [[Deflate#onEnd]] with error code and return false.
3798 *
3799 * We strongly recommend to use `Uint8Array` on input for best speed (output
3800 * array format is detected automatically). Also, don't skip last param and always
3801 * use the same type in your code (boolean or number). That will improve JS speed.
3802 *
3803 * For regular `Array`-s make sure all elements are [0..255].
3804 *
3805 * ##### Example
3806 *
3807 * ```javascript
3808 * push(chunk, false); // push one of data chunks
3809 * ...
3810 * push(chunk, true); // push last chunk
3811 * ```
3812 **/
3813Deflate.prototype.push = function (data, mode) {
3814 var strm = this.strm;
3815 var chunkSize = this.options.chunkSize;
3816 var status, _mode;
3817
3818 if (this.ended) { return false; }
3819
3820 _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
3821
3822 // Convert data if needed
3823 if (typeof data === 'string') {
3824 // If we need to compress text, change encoding to utf8.
3825 strm.input = strings.string2buf(data);
3826 } else if (toString.call(data) === '[object ArrayBuffer]') {
3827 strm.input = new Uint8Array(data);
3828 } else {
3829 strm.input = data;
3830 }
3831
3832 strm.next_in = 0;
3833 strm.avail_in = strm.input.length;
3834
3835 do {
3836 if (strm.avail_out === 0) {
3837 strm.output = new utils.Buf8(chunkSize);
3838 strm.next_out = 0;
3839 strm.avail_out = chunkSize;
3840 }
3841 status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
3842
3843 if (status !== Z_STREAM_END && status !== Z_OK) {
3844 this.onEnd(status);
3845 this.ended = true;
3846 return false;
3847 }
3848 if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
3849 if (this.options.to === 'string') {
3850 this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
3851 } else {
3852 this.onData(utils.shrinkBuf(strm.output, strm.next_out));
3853 }
3854 }
3855 } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
3856
3857 // Finalize on the last chunk.
3858 if (_mode === Z_FINISH) {
3859 status = zlib_deflate.deflateEnd(this.strm);
3860 this.onEnd(status);
3861 this.ended = true;
3862 return status === Z_OK;
3863 }
3864
3865 // callback interim results if Z_SYNC_FLUSH.
3866 if (_mode === Z_SYNC_FLUSH) {
3867 this.onEnd(Z_OK);
3868 strm.avail_out = 0;
3869 return true;
3870 }
3871
3872 return true;
3873};
3874
3875
3876/**
3877 * Deflate#onData(chunk) -> Void
3878 * - chunk (Uint8Array|Array|String): output data. Type of array depends
3879 * on js engine support. When string output requested, each chunk
3880 * will be string.
3881 *
3882 * By default, stores data blocks in `chunks[]` property and glue
3883 * those in `onEnd`. Override this handler, if you need another behaviour.
3884 **/
3885Deflate.prototype.onData = function (chunk) {
3886 this.chunks.push(chunk);
3887};
3888
3889
3890/**
3891 * Deflate#onEnd(status) -> Void
3892 * - status (Number): deflate status. 0 (Z_OK) on success,
3893 * other if not.
3894 *
3895 * Called once after you tell deflate that the input stream is
3896 * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
3897 * or if an error happened. By default - join collected chunks,
3898 * free memory and fill `results` / `err` properties.
3899 **/
3900Deflate.prototype.onEnd = function (status) {
3901 // On success - join
3902 if (status === Z_OK) {
3903 if (this.options.to === 'string') {
3904 this.result = this.chunks.join('');
3905 } else {
3906 this.result = utils.flattenChunks(this.chunks);
3907 }
3908 }
3909 this.chunks = [];
3910 this.err = status;
3911 this.msg = this.strm.msg;
3912};
3913
3914
3915/**
3916 * deflate(data[, options]) -> Uint8Array|Array|String
3917 * - data (Uint8Array|Array|String): input data to compress.
3918 * - options (Object): zlib deflate options.
3919 *
3920 * Compress `data` with deflate algorithm and `options`.
3921 *
3922 * Supported options are:
3923 *
3924 * - level
3925 * - windowBits
3926 * - memLevel
3927 * - strategy
3928 * - dictionary
3929 *
3930 * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
3931 * for more information on these.
3932 *
3933 * Sugar (options):
3934 *
3935 * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
3936 * negative windowBits implicitly.
3937 * - `to` (String) - if equal to 'string', then result will be "binary string"
3938 * (each char code [0..255])
3939 *
3940 * ##### Example:
3941 *
3942 * ```javascript
3943 * var pako = require('pako')
3944 * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
3945 *
3946 * console.log(pako.deflate(data));
3947 * ```
3948 **/
3949function deflate(input, options) {
3950 var deflator = new Deflate(options);
3951
3952 deflator.push(input, true);
3953
3954 // That will never happens, if you don't cheat with options :)
3955 if (deflator.err) { throw deflator.msg || msg[deflator.err]; }
3956
3957 return deflator.result;
3958}
3959
3960
3961/**
3962 * deflateRaw(data[, options]) -> Uint8Array|Array|String
3963 * - data (Uint8Array|Array|String): input data to compress.
3964 * - options (Object): zlib deflate options.
3965 *
3966 * The same as [[deflate]], but creates raw data, without wrapper
3967 * (header and adler32 crc).
3968 **/
3969function deflateRaw(input, options) {
3970 options = options || {};
3971 options.raw = true;
3972 return deflate(input, options);
3973}
3974
3975
3976/**
3977 * gzip(data[, options]) -> Uint8Array|Array|String
3978 * - data (Uint8Array|Array|String): input data to compress.
3979 * - options (Object): zlib deflate options.
3980 *
3981 * The same as [[deflate]], but create gzip wrapper instead of
3982 * deflate one.
3983 **/
3984function gzip(input, options) {
3985 options = options || {};
3986 options.gzip = true;
3987 return deflate(input, options);
3988}
3989
3990
3991exports.Deflate = Deflate;
3992exports.deflate = deflate;
3993exports.deflateRaw = deflateRaw;
3994exports.gzip = gzip;
3995
3996},{"./utils/common":1,"./utils/strings":2,"./zlib/deflate":5,"./zlib/messages":6,"./zlib/zstream":8}]},{},[])("/lib/deflate.js")
3997});
Note: See TracBrowser for help on using the repository browser.