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