+++ /dev/null
-/* inflate.c -- zlib decompression\r
- * Copyright (C) 1995-2012 Mark Adler\r
- * For conditions of distribution and use, see copyright notice in zlib.h\r
- */\r
-\r
-/*\r
- * Change history:\r
- *\r
- * 1.2.beta0 24 Nov 2002\r
- * - First version -- complete rewrite of inflate to simplify code, avoid\r
- * creation of window when not needed, minimize use of window when it is\r
- * needed, make inffast.c even faster, implement gzip decoding, and to\r
- * improve code readability and style over the previous zlib inflate code\r
- *\r
- * 1.2.beta1 25 Nov 2002\r
- * - Use pointers for available input and output checking in inffast.c\r
- * - Remove input and output counters in inffast.c\r
- * - Change inffast.c entry and loop from avail_in >= 7 to >= 6\r
- * - Remove unnecessary second byte pull from length extra in inffast.c\r
- * - Unroll direct copy to three copies per loop in inffast.c\r
- *\r
- * 1.2.beta2 4 Dec 2002\r
- * - Change external routine names to reduce potential conflicts\r
- * - Correct filename to inffixed.h for fixed tables in inflate.c\r
- * - Make hbuf[] unsigned char to match parameter type in inflate.c\r
- * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)\r
- * to avoid negation problem on Alphas (64 bit) in inflate.c\r
- *\r
- * 1.2.beta3 22 Dec 2002\r
- * - Add comments on state->bits assertion in inffast.c\r
- * - Add comments on op field in inftrees.h\r
- * - Fix bug in reuse of allocated window after inflateReset()\r
- * - Remove bit fields--back to byte structure for speed\r
- * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths\r
- * - Change post-increments to pre-increments in inflate_fast(), PPC biased?\r
- * - Add compile time option, POSTINC, to use post-increments instead (Intel?)\r
- * - Make MATCH copy in inflate() much faster for when inflate_fast() not used\r
- * - Use local copies of stream next and avail values, as well as local bit\r
- * buffer and bit count in inflate()--for speed when inflate_fast() not used\r
- *\r
- * 1.2.beta4 1 Jan 2003\r
- * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings\r
- * - Move a comment on output buffer sizes from inffast.c to inflate.c\r
- * - Add comments in inffast.c to introduce the inflate_fast() routine\r
- * - Rearrange window copies in inflate_fast() for speed and simplification\r
- * - Unroll last copy for window match in inflate_fast()\r
- * - Use local copies of window variables in inflate_fast() for speed\r
- * - Pull out common wnext == 0 case for speed in inflate_fast()\r
- * - Make op and len in inflate_fast() unsigned for consistency\r
- * - Add FAR to lcode and dcode declarations in inflate_fast()\r
- * - Simplified bad distance check in inflate_fast()\r
- * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new\r
- * source file infback.c to provide a call-back interface to inflate for\r
- * programs like gzip and unzip -- uses window as output buffer to avoid\r
- * window copying\r
- *\r
- * 1.2.beta5 1 Jan 2003\r
- * - Improved inflateBack() interface to allow the caller to provide initial\r
- * input in strm.\r
- * - Fixed stored blocks bug in inflateBack()\r
- *\r
- * 1.2.beta6 4 Jan 2003\r
- * - Added comments in inffast.c on effectiveness of POSTINC\r
- * - Typecasting all around to reduce compiler warnings\r
- * - Changed loops from while (1) or do {} while (1) to for (;;), again to\r
- * make compilers happy\r
- * - Changed type of window in inflateBackInit() to unsigned char *\r
- *\r
- * 1.2.beta7 27 Jan 2003\r
- * - Changed many types to unsigned or unsigned short to avoid warnings\r
- * - Added inflateCopy() function\r
- *\r
- * 1.2.0 9 Mar 2003\r
- * - Changed inflateBack() interface to provide separate opaque descriptors\r
- * for the in() and out() functions\r
- * - Changed inflateBack() argument and in_func typedef to swap the length\r
- * and buffer address return values for the input function\r
- * - Check next_in and next_out for Z_NULL on entry to inflate()\r
- *\r
- * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.\r
- */\r
-\r
-#include "zutil.h"\r
-#include "inftrees.h"\r
-#include "inflate.h"\r
-#include "inffast.h"\r
-\r
-#ifdef MAKEFIXED\r
-# ifndef BUILDFIXED\r
-# define BUILDFIXED\r
-# endif\r
-#endif\r
-\r
-/* function prototypes */\r
-local void fixedtables OF((struct inflate_state FAR *state));\r
-local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,\r
- unsigned copy));\r
-#ifdef BUILDFIXED\r
- void makefixed OF((void));\r
-#endif\r
-local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,\r
- unsigned len));\r
-\r
-int ZEXPORT inflateResetKeep(strm)\r
-z_streamp strm;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- strm->total_in = strm->total_out = state->total = 0;\r
- strm->msg = Z_NULL;\r
- if (state->wrap) /* to support ill-conceived Java test suite */\r
- strm->adler = state->wrap & 1;\r
- state->mode = HEAD;\r
- state->last = 0;\r
- state->havedict = 0;\r
- state->dmax = 32768U;\r
- state->head = Z_NULL;\r
- state->hold = 0;\r
- state->bits = 0;\r
- state->lencode = state->distcode = state->next = state->codes;\r
- state->sane = 1;\r
- state->back = -1;\r
- Tracev((stderr, "inflate: reset\n"));\r
- return Z_OK;\r
-}\r
-\r
-int ZEXPORT inflateReset(strm)\r
-z_streamp strm;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- state->wsize = 0;\r
- state->whave = 0;\r
- state->wnext = 0;\r
- return inflateResetKeep(strm);\r
-}\r
-\r
-int ZEXPORT inflateReset2(strm, windowBits)\r
-z_streamp strm;\r
-int windowBits;\r
-{\r
- int wrap;\r
- struct inflate_state FAR *state;\r
-\r
- /* get the state */\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
-\r
- /* extract wrap request from windowBits parameter */\r
- if (windowBits < 0) {\r
- wrap = 0;\r
- windowBits = -windowBits;\r
- }\r
- else {\r
- wrap = (windowBits >> 4) + 1;\r
-#ifdef GUNZIP\r
- if (windowBits < 48)\r
- windowBits &= 15;\r
-#endif\r
- }\r
-\r
- /* set number of window bits, free window if different */\r
- if (windowBits && (windowBits < 8 || windowBits > 15))\r
- return Z_STREAM_ERROR;\r
- if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {\r
- ZFREE(strm, state->window);\r
- state->window = Z_NULL;\r
- }\r
-\r
- /* update state and reset the rest of it */\r
- state->wrap = wrap;\r
- state->wbits = (unsigned)windowBits;\r
- return inflateReset(strm);\r
-}\r
-\r
-int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)\r
-z_streamp strm;\r
-int windowBits;\r
-const char *version;\r
-int stream_size;\r
-{\r
- int ret;\r
- struct inflate_state FAR *state;\r
-\r
- if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||\r
- stream_size != (int)(sizeof(z_stream)))\r
- return Z_VERSION_ERROR;\r
- if (strm == Z_NULL) return Z_STREAM_ERROR;\r
- strm->msg = Z_NULL; /* in case we return an error */\r
- if (strm->zalloc == (alloc_func)0) {\r
-#ifdef Z_SOLO\r
- return Z_STREAM_ERROR;\r
-#else\r
- strm->zalloc = zcalloc;\r
- strm->opaque = (voidpf)0;\r
-#endif\r
- }\r
- if (strm->zfree == (free_func)0)\r
-#ifdef Z_SOLO\r
- return Z_STREAM_ERROR;\r
-#else\r
- strm->zfree = zcfree;\r
-#endif\r
- state = (struct inflate_state FAR *)\r
- ZALLOC(strm, 1, sizeof(struct inflate_state));\r
- if (state == Z_NULL) return Z_MEM_ERROR;\r
- Tracev((stderr, "inflate: allocated\n"));\r
- strm->state = (struct internal_state FAR *)state;\r
- state->window = Z_NULL;\r
- ret = inflateReset2(strm, windowBits);\r
- if (ret != Z_OK) {\r
- ZFREE(strm, state);\r
- strm->state = Z_NULL;\r
- }\r
- return ret;\r
-}\r
-\r
-int ZEXPORT inflateInit_(strm, version, stream_size)\r
-z_streamp strm;\r
-const char *version;\r
-int stream_size;\r
-{\r
- return inflateInit2_(strm, DEF_WBITS, version, stream_size);\r
-}\r
-\r
-int ZEXPORT inflatePrime(strm, bits, value)\r
-z_streamp strm;\r
-int bits;\r
-int value;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- if (bits < 0) {\r
- state->hold = 0;\r
- state->bits = 0;\r
- return Z_OK;\r
- }\r
- if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;\r
- value &= (1L << bits) - 1;\r
- state->hold += value << state->bits;\r
- state->bits += bits;\r
- return Z_OK;\r
-}\r
-\r
-/*\r
- Return state with length and distance decoding tables and index sizes set to\r
- fixed code decoding. Normally this returns fixed tables from inffixed.h.\r
- If BUILDFIXED is defined, then instead this routine builds the tables the\r
- first time it's called, and returns those tables the first time and\r
- thereafter. This reduces the size of the code by about 2K bytes, in\r
- exchange for a little execution time. However, BUILDFIXED should not be\r
- used for threaded applications, since the rewriting of the tables and virgin\r
- may not be thread-safe.\r
- */\r
-local void fixedtables(state)\r
-struct inflate_state FAR *state;\r
-{\r
-#ifdef BUILDFIXED\r
- static int virgin = 1;\r
- static code *lenfix, *distfix;\r
- static code fixed[544];\r
-\r
- /* build fixed huffman tables if first call (may not be thread safe) */\r
- if (virgin) {\r
- unsigned sym, bits;\r
- static code *next;\r
-\r
- /* literal/length table */\r
- sym = 0;\r
- while (sym < 144) state->lens[sym++] = 8;\r
- while (sym < 256) state->lens[sym++] = 9;\r
- while (sym < 280) state->lens[sym++] = 7;\r
- while (sym < 288) state->lens[sym++] = 8;\r
- next = fixed;\r
- lenfix = next;\r
- bits = 9;\r
- inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);\r
-\r
- /* distance table */\r
- sym = 0;\r
- while (sym < 32) state->lens[sym++] = 5;\r
- distfix = next;\r
- bits = 5;\r
- inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);\r
-\r
- /* do this just once */\r
- virgin = 0;\r
- }\r
-#else /* !BUILDFIXED */\r
-# include "inffixed.h"\r
-#endif /* BUILDFIXED */\r
- state->lencode = lenfix;\r
- state->lenbits = 9;\r
- state->distcode = distfix;\r
- state->distbits = 5;\r
-}\r
-\r
-#ifdef MAKEFIXED\r
-#include <stdio.h>\r
-\r
-/*\r
- Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also\r
- defines BUILDFIXED, so the tables are built on the fly. makefixed() writes\r
- those tables to stdout, which would be piped to inffixed.h. A small program\r
- can simply call makefixed to do this:\r
-\r
- void makefixed(void);\r
-\r
- int main(void)\r
- {\r
- makefixed();\r
- return 0;\r
- }\r
-\r
- Then that can be linked with zlib built with MAKEFIXED defined and run:\r
-\r
- a.out > inffixed.h\r
- */\r
-void makefixed()\r
-{\r
- unsigned low, size;\r
- struct inflate_state state;\r
-\r
- fixedtables(&state);\r
- puts(" /* inffixed.h -- table for decoding fixed codes");\r
- puts(" * Generated automatically by makefixed().");\r
- puts(" */");\r
- puts("");\r
- puts(" /* WARNING: this file should *not* be used by applications.");\r
- puts(" It is part of the implementation of this library and is");\r
- puts(" subject to change. Applications should only use zlib.h.");\r
- puts(" */");\r
- puts("");\r
- size = 1U << 9;\r
- printf(" static const code lenfix[%u] = {", size);\r
- low = 0;\r
- for (;;) {\r
- if ((low % 7) == 0) printf("\n ");\r
- printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,\r
- state.lencode[low].bits, state.lencode[low].val);\r
- if (++low == size) break;\r
- putchar(',');\r
- }\r
- puts("\n };");\r
- size = 1U << 5;\r
- printf("\n static const code distfix[%u] = {", size);\r
- low = 0;\r
- for (;;) {\r
- if ((low % 6) == 0) printf("\n ");\r
- printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,\r
- state.distcode[low].val);\r
- if (++low == size) break;\r
- putchar(',');\r
- }\r
- puts("\n };");\r
-}\r
-#endif /* MAKEFIXED */\r
-\r
-/*\r
- Update the window with the last wsize (normally 32K) bytes written before\r
- returning. If window does not exist yet, create it. This is only called\r
- when a window is already in use, or when output has been written during this\r
- inflate call, but the end of the deflate stream has not been reached yet.\r
- It is also called to create a window for dictionary data when a dictionary\r
- is loaded.\r
-\r
- Providing output buffers larger than 32K to inflate() should provide a speed\r
- advantage, since only the last 32K of output is copied to the sliding window\r
- upon return from inflate(), and since all distances after the first 32K of\r
- output will fall in the output data, making match copies simpler and faster.\r
- The advantage may be dependent on the size of the processor's data caches.\r
- */\r
-local int updatewindow(strm, end, copy)\r
-z_streamp strm;\r
-const Bytef *end;\r
-unsigned copy;\r
-{\r
- struct inflate_state FAR *state;\r
- unsigned dist;\r
-\r
- state = (struct inflate_state FAR *)strm->state;\r
-\r
- /* if it hasn't been done already, allocate space for the window */\r
- if (state->window == Z_NULL) {\r
- state->window = (unsigned char FAR *)\r
- ZALLOC(strm, 1U << state->wbits,\r
- sizeof(unsigned char));\r
- if (state->window == Z_NULL) return 1;\r
- }\r
-\r
- /* if window not in use yet, initialize */\r
- if (state->wsize == 0) {\r
- state->wsize = 1U << state->wbits;\r
- state->wnext = 0;\r
- state->whave = 0;\r
- }\r
-\r
- /* copy state->wsize or less output bytes into the circular window */\r
- if (copy >= state->wsize) {\r
- zmemcpy(state->window, end - state->wsize, state->wsize);\r
- state->wnext = 0;\r
- state->whave = state->wsize;\r
- }\r
- else {\r
- dist = state->wsize - state->wnext;\r
- if (dist > copy) dist = copy;\r
- zmemcpy(state->window + state->wnext, end - copy, dist);\r
- copy -= dist;\r
- if (copy) {\r
- zmemcpy(state->window, end - copy, copy);\r
- state->wnext = copy;\r
- state->whave = state->wsize;\r
- }\r
- else {\r
- state->wnext += dist;\r
- if (state->wnext == state->wsize) state->wnext = 0;\r
- if (state->whave < state->wsize) state->whave += dist;\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* Macros for inflate(): */\r
-\r
-/* check function to use adler32() for zlib or crc32() for gzip */\r
-#ifdef GUNZIP\r
-# define UPDATE(check, buf, len) \\r
- (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))\r
-#else\r
-# define UPDATE(check, buf, len) adler32(check, buf, len)\r
-#endif\r
-\r
-/* check macros for header crc */\r
-#ifdef GUNZIP\r
-# define CRC2(check, word) \\r
- do { \\r
- hbuf[0] = (unsigned char)(word); \\r
- hbuf[1] = (unsigned char)((word) >> 8); \\r
- check = crc32(check, hbuf, 2); \\r
- } while (0)\r
-\r
-# define CRC4(check, word) \\r
- do { \\r
- hbuf[0] = (unsigned char)(word); \\r
- hbuf[1] = (unsigned char)((word) >> 8); \\r
- hbuf[2] = (unsigned char)((word) >> 16); \\r
- hbuf[3] = (unsigned char)((word) >> 24); \\r
- check = crc32(check, hbuf, 4); \\r
- } while (0)\r
-#endif\r
-\r
-/* Load registers with state in inflate() for speed */\r
-#define LOAD() \\r
- do { \\r
- put = strm->next_out; \\r
- left = strm->avail_out; \\r
- next = strm->next_in; \\r
- have = strm->avail_in; \\r
- hold = state->hold; \\r
- bits = state->bits; \\r
- } while (0)\r
-\r
-/* Restore state from registers in inflate() */\r
-#define RESTORE() \\r
- do { \\r
- strm->next_out = put; \\r
- strm->avail_out = left; \\r
- strm->next_in = next; \\r
- strm->avail_in = have; \\r
- state->hold = hold; \\r
- state->bits = bits; \\r
- } while (0)\r
-\r
-/* Clear the input bit accumulator */\r
-#define INITBITS() \\r
- do { \\r
- hold = 0; \\r
- bits = 0; \\r
- } while (0)\r
-\r
-/* Get a byte of input into the bit accumulator, or return from inflate()\r
- if there is no input available. */\r
-#define PULLBYTE() \\r
- do { \\r
- if (have == 0) goto inf_leave; \\r
- have--; \\r
- hold += (unsigned long)(*next++) << bits; \\r
- bits += 8; \\r
- } while (0)\r
-\r
-/* Assure that there are at least n bits in the bit accumulator. If there is\r
- not enough available input to do that, then return from inflate(). */\r
-#define NEEDBITS(n) \\r
- do { \\r
- while (bits < (unsigned)(n)) \\r
- PULLBYTE(); \\r
- } while (0)\r
-\r
-/* Return the low n bits of the bit accumulator (n < 16) */\r
-#define BITS(n) \\r
- ((unsigned)hold & ((1U << (n)) - 1))\r
-\r
-/* Remove n bits from the bit accumulator */\r
-#define DROPBITS(n) \\r
- do { \\r
- hold >>= (n); \\r
- bits -= (unsigned)(n); \\r
- } while (0)\r
-\r
-/* Remove zero to seven bits as needed to go to a byte boundary */\r
-#define BYTEBITS() \\r
- do { \\r
- hold >>= bits & 7; \\r
- bits -= bits & 7; \\r
- } while (0)\r
-\r
-/*\r
- inflate() uses a state machine to process as much input data and generate as\r
- much output data as possible before returning. The state machine is\r
- structured roughly as follows:\r
-\r
- for (;;) switch (state) {\r
- ...\r
- case STATEn:\r
- if (not enough input data or output space to make progress)\r
- return;\r
- ... make progress ...\r
- state = STATEm;\r
- break;\r
- ...\r
- }\r
-\r
- so when inflate() is called again, the same case is attempted again, and\r
- if the appropriate resources are provided, the machine proceeds to the\r
- next state. The NEEDBITS() macro is usually the way the state evaluates\r
- whether it can proceed or should return. NEEDBITS() does the return if\r
- the requested bits are not available. The typical use of the BITS macros\r
- is:\r
-\r
- NEEDBITS(n);\r
- ... do something with BITS(n) ...\r
- DROPBITS(n);\r
-\r
- where NEEDBITS(n) either returns from inflate() if there isn't enough\r
- input left to load n bits into the accumulator, or it continues. BITS(n)\r
- gives the low n bits in the accumulator. When done, DROPBITS(n) drops\r
- the low n bits off the accumulator. INITBITS() clears the accumulator\r
- and sets the number of available bits to zero. BYTEBITS() discards just\r
- enough bits to put the accumulator on a byte boundary. After BYTEBITS()\r
- and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.\r
-\r
- NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return\r
- if there is no input available. The decoding of variable length codes uses\r
- PULLBYTE() directly in order to pull just enough bytes to decode the next\r
- code, and no more.\r
-\r
- Some states loop until they get enough input, making sure that enough\r
- state information is maintained to continue the loop where it left off\r
- if NEEDBITS() returns in the loop. For example, want, need, and keep\r
- would all have to actually be part of the saved state in case NEEDBITS()\r
- returns:\r
-\r
- case STATEw:\r
- while (want < need) {\r
- NEEDBITS(n);\r
- keep[want++] = BITS(n);\r
- DROPBITS(n);\r
- }\r
- state = STATEx;\r
- case STATEx:\r
-\r
- As shown above, if the next state is also the next case, then the break\r
- is omitted.\r
-\r
- A state may also return if there is not enough output space available to\r
- complete that state. Those states are copying stored data, writing a\r
- literal byte, and copying a matching string.\r
-\r
- When returning, a "goto inf_leave" is used to update the total counters,\r
- update the check value, and determine whether any progress has been made\r
- during that inflate() call in order to return the proper return code.\r
- Progress is defined as a change in either strm->avail_in or strm->avail_out.\r
- When there is a window, goto inf_leave will update the window with the last\r
- output written. If a goto inf_leave occurs in the middle of decompression\r
- and there is no window currently, goto inf_leave will create one and copy\r
- output to the window for the next call of inflate().\r
-\r
- In this implementation, the flush parameter of inflate() only affects the\r
- return code (per zlib.h). inflate() always writes as much as possible to\r
- strm->next_out, given the space available and the provided input--the effect\r
- documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers\r
- the allocation of and copying into a sliding window until necessary, which\r
- provides the effect documented in zlib.h for Z_FINISH when the entire input\r
- stream available. So the only thing the flush parameter actually does is:\r
- when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it\r
- will return Z_BUF_ERROR if it has not reached the end of the stream.\r
- */\r
-\r
-int ZEXPORT inflate(strm, flush)\r
-z_streamp strm;\r
-int flush;\r
-{\r
- struct inflate_state FAR *state;\r
- z_const unsigned char FAR *next; /* next input */\r
- unsigned char FAR *put; /* next output */\r
- unsigned have, left; /* available input and output */\r
- unsigned long hold; /* bit buffer */\r
- unsigned bits; /* bits in bit buffer */\r
- unsigned in, out; /* save starting available input and output */\r
- unsigned copy; /* number of stored or match bytes to copy */\r
- unsigned char FAR *from; /* where to copy match bytes from */\r
- code here; /* current decoding table entry */\r
- code last; /* parent table entry */\r
- unsigned len; /* length to copy for repeats, bits to drop */\r
- int ret; /* return code */\r
-#ifdef GUNZIP\r
- unsigned char hbuf[4]; /* buffer for gzip header crc calculation */\r
-#endif\r
- static const unsigned short order[19] = /* permutation of code lengths */\r
- {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||\r
- (strm->next_in == Z_NULL && strm->avail_in != 0))\r
- return Z_STREAM_ERROR;\r
-\r
- state = (struct inflate_state FAR *)strm->state;\r
- if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */\r
- LOAD();\r
- in = have;\r
- out = left;\r
- ret = Z_OK;\r
- for (;;)\r
- switch (state->mode) {\r
- case HEAD:\r
- if (state->wrap == 0) {\r
- state->mode = TYPEDO;\r
- break;\r
- }\r
- NEEDBITS(16);\r
-#ifdef GUNZIP\r
- if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */\r
- state->check = crc32(0L, Z_NULL, 0);\r
- CRC2(state->check, hold);\r
- INITBITS();\r
- state->mode = FLAGS;\r
- break;\r
- }\r
- state->flags = 0; /* expect zlib header */\r
- if (state->head != Z_NULL)\r
- state->head->done = -1;\r
- if (!(state->wrap & 1) || /* check if zlib header allowed */\r
-#else\r
- if (\r
-#endif\r
- ((BITS(8) << 8) + (hold >> 8)) % 31) {\r
- strm->msg = (char *)"incorrect header check";\r
- state->mode = BAD;\r
- break;\r
- }\r
- if (BITS(4) != Z_DEFLATED) {\r
- strm->msg = (char *)"unknown compression method";\r
- state->mode = BAD;\r
- break;\r
- }\r
- DROPBITS(4);\r
- len = BITS(4) + 8;\r
- if (state->wbits == 0)\r
- state->wbits = len;\r
- else if (len > state->wbits) {\r
- strm->msg = (char *)"invalid window size";\r
- state->mode = BAD;\r
- break;\r
- }\r
- state->dmax = 1U << len;\r
- Tracev((stderr, "inflate: zlib header ok\n"));\r
- strm->adler = state->check = adler32(0L, Z_NULL, 0);\r
- state->mode = hold & 0x200 ? DICTID : TYPE;\r
- INITBITS();\r
- break;\r
-#ifdef GUNZIP\r
- case FLAGS:\r
- NEEDBITS(16);\r
- state->flags = (int)(hold);\r
- if ((state->flags & 0xff) != Z_DEFLATED) {\r
- strm->msg = (char *)"unknown compression method";\r
- state->mode = BAD;\r
- break;\r
- }\r
- if (state->flags & 0xe000) {\r
- strm->msg = (char *)"unknown header flags set";\r
- state->mode = BAD;\r
- break;\r
- }\r
- if (state->head != Z_NULL)\r
- state->head->text = (int)((hold >> 8) & 1);\r
- if (state->flags & 0x0200) CRC2(state->check, hold);\r
- INITBITS();\r
- state->mode = TIME;\r
- case TIME:\r
- NEEDBITS(32);\r
- if (state->head != Z_NULL)\r
- state->head->time = hold;\r
- if (state->flags & 0x0200) CRC4(state->check, hold);\r
- INITBITS();\r
- state->mode = OS;\r
- case OS:\r
- NEEDBITS(16);\r
- if (state->head != Z_NULL) {\r
- state->head->xflags = (int)(hold & 0xff);\r
- state->head->os = (int)(hold >> 8);\r
- }\r
- if (state->flags & 0x0200) CRC2(state->check, hold);\r
- INITBITS();\r
- state->mode = EXLEN;\r
- case EXLEN:\r
- if (state->flags & 0x0400) {\r
- NEEDBITS(16);\r
- state->length = (unsigned)(hold);\r
- if (state->head != Z_NULL)\r
- state->head->extra_len = (unsigned)hold;\r
- if (state->flags & 0x0200) CRC2(state->check, hold);\r
- INITBITS();\r
- }\r
- else if (state->head != Z_NULL)\r
- state->head->extra = Z_NULL;\r
- state->mode = EXTRA;\r
- case EXTRA:\r
- if (state->flags & 0x0400) {\r
- copy = state->length;\r
- if (copy > have) copy = have;\r
- if (copy) {\r
- if (state->head != Z_NULL &&\r
- state->head->extra != Z_NULL) {\r
- len = state->head->extra_len - state->length;\r
- zmemcpy(state->head->extra + len, next,\r
- len + copy > state->head->extra_max ?\r
- state->head->extra_max - len : copy);\r
- }\r
- if (state->flags & 0x0200)\r
- state->check = crc32(state->check, next, copy);\r
- have -= copy;\r
- next += copy;\r
- state->length -= copy;\r
- }\r
- if (state->length) goto inf_leave;\r
- }\r
- state->length = 0;\r
- state->mode = NAME;\r
- case NAME:\r
- if (state->flags & 0x0800) {\r
- if (have == 0) goto inf_leave;\r
- copy = 0;\r
- do {\r
- len = (unsigned)(next[copy++]);\r
- if (state->head != Z_NULL &&\r
- state->head->name != Z_NULL &&\r
- state->length < state->head->name_max)\r
- state->head->name[state->length++] = len;\r
- } while (len && copy < have);\r
- if (state->flags & 0x0200)\r
- state->check = crc32(state->check, next, copy);\r
- have -= copy;\r
- next += copy;\r
- if (len) goto inf_leave;\r
- }\r
- else if (state->head != Z_NULL)\r
- state->head->name = Z_NULL;\r
- state->length = 0;\r
- state->mode = COMMENT;\r
- case COMMENT:\r
- if (state->flags & 0x1000) {\r
- if (have == 0) goto inf_leave;\r
- copy = 0;\r
- do {\r
- len = (unsigned)(next[copy++]);\r
- if (state->head != Z_NULL &&\r
- state->head->comment != Z_NULL &&\r
- state->length < state->head->comm_max)\r
- state->head->comment[state->length++] = len;\r
- } while (len && copy < have);\r
- if (state->flags & 0x0200)\r
- state->check = crc32(state->check, next, copy);\r
- have -= copy;\r
- next += copy;\r
- if (len) goto inf_leave;\r
- }\r
- else if (state->head != Z_NULL)\r
- state->head->comment = Z_NULL;\r
- state->mode = HCRC;\r
- case HCRC:\r
- if (state->flags & 0x0200) {\r
- NEEDBITS(16);\r
- if (hold != (state->check & 0xffff)) {\r
- strm->msg = (char *)"header crc mismatch";\r
- state->mode = BAD;\r
- break;\r
- }\r
- INITBITS();\r
- }\r
- if (state->head != Z_NULL) {\r
- state->head->hcrc = (int)((state->flags >> 9) & 1);\r
- state->head->done = 1;\r
- }\r
- strm->adler = state->check = crc32(0L, Z_NULL, 0);\r
- state->mode = TYPE;\r
- break;\r
-#endif\r
- case DICTID:\r
- NEEDBITS(32);\r
- strm->adler = state->check = ZSWAP32(hold);\r
- INITBITS();\r
- state->mode = DICT;\r
- case DICT:\r
- if (state->havedict == 0) {\r
- RESTORE();\r
- return Z_NEED_DICT;\r
- }\r
- strm->adler = state->check = adler32(0L, Z_NULL, 0);\r
- state->mode = TYPE;\r
- case TYPE:\r
- if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;\r
- case TYPEDO:\r
- if (state->last) {\r
- BYTEBITS();\r
- state->mode = CHECK;\r
- break;\r
- }\r
- NEEDBITS(3);\r
- state->last = BITS(1);\r
- DROPBITS(1);\r
- switch (BITS(2)) {\r
- case 0: /* stored block */\r
- Tracev((stderr, "inflate: stored block%s\n",\r
- state->last ? " (last)" : ""));\r
- state->mode = STORED;\r
- break;\r
- case 1: /* fixed block */\r
- fixedtables(state);\r
- Tracev((stderr, "inflate: fixed codes block%s\n",\r
- state->last ? " (last)" : ""));\r
- state->mode = LEN_; /* decode codes */\r
- if (flush == Z_TREES) {\r
- DROPBITS(2);\r
- goto inf_leave;\r
- }\r
- break;\r
- case 2: /* dynamic block */\r
- Tracev((stderr, "inflate: dynamic codes block%s\n",\r
- state->last ? " (last)" : ""));\r
- state->mode = TABLE;\r
- break;\r
- case 3:\r
- strm->msg = (char *)"invalid block type";\r
- state->mode = BAD;\r
- }\r
- DROPBITS(2);\r
- break;\r
- case STORED:\r
- BYTEBITS(); /* go to byte boundary */\r
- NEEDBITS(32);\r
- if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {\r
- strm->msg = (char *)"invalid stored block lengths";\r
- state->mode = BAD;\r
- break;\r
- }\r
- state->length = (unsigned)hold & 0xffff;\r
- Tracev((stderr, "inflate: stored length %u\n",\r
- state->length));\r
- INITBITS();\r
- state->mode = COPY_;\r
- if (flush == Z_TREES) goto inf_leave;\r
- case COPY_:\r
- state->mode = COPY;\r
- case COPY:\r
- copy = state->length;\r
- if (copy) {\r
- if (copy > have) copy = have;\r
- if (copy > left) copy = left;\r
- if (copy == 0) goto inf_leave;\r
- zmemcpy(put, next, copy);\r
- have -= copy;\r
- next += copy;\r
- left -= copy;\r
- put += copy;\r
- state->length -= copy;\r
- break;\r
- }\r
- Tracev((stderr, "inflate: stored end\n"));\r
- state->mode = TYPE;\r
- break;\r
- case TABLE:\r
- NEEDBITS(14);\r
- state->nlen = BITS(5) + 257;\r
- DROPBITS(5);\r
- state->ndist = BITS(5) + 1;\r
- DROPBITS(5);\r
- state->ncode = BITS(4) + 4;\r
- DROPBITS(4);\r
-#ifndef PKZIP_BUG_WORKAROUND\r
- if (state->nlen > 286 || state->ndist > 30) {\r
- strm->msg = (char *)"too many length or distance symbols";\r
- state->mode = BAD;\r
- break;\r
- }\r
-#endif\r
- Tracev((stderr, "inflate: table sizes ok\n"));\r
- state->have = 0;\r
- state->mode = LENLENS;\r
- case LENLENS:\r
- while (state->have < state->ncode) {\r
- NEEDBITS(3);\r
- state->lens[order[state->have++]] = (unsigned short)BITS(3);\r
- DROPBITS(3);\r
- }\r
- while (state->have < 19)\r
- state->lens[order[state->have++]] = 0;\r
- state->next = state->codes;\r
- state->lencode = (const code FAR *)(state->next);\r
- state->lenbits = 7;\r
- ret = inflate_table(CODES, state->lens, 19, &(state->next),\r
- &(state->lenbits), state->work);\r
- if (ret) {\r
- strm->msg = (char *)"invalid code lengths set";\r
- state->mode = BAD;\r
- break;\r
- }\r
- Tracev((stderr, "inflate: code lengths ok\n"));\r
- state->have = 0;\r
- state->mode = CODELENS;\r
- case CODELENS:\r
- while (state->have < state->nlen + state->ndist) {\r
- for (;;) {\r
- here = state->lencode[BITS(state->lenbits)];\r
- if ((unsigned)(here.bits) <= bits) break;\r
- PULLBYTE();\r
- }\r
- if (here.val < 16) {\r
- DROPBITS(here.bits);\r
- state->lens[state->have++] = here.val;\r
- }\r
- else {\r
- if (here.val == 16) {\r
- NEEDBITS(here.bits + 2);\r
- DROPBITS(here.bits);\r
- if (state->have == 0) {\r
- strm->msg = (char *)"invalid bit length repeat";\r
- state->mode = BAD;\r
- break;\r
- }\r
- len = state->lens[state->have - 1];\r
- copy = 3 + BITS(2);\r
- DROPBITS(2);\r
- }\r
- else if (here.val == 17) {\r
- NEEDBITS(here.bits + 3);\r
- DROPBITS(here.bits);\r
- len = 0;\r
- copy = 3 + BITS(3);\r
- DROPBITS(3);\r
- }\r
- else {\r
- NEEDBITS(here.bits + 7);\r
- DROPBITS(here.bits);\r
- len = 0;\r
- copy = 11 + BITS(7);\r
- DROPBITS(7);\r
- }\r
- if (state->have + copy > state->nlen + state->ndist) {\r
- strm->msg = (char *)"invalid bit length repeat";\r
- state->mode = BAD;\r
- break;\r
- }\r
- while (copy--)\r
- state->lens[state->have++] = (unsigned short)len;\r
- }\r
- }\r
-\r
- /* handle error breaks in while */\r
- if (state->mode == BAD) break;\r
-\r
- /* check for end-of-block code (better have one) */\r
- if (state->lens[256] == 0) {\r
- strm->msg = (char *)"invalid code -- missing end-of-block";\r
- state->mode = BAD;\r
- break;\r
- }\r
-\r
- /* build code tables -- note: do not change the lenbits or distbits\r
- values here (9 and 6) without reading the comments in inftrees.h\r
- concerning the ENOUGH constants, which depend on those values */\r
- state->next = state->codes;\r
- state->lencode = (const code FAR *)(state->next);\r
- state->lenbits = 9;\r
- ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),\r
- &(state->lenbits), state->work);\r
- if (ret) {\r
- strm->msg = (char *)"invalid literal/lengths set";\r
- state->mode = BAD;\r
- break;\r
- }\r
- state->distcode = (const code FAR *)(state->next);\r
- state->distbits = 6;\r
- ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,\r
- &(state->next), &(state->distbits), state->work);\r
- if (ret) {\r
- strm->msg = (char *)"invalid distances set";\r
- state->mode = BAD;\r
- break;\r
- }\r
- Tracev((stderr, "inflate: codes ok\n"));\r
- state->mode = LEN_;\r
- if (flush == Z_TREES) goto inf_leave;\r
- case LEN_:\r
- state->mode = LEN;\r
- case LEN:\r
- if (have >= 6 && left >= 258) {\r
- RESTORE();\r
- inflate_fast(strm, out);\r
- LOAD();\r
- if (state->mode == TYPE)\r
- state->back = -1;\r
- break;\r
- }\r
- state->back = 0;\r
- for (;;) {\r
- here = state->lencode[BITS(state->lenbits)];\r
- if ((unsigned)(here.bits) <= bits) break;\r
- PULLBYTE();\r
- }\r
- if (here.op && (here.op & 0xf0) == 0) {\r
- last = here;\r
- for (;;) {\r
- here = state->lencode[last.val +\r
- (BITS(last.bits + last.op) >> last.bits)];\r
- if ((unsigned)(last.bits + here.bits) <= bits) break;\r
- PULLBYTE();\r
- }\r
- DROPBITS(last.bits);\r
- state->back += last.bits;\r
- }\r
- DROPBITS(here.bits);\r
- state->back += here.bits;\r
- state->length = (unsigned)here.val;\r
- if ((int)(here.op) == 0) {\r
- Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?\r
- "inflate: literal '%c'\n" :\r
- "inflate: literal 0x%02x\n", here.val));\r
- state->mode = LIT;\r
- break;\r
- }\r
- if (here.op & 32) {\r
- Tracevv((stderr, "inflate: end of block\n"));\r
- state->back = -1;\r
- state->mode = TYPE;\r
- break;\r
- }\r
- if (here.op & 64) {\r
- strm->msg = (char *)"invalid literal/length code";\r
- state->mode = BAD;\r
- break;\r
- }\r
- state->extra = (unsigned)(here.op) & 15;\r
- state->mode = LENEXT;\r
- case LENEXT:\r
- if (state->extra) {\r
- NEEDBITS(state->extra);\r
- state->length += BITS(state->extra);\r
- DROPBITS(state->extra);\r
- state->back += state->extra;\r
- }\r
- Tracevv((stderr, "inflate: length %u\n", state->length));\r
- state->was = state->length;\r
- state->mode = DIST;\r
- case DIST:\r
- for (;;) {\r
- here = state->distcode[BITS(state->distbits)];\r
- if ((unsigned)(here.bits) <= bits) break;\r
- PULLBYTE();\r
- }\r
- if ((here.op & 0xf0) == 0) {\r
- last = here;\r
- for (;;) {\r
- here = state->distcode[last.val +\r
- (BITS(last.bits + last.op) >> last.bits)];\r
- if ((unsigned)(last.bits + here.bits) <= bits) break;\r
- PULLBYTE();\r
- }\r
- DROPBITS(last.bits);\r
- state->back += last.bits;\r
- }\r
- DROPBITS(here.bits);\r
- state->back += here.bits;\r
- if (here.op & 64) {\r
- strm->msg = (char *)"invalid distance code";\r
- state->mode = BAD;\r
- break;\r
- }\r
- state->offset = (unsigned)here.val;\r
- state->extra = (unsigned)(here.op) & 15;\r
- state->mode = DISTEXT;\r
- case DISTEXT:\r
- if (state->extra) {\r
- NEEDBITS(state->extra);\r
- state->offset += BITS(state->extra);\r
- DROPBITS(state->extra);\r
- state->back += state->extra;\r
- }\r
-#ifdef INFLATE_STRICT\r
- if (state->offset > state->dmax) {\r
- strm->msg = (char *)"invalid distance too far back";\r
- state->mode = BAD;\r
- break;\r
- }\r
-#endif\r
- Tracevv((stderr, "inflate: distance %u\n", state->offset));\r
- state->mode = MATCH;\r
- case MATCH:\r
- if (left == 0) goto inf_leave;\r
- copy = out - left;\r
- if (state->offset > copy) { /* copy from window */\r
- copy = state->offset - copy;\r
- if (copy > state->whave) {\r
- if (state->sane) {\r
- strm->msg = (char *)"invalid distance too far back";\r
- state->mode = BAD;\r
- break;\r
- }\r
-#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR\r
- Trace((stderr, "inflate.c too far\n"));\r
- copy -= state->whave;\r
- if (copy > state->length) copy = state->length;\r
- if (copy > left) copy = left;\r
- left -= copy;\r
- state->length -= copy;\r
- do {\r
- *put++ = 0;\r
- } while (--copy);\r
- if (state->length == 0) state->mode = LEN;\r
- break;\r
-#endif\r
- }\r
- if (copy > state->wnext) {\r
- copy -= state->wnext;\r
- from = state->window + (state->wsize - copy);\r
- }\r
- else\r
- from = state->window + (state->wnext - copy);\r
- if (copy > state->length) copy = state->length;\r
- }\r
- else { /* copy from output */\r
- from = put - state->offset;\r
- copy = state->length;\r
- }\r
- if (copy > left) copy = left;\r
- left -= copy;\r
- state->length -= copy;\r
- do {\r
- *put++ = *from++;\r
- } while (--copy);\r
- if (state->length == 0) state->mode = LEN;\r
- break;\r
- case LIT:\r
- if (left == 0) goto inf_leave;\r
- *put++ = (unsigned char)(state->length);\r
- left--;\r
- state->mode = LEN;\r
- break;\r
- case CHECK:\r
- if (state->wrap) {\r
- NEEDBITS(32);\r
- out -= left;\r
- strm->total_out += out;\r
- state->total += out;\r
- if (out)\r
- strm->adler = state->check =\r
- UPDATE(state->check, put - out, out);\r
- out = left;\r
- if ((\r
-#ifdef GUNZIP\r
- state->flags ? hold :\r
-#endif\r
- ZSWAP32(hold)) != state->check) {\r
- strm->msg = (char *)"incorrect data check";\r
- state->mode = BAD;\r
- break;\r
- }\r
- INITBITS();\r
- Tracev((stderr, "inflate: check matches trailer\n"));\r
- }\r
-#ifdef GUNZIP\r
- state->mode = LENGTH;\r
- case LENGTH:\r
- if (state->wrap && state->flags) {\r
- NEEDBITS(32);\r
- if (hold != (state->total & 0xffffffffUL)) {\r
- strm->msg = (char *)"incorrect length check";\r
- state->mode = BAD;\r
- break;\r
- }\r
- INITBITS();\r
- Tracev((stderr, "inflate: length matches trailer\n"));\r
- }\r
-#endif\r
- state->mode = DONE;\r
- case DONE:\r
- ret = Z_STREAM_END;\r
- goto inf_leave;\r
- case BAD:\r
- ret = Z_DATA_ERROR;\r
- goto inf_leave;\r
- case MEM:\r
- return Z_MEM_ERROR;\r
- case SYNC:\r
- default:\r
- return Z_STREAM_ERROR;\r
- }\r
-\r
- /*\r
- Return from inflate(), updating the total counts and the check value.\r
- If there was no progress during the inflate() call, return a buffer\r
- error. Call updatewindow() to create and/or update the window state.\r
- Note: a memory error from inflate() is non-recoverable.\r
- */\r
- inf_leave:\r
- RESTORE();\r
- if (state->wsize || (out != strm->avail_out && state->mode < BAD &&\r
- (state->mode < CHECK || flush != Z_FINISH)))\r
- if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {\r
- state->mode = MEM;\r
- return Z_MEM_ERROR;\r
- }\r
- in -= strm->avail_in;\r
- out -= strm->avail_out;\r
- strm->total_in += in;\r
- strm->total_out += out;\r
- state->total += out;\r
- if (state->wrap && out)\r
- strm->adler = state->check =\r
- UPDATE(state->check, strm->next_out - out, out);\r
- strm->data_type = state->bits + (state->last ? 64 : 0) +\r
- (state->mode == TYPE ? 128 : 0) +\r
- (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);\r
- if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)\r
- ret = Z_BUF_ERROR;\r
- return ret;\r
-}\r
-\r
-int ZEXPORT inflateEnd(strm)\r
-z_streamp strm;\r
-{\r
- struct inflate_state FAR *state;\r
- if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)\r
- return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- if (state->window != Z_NULL) ZFREE(strm, state->window);\r
- ZFREE(strm, strm->state);\r
- strm->state = Z_NULL;\r
- Tracev((stderr, "inflate: end\n"));\r
- return Z_OK;\r
-}\r
-\r
-int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)\r
-z_streamp strm;\r
-Bytef *dictionary;\r
-uInt *dictLength;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- /* check state */\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
-\r
- /* copy dictionary */\r
- if (state->whave && dictionary != Z_NULL) {\r
- zmemcpy(dictionary, state->window + state->wnext,\r
- state->whave - state->wnext);\r
- zmemcpy(dictionary + state->whave - state->wnext,\r
- state->window, state->wnext);\r
- }\r
- if (dictLength != Z_NULL)\r
- *dictLength = state->whave;\r
- return Z_OK;\r
-}\r
-\r
-int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)\r
-z_streamp strm;\r
-const Bytef *dictionary;\r
-uInt dictLength;\r
-{\r
- struct inflate_state FAR *state;\r
- unsigned long dictid;\r
- int ret;\r
-\r
- /* check state */\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- if (state->wrap != 0 && state->mode != DICT)\r
- return Z_STREAM_ERROR;\r
-\r
- /* check for correct dictionary identifier */\r
- if (state->mode == DICT) {\r
- dictid = adler32(0L, Z_NULL, 0);\r
- dictid = adler32(dictid, dictionary, dictLength);\r
- if (dictid != state->check)\r
- return Z_DATA_ERROR;\r
- }\r
-\r
- /* copy dictionary to window using updatewindow(), which will amend the\r
- existing dictionary if appropriate */\r
- ret = updatewindow(strm, dictionary + dictLength, dictLength);\r
- if (ret) {\r
- state->mode = MEM;\r
- return Z_MEM_ERROR;\r
- }\r
- state->havedict = 1;\r
- Tracev((stderr, "inflate: dictionary set\n"));\r
- return Z_OK;\r
-}\r
-\r
-int ZEXPORT inflateGetHeader(strm, head)\r
-z_streamp strm;\r
-gz_headerp head;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- /* check state */\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;\r
-\r
- /* save header structure */\r
- state->head = head;\r
- head->done = 0;\r
- return Z_OK;\r
-}\r
-\r
-/*\r
- Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found\r
- or when out of input. When called, *have is the number of pattern bytes\r
- found in order so far, in 0..3. On return *have is updated to the new\r
- state. If on return *have equals four, then the pattern was found and the\r
- return value is how many bytes were read including the last byte of the\r
- pattern. If *have is less than four, then the pattern has not been found\r
- yet and the return value is len. In the latter case, syncsearch() can be\r
- called again with more data and the *have state. *have is initialized to\r
- zero for the first call.\r
- */\r
-local unsigned syncsearch(have, buf, len)\r
-unsigned FAR *have;\r
-const unsigned char FAR *buf;\r
-unsigned len;\r
-{\r
- unsigned got;\r
- unsigned next;\r
-\r
- got = *have;\r
- next = 0;\r
- while (next < len && got < 4) {\r
- if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))\r
- got++;\r
- else if (buf[next])\r
- got = 0;\r
- else\r
- got = 4 - got;\r
- next++;\r
- }\r
- *have = got;\r
- return next;\r
-}\r
-\r
-int ZEXPORT inflateSync(strm)\r
-z_streamp strm;\r
-{\r
- unsigned len; /* number of bytes to look at or looked at */\r
- unsigned long in, out; /* temporary to save total_in and total_out */\r
- unsigned char buf[4]; /* to restore bit buffer to byte string */\r
- struct inflate_state FAR *state;\r
-\r
- /* check parameters */\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;\r
-\r
- /* if first time, start search in bit buffer */\r
- if (state->mode != SYNC) {\r
- state->mode = SYNC;\r
- state->hold <<= state->bits & 7;\r
- state->bits -= state->bits & 7;\r
- len = 0;\r
- while (state->bits >= 8) {\r
- buf[len++] = (unsigned char)(state->hold);\r
- state->hold >>= 8;\r
- state->bits -= 8;\r
- }\r
- state->have = 0;\r
- syncsearch(&(state->have), buf, len);\r
- }\r
-\r
- /* search available input */\r
- len = syncsearch(&(state->have), strm->next_in, strm->avail_in);\r
- strm->avail_in -= len;\r
- strm->next_in += len;\r
- strm->total_in += len;\r
-\r
- /* return no joy or set up to restart inflate() on a new block */\r
- if (state->have != 4) return Z_DATA_ERROR;\r
- in = strm->total_in; out = strm->total_out;\r
- inflateReset(strm);\r
- strm->total_in = in; strm->total_out = out;\r
- state->mode = TYPE;\r
- return Z_OK;\r
-}\r
-\r
-/*\r
- Returns true if inflate is currently at the end of a block generated by\r
- Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP\r
- implementation to provide an additional safety check. PPP uses\r
- Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored\r
- block. When decompressing, PPP checks that at the end of input packet,\r
- inflate is waiting for these length bytes.\r
- */\r
-int ZEXPORT inflateSyncPoint(strm)\r
-z_streamp strm;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- return state->mode == STORED && state->bits == 0;\r
-}\r
-\r
-int ZEXPORT inflateCopy(dest, source)\r
-z_streamp dest;\r
-z_streamp source;\r
-{\r
- struct inflate_state FAR *state;\r
- struct inflate_state FAR *copy;\r
- unsigned char FAR *window;\r
- unsigned wsize;\r
-\r
- /* check input */\r
- if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||\r
- source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)\r
- return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)source->state;\r
-\r
- /* allocate space */\r
- copy = (struct inflate_state FAR *)\r
- ZALLOC(source, 1, sizeof(struct inflate_state));\r
- if (copy == Z_NULL) return Z_MEM_ERROR;\r
- window = Z_NULL;\r
- if (state->window != Z_NULL) {\r
- window = (unsigned char FAR *)\r
- ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));\r
- if (window == Z_NULL) {\r
- ZFREE(source, copy);\r
- return Z_MEM_ERROR;\r
- }\r
- }\r
-\r
- /* copy state */\r
- zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));\r
- zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));\r
- if (state->lencode >= state->codes &&\r
- state->lencode <= state->codes + ENOUGH - 1) {\r
- copy->lencode = copy->codes + (state->lencode - state->codes);\r
- copy->distcode = copy->codes + (state->distcode - state->codes);\r
- }\r
- copy->next = copy->codes + (state->next - state->codes);\r
- if (window != Z_NULL) {\r
- wsize = 1U << state->wbits;\r
- zmemcpy(window, state->window, wsize);\r
- }\r
- copy->window = window;\r
- dest->state = (struct internal_state FAR *)copy;\r
- return Z_OK;\r
-}\r
-\r
-int ZEXPORT inflateUndermine(strm, subvert)\r
-z_streamp strm;\r
-int subvert;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;\r
- state = (struct inflate_state FAR *)strm->state;\r
- state->sane = !subvert;\r
-#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR\r
- return Z_OK;\r
-#else\r
- state->sane = 1;\r
- return Z_DATA_ERROR;\r
-#endif\r
-}\r
-\r
-long ZEXPORT inflateMark(strm)\r
-z_streamp strm;\r
-{\r
- struct inflate_state FAR *state;\r
-\r
- if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;\r
- state = (struct inflate_state FAR *)strm->state;\r
- return ((long)(state->back) << 16) +\r
- (state->mode == COPY ? state->length :\r
- (state->mode == MATCH ? state->was - state->length : 0));\r
-}\r