Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/zlib/src/infback.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/infback.c (Version 11.3.0) and /externals/zlib/src/infback.c (Version 11.2.1)


  1 /* infback.c -- inflate using a call-back inte      1 /* infback.c -- inflate using a call-back interface
  2  * Copyright (C) 1995-2022 Mark Adler               2  * Copyright (C) 1995-2022 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    This code is largely copied from inflate.c.      7    This code is largely copied from inflate.c.  Normally either infback.o or
  8    inflate.o would be linked into an applicati      8    inflate.o would be linked into an application--not both.  The interface
  9    with inffast.c is retained so that optimize      9    with inffast.c is retained so that optimized assembler-coded versions of
 10    inflate_fast() can be used with either infl     10    inflate_fast() can be used with either inflate.c or infback.c.
 11  */                                                11  */
 12                                                    12 
 13 #include "zutil.h"                                 13 #include "zutil.h"
 14 #include "inftrees.h"                              14 #include "inftrees.h"
 15 #include "inflate.h"                               15 #include "inflate.h"
 16 #include "inffast.h"                               16 #include "inffast.h"
 17                                                    17 
 18 /* function prototypes */                          18 /* function prototypes */
 19 local void fixedtables OF((struct inflate_stat     19 local void fixedtables OF((struct inflate_state FAR *state));
 20                                                    20 
 21 /*                                                 21 /*
 22    strm provides memory allocation functions i     22    strm provides memory allocation functions in zalloc and zfree, or
 23    Z_NULL to use the library memory allocation     23    Z_NULL to use the library memory allocation functions.
 24                                                    24 
 25    windowBits is in the range 8..15, and windo     25    windowBits is in the range 8..15, and window is a user-supplied
 26    window and output buffer that is 2**windowB     26    window and output buffer that is 2**windowBits bytes.
 27  */                                                27  */
 28 int ZEXPORT inflateBackInit_(strm, windowBits,     28 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
 29 z_streamp strm;                                    29 z_streamp strm;
 30 int windowBits;                                    30 int windowBits;
 31 unsigned char FAR *window;                         31 unsigned char FAR *window;
 32 const char *version;                               32 const char *version;
 33 int stream_size;                                   33 int stream_size;
 34 {                                                  34 {
 35     struct inflate_state FAR *state;               35     struct inflate_state FAR *state;
 36                                                    36 
 37     if (version == Z_NULL || version[0] != ZLI     37     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 38         stream_size != (int)(sizeof(z_stream))     38         stream_size != (int)(sizeof(z_stream)))
 39         return Z_VERSION_ERROR;                    39         return Z_VERSION_ERROR;
 40     if (strm == Z_NULL || window == Z_NULL ||      40     if (strm == Z_NULL || window == Z_NULL ||
 41         windowBits < 8 || windowBits > 15)         41         windowBits < 8 || windowBits > 15)
 42         return Z_STREAM_ERROR;                     42         return Z_STREAM_ERROR;
 43     strm->msg = Z_NULL;                 /* in      43     strm->msg = Z_NULL;                 /* in case we return an error */
 44     if (strm->zalloc == (alloc_func)0) {           44     if (strm->zalloc == (alloc_func)0) {
 45 #ifdef Z_SOLO                                      45 #ifdef Z_SOLO
 46         return Z_STREAM_ERROR;                     46         return Z_STREAM_ERROR;
 47 #else                                              47 #else
 48         strm->zalloc = zcalloc;                    48         strm->zalloc = zcalloc;
 49         strm->opaque = (voidpf)0;                  49         strm->opaque = (voidpf)0;
 50 #endif                                             50 #endif
 51     }                                              51     }
 52     if (strm->zfree == (free_func)0)               52     if (strm->zfree == (free_func)0)
 53 #ifdef Z_SOLO                                      53 #ifdef Z_SOLO
 54         return Z_STREAM_ERROR;                     54         return Z_STREAM_ERROR;
 55 #else                                              55 #else
 56     strm->zfree = zcfree;                          56     strm->zfree = zcfree;
 57 #endif                                             57 #endif
 58     state = (struct inflate_state FAR *)ZALLOC     58     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
 59                                                    59                                                sizeof(struct inflate_state));
 60     if (state == Z_NULL) return Z_MEM_ERROR;       60     if (state == Z_NULL) return Z_MEM_ERROR;
 61     Tracev((stderr, "inflate: allocated\n"));      61     Tracev((stderr, "inflate: allocated\n"));
 62     strm->state = (struct internal_state FAR *     62     strm->state = (struct internal_state FAR *)state;
 63     state->dmax = 32768U;                          63     state->dmax = 32768U;
 64     state->wbits = (uInt)windowBits;               64     state->wbits = (uInt)windowBits;
 65     state->wsize = 1U << windowBits;               65     state->wsize = 1U << windowBits;
 66     state->window = window;                        66     state->window = window;
 67     state->wnext = 0;                              67     state->wnext = 0;
 68     state->whave = 0;                              68     state->whave = 0;
 69     state->sane = 1;                               69     state->sane = 1;
 70     return Z_OK;                                   70     return Z_OK;
 71 }                                                  71 }
 72                                                    72 
 73 /*                                                 73 /*
 74    Return state with length and distance decod     74    Return state with length and distance decoding tables and index sizes set to
 75    fixed code decoding.  Normally this returns     75    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
 76    If BUILDFIXED is defined, then instead this     76    If BUILDFIXED is defined, then instead this routine builds the tables the
 77    first time it's called, and returns those t     77    first time it's called, and returns those tables the first time and
 78    thereafter.  This reduces the size of the c     78    thereafter.  This reduces the size of the code by about 2K bytes, in
 79    exchange for a little execution time.  Howe     79    exchange for a little execution time.  However, BUILDFIXED should not be
 80    used for threaded applications, since the r     80    used for threaded applications, since the rewriting of the tables and virgin
 81    may not be thread-safe.                         81    may not be thread-safe.
 82  */                                                82  */
 83 local void fixedtables(state)                      83 local void fixedtables(state)
 84 struct inflate_state FAR *state;                   84 struct inflate_state FAR *state;
 85 {                                                  85 {
 86 #ifdef BUILDFIXED                                  86 #ifdef BUILDFIXED
 87     static int virgin = 1;                         87     static int virgin = 1;
 88     static code *lenfix, *distfix;                 88     static code *lenfix, *distfix;
 89     static code fixed[544];                        89     static code fixed[544];
 90                                                    90 
 91     /* build fixed huffman tables if first cal     91     /* build fixed huffman tables if first call (may not be thread safe) */
 92     if (virgin) {                                  92     if (virgin) {
 93         unsigned sym, bits;                        93         unsigned sym, bits;
 94         static code *next;                         94         static code *next;
 95                                                    95 
 96         /* literal/length table */                 96         /* literal/length table */
 97         sym = 0;                                   97         sym = 0;
 98         while (sym < 144) state->lens[sym++] =     98         while (sym < 144) state->lens[sym++] = 8;
 99         while (sym < 256) state->lens[sym++] =     99         while (sym < 256) state->lens[sym++] = 9;
100         while (sym < 280) state->lens[sym++] =    100         while (sym < 280) state->lens[sym++] = 7;
101         while (sym < 288) state->lens[sym++] =    101         while (sym < 288) state->lens[sym++] = 8;
102         next = fixed;                             102         next = fixed;
103         lenfix = next;                            103         lenfix = next;
104         bits = 9;                                 104         bits = 9;
105         inflate_table(LENS, state->lens, 288,     105         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
106                                                   106 
107         /* distance table */                      107         /* distance table */
108         sym = 0;                                  108         sym = 0;
109         while (sym < 32) state->lens[sym++] =     109         while (sym < 32) state->lens[sym++] = 5;
110         distfix = next;                           110         distfix = next;
111         bits = 5;                                 111         bits = 5;
112         inflate_table(DISTS, state->lens, 32,     112         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
113                                                   113 
114         /* do this just once */                   114         /* do this just once */
115         virgin = 0;                               115         virgin = 0;
116     }                                             116     }
117 #else /* !BUILDFIXED */                           117 #else /* !BUILDFIXED */
118 #   include "inffixed.h"                          118 #   include "inffixed.h"
119 #endif /* BUILDFIXED */                           119 #endif /* BUILDFIXED */
120     state->lencode = lenfix;                      120     state->lencode = lenfix;
121     state->lenbits = 9;                           121     state->lenbits = 9;
122     state->distcode = distfix;                    122     state->distcode = distfix;
123     state->distbits = 5;                          123     state->distbits = 5;
124 }                                                 124 }
125                                                   125 
126 /* Macros for inflateBack(): */                   126 /* Macros for inflateBack(): */
127                                                   127 
128 /* Load returned state from inflate_fast() */     128 /* Load returned state from inflate_fast() */
129 #define LOAD() \                                  129 #define LOAD() \
130     do { \                                        130     do { \
131         put = strm->next_out; \                   131         put = strm->next_out; \
132         left = strm->avail_out; \                 132         left = strm->avail_out; \
133         next = strm->next_in; \                   133         next = strm->next_in; \
134         have = strm->avail_in; \                  134         have = strm->avail_in; \
135         hold = state->hold; \                     135         hold = state->hold; \
136         bits = state->bits; \                     136         bits = state->bits; \
137     } while (0)                                   137     } while (0)
138                                                   138 
139 /* Set state from registers for inflate_fast()    139 /* Set state from registers for inflate_fast() */
140 #define RESTORE() \                               140 #define RESTORE() \
141     do { \                                        141     do { \
142         strm->next_out = put; \                   142         strm->next_out = put; \
143         strm->avail_out = left; \                 143         strm->avail_out = left; \
144         strm->next_in = next; \                   144         strm->next_in = next; \
145         strm->avail_in = have; \                  145         strm->avail_in = have; \
146         state->hold = hold; \                     146         state->hold = hold; \
147         state->bits = bits; \                     147         state->bits = bits; \
148     } while (0)                                   148     } while (0)
149                                                   149 
150 /* Clear the input bit accumulator */             150 /* Clear the input bit accumulator */
151 #define INITBITS() \                              151 #define INITBITS() \
152     do { \                                        152     do { \
153         hold = 0; \                               153         hold = 0; \
154         bits = 0; \                               154         bits = 0; \
155     } while (0)                                   155     } while (0)
156                                                   156 
157 /* Assure that some input is available.  If in    157 /* Assure that some input is available.  If input is requested, but denied,
158    then return a Z_BUF_ERROR from inflateBack(    158    then return a Z_BUF_ERROR from inflateBack(). */
159 #define PULL() \                                  159 #define PULL() \
160     do { \                                        160     do { \
161         if (have == 0) { \                        161         if (have == 0) { \
162             have = in(in_desc, &next); \          162             have = in(in_desc, &next); \
163             if (have == 0) { \                    163             if (have == 0) { \
164                 next = Z_NULL; \                  164                 next = Z_NULL; \
165                 ret = Z_BUF_ERROR; \              165                 ret = Z_BUF_ERROR; \
166                 goto inf_leave; \                 166                 goto inf_leave; \
167             } \                                   167             } \
168         } \                                       168         } \
169     } while (0)                                   169     } while (0)
170                                                   170 
171 /* Get a byte of input into the bit accumulato    171 /* Get a byte of input into the bit accumulator, or return from inflateBack()
172    with an error if there is no input availabl    172    with an error if there is no input available. */
173 #define PULLBYTE() \                              173 #define PULLBYTE() \
174     do { \                                        174     do { \
175         PULL(); \                                 175         PULL(); \
176         have--; \                                 176         have--; \
177         hold += (unsigned long)(*next++) << bi    177         hold += (unsigned long)(*next++) << bits; \
178         bits += 8; \                              178         bits += 8; \
179     } while (0)                                   179     } while (0)
180                                                   180 
181 /* Assure that there are at least n bits in th    181 /* Assure that there are at least n bits in the bit accumulator.  If there is
182    not enough available input to do that, then    182    not enough available input to do that, then return from inflateBack() with
183    an error. */                                   183    an error. */
184 #define NEEDBITS(n) \                             184 #define NEEDBITS(n) \
185     do { \                                        185     do { \
186         while (bits < (unsigned)(n)) \            186         while (bits < (unsigned)(n)) \
187             PULLBYTE(); \                         187             PULLBYTE(); \
188     } while (0)                                   188     } while (0)
189                                                   189 
190 /* Return the low n bits of the bit accumulato    190 /* Return the low n bits of the bit accumulator (n < 16) */
191 #define BITS(n) \                                 191 #define BITS(n) \
192     ((unsigned)hold & ((1U << (n)) - 1))          192     ((unsigned)hold & ((1U << (n)) - 1))
193                                                   193 
194 /* Remove n bits from the bit accumulator */      194 /* Remove n bits from the bit accumulator */
195 #define DROPBITS(n) \                             195 #define DROPBITS(n) \
196     do { \                                        196     do { \
197         hold >>= (n); \                           197         hold >>= (n); \
198         bits -= (unsigned)(n); \                  198         bits -= (unsigned)(n); \
199     } while (0)                                   199     } while (0)
200                                                   200 
201 /* Remove zero to seven bits as needed to go t    201 /* Remove zero to seven bits as needed to go to a byte boundary */
202 #define BYTEBITS() \                              202 #define BYTEBITS() \
203     do { \                                        203     do { \
204         hold >>= bits & 7; \                      204         hold >>= bits & 7; \
205         bits -= bits & 7; \                       205         bits -= bits & 7; \
206     } while (0)                                   206     } while (0)
207                                                   207 
208 /* Assure that some output space is available,    208 /* Assure that some output space is available, by writing out the window
209    if it's full.  If the write fails, return f    209    if it's full.  If the write fails, return from inflateBack() with a
210    Z_BUF_ERROR. */                                210    Z_BUF_ERROR. */
211 #define ROOM() \                                  211 #define ROOM() \
212     do { \                                        212     do { \
213         if (left == 0) { \                        213         if (left == 0) { \
214             put = state->window; \                214             put = state->window; \
215             left = state->wsize; \                215             left = state->wsize; \
216             state->whave = left; \                216             state->whave = left; \
217             if (out(out_desc, put, left)) { \     217             if (out(out_desc, put, left)) { \
218                 ret = Z_BUF_ERROR; \              218                 ret = Z_BUF_ERROR; \
219                 goto inf_leave; \                 219                 goto inf_leave; \
220             } \                                   220             } \
221         } \                                       221         } \
222     } while (0)                                   222     } while (0)
223                                                   223 
224 /*                                                224 /*
225    strm provides the memory allocation functio    225    strm provides the memory allocation functions and window buffer on input,
226    and provides information on the unused inpu    226    and provides information on the unused input on return.  For Z_DATA_ERROR
227    returns, strm will also provide an error me    227    returns, strm will also provide an error message.
228                                                   228 
229    in() and out() are the call-back input and     229    in() and out() are the call-back input and output functions.  When
230    inflateBack() needs more input, it calls in    230    inflateBack() needs more input, it calls in().  When inflateBack() has
231    filled the window with output, or when it c    231    filled the window with output, or when it completes with data in the
232    window, it calls out() to write out the dat    232    window, it calls out() to write out the data.  The application must not
233    change the provided input until in() is cal    233    change the provided input until in() is called again or inflateBack()
234    returns.  The application must not change t    234    returns.  The application must not change the window/output buffer until
235    inflateBack() returns.                         235    inflateBack() returns.
236                                                   236 
237    in() and out() are called with a descriptor    237    in() and out() are called with a descriptor parameter provided in the
238    inflateBack() call.  This parameter can be     238    inflateBack() call.  This parameter can be a structure that provides the
239    information required to do the read or writ    239    information required to do the read or write, as well as accumulated
240    information on the input and output such as    240    information on the input and output such as totals and check values.
241                                                   241 
242    in() should return zero on failure.  out()     242    in() should return zero on failure.  out() should return non-zero on
243    failure.  If either in() or out() fails, th    243    failure.  If either in() or out() fails, than inflateBack() returns a
244    Z_BUF_ERROR.  strm->next_in can be checked     244    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
245    was in() or out() that caused in the error.    245    was in() or out() that caused in the error.  Otherwise,  inflateBack()
246    returns Z_STREAM_END on success, Z_DATA_ERR    246    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
247    error, or Z_MEM_ERROR if it could not alloc    247    error, or Z_MEM_ERROR if it could not allocate memory for the state.
248    inflateBack() can also return Z_STREAM_ERRO    248    inflateBack() can also return Z_STREAM_ERROR if the input parameters
249    are not correct, i.e. strm is Z_NULL or the    249    are not correct, i.e. strm is Z_NULL or the state was not initialized.
250  */                                               250  */
251 int ZEXPORT inflateBack(strm, in, in_desc, out    251 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
252 z_streamp strm;                                   252 z_streamp strm;
253 in_func in;                                       253 in_func in;
254 void FAR *in_desc;                                254 void FAR *in_desc;
255 out_func out;                                     255 out_func out;
256 void FAR *out_desc;                               256 void FAR *out_desc;
257 {                                                 257 {
258     struct inflate_state FAR *state;              258     struct inflate_state FAR *state;
259     z_const unsigned char FAR *next;    /* nex    259     z_const unsigned char FAR *next;    /* next input */
260     unsigned char FAR *put;     /* next output    260     unsigned char FAR *put;     /* next output */
261     unsigned have, left;        /* available i    261     unsigned have, left;        /* available input and output */
262     unsigned long hold;         /* bit buffer     262     unsigned long hold;         /* bit buffer */
263     unsigned bits;              /* bits in bit    263     unsigned bits;              /* bits in bit buffer */
264     unsigned copy;              /* number of s    264     unsigned copy;              /* number of stored or match bytes to copy */
265     unsigned char FAR *from;    /* where to co    265     unsigned char FAR *from;    /* where to copy match bytes from */
266     code here;                  /* current dec    266     code here;                  /* current decoding table entry */
267     code last;                  /* parent tabl    267     code last;                  /* parent table entry */
268     unsigned len;               /* length to c    268     unsigned len;               /* length to copy for repeats, bits to drop */
269     int ret;                    /* return code    269     int ret;                    /* return code */
270     static const unsigned short order[19] = /*    270     static const unsigned short order[19] = /* permutation of code lengths */
271         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11,    271         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
272                                                   272 
273     /* Check that the strm exists and that the    273     /* Check that the strm exists and that the state was initialized */
274     if (strm == Z_NULL || strm->state == Z_NUL    274     if (strm == Z_NULL || strm->state == Z_NULL)
275         return Z_STREAM_ERROR;                    275         return Z_STREAM_ERROR;
276     state = (struct inflate_state FAR *)strm->    276     state = (struct inflate_state FAR *)strm->state;
277                                                   277 
278     /* Reset the state */                         278     /* Reset the state */
279     strm->msg = Z_NULL;                           279     strm->msg = Z_NULL;
280     state->mode = TYPE;                           280     state->mode = TYPE;
281     state->last = 0;                              281     state->last = 0;
282     state->whave = 0;                             282     state->whave = 0;
283     next = strm->next_in;                         283     next = strm->next_in;
284     have = next != Z_NULL ? strm->avail_in : 0    284     have = next != Z_NULL ? strm->avail_in : 0;
285     hold = 0;                                     285     hold = 0;
286     bits = 0;                                     286     bits = 0;
287     put = state->window;                          287     put = state->window;
288     left = state->wsize;                          288     left = state->wsize;
289                                                   289 
290     /* Inflate until end of block marked as la    290     /* Inflate until end of block marked as last */
291     for (;;)                                      291     for (;;)
292         switch (state->mode) {                    292         switch (state->mode) {
293         case TYPE:                                293         case TYPE:
294             /* determine and dispatch block ty    294             /* determine and dispatch block type */
295             if (state->last) {                    295             if (state->last) {
296                 BYTEBITS();                       296                 BYTEBITS();
297                 state->mode = DONE;               297                 state->mode = DONE;
298                 break;                            298                 break;
299             }                                     299             }
300             NEEDBITS(3);                          300             NEEDBITS(3);
301             state->last = BITS(1);                301             state->last = BITS(1);
302             DROPBITS(1);                          302             DROPBITS(1);
303             switch (BITS(2)) {                    303             switch (BITS(2)) {
304             case 0:                               304             case 0:                             /* stored block */
305                 Tracev((stderr, "inflate:         305                 Tracev((stderr, "inflate:     stored block%s\n",
306                         state->last ? " (last)    306                         state->last ? " (last)" : ""));
307                 state->mode = STORED;             307                 state->mode = STORED;
308                 break;                            308                 break;
309             case 1:                               309             case 1:                             /* fixed block */
310                 fixedtables(state);               310                 fixedtables(state);
311                 Tracev((stderr, "inflate:         311                 Tracev((stderr, "inflate:     fixed codes block%s\n",
312                         state->last ? " (last)    312                         state->last ? " (last)" : ""));
313                 state->mode = LEN;                313                 state->mode = LEN;              /* decode codes */
314                 break;                            314                 break;
315             case 2:                               315             case 2:                             /* dynamic block */
316                 Tracev((stderr, "inflate:         316                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
317                         state->last ? " (last)    317                         state->last ? " (last)" : ""));
318                 state->mode = TABLE;              318                 state->mode = TABLE;
319                 break;                            319                 break;
320             case 3:                               320             case 3:
321                 strm->msg = (char *)"invalid b    321                 strm->msg = (char *)"invalid block type";
322                 state->mode = BAD;                322                 state->mode = BAD;
323             }                                     323             }
324             DROPBITS(2);                          324             DROPBITS(2);
325             break;                                325             break;
326                                                   326 
327         case STORED:                              327         case STORED:
328             /* get and verify stored block len    328             /* get and verify stored block length */
329             BYTEBITS();                           329             BYTEBITS();                         /* go to byte boundary */
330             NEEDBITS(32);                         330             NEEDBITS(32);
331             if ((hold & 0xffff) != ((hold >> 1    331             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
332                 strm->msg = (char *)"invalid s    332                 strm->msg = (char *)"invalid stored block lengths";
333                 state->mode = BAD;                333                 state->mode = BAD;
334                 break;                            334                 break;
335             }                                     335             }
336             state->length = (unsigned)hold & 0    336             state->length = (unsigned)hold & 0xffff;
337             Tracev((stderr, "inflate:       st    337             Tracev((stderr, "inflate:       stored length %u\n",
338                     state->length));              338                     state->length));
339             INITBITS();                           339             INITBITS();
340                                                   340 
341             /* copy stored block from input to    341             /* copy stored block from input to output */
342             while (state->length != 0) {          342             while (state->length != 0) {
343                 copy = state->length;             343                 copy = state->length;
344                 PULL();                           344                 PULL();
345                 ROOM();                           345                 ROOM();
346                 if (copy > have) copy = have;     346                 if (copy > have) copy = have;
347                 if (copy > left) copy = left;     347                 if (copy > left) copy = left;
348                 zmemcpy(put, next, copy);         348                 zmemcpy(put, next, copy);
349                 have -= copy;                     349                 have -= copy;
350                 next += copy;                     350                 next += copy;
351                 left -= copy;                     351                 left -= copy;
352                 put += copy;                      352                 put += copy;
353                 state->length -= copy;            353                 state->length -= copy;
354             }                                     354             }
355             Tracev((stderr, "inflate:       st    355             Tracev((stderr, "inflate:       stored end\n"));
356             state->mode = TYPE;                   356             state->mode = TYPE;
357             break;                                357             break;
358                                                   358 
359         case TABLE:                               359         case TABLE:
360             /* get dynamic table entries descr    360             /* get dynamic table entries descriptor */
361             NEEDBITS(14);                         361             NEEDBITS(14);
362             state->nlen = BITS(5) + 257;          362             state->nlen = BITS(5) + 257;
363             DROPBITS(5);                          363             DROPBITS(5);
364             state->ndist = BITS(5) + 1;           364             state->ndist = BITS(5) + 1;
365             DROPBITS(5);                          365             DROPBITS(5);
366             state->ncode = BITS(4) + 4;           366             state->ncode = BITS(4) + 4;
367             DROPBITS(4);                          367             DROPBITS(4);
368 #ifndef PKZIP_BUG_WORKAROUND                      368 #ifndef PKZIP_BUG_WORKAROUND
369             if (state->nlen > 286 || state->nd    369             if (state->nlen > 286 || state->ndist > 30) {
370                 strm->msg = (char *)"too many     370                 strm->msg = (char *)"too many length or distance symbols";
371                 state->mode = BAD;                371                 state->mode = BAD;
372                 break;                            372                 break;
373             }                                     373             }
374 #endif                                            374 #endif
375             Tracev((stderr, "inflate:       ta    375             Tracev((stderr, "inflate:       table sizes ok\n"));
376                                                   376 
377             /* get code length code lengths (n    377             /* get code length code lengths (not a typo) */
378             state->have = 0;                      378             state->have = 0;
379             while (state->have < state->ncode)    379             while (state->have < state->ncode) {
380                 NEEDBITS(3);                      380                 NEEDBITS(3);
381                 state->lens[order[state->have+    381                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
382                 DROPBITS(3);                      382                 DROPBITS(3);
383             }                                     383             }
384             while (state->have < 19)              384             while (state->have < 19)
385                 state->lens[order[state->have+    385                 state->lens[order[state->have++]] = 0;
386             state->next = state->codes;           386             state->next = state->codes;
387             state->lencode = (code const FAR *    387             state->lencode = (code const FAR *)(state->next);
388             state->lenbits = 7;                   388             state->lenbits = 7;
389             ret = inflate_table(CODES, state->    389             ret = inflate_table(CODES, state->lens, 19, &(state->next),
390                                 &(state->lenbi    390                                 &(state->lenbits), state->work);
391             if (ret) {                            391             if (ret) {
392                 strm->msg = (char *)"invalid c    392                 strm->msg = (char *)"invalid code lengths set";
393                 state->mode = BAD;                393                 state->mode = BAD;
394                 break;                            394                 break;
395             }                                     395             }
396             Tracev((stderr, "inflate:       co    396             Tracev((stderr, "inflate:       code lengths ok\n"));
397                                                   397 
398             /* get length and distance code co    398             /* get length and distance code code lengths */
399             state->have = 0;                      399             state->have = 0;
400             while (state->have < state->nlen +    400             while (state->have < state->nlen + state->ndist) {
401                 for (;;) {                        401                 for (;;) {
402                     here = state->lencode[BITS    402                     here = state->lencode[BITS(state->lenbits)];
403                     if ((unsigned)(here.bits)     403                     if ((unsigned)(here.bits) <= bits) break;
404                     PULLBYTE();                   404                     PULLBYTE();
405                 }                                 405                 }
406                 if (here.val < 16) {              406                 if (here.val < 16) {
407                     DROPBITS(here.bits);          407                     DROPBITS(here.bits);
408                     state->lens[state->have++]    408                     state->lens[state->have++] = here.val;
409                 }                                 409                 }
410                 else {                            410                 else {
411                     if (here.val == 16) {         411                     if (here.val == 16) {
412                         NEEDBITS(here.bits + 2    412                         NEEDBITS(here.bits + 2);
413                         DROPBITS(here.bits);      413                         DROPBITS(here.bits);
414                         if (state->have == 0)     414                         if (state->have == 0) {
415                             strm->msg = (char     415                             strm->msg = (char *)"invalid bit length repeat";
416                             state->mode = BAD;    416                             state->mode = BAD;
417                             break;                417                             break;
418                         }                         418                         }
419                         len = (unsigned)(state    419                         len = (unsigned)(state->lens[state->have - 1]);
420                         copy = 3 + BITS(2);       420                         copy = 3 + BITS(2);
421                         DROPBITS(2);              421                         DROPBITS(2);
422                     }                             422                     }
423                     else if (here.val == 17) {    423                     else if (here.val == 17) {
424                         NEEDBITS(here.bits + 3    424                         NEEDBITS(here.bits + 3);
425                         DROPBITS(here.bits);      425                         DROPBITS(here.bits);
426                         len = 0;                  426                         len = 0;
427                         copy = 3 + BITS(3);       427                         copy = 3 + BITS(3);
428                         DROPBITS(3);              428                         DROPBITS(3);
429                     }                             429                     }
430                     else {                        430                     else {
431                         NEEDBITS(here.bits + 7    431                         NEEDBITS(here.bits + 7);
432                         DROPBITS(here.bits);      432                         DROPBITS(here.bits);
433                         len = 0;                  433                         len = 0;
434                         copy = 11 + BITS(7);      434                         copy = 11 + BITS(7);
435                         DROPBITS(7);              435                         DROPBITS(7);
436                     }                             436                     }
437                     if (state->have + copy > s    437                     if (state->have + copy > state->nlen + state->ndist) {
438                         strm->msg = (char *)"i    438                         strm->msg = (char *)"invalid bit length repeat";
439                         state->mode = BAD;        439                         state->mode = BAD;
440                         break;                    440                         break;
441                     }                             441                     }
442                     while (copy--)                442                     while (copy--)
443                         state->lens[state->hav    443                         state->lens[state->have++] = (unsigned short)len;
444                 }                                 444                 }
445             }                                     445             }
446                                                   446 
447             /* handle error breaks in while */    447             /* handle error breaks in while */
448             if (state->mode == BAD) break;        448             if (state->mode == BAD) break;
449                                                   449 
450             /* check for end-of-block code (be    450             /* check for end-of-block code (better have one) */
451             if (state->lens[256] == 0) {          451             if (state->lens[256] == 0) {
452                 strm->msg = (char *)"invalid c    452                 strm->msg = (char *)"invalid code -- missing end-of-block";
453                 state->mode = BAD;                453                 state->mode = BAD;
454                 break;                            454                 break;
455             }                                     455             }
456                                                   456 
457             /* build code tables -- note: do n    457             /* build code tables -- note: do not change the lenbits or distbits
458                values here (9 and 6) without r    458                values here (9 and 6) without reading the comments in inftrees.h
459                concerning the ENOUGH constants    459                concerning the ENOUGH constants, which depend on those values */
460             state->next = state->codes;           460             state->next = state->codes;
461             state->lencode = (code const FAR *    461             state->lencode = (code const FAR *)(state->next);
462             state->lenbits = 9;                   462             state->lenbits = 9;
463             ret = inflate_table(LENS, state->l    463             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
464                                 &(state->lenbi    464                                 &(state->lenbits), state->work);
465             if (ret) {                            465             if (ret) {
466                 strm->msg = (char *)"invalid l    466                 strm->msg = (char *)"invalid literal/lengths set";
467                 state->mode = BAD;                467                 state->mode = BAD;
468                 break;                            468                 break;
469             }                                     469             }
470             state->distcode = (code const FAR     470             state->distcode = (code const FAR *)(state->next);
471             state->distbits = 6;                  471             state->distbits = 6;
472             ret = inflate_table(DISTS, state->    472             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
473                             &(state->next), &(    473                             &(state->next), &(state->distbits), state->work);
474             if (ret) {                            474             if (ret) {
475                 strm->msg = (char *)"invalid d    475                 strm->msg = (char *)"invalid distances set";
476                 state->mode = BAD;                476                 state->mode = BAD;
477                 break;                            477                 break;
478             }                                     478             }
479             Tracev((stderr, "inflate:       co    479             Tracev((stderr, "inflate:       codes ok\n"));
480             state->mode = LEN;                    480             state->mode = LEN;
481                 /* fallthrough */                 481                 /* fallthrough */
482                                                   482 
483         case LEN:                                 483         case LEN:
484             /* use inflate_fast() if we have e    484             /* use inflate_fast() if we have enough input and output */
485             if (have >= 6 && left >= 258) {       485             if (have >= 6 && left >= 258) {
486                 RESTORE();                        486                 RESTORE();
487                 if (state->whave < state->wsiz    487                 if (state->whave < state->wsize)
488                     state->whave = state->wsiz    488                     state->whave = state->wsize - left;
489                 inflate_fast(strm, state->wsiz    489                 inflate_fast(strm, state->wsize);
490                 LOAD();                           490                 LOAD();
491                 break;                            491                 break;
492             }                                     492             }
493                                                   493 
494             /* get a literal, length, or end-o    494             /* get a literal, length, or end-of-block code */
495             for (;;) {                            495             for (;;) {
496                 here = state->lencode[BITS(sta    496                 here = state->lencode[BITS(state->lenbits)];
497                 if ((unsigned)(here.bits) <= b    497                 if ((unsigned)(here.bits) <= bits) break;
498                 PULLBYTE();                       498                 PULLBYTE();
499             }                                     499             }
500             if (here.op && (here.op & 0xf0) ==    500             if (here.op && (here.op & 0xf0) == 0) {
501                 last = here;                      501                 last = here;
502                 for (;;) {                        502                 for (;;) {
503                     here = state->lencode[last    503                     here = state->lencode[last.val +
504                             (BITS(last.bits +     504                             (BITS(last.bits + last.op) >> last.bits)];
505                     if ((unsigned)(last.bits +    505                     if ((unsigned)(last.bits + here.bits) <= bits) break;
506                     PULLBYTE();                   506                     PULLBYTE();
507                 }                                 507                 }
508                 DROPBITS(last.bits);              508                 DROPBITS(last.bits);
509             }                                     509             }
510             DROPBITS(here.bits);                  510             DROPBITS(here.bits);
511             state->length = (unsigned)here.val    511             state->length = (unsigned)here.val;
512                                                   512 
513             /* process literal */                 513             /* process literal */
514             if (here.op == 0) {                   514             if (here.op == 0) {
515                 Tracevv((stderr, here.val >= 0    515                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
516                         "inflate:         lite    516                         "inflate:         literal '%c'\n" :
517                         "inflate:         lite    517                         "inflate:         literal 0x%02x\n", here.val));
518                 ROOM();                           518                 ROOM();
519                 *put++ = (unsigned char)(state    519                 *put++ = (unsigned char)(state->length);
520                 left--;                           520                 left--;
521                 state->mode = LEN;                521                 state->mode = LEN;
522                 break;                            522                 break;
523             }                                     523             }
524                                                   524 
525             /* process end of block */            525             /* process end of block */
526             if (here.op & 32) {                   526             if (here.op & 32) {
527                 Tracevv((stderr, "inflate:        527                 Tracevv((stderr, "inflate:         end of block\n"));
528                 state->mode = TYPE;               528                 state->mode = TYPE;
529                 break;                            529                 break;
530             }                                     530             }
531                                                   531 
532             /* invalid code */                    532             /* invalid code */
533             if (here.op & 64) {                   533             if (here.op & 64) {
534                 strm->msg = (char *)"invalid l    534                 strm->msg = (char *)"invalid literal/length code";
535                 state->mode = BAD;                535                 state->mode = BAD;
536                 break;                            536                 break;
537             }                                     537             }
538                                                   538 
539             /* length code -- get extra bits,     539             /* length code -- get extra bits, if any */
540             state->extra = (unsigned)(here.op)    540             state->extra = (unsigned)(here.op) & 15;
541             if (state->extra != 0) {              541             if (state->extra != 0) {
542                 NEEDBITS(state->extra);           542                 NEEDBITS(state->extra);
543                 state->length += BITS(state->e    543                 state->length += BITS(state->extra);
544                 DROPBITS(state->extra);           544                 DROPBITS(state->extra);
545             }                                     545             }
546             Tracevv((stderr, "inflate:            546             Tracevv((stderr, "inflate:         length %u\n", state->length));
547                                                   547 
548             /* get distance code */               548             /* get distance code */
549             for (;;) {                            549             for (;;) {
550                 here = state->distcode[BITS(st    550                 here = state->distcode[BITS(state->distbits)];
551                 if ((unsigned)(here.bits) <= b    551                 if ((unsigned)(here.bits) <= bits) break;
552                 PULLBYTE();                       552                 PULLBYTE();
553             }                                     553             }
554             if ((here.op & 0xf0) == 0) {          554             if ((here.op & 0xf0) == 0) {
555                 last = here;                      555                 last = here;
556                 for (;;) {                        556                 for (;;) {
557                     here = state->distcode[las    557                     here = state->distcode[last.val +
558                             (BITS(last.bits +     558                             (BITS(last.bits + last.op) >> last.bits)];
559                     if ((unsigned)(last.bits +    559                     if ((unsigned)(last.bits + here.bits) <= bits) break;
560                     PULLBYTE();                   560                     PULLBYTE();
561                 }                                 561                 }
562                 DROPBITS(last.bits);              562                 DROPBITS(last.bits);
563             }                                     563             }
564             DROPBITS(here.bits);                  564             DROPBITS(here.bits);
565             if (here.op & 64) {                   565             if (here.op & 64) {
566                 strm->msg = (char *)"invalid d    566                 strm->msg = (char *)"invalid distance code";
567                 state->mode = BAD;                567                 state->mode = BAD;
568                 break;                            568                 break;
569             }                                     569             }
570             state->offset = (unsigned)here.val    570             state->offset = (unsigned)here.val;
571                                                   571 
572             /* get distance extra bits, if any    572             /* get distance extra bits, if any */
573             state->extra = (unsigned)(here.op)    573             state->extra = (unsigned)(here.op) & 15;
574             if (state->extra != 0) {              574             if (state->extra != 0) {
575                 NEEDBITS(state->extra);           575                 NEEDBITS(state->extra);
576                 state->offset += BITS(state->e    576                 state->offset += BITS(state->extra);
577                 DROPBITS(state->extra);           577                 DROPBITS(state->extra);
578             }                                     578             }
579             if (state->offset > state->wsize -    579             if (state->offset > state->wsize - (state->whave < state->wsize ?
580                                                   580                                                 left : 0)) {
581                 strm->msg = (char *)"invalid d    581                 strm->msg = (char *)"invalid distance too far back";
582                 state->mode = BAD;                582                 state->mode = BAD;
583                 break;                            583                 break;
584             }                                     584             }
585             Tracevv((stderr, "inflate:            585             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
586                                                   586 
587             /* copy match from window to outpu    587             /* copy match from window to output */
588             do {                                  588             do {
589                 ROOM();                           589                 ROOM();
590                 copy = state->wsize - state->o    590                 copy = state->wsize - state->offset;
591                 if (copy < left) {                591                 if (copy < left) {
592                     from = put + copy;            592                     from = put + copy;
593                     copy = left - copy;           593                     copy = left - copy;
594                 }                                 594                 }
595                 else {                            595                 else {
596                     from = put - state->offset    596                     from = put - state->offset;
597                     copy = left;                  597                     copy = left;
598                 }                                 598                 }
599                 if (copy > state->length) copy    599                 if (copy > state->length) copy = state->length;
600                 state->length -= copy;            600                 state->length -= copy;
601                 left -= copy;                     601                 left -= copy;
602                 do {                              602                 do {
603                     *put++ = *from++;             603                     *put++ = *from++;
604                 } while (--copy);                 604                 } while (--copy);
605             } while (state->length != 0);         605             } while (state->length != 0);
606             break;                                606             break;
607                                                   607 
608         case DONE:                                608         case DONE:
609             /* inflate stream terminated prope    609             /* inflate stream terminated properly */
610             ret = Z_STREAM_END;                   610             ret = Z_STREAM_END;
611             goto inf_leave;                       611             goto inf_leave;
612                                                   612 
613         case BAD:                                 613         case BAD:
614             ret = Z_DATA_ERROR;                   614             ret = Z_DATA_ERROR;
615             goto inf_leave;                       615             goto inf_leave;
616                                                   616 
617         default:                                  617         default:
618             /* can't happen, but makes compile    618             /* can't happen, but makes compilers happy */
619             ret = Z_STREAM_ERROR;                 619             ret = Z_STREAM_ERROR;
620             goto inf_leave;                       620             goto inf_leave;
621         }                                         621         }
622                                                   622 
623     /* Write leftover output and return unused    623     /* Write leftover output and return unused input */
624   inf_leave:                                      624   inf_leave:
625     if (left < state->wsize) {                    625     if (left < state->wsize) {
626         if (out(out_desc, state->window, state    626         if (out(out_desc, state->window, state->wsize - left) &&
627             ret == Z_STREAM_END)                  627             ret == Z_STREAM_END)
628             ret = Z_BUF_ERROR;                    628             ret = Z_BUF_ERROR;
629     }                                             629     }
630     strm->next_in = next;                         630     strm->next_in = next;
631     strm->avail_in = have;                        631     strm->avail_in = have;
632     return ret;                                   632     return ret;
633 }                                                 633 }
634                                                   634 
635 int ZEXPORT inflateBackEnd(strm)                  635 int ZEXPORT inflateBackEnd(strm)
636 z_streamp strm;                                   636 z_streamp strm;
637 {                                                 637 {
638     if (strm == Z_NULL || strm->state == Z_NUL    638     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
639         return Z_STREAM_ERROR;                    639         return Z_STREAM_ERROR;
640     ZFREE(strm, strm->state);                     640     ZFREE(strm, strm->state);
641     strm->state = Z_NULL;                         641     strm->state = Z_NULL;
642     Tracev((stderr, "inflate: end\n"));           642     Tracev((stderr, "inflate: end\n"));
643     return Z_OK;                                  643     return Z_OK;
644 }                                                 644 }
645                                                   645