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