| 1 |
'use strict'; |
| 2 |
|
| 3 |
import {Buf8,Buf16,Buf32,arraySet} from './utils'; |
| 4 |
import adler32 from './adler32'; |
| 5 |
import crc32 from './crc32'; |
| 6 |
import inflate_fast from './inffast'; |
| 7 |
import inflate_table from './inftrees'; |
| 8 |
|
| 9 |
var CODES = 0; |
| 10 |
var LENS = 1; |
| 11 |
var DISTS = 2; |
| 12 |
|
| 13 |
/* Public constants ==========================================================*/ |
| 14 |
/* ===========================================================================*/ |
| 15 |
|
| 16 |
|
| 17 |
/* Allowed flush values; see deflate() and inflate() below for details */ |
| 18 |
//var Z_NO_FLUSH = 0; |
| 19 |
//var Z_PARTIAL_FLUSH = 1; |
| 20 |
//var Z_SYNC_FLUSH = 2; |
| 21 |
//var Z_FULL_FLUSH = 3; |
| 22 |
var Z_FINISH = 4; |
| 23 |
var Z_BLOCK = 5; |
| 24 |
var Z_TREES = 6; |
| 25 |
|
| 26 |
|
| 27 |
/* Return codes for the compression/decompression functions. Negative values |
| 28 |
* are errors, positive values are used for special but normal events. |
| 29 |
*/ |
| 30 |
var Z_OK = 0; |
| 31 |
var Z_STREAM_END = 1; |
| 32 |
var Z_NEED_DICT = 2; |
| 33 |
//var Z_ERRNO = -1; |
| 34 |
var Z_STREAM_ERROR = -2; |
| 35 |
var Z_DATA_ERROR = -3; |
| 36 |
var Z_MEM_ERROR = -4; |
| 37 |
var Z_BUF_ERROR = -5; |
| 38 |
//var Z_VERSION_ERROR = -6; |
| 39 |
|
| 40 |
/* The deflate compression method */ |
| 41 |
var Z_DEFLATED = 8; |
| 42 |
|
| 43 |
|
| 44 |
/* STATES ====================================================================*/ |
| 45 |
/* ===========================================================================*/ |
| 46 |
|
| 47 |
|
| 48 |
var HEAD = 1; /* i: waiting for magic header */ |
| 49 |
var FLAGS = 2; /* i: waiting for method and flags (gzip) */ |
| 50 |
var TIME = 3; /* i: waiting for modification time (gzip) */ |
| 51 |
var OS = 4; /* i: waiting for extra flags and operating system (gzip) */ |
| 52 |
var EXLEN = 5; /* i: waiting for extra length (gzip) */ |
| 53 |
var EXTRA = 6; /* i: waiting for extra bytes (gzip) */ |
| 54 |
var NAME = 7; /* i: waiting for end of file name (gzip) */ |
| 55 |
var COMMENT = 8; /* i: waiting for end of comment (gzip) */ |
| 56 |
var HCRC = 9; /* i: waiting for header crc (gzip) */ |
| 57 |
var DICTID = 10; /* i: waiting for dictionary check value */ |
| 58 |
var DICT = 11; /* waiting for inflateSetDictionary() call */ |
| 59 |
var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ |
| 60 |
var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */ |
| 61 |
var STORED = 14; /* i: waiting for stored size (length and complement) */ |
| 62 |
var COPY_ = 15; /* i/o: same as COPY below, but only first time in */ |
| 63 |
var COPY = 16; /* i/o: waiting for input or output to copy stored block */ |
| 64 |
var TABLE = 17; /* i: waiting for dynamic block table lengths */ |
| 65 |
var LENLENS = 18; /* i: waiting for code length code lengths */ |
| 66 |
var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */ |
| 67 |
var LEN_ = 20; /* i: same as LEN below, but only first time in */ |
| 68 |
var LEN = 21; /* i: waiting for length/lit/eob code */ |
| 69 |
var LENEXT = 22; /* i: waiting for length extra bits */ |
| 70 |
var DIST = 23; /* i: waiting for distance code */ |
| 71 |
var DISTEXT = 24; /* i: waiting for distance extra bits */ |
| 72 |
var MATCH = 25; /* o: waiting for output space to copy string */ |
| 73 |
var LIT = 26; /* o: waiting for output space to write literal */ |
| 74 |
var CHECK = 27; /* i: waiting for 32-bit check value */ |
| 75 |
var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */ |
| 76 |
var DONE = 29; /* finished check, done -- remain here until reset */ |
| 77 |
var BAD = 30; /* got a data error -- remain here until reset */ |
| 78 |
var MEM = 31; /* got an inflate() memory error -- remain here until reset */ |
| 79 |
var SYNC = 32; /* looking for synchronization bytes to restart inflate() */ |
| 80 |
|
| 81 |
/* ===========================================================================*/ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
var ENOUGH_LENS = 852; |
| 86 |
var ENOUGH_DISTS = 592; |
| 87 |
//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); |
| 88 |
|
| 89 |
var MAX_WBITS = 15; |
| 90 |
/* 32K LZ77 window */ |
| 91 |
var DEF_WBITS = MAX_WBITS; |
| 92 |
|
| 93 |
|
| 94 |
function zswap32(q) { |
| 95 |
return (((q >>> 24) & 0xff) + |
| 96 |
((q >>> 8) & 0xff00) + |
| 97 |
((q & 0xff00) << 8) + |
| 98 |
((q & 0xff) << 24)); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
function InflateState() { |
| 103 |
this.mode = 0; /* current inflate mode */ |
| 104 |
this.last = false; /* true if processing last block */ |
| 105 |
this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ |
| 106 |
this.havedict = false; /* true if dictionary provided */ |
| 107 |
this.flags = 0; /* gzip header method and flags (0 if zlib) */ |
| 108 |
this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ |
| 109 |
this.check = 0; /* protected copy of check value */ |
| 110 |
this.total = 0; /* protected copy of output count */ |
| 111 |
// TODO: may be {} |
| 112 |
this.head = null; /* where to save gzip header information */ |
| 113 |
|
| 114 |
/* sliding window */ |
| 115 |
this.wbits = 0; /* log base 2 of requested window size */ |
| 116 |
this.wsize = 0; /* window size or zero if not using window */ |
| 117 |
this.whave = 0; /* valid bytes in the window */ |
| 118 |
this.wnext = 0; /* window write index */ |
| 119 |
this.window = null; /* allocated sliding window, if needed */ |
| 120 |
|
| 121 |
/* bit accumulator */ |
| 122 |
this.hold = 0; /* input bit accumulator */ |
| 123 |
this.bits = 0; /* number of bits in "in" */ |
| 124 |
|
| 125 |
/* for string and stored block copying */ |
| 126 |
this.length = 0; /* literal or length of data to copy */ |
| 127 |
this.offset = 0; /* distance back to copy string from */ |
| 128 |
|
| 129 |
/* for table and code decoding */ |
| 130 |
this.extra = 0; /* extra bits needed */ |
| 131 |
|
| 132 |
/* fixed and dynamic code tables */ |
| 133 |
this.lencode = null; /* starting table for length/literal codes */ |
| 134 |
this.distcode = null; /* starting table for distance codes */ |
| 135 |
this.lenbits = 0; /* index bits for lencode */ |
| 136 |
this.distbits = 0; /* index bits for distcode */ |
| 137 |
|
| 138 |
/* dynamic table building */ |
| 139 |
this.ncode = 0; /* number of code length code lengths */ |
| 140 |
this.nlen = 0; /* number of length code lengths */ |
| 141 |
this.ndist = 0; /* number of distance code lengths */ |
| 142 |
this.have = 0; /* number of code lengths in lens[] */ |
| 143 |
this.next = null; /* next available space in codes[] */ |
| 144 |
|
| 145 |
this.lens = new Buf16(320); /* temporary storage for code lengths */ |
| 146 |
this.work = new Buf16(288); /* work area for code table building */ |
| 147 |
|
| 148 |
/* |
| 149 |
because we don't have pointers in js, we use lencode and distcode directly |
| 150 |
as buffers so we don't need codes |
| 151 |
*/ |
| 152 |
//this.codes = new Buf32(ENOUGH); /* space for code tables */ |
| 153 |
this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ |
| 154 |
this.distdyn = null; /* dynamic table for distance codes (JS specific) */ |
| 155 |
this.sane = 0; /* if false, allow invalid distance too far */ |
| 156 |
this.back = 0; /* bits back of last unprocessed length/lit */ |
| 157 |
this.was = 0; /* initial length of match */ |
| 158 |
} |
| 159 |
|
| 160 |
export function inflateResetKeep(strm) { |
| 161 |
var state; |
| 162 |
|
| 163 |
if (!strm || !strm.state) { |
| 164 |
return Z_STREAM_ERROR; |
| 165 |
} |
| 166 |
state = strm.state; |
| 167 |
strm.total_in = strm.total_out = state.total = 0; |
| 168 |
strm.msg = ''; /*Z_NULL*/ |
| 169 |
if (state.wrap) { /* to support ill-conceived Java test suite */ |
| 170 |
strm.adler = state.wrap & 1; |
| 171 |
} |
| 172 |
state.mode = HEAD; |
| 173 |
state.last = 0; |
| 174 |
state.havedict = 0; |
| 175 |
state.dmax = 32768; |
| 176 |
state.head = null /*Z_NULL*/ ; |
| 177 |
state.hold = 0; |
| 178 |
state.bits = 0; |
| 179 |
//state.lencode = state.distcode = state.next = state.codes; |
| 180 |
state.lencode = state.lendyn = new Buf32(ENOUGH_LENS); |
| 181 |
state.distcode = state.distdyn = new Buf32(ENOUGH_DISTS); |
| 182 |
|
| 183 |
state.sane = 1; |
| 184 |
state.back = -1; |
| 185 |
//Tracev((stderr, "inflate: reset\n")); |
| 186 |
return Z_OK; |
| 187 |
} |
| 188 |
|
| 189 |
export function inflateReset(strm) { |
| 190 |
var state; |
| 191 |
|
| 192 |
if (!strm || !strm.state) { |
| 193 |
return Z_STREAM_ERROR; |
| 194 |
} |
| 195 |
state = strm.state; |
| 196 |
state.wsize = 0; |
| 197 |
state.whave = 0; |
| 198 |
state.wnext = 0; |
| 199 |
return inflateResetKeep(strm); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
export function inflateReset2(strm, windowBits) { |
| 204 |
var wrap; |
| 205 |
var state; |
| 206 |
|
| 207 |
/* get the state */ |
| 208 |
if (!strm || !strm.state) { |
| 209 |
return Z_STREAM_ERROR; |
| 210 |
} |
| 211 |
state = strm.state; |
| 212 |
|
| 213 |
/* extract wrap request from windowBits parameter */ |
| 214 |
if (windowBits < 0) { |
| 215 |
wrap = 0; |
| 216 |
windowBits = -windowBits; |
| 217 |
} else { |
| 218 |
wrap = (windowBits >> 4) + 1; |
| 219 |
if (windowBits < 48) { |
| 220 |
windowBits &= 15; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/* set number of window bits, free window if different */ |
| 225 |
if (windowBits && (windowBits < 8 || windowBits > 15)) { |
| 226 |
return Z_STREAM_ERROR; |
| 227 |
} |
| 228 |
if (state.window !== null && state.wbits !== windowBits) { |
| 229 |
state.window = null; |
| 230 |
} |
| 231 |
|
| 232 |
/* update state and reset the rest of it */ |
| 233 |
state.wrap = wrap; |
| 234 |
state.wbits = windowBits; |
| 235 |
return inflateReset(strm); |
| 236 |
} |
| 237 |
|
| 238 |
export function inflateInit2(strm, windowBits) { |
| 239 |
var ret; |
| 240 |
var state; |
| 241 |
|
| 242 |
if (!strm) { |
| 243 |
return Z_STREAM_ERROR; |
| 244 |
} |
| 245 |
//strm.msg = Z_NULL; /* in case we return an error */ |
| 246 |
|
| 247 |
state = new InflateState(); |
| 248 |
|
| 249 |
//if (state === Z_NULL) return Z_MEM_ERROR; |
| 250 |
//Tracev((stderr, "inflate: allocated\n")); |
| 251 |
strm.state = state; |
| 252 |
state.window = null /*Z_NULL*/ ; |
| 253 |
ret = inflateReset2(strm, windowBits); |
| 254 |
if (ret !== Z_OK) { |
| 255 |
strm.state = null /*Z_NULL*/ ; |
| 256 |
} |
| 257 |
return ret; |
| 258 |
} |
| 259 |
|
| 260 |
export function inflateInit(strm) { |
| 261 |
return inflateInit2(strm, DEF_WBITS); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
/* |
| 266 |
Return state with length and distance decoding tables and index sizes set to |
| 267 |
fixed code decoding. Normally this returns fixed tables from inffixed.h. |
| 268 |
If BUILDFIXED is defined, then instead this routine builds the tables the |
| 269 |
first time it's called, and returns those tables the first time and |
| 270 |
thereafter. This reduces the size of the code by about 2K bytes, in |
| 271 |
exchange for a little execution time. However, BUILDFIXED should not be |
| 272 |
used for threaded applications, since the rewriting of the tables and virgin |
| 273 |
may not be thread-safe. |
| 274 |
*/ |
| 275 |
var virgin = true; |
| 276 |
|
| 277 |
var lenfix, distfix; // We have no pointers in JS, so keep tables separate |
| 278 |
|
| 279 |
function fixedtables(state) { |
| 280 |
/* build fixed huffman tables if first call (may not be thread safe) */ |
| 281 |
if (virgin) { |
| 282 |
var sym; |
| 283 |
|
| 284 |
lenfix = new Buf32(512); |
| 285 |
distfix = new Buf32(32); |
| 286 |
|
| 287 |
/* literal/length table */ |
| 288 |
sym = 0; |
| 289 |
while (sym < 144) { |
| 290 |
state.lens[sym++] = 8; |
| 291 |
} |
| 292 |
while (sym < 256) { |
| 293 |
state.lens[sym++] = 9; |
| 294 |
} |
| 295 |
while (sym < 280) { |
| 296 |
state.lens[sym++] = 7; |
| 297 |
} |
| 298 |
while (sym < 288) { |
| 299 |
state.lens[sym++] = 8; |
| 300 |
} |
| 301 |
|
| 302 |
inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { |
| 303 |
bits: 9 |
| 304 |
}); |
| 305 |
|
| 306 |
/* distance table */ |
| 307 |
sym = 0; |
| 308 |
while (sym < 32) { |
| 309 |
state.lens[sym++] = 5; |
| 310 |
} |
| 311 |
|
| 312 |
inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { |
| 313 |
bits: 5 |
| 314 |
}); |
| 315 |
|
| 316 |
/* do this just once */ |
| 317 |
virgin = false; |
| 318 |
} |
| 319 |
|
| 320 |
state.lencode = lenfix; |
| 321 |
state.lenbits = 9; |
| 322 |
state.distcode = distfix; |
| 323 |
state.distbits = 5; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/* |
| 328 |
Update the window with the last wsize (normally 32K) bytes written before |
| 329 |
returning. If window does not exist yet, create it. This is only called |
| 330 |
when a window is already in use, or when output has been written during this |
| 331 |
inflate call, but the end of the deflate stream has not been reached yet. |
| 332 |
It is also called to create a window for dictionary data when a dictionary |
| 333 |
is loaded. |
| 334 |
|
| 335 |
Providing output buffers larger than 32K to inflate() should provide a speed |
| 336 |
advantage, since only the last 32K of output is copied to the sliding window |
| 337 |
upon return from inflate(), and since all distances after the first 32K of |
| 338 |
output will fall in the output data, making match copies simpler and faster. |
| 339 |
The advantage may be dependent on the size of the processor's data caches. |
| 340 |
*/ |
| 341 |
function updatewindow(strm, src, end, copy) { |
| 342 |
var dist; |
| 343 |
var state = strm.state; |
| 344 |
|
| 345 |
/* if it hasn't been done already, allocate space for the window */ |
| 346 |
if (state.window === null) { |
| 347 |
state.wsize = 1 << state.wbits; |
| 348 |
state.wnext = 0; |
| 349 |
state.whave = 0; |
| 350 |
|
| 351 |
state.window = new Buf8(state.wsize); |
| 352 |
} |
| 353 |
|
| 354 |
/* copy state->wsize or less output bytes into the circular window */ |
| 355 |
if (copy >= state.wsize) { |
| 356 |
arraySet(state.window, src, end - state.wsize, state.wsize, 0); |
| 357 |
state.wnext = 0; |
| 358 |
state.whave = state.wsize; |
| 359 |
} else { |
| 360 |
dist = state.wsize - state.wnext; |
| 361 |
if (dist > copy) { |
| 362 |
dist = copy; |
| 363 |
} |
| 364 |
//zmemcpy(state->window + state->wnext, end - copy, dist); |
| 365 |
arraySet(state.window, src, end - copy, dist, state.wnext); |
| 366 |
copy -= dist; |
| 367 |
if (copy) { |
| 368 |
//zmemcpy(state->window, end - copy, copy); |
| 369 |
arraySet(state.window, src, end - copy, copy, 0); |
| 370 |
state.wnext = copy; |
| 371 |
state.whave = state.wsize; |
| 372 |
} else { |
| 373 |
state.wnext += dist; |
| 374 |
if (state.wnext === state.wsize) { |
| 375 |
state.wnext = 0; |
| 376 |
} |
| 377 |
if (state.whave < state.wsize) { |
| 378 |
state.whave += dist; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
export function inflate(strm, flush) { |
| 386 |
var state; |
| 387 |
var input, output; // input/output buffers |
| 388 |
var next; /* next input INDEX */ |
| 389 |
var put; /* next output INDEX */ |
| 390 |
var have, left; /* available input and output */ |
| 391 |
var hold; /* bit buffer */ |
| 392 |
var bits; /* bits in bit buffer */ |
| 393 |
var _in, _out; /* save starting available input and output */ |
| 394 |
var copy; /* number of stored or match bytes to copy */ |
| 395 |
var from; /* where to copy match bytes from */ |
| 396 |
var from_source; |
| 397 |
var here = 0; /* current decoding table entry */ |
| 398 |
var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) |
| 399 |
//var last; /* parent table entry */ |
| 400 |
var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) |
| 401 |
var len; /* length to copy for repeats, bits to drop */ |
| 402 |
var ret; /* return code */ |
| 403 |
var hbuf = new Buf8(4); /* buffer for gzip header crc calculation */ |
| 404 |
var opts; |
| 405 |
|
| 406 |
var n; // temporary var for NEED_BITS |
| 407 |
|
| 408 |
var order = /* permutation of code lengths */ [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; |
| 409 |
|
| 410 |
|
| 411 |
if (!strm || !strm.state || !strm.output || |
| 412 |
(!strm.input && strm.avail_in !== 0)) { |
| 413 |
return Z_STREAM_ERROR; |
| 414 |
} |
| 415 |
|
| 416 |
state = strm.state; |
| 417 |
if (state.mode === TYPE) { |
| 418 |
state.mode = TYPEDO; |
| 419 |
} /* skip check */ |
| 420 |
|
| 421 |
|
| 422 |
//--- LOAD() --- |
| 423 |
put = strm.next_out; |
| 424 |
output = strm.output; |
| 425 |
left = strm.avail_out; |
| 426 |
next = strm.next_in; |
| 427 |
input = strm.input; |
| 428 |
have = strm.avail_in; |
| 429 |
hold = state.hold; |
| 430 |
bits = state.bits; |
| 431 |
//--- |
| 432 |
|
| 433 |
_in = have; |
| 434 |
_out = left; |
| 435 |
ret = Z_OK; |
| 436 |
|
| 437 |
inf_leave: // goto emulation |
| 438 |
for (;;) { |
| 439 |
switch (state.mode) { |
| 440 |
case HEAD: |
| 441 |
if (state.wrap === 0) { |
| 442 |
state.mode = TYPEDO; |
| 443 |
break; |
| 444 |
} |
| 445 |
//=== NEEDBITS(16); |
| 446 |
while (bits < 16) { |
| 447 |
if (have === 0) { |
| 448 |
break inf_leave; |
| 449 |
} |
| 450 |
have--; |
| 451 |
hold += input[next++] << bits; |
| 452 |
bits += 8; |
| 453 |
} |
| 454 |
//===// |
| 455 |
if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */ |
| 456 |
state.check = 0 /*crc32(0L, Z_NULL, 0)*/ ; |
| 457 |
//=== CRC2(state.check, hold); |
| 458 |
hbuf[0] = hold & 0xff; |
| 459 |
hbuf[1] = (hold >>> 8) & 0xff; |
| 460 |
state.check = crc32(state.check, hbuf, 2, 0); |
| 461 |
//===// |
| 462 |
|
| 463 |
//=== INITBITS(); |
| 464 |
hold = 0; |
| 465 |
bits = 0; |
| 466 |
//===// |
| 467 |
state.mode = FLAGS; |
| 468 |
break; |
| 469 |
} |
| 470 |
state.flags = 0; /* expect zlib header */ |
| 471 |
if (state.head) { |
| 472 |
state.head.done = false; |
| 473 |
} |
| 474 |
if (!(state.wrap & 1) || /* check if zlib header allowed */ |
| 475 |
(((hold & 0xff) /*BITS(8)*/ << 8) + (hold >> 8)) % 31) { |
| 476 |
strm.msg = 'incorrect header check'; |
| 477 |
state.mode = BAD; |
| 478 |
break; |
| 479 |
} |
| 480 |
if ((hold & 0x0f) /*BITS(4)*/ !== Z_DEFLATED) { |
| 481 |
strm.msg = 'unknown compression method'; |
| 482 |
state.mode = BAD; |
| 483 |
break; |
| 484 |
} |
| 485 |
//--- DROPBITS(4) ---// |
| 486 |
hold >>>= 4; |
| 487 |
bits -= 4; |
| 488 |
//---// |
| 489 |
len = (hold & 0x0f) /*BITS(4)*/ + 8; |
| 490 |
if (state.wbits === 0) { |
| 491 |
state.wbits = len; |
| 492 |
} else if (len > state.wbits) { |
| 493 |
strm.msg = 'invalid window size'; |
| 494 |
state.mode = BAD; |
| 495 |
break; |
| 496 |
} |
| 497 |
state.dmax = 1 << len; |
| 498 |
//Tracev((stderr, "inflate: zlib header ok\n")); |
| 499 |
strm.adler = state.check = 1 /*adler32(0L, Z_NULL, 0)*/ ; |
| 500 |
state.mode = hold & 0x200 ? DICTID : TYPE; |
| 501 |
//=== INITBITS(); |
| 502 |
hold = 0; |
| 503 |
bits = 0; |
| 504 |
//===// |
| 505 |
break; |
| 506 |
case FLAGS: |
| 507 |
//=== NEEDBITS(16); */ |
| 508 |
while (bits < 16) { |
| 509 |
if (have === 0) { |
| 510 |
break inf_leave; |
| 511 |
} |
| 512 |
have--; |
| 513 |
hold += input[next++] << bits; |
| 514 |
bits += 8; |
| 515 |
} |
| 516 |
//===// |
| 517 |
state.flags = hold; |
| 518 |
if ((state.flags & 0xff) !== Z_DEFLATED) { |
| 519 |
strm.msg = 'unknown compression method'; |
| 520 |
state.mode = BAD; |
| 521 |
break; |
| 522 |
} |
| 523 |
if (state.flags & 0xe000) { |
| 524 |
strm.msg = 'unknown header flags set'; |
| 525 |
state.mode = BAD; |
| 526 |
break; |
| 527 |
} |
| 528 |
if (state.head) { |
| 529 |
state.head.text = ((hold >> 8) & 1); |
| 530 |
} |
| 531 |
if (state.flags & 0x0200) { |
| 532 |
//=== CRC2(state.check, hold); |
| 533 |
hbuf[0] = hold & 0xff; |
| 534 |
hbuf[1] = (hold >>> 8) & 0xff; |
| 535 |
state.check = crc32(state.check, hbuf, 2, 0); |
| 536 |
//===// |
| 537 |
} |
| 538 |
//=== INITBITS(); |
| 539 |
hold = 0; |
| 540 |
bits = 0; |
| 541 |
//===// |
| 542 |
state.mode = TIME; |
| 543 |
/* falls through */ |
| 544 |
case TIME: |
| 545 |
//=== NEEDBITS(32); */ |
| 546 |
while (bits < 32) { |
| 547 |
if (have === 0) { |
| 548 |
break inf_leave; |
| 549 |
} |
| 550 |
have--; |
| 551 |
hold += input[next++] << bits; |
| 552 |
bits += 8; |
| 553 |
} |
| 554 |
//===// |
| 555 |
if (state.head) { |
| 556 |
state.head.time = hold; |
| 557 |
} |
| 558 |
if (state.flags & 0x0200) { |
| 559 |
//=== CRC4(state.check, hold) |
| 560 |
hbuf[0] = hold & 0xff; |
| 561 |
hbuf[1] = (hold >>> 8) & 0xff; |
| 562 |
hbuf[2] = (hold >>> 16) & 0xff; |
| 563 |
hbuf[3] = (hold >>> 24) & 0xff; |
| 564 |
state.check = crc32(state.check, hbuf, 4, 0); |
| 565 |
//=== |
| 566 |
} |
| 567 |
//=== INITBITS(); |
| 568 |
hold = 0; |
| 569 |
bits = 0; |
| 570 |
//===// |
| 571 |
state.mode = OS; |
| 572 |
/* falls through */ |
| 573 |
case OS: |
| 574 |
//=== NEEDBITS(16); */ |
| 575 |
while (bits < 16) { |
| 576 |
if (have === 0) { |
| 577 |
break inf_leave; |
| 578 |
} |
| 579 |
have--; |
| 580 |
hold += input[next++] << bits; |
| 581 |
bits += 8; |
| 582 |
} |
| 583 |
//===// |
| 584 |
if (state.head) { |
| 585 |
state.head.xflags = (hold & 0xff); |
| 586 |
state.head.os = (hold >> 8); |
| 587 |
} |
| 588 |
if (state.flags & 0x0200) { |
| 589 |
//=== CRC2(state.check, hold); |
| 590 |
hbuf[0] = hold & 0xff; |
| 591 |
hbuf[1] = (hold >>> 8) & 0xff; |
| 592 |
state.check = crc32(state.check, hbuf, 2, 0); |
| 593 |
//===// |
| 594 |
} |
| 595 |
//=== INITBITS(); |
| 596 |
hold = 0; |
| 597 |
bits = 0; |
| 598 |
//===// |
| 599 |
state.mode = EXLEN; |
| 600 |
/* falls through */ |
| 601 |
case EXLEN: |
| 602 |
if (state.flags & 0x0400) { |
| 603 |
//=== NEEDBITS(16); */ |
| 604 |
while (bits < 16) { |
| 605 |
if (have === 0) { |
| 606 |
break inf_leave; |
| 607 |
} |
| 608 |
have--; |
| 609 |
hold += input[next++] << bits; |
| 610 |
bits += 8; |
| 611 |
} |
| 612 |
//===// |
| 613 |
state.length = hold; |
| 614 |
if (state.head) { |
| 615 |
state.head.extra_len = hold; |
| 616 |
} |
| 617 |
if (state.flags & 0x0200) { |
| 618 |
//=== CRC2(state.check, hold); |
| 619 |
hbuf[0] = hold & 0xff; |
| 620 |
hbuf[1] = (hold >>> 8) & 0xff; |
| 621 |
state.check = crc32(state.check, hbuf, 2, 0); |
| 622 |
//===// |
| 623 |
} |
| 624 |
//=== INITBITS(); |
| 625 |
hold = 0; |
| 626 |
bits = 0; |
| 627 |
//===// |
| 628 |
} else if (state.head) { |
| 629 |
state.head.extra = null /*Z_NULL*/ ; |
| 630 |
} |
| 631 |
state.mode = EXTRA; |
| 632 |
/* falls through */ |
| 633 |
case EXTRA: |
| 634 |
if (state.flags & 0x0400) { |
| 635 |
copy = state.length; |
| 636 |
if (copy > have) { |
| 637 |
copy = have; |
| 638 |
} |
| 639 |
if (copy) { |
| 640 |
if (state.head) { |
| 641 |
len = state.head.extra_len - state.length; |
| 642 |
if (!state.head.extra) { |
| 643 |
// Use untyped array for more conveniend processing later |
| 644 |
state.head.extra = new Array(state.head.extra_len); |
| 645 |
} |
| 646 |
arraySet( |
| 647 |
state.head.extra, |
| 648 |
input, |
| 649 |
next, |
| 650 |
// extra field is limited to 65536 bytes |
| 651 |
// - no need for additional size check |
| 652 |
copy, |
| 653 |
/*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ |
| 654 |
len |
| 655 |
); |
| 656 |
//zmemcpy(state.head.extra + len, next, |
| 657 |
// len + copy > state.head.extra_max ? |
| 658 |
// state.head.extra_max - len : copy); |
| 659 |
} |
| 660 |
if (state.flags & 0x0200) { |
| 661 |
state.check = crc32(state.check, input, copy, next); |
| 662 |
} |
| 663 |
have -= copy; |
| 664 |
next += copy; |
| 665 |
state.length -= copy; |
| 666 |
} |
| 667 |
if (state.length) { |
| 668 |
break inf_leave; |
| 669 |
} |
| 670 |
} |
| 671 |
state.length = 0; |
| 672 |
state.mode = NAME; |
| 673 |
/* falls through */ |
| 674 |
case NAME: |
| 675 |
if (state.flags & 0x0800) { |
| 676 |
if (have === 0) { |
| 677 |
break inf_leave; |
| 678 |
} |
| 679 |
copy = 0; |
| 680 |
do { |
| 681 |
// TODO: 2 or 1 bytes? |
| 682 |
len = input[next + copy++]; |
| 683 |
/* use constant limit because in js we should not preallocate memory */ |
| 684 |
if (state.head && len && |
| 685 |
(state.length < 65536 /*state.head.name_max*/ )) { |
| 686 |
state.head.name += String.fromCharCode(len); |
| 687 |
} |
| 688 |
} while (len && copy < have); |
| 689 |
|
| 690 |
if (state.flags & 0x0200) { |
| 691 |
state.check = crc32(state.check, input, copy, next); |
| 692 |
} |
| 693 |
have -= copy; |
| 694 |
next += copy; |
| 695 |
if (len) { |
| 696 |
break inf_leave; |
| 697 |
} |
| 698 |
} else if (state.head) { |
| 699 |
state.head.name = null; |
| 700 |
} |
| 701 |
state.length = 0; |
| 702 |
state.mode = COMMENT; |
| 703 |
/* falls through */ |
| 704 |
case COMMENT: |
| 705 |
if (state.flags & 0x1000) { |
| 706 |
if (have === 0) { |
| 707 |
break inf_leave; |
| 708 |
} |
| 709 |
copy = 0; |
| 710 |
do { |
| 711 |
len = input[next + copy++]; |
| 712 |
/* use constant limit because in js we should not preallocate memory */ |
| 713 |
if (state.head && len && |
| 714 |
(state.length < 65536 /*state.head.comm_max*/ )) { |
| 715 |
state.head.comment += String.fromCharCode(len); |
| 716 |
} |
| 717 |
} while (len && copy < have); |
| 718 |
if (state.flags & 0x0200) { |
| 719 |
state.check = crc32(state.check, input, copy, next); |
| 720 |
} |
| 721 |
have -= copy; |
| 722 |
next += copy; |
| 723 |
if (len) { |
| 724 |
break inf_leave; |
| 725 |
} |
| 726 |
} else if (state.head) { |
| 727 |
state.head.comment = null; |
| 728 |
} |
| 729 |
state.mode = HCRC; |
| 730 |
/* falls through */ |
| 731 |
case HCRC: |
| 732 |
if (state.flags & 0x0200) { |
| 733 |
//=== NEEDBITS(16); */ |
| 734 |
while (bits < 16) { |
| 735 |
if (have === 0) { |
| 736 |
break inf_leave; |
| 737 |
} |
| 738 |
have--; |
| 739 |
hold += input[next++] << bits; |
| 740 |
bits += 8; |
| 741 |
} |
| 742 |
//===// |
| 743 |
if (hold !== (state.check & 0xffff)) { |
| 744 |
strm.msg = 'header crc mismatch'; |
| 745 |
state.mode = BAD; |
| 746 |
break; |
| 747 |
} |
| 748 |
//=== INITBITS(); |
| 749 |
hold = 0; |
| 750 |
bits = 0; |
| 751 |
//===// |
| 752 |
} |
| 753 |
if (state.head) { |
| 754 |
state.head.hcrc = ((state.flags >> 9) & 1); |
| 755 |
state.head.done = true; |
| 756 |
} |
| 757 |
strm.adler = state.check = 0; |
| 758 |
state.mode = TYPE; |
| 759 |
break; |
| 760 |
case DICTID: |
| 761 |
//=== NEEDBITS(32); */ |
| 762 |
while (bits < 32) { |
| 763 |
if (have === 0) { |
| 764 |
break inf_leave; |
| 765 |
} |
| 766 |
have--; |
| 767 |
hold += input[next++] << bits; |
| 768 |
bits += 8; |
| 769 |
} |
| 770 |
//===// |
| 771 |
strm.adler = state.check = zswap32(hold); |
| 772 |
//=== INITBITS(); |
| 773 |
hold = 0; |
| 774 |
bits = 0; |
| 775 |
//===// |
| 776 |
state.mode = DICT; |
| 777 |
/* falls through */ |
| 778 |
case DICT: |
| 779 |
if (state.havedict === 0) { |
| 780 |
//--- RESTORE() --- |
| 781 |
strm.next_out = put; |
| 782 |
strm.avail_out = left; |
| 783 |
strm.next_in = next; |
| 784 |
strm.avail_in = have; |
| 785 |
state.hold = hold; |
| 786 |
state.bits = bits; |
| 787 |
//--- |
| 788 |
return Z_NEED_DICT; |
| 789 |
} |
| 790 |
strm.adler = state.check = 1 /*adler32(0L, Z_NULL, 0)*/ ; |
| 791 |
state.mode = TYPE; |
| 792 |
/* falls through */ |
| 793 |
case TYPE: |
| 794 |
if (flush === Z_BLOCK || flush === Z_TREES) { |
| 795 |
break inf_leave; |
| 796 |
} |
| 797 |
/* falls through */ |
| 798 |
case TYPEDO: |
| 799 |
if (state.last) { |
| 800 |
//--- BYTEBITS() ---// |
| 801 |
hold >>>= bits & 7; |
| 802 |
bits -= bits & 7; |
| 803 |
//---// |
| 804 |
state.mode = CHECK; |
| 805 |
break; |
| 806 |
} |
| 807 |
//=== NEEDBITS(3); */ |
| 808 |
while (bits < 3) { |
| 809 |
if (have === 0) { |
| 810 |
break inf_leave; |
| 811 |
} |
| 812 |
have--; |
| 813 |
hold += input[next++] << bits; |
| 814 |
bits += 8; |
| 815 |
} |
| 816 |
//===// |
| 817 |
state.last = (hold & 0x01) /*BITS(1)*/ ; |
| 818 |
//--- DROPBITS(1) ---// |
| 819 |
hold >>>= 1; |
| 820 |
bits -= 1; |
| 821 |
//---// |
| 822 |
|
| 823 |
switch ((hold & 0x03) /*BITS(2)*/ ) { |
| 824 |
case 0: |
| 825 |
/* stored block */ |
| 826 |
//Tracev((stderr, "inflate: stored block%s\n", |
| 827 |
// state.last ? " (last)" : "")); |
| 828 |
state.mode = STORED; |
| 829 |
break; |
| 830 |
case 1: |
| 831 |
/* fixed block */ |
| 832 |
fixedtables(state); |
| 833 |
//Tracev((stderr, "inflate: fixed codes block%s\n", |
| 834 |
// state.last ? " (last)" : "")); |
| 835 |
state.mode = LEN_; /* decode codes */ |
| 836 |
if (flush === Z_TREES) { |
| 837 |
//--- DROPBITS(2) ---// |
| 838 |
hold >>>= 2; |
| 839 |
bits -= 2; |
| 840 |
//---// |
| 841 |
break inf_leave; |
| 842 |
} |
| 843 |
break; |
| 844 |
case 2: |
| 845 |
/* dynamic block */ |
| 846 |
//Tracev((stderr, "inflate: dynamic codes block%s\n", |
| 847 |
// state.last ? " (last)" : "")); |
| 848 |
state.mode = TABLE; |
| 849 |
break; |
| 850 |
case 3: |
| 851 |
strm.msg = 'invalid block type'; |
| 852 |
state.mode = BAD; |
| 853 |
} |
| 854 |
//--- DROPBITS(2) ---// |
| 855 |
hold >>>= 2; |
| 856 |
bits -= 2; |
| 857 |
//---// |
| 858 |
break; |
| 859 |
case STORED: |
| 860 |
//--- BYTEBITS() ---// /* go to byte boundary */ |
| 861 |
hold >>>= bits & 7; |
| 862 |
bits -= bits & 7; |
| 863 |
//---// |
| 864 |
//=== NEEDBITS(32); */ |
| 865 |
while (bits < 32) { |
| 866 |
if (have === 0) { |
| 867 |
break inf_leave; |
| 868 |
} |
| 869 |
have--; |
| 870 |
hold += input[next++] << bits; |
| 871 |
bits += 8; |
| 872 |
} |
| 873 |
//===// |
| 874 |
if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) { |
| 875 |
strm.msg = 'invalid stored block lengths'; |
| 876 |
state.mode = BAD; |
| 877 |
break; |
| 878 |
} |
| 879 |
state.length = hold & 0xffff; |
| 880 |
//Tracev((stderr, "inflate: stored length %u\n", |
| 881 |
// state.length)); |
| 882 |
//=== INITBITS(); |
| 883 |
hold = 0; |
| 884 |
bits = 0; |
| 885 |
//===// |
| 886 |
state.mode = COPY_; |
| 887 |
if (flush === Z_TREES) { |
| 888 |
break inf_leave; |
| 889 |
} |
| 890 |
/* falls through */ |
| 891 |
case COPY_: |
| 892 |
state.mode = COPY; |
| 893 |
/* falls through */ |
| 894 |
case COPY: |
| 895 |
copy = state.length; |
| 896 |
if (copy) { |
| 897 |
if (copy > have) { |
| 898 |
copy = have; |
| 899 |
} |
| 900 |
if (copy > left) { |
| 901 |
copy = left; |
| 902 |
} |
| 903 |
if (copy === 0) { |
| 904 |
break inf_leave; |
| 905 |
} |
| 906 |
//--- zmemcpy(put, next, copy); --- |
| 907 |
arraySet(output, input, next, copy, put); |
| 908 |
//---// |
| 909 |
have -= copy; |
| 910 |
next += copy; |
| 911 |
left -= copy; |
| 912 |
put += copy; |
| 913 |
state.length -= copy; |
| 914 |
break; |
| 915 |
} |
| 916 |
//Tracev((stderr, "inflate: stored end\n")); |
| 917 |
state.mode = TYPE; |
| 918 |
break; |
| 919 |
case TABLE: |
| 920 |
//=== NEEDBITS(14); */ |
| 921 |
while (bits < 14) { |
| 922 |
if (have === 0) { |
| 923 |
break inf_leave; |
| 924 |
} |
| 925 |
have--; |
| 926 |
hold += input[next++] << bits; |
| 927 |
bits += 8; |
| 928 |
} |
| 929 |
//===// |
| 930 |
state.nlen = (hold & 0x1f) /*BITS(5)*/ + 257; |
| 931 |
//--- DROPBITS(5) ---// |
| 932 |
hold >>>= 5; |
| 933 |
bits -= 5; |
| 934 |
//---// |
| 935 |
state.ndist = (hold & 0x1f) /*BITS(5)*/ + 1; |
| 936 |
//--- DROPBITS(5) ---// |
| 937 |
hold >>>= 5; |
| 938 |
bits -= 5; |
| 939 |
//---// |
| 940 |
state.ncode = (hold & 0x0f) /*BITS(4)*/ + 4; |
| 941 |
//--- DROPBITS(4) ---// |
| 942 |
hold >>>= 4; |
| 943 |
bits -= 4; |
| 944 |
//---// |
| 945 |
//#ifndef PKZIP_BUG_WORKAROUND |
| 946 |
if (state.nlen > 286 || state.ndist > 30) { |
| 947 |
strm.msg = 'too many length or distance symbols'; |
| 948 |
state.mode = BAD; |
| 949 |
break; |
| 950 |
} |
| 951 |
//#endif |
| 952 |
//Tracev((stderr, "inflate: table sizes ok\n")); |
| 953 |
state.have = 0; |
| 954 |
state.mode = LENLENS; |
| 955 |
/* falls through */ |
| 956 |
case LENLENS: |
| 957 |
while (state.have < state.ncode) { |
| 958 |
//=== NEEDBITS(3); |
| 959 |
while (bits < 3) { |
| 960 |
if (have === 0) { |
| 961 |
break inf_leave; |
| 962 |
} |
| 963 |
have--; |
| 964 |
hold += input[next++] << bits; |
| 965 |
bits += 8; |
| 966 |
} |
| 967 |
//===// |
| 968 |
state.lens[order[state.have++]] = (hold & 0x07); //BITS(3); |
| 969 |
//--- DROPBITS(3) ---// |
| 970 |
hold >>>= 3; |
| 971 |
bits -= 3; |
| 972 |
//---// |
| 973 |
} |
| 974 |
while (state.have < 19) { |
| 975 |
state.lens[order[state.have++]] = 0; |
| 976 |
} |
| 977 |
// We have separate tables & no pointers. 2 commented lines below not needed. |
| 978 |
//state.next = state.codes; |
| 979 |
//state.lencode = state.next; |
| 980 |
// Switch to use dynamic table |
| 981 |
state.lencode = state.lendyn; |
| 982 |
state.lenbits = 7; |
| 983 |
|
| 984 |
opts = { |
| 985 |
bits: state.lenbits |
| 986 |
}; |
| 987 |
ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); |
| 988 |
state.lenbits = opts.bits; |
| 989 |
|
| 990 |
if (ret) { |
| 991 |
strm.msg = 'invalid code lengths set'; |
| 992 |
state.mode = BAD; |
| 993 |
break; |
| 994 |
} |
| 995 |
//Tracev((stderr, "inflate: code lengths ok\n")); |
| 996 |
state.have = 0; |
| 997 |
state.mode = CODELENS; |
| 998 |
/* falls through */ |
| 999 |
case CODELENS: |
| 1000 |
while (state.have < state.nlen + state.ndist) { |
| 1001 |
for (;;) { |
| 1002 |
here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ |
| 1003 |
here_bits = here >>> 24; |
| 1004 |
here_op = (here >>> 16) & 0xff; |
| 1005 |
here_val = here & 0xffff; |
| 1006 |
|
| 1007 |
if ((here_bits) <= bits) { |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
//--- PULLBYTE() ---// |
| 1011 |
if (have === 0) { |
| 1012 |
break inf_leave; |
| 1013 |
} |
| 1014 |
have--; |
| 1015 |
hold += input[next++] << bits; |
| 1016 |
bits += 8; |
| 1017 |
//---// |
| 1018 |
} |
| 1019 |
if (here_val < 16) { |
| 1020 |
//--- DROPBITS(here.bits) ---// |
| 1021 |
hold >>>= here_bits; |
| 1022 |
bits -= here_bits; |
| 1023 |
//---// |
| 1024 |
state.lens[state.have++] = here_val; |
| 1025 |
} else { |
| 1026 |
if (here_val === 16) { |
| 1027 |
//=== NEEDBITS(here.bits + 2); |
| 1028 |
n = here_bits + 2; |
| 1029 |
while (bits < n) { |
| 1030 |
if (have === 0) { |
| 1031 |
break inf_leave; |
| 1032 |
} |
| 1033 |
have--; |
| 1034 |
hold += input[next++] << bits; |
| 1035 |
bits += 8; |
| 1036 |
} |
| 1037 |
//===// |
| 1038 |
//--- DROPBITS(here.bits) ---// |
| 1039 |
hold >>>= here_bits; |
| 1040 |
bits -= here_bits; |
| 1041 |
//---// |
| 1042 |
if (state.have === 0) { |
| 1043 |
strm.msg = 'invalid bit length repeat'; |
| 1044 |
state.mode = BAD; |
| 1045 |
break; |
| 1046 |
} |
| 1047 |
len = state.lens[state.have - 1]; |
| 1048 |
copy = 3 + (hold & 0x03); //BITS(2); |
| 1049 |
//--- DROPBITS(2) ---// |
| 1050 |
hold >>>= 2; |
| 1051 |
bits -= 2; |
| 1052 |
//---// |
| 1053 |
} else if (here_val === 17) { |
| 1054 |
//=== NEEDBITS(here.bits + 3); |
| 1055 |
n = here_bits + 3; |
| 1056 |
while (bits < n) { |
| 1057 |
if (have === 0) { |
| 1058 |
break inf_leave; |
| 1059 |
} |
| 1060 |
have--; |
| 1061 |
hold += input[next++] << bits; |
| 1062 |
bits += 8; |
| 1063 |
} |
| 1064 |
//===// |
| 1065 |
//--- DROPBITS(here.bits) ---// |
| 1066 |
hold >>>= here_bits; |
| 1067 |
bits -= here_bits; |
| 1068 |
//---// |
| 1069 |
len = 0; |
| 1070 |
copy = 3 + (hold & 0x07); //BITS(3); |
| 1071 |
//--- DROPBITS(3) ---// |
| 1072 |
hold >>>= 3; |
| 1073 |
bits -= 3; |
| 1074 |
//---// |
| 1075 |
} else { |
| 1076 |
//=== NEEDBITS(here.bits + 7); |
| 1077 |
n = here_bits + 7; |
| 1078 |
while (bits < n) { |
| 1079 |
if (have === 0) { |
| 1080 |
break inf_leave; |
| 1081 |
} |
| 1082 |
have--; |
| 1083 |
hold += input[next++] << bits; |
| 1084 |
bits += 8; |
| 1085 |
} |
| 1086 |
//===// |
| 1087 |
//--- DROPBITS(here.bits) ---// |
| 1088 |
hold >>>= here_bits; |
| 1089 |
bits -= here_bits; |
| 1090 |
//---// |
| 1091 |
len = 0; |
| 1092 |
copy = 11 + (hold & 0x7f); //BITS(7); |
| 1093 |
//--- DROPBITS(7) ---// |
| 1094 |
hold >>>= 7; |
| 1095 |
bits -= 7; |
| 1096 |
//---// |
| 1097 |
} |
| 1098 |
if (state.have + copy > state.nlen + state.ndist) { |
| 1099 |
strm.msg = 'invalid bit length repeat'; |
| 1100 |
state.mode = BAD; |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
while (copy--) { |
| 1104 |
state.lens[state.have++] = len; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* handle error breaks in while */ |
| 1110 |
if (state.mode === BAD) { |
| 1111 |
break; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* check for end-of-block code (better have one) */ |
| 1115 |
if (state.lens[256] === 0) { |
| 1116 |
strm.msg = 'invalid code -- missing end-of-block'; |
| 1117 |
state.mode = BAD; |
| 1118 |
break; |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* build code tables -- note: do not change the lenbits or distbits |
| 1122 |
values here (9 and 6) without reading the comments in inftrees.h |
| 1123 |
concerning the ENOUGH constants, which depend on those values */ |
| 1124 |
state.lenbits = 9; |
| 1125 |
|
| 1126 |
opts = { |
| 1127 |
bits: state.lenbits |
| 1128 |
}; |
| 1129 |
ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); |
| 1130 |
// We have separate tables & no pointers. 2 commented lines below not needed. |
| 1131 |
// state.next_index = opts.table_index; |
| 1132 |
state.lenbits = opts.bits; |
| 1133 |
// state.lencode = state.next; |
| 1134 |
|
| 1135 |
if (ret) { |
| 1136 |
strm.msg = 'invalid literal/lengths set'; |
| 1137 |
state.mode = BAD; |
| 1138 |
break; |
| 1139 |
} |
| 1140 |
|
| 1141 |
state.distbits = 6; |
| 1142 |
//state.distcode.copy(state.codes); |
| 1143 |
// Switch to use dynamic table |
| 1144 |
state.distcode = state.distdyn; |
| 1145 |
opts = { |
| 1146 |
bits: state.distbits |
| 1147 |
}; |
| 1148 |
ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); |
| 1149 |
// We have separate tables & no pointers. 2 commented lines below not needed. |
| 1150 |
// state.next_index = opts.table_index; |
| 1151 |
state.distbits = opts.bits; |
| 1152 |
// state.distcode = state.next; |
| 1153 |
|
| 1154 |
if (ret) { |
| 1155 |
strm.msg = 'invalid distances set'; |
| 1156 |
state.mode = BAD; |
| 1157 |
break; |
| 1158 |
} |
| 1159 |
//Tracev((stderr, 'inflate: codes ok\n')); |
| 1160 |
state.mode = LEN_; |
| 1161 |
if (flush === Z_TREES) { |
| 1162 |
break inf_leave; |
| 1163 |
} |
| 1164 |
/* falls through */ |
| 1165 |
case LEN_: |
| 1166 |
state.mode = LEN; |
| 1167 |
/* falls through */ |
| 1168 |
case LEN: |
| 1169 |
if (have >= 6 && left >= 258) { |
| 1170 |
//--- RESTORE() --- |
| 1171 |
strm.next_out = put; |
| 1172 |
strm.avail_out = left; |
| 1173 |
strm.next_in = next; |
| 1174 |
strm.avail_in = have; |
| 1175 |
state.hold = hold; |
| 1176 |
state.bits = bits; |
| 1177 |
//--- |
| 1178 |
inflate_fast(strm, _out); |
| 1179 |
//--- LOAD() --- |
| 1180 |
put = strm.next_out; |
| 1181 |
output = strm.output; |
| 1182 |
left = strm.avail_out; |
| 1183 |
next = strm.next_in; |
| 1184 |
input = strm.input; |
| 1185 |
have = strm.avail_in; |
| 1186 |
hold = state.hold; |
| 1187 |
bits = state.bits; |
| 1188 |
//--- |
| 1189 |
|
| 1190 |
if (state.mode === TYPE) { |
| 1191 |
state.back = -1; |
| 1192 |
} |
| 1193 |
break; |
| 1194 |
} |
| 1195 |
state.back = 0; |
| 1196 |
for (;;) { |
| 1197 |
here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ |
| 1198 |
here_bits = here >>> 24; |
| 1199 |
here_op = (here >>> 16) & 0xff; |
| 1200 |
here_val = here & 0xffff; |
| 1201 |
|
| 1202 |
if (here_bits <= bits) { |
| 1203 |
break; |
| 1204 |
} |
| 1205 |
//--- PULLBYTE() ---// |
| 1206 |
if (have === 0) { |
| 1207 |
break inf_leave; |
| 1208 |
} |
| 1209 |
have--; |
| 1210 |
hold += input[next++] << bits; |
| 1211 |
bits += 8; |
| 1212 |
//---// |
| 1213 |
} |
| 1214 |
if (here_op && (here_op & 0xf0) === 0) { |
| 1215 |
last_bits = here_bits; |
| 1216 |
last_op = here_op; |
| 1217 |
last_val = here_val; |
| 1218 |
for (;;) { |
| 1219 |
here = state.lencode[last_val + |
| 1220 |
((hold & ((1 << (last_bits + last_op)) - 1)) /*BITS(last.bits + last.op)*/ >> last_bits)]; |
| 1221 |
here_bits = here >>> 24; |
| 1222 |
here_op = (here >>> 16) & 0xff; |
| 1223 |
here_val = here & 0xffff; |
| 1224 |
|
| 1225 |
if ((last_bits + here_bits) <= bits) { |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
//--- PULLBYTE() ---// |
| 1229 |
if (have === 0) { |
| 1230 |
break inf_leave; |
| 1231 |
} |
| 1232 |
have--; |
| 1233 |
hold += input[next++] << bits; |
| 1234 |
bits += 8; |
| 1235 |
//---// |
| 1236 |
} |
| 1237 |
//--- DROPBITS(last.bits) ---// |
| 1238 |
hold >>>= last_bits; |
| 1239 |
bits -= last_bits; |
| 1240 |
//---// |
| 1241 |
state.back += last_bits; |
| 1242 |
} |
| 1243 |
//--- DROPBITS(here.bits) ---// |
| 1244 |
hold >>>= here_bits; |
| 1245 |
bits -= here_bits; |
| 1246 |
//---// |
| 1247 |
state.back += here_bits; |
| 1248 |
state.length = here_val; |
| 1249 |
if (here_op === 0) { |
| 1250 |
//Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
| 1251 |
// "inflate: literal '%c'\n" : |
| 1252 |
// "inflate: literal 0x%02x\n", here.val)); |
| 1253 |
state.mode = LIT; |
| 1254 |
break; |
| 1255 |
} |
| 1256 |
if (here_op & 32) { |
| 1257 |
//Tracevv((stderr, "inflate: end of block\n")); |
| 1258 |
state.back = -1; |
| 1259 |
state.mode = TYPE; |
| 1260 |
break; |
| 1261 |
} |
| 1262 |
if (here_op & 64) { |
| 1263 |
strm.msg = 'invalid literal/length code'; |
| 1264 |
state.mode = BAD; |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
state.extra = here_op & 15; |
| 1268 |
state.mode = LENEXT; |
| 1269 |
/* falls through */ |
| 1270 |
case LENEXT: |
| 1271 |
if (state.extra) { |
| 1272 |
//=== NEEDBITS(state.extra); |
| 1273 |
n = state.extra; |
| 1274 |
while (bits < n) { |
| 1275 |
if (have === 0) { |
| 1276 |
break inf_leave; |
| 1277 |
} |
| 1278 |
have--; |
| 1279 |
hold += input[next++] << bits; |
| 1280 |
bits += 8; |
| 1281 |
} |
| 1282 |
//===// |
| 1283 |
state.length += hold & ((1 << state.extra) - 1) /*BITS(state.extra)*/ ; |
| 1284 |
//--- DROPBITS(state.extra) ---// |
| 1285 |
hold >>>= state.extra; |
| 1286 |
bits -= state.extra; |
| 1287 |
//---// |
| 1288 |
state.back += state.extra; |
| 1289 |
} |
| 1290 |
//Tracevv((stderr, "inflate: length %u\n", state.length)); |
| 1291 |
state.was = state.length; |
| 1292 |
state.mode = DIST; |
| 1293 |
/* falls through */ |
| 1294 |
case DIST: |
| 1295 |
for (;;) { |
| 1296 |
here = state.distcode[hold & ((1 << state.distbits) - 1)]; /*BITS(state.distbits)*/ |
| 1297 |
here_bits = here >>> 24; |
| 1298 |
here_op = (here >>> 16) & 0xff; |
| 1299 |
here_val = here & 0xffff; |
| 1300 |
|
| 1301 |
if ((here_bits) <= bits) { |
| 1302 |
break; |
| 1303 |
} |
| 1304 |
//--- PULLBYTE() ---// |
| 1305 |
if (have === 0) { |
| 1306 |
break inf_leave; |
| 1307 |
} |
| 1308 |
have--; |
| 1309 |
hold += input[next++] << bits; |
| 1310 |
bits += 8; |
| 1311 |
//---// |
| 1312 |
} |
| 1313 |
if ((here_op & 0xf0) === 0) { |
| 1314 |
last_bits = here_bits; |
| 1315 |
last_op = here_op; |
| 1316 |
last_val = here_val; |
| 1317 |
for (;;) { |
| 1318 |
here = state.distcode[last_val + |
| 1319 |
((hold & ((1 << (last_bits + last_op)) - 1)) /*BITS(last.bits + last.op)*/ >> last_bits)]; |
| 1320 |
here_bits = here >>> 24; |
| 1321 |
here_op = (here >>> 16) & 0xff; |
| 1322 |
here_val = here & 0xffff; |
| 1323 |
|
| 1324 |
if ((last_bits + here_bits) <= bits) { |
| 1325 |
break; |
| 1326 |
} |
| 1327 |
//--- PULLBYTE() ---// |
| 1328 |
if (have === 0) { |
| 1329 |
break inf_leave; |
| 1330 |
} |
| 1331 |
have--; |
| 1332 |
hold += input[next++] << bits; |
| 1333 |
bits += 8; |
| 1334 |
//---// |
| 1335 |
} |
| 1336 |
//--- DROPBITS(last.bits) ---// |
| 1337 |
hold >>>= last_bits; |
| 1338 |
bits -= last_bits; |
| 1339 |
//---// |
| 1340 |
state.back += last_bits; |
| 1341 |
} |
| 1342 |
//--- DROPBITS(here.bits) ---// |
| 1343 |
hold >>>= here_bits; |
| 1344 |
bits -= here_bits; |
| 1345 |
//---// |
| 1346 |
state.back += here_bits; |
| 1347 |
if (here_op & 64) { |
| 1348 |
strm.msg = 'invalid distance code'; |
| 1349 |
state.mode = BAD; |
| 1350 |
break; |
| 1351 |
} |
| 1352 |
state.offset = here_val; |
| 1353 |
state.extra = (here_op) & 15; |
| 1354 |
state.mode = DISTEXT; |
| 1355 |
/* falls through */ |
| 1356 |
case DISTEXT: |
| 1357 |
if (state.extra) { |
| 1358 |
//=== NEEDBITS(state.extra); |
| 1359 |
n = state.extra; |
| 1360 |
while (bits < n) { |
| 1361 |
if (have === 0) { |
| 1362 |
break inf_leave; |
| 1363 |
} |
| 1364 |
have--; |
| 1365 |
hold += input[next++] << bits; |
| 1366 |
bits += 8; |
| 1367 |
} |
| 1368 |
//===// |
| 1369 |
state.offset += hold & ((1 << state.extra) - 1) /*BITS(state.extra)*/ ; |
| 1370 |
//--- DROPBITS(state.extra) ---// |
| 1371 |
hold >>>= state.extra; |
| 1372 |
bits -= state.extra; |
| 1373 |
//---// |
| 1374 |
state.back += state.extra; |
| 1375 |
} |
| 1376 |
//#ifdef INFLATE_STRICT |
| 1377 |
if (state.offset > state.dmax) { |
| 1378 |
strm.msg = 'invalid distance too far back'; |
| 1379 |
state.mode = BAD; |
| 1380 |
break; |
| 1381 |
} |
| 1382 |
//#endif |
| 1383 |
//Tracevv((stderr, "inflate: distance %u\n", state.offset)); |
| 1384 |
state.mode = MATCH; |
| 1385 |
/* falls through */ |
| 1386 |
case MATCH: |
| 1387 |
if (left === 0) { |
| 1388 |
break inf_leave; |
| 1389 |
} |
| 1390 |
copy = _out - left; |
| 1391 |
if (state.offset > copy) { /* copy from window */ |
| 1392 |
copy = state.offset - copy; |
| 1393 |
if (copy > state.whave) { |
| 1394 |
if (state.sane) { |
| 1395 |
strm.msg = 'invalid distance too far back'; |
| 1396 |
state.mode = BAD; |
| 1397 |
break; |
| 1398 |
} |
| 1399 |
// (!) This block is disabled in zlib defailts, |
| 1400 |
// don't enable it for binary compatibility |
| 1401 |
//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
| 1402 |
// Trace((stderr, "inflate.c too far\n")); |
| 1403 |
// copy -= state.whave; |
| 1404 |
// if (copy > state.length) { copy = state.length; } |
| 1405 |
// if (copy > left) { copy = left; } |
| 1406 |
// left -= copy; |
| 1407 |
// state.length -= copy; |
| 1408 |
// do { |
| 1409 |
// output[put++] = 0; |
| 1410 |
// } while (--copy); |
| 1411 |
// if (state.length === 0) { state.mode = LEN; } |
| 1412 |
// break; |
| 1413 |
//#endif |
| 1414 |
} |
| 1415 |
if (copy > state.wnext) { |
| 1416 |
copy -= state.wnext; |
| 1417 |
from = state.wsize - copy; |
| 1418 |
} else { |
| 1419 |
from = state.wnext - copy; |
| 1420 |
} |
| 1421 |
if (copy > state.length) { |
| 1422 |
copy = state.length; |
| 1423 |
} |
| 1424 |
from_source = state.window; |
| 1425 |
} else { /* copy from output */ |
| 1426 |
from_source = output; |
| 1427 |
from = put - state.offset; |
| 1428 |
copy = state.length; |
| 1429 |
} |
| 1430 |
if (copy > left) { |
| 1431 |
copy = left; |
| 1432 |
} |
| 1433 |
left -= copy; |
| 1434 |
state.length -= copy; |
| 1435 |
do { |
| 1436 |
output[put++] = from_source[from++]; |
| 1437 |
} while (--copy); |
| 1438 |
if (state.length === 0) { |
| 1439 |
state.mode = LEN; |
| 1440 |
} |
| 1441 |
break; |
| 1442 |
case LIT: |
| 1443 |
if (left === 0) { |
| 1444 |
break inf_leave; |
| 1445 |
} |
| 1446 |
output[put++] = state.length; |
| 1447 |
left--; |
| 1448 |
state.mode = LEN; |
| 1449 |
break; |
| 1450 |
case CHECK: |
| 1451 |
if (state.wrap) { |
| 1452 |
//=== NEEDBITS(32); |
| 1453 |
while (bits < 32) { |
| 1454 |
if (have === 0) { |
| 1455 |
break inf_leave; |
| 1456 |
} |
| 1457 |
have--; |
| 1458 |
// Use '|' insdead of '+' to make sure that result is signed |
| 1459 |
hold |= input[next++] << bits; |
| 1460 |
bits += 8; |
| 1461 |
} |
| 1462 |
//===// |
| 1463 |
_out -= left; |
| 1464 |
strm.total_out += _out; |
| 1465 |
state.total += _out; |
| 1466 |
if (_out) { |
| 1467 |
strm.adler = state.check = |
| 1468 |
/*UPDATE(state.check, put - _out, _out);*/ |
| 1469 |
(state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out)); |
| 1470 |
|
| 1471 |
} |
| 1472 |
_out = left; |
| 1473 |
// NB: crc32 stored as signed 32-bit int, zswap32 returns signed too |
| 1474 |
if ((state.flags ? hold : zswap32(hold)) !== state.check) { |
| 1475 |
strm.msg = 'incorrect data check'; |
| 1476 |
state.mode = BAD; |
| 1477 |
break; |
| 1478 |
} |
| 1479 |
//=== INITBITS(); |
| 1480 |
hold = 0; |
| 1481 |
bits = 0; |
| 1482 |
//===// |
| 1483 |
//Tracev((stderr, "inflate: check matches trailer\n")); |
| 1484 |
} |
| 1485 |
state.mode = LENGTH; |
| 1486 |
/* falls through */ |
| 1487 |
case LENGTH: |
| 1488 |
if (state.wrap && state.flags) { |
| 1489 |
//=== NEEDBITS(32); |
| 1490 |
while (bits < 32) { |
| 1491 |
if (have === 0) { |
| 1492 |
break inf_leave; |
| 1493 |
} |
| 1494 |
have--; |
| 1495 |
hold += input[next++] << bits; |
| 1496 |
bits += 8; |
| 1497 |
} |
| 1498 |
//===// |
| 1499 |
if (hold !== (state.total & 0xffffffff)) { |
| 1500 |
strm.msg = 'incorrect length check'; |
| 1501 |
state.mode = BAD; |
| 1502 |
break; |
| 1503 |
} |
| 1504 |
//=== INITBITS(); |
| 1505 |
hold = 0; |
| 1506 |
bits = 0; |
| 1507 |
//===// |
| 1508 |
//Tracev((stderr, "inflate: length matches trailer\n")); |
| 1509 |
} |
| 1510 |
state.mode = DONE; |
| 1511 |
/* falls through */ |
| 1512 |
case DONE: |
| 1513 |
ret = Z_STREAM_END; |
| 1514 |
break inf_leave; |
| 1515 |
case BAD: |
| 1516 |
ret = Z_DATA_ERROR; |
| 1517 |
break inf_leave; |
| 1518 |
case MEM: |
| 1519 |
return Z_MEM_ERROR; |
| 1520 |
case SYNC: |
| 1521 |
/* falls through */ |
| 1522 |
default: |
| 1523 |
return Z_STREAM_ERROR; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
// inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" |
| 1528 |
|
| 1529 |
/* |
| 1530 |
Return from inflate(), updating the total counts and the check value. |
| 1531 |
If there was no progress during the inflate() call, return a buffer |
| 1532 |
error. Call updatewindow() to create and/or update the window state. |
| 1533 |
Note: a memory error from inflate() is non-recoverable. |
| 1534 |
*/ |
| 1535 |
|
| 1536 |
//--- RESTORE() --- |
| 1537 |
strm.next_out = put; |
| 1538 |
strm.avail_out = left; |
| 1539 |
strm.next_in = next; |
| 1540 |
strm.avail_in = have; |
| 1541 |
state.hold = hold; |
| 1542 |
state.bits = bits; |
| 1543 |
//--- |
| 1544 |
|
| 1545 |
if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && |
| 1546 |
(state.mode < CHECK || flush !== Z_FINISH))) { |
| 1547 |
if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) { |
| 1548 |
state.mode = MEM; |
| 1549 |
return Z_MEM_ERROR; |
| 1550 |
} |
| 1551 |
} |
| 1552 |
_in -= strm.avail_in; |
| 1553 |
_out -= strm.avail_out; |
| 1554 |
strm.total_in += _in; |
| 1555 |
strm.total_out += _out; |
| 1556 |
state.total += _out; |
| 1557 |
if (state.wrap && _out) { |
| 1558 |
strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/ |
| 1559 |
(state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out)); |
| 1560 |
} |
| 1561 |
strm.data_type = state.bits + (state.last ? 64 : 0) + |
| 1562 |
(state.mode === TYPE ? 128 : 0) + |
| 1563 |
(state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); |
| 1564 |
if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { |
| 1565 |
ret = Z_BUF_ERROR; |
| 1566 |
} |
| 1567 |
return ret; |
| 1568 |
} |
| 1569 |
|
| 1570 |
export function inflateEnd(strm) { |
| 1571 |
|
| 1572 |
if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/ ) { |
| 1573 |
return Z_STREAM_ERROR; |
| 1574 |
} |
| 1575 |
|
| 1576 |
var state = strm.state; |
| 1577 |
if (state.window) { |
| 1578 |
state.window = null; |
| 1579 |
} |
| 1580 |
strm.state = null; |
| 1581 |
return Z_OK; |
| 1582 |
} |
| 1583 |
|
| 1584 |
export function inflateGetHeader(strm, head) { |
| 1585 |
var state; |
| 1586 |
|
| 1587 |
/* check state */ |
| 1588 |
if (!strm || !strm.state) { |
| 1589 |
return Z_STREAM_ERROR; |
| 1590 |
} |
| 1591 |
state = strm.state; |
| 1592 |
if ((state.wrap & 2) === 0) { |
| 1593 |
return Z_STREAM_ERROR; |
| 1594 |
} |
| 1595 |
|
| 1596 |
/* save header structure */ |
| 1597 |
state.head = head; |
| 1598 |
head.done = false; |
| 1599 |
return Z_OK; |
| 1600 |
} |
| 1601 |
|
| 1602 |
export function inflateSetDictionary(strm, dictionary) { |
| 1603 |
var dictLength = dictionary.length; |
| 1604 |
|
| 1605 |
var state; |
| 1606 |
var dictid; |
| 1607 |
var ret; |
| 1608 |
|
| 1609 |
/* check state */ |
| 1610 |
if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */ ) { |
| 1611 |
return Z_STREAM_ERROR; |
| 1612 |
} |
| 1613 |
state = strm.state; |
| 1614 |
|
| 1615 |
if (state.wrap !== 0 && state.mode !== DICT) { |
| 1616 |
return Z_STREAM_ERROR; |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* check for correct dictionary identifier */ |
| 1620 |
if (state.mode === DICT) { |
| 1621 |
dictid = 1; /* adler32(0, null, 0)*/ |
| 1622 |
/* dictid = adler32(dictid, dictionary, dictLength); */ |
| 1623 |
dictid = adler32(dictid, dictionary, dictLength, 0); |
| 1624 |
if (dictid !== state.check) { |
| 1625 |
return Z_DATA_ERROR; |
| 1626 |
} |
| 1627 |
} |
| 1628 |
/* copy dictionary to window using updatewindow(), which will amend the |
| 1629 |
existing dictionary if appropriate */ |
| 1630 |
ret = updatewindow(strm, dictionary, dictLength, dictLength); |
| 1631 |
if (ret) { |
| 1632 |
state.mode = MEM; |
| 1633 |
return Z_MEM_ERROR; |
| 1634 |
} |
| 1635 |
state.havedict = 1; |
| 1636 |
// Tracev((stderr, "inflate: dictionary set\n")); |
| 1637 |
return Z_OK; |
| 1638 |
} |
| 1639 |
|
| 1640 |
export var inflateInfo = 'pako inflate (from Nodeca project)'; |
| 1641 |
|
| 1642 |
/* Not implemented |
| 1643 |
exports.inflateCopy = inflateCopy; |
| 1644 |
exports.inflateGetDictionary = inflateGetDictionary; |
| 1645 |
exports.inflateMark = inflateMark; |
| 1646 |
exports.inflatePrime = inflatePrime; |
| 1647 |
exports.inflateSync = inflateSync; |
| 1648 |
exports.inflateSyncPoint = inflateSyncPoint; |
| 1649 |
exports.inflateUndermine = inflateUndermine; |
| 1650 |
*/ |