{“ast”:null,“code”:“'use strict'; // (C) 1995-2013 Jean-loup Gailly and Mark Adlern// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsinn//n// This software is provided 'as-is', without any express or impliedn// warranty. In no event will the authors be held liable for any damagesn// arising from the use of this software.n//n// Permission is granted to anyone to use this software for any purpose,n// including commercial applications, and to alter it and redistribute itn// freely, subject to the following restrictions:n//n// 1. The origin of this software must not be misrepresented; you must notn// claim that you wrote the original software. If you use this softwaren// in a product, an acknowledgment in the product documentation would ben// appreciated but is not required.n// 2. Altered source versions must be plainly marked as such, and must not ben// misrepresented as being the original software.n// 3. This notice may not be removed or altered from any source distribution.nn/* eslint-disable space-unary-ops */nnvar utils = require('../utils/common');n/* Public constants ==========================================================*/nn/* ===========================================================================*/n//var Z_FILTERED = 1;n//var Z_HUFFMAN_ONLY = 2;n//var Z_RLE = 3;nnnvar Z_FIXED = 4; //var Z_DEFAULT_STRATEGY = 0;nn/* Possible values of the data_type field (though see inflate()) */nnvar Z_BINARY = 0;nvar Z_TEXT = 1; //var Z_ASCII = 1; // = Z_TEXTnnvar Z_UNKNOWN = 2;n/*============================================================================*/nnfunction zero(buf) {n var len = buf.length;nn while (–len >= 0) {n buf = 0;n }n} // From zutil.hnnnvar STORED_BLOCK = 0;nvar STATIC_TREES = 1;nvar DYN_TREES = 2;n/* The three kinds of block type */nnvar MIN_MATCH = 3;nvar MAX_MATCH = 258;n/* The minimum and maximum match lengths */n// From deflate.hnn/* ===========================================================================n * Internal compression state.n */nnvar LENGTH_CODES = 29;n/* number of length codes, not counting the special END_BLOCK code */nnvar LITERALS = 256;n/* number of literal bytes 0..255 */nnvar L_CODES = LITERALS + 1 + LENGTH_CODES;n/* number of Literal or Length codes, including the END_BLOCK code */nnvar D_CODES = 30;n/* number of distance codes */nnvar BL_CODES = 19;n/* number of codes used to transfer the bit lengths */nnvar HEAP_SIZE = 2 * L_CODES + 1;n/* maximum heap size */nnvar MAX_BITS = 15;n/* All codes must not exceed MAX_BITS bits */nnvar Buf_size = 16;n/* size of bit buffer in bi_buf */nn/* ===========================================================================n * Constantsn */nnvar MAX_BL_BITS = 7;n/* Bit length codes must not exceed MAX_BL_BITS bits */nnvar END_BLOCK = 256;n/* end of block literal code */nnvar REP_3_6 = 16;n/* repeat previous bit length 3-6 times (2 bits of repeat count) */nnvar REPZ_3_10 = 17;n/* repeat a zero length 3-10 times (3 bits of repeat count) */nnvar REPZ_11_138 = 18;n/* repeat a zero length 11-138 times (7 bits of repeat count) */nn/* eslint-disable comma-spacing,array-bracket-spacing */nnvar extra_lbits =n/* extra bits for each length code */n[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];nvar extra_dbits =n/* extra bits for each distance code */n[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];nvar extra_blbits =n/* extra bits for each bit length code */n[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7];nvar bl_order = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];n/* eslint-enable comma-spacing,array-bracket-spacing */nn/* The lengths of the bit length codes are sent in order of decreasingn * probability, to avoid transmitting the lengths for unused bit length codes.n */nn/* ===========================================================================n * Local data. These are initialized only once.n */n// We pre-fill arrays with 0 to avoid uninitialized gapsnnvar DIST_CODE_LEN = 512;n/* see definition of array dist_code below */n// !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1nnvar static_ltree = new Array((L_CODES + 2) * 2);nzero(static_ltree);n/* The static literal tree. Since the bit lengths are imposed, there is non * need for the L_CODES extra codes used during heap construction. Howevern * The codes 286 and 287 are needed to build a canonical tree (see _tr_initn * below).n */nnvar static_dtree = new Array(D_CODES * 2);nzero(static_dtree);n/* The static distance tree. (Actually a trivial tree since all codes usen * 5 bits.)n */nnvar _dist_code = new Array(DIST_CODE_LEN);nnzero(_dist_code);n/* Distance codes. The first 256 values correspond to the distancesn * 3 .. 258, the last 256 values correspond to the top 8 bits ofn * the 15 bit distances.n */nnvar _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);nnzero(_length_code);n/* length code for each normalized match length (0 == MIN_MATCH) */nnvar base_length = new Array(LENGTH_CODES);nzero(base_length);n/* First normalized length for each code (0 = MIN_MATCH) */nnvar base_dist = new Array(D_CODES);nzero(base_dist);n/* First normalized distance for each code (0 = distance of 1) */nnfunction StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {n this.static_tree = static_tree;n /* static tree or NULL */nn this.extra_bits = extra_bits;n /* extra bits for each code or NULL */nn this.extra_base = extra_base;n /* base index for extra_bits */nn this.elems = elems;n /* max number of elements in the tree */nn this.max_length = max_length;n /* max bit length for the codes */n // show if `static_tree` has data or dummy - needed for monomorphic objectsnn this.has_stree = static_tree && static_tree.length;n}nnvar static_l_desc;nvar static_d_desc;nvar static_bl_desc;nnfunction TreeDesc(dyn_tree, stat_desc) {n this.dyn_tree = dyn_tree;n /* the dynamic tree */nn this.max_code = 0;n /* largest code with non zero frequency */nn this.stat_desc = stat_desc;n /* the corresponding static tree */n}nnfunction d_code(dist) {n return dist < 256 ? _dist_code : _dist_code[256 + (dist >>> 7)];n}n/* ===========================================================================n * Output a short LSB first on the stream.n * IN assertion: there is enough room in pendingBuf.n */nnnfunction put_short(s, w) {n // put_byte(s, (uch)((w) & 0xff));n // put_byte(s, (uch)((ush)(w) >> 8));n s.pending_buf = w & 0xff;n s.pending_buf = w >>> 8 & 0xff;n}n/* ===========================================================================n * Send a value on a given number of bits.n * IN assertion: length <= 16 and value fits in length bits.n */nnnfunction send_bits(s, value, length) {n if (s.bi_valid > Buf_size - length) {n s.bi_buf |= value << s.bi_valid & 0xffff;n put_short(s, s.bi_buf);n s.bi_buf = value >> Buf_size - s.bi_valid;n s.bi_valid += length - Buf_size;n } else {n s.bi_buf |= value << s.bi_valid & 0xffff;n s.bi_valid += length;n }n}nnfunction send_code(s, c, tree) {n send_bits(s, tree[c * 2]n /.Code/n , tree[c * 2 + 1]n /.Len/n );n}n/* ===========================================================================n * Reverse the first len bits of a code, using straightforward code (a fastern * method would use a table)n * IN assertion: 1 <= len <= 15n */nnnfunction bi_reverse(code, len) {n var res = 0;nn do {n res |= code & 1;n code >>>= 1;n res <<= 1;n } while (–len > 0);nn return res >>> 1;n}n/* ===========================================================================n * Flush the bit buffer, keeping at most 7 bits in it.n */nnnfunction bi_flush(s) {n if (s.bi_valid === 16) {n put_short(s, s.bi_buf);n s.bi_buf = 0;n s.bi_valid = 0;n } else if (s.bi_valid >= 8) {n s.pending_buf = s.bi_buf & 0xff;n s.bi_buf >>= 8;n s.bi_valid -= 8;n }n}n/* ===========================================================================n * Compute the optimal bit lengths for a tree and update the total bit lengthn * for the current block.n * IN assertion: the fields freq and dad are set, heap andn * above are the tree nodes sorted by increasing frequency.n * OUT assertions: the field len is set to the optimal bit length, then * array bl_count contains the frequencies for each bit length.n * The length opt_len is updated; static_len is also updated if stree isn * not null.n */nnnfunction gen_bitlen(s, desc) // deflate_state *s;n// tree_desc *desc; /* the tree descriptor */n{n var tree = desc.dyn_tree;n var max_code = desc.max_code;n var stree = desc.stat_desc.static_tree;n var has_stree = desc.stat_desc.has_stree;n var extra = desc.stat_desc.extra_bits;n var base = desc.stat_desc.extra_base;n var max_length = desc.stat_desc.max_length;n var h;n /* heap index */nn var n, m;n /* iterate over the tree elements */nn var bits;n /* bit length */nn var xbits;n /* extra bits */nn var f;n /* frequency */nn var overflow = 0;n /* number of elements with bit length too large */nn for (bits = 0; bits <= MAX_BITS; bits++) {n s.bl_count = 0;n }n /* In a first pass, compute the optimal bit lengths (which mayn * overflow in the case of the bit length tree).n */nnn tree[s.heap * 2 + 1]n /.Len/n = 0;n /* root of the heap */nn for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {n n = s.heap;n bits = tree[tree[n * 2 + 1]n /.Dad/n * 2 + 1]n /.Len/n + 1;nn if (bits > max_length) {n bits = max_length;n overflow++;n }nn tree[n * 2 + 1]n /.Len/n = bits;n /* We overwrite tree.Dad which is no longer needed */nn if (n > max_code) {n continue;n }n /* not a leaf node */nnn s.bl_count++;n xbits = 0;nn if (n >= base) {n xbits = extra[n - base];n }nn f = tree[n * 2]n /.Freq/n ;n s.opt_len += f * (bits + xbits);nn if (has_stree) {n s.static_len += f * (stree[n * 2 + 1]n /.Len/n + xbits);n }n }nn if (overflow === 0) {n return;n } // Trace((stderr,"\nbit length overflow\n"));nn /* This happens for example on obj2 and pic of the Calgary corpus */nn /* Find the first bit length which could increase: */nnn do {n bits = max_length - 1;nn while (s.bl_count === 0) {n bits–;n }nn s.bl_count–;n /* move one leaf down the tree */nn s.bl_count[bits + 1] += 2;n /* move one overflow item as its brother */nn s.bl_count–;n /* The brother of the overflow item also moves one step up,n * but this does not affect bl_countn */nn overflow -= 2;n } while (overflow > 0);n /* Now recompute all bit lengths, scanning in increasing frequency.n * h is still equal to HEAP_SIZE. (It is simpler to reconstruct alln * lengths instead of fixing only the wrong ones. This idea is takenn * from 'ar' written by Haruhiko Okumura.)n */nnn for (bits = max_length; bits !== 0; bits–) {n n = s.bl_count;nn while (n !== 0) {n m = s.heap;nn if (m > max_code) {n continue;n }nn if (tree[m * 2 + 1]n /.Len/n !== bits) {n // Trace((stderr,"code %d bits %d->%d\n", m, tree.Len, bits));n s.opt_len += (bits - tree[m * 2 + 1]n /.Len/n ) * tree[m * 2]n /.Freq/n ;n tree[m * 2 + 1]n /.Len/n = bits;n }nn n–;n }n }n}n/* ===========================================================================n * Generate the codes for a given tree and bit counts (which need not ben * optimal).n * IN assertion: the array bl_count contains the bit length statistics forn * the given tree and the field len is set for all tree elements.n * OUT assertion: the field code is set for all tree elements of nonn * zero code length.n */nnnfunction gen_codes(tree, max_code, bl_count) // ct_data *tree; /* the tree to decorate */n// int max_code; /* largest code with non zero frequency */n// ushf *bl_count; /* number of codes at each bit length */n{n var next_code = new Array(MAX_BITS + 1);n /* next code value for each bit length */nn var code = 0;n /* running code value */nn var bits;n /* bit index */nn var n;n /* code index */nn /* The distribution counts are first used to generate the code valuesn * without bit reversal.n */nn for (bits = 1; bits <= MAX_BITS; bits++) {n next_code = code = code + bl_count[bits - 1] << 1;n }n /* Check that the bit counts in bl_count are consistent. The last coden * must be all ones.n */n //Assert (code + bl_count-1 == (1<<MAX_BITS)-1,n // "inconsistent bit counts");n //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));nnn for (n = 0; n <= max_code; n++) {n var len = tree[n * 2 + 1]n /.Len/n ;nn if (len === 0) {n continue;n }n /* Now reverse the bits */nnn tree[n * 2]n /.Code/n = bi_reverse(next_code++, len); //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",n // n, (isgraph(n) ? n : ' '), len, tree.Code, next_code-1));n }n}n/* ===========================================================================n * Initialize the various 'constant' tables.n */nnnfunction tr_static_init() {n var n;n /* iterates over tree elements */nn var bits;n /* bit counter */nn var length;n /* length value */nn var code;n /* code value */nn var dist;n /* distance index */nn var bl_count = new Array(MAX_BITS + 1);n /* number of codes at each bit length for an optimal tree */n // do check in _tr_init()n //if (static_init_done) return;nn /* For some embedded targets, global variables are not initialized: */nn /*#ifdef NO_INIT_GLOBAL_POINTERSn static_l_desc.static_tree = static_ltree;n static_l_desc.extra_bits = extra_lbits;n static_d_desc.static_tree = static_dtree;n static_d_desc.extra_bits = extra_dbits;n static_bl_desc.extra_bits = extra_blbits;n endif*/nn /* Initialize the mapping length (0..255) -> length code (0..28) */nn length = 0;nn for (code = 0; code < LENGTH_CODES - 1; code++) {n base_length = length;nn for (n = 0; n < 1 << extra_lbits; n++) {n _length_code = code;n }n } //Assert (length == 256, "tr_static_init: length != 256");nn /* Note that the length 255 (match length 258) can be representedn * in two different ways: code 284 + 5 bits or code 285, so wen * overwrite length_code to use the best encoding:n */nnn _length_code[length - 1] = code;n /* Initialize the mapping dist (0..32K) -> dist code (0..29) */nn dist = 0;nn for (code = 0; code < 16; code++) {n base_dist = dist;nn for (n = 0; n < 1 << extra_dbits; n++) {n _dist_code = code;n }n } //Assert (dist == 256, "tr_static_init: dist != 256");nnn dist >>= 7;n /* from now on, all distances are divided by 128 */nn for (; code < D_CODES; code++) {n base_dist = dist << 7;nn for (n = 0; n < 1 << extra_dbits - 7; n++) {n _dist_code[256 + dist++] = code;n }n } //Assert (dist == 256, "tr_static_init: 256+dist != 512");nn /* Construct the codes of the static literal tree */nnn for (bits = 0; bits <= MAX_BITS; bits++) {n bl_count = 0;n }nn n = 0;nn while (n <= 143) {n static_ltree[n * 2 + 1]n /.Len/n = 8;n n++;n bl_count++;n }nn while (n <= 255) {n static_ltree[n * 2 + 1]n /.Len/n = 9;n n++;n bl_count++;n }nn while (n <= 279) {n static_ltree[n * 2 + 1]n /.Len/n = 7;n n++;n bl_count++;n }nn while (n <= 287) {n static_ltree[n * 2 + 1]n /.Len/n = 8;n n++;n bl_count++;n }n /* Codes 286 and 287 do not exist, but we must include them in then * tree construction to get a canonical Huffman tree (longest coden * all ones)n */nnn gen_codes(static_ltree, L_CODES + 1, bl_count);n /* The static distance tree is trivial: */nn for (n = 0; n < D_CODES; n++) {n static_dtree[n * 2 + 1]n /.Len/n = 5;n static_dtree[n * 2]n /.Code/n = bi_reverse(n, 5);n } // Now data ready and we can init static treesnnn static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);n static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);n static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS); //static_init_done = true;n}n/* ===========================================================================n * Initialize a new block.n */nnnfunction init_block(s) {n var n;n /* iterates over tree elements */nn /* Initialize the trees. */nn for (n = 0; n < L_CODES; n++) {n s.dyn_ltree[n * 2]n /.Freq/n = 0;n }nn for (n = 0; n < D_CODES; n++) {n s.dyn_dtree[n * 2]n /.Freq/n = 0;n }nn for (n = 0; n < BL_CODES; n++) {n s.bl_tree[n * 2]n /.Freq/n = 0;n }nn s.dyn_ltree[END_BLOCK * 2]n /.Freq/n = 1;n s.opt_len = s.static_len = 0;n s.last_lit = s.matches = 0;n}n/* ===========================================================================n * Flush the bit buffer and align the output on a byte boundaryn */nnnfunction bi_windup(s) {n if (s.bi_valid > 8) {n put_short(s, s.bi_buf);n } else if (s.bi_valid > 0) {n //put_byte(s, (Byte)s->bi_buf);n s.pending_buf = s.bi_buf;n }nn s.bi_buf = 0;n s.bi_valid = 0;n}n/* ===========================================================================n * Copy
a stored block, storing first the length and itsn * one's complement if requested.n */nnnfunction copy_block(s, buf, len, header) //DeflateState *s;n//charf *buf; /* the input data */n//unsigned len; /* its length */n//int header; /* true if block header must be written */n{n bi_windup(s);n /* align on byte boundary */nn if (header) {n put_short(s, len);n put_short(s, ~len);n } // while (len–) {n // put_byte(s, *buf++);n // }nnn utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);n s.pending += len;n}n/* ===========================================================================n * Compares to subtrees, using the tree depth as tie breaker whenn * the subtrees have equal frequency. This minimizes the worst case length.n */nnnfunction smaller(tree, n, m, depth) {n var _n2 = n * 2;nn var _m2 = m * 2;nn return treen /.Freq/n < treen /.Freq/n || treen /.Freq/n === treen /.Freq/n && depth <= depth;n}n/* ===========================================================================n * Restore the heap property by moving down the tree starting at node k,n * exchanging a node with the smallest of its two sons if necessary, stoppingn * when the heap property is re-established (each father smaller than itsn * two sons).n */nnnfunction pqdownheap(s, tree, k) // deflate_state *s;n// ct_data *tree; /* the tree to restore */n// int k; /* node to move down */n{n var v = s.heap;n var j = k << 1;n /* left son of k */nn while (j <= s.heap_len) {n /* Set j to the smallest of the two sons: */n if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap, s.depth)) {n j++;n }n /* Exit if v is smaller than both sons */nnn if (smaller(tree, v, s.heap, s.depth)) {n break;n }n /* Exchange v with the smallest son */nnn s.heap = s.heap;n k = j;n /* And continue down the tree, setting j to the left son of k */nn j <<= 1;n }nn s.heap = v;n} // inlined manuallyn// var SMALLEST = 1;nn/* ===========================================================================n * Send the block data compressed using the given Huffman treesn */nnnfunction compress_block(s, ltree, dtree) // deflate_state *s;n// const ct_data *ltree; /* literal tree */n// const ct_data *dtree; /* distance tree */n{n var dist;n /* distance of matched string */nn var lc;n /* match length or unmatched char (if dist == 0) */nn var lx = 0;n /* running index in l_buf */nn var code;n /* the code to send */nn var extra;n /* number of extra bits to send */nn if (s.last_lit !== 0) {n do {n dist = s.pending_buf[s.d_buf + lx * 2] << 8 | s.pending_buf[s.d_buf + lx * 2 + 1];n lc = s.pending_buf[s.l_buf + lx];n lx++;nn if (dist === 0) {n send_code(s, lc, ltree);n /* send a literal byte */n //Tracecv(isgraph(lc), (stderr," '%c' ", lc));n } else {n /* Here, lc is the match length - MIN_MATCH */n code = _length_code;n send_code(s, code + LITERALS + 1, ltree);n /* send the length code */nn extra = extra_lbits;nn if (extra !== 0) {n lc -= base_length;n send_bits(s, lc, extra);n /* send the extra length bits */n }nn dist–;n /* dist is now the match distance - 1 */nn code = d_code(dist); //Assert (code < D_CODES, "bad d_code");nn send_code(s, code, dtree);n /* send the distance code */nn extra = extra_dbits;nn if (extra !== 0) {n dist -= base_dist;n send_bits(s, dist, extra);n /* send the extra distance bits */n }n }n /* literal or match pair ? */nn /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */n //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,n // "pendingBuf overflow");nn } while (lx < s.last_lit);n }nn send_code(s, END_BLOCK, ltree);n}n/* ===========================================================================n * Construct one Huffman tree and assigns the code bit strings and lengths.n * Update the total bit length for the current block.n * IN assertion: the field freq is set for all tree elements.n * OUT assertions: the fields len and code are set to the optimal bit lengthn * and corresponding code. The length opt_len is updated; static_len isn * also updated if stree is not null. The field max_code is set.n */nnnfunction build_tree(s, desc) // deflate_state *s;n// tree_desc *desc; /* the tree descriptor */n{n var tree = desc.dyn_tree;n var stree = desc.stat_desc.static_tree;n var has_stree = desc.stat_desc.has_stree;n var elems = desc.stat_desc.elems;n var n, m;n /* iterate over heap elements */nn var max_code = -1;n /* largest code with non zero frequency */nn var node;n /* new node being created */nn /* Construct the initial heap, with least frequent element inn * heap. The sons of heap are heap and heap.n * heap is not used.n */nn s.heap_len = 0;n s.heap_max = HEAP_SIZE;nn for (n = 0; n < elems; n++) {n if (tree[n * 2]n /.Freq/n !== 0) {n s.heap = max_code = n;n s.depth = 0;n } else {n tree[n * 2 + 1]n /.Len/n = 0;n }n }n /* The pkzip format requires that at least one distance code exists,n * and that at least one bit should be sent even if there is only onen * possible code. So to avoid special checks later on we force at leastn * two codes of non zero frequency.n */nnn while (s.heap_len < 2) {n node = s.heap = max_code < 2 ? ++max_code : 0;n tree[node * 2]n /.Freq/n = 1;n s.depth = 0;n s.opt_len–;nn if (has_stree) {n s.static_len -= stree[node * 2 + 1]n /.Len/n ;n }n /* node is 0 or 1 so it does not have extra bits */nn }nn desc.max_code = max_code;n /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,n * establish sub-heaps of increasing lengths:n */nn for (n = s.heap_len >> 1n /*int /2*/n ; n >= 1; n–) {n pqdownheap(s, tree, n);n }n /* Construct the Huffman tree by repeatedly combining the least twon * frequent nodes.n */nnn node = elems;n /* next internal node of the tree */nn do {n //pqremove(s, tree, n); /* n = node of least frequency */nn /*** pqremove ***/n n = s.heap[1n /SMALLEST/n ];n s.heap[1n /SMALLEST/n ] = s.heap;n pqdownheap(s, tree, 1n /SMALLEST/n );n /***/nn m = s.heap[1n /SMALLEST/n ];n /* m = node of next least frequency */nn s.heap = n;n /* keep the nodes sorted by frequency */nn s.heap = m;n /* Create a new node father of n and m */nn tree[node * 2]n /.Freq/n = tree[n * 2]n /.Freq/n + tree[m * 2]n /.Freq/n ;n s.depth = (s.depth >= s.depth ? s.depth : s.depth) + 1;n tree[n * 2 + 1]n /.Dad/n = tree[m * 2 + 1]n /.Dad/n = node;n /* and insert the new node in the heap */nn s.heap[1n /SMALLEST/n ] = node++;n pqdownheap(s, tree, 1n /SMALLEST/n );n } while (s.heap_len >= 2);nn s.heap = s.heap[1n /SMALLEST/n ];n /* At this point, the fields freq and dad are set. We can nown * generate the bit lengths.n */nn gen_bitlen(s, desc);n /* The field len is now set, we can generate the bit codes */nn gen_codes(tree, max_code, s.bl_count);n}n/* ===========================================================================n * Scan a literal or distance tree to determine the frequencies of the codesn * in the bit length tree.n */nnnfunction scan_tree(s, tree, max_code) // deflate_state *s;n// ct_data *tree; /* the tree to be scanned */n// int max_code; /* and its largest code of non zero frequency */n{n var n;n /* iterates over all tree elements */nn var prevlen = -1;n /* last emitted length */nn var curlen;n /* length of current code */nn var nextlen = tree[0 * 2 + 1]n /.Len/n ;n /* length of next code */nn var count = 0;n /* repeat count of the current code */nn var max_count = 7;n /* max repeat count */nn var min_count = 4;n /* min repeat count */nn if (nextlen === 0) {n max_count = 138;n min_count = 3;n }nn tree[(max_code + 1) * 2 + 1]n /.Len/n = 0xffff;n /* guard */nn for (n = 0; n <= max_code; n++) {n curlen = nextlen;n nextlen = tree[(n + 1) * 2 + 1]n /.Len/n ;nn if (++count < max_count && curlen === nextlen) {n continue;n } else if (count < min_count) {n s.bl_tree[curlen * 2]n /.Freq/n += count;n } else if (curlen !== 0) {n if (curlen !== prevlen) {n s.bl_tree[curlen * 2] /.Freq/++;n }nn s.bl_tree[REP_3_6 * 2] /.Freq/++;n } else if (count <= 10) {n s.bl_tree[REPZ_3_10 * 2] /.Freq/++;n } else {n s.bl_tree[REPZ_11_138 * 2] /.Freq/++;n }nn count = 0;n prevlen = curlen;nn if (nextlen === 0) {n max_count = 138;n min_count = 3;n } else if (curlen === nextlen) {n max_count = 6;n min_count = 3;n } else {n max_count = 7;n min_count = 4;n }n }n}n/* ===========================================================================n * Send a literal or distance tree in compressed form, using the codes inn * bl_tree.n */nnnfunction send_tree(s, tree, max_code) // deflate_state *s;n// ct_data *tree; /* the tree to be scanned */n// int max_code; /* and its largest code of non zero frequency */n{n var n;n /* iterates over all tree elements */nn var prevlen = -1;n /* last emitted length */nn var curlen;n /* length of current code */nn var nextlen = tree[0 * 2 + 1]n /.Len/n ;n /* length of next code */nn var count = 0;n /* repeat count of the current code */nn var max_count = 7;n /* max repeat count */nn var min_count = 4;n /* min repeat count */nn /* tree.Len = -1; */nn /* guard already set */nn if (nextlen === 0) {n max_count = 138;n min_count = 3;n }nn for (n = 0; n <= max_code; n++) {n curlen = nextlen;n nextlen = tree[(n + 1) * 2 + 1]n /.Len/n ;nn if (++count < max_count && curlen === nextlen) {n continue;n } else if (count < min_count) {n do {n send_code(s, curlen, s.bl_tree);n } while (–count !== 0);n } else if (curlen !== 0) {n if (curlen !== prevlen) {n send_code(s, curlen, s.bl_tree);n count–;n } //Assert(count >= 3 && count <= 6, " 3_6?");nnn send_code(s, REP_3_6, s.bl_tree);n send_bits(s, count - 3, 2);n } else if (count <= 10) {n send_code(s, REPZ_3_10, s.bl_tree);n send_bits(s, count - 3, 3);n } else {n send_code(s, REPZ_11_138, s.bl_tree);n send_bits(s, count - 11, 7);n }nn count = 0;n prevlen = curlen;nn if (nextlen === 0) {n max_count = 138;n min_count = 3;n } else if (curlen === nextlen) {n max_count = 6;n min_count = 3;n } else {n max_count = 7;n min_count = 4;n }n }n}n/* ===========================================================================n * Construct the Huffman tree for the bit lengths and return the index inn * bl_order of the last bit length code to send.n */nnnfunction build_bl_tree(s) {n var max_blindex;n /* index of last bit length code of non zero freq */nn /* Determine the bit length frequencies for literal and distance trees */nn scan_tree(s, s.dyn_ltree, s.l_desc.max_code);n scan_tree(s, s.dyn_dtree, s.d_desc.max_code);n /* Build the bit length tree: */nn build_tree(s, s.bl_desc);n /* opt_len now includes the length of the tree representations, exceptn * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.n */nn /* Determine the number of bit length codes to send. The pkzip formatn * requires that at least 4 bit length codes be sent. (appnote.txt saysn * 3 but the actual value used is 4.)n */nn for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex–) {n if (s.bl_tree[bl_order * 2 + 1]n /.Len/n !== 0) {n break;n }n }n /* Update opt_len to include the bit length tree and counts */nnn s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",n // s->opt_len, s->static_len));nn return max_blindex;n}n/* ===========================================================================n * Send the header for a block using dynamic Huffman trees: the counts, then * lengths of the bit length codes, the literal tree and the distance tree.n * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.n */nnnfunction send_all_trees(s, lcodes, dcodes, blcodes) // deflate_state *s;n// int lcodes, dcodes, blcodes; /* number of codes for each tree */n{n var rank;n /* index in bl_order */n //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");n //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,n // "too many codes");n //Tracev((stderr, "\nbl counts: "));nn send_bits(s, lcodes - 257, 5);n /* not +255 as stated in appnote.txt */nn send_bits(s, dcodes - 1, 5);n send_bits(s, blcodes - 4, 4);n /* not -3 as stated in appnote.txt */nn for (rank = 0; rank < blcodes; rank++) {n //Tracev((stderr, "\nbl code %2d ", bl_order));n send_bits(s, s.bl_tree[bl_order * 2 + 1]n /.Len/n , 3);n } //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));nnn send_tree(s, s.dyn_ltree, lcodes - 1);n /* literal tree */n //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));nn send_tree(s, s.dyn_dtree, dcodes - 1);n /* distance tree */n //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));n}n/* ===========================================================================n * Check if the data type is TEXT or BINARY, using the following algorithm:n * - TEXT if the two conditions below are satisfied:n * a) There are no non-portable control characters belonging to then * "black list" (0..6, 14..25, 28..31).n * b) There is at least one printable character belonging to then * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).n * - BINARY otherwise.n * - The following partially-portable control characters form an * "gray list" that is ignored in this detection algorithm:n * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).n * IN assertion: the fields Freq of dyn_ltree are set.n */nnnfunction detect_data_type(s) {n /* black_mask is the bit mask of black-listed bytesn * set bits 0..6, 14..25, and 28..31n * 0xf3ffc07f = binary 11110011111111111100000001111111n */n var black_mask = 0xf3ffc07f;n var n;n /* Check for non-textual ("black-listed") bytes. */nn for (n = 0; n <= 31; n++, black_mask >>>= 1) {n if (black_mask & 1 && s.dyn_ltree[n * 2]n /.Freq/n !== 0) {n return Z_BINARY;n }n }n /* Check for textual ("white-listed") bytes. */nnn if (s.dyn_ltree[9 * 2]n /.Freq/n !== 0 || s.dyn_ltree[10 * 2]n /.Freq/n !== 0 || s.dyn_ltree[13 * 2]n /.Freq/n !== 0) {n return Z_TEXT;n }nn for (n = 32; n < LITERALS; n++) {n if (s.dyn_ltree[n * 2]n /.Freq/n !== 0) {n return Z_TEXT;n }n }n /* There are no "black-listed" or "white-listed" bytes:n * this stream either is empty or has tolerated ("gray-listed") bytes only.n */nnn return Z_BINARY;n}nnvar static_init_done = false;n/* ===========================================================================n * Initialize the tree data structures for a new zlib stream.n */nnfunction _tr_init(s) {n if (!static_init_done) {n tr_static_init();n static_init_done = true;n }nn s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);n s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);n s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);n s.bi_buf = 0;n s.bi_valid = 0;n /* Initialize the first block of the first file: */nn init_block(s);n}n/* ===========================================================================n * Send a stored blockn */nnnfunction _tr_stored_block(s, buf, stored_len, last) //DeflateState *s;n//charf *buf; /* input block */n//ulg stored_len; /* length of input block */n//int last; /* one if this is the last block for a file */n{n send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3);n /* send block type */nn copy_block(s, buf, stored_len, true);n /* with header */n}n/* ===========================================================================n * Send one empty static block to give enough lookahead for inflate.n * This takes 10 bits, of which 7 may remain in the bit buffer.n */nnnfunction _tr_align(s) {n send_bits(s, STATIC_TREES << 1, 3);n send_code(s, END_BLOCK, static_ltree);n bi_flush(s);n}n/* ===========================================================================n * Determine the best encoding for the current block: dynamic trees, staticn * trees or store, and output the encoded block to the zip file.n */nnnfunction _tr_flush_block(s, buf, stored_len, last) //DeflateState *s;n//charf *buf; /* input block, or NULL if too old */n//ulg stored_len; /* length of input block */n//int last; /* one if this is the last block for a file */n{n var opt_lenb, static_lenb;n /* opt_len and static_len in bytes */nn var max_blindex = 0;n /* index of last bit length code of non zero freq */nn /* Build the Huffman trees unless a stored block is forced */nn if (s.level > 0) {n /* Check if the file is binary or text */n if (s.strm.data_type === Z_UNKNOWN) {n s.strm.data_type = detect_data_type(s);n }n /* Construct the literal and distance trees */nnn build_tree(s, s.l_desc); // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,n // s->static_len));nn build_tree(s, s.d_desc); // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,n // s->static_len));nn /* At this point, opt_len and static_len are the total bit lengths ofn * the compressed block data, excluding the tree representations.n */nn /* Build the bit length tree for the above two trees, and get the indexn * in bl_order of the last bit length code to send.n */nn max_blindex = build_bl_tree(s);n /* Determine the best encoding. Compute the block lengths in bytes. */nn opt_lenb = s.opt_len + 3 + 7 >>> 3;n static_lenb = s.static_len + 3 + 7 >>> 3; // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",n // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,n // s->last_lit));nn if (static_lenb <= opt_lenb) {n opt_lenb = static_lenb;n }n } else {n // Assert(buf != (char*)0, "lost buf");n opt_lenb = static_lenb = stored_len + 5;n /* force a stored block */n }nn if (stored_len + 4 <= opt_lenb && buf !== -1) {n /* 4: two words for the lengths */nn /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.n * Otherwise we can't have processed more than WSIZE input bytes sincen * the last block flush, because compression would have beenn * successful. If LIT_BUFSIZE <= WSIZE, it is never too late ton * transform a block into a stored block.n */n _tr_stored_block(s, buf, stored_len, last);n } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {n send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);n compress_block(s, static_ltree, static_dtree);n } else {n send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);n send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);n compress_block(s, s.dyn_ltree, s.dyn_dtree);n } // Assert (s->compressed_len == s->bits_sent, "bad compressed size");nn /* The above check is made mod 2^32, for files larger than 512 MBn * and uLong implemented on 32 bits.n */nnn init_block(s);nn if (last) {n bi_windup(s);n } // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,n // s->compressed_len-7*last));nn}n/* ===========================================================================n * Save the match info and tally the frequency counts. Return true ifn * the current block must be flushed.n */nnnfunction _tr_tally(s, dist, lc) // deflate_state *s;n// unsigned dist; /* distance of matched string */n// unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */n{n //var out_length, in_length, dcode;n s.pending_buf[s.d_buf + s.last_lit * 2] = dist >>> 8 & 0xff;n s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;n s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;n s.last_lit++;nn if (dist === 0) {n /* lc is the unmatched char */n s.dyn_ltree[lc * 2] /.Freq/++;n } else {n s.matches++;n /* Here, lc is the match length - MIN_MATCH */nn dist–;n /* dist = match distance - 1 */n //Assert((ush)dist < (ush)MAX_DIST(s) &&n // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&n // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");nn s.dyn_ltree[(_length_code + LITERALS + 1) * 2] /.Freq/++;n s.dyn_dtree[d_code(dist) * 2] /.Freq/++;n } // (!) This block is disabled in zlib defaults,n // don't enable it for binary compatibilityn //#ifdef TRUNCATE_BLOCKn // /* Try to guess if it is profitable to stop the current block here */n // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {n // /* Compute an upper bound for the compressed length */n // out_length = s.last_lit*8;n // in_length = s.strstart - s.block_start;n //n // for (dcode = 0; dcode < D_CODES; dcode++) {n // out_length += s.dyn_dtree/.Freq/ * (5 + extra_dbits);n // }n // out_length >>>= 3;n // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",n // // s->last_lit, in_length, out_length,n // // 100L - out_length*100L/in_length));n // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {n // return true;n // }n // }n //#endifnnn return s.last_lit === s.lit_bufsize - 1;n /* We avoid equality with lit_bufsize because of wraparound at 64Kn * on 16 bit machines and because stored blocks are restricted ton * 64K-1 bytes.n */n}nnexports._tr_init = _tr_init;nexports._tr_stored_block = _tr_stored_block;nexports._tr_flush_block = _tr_flush_block;nexports._tr_tally = _tr_tally;nexports._tr_align = _tr_align;”,“map”:null,“metadata”:{},“sourceType”:“module”}