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