react / wstein / node_modules / browserify / node_modules / browserify-zlib / node_modules / pako / lib / zlib / deflate.js
80550 views'use strict';12var utils = require('../utils/common');3var trees = require('./trees');4var adler32 = require('./adler32');5var crc32 = require('./crc32');6var msg = require('./messages');78/* Public constants ==========================================================*/9/* ===========================================================================*/101112/* Allowed flush values; see deflate() and inflate() below for details */13var Z_NO_FLUSH = 0;14var Z_PARTIAL_FLUSH = 1;15//var Z_SYNC_FLUSH = 2;16var Z_FULL_FLUSH = 3;17var Z_FINISH = 4;18var Z_BLOCK = 5;19//var Z_TREES = 6;202122/* Return codes for the compression/decompression functions. Negative values23* are errors, positive values are used for special but normal events.24*/25var Z_OK = 0;26var Z_STREAM_END = 1;27//var Z_NEED_DICT = 2;28//var Z_ERRNO = -1;29var Z_STREAM_ERROR = -2;30var Z_DATA_ERROR = -3;31//var Z_MEM_ERROR = -4;32var Z_BUF_ERROR = -5;33//var Z_VERSION_ERROR = -6;343536/* compression levels */37//var Z_NO_COMPRESSION = 0;38//var Z_BEST_SPEED = 1;39//var Z_BEST_COMPRESSION = 9;40var Z_DEFAULT_COMPRESSION = -1;414243var Z_FILTERED = 1;44var Z_HUFFMAN_ONLY = 2;45var Z_RLE = 3;46var Z_FIXED = 4;47var Z_DEFAULT_STRATEGY = 0;4849/* Possible values of the data_type field (though see inflate()) */50//var Z_BINARY = 0;51//var Z_TEXT = 1;52//var Z_ASCII = 1; // = Z_TEXT53var Z_UNKNOWN = 2;545556/* The deflate compression method */57var Z_DEFLATED = 8;5859/*============================================================================*/606162var MAX_MEM_LEVEL = 9;63/* Maximum value for memLevel in deflateInit2 */64var MAX_WBITS = 15;65/* 32K LZ77 window */66var DEF_MEM_LEVEL = 8;676869var LENGTH_CODES = 29;70/* number of length codes, not counting the special END_BLOCK code */71var LITERALS = 256;72/* number of literal bytes 0..255 */73var L_CODES = LITERALS + 1 + LENGTH_CODES;74/* number of Literal or Length codes, including the END_BLOCK code */75var D_CODES = 30;76/* number of distance codes */77var BL_CODES = 19;78/* number of codes used to transfer the bit lengths */79var HEAP_SIZE = 2*L_CODES + 1;80/* maximum heap size */81var MAX_BITS = 15;82/* All codes must not exceed MAX_BITS bits */8384var MIN_MATCH = 3;85var MAX_MATCH = 258;86var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);8788var PRESET_DICT = 0x20;8990var INIT_STATE = 42;91var EXTRA_STATE = 69;92var NAME_STATE = 73;93var COMMENT_STATE = 91;94var HCRC_STATE = 103;95var BUSY_STATE = 113;96var FINISH_STATE = 666;9798var BS_NEED_MORE = 1; /* block not completed, need more input or more output */99var BS_BLOCK_DONE = 2; /* block flush performed */100var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */101var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */102103var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.104105function err(strm, errorCode) {106strm.msg = msg[errorCode];107return errorCode;108}109110function rank(f) {111return ((f) << 1) - ((f) > 4 ? 9 : 0);112}113114function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }115116117/* =========================================================================118* Flush as much pending output as possible. All deflate() output goes119* through this function so some applications may wish to modify it120* to avoid allocating a large strm->output buffer and copying into it.121* (See also read_buf()).122*/123function flush_pending(strm) {124var s = strm.state;125126//_tr_flush_bits(s);127var len = s.pending;128if (len > strm.avail_out) {129len = strm.avail_out;130}131if (len === 0) { return; }132133utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);134strm.next_out += len;135s.pending_out += len;136strm.total_out += len;137strm.avail_out -= len;138s.pending -= len;139if (s.pending === 0) {140s.pending_out = 0;141}142}143144145function flush_block_only (s, last) {146trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);147s.block_start = s.strstart;148flush_pending(s.strm);149}150151152function put_byte(s, b) {153s.pending_buf[s.pending++] = b;154}155156157/* =========================================================================158* Put a short in the pending buffer. The 16-bit value is put in MSB order.159* IN assertion: the stream state is correct and there is enough room in160* pending_buf.161*/162function putShortMSB(s, b) {163// put_byte(s, (Byte)(b >> 8));164// put_byte(s, (Byte)(b & 0xff));165s.pending_buf[s.pending++] = (b >>> 8) & 0xff;166s.pending_buf[s.pending++] = b & 0xff;167}168169170/* ===========================================================================171* Read a new buffer from the current input stream, update the adler32172* and total number of bytes read. All deflate() input goes through173* this function so some applications may wish to modify it to avoid174* allocating a large strm->input buffer and copying from it.175* (See also flush_pending()).176*/177function read_buf(strm, buf, start, size) {178var len = strm.avail_in;179180if (len > size) { len = size; }181if (len === 0) { return 0; }182183strm.avail_in -= len;184185utils.arraySet(buf, strm.input, strm.next_in, len, start);186if (strm.state.wrap === 1) {187strm.adler = adler32(strm.adler, buf, len, start);188}189190else if (strm.state.wrap === 2) {191strm.adler = crc32(strm.adler, buf, len, start);192}193194strm.next_in += len;195strm.total_in += len;196197return len;198}199200201/* ===========================================================================202* Set match_start to the longest match starting at the given string and203* return its length. Matches shorter or equal to prev_length are discarded,204* in which case the result is equal to prev_length and match_start is205* garbage.206* IN assertions: cur_match is the head of the hash chain for the current207* string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1208* OUT assertion: the match length is not greater than s->lookahead.209*/210function longest_match(s, cur_match) {211var chain_length = s.max_chain_length; /* max hash chain length */212var scan = s.strstart; /* current string */213var match; /* matched string */214var len; /* length of current match */215var best_len = s.prev_length; /* best match length so far */216var nice_match = s.nice_match; /* stop if match long enough */217var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?218s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;219220var _win = s.window; // shortcut221222var wmask = s.w_mask;223var prev = s.prev;224225/* Stop when cur_match becomes <= limit. To simplify the code,226* we prevent matches with the string of window index 0.227*/228229var strend = s.strstart + MAX_MATCH;230var scan_end1 = _win[scan + best_len - 1];231var scan_end = _win[scan + best_len];232233/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.234* It is easy to get rid of this optimization if necessary.235*/236// Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");237238/* Do not waste too much time if we already have a good match: */239if (s.prev_length >= s.good_match) {240chain_length >>= 2;241}242/* Do not look for matches beyond the end of the input. This is necessary243* to make deflate deterministic.244*/245if (nice_match > s.lookahead) { nice_match = s.lookahead; }246247// Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");248249do {250// Assert(cur_match < s->strstart, "no future");251match = cur_match;252253/* Skip to next match if the match length cannot increase254* or if the match length is less than 2. Note that the checks below255* for insufficient lookahead only occur occasionally for performance256* reasons. Therefore uninitialized memory will be accessed, and257* conditional jumps will be made that depend on those values.258* However the length of the match is limited to the lookahead, so259* the output of deflate is not affected by the uninitialized values.260*/261262if (_win[match + best_len] !== scan_end ||263_win[match + best_len - 1] !== scan_end1 ||264_win[match] !== _win[scan] ||265_win[++match] !== _win[scan + 1]) {266continue;267}268269/* The check at best_len-1 can be removed because it will be made270* again later. (This heuristic is not always a win.)271* It is not necessary to compare scan[2] and match[2] since they272* are always equal when the other bytes match, given that273* the hash keys are equal and that HASH_BITS >= 8.274*/275scan += 2;276match++;277// Assert(*scan == *match, "match[2]?");278279/* We check for insufficient lookahead only every 8th comparison;280* the 256th check will be made at strstart+258.281*/282do {283/*jshint noempty:false*/284} while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&285_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&286_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&287_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&288scan < strend);289290// Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");291292len = MAX_MATCH - (strend - scan);293scan = strend - MAX_MATCH;294295if (len > best_len) {296s.match_start = cur_match;297best_len = len;298if (len >= nice_match) {299break;300}301scan_end1 = _win[scan + best_len - 1];302scan_end = _win[scan + best_len];303}304} while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);305306if (best_len <= s.lookahead) {307return best_len;308}309return s.lookahead;310}311312313/* ===========================================================================314* Fill the window when the lookahead becomes insufficient.315* Updates strstart and lookahead.316*317* IN assertion: lookahead < MIN_LOOKAHEAD318* OUT assertions: strstart <= window_size-MIN_LOOKAHEAD319* At least one byte has been read, or avail_in == 0; reads are320* performed for at least two bytes (required for the zip translate_eol321* option -- not supported here).322*/323function fill_window(s) {324var _w_size = s.w_size;325var p, n, m, more, str;326327//Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");328329do {330more = s.window_size - s.lookahead - s.strstart;331332// JS ints have 32 bit, block below not needed333/* Deal with !@#$% 64K limit: */334//if (sizeof(int) <= 2) {335// if (more == 0 && s->strstart == 0 && s->lookahead == 0) {336// more = wsize;337//338// } else if (more == (unsigned)(-1)) {339// /* Very unlikely, but possible on 16 bit machine if340// * strstart == 0 && lookahead == 1 (input done a byte at time)341// */342// more--;343// }344//}345346347/* If the window is almost full and there is insufficient lookahead,348* move the upper half to the lower one to make room in the upper half.349*/350if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {351352utils.arraySet(s.window, s.window, _w_size, _w_size, 0);353s.match_start -= _w_size;354s.strstart -= _w_size;355/* we now have strstart >= MAX_DIST */356s.block_start -= _w_size;357358/* Slide the hash table (could be avoided with 32 bit values359at the expense of memory usage). We slide even when level == 0360to keep the hash table consistent if we switch back to level > 0361later. (Using level 0 permanently is not an optimal usage of362zlib, so we don't care about this pathological case.)363*/364365n = s.hash_size;366p = n;367do {368m = s.head[--p];369s.head[p] = (m >= _w_size ? m - _w_size : 0);370} while (--n);371372n = _w_size;373p = n;374do {375m = s.prev[--p];376s.prev[p] = (m >= _w_size ? m - _w_size : 0);377/* If n is not on any hash chain, prev[n] is garbage but378* its value will never be used.379*/380} while (--n);381382more += _w_size;383}384if (s.strm.avail_in === 0) {385break;386}387388/* If there was no sliding:389* strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&390* more == window_size - lookahead - strstart391* => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)392* => more >= window_size - 2*WSIZE + 2393* In the BIG_MEM or MMAP case (not yet supported),394* window_size == input_size + MIN_LOOKAHEAD &&395* strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.396* Otherwise, window_size == 2*WSIZE so more >= 2.397* If there was sliding, more >= WSIZE. So in all cases, more >= 2.398*/399//Assert(more >= 2, "more < 2");400n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);401s.lookahead += n;402403/* Initialize the hash value now that we have some input: */404if (s.lookahead + s.insert >= MIN_MATCH) {405str = s.strstart - s.insert;406s.ins_h = s.window[str];407408/* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */409s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;410//#if MIN_MATCH != 3411// Call update_hash() MIN_MATCH-3 more times412//#endif413while (s.insert) {414/* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */415s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH-1]) & s.hash_mask;416417s.prev[str & s.w_mask] = s.head[s.ins_h];418s.head[s.ins_h] = str;419str++;420s.insert--;421if (s.lookahead + s.insert < MIN_MATCH) {422break;423}424}425}426/* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,427* but this is not important since only literal bytes will be emitted.428*/429430} while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);431432/* If the WIN_INIT bytes after the end of the current data have never been433* written, then zero those bytes in order to avoid memory check reports of434* the use of uninitialized (or uninitialised as Julian writes) bytes by435* the longest match routines. Update the high water mark for the next436* time through here. WIN_INIT is set to MAX_MATCH since the longest match437* routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.438*/439// if (s.high_water < s.window_size) {440// var curr = s.strstart + s.lookahead;441// var init = 0;442//443// if (s.high_water < curr) {444// /* Previous high water mark below current data -- zero WIN_INIT445// * bytes or up to end of window, whichever is less.446// */447// init = s.window_size - curr;448// if (init > WIN_INIT)449// init = WIN_INIT;450// zmemzero(s->window + curr, (unsigned)init);451// s->high_water = curr + init;452// }453// else if (s->high_water < (ulg)curr + WIN_INIT) {454// /* High water mark at or above current data, but below current data455// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up456// * to end of window, whichever is less.457// */458// init = (ulg)curr + WIN_INIT - s->high_water;459// if (init > s->window_size - s->high_water)460// init = s->window_size - s->high_water;461// zmemzero(s->window + s->high_water, (unsigned)init);462// s->high_water += init;463// }464// }465//466// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,467// "not enough room for search");468}469470/* ===========================================================================471* Copy without compression as much as possible from the input stream, return472* the current block state.473* This function does not insert new strings in the dictionary since474* uncompressible data is probably not useful. This function is used475* only for the level=0 compression option.476* NOTE: this function should be optimized to avoid extra copying from477* window to pending_buf.478*/479function deflate_stored(s, flush) {480/* Stored blocks are limited to 0xffff bytes, pending_buf is limited481* to pending_buf_size, and each stored block has a 5 byte header:482*/483var max_block_size = 0xffff;484485if (max_block_size > s.pending_buf_size - 5) {486max_block_size = s.pending_buf_size - 5;487}488489/* Copy as much as possible from input to output: */490for (;;) {491/* Fill the window as much as possible: */492if (s.lookahead <= 1) {493494//Assert(s->strstart < s->w_size+MAX_DIST(s) ||495// s->block_start >= (long)s->w_size, "slide too late");496// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||497// s.block_start >= s.w_size)) {498// throw new Error("slide too late");499// }500501fill_window(s);502if (s.lookahead === 0 && flush === Z_NO_FLUSH) {503return BS_NEED_MORE;504}505506if (s.lookahead === 0) {507break;508}509/* flush the current block */510}511//Assert(s->block_start >= 0L, "block gone");512// if (s.block_start < 0) throw new Error("block gone");513514s.strstart += s.lookahead;515s.lookahead = 0;516517/* Emit a stored block if pending_buf will be full: */518var max_start = s.block_start + max_block_size;519520if (s.strstart === 0 || s.strstart >= max_start) {521/* strstart == 0 is possible when wraparound on 16-bit machine */522s.lookahead = s.strstart - max_start;523s.strstart = max_start;524/*** FLUSH_BLOCK(s, 0); ***/525flush_block_only(s, false);526if (s.strm.avail_out === 0) {527return BS_NEED_MORE;528}529/***/530531532}533/* Flush if we may have to slide, otherwise block_start may become534* negative and the data will be gone:535*/536if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {537/*** FLUSH_BLOCK(s, 0); ***/538flush_block_only(s, false);539if (s.strm.avail_out === 0) {540return BS_NEED_MORE;541}542/***/543}544}545546s.insert = 0;547548if (flush === Z_FINISH) {549/*** FLUSH_BLOCK(s, 1); ***/550flush_block_only(s, true);551if (s.strm.avail_out === 0) {552return BS_FINISH_STARTED;553}554/***/555return BS_FINISH_DONE;556}557558if (s.strstart > s.block_start) {559/*** FLUSH_BLOCK(s, 0); ***/560flush_block_only(s, false);561if (s.strm.avail_out === 0) {562return BS_NEED_MORE;563}564/***/565}566567return BS_NEED_MORE;568}569570/* ===========================================================================571* Compress as much as possible from the input stream, return the current572* block state.573* This function does not perform lazy evaluation of matches and inserts574* new strings in the dictionary only for unmatched strings or for short575* matches. It is used only for the fast compression options.576*/577function deflate_fast(s, flush) {578var hash_head; /* head of the hash chain */579var bflush; /* set if current block must be flushed */580581for (;;) {582/* Make sure that we always have enough lookahead, except583* at the end of the input file. We need MAX_MATCH bytes584* for the next match, plus MIN_MATCH bytes to insert the585* string following the next match.586*/587if (s.lookahead < MIN_LOOKAHEAD) {588fill_window(s);589if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {590return BS_NEED_MORE;591}592if (s.lookahead === 0) {593break; /* flush the current block */594}595}596597/* Insert the string window[strstart .. strstart+2] in the598* dictionary, and set hash_head to the head of the hash chain:599*/600hash_head = 0/*NIL*/;601if (s.lookahead >= MIN_MATCH) {602/*** INSERT_STRING(s, s.strstart, hash_head); ***/603s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;604hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];605s.head[s.ins_h] = s.strstart;606/***/607}608609/* Find the longest match, discarding those <= prev_length.610* At this point we have always match_length < MIN_MATCH611*/612if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {613/* To simplify the code, we prevent matches with the string614* of window index 0 (in particular we have to avoid a match615* of the string with itself at the start of the input file).616*/617s.match_length = longest_match(s, hash_head);618/* longest_match() sets match_start */619}620if (s.match_length >= MIN_MATCH) {621// check_match(s, s.strstart, s.match_start, s.match_length); // for debug only622623/*** _tr_tally_dist(s, s.strstart - s.match_start,624s.match_length - MIN_MATCH, bflush); ***/625bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);626627s.lookahead -= s.match_length;628629/* Insert new strings in the hash table only if the match length630* is not too large. This saves time but degrades compression.631*/632if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {633s.match_length--; /* string at strstart already in table */634do {635s.strstart++;636/*** INSERT_STRING(s, s.strstart, hash_head); ***/637s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;638hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];639s.head[s.ins_h] = s.strstart;640/***/641/* strstart never exceeds WSIZE-MAX_MATCH, so there are642* always MIN_MATCH bytes ahead.643*/644} while (--s.match_length !== 0);645s.strstart++;646} else647{648s.strstart += s.match_length;649s.match_length = 0;650s.ins_h = s.window[s.strstart];651/* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */652s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;653654//#if MIN_MATCH != 3655// Call UPDATE_HASH() MIN_MATCH-3 more times656//#endif657/* If lookahead < MIN_MATCH, ins_h is garbage, but it does not658* matter since it will be recomputed at next deflate call.659*/660}661} else {662/* No match, output a literal byte */663//Tracevv((stderr,"%c", s.window[s.strstart]));664/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/665bflush = trees._tr_tally(s, 0, s.window[s.strstart]);666667s.lookahead--;668s.strstart++;669}670if (bflush) {671/*** FLUSH_BLOCK(s, 0); ***/672flush_block_only(s, false);673if (s.strm.avail_out === 0) {674return BS_NEED_MORE;675}676/***/677}678}679s.insert = ((s.strstart < (MIN_MATCH-1)) ? s.strstart : MIN_MATCH-1);680if (flush === Z_FINISH) {681/*** FLUSH_BLOCK(s, 1); ***/682flush_block_only(s, true);683if (s.strm.avail_out === 0) {684return BS_FINISH_STARTED;685}686/***/687return BS_FINISH_DONE;688}689if (s.last_lit) {690/*** FLUSH_BLOCK(s, 0); ***/691flush_block_only(s, false);692if (s.strm.avail_out === 0) {693return BS_NEED_MORE;694}695/***/696}697return BS_BLOCK_DONE;698}699700/* ===========================================================================701* Same as above, but achieves better compression. We use a lazy702* evaluation for matches: a match is finally adopted only if there is703* no better match at the next window position.704*/705function deflate_slow(s, flush) {706var hash_head; /* head of hash chain */707var bflush; /* set if current block must be flushed */708709var max_insert;710711/* Process the input block. */712for (;;) {713/* Make sure that we always have enough lookahead, except714* at the end of the input file. We need MAX_MATCH bytes715* for the next match, plus MIN_MATCH bytes to insert the716* string following the next match.717*/718if (s.lookahead < MIN_LOOKAHEAD) {719fill_window(s);720if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {721return BS_NEED_MORE;722}723if (s.lookahead === 0) { break; } /* flush the current block */724}725726/* Insert the string window[strstart .. strstart+2] in the727* dictionary, and set hash_head to the head of the hash chain:728*/729hash_head = 0/*NIL*/;730if (s.lookahead >= MIN_MATCH) {731/*** INSERT_STRING(s, s.strstart, hash_head); ***/732s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;733hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];734s.head[s.ins_h] = s.strstart;735/***/736}737738/* Find the longest match, discarding those <= prev_length.739*/740s.prev_length = s.match_length;741s.prev_match = s.match_start;742s.match_length = MIN_MATCH-1;743744if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&745s.strstart - hash_head <= (s.w_size-MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {746/* To simplify the code, we prevent matches with the string747* of window index 0 (in particular we have to avoid a match748* of the string with itself at the start of the input file).749*/750s.match_length = longest_match(s, hash_head);751/* longest_match() sets match_start */752753if (s.match_length <= 5 &&754(s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {755756/* If prev_match is also MIN_MATCH, match_start is garbage757* but we will ignore the current match anyway.758*/759s.match_length = MIN_MATCH-1;760}761}762/* If there was a match at the previous step and the current763* match is not better, output the previous match:764*/765if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {766max_insert = s.strstart + s.lookahead - MIN_MATCH;767/* Do not insert strings in hash table beyond this. */768769//check_match(s, s.strstart-1, s.prev_match, s.prev_length);770771/***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,772s.prev_length - MIN_MATCH, bflush);***/773bflush = trees._tr_tally(s, s.strstart - 1- s.prev_match, s.prev_length - MIN_MATCH);774/* Insert in hash table all strings up to the end of the match.775* strstart-1 and strstart are already inserted. If there is not776* enough lookahead, the last two strings are not inserted in777* the hash table.778*/779s.lookahead -= s.prev_length-1;780s.prev_length -= 2;781do {782if (++s.strstart <= max_insert) {783/*** INSERT_STRING(s, s.strstart, hash_head); ***/784s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;785hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];786s.head[s.ins_h] = s.strstart;787/***/788}789} while (--s.prev_length !== 0);790s.match_available = 0;791s.match_length = MIN_MATCH-1;792s.strstart++;793794if (bflush) {795/*** FLUSH_BLOCK(s, 0); ***/796flush_block_only(s, false);797if (s.strm.avail_out === 0) {798return BS_NEED_MORE;799}800/***/801}802803} else if (s.match_available) {804/* If there was no match at the previous position, output a805* single literal. If there was a match but the current match806* is longer, truncate the previous match to a single literal.807*/808//Tracevv((stderr,"%c", s->window[s->strstart-1]));809/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/810bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);811812if (bflush) {813/*** FLUSH_BLOCK_ONLY(s, 0) ***/814flush_block_only(s, false);815/***/816}817s.strstart++;818s.lookahead--;819if (s.strm.avail_out === 0) {820return BS_NEED_MORE;821}822} else {823/* There is no previous match to compare with, wait for824* the next step to decide.825*/826s.match_available = 1;827s.strstart++;828s.lookahead--;829}830}831//Assert (flush != Z_NO_FLUSH, "no flush?");832if (s.match_available) {833//Tracevv((stderr,"%c", s->window[s->strstart-1]));834/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/835bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);836837s.match_available = 0;838}839s.insert = s.strstart < MIN_MATCH-1 ? s.strstart : MIN_MATCH-1;840if (flush === Z_FINISH) {841/*** FLUSH_BLOCK(s, 1); ***/842flush_block_only(s, true);843if (s.strm.avail_out === 0) {844return BS_FINISH_STARTED;845}846/***/847return BS_FINISH_DONE;848}849if (s.last_lit) {850/*** FLUSH_BLOCK(s, 0); ***/851flush_block_only(s, false);852if (s.strm.avail_out === 0) {853return BS_NEED_MORE;854}855/***/856}857858return BS_BLOCK_DONE;859}860861862/* ===========================================================================863* For Z_RLE, simply look for runs of bytes, generate matches only of distance864* one. Do not maintain a hash table. (It will be regenerated if this run of865* deflate switches away from Z_RLE.)866*/867function deflate_rle(s, flush) {868var bflush; /* set if current block must be flushed */869var prev; /* byte at distance one to match */870var scan, strend; /* scan goes up to strend for length of run */871872var _win = s.window;873874for (;;) {875/* Make sure that we always have enough lookahead, except876* at the end of the input file. We need MAX_MATCH bytes877* for the longest run, plus one for the unrolled loop.878*/879if (s.lookahead <= MAX_MATCH) {880fill_window(s);881if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {882return BS_NEED_MORE;883}884if (s.lookahead === 0) { break; } /* flush the current block */885}886887/* See how many times the previous byte repeats */888s.match_length = 0;889if (s.lookahead >= MIN_MATCH && s.strstart > 0) {890scan = s.strstart - 1;891prev = _win[scan];892if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {893strend = s.strstart + MAX_MATCH;894do {895/*jshint noempty:false*/896} while (prev === _win[++scan] && prev === _win[++scan] &&897prev === _win[++scan] && prev === _win[++scan] &&898prev === _win[++scan] && prev === _win[++scan] &&899prev === _win[++scan] && prev === _win[++scan] &&900scan < strend);901s.match_length = MAX_MATCH - (strend - scan);902if (s.match_length > s.lookahead) {903s.match_length = s.lookahead;904}905}906//Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");907}908909/* Emit match if have run of MIN_MATCH or longer, else emit literal */910if (s.match_length >= MIN_MATCH) {911//check_match(s, s.strstart, s.strstart - 1, s.match_length);912913/*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/914bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);915916s.lookahead -= s.match_length;917s.strstart += s.match_length;918s.match_length = 0;919} else {920/* No match, output a literal byte */921//Tracevv((stderr,"%c", s->window[s->strstart]));922/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/923bflush = trees._tr_tally(s, 0, s.window[s.strstart]);924925s.lookahead--;926s.strstart++;927}928if (bflush) {929/*** FLUSH_BLOCK(s, 0); ***/930flush_block_only(s, false);931if (s.strm.avail_out === 0) {932return BS_NEED_MORE;933}934/***/935}936}937s.insert = 0;938if (flush === Z_FINISH) {939/*** FLUSH_BLOCK(s, 1); ***/940flush_block_only(s, true);941if (s.strm.avail_out === 0) {942return BS_FINISH_STARTED;943}944/***/945return BS_FINISH_DONE;946}947if (s.last_lit) {948/*** FLUSH_BLOCK(s, 0); ***/949flush_block_only(s, false);950if (s.strm.avail_out === 0) {951return BS_NEED_MORE;952}953/***/954}955return BS_BLOCK_DONE;956}957958/* ===========================================================================959* For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.960* (It will be regenerated if this run of deflate switches away from Huffman.)961*/962function deflate_huff(s, flush) {963var bflush; /* set if current block must be flushed */964965for (;;) {966/* Make sure that we have a literal to write. */967if (s.lookahead === 0) {968fill_window(s);969if (s.lookahead === 0) {970if (flush === Z_NO_FLUSH) {971return BS_NEED_MORE;972}973break; /* flush the current block */974}975}976977/* Output a literal byte */978s.match_length = 0;979//Tracevv((stderr,"%c", s->window[s->strstart]));980/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/981bflush = trees._tr_tally(s, 0, s.window[s.strstart]);982s.lookahead--;983s.strstart++;984if (bflush) {985/*** FLUSH_BLOCK(s, 0); ***/986flush_block_only(s, false);987if (s.strm.avail_out === 0) {988return BS_NEED_MORE;989}990/***/991}992}993s.insert = 0;994if (flush === Z_FINISH) {995/*** FLUSH_BLOCK(s, 1); ***/996flush_block_only(s, true);997if (s.strm.avail_out === 0) {998return BS_FINISH_STARTED;999}1000/***/1001return BS_FINISH_DONE;1002}1003if (s.last_lit) {1004/*** FLUSH_BLOCK(s, 0); ***/1005flush_block_only(s, false);1006if (s.strm.avail_out === 0) {1007return BS_NEED_MORE;1008}1009/***/1010}1011return BS_BLOCK_DONE;1012}10131014/* Values for max_lazy_match, good_match and max_chain_length, depending on1015* the desired pack level (0..9). The values given below have been tuned to1016* exclude worst case performance for pathological files. Better values may be1017* found for specific files.1018*/1019var Config = function (good_length, max_lazy, nice_length, max_chain, func) {1020this.good_length = good_length;1021this.max_lazy = max_lazy;1022this.nice_length = nice_length;1023this.max_chain = max_chain;1024this.func = func;1025};10261027var configuration_table;10281029configuration_table = [1030/* good lazy nice chain */1031new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */1032new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */1033new Config(4, 5, 16, 8, deflate_fast), /* 2 */1034new Config(4, 6, 32, 32, deflate_fast), /* 3 */10351036new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */1037new Config(8, 16, 32, 32, deflate_slow), /* 5 */1038new Config(8, 16, 128, 128, deflate_slow), /* 6 */1039new Config(8, 32, 128, 256, deflate_slow), /* 7 */1040new Config(32, 128, 258, 1024, deflate_slow), /* 8 */1041new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */1042];104310441045/* ===========================================================================1046* Initialize the "longest match" routines for a new zlib stream1047*/1048function lm_init(s) {1049s.window_size = 2 * s.w_size;10501051/*** CLEAR_HASH(s); ***/1052zero(s.head); // Fill with NIL (= 0);10531054/* Set the default configuration parameters:1055*/1056s.max_lazy_match = configuration_table[s.level].max_lazy;1057s.good_match = configuration_table[s.level].good_length;1058s.nice_match = configuration_table[s.level].nice_length;1059s.max_chain_length = configuration_table[s.level].max_chain;10601061s.strstart = 0;1062s.block_start = 0;1063s.lookahead = 0;1064s.insert = 0;1065s.match_length = s.prev_length = MIN_MATCH - 1;1066s.match_available = 0;1067s.ins_h = 0;1068}106910701071function DeflateState() {1072this.strm = null; /* pointer back to this zlib stream */1073this.status = 0; /* as the name implies */1074this.pending_buf = null; /* output still pending */1075this.pending_buf_size = 0; /* size of pending_buf */1076this.pending_out = 0; /* next pending byte to output to the stream */1077this.pending = 0; /* nb of bytes in the pending buffer */1078this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */1079this.gzhead = null; /* gzip header information to write */1080this.gzindex = 0; /* where in extra, name, or comment */1081this.method = Z_DEFLATED; /* can only be DEFLATED */1082this.last_flush = -1; /* value of flush param for previous deflate call */10831084this.w_size = 0; /* LZ77 window size (32K by default) */1085this.w_bits = 0; /* log2(w_size) (8..16) */1086this.w_mask = 0; /* w_size - 1 */10871088this.window = null;1089/* Sliding window. Input bytes are read into the second half of the window,1090* and move to the first half later to keep a dictionary of at least wSize1091* bytes. With this organization, matches are limited to a distance of1092* wSize-MAX_MATCH bytes, but this ensures that IO is always1093* performed with a length multiple of the block size.1094*/10951096this.window_size = 0;1097/* Actual size of window: 2*wSize, except when the user input buffer1098* is directly used as sliding window.1099*/11001101this.prev = null;1102/* Link to older string with same hash index. To limit the size of this1103* array to 64K, this link is maintained only for the last 32K strings.1104* An index in this array is thus a window index modulo 32K.1105*/11061107this.head = null; /* Heads of the hash chains or NIL. */11081109this.ins_h = 0; /* hash index of string to be inserted */1110this.hash_size = 0; /* number of elements in hash table */1111this.hash_bits = 0; /* log2(hash_size) */1112this.hash_mask = 0; /* hash_size-1 */11131114this.hash_shift = 0;1115/* Number of bits by which ins_h must be shifted at each input1116* step. It must be such that after MIN_MATCH steps, the oldest1117* byte no longer takes part in the hash key, that is:1118* hash_shift * MIN_MATCH >= hash_bits1119*/11201121this.block_start = 0;1122/* Window position at the beginning of the current output block. Gets1123* negative when the window is moved backwards.1124*/11251126this.match_length = 0; /* length of best match */1127this.prev_match = 0; /* previous match */1128this.match_available = 0; /* set if previous match exists */1129this.strstart = 0; /* start of string to insert */1130this.match_start = 0; /* start of matching string */1131this.lookahead = 0; /* number of valid bytes ahead in window */11321133this.prev_length = 0;1134/* Length of the best match at previous step. Matches not greater than this1135* are discarded. This is used in the lazy match evaluation.1136*/11371138this.max_chain_length = 0;1139/* To speed up deflation, hash chains are never searched beyond this1140* length. A higher limit improves compression ratio but degrades the1141* speed.1142*/11431144this.max_lazy_match = 0;1145/* Attempt to find a better match only when the current match is strictly1146* smaller than this value. This mechanism is used only for compression1147* levels >= 4.1148*/1149// That's alias to max_lazy_match, don't use directly1150//this.max_insert_length = 0;1151/* Insert new strings in the hash table only if the match length is not1152* greater than this length. This saves time but degrades compression.1153* max_insert_length is used only for compression levels <= 3.1154*/11551156this.level = 0; /* compression level (1..9) */1157this.strategy = 0; /* favor or force Huffman coding*/11581159this.good_match = 0;1160/* Use a faster search when the previous match is longer than this */11611162this.nice_match = 0; /* Stop searching when current match exceeds this */11631164/* used by trees.c: */11651166/* Didn't use ct_data typedef below to suppress compiler warning */11671168// struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */1169// struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */1170// struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */11711172// Use flat array of DOUBLE size, with interleaved fata,1173// because JS does not support effective1174this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);1175this.dyn_dtree = new utils.Buf16((2*D_CODES+1) * 2);1176this.bl_tree = new utils.Buf16((2*BL_CODES+1) * 2);1177zero(this.dyn_ltree);1178zero(this.dyn_dtree);1179zero(this.bl_tree);11801181this.l_desc = null; /* desc. for literal tree */1182this.d_desc = null; /* desc. for distance tree */1183this.bl_desc = null; /* desc. for bit length tree */11841185//ush bl_count[MAX_BITS+1];1186this.bl_count = new utils.Buf16(MAX_BITS+1);1187/* number of codes at each bit length for an optimal tree */11881189//int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */1190this.heap = new utils.Buf16(2*L_CODES+1); /* heap used to build the Huffman trees */1191zero(this.heap);11921193this.heap_len = 0; /* number of elements in the heap */1194this.heap_max = 0; /* element of largest frequency */1195/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.1196* The same heap array is used to build all trees.1197*/11981199this.depth = new utils.Buf16(2*L_CODES+1); //uch depth[2*L_CODES+1];1200zero(this.depth);1201/* Depth of each subtree used as tie breaker for trees of equal frequency1202*/12031204this.l_buf = 0; /* buffer index for literals or lengths */12051206this.lit_bufsize = 0;1207/* Size of match buffer for literals/lengths. There are 4 reasons for1208* limiting lit_bufsize to 64K:1209* - frequencies can be kept in 16 bit counters1210* - if compression is not successful for the first block, all input1211* data is still in the window so we can still emit a stored block even1212* when input comes from standard input. (This can also be done for1213* all blocks if lit_bufsize is not greater than 32K.)1214* - if compression is not successful for a file smaller than 64K, we can1215* even emit a stored file instead of a stored block (saving 5 bytes).1216* This is applicable only for zip (not gzip or zlib).1217* - creating new Huffman trees less frequently may not provide fast1218* adaptation to changes in the input data statistics. (Take for1219* example a binary file with poorly compressible code followed by1220* a highly compressible string table.) Smaller buffer sizes give1221* fast adaptation but have of course the overhead of transmitting1222* trees more frequently.1223* - I can't count above 41224*/12251226this.last_lit = 0; /* running index in l_buf */12271228this.d_buf = 0;1229/* Buffer index for distances. To simplify the code, d_buf and l_buf have1230* the same number of elements. To use different lengths, an extra flag1231* array would be necessary.1232*/12331234this.opt_len = 0; /* bit length of current block with optimal trees */1235this.static_len = 0; /* bit length of current block with static trees */1236this.matches = 0; /* number of string matches in current block */1237this.insert = 0; /* bytes at end of window left to insert */123812391240this.bi_buf = 0;1241/* Output buffer. bits are inserted starting at the bottom (least1242* significant bits).1243*/1244this.bi_valid = 0;1245/* Number of valid bits in bi_buf. All bits above the last valid bit1246* are always zero.1247*/12481249// Used for window memory init. We safely ignore it for JS. That makes1250// sense only for pointers and memory check tools.1251//this.high_water = 0;1252/* High water mark offset in window for initialized bytes -- bytes above1253* this are set to zero in order to avoid memory check warnings when1254* longest match routines access bytes past the input. This is then1255* updated to the new high water mark.1256*/1257}125812591260function deflateResetKeep(strm) {1261var s;12621263if (!strm || !strm.state) {1264return err(strm, Z_STREAM_ERROR);1265}12661267strm.total_in = strm.total_out = 0;1268strm.data_type = Z_UNKNOWN;12691270s = strm.state;1271s.pending = 0;1272s.pending_out = 0;12731274if (s.wrap < 0) {1275s.wrap = -s.wrap;1276/* was made negative by deflate(..., Z_FINISH); */1277}1278s.status = (s.wrap ? INIT_STATE : BUSY_STATE);1279strm.adler = (s.wrap === 2) ?12800 // crc32(0, Z_NULL, 0)1281:12821; // adler32(0, Z_NULL, 0)1283s.last_flush = Z_NO_FLUSH;1284trees._tr_init(s);1285return Z_OK;1286}128712881289function deflateReset(strm) {1290var ret = deflateResetKeep(strm);1291if (ret === Z_OK) {1292lm_init(strm.state);1293}1294return ret;1295}129612971298function deflateSetHeader(strm, head) {1299if (!strm || !strm.state) { return Z_STREAM_ERROR; }1300if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }1301strm.state.gzhead = head;1302return Z_OK;1303}130413051306function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {1307if (!strm) { // === Z_NULL1308return Z_STREAM_ERROR;1309}1310var wrap = 1;13111312if (level === Z_DEFAULT_COMPRESSION) {1313level = 6;1314}13151316if (windowBits < 0) { /* suppress zlib wrapper */1317wrap = 0;1318windowBits = -windowBits;1319}13201321else if (windowBits > 15) {1322wrap = 2; /* write gzip wrapper instead */1323windowBits -= 16;1324}132513261327if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||1328windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||1329strategy < 0 || strategy > Z_FIXED) {1330return err(strm, Z_STREAM_ERROR);1331}133213331334if (windowBits === 8) {1335windowBits = 9;1336}1337/* until 256-byte window bug fixed */13381339var s = new DeflateState();13401341strm.state = s;1342s.strm = strm;13431344s.wrap = wrap;1345s.gzhead = null;1346s.w_bits = windowBits;1347s.w_size = 1 << s.w_bits;1348s.w_mask = s.w_size - 1;13491350s.hash_bits = memLevel + 7;1351s.hash_size = 1 << s.hash_bits;1352s.hash_mask = s.hash_size - 1;1353s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);13541355s.window = new utils.Buf8(s.w_size * 2);1356s.head = new utils.Buf16(s.hash_size);1357s.prev = new utils.Buf16(s.w_size);13581359// Don't need mem init magic for JS.1360//s.high_water = 0; /* nothing written to s->window yet */13611362s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */13631364s.pending_buf_size = s.lit_bufsize * 4;1365s.pending_buf = new utils.Buf8(s.pending_buf_size);13661367s.d_buf = s.lit_bufsize >> 1;1368s.l_buf = (1 + 2) * s.lit_bufsize;13691370s.level = level;1371s.strategy = strategy;1372s.method = method;13731374return deflateReset(strm);1375}13761377function deflateInit(strm, level) {1378return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);1379}138013811382function deflate(strm, flush) {1383var old_flush, s;1384var beg, val; // for gzip header write only13851386if (!strm || !strm.state ||1387flush > Z_BLOCK || flush < 0) {1388return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;1389}13901391s = strm.state;13921393if (!strm.output ||1394(!strm.input && strm.avail_in !== 0) ||1395(s.status === FINISH_STATE && flush !== Z_FINISH)) {1396return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);1397}13981399s.strm = strm; /* just in case */1400old_flush = s.last_flush;1401s.last_flush = flush;14021403/* Write the header */1404if (s.status === INIT_STATE) {14051406if (s.wrap === 2) { // GZIP header1407strm.adler = 0; //crc32(0L, Z_NULL, 0);1408put_byte(s, 31);1409put_byte(s, 139);1410put_byte(s, 8);1411if (!s.gzhead) { // s->gzhead == Z_NULL1412put_byte(s, 0);1413put_byte(s, 0);1414put_byte(s, 0);1415put_byte(s, 0);1416put_byte(s, 0);1417put_byte(s, s.level === 9 ? 2 :1418(s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?14194 : 0));1420put_byte(s, OS_CODE);1421s.status = BUSY_STATE;1422}1423else {1424put_byte(s, (s.gzhead.text ? 1 : 0) +1425(s.gzhead.hcrc ? 2 : 0) +1426(!s.gzhead.extra ? 0 : 4) +1427(!s.gzhead.name ? 0 : 8) +1428(!s.gzhead.comment ? 0 : 16)1429);1430put_byte(s, s.gzhead.time & 0xff);1431put_byte(s, (s.gzhead.time >> 8) & 0xff);1432put_byte(s, (s.gzhead.time >> 16) & 0xff);1433put_byte(s, (s.gzhead.time >> 24) & 0xff);1434put_byte(s, s.level === 9 ? 2 :1435(s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?14364 : 0));1437put_byte(s, s.gzhead.os & 0xff);1438if (s.gzhead.extra && s.gzhead.extra.length) {1439put_byte(s, s.gzhead.extra.length & 0xff);1440put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);1441}1442if (s.gzhead.hcrc) {1443strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);1444}1445s.gzindex = 0;1446s.status = EXTRA_STATE;1447}1448}1449else // DEFLATE header1450{1451var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;1452var level_flags = -1;14531454if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {1455level_flags = 0;1456} else if (s.level < 6) {1457level_flags = 1;1458} else if (s.level === 6) {1459level_flags = 2;1460} else {1461level_flags = 3;1462}1463header |= (level_flags << 6);1464if (s.strstart !== 0) { header |= PRESET_DICT; }1465header += 31 - (header % 31);14661467s.status = BUSY_STATE;1468putShortMSB(s, header);14691470/* Save the adler32 of the preset dictionary: */1471if (s.strstart !== 0) {1472putShortMSB(s, strm.adler >>> 16);1473putShortMSB(s, strm.adler & 0xffff);1474}1475strm.adler = 1; // adler32(0L, Z_NULL, 0);1476}1477}14781479//#ifdef GZIP1480if (s.status === EXTRA_STATE) {1481if (s.gzhead.extra/* != Z_NULL*/) {1482beg = s.pending; /* start of bytes to update crc */14831484while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {1485if (s.pending === s.pending_buf_size) {1486if (s.gzhead.hcrc && s.pending > beg) {1487strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1488}1489flush_pending(strm);1490beg = s.pending;1491if (s.pending === s.pending_buf_size) {1492break;1493}1494}1495put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);1496s.gzindex++;1497}1498if (s.gzhead.hcrc && s.pending > beg) {1499strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1500}1501if (s.gzindex === s.gzhead.extra.length) {1502s.gzindex = 0;1503s.status = NAME_STATE;1504}1505}1506else {1507s.status = NAME_STATE;1508}1509}1510if (s.status === NAME_STATE) {1511if (s.gzhead.name/* != Z_NULL*/) {1512beg = s.pending; /* start of bytes to update crc */1513//int val;15141515do {1516if (s.pending === s.pending_buf_size) {1517if (s.gzhead.hcrc && s.pending > beg) {1518strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1519}1520flush_pending(strm);1521beg = s.pending;1522if (s.pending === s.pending_buf_size) {1523val = 1;1524break;1525}1526}1527// JS specific: little magic to add zero terminator to end of string1528if (s.gzindex < s.gzhead.name.length) {1529val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;1530} else {1531val = 0;1532}1533put_byte(s, val);1534} while (val !== 0);15351536if (s.gzhead.hcrc && s.pending > beg){1537strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1538}1539if (val === 0) {1540s.gzindex = 0;1541s.status = COMMENT_STATE;1542}1543}1544else {1545s.status = COMMENT_STATE;1546}1547}1548if (s.status === COMMENT_STATE) {1549if (s.gzhead.comment/* != Z_NULL*/) {1550beg = s.pending; /* start of bytes to update crc */1551//int val;15521553do {1554if (s.pending === s.pending_buf_size) {1555if (s.gzhead.hcrc && s.pending > beg) {1556strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1557}1558flush_pending(strm);1559beg = s.pending;1560if (s.pending === s.pending_buf_size) {1561val = 1;1562break;1563}1564}1565// JS specific: little magic to add zero terminator to end of string1566if (s.gzindex < s.gzhead.comment.length) {1567val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;1568} else {1569val = 0;1570}1571put_byte(s, val);1572} while (val !== 0);15731574if (s.gzhead.hcrc && s.pending > beg) {1575strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);1576}1577if (val === 0) {1578s.status = HCRC_STATE;1579}1580}1581else {1582s.status = HCRC_STATE;1583}1584}1585if (s.status === HCRC_STATE) {1586if (s.gzhead.hcrc) {1587if (s.pending + 2 > s.pending_buf_size) {1588flush_pending(strm);1589}1590if (s.pending + 2 <= s.pending_buf_size) {1591put_byte(s, strm.adler & 0xff);1592put_byte(s, (strm.adler >> 8) & 0xff);1593strm.adler = 0; //crc32(0L, Z_NULL, 0);1594s.status = BUSY_STATE;1595}1596}1597else {1598s.status = BUSY_STATE;1599}1600}1601//#endif16021603/* Flush as much pending output as possible */1604if (s.pending !== 0) {1605flush_pending(strm);1606if (strm.avail_out === 0) {1607/* Since avail_out is 0, deflate will be called again with1608* more output space, but possibly with both pending and1609* avail_in equal to zero. There won't be anything to do,1610* but this is not an error situation so make sure we1611* return OK instead of BUF_ERROR at next call of deflate:1612*/1613s.last_flush = -1;1614return Z_OK;1615}16161617/* Make sure there is something to do and avoid duplicate consecutive1618* flushes. For repeated and useless calls with Z_FINISH, we keep1619* returning Z_STREAM_END instead of Z_BUF_ERROR.1620*/1621} else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&1622flush !== Z_FINISH) {1623return err(strm, Z_BUF_ERROR);1624}16251626/* User must not provide more input after the first FINISH: */1627if (s.status === FINISH_STATE && strm.avail_in !== 0) {1628return err(strm, Z_BUF_ERROR);1629}16301631/* Start a new block or continue the current one.1632*/1633if (strm.avail_in !== 0 || s.lookahead !== 0 ||1634(flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {1635var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :1636(s.strategy === Z_RLE ? deflate_rle(s, flush) :1637configuration_table[s.level].func(s, flush));16381639if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {1640s.status = FINISH_STATE;1641}1642if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {1643if (strm.avail_out === 0) {1644s.last_flush = -1;1645/* avoid BUF_ERROR next call, see above */1646}1647return Z_OK;1648/* If flush != Z_NO_FLUSH && avail_out == 0, the next call1649* of deflate should use the same flush parameter to make sure1650* that the flush is complete. So we don't have to output an1651* empty block here, this will be done at next call. This also1652* ensures that for a very small output buffer, we emit at most1653* one empty block.1654*/1655}1656if (bstate === BS_BLOCK_DONE) {1657if (flush === Z_PARTIAL_FLUSH) {1658trees._tr_align(s);1659}1660else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */16611662trees._tr_stored_block(s, 0, 0, false);1663/* For a full flush, this empty block will be recognized1664* as a special marker by inflate_sync().1665*/1666if (flush === Z_FULL_FLUSH) {1667/*** CLEAR_HASH(s); ***/ /* forget history */1668zero(s.head); // Fill with NIL (= 0);16691670if (s.lookahead === 0) {1671s.strstart = 0;1672s.block_start = 0;1673s.insert = 0;1674}1675}1676}1677flush_pending(strm);1678if (strm.avail_out === 0) {1679s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */1680return Z_OK;1681}1682}1683}1684//Assert(strm->avail_out > 0, "bug2");1685//if (strm.avail_out <= 0) { throw new Error("bug2");}16861687if (flush !== Z_FINISH) { return Z_OK; }1688if (s.wrap <= 0) { return Z_STREAM_END; }16891690/* Write the trailer */1691if (s.wrap === 2) {1692put_byte(s, strm.adler & 0xff);1693put_byte(s, (strm.adler >> 8) & 0xff);1694put_byte(s, (strm.adler >> 16) & 0xff);1695put_byte(s, (strm.adler >> 24) & 0xff);1696put_byte(s, strm.total_in & 0xff);1697put_byte(s, (strm.total_in >> 8) & 0xff);1698put_byte(s, (strm.total_in >> 16) & 0xff);1699put_byte(s, (strm.total_in >> 24) & 0xff);1700}1701else1702{1703putShortMSB(s, strm.adler >>> 16);1704putShortMSB(s, strm.adler & 0xffff);1705}17061707flush_pending(strm);1708/* If avail_out is zero, the application will call deflate again1709* to flush the rest.1710*/1711if (s.wrap > 0) { s.wrap = -s.wrap; }1712/* write the trailer only once! */1713return s.pending !== 0 ? Z_OK : Z_STREAM_END;1714}17151716function deflateEnd(strm) {1717var status;17181719if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {1720return Z_STREAM_ERROR;1721}17221723status = strm.state.status;1724if (status !== INIT_STATE &&1725status !== EXTRA_STATE &&1726status !== NAME_STATE &&1727status !== COMMENT_STATE &&1728status !== HCRC_STATE &&1729status !== BUSY_STATE &&1730status !== FINISH_STATE1731) {1732return err(strm, Z_STREAM_ERROR);1733}17341735strm.state = null;17361737return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;1738}17391740/* =========================================================================1741* Copy the source state to the destination state1742*/1743//function deflateCopy(dest, source) {1744//1745//}17461747exports.deflateInit = deflateInit;1748exports.deflateInit2 = deflateInit2;1749exports.deflateReset = deflateReset;1750exports.deflateResetKeep = deflateResetKeep;1751exports.deflateSetHeader = deflateSetHeader;1752exports.deflate = deflate;1753exports.deflateEnd = deflateEnd;1754exports.deflateInfo = 'pako deflate (from Nodeca project)';17551756/* Not implemented1757exports.deflateBound = deflateBound;1758exports.deflateCopy = deflateCopy;1759exports.deflateSetDictionary = deflateSetDictionary;1760exports.deflateParams = deflateParams;1761exports.deflatePending = deflatePending;1762exports.deflatePrime = deflatePrime;1763exports.deflateTune = deflateTune;1764*/17651766