athena/extern/zlib/inffast.c

438 lines
14 KiB
C
Raw Normal View History

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