mirror of https://github.com/madler/zlib.git
- added inflateBackWrap() for stand-alone validation of inflateBack(). Moreover, inflateBackWrap() exhibits identical functionality as in inflate(). - added comments for code revisions - added log in ChangeLogpull/793/head
parent
befb971b78
commit
cfa12c8c77
8 changed files with 733 additions and 6 deletions
@ -0,0 +1,646 @@ |
|||||||
|
/* infbackwrap.c -- zlib decompression
|
||||||
|
* Copyright (C) 2023-2039 Cody Wu |
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h |
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
#include "zutil.h" |
||||||
|
#include "inftrees.h" |
||||||
|
#include "inflate.h" |
||||||
|
#include "inffast.h" |
||||||
|
|
||||||
|
/* function prototypes */ |
||||||
|
local int inflateStateCheck OF((z_streamp strm)); |
||||||
|
local void fixedtables OF((struct inflate_state FAR* state)); |
||||||
|
local int updatewindow OF((z_streamp strm, const unsigned char FAR* end, |
||||||
|
unsigned copy)); |
||||||
|
|
||||||
|
local unsigned syncsearch OF((unsigned FAR* have, const unsigned char FAR* buf, |
||||||
|
unsigned len)); |
||||||
|
|
||||||
|
local int inflateStateCheck(strm) |
||||||
|
z_streamp strm; |
||||||
|
{ |
||||||
|
struct inflate_state FAR* state; |
||||||
|
if (strm == Z_NULL || |
||||||
|
strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) |
||||||
|
return 1; |
||||||
|
state = (struct inflate_state FAR*)strm->state; |
||||||
|
if (state == Z_NULL || state->strm != strm || |
||||||
|
state->mode < HEAD || state->mode > SYNC) |
||||||
|
return 1; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Update the window with the last wsize (normally 32K) bytes written before |
||||||
|
returning. If window does not exist yet, create it. This is only called |
||||||
|
when a window is already in use, or when output has been written during this |
||||||
|
inflate call, but the end of the deflate stream has not been reached yet. |
||||||
|
It is also called to create a window for dictionary data when a dictionary |
||||||
|
is loaded. |
||||||
|
|
||||||
|
Providing output buffers larger than 32K to inflate() should provide a speed |
||||||
|
advantage, since only the last 32K of output is copied to the sliding window |
||||||
|
upon return from inflate(), and since all distances after the first 32K of |
||||||
|
output will fall in the output data, making match copies simpler and faster. |
||||||
|
The advantage may be dependent on the size of the processor's data caches. |
||||||
|
*/ |
||||||
|
local int updatewindow(strm, end, copy) |
||||||
|
z_streamp strm; |
||||||
|
const Bytef* end; |
||||||
|
unsigned copy; |
||||||
|
{ |
||||||
|
struct inflate_state FAR* state; |
||||||
|
unsigned dist; |
||||||
|
|
||||||
|
state = (struct inflate_state FAR*)strm->state; |
||||||
|
|
||||||
|
/* if it hasn't been done already, allocate space for the window */ |
||||||
|
if (state->window == Z_NULL) { |
||||||
|
state->window = (unsigned char FAR*) |
||||||
|
ZALLOC(strm, 1U << state->wbits, |
||||||
|
sizeof(unsigned char)); |
||||||
|
if (state->window == Z_NULL) return 1; |
||||||
|
} |
||||||
|
|
||||||
|
/* if window not in use yet, initialize */ |
||||||
|
if (state->wsize == 0) { |
||||||
|
state->wsize = 1U << state->wbits; |
||||||
|
state->wnext = 0; |
||||||
|
state->whave = 0; |
||||||
|
} |
||||||
|
|
||||||
|
/* copy state->wsize or less output bytes into the circular window */ |
||||||
|
if (copy >= state->wsize) { |
||||||
|
zmemcpy(state->window, end - state->wsize, state->wsize); |
||||||
|
state->wnext = 0; |
||||||
|
state->whave = state->wsize; |
||||||
|
} |
||||||
|
else { |
||||||
|
dist = state->wsize - state->wnext; |
||||||
|
if (dist > copy) dist = copy; |
||||||
|
zmemcpy(state->window + state->wnext, end - copy, dist); |
||||||
|
copy -= dist; |
||||||
|
if (copy) { |
||||||
|
zmemcpy(state->window, end - copy, copy); |
||||||
|
state->wnext = copy; |
||||||
|
state->whave = state->wsize; |
||||||
|
} |
||||||
|
else { |
||||||
|
state->wnext += dist; |
||||||
|
if (state->wnext == state->wsize) state->wnext = 0; |
||||||
|
if (state->whave < state->wsize) state->whave += dist; |
||||||
|
} |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/* Macros for inflateBackWrap(), all copied from inflate.c */ |
||||||
|
|
||||||
|
/* check function to use adler32() for zlib or crc32() for gzip */ |
||||||
|
#ifdef GUNZIP |
||||||
|
# define UPDATE_CHECK(check, buf, len) \ |
||||||
|
(state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) |
||||||
|
#else |
||||||
|
# define UPDATE_CHECK(check, buf, len) adler32(check, buf, len) |
||||||
|
#endif |
||||||
|
|
||||||
|
/* check macros for header crc */ |
||||||
|
#ifdef GUNZIP |
||||||
|
# define CRC2(check, word) \ |
||||||
|
do { \
|
||||||
|
hbuf[0] = (unsigned char)(word); \
|
||||||
|
hbuf[1] = (unsigned char)((word) >> 8); \
|
||||||
|
check = crc32(check, hbuf, 2); \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
# define CRC4(check, word) \ |
||||||
|
do { \
|
||||||
|
hbuf[0] = (unsigned char)(word); \
|
||||||
|
hbuf[1] = (unsigned char)((word) >> 8); \
|
||||||
|
hbuf[2] = (unsigned char)((word) >> 16); \
|
||||||
|
hbuf[3] = (unsigned char)((word) >> 24); \
|
||||||
|
check = crc32(check, hbuf, 4); \
|
||||||
|
} while (0) |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Load registers with state in inflate() for speed */ |
||||||
|
#define LOAD() \ |
||||||
|
do { \
|
||||||
|
next = strm->next_in; \
|
||||||
|
have = strm->avail_in; \
|
||||||
|
hold = state->hold; \
|
||||||
|
bits = state->bits; \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Restore state from registers in inflate() */ |
||||||
|
#define RESTORE() \ |
||||||
|
do { \
|
||||||
|
strm->next_out = out_buf.bufPtr; \
|
||||||
|
strm->avail_out = out_buf.bufPtrEnd - out_buf.bufPtr; \
|
||||||
|
strm->next_in = next; \
|
||||||
|
strm->avail_in = have; \
|
||||||
|
state->hold = hold; \
|
||||||
|
state->bits = bits; \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Clear the input bit accumulator */ |
||||||
|
#define INITBITS() \ |
||||||
|
do { \
|
||||||
|
hold = 0; \
|
||||||
|
bits = 0; \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Get a byte of input into the bit accumulator, or return from inflate()
|
||||||
|
if there is no input available. */ |
||||||
|
#define PULLBYTE() \ |
||||||
|
do { \
|
||||||
|
if (have == 0) goto inf_leave; \
|
||||||
|
have--; \
|
||||||
|
hold += (unsigned long)(*next++) << bits; \
|
||||||
|
bits += 8; \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Assure that there are at least n bits in the bit accumulator. If there is
|
||||||
|
not enough available input to do that, then return from inflate(). */ |
||||||
|
#define NEEDBITS(n) \ |
||||||
|
do { \
|
||||||
|
while (bits < (unsigned)(n)) \
|
||||||
|
PULLBYTE(); \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Return the low n bits of the bit accumulator (n < 16) */ |
||||||
|
#define BITS(n) \ |
||||||
|
((unsigned)hold & ((1U << (n)) - 1)) |
||||||
|
|
||||||
|
/* Remove n bits from the bit accumulator */ |
||||||
|
#define DROPBITS(n) \ |
||||||
|
do { \
|
||||||
|
hold >>= (n); \
|
||||||
|
bits -= (unsigned)(n); \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/* Remove zero to seven bits as needed to go to a byte boundary */ |
||||||
|
#define BYTEBITS() \ |
||||||
|
do { \
|
||||||
|
hold >>= bits & 7; \
|
||||||
|
bits -= bits & 7; \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
/*
|
||||||
|
inflate()/inflateBackWrap() uses a state machine to process as much input data and generate as |
||||||
|
much output data as possible before returning. The state machine is |
||||||
|
structured roughly as follows: |
||||||
|
|
||||||
|
for (;;) switch (state) { |
||||||
|
... |
||||||
|
case STATEn: |
||||||
|
if (not enough input data or output space to make progress) |
||||||
|
return; |
||||||
|
... make progress ... |
||||||
|
state = STATEm; |
||||||
|
break; |
||||||
|
... |
||||||
|
} |
||||||
|
|
||||||
|
so when inflate() is called again, the same case is attempted again, and |
||||||
|
if the appropriate resources are provided, the machine proceeds to the |
||||||
|
next state. The NEEDBITS() macro is usually the way the state evaluates |
||||||
|
whether it can proceed or should return. NEEDBITS() does the return if |
||||||
|
the requested bits are not available. The typical use of the BITS macros |
||||||
|
is: |
||||||
|
|
||||||
|
NEEDBITS(n); |
||||||
|
... do something with BITS(n) ... |
||||||
|
DROPBITS(n); |
||||||
|
|
||||||
|
where NEEDBITS(n) either returns from inflate() if there isn't enough |
||||||
|
input left to load n bits into the accumulator, or it continues. BITS(n) |
||||||
|
gives the low n bits in the accumulator. When done, DROPBITS(n) drops |
||||||
|
the low n bits off the accumulator. INITBITS() clears the accumulator |
||||||
|
and sets the number of available bits to zero. BYTEBITS() discards just |
||||||
|
enough bits to put the accumulator on a byte boundary. After BYTEBITS() |
||||||
|
and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. |
||||||
|
|
||||||
|
NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return |
||||||
|
if there is no input available. The decoding of variable length codes uses |
||||||
|
PULLBYTE() directly in order to pull just enough bytes to decode the next |
||||||
|
code, and no more. |
||||||
|
|
||||||
|
Some states loop until they get enough input, making sure that enough |
||||||
|
state information is maintained to continue the loop where it left off |
||||||
|
if NEEDBITS() returns in the loop. For example, want, need, and keep |
||||||
|
would all have to actually be part of the saved state in case NEEDBITS() |
||||||
|
returns: |
||||||
|
|
||||||
|
case STATEw: |
||||||
|
while (want < need) { |
||||||
|
NEEDBITS(n); |
||||||
|
keep[want++] = BITS(n); |
||||||
|
DROPBITS(n); |
||||||
|
} |
||||||
|
state = STATEx; |
||||||
|
case STATEx: |
||||||
|
|
||||||
|
As shown above, if the next state is also the next case, then the break |
||||||
|
is omitted. |
||||||
|
|
||||||
|
A state may also return if there is not enough output space available to |
||||||
|
complete that state. Those states are copying stored data, writing a |
||||||
|
literal byte, and copying a matching string. |
||||||
|
|
||||||
|
When returning, a "goto inf_leave" is used to update the total counters, |
||||||
|
update the check value, and determine whether any progress has been made |
||||||
|
during that inflate() call in order to return the proper return code. |
||||||
|
Progress is defined as a change in either strm->avail_in or strm->avail_out. |
||||||
|
When there is a window, goto inf_leave will update the window with the last |
||||||
|
output written. If a goto inf_leave occurs in the middle of decompression |
||||||
|
and there is no window currently, goto inf_leave will create one and copy |
||||||
|
output to the window for the next call of inflate(). |
||||||
|
|
||||||
|
In this implementation, the flush parameter of inflate() only affects the |
||||||
|
return code (per zlib.h). inflate() always writes as much as possible to |
||||||
|
strm->next_out, given the space available and the provided input--the effect |
||||||
|
documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers |
||||||
|
the allocation of and copying into a sliding window until necessary, which |
||||||
|
provides the effect documented in zlib.h for Z_FINISH when the entire input |
||||||
|
stream available. So the only thing the flush parameter actually does is: |
||||||
|
when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
||||||
|
will return Z_BUF_ERROR if it has not reached the end of the stream. |
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
/* Wrapper function of inflateBack():
|
||||||
|
* It is mostly copied from, and carries out the same functionality as, inflate(). |
||||||
|
* It allows for stand-alone validation of infateBack(). |
||||||
|
* it allows for the performance comparison of infateBackWrap() vs. |
||||||
|
* inflate() under different system environments. |
||||||
|
*/ |
||||||
|
|
||||||
|
typedef struct { |
||||||
|
unsigned char* bufPtr; |
||||||
|
unsigned char* bufPtrEnd; |
||||||
|
} OutBuffer; |
||||||
|
|
||||||
|
/* Call - back input function for inflateBack() */ |
||||||
|
local unsigned inb(void* desc, unsigned char** buf) |
||||||
|
{ |
||||||
|
z_stream* strm = (z_stream*)desc; |
||||||
|
|
||||||
|
*buf = strm->next_in; |
||||||
|
unsigned len = strm->avail_in > UINT_MAX ? UINT_MAX : (unsigned)strm->avail_in; |
||||||
|
strm->next_in += len; |
||||||
|
strm->avail_in -= len; |
||||||
|
return len; |
||||||
|
} |
||||||
|
|
||||||
|
/* Call - back output function for inflateBack() */ |
||||||
|
local int outb(void* desc, unsigned char* buf, unsigned len) |
||||||
|
{ |
||||||
|
OutBuffer* outBuf = (OutBuffer*)desc; |
||||||
|
if (outBuf->bufPtr + len > outBuf->bufPtrEnd) |
||||||
|
return 1; |
||||||
|
|
||||||
|
zmemcpy(outBuf->bufPtr, buf, len); |
||||||
|
outBuf->bufPtr += len; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
int ZEXPORT inflateBackWrap(strm, flush) |
||||||
|
z_streamp strm; |
||||||
|
int flush; |
||||||
|
{ |
||||||
|
struct inflate_state FAR* state; |
||||||
|
z_const unsigned char FAR* next; /* next input */ |
||||||
|
unsigned have; /* available input size */ |
||||||
|
unsigned long hold; /* bit buffer */ |
||||||
|
unsigned bits; /* bits in bit buffer */ |
||||||
|
unsigned in, out; /* save starting available input and output */ |
||||||
|
unsigned copy; /* number of stored or match bytes to copy */ |
||||||
|
unsigned len; /* length to copy for repeats, bits to drop */ |
||||||
|
int ret; /* return code */ |
||||||
|
|
||||||
|
#ifdef GUNZIP |
||||||
|
unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ |
||||||
|
#endif |
||||||
|
|
||||||
|
if (inflateStateCheck(strm) || strm->next_out == Z_NULL || |
||||||
|
(strm->next_in == Z_NULL && strm->avail_in != 0)) |
||||||
|
return Z_STREAM_ERROR; |
||||||
|
|
||||||
|
unsigned char dummyDict[] = ""; /*dummy Dictionary to initialize window*/ |
||||||
|
updatewindow(strm, dummyDict, 0); |
||||||
|
OutBuffer out_buf; |
||||||
|
out_buf.bufPtr = strm->next_out; |
||||||
|
out_buf.bufPtrEnd = strm->next_out + strm->avail_out; |
||||||
|
|
||||||
|
state = (struct inflate_state FAR*)strm->state; |
||||||
|
if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ |
||||||
|
LOAD(); |
||||||
|
in = have; |
||||||
|
out = strm->avail_out; |
||||||
|
ret = Z_OK; |
||||||
|
for (;;) |
||||||
|
switch (state->mode) { |
||||||
|
case HEAD: |
||||||
|
if (state->wrap == 0) { |
||||||
|
state->mode = TYPEDO; |
||||||
|
break; |
||||||
|
} |
||||||
|
NEEDBITS(16); |
||||||
|
#ifdef GUNZIP |
||||||
|
if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ |
||||||
|
if (state->wbits == 0) |
||||||
|
state->wbits = 15; |
||||||
|
state->check = crc32(0L, Z_NULL, 0); |
||||||
|
CRC2(state->check, hold); |
||||||
|
INITBITS(); |
||||||
|
state->mode = FLAGS; |
||||||
|
break; |
||||||
|
} |
||||||
|
if (state->head != Z_NULL) |
||||||
|
state->head->done = -1; |
||||||
|
if (!(state->wrap & 1) || /* check if zlib header allowed */ |
||||||
|
#else |
||||||
|
if ( |
||||||
|
#endif |
||||||
|
((BITS(8) << 8) + (hold >> 8)) % 31) { |
||||||
|
strm->msg = (char*)"incorrect header check"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
if (BITS(4) != Z_DEFLATED) { |
||||||
|
strm->msg = (char*)"unknown compression method"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
DROPBITS(4); |
||||||
|
len = BITS(4) + 8; |
||||||
|
if (state->wbits == 0) |
||||||
|
state->wbits = len; |
||||||
|
if (len > 15 || len > state->wbits) { |
||||||
|
strm->msg = (char*)"invalid window size"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
state->dmax = 1U << len; |
||||||
|
state->flags = 0; /* indicate zlib header */ |
||||||
|
Tracev((stderr, "inflate: zlib header ok\n")); |
||||||
|
strm->adler = state->check = adler32(0L, Z_NULL, 0); |
||||||
|
state->mode = hold & 0x200 ? DICTID : TYPE; |
||||||
|
INITBITS(); |
||||||
|
break; |
||||||
|
#ifdef GUNZIP |
||||||
|
case FLAGS: |
||||||
|
NEEDBITS(16); |
||||||
|
state->flags = (int)(hold); |
||||||
|
if ((state->flags & 0xff) != Z_DEFLATED) { |
||||||
|
strm->msg = (char*)"unknown compression method"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
if (state->flags & 0xe000) { |
||||||
|
strm->msg = (char*)"unknown header flags set"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
if (state->head != Z_NULL) |
||||||
|
state->head->text = (int)((hold >> 8) & 1); |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
CRC2(state->check, hold); |
||||||
|
INITBITS(); |
||||||
|
state->mode = TIME; |
||||||
|
/* fallthrough */ |
||||||
|
case TIME: |
||||||
|
NEEDBITS(32); |
||||||
|
if (state->head != Z_NULL) |
||||||
|
state->head->time = hold; |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
CRC4(state->check, hold); |
||||||
|
INITBITS(); |
||||||
|
state->mode = OS; |
||||||
|
/* fallthrough */ |
||||||
|
case OS: |
||||||
|
NEEDBITS(16); |
||||||
|
if (state->head != Z_NULL) { |
||||||
|
state->head->xflags = (int)(hold & 0xff); |
||||||
|
state->head->os = (int)(hold >> 8); |
||||||
|
} |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
CRC2(state->check, hold); |
||||||
|
INITBITS(); |
||||||
|
state->mode = EXLEN; |
||||||
|
/* fallthrough */ |
||||||
|
case EXLEN: |
||||||
|
if (state->flags & 0x0400) { |
||||||
|
NEEDBITS(16); |
||||||
|
state->length = (unsigned)(hold); |
||||||
|
if (state->head != Z_NULL) |
||||||
|
state->head->extra_len = (unsigned)hold; |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
CRC2(state->check, hold); |
||||||
|
INITBITS(); |
||||||
|
} |
||||||
|
else if (state->head != Z_NULL) |
||||||
|
state->head->extra = Z_NULL; |
||||||
|
state->mode = EXTRA; |
||||||
|
/* fallthrough */ |
||||||
|
case EXTRA: |
||||||
|
if (state->flags & 0x0400) { |
||||||
|
copy = state->length; |
||||||
|
if (copy > have) copy = have; |
||||||
|
if (copy) { |
||||||
|
if (state->head != Z_NULL && |
||||||
|
state->head->extra != Z_NULL && |
||||||
|
(len = state->head->extra_len - state->length) < |
||||||
|
state->head->extra_max) { |
||||||
|
zmemcpy(state->head->extra + len, next, |
||||||
|
len + copy > state->head->extra_max ? |
||||||
|
state->head->extra_max - len : copy); |
||||||
|
} |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
state->check = crc32(state->check, next, copy); |
||||||
|
have -= copy; |
||||||
|
next += copy; |
||||||
|
state->length -= copy; |
||||||
|
} |
||||||
|
if (state->length) goto inf_leave; |
||||||
|
} |
||||||
|
state->length = 0; |
||||||
|
state->mode = NAME; |
||||||
|
/* fallthrough */ |
||||||
|
case NAME: |
||||||
|
if (state->flags & 0x0800) { |
||||||
|
if (have == 0) goto inf_leave; |
||||||
|
copy = 0; |
||||||
|
do { |
||||||
|
len = (unsigned)(next[copy++]); |
||||||
|
if (state->head != Z_NULL && |
||||||
|
state->head->name != Z_NULL && |
||||||
|
state->length < state->head->name_max) |
||||||
|
state->head->name[state->length++] = (Bytef)len; |
||||||
|
} while (len && copy < have); |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
state->check = crc32(state->check, next, copy); |
||||||
|
have -= copy; |
||||||
|
next += copy; |
||||||
|
if (len) goto inf_leave; |
||||||
|
} |
||||||
|
else if (state->head != Z_NULL) |
||||||
|
state->head->name = Z_NULL; |
||||||
|
state->length = 0; |
||||||
|
state->mode = COMMENT; |
||||||
|
/* fallthrough */ |
||||||
|
case COMMENT: |
||||||
|
if (state->flags & 0x1000) { |
||||||
|
if (have == 0) goto inf_leave; |
||||||
|
copy = 0; |
||||||
|
do { |
||||||
|
len = (unsigned)(next[copy++]); |
||||||
|
if (state->head != Z_NULL && |
||||||
|
state->head->comment != Z_NULL && |
||||||
|
state->length < state->head->comm_max) |
||||||
|
state->head->comment[state->length++] = (Bytef)len; |
||||||
|
} while (len && copy < have); |
||||||
|
if ((state->flags & 0x0200) && (state->wrap & 4)) |
||||||
|
state->check = crc32(state->check, next, copy); |
||||||
|
have -= copy; |
||||||
|
next += copy; |
||||||
|
if (len) goto inf_leave; |
||||||
|
} |
||||||
|
else if (state->head != Z_NULL) |
||||||
|
state->head->comment = Z_NULL; |
||||||
|
state->mode = HCRC; |
||||||
|
/* fallthrough */ |
||||||
|
case HCRC: |
||||||
|
if (state->flags & 0x0200) { |
||||||
|
NEEDBITS(16); |
||||||
|
if ((state->wrap & 4) && hold != (state->check & 0xffff)) { |
||||||
|
strm->msg = (char*)"header crc mismatch"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
INITBITS(); |
||||||
|
} |
||||||
|
if (state->head != Z_NULL) { |
||||||
|
state->head->hcrc = (int)((state->flags >> 9) & 1); |
||||||
|
state->head->done = 1; |
||||||
|
} |
||||||
|
strm->adler = state->check = crc32(0L, Z_NULL, 0); |
||||||
|
state->mode = TYPE; |
||||||
|
break; |
||||||
|
#endif |
||||||
|
case DICTID: |
||||||
|
NEEDBITS(32); |
||||||
|
strm->adler = state->check = ZSWAP32(hold); |
||||||
|
INITBITS(); |
||||||
|
state->mode = DICT; |
||||||
|
/* fallthrough */ |
||||||
|
case DICT: |
||||||
|
if (state->havedict == 0) { |
||||||
|
RESTORE(); |
||||||
|
return Z_NEED_DICT; |
||||||
|
} |
||||||
|
strm->adler = state->check = adler32(0L, Z_NULL, 0); |
||||||
|
state->mode = TYPE; |
||||||
|
/* fallthrough */ |
||||||
|
case TYPE: |
||||||
|
if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; |
||||||
|
|
||||||
|
|
||||||
|
/* Call inflateBack() */ |
||||||
|
RESTORE(); |
||||||
|
ret = inflateBack(strm, inb, strm, outb, &out_buf); |
||||||
|
if (ret == Z_DATA_ERROR) { |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
LOAD(); |
||||||
|
state->mode = CHECK; |
||||||
|
/* fall through */ |
||||||
|
|
||||||
|
case CHECK: |
||||||
|
if (state->wrap) { |
||||||
|
NEEDBITS(32); |
||||||
|
strm->next_out = out_buf.bufPtr; |
||||||
|
strm->avail_out = out_buf.bufPtrEnd - out_buf.bufPtr; |
||||||
|
out -= strm->avail_out; |
||||||
|
strm->total_out += out; |
||||||
|
state->total += out; |
||||||
|
if ((state->wrap & 4) && out) |
||||||
|
strm->adler = state->check = |
||||||
|
UPDATE_CHECK(state->check, strm->next_out - out, out); |
||||||
|
out = strm->avail_out; |
||||||
|
if ((state->wrap & 4) && ( |
||||||
|
#ifdef GUNZIP |
||||||
|
state->flags ? hold : |
||||||
|
#endif |
||||||
|
ZSWAP32(hold)) != state->check) { |
||||||
|
strm->msg = (char*)"incorrect data check"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
INITBITS(); |
||||||
|
Tracev((stderr, "inflate: check matches trailer\n")); |
||||||
|
} |
||||||
|
#ifdef GUNZIP |
||||||
|
state->mode = LENGTH; |
||||||
|
/* fallthrough */ |
||||||
|
case LENGTH: |
||||||
|
if (state->wrap && state->flags) { |
||||||
|
NEEDBITS(32); |
||||||
|
if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) { |
||||||
|
strm->msg = (char*)"incorrect length check"; |
||||||
|
state->mode = BAD; |
||||||
|
break; |
||||||
|
} |
||||||
|
INITBITS(); |
||||||
|
Tracev((stderr, "inflate: length matches trailer\n")); |
||||||
|
} |
||||||
|
#endif |
||||||
|
state->mode = DONE; |
||||||
|
/* fallthrough */ |
||||||
|
case DONE: |
||||||
|
ret = Z_STREAM_END; |
||||||
|
goto inf_leave; |
||||||
|
case BAD: |
||||||
|
ret = Z_DATA_ERROR; |
||||||
|
goto inf_leave; |
||||||
|
case MEM: |
||||||
|
return Z_MEM_ERROR; |
||||||
|
case SYNC: |
||||||
|
/* fallthrough */ |
||||||
|
default: |
||||||
|
return Z_STREAM_ERROR; |
||||||
|
} |
||||||
|
|
||||||
|
/*Return from inflate()/inflateBackWrap(), updating the total counts and the check value.
|
||||||
|
If there was no progress during the inflate() call, return a buffer |
||||||
|
error. Call updatewindow() to create and/or update the window state. |
||||||
|
Note: a memory error from inflate() is non-recoverable. |
||||||
|
*/ |
||||||
|
inf_leave: |
||||||
|
RESTORE(); |
||||||
|
if (state->wsize || (out != strm->avail_out && state->mode < BAD && |
||||||
|
(state->mode < CHECK || flush != Z_FINISH))) |
||||||
|
if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { |
||||||
|
state->mode = MEM; |
||||||
|
return Z_MEM_ERROR; |
||||||
|
} |
||||||
|
in -= strm->avail_in; |
||||||
|
out -= strm->avail_out; |
||||||
|
strm->total_in += in; |
||||||
|
strm->total_out += out; |
||||||
|
state->total += out; |
||||||
|
if ((state->wrap & 4) && out) |
||||||
|
strm->adler = state->check = |
||||||
|
UPDATE_CHECK(state->check, strm->next_out - out, out); |
||||||
|
strm->data_type = (int)state->bits + (state->last ? 64 : 0) + |
||||||
|
(state->mode == TYPE ? 128 : 0) + |
||||||
|
(state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); |
||||||
|
if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
||||||
|
ret = Z_BUF_ERROR; |
||||||
|
return ret; |
||||||
|
} |
Loading…
Reference in new issue