Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/zlib/src/inflate.c

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/zlib/src/inflate.c (Version 11.3.0) and /externals/zlib/src/inflate.c (Version 11.0.p4)


  1 /* inflate.c -- zlib decompression                  1 /* inflate.c -- zlib decompression
  2  * Copyright (C) 1995-2022 Mark Adler          <<   2  * Copyright (C) 1995-2016 Mark Adler
  3  * For conditions of distribution and use, see      3  * For conditions of distribution and use, see copyright notice in zlib.h
  4  */                                                 4  */
  5                                                     5 
  6 /*                                                  6 /*
  7  * Change history:                                  7  * Change history:
  8  *                                                  8  *
  9  * 1.2.beta0    24 Nov 2002                         9  * 1.2.beta0    24 Nov 2002
 10  * - First version -- complete rewrite of infl     10  * - First version -- complete rewrite of inflate to simplify code, avoid
 11  *   creation of window when not needed, minim     11  *   creation of window when not needed, minimize use of window when it is
 12  *   needed, make inffast.c even faster, imple     12  *   needed, make inffast.c even faster, implement gzip decoding, and to
 13  *   improve code readability and style over t     13  *   improve code readability and style over the previous zlib inflate code
 14  *                                                 14  *
 15  * 1.2.beta1    25 Nov 2002                        15  * 1.2.beta1    25 Nov 2002
 16  * - Use pointers for available input and outp     16  * - Use pointers for available input and output checking in inffast.c
 17  * - Remove input and output counters in inffa     17  * - Remove input and output counters in inffast.c
 18  * - Change inffast.c entry and loop from avai     18  * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
 19  * - Remove unnecessary second byte pull from      19  * - Remove unnecessary second byte pull from length extra in inffast.c
 20  * - Unroll direct copy to three copies per lo     20  * - Unroll direct copy to three copies per loop in inffast.c
 21  *                                                 21  *
 22  * 1.2.beta2    4 Dec 2002                         22  * 1.2.beta2    4 Dec 2002
 23  * - Change external routine names to reduce p     23  * - Change external routine names to reduce potential conflicts
 24  * - Correct filename to inffixed.h for fixed      24  * - Correct filename to inffixed.h for fixed tables in inflate.c
 25  * - Make hbuf[] unsigned char to match parame     25  * - Make hbuf[] unsigned char to match parameter type in inflate.c
 26  * - Change strm->next_out[-state->offset] to      26  * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
 27  *   to avoid negation problem on Alphas (64 b     27  *   to avoid negation problem on Alphas (64 bit) in inflate.c
 28  *                                                 28  *
 29  * 1.2.beta3    22 Dec 2002                        29  * 1.2.beta3    22 Dec 2002
 30  * - Add comments on state->bits assertion in      30  * - Add comments on state->bits assertion in inffast.c
 31  * - Add comments on op field in inftrees.h        31  * - Add comments on op field in inftrees.h
 32  * - Fix bug in reuse of allocated window afte     32  * - Fix bug in reuse of allocated window after inflateReset()
 33  * - Remove bit fields--back to byte structure     33  * - Remove bit fields--back to byte structure for speed
 34  * - Remove distance extra == 0 check in infla     34  * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
 35  * - Change post-increments to pre-increments      35  * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
 36  * - Add compile time option, POSTINC, to use      36  * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
 37  * - Make MATCH copy in inflate() much faster      37  * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
 38  * - Use local copies of stream next and avail     38  * - Use local copies of stream next and avail values, as well as local bit
 39  *   buffer and bit count in inflate()--for sp     39  *   buffer and bit count in inflate()--for speed when inflate_fast() not used
 40  *                                                 40  *
 41  * 1.2.beta4    1 Jan 2003                         41  * 1.2.beta4    1 Jan 2003
 42  * - Split ptr - 257 statements in inflate_tab     42  * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
 43  * - Move a comment on output buffer sizes fro     43  * - Move a comment on output buffer sizes from inffast.c to inflate.c
 44  * - Add comments in inffast.c to introduce th     44  * - Add comments in inffast.c to introduce the inflate_fast() routine
 45  * - Rearrange window copies in inflate_fast()     45  * - Rearrange window copies in inflate_fast() for speed and simplification
 46  * - Unroll last copy for window match in infl     46  * - Unroll last copy for window match in inflate_fast()
 47  * - Use local copies of window variables in i     47  * - Use local copies of window variables in inflate_fast() for speed
 48  * - Pull out common wnext == 0 case for speed     48  * - Pull out common wnext == 0 case for speed in inflate_fast()
 49  * - Make op and len in inflate_fast() unsigne     49  * - Make op and len in inflate_fast() unsigned for consistency
 50  * - Add FAR to lcode and dcode declarations i     50  * - Add FAR to lcode and dcode declarations in inflate_fast()
 51  * - Simplified bad distance check in inflate_     51  * - Simplified bad distance check in inflate_fast()
 52  * - Added inflateBackInit(), inflateBack(), a     52  * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
 53  *   source file infback.c to provide a call-b     53  *   source file infback.c to provide a call-back interface to inflate for
 54  *   programs like gzip and unzip -- uses wind     54  *   programs like gzip and unzip -- uses window as output buffer to avoid
 55  *   window copying                                55  *   window copying
 56  *                                                 56  *
 57  * 1.2.beta5    1 Jan 2003                         57  * 1.2.beta5    1 Jan 2003
 58  * - Improved inflateBack() interface to allow     58  * - Improved inflateBack() interface to allow the caller to provide initial
 59  *   input in strm.                                59  *   input in strm.
 60  * - Fixed stored blocks bug in inflateBack()      60  * - Fixed stored blocks bug in inflateBack()
 61  *                                                 61  *
 62  * 1.2.beta6    4 Jan 2003                         62  * 1.2.beta6    4 Jan 2003
 63  * - Added comments in inffast.c on effectiven     63  * - Added comments in inffast.c on effectiveness of POSTINC
 64  * - Typecasting all around to reduce compiler     64  * - Typecasting all around to reduce compiler warnings
 65  * - Changed loops from while (1) or do {} whi     65  * - Changed loops from while (1) or do {} while (1) to for (;;), again to
 66  *   make compilers happy                          66  *   make compilers happy
 67  * - Changed type of window in inflateBackInit     67  * - Changed type of window in inflateBackInit() to unsigned char *
 68  *                                                 68  *
 69  * 1.2.beta7    27 Jan 2003                        69  * 1.2.beta7    27 Jan 2003
 70  * - Changed many types to unsigned or unsigne     70  * - Changed many types to unsigned or unsigned short to avoid warnings
 71  * - Added inflateCopy() function                  71  * - Added inflateCopy() function
 72  *                                                 72  *
 73  * 1.2.0        9 Mar 2003                         73  * 1.2.0        9 Mar 2003
 74  * - Changed inflateBack() interface to provid     74  * - Changed inflateBack() interface to provide separate opaque descriptors
 75  *   for the in() and out() functions              75  *   for the in() and out() functions
 76  * - Changed inflateBack() argument and in_fun     76  * - Changed inflateBack() argument and in_func typedef to swap the length
 77  *   and buffer address return values for the      77  *   and buffer address return values for the input function
 78  * - Check next_in and next_out for Z_NULL on      78  * - Check next_in and next_out for Z_NULL on entry to inflate()
 79  *                                                 79  *
 80  * The history for versions after 1.2.0 are in     80  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
 81  */                                                81  */
 82                                                    82 
 83 #include "zutil.h"                                 83 #include "zutil.h"
 84 #include "inftrees.h"                              84 #include "inftrees.h"
 85 #include "inflate.h"                               85 #include "inflate.h"
 86 #include "inffast.h"                               86 #include "inffast.h"
 87                                                    87 
 88 #ifdef MAKEFIXED                                   88 #ifdef MAKEFIXED
 89 #  ifndef BUILDFIXED                               89 #  ifndef BUILDFIXED
 90 #    define BUILDFIXED                             90 #    define BUILDFIXED
 91 #  endif                                           91 #  endif
 92 #endif                                             92 #endif
 93                                                    93 
 94 /* function prototypes */                          94 /* function prototypes */
 95 local int inflateStateCheck OF((z_streamp strm     95 local int inflateStateCheck OF((z_streamp strm));
 96 local void fixedtables OF((struct inflate_stat     96 local void fixedtables OF((struct inflate_state FAR *state));
 97 local int updatewindow OF((z_streamp strm, con     97 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
 98                            unsigned copy));        98                            unsigned copy));
 99 #ifdef BUILDFIXED                                  99 #ifdef BUILDFIXED
100    void makefixed OF((void));                     100    void makefixed OF((void));
101 #endif                                            101 #endif
102 local unsigned syncsearch OF((unsigned FAR *ha    102 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
103                               unsigned len));     103                               unsigned len));
104                                                   104 
105 local int inflateStateCheck(strm)                 105 local int inflateStateCheck(strm)
106 z_streamp strm;                                   106 z_streamp strm;
107 {                                                 107 {
108     struct inflate_state FAR *state;              108     struct inflate_state FAR *state;
109     if (strm == Z_NULL ||                         109     if (strm == Z_NULL ||
110         strm->zalloc == (alloc_func)0 || strm-    110         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
111         return 1;                                 111         return 1;
112     state = (struct inflate_state FAR *)strm->    112     state = (struct inflate_state FAR *)strm->state;
113     if (state == Z_NULL || state->strm != strm    113     if (state == Z_NULL || state->strm != strm ||
114         state->mode < HEAD || state->mode > SY    114         state->mode < HEAD || state->mode > SYNC)
115         return 1;                                 115         return 1;
116     return 0;                                     116     return 0;
117 }                                                 117 }
118                                                   118 
119 int ZEXPORT inflateResetKeep(strm)                119 int ZEXPORT inflateResetKeep(strm)
120 z_streamp strm;                                   120 z_streamp strm;
121 {                                                 121 {
122     struct inflate_state FAR *state;              122     struct inflate_state FAR *state;
123                                                   123 
124     if (inflateStateCheck(strm)) return Z_STRE    124     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
125     state = (struct inflate_state FAR *)strm->    125     state = (struct inflate_state FAR *)strm->state;
126     strm->total_in = strm->total_out = state->    126     strm->total_in = strm->total_out = state->total = 0;
127     strm->msg = Z_NULL;                           127     strm->msg = Z_NULL;
128     if (state->wrap)        /* to support ill-    128     if (state->wrap)        /* to support ill-conceived Java test suite */
129         strm->adler = state->wrap & 1;            129         strm->adler = state->wrap & 1;
130     state->mode = HEAD;                           130     state->mode = HEAD;
131     state->last = 0;                              131     state->last = 0;
132     state->havedict = 0;                          132     state->havedict = 0;
133     state->flags = -1;                         << 
134     state->dmax = 32768U;                         133     state->dmax = 32768U;
135     state->head = Z_NULL;                         134     state->head = Z_NULL;
136     state->hold = 0;                              135     state->hold = 0;
137     state->bits = 0;                              136     state->bits = 0;
138     state->lencode = state->distcode = state->    137     state->lencode = state->distcode = state->next = state->codes;
139     state->sane = 1;                              138     state->sane = 1;
140     state->back = -1;                             139     state->back = -1;
141     Tracev((stderr, "inflate: reset\n"));         140     Tracev((stderr, "inflate: reset\n"));
142     return Z_OK;                                  141     return Z_OK;
143 }                                                 142 }
144                                                   143 
145 int ZEXPORT inflateReset(strm)                    144 int ZEXPORT inflateReset(strm)
146 z_streamp strm;                                   145 z_streamp strm;
147 {                                                 146 {
148     struct inflate_state FAR *state;              147     struct inflate_state FAR *state;
149                                                   148 
150     if (inflateStateCheck(strm)) return Z_STRE    149     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
151     state = (struct inflate_state FAR *)strm->    150     state = (struct inflate_state FAR *)strm->state;
152     state->wsize = 0;                             151     state->wsize = 0;
153     state->whave = 0;                             152     state->whave = 0;
154     state->wnext = 0;                             153     state->wnext = 0;
155     return inflateResetKeep(strm);                154     return inflateResetKeep(strm);
156 }                                                 155 }
157                                                   156 
158 int ZEXPORT inflateReset2(strm, windowBits)       157 int ZEXPORT inflateReset2(strm, windowBits)
159 z_streamp strm;                                   158 z_streamp strm;
160 int windowBits;                                   159 int windowBits;
161 {                                                 160 {
162     int wrap;                                     161     int wrap;
163     struct inflate_state FAR *state;              162     struct inflate_state FAR *state;
164                                                   163 
165     /* get the state */                           164     /* get the state */
166     if (inflateStateCheck(strm)) return Z_STRE    165     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
167     state = (struct inflate_state FAR *)strm->    166     state = (struct inflate_state FAR *)strm->state;
168                                                   167 
169     /* extract wrap request from windowBits pa    168     /* extract wrap request from windowBits parameter */
170     if (windowBits < 0) {                         169     if (windowBits < 0) {
171         if (windowBits < -15)                  << 
172             return Z_STREAM_ERROR;             << 
173         wrap = 0;                                 170         wrap = 0;
174         windowBits = -windowBits;                 171         windowBits = -windowBits;
175     }                                             172     }
176     else {                                        173     else {
177         wrap = (windowBits >> 4) + 5;             174         wrap = (windowBits >> 4) + 5;
178 #ifdef GUNZIP                                     175 #ifdef GUNZIP
179         if (windowBits < 48)                      176         if (windowBits < 48)
180             windowBits &= 15;                     177             windowBits &= 15;
181 #endif                                            178 #endif
182     }                                             179     }
183                                                   180 
184     /* set number of window bits, free window     181     /* set number of window bits, free window if different */
185     if (windowBits && (windowBits < 8 || windo    182     if (windowBits && (windowBits < 8 || windowBits > 15))
186         return Z_STREAM_ERROR;                    183         return Z_STREAM_ERROR;
187     if (state->window != Z_NULL && state->wbit    184     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
188         ZFREE(strm, state->window);               185         ZFREE(strm, state->window);
189         state->window = Z_NULL;                   186         state->window = Z_NULL;
190     }                                             187     }
191                                                   188 
192     /* update state and reset the rest of it *    189     /* update state and reset the rest of it */
193     state->wrap = wrap;                           190     state->wrap = wrap;
194     state->wbits = (unsigned)windowBits;          191     state->wbits = (unsigned)windowBits;
195     return inflateReset(strm);                    192     return inflateReset(strm);
196 }                                                 193 }
197                                                   194 
198 int ZEXPORT inflateInit2_(strm, windowBits, ve    195 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
199 z_streamp strm;                                   196 z_streamp strm;
200 int windowBits;                                   197 int windowBits;
201 const char *version;                              198 const char *version;
202 int stream_size;                                  199 int stream_size;
203 {                                                 200 {
204     int ret;                                      201     int ret;
205     struct inflate_state FAR *state;              202     struct inflate_state FAR *state;
206                                                   203 
207     if (version == Z_NULL || version[0] != ZLI    204     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
208         stream_size != (int)(sizeof(z_stream))    205         stream_size != (int)(sizeof(z_stream)))
209         return Z_VERSION_ERROR;                   206         return Z_VERSION_ERROR;
210     if (strm == Z_NULL) return Z_STREAM_ERROR;    207     if (strm == Z_NULL) return Z_STREAM_ERROR;
211     strm->msg = Z_NULL;                 /* in     208     strm->msg = Z_NULL;                 /* in case we return an error */
212     if (strm->zalloc == (alloc_func)0) {          209     if (strm->zalloc == (alloc_func)0) {
213 #ifdef Z_SOLO                                     210 #ifdef Z_SOLO
214         return Z_STREAM_ERROR;                    211         return Z_STREAM_ERROR;
215 #else                                             212 #else
216         strm->zalloc = zcalloc;                   213         strm->zalloc = zcalloc;
217         strm->opaque = (voidpf)0;                 214         strm->opaque = (voidpf)0;
218 #endif                                            215 #endif
219     }                                             216     }
220     if (strm->zfree == (free_func)0)              217     if (strm->zfree == (free_func)0)
221 #ifdef Z_SOLO                                     218 #ifdef Z_SOLO
222         return Z_STREAM_ERROR;                    219         return Z_STREAM_ERROR;
223 #else                                             220 #else
224         strm->zfree = zcfree;                     221         strm->zfree = zcfree;
225 #endif                                            222 #endif
226     state = (struct inflate_state FAR *)          223     state = (struct inflate_state FAR *)
227             ZALLOC(strm, 1, sizeof(struct infl    224             ZALLOC(strm, 1, sizeof(struct inflate_state));
228     if (state == Z_NULL) return Z_MEM_ERROR;      225     if (state == Z_NULL) return Z_MEM_ERROR;
229     Tracev((stderr, "inflate: allocated\n"));     226     Tracev((stderr, "inflate: allocated\n"));
230     strm->state = (struct internal_state FAR *    227     strm->state = (struct internal_state FAR *)state;
231     state->strm = strm;                           228     state->strm = strm;
232     state->window = Z_NULL;                       229     state->window = Z_NULL;
233     state->mode = HEAD;     /* to pass state t    230     state->mode = HEAD;     /* to pass state test in inflateReset2() */
234     ret = inflateReset2(strm, windowBits);        231     ret = inflateReset2(strm, windowBits);
235     if (ret != Z_OK) {                            232     if (ret != Z_OK) {
236         ZFREE(strm, state);                       233         ZFREE(strm, state);
237         strm->state = Z_NULL;                     234         strm->state = Z_NULL;
238     }                                             235     }
239     return ret;                                   236     return ret;
240 }                                                 237 }
241                                                   238 
242 int ZEXPORT inflateInit_(strm, version, stream    239 int ZEXPORT inflateInit_(strm, version, stream_size)
243 z_streamp strm;                                   240 z_streamp strm;
244 const char *version;                              241 const char *version;
245 int stream_size;                                  242 int stream_size;
246 {                                                 243 {
247     return inflateInit2_(strm, DEF_WBITS, vers    244     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
248 }                                                 245 }
249                                                   246 
250 int ZEXPORT inflatePrime(strm, bits, value)       247 int ZEXPORT inflatePrime(strm, bits, value)
251 z_streamp strm;                                   248 z_streamp strm;
252 int bits;                                         249 int bits;
253 int value;                                        250 int value;
254 {                                                 251 {
255     struct inflate_state FAR *state;              252     struct inflate_state FAR *state;
256                                                   253 
257     if (inflateStateCheck(strm)) return Z_STRE    254     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
258     state = (struct inflate_state FAR *)strm->    255     state = (struct inflate_state FAR *)strm->state;
259     if (bits < 0) {                               256     if (bits < 0) {
260         state->hold = 0;                          257         state->hold = 0;
261         state->bits = 0;                          258         state->bits = 0;
262         return Z_OK;                              259         return Z_OK;
263     }                                             260     }
264     if (bits > 16 || state->bits + (uInt)bits     261     if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
265     value &= (1L << bits) - 1;                    262     value &= (1L << bits) - 1;
266     state->hold += (unsigned)value << state->b    263     state->hold += (unsigned)value << state->bits;
267     state->bits += (uInt)bits;                    264     state->bits += (uInt)bits;
268     return Z_OK;                                  265     return Z_OK;
269 }                                                 266 }
270                                                   267 
271 /*                                                268 /*
272    Return state with length and distance decod    269    Return state with length and distance decoding tables and index sizes set to
273    fixed code decoding.  Normally this returns    270    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
274    If BUILDFIXED is defined, then instead this    271    If BUILDFIXED is defined, then instead this routine builds the tables the
275    first time it's called, and returns those t    272    first time it's called, and returns those tables the first time and
276    thereafter.  This reduces the size of the c    273    thereafter.  This reduces the size of the code by about 2K bytes, in
277    exchange for a little execution time.  Howe    274    exchange for a little execution time.  However, BUILDFIXED should not be
278    used for threaded applications, since the r    275    used for threaded applications, since the rewriting of the tables and virgin
279    may not be thread-safe.                        276    may not be thread-safe.
280  */                                               277  */
281 local void fixedtables(state)                     278 local void fixedtables(state)
282 struct inflate_state FAR *state;                  279 struct inflate_state FAR *state;
283 {                                                 280 {
284 #ifdef BUILDFIXED                                 281 #ifdef BUILDFIXED
285     static int virgin = 1;                        282     static int virgin = 1;
286     static code *lenfix, *distfix;                283     static code *lenfix, *distfix;
287     static code fixed[544];                       284     static code fixed[544];
288                                                   285 
289     /* build fixed huffman tables if first cal    286     /* build fixed huffman tables if first call (may not be thread safe) */
290     if (virgin) {                                 287     if (virgin) {
291         unsigned sym, bits;                       288         unsigned sym, bits;
292         static code *next;                        289         static code *next;
293                                                   290 
294         /* literal/length table */                291         /* literal/length table */
295         sym = 0;                                  292         sym = 0;
296         while (sym < 144) state->lens[sym++] =    293         while (sym < 144) state->lens[sym++] = 8;
297         while (sym < 256) state->lens[sym++] =    294         while (sym < 256) state->lens[sym++] = 9;
298         while (sym < 280) state->lens[sym++] =    295         while (sym < 280) state->lens[sym++] = 7;
299         while (sym < 288) state->lens[sym++] =    296         while (sym < 288) state->lens[sym++] = 8;
300         next = fixed;                             297         next = fixed;
301         lenfix = next;                            298         lenfix = next;
302         bits = 9;                                 299         bits = 9;
303         inflate_table(LENS, state->lens, 288,     300         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
304                                                   301 
305         /* distance table */                      302         /* distance table */
306         sym = 0;                                  303         sym = 0;
307         while (sym < 32) state->lens[sym++] =     304         while (sym < 32) state->lens[sym++] = 5;
308         distfix = next;                           305         distfix = next;
309         bits = 5;                                 306         bits = 5;
310         inflate_table(DISTS, state->lens, 32,     307         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
311                                                   308 
312         /* do this just once */                   309         /* do this just once */
313         virgin = 0;                               310         virgin = 0;
314     }                                             311     }
315 #else /* !BUILDFIXED */                           312 #else /* !BUILDFIXED */
316 #   include "inffixed.h"                          313 #   include "inffixed.h"
317 #endif /* BUILDFIXED */                           314 #endif /* BUILDFIXED */
318     state->lencode = lenfix;                      315     state->lencode = lenfix;
319     state->lenbits = 9;                           316     state->lenbits = 9;
320     state->distcode = distfix;                    317     state->distcode = distfix;
321     state->distbits = 5;                          318     state->distbits = 5;
322 }                                                 319 }
323                                                   320 
324 #ifdef MAKEFIXED                                  321 #ifdef MAKEFIXED
325 #include <stdio.h>                                322 #include <stdio.h>
326                                                   323 
327 /*                                                324 /*
328    Write out the inffixed.h that is #include'd    325    Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
329    defines BUILDFIXED, so the tables are built    326    defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
330    those tables to stdout, which would be pipe    327    those tables to stdout, which would be piped to inffixed.h.  A small program
331    can simply call makefixed to do this:          328    can simply call makefixed to do this:
332                                                   329 
333     void makefixed(void);                         330     void makefixed(void);
334                                                   331 
335     int main(void)                                332     int main(void)
336     {                                             333     {
337         makefixed();                              334         makefixed();
338         return 0;                                 335         return 0;
339     }                                             336     }
340                                                   337 
341    Then that can be linked with zlib built wit    338    Then that can be linked with zlib built with MAKEFIXED defined and run:
342                                                   339 
343     a.out > inffixed.h                            340     a.out > inffixed.h
344  */                                               341  */
345 void makefixed()                                  342 void makefixed()
346 {                                                 343 {
347     unsigned low, size;                           344     unsigned low, size;
348     struct inflate_state state;                   345     struct inflate_state state;
349                                                   346 
350     fixedtables(&state);                          347     fixedtables(&state);
351     puts("    /* inffixed.h -- table for decod    348     puts("    /* inffixed.h -- table for decoding fixed codes");
352     puts("     * Generated automatically by ma    349     puts("     * Generated automatically by makefixed().");
353     puts("     */");                              350     puts("     */");
354     puts("");                                     351     puts("");
355     puts("    /* WARNING: this file should *no    352     puts("    /* WARNING: this file should *not* be used by applications.");
356     puts("       It is part of the implementat    353     puts("       It is part of the implementation of this library and is");
357     puts("       subject to change. Applicatio    354     puts("       subject to change. Applications should only use zlib.h.");
358     puts("     */");                              355     puts("     */");
359     puts("");                                     356     puts("");
360     size = 1U << 9;                               357     size = 1U << 9;
361     printf("    static const code lenfix[%u] =    358     printf("    static const code lenfix[%u] = {", size);
362     low = 0;                                      359     low = 0;
363     for (;;) {                                    360     for (;;) {
364         if ((low % 7) == 0) printf("\n            361         if ((low % 7) == 0) printf("\n        ");
365         printf("{%u,%u,%d}", (low & 127) == 99    362         printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
366                state.lencode[low].bits, state.    363                state.lencode[low].bits, state.lencode[low].val);
367         if (++low == size) break;                 364         if (++low == size) break;
368         putchar(',');                             365         putchar(',');
369     }                                             366     }
370     puts("\n    };");                             367     puts("\n    };");
371     size = 1U << 5;                               368     size = 1U << 5;
372     printf("\n    static const code distfix[%u    369     printf("\n    static const code distfix[%u] = {", size);
373     low = 0;                                      370     low = 0;
374     for (;;) {                                    371     for (;;) {
375         if ((low % 6) == 0) printf("\n            372         if ((low % 6) == 0) printf("\n        ");
376         printf("{%u,%u,%d}", state.distcode[lo    373         printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
377                state.distcode[low].val);          374                state.distcode[low].val);
378         if (++low == size) break;                 375         if (++low == size) break;
379         putchar(',');                             376         putchar(',');
380     }                                             377     }
381     puts("\n    };");                             378     puts("\n    };");
382 }                                                 379 }
383 #endif /* MAKEFIXED */                            380 #endif /* MAKEFIXED */
384                                                   381 
385 /*                                                382 /*
386    Update the window with the last wsize (norm    383    Update the window with the last wsize (normally 32K) bytes written before
387    returning.  If window does not exist yet, c    384    returning.  If window does not exist yet, create it.  This is only called
388    when a window is already in use, or when ou    385    when a window is already in use, or when output has been written during this
389    inflate call, but the end of the deflate st    386    inflate call, but the end of the deflate stream has not been reached yet.
390    It is also called to create a window for di    387    It is also called to create a window for dictionary data when a dictionary
391    is loaded.                                     388    is loaded.
392                                                   389 
393    Providing output buffers larger than 32K to    390    Providing output buffers larger than 32K to inflate() should provide a speed
394    advantage, since only the last 32K of outpu    391    advantage, since only the last 32K of output is copied to the sliding window
395    upon return from inflate(), and since all d    392    upon return from inflate(), and since all distances after the first 32K of
396    output will fall in the output data, making    393    output will fall in the output data, making match copies simpler and faster.
397    The advantage may be dependent on the size     394    The advantage may be dependent on the size of the processor's data caches.
398  */                                               395  */
399 local int updatewindow(strm, end, copy)           396 local int updatewindow(strm, end, copy)
400 z_streamp strm;                                   397 z_streamp strm;
401 const Bytef *end;                                 398 const Bytef *end;
402 unsigned copy;                                    399 unsigned copy;
403 {                                                 400 {
404     struct inflate_state FAR *state;              401     struct inflate_state FAR *state;
405     unsigned dist;                                402     unsigned dist;
406                                                   403 
407     state = (struct inflate_state FAR *)strm->    404     state = (struct inflate_state FAR *)strm->state;
408                                                   405 
409     /* if it hasn't been done already, allocat    406     /* if it hasn't been done already, allocate space for the window */
410     if (state->window == Z_NULL) {                407     if (state->window == Z_NULL) {
411         state->window = (unsigned char FAR *)     408         state->window = (unsigned char FAR *)
412                         ZALLOC(strm, 1U << sta    409                         ZALLOC(strm, 1U << state->wbits,
413                                sizeof(unsigned    410                                sizeof(unsigned char));
414         if (state->window == Z_NULL) return 1;    411         if (state->window == Z_NULL) return 1;
415     }                                             412     }
416                                                   413 
417     /* if window not in use yet, initialize */    414     /* if window not in use yet, initialize */
418     if (state->wsize == 0) {                      415     if (state->wsize == 0) {
419         state->wsize = 1U << state->wbits;        416         state->wsize = 1U << state->wbits;
420         state->wnext = 0;                         417         state->wnext = 0;
421         state->whave = 0;                         418         state->whave = 0;
422     }                                             419     }
423                                                   420 
424     /* copy state->wsize or less output bytes     421     /* copy state->wsize or less output bytes into the circular window */
425     if (copy >= state->wsize) {                   422     if (copy >= state->wsize) {
426         zmemcpy(state->window, end - state->ws    423         zmemcpy(state->window, end - state->wsize, state->wsize);
427         state->wnext = 0;                         424         state->wnext = 0;
428         state->whave = state->wsize;              425         state->whave = state->wsize;
429     }                                             426     }
430     else {                                        427     else {
431         dist = state->wsize - state->wnext;       428         dist = state->wsize - state->wnext;
432         if (dist > copy) dist = copy;             429         if (dist > copy) dist = copy;
433         zmemcpy(state->window + state->wnext,     430         zmemcpy(state->window + state->wnext, end - copy, dist);
434         copy -= dist;                             431         copy -= dist;
435         if (copy) {                               432         if (copy) {
436             zmemcpy(state->window, end - copy,    433             zmemcpy(state->window, end - copy, copy);
437             state->wnext = copy;                  434             state->wnext = copy;
438             state->whave = state->wsize;          435             state->whave = state->wsize;
439         }                                         436         }
440         else {                                    437         else {
441             state->wnext += dist;                 438             state->wnext += dist;
442             if (state->wnext == state->wsize)     439             if (state->wnext == state->wsize) state->wnext = 0;
443             if (state->whave < state->wsize) s    440             if (state->whave < state->wsize) state->whave += dist;
444         }                                         441         }
445     }                                             442     }
446     return 0;                                     443     return 0;
447 }                                                 444 }
448                                                   445 
449 /* Macros for inflate(): */                       446 /* Macros for inflate(): */
450                                                   447 
451 /* check function to use adler32() for zlib or    448 /* check function to use adler32() for zlib or crc32() for gzip */
452 #ifdef GUNZIP                                     449 #ifdef GUNZIP
453 #  define UPDATE_CHECK(check, buf, len) \      << 450 #  define UPDATE(check, buf, len) \
454     (state->flags ? crc32(check, buf, len) : a    451     (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
455 #else                                             452 #else
456 #  define UPDATE_CHECK(check, buf, len) adler3 << 453 #  define UPDATE(check, buf, len) adler32(check, buf, len)
457 #endif                                            454 #endif
458                                                   455 
459 /* check macros for header crc */                 456 /* check macros for header crc */
460 #ifdef GUNZIP                                     457 #ifdef GUNZIP
461 #  define CRC2(check, word) \                     458 #  define CRC2(check, word) \
462     do { \                                        459     do { \
463         hbuf[0] = (unsigned char)(word); \        460         hbuf[0] = (unsigned char)(word); \
464         hbuf[1] = (unsigned char)((word) >> 8)    461         hbuf[1] = (unsigned char)((word) >> 8); \
465         check = crc32(check, hbuf, 2); \          462         check = crc32(check, hbuf, 2); \
466     } while (0)                                   463     } while (0)
467                                                   464 
468 #  define CRC4(check, word) \                     465 #  define CRC4(check, word) \
469     do { \                                        466     do { \
470         hbuf[0] = (unsigned char)(word); \        467         hbuf[0] = (unsigned char)(word); \
471         hbuf[1] = (unsigned char)((word) >> 8)    468         hbuf[1] = (unsigned char)((word) >> 8); \
472         hbuf[2] = (unsigned char)((word) >> 16    469         hbuf[2] = (unsigned char)((word) >> 16); \
473         hbuf[3] = (unsigned char)((word) >> 24    470         hbuf[3] = (unsigned char)((word) >> 24); \
474         check = crc32(check, hbuf, 4); \          471         check = crc32(check, hbuf, 4); \
475     } while (0)                                   472     } while (0)
476 #endif                                            473 #endif
477                                                   474 
478 /* Load registers with state in inflate() for     475 /* Load registers with state in inflate() for speed */
479 #define LOAD() \                                  476 #define LOAD() \
480     do { \                                        477     do { \
481         put = strm->next_out; \                   478         put = strm->next_out; \
482         left = strm->avail_out; \                 479         left = strm->avail_out; \
483         next = strm->next_in; \                   480         next = strm->next_in; \
484         have = strm->avail_in; \                  481         have = strm->avail_in; \
485         hold = state->hold; \                     482         hold = state->hold; \
486         bits = state->bits; \                     483         bits = state->bits; \
487     } while (0)                                   484     } while (0)
488                                                   485 
489 /* Restore state from registers in inflate() *    486 /* Restore state from registers in inflate() */
490 #define RESTORE() \                               487 #define RESTORE() \
491     do { \                                        488     do { \
492         strm->next_out = put; \                   489         strm->next_out = put; \
493         strm->avail_out = left; \                 490         strm->avail_out = left; \
494         strm->next_in = next; \                   491         strm->next_in = next; \
495         strm->avail_in = have; \                  492         strm->avail_in = have; \
496         state->hold = hold; \                     493         state->hold = hold; \
497         state->bits = bits; \                     494         state->bits = bits; \
498     } while (0)                                   495     } while (0)
499                                                   496 
500 /* Clear the input bit accumulator */             497 /* Clear the input bit accumulator */
501 #define INITBITS() \                              498 #define INITBITS() \
502     do { \                                        499     do { \
503         hold = 0; \                               500         hold = 0; \
504         bits = 0; \                               501         bits = 0; \
505     } while (0)                                   502     } while (0)
506                                                   503 
507 /* Get a byte of input into the bit accumulato    504 /* Get a byte of input into the bit accumulator, or return from inflate()
508    if there is no input available. */             505    if there is no input available. */
509 #define PULLBYTE() \                              506 #define PULLBYTE() \
510     do { \                                        507     do { \
511         if (have == 0) goto inf_leave; \          508         if (have == 0) goto inf_leave; \
512         have--; \                                 509         have--; \
513         hold += (unsigned long)(*next++) << bi    510         hold += (unsigned long)(*next++) << bits; \
514         bits += 8; \                              511         bits += 8; \
515     } while (0)                                   512     } while (0)
516                                                   513 
517 /* Assure that there are at least n bits in th    514 /* Assure that there are at least n bits in the bit accumulator.  If there is
518    not enough available input to do that, then    515    not enough available input to do that, then return from inflate(). */
519 #define NEEDBITS(n) \                             516 #define NEEDBITS(n) \
520     do { \                                        517     do { \
521         while (bits < (unsigned)(n)) \            518         while (bits < (unsigned)(n)) \
522             PULLBYTE(); \                         519             PULLBYTE(); \
523     } while (0)                                   520     } while (0)
524                                                   521 
525 /* Return the low n bits of the bit accumulato    522 /* Return the low n bits of the bit accumulator (n < 16) */
526 #define BITS(n) \                                 523 #define BITS(n) \
527     ((unsigned)hold & ((1U << (n)) - 1))          524     ((unsigned)hold & ((1U << (n)) - 1))
528                                                   525 
529 /* Remove n bits from the bit accumulator */      526 /* Remove n bits from the bit accumulator */
530 #define DROPBITS(n) \                             527 #define DROPBITS(n) \
531     do { \                                        528     do { \
532         hold >>= (n); \                           529         hold >>= (n); \
533         bits -= (unsigned)(n); \                  530         bits -= (unsigned)(n); \
534     } while (0)                                   531     } while (0)
535                                                   532 
536 /* Remove zero to seven bits as needed to go t    533 /* Remove zero to seven bits as needed to go to a byte boundary */
537 #define BYTEBITS() \                              534 #define BYTEBITS() \
538     do { \                                        535     do { \
539         hold >>= bits & 7; \                      536         hold >>= bits & 7; \
540         bits -= bits & 7; \                       537         bits -= bits & 7; \
541     } while (0)                                   538     } while (0)
542                                                   539 
543 /*                                                540 /*
544    inflate() uses a state machine to process a    541    inflate() uses a state machine to process as much input data and generate as
545    much output data as possible before returni    542    much output data as possible before returning.  The state machine is
546    structured roughly as follows:                 543    structured roughly as follows:
547                                                   544 
548     for (;;) switch (state) {                     545     for (;;) switch (state) {
549     ...                                           546     ...
550     case STATEn:                                  547     case STATEn:
551         if (not enough input data or output sp    548         if (not enough input data or output space to make progress)
552             return;                               549             return;
553         ... make progress ...                     550         ... make progress ...
554         state = STATEm;                           551         state = STATEm;
555         break;                                    552         break;
556     ...                                           553     ...
557     }                                             554     }
558                                                   555 
559    so when inflate() is called again, the same    556    so when inflate() is called again, the same case is attempted again, and
560    if the appropriate resources are provided,     557    if the appropriate resources are provided, the machine proceeds to the
561    next state.  The NEEDBITS() macro is usuall    558    next state.  The NEEDBITS() macro is usually the way the state evaluates
562    whether it can proceed or should return.  N    559    whether it can proceed or should return.  NEEDBITS() does the return if
563    the requested bits are not available.  The     560    the requested bits are not available.  The typical use of the BITS macros
564    is:                                            561    is:
565                                                   562 
566         NEEDBITS(n);                              563         NEEDBITS(n);
567         ... do something with BITS(n) ...         564         ... do something with BITS(n) ...
568         DROPBITS(n);                              565         DROPBITS(n);
569                                                   566 
570    where NEEDBITS(n) either returns from infla    567    where NEEDBITS(n) either returns from inflate() if there isn't enough
571    input left to load n bits into the accumula    568    input left to load n bits into the accumulator, or it continues.  BITS(n)
572    gives the low n bits in the accumulator.  W    569    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
573    the low n bits off the accumulator.  INITBI    570    the low n bits off the accumulator.  INITBITS() clears the accumulator
574    and sets the number of available bits to ze    571    and sets the number of available bits to zero.  BYTEBITS() discards just
575    enough bits to put the accumulator on a byt    572    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
576    and a NEEDBITS(8), then BITS(8) would retur    573    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
577                                                   574 
578    NEEDBITS(n) uses PULLBYTE() to get an avail    575    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
579    if there is no input available.  The decodi    576    if there is no input available.  The decoding of variable length codes uses
580    PULLBYTE() directly in order to pull just e    577    PULLBYTE() directly in order to pull just enough bytes to decode the next
581    code, and no more.                             578    code, and no more.
582                                                   579 
583    Some states loop until they get enough inpu    580    Some states loop until they get enough input, making sure that enough
584    state information is maintained to continue    581    state information is maintained to continue the loop where it left off
585    if NEEDBITS() returns in the loop.  For exa    582    if NEEDBITS() returns in the loop.  For example, want, need, and keep
586    would all have to actually be part of the s    583    would all have to actually be part of the saved state in case NEEDBITS()
587    returns:                                       584    returns:
588                                                   585 
589     case STATEw:                                  586     case STATEw:
590         while (want < need) {                     587         while (want < need) {
591             NEEDBITS(n);                          588             NEEDBITS(n);
592             keep[want++] = BITS(n);               589             keep[want++] = BITS(n);
593             DROPBITS(n);                          590             DROPBITS(n);
594         }                                         591         }
595         state = STATEx;                           592         state = STATEx;
596     case STATEx:                                  593     case STATEx:
597                                                   594 
598    As shown above, if the next state is also t    595    As shown above, if the next state is also the next case, then the break
599    is omitted.                                    596    is omitted.
600                                                   597 
601    A state may also return if there is not eno    598    A state may also return if there is not enough output space available to
602    complete that state.  Those states are copy    599    complete that state.  Those states are copying stored data, writing a
603    literal byte, and copying a matching string    600    literal byte, and copying a matching string.
604                                                   601 
605    When returning, a "goto inf_leave" is used     602    When returning, a "goto inf_leave" is used to update the total counters,
606    update the check value, and determine wheth    603    update the check value, and determine whether any progress has been made
607    during that inflate() call in order to retu    604    during that inflate() call in order to return the proper return code.
608    Progress is defined as a change in either s    605    Progress is defined as a change in either strm->avail_in or strm->avail_out.
609    When there is a window, goto inf_leave will    606    When there is a window, goto inf_leave will update the window with the last
610    output written.  If a goto inf_leave occurs    607    output written.  If a goto inf_leave occurs in the middle of decompression
611    and there is no window currently, goto inf_    608    and there is no window currently, goto inf_leave will create one and copy
612    output to the window for the next call of i    609    output to the window for the next call of inflate().
613                                                   610 
614    In this implementation, the flush parameter    611    In this implementation, the flush parameter of inflate() only affects the
615    return code (per zlib.h).  inflate() always    612    return code (per zlib.h).  inflate() always writes as much as possible to
616    strm->next_out, given the space available a    613    strm->next_out, given the space available and the provided input--the effect
617    documented in zlib.h of Z_SYNC_FLUSH.  Furt    614    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
618    the allocation of and copying into a slidin    615    the allocation of and copying into a sliding window until necessary, which
619    provides the effect documented in zlib.h fo    616    provides the effect documented in zlib.h for Z_FINISH when the entire input
620    stream available.  So the only thing the fl    617    stream available.  So the only thing the flush parameter actually does is:
621    when flush is set to Z_FINISH, inflate() ca    618    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
622    will return Z_BUF_ERROR if it has not reach    619    will return Z_BUF_ERROR if it has not reached the end of the stream.
623  */                                               620  */
624                                                   621 
625 int ZEXPORT inflate(strm, flush)                  622 int ZEXPORT inflate(strm, flush)
626 z_streamp strm;                                   623 z_streamp strm;
627 int flush;                                        624 int flush;
628 {                                                 625 {
629     struct inflate_state FAR *state;              626     struct inflate_state FAR *state;
630     z_const unsigned char FAR *next;    /* nex    627     z_const unsigned char FAR *next;    /* next input */
631     unsigned char FAR *put;     /* next output    628     unsigned char FAR *put;     /* next output */
632     unsigned have, left;        /* available i    629     unsigned have, left;        /* available input and output */
633     unsigned long hold;         /* bit buffer     630     unsigned long hold;         /* bit buffer */
634     unsigned bits;              /* bits in bit    631     unsigned bits;              /* bits in bit buffer */
635     unsigned in, out;           /* save starti    632     unsigned in, out;           /* save starting available input and output */
636     unsigned copy;              /* number of s    633     unsigned copy;              /* number of stored or match bytes to copy */
637     unsigned char FAR *from;    /* where to co    634     unsigned char FAR *from;    /* where to copy match bytes from */
638     code here;                  /* current dec    635     code here;                  /* current decoding table entry */
639     code last;                  /* parent tabl    636     code last;                  /* parent table entry */
640     unsigned len;               /* length to c    637     unsigned len;               /* length to copy for repeats, bits to drop */
641     int ret;                    /* return code    638     int ret;                    /* return code */
642 #ifdef GUNZIP                                     639 #ifdef GUNZIP
643     unsigned char hbuf[4];      /* buffer for     640     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
644 #endif                                            641 #endif
645     static const unsigned short order[19] = /*    642     static const unsigned short order[19] = /* permutation of code lengths */
646         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11,    643         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
647                                                   644 
648     if (inflateStateCheck(strm) || strm->next_    645     if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
649         (strm->next_in == Z_NULL && strm->avai    646         (strm->next_in == Z_NULL && strm->avail_in != 0))
650         return Z_STREAM_ERROR;                    647         return Z_STREAM_ERROR;
651                                                   648 
652     state = (struct inflate_state FAR *)strm->    649     state = (struct inflate_state FAR *)strm->state;
653     if (state->mode == TYPE) state->mode = TYP    650     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
654     LOAD();                                       651     LOAD();
655     in = have;                                    652     in = have;
656     out = left;                                   653     out = left;
657     ret = Z_OK;                                   654     ret = Z_OK;
658     for (;;)                                      655     for (;;)
659         switch (state->mode) {                    656         switch (state->mode) {
660         case HEAD:                                657         case HEAD:
661             if (state->wrap == 0) {               658             if (state->wrap == 0) {
662                 state->mode = TYPEDO;             659                 state->mode = TYPEDO;
663                 break;                            660                 break;
664             }                                     661             }
665             NEEDBITS(16);                         662             NEEDBITS(16);
666 #ifdef GUNZIP                                     663 #ifdef GUNZIP
667             if ((state->wrap & 2) && hold == 0    664             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
668                 if (state->wbits == 0)            665                 if (state->wbits == 0)
669                     state->wbits = 15;            666                     state->wbits = 15;
670                 state->check = crc32(0L, Z_NUL    667                 state->check = crc32(0L, Z_NULL, 0);
671                 CRC2(state->check, hold);         668                 CRC2(state->check, hold);
672                 INITBITS();                       669                 INITBITS();
673                 state->mode = FLAGS;              670                 state->mode = FLAGS;
674                 break;                            671                 break;
675             }                                     672             }
                                                   >> 673             state->flags = 0;           /* expect zlib header */
676             if (state->head != Z_NULL)            674             if (state->head != Z_NULL)
677                 state->head->done = -1;           675                 state->head->done = -1;
678             if (!(state->wrap & 1) ||   /* che    676             if (!(state->wrap & 1) ||   /* check if zlib header allowed */
679 #else                                             677 #else
680             if (                                  678             if (
681 #endif                                            679 #endif
682                 ((BITS(8) << 8) + (hold >> 8))    680                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
683                 strm->msg = (char *)"incorrect    681                 strm->msg = (char *)"incorrect header check";
684                 state->mode = BAD;                682                 state->mode = BAD;
685                 break;                            683                 break;
686             }                                     684             }
687             if (BITS(4) != Z_DEFLATED) {          685             if (BITS(4) != Z_DEFLATED) {
688                 strm->msg = (char *)"unknown c    686                 strm->msg = (char *)"unknown compression method";
689                 state->mode = BAD;                687                 state->mode = BAD;
690                 break;                            688                 break;
691             }                                     689             }
692             DROPBITS(4);                          690             DROPBITS(4);
693             len = BITS(4) + 8;                    691             len = BITS(4) + 8;
694             if (state->wbits == 0)                692             if (state->wbits == 0)
695                 state->wbits = len;               693                 state->wbits = len;
696             if (len > 15 || len > state->wbits    694             if (len > 15 || len > state->wbits) {
697                 strm->msg = (char *)"invalid w    695                 strm->msg = (char *)"invalid window size";
698                 state->mode = BAD;                696                 state->mode = BAD;
699                 break;                            697                 break;
700             }                                     698             }
701             state->dmax = 1U << len;              699             state->dmax = 1U << len;
702             state->flags = 0;               /* << 
703             Tracev((stderr, "inflate:   zlib h    700             Tracev((stderr, "inflate:   zlib header ok\n"));
704             strm->adler = state->check = adler    701             strm->adler = state->check = adler32(0L, Z_NULL, 0);
705             state->mode = hold & 0x200 ? DICTI    702             state->mode = hold & 0x200 ? DICTID : TYPE;
706             INITBITS();                           703             INITBITS();
707             break;                                704             break;
708 #ifdef GUNZIP                                     705 #ifdef GUNZIP
709         case FLAGS:                               706         case FLAGS:
710             NEEDBITS(16);                         707             NEEDBITS(16);
711             state->flags = (int)(hold);           708             state->flags = (int)(hold);
712             if ((state->flags & 0xff) != Z_DEF    709             if ((state->flags & 0xff) != Z_DEFLATED) {
713                 strm->msg = (char *)"unknown c    710                 strm->msg = (char *)"unknown compression method";
714                 state->mode = BAD;                711                 state->mode = BAD;
715                 break;                            712                 break;
716             }                                     713             }
717             if (state->flags & 0xe000) {          714             if (state->flags & 0xe000) {
718                 strm->msg = (char *)"unknown h    715                 strm->msg = (char *)"unknown header flags set";
719                 state->mode = BAD;                716                 state->mode = BAD;
720                 break;                            717                 break;
721             }                                     718             }
722             if (state->head != Z_NULL)            719             if (state->head != Z_NULL)
723                 state->head->text = (int)((hol    720                 state->head->text = (int)((hold >> 8) & 1);
724             if ((state->flags & 0x0200) && (st    721             if ((state->flags & 0x0200) && (state->wrap & 4))
725                 CRC2(state->check, hold);         722                 CRC2(state->check, hold);
726             INITBITS();                           723             INITBITS();
727             state->mode = TIME;                   724             state->mode = TIME;
728                 /* fallthrough */              << 
729         case TIME:                                725         case TIME:
730             NEEDBITS(32);                         726             NEEDBITS(32);
731             if (state->head != Z_NULL)            727             if (state->head != Z_NULL)
732                 state->head->time = hold;         728                 state->head->time = hold;
733             if ((state->flags & 0x0200) && (st    729             if ((state->flags & 0x0200) && (state->wrap & 4))
734                 CRC4(state->check, hold);         730                 CRC4(state->check, hold);
735             INITBITS();                           731             INITBITS();
736             state->mode = OS;                     732             state->mode = OS;
737                 /* fallthrough */              << 
738         case OS:                                  733         case OS:
739             NEEDBITS(16);                         734             NEEDBITS(16);
740             if (state->head != Z_NULL) {          735             if (state->head != Z_NULL) {
741                 state->head->xflags = (int)(ho    736                 state->head->xflags = (int)(hold & 0xff);
742                 state->head->os = (int)(hold >    737                 state->head->os = (int)(hold >> 8);
743             }                                     738             }
744             if ((state->flags & 0x0200) && (st    739             if ((state->flags & 0x0200) && (state->wrap & 4))
745                 CRC2(state->check, hold);         740                 CRC2(state->check, hold);
746             INITBITS();                           741             INITBITS();
747             state->mode = EXLEN;                  742             state->mode = EXLEN;
748                 /* fallthrough */              << 
749         case EXLEN:                               743         case EXLEN:
750             if (state->flags & 0x0400) {          744             if (state->flags & 0x0400) {
751                 NEEDBITS(16);                     745                 NEEDBITS(16);
752                 state->length = (unsigned)(hol    746                 state->length = (unsigned)(hold);
753                 if (state->head != Z_NULL)        747                 if (state->head != Z_NULL)
754                     state->head->extra_len = (    748                     state->head->extra_len = (unsigned)hold;
755                 if ((state->flags & 0x0200) &&    749                 if ((state->flags & 0x0200) && (state->wrap & 4))
756                     CRC2(state->check, hold);     750                     CRC2(state->check, hold);
757                 INITBITS();                       751                 INITBITS();
758             }                                     752             }
759             else if (state->head != Z_NULL)       753             else if (state->head != Z_NULL)
760                 state->head->extra = Z_NULL;      754                 state->head->extra = Z_NULL;
761             state->mode = EXTRA;                  755             state->mode = EXTRA;
762                 /* fallthrough */              << 
763         case EXTRA:                               756         case EXTRA:
764             if (state->flags & 0x0400) {          757             if (state->flags & 0x0400) {
765                 copy = state->length;             758                 copy = state->length;
766                 if (copy > have) copy = have;     759                 if (copy > have) copy = have;
767                 if (copy) {                       760                 if (copy) {
768                     if (state->head != Z_NULL     761                     if (state->head != Z_NULL &&
769                         state->head->extra !=  << 762                         state->head->extra != Z_NULL) {
770                         (len = state->head->ex << 763                         len = state->head->extra_len - state->length;
771                             state->head->extra << 
772                         zmemcpy(state->head->e    764                         zmemcpy(state->head->extra + len, next,
773                                 len + copy > s    765                                 len + copy > state->head->extra_max ?
774                                 state->head->e    766                                 state->head->extra_max - len : copy);
775                     }                             767                     }
776                     if ((state->flags & 0x0200    768                     if ((state->flags & 0x0200) && (state->wrap & 4))
777                         state->check = crc32(s    769                         state->check = crc32(state->check, next, copy);
778                     have -= copy;                 770                     have -= copy;
779                     next += copy;                 771                     next += copy;
780                     state->length -= copy;        772                     state->length -= copy;
781                 }                                 773                 }
782                 if (state->length) goto inf_le    774                 if (state->length) goto inf_leave;
783             }                                     775             }
784             state->length = 0;                    776             state->length = 0;
785             state->mode = NAME;                   777             state->mode = NAME;
786                 /* fallthrough */              << 
787         case NAME:                                778         case NAME:
788             if (state->flags & 0x0800) {          779             if (state->flags & 0x0800) {
789                 if (have == 0) goto inf_leave;    780                 if (have == 0) goto inf_leave;
790                 copy = 0;                         781                 copy = 0;
791                 do {                              782                 do {
792                     len = (unsigned)(next[copy    783                     len = (unsigned)(next[copy++]);
793                     if (state->head != Z_NULL     784                     if (state->head != Z_NULL &&
794                             state->head->name     785                             state->head->name != Z_NULL &&
795                             state->length < st    786                             state->length < state->head->name_max)
796                         state->head->name[stat    787                         state->head->name[state->length++] = (Bytef)len;
797                 } while (len && copy < have);     788                 } while (len && copy < have);
798                 if ((state->flags & 0x0200) &&    789                 if ((state->flags & 0x0200) && (state->wrap & 4))
799                     state->check = crc32(state    790                     state->check = crc32(state->check, next, copy);
800                 have -= copy;                     791                 have -= copy;
801                 next += copy;                     792                 next += copy;
802                 if (len) goto inf_leave;          793                 if (len) goto inf_leave;
803             }                                     794             }
804             else if (state->head != Z_NULL)       795             else if (state->head != Z_NULL)
805                 state->head->name = Z_NULL;       796                 state->head->name = Z_NULL;
806             state->length = 0;                    797             state->length = 0;
807             state->mode = COMMENT;                798             state->mode = COMMENT;
808                 /* fallthrough */              << 
809         case COMMENT:                             799         case COMMENT:
810             if (state->flags & 0x1000) {          800             if (state->flags & 0x1000) {
811                 if (have == 0) goto inf_leave;    801                 if (have == 0) goto inf_leave;
812                 copy = 0;                         802                 copy = 0;
813                 do {                              803                 do {
814                     len = (unsigned)(next[copy    804                     len = (unsigned)(next[copy++]);
815                     if (state->head != Z_NULL     805                     if (state->head != Z_NULL &&
816                             state->head->comme    806                             state->head->comment != Z_NULL &&
817                             state->length < st    807                             state->length < state->head->comm_max)
818                         state->head->comment[s    808                         state->head->comment[state->length++] = (Bytef)len;
819                 } while (len && copy < have);     809                 } while (len && copy < have);
820                 if ((state->flags & 0x0200) &&    810                 if ((state->flags & 0x0200) && (state->wrap & 4))
821                     state->check = crc32(state    811                     state->check = crc32(state->check, next, copy);
822                 have -= copy;                     812                 have -= copy;
823                 next += copy;                     813                 next += copy;
824                 if (len) goto inf_leave;          814                 if (len) goto inf_leave;
825             }                                     815             }
826             else if (state->head != Z_NULL)       816             else if (state->head != Z_NULL)
827                 state->head->comment = Z_NULL;    817                 state->head->comment = Z_NULL;
828             state->mode = HCRC;                   818             state->mode = HCRC;
829                 /* fallthrough */              << 
830         case HCRC:                                819         case HCRC:
831             if (state->flags & 0x0200) {          820             if (state->flags & 0x0200) {
832                 NEEDBITS(16);                     821                 NEEDBITS(16);
833                 if ((state->wrap & 4) && hold     822                 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
834                     strm->msg = (char *)"heade    823                     strm->msg = (char *)"header crc mismatch";
835                     state->mode = BAD;            824                     state->mode = BAD;
836                     break;                        825                     break;
837                 }                                 826                 }
838                 INITBITS();                       827                 INITBITS();
839             }                                     828             }
840             if (state->head != Z_NULL) {          829             if (state->head != Z_NULL) {
841                 state->head->hcrc = (int)((sta    830                 state->head->hcrc = (int)((state->flags >> 9) & 1);
842                 state->head->done = 1;            831                 state->head->done = 1;
843             }                                     832             }
844             strm->adler = state->check = crc32    833             strm->adler = state->check = crc32(0L, Z_NULL, 0);
845             state->mode = TYPE;                   834             state->mode = TYPE;
846             break;                                835             break;
847 #endif                                            836 #endif
848         case DICTID:                              837         case DICTID:
849             NEEDBITS(32);                         838             NEEDBITS(32);
850             strm->adler = state->check = ZSWAP    839             strm->adler = state->check = ZSWAP32(hold);
851             INITBITS();                           840             INITBITS();
852             state->mode = DICT;                   841             state->mode = DICT;
853                 /* fallthrough */              << 
854         case DICT:                                842         case DICT:
855             if (state->havedict == 0) {           843             if (state->havedict == 0) {
856                 RESTORE();                        844                 RESTORE();
857                 return Z_NEED_DICT;               845                 return Z_NEED_DICT;
858             }                                     846             }
859             strm->adler = state->check = adler    847             strm->adler = state->check = adler32(0L, Z_NULL, 0);
860             state->mode = TYPE;                   848             state->mode = TYPE;
861                 /* fallthrough */              << 
862         case TYPE:                                849         case TYPE:
863             if (flush == Z_BLOCK || flush == Z    850             if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
864                 /* fallthrough */              << 
865         case TYPEDO:                              851         case TYPEDO:
866             if (state->last) {                    852             if (state->last) {
867                 BYTEBITS();                       853                 BYTEBITS();
868                 state->mode = CHECK;              854                 state->mode = CHECK;
869                 break;                            855                 break;
870             }                                     856             }
871             NEEDBITS(3);                          857             NEEDBITS(3);
872             state->last = BITS(1);                858             state->last = BITS(1);
873             DROPBITS(1);                          859             DROPBITS(1);
874             switch (BITS(2)) {                    860             switch (BITS(2)) {
875             case 0:                               861             case 0:                             /* stored block */
876                 Tracev((stderr, "inflate:         862                 Tracev((stderr, "inflate:     stored block%s\n",
877                         state->last ? " (last)    863                         state->last ? " (last)" : ""));
878                 state->mode = STORED;             864                 state->mode = STORED;
879                 break;                            865                 break;
880             case 1:                               866             case 1:                             /* fixed block */
881                 fixedtables(state);               867                 fixedtables(state);
882                 Tracev((stderr, "inflate:         868                 Tracev((stderr, "inflate:     fixed codes block%s\n",
883                         state->last ? " (last)    869                         state->last ? " (last)" : ""));
884                 state->mode = LEN_;               870                 state->mode = LEN_;             /* decode codes */
885                 if (flush == Z_TREES) {           871                 if (flush == Z_TREES) {
886                     DROPBITS(2);                  872                     DROPBITS(2);
887                     goto inf_leave;               873                     goto inf_leave;
888                 }                                 874                 }
889                 break;                            875                 break;
890             case 2:                               876             case 2:                             /* dynamic block */
891                 Tracev((stderr, "inflate:         877                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
892                         state->last ? " (last)    878                         state->last ? " (last)" : ""));
893                 state->mode = TABLE;              879                 state->mode = TABLE;
894                 break;                            880                 break;
895             case 3:                               881             case 3:
896                 strm->msg = (char *)"invalid b    882                 strm->msg = (char *)"invalid block type";
897                 state->mode = BAD;                883                 state->mode = BAD;
898             }                                     884             }
899             DROPBITS(2);                          885             DROPBITS(2);
900             break;                                886             break;
901         case STORED:                              887         case STORED:
902             BYTEBITS();                           888             BYTEBITS();                         /* go to byte boundary */
903             NEEDBITS(32);                         889             NEEDBITS(32);
904             if ((hold & 0xffff) != ((hold >> 1    890             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
905                 strm->msg = (char *)"invalid s    891                 strm->msg = (char *)"invalid stored block lengths";
906                 state->mode = BAD;                892                 state->mode = BAD;
907                 break;                            893                 break;
908             }                                     894             }
909             state->length = (unsigned)hold & 0    895             state->length = (unsigned)hold & 0xffff;
910             Tracev((stderr, "inflate:       st    896             Tracev((stderr, "inflate:       stored length %u\n",
911                     state->length));              897                     state->length));
912             INITBITS();                           898             INITBITS();
913             state->mode = COPY_;                  899             state->mode = COPY_;
914             if (flush == Z_TREES) goto inf_lea    900             if (flush == Z_TREES) goto inf_leave;
915                 /* fallthrough */              << 
916         case COPY_:                               901         case COPY_:
917             state->mode = COPY;                   902             state->mode = COPY;
918                 /* fallthrough */              << 
919         case COPY:                                903         case COPY:
920             copy = state->length;                 904             copy = state->length;
921             if (copy) {                           905             if (copy) {
922                 if (copy > have) copy = have;     906                 if (copy > have) copy = have;
923                 if (copy > left) copy = left;     907                 if (copy > left) copy = left;
924                 if (copy == 0) goto inf_leave;    908                 if (copy == 0) goto inf_leave;
925                 zmemcpy(put, next, copy);         909                 zmemcpy(put, next, copy);
926                 have -= copy;                     910                 have -= copy;
927                 next += copy;                     911                 next += copy;
928                 left -= copy;                     912                 left -= copy;
929                 put += copy;                      913                 put += copy;
930                 state->length -= copy;            914                 state->length -= copy;
931                 break;                            915                 break;
932             }                                     916             }
933             Tracev((stderr, "inflate:       st    917             Tracev((stderr, "inflate:       stored end\n"));
934             state->mode = TYPE;                   918             state->mode = TYPE;
935             break;                                919             break;
936         case TABLE:                               920         case TABLE:
937             NEEDBITS(14);                         921             NEEDBITS(14);
938             state->nlen = BITS(5) + 257;          922             state->nlen = BITS(5) + 257;
939             DROPBITS(5);                          923             DROPBITS(5);
940             state->ndist = BITS(5) + 1;           924             state->ndist = BITS(5) + 1;
941             DROPBITS(5);                          925             DROPBITS(5);
942             state->ncode = BITS(4) + 4;           926             state->ncode = BITS(4) + 4;
943             DROPBITS(4);                          927             DROPBITS(4);
944 #ifndef PKZIP_BUG_WORKAROUND                      928 #ifndef PKZIP_BUG_WORKAROUND
945             if (state->nlen > 286 || state->nd    929             if (state->nlen > 286 || state->ndist > 30) {
946                 strm->msg = (char *)"too many     930                 strm->msg = (char *)"too many length or distance symbols";
947                 state->mode = BAD;                931                 state->mode = BAD;
948                 break;                            932                 break;
949             }                                     933             }
950 #endif                                            934 #endif
951             Tracev((stderr, "inflate:       ta    935             Tracev((stderr, "inflate:       table sizes ok\n"));
952             state->have = 0;                      936             state->have = 0;
953             state->mode = LENLENS;                937             state->mode = LENLENS;
954                 /* fallthrough */              << 
955         case LENLENS:                             938         case LENLENS:
956             while (state->have < state->ncode)    939             while (state->have < state->ncode) {
957                 NEEDBITS(3);                      940                 NEEDBITS(3);
958                 state->lens[order[state->have+    941                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
959                 DROPBITS(3);                      942                 DROPBITS(3);
960             }                                     943             }
961             while (state->have < 19)              944             while (state->have < 19)
962                 state->lens[order[state->have+    945                 state->lens[order[state->have++]] = 0;
963             state->next = state->codes;           946             state->next = state->codes;
964             state->lencode = (const code FAR *    947             state->lencode = (const code FAR *)(state->next);
965             state->lenbits = 7;                   948             state->lenbits = 7;
966             ret = inflate_table(CODES, state->    949             ret = inflate_table(CODES, state->lens, 19, &(state->next),
967                                 &(state->lenbi    950                                 &(state->lenbits), state->work);
968             if (ret) {                            951             if (ret) {
969                 strm->msg = (char *)"invalid c    952                 strm->msg = (char *)"invalid code lengths set";
970                 state->mode = BAD;                953                 state->mode = BAD;
971                 break;                            954                 break;
972             }                                     955             }
973             Tracev((stderr, "inflate:       co    956             Tracev((stderr, "inflate:       code lengths ok\n"));
974             state->have = 0;                      957             state->have = 0;
975             state->mode = CODELENS;               958             state->mode = CODELENS;
976                 /* fallthrough */              << 
977         case CODELENS:                            959         case CODELENS:
978             while (state->have < state->nlen +    960             while (state->have < state->nlen + state->ndist) {
979                 for (;;) {                        961                 for (;;) {
980                     here = state->lencode[BITS    962                     here = state->lencode[BITS(state->lenbits)];
981                     if ((unsigned)(here.bits)     963                     if ((unsigned)(here.bits) <= bits) break;
982                     PULLBYTE();                   964                     PULLBYTE();
983                 }                                 965                 }
984                 if (here.val < 16) {              966                 if (here.val < 16) {
985                     DROPBITS(here.bits);          967                     DROPBITS(here.bits);
986                     state->lens[state->have++]    968                     state->lens[state->have++] = here.val;
987                 }                                 969                 }
988                 else {                            970                 else {
989                     if (here.val == 16) {         971                     if (here.val == 16) {
990                         NEEDBITS(here.bits + 2    972                         NEEDBITS(here.bits + 2);
991                         DROPBITS(here.bits);      973                         DROPBITS(here.bits);
992                         if (state->have == 0)     974                         if (state->have == 0) {
993                             strm->msg = (char     975                             strm->msg = (char *)"invalid bit length repeat";
994                             state->mode = BAD;    976                             state->mode = BAD;
995                             break;                977                             break;
996                         }                         978                         }
997                         len = state->lens[stat    979                         len = state->lens[state->have - 1];
998                         copy = 3 + BITS(2);       980                         copy = 3 + BITS(2);
999                         DROPBITS(2);              981                         DROPBITS(2);
1000                     }                            982                     }
1001                     else if (here.val == 17)     983                     else if (here.val == 17) {
1002                         NEEDBITS(here.bits +     984                         NEEDBITS(here.bits + 3);
1003                         DROPBITS(here.bits);     985                         DROPBITS(here.bits);
1004                         len = 0;                 986                         len = 0;
1005                         copy = 3 + BITS(3);      987                         copy = 3 + BITS(3);
1006                         DROPBITS(3);             988                         DROPBITS(3);
1007                     }                            989                     }
1008                     else {                       990                     else {
1009                         NEEDBITS(here.bits +     991                         NEEDBITS(here.bits + 7);
1010                         DROPBITS(here.bits);     992                         DROPBITS(here.bits);
1011                         len = 0;                 993                         len = 0;
1012                         copy = 11 + BITS(7);     994                         copy = 11 + BITS(7);
1013                         DROPBITS(7);             995                         DROPBITS(7);
1014                     }                            996                     }
1015                     if (state->have + copy >     997                     if (state->have + copy > state->nlen + state->ndist) {
1016                         strm->msg = (char *)"    998                         strm->msg = (char *)"invalid bit length repeat";
1017                         state->mode = BAD;       999                         state->mode = BAD;
1018                         break;                   1000                         break;
1019                     }                            1001                     }
1020                     while (copy--)               1002                     while (copy--)
1021                         state->lens[state->ha    1003                         state->lens[state->have++] = (unsigned short)len;
1022                 }                                1004                 }
1023             }                                    1005             }
1024                                                  1006 
1025             /* handle error breaks in while *    1007             /* handle error breaks in while */
1026             if (state->mode == BAD) break;       1008             if (state->mode == BAD) break;
1027                                                  1009 
1028             /* check for end-of-block code (b    1010             /* check for end-of-block code (better have one) */
1029             if (state->lens[256] == 0) {         1011             if (state->lens[256] == 0) {
1030                 strm->msg = (char *)"invalid     1012                 strm->msg = (char *)"invalid code -- missing end-of-block";
1031                 state->mode = BAD;               1013                 state->mode = BAD;
1032                 break;                           1014                 break;
1033             }                                    1015             }
1034                                                  1016 
1035             /* build code tables -- note: do     1017             /* build code tables -- note: do not change the lenbits or distbits
1036                values here (9 and 6) without     1018                values here (9 and 6) without reading the comments in inftrees.h
1037                concerning the ENOUGH constant    1019                concerning the ENOUGH constants, which depend on those values */
1038             state->next = state->codes;          1020             state->next = state->codes;
1039             state->lencode = (const code FAR     1021             state->lencode = (const code FAR *)(state->next);
1040             state->lenbits = 9;                  1022             state->lenbits = 9;
1041             ret = inflate_table(LENS, state->    1023             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1042                                 &(state->lenb    1024                                 &(state->lenbits), state->work);
1043             if (ret) {                           1025             if (ret) {
1044                 strm->msg = (char *)"invalid     1026                 strm->msg = (char *)"invalid literal/lengths set";
1045                 state->mode = BAD;               1027                 state->mode = BAD;
1046                 break;                           1028                 break;
1047             }                                    1029             }
1048             state->distcode = (const code FAR    1030             state->distcode = (const code FAR *)(state->next);
1049             state->distbits = 6;                 1031             state->distbits = 6;
1050             ret = inflate_table(DISTS, state-    1032             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1051                             &(state->next), &    1033                             &(state->next), &(state->distbits), state->work);
1052             if (ret) {                           1034             if (ret) {
1053                 strm->msg = (char *)"invalid     1035                 strm->msg = (char *)"invalid distances set";
1054                 state->mode = BAD;               1036                 state->mode = BAD;
1055                 break;                           1037                 break;
1056             }                                    1038             }
1057             Tracev((stderr, "inflate:       c    1039             Tracev((stderr, "inflate:       codes ok\n"));
1058             state->mode = LEN_;                  1040             state->mode = LEN_;
1059             if (flush == Z_TREES) goto inf_le    1041             if (flush == Z_TREES) goto inf_leave;
1060                 /* fallthrough */             << 
1061         case LEN_:                               1042         case LEN_:
1062             state->mode = LEN;                   1043             state->mode = LEN;
1063                 /* fallthrough */             << 
1064         case LEN:                                1044         case LEN:
1065             if (have >= 6 && left >= 258) {      1045             if (have >= 6 && left >= 258) {
1066                 RESTORE();                       1046                 RESTORE();
1067                 inflate_fast(strm, out);         1047                 inflate_fast(strm, out);
1068                 LOAD();                          1048                 LOAD();
1069                 if (state->mode == TYPE)         1049                 if (state->mode == TYPE)
1070                     state->back = -1;            1050                     state->back = -1;
1071                 break;                           1051                 break;
1072             }                                    1052             }
1073             state->back = 0;                     1053             state->back = 0;
1074             for (;;) {                           1054             for (;;) {
1075                 here = state->lencode[BITS(st    1055                 here = state->lencode[BITS(state->lenbits)];
1076                 if ((unsigned)(here.bits) <=     1056                 if ((unsigned)(here.bits) <= bits) break;
1077                 PULLBYTE();                      1057                 PULLBYTE();
1078             }                                    1058             }
1079             if (here.op && (here.op & 0xf0) =    1059             if (here.op && (here.op & 0xf0) == 0) {
1080                 last = here;                     1060                 last = here;
1081                 for (;;) {                       1061                 for (;;) {
1082                     here = state->lencode[las    1062                     here = state->lencode[last.val +
1083                             (BITS(last.bits +    1063                             (BITS(last.bits + last.op) >> last.bits)];
1084                     if ((unsigned)(last.bits     1064                     if ((unsigned)(last.bits + here.bits) <= bits) break;
1085                     PULLBYTE();                  1065                     PULLBYTE();
1086                 }                                1066                 }
1087                 DROPBITS(last.bits);             1067                 DROPBITS(last.bits);
1088                 state->back += last.bits;        1068                 state->back += last.bits;
1089             }                                    1069             }
1090             DROPBITS(here.bits);                 1070             DROPBITS(here.bits);
1091             state->back += here.bits;            1071             state->back += here.bits;
1092             state->length = (unsigned)here.va    1072             state->length = (unsigned)here.val;
1093             if ((int)(here.op) == 0) {           1073             if ((int)(here.op) == 0) {
1094                 Tracevv((stderr, here.val >=     1074                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1095                         "inflate:         lit    1075                         "inflate:         literal '%c'\n" :
1096                         "inflate:         lit    1076                         "inflate:         literal 0x%02x\n", here.val));
1097                 state->mode = LIT;               1077                 state->mode = LIT;
1098                 break;                           1078                 break;
1099             }                                    1079             }
1100             if (here.op & 32) {                  1080             if (here.op & 32) {
1101                 Tracevv((stderr, "inflate:       1081                 Tracevv((stderr, "inflate:         end of block\n"));
1102                 state->back = -1;                1082                 state->back = -1;
1103                 state->mode = TYPE;              1083                 state->mode = TYPE;
1104                 break;                           1084                 break;
1105             }                                    1085             }
1106             if (here.op & 64) {                  1086             if (here.op & 64) {
1107                 strm->msg = (char *)"invalid     1087                 strm->msg = (char *)"invalid literal/length code";
1108                 state->mode = BAD;               1088                 state->mode = BAD;
1109                 break;                           1089                 break;
1110             }                                    1090             }
1111             state->extra = (unsigned)(here.op    1091             state->extra = (unsigned)(here.op) & 15;
1112             state->mode = LENEXT;                1092             state->mode = LENEXT;
1113                 /* fallthrough */             << 
1114         case LENEXT:                             1093         case LENEXT:
1115             if (state->extra) {                  1094             if (state->extra) {
1116                 NEEDBITS(state->extra);          1095                 NEEDBITS(state->extra);
1117                 state->length += BITS(state->    1096                 state->length += BITS(state->extra);
1118                 DROPBITS(state->extra);          1097                 DROPBITS(state->extra);
1119                 state->back += state->extra;     1098                 state->back += state->extra;
1120             }                                    1099             }
1121             Tracevv((stderr, "inflate:           1100             Tracevv((stderr, "inflate:         length %u\n", state->length));
1122             state->was = state->length;          1101             state->was = state->length;
1123             state->mode = DIST;                  1102             state->mode = DIST;
1124                 /* fallthrough */             << 
1125         case DIST:                               1103         case DIST:
1126             for (;;) {                           1104             for (;;) {
1127                 here = state->distcode[BITS(s    1105                 here = state->distcode[BITS(state->distbits)];
1128                 if ((unsigned)(here.bits) <=     1106                 if ((unsigned)(here.bits) <= bits) break;
1129                 PULLBYTE();                      1107                 PULLBYTE();
1130             }                                    1108             }
1131             if ((here.op & 0xf0) == 0) {         1109             if ((here.op & 0xf0) == 0) {
1132                 last = here;                     1110                 last = here;
1133                 for (;;) {                       1111                 for (;;) {
1134                     here = state->distcode[la    1112                     here = state->distcode[last.val +
1135                             (BITS(last.bits +    1113                             (BITS(last.bits + last.op) >> last.bits)];
1136                     if ((unsigned)(last.bits     1114                     if ((unsigned)(last.bits + here.bits) <= bits) break;
1137                     PULLBYTE();                  1115                     PULLBYTE();
1138                 }                                1116                 }
1139                 DROPBITS(last.bits);             1117                 DROPBITS(last.bits);
1140                 state->back += last.bits;        1118                 state->back += last.bits;
1141             }                                    1119             }
1142             DROPBITS(here.bits);                 1120             DROPBITS(here.bits);
1143             state->back += here.bits;            1121             state->back += here.bits;
1144             if (here.op & 64) {                  1122             if (here.op & 64) {
1145                 strm->msg = (char *)"invalid     1123                 strm->msg = (char *)"invalid distance code";
1146                 state->mode = BAD;               1124                 state->mode = BAD;
1147                 break;                           1125                 break;
1148             }                                    1126             }
1149             state->offset = (unsigned)here.va    1127             state->offset = (unsigned)here.val;
1150             state->extra = (unsigned)(here.op    1128             state->extra = (unsigned)(here.op) & 15;
1151             state->mode = DISTEXT;               1129             state->mode = DISTEXT;
1152                 /* fallthrough */             << 
1153         case DISTEXT:                            1130         case DISTEXT:
1154             if (state->extra) {                  1131             if (state->extra) {
1155                 NEEDBITS(state->extra);          1132                 NEEDBITS(state->extra);
1156                 state->offset += BITS(state->    1133                 state->offset += BITS(state->extra);
1157                 DROPBITS(state->extra);          1134                 DROPBITS(state->extra);
1158                 state->back += state->extra;     1135                 state->back += state->extra;
1159             }                                    1136             }
1160 #ifdef INFLATE_STRICT                            1137 #ifdef INFLATE_STRICT
1161             if (state->offset > state->dmax)     1138             if (state->offset > state->dmax) {
1162                 strm->msg = (char *)"invalid     1139                 strm->msg = (char *)"invalid distance too far back";
1163                 state->mode = BAD;               1140                 state->mode = BAD;
1164                 break;                           1141                 break;
1165             }                                    1142             }
1166 #endif                                           1143 #endif
1167             Tracevv((stderr, "inflate:           1144             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1168             state->mode = MATCH;                 1145             state->mode = MATCH;
1169                 /* fallthrough */             << 
1170         case MATCH:                              1146         case MATCH:
1171             if (left == 0) goto inf_leave;       1147             if (left == 0) goto inf_leave;
1172             copy = out - left;                   1148             copy = out - left;
1173             if (state->offset > copy) {          1149             if (state->offset > copy) {         /* copy from window */
1174                 copy = state->offset - copy;     1150                 copy = state->offset - copy;
1175                 if (copy > state->whave) {       1151                 if (copy > state->whave) {
1176                     if (state->sane) {           1152                     if (state->sane) {
1177                         strm->msg = (char *)"    1153                         strm->msg = (char *)"invalid distance too far back";
1178                         state->mode = BAD;       1154                         state->mode = BAD;
1179                         break;                   1155                         break;
1180                     }                            1156                     }
1181 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_    1157 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1182                     Trace((stderr, "inflate.c    1158                     Trace((stderr, "inflate.c too far\n"));
1183                     copy -= state->whave;        1159                     copy -= state->whave;
1184                     if (copy > state->length)    1160                     if (copy > state->length) copy = state->length;
1185                     if (copy > left) copy = l    1161                     if (copy > left) copy = left;
1186                     left -= copy;                1162                     left -= copy;
1187                     state->length -= copy;       1163                     state->length -= copy;
1188                     do {                         1164                     do {
1189                         *put++ = 0;              1165                         *put++ = 0;
1190                     } while (--copy);            1166                     } while (--copy);
1191                     if (state->length == 0) s    1167                     if (state->length == 0) state->mode = LEN;
1192                     break;                       1168                     break;
1193 #endif                                           1169 #endif
1194                 }                                1170                 }
1195                 if (copy > state->wnext) {       1171                 if (copy > state->wnext) {
1196                     copy -= state->wnext;        1172                     copy -= state->wnext;
1197                     from = state->window + (s    1173                     from = state->window + (state->wsize - copy);
1198                 }                                1174                 }
1199                 else                             1175                 else
1200                     from = state->window + (s    1176                     from = state->window + (state->wnext - copy);
1201                 if (copy > state->length) cop    1177                 if (copy > state->length) copy = state->length;
1202             }                                    1178             }
1203             else {                               1179             else {                              /* copy from output */
1204                 from = put - state->offset;      1180                 from = put - state->offset;
1205                 copy = state->length;            1181                 copy = state->length;
1206             }                                    1182             }
1207             if (copy > left) copy = left;        1183             if (copy > left) copy = left;
1208             left -= copy;                        1184             left -= copy;
1209             state->length -= copy;               1185             state->length -= copy;
1210             do {                                 1186             do {
1211                 *put++ = *from++;                1187                 *put++ = *from++;
1212             } while (--copy);                    1188             } while (--copy);
1213             if (state->length == 0) state->mo    1189             if (state->length == 0) state->mode = LEN;
1214             break;                               1190             break;
1215         case LIT:                                1191         case LIT:
1216             if (left == 0) goto inf_leave;       1192             if (left == 0) goto inf_leave;
1217             *put++ = (unsigned char)(state->l    1193             *put++ = (unsigned char)(state->length);
1218             left--;                              1194             left--;
1219             state->mode = LEN;                   1195             state->mode = LEN;
1220             break;                               1196             break;
1221         case CHECK:                              1197         case CHECK:
1222             if (state->wrap) {                   1198             if (state->wrap) {
1223                 NEEDBITS(32);                    1199                 NEEDBITS(32);
1224                 out -= left;                     1200                 out -= left;
1225                 strm->total_out += out;          1201                 strm->total_out += out;
1226                 state->total += out;             1202                 state->total += out;
1227                 if ((state->wrap & 4) && out)    1203                 if ((state->wrap & 4) && out)
1228                     strm->adler = state->chec    1204                     strm->adler = state->check =
1229                         UPDATE_CHECK(state->c << 1205                         UPDATE(state->check, put - out, out);
1230                 out = left;                      1206                 out = left;
1231                 if ((state->wrap & 4) && (       1207                 if ((state->wrap & 4) && (
1232 #ifdef GUNZIP                                    1208 #ifdef GUNZIP
1233                      state->flags ? hold :       1209                      state->flags ? hold :
1234 #endif                                           1210 #endif
1235                      ZSWAP32(hold)) != state-    1211                      ZSWAP32(hold)) != state->check) {
1236                     strm->msg = (char *)"inco    1212                     strm->msg = (char *)"incorrect data check";
1237                     state->mode = BAD;           1213                     state->mode = BAD;
1238                     break;                       1214                     break;
1239                 }                                1215                 }
1240                 INITBITS();                      1216                 INITBITS();
1241                 Tracev((stderr, "inflate:   c    1217                 Tracev((stderr, "inflate:   check matches trailer\n"));
1242             }                                    1218             }
1243 #ifdef GUNZIP                                    1219 #ifdef GUNZIP
1244             state->mode = LENGTH;                1220             state->mode = LENGTH;
1245                 /* fallthrough */             << 
1246         case LENGTH:                             1221         case LENGTH:
1247             if (state->wrap && state->flags)     1222             if (state->wrap && state->flags) {
1248                 NEEDBITS(32);                    1223                 NEEDBITS(32);
1249                 if ((state->wrap & 4) && hold << 1224                 if (hold != (state->total & 0xffffffffUL)) {
1250                     strm->msg = (char *)"inco    1225                     strm->msg = (char *)"incorrect length check";
1251                     state->mode = BAD;           1226                     state->mode = BAD;
1252                     break;                       1227                     break;
1253                 }                                1228                 }
1254                 INITBITS();                      1229                 INITBITS();
1255                 Tracev((stderr, "inflate:   l    1230                 Tracev((stderr, "inflate:   length matches trailer\n"));
1256             }                                    1231             }
1257 #endif                                           1232 #endif
1258             state->mode = DONE;                  1233             state->mode = DONE;
1259                 /* fallthrough */             << 
1260         case DONE:                               1234         case DONE:
1261             ret = Z_STREAM_END;                  1235             ret = Z_STREAM_END;
1262             goto inf_leave;                      1236             goto inf_leave;
1263         case BAD:                                1237         case BAD:
1264             ret = Z_DATA_ERROR;                  1238             ret = Z_DATA_ERROR;
1265             goto inf_leave;                      1239             goto inf_leave;
1266         case MEM:                                1240         case MEM:
1267             return Z_MEM_ERROR;                  1241             return Z_MEM_ERROR;
1268         case SYNC:                               1242         case SYNC:
1269                 /* fallthrough */             << 
1270         default:                                 1243         default:
1271             return Z_STREAM_ERROR;               1244             return Z_STREAM_ERROR;
1272         }                                        1245         }
1273                                                  1246 
1274     /*                                           1247     /*
1275        Return from inflate(), updating the to    1248        Return from inflate(), updating the total counts and the check value.
1276        If there was no progress during the in    1249        If there was no progress during the inflate() call, return a buffer
1277        error.  Call updatewindow() to create     1250        error.  Call updatewindow() to create and/or update the window state.
1278        Note: a memory error from inflate() is    1251        Note: a memory error from inflate() is non-recoverable.
1279      */                                          1252      */
1280   inf_leave:                                     1253   inf_leave:
1281     RESTORE();                                   1254     RESTORE();
1282     if (state->wsize || (out != strm->avail_o    1255     if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1283             (state->mode < CHECK || flush !=     1256             (state->mode < CHECK || flush != Z_FINISH)))
1284         if (updatewindow(strm, strm->next_out    1257         if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1285             state->mode = MEM;                   1258             state->mode = MEM;
1286             return Z_MEM_ERROR;                  1259             return Z_MEM_ERROR;
1287         }                                        1260         }
1288     in -= strm->avail_in;                        1261     in -= strm->avail_in;
1289     out -= strm->avail_out;                      1262     out -= strm->avail_out;
1290     strm->total_in += in;                        1263     strm->total_in += in;
1291     strm->total_out += out;                      1264     strm->total_out += out;
1292     state->total += out;                         1265     state->total += out;
1293     if ((state->wrap & 4) && out)                1266     if ((state->wrap & 4) && out)
1294         strm->adler = state->check =             1267         strm->adler = state->check =
1295             UPDATE_CHECK(state->check, strm-> << 1268             UPDATE(state->check, strm->next_out - out, out);
1296     strm->data_type = (int)state->bits + (sta    1269     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1297                       (state->mode == TYPE ?     1270                       (state->mode == TYPE ? 128 : 0) +
1298                       (state->mode == LEN_ ||    1271                       (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1299     if (((in == 0 && out == 0) || flush == Z_    1272     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1300         ret = Z_BUF_ERROR;                       1273         ret = Z_BUF_ERROR;
1301     return ret;                                  1274     return ret;
1302 }                                                1275 }
1303                                                  1276 
1304 int ZEXPORT inflateEnd(strm)                     1277 int ZEXPORT inflateEnd(strm)
1305 z_streamp strm;                                  1278 z_streamp strm;
1306 {                                                1279 {
1307     struct inflate_state FAR *state;             1280     struct inflate_state FAR *state;
1308     if (inflateStateCheck(strm))                 1281     if (inflateStateCheck(strm))
1309         return Z_STREAM_ERROR;                   1282         return Z_STREAM_ERROR;
1310     state = (struct inflate_state FAR *)strm-    1283     state = (struct inflate_state FAR *)strm->state;
1311     if (state->window != Z_NULL) ZFREE(strm,     1284     if (state->window != Z_NULL) ZFREE(strm, state->window);
1312     ZFREE(strm, strm->state);                    1285     ZFREE(strm, strm->state);
1313     strm->state = Z_NULL;                        1286     strm->state = Z_NULL;
1314     Tracev((stderr, "inflate: end\n"));          1287     Tracev((stderr, "inflate: end\n"));
1315     return Z_OK;                                 1288     return Z_OK;
1316 }                                                1289 }
1317                                                  1290 
1318 int ZEXPORT inflateGetDictionary(strm, dictio    1291 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1319 z_streamp strm;                                  1292 z_streamp strm;
1320 Bytef *dictionary;                               1293 Bytef *dictionary;
1321 uInt *dictLength;                                1294 uInt *dictLength;
1322 {                                                1295 {
1323     struct inflate_state FAR *state;             1296     struct inflate_state FAR *state;
1324                                                  1297 
1325     /* check state */                            1298     /* check state */
1326     if (inflateStateCheck(strm)) return Z_STR    1299     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1327     state = (struct inflate_state FAR *)strm-    1300     state = (struct inflate_state FAR *)strm->state;
1328                                                  1301 
1329     /* copy dictionary */                        1302     /* copy dictionary */
1330     if (state->whave && dictionary != Z_NULL)    1303     if (state->whave && dictionary != Z_NULL) {
1331         zmemcpy(dictionary, state->window + s    1304         zmemcpy(dictionary, state->window + state->wnext,
1332                 state->whave - state->wnext);    1305                 state->whave - state->wnext);
1333         zmemcpy(dictionary + state->whave - s    1306         zmemcpy(dictionary + state->whave - state->wnext,
1334                 state->window, state->wnext);    1307                 state->window, state->wnext);
1335     }                                            1308     }
1336     if (dictLength != Z_NULL)                    1309     if (dictLength != Z_NULL)
1337         *dictLength = state->whave;              1310         *dictLength = state->whave;
1338     return Z_OK;                                 1311     return Z_OK;
1339 }                                                1312 }
1340                                                  1313 
1341 int ZEXPORT inflateSetDictionary(strm, dictio    1314 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1342 z_streamp strm;                                  1315 z_streamp strm;
1343 const Bytef *dictionary;                         1316 const Bytef *dictionary;
1344 uInt dictLength;                                 1317 uInt dictLength;
1345 {                                                1318 {
1346     struct inflate_state FAR *state;             1319     struct inflate_state FAR *state;
1347     unsigned long dictid;                        1320     unsigned long dictid;
1348     int ret;                                     1321     int ret;
1349                                                  1322 
1350     /* check state */                            1323     /* check state */
1351     if (inflateStateCheck(strm)) return Z_STR    1324     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1352     state = (struct inflate_state FAR *)strm-    1325     state = (struct inflate_state FAR *)strm->state;
1353     if (state->wrap != 0 && state->mode != DI    1326     if (state->wrap != 0 && state->mode != DICT)
1354         return Z_STREAM_ERROR;                   1327         return Z_STREAM_ERROR;
1355                                                  1328 
1356     /* check for correct dictionary identifie    1329     /* check for correct dictionary identifier */
1357     if (state->mode == DICT) {                   1330     if (state->mode == DICT) {
1358         dictid = adler32(0L, Z_NULL, 0);         1331         dictid = adler32(0L, Z_NULL, 0);
1359         dictid = adler32(dictid, dictionary,     1332         dictid = adler32(dictid, dictionary, dictLength);
1360         if (dictid != state->check)              1333         if (dictid != state->check)
1361             return Z_DATA_ERROR;                 1334             return Z_DATA_ERROR;
1362     }                                            1335     }
1363                                                  1336 
1364     /* copy dictionary to window using update    1337     /* copy dictionary to window using updatewindow(), which will amend the
1365        existing dictionary if appropriate */     1338        existing dictionary if appropriate */
1366     ret = updatewindow(strm, dictionary + dic    1339     ret = updatewindow(strm, dictionary + dictLength, dictLength);
1367     if (ret) {                                   1340     if (ret) {
1368         state->mode = MEM;                       1341         state->mode = MEM;
1369         return Z_MEM_ERROR;                      1342         return Z_MEM_ERROR;
1370     }                                            1343     }
1371     state->havedict = 1;                         1344     state->havedict = 1;
1372     Tracev((stderr, "inflate:   dictionary se    1345     Tracev((stderr, "inflate:   dictionary set\n"));
1373     return Z_OK;                                 1346     return Z_OK;
1374 }                                                1347 }
1375                                                  1348 
1376 int ZEXPORT inflateGetHeader(strm, head)         1349 int ZEXPORT inflateGetHeader(strm, head)
1377 z_streamp strm;                                  1350 z_streamp strm;
1378 gz_headerp head;                                 1351 gz_headerp head;
1379 {                                                1352 {
1380     struct inflate_state FAR *state;             1353     struct inflate_state FAR *state;
1381                                                  1354 
1382     /* check state */                            1355     /* check state */
1383     if (inflateStateCheck(strm)) return Z_STR    1356     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1384     state = (struct inflate_state FAR *)strm-    1357     state = (struct inflate_state FAR *)strm->state;
1385     if ((state->wrap & 2) == 0) return Z_STRE    1358     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1386                                                  1359 
1387     /* save header structure */                  1360     /* save header structure */
1388     state->head = head;                          1361     state->head = head;
1389     head->done = 0;                              1362     head->done = 0;
1390     return Z_OK;                                 1363     return Z_OK;
1391 }                                                1364 }
1392                                                  1365 
1393 /*                                               1366 /*
1394    Search buf[0..len-1] for the pattern: 0, 0    1367    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1395    or when out of input.  When called, *have     1368    or when out of input.  When called, *have is the number of pattern bytes
1396    found in order so far, in 0..3.  On return    1369    found in order so far, in 0..3.  On return *have is updated to the new
1397    state.  If on return *have equals four, th    1370    state.  If on return *have equals four, then the pattern was found and the
1398    return value is how many bytes were read i    1371    return value is how many bytes were read including the last byte of the
1399    pattern.  If *have is less than four, then    1372    pattern.  If *have is less than four, then the pattern has not been found
1400    yet and the return value is len.  In the l    1373    yet and the return value is len.  In the latter case, syncsearch() can be
1401    called again with more data and the *have     1374    called again with more data and the *have state.  *have is initialized to
1402    zero for the first call.                      1375    zero for the first call.
1403  */                                              1376  */
1404 local unsigned syncsearch(have, buf, len)        1377 local unsigned syncsearch(have, buf, len)
1405 unsigned FAR *have;                              1378 unsigned FAR *have;
1406 const unsigned char FAR *buf;                    1379 const unsigned char FAR *buf;
1407 unsigned len;                                    1380 unsigned len;
1408 {                                                1381 {
1409     unsigned got;                                1382     unsigned got;
1410     unsigned next;                               1383     unsigned next;
1411                                                  1384 
1412     got = *have;                                 1385     got = *have;
1413     next = 0;                                    1386     next = 0;
1414     while (next < len && got < 4) {              1387     while (next < len && got < 4) {
1415         if ((int)(buf[next]) == (got < 2 ? 0     1388         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1416             got++;                               1389             got++;
1417         else if (buf[next])                      1390         else if (buf[next])
1418             got = 0;                             1391             got = 0;
1419         else                                     1392         else
1420             got = 4 - got;                       1393             got = 4 - got;
1421         next++;                                  1394         next++;
1422     }                                            1395     }
1423     *have = got;                                 1396     *have = got;
1424     return next;                                 1397     return next;
1425 }                                                1398 }
1426                                                  1399 
1427 int ZEXPORT inflateSync(strm)                    1400 int ZEXPORT inflateSync(strm)
1428 z_streamp strm;                                  1401 z_streamp strm;
1429 {                                                1402 {
1430     unsigned len;               /* number of     1403     unsigned len;               /* number of bytes to look at or looked at */
1431     int flags;                  /* temporary  << 
1432     unsigned long in, out;      /* temporary     1404     unsigned long in, out;      /* temporary to save total_in and total_out */
1433     unsigned char buf[4];       /* to restore    1405     unsigned char buf[4];       /* to restore bit buffer to byte string */
1434     struct inflate_state FAR *state;             1406     struct inflate_state FAR *state;
1435                                                  1407 
1436     /* check parameters */                       1408     /* check parameters */
1437     if (inflateStateCheck(strm)) return Z_STR    1409     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1438     state = (struct inflate_state FAR *)strm-    1410     state = (struct inflate_state FAR *)strm->state;
1439     if (strm->avail_in == 0 && state->bits <     1411     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1440                                                  1412 
1441     /* if first time, start search in bit buf    1413     /* if first time, start search in bit buffer */
1442     if (state->mode != SYNC) {                   1414     if (state->mode != SYNC) {
1443         state->mode = SYNC;                      1415         state->mode = SYNC;
1444         state->hold <<= state->bits & 7;         1416         state->hold <<= state->bits & 7;
1445         state->bits -= state->bits & 7;          1417         state->bits -= state->bits & 7;
1446         len = 0;                                 1418         len = 0;
1447         while (state->bits >= 8) {               1419         while (state->bits >= 8) {
1448             buf[len++] = (unsigned char)(stat    1420             buf[len++] = (unsigned char)(state->hold);
1449             state->hold >>= 8;                   1421             state->hold >>= 8;
1450             state->bits -= 8;                    1422             state->bits -= 8;
1451         }                                        1423         }
1452         state->have = 0;                         1424         state->have = 0;
1453         syncsearch(&(state->have), buf, len);    1425         syncsearch(&(state->have), buf, len);
1454     }                                            1426     }
1455                                                  1427 
1456     /* search available input */                 1428     /* search available input */
1457     len = syncsearch(&(state->have), strm->ne    1429     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1458     strm->avail_in -= len;                       1430     strm->avail_in -= len;
1459     strm->next_in += len;                        1431     strm->next_in += len;
1460     strm->total_in += len;                       1432     strm->total_in += len;
1461                                                  1433 
1462     /* return no joy or set up to restart inf    1434     /* return no joy or set up to restart inflate() on a new block */
1463     if (state->have != 4) return Z_DATA_ERROR    1435     if (state->have != 4) return Z_DATA_ERROR;
1464     if (state->flags == -1)                   << 
1465         state->wrap = 0;    /* if no header y << 
1466     else                                      << 
1467         state->wrap &= ~4;  /* no point in co << 
1468     flags = state->flags;                     << 
1469     in = strm->total_in;  out = strm->total_o    1436     in = strm->total_in;  out = strm->total_out;
1470     inflateReset(strm);                          1437     inflateReset(strm);
1471     strm->total_in = in;  strm->total_out = o    1438     strm->total_in = in;  strm->total_out = out;
1472     state->flags = flags;                     << 
1473     state->mode = TYPE;                          1439     state->mode = TYPE;
1474     return Z_OK;                                 1440     return Z_OK;
1475 }                                                1441 }
1476                                                  1442 
1477 /*                                               1443 /*
1478    Returns true if inflate is currently at th    1444    Returns true if inflate is currently at the end of a block generated by
1479    Z_SYNC_FLUSH or Z_FULL_FLUSH. This functio    1445    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1480    implementation to provide an additional sa    1446    implementation to provide an additional safety check. PPP uses
1481    Z_SYNC_FLUSH but removes the length bytes     1447    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1482    block. When decompressing, PPP checks that    1448    block. When decompressing, PPP checks that at the end of input packet,
1483    inflate is waiting for these length bytes.    1449    inflate is waiting for these length bytes.
1484  */                                              1450  */
1485 int ZEXPORT inflateSyncPoint(strm)               1451 int ZEXPORT inflateSyncPoint(strm)
1486 z_streamp strm;                                  1452 z_streamp strm;
1487 {                                                1453 {
1488     struct inflate_state FAR *state;             1454     struct inflate_state FAR *state;
1489                                                  1455 
1490     if (inflateStateCheck(strm)) return Z_STR    1456     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1491     state = (struct inflate_state FAR *)strm-    1457     state = (struct inflate_state FAR *)strm->state;
1492     return state->mode == STORED && state->bi    1458     return state->mode == STORED && state->bits == 0;
1493 }                                                1459 }
1494                                                  1460 
1495 int ZEXPORT inflateCopy(dest, source)            1461 int ZEXPORT inflateCopy(dest, source)
1496 z_streamp dest;                                  1462 z_streamp dest;
1497 z_streamp source;                                1463 z_streamp source;
1498 {                                                1464 {
1499     struct inflate_state FAR *state;             1465     struct inflate_state FAR *state;
1500     struct inflate_state FAR *copy;              1466     struct inflate_state FAR *copy;
1501     unsigned char FAR *window;                   1467     unsigned char FAR *window;
1502     unsigned wsize;                              1468     unsigned wsize;
1503                                                  1469 
1504     /* check input */                            1470     /* check input */
1505     if (inflateStateCheck(source) || dest ==     1471     if (inflateStateCheck(source) || dest == Z_NULL)
1506         return Z_STREAM_ERROR;                   1472         return Z_STREAM_ERROR;
1507     state = (struct inflate_state FAR *)sourc    1473     state = (struct inflate_state FAR *)source->state;
1508                                                  1474 
1509     /* allocate space */                         1475     /* allocate space */
1510     copy = (struct inflate_state FAR *)          1476     copy = (struct inflate_state FAR *)
1511            ZALLOC(source, 1, sizeof(struct in    1477            ZALLOC(source, 1, sizeof(struct inflate_state));
1512     if (copy == Z_NULL) return Z_MEM_ERROR;      1478     if (copy == Z_NULL) return Z_MEM_ERROR;
1513     window = Z_NULL;                             1479     window = Z_NULL;
1514     if (state->window != Z_NULL) {               1480     if (state->window != Z_NULL) {
1515         window = (unsigned char FAR *)           1481         window = (unsigned char FAR *)
1516                  ZALLOC(source, 1U << state->    1482                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1517         if (window == Z_NULL) {                  1483         if (window == Z_NULL) {
1518             ZFREE(source, copy);                 1484             ZFREE(source, copy);
1519             return Z_MEM_ERROR;                  1485             return Z_MEM_ERROR;
1520         }                                        1486         }
1521     }                                            1487     }
1522                                                  1488 
1523     /* copy state */                             1489     /* copy state */
1524     zmemcpy((voidpf)dest, (voidpf)source, siz    1490     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1525     zmemcpy((voidpf)copy, (voidpf)state, size    1491     zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1526     copy->strm = dest;                           1492     copy->strm = dest;
1527     if (state->lencode >= state->codes &&        1493     if (state->lencode >= state->codes &&
1528         state->lencode <= state->codes + ENOU    1494         state->lencode <= state->codes + ENOUGH - 1) {
1529         copy->lencode = copy->codes + (state-    1495         copy->lencode = copy->codes + (state->lencode - state->codes);
1530         copy->distcode = copy->codes + (state    1496         copy->distcode = copy->codes + (state->distcode - state->codes);
1531     }                                            1497     }
1532     copy->next = copy->codes + (state->next -    1498     copy->next = copy->codes + (state->next - state->codes);
1533     if (window != Z_NULL) {                      1499     if (window != Z_NULL) {
1534         wsize = 1U << state->wbits;              1500         wsize = 1U << state->wbits;
1535         zmemcpy(window, state->window, wsize)    1501         zmemcpy(window, state->window, wsize);
1536     }                                            1502     }
1537     copy->window = window;                       1503     copy->window = window;
1538     dest->state = (struct internal_state FAR     1504     dest->state = (struct internal_state FAR *)copy;
1539     return Z_OK;                                 1505     return Z_OK;
1540 }                                                1506 }
1541                                                  1507 
1542 int ZEXPORT inflateUndermine(strm, subvert)      1508 int ZEXPORT inflateUndermine(strm, subvert)
1543 z_streamp strm;                                  1509 z_streamp strm;
1544 int subvert;                                     1510 int subvert;
1545 {                                                1511 {
1546     struct inflate_state FAR *state;             1512     struct inflate_state FAR *state;
1547                                                  1513 
1548     if (inflateStateCheck(strm)) return Z_STR    1514     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1549     state = (struct inflate_state FAR *)strm-    1515     state = (struct inflate_state FAR *)strm->state;
1550 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_    1516 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1551     state->sane = !subvert;                      1517     state->sane = !subvert;
1552     return Z_OK;                                 1518     return Z_OK;
1553 #else                                            1519 #else
1554     (void)subvert;                               1520     (void)subvert;
1555     state->sane = 1;                             1521     state->sane = 1;
1556     return Z_DATA_ERROR;                         1522     return Z_DATA_ERROR;
1557 #endif                                           1523 #endif
1558 }                                                1524 }
1559                                                  1525 
1560 int ZEXPORT inflateValidate(strm, check)         1526 int ZEXPORT inflateValidate(strm, check)
1561 z_streamp strm;                                  1527 z_streamp strm;
1562 int check;                                       1528 int check;
1563 {                                                1529 {
1564     struct inflate_state FAR *state;             1530     struct inflate_state FAR *state;
1565                                                  1531 
1566     if (inflateStateCheck(strm)) return Z_STR    1532     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1567     state = (struct inflate_state FAR *)strm-    1533     state = (struct inflate_state FAR *)strm->state;
1568     if (check && state->wrap)                 << 1534     if (check)
1569         state->wrap |= 4;                        1535         state->wrap |= 4;
1570     else                                         1536     else
1571         state->wrap &= ~4;                       1537         state->wrap &= ~4;
1572     return Z_OK;                                 1538     return Z_OK;
1573 }                                                1539 }
1574                                                  1540 
1575 long ZEXPORT inflateMark(strm)                   1541 long ZEXPORT inflateMark(strm)
1576 z_streamp strm;                                  1542 z_streamp strm;
1577 {                                                1543 {
1578     struct inflate_state FAR *state;             1544     struct inflate_state FAR *state;
1579                                                  1545 
1580     if (inflateStateCheck(strm))                 1546     if (inflateStateCheck(strm))
1581         return -(1L << 16);                      1547         return -(1L << 16);
1582     state = (struct inflate_state FAR *)strm-    1548     state = (struct inflate_state FAR *)strm->state;
1583     return (long)(((unsigned long)((long)stat    1549     return (long)(((unsigned long)((long)state->back)) << 16) +
1584         (state->mode == COPY ? state->length     1550         (state->mode == COPY ? state->length :
1585             (state->mode == MATCH ? state->wa    1551             (state->mode == MATCH ? state->was - state->length : 0));
1586 }                                                1552 }
1587                                                  1553 
1588 unsigned long ZEXPORT inflateCodesUsed(strm)     1554 unsigned long ZEXPORT inflateCodesUsed(strm)
1589 z_streamp strm;                                  1555 z_streamp strm;
1590 {                                                1556 {
1591     struct inflate_state FAR *state;             1557     struct inflate_state FAR *state;
1592     if (inflateStateCheck(strm)) return (unsi    1558     if (inflateStateCheck(strm)) return (unsigned long)-1;
1593     state = (struct inflate_state FAR *)strm-    1559     state = (struct inflate_state FAR *)strm->state;
1594     return (unsigned long)(state->next - stat    1560     return (unsigned long)(state->next - state->codes);
1595 }                                                1561 }
1596                                                  1562