michael@0: /* inffast.c -- fast decoding michael@0: * Copyright (C) 1995-2008, 2010 Mark Adler michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: #include "zutil.h" michael@0: #include "inftrees.h" michael@0: #include "inflate.h" michael@0: #include "inffast.h" michael@0: michael@0: #ifndef ASMINF michael@0: michael@0: /* Allow machine dependent optimization for post-increment or pre-increment. michael@0: Based on testing to date, michael@0: Pre-increment preferred for: michael@0: - PowerPC G3 (Adler) michael@0: - MIPS R5000 (Randers-Pehrson) michael@0: Post-increment preferred for: michael@0: - none michael@0: No measurable difference: michael@0: - Pentium III (Anderson) michael@0: - M68060 (Nikl) michael@0: */ michael@0: #ifdef POSTINC michael@0: # define OFF 0 michael@0: # define PUP(a) *(a)++ michael@0: #else michael@0: # define OFF 1 michael@0: # define PUP(a) *++(a) michael@0: #endif michael@0: michael@0: /* michael@0: Decode literal, length, and distance codes and write out the resulting michael@0: literal and match bytes until either not enough input or output is michael@0: available, an end-of-block is encountered, or a data error is encountered. michael@0: When large enough input and output buffers are supplied to inflate(), for michael@0: example, a 16K input buffer and a 64K output buffer, more than 95% of the michael@0: inflate execution time is spent in this routine. michael@0: michael@0: Entry assumptions: michael@0: michael@0: state->mode == LEN michael@0: strm->avail_in >= 6 michael@0: strm->avail_out >= 258 michael@0: start >= strm->avail_out michael@0: state->bits < 8 michael@0: michael@0: On return, state->mode is one of: michael@0: michael@0: LEN -- ran out of enough output space or enough available input michael@0: TYPE -- reached end of block code, inflate() to interpret next block michael@0: BAD -- error in block data michael@0: michael@0: Notes: michael@0: michael@0: - The maximum input bits used by a length/distance pair is 15 bits for the michael@0: length code, 5 bits for the length extra, 15 bits for the distance code, michael@0: and 13 bits for the distance extra. This totals 48 bits, or six bytes. michael@0: Therefore if strm->avail_in >= 6, then there is enough input to avoid michael@0: checking for available input while decoding. michael@0: michael@0: - The maximum bytes that a single length/distance pair can output is 258 michael@0: bytes, which is the maximum length that can be coded. inflate_fast() michael@0: requires strm->avail_out >= 258 for each loop to avoid checking for michael@0: output space. michael@0: */ michael@0: void ZLIB_INTERNAL inflate_fast(strm, start) michael@0: z_streamp strm; michael@0: unsigned start; /* inflate()'s starting value for strm->avail_out */ michael@0: { michael@0: struct inflate_state FAR *state; michael@0: unsigned char FAR *in; /* local strm->next_in */ michael@0: unsigned char FAR *last; /* while in < last, enough input available */ michael@0: unsigned char FAR *out; /* local strm->next_out */ michael@0: unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ michael@0: unsigned char FAR *end; /* while out < end, enough space available */ michael@0: #ifdef INFLATE_STRICT michael@0: unsigned dmax; /* maximum distance from zlib header */ michael@0: #endif michael@0: unsigned wsize; /* window size or zero if not using window */ michael@0: unsigned whave; /* valid bytes in the window */ michael@0: unsigned wnext; /* window write index */ michael@0: unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ michael@0: unsigned long hold; /* local strm->hold */ michael@0: unsigned bits; /* local strm->bits */ michael@0: code const FAR *lcode; /* local strm->lencode */ michael@0: code const FAR *dcode; /* local strm->distcode */ michael@0: unsigned lmask; /* mask for first level of length codes */ michael@0: unsigned dmask; /* mask for first level of distance codes */ michael@0: code here; /* retrieved table entry */ michael@0: unsigned op; /* code bits, operation, extra bits, or */ michael@0: /* window position, window bytes to copy */ michael@0: unsigned len; /* match length, unused bytes */ michael@0: unsigned dist; /* match distance */ michael@0: unsigned char FAR *from; /* where to copy match from */ michael@0: michael@0: /* copy state to local variables */ michael@0: state = (struct inflate_state FAR *)strm->state; michael@0: in = strm->next_in - OFF; michael@0: last = in + (strm->avail_in - 5); michael@0: out = strm->next_out - OFF; michael@0: beg = out - (start - strm->avail_out); michael@0: end = out + (strm->avail_out - 257); michael@0: #ifdef INFLATE_STRICT michael@0: dmax = state->dmax; michael@0: #endif michael@0: wsize = state->wsize; michael@0: whave = state->whave; michael@0: wnext = state->wnext; michael@0: window = state->window; michael@0: hold = state->hold; michael@0: bits = state->bits; michael@0: lcode = state->lencode; michael@0: dcode = state->distcode; michael@0: lmask = (1U << state->lenbits) - 1; michael@0: dmask = (1U << state->distbits) - 1; michael@0: michael@0: /* decode literals and length/distances until end-of-block or not enough michael@0: input data or output space */ michael@0: do { michael@0: if (bits < 15) { michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: } michael@0: here = lcode[hold & lmask]; michael@0: dolen: michael@0: op = (unsigned)(here.bits); michael@0: hold >>= op; michael@0: bits -= op; michael@0: op = (unsigned)(here.op); michael@0: if (op == 0) { /* literal */ michael@0: Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? michael@0: "inflate: literal '%c'\n" : michael@0: "inflate: literal 0x%02x\n", here.val)); michael@0: PUP(out) = (unsigned char)(here.val); michael@0: } michael@0: else if (op & 16) { /* length base */ michael@0: len = (unsigned)(here.val); michael@0: op &= 15; /* number of extra bits */ michael@0: if (op) { michael@0: if (bits < op) { michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: } michael@0: len += (unsigned)hold & ((1U << op) - 1); michael@0: hold >>= op; michael@0: bits -= op; michael@0: } michael@0: Tracevv((stderr, "inflate: length %u\n", len)); michael@0: if (bits < 15) { michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: } michael@0: here = dcode[hold & dmask]; michael@0: dodist: michael@0: op = (unsigned)(here.bits); michael@0: hold >>= op; michael@0: bits -= op; michael@0: op = (unsigned)(here.op); michael@0: if (op & 16) { /* distance base */ michael@0: dist = (unsigned)(here.val); michael@0: op &= 15; /* number of extra bits */ michael@0: if (bits < op) { michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: if (bits < op) { michael@0: hold += (unsigned long)(PUP(in)) << bits; michael@0: bits += 8; michael@0: } michael@0: } michael@0: dist += (unsigned)hold & ((1U << op) - 1); michael@0: #ifdef INFLATE_STRICT michael@0: if (dist > dmax) { michael@0: strm->msg = (char *)"invalid distance too far back"; michael@0: state->mode = BAD; michael@0: break; michael@0: } michael@0: #endif michael@0: hold >>= op; michael@0: bits -= op; michael@0: Tracevv((stderr, "inflate: distance %u\n", dist)); michael@0: op = (unsigned)(out - beg); /* max distance in output */ michael@0: if (dist > op) { /* see if copy from window */ michael@0: op = dist - op; /* distance back in window */ michael@0: if (op > whave) { michael@0: if (state->sane) { michael@0: strm->msg = michael@0: (char *)"invalid distance too far back"; michael@0: state->mode = BAD; michael@0: break; michael@0: } michael@0: #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR michael@0: if (len <= op - whave) { michael@0: do { michael@0: PUP(out) = 0; michael@0: } while (--len); michael@0: continue; michael@0: } michael@0: len -= op - whave; michael@0: do { michael@0: PUP(out) = 0; michael@0: } while (--op > whave); michael@0: if (op == 0) { michael@0: from = out - dist; michael@0: do { michael@0: PUP(out) = PUP(from); michael@0: } while (--len); michael@0: continue; michael@0: } michael@0: #endif michael@0: } michael@0: from = window - OFF; michael@0: if (wnext == 0) { /* very common case */ michael@0: from += wsize - op; michael@0: if (op < len) { /* some from window */ michael@0: len -= op; michael@0: do { michael@0: PUP(out) = PUP(from); michael@0: } while (--op); michael@0: from = out - dist; /* rest from output */ michael@0: } michael@0: } michael@0: else if (wnext < op) { /* wrap around window */ michael@0: from += wsize + wnext - op; michael@0: op -= wnext; michael@0: if (op < len) { /* some from end of window */ michael@0: len -= op; michael@0: do { michael@0: PUP(out) = PUP(from); michael@0: } while (--op); michael@0: from = window - OFF; michael@0: if (wnext < len) { /* some from start of window */ michael@0: op = wnext; michael@0: len -= op; michael@0: do { michael@0: PUP(out) = PUP(from); michael@0: } while (--op); michael@0: from = out - dist; /* rest from output */ michael@0: } michael@0: } michael@0: } michael@0: else { /* contiguous in window */ michael@0: from += wnext - op; michael@0: if (op < len) { /* some from window */ michael@0: len -= op; michael@0: do { michael@0: PUP(out) = PUP(from); michael@0: } while (--op); michael@0: from = out - dist; /* rest from output */ michael@0: } michael@0: } michael@0: while (len > 2) { michael@0: PUP(out) = PUP(from); michael@0: PUP(out) = PUP(from); michael@0: PUP(out) = PUP(from); michael@0: len -= 3; michael@0: } michael@0: if (len) { michael@0: PUP(out) = PUP(from); michael@0: if (len > 1) michael@0: PUP(out) = PUP(from); michael@0: } michael@0: } michael@0: else { michael@0: from = out - dist; /* copy direct from output */ michael@0: do { /* minimum length is three */ michael@0: PUP(out) = PUP(from); michael@0: PUP(out) = PUP(from); michael@0: PUP(out) = PUP(from); michael@0: len -= 3; michael@0: } while (len > 2); michael@0: if (len) { michael@0: PUP(out) = PUP(from); michael@0: if (len > 1) michael@0: PUP(out) = PUP(from); michael@0: } michael@0: } michael@0: } michael@0: else if ((op & 64) == 0) { /* 2nd level distance code */ michael@0: here = dcode[here.val + (hold & ((1U << op) - 1))]; michael@0: goto dodist; michael@0: } michael@0: else { michael@0: strm->msg = (char *)"invalid distance code"; michael@0: state->mode = BAD; michael@0: break; michael@0: } michael@0: } michael@0: else if ((op & 64) == 0) { /* 2nd level length code */ michael@0: here = lcode[here.val + (hold & ((1U << op) - 1))]; michael@0: goto dolen; michael@0: } michael@0: else if (op & 32) { /* end-of-block */ michael@0: Tracevv((stderr, "inflate: end of block\n")); michael@0: state->mode = TYPE; michael@0: break; michael@0: } michael@0: else { michael@0: strm->msg = (char *)"invalid literal/length code"; michael@0: state->mode = BAD; michael@0: break; michael@0: } michael@0: } while (in < last && out < end); michael@0: michael@0: /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ michael@0: len = bits >> 3; michael@0: in -= len; michael@0: bits -= len << 3; michael@0: hold &= (1U << bits) - 1; michael@0: michael@0: /* update state and return */ michael@0: strm->next_in = in + OFF; michael@0: strm->next_out = out + OFF; michael@0: strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); michael@0: strm->avail_out = (unsigned)(out < end ? michael@0: 257 + (end - out) : 257 - (out - end)); michael@0: state->hold = hold; michael@0: state->bits = bits; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): michael@0: - Using bit fields for code structure michael@0: - Different op definition to avoid & for extra bits (do & for table bits) michael@0: - Three separate decoding do-loops for direct, window, and wnext == 0 michael@0: - Special case for distance > 1 copies to do overlapped load and store copy michael@0: - Explicit branch predictions (based on measured branch probabilities) michael@0: - Deferring match copy and interspersed it with decoding subsequent codes michael@0: - Swapping literal/length else michael@0: - Swapping window/direct else michael@0: - Larger unrolled copy loops (three is about right) michael@0: - Moving len -= 3 statement into middle of loop michael@0: */ michael@0: michael@0: #endif /* !ASMINF */