source: trip-planner-front/node_modules/pako/lib/zlib/deflate.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 59.8 KB
RevLine 
[6a3a178]1'use strict';
2
3// (C) 1995-2013 Jean-loup Gailly and Mark Adler
4// (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
5//
6// This software is provided 'as-is', without any express or implied
7// warranty. In no event will the authors be held liable for any damages
8// arising from the use of this software.
9//
10// Permission is granted to anyone to use this software for any purpose,
11// including commercial applications, and to alter it and redistribute it
12// freely, subject to the following restrictions:
13//
14// 1. The origin of this software must not be misrepresented; you must not
15// claim that you wrote the original software. If you use this software
16// in a product, an acknowledgment in the product documentation would be
17// appreciated but is not required.
18// 2. Altered source versions must be plainly marked as such, and must not be
19// misrepresented as being the original software.
20// 3. This notice may not be removed or altered from any source distribution.
21
22var utils = require('../utils/common');
23var trees = require('./trees');
24var adler32 = require('./adler32');
25var crc32 = require('./crc32');
26var msg = require('./messages');
27
28/* Public constants ==========================================================*/
29/* ===========================================================================*/
30
31
32/* Allowed flush values; see deflate() and inflate() below for details */
33var Z_NO_FLUSH = 0;
34var Z_PARTIAL_FLUSH = 1;
35//var Z_SYNC_FLUSH = 2;
36var Z_FULL_FLUSH = 3;
37var Z_FINISH = 4;
38var Z_BLOCK = 5;
39//var Z_TREES = 6;
40
41
42/* Return codes for the compression/decompression functions. Negative values
43 * are errors, positive values are used for special but normal events.
44 */
45var Z_OK = 0;
46var Z_STREAM_END = 1;
47//var Z_NEED_DICT = 2;
48//var Z_ERRNO = -1;
49var Z_STREAM_ERROR = -2;
50var Z_DATA_ERROR = -3;
51//var Z_MEM_ERROR = -4;
52var Z_BUF_ERROR = -5;
53//var Z_VERSION_ERROR = -6;
54
55
56/* compression levels */
57//var Z_NO_COMPRESSION = 0;
58//var Z_BEST_SPEED = 1;
59//var Z_BEST_COMPRESSION = 9;
60var Z_DEFAULT_COMPRESSION = -1;
61
62
63var Z_FILTERED = 1;
64var Z_HUFFMAN_ONLY = 2;
65var Z_RLE = 3;
66var Z_FIXED = 4;
67var Z_DEFAULT_STRATEGY = 0;
68
69/* Possible values of the data_type field (though see inflate()) */
70//var Z_BINARY = 0;
71//var Z_TEXT = 1;
72//var Z_ASCII = 1; // = Z_TEXT
73var Z_UNKNOWN = 2;
74
75
76/* The deflate compression method */
77var Z_DEFLATED = 8;
78
79/*============================================================================*/
80
81
82var MAX_MEM_LEVEL = 9;
83/* Maximum value for memLevel in deflateInit2 */
84var MAX_WBITS = 15;
85/* 32K LZ77 window */
86var DEF_MEM_LEVEL = 8;
87
88
89var LENGTH_CODES = 29;
90/* number of length codes, not counting the special END_BLOCK code */
91var LITERALS = 256;
92/* number of literal bytes 0..255 */
93var L_CODES = LITERALS + 1 + LENGTH_CODES;
94/* number of Literal or Length codes, including the END_BLOCK code */
95var D_CODES = 30;
96/* number of distance codes */
97var BL_CODES = 19;
98/* number of codes used to transfer the bit lengths */
99var HEAP_SIZE = 2 * L_CODES + 1;
100/* maximum heap size */
101var MAX_BITS = 15;
102/* All codes must not exceed MAX_BITS bits */
103
104var MIN_MATCH = 3;
105var MAX_MATCH = 258;
106var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
107
108var PRESET_DICT = 0x20;
109
110var INIT_STATE = 42;
111var EXTRA_STATE = 69;
112var NAME_STATE = 73;
113var COMMENT_STATE = 91;
114var HCRC_STATE = 103;
115var BUSY_STATE = 113;
116var FINISH_STATE = 666;
117
118var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
119var BS_BLOCK_DONE = 2; /* block flush performed */
120var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
121var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
122
123var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
124
125function err(strm, errorCode) {
126 strm.msg = msg[errorCode];
127 return errorCode;
128}
129
130function rank(f) {
131 return ((f) << 1) - ((f) > 4 ? 9 : 0);
132}
133
134function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
135
136
137/* =========================================================================
138 * Flush as much pending output as possible. All deflate() output goes
139 * through this function so some applications may wish to modify it
140 * to avoid allocating a large strm->output buffer and copying into it.
141 * (See also read_buf()).
142 */
143function flush_pending(strm) {
144 var s = strm.state;
145
146 //_tr_flush_bits(s);
147 var len = s.pending;
148 if (len > strm.avail_out) {
149 len = strm.avail_out;
150 }
151 if (len === 0) { return; }
152
153 utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
154 strm.next_out += len;
155 s.pending_out += len;
156 strm.total_out += len;
157 strm.avail_out -= len;
158 s.pending -= len;
159 if (s.pending === 0) {
160 s.pending_out = 0;
161 }
162}
163
164
165function flush_block_only(s, last) {
166 trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
167 s.block_start = s.strstart;
168 flush_pending(s.strm);
169}
170
171
172function put_byte(s, b) {
173 s.pending_buf[s.pending++] = b;
174}
175
176
177/* =========================================================================
178 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
179 * IN assertion: the stream state is correct and there is enough room in
180 * pending_buf.
181 */
182function putShortMSB(s, b) {
183// put_byte(s, (Byte)(b >> 8));
184// put_byte(s, (Byte)(b & 0xff));
185 s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
186 s.pending_buf[s.pending++] = b & 0xff;
187}
188
189
190/* ===========================================================================
191 * Read a new buffer from the current input stream, update the adler32
192 * and total number of bytes read. All deflate() input goes through
193 * this function so some applications may wish to modify it to avoid
194 * allocating a large strm->input buffer and copying from it.
195 * (See also flush_pending()).
196 */
197function read_buf(strm, buf, start, size) {
198 var len = strm.avail_in;
199
200 if (len > size) { len = size; }
201 if (len === 0) { return 0; }
202
203 strm.avail_in -= len;
204
205 // zmemcpy(buf, strm->next_in, len);
206 utils.arraySet(buf, strm.input, strm.next_in, len, start);
207 if (strm.state.wrap === 1) {
208 strm.adler = adler32(strm.adler, buf, len, start);
209 }
210
211 else if (strm.state.wrap === 2) {
212 strm.adler = crc32(strm.adler, buf, len, start);
213 }
214
215 strm.next_in += len;
216 strm.total_in += len;
217
218 return len;
219}
220
221
222/* ===========================================================================
223 * Set match_start to the longest match starting at the given string and
224 * return its length. Matches shorter or equal to prev_length are discarded,
225 * in which case the result is equal to prev_length and match_start is
226 * garbage.
227 * IN assertions: cur_match is the head of the hash chain for the current
228 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
229 * OUT assertion: the match length is not greater than s->lookahead.
230 */
231function longest_match(s, cur_match) {
232 var chain_length = s.max_chain_length; /* max hash chain length */
233 var scan = s.strstart; /* current string */
234 var match; /* matched string */
235 var len; /* length of current match */
236 var best_len = s.prev_length; /* best match length so far */
237 var nice_match = s.nice_match; /* stop if match long enough */
238 var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
239 s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
240
241 var _win = s.window; // shortcut
242
243 var wmask = s.w_mask;
244 var prev = s.prev;
245
246 /* Stop when cur_match becomes <= limit. To simplify the code,
247 * we prevent matches with the string of window index 0.
248 */
249
250 var strend = s.strstart + MAX_MATCH;
251 var scan_end1 = _win[scan + best_len - 1];
252 var scan_end = _win[scan + best_len];
253
254 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
255 * It is easy to get rid of this optimization if necessary.
256 */
257 // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
258
259 /* Do not waste too much time if we already have a good match: */
260 if (s.prev_length >= s.good_match) {
261 chain_length >>= 2;
262 }
263 /* Do not look for matches beyond the end of the input. This is necessary
264 * to make deflate deterministic.
265 */
266 if (nice_match > s.lookahead) { nice_match = s.lookahead; }
267
268 // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
269
270 do {
271 // Assert(cur_match < s->strstart, "no future");
272 match = cur_match;
273
274 /* Skip to next match if the match length cannot increase
275 * or if the match length is less than 2. Note that the checks below
276 * for insufficient lookahead only occur occasionally for performance
277 * reasons. Therefore uninitialized memory will be accessed, and
278 * conditional jumps will be made that depend on those values.
279 * However the length of the match is limited to the lookahead, so
280 * the output of deflate is not affected by the uninitialized values.
281 */
282
283 if (_win[match + best_len] !== scan_end ||
284 _win[match + best_len - 1] !== scan_end1 ||
285 _win[match] !== _win[scan] ||
286 _win[++match] !== _win[scan + 1]) {
287 continue;
288 }
289
290 /* The check at best_len-1 can be removed because it will be made
291 * again later. (This heuristic is not always a win.)
292 * It is not necessary to compare scan[2] and match[2] since they
293 * are always equal when the other bytes match, given that
294 * the hash keys are equal and that HASH_BITS >= 8.
295 */
296 scan += 2;
297 match++;
298 // Assert(*scan == *match, "match[2]?");
299
300 /* We check for insufficient lookahead only every 8th comparison;
301 * the 256th check will be made at strstart+258.
302 */
303 do {
304 /*jshint noempty:false*/
305 } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
306 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
307 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
308 _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
309 scan < strend);
310
311 // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
312
313 len = MAX_MATCH - (strend - scan);
314 scan = strend - MAX_MATCH;
315
316 if (len > best_len) {
317 s.match_start = cur_match;
318 best_len = len;
319 if (len >= nice_match) {
320 break;
321 }
322 scan_end1 = _win[scan + best_len - 1];
323 scan_end = _win[scan + best_len];
324 }
325 } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
326
327 if (best_len <= s.lookahead) {
328 return best_len;
329 }
330 return s.lookahead;
331}
332
333
334/* ===========================================================================
335 * Fill the window when the lookahead becomes insufficient.
336 * Updates strstart and lookahead.
337 *
338 * IN assertion: lookahead < MIN_LOOKAHEAD
339 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
340 * At least one byte has been read, or avail_in == 0; reads are
341 * performed for at least two bytes (required for the zip translate_eol
342 * option -- not supported here).
343 */
344function fill_window(s) {
345 var _w_size = s.w_size;
346 var p, n, m, more, str;
347
348 //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
349
350 do {
351 more = s.window_size - s.lookahead - s.strstart;
352
353 // JS ints have 32 bit, block below not needed
354 /* Deal with !@#$% 64K limit: */
355 //if (sizeof(int) <= 2) {
356 // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
357 // more = wsize;
358 //
359 // } else if (more == (unsigned)(-1)) {
360 // /* Very unlikely, but possible on 16 bit machine if
361 // * strstart == 0 && lookahead == 1 (input done a byte at time)
362 // */
363 // more--;
364 // }
365 //}
366
367
368 /* If the window is almost full and there is insufficient lookahead,
369 * move the upper half to the lower one to make room in the upper half.
370 */
371 if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
372
373 utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
374 s.match_start -= _w_size;
375 s.strstart -= _w_size;
376 /* we now have strstart >= MAX_DIST */
377 s.block_start -= _w_size;
378
379 /* Slide the hash table (could be avoided with 32 bit values
380 at the expense of memory usage). We slide even when level == 0
381 to keep the hash table consistent if we switch back to level > 0
382 later. (Using level 0 permanently is not an optimal usage of
383 zlib, so we don't care about this pathological case.)
384 */
385
386 n = s.hash_size;
387 p = n;
388 do {
389 m = s.head[--p];
390 s.head[p] = (m >= _w_size ? m - _w_size : 0);
391 } while (--n);
392
393 n = _w_size;
394 p = n;
395 do {
396 m = s.prev[--p];
397 s.prev[p] = (m >= _w_size ? m - _w_size : 0);
398 /* If n is not on any hash chain, prev[n] is garbage but
399 * its value will never be used.
400 */
401 } while (--n);
402
403 more += _w_size;
404 }
405 if (s.strm.avail_in === 0) {
406 break;
407 }
408
409 /* If there was no sliding:
410 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
411 * more == window_size - lookahead - strstart
412 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
413 * => more >= window_size - 2*WSIZE + 2
414 * In the BIG_MEM or MMAP case (not yet supported),
415 * window_size == input_size + MIN_LOOKAHEAD &&
416 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
417 * Otherwise, window_size == 2*WSIZE so more >= 2.
418 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
419 */
420 //Assert(more >= 2, "more < 2");
421 n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
422 s.lookahead += n;
423
424 /* Initialize the hash value now that we have some input: */
425 if (s.lookahead + s.insert >= MIN_MATCH) {
426 str = s.strstart - s.insert;
427 s.ins_h = s.window[str];
428
429 /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
430 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
431//#if MIN_MATCH != 3
432// Call update_hash() MIN_MATCH-3 more times
433//#endif
434 while (s.insert) {
435 /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
436 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
437
438 s.prev[str & s.w_mask] = s.head[s.ins_h];
439 s.head[s.ins_h] = str;
440 str++;
441 s.insert--;
442 if (s.lookahead + s.insert < MIN_MATCH) {
443 break;
444 }
445 }
446 }
447 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
448 * but this is not important since only literal bytes will be emitted.
449 */
450
451 } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
452
453 /* If the WIN_INIT bytes after the end of the current data have never been
454 * written, then zero those bytes in order to avoid memory check reports of
455 * the use of uninitialized (or uninitialised as Julian writes) bytes by
456 * the longest match routines. Update the high water mark for the next
457 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
458 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
459 */
460// if (s.high_water < s.window_size) {
461// var curr = s.strstart + s.lookahead;
462// var init = 0;
463//
464// if (s.high_water < curr) {
465// /* Previous high water mark below current data -- zero WIN_INIT
466// * bytes or up to end of window, whichever is less.
467// */
468// init = s.window_size - curr;
469// if (init > WIN_INIT)
470// init = WIN_INIT;
471// zmemzero(s->window + curr, (unsigned)init);
472// s->high_water = curr + init;
473// }
474// else if (s->high_water < (ulg)curr + WIN_INIT) {
475// /* High water mark at or above current data, but below current data
476// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
477// * to end of window, whichever is less.
478// */
479// init = (ulg)curr + WIN_INIT - s->high_water;
480// if (init > s->window_size - s->high_water)
481// init = s->window_size - s->high_water;
482// zmemzero(s->window + s->high_water, (unsigned)init);
483// s->high_water += init;
484// }
485// }
486//
487// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
488// "not enough room for search");
489}
490
491/* ===========================================================================
492 * Copy without compression as much as possible from the input stream, return
493 * the current block state.
494 * This function does not insert new strings in the dictionary since
495 * uncompressible data is probably not useful. This function is used
496 * only for the level=0 compression option.
497 * NOTE: this function should be optimized to avoid extra copying from
498 * window to pending_buf.
499 */
500function deflate_stored(s, flush) {
501 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
502 * to pending_buf_size, and each stored block has a 5 byte header:
503 */
504 var max_block_size = 0xffff;
505
506 if (max_block_size > s.pending_buf_size - 5) {
507 max_block_size = s.pending_buf_size - 5;
508 }
509
510 /* Copy as much as possible from input to output: */
511 for (;;) {
512 /* Fill the window as much as possible: */
513 if (s.lookahead <= 1) {
514
515 //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
516 // s->block_start >= (long)s->w_size, "slide too late");
517// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
518// s.block_start >= s.w_size)) {
519// throw new Error("slide too late");
520// }
521
522 fill_window(s);
523 if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
524 return BS_NEED_MORE;
525 }
526
527 if (s.lookahead === 0) {
528 break;
529 }
530 /* flush the current block */
531 }
532 //Assert(s->block_start >= 0L, "block gone");
533// if (s.block_start < 0) throw new Error("block gone");
534
535 s.strstart += s.lookahead;
536 s.lookahead = 0;
537
538 /* Emit a stored block if pending_buf will be full: */
539 var max_start = s.block_start + max_block_size;
540
541 if (s.strstart === 0 || s.strstart >= max_start) {
542 /* strstart == 0 is possible when wraparound on 16-bit machine */
543 s.lookahead = s.strstart - max_start;
544 s.strstart = max_start;
545 /*** FLUSH_BLOCK(s, 0); ***/
546 flush_block_only(s, false);
547 if (s.strm.avail_out === 0) {
548 return BS_NEED_MORE;
549 }
550 /***/
551
552
553 }
554 /* Flush if we may have to slide, otherwise block_start may become
555 * negative and the data will be gone:
556 */
557 if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
558 /*** FLUSH_BLOCK(s, 0); ***/
559 flush_block_only(s, false);
560 if (s.strm.avail_out === 0) {
561 return BS_NEED_MORE;
562 }
563 /***/
564 }
565 }
566
567 s.insert = 0;
568
569 if (flush === Z_FINISH) {
570 /*** FLUSH_BLOCK(s, 1); ***/
571 flush_block_only(s, true);
572 if (s.strm.avail_out === 0) {
573 return BS_FINISH_STARTED;
574 }
575 /***/
576 return BS_FINISH_DONE;
577 }
578
579 if (s.strstart > s.block_start) {
580 /*** FLUSH_BLOCK(s, 0); ***/
581 flush_block_only(s, false);
582 if (s.strm.avail_out === 0) {
583 return BS_NEED_MORE;
584 }
585 /***/
586 }
587
588 return BS_NEED_MORE;
589}
590
591/* ===========================================================================
592 * Compress as much as possible from the input stream, return the current
593 * block state.
594 * This function does not perform lazy evaluation of matches and inserts
595 * new strings in the dictionary only for unmatched strings or for short
596 * matches. It is used only for the fast compression options.
597 */
598function deflate_fast(s, flush) {
599 var hash_head; /* head of the hash chain */
600 var bflush; /* set if current block must be flushed */
601
602 for (;;) {
603 /* Make sure that we always have enough lookahead, except
604 * at the end of the input file. We need MAX_MATCH bytes
605 * for the next match, plus MIN_MATCH bytes to insert the
606 * string following the next match.
607 */
608 if (s.lookahead < MIN_LOOKAHEAD) {
609 fill_window(s);
610 if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
611 return BS_NEED_MORE;
612 }
613 if (s.lookahead === 0) {
614 break; /* flush the current block */
615 }
616 }
617
618 /* Insert the string window[strstart .. strstart+2] in the
619 * dictionary, and set hash_head to the head of the hash chain:
620 */
621 hash_head = 0/*NIL*/;
622 if (s.lookahead >= MIN_MATCH) {
623 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
624 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
625 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
626 s.head[s.ins_h] = s.strstart;
627 /***/
628 }
629
630 /* Find the longest match, discarding those <= prev_length.
631 * At this point we have always match_length < MIN_MATCH
632 */
633 if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
634 /* To simplify the code, we prevent matches with the string
635 * of window index 0 (in particular we have to avoid a match
636 * of the string with itself at the start of the input file).
637 */
638 s.match_length = longest_match(s, hash_head);
639 /* longest_match() sets match_start */
640 }
641 if (s.match_length >= MIN_MATCH) {
642 // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
643
644 /*** _tr_tally_dist(s, s.strstart - s.match_start,
645 s.match_length - MIN_MATCH, bflush); ***/
646 bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
647
648 s.lookahead -= s.match_length;
649
650 /* Insert new strings in the hash table only if the match length
651 * is not too large. This saves time but degrades compression.
652 */
653 if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
654 s.match_length--; /* string at strstart already in table */
655 do {
656 s.strstart++;
657 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
658 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
659 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
660 s.head[s.ins_h] = s.strstart;
661 /***/
662 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
663 * always MIN_MATCH bytes ahead.
664 */
665 } while (--s.match_length !== 0);
666 s.strstart++;
667 } else
668 {
669 s.strstart += s.match_length;
670 s.match_length = 0;
671 s.ins_h = s.window[s.strstart];
672 /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
673 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
674
675//#if MIN_MATCH != 3
676// Call UPDATE_HASH() MIN_MATCH-3 more times
677//#endif
678 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
679 * matter since it will be recomputed at next deflate call.
680 */
681 }
682 } else {
683 /* No match, output a literal byte */
684 //Tracevv((stderr,"%c", s.window[s.strstart]));
685 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
686 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
687
688 s.lookahead--;
689 s.strstart++;
690 }
691 if (bflush) {
692 /*** FLUSH_BLOCK(s, 0); ***/
693 flush_block_only(s, false);
694 if (s.strm.avail_out === 0) {
695 return BS_NEED_MORE;
696 }
697 /***/
698 }
699 }
700 s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
701 if (flush === Z_FINISH) {
702 /*** FLUSH_BLOCK(s, 1); ***/
703 flush_block_only(s, true);
704 if (s.strm.avail_out === 0) {
705 return BS_FINISH_STARTED;
706 }
707 /***/
708 return BS_FINISH_DONE;
709 }
710 if (s.last_lit) {
711 /*** FLUSH_BLOCK(s, 0); ***/
712 flush_block_only(s, false);
713 if (s.strm.avail_out === 0) {
714 return BS_NEED_MORE;
715 }
716 /***/
717 }
718 return BS_BLOCK_DONE;
719}
720
721/* ===========================================================================
722 * Same as above, but achieves better compression. We use a lazy
723 * evaluation for matches: a match is finally adopted only if there is
724 * no better match at the next window position.
725 */
726function deflate_slow(s, flush) {
727 var hash_head; /* head of hash chain */
728 var bflush; /* set if current block must be flushed */
729
730 var max_insert;
731
732 /* Process the input block. */
733 for (;;) {
734 /* Make sure that we always have enough lookahead, except
735 * at the end of the input file. We need MAX_MATCH bytes
736 * for the next match, plus MIN_MATCH bytes to insert the
737 * string following the next match.
738 */
739 if (s.lookahead < MIN_LOOKAHEAD) {
740 fill_window(s);
741 if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
742 return BS_NEED_MORE;
743 }
744 if (s.lookahead === 0) { break; } /* flush the current block */
745 }
746
747 /* Insert the string window[strstart .. strstart+2] in the
748 * dictionary, and set hash_head to the head of the hash chain:
749 */
750 hash_head = 0/*NIL*/;
751 if (s.lookahead >= MIN_MATCH) {
752 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
753 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
754 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
755 s.head[s.ins_h] = s.strstart;
756 /***/
757 }
758
759 /* Find the longest match, discarding those <= prev_length.
760 */
761 s.prev_length = s.match_length;
762 s.prev_match = s.match_start;
763 s.match_length = MIN_MATCH - 1;
764
765 if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
766 s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
767 /* To simplify the code, we prevent matches with the string
768 * of window index 0 (in particular we have to avoid a match
769 * of the string with itself at the start of the input file).
770 */
771 s.match_length = longest_match(s, hash_head);
772 /* longest_match() sets match_start */
773
774 if (s.match_length <= 5 &&
775 (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
776
777 /* If prev_match is also MIN_MATCH, match_start is garbage
778 * but we will ignore the current match anyway.
779 */
780 s.match_length = MIN_MATCH - 1;
781 }
782 }
783 /* If there was a match at the previous step and the current
784 * match is not better, output the previous match:
785 */
786 if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
787 max_insert = s.strstart + s.lookahead - MIN_MATCH;
788 /* Do not insert strings in hash table beyond this. */
789
790 //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
791
792 /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
793 s.prev_length - MIN_MATCH, bflush);***/
794 bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
795 /* Insert in hash table all strings up to the end of the match.
796 * strstart-1 and strstart are already inserted. If there is not
797 * enough lookahead, the last two strings are not inserted in
798 * the hash table.
799 */
800 s.lookahead -= s.prev_length - 1;
801 s.prev_length -= 2;
802 do {
803 if (++s.strstart <= max_insert) {
804 /*** INSERT_STRING(s, s.strstart, hash_head); ***/
805 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
806 hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
807 s.head[s.ins_h] = s.strstart;
808 /***/
809 }
810 } while (--s.prev_length !== 0);
811 s.match_available = 0;
812 s.match_length = MIN_MATCH - 1;
813 s.strstart++;
814
815 if (bflush) {
816 /*** FLUSH_BLOCK(s, 0); ***/
817 flush_block_only(s, false);
818 if (s.strm.avail_out === 0) {
819 return BS_NEED_MORE;
820 }
821 /***/
822 }
823
824 } else if (s.match_available) {
825 /* If there was no match at the previous position, output a
826 * single literal. If there was a match but the current match
827 * is longer, truncate the previous match to a single literal.
828 */
829 //Tracevv((stderr,"%c", s->window[s->strstart-1]));
830 /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
831 bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
832
833 if (bflush) {
834 /*** FLUSH_BLOCK_ONLY(s, 0) ***/
835 flush_block_only(s, false);
836 /***/
837 }
838 s.strstart++;
839 s.lookahead--;
840 if (s.strm.avail_out === 0) {
841 return BS_NEED_MORE;
842 }
843 } else {
844 /* There is no previous match to compare with, wait for
845 * the next step to decide.
846 */
847 s.match_available = 1;
848 s.strstart++;
849 s.lookahead--;
850 }
851 }
852 //Assert (flush != Z_NO_FLUSH, "no flush?");
853 if (s.match_available) {
854 //Tracevv((stderr,"%c", s->window[s->strstart-1]));
855 /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
856 bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
857
858 s.match_available = 0;
859 }
860 s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
861 if (flush === Z_FINISH) {
862 /*** FLUSH_BLOCK(s, 1); ***/
863 flush_block_only(s, true);
864 if (s.strm.avail_out === 0) {
865 return BS_FINISH_STARTED;
866 }
867 /***/
868 return BS_FINISH_DONE;
869 }
870 if (s.last_lit) {
871 /*** FLUSH_BLOCK(s, 0); ***/
872 flush_block_only(s, false);
873 if (s.strm.avail_out === 0) {
874 return BS_NEED_MORE;
875 }
876 /***/
877 }
878
879 return BS_BLOCK_DONE;
880}
881
882
883/* ===========================================================================
884 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
885 * one. Do not maintain a hash table. (It will be regenerated if this run of
886 * deflate switches away from Z_RLE.)
887 */
888function deflate_rle(s, flush) {
889 var bflush; /* set if current block must be flushed */
890 var prev; /* byte at distance one to match */
891 var scan, strend; /* scan goes up to strend for length of run */
892
893 var _win = s.window;
894
895 for (;;) {
896 /* Make sure that we always have enough lookahead, except
897 * at the end of the input file. We need MAX_MATCH bytes
898 * for the longest run, plus one for the unrolled loop.
899 */
900 if (s.lookahead <= MAX_MATCH) {
901 fill_window(s);
902 if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
903 return BS_NEED_MORE;
904 }
905 if (s.lookahead === 0) { break; } /* flush the current block */
906 }
907
908 /* See how many times the previous byte repeats */
909 s.match_length = 0;
910 if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
911 scan = s.strstart - 1;
912 prev = _win[scan];
913 if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
914 strend = s.strstart + MAX_MATCH;
915 do {
916 /*jshint noempty:false*/
917 } while (prev === _win[++scan] && prev === _win[++scan] &&
918 prev === _win[++scan] && prev === _win[++scan] &&
919 prev === _win[++scan] && prev === _win[++scan] &&
920 prev === _win[++scan] && prev === _win[++scan] &&
921 scan < strend);
922 s.match_length = MAX_MATCH - (strend - scan);
923 if (s.match_length > s.lookahead) {
924 s.match_length = s.lookahead;
925 }
926 }
927 //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
928 }
929
930 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
931 if (s.match_length >= MIN_MATCH) {
932 //check_match(s, s.strstart, s.strstart - 1, s.match_length);
933
934 /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
935 bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
936
937 s.lookahead -= s.match_length;
938 s.strstart += s.match_length;
939 s.match_length = 0;
940 } else {
941 /* No match, output a literal byte */
942 //Tracevv((stderr,"%c", s->window[s->strstart]));
943 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
944 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
945
946 s.lookahead--;
947 s.strstart++;
948 }
949 if (bflush) {
950 /*** FLUSH_BLOCK(s, 0); ***/
951 flush_block_only(s, false);
952 if (s.strm.avail_out === 0) {
953 return BS_NEED_MORE;
954 }
955 /***/
956 }
957 }
958 s.insert = 0;
959 if (flush === Z_FINISH) {
960 /*** FLUSH_BLOCK(s, 1); ***/
961 flush_block_only(s, true);
962 if (s.strm.avail_out === 0) {
963 return BS_FINISH_STARTED;
964 }
965 /***/
966 return BS_FINISH_DONE;
967 }
968 if (s.last_lit) {
969 /*** FLUSH_BLOCK(s, 0); ***/
970 flush_block_only(s, false);
971 if (s.strm.avail_out === 0) {
972 return BS_NEED_MORE;
973 }
974 /***/
975 }
976 return BS_BLOCK_DONE;
977}
978
979/* ===========================================================================
980 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
981 * (It will be regenerated if this run of deflate switches away from Huffman.)
982 */
983function deflate_huff(s, flush) {
984 var bflush; /* set if current block must be flushed */
985
986 for (;;) {
987 /* Make sure that we have a literal to write. */
988 if (s.lookahead === 0) {
989 fill_window(s);
990 if (s.lookahead === 0) {
991 if (flush === Z_NO_FLUSH) {
992 return BS_NEED_MORE;
993 }
994 break; /* flush the current block */
995 }
996 }
997
998 /* Output a literal byte */
999 s.match_length = 0;
1000 //Tracevv((stderr,"%c", s->window[s->strstart]));
1001 /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
1002 bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
1003 s.lookahead--;
1004 s.strstart++;
1005 if (bflush) {
1006 /*** FLUSH_BLOCK(s, 0); ***/
1007 flush_block_only(s, false);
1008 if (s.strm.avail_out === 0) {
1009 return BS_NEED_MORE;
1010 }
1011 /***/
1012 }
1013 }
1014 s.insert = 0;
1015 if (flush === Z_FINISH) {
1016 /*** FLUSH_BLOCK(s, 1); ***/
1017 flush_block_only(s, true);
1018 if (s.strm.avail_out === 0) {
1019 return BS_FINISH_STARTED;
1020 }
1021 /***/
1022 return BS_FINISH_DONE;
1023 }
1024 if (s.last_lit) {
1025 /*** FLUSH_BLOCK(s, 0); ***/
1026 flush_block_only(s, false);
1027 if (s.strm.avail_out === 0) {
1028 return BS_NEED_MORE;
1029 }
1030 /***/
1031 }
1032 return BS_BLOCK_DONE;
1033}
1034
1035/* Values for max_lazy_match, good_match and max_chain_length, depending on
1036 * the desired pack level (0..9). The values given below have been tuned to
1037 * exclude worst case performance for pathological files. Better values may be
1038 * found for specific files.
1039 */
1040function Config(good_length, max_lazy, nice_length, max_chain, func) {
1041 this.good_length = good_length;
1042 this.max_lazy = max_lazy;
1043 this.nice_length = nice_length;
1044 this.max_chain = max_chain;
1045 this.func = func;
1046}
1047
1048var configuration_table;
1049
1050configuration_table = [
1051 /* good lazy nice chain */
1052 new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
1053 new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
1054 new Config(4, 5, 16, 8, deflate_fast), /* 2 */
1055 new Config(4, 6, 32, 32, deflate_fast), /* 3 */
1056
1057 new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
1058 new Config(8, 16, 32, 32, deflate_slow), /* 5 */
1059 new Config(8, 16, 128, 128, deflate_slow), /* 6 */
1060 new Config(8, 32, 128, 256, deflate_slow), /* 7 */
1061 new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
1062 new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
1063];
1064
1065
1066/* ===========================================================================
1067 * Initialize the "longest match" routines for a new zlib stream
1068 */
1069function lm_init(s) {
1070 s.window_size = 2 * s.w_size;
1071
1072 /*** CLEAR_HASH(s); ***/
1073 zero(s.head); // Fill with NIL (= 0);
1074
1075 /* Set the default configuration parameters:
1076 */
1077 s.max_lazy_match = configuration_table[s.level].max_lazy;
1078 s.good_match = configuration_table[s.level].good_length;
1079 s.nice_match = configuration_table[s.level].nice_length;
1080 s.max_chain_length = configuration_table[s.level].max_chain;
1081
1082 s.strstart = 0;
1083 s.block_start = 0;
1084 s.lookahead = 0;
1085 s.insert = 0;
1086 s.match_length = s.prev_length = MIN_MATCH - 1;
1087 s.match_available = 0;
1088 s.ins_h = 0;
1089}
1090
1091
1092function DeflateState() {
1093 this.strm = null; /* pointer back to this zlib stream */
1094 this.status = 0; /* as the name implies */
1095 this.pending_buf = null; /* output still pending */
1096 this.pending_buf_size = 0; /* size of pending_buf */
1097 this.pending_out = 0; /* next pending byte to output to the stream */
1098 this.pending = 0; /* nb of bytes in the pending buffer */
1099 this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
1100 this.gzhead = null; /* gzip header information to write */
1101 this.gzindex = 0; /* where in extra, name, or comment */
1102 this.method = Z_DEFLATED; /* can only be DEFLATED */
1103 this.last_flush = -1; /* value of flush param for previous deflate call */
1104
1105 this.w_size = 0; /* LZ77 window size (32K by default) */
1106 this.w_bits = 0; /* log2(w_size) (8..16) */
1107 this.w_mask = 0; /* w_size - 1 */
1108
1109 this.window = null;
1110 /* Sliding window. Input bytes are read into the second half of the window,
1111 * and move to the first half later to keep a dictionary of at least wSize
1112 * bytes. With this organization, matches are limited to a distance of
1113 * wSize-MAX_MATCH bytes, but this ensures that IO is always
1114 * performed with a length multiple of the block size.
1115 */
1116
1117 this.window_size = 0;
1118 /* Actual size of window: 2*wSize, except when the user input buffer
1119 * is directly used as sliding window.
1120 */
1121
1122 this.prev = null;
1123 /* Link to older string with same hash index. To limit the size of this
1124 * array to 64K, this link is maintained only for the last 32K strings.
1125 * An index in this array is thus a window index modulo 32K.
1126 */
1127
1128 this.head = null; /* Heads of the hash chains or NIL. */
1129
1130 this.ins_h = 0; /* hash index of string to be inserted */
1131 this.hash_size = 0; /* number of elements in hash table */
1132 this.hash_bits = 0; /* log2(hash_size) */
1133 this.hash_mask = 0; /* hash_size-1 */
1134
1135 this.hash_shift = 0;
1136 /* Number of bits by which ins_h must be shifted at each input
1137 * step. It must be such that after MIN_MATCH steps, the oldest
1138 * byte no longer takes part in the hash key, that is:
1139 * hash_shift * MIN_MATCH >= hash_bits
1140 */
1141
1142 this.block_start = 0;
1143 /* Window position at the beginning of the current output block. Gets
1144 * negative when the window is moved backwards.
1145 */
1146
1147 this.match_length = 0; /* length of best match */
1148 this.prev_match = 0; /* previous match */
1149 this.match_available = 0; /* set if previous match exists */
1150 this.strstart = 0; /* start of string to insert */
1151 this.match_start = 0; /* start of matching string */
1152 this.lookahead = 0; /* number of valid bytes ahead in window */
1153
1154 this.prev_length = 0;
1155 /* Length of the best match at previous step. Matches not greater than this
1156 * are discarded. This is used in the lazy match evaluation.
1157 */
1158
1159 this.max_chain_length = 0;
1160 /* To speed up deflation, hash chains are never searched beyond this
1161 * length. A higher limit improves compression ratio but degrades the
1162 * speed.
1163 */
1164
1165 this.max_lazy_match = 0;
1166 /* Attempt to find a better match only when the current match is strictly
1167 * smaller than this value. This mechanism is used only for compression
1168 * levels >= 4.
1169 */
1170 // That's alias to max_lazy_match, don't use directly
1171 //this.max_insert_length = 0;
1172 /* Insert new strings in the hash table only if the match length is not
1173 * greater than this length. This saves time but degrades compression.
1174 * max_insert_length is used only for compression levels <= 3.
1175 */
1176
1177 this.level = 0; /* compression level (1..9) */
1178 this.strategy = 0; /* favor or force Huffman coding*/
1179
1180 this.good_match = 0;
1181 /* Use a faster search when the previous match is longer than this */
1182
1183 this.nice_match = 0; /* Stop searching when current match exceeds this */
1184
1185 /* used by trees.c: */
1186
1187 /* Didn't use ct_data typedef below to suppress compiler warning */
1188
1189 // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
1190 // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1191 // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
1192
1193 // Use flat array of DOUBLE size, with interleaved fata,
1194 // because JS does not support effective
1195 this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
1196 this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
1197 this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
1198 zero(this.dyn_ltree);
1199 zero(this.dyn_dtree);
1200 zero(this.bl_tree);
1201
1202 this.l_desc = null; /* desc. for literal tree */
1203 this.d_desc = null; /* desc. for distance tree */
1204 this.bl_desc = null; /* desc. for bit length tree */
1205
1206 //ush bl_count[MAX_BITS+1];
1207 this.bl_count = new utils.Buf16(MAX_BITS + 1);
1208 /* number of codes at each bit length for an optimal tree */
1209
1210 //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
1211 this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
1212 zero(this.heap);
1213
1214 this.heap_len = 0; /* number of elements in the heap */
1215 this.heap_max = 0; /* element of largest frequency */
1216 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1217 * The same heap array is used to build all trees.
1218 */
1219
1220 this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
1221 zero(this.depth);
1222 /* Depth of each subtree used as tie breaker for trees of equal frequency
1223 */
1224
1225 this.l_buf = 0; /* buffer index for literals or lengths */
1226
1227 this.lit_bufsize = 0;
1228 /* Size of match buffer for literals/lengths. There are 4 reasons for
1229 * limiting lit_bufsize to 64K:
1230 * - frequencies can be kept in 16 bit counters
1231 * - if compression is not successful for the first block, all input
1232 * data is still in the window so we can still emit a stored block even
1233 * when input comes from standard input. (This can also be done for
1234 * all blocks if lit_bufsize is not greater than 32K.)
1235 * - if compression is not successful for a file smaller than 64K, we can
1236 * even emit a stored file instead of a stored block (saving 5 bytes).
1237 * This is applicable only for zip (not gzip or zlib).
1238 * - creating new Huffman trees less frequently may not provide fast
1239 * adaptation to changes in the input data statistics. (Take for
1240 * example a binary file with poorly compressible code followed by
1241 * a highly compressible string table.) Smaller buffer sizes give
1242 * fast adaptation but have of course the overhead of transmitting
1243 * trees more frequently.
1244 * - I can't count above 4
1245 */
1246
1247 this.last_lit = 0; /* running index in l_buf */
1248
1249 this.d_buf = 0;
1250 /* Buffer index for distances. To simplify the code, d_buf and l_buf have
1251 * the same number of elements. To use different lengths, an extra flag
1252 * array would be necessary.
1253 */
1254
1255 this.opt_len = 0; /* bit length of current block with optimal trees */
1256 this.static_len = 0; /* bit length of current block with static trees */
1257 this.matches = 0; /* number of string matches in current block */
1258 this.insert = 0; /* bytes at end of window left to insert */
1259
1260
1261 this.bi_buf = 0;
1262 /* Output buffer. bits are inserted starting at the bottom (least
1263 * significant bits).
1264 */
1265 this.bi_valid = 0;
1266 /* Number of valid bits in bi_buf. All bits above the last valid bit
1267 * are always zero.
1268 */
1269
1270 // Used for window memory init. We safely ignore it for JS. That makes
1271 // sense only for pointers and memory check tools.
1272 //this.high_water = 0;
1273 /* High water mark offset in window for initialized bytes -- bytes above
1274 * this are set to zero in order to avoid memory check warnings when
1275 * longest match routines access bytes past the input. This is then
1276 * updated to the new high water mark.
1277 */
1278}
1279
1280
1281function deflateResetKeep(strm) {
1282 var s;
1283
1284 if (!strm || !strm.state) {
1285 return err(strm, Z_STREAM_ERROR);
1286 }
1287
1288 strm.total_in = strm.total_out = 0;
1289 strm.data_type = Z_UNKNOWN;
1290
1291 s = strm.state;
1292 s.pending = 0;
1293 s.pending_out = 0;
1294
1295 if (s.wrap < 0) {
1296 s.wrap = -s.wrap;
1297 /* was made negative by deflate(..., Z_FINISH); */
1298 }
1299 s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
1300 strm.adler = (s.wrap === 2) ?
1301 0 // crc32(0, Z_NULL, 0)
1302 :
1303 1; // adler32(0, Z_NULL, 0)
1304 s.last_flush = Z_NO_FLUSH;
1305 trees._tr_init(s);
1306 return Z_OK;
1307}
1308
1309
1310function deflateReset(strm) {
1311 var ret = deflateResetKeep(strm);
1312 if (ret === Z_OK) {
1313 lm_init(strm.state);
1314 }
1315 return ret;
1316}
1317
1318
1319function deflateSetHeader(strm, head) {
1320 if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1321 if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
1322 strm.state.gzhead = head;
1323 return Z_OK;
1324}
1325
1326
1327function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
1328 if (!strm) { // === Z_NULL
1329 return Z_STREAM_ERROR;
1330 }
1331 var wrap = 1;
1332
1333 if (level === Z_DEFAULT_COMPRESSION) {
1334 level = 6;
1335 }
1336
1337 if (windowBits < 0) { /* suppress zlib wrapper */
1338 wrap = 0;
1339 windowBits = -windowBits;
1340 }
1341
1342 else if (windowBits > 15) {
1343 wrap = 2; /* write gzip wrapper instead */
1344 windowBits -= 16;
1345 }
1346
1347
1348 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
1349 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
1350 strategy < 0 || strategy > Z_FIXED) {
1351 return err(strm, Z_STREAM_ERROR);
1352 }
1353
1354
1355 if (windowBits === 8) {
1356 windowBits = 9;
1357 }
1358 /* until 256-byte window bug fixed */
1359
1360 var s = new DeflateState();
1361
1362 strm.state = s;
1363 s.strm = strm;
1364
1365 s.wrap = wrap;
1366 s.gzhead = null;
1367 s.w_bits = windowBits;
1368 s.w_size = 1 << s.w_bits;
1369 s.w_mask = s.w_size - 1;
1370
1371 s.hash_bits = memLevel + 7;
1372 s.hash_size = 1 << s.hash_bits;
1373 s.hash_mask = s.hash_size - 1;
1374 s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
1375
1376 s.window = new utils.Buf8(s.w_size * 2);
1377 s.head = new utils.Buf16(s.hash_size);
1378 s.prev = new utils.Buf16(s.w_size);
1379
1380 // Don't need mem init magic for JS.
1381 //s.high_water = 0; /* nothing written to s->window yet */
1382
1383 s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
1384
1385 s.pending_buf_size = s.lit_bufsize * 4;
1386
1387 //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
1388 //s->pending_buf = (uchf *) overlay;
1389 s.pending_buf = new utils.Buf8(s.pending_buf_size);
1390
1391 // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
1392 //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
1393 s.d_buf = 1 * s.lit_bufsize;
1394
1395 //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
1396 s.l_buf = (1 + 2) * s.lit_bufsize;
1397
1398 s.level = level;
1399 s.strategy = strategy;
1400 s.method = method;
1401
1402 return deflateReset(strm);
1403}
1404
1405function deflateInit(strm, level) {
1406 return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
1407}
1408
1409
1410function deflate(strm, flush) {
1411 var old_flush, s;
1412 var beg, val; // for gzip header write only
1413
1414 if (!strm || !strm.state ||
1415 flush > Z_BLOCK || flush < 0) {
1416 return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
1417 }
1418
1419 s = strm.state;
1420
1421 if (!strm.output ||
1422 (!strm.input && strm.avail_in !== 0) ||
1423 (s.status === FINISH_STATE && flush !== Z_FINISH)) {
1424 return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
1425 }
1426
1427 s.strm = strm; /* just in case */
1428 old_flush = s.last_flush;
1429 s.last_flush = flush;
1430
1431 /* Write the header */
1432 if (s.status === INIT_STATE) {
1433
1434 if (s.wrap === 2) { // GZIP header
1435 strm.adler = 0; //crc32(0L, Z_NULL, 0);
1436 put_byte(s, 31);
1437 put_byte(s, 139);
1438 put_byte(s, 8);
1439 if (!s.gzhead) { // s->gzhead == Z_NULL
1440 put_byte(s, 0);
1441 put_byte(s, 0);
1442 put_byte(s, 0);
1443 put_byte(s, 0);
1444 put_byte(s, 0);
1445 put_byte(s, s.level === 9 ? 2 :
1446 (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1447 4 : 0));
1448 put_byte(s, OS_CODE);
1449 s.status = BUSY_STATE;
1450 }
1451 else {
1452 put_byte(s, (s.gzhead.text ? 1 : 0) +
1453 (s.gzhead.hcrc ? 2 : 0) +
1454 (!s.gzhead.extra ? 0 : 4) +
1455 (!s.gzhead.name ? 0 : 8) +
1456 (!s.gzhead.comment ? 0 : 16)
1457 );
1458 put_byte(s, s.gzhead.time & 0xff);
1459 put_byte(s, (s.gzhead.time >> 8) & 0xff);
1460 put_byte(s, (s.gzhead.time >> 16) & 0xff);
1461 put_byte(s, (s.gzhead.time >> 24) & 0xff);
1462 put_byte(s, s.level === 9 ? 2 :
1463 (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1464 4 : 0));
1465 put_byte(s, s.gzhead.os & 0xff);
1466 if (s.gzhead.extra && s.gzhead.extra.length) {
1467 put_byte(s, s.gzhead.extra.length & 0xff);
1468 put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
1469 }
1470 if (s.gzhead.hcrc) {
1471 strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
1472 }
1473 s.gzindex = 0;
1474 s.status = EXTRA_STATE;
1475 }
1476 }
1477 else // DEFLATE header
1478 {
1479 var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
1480 var level_flags = -1;
1481
1482 if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
1483 level_flags = 0;
1484 } else if (s.level < 6) {
1485 level_flags = 1;
1486 } else if (s.level === 6) {
1487 level_flags = 2;
1488 } else {
1489 level_flags = 3;
1490 }
1491 header |= (level_flags << 6);
1492 if (s.strstart !== 0) { header |= PRESET_DICT; }
1493 header += 31 - (header % 31);
1494
1495 s.status = BUSY_STATE;
1496 putShortMSB(s, header);
1497
1498 /* Save the adler32 of the preset dictionary: */
1499 if (s.strstart !== 0) {
1500 putShortMSB(s, strm.adler >>> 16);
1501 putShortMSB(s, strm.adler & 0xffff);
1502 }
1503 strm.adler = 1; // adler32(0L, Z_NULL, 0);
1504 }
1505 }
1506
1507//#ifdef GZIP
1508 if (s.status === EXTRA_STATE) {
1509 if (s.gzhead.extra/* != Z_NULL*/) {
1510 beg = s.pending; /* start of bytes to update crc */
1511
1512 while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
1513 if (s.pending === s.pending_buf_size) {
1514 if (s.gzhead.hcrc && s.pending > beg) {
1515 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1516 }
1517 flush_pending(strm);
1518 beg = s.pending;
1519 if (s.pending === s.pending_buf_size) {
1520 break;
1521 }
1522 }
1523 put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
1524 s.gzindex++;
1525 }
1526 if (s.gzhead.hcrc && s.pending > beg) {
1527 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1528 }
1529 if (s.gzindex === s.gzhead.extra.length) {
1530 s.gzindex = 0;
1531 s.status = NAME_STATE;
1532 }
1533 }
1534 else {
1535 s.status = NAME_STATE;
1536 }
1537 }
1538 if (s.status === NAME_STATE) {
1539 if (s.gzhead.name/* != Z_NULL*/) {
1540 beg = s.pending; /* start of bytes to update crc */
1541 //int val;
1542
1543 do {
1544 if (s.pending === s.pending_buf_size) {
1545 if (s.gzhead.hcrc && s.pending > beg) {
1546 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1547 }
1548 flush_pending(strm);
1549 beg = s.pending;
1550 if (s.pending === s.pending_buf_size) {
1551 val = 1;
1552 break;
1553 }
1554 }
1555 // JS specific: little magic to add zero terminator to end of string
1556 if (s.gzindex < s.gzhead.name.length) {
1557 val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
1558 } else {
1559 val = 0;
1560 }
1561 put_byte(s, val);
1562 } while (val !== 0);
1563
1564 if (s.gzhead.hcrc && s.pending > beg) {
1565 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1566 }
1567 if (val === 0) {
1568 s.gzindex = 0;
1569 s.status = COMMENT_STATE;
1570 }
1571 }
1572 else {
1573 s.status = COMMENT_STATE;
1574 }
1575 }
1576 if (s.status === COMMENT_STATE) {
1577 if (s.gzhead.comment/* != Z_NULL*/) {
1578 beg = s.pending; /* start of bytes to update crc */
1579 //int val;
1580
1581 do {
1582 if (s.pending === s.pending_buf_size) {
1583 if (s.gzhead.hcrc && s.pending > beg) {
1584 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1585 }
1586 flush_pending(strm);
1587 beg = s.pending;
1588 if (s.pending === s.pending_buf_size) {
1589 val = 1;
1590 break;
1591 }
1592 }
1593 // JS specific: little magic to add zero terminator to end of string
1594 if (s.gzindex < s.gzhead.comment.length) {
1595 val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
1596 } else {
1597 val = 0;
1598 }
1599 put_byte(s, val);
1600 } while (val !== 0);
1601
1602 if (s.gzhead.hcrc && s.pending > beg) {
1603 strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1604 }
1605 if (val === 0) {
1606 s.status = HCRC_STATE;
1607 }
1608 }
1609 else {
1610 s.status = HCRC_STATE;
1611 }
1612 }
1613 if (s.status === HCRC_STATE) {
1614 if (s.gzhead.hcrc) {
1615 if (s.pending + 2 > s.pending_buf_size) {
1616 flush_pending(strm);
1617 }
1618 if (s.pending + 2 <= s.pending_buf_size) {
1619 put_byte(s, strm.adler & 0xff);
1620 put_byte(s, (strm.adler >> 8) & 0xff);
1621 strm.adler = 0; //crc32(0L, Z_NULL, 0);
1622 s.status = BUSY_STATE;
1623 }
1624 }
1625 else {
1626 s.status = BUSY_STATE;
1627 }
1628 }
1629//#endif
1630
1631 /* Flush as much pending output as possible */
1632 if (s.pending !== 0) {
1633 flush_pending(strm);
1634 if (strm.avail_out === 0) {
1635 /* Since avail_out is 0, deflate will be called again with
1636 * more output space, but possibly with both pending and
1637 * avail_in equal to zero. There won't be anything to do,
1638 * but this is not an error situation so make sure we
1639 * return OK instead of BUF_ERROR at next call of deflate:
1640 */
1641 s.last_flush = -1;
1642 return Z_OK;
1643 }
1644
1645 /* Make sure there is something to do and avoid duplicate consecutive
1646 * flushes. For repeated and useless calls with Z_FINISH, we keep
1647 * returning Z_STREAM_END instead of Z_BUF_ERROR.
1648 */
1649 } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
1650 flush !== Z_FINISH) {
1651 return err(strm, Z_BUF_ERROR);
1652 }
1653
1654 /* User must not provide more input after the first FINISH: */
1655 if (s.status === FINISH_STATE && strm.avail_in !== 0) {
1656 return err(strm, Z_BUF_ERROR);
1657 }
1658
1659 /* Start a new block or continue the current one.
1660 */
1661 if (strm.avail_in !== 0 || s.lookahead !== 0 ||
1662 (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
1663 var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
1664 (s.strategy === Z_RLE ? deflate_rle(s, flush) :
1665 configuration_table[s.level].func(s, flush));
1666
1667 if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
1668 s.status = FINISH_STATE;
1669 }
1670 if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
1671 if (strm.avail_out === 0) {
1672 s.last_flush = -1;
1673 /* avoid BUF_ERROR next call, see above */
1674 }
1675 return Z_OK;
1676 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1677 * of deflate should use the same flush parameter to make sure
1678 * that the flush is complete. So we don't have to output an
1679 * empty block here, this will be done at next call. This also
1680 * ensures that for a very small output buffer, we emit at most
1681 * one empty block.
1682 */
1683 }
1684 if (bstate === BS_BLOCK_DONE) {
1685 if (flush === Z_PARTIAL_FLUSH) {
1686 trees._tr_align(s);
1687 }
1688 else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1689
1690 trees._tr_stored_block(s, 0, 0, false);
1691 /* For a full flush, this empty block will be recognized
1692 * as a special marker by inflate_sync().
1693 */
1694 if (flush === Z_FULL_FLUSH) {
1695 /*** CLEAR_HASH(s); ***/ /* forget history */
1696 zero(s.head); // Fill with NIL (= 0);
1697
1698 if (s.lookahead === 0) {
1699 s.strstart = 0;
1700 s.block_start = 0;
1701 s.insert = 0;
1702 }
1703 }
1704 }
1705 flush_pending(strm);
1706 if (strm.avail_out === 0) {
1707 s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1708 return Z_OK;
1709 }
1710 }
1711 }
1712 //Assert(strm->avail_out > 0, "bug2");
1713 //if (strm.avail_out <= 0) { throw new Error("bug2");}
1714
1715 if (flush !== Z_FINISH) { return Z_OK; }
1716 if (s.wrap <= 0) { return Z_STREAM_END; }
1717
1718 /* Write the trailer */
1719 if (s.wrap === 2) {
1720 put_byte(s, strm.adler & 0xff);
1721 put_byte(s, (strm.adler >> 8) & 0xff);
1722 put_byte(s, (strm.adler >> 16) & 0xff);
1723 put_byte(s, (strm.adler >> 24) & 0xff);
1724 put_byte(s, strm.total_in & 0xff);
1725 put_byte(s, (strm.total_in >> 8) & 0xff);
1726 put_byte(s, (strm.total_in >> 16) & 0xff);
1727 put_byte(s, (strm.total_in >> 24) & 0xff);
1728 }
1729 else
1730 {
1731 putShortMSB(s, strm.adler >>> 16);
1732 putShortMSB(s, strm.adler & 0xffff);
1733 }
1734
1735 flush_pending(strm);
1736 /* If avail_out is zero, the application will call deflate again
1737 * to flush the rest.
1738 */
1739 if (s.wrap > 0) { s.wrap = -s.wrap; }
1740 /* write the trailer only once! */
1741 return s.pending !== 0 ? Z_OK : Z_STREAM_END;
1742}
1743
1744function deflateEnd(strm) {
1745 var status;
1746
1747 if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
1748 return Z_STREAM_ERROR;
1749 }
1750
1751 status = strm.state.status;
1752 if (status !== INIT_STATE &&
1753 status !== EXTRA_STATE &&
1754 status !== NAME_STATE &&
1755 status !== COMMENT_STATE &&
1756 status !== HCRC_STATE &&
1757 status !== BUSY_STATE &&
1758 status !== FINISH_STATE
1759 ) {
1760 return err(strm, Z_STREAM_ERROR);
1761 }
1762
1763 strm.state = null;
1764
1765 return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
1766}
1767
1768
1769/* =========================================================================
1770 * Initializes the compression dictionary from the given byte
1771 * sequence without producing any compressed output.
1772 */
1773function deflateSetDictionary(strm, dictionary) {
1774 var dictLength = dictionary.length;
1775
1776 var s;
1777 var str, n;
1778 var wrap;
1779 var avail;
1780 var next;
1781 var input;
1782 var tmpDict;
1783
1784 if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
1785 return Z_STREAM_ERROR;
1786 }
1787
1788 s = strm.state;
1789 wrap = s.wrap;
1790
1791 if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
1792 return Z_STREAM_ERROR;
1793 }
1794
1795 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
1796 if (wrap === 1) {
1797 /* adler32(strm->adler, dictionary, dictLength); */
1798 strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
1799 }
1800
1801 s.wrap = 0; /* avoid computing Adler-32 in read_buf */
1802
1803 /* if dictionary would fill window, just replace the history */
1804 if (dictLength >= s.w_size) {
1805 if (wrap === 0) { /* already empty otherwise */
1806 /*** CLEAR_HASH(s); ***/
1807 zero(s.head); // Fill with NIL (= 0);
1808 s.strstart = 0;
1809 s.block_start = 0;
1810 s.insert = 0;
1811 }
1812 /* use the tail */
1813 // dictionary = dictionary.slice(dictLength - s.w_size);
1814 tmpDict = new utils.Buf8(s.w_size);
1815 utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
1816 dictionary = tmpDict;
1817 dictLength = s.w_size;
1818 }
1819 /* insert dictionary into window and hash */
1820 avail = strm.avail_in;
1821 next = strm.next_in;
1822 input = strm.input;
1823 strm.avail_in = dictLength;
1824 strm.next_in = 0;
1825 strm.input = dictionary;
1826 fill_window(s);
1827 while (s.lookahead >= MIN_MATCH) {
1828 str = s.strstart;
1829 n = s.lookahead - (MIN_MATCH - 1);
1830 do {
1831 /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
1832 s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
1833
1834 s.prev[str & s.w_mask] = s.head[s.ins_h];
1835
1836 s.head[s.ins_h] = str;
1837 str++;
1838 } while (--n);
1839 s.strstart = str;
1840 s.lookahead = MIN_MATCH - 1;
1841 fill_window(s);
1842 }
1843 s.strstart += s.lookahead;
1844 s.block_start = s.strstart;
1845 s.insert = s.lookahead;
1846 s.lookahead = 0;
1847 s.match_length = s.prev_length = MIN_MATCH - 1;
1848 s.match_available = 0;
1849 strm.next_in = next;
1850 strm.input = input;
1851 strm.avail_in = avail;
1852 s.wrap = wrap;
1853 return Z_OK;
1854}
1855
1856
1857exports.deflateInit = deflateInit;
1858exports.deflateInit2 = deflateInit2;
1859exports.deflateReset = deflateReset;
1860exports.deflateResetKeep = deflateResetKeep;
1861exports.deflateSetHeader = deflateSetHeader;
1862exports.deflate = deflate;
1863exports.deflateEnd = deflateEnd;
1864exports.deflateSetDictionary = deflateSetDictionary;
1865exports.deflateInfo = 'pako deflate (from Nodeca project)';
1866
1867/* Not implemented
1868exports.deflateBound = deflateBound;
1869exports.deflateCopy = deflateCopy;
1870exports.deflateParams = deflateParams;
1871exports.deflatePending = deflatePending;
1872exports.deflatePrime = deflatePrime;
1873exports.deflateTune = deflateTune;
1874*/
Note: See TracBrowser for help on using the repository browser.