📄 inflate.c
字号:
printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
state.distcode[low].val);
if (++low == size) break;
putchar(',');
}
puts("\n };");
}
#endif /* MAKEFIXED */
/*
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, out)
z_streamp strm;
unsigned out;
{
struct inflate_state FAR *state;
unsigned copy, 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->write = 0;
state->whave = 0;
}
/* copy state->wsize or less output bytes into the circular window */
copy = out - strm->avail_out;
if (copy >= state->wsize) {
zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
state->write = 0;
state->whave = state->wsize;
}
else {
dist = state->wsize - state->write;
if (dist > copy) dist = copy;
zmemcpy(state->window + state->write, strm->next_out - copy, dist);
copy -= dist;
if (copy) {
zmemcpy(state->window, strm->next_out - copy, copy);
state->write = copy;
state->whave = state->wsize;
}
else {
state->write += dist;
if (state->write == state->wsize) state->write = 0;
if (state->whave < state->wsize) state->whave += dist;
}
}
return 0;
}
/* Macros for inflate(): */
/* check function to use adler32() for zlib or crc32() for gzip */
#ifdef GUNZIP
# define UPDATE(check, buf, len) \
(state->headerFlags ? crc32(check, buf, len) : adler32(check, buf, len))
#else
# define UPDATE(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 { \
put = strm->next_out; \
left = strm->avail_out; \
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 = put; \
strm->avail_out = left; \
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)
/* Reverse the bytes in a 32-bit value */
#define REVERSE(q) \
((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
/*
inflate() 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.
*/
#ifdef INFLATE_OMP
#if defined (_OPENMP)
# define SWAP_PAIRS(pPairConstEx, pPairConstExMT, pairsIndEx, pairsIndExMT, pairsLenConstEx, pairsLenConstExMT, state) \
ippsDecodeLZ77GetPairs_8u(&pPairConstEx, &pairsIndEx, &pairsLenConstEx, state->ipp_state); \
ippsDecodeLZ77GetPairs_8u(&pPairConstExMT, &pairsIndExMT, &pairsLenConstExMT, state->ipp_stateMT); \
ippsDecodeLZ77SetPairs_8u(pPairConstExMT, pairsIndExMT, pairsLenConstExMT, state->ipp_state); \
ippsDecodeLZ77SetPairs_8u(pPairConstEx, pairsIndEx, pairsLenConstEx, state->ipp_stateMT); \
(state->firstSwapDone)++;
#endif
#endif
#if defined (INFLATE_OMP) && defined (_OPENMP) /* parallel implementaion of inflate() function */
int ZEXPORT inflate(strm, flush)
z_streamp strm;
int flush;
{
struct inflate_state FAR *state;
int ret;
unsigned int in, out; /* save starting available input and output */
IppLZ77State_8u* pLZ77State;
IppLZ77State_8u* pLZ77StateMT;
IppLZ77InflateStatus inflateStatus;
IppStatus retStatus;
IppLZ77Flush ippflush;
IppLZ77Pair* pPairConst;
IppLZ77Pair* pPair;
int pairsLenConst;
int pairsInd;
Ipp8u* pSrc;
int srcLen;
Ipp8u* pDst;
int dstLen;
IppStatus retStatusMT;
IppLZ77Pair* pPairConstMT;
IppLZ77Pair* pPairMT;
int pairsLenConstMT;
int pairsIndMT;
IppLZ77Pair* pPairConstSW;
int pairsLenConstSW;
int pairsIndSW;
IppLZ77Pair* pPairConstSWMT;
int pairsLenConstSWMT;
int pairsIndSWMT;
int srcLenMT;
int dstLenMT;
IppStatus decodeHuffStatus;
IppStatus decodeLZ77Status;
IppLZ77HuffMode huffMode;
int i;
int copy;
unsigned int len;
in = strm->avail_in;
out = strm->avail_out;
state = (struct inflate_state FAR *) strm->state;
pLZ77State = (IppLZ77State_8u*) state->ipp_state;
pLZ77StateMT = (IppLZ77State_8u*) state->ipp_stateMT;
ippflush = IppLZ77NoFlush;
next_iteration:
ippsDecodeLZ77GetStatus_8u(&inflateStatus, pLZ77State);
ippsDecodeLZ77GetPairs_8u(&pPairConst, &pairsInd, &pairsLenConst, pLZ77State);
if(inflateStatus == IppLZ77InflateStatusInit)
{
/* processing GZIP or ZLIB header */
for( ; ; )
{
if(state->headerMode == infhead)
{
if (state->wrap == 0)
{
state->headerBits = 0;
state->headerBuf = 0;
state->headerMode = infmain;
break;
} /* if */
while(state->headerBits < 16)
{
if(strm->avail_in == 0)
{
ret = Z_OK;
goto inf_leave;
} /* if */
(strm->avail_in)--;
state->headerBuf += ((*(strm->next_in)) << state->headerBits);
(strm->next_in)++;
state->headerBits += 8;
} /* while */
#ifdef GUNZIP
if( state->wrap & 2 )
{ /* gzip header */
if (state->headerBuf == 0x8b1f)
{
state->check = crc32(0, Z_NULL, 0);
state->headerBuf = 0;
state->headerBits = 0;
state->headerMode = infflags;
}
else
{
ret = Z_DATA_ERROR;
goto inf_leave;
} /* if else */
}
if (!(state->wrap & 1) || /* check if zlib header allowed */
#else
if(
#endif
(((state->headerBuf & 0xff) << 8) + ((state->headerBuf)>>8))%31 )
{
ret = Z_DATA_ERROR;
goto inf_leave;
}
else
{
strm->adler = state->check = adler32(0, Z_NULL, 0);
state->headerMode = infmain;
if( (state->headerBuf >> 4) & 0x200) state->headerMode = infdictid;
state->headerBuf = 0;
state->headerBits = 0;
} /* if else */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -