@cloudcare/browser-worker 3.0.22

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