react / wstein / node_modules / browserify / node_modules / browserify-zlib / node_modules / pako / lib / zlib / inffast.js
80549 views'use strict';12// See state defs from inflate.js3var BAD = 30; /* got a data error -- remain here until reset */4var TYPE = 12; /* i: waiting for type bits, including last-flag bit */56/*7Decode literal, length, and distance codes and write out the resulting8literal and match bytes until either not enough input or output is9available, an end-of-block is encountered, or a data error is encountered.10When large enough input and output buffers are supplied to inflate(), for11example, a 16K input buffer and a 64K output buffer, more than 95% of the12inflate execution time is spent in this routine.1314Entry assumptions:1516state.mode === LEN17strm.avail_in >= 618strm.avail_out >= 25819start >= strm.avail_out20state.bits < 82122On return, state.mode is one of:2324LEN -- ran out of enough output space or enough available input25TYPE -- reached end of block code, inflate() to interpret next block26BAD -- error in block data2728Notes:2930- The maximum input bits used by a length/distance pair is 15 bits for the31length code, 5 bits for the length extra, 15 bits for the distance code,32and 13 bits for the distance extra. This totals 48 bits, or six bytes.33Therefore if strm.avail_in >= 6, then there is enough input to avoid34checking for available input while decoding.3536- The maximum bytes that a single length/distance pair can output is 25837bytes, which is the maximum length that can be coded. inflate_fast()38requires strm.avail_out >= 258 for each loop to avoid checking for39output space.40*/41module.exports = function inflate_fast(strm, start) {42var state;43var _in; /* local strm.input */44var last; /* have enough input while in < last */45var _out; /* local strm.output */46var beg; /* inflate()'s initial strm.output */47var end; /* while out < end, enough space available */48//#ifdef INFLATE_STRICT49var dmax; /* maximum distance from zlib header */50//#endif51var wsize; /* window size or zero if not using window */52var whave; /* valid bytes in the window */53var wnext; /* window write index */54var window; /* allocated sliding window, if wsize != 0 */55var hold; /* local strm.hold */56var bits; /* local strm.bits */57var lcode; /* local strm.lencode */58var dcode; /* local strm.distcode */59var lmask; /* mask for first level of length codes */60var dmask; /* mask for first level of distance codes */61var here; /* retrieved table entry */62var op; /* code bits, operation, extra bits, or */63/* window position, window bytes to copy */64var len; /* match length, unused bytes */65var dist; /* match distance */66var from; /* where to copy match from */67var from_source;686970var input, output; // JS specific, because we have no pointers7172/* copy state to local variables */73state = strm.state;74//here = state.here;75_in = strm.next_in;76input = strm.input;77last = _in + (strm.avail_in - 5);78_out = strm.next_out;79output = strm.output;80beg = _out - (start - strm.avail_out);81end = _out + (strm.avail_out - 257);82//#ifdef INFLATE_STRICT83dmax = state.dmax;84//#endif85wsize = state.wsize;86whave = state.whave;87wnext = state.wnext;88window = state.window;89hold = state.hold;90bits = state.bits;91lcode = state.lencode;92dcode = state.distcode;93lmask = (1 << state.lenbits) - 1;94dmask = (1 << state.distbits) - 1;959697/* decode literals and length/distances until end-of-block or not enough98input data or output space */99100top:101do {102if (bits < 15) {103hold += input[_in++] << bits;104bits += 8;105hold += input[_in++] << bits;106bits += 8;107}108109here = lcode[hold & lmask];110111dolen:112for (;;) { // Goto emulation113op = here >>> 24/*here.bits*/;114hold >>>= op;115bits -= op;116op = (here >>> 16) & 0xff/*here.op*/;117if (op === 0) { /* literal */118//Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?119// "inflate: literal '%c'\n" :120// "inflate: literal 0x%02x\n", here.val));121output[_out++] = here & 0xffff/*here.val*/;122}123else if (op & 16) { /* length base */124len = here & 0xffff/*here.val*/;125op &= 15; /* number of extra bits */126if (op) {127if (bits < op) {128hold += input[_in++] << bits;129bits += 8;130}131len += hold & ((1 << op) - 1);132hold >>>= op;133bits -= op;134}135//Tracevv((stderr, "inflate: length %u\n", len));136if (bits < 15) {137hold += input[_in++] << bits;138bits += 8;139hold += input[_in++] << bits;140bits += 8;141}142here = dcode[hold & dmask];143144dodist:145for (;;) { // goto emulation146op = here >>> 24/*here.bits*/;147hold >>>= op;148bits -= op;149op = (here >>> 16) & 0xff/*here.op*/;150151if (op & 16) { /* distance base */152dist = here & 0xffff/*here.val*/;153op &= 15; /* number of extra bits */154if (bits < op) {155hold += input[_in++] << bits;156bits += 8;157if (bits < op) {158hold += input[_in++] << bits;159bits += 8;160}161}162dist += hold & ((1 << op) - 1);163//#ifdef INFLATE_STRICT164if (dist > dmax) {165strm.msg = 'invalid distance too far back';166state.mode = BAD;167break top;168}169//#endif170hold >>>= op;171bits -= op;172//Tracevv((stderr, "inflate: distance %u\n", dist));173op = _out - beg; /* max distance in output */174if (dist > op) { /* see if copy from window */175op = dist - op; /* distance back in window */176if (op > whave) {177if (state.sane) {178strm.msg = 'invalid distance too far back';179state.mode = BAD;180break top;181}182183// (!) This block is disabled in zlib defailts,184// don't enable it for binary compatibility185//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR186// if (len <= op - whave) {187// do {188// output[_out++] = 0;189// } while (--len);190// continue top;191// }192// len -= op - whave;193// do {194// output[_out++] = 0;195// } while (--op > whave);196// if (op === 0) {197// from = _out - dist;198// do {199// output[_out++] = output[from++];200// } while (--len);201// continue top;202// }203//#endif204}205from = 0; // window index206from_source = window;207if (wnext === 0) { /* very common case */208from += wsize - op;209if (op < len) { /* some from window */210len -= op;211do {212output[_out++] = window[from++];213} while (--op);214from = _out - dist; /* rest from output */215from_source = output;216}217}218else if (wnext < op) { /* wrap around window */219from += wsize + wnext - op;220op -= wnext;221if (op < len) { /* some from end of window */222len -= op;223do {224output[_out++] = window[from++];225} while (--op);226from = 0;227if (wnext < len) { /* some from start of window */228op = wnext;229len -= op;230do {231output[_out++] = window[from++];232} while (--op);233from = _out - dist; /* rest from output */234from_source = output;235}236}237}238else { /* contiguous in window */239from += wnext - op;240if (op < len) { /* some from window */241len -= op;242do {243output[_out++] = window[from++];244} while (--op);245from = _out - dist; /* rest from output */246from_source = output;247}248}249while (len > 2) {250output[_out++] = from_source[from++];251output[_out++] = from_source[from++];252output[_out++] = from_source[from++];253len -= 3;254}255if (len) {256output[_out++] = from_source[from++];257if (len > 1) {258output[_out++] = from_source[from++];259}260}261}262else {263from = _out - dist; /* copy direct from output */264do { /* minimum length is three */265output[_out++] = output[from++];266output[_out++] = output[from++];267output[_out++] = output[from++];268len -= 3;269} while (len > 2);270if (len) {271output[_out++] = output[from++];272if (len > 1) {273output[_out++] = output[from++];274}275}276}277}278else if ((op & 64) === 0) { /* 2nd level distance code */279here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];280continue dodist;281}282else {283strm.msg = 'invalid distance code';284state.mode = BAD;285break top;286}287288break; // need to emulate goto via "continue"289}290}291else if ((op & 64) === 0) { /* 2nd level length code */292here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];293continue dolen;294}295else if (op & 32) { /* end-of-block */296//Tracevv((stderr, "inflate: end of block\n"));297state.mode = TYPE;298break top;299}300else {301strm.msg = 'invalid literal/length code';302state.mode = BAD;303break top;304}305306break; // need to emulate goto via "continue"307}308} while (_in < last && _out < end);309310/* return unused bytes (on entry, bits < 8, so in won't go too far back) */311len = bits >> 3;312_in -= len;313bits -= len << 3;314hold &= (1 << bits) - 1;315316/* update state and return */317strm.next_in = _in;318strm.next_out = _out;319strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));320strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));321state.hold = hold;322state.bits = bits;323return;324};325326327