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


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17