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