/* inffast.c -- fast decoding1* Copyright (C) 1995-2017 Mark Adler2* For conditions of distribution and use, see copyright notice in zlib.h3*/45#include "zutil.h"6#include "inftrees.h"7#include "inflate.h"8#include "inffast.h"910#ifdef ASMINF11# pragma message("Assembler code may have bugs -- use at your own risk")12#else1314/*15Decode literal, length, and distance codes and write out the resulting16literal and match bytes until either not enough input or output is17available, an end-of-block is encountered, or a data error is encountered.18When large enough input and output buffers are supplied to inflate(), for19example, a 16K input buffer and a 64K output buffer, more than 95% of the20inflate execution time is spent in this routine.2122Entry assumptions:2324state->mode == LEN25strm->avail_in >= 626strm->avail_out >= 25827start >= strm->avail_out28state->bits < 82930On return, state->mode is one of:3132LEN -- ran out of enough output space or enough available input33TYPE -- reached end of block code, inflate() to interpret next block34BAD -- error in block data3536Notes:3738- The maximum input bits used by a length/distance pair is 15 bits for the39length code, 5 bits for the length extra, 15 bits for the distance code,40and 13 bits for the distance extra. This totals 48 bits, or six bytes.41Therefore if strm->avail_in >= 6, then there is enough input to avoid42checking for available input while decoding.4344- The maximum bytes that a single length/distance pair can output is 25845bytes, which is the maximum length that can be coded. inflate_fast()46requires strm->avail_out >= 258 for each loop to avoid checking for47output space.48*/49void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {50struct inflate_state FAR *state;51z_const unsigned char FAR *in; /* local strm->next_in */52z_const unsigned char FAR *last; /* have enough input while in < last */53unsigned char FAR *out; /* local strm->next_out */54unsigned char FAR *beg; /* inflate()'s initial strm->next_out */55unsigned char FAR *end; /* while out < end, enough space available */56#ifdef INFLATE_STRICT57unsigned dmax; /* maximum distance from zlib header */58#endif59unsigned wsize; /* window size or zero if not using window */60unsigned whave; /* valid bytes in the window */61unsigned wnext; /* window write index */62unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */63unsigned long hold; /* local strm->hold */64unsigned bits; /* local strm->bits */65code const FAR *lcode; /* local strm->lencode */66code const FAR *dcode; /* local strm->distcode */67unsigned lmask; /* mask for first level of length codes */68unsigned dmask; /* mask for first level of distance codes */69code const *here; /* retrieved table entry */70unsigned op; /* code bits, operation, extra bits, or */71/* window position, window bytes to copy */72unsigned len; /* match length, unused bytes */73unsigned dist; /* match distance */74unsigned char FAR *from; /* where to copy match from */7576/* copy state to local variables */77state = (struct inflate_state FAR *)strm->state;78in = strm->next_in;79last = in + (strm->avail_in - 5);80out = strm->next_out;81beg = out - (start - strm->avail_out);82end = out + (strm->avail_out - 257);83#ifdef INFLATE_STRICT84dmax = state->dmax;85#endif86wsize = state->wsize;87whave = state->whave;88wnext = state->wnext;89window = state->window;90hold = state->hold;91bits = state->bits;92lcode = state->lencode;93dcode = state->distcode;94lmask = (1U << state->lenbits) - 1;95dmask = (1U << state->distbits) - 1;9697/* decode literals and length/distances until end-of-block or not enough98input data or output space */99do {100if (bits < 15) {101hold += (unsigned long)(*in++) << bits;102bits += 8;103hold += (unsigned long)(*in++) << bits;104bits += 8;105}106here = lcode + (hold & lmask);107dolen:108op = (unsigned)(here->bits);109hold >>= op;110bits -= op;111op = (unsigned)(here->op);112if (op == 0) { /* literal */113Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?114"inflate: literal '%c'\n" :115"inflate: literal 0x%02x\n", here->val));116*out++ = (unsigned char)(here->val);117}118else if (op & 16) { /* length base */119len = (unsigned)(here->val);120op &= 15; /* number of extra bits */121if (op) {122if (bits < op) {123hold += (unsigned long)(*in++) << bits;124bits += 8;125}126len += (unsigned)hold & ((1U << op) - 1);127hold >>= op;128bits -= op;129}130Tracevv((stderr, "inflate: length %u\n", len));131if (bits < 15) {132hold += (unsigned long)(*in++) << bits;133bits += 8;134hold += (unsigned long)(*in++) << bits;135bits += 8;136}137here = dcode + (hold & dmask);138dodist:139op = (unsigned)(here->bits);140hold >>= op;141bits -= op;142op = (unsigned)(here->op);143if (op & 16) { /* distance base */144dist = (unsigned)(here->val);145op &= 15; /* number of extra bits */146if (bits < op) {147hold += (unsigned long)(*in++) << bits;148bits += 8;149if (bits < op) {150hold += (unsigned long)(*in++) << bits;151bits += 8;152}153}154dist += (unsigned)hold & ((1U << op) - 1);155#ifdef INFLATE_STRICT156if (dist > dmax) {157strm->msg = (char *)"invalid distance too far back";158state->mode = BAD;159break;160}161#endif162hold >>= op;163bits -= op;164Tracevv((stderr, "inflate: distance %u\n", dist));165op = (unsigned)(out - beg); /* max distance in output */166if (dist > op) { /* see if copy from window */167op = dist - op; /* distance back in window */168if (op > whave) {169if (state->sane) {170strm->msg =171(char *)"invalid distance too far back";172state->mode = BAD;173break;174}175#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR176if (len <= op - whave) {177do {178*out++ = 0;179} while (--len);180continue;181}182len -= op - whave;183do {184*out++ = 0;185} while (--op > whave);186if (op == 0) {187from = out - dist;188do {189*out++ = *from++;190} while (--len);191continue;192}193#endif194}195from = window;196if (wnext == 0) { /* very common case */197from += wsize - op;198if (op < len) { /* some from window */199len -= op;200do {201*out++ = *from++;202} while (--op);203from = out - dist; /* rest from output */204}205}206else if (wnext < op) { /* wrap around window */207from += wsize + wnext - op;208op -= wnext;209if (op < len) { /* some from end of window */210len -= op;211do {212*out++ = *from++;213} while (--op);214from = window;215if (wnext < len) { /* some from start of window */216op = wnext;217len -= op;218do {219*out++ = *from++;220} while (--op);221from = out - dist; /* rest from output */222}223}224}225else { /* contiguous in window */226from += wnext - op;227if (op < len) { /* some from window */228len -= op;229do {230*out++ = *from++;231} while (--op);232from = out - dist; /* rest from output */233}234}235while (len > 2) {236*out++ = *from++;237*out++ = *from++;238*out++ = *from++;239len -= 3;240}241if (len) {242*out++ = *from++;243if (len > 1)244*out++ = *from++;245}246}247else {248from = out - dist; /* copy direct from output */249do { /* minimum length is three */250*out++ = *from++;251*out++ = *from++;252*out++ = *from++;253len -= 3;254} while (len > 2);255if (len) {256*out++ = *from++;257if (len > 1)258*out++ = *from++;259}260}261}262else if ((op & 64) == 0) { /* 2nd level distance code */263here = dcode + here->val + (hold & ((1U << op) - 1));264goto dodist;265}266else {267strm->msg = (char *)"invalid distance code";268state->mode = BAD;269break;270}271}272else if ((op & 64) == 0) { /* 2nd level length code */273here = lcode + here->val + (hold & ((1U << op) - 1));274goto dolen;275}276else if (op & 32) { /* end-of-block */277Tracevv((stderr, "inflate: end of block\n"));278state->mode = TYPE;279break;280}281else {282strm->msg = (char *)"invalid literal/length code";283state->mode = BAD;284break;285}286} while (in < last && out < end);287288/* return unused bytes (on entry, bits < 8, so in won't go too far back) */289len = bits >> 3;290in -= len;291bits -= len << 3;292hold &= (1U << bits) - 1;293294/* update state and return */295strm->next_in = in;296strm->next_out = out;297strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));298strm->avail_out = (unsigned)(out < end ?299257 + (end - out) : 257 - (out - end));300state->hold = hold;301state->bits = bits;302return;303}304305/*306inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):307- Using bit fields for code structure308- Different op definition to avoid & for extra bits (do & for table bits)309- Three separate decoding do-loops for direct, window, and wnext == 0310- Special case for distance > 1 copies to do overlapped load and store copy311- Explicit branch predictions (based on measured branch probabilities)312- Deferring match copy and interspersed it with decoding subsequent codes313- Swapping literal/length else314- Swapping window/direct else315- Larger unrolled copy loops (three is about right)316- Moving len -= 3 statement into middle of loop317*/318319#endif /* !ASMINF */320321322