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