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