| 1 | /* zlib-inflate.js -- JavaScript implementation for the zlib inflate. |
| 2 | Version: 0.2.0 |
| 3 | LastModified: Apr 12 2012 |
| 4 | Copyright (C) 2012 Masanao Izumo <iz@onicos.co.jp> |
| 5 | |
| 6 | This library is one of the JavaScript zlib implementation. |
| 7 | Some API's are modified from the original. |
| 8 | Only inflate API is implemented. |
| 9 | |
| 10 | The original copyright notice (zlib 1.2.6): |
| 11 | |
| 12 | Copyright (C) 1995-2012 Jean-loup Gailly and Mark Adler |
| 13 | |
| 14 | This software is provided 'as-is', without any express or implied |
| 15 | warranty. In no event will the authors be held liable for any damages |
| 16 | arising from the use of this software. |
| 17 | |
| 18 | Permission is granted to anyone to use this software for any purpose, |
| 19 | including commercial applications, and to alter it and redistribute it |
| 20 | freely, subject to the following restrictions: |
| 21 | |
| 22 | 1. The origin of this software must not be misrepresented; you must not |
| 23 | claim that you wrote the original software. If you use this software |
| 24 | in a product, an acknowledgment in the product documentation would be |
| 25 | appreciated but is not required. |
| 26 | 2. Altered source versions must be plainly marked as such, and must not be |
| 27 | misrepresented as being the original software. |
| 28 | 3. This notice may not be removed or altered from any source distribution. |
| 29 | |
| 30 | Jean-loup Gailly Mark Adler |
| 31 | jloup@gzip.org madler@alumni.caltech.edu |
| 32 | |
| 33 | |
| 34 | The data format used by the zlib library is described by RFCs (Request for |
| 35 | Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 |
| 36 | (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | API documentation |
| 41 | ============================================================================== |
| 42 | Usage: z_stream = ZLIB.inflateInit([windowBits]); |
| 43 | |
| 44 | Create the stream object for decompression. |
| 45 | See zlib.h for windowBits information. |
| 46 | |
| 47 | ============================================================================== |
| 48 | Usage: decoded_string = z_stream.inflate(encoded_string [, {OPTIONS...}]); |
| 49 | |
| 50 | OPTIONS: |
| 51 | next_in: decode start offset for encoded_string. |
| 52 | |
| 53 | avail_in: // TODO document. See zlib.h for the information. |
| 54 | |
| 55 | avail_out: // TODO document. See zlib.h for the information. |
| 56 | |
| 57 | flush: // TODO document. See zlib.h for the information. |
| 58 | |
| 59 | Ex: decoded_string = z_stream.inflate(encoded_string); |
| 60 | decoded_string = z_stream.inflate(encoded_string, |
| 61 | {next_in: 0, |
| 62 | avail_in: encoded_string.length, |
| 63 | avail_out: 1024, |
| 64 | flush: ZLIB.Z_NO_FLUSH}); |
| 65 | |
| 66 | See zlib.h for more information. |
| 67 | |
| 68 | ============================================================================== |
| 69 | Usage: z_stream.inflateReset(); |
| 70 | TODO document |
| 71 | |
| 72 | */ |
| 73 | |
| 74 | if( typeof ZLIB === 'undefined' ) { |
| 75 | alert('ZLIB is not defined. SRC zlib.js before zlib-inflate.js') |
| 76 | } |
| 77 | |
| 78 | (function() { |
| 79 | |
| 80 | /* inflate.c -- zlib decompression |
| 81 | * Copyright (C) 1995-2011 Mark Adler |
| 82 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 83 | */ |
| 84 | |
| 85 | var DEF_WBITS = 15; |
| 86 | |
| 87 | // inflate_mode |
| 88 | var HEAD = 0; /* i: waiting for magic header */ |
| 89 | var FLAGS = 1; /* i: waiting for method and flags (gzip) */ |
| 90 | var TIME = 2; /* i: waiting for modification time (gzip) */ |
| 91 | var OS = 3; /* i: waiting for extra flags and operating system (gzip) */ |
| 92 | var EXLEN = 4; /* i: waiting for extra length (gzip) */ |
| 93 | var EXTRA = 5; /* i: waiting for extra bytes (gzip) */ |
| 94 | var NAME = 6; /* i: waiting for end of file name (gzip) */ |
| 95 | var COMMENT = 7; /* i: waiting for end of comment (gzip) */ |
| 96 | var HCRC = 8; /* i: waiting for header crc (gzip) */ |
| 97 | var DICTID = 9; /* i: waiting for dictionary check value */ |
| 98 | var DICT = 10; /* waiting for inflateSetDictionary() call */ |
| 99 | var TYPE = 11; /* i: waiting for type bits, including last-flag bit */ |
| 100 | var TYPEDO = 12; /* i: same, but skip check to exit inflate on new block */ |
| 101 | var STORED = 13; /* i: waiting for stored size (length and complement) */ |
| 102 | var COPY_ = 14; /* i/o: same as COPY below, but only first time in */ |
| 103 | var COPY = 15; /* i/o: waiting for input or output to copy stored block */ |
| 104 | var TABLE = 16; /* i: waiting for dynamic block table lengths */ |
| 105 | var LENLENS = 17; /* i: waiting for code length code lengths */ |
| 106 | var CODELENS = 18; /* i: waiting for length/lit and distance code lengths */ |
| 107 | var LEN_ = 19; /* i: same as LEN below, but only first time in */ |
| 108 | var LEN = 20; /* i: waiting for length/lit/eob code */ |
| 109 | var LENEXT = 21; /* i: waiting for length extra bits */ |
| 110 | var DIST = 22; /* i: waiting for distance code */ |
| 111 | var DISTEXT = 23; /* i: waiting for distance extra bits */ |
| 112 | var MATCH = 24; /* o: waiting for output space to copy string */ |
| 113 | var LIT = 25; /* o: waiting for output space to write literal */ |
| 114 | var CHECK = 26; /* i: waiting for 32-bit check value */ |
| 115 | var LENGTH = 27; /* i: waiting for 32-bit length (gzip) */ |
| 116 | var DONE = 28; /* finished check, done -- remain here until reset */ |
| 117 | var BAD = 29; /* got a data error -- remain here until reset */ |
| 118 | var MEM = 30; /* got an inflate() memory error -- remain here until reset */ |
| 119 | var SYNC = 31; /* looking for synchronization bytes to restart inflate() */ |
| 120 | |
| 121 | /* Maximum size of the dynamic table. The maximum number of code structures is |
| 122 | 1444, which is the sum of 852 for literal/length codes and 592 for distance |
| 123 | codes. These values were found by exhaustive searches using the program |
| 124 | examples/enough.c found in the zlib distribtution. The arguments to that |
| 125 | program are the number of symbols, the initial root table size, and the |
| 126 | maximum bit length of a code. "enough 286 9 15" for literal/length codes |
| 127 | returns returns 852, and "enough 30 6 15" for distance codes returns 592. |
| 128 | The initial root table size (9 or 6) is found in the fifth argument of the |
| 129 | inflate_table() calls in inflate.c and infback.c. If the root table size is |
| 130 | changed, then these maximum sizes would be need to be recalculated and |
| 131 | updated. */ |
| 132 | var ENOUGH_LENS = 852; |
| 133 | var ENOUGH_DISTS = 592; |
| 134 | var ENOUGH = (ENOUGH_LENS + ENOUGH_DISTS); |
| 135 | |
| 136 | /* Type of code to build for inflate_table() */ |
| 137 | var CODES = 0; |
| 138 | var LENS = 1; |
| 139 | var DISTS = 2; |
| 140 | |
| 141 | |
| 142 | |
| 143 | var inflate_table_lbase = [ /* Length codes 257..285 base */ |
| 144 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
| 145 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0]; |
| 146 | var inflate_table_lext = [ /* Length codes 257..285 extra */ |
| 147 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, |
| 148 | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 69]; |
| 149 | var inflate_table_dbase = [ /* Distance codes 0..29 base */ |
| 150 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
| 151 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
| 152 | 8193, 12289, 16385, 24577, 0, 0]; |
| 153 | var inflate_table_dext = [ /* Distance codes 0..29 extra */ |
| 154 | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, |
| 155 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, |
| 156 | 28, 28, 29, 29, 64, 64]; |
| 157 | |
| 158 | /* inftrees.c -- generate Huffman trees for efficient decoding |
| 159 | * Copyright (C) 1995-2012 Mark Adler |
| 160 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 161 | */ |
| 162 | |
| 163 | ZLIB.inflate_copyright = |
| 164 | ' inflate 1.2.6 Copyright 1995-2012 Mark Adler '; |
| 165 | /* |
| 166 | If you use the zlib library in a product, an acknowledgment is welcome |
| 167 | in the documentation of your product. If for some reason you cannot |
| 168 | include such an acknowledgment, I would appreciate that you keep this |
| 169 | copyright string in the executable of your product. |
| 170 | */ |
| 171 | |
| 172 | /* |
| 173 | Build a set of tables to decode the provided canonical Huffman code. |
| 174 | The code lengths are lens[0..codes-1]. The result starts at *table, |
| 175 | whose indices are 0..2^bits-1. work is a writable array of at least |
| 176 | lens shorts, which is used as a work area. type is the type of code |
| 177 | to be generated, CODES, LENS, or DISTS. On return, zero is success, |
| 178 | -1 is an invalid code, and +1 means that ENOUGH isn't enough. table |
| 179 | on return points to the next available entry's address. bits is the |
| 180 | requested root table index bits, and on return it is the actual root |
| 181 | table index bits. It will differ if the request is greater than the |
| 182 | longest code or if it is less than the shortest code. |
| 183 | */ |
| 184 | function inflate_table(state, type) |
| 185 | { |
| 186 | var MAXBITS = 15; |
| 187 | var table = state.next; |
| 188 | var bits = (type == DISTS ? state.distbits : state.lenbits); |
| 189 | var work = state.work; |
| 190 | var lens = state.lens; |
| 191 | var lens_offset = (type == DISTS ? state.nlen : 0); |
| 192 | var state_codes = state.codes; |
| 193 | var codes; |
| 194 | if(type == LENS) |
| 195 | codes = state.nlen; |
| 196 | else if(type == DISTS) |
| 197 | codes = state.ndist; |
| 198 | else // CODES |
| 199 | codes = 19; |
| 200 | |
| 201 | var len; /* a code's length in bits */ |
| 202 | var sym; /* index of code symbols */ |
| 203 | var min, max; /* minimum and maximum code lengths */ |
| 204 | var root; /* number of index bits for root table */ |
| 205 | var curr; /* number of index bits for current table */ |
| 206 | var drop; /* code bits to drop for sub-table */ |
| 207 | var left; /* number of prefix codes available */ |
| 208 | var used; /* code entries in table used */ |
| 209 | var huff; /* Huffman code */ |
| 210 | var incr; /* for incrementing code, index */ |
| 211 | var fill; /* index for replicating entries */ |
| 212 | var low; /* low bits for current root entry */ |
| 213 | var mask; /* mask for low root bits */ |
| 214 | var here; /* table entry for duplication */ |
| 215 | var next; /* next available space in table */ |
| 216 | var base; /* base value table to use */ |
| 217 | var base_offset; |
| 218 | var extra; /* extra bits table to use */ |
| 219 | var extra_offset; |
| 220 | var end; /* use base and extra for symbol > end */ |
| 221 | var count = new Array(MAXBITS+1); /* number of codes of each length */ |
| 222 | var offs = new Array(MAXBITS+1); /* offsets in table for each length */ |
| 223 | |
| 224 | /* |
| 225 | Process a set of code lengths to create a canonical Huffman code. The |
| 226 | code lengths are lens[0..codes-1]. Each length corresponds to the |
| 227 | symbols 0..codes-1. The Huffman code is generated by first sorting the |
| 228 | symbols by length from short to long, and retaining the symbol order |
| 229 | for codes with equal lengths. Then the code starts with all zero bits |
| 230 | for the first code of the shortest length, and the codes are integer |
| 231 | increments for the same length, and zeros are appended as the length |
| 232 | increases. For the deflate format, these bits are stored backwards |
| 233 | from their more natural integer increment ordering, and so when the |
| 234 | decoding tables are built in the large loop below, the integer codes |
| 235 | are incremented backwards. |
| 236 | |
| 237 | This routine assumes, but does not check, that all of the entries in |
| 238 | lens[] are in the range 0..MAXBITS. The caller must assure this. |
| 239 | 1..MAXBITS is interpreted as that code length. zero means that that |
| 240 | symbol does not occur in this code. |
| 241 | |
| 242 | The codes are sorted by computing a count of codes for each length, |
| 243 | creating from that a table of starting indices for each length in the |
| 244 | sorted table, and then entering the symbols in order in the sorted |
| 245 | table. The sorted table is work[], with that space being provided by |
| 246 | the caller. |
| 247 | |
| 248 | The length counts are used for other purposes as well, i.e. finding |
| 249 | the minimum and maximum length codes, determining if there are any |
| 250 | codes at all, checking for a valid set of lengths, and looking ahead |
| 251 | at length counts to determine sub-table sizes when building the |
| 252 | decoding tables. |
| 253 | */ |
| 254 | |
| 255 | /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ |
| 256 | for (len = 0; len <= MAXBITS; len++) |
| 257 | count[len] = 0; |
| 258 | for (sym = 0; sym < codes; sym++) |
| 259 | count[lens[lens_offset + sym]]++; |
| 260 | |
| 261 | /* bound code lengths, force root to be within code lengths */ |
| 262 | root = bits; |
| 263 | |
| 264 | for (max = MAXBITS; max >= 1; max--) |
| 265 | if (count[max] != 0) break; |
| 266 | if (root > max) root = max; |
| 267 | if (max == 0) { |
| 268 | /* no symbols to code at all */ |
| 269 | /* invalid code marker */ |
| 270 | here = {op:64, bits:1, val:0}; |
| 271 | state_codes[table++] = here; /* make a table to force an error */ |
| 272 | state_codes[table++] = here; |
| 273 | if(type == DISTS) state.distbits = 1; else state.lenbits = 1; // *bits = 1; |
| 274 | state.next = table; |
| 275 | return 0; /* no symbols, but wait for decoding to report error */ |
| 276 | } |
| 277 | for (min = 1; min < max; min++) |
| 278 | if (count[min] != 0) break; |
| 279 | if (root < min) root = min; |
| 280 | |
| 281 | /* check for an over-subscribed or incomplete set of lengths */ |
| 282 | left = 1; |
| 283 | for (len = 1; len <= MAXBITS; len++) { |
| 284 | left <<= 1; |
| 285 | left -= count[len]; |
| 286 | if (left < 0) return -1; /* over-subscribed */ |
| 287 | } |
| 288 | if (left > 0 && (type == CODES || max != 1)) { |
| 289 | state.next = table; |
| 290 | return -1; /* incomplete set */ |
| 291 | } |
| 292 | |
| 293 | /* generate offsets into symbol table for each length for sorting */ |
| 294 | offs[1] = 0; |
| 295 | for (len = 1; len < MAXBITS; len++) |
| 296 | offs[len + 1] = offs[len] + count[len]; |
| 297 | |
| 298 | /* sort symbols by length, by symbol order within each length */ |
| 299 | for (sym = 0; sym < codes; sym++) |
| 300 | if (lens[lens_offset + sym] != 0) work[offs[lens[lens_offset + sym]]++] = sym; |
| 301 | |
| 302 | /* |
| 303 | Create and fill in decoding tables. In this loop, the table being |
| 304 | filled is at next and has curr index bits. The code being used is huff |
| 305 | with length len. That code is converted to an index by dropping drop |
| 306 | bits off of the bottom. For codes where len is less than drop + curr, |
| 307 | those top drop + curr - len bits are incremented through all values to |
| 308 | fill the table with replicated entries. |
| 309 | |
| 310 | root is the number of index bits for the root table. When len exceeds |
| 311 | root, sub-tables are created pointed to by the root entry with an index |
| 312 | of the low root bits of huff. This is saved in low to check for when a |
| 313 | new sub-table should be started. drop is zero when the root table is |
| 314 | being filled, and drop is root when sub-tables are being filled. |
| 315 | |
| 316 | When a new sub-table is needed, it is necessary to look ahead in the |
| 317 | code lengths to determine what size sub-table is needed. The length |
| 318 | counts are used for this, and so count[] is decremented as codes are |
| 319 | entered in the tables. |
| 320 | |
| 321 | used keeps track of how many table entries have been allocated from the |
| 322 | provided *table space. It is checked for LENS and DIST tables against |
| 323 | the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in |
| 324 | the initial root table size constants. See the comments in inftrees.h |
| 325 | for more information. |
| 326 | |
| 327 | sym increments through all symbols, and the loop terminates when |
| 328 | all codes of length max, i.e. all codes, have been processed. This |
| 329 | routine permits incomplete codes, so another loop after this one fills |
| 330 | in the rest of the decoding tables with invalid code markers. |
| 331 | */ |
| 332 | |
| 333 | /* set up for code type */ |
| 334 | switch (type) { |
| 335 | case CODES: |
| 336 | base = extra = work; /* dummy value--not used */ |
| 337 | base_offset = 0; |
| 338 | extra_offset = 0; |
| 339 | end = 19; |
| 340 | break; |
| 341 | case LENS: |
| 342 | base = inflate_table_lbase; |
| 343 | base_offset = -257; // base -= 257; |
| 344 | extra = inflate_table_lext; |
| 345 | extra_offset = -257; // extra -= 257; |
| 346 | end = 256; |
| 347 | break; |
| 348 | default: /* DISTS */ |
| 349 | base = inflate_table_dbase; |
| 350 | extra = inflate_table_dext; |
| 351 | base_offset = 0; |
| 352 | extra_offset = 0; |
| 353 | end = -1; |
| 354 | } |
| 355 | |
| 356 | /* initialize state for loop */ |
| 357 | huff = 0; /* starting code */ |
| 358 | sym = 0; /* starting code symbol */ |
| 359 | len = min; /* starting code length */ |
| 360 | next = table; /* current table to fill in */ |
| 361 | curr = root; /* current table index bits */ |
| 362 | drop = 0; /* current bits to drop from code for index */ |
| 363 | low = -1; /* trigger new sub-table when len > root */ |
| 364 | used = 1 << root; /* use root table entries */ |
| 365 | mask = used - 1; /* mask for comparing low */ |
| 366 | |
| 367 | /* check available table space */ |
| 368 | if ((type == LENS && used >= ENOUGH_LENS) || |
| 369 | (type == DISTS && used >= ENOUGH_DISTS)) { |
| 370 | state.next = table; |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | /* process all codes and make table entries */ |
| 375 | for (;;) { |
| 376 | /* create table entry */ |
| 377 | here = {op:0, bits:len - drop, val:0}; |
| 378 | if (work[sym] < end) { |
| 379 | here.val = work[sym]; |
| 380 | } |
| 381 | else if (work[sym] > end) { |
| 382 | here.op = extra[extra_offset + work[sym]]; |
| 383 | here.val = base[base_offset + work[sym]]; |
| 384 | } |
| 385 | else { |
| 386 | here.op = 32 + 64; /* end of block */ |
| 387 | } |
| 388 | |
| 389 | /* replicate for those indices with low len bits equal to huff */ |
| 390 | incr = 1 << (len - drop); |
| 391 | fill = 1 << curr; |
| 392 | min = fill; /* save offset to next table */ |
| 393 | do { |
| 394 | fill -= incr; |
| 395 | state_codes[next + (huff >>> drop) + fill] = here; |
| 396 | } while (fill != 0); |
| 397 | |
| 398 | /* backwards increment the len-bit code huff */ |
| 399 | incr = 1 << (len - 1); |
| 400 | while (huff & incr) |
| 401 | incr >>>= 1; |
| 402 | if (incr != 0) { |
| 403 | huff &= incr - 1; |
| 404 | huff += incr; |
| 405 | } |
| 406 | else |
| 407 | huff = 0; |
| 408 | |
| 409 | /* go to next symbol, update count, len */ |
| 410 | sym++; |
| 411 | if (--(count[len]) == 0) { |
| 412 | if (len == max) break; |
| 413 | len = lens[lens_offset + work[sym]]; |
| 414 | } |
| 415 | |
| 416 | /* create new sub-table if needed */ |
| 417 | if (len > root && (huff & mask) != low) { |
| 418 | /* if first time, transition to sub-tables */ |
| 419 | if (drop == 0) |
| 420 | drop = root; |
| 421 | |
| 422 | /* increment past last table */ |
| 423 | next += min; /* here min is 1 << curr */ |
| 424 | |
| 425 | /* determine length of next table */ |
| 426 | curr = len - drop; |
| 427 | left = (1 << curr); |
| 428 | while (curr + drop < max) { |
| 429 | left -= count[curr + drop]; |
| 430 | if (left <= 0) break; |
| 431 | curr++; |
| 432 | left <<= 1; |
| 433 | } |
| 434 | |
| 435 | /* check for enough space */ |
| 436 | used += 1 << curr; |
| 437 | if ((type == LENS && used >= ENOUGH_LENS) || |
| 438 | (type == DISTS && used >= ENOUGH_DISTS)) { |
| 439 | state.next = table; |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | /* point entry in root table to sub-table */ |
| 444 | low = huff & mask; |
| 445 | state_codes[table + low] = {op:curr, bits:root, val:next - table}; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* fill in remaining table entry if code is incomplete (guaranteed to have |
| 450 | at most one remaining entry, since if the code is incomplete, the |
| 451 | maximum code length that was allowed to get this far is one bit) */ |
| 452 | if (huff != 0) { |
| 453 | state_codes[next + huff] = {op:64, bits:len - drop, val:0}; |
| 454 | } |
| 455 | |
| 456 | /* set return parameters */ |
| 457 | state.next = table + used; |
| 458 | if(type == DISTS) state.distbits = root; else state.lenbits = root; //*bits = root; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* inffast.c -- fast decoding |
| 463 | * Copyright (C) 1995-2008, 2010 Mark Adler |
| 464 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 465 | */ |
| 466 | |
| 467 | /* |
| 468 | Decode literal, length, and distance codes and write out the resulting |
| 469 | literal and match bytes until either not enough input or output is |
| 470 | available, an end-of-block is encountered, or a data error is encountered. |
| 471 | When large enough input and output buffers are supplied to inflate(), for |
| 472 | example, a 16K input buffer and a 64K output buffer, more than 95% of the |
| 473 | inflate execution time is spent in this routine. |
| 474 | |
| 475 | Entry assumptions: |
| 476 | |
| 477 | state->mode == LEN |
| 478 | strm->avail_in >= 6 |
| 479 | strm->avail_out >= 258 |
| 480 | start >= strm->avail_out |
| 481 | state->bits < 8 |
| 482 | |
| 483 | On return, state->mode is one of: |
| 484 | |
| 485 | LEN -- ran out of enough output space or enough available input |
| 486 | TYPE -- reached end of block code, inflate() to interpret next block |
| 487 | BAD -- error in block data |
| 488 | |
| 489 | Notes: |
| 490 | |
| 491 | - The maximum input bits used by a length/distance pair is 15 bits for the |
| 492 | length code, 5 bits for the length extra, 15 bits for the distance code, |
| 493 | and 13 bits for the distance extra. This totals 48 bits, or six bytes. |
| 494 | Therefore if strm->avail_in >= 6, then there is enough input to avoid |
| 495 | checking for available input while decoding. |
| 496 | |
| 497 | - The maximum bytes that a single length/distance pair can output is 258 |
| 498 | bytes, which is the maximum length that can be coded. inflate_fast() |
| 499 | requires strm->avail_out >= 258 for each loop to avoid checking for |
| 500 | output space. |
| 501 | */ |
| 502 | function inflate_fast(strm, |
| 503 | start) /* inflate()'s starting value for strm->avail_out */ |
| 504 | { |
| 505 | var state; |
| 506 | var input_data; /* local strm->input_data */ |
| 507 | var next_in; /* zlib.js: index of input_data */ |
| 508 | var last; /* while next_in < last, enough input available */ |
| 509 | var out; /* local strm.next_out */ |
| 510 | var beg; /* inflate()'s initial strm.next_out */ |
| 511 | var end; /* while out < end, enough space available */ |
| 512 | //NOSPRT #ifdef INFLATE_STRICT |
| 513 | // unsigned dmax; /* maximum distance from zlib header */ |
| 514 | //#endif |
| 515 | var wsize; /* window size or zero if not using window */ |
| 516 | var whave; /* valid bytes in the window */ |
| 517 | var wnext; /* window write index */ |
| 518 | var window; /* allocated sliding window, if wsize != 0 */ |
| 519 | var hold; /* local strm->hold */ |
| 520 | var bits; /* local strm->bits */ |
| 521 | var codes; /* zlib.js: local state.codes */ |
| 522 | var lcode; /* local strm->lencode */ |
| 523 | var dcode; /* local strm->distcode */ |
| 524 | var lmask; /* mask for first level of length codes */ |
| 525 | var dmask; /* mask for first level of distance codes */ |
| 526 | var here; /* retrieved table entry */ |
| 527 | var op; /* code bits, operation, extra bits, or */ |
| 528 | /* window position, window bytes to copy */ |
| 529 | var len; /* match length, unused bytes */ |
| 530 | var dist; /* match distance */ |
| 531 | // var from; /* where to copy match from */ |
| 532 | var from_window_offset = -1; /* index of window[] */ |
| 533 | var from_out_offset = -1; /* index of next_out[] */ |
| 534 | |
| 535 | /* copy state to local variables */ |
| 536 | state = strm.state; |
| 537 | input_data = strm.input_data; |
| 538 | next_in = strm.next_in; |
| 539 | last = next_in + strm.avail_in - 5; |
| 540 | out = strm.next_out; |
| 541 | beg = out - (start - strm.avail_out); |
| 542 | end = out + (strm.avail_out - 257); |
| 543 | //NOSPRT #ifdef INFLATE_STRICT |
| 544 | // dmax = state->dmax; |
| 545 | //#endif |
| 546 | wsize = state.wsize; |
| 547 | whave = state.whave; |
| 548 | wnext = state.wnext; |
| 549 | window = state.window; |
| 550 | hold = state.hold; |
| 551 | bits = state.bits; |
| 552 | codes = state.codes; |
| 553 | lcode = state.lencode; |
| 554 | dcode = state.distcode; |
| 555 | lmask = (1 << state.lenbits) - 1; |
| 556 | dmask = (1 << state.distbits) - 1; |
| 557 | |
| 558 | /* decode literals and length/distances until end-of-block or not enough |
| 559 | input data or output space */ |
| 560 | loop: do { |
| 561 | if (bits < 15) { |
| 562 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 563 | bits += 8; |
| 564 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 565 | bits += 8; |
| 566 | } |
| 567 | here = codes[lcode + (hold & lmask)]; |
| 568 | dolen: while(true) { |
| 569 | op = here.bits; |
| 570 | hold >>>= op; |
| 571 | bits -= op; |
| 572 | op = here.op; |
| 573 | if (op == 0) { /* literal */ |
| 574 | // Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
| 575 | // "inflate: literal '%c'\n" : |
| 576 | // "inflate: literal 0x%02x\n", here.val)); |
| 577 | strm.output_data += String.fromCharCode(here.val); |
| 578 | out++; |
| 579 | } |
| 580 | else if (op & 16) { /* length base */ |
| 581 | len = here.val; |
| 582 | op &= 15; /* number of extra bits */ |
| 583 | if (op) { |
| 584 | if (bits < op) { |
| 585 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 586 | bits += 8; |
| 587 | } |
| 588 | len += hold & ((1 << op) - 1); |
| 589 | hold >>>= op; |
| 590 | bits -= op; |
| 591 | } |
| 592 | // Tracevv((stderr, "inflate: length %u\n", len)); |
| 593 | if (bits < 15) { |
| 594 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 595 | bits += 8; |
| 596 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 597 | bits += 8; |
| 598 | } |
| 599 | here = codes[dcode + (hold & dmask)]; |
| 600 | dodist: while(true) { |
| 601 | op = here.bits; |
| 602 | hold >>>= op; |
| 603 | bits -= op; |
| 604 | op = here.op; |
| 605 | if (op & 16) { /* distance base */ |
| 606 | dist = here.val; |
| 607 | op &= 15; /* number of extra bits */ |
| 608 | if (bits < op) { |
| 609 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 610 | bits += 8; |
| 611 | if (bits < op) { |
| 612 | hold += (input_data.charCodeAt(next_in++) & 0xff) << bits; |
| 613 | bits += 8; |
| 614 | } |
| 615 | } |
| 616 | dist += hold & ((1 << op) - 1); |
| 617 | //NOSPRT #ifdef INFLATE_STRICT |
| 618 | // if (dist > dmax) { |
| 619 | // strm->msg = (char *)"invalid distance too far back"; |
| 620 | // state->mode = BAD; |
| 621 | // break loop; |
| 622 | // } |
| 623 | //#endif |
| 624 | hold >>>= op; |
| 625 | bits -= op; |
| 626 | // Tracevv((stderr, "inflate: distance %u\n", dist)); |
| 627 | op = out - beg; /* max distance in output */ |
| 628 | if (dist > op) { /* see if copy from window */ |
| 629 | op = dist - op; /* distance back in window */ |
| 630 | if (op > whave) { |
| 631 | if (state.sane) { |
| 632 | strm.msg = 'invalid distance too far back'; |
| 633 | state.mode = BAD; |
| 634 | break loop; |
| 635 | } |
| 636 | //NOSPRT #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
| 637 | // if (len <= op - whave) { |
| 638 | // do { |
| 639 | // PUP(out) = 0; |
| 640 | // } while (--len); |
| 641 | // continue; |
| 642 | // } |
| 643 | // len -= op - whave; |
| 644 | // do { |
| 645 | // PUP(out) = 0; |
| 646 | // } while (--op > whave); |
| 647 | // if (op == 0) { |
| 648 | // from = out - dist; |
| 649 | // do { |
| 650 | // PUP(out) = PUP(from); |
| 651 | // } while (--len); |
| 652 | // continue; |
| 653 | // } |
| 654 | //#endif |
| 655 | } // if (op > whave) |
| 656 | |
| 657 | from_window_offset = 0; |
| 658 | from_out_offset = -1; |
| 659 | if (wnext == 0) { /* very common case */ |
| 660 | from_window_offset += wsize - op; |
| 661 | if (op < len) { /* some from window */ |
| 662 | len -= op; |
| 663 | strm.output_data += window.substring(from_window_offset, from_window_offset + op); |
| 664 | out += op; |
| 665 | op = 0; |
| 666 | from_window_offset = -1; |
| 667 | from_out_offset = out - dist; /* rest from output */ |
| 668 | } |
| 669 | } |
| 670 | //NOTREACHED else if (wnext < op) { /* wrap around window */ |
| 671 | //NOTREACHED from += wsize + wnext - op; |
| 672 | //NOTREACHED op -= wnext; |
| 673 | //NOTREACHED if (op < len) { /* some from end of window */ |
| 674 | //NOTREACHED len -= op; |
| 675 | //NOTREACHED do { |
| 676 | //NOTREACHED PUP(out) = PUP(from); |
| 677 | //NOTREACHED } while (--op); |
| 678 | //NOTREACHED from = window - OFF; |
| 679 | //NOTREACHED if (wnext < len) { /* some from start of window */ |
| 680 | //NOTREACHED op = wnext; |
| 681 | //NOTREACHED len -= op; |
| 682 | //NOTREACHED do { |
| 683 | //NOTREACHED PUP(out) = PUP(from); |
| 684 | //NOTREACHED } while (--op); |
| 685 | //NOTREACHED from = out - dist; /* rest from output */ |
| 686 | //NOTREACHED } |
| 687 | //NOTREACHED } |
| 688 | //NOTREACHED } |
| 689 | else { /* contiguous in window */ |
| 690 | from_window_offset += wnext - op; |
| 691 | if (op < len) { /* some from window */ |
| 692 | len -= op; |
| 693 | strm.output_data += window.substring(from_window_offset, from_window_offset + op); |
| 694 | out += op; |
| 695 | from_window_offset = -1; |
| 696 | from_out_offset = out - dist; /* rest from output */ |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | else { |
| 701 | from_window_offset = -1; |
| 702 | from_out_offset = out - dist; /* copy direct from output */ |
| 703 | } |
| 704 | |
| 705 | if (from_window_offset >= 0) { |
| 706 | strm.output_data += window.substring(from_window_offset, from_window_offset + len); |
| 707 | out += len; |
| 708 | from_window_offset += len; |
| 709 | } else { |
| 710 | var len_inner = len; |
| 711 | if(len_inner > out - from_out_offset) |
| 712 | len_inner = out - from_out_offset; |
| 713 | strm.output_data += strm.output_data.substring( |
| 714 | from_out_offset, from_out_offset + len_inner); |
| 715 | out += len_inner; |
| 716 | len -= len_inner; |
| 717 | from_out_offset += len_inner; |
| 718 | out += len; |
| 719 | while (len > 2) { |
| 720 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 721 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 722 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 723 | len -= 3; |
| 724 | } |
| 725 | if (len) { |
| 726 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 727 | if (len > 1) |
| 728 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | else if ((op & 64) == 0) { /* 2nd level distance code */ |
| 733 | here = codes[dcode + (here.val + (hold & ((1 << op) - 1)))]; |
| 734 | continue dodist; // goto dodist |
| 735 | } |
| 736 | else { |
| 737 | strm.msg = 'invalid distance code'; |
| 738 | state.mode = BAD; |
| 739 | break loop; |
| 740 | } |
| 741 | break dodist; } |
| 742 | } |
| 743 | else if ((op & 64) == 0) { /* 2nd level length code */ |
| 744 | here = codes[lcode + (here.val + (hold & ((1 << op) - 1)))]; |
| 745 | continue dolen; // goto dolen; |
| 746 | } |
| 747 | else if (op & 32) { /* end-of-block */ |
| 748 | // Tracevv((stderr, "inflate: end of block\n")); |
| 749 | state.mode = TYPE; |
| 750 | break loop; |
| 751 | } |
| 752 | else { |
| 753 | strm.msg = 'invalid literal/length code'; |
| 754 | state.mode = BAD; |
| 755 | break loop; |
| 756 | } |
| 757 | break dolen; } |
| 758 | } while (next_in < last && out < end); |
| 759 | |
| 760 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ |
| 761 | len = bits >>> 3; |
| 762 | next_in -= len; |
| 763 | bits -= len << 3; |
| 764 | hold &= (1 << bits) - 1; |
| 765 | |
| 766 | /* update state and return */ |
| 767 | strm.next_in = next_in; |
| 768 | strm.next_out = out; |
| 769 | strm.avail_in = (next_in < last ? 5 + (last - next_in) : 5 - (next_in - last)); |
| 770 | strm.avail_out = (out < end ? |
| 771 | 257 + (end - out) : 257 - (out - end)); |
| 772 | state.hold = hold; |
| 773 | state.bits = bits; |
| 774 | } |
| 775 | |
| 776 | function new_array(size) |
| 777 | { |
| 778 | var i; |
| 779 | var ary = new Array(size); |
| 780 | for(i = 0; i < size; i++) |
| 781 | ary[i] = 0; |
| 782 | return ary; |
| 783 | } |
| 784 | |
| 785 | function getarg(opts, name, def_value) |
| 786 | { |
| 787 | return (opts && (name in opts)) ? opts[name] : def_value; |
| 788 | } |
| 789 | |
| 790 | function checksum_none() |
| 791 | { |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * z_stream constructor |
| 797 | * @constructor |
| 798 | */ |
| 799 | function inflate_state() |
| 800 | { |
| 801 | var i; |
| 802 | |
| 803 | this.mode = 0; /* current inflate mode */ |
| 804 | this.last = 0; /* true if processing last block */ |
| 805 | this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ |
| 806 | this.havedict = 0; /* true if dictionary provided */ |
| 807 | this.flags = 0; /* gzip header method and flags (0 if zlib) */ |
| 808 | this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ |
| 809 | this.check = 0; /* protected copy of check value */ |
| 810 | this.total = 0; /* protected copy of output count */ |
| 811 | this.head = null; /* where to save gzip header information */ |
| 812 | /* sliding window */ |
| 813 | this.wbits = 0; /* log base 2 of requested window size */ |
| 814 | this.wsize = 0; /* window size or zero if not using window */ |
| 815 | this.whave = 0; /* valid bytes in the window */ |
| 816 | this.wnext = 0; /* window write index (TODO remove) */ |
| 817 | this.window = null; /* allocated sliding window, if needed */ |
| 818 | /* bit accumulator */ |
| 819 | this.hold = 0; /* input bit accumulator */ |
| 820 | this.bits = 0; /* number of bits in "in" */ |
| 821 | /* for string and stored block copying */ |
| 822 | this.length = 0; /* literal or length of data to copy */ |
| 823 | this.offset = 0; /* distance back to copy string from */ |
| 824 | /* for table and code decoding */ |
| 825 | this.extra = 0; /* extra bits needed */ |
| 826 | /* fixed and dynamic code tables */ |
| 827 | |
| 828 | /* zlib.js: modified implementation: lencode, distcode, next are offset of codes[] */ |
| 829 | this.lencode = 0; /* starting table for length/literal codes */ |
| 830 | this.distcode = 0; /* starting table for distance codes */ |
| 831 | this.lenbits = 0; /* index bits for lencode */ |
| 832 | this.distbits = 0; /* index bits for distcode */ |
| 833 | /* dynamic table building */ |
| 834 | this.ncode = 0; /* number of code length code lengths */ |
| 835 | this.nlen = 0; /* number of length code lengths */ |
| 836 | this.ndist = 0; /* number of distance code lengths */ |
| 837 | this.have = 0; /* number of code lengths in lens[] */ |
| 838 | this.next = 0; /* next available space in codes[] */ |
| 839 | this.lens = new_array(320); /* temporary storage for code lengths */ |
| 840 | this.work = new_array(288); /* work area for code table building */ |
| 841 | this.codes = new Array(ENOUGH); /* space for code tables */ |
| 842 | var c = {op:0, bits:0, val:0}; |
| 843 | for(i = 0; i < ENOUGH; i++) |
| 844 | this.codes[i] = c; |
| 845 | this.sane = 0; /* if false, allow invalid distance too far */ |
| 846 | this.back = 0; /* bits back of last unprocessed length/lit */ |
| 847 | this.was = 0; /* initial length of match */ |
| 848 | } |
| 849 | |
| 850 | ZLIB.inflateResetKeep = function(strm) |
| 851 | { |
| 852 | var state; |
| 853 | |
| 854 | if (!strm || !strm.state) return ZLIB.Z_STREAM_ERROR; |
| 855 | state = strm.state; |
| 856 | strm.total_in = strm.total_out = state.total = 0; |
| 857 | strm.msg = null; |
| 858 | if (state.wrap) { /* to support ill-conceived Java test suite */ |
| 859 | strm.adler = state.wrap & 1; |
| 860 | } |
| 861 | |
| 862 | state.mode = HEAD; |
| 863 | state.last = 0; |
| 864 | state.havedict = 0; |
| 865 | state.dmax = 32768; |
| 866 | state.head = null; |
| 867 | state.hold = 0; |
| 868 | state.bits = 0; |
| 869 | state.lencode = 0; |
| 870 | state.distcode = 0; |
| 871 | state.next = 0; |
| 872 | state.sane = 1; |
| 873 | state.back = -1; |
| 874 | return ZLIB.Z_OK; |
| 875 | }; |
| 876 | |
| 877 | // Usage: strm = ZLIB.inflateReset(z_stream [, windowBits]); |
| 878 | ZLIB.inflateReset = function(strm, windowBits) |
| 879 | { |
| 880 | var wrap; |
| 881 | var state; |
| 882 | |
| 883 | /* get the state */ |
| 884 | if (!strm || !strm.state) return ZLIB.Z_STREAM_ERROR; |
| 885 | state = strm.state; |
| 886 | |
| 887 | if(typeof windowBits === "undefined") |
| 888 | windowBits = DEF_WBITS; |
| 889 | |
| 890 | /* extract wrap request from windowBits parameter */ |
| 891 | if (windowBits < 0) { |
| 892 | wrap = 0; |
| 893 | windowBits = -windowBits; |
| 894 | } |
| 895 | else { |
| 896 | wrap = (windowBits >>> 4) + 1; |
| 897 | if (windowBits < 48) |
| 898 | windowBits &= 15; |
| 899 | } |
| 900 | |
| 901 | if(wrap == 1 && (typeof ZLIB.adler32 === 'function')) { |
| 902 | strm.checksum_function = ZLIB.adler32; |
| 903 | } else if(wrap == 2 && (typeof ZLIB.crc32 === 'function')) { |
| 904 | strm.checksum_function = ZLIB.crc32; |
| 905 | } else { |
| 906 | strm.checksum_function = checksum_none; |
| 907 | } |
| 908 | |
| 909 | /* set number of window bits, free window if different */ |
| 910 | if (windowBits && (windowBits < 8 || windowBits > 15)) |
| 911 | return ZLIB.Z_STREAM_ERROR; |
| 912 | if (state.window && state.wbits != windowBits) { |
| 913 | state.window = null; |
| 914 | } |
| 915 | |
| 916 | /* update state and reset the rest of it */ |
| 917 | state.wrap = wrap; |
| 918 | state.wbits = windowBits; |
| 919 | state.wsize = 0; |
| 920 | state.whave = 0; |
| 921 | state.wnext = 0; |
| 922 | return ZLIB.inflateResetKeep(strm); |
| 923 | }; |
| 924 | |
| 925 | // Usage: strm = ZLIB.inflateInit([windowBits]); |
| 926 | ZLIB.inflateInit = function(windowBits) |
| 927 | { |
| 928 | var strm = new ZLIB.z_stream(); |
| 929 | strm.state = new inflate_state(); |
| 930 | ZLIB.inflateReset(strm, windowBits); |
| 931 | return strm; |
| 932 | }; |
| 933 | |
| 934 | ZLIB.inflatePrime = function(strm, bits, value) |
| 935 | { |
| 936 | var state; |
| 937 | |
| 938 | if (!strm || !strm.state) return ZLIB.Z_STREAM_ERROR; |
| 939 | state = strm.state; |
| 940 | if (bits < 0) { |
| 941 | state.hold = 0; |
| 942 | state.bits = 0; |
| 943 | return ZLIB.Z_OK; |
| 944 | } |
| 945 | if (bits > 16 || state.bits + bits > 32) return ZLIB.Z_STREAM_ERROR; |
| 946 | value &= (1 << bits) - 1; |
| 947 | state.hold += value << state.bits; |
| 948 | state.bits += bits; |
| 949 | return ZLIB.Z_OK; |
| 950 | }; |
| 951 | |
| 952 | var lenfix_ary = null; |
| 953 | var distfix_ary = null; |
| 954 | function fixedtables(state) |
| 955 | { |
| 956 | var i; |
| 957 | if (!lenfix_ary) lenfix_ary = [ { op: 96, bits: 7, val: 0 }, { op: 0, bits: 8, val: 80 }, { op: 0, bits: 8, val: 16 }, { op: 20, bits: 8, val: 115 }, { op: 18, bits: 7, val: 31 }, { op: 0, bits: 8, val: 112 }, { op: 0, bits: 8, val: 48 }, { op: 0, bits: 9, val: 192 }, { op: 16, bits: 7, val: 10 }, { op: 0, bits: 8, val: 96 }, { op: 0, bits: 8, val: 32 }, { op: 0, bits: 9, val: 160 }, { op: 0, bits: 8, val: 0 }, { op: 0, bits: 8, val: 128 }, { op: 0, bits: 8, val: 64 }, { op: 0, bits: 9, val: 224 }, { op: 16, bits: 7, val: 6 }, { op: 0, bits: 8, val: 88 }, { op: 0, bits: 8, val: 24 }, { op: 0, bits: 9, val: 144 }, { op: 19, bits: 7, val: 59 }, { op: 0, bits: 8, val: 120 }, { op: 0, bits: 8, val: 56 }, { op: 0, bits: 9, val: 208 }, { op: 17, bits: 7, val: 17 }, { op: 0, bits: 8, val: 104 }, { op: 0, bits: 8, val: 40 }, { op: 0, bits: 9, val: 176 }, { op: 0, bits: 8, val: 8 }, { op: 0, bits: 8, val: 136 }, { op: 0, bits: 8, val: 72 }, { op: 0, bits: 9, val: 240 }, { op: 16, bits: 7, val: 4 }, { op: 0, bits: 8, val: 84 }, { op: 0, bits: 8, val: 20 }, { op: 21, bits: 8, val: 227 }, { op: 19, bits: 7, val: 43 }, { op: 0, bits: 8, val: 116 }, { op: 0, bits: 8, val: 52 }, { op: 0, bits: 9, val: 200 }, { op: 17, bits: 7, val: 13 }, { op: 0, bits: 8, val: 100 }, { op: 0, bits: 8, val: 36 }, { op: 0, bits: 9, val: 168 }, { op: 0, bits: 8, val: 4 }, { op: 0, bits: 8, val: 132 }, { op: 0, bits: 8, val: 68 }, { op: 0, bits: 9, val: 232 }, { op: 16, bits: 7, val: 8 }, { op: 0, bits: 8, val: 92 }, { op: 0, bits: 8, val: 28 }, { op: 0, bits: 9, val: 152 }, { op: 20, bits: 7, val: 83 }, { op: 0, bits: 8, val: 124 }, { op: 0, bits: 8, val: 60 }, { op: 0, bits: 9, val: 216 }, { op: 18, bits: 7, val: 23 }, { op: 0, bits: 8, val: 108 }, { op: 0, bits: 8, val: 44 }, { op: 0, bits: 9, val: 184 }, { op: 0, bits: 8, val: 12 }, { op: 0, bits: 8, val: 140 }, { op: 0, bits: 8, val: 76 }, { op: 0, bits: 9, val: 248 }, { op: 16, bits: 7, val: 3 }, { op: 0, bits: 8, val: 82 }, { op: 0, bits: 8, val: 18 }, { op: 21, bits: 8, val: 163 }, { op: 19, bits: 7, val: 35 }, { op: 0, bits: 8, val: 114 }, { op: 0, bits: 8, val: 50 }, { op: 0, bits: 9, val: 196 }, { op: 17, bits: 7, val: 11 }, { op: 0, bits: 8, val: 98 }, { op: 0, bits: 8, val: 34 }, { op: 0, bits: 9, val: 164 }, { op: 0, bits: 8, val: 2 }, { op: 0, bits: 8, val: 130 }, { op: 0, bits: 8, val: 66 }, { op: 0, bits: 9, val: 228 }, { op: 16, bits: 7, val: 7 }, { op: 0, bits: 8, val: 90 }, { op: 0, bits: 8, val: 26 }, { op: 0, bits: 9, val: 148 }, { op: 20, bits: 7, val: 67 }, { op: 0, bits: 8, val: 122 }, { op: 0, bits: 8, val: 58 }, { op: 0, bits: 9, val: 212 }, { op: 18, bits: 7, val: 19 }, { op: 0, bits: 8, val: 106 }, { op: 0, bits: 8, val: 42 }, { op: 0, bits: 9, val: 180 }, { op: 0, bits: 8, val: 10 }, { op: 0, bits: 8, val: 138 }, { op: 0, bits: 8, val: 74 }, { op: 0, bits: 9, val: 244 }, { op: 16, bits: 7, val: 5 }, { op: 0, bits: 8, val: 86 }, { op: 0, bits: 8, val: 22 }, { op: 64, bits: 8, val: 0 }, { op: 19, bits: 7, val: 51 }, { op: 0, bits: 8, val: 118 }, { op: 0, bits: 8, val: 54 }, { op: 0, bits: 9, val: 204 }, { op: 17, bits: 7, val: 15 }, { op: 0, bits: 8, val: 102 }, { op: 0, bits: 8, val: 38 }, { op: 0, bits: 9, val: 172 }, { op: 0, bits: 8, val: 6 }, { op: 0, bits: 8, val: 134 }, { op: 0, bits: 8, val: 70 }, { op: 0, bits: 9, val: 236 }, { op: 16, bits: 7, val: 9 }, { op: 0, bits: 8, val: 94 }, { op: 0, bits: 8, val: 30 }, { op: 0, bits: 9, val: 156 }, { op: 20, bits: 7, val: 99 }, { op: 0, bits: 8, val: 126 }, { op: 0, bits: 8, val: 62 }, { op: 0, bits: 9, val: 220 }, { op: 18, bits: 7, val: 27 }, { op: 0, bits: 8, val: 110 }, { op: 0, bits: 8, val: 46 }, { op: 0, bits: 9, val: 188 }, { op: 0, bits: 8, val: 14 }, { op: 0, bits: 8, val: 142 }, { op: 0, bits: 8, val: 78 }, { op: 0, bits: 9, val: 252 }, { op: 96, bits: 7, val: 0 }, { op: 0, bits: 8, val: 81 }, { op: 0, bits: 8, val: 17 }, { op: 21, bits: 8, val: 131 }, { op: 18, bits: 7, val: 31 }, { op: 0, bits: 8, val: 113 }, { op: 0, bits: 8, val: 49 }, { op: 0, bits: 9, val: 194 }, { op: 16, bits: 7, val: 10 }, { op: 0, bits: 8, val: 97 }, { op: 0, bits: 8, val: 33 }, { op: 0, bits: 9, val: 162 }, { op: 0, bits: 8, val: 1 }, { op: 0, bits: 8, val: 129 }, { op: 0, bits: 8, val: 65 }, { op: 0, bits: 9, val: 226 }, { op: 16, bits: 7, val: 6 }, { op: 0, bits: 8, val: 89 }, { op: 0, bits: 8, val: 25 }, { op: 0, bits: 9, val: 146 }, { op: 19, bits: 7, val: 59 }, { op: 0, bits: 8, val: 121 }, { op: 0, bits: 8, val: 57 }, { op: 0, bits: 9, val: 210 }, { op: 17, bits: 7, val: 17 }, { op: 0, bits: 8, val: 105 }, { op: 0, bits: 8, val: 41 }, { op: 0, bits: 9, val: 178 }, { op: 0, bits: 8, val: 9 }, { op: 0, bits: 8, val: 137 }, { op: 0, bits: 8, val: 73 }, { op: 0, bits: 9, val: 242 }, { op: 16, bits: 7, val: 4 }, { op: 0, bits: 8, val: 85 }, { op: 0, bits: 8, val: 21 }, { op: 16, bits: 8, val: 258 }, { op: 19, bits: 7, val: 43 }, { op: 0, bits: 8, val: 117 }, { op: 0, bits: 8, val: 53 }, { op: 0, bits: 9, val: 202 }, { op: 17, bits: 7, val: 13 }, { op: 0, bits: 8, val: 101 }, { op: 0, bits: 8, val: 37 }, { op: 0, bits: 9, val: 170 }, { op: 0, bits: 8, val: 5 }, { op: 0, bits: 8, val: 133 }, { op: 0, bits: 8, val: 69 }, { op: 0, bits: 9, val: 234 }, { op: 16, bits: 7, val: 8 }, { op: 0, bits: 8, val: 93 }, { op: 0, bits: 8, val: 29 }, { op: 0, bits: 9, val: 154 }, { op: 20, bits: 7, val: 83 }, { op: 0, bits: 8, val: 125 }, { op: 0, bits: 8, val: 61 }, { op: 0, bits: 9, val: 218 }, { op: 18, bits: 7, val: 23 }, { op: 0, bits: 8, val: 109 }, { op: 0, bits: 8, val: 45 }, { op: 0, bits: 9, val: 186 }, { op: 0, bits: 8, val: 13 }, { op: 0, bits: 8, val: 141 }, { op: 0, bits: 8, val: 77 }, { op: 0, bits: 9, val: 250 }, { op: 16, bits: 7, val: 3 }, { op: 0, bits: 8, val: 83 }, { op: 0, bits: 8, val: 19 }, { op: 21, bits: 8, val: 195 }, { op: 19, bits: 7, val: 35 }, { op: 0, bits: 8, val: 115 }, { op: 0, bits: 8, val: 51 }, { op: 0, bits: 9, val: 198 }, { op: 17, bits: 7, val: 11 }, { op: 0, bits: 8, val: 99 }, { op: 0, bits: 8, val: 35 }, { op: 0, bits: 9, val: 166 }, { op: 0, bits: 8, val: 3 }, { op: 0, bits: 8, val: 131 }, { op: 0, bits: 8, val: 67 }, { op: 0, bits: 9, val: 230 }, { op: 16, bits: 7, val: 7 }, { op: 0, bits: 8, val: 91 }, { op: 0, bits: 8, val: 27 }, { op: 0, bits: 9, val: 150 }, { op: 20, bits: 7, val: 67 }, { op: 0, bits: 8, val: 123 }, { op: 0, bits: 8, val: 59 }, { op: 0, bits: 9, val: 214 }, { op: 18, bits: 7, val: 19 }, { op: 0, bits: 8, val: 107 }, { op: 0, bits: 8, val: 43 }, { op: 0, bits: 9, val: 182 }, { op: 0, bits: 8, val: 11 }, { op: 0, bits: 8, val: 139 }, { op: 0, bits: 8, val: 75 }, { op: 0, bits: 9, val: 246 }, { op: 16, bits: 7, val: 5 }, { op: 0, bits: 8, val: 87 }, { op: 0, bits: 8, val: 23 }, { op: 64, bits: 8, val: 0 }, { op: 19, bits: 7, val: 51 }, { op: 0, bits: 8, val: 119 }, { op: 0, bits: 8, val: 55 }, { op: 0, bits: 9, val: 206 }, { op: 17, bits: 7, val: 15 }, { op: 0, bits: 8, val: 103 }, { op: 0, bits: 8, val: 39 }, { op: 0, bits: 9, val: 174 }, { op: 0, bits: 8, val: 7 }, { op: 0, bits: 8, val: 135 }, { op: 0, bits: 8, val: 71 }, { op: 0, bits: 9, val: 238 }, { op: 16, bits: 7, val: 9 }, { op: 0, bits: 8, val: 95 }, { op: 0, bits: 8, val: 31 }, { op: 0, bits: 9, val: 158 }, { op: 20, bits: 7, val: 99 }, { op: 0, bits: 8, val: 127 }, { op: 0, bits: 8, val: 63 }, { op: 0, bits: 9, val: 222 }, { op: 18, bits: 7, val: 27 }, { op: 0, bits: 8, val: 111 }, { op: 0, bits: 8, val: 47 }, { op: 0, bits: 9, val: 190 }, { op: 0, bits: 8, val: 15 }, { op: 0, bits: 8, val: 143 }, { op: 0, bits: 8, val: 79 }, { op: 0, bits: 9, val: 254 }, { op: 96, bits: 7, val: 0 }, { op: 0, bits: 8, val: 80 }, { op: 0, bits: 8, val: 16 }, { op: 20, bits: 8, val: 115 }, { op: 18, bits: 7, val: 31 }, { op: 0, bits: 8, val: 112 }, { op: 0, bits: 8, val: 48 }, { op: 0, bits: 9, val: 193 }, { op: 16, bits: 7, val: 10 }, { op: 0, bits: 8, val: 96 }, { op: 0, bits: 8, val: 32 }, { op: 0, bits: 9, val: 161 }, { op: 0, bits: 8, val: 0 }, { op: 0, bits: 8, val: 128 }, { op: 0, bits: 8, val: 64 }, { op: 0, bits: 9, val: 225 }, { op: 16, bits: 7, val: 6 }, { op: 0, bits: 8, val: 88 }, { op: 0, bits: 8, val: 24 }, { op: 0, bits: 9, val: 145 }, { op: 19, bits: 7, val: 59 }, { op: 0, bits: 8, val: 120 }, { op: 0, bits: 8, val: 56 }, { op: 0, bits: 9, val: 209 }, { op: 17, bits: 7, val: 17 }, { op: 0, bits: 8, val: 104 }, { op: 0, bits: 8, val: 40 }, { op: 0, bits: 9, val: 177 }, { op: 0, bits: 8, val: 8 }, { op: 0, bits: 8, val: 136 }, { op: 0, bits: 8, val: 72 }, { op: 0, bits: 9, val: 241 }, { op: 16, bits: 7, val: 4 }, { op: 0, bits: 8, val: 84 }, { op: 0, bits: 8, val: 20 }, { op: 21, bits: 8, val: 227 }, { op: 19, bits: 7, val: 43 }, { op: 0, bits: 8, val: 116 }, { op: 0, bits: 8, val: 52 }, { op: 0, bits: 9, val: 201 }, { op: 17, bits: 7, val: 13 }, { op: 0, bits: 8, val: 100 }, { op: 0, bits: 8, val: 36 }, { op: 0, bits: 9, val: 169 }, { op: 0, bits: 8, val: 4 }, { op: 0, bits: 8, val: 132 }, { op: 0, bits: 8, val: 68 }, { op: 0, bits: 9, val: 233 }, { op: 16, bits: 7, val: 8 }, { op: 0, bits: 8, val: 92 }, { op: 0, bits: 8, val: 28 }, { op: 0, bits: 9, val: 153 }, { op: 20, bits: 7, val: 83 }, { op: 0, bits: 8, val: 124 }, { op: 0, bits: 8, val: 60 }, { op: 0, bits: 9, val: 217 }, { op: 18, bits: 7, val: 23 }, { op: 0, bits: 8, val: 108 }, { op: 0, bits: 8, val: 44 }, { op: 0, bits: 9, val: 185 }, { op: 0, bits: 8, val: 12 }, { op: 0, bits: 8, val: 140 }, { op: 0, bits: 8, val: 76 }, { op: 0, bits: 9, val: 249 }, { op: 16, bits: 7, val: 3 }, { op: 0, bits: 8, val: 82 }, { op: 0, bits: 8, val: 18 }, { op: 21, bits: 8, val: 163 }, { op: 19, bits: 7, val: 35 }, { op: 0, bits: 8, val: 114 }, { op: 0, bits: 8, val: 50 }, { op: 0, bits: 9, val: 197 }, { op: 17, bits: 7, val: 11 }, { op: 0, bits: 8, val: 98 }, { op: 0, bits: 8, val: 34 }, { op: 0, bits: 9, val: 165 }, { op: 0, bits: 8, val: 2 }, { op: 0, bits: 8, val: 130 }, { op: 0, bits: 8, val: 66 }, { op: 0, bits: 9, val: 229 }, { op: 16, bits: 7, val: 7 }, { op: 0, bits: 8, val: 90 }, { op: 0, bits: 8, val: 26 }, { op: 0, bits: 9, val: 149 }, { op: 20, bits: 7, val: 67 }, { op: 0, bits: 8, val: 122 }, { op: 0, bits: 8, val: 58 },{ op: 0, bits: 9, val: 213 }, { op: 18, bits: 7, val: 19 }, { op: 0, bits: 8, val: 106 }, { op: 0, bits: 8, val: 42 }, { op: 0, bits: 9, val: 181 }, { op: 0, bits: 8, val: 10 }, { op: 0, bits: 8, val: 138 },{ op: 0, bits: 8, val: 74 }, { op: 0, bits: 9, val: 245 }, { op: 16, bits: 7, val: 5 }, { op: 0, bits: 8, val: 86 }, { op: 0, bits: 8, val: 22 }, { op: 64, bits: 8, val: 0 }, { op: 19, bits: 7, val: 51 },{ op: 0, bits: 8, val: 118 }, { op: 0, bits: 8, val: 54 }, { op: 0, bits: 9, val: 205 }, { op: 17, bits: 7, val: 15 }, { op: 0, bits: 8, val: 102 }, { op: 0, bits: 8, val: 38 }, { op: 0, bits: 9, val: 173 },{ op: 0, bits: 8, val: 6 }, { op: 0, bits: 8, val: 134 }, { op: 0, bits: 8, val: 70 }, { op: 0, bits: 9, val: 237 }, { op: 16, bits: 7, val: 9 }, { op: 0, bits: 8, val: 94 }, { op: 0, bits: 8, val: 30 },{ op: 0, bits: 9, val: 157 }, { op: 20, bits: 7, val: 99 }, { op: 0, bits: 8, val: 126 }, { op: 0, bits: 8, val: 62 }, { op: 0, bits: 9, val: 221 }, { op: 18, bits: 7, val: 27 }, { op: 0, bits: 8, val: 110 }, { op: 0, bits: 8, val: 46 }, { op: 0, bits: 9, val: 189 }, { op: 0, bits: 8, val: 14 }, { op: 0, bits: 8, val: 142 }, { op: 0, bits: 8, val: 78 }, { op: 0, bits: 9, val: 253 }, { op: 96, bits: 7, val: 0 }, { op: 0, bits: 8, val: 81 }, { op: 0, bits: 8, val: 17 }, { op: 21, bits: 8, val: 131 }, { op: 18, bits: 7, val: 31 }, { op: 0, bits: 8, val: 113 }, { op: 0, bits: 8, val: 49 }, { op: 0, bits: 9, val: 195 }, { op: 16, bits: 7, val: 10 }, { op: 0, bits: 8, val: 97 }, { op: 0, bits: 8, val: 33 }, { op: 0, bits: 9, val: 163 }, { op: 0, bits: 8, val: 1 }, { op: 0, bits: 8, val: 129 }, { op: 0, bits: 8, val: 65 }, { op: 0, bits: 9, val: 227 }, { op: 16, bits: 7, val: 6 }, { op: 0, bits: 8, val: 89 }, { op: 0, bits: 8, val: 25 }, { op: 0, bits: 9, val: 147 }, { op: 19, bits: 7, val: 59 }, { op: 0, bits: 8, val: 121 }, { op: 0, bits: 8, val: 57 }, { op: 0, bits: 9, val: 211 }, { op: 17, bits: 7, val: 17 }, { op: 0, bits: 8, val: 105 }, { op: 0, bits: 8, val: 41 }, { op: 0, bits: 9, val: 179 }, { op: 0, bits: 8, val: 9 },{ op: 0, bits: 8, val: 137 }, { op: 0, bits: 8, val: 73 }, { op: 0, bits: 9, val: 243 }, { op: 16, bits: 7, val: 4 }, { op: 0, bits: 8, val: 85 }, { op: 0, bits: 8, val: 21 }, { op: 16, bits: 8, val: 258 },{ op: 19, bits: 7, val: 43 }, { op: 0, bits: 8, val: 117 }, { op: 0, bits: 8, val: 53 }, { op: 0, bits: 9, val: 203 }, { op: 17, bits: 7, val: 13 }, { op: 0, bits: 8, val: 101 }, { op: 0, bits: 8, val: 37 },{ op: 0, bits: 9, val: 171 }, { op: 0, bits: 8, val: 5 }, { op: 0, bits: 8, val: 133 }, { op: 0, bits: 8, val: 69 }, { op: 0, bits: 9, val: 235 }, { op: 16, bits: 7, val: 8 }, { op: 0, bits: 8, val: 93 },{ op: 0, bits: 8, val: 29 }, { op: 0, bits: 9, val: 155 }, { op: 20, bits: 7, val: 83 }, { op: 0, bits: 8, val: 125 }, { op: 0, bits: 8, val: 61 }, { op: 0, bits: 9, val: 219 }, { op: 18, bits: 7, val: 23 },{ op: 0, bits: 8, val: 109 }, { op: 0, bits: 8, val: 45 }, { op: 0, bits: 9, val: 187 }, { op: 0, bits: 8, val: 13 }, { op: 0, bits: 8, val: 141 }, { op: 0, bits: 8, val: 77 }, { op: 0, bits: 9, val: 251 }, { op: 16, bits: 7, val: 3 }, { op: 0, bits: 8, val: 83 }, { op: 0, bits: 8, val: 19 }, { op: 21, bits: 8, val: 195 }, { op: 19, bits: 7, val: 35 }, { op: 0, bits: 8, val: 115 }, { op: 0, bits: 8, val: 51 }, { op: 0, bits: 9, val: 199 }, { op: 17, bits: 7, val: 11 }, { op: 0, bits: 8, val: 99 }, { op: 0, bits: 8, val: 35 }, { op: 0, bits: 9, val: 167 }, { op: 0, bits: 8, val: 3 }, { op: 0, bits: 8, val: 131 }, { op: 0, bits: 8, val: 67 }, { op: 0, bits: 9, val: 231 }, { op: 16, bits: 7, val: 7 }, { op: 0, bits: 8, val: 91 }, { op: 0, bits: 8, val: 27 }, { op: 0, bits: 9, val: 151 }, { op: 20, bits: 7, val: 67 }, { op: 0, bits: 8, val: 123 }, { op: 0, bits: 8, val: 59 }, { op: 0, bits: 9, val: 215 }, { op: 18, bits: 7, val: 19 }, { op: 0, bits: 8, val: 107 }, { op: 0, bits: 8, val: 43 }, { op: 0, bits: 9, val: 183 }, { op: 0, bits: 8, val: 11 }, { op: 0, bits: 8, val: 139 }, { op: 0, bits: 8, val: 75 }, { op: 0, bits: 9, val: 247 }, { op: 16, bits: 7, val: 5 }, { op: 0, bits: 8, val: 87 }, { op: 0, bits: 8, val: 23 }, { op: 64, bits: 8, val: 0 }, { op: 19, bits: 7, val: 51 }, { op: 0, bits: 8, val: 119 }, { op: 0, bits: 8, val: 55 }, { op: 0, bits: 9, val: 207 }, { op: 17, bits: 7, val: 15 }, { op: 0, bits: 8, val: 103 }, { op: 0, bits: 8, val: 39 }, { op: 0, bits: 9, val: 175 }, { op: 0, bits: 8, val: 7 }, { op: 0, bits: 8, val: 135 }, { op: 0, bits: 8, val: 71 }, { op: 0, bits: 9, val: 239 }, { op: 16, bits: 7, val: 9 }, { op: 0, bits: 8, val: 95 }, { op: 0, bits: 8, val: 31 }, { op: 0, bits: 9, val: 159 }, { op: 20, bits: 7, val: 99 }, { op: 0, bits: 8, val: 127 }, { op: 0, bits: 8, val: 63 }, { op: 0, bits: 9, val: 223 }, { op: 18, bits: 7, val: 27 }, { op: 0, bits: 8, val: 111 }, { op: 0, bits: 8, val: 47 }, { op: 0, bits: 9, val: 191 }, { op: 0, bits: 8, val: 15 }, { op: 0, bits: 8, val: 143 }, { op: 0, bits: 8, val: 79 }, { op: 0, bits: 9, val: 255 } ]; |
| 958 | if (!distfix_ary) distfix_ary = [ { op: 16, bits: 5, val: 1 }, { op: 23, bits: 5, val: 257 }, { op: 19, bits: 5, val: 17 }, { op: 27, bits: 5, val: 4097 }, { op: 17, bits: 5, val: 5 }, { op: 25, bits: 5, val: 1025 }, { op: 21, bits: 5, val: 65 }, { op: 29, bits: 5, val: 16385 }, { op: 16, bits: 5, val: 3 }, { op: 24, bits: 5, val: 513 }, { op: 20, bits: 5, val: 33 }, { op: 28, bits: 5, val: 8193 }, { op: 18, bits: 5, val: 9 }, { op: 26, bits: 5, val: 2049 }, { op: 22, bits: 5, val: 129 }, { op: 64, bits: 5, val: 0 }, { op: 16, bits: 5, val: 2 }, { op: 23, bits: 5, val: 385 }, { op: 19, bits: 5, val: 25 }, { op: 27, bits: 5, val: 6145 }, { op: 17, bits: 5, val: 7 }, { op: 25, bits: 5, val: 1537 }, { op: 21, bits: 5, val: 97 }, { op: 29, bits: 5, val: 24577 }, { op: 16, bits: 5, val: 4 }, { op: 24, bits: 5, val: 769 }, { op: 20, bits: 5, val: 49 }, { op: 28, bits: 5, val: 12289 }, { op: 18, bits: 5, val: 13 }, { op: 26, bits: 5, val: 3073 }, { op: 22, bits: 5, val: 193 }, { op: 64, bits: 5, val: 0 } ]; |
| 959 | state.lencode = 0; |
| 960 | state.distcode = 512; |
| 961 | for (i = 0; i < 512; i++) { state.codes[i] = lenfix_ary[i]; } |
| 962 | for (i = 0; i < 32; i++) { state.codes[i + 512] = distfix_ary[i]; } |
| 963 | state.lenbits = 9; |
| 964 | state.distbits = 5; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | Update the window with the last wsize (normally 32K) bytes written before |
| 969 | returning. If window does not exist yet, create it. This is only called |
| 970 | when a window is already in use, or when output has been written during this |
| 971 | inflate call, but the end of the deflate stream has not been reached yet. |
| 972 | It is also called to create a window for dictionary data when a dictionary |
| 973 | is loaded. |
| 974 | |
| 975 | Providing output buffers larger than 32K to inflate() should provide a speed |
| 976 | advantage, since only the last 32K of output is copied to the sliding window |
| 977 | upon return from inflate(), and since all distances after the first 32K of |
| 978 | output will fall in the output data, making match copies simpler and faster. |
| 979 | The advantage may be dependent on the size of the processor's data caches. |
| 980 | */ |
| 981 | function updatewindow(strm) |
| 982 | { |
| 983 | var state = strm.state; |
| 984 | var out = strm.output_data.length; |
| 985 | |
| 986 | /* if it hasn't been done already, allocate space for the window */ |
| 987 | if (state.window === null) { |
| 988 | state.window = ''; |
| 989 | } |
| 990 | |
| 991 | /* if window not in use yet, initialize */ |
| 992 | if (state.wsize == 0) { |
| 993 | state.wsize = 1 << state.wbits; |
| 994 | } |
| 995 | |
| 996 | // zlib.js: Sliding window |
| 997 | if (out >= state.wsize) { |
| 998 | state.window = strm.output_data.substring(out - state.wsize); |
| 999 | } else { |
| 1000 | if(state.whave + out < state.wsize) { |
| 1001 | state.window += strm.output_data; |
| 1002 | } else { |
| 1003 | state.window = state.window.substring(state.whave - (state.wsize - out)) + strm.output_data; |
| 1004 | } |
| 1005 | } |
| 1006 | state.whave = state.window.length; |
| 1007 | if(state.whave < state.wsize) { |
| 1008 | state.wnext = state.whave; |
| 1009 | } else { |
| 1010 | state.wnext = 0; |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | // #ifdef GUNZIP |
| 1017 | function CRC2(strm, word) |
| 1018 | { |
| 1019 | var hbuf = [word & 0xff, (word >>> 8) & 0xff]; |
| 1020 | strm.state.check = strm.checksum_function(strm.state.check, hbuf, 0, 2); |
| 1021 | } |
| 1022 | |
| 1023 | function CRC4(strm, word) |
| 1024 | { |
| 1025 | var hbuf = [word & 0xff, |
| 1026 | (word >>> 8) & 0xff, |
| 1027 | (word >>> 16) & 0xff, |
| 1028 | (word >>> 24) & 0xff]; |
| 1029 | strm.state.check = strm.checksum_function(strm.state.check, hbuf, 0, 4); |
| 1030 | } |
| 1031 | |
| 1032 | /* Load registers with state in inflate() for speed */ |
| 1033 | function LOAD(strm, s) |
| 1034 | { |
| 1035 | s.strm = strm; /* z_stream */ |
| 1036 | s.left = strm.avail_out; /* available output */ |
| 1037 | s.next = strm.next_in; /* next input */ |
| 1038 | s.have = strm.avail_in; /* available input */ |
| 1039 | s.hold = strm.state.hold; /* bit buffer */ |
| 1040 | s.bits = strm.state.bits; /* bits in bit buffer */ |
| 1041 | return s; |
| 1042 | } |
| 1043 | |
| 1044 | /* Restore state from registers in inflate() */ |
| 1045 | function RESTORE(s) |
| 1046 | { |
| 1047 | var strm = s.strm; |
| 1048 | strm.next_in = s.next; |
| 1049 | strm.avail_out = s.left; |
| 1050 | strm.avail_in = s.have; |
| 1051 | strm.state.hold = s.hold; |
| 1052 | strm.state.bits = s.bits; |
| 1053 | } |
| 1054 | |
| 1055 | /* Clear the input bit accumulator */ |
| 1056 | function INITBITS(s) |
| 1057 | { |
| 1058 | s.hold = 0; |
| 1059 | s.bits = 0; |
| 1060 | } |
| 1061 | |
| 1062 | /* Get a byte of input into the bit accumulator, or return from inflate() |
| 1063 | if there is no input available. */ |
| 1064 | function PULLBYTE(s) |
| 1065 | { |
| 1066 | if (s.have == 0) return false; |
| 1067 | s.have--; |
| 1068 | s.hold += (s.strm.input_data.charCodeAt(s.next++) & 0xff) << s.bits; |
| 1069 | s.bits += 8; |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | /* Assure that there are at least n bits in the bit accumulator. If there is |
| 1074 | not enough available input to do that, then return from inflate(). */ |
| 1075 | function NEEDBITS(s, n) |
| 1076 | { |
| 1077 | // if(typeof n != 'number') throw 'ERROR'; |
| 1078 | while (s.bits < n) { |
| 1079 | if(!PULLBYTE(s)) |
| 1080 | return false; |
| 1081 | } |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | /* Return the low n bits of the bit accumulator (n < 16) */ |
| 1086 | function BITS(s, n) |
| 1087 | { |
| 1088 | return s.hold & ((1 << n) - 1); |
| 1089 | } |
| 1090 | |
| 1091 | /* Remove n bits from the bit accumulator */ |
| 1092 | function DROPBITS(s, n) |
| 1093 | { |
| 1094 | // if(typeof n != 'number') throw 'ERROR'; |
| 1095 | s.hold >>>= n; |
| 1096 | s.bits -= n; |
| 1097 | } |
| 1098 | |
| 1099 | /* Remove zero to seven bits as needed to go to a byte boundary */ |
| 1100 | function BYTEBITS(s) |
| 1101 | { |
| 1102 | s.hold >>>= s.bits & 7; |
| 1103 | s.bits -= s.bits & 7; |
| 1104 | } |
| 1105 | |
| 1106 | /* Reverse the bytes in a 32-bit value */ |
| 1107 | function REVERSE(q) |
| 1108 | { |
| 1109 | return ((q >>> 24) & 0xff) + |
| 1110 | ((q >>> 8) & 0xff00) + |
| 1111 | ((q & 0xff00) << 8) + |
| 1112 | ((q & 0xff) << 24); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | inflate() uses a state machine to process as much input data and generate as |
| 1117 | much output data as possible before returning. The state machine is |
| 1118 | structured roughly as follows: |
| 1119 | |
| 1120 | for (;;) switch (state) { |
| 1121 | ... |
| 1122 | case STATEn: |
| 1123 | if (not enough input data or output space to make progress) |
| 1124 | return; |
| 1125 | ... make progress ... |
| 1126 | state = STATEm; |
| 1127 | break; |
| 1128 | ... |
| 1129 | } |
| 1130 | |
| 1131 | so when inflate() is called again, the same case is attempted again, and |
| 1132 | if the appropriate resources are provided, the machine proceeds to the |
| 1133 | next state. The NEEDBITS() macro is usually the way the state evaluates |
| 1134 | whether it can proceed or should return. NEEDBITS() does the return if |
| 1135 | the requested bits are not available. The typical use of the BITS macros |
| 1136 | is: |
| 1137 | |
| 1138 | NEEDBITS(n); |
| 1139 | ... do something with BITS(n) ... |
| 1140 | DROPBITS(n); |
| 1141 | |
| 1142 | where NEEDBITS(n) either returns from inflate() if there isn't enough |
| 1143 | input left to load n bits into the accumulator, or it continues. BITS(n) |
| 1144 | gives the low n bits in the accumulator. When done, DROPBITS(n) drops |
| 1145 | the low n bits off the accumulator. INITBITS() clears the accumulator |
| 1146 | and sets the number of available bits to zero. BYTEBITS() discards just |
| 1147 | enough bits to put the accumulator on a byte boundary. After BYTEBITS() |
| 1148 | and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. |
| 1149 | |
| 1150 | NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return |
| 1151 | if there is no input available. The decoding of variable length codes uses |
| 1152 | PULLBYTE() directly in order to pull just enough bytes to decode the next |
| 1153 | code, and no more. |
| 1154 | |
| 1155 | Some states loop until they get enough input, making sure that enough |
| 1156 | state information is maintained to continue the loop where it left off |
| 1157 | if NEEDBITS() returns in the loop. For example, want, need, and keep |
| 1158 | would all have to actually be part of the saved state in case NEEDBITS() |
| 1159 | returns: |
| 1160 | |
| 1161 | case STATEw: |
| 1162 | while (want < need) { |
| 1163 | NEEDBITS(n); |
| 1164 | keep[want++] = BITS(n); |
| 1165 | DROPBITS(n); |
| 1166 | } |
| 1167 | state = STATEx; |
| 1168 | case STATEx: |
| 1169 | |
| 1170 | As shown above, if the next state is also the next case, then the break |
| 1171 | is omitted. |
| 1172 | |
| 1173 | A state may also return if there is not enough output space available to |
| 1174 | complete that state. Those states are copying stored data, writing a |
| 1175 | literal byte, and copying a matching string. |
| 1176 | |
| 1177 | When returning, a "goto inf_leave" is used to update the total counters, |
| 1178 | update the check value, and determine whether any progress has been made |
| 1179 | during that inflate() call in order to return the proper return code. |
| 1180 | Progress is defined as a change in either strm->avail_in or strm->avail_out. |
| 1181 | When there is a window, goto inf_leave will update the window with the last |
| 1182 | output written. If a goto inf_leave occurs in the middle of decompression |
| 1183 | and there is no window currently, goto inf_leave will create one and copy |
| 1184 | output to the window for the next call of inflate(). |
| 1185 | |
| 1186 | In this implementation, the flush parameter of inflate() only affects the |
| 1187 | return code (per zlib.h). inflate() always writes as much as possible to |
| 1188 | strm->next_out, given the space available and the provided input--the effect |
| 1189 | documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers |
| 1190 | the allocation of and copying into a sliding window until necessary, which |
| 1191 | provides the effect documented in zlib.h for Z_FINISH when the entire input |
| 1192 | stream available. So the only thing the flush parameter actually does is: |
| 1193 | when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
| 1194 | will return Z_BUF_ERROR if it has not reached the end of the stream. |
| 1195 | */ |
| 1196 | |
| 1197 | /* permutation of code lengths */ |
| 1198 | var inflate_order = [ |
| 1199 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; |
| 1200 | ZLIB.inflate = function(strm, flush) |
| 1201 | { |
| 1202 | var state; |
| 1203 | var s; |
| 1204 | var _in, out; /* save starting available input and output */ |
| 1205 | var copy; /* number of stored or match bytes to copy */ |
| 1206 | var from_window_offset = -1; /* index of window[] */ |
| 1207 | var from_out_offset = -1; /* index of next_out[] */ |
| 1208 | var here; /* current decoding table entry */ |
| 1209 | var last; /* parent table entry */ |
| 1210 | var len; /* length to copy for repeats, bits to drop */ |
| 1211 | var ret; /* return code */ |
| 1212 | |
| 1213 | if (!strm || !strm.state || |
| 1214 | (!strm.input_data && strm.avail_in != 0)) |
| 1215 | return ZLIB.Z_STREAM_ERROR; |
| 1216 | |
| 1217 | state = strm.state; |
| 1218 | if (state.mode == TYPE) state.mode = TYPEDO; /* skip check */ |
| 1219 | |
| 1220 | // LOAD |
| 1221 | s = {}; |
| 1222 | LOAD(strm, s); |
| 1223 | |
| 1224 | _in = s.have; |
| 1225 | out = s.left; |
| 1226 | ret = ZLIB.Z_OK; |
| 1227 | inf_leave: for (;;) { |
| 1228 | switch (state.mode) { |
| 1229 | case HEAD: |
| 1230 | if (state.wrap == 0) { |
| 1231 | state.mode = TYPEDO; |
| 1232 | break; |
| 1233 | } |
| 1234 | if(!NEEDBITS(s, 16)) break inf_leave; |
| 1235 | // #ifdef GUNZIP |
| 1236 | if ((state.wrap & 2) && s.hold == 0x8b1f) { /* gzip header */ |
| 1237 | state.check = strm.checksum_function(0, null, 0, 0); |
| 1238 | CRC2(strm, s.hold); |
| 1239 | INITBITS(s); |
| 1240 | state.mode = FLAGS; |
| 1241 | break; |
| 1242 | } |
| 1243 | state.flags = 0; /* expect zlib header */ |
| 1244 | if (state.head !== null) |
| 1245 | state.head.done = -1; |
| 1246 | if (!(state.wrap & 1) || /* check if zlib header allowed */ |
| 1247 | //#else |
| 1248 | // if ( |
| 1249 | //#endif |
| 1250 | ((BITS(s, 8) << 8) + (s.hold >>> 8)) % 31) { |
| 1251 | strm.msg = 'incorrect header check'; |
| 1252 | state.mode = BAD; |
| 1253 | break; |
| 1254 | } |
| 1255 | if (BITS(s, 4) != ZLIB.Z_DEFLATED) { |
| 1256 | strm.msg = 'unknown compression method'; |
| 1257 | state.mode = BAD; |
| 1258 | break; |
| 1259 | } |
| 1260 | |
| 1261 | DROPBITS(s, 4); |
| 1262 | len = BITS(s, 4) + 8; |
| 1263 | if (state.wbits == 0) |
| 1264 | state.wbits = len; |
| 1265 | else if (len > state.wbits) { |
| 1266 | strm.msg = 'invalid window size'; |
| 1267 | state.mode = BAD; |
| 1268 | break; |
| 1269 | } |
| 1270 | state.dmax = 1 << len; |
| 1271 | // Tracev((stderr, "inflate: zlib header ok\n")); |
| 1272 | strm.adler = state.check = strm.checksum_function(0, null, 0, 0); |
| 1273 | state.mode = s.hold & 0x200 ? DICTID : TYPE; |
| 1274 | INITBITS(s); |
| 1275 | break; |
| 1276 | // #ifdef GUNZIP |
| 1277 | case FLAGS: |
| 1278 | if(!NEEDBITS(s, 16)) break inf_leave; |
| 1279 | state.flags = s.hold; |
| 1280 | if ((state.flags & 0xff) != ZLIB.Z_DEFLATED) { |
| 1281 | strm.msg = "unknown compression method"; |
| 1282 | state.mode = BAD; |
| 1283 | break; |
| 1284 | } |
| 1285 | if (state.flags & 0xe000) { |
| 1286 | strm.msg = "unknown header flags set"; |
| 1287 | state.mode = BAD; |
| 1288 | break; |
| 1289 | } |
| 1290 | if (state.head !== null) |
| 1291 | state.head.text = (s.hold >>> 8) & 1; |
| 1292 | if (state.flags & 0x0200) { |
| 1293 | CRC2(strm, s.hold); |
| 1294 | } |
| 1295 | INITBITS(s); |
| 1296 | state.mode = TIME; |
| 1297 | case TIME: |
| 1298 | if(!NEEDBITS(s, 32)) break inf_leave; |
| 1299 | if (state.head !== null) |
| 1300 | state.head.time = s.hold; |
| 1301 | if (state.flags & 0x0200) { |
| 1302 | CRC4(strm, s.hold); |
| 1303 | } |
| 1304 | INITBITS(s); |
| 1305 | state.mode = OS; |
| 1306 | case OS: |
| 1307 | if(!NEEDBITS(s, 16)) break inf_leave; |
| 1308 | if (state.head !== null) { |
| 1309 | state.head.xflags = s.hold & 0xff; |
| 1310 | state.head.os = s.hold >>> 8; |
| 1311 | } |
| 1312 | if (state.flags & 0x0200) { |
| 1313 | CRC2(strm, s.hold); |
| 1314 | } |
| 1315 | INITBITS(s); |
| 1316 | state.mode = EXLEN; |
| 1317 | case EXLEN: |
| 1318 | if (state.flags & 0x0400) { |
| 1319 | if(!NEEDBITS(s, 16)) break inf_leave; |
| 1320 | state.length = s.hold; |
| 1321 | if (state.head !== null) { |
| 1322 | state.head.extra_len = s.hold; |
| 1323 | } |
| 1324 | if (state.flags & 0x0200) { |
| 1325 | CRC2(strm, s.hold); |
| 1326 | } |
| 1327 | INITBITS(s); |
| 1328 | state.head.extra = ""; |
| 1329 | } |
| 1330 | else if (state.head !== null) { |
| 1331 | state.head.extra = null; |
| 1332 | } |
| 1333 | state.mode = EXTRA; |
| 1334 | case EXTRA: |
| 1335 | if (state.flags & 0x0400) { |
| 1336 | copy = state.length; |
| 1337 | if (copy > s.have) copy = s.have; |
| 1338 | if (copy) { |
| 1339 | if (state.head !== null && |
| 1340 | state.head.extra !== null) { |
| 1341 | len = state.head.extra_len - state.length; |
| 1342 | /* |
| 1343 | zmemcpy(state->head->extra + len, next, |
| 1344 | len + copy > state->head->extra_max ? |
| 1345 | state->head->extra_max - len : copy); |
| 1346 | */ |
| 1347 | state.head.extra += strm.input_data.substring( |
| 1348 | s.next, s.next + (len + copy > state.head.extra_max ? |
| 1349 | state.head.extra_max - len : copy)); |
| 1350 | |
| 1351 | } |
| 1352 | if (state.flags & 0x0200) |
| 1353 | state.check = strm.checksum_function(state.check, strm.input_data, s.next, copy); |
| 1354 | s.have -= copy; |
| 1355 | s.next += copy; |
| 1356 | state.length -= copy; |
| 1357 | } |
| 1358 | if (state.length) break inf_leave; |
| 1359 | } |
| 1360 | state.length = 0; |
| 1361 | state.mode = NAME; |
| 1362 | case NAME: |
| 1363 | if (state.flags & 0x0800) { |
| 1364 | if (s.have == 0) break inf_leave; |
| 1365 | if (state.head !== null && state.head.name === null) { |
| 1366 | state.head.name = ""; |
| 1367 | } |
| 1368 | copy = 0; |
| 1369 | // TODO end = strm.input_data.indexOf("\0", s.next); |
| 1370 | // TODO state.length => state.head.name.length |
| 1371 | do { |
| 1372 | len = strm.input_data.charAt(s.next + copy); copy++; |
| 1373 | if(len === "\0") |
| 1374 | break; |
| 1375 | if (state.head !== null && |
| 1376 | state.length < state.head.name_max) { |
| 1377 | state.head.name += len; |
| 1378 | state.length++; |
| 1379 | } |
| 1380 | } while (copy < s.have); |
| 1381 | if (state.flags & 0x0200) { |
| 1382 | state.check = strm.checksum_function(state.check, strm.input_data, s.next, copy); |
| 1383 | } |
| 1384 | s.have -= copy; |
| 1385 | s.next += copy; |
| 1386 | if (len !== "\0") break inf_leave; |
| 1387 | } |
| 1388 | else if (state.head !== null) |
| 1389 | state.head.name = null; |
| 1390 | state.length = 0; |
| 1391 | state.mode = COMMENT; |
| 1392 | case COMMENT: |
| 1393 | if (state.flags & 0x1000) { |
| 1394 | if (s.have == 0) break inf_leave; |
| 1395 | copy = 0; |
| 1396 | if (state.head !== null && state.head.comment === null) { |
| 1397 | state.head.comment = ""; |
| 1398 | } |
| 1399 | // TODO end = strm.input_data.indexOf("\0", s.next); |
| 1400 | // TODO state.length => state.head.comment.length |
| 1401 | do { |
| 1402 | len = strm.input_data.charAt(s.next + copy); copy++; |
| 1403 | if(len === "\0") |
| 1404 | break; |
| 1405 | if (state.head !== null && |
| 1406 | state.length < state.head.comm_max) { |
| 1407 | state.head.comment += len; |
| 1408 | state.length++; |
| 1409 | } |
| 1410 | } while (copy < s.have); |
| 1411 | if (state.flags & 0x0200) |
| 1412 | state.check = strm.checksum_function(state.check, strm.input_data, s.next, copy); |
| 1413 | s.have -= copy; |
| 1414 | s.next += copy; |
| 1415 | if (len !== "\0") break inf_leave; |
| 1416 | } |
| 1417 | else if (state.head !== null) |
| 1418 | state.head.comment = null; |
| 1419 | state.mode = HCRC; |
| 1420 | case HCRC: |
| 1421 | if (state.flags & 0x0200) { |
| 1422 | if(!NEEDBITS(s, 16)) break inf_leave; |
| 1423 | if (s.hold != (state.check & 0xffff)) { |
| 1424 | strm.msg = "header crc mismatch"; |
| 1425 | state.mode = BAD; |
| 1426 | break; |
| 1427 | } |
| 1428 | INITBITS(s); |
| 1429 | } |
| 1430 | if (state.head !== null) { |
| 1431 | state.head.hcrc = (state.flags >>> 9) & 1; |
| 1432 | state.head.done = 1; |
| 1433 | } |
| 1434 | strm.adler = state.check = strm.checksum_function(0, null, 0, 0); |
| 1435 | state.mode = TYPE; |
| 1436 | break; |
| 1437 | //#endif |
| 1438 | case DICTID: |
| 1439 | if(!NEEDBITS(s, 32)) break inf_leave; |
| 1440 | strm.adler = state.check = REVERSE(s.hold); |
| 1441 | INITBITS(s); |
| 1442 | state.mode = DICT; |
| 1443 | case DICT: |
| 1444 | if (state.havedict == 0) { |
| 1445 | RESTORE(s); |
| 1446 | return ZLIB.Z_NEED_DICT; |
| 1447 | } |
| 1448 | strm.adler = state.check = strm.checksum_function(0, null, 0, 0); |
| 1449 | state.mode = TYPE; |
| 1450 | case TYPE: |
| 1451 | if (flush == ZLIB.Z_BLOCK || flush == ZLIB.Z_TREES) break inf_leave; |
| 1452 | case TYPEDO: |
| 1453 | if (state.last) { |
| 1454 | BYTEBITS(s); |
| 1455 | state.mode = CHECK; |
| 1456 | break; |
| 1457 | } |
| 1458 | if(!NEEDBITS(s, 3)) break inf_leave; |
| 1459 | state.last = BITS(s, 1); |
| 1460 | DROPBITS(s, 1); |
| 1461 | switch (BITS(s, 2)) { |
| 1462 | case 0: /* stored block */ |
| 1463 | // Tracev((stderr, "inflate: stored block%s\n", |
| 1464 | // state->last ? " (last)" : "")); |
| 1465 | state.mode = STORED; |
| 1466 | break; |
| 1467 | case 1: /* fixed block */ |
| 1468 | fixedtables(state); |
| 1469 | // Tracev((stderr, "inflate: fixed codes block%s\n", |
| 1470 | // state->last ? " (last)" : "")); |
| 1471 | state.mode = LEN_; /* decode codes */ |
| 1472 | if (flush == ZLIB.Z_TREES) { |
| 1473 | DROPBITS(s, 2); |
| 1474 | break inf_leave; |
| 1475 | } |
| 1476 | break; |
| 1477 | case 2: /* dynamic block */ |
| 1478 | // Tracev((stderr, "inflate: dynamic codes block%s\n", |
| 1479 | // state->last ? " (last)" : "")); |
| 1480 | state.mode = TABLE; |
| 1481 | break; |
| 1482 | case 3: |
| 1483 | strm.msg = 'invalid block type'; |
| 1484 | state.mode = BAD; |
| 1485 | } |
| 1486 | DROPBITS(s, 2); |
| 1487 | break; |
| 1488 | case STORED: |
| 1489 | BYTEBITS(s); /* go to byte boundary */ |
| 1490 | if(!NEEDBITS(s, 32)) break inf_leave; |
| 1491 | if ((s.hold & 0xffff) != (((s.hold >>> 16) & 0xffff) ^ 0xffff)) { |
| 1492 | strm.msg = 'invalid stored block lengths'; |
| 1493 | state.mode = BAD; |
| 1494 | break; |
| 1495 | } |
| 1496 | state.length = s.hold & 0xffff; |
| 1497 | // Tracev((stderr, "inflate: stored length %u\n", |
| 1498 | // state->length)); |
| 1499 | INITBITS(s); |
| 1500 | state.mode = COPY_; |
| 1501 | if (flush == ZLIB.Z_TREES) break inf_leave; |
| 1502 | case COPY_: |
| 1503 | state.mode = COPY; |
| 1504 | case COPY: |
| 1505 | copy = state.length; |
| 1506 | if (copy) { |
| 1507 | if (copy > s.have) copy = s.have; |
| 1508 | if (copy > s.left) copy = s.left; |
| 1509 | if (copy == 0) break inf_leave; |
| 1510 | strm.output_data += strm.input_data.substring(s.next, s.next + copy); |
| 1511 | strm.next_out += copy; |
| 1512 | s.have -= copy; |
| 1513 | s.next += copy; |
| 1514 | s.left -= copy; |
| 1515 | state.length -= copy; |
| 1516 | break; |
| 1517 | } |
| 1518 | // Tracev((stderr, "inflate: stored end\n")); |
| 1519 | state.mode = TYPE; |
| 1520 | break; |
| 1521 | case TABLE: |
| 1522 | if(!NEEDBITS(s, 14)) break inf_leave; |
| 1523 | state.nlen = BITS(s, 5) + 257; |
| 1524 | DROPBITS(s, 5); |
| 1525 | state.ndist = BITS(s, 5) + 1; |
| 1526 | DROPBITS(s, 5); |
| 1527 | state.ncode = BITS(s, 4) + 4; |
| 1528 | DROPBITS(s, 4); |
| 1529 | //#ifndef PKZIP_BUG_WORKAROUND |
| 1530 | if (state.nlen > 286 || state.ndist > 30) { |
| 1531 | strm.msg = 'too many length or distance symbols'; |
| 1532 | state.mode = BAD; |
| 1533 | break; |
| 1534 | } |
| 1535 | //#endif |
| 1536 | // Tracev((stderr, "inflate: table sizes ok\n")); |
| 1537 | state.have = 0; |
| 1538 | state.mode = LENLENS; |
| 1539 | case LENLENS: |
| 1540 | while (state.have < state.ncode) { |
| 1541 | if(!NEEDBITS(s, 3)) break inf_leave; |
| 1542 | var tmp = BITS(s, 3); |
| 1543 | state.lens[inflate_order[state.have++]] = tmp; |
| 1544 | DROPBITS(s, 3); |
| 1545 | } |
| 1546 | while (state.have < 19) |
| 1547 | state.lens[inflate_order[state.have++]] = 0; |
| 1548 | state.next = 0; |
| 1549 | state.lencode = 0; |
| 1550 | state.lenbits = 7; |
| 1551 | |
| 1552 | // ret = inflate_table(CODES, state->lens, 19, &(state->next), |
| 1553 | // &(state->lenbits), state->work); |
| 1554 | ret = inflate_table(state, CODES); |
| 1555 | |
| 1556 | if (ret) { |
| 1557 | strm.msg = 'invalid code lengths set'; |
| 1558 | state.mode = BAD; |
| 1559 | break; |
| 1560 | } |
| 1561 | // Tracev((stderr, "inflate: code lengths ok\n")); |
| 1562 | state.have = 0; |
| 1563 | state.mode = CODELENS; |
| 1564 | case CODELENS: |
| 1565 | while (state.have < state.nlen + state.ndist) { |
| 1566 | for (;;) { |
| 1567 | here = state.codes[state.lencode + BITS(s, state.lenbits)]; |
| 1568 | if (here.bits <= s.bits) break; |
| 1569 | if(!PULLBYTE(s)) break inf_leave; |
| 1570 | } |
| 1571 | if (here.val < 16) { |
| 1572 | DROPBITS(s, here.bits); |
| 1573 | state.lens[state.have++] = here.val; |
| 1574 | } |
| 1575 | else { |
| 1576 | if (here.val == 16) { |
| 1577 | if(!NEEDBITS(s, here.bits + 2)) break inf_leave; |
| 1578 | DROPBITS(s, here.bits); |
| 1579 | if (state.have == 0) { |
| 1580 | strm.msg = 'invalid bit length repeat'; |
| 1581 | state.mode = BAD; |
| 1582 | break; |
| 1583 | } |
| 1584 | len = state.lens[state.have - 1]; |
| 1585 | copy = 3 + BITS(s, 2); |
| 1586 | DROPBITS(s, 2); |
| 1587 | } |
| 1588 | else if (here.val == 17) { |
| 1589 | if(!NEEDBITS(s, here.bits + 3)) break inf_leave; |
| 1590 | DROPBITS(s, here.bits); |
| 1591 | len = 0; |
| 1592 | copy = 3 + BITS(s, 3); |
| 1593 | DROPBITS(s, 3); |
| 1594 | } |
| 1595 | else { |
| 1596 | if(!NEEDBITS(s, here.bits + 7)) break inf_leave; |
| 1597 | DROPBITS(s, here.bits); |
| 1598 | len = 0; |
| 1599 | copy = 11 + BITS(s, 7); |
| 1600 | DROPBITS(s, 7); |
| 1601 | } |
| 1602 | if (state.have + copy > state.nlen + state.ndist) { |
| 1603 | strm.msg = 'invalid bit length repeat'; |
| 1604 | state.mode = BAD; |
| 1605 | break; |
| 1606 | } |
| 1607 | while (copy--) |
| 1608 | state.lens[state.have++] = len; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /* handle error breaks in while */ |
| 1613 | if (state.mode == BAD) break; |
| 1614 | |
| 1615 | /* check for end-of-block code (better have one) */ |
| 1616 | if (state.lens[256] == 0) { |
| 1617 | strm.msg = 'invalid code -- missing end-of-block'; |
| 1618 | state.mode = BAD; |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | /* build code tables -- note: do not change the lenbits or distbits |
| 1623 | values here (9 and 6) without reading the comments in inftrees.h |
| 1624 | concerning the ENOUGH constants, which depend on those values */ |
| 1625 | state.next = 0; |
| 1626 | state.lencode = state.next; |
| 1627 | state.lenbits = 9; |
| 1628 | // ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
| 1629 | // &(state->lenbits), state->work); |
| 1630 | ret = inflate_table(state, LENS); |
| 1631 | if (ret) { |
| 1632 | strm.msg = 'invalid literal/lengths set'; |
| 1633 | state.mode = BAD; |
| 1634 | break; |
| 1635 | } |
| 1636 | state.distcode = state.next; |
| 1637 | state.distbits = 6; |
| 1638 | // ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, &(state->next), |
| 1639 | // &(state->distbits), state->work); |
| 1640 | ret = inflate_table(state, DISTS); |
| 1641 | if (ret) { |
| 1642 | strm.msg = 'invalid distances set'; |
| 1643 | state.mode = BAD; |
| 1644 | break; |
| 1645 | } |
| 1646 | // Tracev((stderr, "inflate: codes ok\n")); |
| 1647 | state.mode = LEN_; |
| 1648 | if (flush == ZLIB.Z_TREES) break inf_leave; |
| 1649 | case LEN_: |
| 1650 | state.mode = LEN; |
| 1651 | case LEN: |
| 1652 | if (s.have >= 6 && s.left >= 258) { |
| 1653 | RESTORE(s); |
| 1654 | inflate_fast(strm, out); |
| 1655 | LOAD(strm, s); |
| 1656 | if (state.mode == TYPE) |
| 1657 | state.back = -1; |
| 1658 | break; |
| 1659 | } |
| 1660 | state.back = 0; |
| 1661 | for (;;) { |
| 1662 | here = state.codes[state.lencode + BITS(s, state.lenbits)]; |
| 1663 | if (here.bits <= s.bits) break; |
| 1664 | if(!PULLBYTE(s)) break inf_leave; |
| 1665 | } |
| 1666 | if (here.op && (here.op & 0xf0) == 0) { |
| 1667 | last = here; |
| 1668 | for (;;) { |
| 1669 | here = state.codes[state.lencode + last.val + |
| 1670 | (BITS(s, last.bits + last.op) >>> last.bits)]; |
| 1671 | if (last.bits + here.bits <= s.bits) break; |
| 1672 | if(!PULLBYTE(s)) break inf_leave; |
| 1673 | } |
| 1674 | DROPBITS(s, last.bits); |
| 1675 | state.back += last.bits; |
| 1676 | } |
| 1677 | DROPBITS(s, here.bits); |
| 1678 | state.back += here.bits; |
| 1679 | state.length = here.val; |
| 1680 | if (here.op == 0) { |
| 1681 | // Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
| 1682 | // "inflate: literal '%c'\n" : |
| 1683 | // "inflate: literal 0x%02x\n", here.val)); |
| 1684 | state.mode = LIT; |
| 1685 | break; |
| 1686 | } |
| 1687 | if (here.op & 32) { |
| 1688 | // Tracevv((stderr, "inflate: end of block\n")); |
| 1689 | state.back = -1; |
| 1690 | state.mode = TYPE; |
| 1691 | break; |
| 1692 | } |
| 1693 | if (here.op & 64) { |
| 1694 | strm.msg = 'invalid literal/length code'; |
| 1695 | state.mode = BAD; |
| 1696 | break; |
| 1697 | } |
| 1698 | state.extra = here.op & 15; |
| 1699 | state.mode = LENEXT; |
| 1700 | case LENEXT: |
| 1701 | if (state.extra) { |
| 1702 | if(!NEEDBITS(s, state.extra)) break inf_leave; |
| 1703 | state.length += BITS(s, state.extra); |
| 1704 | DROPBITS(s, state.extra); |
| 1705 | state.back += state.extra; |
| 1706 | } |
| 1707 | //Tracevv((stderr, "inflate: length %u\n", state->length)); |
| 1708 | state.was = state.length; |
| 1709 | state.mode = DIST; |
| 1710 | case DIST: |
| 1711 | for (;;) { |
| 1712 | here = state.codes[state.distcode + BITS(s, state.distbits)]; |
| 1713 | if (here.bits <= s.bits) break; |
| 1714 | if(!PULLBYTE(s)) break inf_leave; |
| 1715 | } |
| 1716 | if ((here.op & 0xf0) == 0) { |
| 1717 | last = here; |
| 1718 | for (;;) { |
| 1719 | here = state.codes[state.distcode + last.val + |
| 1720 | (BITS(s, last.bits + last.op) >>> last.bits)]; |
| 1721 | if ((last.bits + here.bits) <= s.bits) break; |
| 1722 | if(!PULLBYTE(s)) break inf_leave; |
| 1723 | } |
| 1724 | DROPBITS(s, last.bits); |
| 1725 | state.back += last.bits; |
| 1726 | } |
| 1727 | DROPBITS(s, here.bits); |
| 1728 | state.back += here.bits; |
| 1729 | if (here.op & 64) { |
| 1730 | strm.msg = 'invalid distance code'; |
| 1731 | state.mode = BAD; |
| 1732 | break; |
| 1733 | } |
| 1734 | state.offset = here.val; |
| 1735 | state.extra = here.op & 15; |
| 1736 | state.mode = DISTEXT; |
| 1737 | case DISTEXT: |
| 1738 | if (state.extra) { |
| 1739 | if(!NEEDBITS(s, state.extra)) break inf_leave; |
| 1740 | state.offset += BITS(s, state.extra); |
| 1741 | DROPBITS(s, state.extra); |
| 1742 | state.back += state.extra; |
| 1743 | } |
| 1744 | //NOSPRT #ifdef INFLATE_STRICT |
| 1745 | // if (state->offset > state->dmax) { |
| 1746 | // strm->msg = (char *)"invalid distance too far back"; |
| 1747 | // state->mode = BAD; |
| 1748 | // break; |
| 1749 | // } |
| 1750 | //#endif |
| 1751 | // Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
| 1752 | state.mode = MATCH; |
| 1753 | case MATCH: |
| 1754 | if (s.left == 0) break inf_leave; |
| 1755 | copy = out - s.left; |
| 1756 | if (state.offset > copy) { /* copy from window */ |
| 1757 | copy = state.offset - copy; |
| 1758 | if (copy > state.whave) { |
| 1759 | if (state.sane) { |
| 1760 | strm.msg = 'invalid distance too far back'; |
| 1761 | state.mode = BAD; |
| 1762 | break; |
| 1763 | } |
| 1764 | //NOSPRT #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
| 1765 | // Trace((stderr, "inflate.c too far\n")); |
| 1766 | // copy -= state->whave; |
| 1767 | // if (copy > state->length) copy = state->length; |
| 1768 | // if (copy > left) copy = left; |
| 1769 | // left -= copy; |
| 1770 | // state->length -= copy; |
| 1771 | // do { |
| 1772 | // *put++ = 0; |
| 1773 | // } while (--copy); |
| 1774 | // if (state->length == 0) state->mode = LEN; |
| 1775 | // break; |
| 1776 | //#endif |
| 1777 | } |
| 1778 | if (copy > state.wnext) { |
| 1779 | copy -= state.wnext; |
| 1780 | // from = state->window + (state->wsize - copy); |
| 1781 | from_window_offset = state.wsize - copy; |
| 1782 | from_out_offset = -1; |
| 1783 | } |
| 1784 | else { |
| 1785 | // from = state->window + (state->wnext - copy); |
| 1786 | from_window_offset = state.wnext - copy; |
| 1787 | from_out_offset = -1; |
| 1788 | } |
| 1789 | if (copy > state.length) copy = state.length; |
| 1790 | } |
| 1791 | else { /* copy from output */ |
| 1792 | // from = put - state->offset; |
| 1793 | from_window_offset = -1; |
| 1794 | from_out_offset = strm.next_out - state.offset; |
| 1795 | copy = state.length; |
| 1796 | } |
| 1797 | if (copy > s.left) copy = s.left; |
| 1798 | s.left -= copy; |
| 1799 | state.length -= copy; |
| 1800 | if( from_window_offset >= 0 ) { |
| 1801 | strm.output_data += state.window.substring(from_window_offset, from_window_offset + copy); |
| 1802 | strm.next_out += copy; |
| 1803 | copy = 0; |
| 1804 | } else { |
| 1805 | strm.next_out += copy; |
| 1806 | do { |
| 1807 | strm.output_data += strm.output_data.charAt(from_out_offset++); |
| 1808 | } while (--copy); |
| 1809 | } |
| 1810 | if (state.length == 0) state.mode = LEN; |
| 1811 | break; |
| 1812 | case LIT: |
| 1813 | if (s.left == 0) break inf_leave; |
| 1814 | |
| 1815 | strm.output_data += String.fromCharCode(state.length); |
| 1816 | strm.next_out++; |
| 1817 | //*put++ = (unsigned char)(state->length); |
| 1818 | |
| 1819 | s.left--; |
| 1820 | state.mode = LEN; |
| 1821 | break; |
| 1822 | case CHECK: |
| 1823 | if (state.wrap) { |
| 1824 | if(!NEEDBITS(s, 32)) break inf_leave; |
| 1825 | out -= s.left; |
| 1826 | strm.total_out += out; |
| 1827 | state.total += out; |
| 1828 | if (out) |
| 1829 | strm.adler = state.check = |
| 1830 | strm.checksum_function(state.check, strm.output_data, strm.output_data.length - out, out); |
| 1831 | out = s.left; |
| 1832 | if (( |
| 1833 | // #ifdef GUNZIP |
| 1834 | state.flags ? s.hold : |
| 1835 | //#endif |
| 1836 | REVERSE(s.hold)) != state.check) { |
| 1837 | strm.msg = "incorrect data check"; |
| 1838 | state.mode = BAD; |
| 1839 | break; |
| 1840 | } |
| 1841 | INITBITS(s); |
| 1842 | //debug("## inflate: check matches trailer\n"); |
| 1843 | // Tracev((stderr, "inflate: check matches trailer\n")); |
| 1844 | } |
| 1845 | //#ifdef GUNZIP |
| 1846 | state.mode = LENGTH; |
| 1847 | case LENGTH: |
| 1848 | if (state.wrap && state.flags) { |
| 1849 | if(!NEEDBITS(s, 32)) break inf_leave; |
| 1850 | if (s.hold != (state.total & 0xffffffff)) { |
| 1851 | strm.msg = 'incorrect length check'; |
| 1852 | state.mode = BAD; |
| 1853 | break; |
| 1854 | } |
| 1855 | INITBITS(s); |
| 1856 | //Tracev((stderr, "inflate: length matches trailer\n")); |
| 1857 | } |
| 1858 | //#endif |
| 1859 | state.mode = DONE; |
| 1860 | case DONE: |
| 1861 | ret = ZLIB.Z_STREAM_END; |
| 1862 | break inf_leave; |
| 1863 | case BAD: |
| 1864 | ret = ZLIB.Z_DATA_ERROR; |
| 1865 | break inf_leave; |
| 1866 | case MEM: |
| 1867 | return ZLIB.Z_MEM_ERROR; |
| 1868 | case SYNC: |
| 1869 | default: |
| 1870 | return ZLIB.Z_STREAM_ERROR; |
| 1871 | } } |
| 1872 | |
| 1873 | /* |
| 1874 | Return from inflate(), updating the total counts and the check value. |
| 1875 | If there was no progress during the inflate() call, return a buffer |
| 1876 | error. Call updatewindow() to create and/or update the window state. |
| 1877 | Note: a memory error from inflate() is non-recoverable. |
| 1878 | */ |
| 1879 | inf_leave: |
| 1880 | RESTORE(s); |
| 1881 | if (state.wsize || (out != strm.avail_out && state.mode < BAD && |
| 1882 | (state.mode < CHECK || flush != ZLIB.Z_FINISH))) |
| 1883 | if (updatewindow(strm)) { |
| 1884 | state.mode = MEM; |
| 1885 | return ZLIB.Z_MEM_ERROR; |
| 1886 | } |
| 1887 | _in -= strm.avail_in; |
| 1888 | out -= strm.avail_out; |
| 1889 | strm.total_in += _in; |
| 1890 | strm.total_out += out; |
| 1891 | state.total += out; |
| 1892 | if (state.wrap && out) |
| 1893 | strm.adler = state.check = strm.checksum_function(state.check, strm.output_data, 0, strm.output_data.length); |
| 1894 | strm.data_type = state.bits + (state.last ? 64 : 0) + |
| 1895 | (state.mode == TYPE ? 128 : 0) + |
| 1896 | (state.mode == LEN_ || state.mode == COPY_ ? 256 : 0); |
| 1897 | if (((_in == 0 && out == 0) || flush == ZLIB.Z_FINISH) && ret == ZLIB.Z_OK) |
| 1898 | ret = ZLIB.Z_BUF_ERROR; |
| 1899 | return ret; |
| 1900 | }; |
| 1901 | |
| 1902 | ZLIB.inflateEnd = function(strm) |
| 1903 | { |
| 1904 | var state; |
| 1905 | if (!strm || !strm.state ) |
| 1906 | return ZLIB.Z_STREAM_ERROR; |
| 1907 | state = strm.state; |
| 1908 | state.window = null; |
| 1909 | strm.state = null; |
| 1910 | // Tracev((stderr, "inflate: end\n")); |
| 1911 | return ZLIB.Z_OK; |
| 1912 | }; |
| 1913 | |
| 1914 | ZLIB.z_stream.prototype.inflate = function(input_string, opts) |
| 1915 | { |
| 1916 | var flush; |
| 1917 | var avail_out; |
| 1918 | var DEFAULT_BUFFER_SIZE = 16384; |
| 1919 | |
| 1920 | this.input_data = input_string; |
| 1921 | this.next_in = getarg(opts, 'next_in', 0); |
| 1922 | this.avail_in = getarg(opts, 'avail_in', input_string.length - this.next_in); |
| 1923 | |
| 1924 | flush = getarg(opts, 'flush', ZLIB.Z_SYNC_FLUSH); |
| 1925 | avail_out = getarg(opts, 'avail_out', -1); |
| 1926 | |
| 1927 | var result = ''; |
| 1928 | do { |
| 1929 | this.avail_out = (avail_out >= 0 ? avail_out : DEFAULT_BUFFER_SIZE); |
| 1930 | this.output_data = ''; |
| 1931 | this.next_out = 0; |
| 1932 | this.error = ZLIB.inflate(this, flush); |
| 1933 | if(avail_out >= 0) { |
| 1934 | return this.output_data; |
| 1935 | } |
| 1936 | result += this.output_data; |
| 1937 | if(this.avail_out > 0) { |
| 1938 | break; |
| 1939 | } |
| 1940 | } while(this.error == ZLIB.Z_OK); |
| 1941 | |
| 1942 | return result; |
| 1943 | }; |
| 1944 | |
| 1945 | ZLIB.z_stream.prototype.inflateReset = function(windowBits) |
| 1946 | { |
| 1947 | return ZLIB.inflateReset(this, windowBits); |
| 1948 | }; |
| 1949 | |
| 1950 | }()); |