[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | // (C) 1995-2013 Jean-loup Gailly and Mark Adler
|
---|
| 4 | // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
|
---|
| 5 | //
|
---|
| 6 | // This software is provided 'as-is', without any express or implied
|
---|
| 7 | // warranty. In no event will the authors be held liable for any damages
|
---|
| 8 | // arising from the use of this software.
|
---|
| 9 | //
|
---|
| 10 | // Permission is granted to anyone to use this software for any purpose,
|
---|
| 11 | // including commercial applications, and to alter it and redistribute it
|
---|
| 12 | // freely, subject to the following restrictions:
|
---|
| 13 | //
|
---|
| 14 | // 1. The origin of this software must not be misrepresented; you must not
|
---|
| 15 | // claim that you wrote the original software. If you use this software
|
---|
| 16 | // in a product, an acknowledgment in the product documentation would be
|
---|
| 17 | // appreciated but is not required.
|
---|
| 18 | // 2. Altered source versions must be plainly marked as such, and must not be
|
---|
| 19 | // misrepresented as being the original software.
|
---|
| 20 | // 3. This notice may not be removed or altered from any source distribution.
|
---|
| 21 |
|
---|
| 22 | // See state defs from inflate.js
|
---|
| 23 | var BAD = 30; /* got a data error -- remain here until reset */
|
---|
| 24 | var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
|
---|
| 25 |
|
---|
| 26 | /*
|
---|
| 27 | Decode literal, length, and distance codes and write out the resulting
|
---|
| 28 | literal and match bytes until either not enough input or output is
|
---|
| 29 | available, an end-of-block is encountered, or a data error is encountered.
|
---|
| 30 | When large enough input and output buffers are supplied to inflate(), for
|
---|
| 31 | example, a 16K input buffer and a 64K output buffer, more than 95% of the
|
---|
| 32 | inflate execution time is spent in this routine.
|
---|
| 33 |
|
---|
| 34 | Entry assumptions:
|
---|
| 35 |
|
---|
| 36 | state.mode === LEN
|
---|
| 37 | strm.avail_in >= 6
|
---|
| 38 | strm.avail_out >= 258
|
---|
| 39 | start >= strm.avail_out
|
---|
| 40 | state.bits < 8
|
---|
| 41 |
|
---|
| 42 | On return, state.mode is one of:
|
---|
| 43 |
|
---|
| 44 | LEN -- ran out of enough output space or enough available input
|
---|
| 45 | TYPE -- reached end of block code, inflate() to interpret next block
|
---|
| 46 | BAD -- error in block data
|
---|
| 47 |
|
---|
| 48 | Notes:
|
---|
| 49 |
|
---|
| 50 | - The maximum input bits used by a length/distance pair is 15 bits for the
|
---|
| 51 | length code, 5 bits for the length extra, 15 bits for the distance code,
|
---|
| 52 | and 13 bits for the distance extra. This totals 48 bits, or six bytes.
|
---|
| 53 | Therefore if strm.avail_in >= 6, then there is enough input to avoid
|
---|
| 54 | checking for available input while decoding.
|
---|
| 55 |
|
---|
| 56 | - The maximum bytes that a single length/distance pair can output is 258
|
---|
| 57 | bytes, which is the maximum length that can be coded. inflate_fast()
|
---|
| 58 | requires strm.avail_out >= 258 for each loop to avoid checking for
|
---|
| 59 | output space.
|
---|
| 60 | */
|
---|
| 61 | module.exports = function inflate_fast(strm, start) {
|
---|
| 62 | var state;
|
---|
| 63 | var _in; /* local strm.input */
|
---|
| 64 | var last; /* have enough input while in < last */
|
---|
| 65 | var _out; /* local strm.output */
|
---|
| 66 | var beg; /* inflate()'s initial strm.output */
|
---|
| 67 | var end; /* while out < end, enough space available */
|
---|
| 68 | //#ifdef INFLATE_STRICT
|
---|
| 69 | var dmax; /* maximum distance from zlib header */
|
---|
| 70 | //#endif
|
---|
| 71 | var wsize; /* window size or zero if not using window */
|
---|
| 72 | var whave; /* valid bytes in the window */
|
---|
| 73 | var wnext; /* window write index */
|
---|
| 74 | // Use `s_window` instead `window`, avoid conflict with instrumentation tools
|
---|
| 75 | var s_window; /* allocated sliding window, if wsize != 0 */
|
---|
| 76 | var hold; /* local strm.hold */
|
---|
| 77 | var bits; /* local strm.bits */
|
---|
| 78 | var lcode; /* local strm.lencode */
|
---|
| 79 | var dcode; /* local strm.distcode */
|
---|
| 80 | var lmask; /* mask for first level of length codes */
|
---|
| 81 | var dmask; /* mask for first level of distance codes */
|
---|
| 82 | var here; /* retrieved table entry */
|
---|
| 83 | var op; /* code bits, operation, extra bits, or */
|
---|
| 84 | /* window position, window bytes to copy */
|
---|
| 85 | var len; /* match length, unused bytes */
|
---|
| 86 | var dist; /* match distance */
|
---|
| 87 | var from; /* where to copy match from */
|
---|
| 88 | var from_source;
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | var input, output; // JS specific, because we have no pointers
|
---|
| 92 |
|
---|
| 93 | /* copy state to local variables */
|
---|
| 94 | state = strm.state;
|
---|
| 95 | //here = state.here;
|
---|
| 96 | _in = strm.next_in;
|
---|
| 97 | input = strm.input;
|
---|
| 98 | last = _in + (strm.avail_in - 5);
|
---|
| 99 | _out = strm.next_out;
|
---|
| 100 | output = strm.output;
|
---|
| 101 | beg = _out - (start - strm.avail_out);
|
---|
| 102 | end = _out + (strm.avail_out - 257);
|
---|
| 103 | //#ifdef INFLATE_STRICT
|
---|
| 104 | dmax = state.dmax;
|
---|
| 105 | //#endif
|
---|
| 106 | wsize = state.wsize;
|
---|
| 107 | whave = state.whave;
|
---|
| 108 | wnext = state.wnext;
|
---|
| 109 | s_window = state.window;
|
---|
| 110 | hold = state.hold;
|
---|
| 111 | bits = state.bits;
|
---|
| 112 | lcode = state.lencode;
|
---|
| 113 | dcode = state.distcode;
|
---|
| 114 | lmask = (1 << state.lenbits) - 1;
|
---|
| 115 | dmask = (1 << state.distbits) - 1;
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | /* decode literals and length/distances until end-of-block or not enough
|
---|
| 119 | input data or output space */
|
---|
| 120 |
|
---|
| 121 | top:
|
---|
| 122 | do {
|
---|
| 123 | if (bits < 15) {
|
---|
| 124 | hold += input[_in++] << bits;
|
---|
| 125 | bits += 8;
|
---|
| 126 | hold += input[_in++] << bits;
|
---|
| 127 | bits += 8;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | here = lcode[hold & lmask];
|
---|
| 131 |
|
---|
| 132 | dolen:
|
---|
| 133 | for (;;) { // Goto emulation
|
---|
| 134 | op = here >>> 24/*here.bits*/;
|
---|
| 135 | hold >>>= op;
|
---|
| 136 | bits -= op;
|
---|
| 137 | op = (here >>> 16) & 0xff/*here.op*/;
|
---|
| 138 | if (op === 0) { /* literal */
|
---|
| 139 | //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
|
---|
| 140 | // "inflate: literal '%c'\n" :
|
---|
| 141 | // "inflate: literal 0x%02x\n", here.val));
|
---|
| 142 | output[_out++] = here & 0xffff/*here.val*/;
|
---|
| 143 | }
|
---|
| 144 | else if (op & 16) { /* length base */
|
---|
| 145 | len = here & 0xffff/*here.val*/;
|
---|
| 146 | op &= 15; /* number of extra bits */
|
---|
| 147 | if (op) {
|
---|
| 148 | if (bits < op) {
|
---|
| 149 | hold += input[_in++] << bits;
|
---|
| 150 | bits += 8;
|
---|
| 151 | }
|
---|
| 152 | len += hold & ((1 << op) - 1);
|
---|
| 153 | hold >>>= op;
|
---|
| 154 | bits -= op;
|
---|
| 155 | }
|
---|
| 156 | //Tracevv((stderr, "inflate: length %u\n", len));
|
---|
| 157 | if (bits < 15) {
|
---|
| 158 | hold += input[_in++] << bits;
|
---|
| 159 | bits += 8;
|
---|
| 160 | hold += input[_in++] << bits;
|
---|
| 161 | bits += 8;
|
---|
| 162 | }
|
---|
| 163 | here = dcode[hold & dmask];
|
---|
| 164 |
|
---|
| 165 | dodist:
|
---|
| 166 | for (;;) { // goto emulation
|
---|
| 167 | op = here >>> 24/*here.bits*/;
|
---|
| 168 | hold >>>= op;
|
---|
| 169 | bits -= op;
|
---|
| 170 | op = (here >>> 16) & 0xff/*here.op*/;
|
---|
| 171 |
|
---|
| 172 | if (op & 16) { /* distance base */
|
---|
| 173 | dist = here & 0xffff/*here.val*/;
|
---|
| 174 | op &= 15; /* number of extra bits */
|
---|
| 175 | if (bits < op) {
|
---|
| 176 | hold += input[_in++] << bits;
|
---|
| 177 | bits += 8;
|
---|
| 178 | if (bits < op) {
|
---|
| 179 | hold += input[_in++] << bits;
|
---|
| 180 | bits += 8;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | dist += hold & ((1 << op) - 1);
|
---|
| 184 | //#ifdef INFLATE_STRICT
|
---|
| 185 | if (dist > dmax) {
|
---|
| 186 | strm.msg = 'invalid distance too far back';
|
---|
| 187 | state.mode = BAD;
|
---|
| 188 | break top;
|
---|
| 189 | }
|
---|
| 190 | //#endif
|
---|
| 191 | hold >>>= op;
|
---|
| 192 | bits -= op;
|
---|
| 193 | //Tracevv((stderr, "inflate: distance %u\n", dist));
|
---|
| 194 | op = _out - beg; /* max distance in output */
|
---|
| 195 | if (dist > op) { /* see if copy from window */
|
---|
| 196 | op = dist - op; /* distance back in window */
|
---|
| 197 | if (op > whave) {
|
---|
| 198 | if (state.sane) {
|
---|
| 199 | strm.msg = 'invalid distance too far back';
|
---|
| 200 | state.mode = BAD;
|
---|
| 201 | break top;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // (!) This block is disabled in zlib defaults,
|
---|
| 205 | // don't enable it for binary compatibility
|
---|
| 206 | //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
|
---|
| 207 | // if (len <= op - whave) {
|
---|
| 208 | // do {
|
---|
| 209 | // output[_out++] = 0;
|
---|
| 210 | // } while (--len);
|
---|
| 211 | // continue top;
|
---|
| 212 | // }
|
---|
| 213 | // len -= op - whave;
|
---|
| 214 | // do {
|
---|
| 215 | // output[_out++] = 0;
|
---|
| 216 | // } while (--op > whave);
|
---|
| 217 | // if (op === 0) {
|
---|
| 218 | // from = _out - dist;
|
---|
| 219 | // do {
|
---|
| 220 | // output[_out++] = output[from++];
|
---|
| 221 | // } while (--len);
|
---|
| 222 | // continue top;
|
---|
| 223 | // }
|
---|
| 224 | //#endif
|
---|
| 225 | }
|
---|
| 226 | from = 0; // window index
|
---|
| 227 | from_source = s_window;
|
---|
| 228 | if (wnext === 0) { /* very common case */
|
---|
| 229 | from += wsize - op;
|
---|
| 230 | if (op < len) { /* some from window */
|
---|
| 231 | len -= op;
|
---|
| 232 | do {
|
---|
| 233 | output[_out++] = s_window[from++];
|
---|
| 234 | } while (--op);
|
---|
| 235 | from = _out - dist; /* rest from output */
|
---|
| 236 | from_source = output;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | else if (wnext < op) { /* wrap around window */
|
---|
| 240 | from += wsize + wnext - op;
|
---|
| 241 | op -= wnext;
|
---|
| 242 | if (op < len) { /* some from end of window */
|
---|
| 243 | len -= op;
|
---|
| 244 | do {
|
---|
| 245 | output[_out++] = s_window[from++];
|
---|
| 246 | } while (--op);
|
---|
| 247 | from = 0;
|
---|
| 248 | if (wnext < len) { /* some from start of window */
|
---|
| 249 | op = wnext;
|
---|
| 250 | len -= op;
|
---|
| 251 | do {
|
---|
| 252 | output[_out++] = s_window[from++];
|
---|
| 253 | } while (--op);
|
---|
| 254 | from = _out - dist; /* rest from output */
|
---|
| 255 | from_source = output;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 | else { /* contiguous in window */
|
---|
| 260 | from += wnext - op;
|
---|
| 261 | if (op < len) { /* some from window */
|
---|
| 262 | len -= op;
|
---|
| 263 | do {
|
---|
| 264 | output[_out++] = s_window[from++];
|
---|
| 265 | } while (--op);
|
---|
| 266 | from = _out - dist; /* rest from output */
|
---|
| 267 | from_source = output;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | while (len > 2) {
|
---|
| 271 | output[_out++] = from_source[from++];
|
---|
| 272 | output[_out++] = from_source[from++];
|
---|
| 273 | output[_out++] = from_source[from++];
|
---|
| 274 | len -= 3;
|
---|
| 275 | }
|
---|
| 276 | if (len) {
|
---|
| 277 | output[_out++] = from_source[from++];
|
---|
| 278 | if (len > 1) {
|
---|
| 279 | output[_out++] = from_source[from++];
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | else {
|
---|
| 284 | from = _out - dist; /* copy direct from output */
|
---|
| 285 | do { /* minimum length is three */
|
---|
| 286 | output[_out++] = output[from++];
|
---|
| 287 | output[_out++] = output[from++];
|
---|
| 288 | output[_out++] = output[from++];
|
---|
| 289 | len -= 3;
|
---|
| 290 | } while (len > 2);
|
---|
| 291 | if (len) {
|
---|
| 292 | output[_out++] = output[from++];
|
---|
| 293 | if (len > 1) {
|
---|
| 294 | output[_out++] = output[from++];
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | else if ((op & 64) === 0) { /* 2nd level distance code */
|
---|
| 300 | here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
|
---|
| 301 | continue dodist;
|
---|
| 302 | }
|
---|
| 303 | else {
|
---|
| 304 | strm.msg = 'invalid distance code';
|
---|
| 305 | state.mode = BAD;
|
---|
| 306 | break top;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | break; // need to emulate goto via "continue"
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | else if ((op & 64) === 0) { /* 2nd level length code */
|
---|
| 313 | here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
|
---|
| 314 | continue dolen;
|
---|
| 315 | }
|
---|
| 316 | else if (op & 32) { /* end-of-block */
|
---|
| 317 | //Tracevv((stderr, "inflate: end of block\n"));
|
---|
| 318 | state.mode = TYPE;
|
---|
| 319 | break top;
|
---|
| 320 | }
|
---|
| 321 | else {
|
---|
| 322 | strm.msg = 'invalid literal/length code';
|
---|
| 323 | state.mode = BAD;
|
---|
| 324 | break top;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | break; // need to emulate goto via "continue"
|
---|
| 328 | }
|
---|
| 329 | } while (_in < last && _out < end);
|
---|
| 330 |
|
---|
| 331 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
|
---|
| 332 | len = bits >> 3;
|
---|
| 333 | _in -= len;
|
---|
| 334 | bits -= len << 3;
|
---|
| 335 | hold &= (1 << bits) - 1;
|
---|
| 336 |
|
---|
| 337 | /* update state and return */
|
---|
| 338 | strm.next_in = _in;
|
---|
| 339 | strm.next_out = _out;
|
---|
| 340 | strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
|
---|
| 341 | strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
|
---|
| 342 | state.hold = hold;
|
---|
| 343 | state.bits = bits;
|
---|
| 344 | return;
|
---|
| 345 | };
|
---|