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