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