main
1import * as utils from "../utils/common.js";
2
3/* Public constants ==========================================================*/
4/* ===========================================================================*/
5
6
7//var Z_FILTERED = 1;
8//var Z_HUFFMAN_ONLY = 2;
9//var Z_RLE = 3;
10var Z_FIXED = 4;
11//var Z_DEFAULT_STRATEGY = 0;
12
13/* Possible values of the data_type field (though see inflate()) */
14var Z_BINARY = 0;
15var Z_TEXT = 1;
16//var Z_ASCII = 1; // = Z_TEXT
17var Z_UNKNOWN = 2;
18
19/*============================================================================*/
20
21
22function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
23
24// From zutil.h
25
26var STORED_BLOCK = 0;
27var STATIC_TREES = 1;
28var DYN_TREES = 2;
29/* The three kinds of block type */
30
31var MIN_MATCH = 3;
32var MAX_MATCH = 258;
33/* The minimum and maximum match lengths */
34
35// From deflate.h
36/* ===========================================================================
37 * Internal compression state.
38 */
39
40var LENGTH_CODES = 29;
41/* number of length codes, not counting the special END_BLOCK code */
42
43var LITERALS = 256;
44/* number of literal bytes 0..255 */
45
46var L_CODES = LITERALS + 1 + LENGTH_CODES;
47/* number of Literal or Length codes, including the END_BLOCK code */
48
49var D_CODES = 30;
50/* number of distance codes */
51
52var BL_CODES = 19;
53/* number of codes used to transfer the bit lengths */
54
55var HEAP_SIZE = 2 * L_CODES + 1;
56/* maximum heap size */
57
58var MAX_BITS = 15;
59/* All codes must not exceed MAX_BITS bits */
60
61var Buf_size = 16;
62/* size of bit buffer in bi_buf */
63
64
65/* ===========================================================================
66 * Constants
67 */
68
69var MAX_BL_BITS = 7;
70/* Bit length codes must not exceed MAX_BL_BITS bits */
71
72var END_BLOCK = 256;
73/* end of block literal code */
74
75var REP_3_6 = 16;
76/* repeat previous bit length 3-6 times (2 bits of repeat count) */
77
78var REPZ_3_10 = 17;
79/* repeat a zero length 3-10 times (3 bits of repeat count) */
80
81var REPZ_11_138 = 18;
82/* repeat a zero length 11-138 times (7 bits of repeat count) */
83
84/* eslint-disable comma-spacing,array-bracket-spacing */
85var extra_lbits = /* extra bits for each length code */
86 [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];
87
88var extra_dbits = /* extra bits for each distance code */
89 [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];
90
91var extra_blbits = /* extra bits for each bit length code */
92 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
93
94var bl_order =
95 [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
96/* eslint-enable comma-spacing,array-bracket-spacing */
97
98/* The lengths of the bit length codes are sent in order of decreasing
99 * probability, to avoid transmitting the lengths for unused bit length codes.
100 */
101
102/* ===========================================================================
103 * Local data. These are initialized only once.
104 */
105
106// We pre-fill arrays with 0 to avoid uninitialized gaps
107
108var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
109
110// !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1
111var static_ltree = new Array((L_CODES + 2) * 2);
112zero(static_ltree);
113/* The static literal tree. Since the bit lengths are imposed, there is no
114 * need for the L_CODES extra codes used during heap construction. However
115 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
116 * below).
117 */
118
119var static_dtree = new Array(D_CODES * 2);
120zero(static_dtree);
121/* The static distance tree. (Actually a trivial tree since all codes use
122 * 5 bits.)
123 */
124
125var _dist_code = new Array(DIST_CODE_LEN);
126zero(_dist_code);
127/* Distance codes. The first 256 values correspond to the distances
128 * 3 .. 258, the last 256 values correspond to the top 8 bits of
129 * the 15 bit distances.
130 */
131
132var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
133zero(_length_code);
134/* length code for each normalized match length (0 == MIN_MATCH) */
135
136var base_length = new Array(LENGTH_CODES);
137zero(base_length);
138/* First normalized length for each code (0 = MIN_MATCH) */
139
140var base_dist = new Array(D_CODES);
141zero(base_dist);
142/* First normalized distance for each code (0 = distance of 1) */
143
144
145function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
146
147 this.static_tree = static_tree; /* static tree or NULL */
148 this.extra_bits = extra_bits; /* extra bits for each code or NULL */
149 this.extra_base = extra_base; /* base index for extra_bits */
150 this.elems = elems; /* max number of elements in the tree */
151 this.max_length = max_length; /* max bit length for the codes */
152
153 // show if `static_tree` has data or dummy - needed for monomorphic objects
154 this.has_stree = static_tree && static_tree.length;
155}
156
157
158var static_l_desc;
159var static_d_desc;
160var static_bl_desc;
161
162
163function TreeDesc(dyn_tree, stat_desc) {
164 this.dyn_tree = dyn_tree; /* the dynamic tree */
165 this.max_code = 0; /* largest code with non zero frequency */
166 this.stat_desc = stat_desc; /* the corresponding static tree */
167}
168
169
170
171function d_code(dist) {
172 return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
173}
174
175
176/* ===========================================================================
177 * Output a short LSB first on the stream.
178 * IN assertion: there is enough room in pendingBuf.
179 */
180function put_short(s, w) {
181// put_byte(s, (uch)((w) & 0xff));
182// put_byte(s, (uch)((ush)(w) >> 8));
183 s.pending_buf[s.pending++] = (w) & 0xff;
184 s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
185}
186
187
188/* ===========================================================================
189 * Send a value on a given number of bits.
190 * IN assertion: length <= 16 and value fits in length bits.
191 */
192function send_bits(s, value, length) {
193 if (s.bi_valid > (Buf_size - length)) {
194 s.bi_buf |= (value << s.bi_valid) & 0xffff;
195 put_short(s, s.bi_buf);
196 s.bi_buf = value >> (Buf_size - s.bi_valid);
197 s.bi_valid += length - Buf_size;
198 } else {
199 s.bi_buf |= (value << s.bi_valid) & 0xffff;
200 s.bi_valid += length;
201 }
202}
203
204
205function send_code(s, c, tree) {
206 send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
207}
208
209
210/* ===========================================================================
211 * Reverse the first len bits of a code, using straightforward code (a faster
212 * method would use a table)
213 * IN assertion: 1 <= len <= 15
214 */
215function bi_reverse(code, len) {
216 var res = 0;
217 do {
218 res |= code & 1;
219 code >>>= 1;
220 res <<= 1;
221 } while (--len > 0);
222 return res >>> 1;
223}
224
225
226/* ===========================================================================
227 * Flush the bit buffer, keeping at most 7 bits in it.
228 */
229function bi_flush(s) {
230 if (s.bi_valid === 16) {
231 put_short(s, s.bi_buf);
232 s.bi_buf = 0;
233 s.bi_valid = 0;
234
235 } else if (s.bi_valid >= 8) {
236 s.pending_buf[s.pending++] = s.bi_buf & 0xff;
237 s.bi_buf >>= 8;
238 s.bi_valid -= 8;
239 }
240}
241
242
243/* ===========================================================================
244 * Compute the optimal bit lengths for a tree and update the total bit length
245 * for the current block.
246 * IN assertion: the fields freq and dad are set, heap[heap_max] and
247 * above are the tree nodes sorted by increasing frequency.
248 * OUT assertions: the field len is set to the optimal bit length, the
249 * array bl_count contains the frequencies for each bit length.
250 * The length opt_len is updated; static_len is also updated if stree is
251 * not null.
252 */
253function gen_bitlen(s, desc)
254// deflate_state *s;
255// tree_desc *desc; /* the tree descriptor */
256{
257 var tree = desc.dyn_tree;
258 var max_code = desc.max_code;
259 var stree = desc.stat_desc.static_tree;
260 var has_stree = desc.stat_desc.has_stree;
261 var extra = desc.stat_desc.extra_bits;
262 var base = desc.stat_desc.extra_base;
263 var max_length = desc.stat_desc.max_length;
264 var h; /* heap index */
265 var n, m; /* iterate over the tree elements */
266 var bits; /* bit length */
267 var xbits; /* extra bits */
268 var f; /* frequency */
269 var overflow = 0; /* number of elements with bit length too large */
270
271 for (bits = 0; bits <= MAX_BITS; bits++) {
272 s.bl_count[bits] = 0;
273 }
274
275 /* In a first pass, compute the optimal bit lengths (which may
276 * overflow in the case of the bit length tree).
277 */
278 tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
279
280 for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
281 n = s.heap[h];
282 bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
283 if (bits > max_length) {
284 bits = max_length;
285 overflow++;
286 }
287 tree[n * 2 + 1]/*.Len*/ = bits;
288 /* We overwrite tree[n].Dad which is no longer needed */
289
290 if (n > max_code) { continue; } /* not a leaf node */
291
292 s.bl_count[bits]++;
293 xbits = 0;
294 if (n >= base) {
295 xbits = extra[n - base];
296 }
297 f = tree[n * 2]/*.Freq*/;
298 s.opt_len += f * (bits + xbits);
299 if (has_stree) {
300 s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
301 }
302 }
303 if (overflow === 0) { return; }
304
305 // Trace((stderr,"\nbit length overflow\n"));
306 /* This happens for example on obj2 and pic of the Calgary corpus */
307
308 /* Find the first bit length which could increase: */
309 do {
310 bits = max_length - 1;
311 while (s.bl_count[bits] === 0) { bits--; }
312 s.bl_count[bits]--; /* move one leaf down the tree */
313 s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
314 s.bl_count[max_length]--;
315 /* The brother of the overflow item also moves one step up,
316 * but this does not affect bl_count[max_length]
317 */
318 overflow -= 2;
319 } while (overflow > 0);
320
321 /* Now recompute all bit lengths, scanning in increasing frequency.
322 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
323 * lengths instead of fixing only the wrong ones. This idea is taken
324 * from 'ar' written by Haruhiko Okumura.)
325 */
326 for (bits = max_length; bits !== 0; bits--) {
327 n = s.bl_count[bits];
328 while (n !== 0) {
329 m = s.heap[--h];
330 if (m > max_code) { continue; }
331 if (tree[m * 2 + 1]/*.Len*/ !== bits) {
332 // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
333 s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
334 tree[m * 2 + 1]/*.Len*/ = bits;
335 }
336 n--;
337 }
338 }
339}
340
341
342/* ===========================================================================
343 * Generate the codes for a given tree and bit counts (which need not be
344 * optimal).
345 * IN assertion: the array bl_count contains the bit length statistics for
346 * the given tree and the field len is set for all tree elements.
347 * OUT assertion: the field code is set for all tree elements of non
348 * zero code length.
349 */
350function gen_codes(tree, max_code, bl_count)
351// ct_data *tree; /* the tree to decorate */
352// int max_code; /* largest code with non zero frequency */
353// ushf *bl_count; /* number of codes at each bit length */
354{
355 var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
356 var code = 0; /* running code value */
357 var bits; /* bit index */
358 var n; /* code index */
359
360 /* The distribution counts are first used to generate the code values
361 * without bit reversal.
362 */
363 for (bits = 1; bits <= MAX_BITS; bits++) {
364 next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
365 }
366 /* Check that the bit counts in bl_count are consistent. The last code
367 * must be all ones.
368 */
369 //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
370 // "inconsistent bit counts");
371 //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
372
373 for (n = 0; n <= max_code; n++) {
374 var len = tree[n * 2 + 1]/*.Len*/;
375 if (len === 0) { continue; }
376 /* Now reverse the bits */
377 tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
378
379 //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
380 // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
381 }
382}
383
384
385/* ===========================================================================
386 * Initialize the various 'constant' tables.
387 */
388function tr_static_init() {
389 var n; /* iterates over tree elements */
390 var bits; /* bit counter */
391 var length; /* length value */
392 var code; /* code value */
393 var dist; /* distance index */
394 var bl_count = new Array(MAX_BITS + 1);
395 /* number of codes at each bit length for an optimal tree */
396
397 // do check in _tr_init()
398 //if (static_init_done) return;
399
400 /* For some embedded targets, global variables are not initialized: */
401/*#ifdef NO_INIT_GLOBAL_POINTERS
402 static_l_desc.static_tree = static_ltree;
403 static_l_desc.extra_bits = extra_lbits;
404 static_d_desc.static_tree = static_dtree;
405 static_d_desc.extra_bits = extra_dbits;
406 static_bl_desc.extra_bits = extra_blbits;
407#endif*/
408
409 /* Initialize the mapping length (0..255) -> length code (0..28) */
410 length = 0;
411 for (code = 0; code < LENGTH_CODES - 1; code++) {
412 base_length[code] = length;
413 for (n = 0; n < (1 << extra_lbits[code]); n++) {
414 _length_code[length++] = code;
415 }
416 }
417 //Assert (length == 256, "tr_static_init: length != 256");
418 /* Note that the length 255 (match length 258) can be represented
419 * in two different ways: code 284 + 5 bits or code 285, so we
420 * overwrite length_code[255] to use the best encoding:
421 */
422 _length_code[length - 1] = code;
423
424 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
425 dist = 0;
426 for (code = 0; code < 16; code++) {
427 base_dist[code] = dist;
428 for (n = 0; n < (1 << extra_dbits[code]); n++) {
429 _dist_code[dist++] = code;
430 }
431 }
432 //Assert (dist == 256, "tr_static_init: dist != 256");
433 dist >>= 7; /* from now on, all distances are divided by 128 */
434 for (; code < D_CODES; code++) {
435 base_dist[code] = dist << 7;
436 for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
437 _dist_code[256 + dist++] = code;
438 }
439 }
440 //Assert (dist == 256, "tr_static_init: 256+dist != 512");
441
442 /* Construct the codes of the static literal tree */
443 for (bits = 0; bits <= MAX_BITS; bits++) {
444 bl_count[bits] = 0;
445 }
446
447 n = 0;
448 while (n <= 143) {
449 static_ltree[n * 2 + 1]/*.Len*/ = 8;
450 n++;
451 bl_count[8]++;
452 }
453 while (n <= 255) {
454 static_ltree[n * 2 + 1]/*.Len*/ = 9;
455 n++;
456 bl_count[9]++;
457 }
458 while (n <= 279) {
459 static_ltree[n * 2 + 1]/*.Len*/ = 7;
460 n++;
461 bl_count[7]++;
462 }
463 while (n <= 287) {
464 static_ltree[n * 2 + 1]/*.Len*/ = 8;
465 n++;
466 bl_count[8]++;
467 }
468 /* Codes 286 and 287 do not exist, but we must include them in the
469 * tree construction to get a canonical Huffman tree (longest code
470 * all ones)
471 */
472 gen_codes(static_ltree, L_CODES + 1, bl_count);
473
474 /* The static distance tree is trivial: */
475 for (n = 0; n < D_CODES; n++) {
476 static_dtree[n * 2 + 1]/*.Len*/ = 5;
477 static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
478 }
479
480 // Now data ready and we can init static trees
481 static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
482 static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
483 static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
484
485 //static_init_done = true;
486}
487
488
489/* ===========================================================================
490 * Initialize a new block.
491 */
492function init_block(s) {
493 var n; /* iterates over tree elements */
494
495 /* Initialize the trees. */
496 for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
497 for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
498 for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
499
500 s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
501 s.opt_len = s.static_len = 0;
502 s.last_lit = s.matches = 0;
503}
504
505
506/* ===========================================================================
507 * Flush the bit buffer and align the output on a byte boundary
508 */
509function bi_windup(s)
510{
511 if (s.bi_valid > 8) {
512 put_short(s, s.bi_buf);
513 } else if (s.bi_valid > 0) {
514 //put_byte(s, (Byte)s->bi_buf);
515 s.pending_buf[s.pending++] = s.bi_buf;
516 }
517 s.bi_buf = 0;
518 s.bi_valid = 0;
519}
520
521/* ===========================================================================
522 * Copy a stored block, storing first the length and its
523 * one's complement if requested.
524 */
525function copy_block(s, buf, len, header)
526//DeflateState *s;
527//charf *buf; /* the input data */
528//unsigned len; /* its length */
529//int header; /* true if block header must be written */
530{
531 bi_windup(s); /* align on byte boundary */
532
533 if (header) {
534 put_short(s, len);
535 put_short(s, ~len);
536 }
537// while (len--) {
538// put_byte(s, *buf++);
539// }
540 utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
541 s.pending += len;
542}
543
544/* ===========================================================================
545 * Compares to subtrees, using the tree depth as tie breaker when
546 * the subtrees have equal frequency. This minimizes the worst case length.
547 */
548function smaller(tree, n, m, depth) {
549 var _n2 = n * 2;
550 var _m2 = m * 2;
551 return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
552 (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
553}
554
555/* ===========================================================================
556 * Restore the heap property by moving down the tree starting at node k,
557 * exchanging a node with the smallest of its two sons if necessary, stopping
558 * when the heap property is re-established (each father smaller than its
559 * two sons).
560 */
561function pqdownheap(s, tree, k)
562// deflate_state *s;
563// ct_data *tree; /* the tree to restore */
564// int k; /* node to move down */
565{
566 var v = s.heap[k];
567 var j = k << 1; /* left son of k */
568 while (j <= s.heap_len) {
569 /* Set j to the smallest of the two sons: */
570 if (j < s.heap_len &&
571 smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
572 j++;
573 }
574 /* Exit if v is smaller than both sons */
575 if (smaller(tree, v, s.heap[j], s.depth)) { break; }
576
577 /* Exchange v with the smallest son */
578 s.heap[k] = s.heap[j];
579 k = j;
580
581 /* And continue down the tree, setting j to the left son of k */
582 j <<= 1;
583 }
584 s.heap[k] = v;
585}
586
587
588// inlined manually
589// var SMALLEST = 1;
590
591/* ===========================================================================
592 * Send the block data compressed using the given Huffman trees
593 */
594function compress_block(s, ltree, dtree)
595// deflate_state *s;
596// const ct_data *ltree; /* literal tree */
597// const ct_data *dtree; /* distance tree */
598{
599 var dist; /* distance of matched string */
600 var lc; /* match length or unmatched char (if dist == 0) */
601 var lx = 0; /* running index in l_buf */
602 var code; /* the code to send */
603 var extra; /* number of extra bits to send */
604
605 if (s.last_lit !== 0) {
606 do {
607 dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
608 lc = s.pending_buf[s.l_buf + lx];
609 lx++;
610
611 if (dist === 0) {
612 send_code(s, lc, ltree); /* send a literal byte */
613 //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
614 } else {
615 /* Here, lc is the match length - MIN_MATCH */
616 code = _length_code[lc];
617 send_code(s, code + LITERALS + 1, ltree); /* send the length code */
618 extra = extra_lbits[code];
619 if (extra !== 0) {
620 lc -= base_length[code];
621 send_bits(s, lc, extra); /* send the extra length bits */
622 }
623 dist--; /* dist is now the match distance - 1 */
624 code = d_code(dist);
625 //Assert (code < D_CODES, "bad d_code");
626
627 send_code(s, code, dtree); /* send the distance code */
628 extra = extra_dbits[code];
629 if (extra !== 0) {
630 dist -= base_dist[code];
631 send_bits(s, dist, extra); /* send the extra distance bits */
632 }
633 } /* literal or match pair ? */
634
635 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
636 //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
637 // "pendingBuf overflow");
638
639 } while (lx < s.last_lit);
640 }
641
642 send_code(s, END_BLOCK, ltree);
643}
644
645
646/* ===========================================================================
647 * Construct one Huffman tree and assigns the code bit strings and lengths.
648 * Update the total bit length for the current block.
649 * IN assertion: the field freq is set for all tree elements.
650 * OUT assertions: the fields len and code are set to the optimal bit length
651 * and corresponding code. The length opt_len is updated; static_len is
652 * also updated if stree is not null. The field max_code is set.
653 */
654function build_tree(s, desc)
655// deflate_state *s;
656// tree_desc *desc; /* the tree descriptor */
657{
658 var tree = desc.dyn_tree;
659 var stree = desc.stat_desc.static_tree;
660 var has_stree = desc.stat_desc.has_stree;
661 var elems = desc.stat_desc.elems;
662 var n, m; /* iterate over heap elements */
663 var max_code = -1; /* largest code with non zero frequency */
664 var node; /* new node being created */
665
666 /* Construct the initial heap, with least frequent element in
667 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
668 * heap[0] is not used.
669 */
670 s.heap_len = 0;
671 s.heap_max = HEAP_SIZE;
672
673 for (n = 0; n < elems; n++) {
674 if (tree[n * 2]/*.Freq*/ !== 0) {
675 s.heap[++s.heap_len] = max_code = n;
676 s.depth[n] = 0;
677
678 } else {
679 tree[n * 2 + 1]/*.Len*/ = 0;
680 }
681 }
682
683 /* The pkzip format requires that at least one distance code exists,
684 * and that at least one bit should be sent even if there is only one
685 * possible code. So to avoid special checks later on we force at least
686 * two codes of non zero frequency.
687 */
688 while (s.heap_len < 2) {
689 node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
690 tree[node * 2]/*.Freq*/ = 1;
691 s.depth[node] = 0;
692 s.opt_len--;
693
694 if (has_stree) {
695 s.static_len -= stree[node * 2 + 1]/*.Len*/;
696 }
697 /* node is 0 or 1 so it does not have extra bits */
698 }
699 desc.max_code = max_code;
700
701 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
702 * establish sub-heaps of increasing lengths:
703 */
704 for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
705
706 /* Construct the Huffman tree by repeatedly combining the least two
707 * frequent nodes.
708 */
709 node = elems; /* next internal node of the tree */
710 do {
711 //pqremove(s, tree, n); /* n = node of least frequency */
712 /*** pqremove ***/
713 n = s.heap[1/*SMALLEST*/];
714 s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
715 pqdownheap(s, tree, 1/*SMALLEST*/);
716 /***/
717
718 m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
719
720 s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
721 s.heap[--s.heap_max] = m;
722
723 /* Create a new node father of n and m */
724 tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
725 s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
726 tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
727
728 /* and insert the new node in the heap */
729 s.heap[1/*SMALLEST*/] = node++;
730 pqdownheap(s, tree, 1/*SMALLEST*/);
731
732 } while (s.heap_len >= 2);
733
734 s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
735
736 /* At this point, the fields freq and dad are set. We can now
737 * generate the bit lengths.
738 */
739 gen_bitlen(s, desc);
740
741 /* The field len is now set, we can generate the bit codes */
742 gen_codes(tree, max_code, s.bl_count);
743}
744
745
746/* ===========================================================================
747 * Scan a literal or distance tree to determine the frequencies of the codes
748 * in the bit length tree.
749 */
750function scan_tree(s, tree, max_code)
751// deflate_state *s;
752// ct_data *tree; /* the tree to be scanned */
753// int max_code; /* and its largest code of non zero frequency */
754{
755 var n; /* iterates over all tree elements */
756 var prevlen = -1; /* last emitted length */
757 var curlen; /* length of current code */
758
759 var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
760
761 var count = 0; /* repeat count of the current code */
762 var max_count = 7; /* max repeat count */
763 var min_count = 4; /* min repeat count */
764
765 if (nextlen === 0) {
766 max_count = 138;
767 min_count = 3;
768 }
769 tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
770
771 for (n = 0; n <= max_code; n++) {
772 curlen = nextlen;
773 nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
774
775 if (++count < max_count && curlen === nextlen) {
776 continue;
777
778 } else if (count < min_count) {
779 s.bl_tree[curlen * 2]/*.Freq*/ += count;
780
781 } else if (curlen !== 0) {
782
783 if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
784 s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
785
786 } else if (count <= 10) {
787 s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
788
789 } else {
790 s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
791 }
792
793 count = 0;
794 prevlen = curlen;
795
796 if (nextlen === 0) {
797 max_count = 138;
798 min_count = 3;
799
800 } else if (curlen === nextlen) {
801 max_count = 6;
802 min_count = 3;
803
804 } else {
805 max_count = 7;
806 min_count = 4;
807 }
808 }
809}
810
811
812/* ===========================================================================
813 * Send a literal or distance tree in compressed form, using the codes in
814 * bl_tree.
815 */
816function send_tree(s, tree, max_code)
817// deflate_state *s;
818// ct_data *tree; /* the tree to be scanned */
819// int max_code; /* and its largest code of non zero frequency */
820{
821 var n; /* iterates over all tree elements */
822 var prevlen = -1; /* last emitted length */
823 var curlen; /* length of current code */
824
825 var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
826
827 var count = 0; /* repeat count of the current code */
828 var max_count = 7; /* max repeat count */
829 var min_count = 4; /* min repeat count */
830
831 /* tree[max_code+1].Len = -1; */ /* guard already set */
832 if (nextlen === 0) {
833 max_count = 138;
834 min_count = 3;
835 }
836
837 for (n = 0; n <= max_code; n++) {
838 curlen = nextlen;
839 nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
840
841 if (++count < max_count && curlen === nextlen) {
842 continue;
843
844 } else if (count < min_count) {
845 do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
846
847 } else if (curlen !== 0) {
848 if (curlen !== prevlen) {
849 send_code(s, curlen, s.bl_tree);
850 count--;
851 }
852 //Assert(count >= 3 && count <= 6, " 3_6?");
853 send_code(s, REP_3_6, s.bl_tree);
854 send_bits(s, count - 3, 2);
855
856 } else if (count <= 10) {
857 send_code(s, REPZ_3_10, s.bl_tree);
858 send_bits(s, count - 3, 3);
859
860 } else {
861 send_code(s, REPZ_11_138, s.bl_tree);
862 send_bits(s, count - 11, 7);
863 }
864
865 count = 0;
866 prevlen = curlen;
867 if (nextlen === 0) {
868 max_count = 138;
869 min_count = 3;
870
871 } else if (curlen === nextlen) {
872 max_count = 6;
873 min_count = 3;
874
875 } else {
876 max_count = 7;
877 min_count = 4;
878 }
879 }
880}
881
882
883/* ===========================================================================
884 * Construct the Huffman tree for the bit lengths and return the index in
885 * bl_order of the last bit length code to send.
886 */
887function build_bl_tree(s) {
888 var max_blindex; /* index of last bit length code of non zero freq */
889
890 /* Determine the bit length frequencies for literal and distance trees */
891 scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
892 scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
893
894 /* Build the bit length tree: */
895 build_tree(s, s.bl_desc);
896 /* opt_len now includes the length of the tree representations, except
897 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
898 */
899
900 /* Determine the number of bit length codes to send. The pkzip format
901 * requires that at least 4 bit length codes be sent. (appnote.txt says
902 * 3 but the actual value used is 4.)
903 */
904 for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
905 if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
906 break;
907 }
908 }
909 /* Update opt_len to include the bit length tree and counts */
910 s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
911 //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
912 // s->opt_len, s->static_len));
913
914 return max_blindex;
915}
916
917
918/* ===========================================================================
919 * Send the header for a block using dynamic Huffman trees: the counts, the
920 * lengths of the bit length codes, the literal tree and the distance tree.
921 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
922 */
923function send_all_trees(s, lcodes, dcodes, blcodes)
924// deflate_state *s;
925// int lcodes, dcodes, blcodes; /* number of codes for each tree */
926{
927 var rank; /* index in bl_order */
928
929 //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
930 //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
931 // "too many codes");
932 //Tracev((stderr, "\nbl counts: "));
933 send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
934 send_bits(s, dcodes - 1, 5);
935 send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
936 for (rank = 0; rank < blcodes; rank++) {
937 //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
938 send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
939 }
940 //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
941
942 send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
943 //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
944
945 send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
946 //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
947}
948
949
950/* ===========================================================================
951 * Check if the data type is TEXT or BINARY, using the following algorithm:
952 * - TEXT if the two conditions below are satisfied:
953 * a) There are no non-portable control characters belonging to the
954 * "black list" (0..6, 14..25, 28..31).
955 * b) There is at least one printable character belonging to the
956 * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
957 * - BINARY otherwise.
958 * - The following partially-portable control characters form a
959 * "gray list" that is ignored in this detection algorithm:
960 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
961 * IN assertion: the fields Freq of dyn_ltree are set.
962 */
963function detect_data_type(s) {
964 /* black_mask is the bit mask of black-listed bytes
965 * set bits 0..6, 14..25, and 28..31
966 * 0xf3ffc07f = binary 11110011111111111100000001111111
967 */
968 var black_mask = 0xf3ffc07f;
969 var n;
970
971 /* Check for non-textual ("black-listed") bytes. */
972 for (n = 0; n <= 31; n++, black_mask >>>= 1) {
973 if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
974 return Z_BINARY;
975 }
976 }
977
978 /* Check for textual ("white-listed") bytes. */
979 if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
980 s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
981 return Z_TEXT;
982 }
983 for (n = 32; n < LITERALS; n++) {
984 if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
985 return Z_TEXT;
986 }
987 }
988
989 /* There are no "black-listed" or "white-listed" bytes:
990 * this stream either is empty or has tolerated ("gray-listed") bytes only.
991 */
992 return Z_BINARY;
993}
994
995
996var static_init_done = false;
997
998/* ===========================================================================
999 * Initialize the tree data structures for a new zlib stream.
1000 */
1001function _tr_init(s)
1002{
1003
1004 if (!static_init_done) {
1005 tr_static_init();
1006 static_init_done = true;
1007 }
1008
1009 s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
1010 s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
1011 s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
1012
1013 s.bi_buf = 0;
1014 s.bi_valid = 0;
1015
1016 /* Initialize the first block of the first file: */
1017 init_block(s);
1018}
1019
1020
1021/* ===========================================================================
1022 * Send a stored block
1023 */
1024function _tr_stored_block(s, buf, stored_len, last)
1025//DeflateState *s;
1026//charf *buf; /* input block */
1027//ulg stored_len; /* length of input block */
1028//int last; /* one if this is the last block for a file */
1029{
1030 send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
1031 copy_block(s, buf, stored_len, true); /* with header */
1032}
1033
1034
1035/* ===========================================================================
1036 * Send one empty static block to give enough lookahead for inflate.
1037 * This takes 10 bits, of which 7 may remain in the bit buffer.
1038 */
1039function _tr_align(s) {
1040 send_bits(s, STATIC_TREES << 1, 3);
1041 send_code(s, END_BLOCK, static_ltree);
1042 bi_flush(s);
1043}
1044
1045
1046/* ===========================================================================
1047 * Determine the best encoding for the current block: dynamic trees, static
1048 * trees or store, and output the encoded block to the zip file.
1049 */
1050function _tr_flush_block(s, buf, stored_len, last)
1051//DeflateState *s;
1052//charf *buf; /* input block, or NULL if too old */
1053//ulg stored_len; /* length of input block */
1054//int last; /* one if this is the last block for a file */
1055{
1056 var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
1057 var max_blindex = 0; /* index of last bit length code of non zero freq */
1058
1059 /* Build the Huffman trees unless a stored block is forced */
1060 if (s.level > 0) {
1061
1062 /* Check if the file is binary or text */
1063 if (s.strm.data_type === Z_UNKNOWN) {
1064 s.strm.data_type = detect_data_type(s);
1065 }
1066
1067 /* Construct the literal and distance trees */
1068 build_tree(s, s.l_desc);
1069 // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
1070 // s->static_len));
1071
1072 build_tree(s, s.d_desc);
1073 // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
1074 // s->static_len));
1075 /* At this point, opt_len and static_len are the total bit lengths of
1076 * the compressed block data, excluding the tree representations.
1077 */
1078
1079 /* Build the bit length tree for the above two trees, and get the index
1080 * in bl_order of the last bit length code to send.
1081 */
1082 max_blindex = build_bl_tree(s);
1083
1084 /* Determine the best encoding. Compute the block lengths in bytes. */
1085 opt_lenb = (s.opt_len + 3 + 7) >>> 3;
1086 static_lenb = (s.static_len + 3 + 7) >>> 3;
1087
1088 // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
1089 // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
1090 // s->last_lit));
1091
1092 if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
1093
1094 } else {
1095 // Assert(buf != (char*)0, "lost buf");
1096 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
1097 }
1098
1099 if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
1100 /* 4: two words for the lengths */
1101
1102 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
1103 * Otherwise we can't have processed more than WSIZE input bytes since
1104 * the last block flush, because compression would have been
1105 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
1106 * transform a block into a stored block.
1107 */
1108 _tr_stored_block(s, buf, stored_len, last);
1109
1110 } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
1111
1112 send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
1113 compress_block(s, static_ltree, static_dtree);
1114
1115 } else {
1116 send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
1117 send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
1118 compress_block(s, s.dyn_ltree, s.dyn_dtree);
1119 }
1120 // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1121 /* The above check is made mod 2^32, for files larger than 512 MB
1122 * and uLong implemented on 32 bits.
1123 */
1124 init_block(s);
1125
1126 if (last) {
1127 bi_windup(s);
1128 }
1129 // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1130 // s->compressed_len-7*last));
1131}
1132
1133/* ===========================================================================
1134 * Save the match info and tally the frequency counts. Return true if
1135 * the current block must be flushed.
1136 */
1137function _tr_tally(s, dist, lc)
1138// deflate_state *s;
1139// unsigned dist; /* distance of matched string */
1140// unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1141{
1142 //var out_length, in_length, dcode;
1143
1144 s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
1145 s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
1146
1147 s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
1148 s.last_lit++;
1149
1150 if (dist === 0) {
1151 /* lc is the unmatched char */
1152 s.dyn_ltree[lc * 2]/*.Freq*/++;
1153 } else {
1154 s.matches++;
1155 /* Here, lc is the match length - MIN_MATCH */
1156 dist--; /* dist = match distance - 1 */
1157 //Assert((ush)dist < (ush)MAX_DIST(s) &&
1158 // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1159 // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1160
1161 s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
1162 s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
1163 }
1164
1165// (!) This block is disabled in zlib defailts,
1166// don't enable it for binary compatibility
1167
1168//#ifdef TRUNCATE_BLOCK
1169// /* Try to guess if it is profitable to stop the current block here */
1170// if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
1171// /* Compute an upper bound for the compressed length */
1172// out_length = s.last_lit*8;
1173// in_length = s.strstart - s.block_start;
1174//
1175// for (dcode = 0; dcode < D_CODES; dcode++) {
1176// out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
1177// }
1178// out_length >>>= 3;
1179// //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1180// // s->last_lit, in_length, out_length,
1181// // 100L - out_length*100L/in_length));
1182// if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
1183// return true;
1184// }
1185// }
1186//#endif
1187
1188 return (s.last_lit === s.lit_bufsize - 1);
1189 /* We avoid equality with lit_bufsize because of wraparound at 64K
1190 * on 16 bit machines and because stored blocks are restricted to
1191 * 64K-1 bytes.
1192 */
1193}
1194
1195export { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align };