@zip.js/zip.js 2.6.25 → 2.6.28
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.
- package/README.md +16 -17
- package/dist/z-worker-fflate.js +1 -1
- package/dist/z-worker-pako.js +1 -1
- package/dist/z-worker.js +1 -1
- package/dist/zip-fs-full.js +5593 -5582
- package/dist/zip-fs-full.min.js +1 -1
- package/dist/zip-fs.js +536 -514
- package/dist/zip-fs.min.js +1 -1
- package/dist/zip-full.js +4110 -4099
- package/dist/zip-full.min.js +1 -1
- package/dist/zip-no-worker-deflate.min.js +1 -1
- package/dist/zip-no-worker-inflate.min.js +1 -1
- package/dist/zip-no-worker.min.js +1 -1
- package/dist/zip.js +536 -514
- package/dist/zip.min.js +1 -1
- package/index.d.ts +46 -23
- package/index.js +2 -2
- package/lib/core/codec-interface.js +17 -9
- package/lib/core/codec-pool-worker.js +2 -3
- package/lib/core/codec-pool.js +1 -1
- package/lib/core/configuration.js +29 -8
- package/lib/core/io.js +36 -46
- package/lib/core/streams/aes-crypto-stream.js +87 -62
- package/lib/core/streams/codec-stream.js +4 -3
- package/lib/core/streams/codecs/crc32.js +3 -1
- package/lib/core/streams/codecs/deflate.js +1792 -1796
- package/lib/core/streams/codecs/inflate.js +1828 -1831
- package/lib/core/streams/common-crypto.js +3 -1
- package/lib/core/streams/crc32-stream.js +7 -7
- package/lib/core/streams/zip-crypto-stream.js +13 -14
- package/lib/core/streams/zip-entry-stream.js +42 -58
- package/lib/core/util/cp437-decode.js +6 -2
- package/lib/core/util/decode-text.js +4 -2
- package/lib/core/util/encode-text.js +3 -1
- package/lib/core/util/stream-codec-shim.js +6 -2
- package/lib/core/z-worker-core.js +9 -9
- package/lib/core/zip-entry.js +6 -2
- package/lib/core/zip-reader.js +15 -6
- package/lib/core/zip-writer.js +74 -50
- package/lib/z-worker-bootstrap-fflate.js +1 -1
- package/lib/z-worker-bootstrap-pako.js +1 -1
- package/lib/z-worker-inline-template.js +9 -7
- package/lib/z-worker-inline.js +1 -1
- package/lib/z-worker.js +2 -2
- package/lib/zip-fflate-shim.js +1 -1
- package/lib/zip-fs.js +2 -2
- package/lib/zip-full-fflate.js +2 -2
- package/lib/zip-full.js +4 -4
- package/lib/zip-no-worker-deflate.js +1 -1
- package/lib/zip-no-worker-inflate.js +1 -1
- package/lib/zip-no-worker.js +2 -2
- package/lib/zip.js +2 -2
- package/package.json +5 -2
- package/rollup-fflate.config.dev.js +2 -2
- package/rollup-fflate.config.js +2 -2
- package/rollup.config.dev.js +2 -2
- package/rollup.config.js +2 -2
- package/tests/all/test-streams.js +29 -0
- package/tests/tests-data.js +1 -0
- package/lib/z-worker-inline-template-base64.js +0 -36
|
@@ -37,2031 +37,2027 @@
|
|
|
37
37
|
|
|
38
38
|
// Global
|
|
39
39
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
bits =
|
|
146
|
-
|
|
147
|
-
bits = max_length;
|
|
148
|
-
overflow++;
|
|
149
|
-
}
|
|
150
|
-
tree[n * 2 + 1] = bits;
|
|
151
|
-
// We overwrite tree[n*2+1] which is no longer needed
|
|
152
|
-
|
|
153
|
-
if (n > that.max_code)
|
|
154
|
-
continue; // not a leaf node
|
|
155
|
-
|
|
156
|
-
s.bl_count[bits]++;
|
|
157
|
-
xbits = 0;
|
|
158
|
-
if (n >= base)
|
|
159
|
-
xbits = extra[n - base];
|
|
160
|
-
f = tree[n * 2];
|
|
161
|
-
s.opt_len += f * (bits + xbits);
|
|
162
|
-
if (stree)
|
|
163
|
-
s.static_len += f * (stree[n * 2 + 1] + xbits);
|
|
40
|
+
const MAX_BITS = 15;
|
|
41
|
+
const D_CODES = 30;
|
|
42
|
+
const BL_CODES = 19;
|
|
43
|
+
|
|
44
|
+
const LENGTH_CODES = 29;
|
|
45
|
+
const LITERALS = 256;
|
|
46
|
+
const L_CODES = (LITERALS + 1 + LENGTH_CODES);
|
|
47
|
+
const HEAP_SIZE = (2 * L_CODES + 1);
|
|
48
|
+
|
|
49
|
+
const END_BLOCK = 256;
|
|
50
|
+
|
|
51
|
+
// Bit length codes must not exceed MAX_BL_BITS bits
|
|
52
|
+
const MAX_BL_BITS = 7;
|
|
53
|
+
|
|
54
|
+
// repeat previous bit length 3-6 times (2 bits of repeat count)
|
|
55
|
+
const REP_3_6 = 16;
|
|
56
|
+
|
|
57
|
+
// repeat a zero length 3-10 times (3 bits of repeat count)
|
|
58
|
+
const REPZ_3_10 = 17;
|
|
59
|
+
|
|
60
|
+
// repeat a zero length 11-138 times (7 bits of repeat count)
|
|
61
|
+
const REPZ_11_138 = 18;
|
|
62
|
+
|
|
63
|
+
// The lengths of the bit length codes are sent in order of decreasing
|
|
64
|
+
// probability, to avoid transmitting the lengths for unused bit
|
|
65
|
+
// length codes.
|
|
66
|
+
|
|
67
|
+
const Buf_size = 8 * 2;
|
|
68
|
+
|
|
69
|
+
// JZlib version : "1.0.2"
|
|
70
|
+
const Z_DEFAULT_COMPRESSION = -1;
|
|
71
|
+
|
|
72
|
+
// compression strategy
|
|
73
|
+
const Z_FILTERED = 1;
|
|
74
|
+
const Z_HUFFMAN_ONLY = 2;
|
|
75
|
+
const Z_DEFAULT_STRATEGY = 0;
|
|
76
|
+
|
|
77
|
+
const Z_NO_FLUSH = 0;
|
|
78
|
+
const Z_PARTIAL_FLUSH = 1;
|
|
79
|
+
const Z_FULL_FLUSH = 3;
|
|
80
|
+
const Z_FINISH = 4;
|
|
81
|
+
|
|
82
|
+
const Z_OK = 0;
|
|
83
|
+
const Z_STREAM_END = 1;
|
|
84
|
+
const Z_NEED_DICT = 2;
|
|
85
|
+
const Z_STREAM_ERROR = -2;
|
|
86
|
+
const Z_DATA_ERROR = -3;
|
|
87
|
+
const Z_BUF_ERROR = -5;
|
|
88
|
+
|
|
89
|
+
// Tree
|
|
90
|
+
|
|
91
|
+
function extractArray(array) {
|
|
92
|
+
return flatArray(array.map(([length, value]) => (new Array(length)).fill(value, 0, length)));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function flatArray(array) {
|
|
96
|
+
return array.reduce((a, b) => a.concat(Array.isArray(b) ? flatArray(b) : b), []);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// see definition of array dist_code below
|
|
100
|
+
const _dist_code = [0, 1, 2, 3].concat(...extractArray([
|
|
101
|
+
[2, 4], [2, 5], [4, 6], [4, 7], [8, 8], [8, 9], [16, 10], [16, 11], [32, 12], [32, 13], [64, 14], [64, 15], [2, 0], [1, 16],
|
|
102
|
+
[1, 17], [2, 18], [2, 19], [4, 20], [4, 21], [8, 22], [8, 23], [16, 24], [16, 25], [32, 26], [32, 27], [64, 28], [64, 29]
|
|
103
|
+
]));
|
|
104
|
+
|
|
105
|
+
function Tree() {
|
|
106
|
+
const that = this;
|
|
107
|
+
|
|
108
|
+
// dyn_tree; // the dynamic tree
|
|
109
|
+
// max_code; // largest code with non zero frequency
|
|
110
|
+
// stat_desc; // the corresponding static tree
|
|
111
|
+
|
|
112
|
+
// Compute the optimal bit lengths for a tree and update the total bit
|
|
113
|
+
// length
|
|
114
|
+
// for the current block.
|
|
115
|
+
// IN assertion: the fields freq and dad are set, heap[heap_max] and
|
|
116
|
+
// above are the tree nodes sorted by increasing frequency.
|
|
117
|
+
// OUT assertions: the field len is set to the optimal bit length, the
|
|
118
|
+
// array bl_count contains the frequencies for each bit length.
|
|
119
|
+
// The length opt_len is updated; static_len is also updated if stree is
|
|
120
|
+
// not null.
|
|
121
|
+
function gen_bitlen(s) {
|
|
122
|
+
const tree = that.dyn_tree;
|
|
123
|
+
const stree = that.stat_desc.static_tree;
|
|
124
|
+
const extra = that.stat_desc.extra_bits;
|
|
125
|
+
const base = that.stat_desc.extra_base;
|
|
126
|
+
const max_length = that.stat_desc.max_length;
|
|
127
|
+
let h; // heap index
|
|
128
|
+
let n, m; // iterate over the tree elements
|
|
129
|
+
let bits; // bit length
|
|
130
|
+
let xbits; // extra bits
|
|
131
|
+
let f; // frequency
|
|
132
|
+
let overflow = 0; // number of elements with bit length too large
|
|
133
|
+
|
|
134
|
+
for (bits = 0; bits <= MAX_BITS; bits++)
|
|
135
|
+
s.bl_count[bits] = 0;
|
|
136
|
+
|
|
137
|
+
// In a first pass, compute the optimal bit lengths (which may
|
|
138
|
+
// overflow in the case of the bit length tree).
|
|
139
|
+
tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
|
|
140
|
+
|
|
141
|
+
for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
|
|
142
|
+
n = s.heap[h];
|
|
143
|
+
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
|
|
144
|
+
if (bits > max_length) {
|
|
145
|
+
bits = max_length;
|
|
146
|
+
overflow++;
|
|
164
147
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
148
|
+
tree[n * 2 + 1] = bits;
|
|
149
|
+
// We overwrite tree[n*2+1] which is no longer needed
|
|
150
|
+
|
|
151
|
+
if (n > that.max_code)
|
|
152
|
+
continue; // not a leaf node
|
|
153
|
+
|
|
154
|
+
s.bl_count[bits]++;
|
|
155
|
+
xbits = 0;
|
|
156
|
+
if (n >= base)
|
|
157
|
+
xbits = extra[n - base];
|
|
158
|
+
f = tree[n * 2];
|
|
159
|
+
s.opt_len += f * (bits + xbits);
|
|
160
|
+
if (stree)
|
|
161
|
+
s.static_len += f * (stree[n * 2 + 1] + xbits);
|
|
162
|
+
}
|
|
163
|
+
if (overflow === 0)
|
|
164
|
+
return;
|
|
165
|
+
|
|
166
|
+
// This happens for example on obj2 and pic of the Calgary corpus
|
|
167
|
+
// Find the first bit length which could increase:
|
|
168
|
+
do {
|
|
169
|
+
bits = max_length - 1;
|
|
170
|
+
while (s.bl_count[bits] === 0)
|
|
171
|
+
bits--;
|
|
172
|
+
s.bl_count[bits]--; // move one leaf down the tree
|
|
173
|
+
s.bl_count[bits + 1] += 2; // move one overflow item as its brother
|
|
174
|
+
s.bl_count[max_length]--;
|
|
175
|
+
// The brother of the overflow item also moves one step up,
|
|
176
|
+
// but this does not affect bl_count[max_length]
|
|
177
|
+
overflow -= 2;
|
|
178
|
+
} while (overflow > 0);
|
|
179
|
+
|
|
180
|
+
for (bits = max_length; bits !== 0; bits--) {
|
|
181
|
+
n = s.bl_count[bits];
|
|
182
|
+
while (n !== 0) {
|
|
183
|
+
m = s.heap[--h];
|
|
184
|
+
if (m > that.max_code)
|
|
185
|
+
continue;
|
|
186
|
+
if (tree[m * 2 + 1] != bits) {
|
|
187
|
+
s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
|
|
188
|
+
tree[m * 2 + 1] = bits;
|
|
193
189
|
}
|
|
190
|
+
n--;
|
|
194
191
|
}
|
|
195
192
|
}
|
|
193
|
+
}
|
|
196
194
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
195
|
+
// Reverse the first len bits of a code, using straightforward code (a
|
|
196
|
+
// faster
|
|
197
|
+
// method would use a table)
|
|
198
|
+
// IN assertion: 1 <= len <= 15
|
|
199
|
+
function bi_reverse(code, // the value to invert
|
|
200
|
+
len // its bit length
|
|
201
|
+
) {
|
|
202
|
+
let res = 0;
|
|
203
|
+
do {
|
|
204
|
+
res |= code & 1;
|
|
205
|
+
code >>>= 1;
|
|
206
|
+
res <<= 1;
|
|
207
|
+
} while (--len > 0);
|
|
208
|
+
return res >>> 1;
|
|
209
|
+
}
|
|
212
210
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
211
|
+
// Generate the codes for a given tree and bit counts (which need not be
|
|
212
|
+
// optimal).
|
|
213
|
+
// IN assertion: the array bl_count contains the bit length statistics for
|
|
214
|
+
// the given tree and the field len is set for all tree elements.
|
|
215
|
+
// OUT assertion: the field code is set for all tree elements of non
|
|
216
|
+
// zero code length.
|
|
217
|
+
function gen_codes(tree, // the tree to decorate
|
|
218
|
+
max_code, // largest code with non zero frequency
|
|
219
|
+
bl_count // number of codes at each bit length
|
|
220
|
+
) {
|
|
221
|
+
const next_code = []; // next code value for each
|
|
222
|
+
// bit length
|
|
223
|
+
let code = 0; // running code value
|
|
224
|
+
let bits; // bit index
|
|
225
|
+
let n; // code index
|
|
226
|
+
let len;
|
|
227
|
+
|
|
228
|
+
// The distribution counts are first used to generate the code values
|
|
229
|
+
// without bit reversal.
|
|
230
|
+
for (bits = 1; bits <= MAX_BITS; bits++) {
|
|
231
|
+
next_code[bits] = code = ((code + bl_count[bits - 1]) << 1);
|
|
232
|
+
}
|
|
235
233
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
234
|
+
// Check that the bit counts in bl_count are consistent. The last code
|
|
235
|
+
// must be all ones.
|
|
236
|
+
// Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
|
|
237
|
+
// "inconsistent bit counts");
|
|
238
|
+
// Tracev((stderr,"gen_codes: max_code %d ", max_code));
|
|
241
239
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
240
|
+
for (n = 0; n <= max_code; n++) {
|
|
241
|
+
len = tree[n * 2 + 1];
|
|
242
|
+
if (len === 0)
|
|
243
|
+
continue;
|
|
244
|
+
// Now reverse the bits
|
|
245
|
+
tree[n * 2] = bi_reverse(next_code[len]++, len);
|
|
249
246
|
}
|
|
247
|
+
}
|
|
250
248
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// The pkzip format requires that at least one distance code exists,
|
|
281
|
-
// and that at least one bit should be sent even if there is only one
|
|
282
|
-
// possible code. So to avoid special checks later on we force at least
|
|
283
|
-
// two codes of non zero frequency.
|
|
284
|
-
while (s.heap_len < 2) {
|
|
285
|
-
node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
|
|
286
|
-
tree[node * 2] = 1;
|
|
287
|
-
s.depth[node] = 0;
|
|
288
|
-
s.opt_len--;
|
|
289
|
-
if (stree)
|
|
290
|
-
s.static_len -= stree[node * 2 + 1];
|
|
291
|
-
// node is 0 or 1 so it does not have extra bits
|
|
249
|
+
// Construct one Huffman tree and assigns the code bit strings and lengths.
|
|
250
|
+
// Update the total bit length for the current block.
|
|
251
|
+
// IN assertion: the field freq is set for all tree elements.
|
|
252
|
+
// OUT assertions: the fields len and code are set to the optimal bit length
|
|
253
|
+
// and corresponding code. The length opt_len is updated; static_len is
|
|
254
|
+
// also updated if stree is not null. The field max_code is set.
|
|
255
|
+
that.build_tree = function (s) {
|
|
256
|
+
const tree = that.dyn_tree;
|
|
257
|
+
const stree = that.stat_desc.static_tree;
|
|
258
|
+
const elems = that.stat_desc.elems;
|
|
259
|
+
let n, m; // iterate over heap elements
|
|
260
|
+
let max_code = -1; // largest code with non zero frequency
|
|
261
|
+
let node; // new node being created
|
|
262
|
+
|
|
263
|
+
// Construct the initial heap, with least frequent element in
|
|
264
|
+
// heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
|
|
265
|
+
// heap[0] is not used.
|
|
266
|
+
s.heap_len = 0;
|
|
267
|
+
s.heap_max = HEAP_SIZE;
|
|
268
|
+
|
|
269
|
+
for (n = 0; n < elems; n++) {
|
|
270
|
+
if (tree[n * 2] !== 0) {
|
|
271
|
+
s.heap[++s.heap_len] = max_code = n;
|
|
272
|
+
s.depth[n] = 0;
|
|
273
|
+
} else {
|
|
274
|
+
tree[n * 2 + 1] = 0;
|
|
292
275
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
|
|
296
|
-
// establish sub-heaps of increasing lengths:
|
|
297
|
-
|
|
298
|
-
for (n = Math.floor(s.heap_len / 2); n >= 1; n--)
|
|
299
|
-
s.pqdownheap(tree, n);
|
|
300
|
-
|
|
301
|
-
// Construct the Huffman tree by repeatedly combining the least two
|
|
302
|
-
// frequent nodes.
|
|
303
|
-
|
|
304
|
-
node = elems; // next internal node of the tree
|
|
305
|
-
do {
|
|
306
|
-
// n = node of least frequency
|
|
307
|
-
n = s.heap[1];
|
|
308
|
-
s.heap[1] = s.heap[s.heap_len--];
|
|
309
|
-
s.pqdownheap(tree, 1);
|
|
310
|
-
m = s.heap[1]; // m = node of next least frequency
|
|
276
|
+
}
|
|
311
277
|
|
|
312
|
-
|
|
313
|
-
|
|
278
|
+
// The pkzip format requires that at least one distance code exists,
|
|
279
|
+
// and that at least one bit should be sent even if there is only one
|
|
280
|
+
// possible code. So to avoid special checks later on we force at least
|
|
281
|
+
// two codes of non zero frequency.
|
|
282
|
+
while (s.heap_len < 2) {
|
|
283
|
+
node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
|
|
284
|
+
tree[node * 2] = 1;
|
|
285
|
+
s.depth[node] = 0;
|
|
286
|
+
s.opt_len--;
|
|
287
|
+
if (stree)
|
|
288
|
+
s.static_len -= stree[node * 2 + 1];
|
|
289
|
+
// node is 0 or 1 so it does not have extra bits
|
|
290
|
+
}
|
|
291
|
+
that.max_code = max_code;
|
|
314
292
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
s.depth[node] = Math.max(s.depth[n], s.depth[m]) + 1;
|
|
318
|
-
tree[n * 2 + 1] = tree[m * 2 + 1] = node;
|
|
293
|
+
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
|
|
294
|
+
// establish sub-heaps of increasing lengths:
|
|
319
295
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
s.pqdownheap(tree, 1);
|
|
323
|
-
} while (s.heap_len >= 2);
|
|
296
|
+
for (n = Math.floor(s.heap_len / 2); n >= 1; n--)
|
|
297
|
+
s.pqdownheap(tree, n);
|
|
324
298
|
|
|
325
|
-
|
|
299
|
+
// Construct the Huffman tree by repeatedly combining the least two
|
|
300
|
+
// frequent nodes.
|
|
326
301
|
|
|
327
|
-
|
|
328
|
-
|
|
302
|
+
node = elems; // next internal node of the tree
|
|
303
|
+
do {
|
|
304
|
+
// n = node of least frequency
|
|
305
|
+
n = s.heap[1];
|
|
306
|
+
s.heap[1] = s.heap[s.heap_len--];
|
|
307
|
+
s.pqdownheap(tree, 1);
|
|
308
|
+
m = s.heap[1]; // m = node of next least frequency
|
|
329
309
|
|
|
330
|
-
|
|
310
|
+
s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
|
|
311
|
+
s.heap[--s.heap_max] = m;
|
|
331
312
|
|
|
332
|
-
//
|
|
333
|
-
|
|
334
|
-
|
|
313
|
+
// Create a new node father of n and m
|
|
314
|
+
tree[node * 2] = (tree[n * 2] + tree[m * 2]);
|
|
315
|
+
s.depth[node] = Math.max(s.depth[n], s.depth[m]) + 1;
|
|
316
|
+
tree[n * 2 + 1] = tree[m * 2 + 1] = node;
|
|
335
317
|
|
|
336
|
-
|
|
318
|
+
// and insert the new node in the heap
|
|
319
|
+
s.heap[1] = node++;
|
|
320
|
+
s.pqdownheap(tree, 1);
|
|
321
|
+
} while (s.heap_len >= 2);
|
|
337
322
|
|
|
338
|
-
|
|
339
|
-
[2, 8], [2, 9], [2, 10], [2, 11], [4, 12], [4, 13], [4, 14], [4, 15], [8, 16], [8, 17], [8, 18], [8, 19],
|
|
340
|
-
[16, 20], [16, 21], [16, 22], [16, 23], [32, 24], [32, 25], [32, 26], [31, 27], [1, 28]]));
|
|
323
|
+
s.heap[--s.heap_max] = s.heap[1];
|
|
341
324
|
|
|
342
|
-
|
|
325
|
+
// At this point, the fields freq and dad are set. We can now
|
|
326
|
+
// generate the bit lengths.
|
|
343
327
|
|
|
344
|
-
|
|
345
|
-
24576];
|
|
328
|
+
gen_bitlen(s);
|
|
346
329
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
// used.
|
|
350
|
-
Tree.d_code = function (dist) {
|
|
351
|
-
return ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >>> 7)]);
|
|
330
|
+
// The field len is now set, we can generate the bit codes
|
|
331
|
+
gen_codes(tree, that.max_code, s.bl_count);
|
|
352
332
|
};
|
|
353
333
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
Tree._length_code = [0, 1, 2, 3, 4, 5, 6, 7].concat(...extractArray([
|
|
337
|
+
[2, 8], [2, 9], [2, 10], [2, 11], [4, 12], [4, 13], [4, 14], [4, 15], [8, 16], [8, 17], [8, 18], [8, 19],
|
|
338
|
+
[16, 20], [16, 21], [16, 22], [16, 23], [32, 24], [32, 25], [32, 26], [31, 27], [1, 28]]));
|
|
339
|
+
|
|
340
|
+
Tree.base_length = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0];
|
|
341
|
+
|
|
342
|
+
Tree.base_dist = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384,
|
|
343
|
+
24576];
|
|
344
|
+
|
|
345
|
+
// Mapping from a distance to a distance code. dist is the distance - 1 and
|
|
346
|
+
// must not have side effects. _dist_code[256] and _dist_code[257] are never
|
|
347
|
+
// used.
|
|
348
|
+
Tree.d_code = function (dist) {
|
|
349
|
+
return ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >>> 7)]);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// extra bits for each length code
|
|
353
|
+
Tree.extra_lbits = [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];
|
|
354
|
+
|
|
355
|
+
// extra bits for each distance code
|
|
356
|
+
Tree.extra_dbits = [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];
|
|
357
|
+
|
|
358
|
+
// extra bits for each bit length code
|
|
359
|
+
Tree.extra_blbits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7];
|
|
360
|
+
|
|
361
|
+
Tree.bl_order = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
362
|
+
|
|
363
|
+
// StaticTree
|
|
364
|
+
|
|
365
|
+
function StaticTree(static_tree, extra_bits, extra_base, elems, max_length) {
|
|
366
|
+
const that = this;
|
|
367
|
+
that.static_tree = static_tree;
|
|
368
|
+
that.extra_bits = extra_bits;
|
|
369
|
+
that.extra_base = extra_base;
|
|
370
|
+
that.elems = elems;
|
|
371
|
+
that.max_length = max_length;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const static_ltree2_first_part = [12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82,
|
|
375
|
+
210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86,
|
|
376
|
+
214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81,
|
|
377
|
+
209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85,
|
|
378
|
+
213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 19, 275, 147, 403, 83, 339, 211, 467, 51, 307,
|
|
379
|
+
179, 435, 115, 371, 243, 499, 11, 267, 139, 395, 75, 331, 203, 459, 43, 299, 171, 427, 107, 363, 235, 491, 27, 283, 155, 411, 91, 347, 219, 475,
|
|
380
|
+
59, 315, 187, 443, 123, 379, 251, 507, 7, 263, 135, 391, 71, 327, 199, 455, 39, 295, 167, 423, 103, 359, 231, 487, 23, 279, 151, 407, 87, 343, 215,
|
|
381
|
+
471, 55, 311, 183, 439, 119, 375, 247, 503, 15, 271, 143, 399, 79, 335, 207, 463, 47, 303, 175, 431, 111, 367, 239, 495, 31, 287, 159, 415, 95,
|
|
382
|
+
351, 223, 479, 63, 319, 191, 447, 127, 383, 255, 511, 0, 64, 32, 96, 16, 80, 48, 112, 8, 72, 40, 104, 24, 88, 56, 120, 4, 68, 36, 100, 20, 84, 52,
|
|
383
|
+
116, 3, 131, 67, 195, 35, 163, 99, 227];
|
|
384
|
+
const static_ltree2_second_part = extractArray([[144, 8], [112, 9], [24, 7], [8, 8]]);
|
|
385
|
+
StaticTree.static_ltree = flatArray(static_ltree2_first_part.map((value, index) => [value, static_ltree2_second_part[index]]));
|
|
386
|
+
|
|
387
|
+
const static_dtree_first_part = [0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30, 1, 17, 9, 25, 5, 21, 13, 29, 3, 19, 11, 27, 7, 23];
|
|
388
|
+
const static_dtree_second_part = extractArray([[30, 5]]);
|
|
389
|
+
StaticTree.static_dtree = flatArray(static_dtree_first_part.map((value, index) => [value, static_dtree_second_part[index]]));
|
|
390
|
+
|
|
391
|
+
StaticTree.static_l_desc = new StaticTree(StaticTree.static_ltree, Tree.extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
|
|
392
|
+
|
|
393
|
+
StaticTree.static_d_desc = new StaticTree(StaticTree.static_dtree, Tree.extra_dbits, 0, D_CODES, MAX_BITS);
|
|
394
|
+
|
|
395
|
+
StaticTree.static_bl_desc = new StaticTree(null, Tree.extra_blbits, 0, BL_CODES, MAX_BL_BITS);
|
|
396
|
+
|
|
397
|
+
// Deflate
|
|
398
|
+
|
|
399
|
+
const MAX_MEM_LEVEL = 9;
|
|
400
|
+
const DEF_MEM_LEVEL = 8;
|
|
401
|
+
|
|
402
|
+
function Config(good_length, max_lazy, nice_length, max_chain, func) {
|
|
403
|
+
const that = this;
|
|
404
|
+
that.good_length = good_length;
|
|
405
|
+
that.max_lazy = max_lazy;
|
|
406
|
+
that.nice_length = nice_length;
|
|
407
|
+
that.max_chain = max_chain;
|
|
408
|
+
that.func = func;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const STORED = 0;
|
|
412
|
+
const FAST = 1;
|
|
413
|
+
const SLOW = 2;
|
|
414
|
+
const config_table = [
|
|
415
|
+
new Config(0, 0, 0, 0, STORED),
|
|
416
|
+
new Config(4, 4, 8, 4, FAST),
|
|
417
|
+
new Config(4, 5, 16, 8, FAST),
|
|
418
|
+
new Config(4, 6, 32, 32, FAST),
|
|
419
|
+
new Config(4, 4, 16, 16, SLOW),
|
|
420
|
+
new Config(8, 16, 32, 32, SLOW),
|
|
421
|
+
new Config(8, 16, 128, 128, SLOW),
|
|
422
|
+
new Config(8, 32, 128, 256, SLOW),
|
|
423
|
+
new Config(32, 128, 258, 1024, SLOW),
|
|
424
|
+
new Config(32, 258, 258, 4096, SLOW)
|
|
425
|
+
];
|
|
426
|
+
|
|
427
|
+
const z_errmsg = ["need dictionary", // Z_NEED_DICT
|
|
428
|
+
// 2
|
|
429
|
+
"stream end", // Z_STREAM_END 1
|
|
430
|
+
"", // Z_OK 0
|
|
431
|
+
"", // Z_ERRNO (-1)
|
|
432
|
+
"stream error", // Z_STREAM_ERROR (-2)
|
|
433
|
+
"data error", // Z_DATA_ERROR (-3)
|
|
434
|
+
"", // Z_MEM_ERROR (-4)
|
|
435
|
+
"buffer error", // Z_BUF_ERROR (-5)
|
|
436
|
+
"",// Z_VERSION_ERROR (-6)
|
|
437
|
+
""];
|
|
438
|
+
|
|
439
|
+
// block not completed, need more input or more output
|
|
440
|
+
const NeedMore = 0;
|
|
441
|
+
|
|
442
|
+
// block flush performed
|
|
443
|
+
const BlockDone = 1;
|
|
444
|
+
|
|
445
|
+
// finish started, need only more output at next deflate
|
|
446
|
+
const FinishStarted = 2;
|
|
447
|
+
|
|
448
|
+
// finish done, accept no more input or output
|
|
449
|
+
const FinishDone = 3;
|
|
450
|
+
|
|
451
|
+
// preset dictionary flag in zlib header
|
|
452
|
+
const PRESET_DICT = 0x20;
|
|
453
|
+
|
|
454
|
+
const INIT_STATE = 42;
|
|
455
|
+
const BUSY_STATE = 113;
|
|
456
|
+
const FINISH_STATE = 666;
|
|
457
|
+
|
|
458
|
+
// The deflate compression method
|
|
459
|
+
const Z_DEFLATED = 8;
|
|
460
|
+
|
|
461
|
+
const STORED_BLOCK = 0;
|
|
462
|
+
const STATIC_TREES = 1;
|
|
463
|
+
const DYN_TREES = 2;
|
|
464
|
+
|
|
465
|
+
const MIN_MATCH = 3;
|
|
466
|
+
const MAX_MATCH = 258;
|
|
467
|
+
const MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
|
|
468
|
+
|
|
469
|
+
function smaller(tree, n, m, depth) {
|
|
470
|
+
const tn2 = tree[n * 2];
|
|
471
|
+
const tm2 = tree[m * 2];
|
|
472
|
+
return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function Deflate() {
|
|
476
|
+
|
|
477
|
+
const that = this;
|
|
478
|
+
let strm; // pointer back to this zlib stream
|
|
479
|
+
let status; // as the name implies
|
|
480
|
+
// pending_buf; // output still pending
|
|
481
|
+
let pending_buf_size; // size of pending_buf
|
|
482
|
+
// pending_out; // next pending byte to output to the stream
|
|
483
|
+
// pending; // nb of bytes in the pending buffer
|
|
484
|
+
|
|
485
|
+
// dist_buf; // buffer for distances
|
|
486
|
+
// lc_buf; // buffer for literals or lengths
|
|
487
|
+
// To simplify the code, dist_buf and lc_buf have the same number of elements.
|
|
488
|
+
// To use different lengths, an extra flag array would be necessary.
|
|
489
|
+
|
|
490
|
+
let last_flush; // value of flush param for previous deflate call
|
|
491
|
+
|
|
492
|
+
let w_size; // LZ77 win size (32K by default)
|
|
493
|
+
let w_bits; // log2(w_size) (8..16)
|
|
494
|
+
let w_mask; // w_size - 1
|
|
495
|
+
|
|
496
|
+
let win;
|
|
497
|
+
// Sliding win. Input bytes are read into the second half of the win,
|
|
498
|
+
// and move to the first half later to keep a dictionary of at least wSize
|
|
499
|
+
// bytes. With this organization, matches are limited to a distance of
|
|
500
|
+
// wSize-MAX_MATCH bytes, but this ensures that IO is always
|
|
501
|
+
// performed with a length multiple of the block size. Also, it limits
|
|
502
|
+
// the win size to 64K, which is quite useful on MSDOS.
|
|
503
|
+
// To do: use the user input buffer as sliding win.
|
|
504
|
+
|
|
505
|
+
let window_size;
|
|
506
|
+
// Actual size of win: 2*wSize, except when the user input buffer
|
|
507
|
+
// is directly used as sliding win.
|
|
508
|
+
|
|
509
|
+
let prev;
|
|
510
|
+
// Link to older string with same hash index. To limit the size of this
|
|
511
|
+
// array to 64K, this link is maintained only for the last 32K strings.
|
|
512
|
+
// An index in this array is thus a win index modulo 32K.
|
|
513
|
+
|
|
514
|
+
let head; // Heads of the hash chains or NIL.
|
|
515
|
+
|
|
516
|
+
let ins_h; // hash index of string to be inserted
|
|
517
|
+
let hash_size; // number of elements in hash table
|
|
518
|
+
let hash_bits; // log2(hash_size)
|
|
519
|
+
let hash_mask; // hash_size-1
|
|
520
|
+
|
|
521
|
+
// Number of bits by which ins_h must be shifted at each input
|
|
522
|
+
// step. It must be such that after MIN_MATCH steps, the oldest
|
|
523
|
+
// byte no longer takes part in the hash key, that is:
|
|
524
|
+
// hash_shift * MIN_MATCH >= hash_bits
|
|
525
|
+
let hash_shift;
|
|
526
|
+
|
|
527
|
+
// Window position at the beginning of the current output block. Gets
|
|
528
|
+
// negative when the win is moved backwards.
|
|
529
|
+
|
|
530
|
+
let block_start;
|
|
531
|
+
|
|
532
|
+
let match_length; // length of best match
|
|
533
|
+
let prev_match; // previous match
|
|
534
|
+
let match_available; // set if previous match exists
|
|
535
|
+
let strstart; // start of string to insert
|
|
536
|
+
let match_start; // start of matching string
|
|
537
|
+
let lookahead; // number of valid bytes ahead in win
|
|
538
|
+
|
|
539
|
+
// Length of the best match at previous step. Matches not greater than this
|
|
540
|
+
// are discarded. This is used in the lazy match evaluation.
|
|
541
|
+
let prev_length;
|
|
542
|
+
|
|
543
|
+
// To speed up deflation, hash chains are never searched beyond this
|
|
544
|
+
// length. A higher limit improves compression ratio but degrades the speed.
|
|
545
|
+
let max_chain_length;
|
|
546
|
+
|
|
547
|
+
// Attempt to find a better match only when the current match is strictly
|
|
548
|
+
// smaller than this value. This mechanism is used only for compression
|
|
549
|
+
// levels >= 4.
|
|
550
|
+
let max_lazy_match;
|
|
551
|
+
|
|
552
|
+
// Insert new strings in the hash table only if the match length is not
|
|
553
|
+
// greater than this length. This saves time but degrades compression.
|
|
554
|
+
// max_insert_length is used only for compression levels <= 3.
|
|
555
|
+
|
|
556
|
+
let level; // compression level (1..9)
|
|
557
|
+
let strategy; // favor or force Huffman coding
|
|
558
|
+
|
|
559
|
+
// Use a faster search when the previous match is longer than this
|
|
560
|
+
let good_match;
|
|
561
|
+
|
|
562
|
+
// Stop searching when current match exceeds this
|
|
563
|
+
let nice_match;
|
|
564
|
+
|
|
565
|
+
let dyn_ltree; // literal and length tree
|
|
566
|
+
let dyn_dtree; // distance tree
|
|
567
|
+
let bl_tree; // Huffman tree for bit lengths
|
|
568
|
+
|
|
569
|
+
const l_desc = new Tree(); // desc for literal tree
|
|
570
|
+
const d_desc = new Tree(); // desc for distance tree
|
|
571
|
+
const bl_desc = new Tree(); // desc for bit length tree
|
|
572
|
+
|
|
573
|
+
// that.heap_len; // number of elements in the heap
|
|
574
|
+
// that.heap_max; // element of largest frequency
|
|
575
|
+
// The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
|
|
576
|
+
// The same heap array is used to build all trees.
|
|
577
|
+
|
|
578
|
+
// Depth of each subtree used as tie breaker for trees of equal frequency
|
|
579
|
+
that.depth = [];
|
|
580
|
+
|
|
581
|
+
// Size of match buffer for literals/lengths. There are 4 reasons for
|
|
582
|
+
// limiting lit_bufsize to 64K:
|
|
583
|
+
// - frequencies can be kept in 16 bit counters
|
|
584
|
+
// - if compression is not successful for the first block, all input
|
|
585
|
+
// data is still in the win so we can still emit a stored block even
|
|
586
|
+
// when input comes from standard input. (This can also be done for
|
|
587
|
+
// all blocks if lit_bufsize is not greater than 32K.)
|
|
588
|
+
// - if compression is not successful for a file smaller than 64K, we can
|
|
589
|
+
// even emit a stored file instead of a stored block (saving 5 bytes).
|
|
590
|
+
// This is applicable only for zip (not gzip or zlib).
|
|
591
|
+
// - creating new Huffman trees less frequently may not provide fast
|
|
592
|
+
// adaptation to changes in the input data statistics. (Take for
|
|
593
|
+
// example a binary file with poorly compressible code followed by
|
|
594
|
+
// a highly compressible string table.) Smaller buffer sizes give
|
|
595
|
+
// fast adaptation but have of course the overhead of transmitting
|
|
596
|
+
// trees more frequently.
|
|
597
|
+
// - I can't count above 4
|
|
598
|
+
let lit_bufsize;
|
|
599
|
+
|
|
600
|
+
let last_lit; // running index in dist_buf and lc_buf
|
|
601
|
+
|
|
602
|
+
// that.opt_len; // bit length of current block with optimal trees
|
|
603
|
+
// that.static_len; // bit length of current block with static trees
|
|
604
|
+
let matches; // number of string matches in current block
|
|
605
|
+
let last_eob_len; // bit length of EOB code for last block
|
|
606
|
+
|
|
607
|
+
// Output buffer. bits are inserted starting at the bottom (least
|
|
608
|
+
// significant bits).
|
|
609
|
+
let bi_buf;
|
|
610
|
+
|
|
611
|
+
// Number of valid bits in bi_buf. All bits above the last valid bit
|
|
612
|
+
// are always zero.
|
|
613
|
+
let bi_valid;
|
|
614
|
+
|
|
615
|
+
// number of codes at each bit length for an optimal tree
|
|
616
|
+
that.bl_count = [];
|
|
617
|
+
|
|
618
|
+
// heap used to build the Huffman trees
|
|
619
|
+
that.heap = [];
|
|
620
|
+
|
|
621
|
+
dyn_ltree = [];
|
|
622
|
+
dyn_dtree = [];
|
|
623
|
+
bl_tree = [];
|
|
624
|
+
|
|
625
|
+
function lm_init() {
|
|
626
|
+
window_size = 2 * w_size;
|
|
627
|
+
|
|
628
|
+
head[hash_size - 1] = 0;
|
|
629
|
+
for (let i = 0; i < hash_size - 1; i++) {
|
|
630
|
+
head[i] = 0;
|
|
631
|
+
}
|
|
366
632
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
633
|
+
// Set the default configuration parameters:
|
|
634
|
+
max_lazy_match = config_table[level].max_lazy;
|
|
635
|
+
good_match = config_table[level].good_length;
|
|
636
|
+
nice_match = config_table[level].nice_length;
|
|
637
|
+
max_chain_length = config_table[level].max_chain;
|
|
638
|
+
|
|
639
|
+
strstart = 0;
|
|
640
|
+
block_start = 0;
|
|
641
|
+
lookahead = 0;
|
|
642
|
+
match_length = prev_length = MIN_MATCH - 1;
|
|
643
|
+
match_available = 0;
|
|
644
|
+
ins_h = 0;
|
|
374
645
|
}
|
|
375
646
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
const static_dtree_second_part = extractArray([[30, 5]]);
|
|
391
|
-
StaticTree.static_dtree = flatArray(static_dtree_first_part.map((value, index) => [value, static_dtree_second_part[index]]));
|
|
392
|
-
|
|
393
|
-
StaticTree.static_l_desc = new StaticTree(StaticTree.static_ltree, Tree.extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
|
|
647
|
+
function init_block() {
|
|
648
|
+
let i;
|
|
649
|
+
// Initialize the trees.
|
|
650
|
+
for (i = 0; i < L_CODES; i++)
|
|
651
|
+
dyn_ltree[i * 2] = 0;
|
|
652
|
+
for (i = 0; i < D_CODES; i++)
|
|
653
|
+
dyn_dtree[i * 2] = 0;
|
|
654
|
+
for (i = 0; i < BL_CODES; i++)
|
|
655
|
+
bl_tree[i * 2] = 0;
|
|
656
|
+
|
|
657
|
+
dyn_ltree[END_BLOCK * 2] = 1;
|
|
658
|
+
that.opt_len = that.static_len = 0;
|
|
659
|
+
last_lit = matches = 0;
|
|
660
|
+
}
|
|
394
661
|
|
|
395
|
-
|
|
662
|
+
// Initialize the tree data structures for a new zlib stream.
|
|
663
|
+
function tr_init() {
|
|
396
664
|
|
|
397
|
-
|
|
665
|
+
l_desc.dyn_tree = dyn_ltree;
|
|
666
|
+
l_desc.stat_desc = StaticTree.static_l_desc;
|
|
398
667
|
|
|
399
|
-
|
|
668
|
+
d_desc.dyn_tree = dyn_dtree;
|
|
669
|
+
d_desc.stat_desc = StaticTree.static_d_desc;
|
|
400
670
|
|
|
401
|
-
|
|
402
|
-
|
|
671
|
+
bl_desc.dyn_tree = bl_tree;
|
|
672
|
+
bl_desc.stat_desc = StaticTree.static_bl_desc;
|
|
403
673
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
that.max_lazy = max_lazy;
|
|
408
|
-
that.nice_length = nice_length;
|
|
409
|
-
that.max_chain = max_chain;
|
|
410
|
-
that.func = func;
|
|
411
|
-
}
|
|
674
|
+
bi_buf = 0;
|
|
675
|
+
bi_valid = 0;
|
|
676
|
+
last_eob_len = 8; // enough lookahead for inflate
|
|
412
677
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const SLOW = 2;
|
|
416
|
-
const config_table = [
|
|
417
|
-
new Config(0, 0, 0, 0, STORED),
|
|
418
|
-
new Config(4, 4, 8, 4, FAST),
|
|
419
|
-
new Config(4, 5, 16, 8, FAST),
|
|
420
|
-
new Config(4, 6, 32, 32, FAST),
|
|
421
|
-
new Config(4, 4, 16, 16, SLOW),
|
|
422
|
-
new Config(8, 16, 32, 32, SLOW),
|
|
423
|
-
new Config(8, 16, 128, 128, SLOW),
|
|
424
|
-
new Config(8, 32, 128, 256, SLOW),
|
|
425
|
-
new Config(32, 128, 258, 1024, SLOW),
|
|
426
|
-
new Config(32, 258, 258, 4096, SLOW)
|
|
427
|
-
];
|
|
428
|
-
|
|
429
|
-
const z_errmsg = ["need dictionary", // Z_NEED_DICT
|
|
430
|
-
// 2
|
|
431
|
-
"stream end", // Z_STREAM_END 1
|
|
432
|
-
"", // Z_OK 0
|
|
433
|
-
"", // Z_ERRNO (-1)
|
|
434
|
-
"stream error", // Z_STREAM_ERROR (-2)
|
|
435
|
-
"data error", // Z_DATA_ERROR (-3)
|
|
436
|
-
"", // Z_MEM_ERROR (-4)
|
|
437
|
-
"buffer error", // Z_BUF_ERROR (-5)
|
|
438
|
-
"",// Z_VERSION_ERROR (-6)
|
|
439
|
-
""];
|
|
440
|
-
|
|
441
|
-
// block not completed, need more input or more output
|
|
442
|
-
const NeedMore = 0;
|
|
443
|
-
|
|
444
|
-
// block flush performed
|
|
445
|
-
const BlockDone = 1;
|
|
446
|
-
|
|
447
|
-
// finish started, need only more output at next deflate
|
|
448
|
-
const FinishStarted = 2;
|
|
449
|
-
|
|
450
|
-
// finish done, accept no more input or output
|
|
451
|
-
const FinishDone = 3;
|
|
452
|
-
|
|
453
|
-
// preset dictionary flag in zlib header
|
|
454
|
-
const PRESET_DICT = 0x20;
|
|
455
|
-
|
|
456
|
-
const INIT_STATE = 42;
|
|
457
|
-
const BUSY_STATE = 113;
|
|
458
|
-
const FINISH_STATE = 666;
|
|
459
|
-
|
|
460
|
-
// The deflate compression method
|
|
461
|
-
const Z_DEFLATED = 8;
|
|
462
|
-
|
|
463
|
-
const STORED_BLOCK = 0;
|
|
464
|
-
const STATIC_TREES = 1;
|
|
465
|
-
const DYN_TREES = 2;
|
|
466
|
-
|
|
467
|
-
const MIN_MATCH = 3;
|
|
468
|
-
const MAX_MATCH = 258;
|
|
469
|
-
const MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
|
|
470
|
-
|
|
471
|
-
function smaller(tree, n, m, depth) {
|
|
472
|
-
const tn2 = tree[n * 2];
|
|
473
|
-
const tm2 = tree[m * 2];
|
|
474
|
-
return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
|
|
678
|
+
// Initialize the first block of the first file:
|
|
679
|
+
init_block();
|
|
475
680
|
}
|
|
476
681
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
let last_flush; // value of flush param for previous deflate call
|
|
493
|
-
|
|
494
|
-
let w_size; // LZ77 win size (32K by default)
|
|
495
|
-
let w_bits; // log2(w_size) (8..16)
|
|
496
|
-
let w_mask; // w_size - 1
|
|
497
|
-
|
|
498
|
-
let win;
|
|
499
|
-
// Sliding win. Input bytes are read into the second half of the win,
|
|
500
|
-
// and move to the first half later to keep a dictionary of at least wSize
|
|
501
|
-
// bytes. With this organization, matches are limited to a distance of
|
|
502
|
-
// wSize-MAX_MATCH bytes, but this ensures that IO is always
|
|
503
|
-
// performed with a length multiple of the block size. Also, it limits
|
|
504
|
-
// the win size to 64K, which is quite useful on MSDOS.
|
|
505
|
-
// To do: use the user input buffer as sliding win.
|
|
506
|
-
|
|
507
|
-
let window_size;
|
|
508
|
-
// Actual size of win: 2*wSize, except when the user input buffer
|
|
509
|
-
// is directly used as sliding win.
|
|
510
|
-
|
|
511
|
-
let prev;
|
|
512
|
-
// Link to older string with same hash index. To limit the size of this
|
|
513
|
-
// array to 64K, this link is maintained only for the last 32K strings.
|
|
514
|
-
// An index in this array is thus a win index modulo 32K.
|
|
515
|
-
|
|
516
|
-
let head; // Heads of the hash chains or NIL.
|
|
517
|
-
|
|
518
|
-
let ins_h; // hash index of string to be inserted
|
|
519
|
-
let hash_size; // number of elements in hash table
|
|
520
|
-
let hash_bits; // log2(hash_size)
|
|
521
|
-
let hash_mask; // hash_size-1
|
|
522
|
-
|
|
523
|
-
// Number of bits by which ins_h must be shifted at each input
|
|
524
|
-
// step. It must be such that after MIN_MATCH steps, the oldest
|
|
525
|
-
// byte no longer takes part in the hash key, that is:
|
|
526
|
-
// hash_shift * MIN_MATCH >= hash_bits
|
|
527
|
-
let hash_shift;
|
|
528
|
-
|
|
529
|
-
// Window position at the beginning of the current output block. Gets
|
|
530
|
-
// negative when the win is moved backwards.
|
|
531
|
-
|
|
532
|
-
let block_start;
|
|
533
|
-
|
|
534
|
-
let match_length; // length of best match
|
|
535
|
-
let prev_match; // previous match
|
|
536
|
-
let match_available; // set if previous match exists
|
|
537
|
-
let strstart; // start of string to insert
|
|
538
|
-
let match_start; // start of matching string
|
|
539
|
-
let lookahead; // number of valid bytes ahead in win
|
|
540
|
-
|
|
541
|
-
// Length of the best match at previous step. Matches not greater than this
|
|
542
|
-
// are discarded. This is used in the lazy match evaluation.
|
|
543
|
-
let prev_length;
|
|
544
|
-
|
|
545
|
-
// To speed up deflation, hash chains are never searched beyond this
|
|
546
|
-
// length. A higher limit improves compression ratio but degrades the speed.
|
|
547
|
-
let max_chain_length;
|
|
548
|
-
|
|
549
|
-
// Attempt to find a better match only when the current match is strictly
|
|
550
|
-
// smaller than this value. This mechanism is used only for compression
|
|
551
|
-
// levels >= 4.
|
|
552
|
-
let max_lazy_match;
|
|
553
|
-
|
|
554
|
-
// Insert new strings in the hash table only if the match length is not
|
|
555
|
-
// greater than this length. This saves time but degrades compression.
|
|
556
|
-
// max_insert_length is used only for compression levels <= 3.
|
|
557
|
-
|
|
558
|
-
let level; // compression level (1..9)
|
|
559
|
-
let strategy; // favor or force Huffman coding
|
|
560
|
-
|
|
561
|
-
// Use a faster search when the previous match is longer than this
|
|
562
|
-
let good_match;
|
|
563
|
-
|
|
564
|
-
// Stop searching when current match exceeds this
|
|
565
|
-
let nice_match;
|
|
566
|
-
|
|
567
|
-
let dyn_ltree; // literal and length tree
|
|
568
|
-
let dyn_dtree; // distance tree
|
|
569
|
-
let bl_tree; // Huffman tree for bit lengths
|
|
570
|
-
|
|
571
|
-
const l_desc = new Tree(); // desc for literal tree
|
|
572
|
-
const d_desc = new Tree(); // desc for distance tree
|
|
573
|
-
const bl_desc = new Tree(); // desc for bit length tree
|
|
574
|
-
|
|
575
|
-
// that.heap_len; // number of elements in the heap
|
|
576
|
-
// that.heap_max; // element of largest frequency
|
|
577
|
-
// The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
|
|
578
|
-
// The same heap array is used to build all trees.
|
|
579
|
-
|
|
580
|
-
// Depth of each subtree used as tie breaker for trees of equal frequency
|
|
581
|
-
that.depth = [];
|
|
582
|
-
|
|
583
|
-
// Size of match buffer for literals/lengths. There are 4 reasons for
|
|
584
|
-
// limiting lit_bufsize to 64K:
|
|
585
|
-
// - frequencies can be kept in 16 bit counters
|
|
586
|
-
// - if compression is not successful for the first block, all input
|
|
587
|
-
// data is still in the win so we can still emit a stored block even
|
|
588
|
-
// when input comes from standard input. (This can also be done for
|
|
589
|
-
// all blocks if lit_bufsize is not greater than 32K.)
|
|
590
|
-
// - if compression is not successful for a file smaller than 64K, we can
|
|
591
|
-
// even emit a stored file instead of a stored block (saving 5 bytes).
|
|
592
|
-
// This is applicable only for zip (not gzip or zlib).
|
|
593
|
-
// - creating new Huffman trees less frequently may not provide fast
|
|
594
|
-
// adaptation to changes in the input data statistics. (Take for
|
|
595
|
-
// example a binary file with poorly compressible code followed by
|
|
596
|
-
// a highly compressible string table.) Smaller buffer sizes give
|
|
597
|
-
// fast adaptation but have of course the overhead of transmitting
|
|
598
|
-
// trees more frequently.
|
|
599
|
-
// - I can't count above 4
|
|
600
|
-
let lit_bufsize;
|
|
601
|
-
|
|
602
|
-
let last_lit; // running index in dist_buf and lc_buf
|
|
603
|
-
|
|
604
|
-
// that.opt_len; // bit length of current block with optimal trees
|
|
605
|
-
// that.static_len; // bit length of current block with static trees
|
|
606
|
-
let matches; // number of string matches in current block
|
|
607
|
-
let last_eob_len; // bit length of EOB code for last block
|
|
608
|
-
|
|
609
|
-
// Output buffer. bits are inserted starting at the bottom (least
|
|
610
|
-
// significant bits).
|
|
611
|
-
let bi_buf;
|
|
612
|
-
|
|
613
|
-
// Number of valid bits in bi_buf. All bits above the last valid bit
|
|
614
|
-
// are always zero.
|
|
615
|
-
let bi_valid;
|
|
616
|
-
|
|
617
|
-
// number of codes at each bit length for an optimal tree
|
|
618
|
-
that.bl_count = [];
|
|
619
|
-
|
|
620
|
-
// heap used to build the Huffman trees
|
|
621
|
-
that.heap = [];
|
|
622
|
-
|
|
623
|
-
dyn_ltree = [];
|
|
624
|
-
dyn_dtree = [];
|
|
625
|
-
bl_tree = [];
|
|
626
|
-
|
|
627
|
-
function lm_init() {
|
|
628
|
-
window_size = 2 * w_size;
|
|
629
|
-
|
|
630
|
-
head[hash_size - 1] = 0;
|
|
631
|
-
for (let i = 0; i < hash_size - 1; i++) {
|
|
632
|
-
head[i] = 0;
|
|
682
|
+
// Restore the heap property by moving down the tree starting at node k,
|
|
683
|
+
// exchanging a node with the smallest of its two sons if necessary,
|
|
684
|
+
// stopping
|
|
685
|
+
// when the heap property is re-established (each father smaller than its
|
|
686
|
+
// two sons).
|
|
687
|
+
that.pqdownheap = function (tree, // the tree to restore
|
|
688
|
+
k // node to move down
|
|
689
|
+
) {
|
|
690
|
+
const heap = that.heap;
|
|
691
|
+
const v = heap[k];
|
|
692
|
+
let j = k << 1; // left son of k
|
|
693
|
+
while (j <= that.heap_len) {
|
|
694
|
+
// Set j to the smallest of the two sons:
|
|
695
|
+
if (j < that.heap_len && smaller(tree, heap[j + 1], heap[j], that.depth)) {
|
|
696
|
+
j++;
|
|
633
697
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
lookahead = 0;
|
|
644
|
-
match_length = prev_length = MIN_MATCH - 1;
|
|
645
|
-
match_available = 0;
|
|
646
|
-
ins_h = 0;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
function init_block() {
|
|
650
|
-
let i;
|
|
651
|
-
// Initialize the trees.
|
|
652
|
-
for (i = 0; i < L_CODES; i++)
|
|
653
|
-
dyn_ltree[i * 2] = 0;
|
|
654
|
-
for (i = 0; i < D_CODES; i++)
|
|
655
|
-
dyn_dtree[i * 2] = 0;
|
|
656
|
-
for (i = 0; i < BL_CODES; i++)
|
|
657
|
-
bl_tree[i * 2] = 0;
|
|
658
|
-
|
|
659
|
-
dyn_ltree[END_BLOCK * 2] = 1;
|
|
660
|
-
that.opt_len = that.static_len = 0;
|
|
661
|
-
last_lit = matches = 0;
|
|
698
|
+
// Exit if v is smaller than both sons
|
|
699
|
+
if (smaller(tree, v, heap[j], that.depth))
|
|
700
|
+
break;
|
|
701
|
+
|
|
702
|
+
// Exchange v with the smallest son
|
|
703
|
+
heap[k] = heap[j];
|
|
704
|
+
k = j;
|
|
705
|
+
// And continue down the tree, setting j to the left son of k
|
|
706
|
+
j <<= 1;
|
|
662
707
|
}
|
|
708
|
+
heap[k] = v;
|
|
709
|
+
};
|
|
663
710
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
// Initialize the first block of the first file:
|
|
681
|
-
init_block();
|
|
711
|
+
// Scan a literal or distance tree to determine the frequencies of the codes
|
|
712
|
+
// in the bit length tree.
|
|
713
|
+
function scan_tree(tree,// the tree to be scanned
|
|
714
|
+
max_code // and its largest code of non zero frequency
|
|
715
|
+
) {
|
|
716
|
+
let prevlen = -1; // last emitted length
|
|
717
|
+
let curlen; // length of current code
|
|
718
|
+
let nextlen = tree[0 * 2 + 1]; // length of next code
|
|
719
|
+
let count = 0; // repeat count of the current code
|
|
720
|
+
let max_count = 7; // max repeat count
|
|
721
|
+
let min_count = 4; // min repeat count
|
|
722
|
+
|
|
723
|
+
if (nextlen === 0) {
|
|
724
|
+
max_count = 138;
|
|
725
|
+
min_count = 3;
|
|
682
726
|
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
// Exit if v is smaller than both sons
|
|
701
|
-
if (smaller(tree, v, heap[j], that.depth))
|
|
702
|
-
break;
|
|
703
|
-
|
|
704
|
-
// Exchange v with the smallest son
|
|
705
|
-
heap[k] = heap[j];
|
|
706
|
-
k = j;
|
|
707
|
-
// And continue down the tree, setting j to the left son of k
|
|
708
|
-
j <<= 1;
|
|
727
|
+
tree[(max_code + 1) * 2 + 1] = 0xffff; // guard
|
|
728
|
+
|
|
729
|
+
for (let n = 0; n <= max_code; n++) {
|
|
730
|
+
curlen = nextlen;
|
|
731
|
+
nextlen = tree[(n + 1) * 2 + 1];
|
|
732
|
+
if (++count < max_count && curlen == nextlen) {
|
|
733
|
+
continue;
|
|
734
|
+
} else if (count < min_count) {
|
|
735
|
+
bl_tree[curlen * 2] += count;
|
|
736
|
+
} else if (curlen !== 0) {
|
|
737
|
+
if (curlen != prevlen)
|
|
738
|
+
bl_tree[curlen * 2]++;
|
|
739
|
+
bl_tree[REP_3_6 * 2]++;
|
|
740
|
+
} else if (count <= 10) {
|
|
741
|
+
bl_tree[REPZ_3_10 * 2]++;
|
|
742
|
+
} else {
|
|
743
|
+
bl_tree[REPZ_11_138 * 2]++;
|
|
709
744
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
// Scan a literal or distance tree to determine the frequencies of the codes
|
|
714
|
-
// in the bit length tree.
|
|
715
|
-
function scan_tree(tree,// the tree to be scanned
|
|
716
|
-
max_code // and its largest code of non zero frequency
|
|
717
|
-
) {
|
|
718
|
-
let prevlen = -1; // last emitted length
|
|
719
|
-
let curlen; // length of current code
|
|
720
|
-
let nextlen = tree[0 * 2 + 1]; // length of next code
|
|
721
|
-
let count = 0; // repeat count of the current code
|
|
722
|
-
let max_count = 7; // max repeat count
|
|
723
|
-
let min_count = 4; // min repeat count
|
|
724
|
-
|
|
745
|
+
count = 0;
|
|
746
|
+
prevlen = curlen;
|
|
725
747
|
if (nextlen === 0) {
|
|
726
748
|
max_count = 138;
|
|
727
749
|
min_count = 3;
|
|
750
|
+
} else if (curlen == nextlen) {
|
|
751
|
+
max_count = 6;
|
|
752
|
+
min_count = 3;
|
|
753
|
+
} else {
|
|
754
|
+
max_count = 7;
|
|
755
|
+
min_count = 4;
|
|
728
756
|
}
|
|
729
|
-
|
|
757
|
+
}
|
|
758
|
+
}
|
|
730
759
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
} else if (curlen == nextlen) {
|
|
753
|
-
max_count = 6;
|
|
754
|
-
min_count = 3;
|
|
755
|
-
} else {
|
|
756
|
-
max_count = 7;
|
|
757
|
-
min_count = 4;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
+
// Construct the Huffman tree for the bit lengths and return the index in
|
|
761
|
+
// bl_order of the last bit length code to send.
|
|
762
|
+
function build_bl_tree() {
|
|
763
|
+
let max_blindex; // index of last bit length code of non zero freq
|
|
764
|
+
|
|
765
|
+
// Determine the bit length frequencies for literal and distance trees
|
|
766
|
+
scan_tree(dyn_ltree, l_desc.max_code);
|
|
767
|
+
scan_tree(dyn_dtree, d_desc.max_code);
|
|
768
|
+
|
|
769
|
+
// Build the bit length tree:
|
|
770
|
+
bl_desc.build_tree(that);
|
|
771
|
+
// opt_len now includes the length of the tree representations, except
|
|
772
|
+
// the lengths of the bit lengths codes and the 5+5+4 bits for the
|
|
773
|
+
// counts.
|
|
774
|
+
|
|
775
|
+
// Determine the number of bit length codes to send. The pkzip format
|
|
776
|
+
// requires that at least 4 bit length codes be sent. (appnote.txt says
|
|
777
|
+
// 3 but the actual value used is 4.)
|
|
778
|
+
for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
|
|
779
|
+
if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] !== 0)
|
|
780
|
+
break;
|
|
760
781
|
}
|
|
782
|
+
// Update opt_len to include the bit length tree and counts
|
|
783
|
+
that.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
|
|
761
784
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
function build_bl_tree() {
|
|
765
|
-
let max_blindex; // index of last bit length code of non zero freq
|
|
766
|
-
|
|
767
|
-
// Determine the bit length frequencies for literal and distance trees
|
|
768
|
-
scan_tree(dyn_ltree, l_desc.max_code);
|
|
769
|
-
scan_tree(dyn_dtree, d_desc.max_code);
|
|
770
|
-
|
|
771
|
-
// Build the bit length tree:
|
|
772
|
-
bl_desc.build_tree(that);
|
|
773
|
-
// opt_len now includes the length of the tree representations, except
|
|
774
|
-
// the lengths of the bit lengths codes and the 5+5+4 bits for the
|
|
775
|
-
// counts.
|
|
776
|
-
|
|
777
|
-
// Determine the number of bit length codes to send. The pkzip format
|
|
778
|
-
// requires that at least 4 bit length codes be sent. (appnote.txt says
|
|
779
|
-
// 3 but the actual value used is 4.)
|
|
780
|
-
for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
|
|
781
|
-
if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] !== 0)
|
|
782
|
-
break;
|
|
783
|
-
}
|
|
784
|
-
// Update opt_len to include the bit length tree and counts
|
|
785
|
-
that.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
|
|
785
|
+
return max_blindex;
|
|
786
|
+
}
|
|
786
787
|
|
|
787
|
-
|
|
788
|
-
|
|
788
|
+
// Output a byte on the stream.
|
|
789
|
+
// IN assertion: there is enough room in pending_buf.
|
|
790
|
+
function put_byte(p) {
|
|
791
|
+
that.pending_buf[that.pending++] = p;
|
|
792
|
+
}
|
|
789
793
|
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
}
|
|
794
|
+
function put_short(w) {
|
|
795
|
+
put_byte(w & 0xff);
|
|
796
|
+
put_byte((w >>> 8) & 0xff);
|
|
797
|
+
}
|
|
795
798
|
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
799
|
+
function putShortMSB(b) {
|
|
800
|
+
put_byte((b >> 8) & 0xff);
|
|
801
|
+
put_byte((b & 0xff) & 0xff);
|
|
802
|
+
}
|
|
800
803
|
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
+
function send_bits(value, length) {
|
|
805
|
+
let val;
|
|
806
|
+
const len = length;
|
|
807
|
+
if (bi_valid > Buf_size - len) {
|
|
808
|
+
val = value;
|
|
809
|
+
// bi_buf |= (val << bi_valid);
|
|
810
|
+
bi_buf |= ((val << bi_valid) & 0xffff);
|
|
811
|
+
put_short(bi_buf);
|
|
812
|
+
bi_buf = val >>> (Buf_size - bi_valid);
|
|
813
|
+
bi_valid += len - Buf_size;
|
|
814
|
+
} else {
|
|
815
|
+
// bi_buf |= (value) << bi_valid;
|
|
816
|
+
bi_buf |= (((value) << bi_valid) & 0xffff);
|
|
817
|
+
bi_valid += len;
|
|
804
818
|
}
|
|
819
|
+
}
|
|
805
820
|
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
val = value;
|
|
811
|
-
// bi_buf |= (val << bi_valid);
|
|
812
|
-
bi_buf |= ((val << bi_valid) & 0xffff);
|
|
813
|
-
put_short(bi_buf);
|
|
814
|
-
bi_buf = val >>> (Buf_size - bi_valid);
|
|
815
|
-
bi_valid += len - Buf_size;
|
|
816
|
-
} else {
|
|
817
|
-
// bi_buf |= (value) << bi_valid;
|
|
818
|
-
bi_buf |= (((value) << bi_valid) & 0xffff);
|
|
819
|
-
bi_valid += len;
|
|
820
|
-
}
|
|
821
|
-
}
|
|
821
|
+
function send_code(c, tree) {
|
|
822
|
+
const c2 = c * 2;
|
|
823
|
+
send_bits(tree[c2] & 0xffff, tree[c2 + 1] & 0xffff);
|
|
824
|
+
}
|
|
822
825
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
+
// Send a literal or distance tree in compressed form, using the codes in
|
|
827
|
+
// bl_tree.
|
|
828
|
+
function send_tree(tree,// the tree to be sent
|
|
829
|
+
max_code // and its largest code of non zero frequency
|
|
830
|
+
) {
|
|
831
|
+
let n; // iterates over all tree elements
|
|
832
|
+
let prevlen = -1; // last emitted length
|
|
833
|
+
let curlen; // length of current code
|
|
834
|
+
let nextlen = tree[0 * 2 + 1]; // length of next code
|
|
835
|
+
let count = 0; // repeat count of the current code
|
|
836
|
+
let max_count = 7; // max repeat count
|
|
837
|
+
let min_count = 4; // min repeat count
|
|
838
|
+
|
|
839
|
+
if (nextlen === 0) {
|
|
840
|
+
max_count = 138;
|
|
841
|
+
min_count = 3;
|
|
826
842
|
}
|
|
827
843
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
844
|
+
for (n = 0; n <= max_code; n++) {
|
|
845
|
+
curlen = nextlen;
|
|
846
|
+
nextlen = tree[(n + 1) * 2 + 1];
|
|
847
|
+
if (++count < max_count && curlen == nextlen) {
|
|
848
|
+
continue;
|
|
849
|
+
} else if (count < min_count) {
|
|
850
|
+
do {
|
|
851
|
+
send_code(curlen, bl_tree);
|
|
852
|
+
} while (--count !== 0);
|
|
853
|
+
} else if (curlen !== 0) {
|
|
854
|
+
if (curlen != prevlen) {
|
|
855
|
+
send_code(curlen, bl_tree);
|
|
856
|
+
count--;
|
|
857
|
+
}
|
|
858
|
+
send_code(REP_3_6, bl_tree);
|
|
859
|
+
send_bits(count - 3, 2);
|
|
860
|
+
} else if (count <= 10) {
|
|
861
|
+
send_code(REPZ_3_10, bl_tree);
|
|
862
|
+
send_bits(count - 3, 3);
|
|
863
|
+
} else {
|
|
864
|
+
send_code(REPZ_11_138, bl_tree);
|
|
865
|
+
send_bits(count - 11, 7);
|
|
866
|
+
}
|
|
867
|
+
count = 0;
|
|
868
|
+
prevlen = curlen;
|
|
841
869
|
if (nextlen === 0) {
|
|
842
870
|
max_count = 138;
|
|
843
871
|
min_count = 3;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
continue;
|
|
851
|
-
} else if (count < min_count) {
|
|
852
|
-
do {
|
|
853
|
-
send_code(curlen, bl_tree);
|
|
854
|
-
} while (--count !== 0);
|
|
855
|
-
} else if (curlen !== 0) {
|
|
856
|
-
if (curlen != prevlen) {
|
|
857
|
-
send_code(curlen, bl_tree);
|
|
858
|
-
count--;
|
|
859
|
-
}
|
|
860
|
-
send_code(REP_3_6, bl_tree);
|
|
861
|
-
send_bits(count - 3, 2);
|
|
862
|
-
} else if (count <= 10) {
|
|
863
|
-
send_code(REPZ_3_10, bl_tree);
|
|
864
|
-
send_bits(count - 3, 3);
|
|
865
|
-
} else {
|
|
866
|
-
send_code(REPZ_11_138, bl_tree);
|
|
867
|
-
send_bits(count - 11, 7);
|
|
868
|
-
}
|
|
869
|
-
count = 0;
|
|
870
|
-
prevlen = curlen;
|
|
871
|
-
if (nextlen === 0) {
|
|
872
|
-
max_count = 138;
|
|
873
|
-
min_count = 3;
|
|
874
|
-
} else if (curlen == nextlen) {
|
|
875
|
-
max_count = 6;
|
|
876
|
-
min_count = 3;
|
|
877
|
-
} else {
|
|
878
|
-
max_count = 7;
|
|
879
|
-
min_count = 4;
|
|
880
|
-
}
|
|
872
|
+
} else if (curlen == nextlen) {
|
|
873
|
+
max_count = 6;
|
|
874
|
+
min_count = 3;
|
|
875
|
+
} else {
|
|
876
|
+
max_count = 7;
|
|
877
|
+
min_count = 4;
|
|
881
878
|
}
|
|
882
879
|
}
|
|
880
|
+
}
|
|
883
881
|
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
}
|
|
896
|
-
send_tree(dyn_ltree, lcodes - 1); // literal tree
|
|
897
|
-
send_tree(dyn_dtree, dcodes - 1); // distance tree
|
|
882
|
+
// Send the header for a block using dynamic Huffman trees: the counts, the
|
|
883
|
+
// lengths of the bit length codes, the literal tree and the distance tree.
|
|
884
|
+
// IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
|
|
885
|
+
function send_all_trees(lcodes, dcodes, blcodes) {
|
|
886
|
+
let rank; // index in bl_order
|
|
887
|
+
|
|
888
|
+
send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt
|
|
889
|
+
send_bits(dcodes - 1, 5);
|
|
890
|
+
send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt
|
|
891
|
+
for (rank = 0; rank < blcodes; rank++) {
|
|
892
|
+
send_bits(bl_tree[Tree.bl_order[rank] * 2 + 1], 3);
|
|
898
893
|
}
|
|
894
|
+
send_tree(dyn_ltree, lcodes - 1); // literal tree
|
|
895
|
+
send_tree(dyn_dtree, dcodes - 1); // distance tree
|
|
896
|
+
}
|
|
899
897
|
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
}
|
|
898
|
+
// Flush the bit buffer, keeping at most 7 bits in it.
|
|
899
|
+
function bi_flush() {
|
|
900
|
+
if (bi_valid == 16) {
|
|
901
|
+
put_short(bi_buf);
|
|
902
|
+
bi_buf = 0;
|
|
903
|
+
bi_valid = 0;
|
|
904
|
+
} else if (bi_valid >= 8) {
|
|
905
|
+
put_byte(bi_buf & 0xff);
|
|
906
|
+
bi_buf >>>= 8;
|
|
907
|
+
bi_valid -= 8;
|
|
911
908
|
}
|
|
909
|
+
}
|
|
912
910
|
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
911
|
+
// Send one empty static block to give enough lookahead for inflate.
|
|
912
|
+
// This takes 10 bits, of which 7 may remain in the bit buffer.
|
|
913
|
+
// The current inflate code requires 9 bits of lookahead. If the
|
|
914
|
+
// last two codes for the previous block (real code plus EOB) were coded
|
|
915
|
+
// on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
|
|
916
|
+
// the last real code. In this case we send two empty static blocks instead
|
|
917
|
+
// of one. (There are no problems if the previous block is stored or fixed.)
|
|
918
|
+
// To simplify the code, we assume the worst case of last real code encoded
|
|
919
|
+
// on one bit only.
|
|
920
|
+
function _tr_align() {
|
|
921
|
+
send_bits(STATIC_TREES << 1, 3);
|
|
922
|
+
send_code(END_BLOCK, StaticTree.static_ltree);
|
|
923
|
+
|
|
924
|
+
bi_flush();
|
|
925
|
+
|
|
926
|
+
// Of the 10 bits for the empty block, we have already sent
|
|
927
|
+
// (10 - bi_valid) bits. The lookahead for the last real code (before
|
|
928
|
+
// the EOB of the previous block) was thus at least one plus the length
|
|
929
|
+
// of the EOB plus what we have just sent of the empty static block.
|
|
930
|
+
if (1 + last_eob_len + 10 - bi_valid < 9) {
|
|
923
931
|
send_bits(STATIC_TREES << 1, 3);
|
|
924
932
|
send_code(END_BLOCK, StaticTree.static_ltree);
|
|
925
|
-
|
|
926
933
|
bi_flush();
|
|
927
|
-
|
|
928
|
-
// Of the 10 bits for the empty block, we have already sent
|
|
929
|
-
// (10 - bi_valid) bits. The lookahead for the last real code (before
|
|
930
|
-
// the EOB of the previous block) was thus at least one plus the length
|
|
931
|
-
// of the EOB plus what we have just sent of the empty static block.
|
|
932
|
-
if (1 + last_eob_len + 10 - bi_valid < 9) {
|
|
933
|
-
send_bits(STATIC_TREES << 1, 3);
|
|
934
|
-
send_code(END_BLOCK, StaticTree.static_ltree);
|
|
935
|
-
bi_flush();
|
|
936
|
-
}
|
|
937
|
-
last_eob_len = 7;
|
|
938
934
|
}
|
|
935
|
+
last_eob_len = 7;
|
|
936
|
+
}
|
|
939
937
|
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
938
|
+
// Save the match info and tally the frequency counts. Return true if
|
|
939
|
+
// the current block must be flushed.
|
|
940
|
+
function _tr_tally(dist, // distance of matched string
|
|
941
|
+
lc // match length-MIN_MATCH or unmatched char (if dist==0)
|
|
942
|
+
) {
|
|
943
|
+
let out_length, in_length, dcode;
|
|
944
|
+
that.dist_buf[last_lit] = dist;
|
|
945
|
+
that.lc_buf[last_lit] = lc & 0xff;
|
|
946
|
+
last_lit++;
|
|
947
|
+
|
|
948
|
+
if (dist === 0) {
|
|
949
|
+
// lc is the unmatched char
|
|
950
|
+
dyn_ltree[lc * 2]++;
|
|
951
|
+
} else {
|
|
952
|
+
matches++;
|
|
953
|
+
// Here, lc is the match length - MIN_MATCH
|
|
954
|
+
dist--; // dist = match distance - 1
|
|
955
|
+
dyn_ltree[(Tree._length_code[lc] + LITERALS + 1) * 2]++;
|
|
956
|
+
dyn_dtree[Tree.d_code(dist) * 2]++;
|
|
957
|
+
}
|
|
960
958
|
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
}
|
|
968
|
-
out_length >>>= 3;
|
|
969
|
-
if ((matches < Math.floor(last_lit / 2)) && out_length < Math.floor(in_length / 2))
|
|
970
|
-
return true;
|
|
959
|
+
if ((last_lit & 0x1fff) === 0 && level > 2) {
|
|
960
|
+
// Compute an upper bound for the compressed length
|
|
961
|
+
out_length = last_lit * 8;
|
|
962
|
+
in_length = strstart - block_start;
|
|
963
|
+
for (dcode = 0; dcode < D_CODES; dcode++) {
|
|
964
|
+
out_length += dyn_dtree[dcode * 2] * (5 + Tree.extra_dbits[dcode]);
|
|
971
965
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
// on 16 bit machines and because stored blocks are restricted to
|
|
976
|
-
// 64K-1 bytes.
|
|
966
|
+
out_length >>>= 3;
|
|
967
|
+
if ((matches < Math.floor(last_lit / 2)) && out_length < Math.floor(in_length / 2))
|
|
968
|
+
return true;
|
|
977
969
|
}
|
|
978
970
|
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
let code; // the code to send
|
|
985
|
-
let extra; // number of extra bits to send
|
|
971
|
+
return (last_lit == lit_bufsize - 1);
|
|
972
|
+
// We avoid equality with lit_bufsize because of wraparound at 64K
|
|
973
|
+
// on 16 bit machines and because stored blocks are restricted to
|
|
974
|
+
// 64K-1 bytes.
|
|
975
|
+
}
|
|
986
976
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
send_code(lc, ltree); // send a literal byte
|
|
995
|
-
} else {
|
|
996
|
-
// Here, lc is the match length - MIN_MATCH
|
|
997
|
-
code = Tree._length_code[lc];
|
|
998
|
-
|
|
999
|
-
send_code(code + LITERALS + 1, ltree); // send the length
|
|
1000
|
-
// code
|
|
1001
|
-
extra = Tree.extra_lbits[code];
|
|
1002
|
-
if (extra !== 0) {
|
|
1003
|
-
lc -= Tree.base_length[code];
|
|
1004
|
-
send_bits(lc, extra); // send the extra length bits
|
|
1005
|
-
}
|
|
1006
|
-
dist--; // dist is now the match distance - 1
|
|
1007
|
-
code = Tree.d_code(dist);
|
|
1008
|
-
|
|
1009
|
-
send_code(code, dtree); // send the distance code
|
|
1010
|
-
extra = Tree.extra_dbits[code];
|
|
1011
|
-
if (extra !== 0) {
|
|
1012
|
-
dist -= Tree.base_dist[code];
|
|
1013
|
-
send_bits(dist, extra); // send the extra distance bits
|
|
1014
|
-
}
|
|
1015
|
-
} // literal or match pair ?
|
|
1016
|
-
} while (lx < last_lit);
|
|
1017
|
-
}
|
|
977
|
+
// Send the block data compressed using the given Huffman trees
|
|
978
|
+
function compress_block(ltree, dtree) {
|
|
979
|
+
let dist; // distance of matched string
|
|
980
|
+
let lc; // match length or unmatched char (if dist === 0)
|
|
981
|
+
let lx = 0; // running index in dist_buf and lc_buf
|
|
982
|
+
let code; // the code to send
|
|
983
|
+
let extra; // number of extra bits to send
|
|
1018
984
|
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
985
|
+
if (last_lit !== 0) {
|
|
986
|
+
do {
|
|
987
|
+
dist = that.dist_buf[lx];
|
|
988
|
+
lc = that.lc_buf[lx];
|
|
989
|
+
lx++;
|
|
1022
990
|
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
991
|
+
if (dist === 0) {
|
|
992
|
+
send_code(lc, ltree); // send a literal byte
|
|
993
|
+
} else {
|
|
994
|
+
// Here, lc is the match length - MIN_MATCH
|
|
995
|
+
code = Tree._length_code[lc];
|
|
996
|
+
|
|
997
|
+
send_code(code + LITERALS + 1, ltree); // send the length
|
|
998
|
+
// code
|
|
999
|
+
extra = Tree.extra_lbits[code];
|
|
1000
|
+
if (extra !== 0) {
|
|
1001
|
+
lc -= Tree.base_length[code];
|
|
1002
|
+
send_bits(lc, extra); // send the extra length bits
|
|
1003
|
+
}
|
|
1004
|
+
dist--; // dist is now the match distance - 1
|
|
1005
|
+
code = Tree.d_code(dist);
|
|
1006
|
+
|
|
1007
|
+
send_code(code, dtree); // send the distance code
|
|
1008
|
+
extra = Tree.extra_dbits[code];
|
|
1009
|
+
if (extra !== 0) {
|
|
1010
|
+
dist -= Tree.base_dist[code];
|
|
1011
|
+
send_bits(dist, extra); // send the extra distance bits
|
|
1012
|
+
}
|
|
1013
|
+
} // literal or match pair ?
|
|
1014
|
+
} while (lx < last_lit);
|
|
1032
1015
|
}
|
|
1033
1016
|
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
len, // its length
|
|
1038
|
-
header // true if block header must be written
|
|
1039
|
-
) {
|
|
1040
|
-
bi_windup(); // align on byte boundary
|
|
1041
|
-
last_eob_len = 8; // enough lookahead for inflate
|
|
1042
|
-
|
|
1043
|
-
if (header) {
|
|
1044
|
-
put_short(len);
|
|
1045
|
-
put_short(~len);
|
|
1046
|
-
}
|
|
1017
|
+
send_code(END_BLOCK, ltree);
|
|
1018
|
+
last_eob_len = ltree[END_BLOCK * 2 + 1];
|
|
1019
|
+
}
|
|
1047
1020
|
|
|
1048
|
-
|
|
1049
|
-
|
|
1021
|
+
// Flush the bit buffer and align the output on a byte boundary
|
|
1022
|
+
function bi_windup() {
|
|
1023
|
+
if (bi_valid > 8) {
|
|
1024
|
+
put_short(bi_buf);
|
|
1025
|
+
} else if (bi_valid > 0) {
|
|
1026
|
+
put_byte(bi_buf & 0xff);
|
|
1050
1027
|
}
|
|
1028
|
+
bi_buf = 0;
|
|
1029
|
+
bi_valid = 0;
|
|
1030
|
+
}
|
|
1051
1031
|
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1032
|
+
// Copy a stored block, storing first the length and its
|
|
1033
|
+
// one's complement if requested.
|
|
1034
|
+
function copy_block(buf, // the input data
|
|
1035
|
+
len, // its length
|
|
1036
|
+
header // true if block header must be written
|
|
1037
|
+
) {
|
|
1038
|
+
bi_windup(); // align on byte boundary
|
|
1039
|
+
last_eob_len = 8; // enough lookahead for inflate
|
|
1040
|
+
|
|
1041
|
+
if (header) {
|
|
1042
|
+
put_short(len);
|
|
1043
|
+
put_short(~len);
|
|
1059
1044
|
}
|
|
1060
1045
|
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
stored_len, // length of input block
|
|
1065
|
-
eof // true if this is the last block for a file
|
|
1066
|
-
) {
|
|
1067
|
-
let opt_lenb, static_lenb;// opt_len and static_len in bytes
|
|
1068
|
-
let max_blindex = 0; // index of last bit length code of non zero freq
|
|
1069
|
-
|
|
1070
|
-
// Build the Huffman trees unless a stored block is forced
|
|
1071
|
-
if (level > 0) {
|
|
1072
|
-
// Construct the literal and distance trees
|
|
1073
|
-
l_desc.build_tree(that);
|
|
1074
|
-
|
|
1075
|
-
d_desc.build_tree(that);
|
|
1076
|
-
|
|
1077
|
-
// At this point, opt_len and static_len are the total bit lengths
|
|
1078
|
-
// of
|
|
1079
|
-
// the compressed block data, excluding the tree representations.
|
|
1080
|
-
|
|
1081
|
-
// Build the bit length tree for the above two trees, and get the
|
|
1082
|
-
// index
|
|
1083
|
-
// in bl_order of the last bit length code to send.
|
|
1084
|
-
max_blindex = build_bl_tree();
|
|
1085
|
-
|
|
1086
|
-
// Determine the best encoding. Compute first the block length in
|
|
1087
|
-
// bytes
|
|
1088
|
-
opt_lenb = (that.opt_len + 3 + 7) >>> 3;
|
|
1089
|
-
static_lenb = (that.static_len + 3 + 7) >>> 3;
|
|
1090
|
-
|
|
1091
|
-
if (static_lenb <= opt_lenb)
|
|
1092
|
-
opt_lenb = static_lenb;
|
|
1093
|
-
} else {
|
|
1094
|
-
opt_lenb = static_lenb = stored_len + 5; // force a stored block
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
if ((stored_len + 4 <= opt_lenb) && buf != -1) {
|
|
1098
|
-
// 4: two words for the lengths
|
|
1099
|
-
// The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
|
|
1100
|
-
// Otherwise we can't have processed more than WSIZE input bytes
|
|
1101
|
-
// since
|
|
1102
|
-
// the last block flush, because compression would have been
|
|
1103
|
-
// successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
|
|
1104
|
-
// transform a block into a stored block.
|
|
1105
|
-
_tr_stored_block(buf, stored_len, eof);
|
|
1106
|
-
} else if (static_lenb == opt_lenb) {
|
|
1107
|
-
send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3);
|
|
1108
|
-
compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
|
|
1109
|
-
} else {
|
|
1110
|
-
send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3);
|
|
1111
|
-
send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1);
|
|
1112
|
-
compress_block(dyn_ltree, dyn_dtree);
|
|
1113
|
-
}
|
|
1046
|
+
that.pending_buf.set(win.subarray(buf, buf + len), that.pending);
|
|
1047
|
+
that.pending += len;
|
|
1048
|
+
}
|
|
1114
1049
|
|
|
1115
|
-
|
|
1116
|
-
|
|
1050
|
+
// Send a stored block
|
|
1051
|
+
function _tr_stored_block(buf, // input block
|
|
1052
|
+
stored_len, // length of input block
|
|
1053
|
+
eof // true if this is the last block for a file
|
|
1054
|
+
) {
|
|
1055
|
+
send_bits((STORED_BLOCK << 1) + (eof ? 1 : 0), 3); // send block type
|
|
1056
|
+
copy_block(buf, stored_len, true); // with header
|
|
1057
|
+
}
|
|
1117
1058
|
|
|
1118
|
-
|
|
1059
|
+
// Determine the best encoding for the current block: dynamic trees, static
|
|
1060
|
+
// trees or store, and output the encoded block to the zip file.
|
|
1061
|
+
function _tr_flush_block(buf, // input block, or NULL if too old
|
|
1062
|
+
stored_len, // length of input block
|
|
1063
|
+
eof // true if this is the last block for a file
|
|
1064
|
+
) {
|
|
1065
|
+
let opt_lenb, static_lenb;// opt_len and static_len in bytes
|
|
1066
|
+
let max_blindex = 0; // index of last bit length code of non zero freq
|
|
1067
|
+
|
|
1068
|
+
// Build the Huffman trees unless a stored block is forced
|
|
1069
|
+
if (level > 0) {
|
|
1070
|
+
// Construct the literal and distance trees
|
|
1071
|
+
l_desc.build_tree(that);
|
|
1072
|
+
|
|
1073
|
+
d_desc.build_tree(that);
|
|
1074
|
+
|
|
1075
|
+
// At this point, opt_len and static_len are the total bit lengths
|
|
1076
|
+
// of
|
|
1077
|
+
// the compressed block data, excluding the tree representations.
|
|
1078
|
+
|
|
1079
|
+
// Build the bit length tree for the above two trees, and get the
|
|
1080
|
+
// index
|
|
1081
|
+
// in bl_order of the last bit length code to send.
|
|
1082
|
+
max_blindex = build_bl_tree();
|
|
1083
|
+
|
|
1084
|
+
// Determine the best encoding. Compute first the block length in
|
|
1085
|
+
// bytes
|
|
1086
|
+
opt_lenb = (that.opt_len + 3 + 7) >>> 3;
|
|
1087
|
+
static_lenb = (that.static_len + 3 + 7) >>> 3;
|
|
1088
|
+
|
|
1089
|
+
if (static_lenb <= opt_lenb)
|
|
1090
|
+
opt_lenb = static_lenb;
|
|
1091
|
+
} else {
|
|
1092
|
+
opt_lenb = static_lenb = stored_len + 5; // force a stored block
|
|
1093
|
+
}
|
|
1119
1094
|
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1095
|
+
if ((stored_len + 4 <= opt_lenb) && buf != -1) {
|
|
1096
|
+
// 4: two words for the lengths
|
|
1097
|
+
// The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
|
|
1098
|
+
// Otherwise we can't have processed more than WSIZE input bytes
|
|
1099
|
+
// since
|
|
1100
|
+
// the last block flush, because compression would have been
|
|
1101
|
+
// successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
|
|
1102
|
+
// transform a block into a stored block.
|
|
1103
|
+
_tr_stored_block(buf, stored_len, eof);
|
|
1104
|
+
} else if (static_lenb == opt_lenb) {
|
|
1105
|
+
send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3);
|
|
1106
|
+
compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
|
|
1107
|
+
} else {
|
|
1108
|
+
send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3);
|
|
1109
|
+
send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1);
|
|
1110
|
+
compress_block(dyn_ltree, dyn_dtree);
|
|
1123
1111
|
}
|
|
1124
1112
|
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1113
|
+
// The above check is made mod 2^32, for files larger than 512 MB
|
|
1114
|
+
// and uLong implemented on 32 bits.
|
|
1115
|
+
|
|
1116
|
+
init_block();
|
|
1117
|
+
|
|
1118
|
+
if (eof) {
|
|
1119
|
+
bi_windup();
|
|
1129
1120
|
}
|
|
1121
|
+
}
|
|
1130
1122
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
// At least one byte has been read, or avail_in === 0; reads are
|
|
1137
|
-
// performed for at least two bytes (required for the zip translate_eol
|
|
1138
|
-
// option -- not supported here).
|
|
1139
|
-
function fill_window() {
|
|
1140
|
-
let n, m;
|
|
1141
|
-
let p;
|
|
1142
|
-
let more; // Amount of free space at the end of the win.
|
|
1123
|
+
function flush_block_only(eof) {
|
|
1124
|
+
_tr_flush_block(block_start >= 0 ? block_start : -1, strstart - block_start, eof);
|
|
1125
|
+
block_start = strstart;
|
|
1126
|
+
strm.flush_pending();
|
|
1127
|
+
}
|
|
1143
1128
|
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1129
|
+
// Fill the win when the lookahead becomes insufficient.
|
|
1130
|
+
// Updates strstart and lookahead.
|
|
1131
|
+
//
|
|
1132
|
+
// IN assertion: lookahead < MIN_LOOKAHEAD
|
|
1133
|
+
// OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
|
|
1134
|
+
// At least one byte has been read, or avail_in === 0; reads are
|
|
1135
|
+
// performed for at least two bytes (required for the zip translate_eol
|
|
1136
|
+
// option -- not supported here).
|
|
1137
|
+
function fill_window() {
|
|
1138
|
+
let n, m;
|
|
1139
|
+
let p;
|
|
1140
|
+
let more; // Amount of free space at the end of the win.
|
|
1141
|
+
|
|
1142
|
+
do {
|
|
1143
|
+
more = (window_size - lookahead - strstart);
|
|
1144
|
+
|
|
1145
|
+
// Deal with !@#$% 64K limit:
|
|
1146
|
+
if (more === 0 && strstart === 0 && lookahead === 0) {
|
|
1147
|
+
more = w_size;
|
|
1148
|
+
} else if (more == -1) {
|
|
1149
|
+
// Very unlikely, but possible on 16 bit machine if strstart ==
|
|
1150
|
+
// 0
|
|
1151
|
+
// and lookahead == 1 (input done one byte at time)
|
|
1152
|
+
more--;
|
|
1153
|
+
|
|
1154
|
+
// If the win is almost full and there is insufficient
|
|
1155
|
+
// lookahead,
|
|
1156
|
+
// move the upper half to the lower one to make room in the
|
|
1157
|
+
// upper half.
|
|
1158
|
+
} else if (strstart >= w_size + w_size - MIN_LOOKAHEAD) {
|
|
1159
|
+
win.set(win.subarray(w_size, w_size + w_size), 0);
|
|
1160
|
+
|
|
1161
|
+
match_start -= w_size;
|
|
1162
|
+
strstart -= w_size; // we now have strstart >= MAX_DIST
|
|
1163
|
+
block_start -= w_size;
|
|
1164
|
+
|
|
1165
|
+
// Slide the hash table (could be avoided with 32 bit values
|
|
1166
|
+
// at the expense of memory usage). We slide even when level ==
|
|
1167
|
+
// 0
|
|
1168
|
+
// to keep the hash table consistent if we switch back to level
|
|
1169
|
+
// > 0
|
|
1170
|
+
// later. (Using level 0 permanently is not an optimal usage of
|
|
1171
|
+
// zlib, so we don't care about this pathological case.)
|
|
1172
|
+
|
|
1173
|
+
n = hash_size;
|
|
1174
|
+
p = n;
|
|
1175
|
+
do {
|
|
1176
|
+
m = (head[--p] & 0xffff);
|
|
1177
|
+
head[p] = (m >= w_size ? m - w_size : 0);
|
|
1178
|
+
} while (--n !== 0);
|
|
1181
1179
|
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1180
|
+
n = w_size;
|
|
1181
|
+
p = n;
|
|
1182
|
+
do {
|
|
1183
|
+
m = (prev[--p] & 0xffff);
|
|
1184
|
+
prev[p] = (m >= w_size ? m - w_size : 0);
|
|
1185
|
+
// If n is not on any hash chain, prev[n] is garbage but
|
|
1186
|
+
// its value will never be used.
|
|
1187
|
+
} while (--n !== 0);
|
|
1188
|
+
more += w_size;
|
|
1189
|
+
}
|
|
1192
1190
|
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
// If there was no sliding:
|
|
1197
|
-
// strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
|
|
1198
|
-
// more == window_size - lookahead - strstart
|
|
1199
|
-
// => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
|
|
1200
|
-
// => more >= window_size - 2*WSIZE + 2
|
|
1201
|
-
// In the BIG_MEM or MMAP case (not yet supported),
|
|
1202
|
-
// window_size == input_size + MIN_LOOKAHEAD &&
|
|
1203
|
-
// strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
|
|
1204
|
-
// Otherwise, window_size == 2*WSIZE so more >= 2.
|
|
1205
|
-
// If there was sliding, more >= WSIZE. So in all cases, more >= 2.
|
|
1206
|
-
|
|
1207
|
-
n = strm.read_buf(win, strstart + lookahead, more);
|
|
1208
|
-
lookahead += n;
|
|
1209
|
-
|
|
1210
|
-
// Initialize the hash value now that we have some input:
|
|
1211
|
-
if (lookahead >= MIN_MATCH) {
|
|
1212
|
-
ins_h = win[strstart] & 0xff;
|
|
1213
|
-
ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
|
|
1214
|
-
}
|
|
1215
|
-
// If the whole input has less than MIN_MATCH bytes, ins_h is
|
|
1216
|
-
// garbage,
|
|
1217
|
-
// but this is not important since only literal bytes will be
|
|
1218
|
-
// emitted.
|
|
1219
|
-
} while (lookahead < MIN_LOOKAHEAD && strm.avail_in !== 0);
|
|
1220
|
-
}
|
|
1191
|
+
if (strm.avail_in === 0)
|
|
1192
|
+
return;
|
|
1221
1193
|
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
//
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
if (
|
|
1238
|
-
|
|
1194
|
+
// If there was no sliding:
|
|
1195
|
+
// strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
|
|
1196
|
+
// more == window_size - lookahead - strstart
|
|
1197
|
+
// => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
|
|
1198
|
+
// => more >= window_size - 2*WSIZE + 2
|
|
1199
|
+
// In the BIG_MEM or MMAP case (not yet supported),
|
|
1200
|
+
// window_size == input_size + MIN_LOOKAHEAD &&
|
|
1201
|
+
// strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
|
|
1202
|
+
// Otherwise, window_size == 2*WSIZE so more >= 2.
|
|
1203
|
+
// If there was sliding, more >= WSIZE. So in all cases, more >= 2.
|
|
1204
|
+
|
|
1205
|
+
n = strm.read_buf(win, strstart + lookahead, more);
|
|
1206
|
+
lookahead += n;
|
|
1207
|
+
|
|
1208
|
+
// Initialize the hash value now that we have some input:
|
|
1209
|
+
if (lookahead >= MIN_MATCH) {
|
|
1210
|
+
ins_h = win[strstart] & 0xff;
|
|
1211
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
|
|
1239
1212
|
}
|
|
1213
|
+
// If the whole input has less than MIN_MATCH bytes, ins_h is
|
|
1214
|
+
// garbage,
|
|
1215
|
+
// but this is not important since only literal bytes will be
|
|
1216
|
+
// emitted.
|
|
1217
|
+
} while (lookahead < MIN_LOOKAHEAD && strm.avail_in !== 0);
|
|
1218
|
+
}
|
|
1240
1219
|
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1220
|
+
// Copy without compression as much as possible from the input stream,
|
|
1221
|
+
// return
|
|
1222
|
+
// the current block state.
|
|
1223
|
+
// This function does not insert new strings in the dictionary since
|
|
1224
|
+
// uncompressible data is probably not useful. This function is used
|
|
1225
|
+
// only for the level=0 compression option.
|
|
1226
|
+
// NOTE: this function should be optimized to avoid extra copying from
|
|
1227
|
+
// win to pending_buf.
|
|
1228
|
+
function deflate_stored(flush) {
|
|
1229
|
+
// Stored blocks are limited to 0xffff bytes, pending_buf is limited
|
|
1230
|
+
// to pending_buf_size, and each stored block has a 5 byte header:
|
|
1231
|
+
|
|
1232
|
+
let max_block_size = 0xffff;
|
|
1233
|
+
let max_start;
|
|
1234
|
+
|
|
1235
|
+
if (max_block_size > pending_buf_size - 5) {
|
|
1236
|
+
max_block_size = pending_buf_size - 5;
|
|
1237
|
+
}
|
|
1252
1238
|
|
|
1253
|
-
|
|
1254
|
-
|
|
1239
|
+
// Copy as much as possible from input to output:
|
|
1240
|
+
// eslint-disable-next-line no-constant-condition
|
|
1241
|
+
while (true) {
|
|
1242
|
+
// Fill the win as much as possible:
|
|
1243
|
+
if (lookahead <= 1) {
|
|
1244
|
+
fill_window();
|
|
1245
|
+
if (lookahead === 0 && flush == Z_NO_FLUSH)
|
|
1246
|
+
return NeedMore;
|
|
1247
|
+
if (lookahead === 0)
|
|
1248
|
+
break; // flush the current block
|
|
1249
|
+
}
|
|
1255
1250
|
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
if (strstart === 0 || strstart >= max_start) {
|
|
1259
|
-
// strstart === 0 is possible when wraparound on 16-bit machine
|
|
1260
|
-
lookahead = (strstart - max_start);
|
|
1261
|
-
strstart = max_start;
|
|
1251
|
+
strstart += lookahead;
|
|
1252
|
+
lookahead = 0;
|
|
1262
1253
|
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1254
|
+
// Emit a stored block if pending_buf will be full:
|
|
1255
|
+
max_start = block_start + max_block_size;
|
|
1256
|
+
if (strstart === 0 || strstart >= max_start) {
|
|
1257
|
+
// strstart === 0 is possible when wraparound on 16-bit machine
|
|
1258
|
+
lookahead = (strstart - max_start);
|
|
1259
|
+
strstart = max_start;
|
|
1266
1260
|
|
|
1267
|
-
|
|
1261
|
+
flush_block_only(false);
|
|
1262
|
+
if (strm.avail_out === 0)
|
|
1263
|
+
return NeedMore;
|
|
1268
1264
|
|
|
1269
|
-
// Flush if we may have to slide, otherwise block_start may become
|
|
1270
|
-
// negative and the data will be gone:
|
|
1271
|
-
if (strstart - block_start >= w_size - MIN_LOOKAHEAD) {
|
|
1272
|
-
flush_block_only(false);
|
|
1273
|
-
if (strm.avail_out === 0)
|
|
1274
|
-
return NeedMore;
|
|
1275
|
-
}
|
|
1276
1265
|
}
|
|
1277
1266
|
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1267
|
+
// Flush if we may have to slide, otherwise block_start may become
|
|
1268
|
+
// negative and the data will be gone:
|
|
1269
|
+
if (strstart - block_start >= w_size - MIN_LOOKAHEAD) {
|
|
1270
|
+
flush_block_only(false);
|
|
1271
|
+
if (strm.avail_out === 0)
|
|
1272
|
+
return NeedMore;
|
|
1273
|
+
}
|
|
1283
1274
|
}
|
|
1284
1275
|
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
let match; // matched string
|
|
1289
|
-
let len; // length of current match
|
|
1290
|
-
let best_len = prev_length; // best match length so far
|
|
1291
|
-
const limit = strstart > (w_size - MIN_LOOKAHEAD) ? strstart - (w_size - MIN_LOOKAHEAD) : 0;
|
|
1292
|
-
let _nice_match = nice_match;
|
|
1293
|
-
|
|
1294
|
-
// Stop when cur_match becomes <= limit. To simplify the code,
|
|
1295
|
-
// we prevent matches with the string of win index 0.
|
|
1276
|
+
flush_block_only(flush == Z_FINISH);
|
|
1277
|
+
if (strm.avail_out === 0)
|
|
1278
|
+
return (flush == Z_FINISH) ? FinishStarted : NeedMore;
|
|
1296
1279
|
|
|
1297
|
-
|
|
1280
|
+
return flush == Z_FINISH ? FinishDone : BlockDone;
|
|
1281
|
+
}
|
|
1298
1282
|
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1283
|
+
function longest_match(cur_match) {
|
|
1284
|
+
let chain_length = max_chain_length; // max hash chain length
|
|
1285
|
+
let scan = strstart; // current string
|
|
1286
|
+
let match; // matched string
|
|
1287
|
+
let len; // length of current match
|
|
1288
|
+
let best_len = prev_length; // best match length so far
|
|
1289
|
+
const limit = strstart > (w_size - MIN_LOOKAHEAD) ? strstart - (w_size - MIN_LOOKAHEAD) : 0;
|
|
1290
|
+
let _nice_match = nice_match;
|
|
1302
1291
|
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
// It is easy to get rid of this optimization if necessary.
|
|
1292
|
+
// Stop when cur_match becomes <= limit. To simplify the code,
|
|
1293
|
+
// we prevent matches with the string of win index 0.
|
|
1306
1294
|
|
|
1307
|
-
|
|
1308
|
-
if (prev_length >= good_match) {
|
|
1309
|
-
chain_length >>= 2;
|
|
1310
|
-
}
|
|
1295
|
+
const wmask = w_mask;
|
|
1311
1296
|
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
if (_nice_match > lookahead)
|
|
1316
|
-
_nice_match = lookahead;
|
|
1297
|
+
const strend = strstart + MAX_MATCH;
|
|
1298
|
+
let scan_end1 = win[scan + best_len - 1];
|
|
1299
|
+
let scan_end = win[scan + best_len];
|
|
1317
1300
|
|
|
1318
|
-
|
|
1319
|
-
|
|
1301
|
+
// The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of
|
|
1302
|
+
// 16.
|
|
1303
|
+
// It is easy to get rid of this optimization if necessary.
|
|
1320
1304
|
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
continue;
|
|
1305
|
+
// Do not waste too much time if we already have a good match:
|
|
1306
|
+
if (prev_length >= good_match) {
|
|
1307
|
+
chain_length >>= 2;
|
|
1308
|
+
}
|
|
1326
1309
|
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1310
|
+
// Do not look for matches beyond the end of the input. This is
|
|
1311
|
+
// necessary
|
|
1312
|
+
// to make deflate deterministic.
|
|
1313
|
+
if (_nice_match > lookahead)
|
|
1314
|
+
_nice_match = lookahead;
|
|
1315
|
+
|
|
1316
|
+
do {
|
|
1317
|
+
match = cur_match;
|
|
1318
|
+
|
|
1319
|
+
// Skip to next match if the match length cannot increase
|
|
1320
|
+
// or if the match length is less than 2:
|
|
1321
|
+
if (win[match + best_len] != scan_end || win[match + best_len - 1] != scan_end1 || win[match] != win[scan]
|
|
1322
|
+
|| win[++match] != win[scan + 1])
|
|
1323
|
+
continue;
|
|
1324
|
+
|
|
1325
|
+
// The check at best_len-1 can be removed because it will be made
|
|
1326
|
+
// again later. (This heuristic is not always a win.)
|
|
1327
|
+
// It is not necessary to compare scan[2] and match[2] since they
|
|
1328
|
+
// are always equal when the other bytes match, given that
|
|
1329
|
+
// the hash keys are equal and that HASH_BITS >= 8.
|
|
1330
|
+
scan += 2;
|
|
1331
|
+
match++;
|
|
1332
|
+
|
|
1333
|
+
// We check for insufficient lookahead only every 8th comparison;
|
|
1334
|
+
// the 256th check will be made at strstart+258.
|
|
1335
|
+
// eslint-disable-next-line no-empty
|
|
1336
|
+
do {
|
|
1337
|
+
// empty block
|
|
1338
|
+
} while (win[++scan] == win[++match] && win[++scan] == win[++match] && win[++scan] == win[++match]
|
|
1339
|
+
&& win[++scan] == win[++match] && win[++scan] == win[++match] && win[++scan] == win[++match]
|
|
1340
|
+
&& win[++scan] == win[++match] && win[++scan] == win[++match] && scan < strend);
|
|
1341
|
+
|
|
1342
|
+
len = MAX_MATCH - (strend - scan);
|
|
1343
|
+
scan = strend - MAX_MATCH;
|
|
1344
|
+
|
|
1345
|
+
if (len > best_len) {
|
|
1346
|
+
match_start = cur_match;
|
|
1347
|
+
best_len = len;
|
|
1348
|
+
if (len >= _nice_match)
|
|
1349
|
+
break;
|
|
1350
|
+
scan_end1 = win[scan + best_len - 1];
|
|
1351
|
+
scan_end = win[scan + best_len];
|
|
1352
|
+
}
|
|
1355
1353
|
|
|
1356
|
-
|
|
1354
|
+
} while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit && --chain_length !== 0);
|
|
1357
1355
|
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1356
|
+
if (best_len <= lookahead)
|
|
1357
|
+
return best_len;
|
|
1358
|
+
return lookahead;
|
|
1359
|
+
}
|
|
1362
1360
|
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
}
|
|
1384
|
-
if (lookahead === 0)
|
|
1385
|
-
break; // flush the current block
|
|
1361
|
+
// Compress as much as possible from the input stream, return the current
|
|
1362
|
+
// block state.
|
|
1363
|
+
// This function does not perform lazy evaluation of matches and inserts
|
|
1364
|
+
// new strings in the dictionary only for unmatched strings or for short
|
|
1365
|
+
// matches. It is used only for the fast compression options.
|
|
1366
|
+
function deflate_fast(flush) {
|
|
1367
|
+
// short hash_head = 0; // head of the hash chain
|
|
1368
|
+
let hash_head = 0; // head of the hash chain
|
|
1369
|
+
let bflush; // set if current block must be flushed
|
|
1370
|
+
|
|
1371
|
+
// eslint-disable-next-line no-constant-condition
|
|
1372
|
+
while (true) {
|
|
1373
|
+
// Make sure that we always have enough lookahead, except
|
|
1374
|
+
// at the end of the input file. We need MAX_MATCH bytes
|
|
1375
|
+
// for the next match, plus MIN_MATCH bytes to insert the
|
|
1376
|
+
// string following the next match.
|
|
1377
|
+
if (lookahead < MIN_LOOKAHEAD) {
|
|
1378
|
+
fill_window();
|
|
1379
|
+
if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
|
|
1380
|
+
return NeedMore;
|
|
1386
1381
|
}
|
|
1382
|
+
if (lookahead === 0)
|
|
1383
|
+
break; // flush the current block
|
|
1384
|
+
}
|
|
1387
1385
|
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1386
|
+
// Insert the string win[strstart .. strstart+2] in the
|
|
1387
|
+
// dictionary, and set hash_head to the head of the hash chain:
|
|
1388
|
+
if (lookahead >= MIN_MATCH) {
|
|
1389
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1392
1390
|
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1391
|
+
// prev[strstart&w_mask]=hash_head=head[ins_h];
|
|
1392
|
+
hash_head = (head[ins_h] & 0xffff);
|
|
1393
|
+
prev[strstart & w_mask] = head[ins_h];
|
|
1394
|
+
head[ins_h] = strstart;
|
|
1395
|
+
}
|
|
1398
1396
|
|
|
1399
|
-
|
|
1400
|
-
|
|
1397
|
+
// Find the longest match, discarding those <= prev_length.
|
|
1398
|
+
// At this point we have always match_length < MIN_MATCH
|
|
1401
1399
|
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
}
|
|
1409
|
-
// longest_match() sets match_start
|
|
1400
|
+
if (hash_head !== 0 && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
|
|
1401
|
+
// To simplify the code, we prevent matches with the string
|
|
1402
|
+
// of win index 0 (in particular we have to avoid a match
|
|
1403
|
+
// of the string with itself at the start of the input file).
|
|
1404
|
+
if (strategy != Z_HUFFMAN_ONLY) {
|
|
1405
|
+
match_length = longest_match(hash_head);
|
|
1410
1406
|
}
|
|
1411
|
-
|
|
1412
|
-
|
|
1407
|
+
// longest_match() sets match_start
|
|
1408
|
+
}
|
|
1409
|
+
if (match_length >= MIN_MATCH) {
|
|
1410
|
+
// check_match(strstart, match_start, match_length);
|
|
1413
1411
|
|
|
1414
|
-
|
|
1412
|
+
bflush = _tr_tally(strstart - match_start, match_length - MIN_MATCH);
|
|
1415
1413
|
|
|
1416
|
-
|
|
1414
|
+
lookahead -= match_length;
|
|
1417
1415
|
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1416
|
+
// Insert new strings in the hash table only if the match length
|
|
1417
|
+
// is not too large. This saves time but degrades compression.
|
|
1418
|
+
if (match_length <= max_lazy_match && lookahead >= MIN_MATCH) {
|
|
1419
|
+
match_length--; // string at strstart already in hash table
|
|
1420
|
+
do {
|
|
1421
|
+
strstart++;
|
|
1424
1422
|
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1423
|
+
ins_h = ((ins_h << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1424
|
+
// prev[strstart&w_mask]=hash_head=head[ins_h];
|
|
1425
|
+
hash_head = (head[ins_h] & 0xffff);
|
|
1426
|
+
prev[strstart & w_mask] = head[ins_h];
|
|
1427
|
+
head[ins_h] = strstart;
|
|
1430
1428
|
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
} else {
|
|
1436
|
-
strstart += match_length;
|
|
1437
|
-
match_length = 0;
|
|
1438
|
-
ins_h = win[strstart] & 0xff;
|
|
1439
|
-
|
|
1440
|
-
ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
|
|
1441
|
-
// If lookahead < MIN_MATCH, ins_h is garbage, but it does
|
|
1442
|
-
// not
|
|
1443
|
-
// matter since it will be recomputed at next deflate call.
|
|
1444
|
-
}
|
|
1429
|
+
// strstart never exceeds WSIZE-MAX_MATCH, so there are
|
|
1430
|
+
// always MIN_MATCH bytes ahead.
|
|
1431
|
+
} while (--match_length !== 0);
|
|
1432
|
+
strstart++;
|
|
1445
1433
|
} else {
|
|
1446
|
-
|
|
1434
|
+
strstart += match_length;
|
|
1435
|
+
match_length = 0;
|
|
1436
|
+
ins_h = win[strstart] & 0xff;
|
|
1447
1437
|
|
|
1448
|
-
|
|
1449
|
-
lookahead
|
|
1450
|
-
|
|
1438
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
|
|
1439
|
+
// If lookahead < MIN_MATCH, ins_h is garbage, but it does
|
|
1440
|
+
// not
|
|
1441
|
+
// matter since it will be recomputed at next deflate call.
|
|
1451
1442
|
}
|
|
1452
|
-
|
|
1443
|
+
} else {
|
|
1444
|
+
// No match, output a literal byte
|
|
1453
1445
|
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
}
|
|
1446
|
+
bflush = _tr_tally(0, win[strstart] & 0xff);
|
|
1447
|
+
lookahead--;
|
|
1448
|
+
strstart++;
|
|
1458
1449
|
}
|
|
1450
|
+
if (bflush) {
|
|
1459
1451
|
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
if (flush == Z_FINISH)
|
|
1463
|
-
return FinishStarted;
|
|
1464
|
-
else
|
|
1452
|
+
flush_block_only(false);
|
|
1453
|
+
if (strm.avail_out === 0)
|
|
1465
1454
|
return NeedMore;
|
|
1466
1455
|
}
|
|
1467
|
-
return flush == Z_FINISH ? FinishDone : BlockDone;
|
|
1468
1456
|
}
|
|
1469
1457
|
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
// Process the input block.
|
|
1480
|
-
// eslint-disable-next-line no-constant-condition
|
|
1481
|
-
while (true) {
|
|
1482
|
-
// Make sure that we always have enough lookahead, except
|
|
1483
|
-
// at the end of the input file. We need MAX_MATCH bytes
|
|
1484
|
-
// for the next match, plus MIN_MATCH bytes to insert the
|
|
1485
|
-
// string following the next match.
|
|
1486
|
-
|
|
1487
|
-
if (lookahead < MIN_LOOKAHEAD) {
|
|
1488
|
-
fill_window();
|
|
1489
|
-
if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
|
|
1490
|
-
return NeedMore;
|
|
1491
|
-
}
|
|
1492
|
-
if (lookahead === 0)
|
|
1493
|
-
break; // flush the current block
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
// Insert the string win[strstart .. strstart+2] in the
|
|
1497
|
-
// dictionary, and set hash_head to the head of the hash chain:
|
|
1458
|
+
flush_block_only(flush == Z_FINISH);
|
|
1459
|
+
if (strm.avail_out === 0) {
|
|
1460
|
+
if (flush == Z_FINISH)
|
|
1461
|
+
return FinishStarted;
|
|
1462
|
+
else
|
|
1463
|
+
return NeedMore;
|
|
1464
|
+
}
|
|
1465
|
+
return flush == Z_FINISH ? FinishDone : BlockDone;
|
|
1466
|
+
}
|
|
1498
1467
|
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1468
|
+
// Same as above, but achieves better compression. We use a lazy
|
|
1469
|
+
// evaluation for matches: a match is finally adopted only if there is
|
|
1470
|
+
// no better match at the next win position.
|
|
1471
|
+
function deflate_slow(flush) {
|
|
1472
|
+
// short hash_head = 0; // head of hash chain
|
|
1473
|
+
let hash_head = 0; // head of hash chain
|
|
1474
|
+
let bflush; // set if current block must be flushed
|
|
1475
|
+
let max_insert;
|
|
1476
|
+
|
|
1477
|
+
// Process the input block.
|
|
1478
|
+
// eslint-disable-next-line no-constant-condition
|
|
1479
|
+
while (true) {
|
|
1480
|
+
// Make sure that we always have enough lookahead, except
|
|
1481
|
+
// at the end of the input file. We need MAX_MATCH bytes
|
|
1482
|
+
// for the next match, plus MIN_MATCH bytes to insert the
|
|
1483
|
+
// string following the next match.
|
|
1484
|
+
|
|
1485
|
+
if (lookahead < MIN_LOOKAHEAD) {
|
|
1486
|
+
fill_window();
|
|
1487
|
+
if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
|
|
1488
|
+
return NeedMore;
|
|
1505
1489
|
}
|
|
1490
|
+
if (lookahead === 0)
|
|
1491
|
+
break; // flush the current block
|
|
1492
|
+
}
|
|
1506
1493
|
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
prev_match = match_start;
|
|
1510
|
-
match_length = MIN_MATCH - 1;
|
|
1494
|
+
// Insert the string win[strstart .. strstart+2] in the
|
|
1495
|
+
// dictionary, and set hash_head to the head of the hash chain:
|
|
1511
1496
|
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1497
|
+
if (lookahead >= MIN_MATCH) {
|
|
1498
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1499
|
+
// prev[strstart&w_mask]=hash_head=head[ins_h];
|
|
1500
|
+
hash_head = (head[ins_h] & 0xffff);
|
|
1501
|
+
prev[strstart & w_mask] = head[ins_h];
|
|
1502
|
+
head[ins_h] = strstart;
|
|
1503
|
+
}
|
|
1516
1504
|
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1505
|
+
// Find the longest match, discarding those <= prev_length.
|
|
1506
|
+
prev_length = match_length;
|
|
1507
|
+
prev_match = match_start;
|
|
1508
|
+
match_length = MIN_MATCH - 1;
|
|
1521
1509
|
|
|
1522
|
-
|
|
1510
|
+
if (hash_head !== 0 && prev_length < max_lazy_match && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
|
|
1511
|
+
// To simplify the code, we prevent matches with the string
|
|
1512
|
+
// of win index 0 (in particular we have to avoid a match
|
|
1513
|
+
// of the string with itself at the start of the input file).
|
|
1523
1514
|
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
match_length = MIN_MATCH - 1;
|
|
1527
|
-
}
|
|
1515
|
+
if (strategy != Z_HUFFMAN_ONLY) {
|
|
1516
|
+
match_length = longest_match(hash_head);
|
|
1528
1517
|
}
|
|
1518
|
+
// longest_match() sets match_start
|
|
1529
1519
|
|
|
1530
|
-
|
|
1531
|
-
// match is not better, output the previous match:
|
|
1532
|
-
if (prev_length >= MIN_MATCH && match_length <= prev_length) {
|
|
1533
|
-
max_insert = strstart + lookahead - MIN_MATCH;
|
|
1534
|
-
// Do not insert strings in hash table beyond this.
|
|
1535
|
-
|
|
1536
|
-
// check_match(strstart-1, prev_match, prev_length);
|
|
1537
|
-
|
|
1538
|
-
bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
|
|
1520
|
+
if (match_length <= 5 && (strategy == Z_FILTERED || (match_length == MIN_MATCH && strstart - match_start > 4096))) {
|
|
1539
1521
|
|
|
1540
|
-
//
|
|
1541
|
-
//
|
|
1542
|
-
// enough lookahead, the last two strings are not inserted in
|
|
1543
|
-
// the hash table.
|
|
1544
|
-
lookahead -= prev_length - 1;
|
|
1545
|
-
prev_length -= 2;
|
|
1546
|
-
do {
|
|
1547
|
-
if (++strstart <= max_insert) {
|
|
1548
|
-
ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1549
|
-
// prev[strstart&w_mask]=hash_head=head[ins_h];
|
|
1550
|
-
hash_head = (head[ins_h] & 0xffff);
|
|
1551
|
-
prev[strstart & w_mask] = head[ins_h];
|
|
1552
|
-
head[ins_h] = strstart;
|
|
1553
|
-
}
|
|
1554
|
-
} while (--prev_length !== 0);
|
|
1555
|
-
match_available = 0;
|
|
1522
|
+
// If prev_match is also MIN_MATCH, match_start is garbage
|
|
1523
|
+
// but we will ignore the current match anyway.
|
|
1556
1524
|
match_length = MIN_MATCH - 1;
|
|
1557
|
-
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1558
1527
|
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
} else if (match_available !== 0) {
|
|
1528
|
+
// If there was a match at the previous step and the current
|
|
1529
|
+
// match is not better, output the previous match:
|
|
1530
|
+
if (prev_length >= MIN_MATCH && match_length <= prev_length) {
|
|
1531
|
+
max_insert = strstart + lookahead - MIN_MATCH;
|
|
1532
|
+
// Do not insert strings in hash table beyond this.
|
|
1565
1533
|
|
|
1566
|
-
|
|
1567
|
-
// single literal. If there was a match but the current match
|
|
1568
|
-
// is longer, truncate the previous match to a single literal.
|
|
1534
|
+
// check_match(strstart-1, prev_match, prev_length);
|
|
1569
1535
|
|
|
1570
|
-
|
|
1536
|
+
bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
|
|
1571
1537
|
|
|
1572
|
-
|
|
1573
|
-
|
|
1538
|
+
// Insert in hash table all strings up to the end of the match.
|
|
1539
|
+
// strstart-1 and strstart are already inserted. If there is not
|
|
1540
|
+
// enough lookahead, the last two strings are not inserted in
|
|
1541
|
+
// the hash table.
|
|
1542
|
+
lookahead -= prev_length - 1;
|
|
1543
|
+
prev_length -= 2;
|
|
1544
|
+
do {
|
|
1545
|
+
if (++strstart <= max_insert) {
|
|
1546
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1547
|
+
// prev[strstart&w_mask]=hash_head=head[ins_h];
|
|
1548
|
+
hash_head = (head[ins_h] & 0xffff);
|
|
1549
|
+
prev[strstart & w_mask] = head[ins_h];
|
|
1550
|
+
head[ins_h] = strstart;
|
|
1574
1551
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1552
|
+
} while (--prev_length !== 0);
|
|
1553
|
+
match_available = 0;
|
|
1554
|
+
match_length = MIN_MATCH - 1;
|
|
1555
|
+
strstart++;
|
|
1556
|
+
|
|
1557
|
+
if (bflush) {
|
|
1558
|
+
flush_block_only(false);
|
|
1577
1559
|
if (strm.avail_out === 0)
|
|
1578
1560
|
return NeedMore;
|
|
1579
|
-
} else {
|
|
1580
|
-
// There is no previous match to compare with, wait for
|
|
1581
|
-
// the next step to decide.
|
|
1582
|
-
|
|
1583
|
-
match_available = 1;
|
|
1584
|
-
strstart++;
|
|
1585
|
-
lookahead--;
|
|
1586
1561
|
}
|
|
1587
|
-
}
|
|
1562
|
+
} else if (match_available !== 0) {
|
|
1563
|
+
|
|
1564
|
+
// If there was no match at the previous position, output a
|
|
1565
|
+
// single literal. If there was a match but the current match
|
|
1566
|
+
// is longer, truncate the previous match to a single literal.
|
|
1588
1567
|
|
|
1589
|
-
if (match_available !== 0) {
|
|
1590
1568
|
bflush = _tr_tally(0, win[strstart - 1] & 0xff);
|
|
1591
|
-
match_available = 0;
|
|
1592
|
-
}
|
|
1593
|
-
flush_block_only(flush == Z_FINISH);
|
|
1594
1569
|
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1570
|
+
if (bflush) {
|
|
1571
|
+
flush_block_only(false);
|
|
1572
|
+
}
|
|
1573
|
+
strstart++;
|
|
1574
|
+
lookahead--;
|
|
1575
|
+
if (strm.avail_out === 0)
|
|
1599
1576
|
return NeedMore;
|
|
1577
|
+
} else {
|
|
1578
|
+
// There is no previous match to compare with, wait for
|
|
1579
|
+
// the next step to decide.
|
|
1580
|
+
|
|
1581
|
+
match_available = 1;
|
|
1582
|
+
strstart++;
|
|
1583
|
+
lookahead--;
|
|
1600
1584
|
}
|
|
1585
|
+
}
|
|
1601
1586
|
|
|
1602
|
-
|
|
1587
|
+
if (match_available !== 0) {
|
|
1588
|
+
bflush = _tr_tally(0, win[strstart - 1] & 0xff);
|
|
1589
|
+
match_available = 0;
|
|
1603
1590
|
}
|
|
1591
|
+
flush_block_only(flush == Z_FINISH);
|
|
1604
1592
|
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1593
|
+
if (strm.avail_out === 0) {
|
|
1594
|
+
if (flush == Z_FINISH)
|
|
1595
|
+
return FinishStarted;
|
|
1596
|
+
else
|
|
1597
|
+
return NeedMore;
|
|
1598
|
+
}
|
|
1608
1599
|
|
|
1609
|
-
|
|
1610
|
-
|
|
1600
|
+
return flush == Z_FINISH ? FinishDone : BlockDone;
|
|
1601
|
+
}
|
|
1611
1602
|
|
|
1612
|
-
|
|
1603
|
+
function deflateReset(strm) {
|
|
1604
|
+
strm.total_in = strm.total_out = 0;
|
|
1605
|
+
strm.msg = null; //
|
|
1613
1606
|
|
|
1614
|
-
|
|
1607
|
+
that.pending = 0;
|
|
1608
|
+
that.pending_out = 0;
|
|
1615
1609
|
|
|
1616
|
-
|
|
1617
|
-
lm_init();
|
|
1618
|
-
return Z_OK;
|
|
1619
|
-
}
|
|
1610
|
+
status = BUSY_STATE;
|
|
1620
1611
|
|
|
1621
|
-
|
|
1622
|
-
if (!_method)
|
|
1623
|
-
_method = Z_DEFLATED;
|
|
1624
|
-
if (!memLevel)
|
|
1625
|
-
memLevel = DEF_MEM_LEVEL;
|
|
1626
|
-
if (!_strategy)
|
|
1627
|
-
_strategy = Z_DEFAULT_STRATEGY;
|
|
1612
|
+
last_flush = Z_NO_FLUSH;
|
|
1628
1613
|
|
|
1629
|
-
|
|
1614
|
+
tr_init();
|
|
1615
|
+
lm_init();
|
|
1616
|
+
return Z_OK;
|
|
1617
|
+
}
|
|
1630
1618
|
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1619
|
+
that.deflateInit = function (strm, _level, bits, _method, memLevel, _strategy) {
|
|
1620
|
+
if (!_method)
|
|
1621
|
+
_method = Z_DEFLATED;
|
|
1622
|
+
if (!memLevel)
|
|
1623
|
+
memLevel = DEF_MEM_LEVEL;
|
|
1624
|
+
if (!_strategy)
|
|
1625
|
+
_strategy = Z_DEFAULT_STRATEGY;
|
|
1636
1626
|
|
|
1637
|
-
|
|
1627
|
+
// byte[] my_version=ZLIB_VERSION;
|
|
1638
1628
|
|
|
1639
|
-
|
|
1640
|
-
|
|
1629
|
+
//
|
|
1630
|
+
// if (!version || version[0] != my_version[0]
|
|
1631
|
+
// || stream_size != sizeof(z_stream)) {
|
|
1632
|
+
// return Z_VERSION_ERROR;
|
|
1633
|
+
// }
|
|
1641
1634
|
|
|
1642
|
-
|
|
1643
|
-
|| _strategy > Z_HUFFMAN_ONLY) {
|
|
1644
|
-
return Z_STREAM_ERROR;
|
|
1645
|
-
}
|
|
1635
|
+
strm.msg = null;
|
|
1646
1636
|
|
|
1647
|
-
|
|
1637
|
+
if (_level == Z_DEFAULT_COMPRESSION)
|
|
1638
|
+
_level = 6;
|
|
1648
1639
|
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1640
|
+
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || _method != Z_DEFLATED || bits < 9 || bits > 15 || _level < 0 || _level > 9 || _strategy < 0
|
|
1641
|
+
|| _strategy > Z_HUFFMAN_ONLY) {
|
|
1642
|
+
return Z_STREAM_ERROR;
|
|
1643
|
+
}
|
|
1652
1644
|
|
|
1653
|
-
|
|
1654
|
-
hash_size = 1 << hash_bits;
|
|
1655
|
-
hash_mask = hash_size - 1;
|
|
1656
|
-
hash_shift = Math.floor((hash_bits + MIN_MATCH - 1) / MIN_MATCH);
|
|
1645
|
+
strm.dstate = that;
|
|
1657
1646
|
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1647
|
+
w_bits = bits;
|
|
1648
|
+
w_size = 1 << w_bits;
|
|
1649
|
+
w_mask = w_size - 1;
|
|
1661
1650
|
|
|
1662
|
-
|
|
1651
|
+
hash_bits = memLevel + 7;
|
|
1652
|
+
hash_size = 1 << hash_bits;
|
|
1653
|
+
hash_mask = hash_size - 1;
|
|
1654
|
+
hash_shift = Math.floor((hash_bits + MIN_MATCH - 1) / MIN_MATCH);
|
|
1663
1655
|
|
|
1664
|
-
|
|
1665
|
-
|
|
1656
|
+
win = new Uint8Array(w_size * 2);
|
|
1657
|
+
prev = [];
|
|
1658
|
+
head = [];
|
|
1666
1659
|
|
|
1667
|
-
|
|
1668
|
-
that.lc_buf = new Uint8Array(lit_bufsize);
|
|
1660
|
+
lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
|
|
1669
1661
|
|
|
1670
|
-
|
|
1662
|
+
that.pending_buf = new Uint8Array(lit_bufsize * 4);
|
|
1663
|
+
pending_buf_size = lit_bufsize * 4;
|
|
1671
1664
|
|
|
1672
|
-
|
|
1665
|
+
that.dist_buf = new Uint16Array(lit_bufsize);
|
|
1666
|
+
that.lc_buf = new Uint8Array(lit_bufsize);
|
|
1673
1667
|
|
|
1674
|
-
|
|
1675
|
-
};
|
|
1668
|
+
level = _level;
|
|
1676
1669
|
|
|
1677
|
-
|
|
1678
|
-
if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE) {
|
|
1679
|
-
return Z_STREAM_ERROR;
|
|
1680
|
-
}
|
|
1681
|
-
// Deallocate in reverse order of allocations:
|
|
1682
|
-
that.lc_buf = null;
|
|
1683
|
-
that.dist_buf = null;
|
|
1684
|
-
that.pending_buf = null;
|
|
1685
|
-
head = null;
|
|
1686
|
-
prev = null;
|
|
1687
|
-
win = null;
|
|
1688
|
-
// free
|
|
1689
|
-
that.dstate = null;
|
|
1690
|
-
return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
|
|
1691
|
-
};
|
|
1692
|
-
|
|
1693
|
-
that.deflateParams = function (strm, _level, _strategy) {
|
|
1694
|
-
let err = Z_OK;
|
|
1695
|
-
|
|
1696
|
-
if (_level == Z_DEFAULT_COMPRESSION) {
|
|
1697
|
-
_level = 6;
|
|
1698
|
-
}
|
|
1699
|
-
if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
|
|
1700
|
-
return Z_STREAM_ERROR;
|
|
1701
|
-
}
|
|
1670
|
+
strategy = _strategy;
|
|
1702
1671
|
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
err = strm.deflate(Z_PARTIAL_FLUSH);
|
|
1706
|
-
}
|
|
1672
|
+
return deflateReset(strm);
|
|
1673
|
+
};
|
|
1707
1674
|
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1675
|
+
that.deflateEnd = function () {
|
|
1676
|
+
if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE) {
|
|
1677
|
+
return Z_STREAM_ERROR;
|
|
1678
|
+
}
|
|
1679
|
+
// Deallocate in reverse order of allocations:
|
|
1680
|
+
that.lc_buf = null;
|
|
1681
|
+
that.dist_buf = null;
|
|
1682
|
+
that.pending_buf = null;
|
|
1683
|
+
head = null;
|
|
1684
|
+
prev = null;
|
|
1685
|
+
win = null;
|
|
1686
|
+
// free
|
|
1687
|
+
that.dstate = null;
|
|
1688
|
+
return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
|
|
1689
|
+
};
|
|
1718
1690
|
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
let n, index = 0;
|
|
1691
|
+
that.deflateParams = function (strm, _level, _strategy) {
|
|
1692
|
+
let err = Z_OK;
|
|
1722
1693
|
|
|
1723
|
-
|
|
1724
|
-
|
|
1694
|
+
if (_level == Z_DEFAULT_COMPRESSION) {
|
|
1695
|
+
_level = 6;
|
|
1696
|
+
}
|
|
1697
|
+
if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
|
|
1698
|
+
return Z_STREAM_ERROR;
|
|
1699
|
+
}
|
|
1725
1700
|
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
index = dictLength - length; // use the tail of the dictionary
|
|
1731
|
-
}
|
|
1732
|
-
win.set(dictionary.subarray(index, index + length), 0);
|
|
1701
|
+
if (config_table[level].func != config_table[_level].func && strm.total_in !== 0) {
|
|
1702
|
+
// Flush the last buffer:
|
|
1703
|
+
err = strm.deflate(Z_PARTIAL_FLUSH);
|
|
1704
|
+
}
|
|
1733
1705
|
|
|
1734
|
-
|
|
1735
|
-
|
|
1706
|
+
if (level != _level) {
|
|
1707
|
+
level = _level;
|
|
1708
|
+
max_lazy_match = config_table[level].max_lazy;
|
|
1709
|
+
good_match = config_table[level].good_length;
|
|
1710
|
+
nice_match = config_table[level].nice_length;
|
|
1711
|
+
max_chain_length = config_table[level].max_chain;
|
|
1712
|
+
}
|
|
1713
|
+
strategy = _strategy;
|
|
1714
|
+
return err;
|
|
1715
|
+
};
|
|
1736
1716
|
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1717
|
+
that.deflateSetDictionary = function (_strm, dictionary, dictLength) {
|
|
1718
|
+
let length = dictLength;
|
|
1719
|
+
let n, index = 0;
|
|
1740
1720
|
|
|
1741
|
-
|
|
1742
|
-
|
|
1721
|
+
if (!dictionary || status != INIT_STATE)
|
|
1722
|
+
return Z_STREAM_ERROR;
|
|
1743
1723
|
|
|
1744
|
-
|
|
1745
|
-
ins_h = (((ins_h) << hash_shift) ^ (win[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1746
|
-
prev[n & w_mask] = head[ins_h];
|
|
1747
|
-
head[ins_h] = n;
|
|
1748
|
-
}
|
|
1724
|
+
if (length < MIN_MATCH)
|
|
1749
1725
|
return Z_OK;
|
|
1750
|
-
|
|
1726
|
+
if (length > w_size - MIN_LOOKAHEAD) {
|
|
1727
|
+
length = w_size - MIN_LOOKAHEAD;
|
|
1728
|
+
index = dictLength - length; // use the tail of the dictionary
|
|
1729
|
+
}
|
|
1730
|
+
win.set(dictionary.subarray(index, index + length), 0);
|
|
1751
1731
|
|
|
1752
|
-
|
|
1753
|
-
|
|
1732
|
+
strstart = length;
|
|
1733
|
+
block_start = length;
|
|
1754
1734
|
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1735
|
+
// Insert all strings in the hash table (except for the last two bytes).
|
|
1736
|
+
// s->lookahead stays null, so s->ins_h will be recomputed at the next
|
|
1737
|
+
// call of fill_window.
|
|
1758
1738
|
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
return Z_STREAM_ERROR;
|
|
1762
|
-
}
|
|
1763
|
-
if (_strm.avail_out === 0) {
|
|
1764
|
-
_strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
|
|
1765
|
-
return Z_BUF_ERROR;
|
|
1766
|
-
}
|
|
1739
|
+
ins_h = win[0] & 0xff;
|
|
1740
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[1] & 0xff)) & hash_mask;
|
|
1767
1741
|
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1742
|
+
for (n = 0; n <= length - MIN_MATCH; n++) {
|
|
1743
|
+
ins_h = (((ins_h) << hash_shift) ^ (win[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
|
|
1744
|
+
prev[n & w_mask] = head[ins_h];
|
|
1745
|
+
head[ins_h] = n;
|
|
1746
|
+
}
|
|
1747
|
+
return Z_OK;
|
|
1748
|
+
};
|
|
1771
1749
|
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
|
|
1775
|
-
level_flags = ((level - 1) & 0xff) >> 1;
|
|
1750
|
+
that.deflate = function (_strm, flush) {
|
|
1751
|
+
let i, header, level_flags, old_flush, bstate;
|
|
1776
1752
|
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
if (strstart !== 0)
|
|
1781
|
-
header |= PRESET_DICT;
|
|
1782
|
-
header += 31 - (header % 31);
|
|
1753
|
+
if (flush > Z_FINISH || flush < 0) {
|
|
1754
|
+
return Z_STREAM_ERROR;
|
|
1755
|
+
}
|
|
1783
1756
|
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1757
|
+
if (!_strm.next_out || (!_strm.next_in && _strm.avail_in !== 0) || (status == FINISH_STATE && flush != Z_FINISH)) {
|
|
1758
|
+
_strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
|
|
1759
|
+
return Z_STREAM_ERROR;
|
|
1760
|
+
}
|
|
1761
|
+
if (_strm.avail_out === 0) {
|
|
1762
|
+
_strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
|
|
1763
|
+
return Z_BUF_ERROR;
|
|
1764
|
+
}
|
|
1787
1765
|
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1766
|
+
strm = _strm; // just in case
|
|
1767
|
+
old_flush = last_flush;
|
|
1768
|
+
last_flush = flush;
|
|
1769
|
+
|
|
1770
|
+
// Write the zlib header
|
|
1771
|
+
if (status == INIT_STATE) {
|
|
1772
|
+
header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
|
|
1773
|
+
level_flags = ((level - 1) & 0xff) >> 1;
|
|
1774
|
+
|
|
1775
|
+
if (level_flags > 3)
|
|
1776
|
+
level_flags = 3;
|
|
1777
|
+
header |= (level_flags << 6);
|
|
1778
|
+
if (strstart !== 0)
|
|
1779
|
+
header |= PRESET_DICT;
|
|
1780
|
+
header += 31 - (header % 31);
|
|
1781
|
+
|
|
1782
|
+
status = BUSY_STATE;
|
|
1783
|
+
putShortMSB(header);
|
|
1784
|
+
}
|
|
1801
1785
|
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1786
|
+
// Flush as much pending output as possible
|
|
1787
|
+
if (that.pending !== 0) {
|
|
1788
|
+
strm.flush_pending();
|
|
1789
|
+
if (strm.avail_out === 0) {
|
|
1790
|
+
// console.log(" avail_out==0");
|
|
1791
|
+
// Since avail_out is 0, deflate will be called again with
|
|
1792
|
+
// more output space, but possibly with both pending and
|
|
1793
|
+
// avail_in equal to zero. There won't be anything to do,
|
|
1794
|
+
// but this is not an error situation so make sure we
|
|
1795
|
+
// return OK instead of BUF_ERROR at next call of deflate:
|
|
1796
|
+
last_flush = -1;
|
|
1797
|
+
return Z_OK;
|
|
1809
1798
|
}
|
|
1810
1799
|
|
|
1811
|
-
//
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1800
|
+
// Make sure there is something to do and avoid duplicate
|
|
1801
|
+
// consecutive
|
|
1802
|
+
// flushes. For repeated and useless calls with Z_FINISH, we keep
|
|
1803
|
+
// returning Z_STREAM_END instead of Z_BUFF_ERROR.
|
|
1804
|
+
} else if (strm.avail_in === 0 && flush <= old_flush && flush != Z_FINISH) {
|
|
1805
|
+
strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
|
|
1806
|
+
return Z_BUF_ERROR;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
// User must not provide more input after the first FINISH:
|
|
1810
|
+
if (status == FINISH_STATE && strm.avail_in !== 0) {
|
|
1811
|
+
_strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
|
|
1812
|
+
return Z_BUF_ERROR;
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
// Start a new block or continue the current one.
|
|
1816
|
+
if (strm.avail_in !== 0 || lookahead !== 0 || (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
|
|
1817
|
+
bstate = -1;
|
|
1818
|
+
switch (config_table[level].func) {
|
|
1819
|
+
case STORED:
|
|
1820
|
+
bstate = deflate_stored(flush);
|
|
1821
|
+
break;
|
|
1822
|
+
case FAST:
|
|
1823
|
+
bstate = deflate_fast(flush);
|
|
1824
|
+
break;
|
|
1825
|
+
case SLOW:
|
|
1826
|
+
bstate = deflate_slow(flush);
|
|
1827
|
+
break;
|
|
1828
|
+
default:
|
|
1815
1829
|
}
|
|
1816
1830
|
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
break;
|
|
1824
|
-
case FAST:
|
|
1825
|
-
bstate = deflate_fast(flush);
|
|
1826
|
-
break;
|
|
1827
|
-
case SLOW:
|
|
1828
|
-
bstate = deflate_slow(flush);
|
|
1829
|
-
break;
|
|
1830
|
-
default:
|
|
1831
|
+
if (bstate == FinishStarted || bstate == FinishDone) {
|
|
1832
|
+
status = FINISH_STATE;
|
|
1833
|
+
}
|
|
1834
|
+
if (bstate == NeedMore || bstate == FinishStarted) {
|
|
1835
|
+
if (strm.avail_out === 0) {
|
|
1836
|
+
last_flush = -1; // avoid BUF_ERROR next call, see above
|
|
1831
1837
|
}
|
|
1838
|
+
return Z_OK;
|
|
1839
|
+
// If flush != Z_NO_FLUSH && avail_out === 0, the next call
|
|
1840
|
+
// of deflate should use the same flush parameter to make sure
|
|
1841
|
+
// that the flush is complete. So we don't have to output an
|
|
1842
|
+
// empty block here, this will be done at next call. This also
|
|
1843
|
+
// ensures that for a very small output buffer, we emit at most
|
|
1844
|
+
// one empty block.
|
|
1845
|
+
}
|
|
1832
1846
|
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1847
|
+
if (bstate == BlockDone) {
|
|
1848
|
+
if (flush == Z_PARTIAL_FLUSH) {
|
|
1849
|
+
_tr_align();
|
|
1850
|
+
} else { // FULL_FLUSH or SYNC_FLUSH
|
|
1851
|
+
_tr_stored_block(0, 0, false);
|
|
1852
|
+
// For a full flush, this empty block will be recognized
|
|
1853
|
+
// as a special marker by inflate_sync().
|
|
1854
|
+
if (flush == Z_FULL_FLUSH) {
|
|
1855
|
+
// state.head[s.hash_size-1]=0;
|
|
1856
|
+
for (i = 0; i < hash_size/*-1*/; i++)
|
|
1857
|
+
// forget history
|
|
1858
|
+
head[i] = 0;
|
|
1839
1859
|
}
|
|
1840
|
-
return Z_OK;
|
|
1841
|
-
// If flush != Z_NO_FLUSH && avail_out === 0, the next call
|
|
1842
|
-
// of deflate should use the same flush parameter to make sure
|
|
1843
|
-
// that the flush is complete. So we don't have to output an
|
|
1844
|
-
// empty block here, this will be done at next call. This also
|
|
1845
|
-
// ensures that for a very small output buffer, we emit at most
|
|
1846
|
-
// one empty block.
|
|
1847
1860
|
}
|
|
1848
|
-
|
|
1849
|
-
if (
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
} else { // FULL_FLUSH or SYNC_FLUSH
|
|
1853
|
-
_tr_stored_block(0, 0, false);
|
|
1854
|
-
// For a full flush, this empty block will be recognized
|
|
1855
|
-
// as a special marker by inflate_sync().
|
|
1856
|
-
if (flush == Z_FULL_FLUSH) {
|
|
1857
|
-
// state.head[s.hash_size-1]=0;
|
|
1858
|
-
for (i = 0; i < hash_size/*-1*/; i++)
|
|
1859
|
-
// forget history
|
|
1860
|
-
head[i] = 0;
|
|
1861
|
-
}
|
|
1862
|
-
}
|
|
1863
|
-
strm.flush_pending();
|
|
1864
|
-
if (strm.avail_out === 0) {
|
|
1865
|
-
last_flush = -1; // avoid BUF_ERROR at next call, see above
|
|
1866
|
-
return Z_OK;
|
|
1867
|
-
}
|
|
1861
|
+
strm.flush_pending();
|
|
1862
|
+
if (strm.avail_out === 0) {
|
|
1863
|
+
last_flush = -1; // avoid BUF_ERROR at next call, see above
|
|
1864
|
+
return Z_OK;
|
|
1868
1865
|
}
|
|
1869
1866
|
}
|
|
1867
|
+
}
|
|
1870
1868
|
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1869
|
+
if (flush != Z_FINISH)
|
|
1870
|
+
return Z_OK;
|
|
1871
|
+
return Z_STREAM_END;
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
// ZStream
|
|
1876
|
+
|
|
1877
|
+
function ZStream() {
|
|
1878
|
+
const that = this;
|
|
1879
|
+
that.next_in_index = 0;
|
|
1880
|
+
that.next_out_index = 0;
|
|
1881
|
+
// that.next_in; // next input byte
|
|
1882
|
+
that.avail_in = 0; // number of bytes available at next_in
|
|
1883
|
+
that.total_in = 0; // total nb of input bytes read so far
|
|
1884
|
+
// that.next_out; // next output byte should be put there
|
|
1885
|
+
that.avail_out = 0; // remaining free space at next_out
|
|
1886
|
+
that.total_out = 0; // total nb of bytes output so far
|
|
1887
|
+
// that.msg;
|
|
1888
|
+
// that.dstate;
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
ZStream.prototype = {
|
|
1892
|
+
deflateInit(level, bits) {
|
|
1880
1893
|
const that = this;
|
|
1881
|
-
that.
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
that.
|
|
1885
|
-
|
|
1886
|
-
// that.next_out; // next output byte should be put there
|
|
1887
|
-
that.avail_out = 0; // remaining free space at next_out
|
|
1888
|
-
that.total_out = 0; // total nb of bytes output so far
|
|
1889
|
-
// that.msg;
|
|
1890
|
-
// that.dstate;
|
|
1891
|
-
}
|
|
1892
|
-
|
|
1893
|
-
ZStream.prototype = {
|
|
1894
|
-
deflateInit(level, bits) {
|
|
1895
|
-
const that = this;
|
|
1896
|
-
that.dstate = new Deflate();
|
|
1897
|
-
if (!bits)
|
|
1898
|
-
bits = MAX_BITS;
|
|
1899
|
-
return that.dstate.deflateInit(that, level, bits);
|
|
1900
|
-
},
|
|
1901
|
-
|
|
1902
|
-
deflate(flush) {
|
|
1903
|
-
const that = this;
|
|
1904
|
-
if (!that.dstate) {
|
|
1905
|
-
return Z_STREAM_ERROR;
|
|
1906
|
-
}
|
|
1907
|
-
return that.dstate.deflate(that, flush);
|
|
1908
|
-
},
|
|
1909
|
-
|
|
1910
|
-
deflateEnd() {
|
|
1911
|
-
const that = this;
|
|
1912
|
-
if (!that.dstate)
|
|
1913
|
-
return Z_STREAM_ERROR;
|
|
1914
|
-
const ret = that.dstate.deflateEnd();
|
|
1915
|
-
that.dstate = null;
|
|
1916
|
-
return ret;
|
|
1917
|
-
},
|
|
1918
|
-
|
|
1919
|
-
deflateParams(level, strategy) {
|
|
1920
|
-
const that = this;
|
|
1921
|
-
if (!that.dstate)
|
|
1922
|
-
return Z_STREAM_ERROR;
|
|
1923
|
-
return that.dstate.deflateParams(that, level, strategy);
|
|
1924
|
-
},
|
|
1925
|
-
|
|
1926
|
-
deflateSetDictionary(dictionary, dictLength) {
|
|
1927
|
-
const that = this;
|
|
1928
|
-
if (!that.dstate)
|
|
1929
|
-
return Z_STREAM_ERROR;
|
|
1930
|
-
return that.dstate.deflateSetDictionary(that, dictionary, dictLength);
|
|
1931
|
-
},
|
|
1932
|
-
|
|
1933
|
-
// Read a new buffer from the current input stream, update the
|
|
1934
|
-
// total number of bytes read. All deflate() input goes through
|
|
1935
|
-
// this function so some applications may wish to modify it to avoid
|
|
1936
|
-
// allocating a large strm->next_in buffer and copying from it.
|
|
1937
|
-
// (See also flush_pending()).
|
|
1938
|
-
read_buf(buf, start, size) {
|
|
1939
|
-
const that = this;
|
|
1940
|
-
let len = that.avail_in;
|
|
1941
|
-
if (len > size)
|
|
1942
|
-
len = size;
|
|
1943
|
-
if (len === 0)
|
|
1944
|
-
return 0;
|
|
1945
|
-
that.avail_in -= len;
|
|
1946
|
-
buf.set(that.next_in.subarray(that.next_in_index, that.next_in_index + len), start);
|
|
1947
|
-
that.next_in_index += len;
|
|
1948
|
-
that.total_in += len;
|
|
1949
|
-
return len;
|
|
1950
|
-
},
|
|
1951
|
-
|
|
1952
|
-
// Flush as much pending output as possible. All deflate() output goes
|
|
1953
|
-
// through this function so some applications may wish to modify it
|
|
1954
|
-
// to avoid allocating a large strm->next_out buffer and copying into it.
|
|
1955
|
-
// (See also read_buf()).
|
|
1956
|
-
flush_pending() {
|
|
1957
|
-
const that = this;
|
|
1958
|
-
let len = that.dstate.pending;
|
|
1959
|
-
|
|
1960
|
-
if (len > that.avail_out)
|
|
1961
|
-
len = that.avail_out;
|
|
1962
|
-
if (len === 0)
|
|
1963
|
-
return;
|
|
1894
|
+
that.dstate = new Deflate();
|
|
1895
|
+
if (!bits)
|
|
1896
|
+
bits = MAX_BITS;
|
|
1897
|
+
return that.dstate.deflateInit(that, level, bits);
|
|
1898
|
+
},
|
|
1964
1899
|
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
// that.next_out_index + ", " + len);
|
|
1970
|
-
// console.log("avail_out=" + that.avail_out);
|
|
1971
|
-
// }
|
|
1972
|
-
|
|
1973
|
-
that.next_out.set(that.dstate.pending_buf.subarray(that.dstate.pending_out, that.dstate.pending_out + len), that.next_out_index);
|
|
1974
|
-
|
|
1975
|
-
that.next_out_index += len;
|
|
1976
|
-
that.dstate.pending_out += len;
|
|
1977
|
-
that.total_out += len;
|
|
1978
|
-
that.avail_out -= len;
|
|
1979
|
-
that.dstate.pending -= len;
|
|
1980
|
-
if (that.dstate.pending === 0) {
|
|
1981
|
-
that.dstate.pending_out = 0;
|
|
1982
|
-
}
|
|
1900
|
+
deflate(flush) {
|
|
1901
|
+
const that = this;
|
|
1902
|
+
if (!that.dstate) {
|
|
1903
|
+
return Z_STREAM_ERROR;
|
|
1983
1904
|
}
|
|
1984
|
-
|
|
1905
|
+
return that.dstate.deflate(that, flush);
|
|
1906
|
+
},
|
|
1985
1907
|
|
|
1986
|
-
|
|
1908
|
+
deflateEnd() {
|
|
1909
|
+
const that = this;
|
|
1910
|
+
if (!that.dstate)
|
|
1911
|
+
return Z_STREAM_ERROR;
|
|
1912
|
+
const ret = that.dstate.deflateEnd();
|
|
1913
|
+
that.dstate = null;
|
|
1914
|
+
return ret;
|
|
1915
|
+
},
|
|
1916
|
+
|
|
1917
|
+
deflateParams(level, strategy) {
|
|
1918
|
+
const that = this;
|
|
1919
|
+
if (!that.dstate)
|
|
1920
|
+
return Z_STREAM_ERROR;
|
|
1921
|
+
return that.dstate.deflateParams(that, level, strategy);
|
|
1922
|
+
},
|
|
1987
1923
|
|
|
1988
|
-
|
|
1924
|
+
deflateSetDictionary(dictionary, dictLength) {
|
|
1989
1925
|
const that = this;
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
1926
|
+
if (!that.dstate)
|
|
1927
|
+
return Z_STREAM_ERROR;
|
|
1928
|
+
return that.dstate.deflateSetDictionary(that, dictionary, dictLength);
|
|
1929
|
+
},
|
|
1930
|
+
|
|
1931
|
+
// Read a new buffer from the current input stream, update the
|
|
1932
|
+
// total number of bytes read. All deflate() input goes through
|
|
1933
|
+
// this function so some applications may wish to modify it to avoid
|
|
1934
|
+
// allocating a large strm->next_in buffer and copying from it.
|
|
1935
|
+
// (See also flush_pending()).
|
|
1936
|
+
read_buf(buf, start, size) {
|
|
1937
|
+
const that = this;
|
|
1938
|
+
let len = that.avail_in;
|
|
1939
|
+
if (len > size)
|
|
1940
|
+
len = size;
|
|
1941
|
+
if (len === 0)
|
|
1942
|
+
return 0;
|
|
1943
|
+
that.avail_in -= len;
|
|
1944
|
+
buf.set(that.next_in.subarray(that.next_in_index, that.next_in_index + len), start);
|
|
1945
|
+
that.next_in_index += len;
|
|
1946
|
+
that.total_in += len;
|
|
1947
|
+
return len;
|
|
1948
|
+
},
|
|
1949
|
+
|
|
1950
|
+
// Flush as much pending output as possible. All deflate() output goes
|
|
1951
|
+
// through this function so some applications may wish to modify it
|
|
1952
|
+
// to avoid allocating a large strm->next_out buffer and copying into it.
|
|
1953
|
+
// (See also read_buf()).
|
|
1954
|
+
flush_pending() {
|
|
1955
|
+
const that = this;
|
|
1956
|
+
let len = that.dstate.pending;
|
|
1957
|
+
|
|
1958
|
+
if (len > that.avail_out)
|
|
1959
|
+
len = that.avail_out;
|
|
1960
|
+
if (len === 0)
|
|
1961
|
+
return;
|
|
1962
|
+
|
|
1963
|
+
// if (that.dstate.pending_buf.length <= that.dstate.pending_out || that.next_out.length <= that.next_out_index
|
|
1964
|
+
// || that.dstate.pending_buf.length < (that.dstate.pending_out + len) || that.next_out.length < (that.next_out_index +
|
|
1965
|
+
// len)) {
|
|
1966
|
+
// console.log(that.dstate.pending_buf.length + ", " + that.dstate.pending_out + ", " + that.next_out.length + ", " +
|
|
1967
|
+
// that.next_out_index + ", " + len);
|
|
1968
|
+
// console.log("avail_out=" + that.avail_out);
|
|
1969
|
+
// }
|
|
1970
|
+
|
|
1971
|
+
that.next_out.set(that.dstate.pending_buf.subarray(that.dstate.pending_out, that.dstate.pending_out + len), that.next_out_index);
|
|
1972
|
+
|
|
1973
|
+
that.next_out_index += len;
|
|
1974
|
+
that.dstate.pending_out += len;
|
|
1975
|
+
that.total_out += len;
|
|
1976
|
+
that.avail_out -= len;
|
|
1977
|
+
that.dstate.pending -= len;
|
|
1978
|
+
if (that.dstate.pending === 0) {
|
|
1979
|
+
that.dstate.pending_out = 0;
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
};
|
|
1983
|
+
|
|
1984
|
+
// Deflate
|
|
1985
|
+
|
|
1986
|
+
function ZipDeflate(options) {
|
|
1987
|
+
const that = this;
|
|
1988
|
+
const z = new ZStream();
|
|
1989
|
+
const bufsize = getMaximumCompressedSize(options && options.chunkSize ? options.chunkSize : 64 * 1024);
|
|
1990
|
+
const flush = Z_NO_FLUSH;
|
|
1991
|
+
const buf = new Uint8Array(bufsize);
|
|
1992
|
+
let level = options ? options.level : Z_DEFAULT_COMPRESSION;
|
|
1993
|
+
if (typeof level == "undefined")
|
|
1994
|
+
level = Z_DEFAULT_COMPRESSION;
|
|
1995
|
+
z.deflateInit(level);
|
|
1996
|
+
z.next_out = buf;
|
|
1997
|
+
|
|
1998
|
+
that.append = function (data, onprogress) {
|
|
1999
|
+
let err, array, lastIndex = 0, bufferIndex = 0, bufferSize = 0;
|
|
2000
|
+
const buffers = [];
|
|
2001
|
+
if (!data.length)
|
|
2002
|
+
return;
|
|
2003
|
+
z.next_in_index = 0;
|
|
2004
|
+
z.next_in = data;
|
|
2005
|
+
z.avail_in = data.length;
|
|
2006
|
+
do {
|
|
2007
|
+
z.next_out_index = 0;
|
|
2008
|
+
z.avail_out = bufsize;
|
|
2009
|
+
err = z.deflate(flush);
|
|
2010
|
+
if (err != Z_OK)
|
|
2011
|
+
throw new Error("deflating: " + z.msg);
|
|
2012
|
+
if (z.next_out_index)
|
|
2013
|
+
if (z.next_out_index == bufsize)
|
|
2014
|
+
buffers.push(new Uint8Array(buf));
|
|
2015
|
+
else
|
|
2046
2016
|
buffers.push(buf.slice(0, z.next_out_index));
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2017
|
+
bufferSize += z.next_out_index;
|
|
2018
|
+
if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
|
|
2019
|
+
onprogress(z.next_in_index);
|
|
2020
|
+
lastIndex = z.next_in_index;
|
|
2021
|
+
}
|
|
2022
|
+
} while (z.avail_in > 0 || z.avail_out === 0);
|
|
2023
|
+
if (buffers.length > 1) {
|
|
2050
2024
|
array = new Uint8Array(bufferSize);
|
|
2051
2025
|
buffers.forEach(function (chunk) {
|
|
2052
2026
|
array.set(chunk, bufferIndex);
|
|
2053
2027
|
bufferIndex += chunk.length;
|
|
2054
2028
|
});
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2029
|
+
} else {
|
|
2030
|
+
array = buffers[0] || new Uint8Array();
|
|
2031
|
+
}
|
|
2032
|
+
return array;
|
|
2033
|
+
};
|
|
2034
|
+
that.flush = function () {
|
|
2035
|
+
let err, array, bufferIndex = 0, bufferSize = 0;
|
|
2036
|
+
const buffers = [];
|
|
2037
|
+
do {
|
|
2038
|
+
z.next_out_index = 0;
|
|
2039
|
+
z.avail_out = bufsize;
|
|
2040
|
+
err = z.deflate(Z_FINISH);
|
|
2041
|
+
if (err != Z_STREAM_END && err != Z_OK)
|
|
2042
|
+
throw new Error("deflating: " + z.msg);
|
|
2043
|
+
if (bufsize - z.avail_out > 0)
|
|
2044
|
+
buffers.push(buf.slice(0, z.next_out_index));
|
|
2045
|
+
bufferSize += z.next_out_index;
|
|
2046
|
+
} while (z.avail_in > 0 || z.avail_out === 0);
|
|
2047
|
+
z.deflateEnd();
|
|
2048
|
+
array = new Uint8Array(bufferSize);
|
|
2049
|
+
buffers.forEach(function (chunk) {
|
|
2050
|
+
array.set(chunk, bufferIndex);
|
|
2051
|
+
bufferIndex += chunk.length;
|
|
2052
|
+
});
|
|
2053
|
+
return array;
|
|
2054
|
+
};
|
|
2055
|
+
}
|
|
2064
2056
|
|
|
2065
|
-
|
|
2057
|
+
function getMaximumCompressedSize(uncompressedSize) {
|
|
2058
|
+
return uncompressedSize + (5 * (Math.floor(uncompressedSize / 16383) + 1));
|
|
2059
|
+
}
|
|
2066
2060
|
|
|
2067
|
-
export
|
|
2061
|
+
export {
|
|
2062
|
+
ZipDeflate as Deflate
|
|
2063
|
+
};
|