Basic gzip/gunzip in memory buffer examples using zlib code.

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers inflate.c Source File

inflate.c

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