• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/zlib/inflate.c

00001 /* inflate.c -- zlib decompression
00002  * Copyright (C) 1995-2010 Mark Adler
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 /*
00007  * Change history:
00008  *
00009  * 1.2.beta0    24 Nov 2002
00010  * - First version -- complete rewrite of inflate to simplify code, avoid
00011  *   creation of window when not needed, minimize use of window when it is
00012  *   needed, make inffast.c even faster, implement gzip decoding, and to
00013  *   improve code readability and style over the previous zlib inflate code
00014  *
00015  * 1.2.beta1    25 Nov 2002
00016  * - Use pointers for available input and output checking in inffast.c
00017  * - Remove input and output counters in inffast.c
00018  * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
00019  * - Remove unnecessary second byte pull from length extra in inffast.c
00020  * - Unroll direct copy to three copies per loop in inffast.c
00021  *
00022  * 1.2.beta2    4 Dec 2002
00023  * - Change external routine names to reduce potential conflicts
00024  * - Correct filename to inffixed.h for fixed tables in inflate.c
00025  * - Make hbuf[] unsigned char to match parameter type in inflate.c
00026  * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
00027  *   to avoid negation problem on Alphas (64 bit) in inflate.c
00028  *
00029  * 1.2.beta3    22 Dec 2002
00030  * - Add comments on state->bits assertion in inffast.c
00031  * - Add comments on op field in inftrees.h
00032  * - Fix bug in reuse of allocated window after inflateReset()
00033  * - Remove bit fields--back to byte structure for speed
00034  * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
00035  * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
00036  * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
00037  * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
00038  * - Use local copies of stream next and avail values, as well as local bit
00039  *   buffer and bit count in inflate()--for speed when inflate_fast() not used
00040  *
00041  * 1.2.beta4    1 Jan 2003
00042  * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
00043  * - Move a comment on output buffer sizes from inffast.c to inflate.c
00044  * - Add comments in inffast.c to introduce the inflate_fast() routine
00045  * - Rearrange window copies in inflate_fast() for speed and simplification
00046  * - Unroll last copy for window match in inflate_fast()
00047  * - Use local copies of window variables in inflate_fast() for speed
00048  * - Pull out common wnext == 0 case for speed in inflate_fast()
00049  * - Make op and len in inflate_fast() unsigned for consistency
00050  * - Add FAR to lcode and dcode declarations in inflate_fast()
00051  * - Simplified bad distance check in inflate_fast()
00052  * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
00053  *   source file infback.c to provide a call-back interface to inflate for
00054  *   programs like gzip and unzip -- uses window as output buffer to avoid
00055  *   window copying
00056  *
00057  * 1.2.beta5    1 Jan 2003
00058  * - Improved inflateBack() interface to allow the caller to provide initial
00059  *   input in strm.
00060  * - Fixed stored blocks bug in inflateBack()
00061  *
00062  * 1.2.beta6    4 Jan 2003
00063  * - Added comments in inffast.c on effectiveness of POSTINC
00064  * - Typecasting all around to reduce compiler warnings
00065  * - Changed loops from while (1) or do {} while (1) to for (;;), again to
00066  *   make compilers happy
00067  * - Changed type of window in inflateBackInit() to unsigned char *
00068  *
00069  * 1.2.beta7    27 Jan 2003
00070  * - Changed many types to unsigned or unsigned short to avoid warnings
00071  * - Added inflateCopy() function
00072  *
00073  * 1.2.0        9 Mar 2003
00074  * - Changed inflateBack() interface to provide separate opaque descriptors
00075  *   for the in() and out() functions
00076  * - Changed inflateBack() argument and in_func typedef to swap the length
00077  *   and buffer address return values for the input function
00078  * - Check next_in and next_out for Z_NULL on entry to inflate()
00079  *
00080  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
00081  */
00082 
00083 #include "zutil.h"
00084 #include "inftrees.h"
00085 #include "inflate.h"
00086 #include "inffast.h"
00087 
00088 #ifdef MAKEFIXED
00089 #  ifndef BUILDFIXED
00090 #    define BUILDFIXED
00091 #  endif
00092 #endif
00093 
00094 /* function prototypes */
00095 local void fixedtables OF((struct inflate_state FAR *state));
00096 local int updatewindow OF((z_streamp strm, unsigned out));
00097 #ifdef BUILDFIXED
00098    void makefixed OF((void));
00099 #endif
00100 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
00101                               unsigned len));
00102 
00103 int ZEXPORT inflateReset(strm)
00104 z_streamp strm;
00105 {
00106     struct inflate_state FAR *state;
00107 
00108     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
00109     state = (struct inflate_state FAR *)strm->state;
00110     strm->total_in = strm->total_out = state->total = 0;
00111     strm->msg = Z_NULL;
00112     strm->adler = 1;        /* to support ill-conceived Java test suite */
00113     state->mode = HEAD;
00114     state->last = 0;
00115     state->havedict = 0;
00116     state->dmax = 32768U;
00117     state->head = Z_NULL;
00118     state->wsize = 0;
00119     state->whave = 0;
00120     state->wnext = 0;
00121     state->hold = 0;
00122     state->bits = 0;
00123     state->lencode = state->distcode = state->next = state->codes;
00124     state->sane = 1;
00125     state->back = -1;
00126     Tracev((stderr, "inflate: reset\n"));
00127     return Z_OK;
00128 }
00129 
00130 int ZEXPORT inflateReset2(strm, windowBits)
00131 z_streamp strm;
00132 int windowBits;
00133 {
00134     int wrap;
00135     struct inflate_state FAR *state;
00136 
00137     /* get the state */
00138     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
00139     state = (struct inflate_state FAR *)strm->state;
00140 
00141     /* extract wrap request from windowBits parameter */
00142     if (windowBits < 0) {
00143         wrap = 0;
00144         windowBits = -windowBits;
00145     }
00146     else {
00147         wrap = (windowBits >> 4) + 1;
00148 #ifdef GUNZIP
00149         if (windowBits < 48)
00150             windowBits &= 15;
00151 #endif
00152     }
00153 
00154     /* set number of window bits, free window if different */
00155     if (windowBits && (windowBits < 8 || windowBits > 15))
00156         return Z_STREAM_ERROR;
00157     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
00158         ZFREE(strm, state->window);
00159         state->window = Z_NULL;
00160     }
00161 
00162     /* update state and reset the rest of it */
00163     state->wrap = wrap;
00164     state->wbits = (unsigned)windowBits;
00165     return inflateReset(strm);
00166 }
00167 
00168 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
00169 z_streamp strm;
00170 int windowBits;
00171 const char *version;
00172 int stream_size;
00173 {
00174     int ret;
00175     struct inflate_state FAR *state;
00176 
00177     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
00178         stream_size != (int)(sizeof(z_stream)))
00179         return Z_VERSION_ERROR;
00180     if (strm == Z_NULL) return Z_STREAM_ERROR;
00181     strm->msg = Z_NULL;                 /* in case we return an error */
00182     if (strm->zalloc == (alloc_func)0) {
00183         strm->zalloc = zcalloc;
00184         strm->opaque = (voidpf)0;
00185     }
00186     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
00187     state = (struct inflate_state FAR *)
00188             ZALLOC(strm, 1, sizeof(struct inflate_state));
00189     if (state == Z_NULL) return Z_MEM_ERROR;
00190     Tracev((stderr, "inflate: allocated\n"));
00191     strm->state = (struct internal_state FAR *)state;
00192     state->window = Z_NULL;
00193     ret = inflateReset2(strm, windowBits);
00194     if (ret != Z_OK) {
00195         ZFREE(strm, state);
00196         strm->state = Z_NULL;
00197     }
00198     return ret;
00199 }
00200 
00201 int ZEXPORT inflateInit_(strm, version, stream_size)
00202 z_streamp strm;
00203 const char *version;
00204 int stream_size;
00205 {
00206     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
00207 }
00208 
00209 int ZEXPORT inflatePrime(strm, bits, value)
00210 z_streamp strm;
00211 int bits;
00212 int value;
00213 {
00214     struct inflate_state FAR *state;
00215 
00216     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
00217     state = (struct inflate_state FAR *)strm->state;
00218     if (bits < 0) {
00219         state->hold = 0;
00220         state->bits = 0;
00221         return Z_OK;
00222     }
00223     if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
00224     value &= (1L << bits) - 1;
00225     state->hold += value << state->bits;
00226     state->bits += bits;
00227     return Z_OK;
00228 }
00229 
00230 /*
00231    Return state with length and distance decoding tables and index sizes set to
00232    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
00233    If BUILDFIXED is defined, then instead this routine builds the tables the
00234    first time it's called, and returns those tables the first time and
00235    thereafter.  This reduces the size of the code by about 2K bytes, in
00236    exchange for a little execution time.  However, BUILDFIXED should not be
00237    used for threaded applications, since the rewriting of the tables and virgin
00238    may not be thread-safe.
00239  */
00240 local void fixedtables(state)
00241 struct inflate_state FAR *state;
00242 {
00243 #ifdef BUILDFIXED
00244     static int virgin = 1;
00245     static code *lenfix, *distfix;
00246     static code fixed[544];
00247 
00248     /* build fixed huffman tables if first call (may not be thread safe) */
00249     if (virgin) {
00250         unsigned sym, bits;
00251         static code *next;
00252 
00253         /* literal/length table */
00254         sym = 0;
00255         while (sym < 144) state->lens[sym++] = 8;
00256         while (sym < 256) state->lens[sym++] = 9;
00257         while (sym < 280) state->lens[sym++] = 7;
00258         while (sym < 288) state->lens[sym++] = 8;
00259         next = fixed;
00260         lenfix = next;
00261         bits = 9;
00262         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
00263 
00264         /* distance table */
00265         sym = 0;
00266         while (sym < 32) state->lens[sym++] = 5;
00267         distfix = next;
00268         bits = 5;
00269         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
00270 
00271         /* do this just once */
00272         virgin = 0;
00273     }
00274 #else /* !BUILDFIXED */
00275 #   include "inffixed.h"
00276 #endif /* BUILDFIXED */
00277     state->lencode = lenfix;
00278     state->lenbits = 9;
00279     state->distcode = distfix;
00280     state->distbits = 5;
00281 }
00282 
00283 #ifdef MAKEFIXED
00284 #include <stdio.h>
00285 
00286 /*
00287    Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
00288    defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
00289    those tables to stdout, which would be piped to inffixed.h.  A small program
00290    can simply call makefixed to do this:
00291 
00292     void makefixed(void);
00293 
00294     int main(void)
00295     {
00296         makefixed();
00297         return 0;
00298     }
00299 
00300    Then that can be linked with zlib built with MAKEFIXED defined and run:
00301 
00302     a.out > inffixed.h
00303  */
00304 void makefixed()
00305 {
00306     unsigned low, size;
00307     struct inflate_state state;
00308 
00309     fixedtables(&state);
00310     puts("    /* inffixed.h -- table for decoding fixed codes");
00311     puts("     * Generated automatically by makefixed().");
00312     puts("     */");
00313     puts("");
00314     puts("    /* WARNING: this file should *not* be used by applications.");
00315     puts("       It is part of the implementation of this library and is");
00316     puts("       subject to change. Applications should only use zlib.h.");
00317     puts("     */");
00318     puts("");
00319     size = 1U << 9;
00320     printf("    static const code lenfix[%u] = {", size);
00321     low = 0;
00322     for (;;) {
00323         if ((low % 7) == 0) printf("\n        ");
00324         printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
00325                state.lencode[low].val);
00326         if (++low == size) break;
00327         putchar(',');
00328     }
00329     puts("\n    };");
00330     size = 1U << 5;
00331     printf("\n    static const code distfix[%u] = {", size);
00332     low = 0;
00333     for (;;) {
00334         if ((low % 6) == 0) printf("\n        ");
00335         printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
00336                state.distcode[low].val);
00337         if (++low == size) break;
00338         putchar(',');
00339     }
00340     puts("\n    };");
00341 }
00342 #endif /* MAKEFIXED */
00343 
00344 /*
00345    Update the window with the last wsize (normally 32K) bytes written before
00346    returning.  If window does not exist yet, create it.  This is only called
00347    when a window is already in use, or when output has been written during this
00348    inflate call, but the end of the deflate stream has not been reached yet.
00349    It is also called to create a window for dictionary data when a dictionary
00350    is loaded.
00351 
00352    Providing output buffers larger than 32K to inflate() should provide a speed
00353    advantage, since only the last 32K of output is copied to the sliding window
00354    upon return from inflate(), and since all distances after the first 32K of
00355    output will fall in the output data, making match copies simpler and faster.
00356    The advantage may be dependent on the size of the processor's data caches.
00357  */
00358 local int updatewindow(strm, out)
00359 z_streamp strm;
00360 unsigned out;
00361 {
00362     struct inflate_state FAR *state;
00363     unsigned copy, dist;
00364 
00365     state = (struct inflate_state FAR *)strm->state;
00366 
00367     /* if it hasn't been done already, allocate space for the window */
00368     if (state->window == Z_NULL) {
00369         state->window = (unsigned char FAR *)
00370                         ZALLOC(strm, 1U << state->wbits,
00371                                sizeof(unsigned char));
00372         if (state->window == Z_NULL) return 1;
00373     }
00374 
00375     /* if window not in use yet, initialize */
00376     if (state->wsize == 0) {
00377         state->wsize = 1U << state->wbits;
00378         state->wnext = 0;
00379         state->whave = 0;
00380     }
00381 
00382     /* copy state->wsize or less output bytes into the circular window */
00383     copy = out - strm->avail_out;
00384     if (copy >= state->wsize) {
00385         zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
00386         state->wnext = 0;
00387         state->whave = state->wsize;
00388     }
00389     else {
00390         dist = state->wsize - state->wnext;
00391         if (dist > copy) dist = copy;
00392         zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
00393         copy -= dist;
00394         if (copy) {
00395             zmemcpy(state->window, strm->next_out - copy, copy);
00396             state->wnext = copy;
00397             state->whave = state->wsize;
00398         }
00399         else {
00400             state->wnext += dist;
00401             if (state->wnext == state->wsize) state->wnext = 0;
00402             if (state->whave < state->wsize) state->whave += dist;
00403         }
00404     }
00405     return 0;
00406 }
00407 
00408 /* Macros for inflate(): */
00409 
00410 /* check function to use adler32() for zlib or crc32() for gzip */
00411 #ifdef GUNZIP
00412 #  define UPDATE(check, buf, len) \
00413     (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
00414 #else
00415 #  define UPDATE(check, buf, len) adler32(check, buf, len)
00416 #endif
00417 
00418 /* check macros for header crc */
00419 #ifdef GUNZIP
00420 #  define CRC2(check, word) \
00421     do { \
00422         hbuf[0] = (unsigned char)(word); \
00423         hbuf[1] = (unsigned char)((word) >> 8); \
00424         check = crc32(check, hbuf, 2); \
00425     } while (0)
00426 
00427 #  define CRC4(check, word) \
00428     do { \
00429         hbuf[0] = (unsigned char)(word); \
00430         hbuf[1] = (unsigned char)((word) >> 8); \
00431         hbuf[2] = (unsigned char)((word) >> 16); \
00432         hbuf[3] = (unsigned char)((word) >> 24); \
00433         check = crc32(check, hbuf, 4); \
00434     } while (0)
00435 #endif
00436 
00437 /* Load registers with state in inflate() for speed */
00438 #define LOAD() \
00439     do { \
00440         put = strm->next_out; \
00441         left = strm->avail_out; \
00442         next = strm->next_in; \
00443         have = strm->avail_in; \
00444         hold = state->hold; \
00445         bits = state->bits; \
00446     } while (0)
00447 
00448 /* Restore state from registers in inflate() */
00449 #define RESTORE() \
00450     do { \
00451         strm->next_out = put; \
00452         strm->avail_out = left; \
00453         strm->next_in = next; \
00454         strm->avail_in = have; \
00455         state->hold = hold; \
00456         state->bits = bits; \
00457     } while (0)
00458 
00459 /* Clear the input bit accumulator */
00460 #define INITBITS() \
00461     do { \
00462         hold = 0; \
00463         bits = 0; \
00464     } while (0)
00465 
00466 /* Get a byte of input into the bit accumulator, or return from inflate()
00467    if there is no input available. */
00468 #define PULLBYTE() \
00469     do { \
00470         if (have == 0) goto inf_leave; \
00471         have--; \
00472         hold += (unsigned long)(*next++) << bits; \
00473         bits += 8; \
00474     } while (0)
00475 
00476 /* Assure that there are at least n bits in the bit accumulator.  If there is
00477    not enough available input to do that, then return from inflate(). */
00478 #define NEEDBITS(n) \
00479     do { \
00480         while (bits < (unsigned)(n)) \
00481             PULLBYTE(); \
00482     } while (0)
00483 
00484 /* Return the low n bits of the bit accumulator (n < 16) */
00485 #define BITS(n) \
00486     ((unsigned)hold & ((1U << (n)) - 1))
00487 
00488 /* Remove n bits from the bit accumulator */
00489 #define DROPBITS(n) \
00490     do { \
00491         hold >>= (n); \
00492         bits -= (unsigned)(n); \
00493     } while (0)
00494 
00495 /* Remove zero to seven bits as needed to go to a byte boundary */
00496 #define BYTEBITS() \
00497     do { \
00498         hold >>= bits & 7; \
00499         bits -= bits & 7; \
00500     } while (0)
00501 
00502 /* Reverse the bytes in a 32-bit value */
00503 #define REVERSE(q) \
00504     ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
00505      (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
00506 
00507 /*
00508    inflate() uses a state machine to process as much input data and generate as
00509    much output data as possible before returning.  The state machine is
00510    structured roughly as follows:
00511 
00512     for (;;) switch (state) {
00513     ...
00514     case STATEn:
00515         if (not enough input data or output space to make progress)
00516             return;
00517         ... make progress ...
00518         state = STATEm;
00519         break;
00520     ...
00521     }
00522 
00523    so when inflate() is called again, the same case is attempted again, and
00524    if the appropriate resources are provided, the machine proceeds to the
00525    next state.  The NEEDBITS() macro is usually the way the state evaluates
00526    whether it can proceed or should return.  NEEDBITS() does the return if
00527    the requested bits are not available.  The typical use of the BITS macros
00528    is:
00529 
00530         NEEDBITS(n);
00531         ... do something with BITS(n) ...
00532         DROPBITS(n);
00533 
00534    where NEEDBITS(n) either returns from inflate() if there isn't enough
00535    input left to load n bits into the accumulator, or it continues.  BITS(n)
00536    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
00537    the low n bits off the accumulator.  INITBITS() clears the accumulator
00538    and sets the number of available bits to zero.  BYTEBITS() discards just
00539    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
00540    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
00541 
00542    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
00543    if there is no input available.  The decoding of variable length codes uses
00544    PULLBYTE() directly in order to pull just enough bytes to decode the next
00545    code, and no more.
00546 
00547    Some states loop until they get enough input, making sure that enough
00548    state information is maintained to continue the loop where it left off
00549    if NEEDBITS() returns in the loop.  For example, want, need, and keep
00550    would all have to actually be part of the saved state in case NEEDBITS()
00551    returns:
00552 
00553     case STATEw:
00554         while (want < need) {
00555             NEEDBITS(n);
00556             keep[want++] = BITS(n);
00557             DROPBITS(n);
00558         }
00559         state = STATEx;
00560     case STATEx:
00561 
00562    As shown above, if the next state is also the next case, then the break
00563    is omitted.
00564 
00565    A state may also return if there is not enough output space available to
00566    complete that state.  Those states are copying stored data, writing a
00567    literal byte, and copying a matching string.
00568 
00569    When returning, a "goto inf_leave" is used to update the total counters,
00570    update the check value, and determine whether any progress has been made
00571    during that inflate() call in order to return the proper return code.
00572    Progress is defined as a change in either strm->avail_in or strm->avail_out.
00573    When there is a window, goto inf_leave will update the window with the last
00574    output written.  If a goto inf_leave occurs in the middle of decompression
00575    and there is no window currently, goto inf_leave will create one and copy
00576    output to the window for the next call of inflate().
00577 
00578    In this implementation, the flush parameter of inflate() only affects the
00579    return code (per zlib.h).  inflate() always writes as much as possible to
00580    strm->next_out, given the space available and the provided input--the effect
00581    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
00582    the allocation of and copying into a sliding window until necessary, which
00583    provides the effect documented in zlib.h for Z_FINISH when the entire input
00584    stream available.  So the only thing the flush parameter actually does is:
00585    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
00586    will return Z_BUF_ERROR if it has not reached the end of the stream.
00587  */
00588 
00589 int ZEXPORT inflate(strm, flush)
00590 z_streamp strm;
00591 int flush;
00592 {
00593     struct inflate_state FAR *state;
00594     unsigned char FAR *next;    /* next input */
00595     unsigned char FAR *put;     /* next output */
00596     unsigned have, left;        /* available input and output */
00597     unsigned long hold;         /* bit buffer */
00598     unsigned bits;              /* bits in bit buffer */
00599     unsigned in, out;           /* save starting available input and output */
00600     unsigned copy;              /* number of stored or match bytes to copy */
00601     unsigned char FAR *from;    /* where to copy match bytes from */
00602     code here;                  /* current decoding table entry */
00603     code last;                  /* parent table entry */
00604     unsigned len;               /* length to copy for repeats, bits to drop */
00605     int ret;                    /* return code */
00606 #ifdef GUNZIP
00607     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
00608 #endif
00609     static const unsigned short order[19] = /* permutation of code lengths */
00610         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
00611 
00612     if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
00613         (strm->next_in == Z_NULL && strm->avail_in != 0))
00614         return Z_STREAM_ERROR;
00615 
00616     state = (struct inflate_state FAR *)strm->state;
00617     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
00618     LOAD();
00619     in = have;
00620     out = left;
00621     ret = Z_OK;
00622     for (;;)
00623         switch (state->mode) {
00624         case HEAD:
00625             if (state->wrap == 0) {
00626                 state->mode = TYPEDO;
00627                 break;
00628             }
00629             NEEDBITS(16);
00630 #ifdef GUNZIP
00631             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
00632                 state->check = crc32(0L, Z_NULL, 0);
00633                 CRC2(state->check, hold);
00634                 INITBITS();
00635                 state->mode = FLAGS;
00636                 break;
00637             }
00638             state->flags = 0;           /* expect zlib header */
00639             if (state->head != Z_NULL)
00640                 state->head->done = -1;
00641             if (!(state->wrap & 1) ||   /* check if zlib header allowed */
00642 #else
00643             if (
00644 #endif
00645                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
00646                 strm->msg = (char *)"incorrect header check";
00647                 state->mode = BAD;
00648                 break;
00649             }
00650             if (BITS(4) != Z_DEFLATED) {
00651                 strm->msg = (char *)"unknown compression method";
00652                 state->mode = BAD;
00653                 break;
00654             }
00655             DROPBITS(4);
00656             len = BITS(4) + 8;
00657             if (state->wbits == 0)
00658                 state->wbits = len;
00659             else if (len > state->wbits) {
00660                 strm->msg = (char *)"invalid window size";
00661                 state->mode = BAD;
00662                 break;
00663             }
00664             state->dmax = 1U << len;
00665             Tracev((stderr, "inflate:   zlib header ok\n"));
00666             strm->adler = state->check = adler32(0L, Z_NULL, 0);
00667             state->mode = hold & 0x200 ? DICTID : TYPE;
00668             INITBITS();
00669             break;
00670 #ifdef GUNZIP
00671         case FLAGS:
00672             NEEDBITS(16);
00673             state->flags = (int)(hold);
00674             if ((state->flags & 0xff) != Z_DEFLATED) {
00675                 strm->msg = (char *)"unknown compression method";
00676                 state->mode = BAD;
00677                 break;
00678             }
00679             if (state->flags & 0xe000) {
00680                 strm->msg = (char *)"unknown header flags set";
00681                 state->mode = BAD;
00682                 break;
00683             }
00684             if (state->head != Z_NULL)
00685                 state->head->text = (int)((hold >> 8) & 1);
00686             if (state->flags & 0x0200) CRC2(state->check, hold);
00687             INITBITS();
00688             state->mode = TIME;
00689         case TIME:
00690             NEEDBITS(32);
00691             if (state->head != Z_NULL)
00692                 state->head->time = hold;
00693             if (state->flags & 0x0200) CRC4(state->check, hold);
00694             INITBITS();
00695             state->mode = OS;
00696         case OS:
00697             NEEDBITS(16);
00698             if (state->head != Z_NULL) {
00699                 state->head->xflags = (int)(hold & 0xff);
00700                 state->head->os = (int)(hold >> 8);
00701             }
00702             if (state->flags & 0x0200) CRC2(state->check, hold);
00703             INITBITS();
00704             state->mode = EXLEN;
00705         case EXLEN:
00706             if (state->flags & 0x0400) {
00707                 NEEDBITS(16);
00708                 state->length = (unsigned)(hold);
00709                 if (state->head != Z_NULL)
00710                     state->head->extra_len = (unsigned)hold;
00711                 if (state->flags & 0x0200) CRC2(state->check, hold);
00712                 INITBITS();
00713             }
00714             else if (state->head != Z_NULL)
00715                 state->head->extra = Z_NULL;
00716             state->mode = EXTRA;
00717         case EXTRA:
00718             if (state->flags & 0x0400) {
00719                 copy = state->length;
00720                 if (copy > have) copy = have;
00721                 if (copy) {
00722                     if (state->head != Z_NULL &&
00723                         state->head->extra != Z_NULL) {
00724                         len = state->head->extra_len - state->length;
00725                         zmemcpy(state->head->extra + len, next,
00726                                 len + copy > state->head->extra_max ?
00727                                 state->head->extra_max - len : copy);
00728                     }
00729                     if (state->flags & 0x0200)
00730                         state->check = crc32(state->check, next, copy);
00731                     have -= copy;
00732                     next += copy;
00733                     state->length -= copy;
00734                 }
00735                 if (state->length) goto inf_leave;
00736             }
00737             state->length = 0;
00738             state->mode = NAME;
00739         case NAME:
00740             if (state->flags & 0x0800) {
00741                 if (have == 0) goto inf_leave;
00742                 copy = 0;
00743                 do {
00744                     len = (unsigned)(next[copy++]);
00745                     if (state->head != Z_NULL &&
00746                             state->head->name != Z_NULL &&
00747                             state->length < state->head->name_max)
00748                         state->head->name[state->length++] = len;
00749                 } while (len && copy < have);
00750                 if (state->flags & 0x0200)
00751                     state->check = crc32(state->check, next, copy);
00752                 have -= copy;
00753                 next += copy;
00754                 if (len) goto inf_leave;
00755             }
00756             else if (state->head != Z_NULL)
00757                 state->head->name = Z_NULL;
00758             state->length = 0;
00759             state->mode = COMMENT;
00760         case COMMENT:
00761             if (state->flags & 0x1000) {
00762                 if (have == 0) goto inf_leave;
00763                 copy = 0;
00764                 do {
00765                     len = (unsigned)(next[copy++]);
00766                     if (state->head != Z_NULL &&
00767                             state->head->comment != Z_NULL &&
00768                             state->length < state->head->comm_max)
00769                         state->head->comment[state->length++] = len;
00770                 } while (len && copy < have);
00771                 if (state->flags & 0x0200)
00772                     state->check = crc32(state->check, next, copy);
00773                 have -= copy;
00774                 next += copy;
00775                 if (len) goto inf_leave;
00776             }
00777             else if (state->head != Z_NULL)
00778                 state->head->comment = Z_NULL;
00779             state->mode = HCRC;
00780         case HCRC:
00781             if (state->flags & 0x0200) {
00782                 NEEDBITS(16);
00783                 if (hold != (state->check & 0xffff)) {
00784                     strm->msg = (char *)"header crc mismatch";
00785                     state->mode = BAD;
00786                     break;
00787                 }
00788                 INITBITS();
00789             }
00790             if (state->head != Z_NULL) {
00791                 state->head->hcrc = (int)((state->flags >> 9) & 1);
00792                 state->head->done = 1;
00793             }
00794             strm->adler = state->check = crc32(0L, Z_NULL, 0);
00795             state->mode = TYPE;
00796             break;
00797 #endif
00798         case DICTID:
00799             NEEDBITS(32);
00800             strm->adler = state->check = REVERSE(hold);
00801             INITBITS();
00802             state->mode = DICT;
00803         case DICT:
00804             if (state->havedict == 0) {
00805                 RESTORE();
00806                 return Z_NEED_DICT;
00807             }
00808             strm->adler = state->check = adler32(0L, Z_NULL, 0);
00809             state->mode = TYPE;
00810         case TYPE:
00811             if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
00812         case TYPEDO:
00813             if (state->last) {
00814                 BYTEBITS();
00815                 state->mode = CHECK;
00816                 break;
00817             }
00818             NEEDBITS(3);
00819             state->last = BITS(1);
00820             DROPBITS(1);
00821             switch (BITS(2)) {
00822             case 0:                             /* stored block */
00823                 Tracev((stderr, "inflate:     stored block%s\n",
00824                         state->last ? " (last)" : ""));
00825                 state->mode = STORED;
00826                 break;
00827             case 1:                             /* fixed block */
00828                 fixedtables(state);
00829                 Tracev((stderr, "inflate:     fixed codes block%s\n",
00830                         state->last ? " (last)" : ""));
00831                 state->mode = LEN_;             /* decode codes */
00832                 if (flush == Z_TREES) {
00833                     DROPBITS(2);
00834                     goto inf_leave;
00835                 }
00836                 break;
00837             case 2:                             /* dynamic block */
00838                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
00839                         state->last ? " (last)" : ""));
00840                 state->mode = TABLE;
00841                 break;
00842             case 3:
00843                 strm->msg = (char *)"invalid block type";
00844                 state->mode = BAD;
00845             }
00846             DROPBITS(2);
00847             break;
00848         case STORED:
00849             BYTEBITS();                         /* go to byte boundary */
00850             NEEDBITS(32);
00851             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
00852                 strm->msg = (char *)"invalid stored block lengths";
00853                 state->mode = BAD;
00854                 break;
00855             }
00856             state->length = (unsigned)hold & 0xffff;
00857             Tracev((stderr, "inflate:       stored length %u\n",
00858                     state->length));
00859             INITBITS();
00860             state->mode = COPY_;
00861             if (flush == Z_TREES) goto inf_leave;
00862         case COPY_:
00863             state->mode = COPY;
00864         case COPY:
00865             copy = state->length;
00866             if (copy) {
00867                 if (copy > have) copy = have;
00868                 if (copy > left) copy = left;
00869                 if (copy == 0) goto inf_leave;
00870                 zmemcpy(put, next, copy);
00871                 have -= copy;
00872                 next += copy;
00873                 left -= copy;
00874                 put += copy;
00875                 state->length -= copy;
00876                 break;
00877             }
00878             Tracev((stderr, "inflate:       stored end\n"));
00879             state->mode = TYPE;
00880             break;
00881         case TABLE:
00882             NEEDBITS(14);
00883             state->nlen = BITS(5) + 257;
00884             DROPBITS(5);
00885             state->ndist = BITS(5) + 1;
00886             DROPBITS(5);
00887             state->ncode = BITS(4) + 4;
00888             DROPBITS(4);
00889 #ifndef PKZIP_BUG_WORKAROUND
00890             if (state->nlen > 286 || state->ndist > 30) {
00891                 strm->msg = (char *)"too many length or distance symbols";
00892                 state->mode = BAD;
00893                 break;
00894             }
00895 #endif
00896             Tracev((stderr, "inflate:       table sizes ok\n"));
00897             state->have = 0;
00898             state->mode = LENLENS;
00899         case LENLENS:
00900             while (state->have < state->ncode) {
00901                 NEEDBITS(3);
00902                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
00903                 DROPBITS(3);
00904             }
00905             while (state->have < 19)
00906                 state->lens[order[state->have++]] = 0;
00907             state->next = state->codes;
00908             state->lencode = (code const FAR *)(state->next);
00909             state->lenbits = 7;
00910             ret = inflate_table(CODES, state->lens, 19, &(state->next),
00911                                 &(state->lenbits), state->work);
00912             if (ret) {
00913                 strm->msg = (char *)"invalid code lengths set";
00914                 state->mode = BAD;
00915                 break;
00916             }
00917             Tracev((stderr, "inflate:       code lengths ok\n"));
00918             state->have = 0;
00919             state->mode = CODELENS;
00920         case CODELENS:
00921             while (state->have < state->nlen + state->ndist) {
00922                 for (;;) {
00923                     here = state->lencode[BITS(state->lenbits)];
00924                     if ((unsigned)(here.bits) <= bits) break;
00925                     PULLBYTE();
00926                 }
00927                 if (here.val < 16) {
00928                     NEEDBITS(here.bits);
00929                     DROPBITS(here.bits);
00930                     state->lens[state->have++] = here.val;
00931                 }
00932                 else {
00933                     if (here.val == 16) {
00934                         NEEDBITS(here.bits + 2);
00935                         DROPBITS(here.bits);
00936                         if (state->have == 0) {
00937                             strm->msg = (char *)"invalid bit length repeat";
00938                             state->mode = BAD;
00939                             break;
00940                         }
00941                         len = state->lens[state->have - 1];
00942                         copy = 3 + BITS(2);
00943                         DROPBITS(2);
00944                     }
00945                     else if (here.val == 17) {
00946                         NEEDBITS(here.bits + 3);
00947                         DROPBITS(here.bits);
00948                         len = 0;
00949                         copy = 3 + BITS(3);
00950                         DROPBITS(3);
00951                     }
00952                     else {
00953                         NEEDBITS(here.bits + 7);
00954                         DROPBITS(here.bits);
00955                         len = 0;
00956                         copy = 11 + BITS(7);
00957                         DROPBITS(7);
00958                     }
00959                     if (state->have + copy > state->nlen + state->ndist) {
00960                         strm->msg = (char *)"invalid bit length repeat";
00961                         state->mode = BAD;
00962                         break;
00963                     }
00964                     while (copy--)
00965                         state->lens[state->have++] = (unsigned short)len;
00966                 }
00967             }
00968 
00969             /* handle error breaks in while */
00970             if (state->mode == BAD) break;
00971 
00972             /* check for end-of-block code (better have one) */
00973             if (state->lens[256] == 0) {
00974                 strm->msg = (char *)"invalid code -- missing end-of-block";
00975                 state->mode = BAD;
00976                 break;
00977             }
00978 
00979             /* build code tables -- note: do not change the lenbits or distbits
00980                values here (9 and 6) without reading the comments in inftrees.h
00981                concerning the ENOUGH constants, which depend on those values */
00982             state->next = state->codes;
00983             state->lencode = (code const FAR *)(state->next);
00984             state->lenbits = 9;
00985             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
00986                                 &(state->lenbits), state->work);
00987             if (ret) {
00988                 strm->msg = (char *)"invalid literal/lengths set";
00989                 state->mode = BAD;
00990                 break;
00991             }
00992             state->distcode = (code const FAR *)(state->next);
00993             state->distbits = 6;
00994             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
00995                             &(state->next), &(state->distbits), state->work);
00996             if (ret) {
00997                 strm->msg = (char *)"invalid distances set";
00998                 state->mode = BAD;
00999                 break;
01000             }
01001             Tracev((stderr, "inflate:       codes ok\n"));
01002             state->mode = LEN_;
01003             if (flush == Z_TREES) goto inf_leave;
01004         case LEN_:
01005             state->mode = LEN;
01006         case LEN:
01007             if (have >= 6 && left >= 258) {
01008                 RESTORE();
01009                 inflate_fast(strm, out);
01010                 LOAD();
01011                 if (state->mode == TYPE)
01012                     state->back = -1;
01013                 break;
01014             }
01015             state->back = 0;
01016             for (;;) {
01017                 here = state->lencode[BITS(state->lenbits)];
01018                 if ((unsigned)(here.bits) <= bits) break;
01019                 PULLBYTE();
01020             }
01021             if (here.op && (here.op & 0xf0) == 0) {
01022                 last = here;
01023                 for (;;) {
01024                     here = state->lencode[last.val +
01025                             (BITS(last.bits + last.op) >> last.bits)];
01026                     if ((unsigned)(last.bits + here.bits) <= bits) break;
01027                     PULLBYTE();
01028                 }
01029                 DROPBITS(last.bits);
01030                 state->back += last.bits;
01031             }
01032             DROPBITS(here.bits);
01033             state->back += here.bits;
01034             state->length = (unsigned)here.val;
01035             if ((int)(here.op) == 0) {
01036                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
01037                         "inflate:         literal '%c'\n" :
01038                         "inflate:         literal 0x%02x\n", here.val));
01039                 state->mode = LIT;
01040                 break;
01041             }
01042             if (here.op & 32) {
01043                 Tracevv((stderr, "inflate:         end of block\n"));
01044                 state->back = -1;
01045                 state->mode = TYPE;
01046                 break;
01047             }
01048             if (here.op & 64) {
01049                 strm->msg = (char *)"invalid literal/length code";
01050                 state->mode = BAD;
01051                 break;
01052             }
01053             state->extra = (unsigned)(here.op) & 15;
01054             state->mode = LENEXT;
01055         case LENEXT:
01056             if (state->extra) {
01057                 NEEDBITS(state->extra);
01058                 state->length += BITS(state->extra);
01059                 DROPBITS(state->extra);
01060                 state->back += state->extra;
01061             }
01062             Tracevv((stderr, "inflate:         length %u\n", state->length));
01063             state->was = state->length;
01064             state->mode = DIST;
01065         case DIST:
01066             for (;;) {
01067                 here = state->distcode[BITS(state->distbits)];
01068                 if ((unsigned)(here.bits) <= bits) break;
01069                 PULLBYTE();
01070             }
01071             if ((here.op & 0xf0) == 0) {
01072                 last = here;
01073                 for (;;) {
01074                     here = state->distcode[last.val +
01075                             (BITS(last.bits + last.op) >> last.bits)];
01076                     if ((unsigned)(last.bits + here.bits) <= bits) break;
01077                     PULLBYTE();
01078                 }
01079                 DROPBITS(last.bits);
01080                 state->back += last.bits;
01081             }
01082             DROPBITS(here.bits);
01083             state->back += here.bits;
01084             if (here.op & 64) {
01085                 strm->msg = (char *)"invalid distance code";
01086                 state->mode = BAD;
01087                 break;
01088             }
01089             state->offset = (unsigned)here.val;
01090             state->extra = (unsigned)(here.op) & 15;
01091             state->mode = DISTEXT;
01092         case DISTEXT:
01093             if (state->extra) {
01094                 NEEDBITS(state->extra);
01095                 state->offset += BITS(state->extra);
01096                 DROPBITS(state->extra);
01097                 state->back += state->extra;
01098             }
01099 #ifdef INFLATE_STRICT
01100             if (state->offset > state->dmax) {
01101                 strm->msg = (char *)"invalid distance too far back";
01102                 state->mode = BAD;
01103                 break;
01104             }
01105 #endif
01106             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
01107             state->mode = MATCH;
01108         case MATCH:
01109             if (left == 0) goto inf_leave;
01110             copy = out - left;
01111             if (state->offset > copy) {         /* copy from window */
01112                 copy = state->offset - copy;
01113                 if (copy > state->whave) {
01114                     if (state->sane) {
01115                         strm->msg = (char *)"invalid distance too far back";
01116                         state->mode = BAD;
01117                         break;
01118                     }
01119 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
01120                     Trace((stderr, "inflate.c too far\n"));
01121                     copy -= state->whave;
01122                     if (copy > state->length) copy = state->length;
01123                     if (copy > left) copy = left;
01124                     left -= copy;
01125                     state->length -= copy;
01126                     do {
01127                         *put++ = 0;
01128                     } while (--copy);
01129                     if (state->length == 0) state->mode = LEN;
01130                     break;
01131 #endif
01132                 }
01133                 if (copy > state->wnext) {
01134                     copy -= state->wnext;
01135                     from = state->window + (state->wsize - copy);
01136                 }
01137                 else
01138                     from = state->window + (state->wnext - copy);
01139                 if (copy > state->length) copy = state->length;
01140             }
01141             else {                              /* copy from output */
01142                 from = put - state->offset;
01143                 copy = state->length;
01144             }
01145             if (copy > left) copy = left;
01146             left -= copy;
01147             state->length -= copy;
01148             do {
01149                 *put++ = *from++;
01150             } while (--copy);
01151             if (state->length == 0) state->mode = LEN;
01152             break;
01153         case LIT:
01154             if (left == 0) goto inf_leave;
01155             *put++ = (unsigned char)(state->length);
01156             left--;
01157             state->mode = LEN;
01158             break;
01159         case CHECK:
01160             if (state->wrap) {
01161                 NEEDBITS(32);
01162                 out -= left;
01163                 strm->total_out += out;
01164                 state->total += out;
01165                 if (out)
01166                     strm->adler = state->check =
01167                         UPDATE(state->check, put - out, out);
01168                 out = left;
01169                 if ((
01170 #ifdef GUNZIP
01171                      state->flags ? hold :
01172 #endif
01173                      REVERSE(hold)) != state->check) {
01174                     strm->msg = (char *)"incorrect data check";
01175                     state->mode = BAD;
01176                     break;
01177                 }
01178                 INITBITS();
01179                 Tracev((stderr, "inflate:   check matches trailer\n"));
01180             }
01181 #ifdef GUNZIP
01182             state->mode = LENGTH;
01183         case LENGTH:
01184             if (state->wrap && state->flags) {
01185                 NEEDBITS(32);
01186                 if (hold != (state->total & 0xffffffffUL)) {
01187                     strm->msg = (char *)"incorrect length check";
01188                     state->mode = BAD;
01189                     break;
01190                 }
01191                 INITBITS();
01192                 Tracev((stderr, "inflate:   length matches trailer\n"));
01193             }
01194 #endif
01195             state->mode = DONE;
01196         case DONE:
01197             ret = Z_STREAM_END;
01198             goto inf_leave;
01199         case BAD:
01200             ret = Z_DATA_ERROR;
01201             goto inf_leave;
01202         case MEM:
01203             return Z_MEM_ERROR;
01204         case SYNC:
01205         default:
01206             return Z_STREAM_ERROR;
01207         }
01208 
01209     /*
01210        Return from inflate(), updating the total counts and the check value.
01211        If there was no progress during the inflate() call, return a buffer
01212        error.  Call updatewindow() to create and/or update the window state.
01213        Note: a memory error from inflate() is non-recoverable.
01214      */
01215   inf_leave:
01216     RESTORE();
01217     if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
01218         if (updatewindow(strm, out)) {
01219             state->mode = MEM;
01220             return Z_MEM_ERROR;
01221         }
01222     in -= strm->avail_in;
01223     out -= strm->avail_out;
01224     strm->total_in += in;
01225     strm->total_out += out;
01226     state->total += out;
01227     if (state->wrap && out)
01228         strm->adler = state->check =
01229             UPDATE(state->check, strm->next_out - out, out);
01230     strm->data_type = state->bits + (state->last ? 64 : 0) +
01231                       (state->mode == TYPE ? 128 : 0) +
01232                       (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
01233     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
01234         ret = Z_BUF_ERROR;
01235     return ret;
01236 }
01237 
01238 int ZEXPORT inflateEnd(strm)
01239 z_streamp strm;
01240 {
01241     struct inflate_state FAR *state;
01242     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
01243         return Z_STREAM_ERROR;
01244     state = (struct inflate_state FAR *)strm->state;
01245     if (state->window != Z_NULL) ZFREE(strm, state->window);
01246     ZFREE(strm, strm->state);
01247     strm->state = Z_NULL;
01248     Tracev((stderr, "inflate: end\n"));
01249     return Z_OK;
01250 }
01251 
01252 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
01253 z_streamp strm;
01254 const Bytef *dictionary;
01255 uInt dictLength;
01256 {
01257     struct inflate_state FAR *state;
01258     unsigned long id;
01259 
01260     /* check state */
01261     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01262     state = (struct inflate_state FAR *)strm->state;
01263     if (state->wrap != 0 && state->mode != DICT)
01264         return Z_STREAM_ERROR;
01265 
01266     /* check for correct dictionary id */
01267     if (state->mode == DICT) {
01268         id = adler32(0L, Z_NULL, 0);
01269         id = adler32(id, dictionary, dictLength);
01270         if (id != state->check)
01271             return Z_DATA_ERROR;
01272     }
01273 
01274     /* copy dictionary to window */
01275     if (updatewindow(strm, strm->avail_out)) {
01276         state->mode = MEM;
01277         return Z_MEM_ERROR;
01278     }
01279     if (dictLength > state->wsize) {
01280         zmemcpy(state->window, dictionary + dictLength - state->wsize,
01281                 state->wsize);
01282         state->whave = state->wsize;
01283     }
01284     else {
01285         zmemcpy(state->window + state->wsize - dictLength, dictionary,
01286                 dictLength);
01287         state->whave = dictLength;
01288     }
01289     state->havedict = 1;
01290     Tracev((stderr, "inflate:   dictionary set\n"));
01291     return Z_OK;
01292 }
01293 
01294 int ZEXPORT inflateGetHeader(strm, head)
01295 z_streamp strm;
01296 gz_headerp head;
01297 {
01298     struct inflate_state FAR *state;
01299 
01300     /* check state */
01301     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01302     state = (struct inflate_state FAR *)strm->state;
01303     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
01304 
01305     /* save header structure */
01306     state->head = head;
01307     head->done = 0;
01308     return Z_OK;
01309 }
01310 
01311 /*
01312    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
01313    or when out of input.  When called, *have is the number of pattern bytes
01314    found in order so far, in 0..3.  On return *have is updated to the new
01315    state.  If on return *have equals four, then the pattern was found and the
01316    return value is how many bytes were read including the last byte of the
01317    pattern.  If *have is less than four, then the pattern has not been found
01318    yet and the return value is len.  In the latter case, syncsearch() can be
01319    called again with more data and the *have state.  *have is initialized to
01320    zero for the first call.
01321  */
01322 local unsigned syncsearch(have, buf, len)
01323 unsigned FAR *have;
01324 unsigned char FAR *buf;
01325 unsigned len;
01326 {
01327     unsigned got;
01328     unsigned next;
01329 
01330     got = *have;
01331     next = 0;
01332     while (next < len && got < 4) {
01333         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
01334             got++;
01335         else if (buf[next])
01336             got = 0;
01337         else
01338             got = 4 - got;
01339         next++;
01340     }
01341     *have = got;
01342     return next;
01343 }
01344 
01345 int ZEXPORT inflateSync(strm)
01346 z_streamp strm;
01347 {
01348     unsigned len;               /* number of bytes to look at or looked at */
01349     unsigned long in, out;      /* temporary to save total_in and total_out */
01350     unsigned char buf[4];       /* to restore bit buffer to byte string */
01351     struct inflate_state FAR *state;
01352 
01353     /* check parameters */
01354     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01355     state = (struct inflate_state FAR *)strm->state;
01356     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
01357 
01358     /* if first time, start search in bit buffer */
01359     if (state->mode != SYNC) {
01360         state->mode = SYNC;
01361         state->hold <<= state->bits & 7;
01362         state->bits -= state->bits & 7;
01363         len = 0;
01364         while (state->bits >= 8) {
01365             buf[len++] = (unsigned char)(state->hold);
01366             state->hold >>= 8;
01367             state->bits -= 8;
01368         }
01369         state->have = 0;
01370         syncsearch(&(state->have), buf, len);
01371     }
01372 
01373     /* search available input */
01374     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
01375     strm->avail_in -= len;
01376     strm->next_in += len;
01377     strm->total_in += len;
01378 
01379     /* return no joy or set up to restart inflate() on a new block */
01380     if (state->have != 4) return Z_DATA_ERROR;
01381     in = strm->total_in;  out = strm->total_out;
01382     inflateReset(strm);
01383     strm->total_in = in;  strm->total_out = out;
01384     state->mode = TYPE;
01385     return Z_OK;
01386 }
01387 
01388 /*
01389    Returns true if inflate is currently at the end of a block generated by
01390    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
01391    implementation to provide an additional safety check. PPP uses
01392    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
01393    block. When decompressing, PPP checks that at the end of input packet,
01394    inflate is waiting for these length bytes.
01395  */
01396 int ZEXPORT inflateSyncPoint(strm)
01397 z_streamp strm;
01398 {
01399     struct inflate_state FAR *state;
01400 
01401     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01402     state = (struct inflate_state FAR *)strm->state;
01403     return state->mode == STORED && state->bits == 0;
01404 }
01405 
01406 int ZEXPORT inflateCopy(dest, source)
01407 z_streamp dest;
01408 z_streamp source;
01409 {
01410     struct inflate_state FAR *state;
01411     struct inflate_state FAR *copy;
01412     unsigned char FAR *window;
01413     unsigned wsize;
01414 
01415     /* check input */
01416     if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
01417         source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
01418         return Z_STREAM_ERROR;
01419     state = (struct inflate_state FAR *)source->state;
01420 
01421     /* allocate space */
01422     copy = (struct inflate_state FAR *)
01423            ZALLOC(source, 1, sizeof(struct inflate_state));
01424     if (copy == Z_NULL) return Z_MEM_ERROR;
01425     window = Z_NULL;
01426     if (state->window != Z_NULL) {
01427         window = (unsigned char FAR *)
01428                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
01429         if (window == Z_NULL) {
01430             ZFREE(source, copy);
01431             return Z_MEM_ERROR;
01432         }
01433     }
01434 
01435     /* copy state */
01436     zmemcpy(dest, source, sizeof(z_stream));
01437     zmemcpy(copy, state, sizeof(struct inflate_state));
01438     if (state->lencode >= state->codes &&
01439         state->lencode <= state->codes + ENOUGH - 1) {
01440         copy->lencode = copy->codes + (state->lencode - state->codes);
01441         copy->distcode = copy->codes + (state->distcode - state->codes);
01442     }
01443     copy->next = copy->codes + (state->next - state->codes);
01444     if (window != Z_NULL) {
01445         wsize = 1U << state->wbits;
01446         zmemcpy(window, state->window, wsize);
01447     }
01448     copy->window = window;
01449     dest->state = (struct internal_state FAR *)copy;
01450     return Z_OK;
01451 }
01452 
01453 int ZEXPORT inflateUndermine(strm, subvert)
01454 z_streamp strm;
01455 int subvert;
01456 {
01457     struct inflate_state FAR *state;
01458 
01459     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01460     state = (struct inflate_state FAR *)strm->state;
01461     state->sane = !subvert;
01462 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
01463     return Z_OK;
01464 #else
01465     state->sane = 1;
01466     return Z_DATA_ERROR;
01467 #endif
01468 }
01469 
01470 long ZEXPORT inflateMark(strm)
01471 z_streamp strm;
01472 {
01473     struct inflate_state FAR *state;
01474 
01475     if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
01476     state = (struct inflate_state FAR *)strm->state;
01477     return ((long)(state->back) << 16) +
01478         (state->mode == COPY ? state->length :
01479             (state->mode == MATCH ? state->was - state->length : 0));
01480 }

Generated on Wed Oct 20 2010 11:12:18 for APBS by  doxygen 1.7.2