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