Line  | Count  | Source  | 
1  |  | /* inflate.c -- zlib decompression  | 
2  |  |  * Copyright (C) 1995-2022 Mark Adler  | 
3  |  |  * For conditions of distribution and use, see copyright notice in zlib.h  | 
4  |  |  */  | 
5  |  |  | 
6  |  | #include "zbuild.h"  | 
7  |  | #include "zutil.h"  | 
8  |  | #include "inftrees.h"  | 
9  |  | #include "inflate.h"  | 
10  |  | #include "inflate_p.h"  | 
11  |  | #include "inffixed_tbl.h"  | 
12  |  | #include "functable.h"  | 
13  |  |  | 
14  |  | /* Avoid conflicts with zlib.h macros */  | 
15  |  | #ifdef ZLIB_COMPAT  | 
16  |  | # undef inflateInit  | 
17  |  | # undef inflateInit2  | 
18  |  | #endif  | 
19  |  |  | 
20  |  | /* function prototypes */  | 
21  |  | static int inflateStateCheck(PREFIX3(stream) *strm);  | 
22  |  | static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum);  | 
23  |  | static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);  | 
24  |  |  | 
25  |  | static inline void inf_chksum_cpy(PREFIX3(stream) *strm, uint8_t *dst,  | 
26  | 0  |                            const uint8_t *src, uint32_t copy) { | 
27  | 0  |     if (!copy) return;  | 
28  | 0  |     struct inflate_state *state = (struct inflate_state*)strm->state;  | 
29  | 0  | #ifdef GUNZIP  | 
30  | 0  |     if (state->flags) { | 
31  | 0  |         FUNCTABLE_CALL(crc32_fold_copy)(&state->crc_fold, dst, src, copy);  | 
32  | 0  |     } else  | 
33  | 0  | #endif  | 
34  | 0  |     { | 
35  | 0  |         strm->adler = state->check = FUNCTABLE_CALL(adler32_fold_copy)(state->check, dst, src, copy);  | 
36  | 0  |     }  | 
37  | 0  | }  | 
38  |  |  | 
39  | 0  | static inline void inf_chksum(PREFIX3(stream) *strm, const uint8_t *src, uint32_t len) { | 
40  | 0  |     struct inflate_state *state = (struct inflate_state*)strm->state;  | 
41  | 0  | #ifdef GUNZIP  | 
42  | 0  |     if (state->flags) { | 
43  | 0  |         FUNCTABLE_CALL(crc32_fold)(&state->crc_fold, src, len, 0);  | 
44  | 0  |     } else  | 
45  | 0  | #endif  | 
46  | 0  |     { | 
47  | 0  |         strm->adler = state->check = FUNCTABLE_CALL(adler32)(state->check, src, len);  | 
48  | 0  |     }  | 
49  | 0  | }  | 
50  |  |  | 
51  | 26.8k  | static int inflateStateCheck(PREFIX3(stream) *strm) { | 
52  | 26.8k  |     struct inflate_state *state;  | 
53  | 26.8k  |     if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)  | 
54  | 0  |         return 1;  | 
55  | 26.8k  |     state = (struct inflate_state *)strm->state;  | 
56  | 26.8k  |     if (state == NULL || state->alloc_bufs == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)  | 
57  | 0  |         return 1;  | 
58  | 26.8k  |     return 0;  | 
59  | 26.8k  | }  | 
60  |  |  | 
61  | 5.97k  | int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) { | 
62  | 5.97k  |     struct inflate_state *state;  | 
63  |  |  | 
64  | 5.97k  |     if (inflateStateCheck(strm))  | 
65  | 0  |         return Z_STREAM_ERROR;  | 
66  | 5.97k  |     state = (struct inflate_state *)strm->state;  | 
67  | 5.97k  |     strm->total_in = strm->total_out = state->total = 0;  | 
68  | 5.97k  |     strm->msg = NULL;  | 
69  | 5.97k  |     if (state->wrap)        /* to support ill-conceived Java test suite */  | 
70  | 5.97k  |         strm->adler = state->wrap & 1;  | 
71  | 5.97k  |     state->mode = HEAD;  | 
72  | 5.97k  |     state->check = ADLER32_INITIAL_VALUE;  | 
73  | 5.97k  |     state->last = 0;  | 
74  | 5.97k  |     state->havedict = 0;  | 
75  | 5.97k  |     state->flags = -1;  | 
76  | 5.97k  |     state->head = NULL;  | 
77  | 5.97k  |     state->hold = 0;  | 
78  | 5.97k  |     state->bits = 0;  | 
79  | 5.97k  |     state->lencode = state->distcode = state->next = state->codes;  | 
80  | 5.97k  |     state->back = -1;  | 
81  |  | #ifdef INFLATE_STRICT  | 
82  |  |     state->dmax = 32768U;  | 
83  |  | #endif  | 
84  |  | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR  | 
85  |  |     state->sane = 1;  | 
86  |  | #endif  | 
87  | 5.97k  |     INFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */  | 
88  | 5.97k  |     Tracev((stderr, "inflate: reset\n"));  | 
89  | 5.97k  |     return Z_OK;  | 
90  | 5.97k  | }  | 
91  |  |  | 
92  | 5.97k  | int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) { | 
93  | 5.97k  |     struct inflate_state *state;  | 
94  |  |  | 
95  | 5.97k  |     if (inflateStateCheck(strm))  | 
96  | 0  |         return Z_STREAM_ERROR;  | 
97  | 5.97k  |     state = (struct inflate_state *)strm->state;  | 
98  | 5.97k  |     state->wsize = 0;  | 
99  | 5.97k  |     state->whave = 0;  | 
100  | 5.97k  |     state->wnext = 0;  | 
101  | 5.97k  |     return PREFIX(inflateResetKeep)(strm);  | 
102  | 5.97k  | }  | 
103  |  |  | 
104  | 2.98k  | int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) { | 
105  | 2.98k  |     int wrap;  | 
106  | 2.98k  |     struct inflate_state *state;  | 
107  |  |  | 
108  |  |     /* get the state */  | 
109  | 2.98k  |     if (inflateStateCheck(strm))  | 
110  | 0  |         return Z_STREAM_ERROR;  | 
111  | 2.98k  |     state = (struct inflate_state *)strm->state;  | 
112  |  |  | 
113  |  |     /* extract wrap request from windowBits parameter */  | 
114  | 2.98k  |     if (windowBits < 0) { | 
115  | 0  |         wrap = 0;  | 
116  | 0  |         if (windowBits < -MAX_WBITS)  | 
117  | 0  |             return Z_STREAM_ERROR;  | 
118  | 0  |         windowBits = -windowBits;  | 
119  | 2.98k  |     } else { | 
120  | 2.98k  |         wrap = (windowBits >> 4) + 5;  | 
121  | 2.98k  | #ifdef GUNZIP  | 
122  | 2.98k  |         if (windowBits < 48)  | 
123  | 2.98k  |             windowBits &= MAX_WBITS;  | 
124  | 2.98k  | #endif  | 
125  | 2.98k  |     }  | 
126  |  |  | 
127  |  |     /* set number of window bits */  | 
128  | 2.98k  |     if (windowBits && (windowBits < MIN_WBITS || windowBits > MAX_WBITS))  | 
129  | 0  |         return Z_STREAM_ERROR;  | 
130  |  |  | 
131  |  |     /* update state and reset the rest of it */  | 
132  | 2.98k  |     state->wrap = wrap;  | 
133  | 2.98k  |     state->wbits = (unsigned)windowBits;  | 
134  | 2.98k  |     return PREFIX(inflateReset)(strm);  | 
135  | 2.98k  | }  | 
136  |  |  | 
137  |  | #ifdef INF_ALLOC_DEBUG  | 
138  |  | #  include <stdio.h>  | 
139  |  | #  define LOGSZ(name,size)           fprintf(stderr, "%s is %d bytes\n", name, size)  | 
140  |  | #  define LOGSZP(name,size,loc,pad)  fprintf(stderr, "%s is %d bytes, offset %d, padded %d\n", name, size, loc, pad)  | 
141  |  | #  define LOGSZPL(name,size,loc,pad) fprintf(stderr, "%s is %d bytes, offset %ld, padded %d\n", name, size, loc, pad)  | 
142  |  | #else  | 
143  |  | #  define LOGSZ(name,size)  | 
144  |  | #  define LOGSZP(name,size,loc,pad)  | 
145  |  | #  define LOGSZPL(name,size,loc,pad)  | 
146  |  | #endif  | 
147  |  |  | 
148  |  | /* ===========================================================================  | 
149  |  |  * Allocate a big buffer and divide it up into the various buffers inflate needs.  | 
150  |  |  * Handles alignment of allocated buffer and alignment of individual buffers.  | 
151  |  |  */  | 
152  | 2.98k  | Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm) { | 
153  | 2.98k  |     int curr_size = 0;  | 
154  |  |  | 
155  |  |     /* Define sizes */  | 
156  | 2.98k  |     int window_size = INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64); /* 64B padding for chunksize */  | 
157  | 2.98k  |     int state_size = sizeof(inflate_state);  | 
158  | 2.98k  |     int alloc_size = sizeof(inflate_allocs);  | 
159  |  |  | 
160  |  |     /* Calculate relative buffer positions and paddings */  | 
161  | 2.98k  |     LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size,WINDOW_PAD_SIZE)); | 
162  | 2.98k  |     int window_pos = PAD_WINDOW(curr_size);  | 
163  | 2.98k  |     curr_size = window_pos + window_size;  | 
164  |  |  | 
165  | 2.98k  |     LOGSZP("state", state_size, PAD_64(curr_size), PADSZ(curr_size,64)); | 
166  | 2.98k  |     int state_pos = PAD_64(curr_size);  | 
167  | 2.98k  |     curr_size = state_pos + state_size;  | 
168  |  |  | 
169  | 2.98k  |     LOGSZP("alloc", alloc_size, PAD_16(curr_size), PADSZ(curr_size,16)); | 
170  | 2.98k  |     int alloc_pos = PAD_16(curr_size);  | 
171  | 2.98k  |     curr_size = alloc_pos + alloc_size;  | 
172  |  |  | 
173  |  |     /* Add 64-1 or 4096-1 to allow window alignment, and round size of buffer up to multiple of 64 */  | 
174  | 2.98k  |     int total_size = PAD_64(curr_size + (WINDOW_PAD_SIZE - 1));  | 
175  |  |  | 
176  |  |     /* Allocate buffer, align to 64-byte cacheline, and zerofill the resulting buffer */  | 
177  | 2.98k  |     char *original_buf = (char *)strm->zalloc(strm->opaque, 1, total_size);  | 
178  | 2.98k  |     if (original_buf == NULL)  | 
179  | 0  |         return NULL;  | 
180  |  |  | 
181  | 2.98k  |     char *buff = (char *)HINT_ALIGNED_WINDOW((char *)PAD_WINDOW(original_buf));  | 
182  | 2.98k  |     LOGSZPL("Buffer alloc", total_size, PADSZ((uintptr_t)original_buf,WINDOW_PAD_SIZE), PADSZ(curr_size,WINDOW_PAD_SIZE)); | 
183  |  |  | 
184  |  |     /* Initialize alloc_bufs */  | 
185  | 2.98k  |     inflate_allocs *alloc_bufs  = (struct inflate_allocs_s *)(buff + alloc_pos);  | 
186  | 2.98k  |     alloc_bufs->buf_start = original_buf;  | 
187  | 2.98k  |     alloc_bufs->zfree = strm->zfree;  | 
188  |  |  | 
189  | 2.98k  |     alloc_bufs->window =  (unsigned char *)HINT_ALIGNED_WINDOW((buff + window_pos));  | 
190  | 2.98k  |     alloc_bufs->state = (inflate_state *)HINT_ALIGNED_64((buff + state_pos));  | 
191  |  |  | 
192  |  | #ifdef Z_MEMORY_SANITIZER  | 
193  |  |     /* This is _not_ to subvert the memory sanitizer but to instead unposion some  | 
194  |  |        data we willingly and purposefully load uninitialized into vector registers  | 
195  |  |        in order to safely read the last < chunksize bytes of the window. */  | 
196  |  |     __msan_unpoison(alloc_bufs->window + window_size, 64);  | 
197  |  | #endif  | 
198  |  |  | 
199  | 2.98k  |     return alloc_bufs;  | 
200  | 2.98k  | }  | 
201  |  |  | 
202  |  | /* ===========================================================================  | 
203  |  |  * Free all allocated inflate buffers  | 
204  |  |  */  | 
205  | 2.98k  | Z_INTERNAL void free_inflate(PREFIX3(stream) *strm) { | 
206  | 2.98k  |     struct inflate_state *state = (struct inflate_state *)strm->state;  | 
207  |  |  | 
208  | 2.98k  |     if (state->alloc_bufs != NULL) { | 
209  | 2.98k  |         inflate_allocs *alloc_bufs = state->alloc_bufs;  | 
210  | 2.98k  |         alloc_bufs->zfree(strm->opaque, alloc_bufs->buf_start);  | 
211  | 2.98k  |         strm->state = NULL;  | 
212  | 2.98k  |     }  | 
213  | 2.98k  | }  | 
214  |  |  | 
215  |  | /* ===========================================================================  | 
216  |  |  * Initialize inflate state and buffers.  | 
217  |  |  * This function is hidden in ZLIB_COMPAT builds.  | 
218  |  |  */  | 
219  | 2.98k  | int32_t ZNG_CONDEXPORT PREFIX(inflateInit2)(PREFIX3(stream) *strm, int32_t windowBits) { | 
220  | 2.98k  |     struct inflate_state *state;  | 
221  | 2.98k  |     int32_t ret;  | 
222  |  |  | 
223  |  |     /* Initialize functable */  | 
224  | 2.98k  |     FUNCTABLE_INIT;  | 
225  |  |  | 
226  | 2.98k  |     if (strm == NULL)  | 
227  | 0  |         return Z_STREAM_ERROR;  | 
228  | 2.98k  |     strm->msg = NULL;                   /* in case we return an error */  | 
229  | 2.98k  |     if (strm->zalloc == NULL) { | 
230  | 2.98k  |         strm->zalloc = PREFIX(zcalloc);  | 
231  | 2.98k  |         strm->opaque = NULL;  | 
232  | 2.98k  |     }  | 
233  | 2.98k  |     if (strm->zfree == NULL)  | 
234  | 2.98k  |         strm->zfree = PREFIX(zcfree);  | 
235  |  |  | 
236  | 2.98k  |     inflate_allocs *alloc_bufs = alloc_inflate(strm);  | 
237  | 2.98k  |     if (alloc_bufs == NULL)  | 
238  | 0  |         return Z_MEM_ERROR;  | 
239  |  |  | 
240  | 2.98k  |     state = alloc_bufs->state;  | 
241  | 2.98k  |     state->window = alloc_bufs->window;  | 
242  | 2.98k  |     state->alloc_bufs = alloc_bufs;  | 
243  | 2.98k  |     state->wbufsize = INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64);  | 
244  | 2.98k  |     Tracev((stderr, "inflate: allocated\n"));  | 
245  |  |  | 
246  | 2.98k  |     strm->state = (struct internal_state *)state;  | 
247  | 2.98k  |     state->strm = strm;  | 
248  | 2.98k  |     state->mode = HEAD;     /* to pass state test in inflateReset2() */  | 
249  | 2.98k  |     ret = PREFIX(inflateReset2)(strm, windowBits);  | 
250  | 2.98k  |     if (ret != Z_OK) { | 
251  | 0  |         free_inflate(strm);  | 
252  | 0  |     }  | 
253  | 2.98k  |     return ret;  | 
254  | 2.98k  | }  | 
255  |  |  | 
256  |  | #ifndef ZLIB_COMPAT  | 
257  | 2.98k  | int32_t Z_EXPORT PREFIX(inflateInit)(PREFIX3(stream) *strm) { | 
258  | 2.98k  |     return PREFIX(inflateInit2)(strm, DEF_WBITS);  | 
259  | 2.98k  | }  | 
260  |  | #endif  | 
261  |  |  | 
262  |  | /* Function used by zlib.h and zlib-ng version 2.0 macros */  | 
263  | 0  | int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) { | 
264  | 0  |     if (CHECK_VER_STSIZE(version, stream_size))  | 
265  | 0  |         return Z_VERSION_ERROR;  | 
266  | 0  |     return PREFIX(inflateInit2)(strm, DEF_WBITS);  | 
267  | 0  | }  | 
268  |  |  | 
269  |  | /* Function used by zlib.h and zlib-ng version 2.0 macros */  | 
270  | 0  | int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) { | 
271  | 0  |     if (CHECK_VER_STSIZE(version, stream_size))  | 
272  | 0  |         return Z_VERSION_ERROR;  | 
273  | 0  |     return PREFIX(inflateInit2)(strm, windowBits);  | 
274  | 0  | }  | 
275  |  |  | 
276  | 0  | int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) { | 
277  | 0  |     struct inflate_state *state;  | 
278  |  | 
  | 
279  | 0  |     if (inflateStateCheck(strm))  | 
280  | 0  |         return Z_STREAM_ERROR;  | 
281  | 0  |     if (bits == 0)  | 
282  | 0  |         return Z_OK;  | 
283  | 0  |     INFLATE_PRIME_HOOK(strm, bits, value);  /* hook for IBM Z DFLTCC */  | 
284  | 0  |     state = (struct inflate_state *)strm->state;  | 
285  | 0  |     if (bits < 0) { | 
286  | 0  |         state->hold = 0;  | 
287  | 0  |         state->bits = 0;  | 
288  | 0  |         return Z_OK;  | 
289  | 0  |     }  | 
290  | 0  |     if (bits > 16 || state->bits + (unsigned int)bits > 32)  | 
291  | 0  |         return Z_STREAM_ERROR;  | 
292  | 0  |     value &= (1L << bits) - 1;  | 
293  | 0  |     state->hold += (uint64_t)value << state->bits;  | 
294  | 0  |     state->bits += (unsigned int)bits;  | 
295  | 0  |     return Z_OK;  | 
296  | 0  | }  | 
297  |  |  | 
298  |  | /*  | 
299  |  |    Return state with length and distance decoding tables and index sizes set to  | 
300  |  |    fixed code decoding.  This returns fixed tables from inffixed_tbl.h.  | 
301  |  |  */  | 
302  |  |  | 
303  | 1.43k  | void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state) { | 
304  | 1.43k  |     state->lencode = lenfix;  | 
305  | 1.43k  |     state->lenbits = 9;  | 
306  | 1.43k  |     state->distcode = distfix;  | 
307  | 1.43k  |     state->distbits = 5;  | 
308  | 1.43k  | }  | 
309  |  |  | 
310  |  | /*  | 
311  |  |    Update the window with the last wsize (normally 32K) bytes written before  | 
312  |  |    returning.  If window does not exist yet, create it.  This is only called  | 
313  |  |    when a window is already in use, or when output has been written during this  | 
314  |  |    inflate call, but the end of the deflate stream has not been reached yet.  | 
315  |  |    It is also called to create a window for dictionary data when a dictionary  | 
316  |  |    is loaded.  | 
317  |  |  | 
318  |  |    Providing output buffers larger than 32K to inflate() should provide a speed  | 
319  |  |    advantage, since only the last 32K of output is copied to the sliding window  | 
320  |  |    upon return from inflate(), and since all distances after the first 32K of  | 
321  |  |    output will fall in the output data, making match copies simpler and faster.  | 
322  |  |    The advantage may be dependent on the size of the processor's data caches.  | 
323  |  |  */  | 
324  | 0  | static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum) { | 
325  | 0  |     struct inflate_state *state;  | 
326  | 0  |     uint32_t dist;  | 
327  |  | 
  | 
328  | 0  |     state = (struct inflate_state *)strm->state;  | 
329  |  |  | 
330  |  |     /* if window not in use yet, initialize */  | 
331  | 0  |     if (state->wsize == 0)  | 
332  | 0  |         state->wsize = 1U << state->wbits;  | 
333  |  |  | 
334  |  |     /* len state->wsize or less output bytes into the circular window */  | 
335  | 0  |     if (len >= state->wsize) { | 
336  |  |         /* Only do this if the caller specifies to checksum bytes AND the platform requires  | 
337  |  |          * it (s/390 being the primary exception to this) */  | 
338  | 0  |         if (INFLATE_NEED_CHECKSUM(strm) && cksum) { | 
339  |  |             /* We have to split the checksum over non-copied and copied bytes */  | 
340  | 0  |             if (len > state->wsize)  | 
341  | 0  |                 inf_chksum(strm, end - len, len - state->wsize);  | 
342  | 0  |             inf_chksum_cpy(strm, state->window, end - state->wsize, state->wsize);  | 
343  | 0  |         } else { | 
344  | 0  |             memcpy(state->window, end - state->wsize, state->wsize);  | 
345  | 0  |         }  | 
346  |  | 
  | 
347  | 0  |         state->wnext = 0;  | 
348  | 0  |         state->whave = state->wsize;  | 
349  | 0  |     } else { | 
350  | 0  |         dist = state->wsize - state->wnext;  | 
351  |  |         /* Only do this if the caller specifies to checksum bytes AND the platform requires  | 
352  |  |          * We need to maintain the correct order here for the checksum */  | 
353  | 0  |         dist = MIN(dist, len);  | 
354  | 0  |         if (INFLATE_NEED_CHECKSUM(strm) && cksum) { | 
355  | 0  |             inf_chksum_cpy(strm, state->window + state->wnext, end - len, dist);  | 
356  | 0  |         } else { | 
357  | 0  |             memcpy(state->window + state->wnext, end - len, dist);  | 
358  | 0  |         }  | 
359  | 0  |         len -= dist;  | 
360  | 0  |         if (len) { | 
361  | 0  |             if (INFLATE_NEED_CHECKSUM(strm) && cksum) { | 
362  | 0  |                 inf_chksum_cpy(strm, state->window, end - len, len);  | 
363  | 0  |             } else { | 
364  | 0  |                 memcpy(state->window, end - len, len);  | 
365  | 0  |             }  | 
366  |  | 
  | 
367  | 0  |             state->wnext = len;  | 
368  | 0  |             state->whave = state->wsize;  | 
369  | 0  |         } else { | 
370  | 0  |             state->wnext += dist;  | 
371  | 0  |             if (state->wnext == state->wsize)  | 
372  | 0  |                 state->wnext = 0;  | 
373  | 0  |             if (state->whave < state->wsize)  | 
374  | 0  |                 state->whave += dist;  | 
375  | 0  |         }  | 
376  | 0  |     }  | 
377  | 0  | }  | 
378  |  |  | 
379  |  | /*  | 
380  |  |    Private macros for inflate()  | 
381  |  |    Look in inflate_p.h for macros shared with inflateBack()  | 
382  |  | */  | 
383  |  |  | 
384  |  | /* Get a byte of input into the bit accumulator, or return from inflate() if there is no input available. */  | 
385  |  | #define PULLBYTE() \  | 
386  | 663k  |     do { \ | 
387  | 663k  |         if (have == 0) goto inf_leave; \  | 
388  | 663k  |         have--; \  | 
389  | 660k  |         hold += ((uint64_t)(*next++) << bits); \  | 
390  | 660k  |         bits += 8; \  | 
391  | 660k  |     } while (0)  | 
392  |  |  | 
393  |  | /*  | 
394  |  |    inflate() uses a state machine to process as much input data and generate as  | 
395  |  |    much output data as possible before returning.  The state machine is  | 
396  |  |    structured roughly as follows:  | 
397  |  |  | 
398  |  |     for (;;) switch (state) { | 
399  |  |     ...  | 
400  |  |     case STATEn:  | 
401  |  |         if (not enough input data or output space to make progress)  | 
402  |  |             return;  | 
403  |  |         ... make progress ...  | 
404  |  |         state = STATEm;  | 
405  |  |         break;  | 
406  |  |     ...  | 
407  |  |     }  | 
408  |  |  | 
409  |  |    so when inflate() is called again, the same case is attempted again, and  | 
410  |  |    if the appropriate resources are provided, the machine proceeds to the  | 
411  |  |    next state.  The NEEDBITS() macro is usually the way the state evaluates  | 
412  |  |    whether it can proceed or should return.  NEEDBITS() does the return if  | 
413  |  |    the requested bits are not available.  The typical use of the BITS macros  | 
414  |  |    is:  | 
415  |  |  | 
416  |  |         NEEDBITS(n);  | 
417  |  |         ... do something with BITS(n) ...  | 
418  |  |         DROPBITS(n);  | 
419  |  |  | 
420  |  |    where NEEDBITS(n) either returns from inflate() if there isn't enough  | 
421  |  |    input left to load n bits into the accumulator, or it continues.  BITS(n)  | 
422  |  |    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops  | 
423  |  |    the low n bits off the accumulator.  INITBITS() clears the accumulator  | 
424  |  |    and sets the number of available bits to zero.  BYTEBITS() discards just  | 
425  |  |    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()  | 
426  |  |    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.  | 
427  |  |  | 
428  |  |    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return  | 
429  |  |    if there is no input available.  The decoding of variable length codes uses  | 
430  |  |    PULLBYTE() directly in order to pull just enough bytes to decode the next  | 
431  |  |    code, and no more.  | 
432  |  |  | 
433  |  |    Some states loop until they get enough input, making sure that enough  | 
434  |  |    state information is maintained to continue the loop where it left off  | 
435  |  |    if NEEDBITS() returns in the loop.  For example, want, need, and keep  | 
436  |  |    would all have to actually be part of the saved state in case NEEDBITS()  | 
437  |  |    returns:  | 
438  |  |  | 
439  |  |     case STATEw:  | 
440  |  |         while (want < need) { | 
441  |  |             NEEDBITS(n);  | 
442  |  |             keep[want++] = BITS(n);  | 
443  |  |             DROPBITS(n);  | 
444  |  |         }  | 
445  |  |         state = STATEx;  | 
446  |  |     case STATEx:  | 
447  |  |  | 
448  |  |    As shown above, if the next state is also the next case, then the break  | 
449  |  |    is omitted.  | 
450  |  |  | 
451  |  |    A state may also return if there is not enough output space available to  | 
452  |  |    complete that state.  Those states are copying stored data, writing a  | 
453  |  |    literal byte, and copying a matching string.  | 
454  |  |  | 
455  |  |    When returning, a "goto inf_leave" is used to update the total counters,  | 
456  |  |    update the check value, and determine whether any progress has been made  | 
457  |  |    during that inflate() call in order to return the proper return code.  | 
458  |  |    Progress is defined as a change in either strm->avail_in or strm->avail_out.  | 
459  |  |    When there is a window, goto inf_leave will update the window with the last  | 
460  |  |    output written.  If a goto inf_leave occurs in the middle of decompression  | 
461  |  |    and there is no window currently, goto inf_leave will create one and copy  | 
462  |  |    output to the window for the next call of inflate().  | 
463  |  |  | 
464  |  |    In this implementation, the flush parameter of inflate() only affects the  | 
465  |  |    return code (per zlib.h).  inflate() always writes as much as possible to  | 
466  |  |    strm->next_out, given the space available and the provided input--the effect  | 
467  |  |    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers  | 
468  |  |    the allocation of and copying into a sliding window until necessary, which  | 
469  |  |    provides the effect documented in zlib.h for Z_FINISH when the entire input  | 
470  |  |    stream available.  So the only thing the flush parameter actually does is:  | 
471  |  |    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it  | 
472  |  |    will return Z_BUF_ERROR if it has not reached the end of the stream.  | 
473  |  |  */  | 
474  |  |  | 
475  | 5.97k  | int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) { | 
476  | 5.97k  |     struct inflate_state *state;  | 
477  | 5.97k  |     const unsigned char *next;  /* next input */  | 
478  | 5.97k  |     unsigned char *put;         /* next output */  | 
479  | 5.97k  |     unsigned char *from;        /* where to copy match bytes from */  | 
480  | 5.97k  |     unsigned have, left;        /* available input and output */  | 
481  | 5.97k  |     uint64_t hold;              /* bit buffer */  | 
482  | 5.97k  |     unsigned bits;              /* bits in bit buffer */  | 
483  | 5.97k  |     uint32_t in, out;           /* save starting available input and output */  | 
484  | 5.97k  |     unsigned copy;              /* number of stored or match bytes to copy */  | 
485  | 5.97k  |     code here;                  /* current decoding table entry */  | 
486  | 5.97k  |     code last;                  /* parent table entry */  | 
487  | 5.97k  |     unsigned len;               /* length to copy for repeats, bits to drop */  | 
488  | 5.97k  |     int32_t ret;                /* return code */  | 
489  | 5.97k  | #ifdef GUNZIP  | 
490  | 5.97k  |     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */  | 
491  | 5.97k  | #endif  | 
492  | 5.97k  |     static const uint16_t order[19] = /* permutation of code lengths */  | 
493  | 5.97k  |         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 
494  |  |  | 
495  | 5.97k  |     if (inflateStateCheck(strm) || strm->next_out == NULL ||  | 
496  | 5.97k  |         (strm->next_in == NULL && strm->avail_in != 0))  | 
497  | 0  |         return Z_STREAM_ERROR;  | 
498  |  |  | 
499  | 5.97k  |     state = (struct inflate_state *)strm->state;  | 
500  | 5.97k  |     if (state->mode == TYPE)      /* skip check */  | 
501  | 2.98k  |         state->mode = TYPEDO;  | 
502  | 5.97k  |     LOAD();  | 
503  | 5.97k  |     in = have;  | 
504  | 5.97k  |     out = left;  | 
505  | 5.97k  |     ret = Z_OK;  | 
506  | 5.97k  |     for (;;)  | 
507  | 394k  |         switch (state->mode) { | 
508  | 2.98k  |         case HEAD:  | 
509  | 2.98k  |             if (state->wrap == 0) { | 
510  | 0  |                 state->mode = TYPEDO;  | 
511  | 0  |                 break;  | 
512  | 0  |             }  | 
513  | 2.98k  |             NEEDBITS(16);  | 
514  | 2.98k  | #ifdef GUNZIP  | 
515  | 2.98k  |             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */ | 
516  | 0  |                 if (state->wbits == 0)  | 
517  | 0  |                     state->wbits = MAX_WBITS;  | 
518  | 0  |                 state->check = CRC32_INITIAL_VALUE;  | 
519  | 0  |                 CRC2(state->check, hold);  | 
520  | 0  |                 INITBITS();  | 
521  | 0  |                 state->mode = FLAGS;  | 
522  | 0  |                 break;  | 
523  | 0  |             }  | 
524  | 2.98k  |             if (state->head != NULL)  | 
525  | 0  |                 state->head->done = -1;  | 
526  | 2.98k  |             if (!(state->wrap & 1) ||   /* check if zlib header allowed */  | 
527  |  | #else  | 
528  |  |             if (  | 
529  |  | #endif  | 
530  | 2.98k  |                 ((BITS(8) << 8) + (hold >> 8)) % 31) { | 
531  | 0  |                 SET_BAD("incorrect header check"); | 
532  | 0  |                 break;  | 
533  | 0  |             }  | 
534  | 2.98k  |             if (BITS(4) != Z_DEFLATED) { | 
535  | 0  |                 SET_BAD("unknown compression method"); | 
536  | 0  |                 break;  | 
537  | 0  |             }  | 
538  | 2.98k  |             DROPBITS(4);  | 
539  | 2.98k  |             len = BITS(4) + 8;  | 
540  | 2.98k  |             if (state->wbits == 0)  | 
541  | 0  |                 state->wbits = len;  | 
542  | 2.98k  |             if (len > MAX_WBITS || len > state->wbits) { | 
543  | 0  |                 SET_BAD("invalid window size"); | 
544  | 0  |                 break;  | 
545  | 0  |             }  | 
546  |  | #ifdef INFLATE_STRICT  | 
547  |  |             state->dmax = 1U << len;  | 
548  |  | #endif  | 
549  | 2.98k  |             state->flags = 0;               /* indicate zlib header */  | 
550  | 2.98k  |             Tracev((stderr, "inflate:   zlib header ok\n"));  | 
551  | 2.98k  |             strm->adler = state->check = ADLER32_INITIAL_VALUE;  | 
552  | 2.98k  |             state->mode = hold & 0x200 ? DICTID : TYPE;  | 
553  | 2.98k  |             INITBITS();  | 
554  | 2.98k  |             break;  | 
555  | 0  | #ifdef GUNZIP  | 
556  |  |  | 
557  | 0  |         case FLAGS:  | 
558  | 0  |             NEEDBITS(16);  | 
559  | 0  |             state->flags = (int)(hold);  | 
560  | 0  |             if ((state->flags & 0xff) != Z_DEFLATED) { | 
561  | 0  |                 SET_BAD("unknown compression method"); | 
562  | 0  |                 break;  | 
563  | 0  |             }  | 
564  | 0  |             if (state->flags & 0xe000) { | 
565  | 0  |                 SET_BAD("unknown header flags set"); | 
566  | 0  |                 break;  | 
567  | 0  |             }  | 
568  | 0  |             if (state->head != NULL)  | 
569  | 0  |                 state->head->text = (int)((hold >> 8) & 1);  | 
570  | 0  |             if ((state->flags & 0x0200) && (state->wrap & 4))  | 
571  | 0  |                 CRC2(state->check, hold);  | 
572  | 0  |             INITBITS();  | 
573  | 0  |             state->mode = TIME;  | 
574  | 0  |             Z_FALLTHROUGH;  | 
575  |  | 
  | 
576  | 0  |         case TIME:  | 
577  | 0  |             NEEDBITS(32);  | 
578  | 0  |             if (state->head != NULL)  | 
579  | 0  |                 state->head->time = (unsigned)(hold);  | 
580  | 0  |             if ((state->flags & 0x0200) && (state->wrap & 4))  | 
581  | 0  |                 CRC4(state->check, hold);  | 
582  | 0  |             INITBITS();  | 
583  | 0  |             state->mode = OS;  | 
584  | 0  |             Z_FALLTHROUGH;  | 
585  |  | 
  | 
586  | 0  |         case OS:  | 
587  | 0  |             NEEDBITS(16);  | 
588  | 0  |             if (state->head != NULL) { | 
589  | 0  |                 state->head->xflags = (int)(hold & 0xff);  | 
590  | 0  |                 state->head->os = (int)(hold >> 8);  | 
591  | 0  |             }  | 
592  | 0  |             if ((state->flags & 0x0200) && (state->wrap & 4))  | 
593  | 0  |                 CRC2(state->check, hold);  | 
594  | 0  |             INITBITS();  | 
595  | 0  |             state->mode = EXLEN;  | 
596  | 0  |             Z_FALLTHROUGH;  | 
597  |  | 
  | 
598  | 0  |         case EXLEN:  | 
599  | 0  |             if (state->flags & 0x0400) { | 
600  | 0  |                 NEEDBITS(16);  | 
601  | 0  |                 state->length = (uint16_t)hold;  | 
602  | 0  |                 if (state->head != NULL)  | 
603  | 0  |                     state->head->extra_len = (uint16_t)hold;  | 
604  | 0  |                 if ((state->flags & 0x0200) && (state->wrap & 4))  | 
605  | 0  |                     CRC2(state->check, hold);  | 
606  | 0  |                 INITBITS();  | 
607  | 0  |             } else if (state->head != NULL) { | 
608  | 0  |                 state->head->extra = NULL;  | 
609  | 0  |             }  | 
610  | 0  |             state->mode = EXTRA;  | 
611  | 0  |             Z_FALLTHROUGH;  | 
612  |  | 
  | 
613  | 0  |         case EXTRA:  | 
614  | 0  |             if (state->flags & 0x0400) { | 
615  | 0  |                 copy = state->length;  | 
616  | 0  |                 if (copy > have)  | 
617  | 0  |                     copy = have;  | 
618  | 0  |                 if (copy) { | 
619  | 0  |                     if (state->head != NULL && state->head->extra != NULL) { | 
620  | 0  |                         len = state->head->extra_len - state->length;  | 
621  | 0  |                         if (len < state->head->extra_max) { | 
622  | 0  |                             memcpy(state->head->extra + len, next,  | 
623  | 0  |                                     len + copy > state->head->extra_max ?  | 
624  | 0  |                                     state->head->extra_max - len : copy);  | 
625  | 0  |                         }  | 
626  | 0  |                     }  | 
627  | 0  |                     if ((state->flags & 0x0200) && (state->wrap & 4)) { | 
628  | 0  |                         state->check = PREFIX(crc32)(state->check, next, copy);  | 
629  | 0  |                     }  | 
630  | 0  |                     have -= copy;  | 
631  | 0  |                     next += copy;  | 
632  | 0  |                     state->length -= copy;  | 
633  | 0  |                 }  | 
634  | 0  |                 if (state->length)  | 
635  | 0  |                     goto inf_leave;  | 
636  | 0  |             }  | 
637  | 0  |             state->length = 0;  | 
638  | 0  |             state->mode = NAME;  | 
639  | 0  |             Z_FALLTHROUGH;  | 
640  |  | 
  | 
641  | 0  |         case NAME:  | 
642  | 0  |             if (state->flags & 0x0800) { | 
643  | 0  |                 if (have == 0) goto inf_leave;  | 
644  | 0  |                 copy = 0;  | 
645  | 0  |                 do { | 
646  | 0  |                     len = (unsigned)(next[copy++]);  | 
647  | 0  |                     if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)  | 
648  | 0  |                         state->head->name[state->length++] = (unsigned char)len;  | 
649  | 0  |                 } while (len && copy < have);  | 
650  | 0  |                 if ((state->flags & 0x0200) && (state->wrap & 4))  | 
651  | 0  |                     state->check = PREFIX(crc32)(state->check, next, copy);  | 
652  | 0  |                 have -= copy;  | 
653  | 0  |                 next += copy;  | 
654  | 0  |                 if (len)  | 
655  | 0  |                     goto inf_leave;  | 
656  | 0  |             } else if (state->head != NULL) { | 
657  | 0  |                 state->head->name = NULL;  | 
658  | 0  |             }  | 
659  | 0  |             state->length = 0;  | 
660  | 0  |             state->mode = COMMENT;  | 
661  | 0  |             Z_FALLTHROUGH;  | 
662  |  | 
  | 
663  | 0  |         case COMMENT:  | 
664  | 0  |             if (state->flags & 0x1000) { | 
665  | 0  |                 if (have == 0) goto inf_leave;  | 
666  | 0  |                 copy = 0;  | 
667  | 0  |                 do { | 
668  | 0  |                     len = (unsigned)(next[copy++]);  | 
669  | 0  |                     if (state->head != NULL && state->head->comment != NULL  | 
670  | 0  |                         && state->length < state->head->comm_max)  | 
671  | 0  |                         state->head->comment[state->length++] = (unsigned char)len;  | 
672  | 0  |                 } while (len && copy < have);  | 
673  | 0  |                 if ((state->flags & 0x0200) && (state->wrap & 4))  | 
674  | 0  |                     state->check = PREFIX(crc32)(state->check, next, copy);  | 
675  | 0  |                 have -= copy;  | 
676  | 0  |                 next += copy;  | 
677  | 0  |                 if (len)  | 
678  | 0  |                     goto inf_leave;  | 
679  | 0  |             } else if (state->head != NULL) { | 
680  | 0  |                 state->head->comment = NULL;  | 
681  | 0  |             }  | 
682  | 0  |             state->mode = HCRC;  | 
683  | 0  |             Z_FALLTHROUGH;  | 
684  |  | 
  | 
685  | 0  |         case HCRC:  | 
686  | 0  |             if (state->flags & 0x0200) { | 
687  | 0  |                 NEEDBITS(16);  | 
688  | 0  |                 if ((state->wrap & 4) && hold != (state->check & 0xffff)) { | 
689  | 0  |                     SET_BAD("header crc mismatch"); | 
690  | 0  |                     break;  | 
691  | 0  |                 }  | 
692  | 0  |                 INITBITS();  | 
693  | 0  |             }  | 
694  | 0  |             if (state->head != NULL) { | 
695  | 0  |                 state->head->hcrc = (int)((state->flags >> 9) & 1);  | 
696  | 0  |                 state->head->done = 1;  | 
697  | 0  |             }  | 
698  |  |             /* compute crc32 checksum if not in raw mode */  | 
699  | 0  |             if ((state->wrap & 4) && state->flags)  | 
700  | 0  |                 strm->adler = state->check = FUNCTABLE_CALL(crc32_fold_reset)(&state->crc_fold);  | 
701  | 0  |             state->mode = TYPE;  | 
702  | 0  |             break;  | 
703  | 0  | #endif  | 
704  | 0  |         case DICTID:  | 
705  | 0  |             NEEDBITS(32);  | 
706  | 0  |             strm->adler = state->check = ZSWAP32((unsigned)hold);  | 
707  | 0  |             INITBITS();  | 
708  | 0  |             state->mode = DICT;  | 
709  | 0  |             Z_FALLTHROUGH;  | 
710  |  | 
  | 
711  | 0  |         case DICT:  | 
712  | 0  |             if (state->havedict == 0) { | 
713  | 0  |                 RESTORE();  | 
714  | 0  |                 return Z_NEED_DICT;  | 
715  | 0  |             }  | 
716  | 0  |             strm->adler = state->check = ADLER32_INITIAL_VALUE;  | 
717  | 0  |             state->mode = TYPE;  | 
718  | 0  |             Z_FALLTHROUGH;  | 
719  |  | 
  | 
720  | 13.8k  |         case TYPE:  | 
721  | 13.8k  |             if (flush == Z_BLOCK || flush == Z_TREES)  | 
722  | 0  |                 goto inf_leave;  | 
723  | 13.8k  |             Z_FALLTHROUGH;  | 
724  |  |  | 
725  | 16.8k  |         case TYPEDO:  | 
726  |  |             /* determine and dispatch block type */  | 
727  | 16.8k  |             INFLATE_TYPEDO_HOOK(strm, flush);  /* hook for IBM Z DFLTCC */  | 
728  | 16.8k  |             if (state->last) { | 
729  | 2.98k  |                 BYTEBITS();  | 
730  | 2.98k  |                 state->mode = CHECK;  | 
731  | 2.98k  |                 break;  | 
732  | 2.98k  |             }  | 
733  | 13.8k  |             NEEDBITS(3);  | 
734  | 10.8k  |             state->last = BITS(1);  | 
735  | 10.8k  |             DROPBITS(1);  | 
736  | 10.8k  |             switch (BITS(2)) { | 
737  | 2.64k  |             case 0:                             /* stored block */  | 
738  | 2.64k  |                 Tracev((stderr, "inflate:     stored block%s\n", state->last ? " (last)" : ""));  | 
739  | 2.64k  |                 state->mode = STORED;  | 
740  | 2.64k  |                 break;  | 
741  | 1.43k  |             case 1:                             /* fixed block */  | 
742  | 1.43k  |                 PREFIX(fixedtables)(state);  | 
743  | 1.43k  |                 Tracev((stderr, "inflate:     fixed codes block%s\n", state->last ? " (last)" : ""));  | 
744  | 1.43k  |                 state->mode = LEN_;             /* decode codes */  | 
745  | 1.43k  |                 if (flush == Z_TREES) { | 
746  | 0  |                     DROPBITS(2);  | 
747  | 0  |                     goto inf_leave;  | 
748  | 0  |                 }  | 
749  | 1.43k  |                 break;  | 
750  | 6.78k  |             case 2:                             /* dynamic block */  | 
751  | 6.78k  |                 Tracev((stderr, "inflate:     dynamic codes block%s\n", state->last ? " (last)" : ""));  | 
752  | 6.78k  |                 state->mode = TABLE;  | 
753  | 6.78k  |                 break;  | 
754  | 0  |             case 3:  | 
755  | 0  |                 SET_BAD("invalid block type"); | 
756  | 10.8k  |             }  | 
757  | 10.8k  |             DROPBITS(2);  | 
758  | 10.8k  |             break;  | 
759  |  |  | 
760  | 2.64k  |         case STORED:  | 
761  |  |             /* get and verify stored block length */  | 
762  | 2.64k  |             BYTEBITS();                         /* go to byte boundary */  | 
763  | 2.64k  |             NEEDBITS(32);  | 
764  | 2.64k  |             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { | 
765  | 0  |                 SET_BAD("invalid stored block lengths"); | 
766  | 0  |                 break;  | 
767  | 0  |             }  | 
768  | 2.64k  |             state->length = (uint16_t)hold;  | 
769  | 2.64k  |             Tracev((stderr, "inflate:       stored length %u\n", state->length));  | 
770  | 2.64k  |             INITBITS();  | 
771  | 2.64k  |             state->mode = COPY_;  | 
772  | 2.64k  |             if (flush == Z_TREES)  | 
773  | 0  |                 goto inf_leave;  | 
774  | 2.64k  |             Z_FALLTHROUGH;  | 
775  |  |  | 
776  | 2.64k  |         case COPY_:  | 
777  | 2.64k  |             state->mode = COPY;  | 
778  | 2.64k  |             Z_FALLTHROUGH;  | 
779  |  |  | 
780  | 5.28k  |         case COPY:  | 
781  |  |             /* copy stored block from input to output */  | 
782  | 5.28k  |             copy = state->length;  | 
783  | 5.28k  |             if (copy) { | 
784  | 2.64k  |                 copy = MIN(copy, have);  | 
785  | 2.64k  |                 copy = MIN(copy, left);  | 
786  | 2.64k  |                 if (copy == 0)  | 
787  | 0  |                     goto inf_leave;  | 
788  | 2.64k  |                 memcpy(put, next, copy);  | 
789  | 2.64k  |                 have -= copy;  | 
790  | 2.64k  |                 next += copy;  | 
791  | 2.64k  |                 left -= copy;  | 
792  | 2.64k  |                 put += copy;  | 
793  | 2.64k  |                 state->length -= copy;  | 
794  | 2.64k  |                 break;  | 
795  | 2.64k  |             }  | 
796  | 2.64k  |             Tracev((stderr, "inflate:       stored end\n"));  | 
797  | 2.64k  |             state->mode = TYPE;  | 
798  | 2.64k  |             break;  | 
799  |  |  | 
800  | 6.78k  |         case TABLE:  | 
801  |  |             /* get dynamic table entries descriptor */  | 
802  | 6.78k  |             NEEDBITS(14);  | 
803  | 6.78k  |             state->nlen = BITS(5) + 257;  | 
804  | 6.78k  |             DROPBITS(5);  | 
805  | 6.78k  |             state->ndist = BITS(5) + 1;  | 
806  | 6.78k  |             DROPBITS(5);  | 
807  | 6.78k  |             state->ncode = BITS(4) + 4;  | 
808  | 6.78k  |             DROPBITS(4);  | 
809  | 6.78k  | #ifndef PKZIP_BUG_WORKAROUND  | 
810  | 6.78k  |             if (state->nlen > 286 || state->ndist > 30) { | 
811  | 0  |                 SET_BAD("too many length or distance symbols"); | 
812  | 0  |                 break;  | 
813  | 0  |             }  | 
814  | 6.78k  | #endif  | 
815  | 6.78k  |             Tracev((stderr, "inflate:       table sizes ok\n"));  | 
816  | 6.78k  |             state->have = 0;  | 
817  | 6.78k  |             state->mode = LENLENS;  | 
818  | 6.78k  |             Z_FALLTHROUGH;  | 
819  |  |  | 
820  | 6.78k  |         case LENLENS:  | 
821  |  |             /* get code length code lengths (not a typo) */  | 
822  | 122k  |             while (state->have < state->ncode) { | 
823  | 115k  |                 NEEDBITS(3);  | 
824  | 115k  |                 state->lens[order[state->have++]] = (uint16_t)BITS(3);  | 
825  | 115k  |                 DROPBITS(3);  | 
826  | 115k  |             }  | 
827  | 20.3k  |             while (state->have < 19)  | 
828  | 13.5k  |                 state->lens[order[state->have++]] = 0;  | 
829  | 6.78k  |             state->next = state->codes;  | 
830  | 6.78k  |             state->lencode = (const code *)(state->next);  | 
831  | 6.78k  |             state->lenbits = 7;  | 
832  | 6.78k  |             ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);  | 
833  | 6.78k  |             if (ret) { | 
834  | 0  |                 SET_BAD("invalid code lengths set"); | 
835  | 0  |                 break;  | 
836  | 0  |             }  | 
837  | 6.78k  |             Tracev((stderr, "inflate:       code lengths ok\n"));  | 
838  | 6.78k  |             state->have = 0;  | 
839  | 6.78k  |             state->mode = CODELENS;  | 
840  | 6.78k  |             Z_FALLTHROUGH;  | 
841  |  |  | 
842  | 6.78k  |         case CODELENS:  | 
843  |  |             /* get length and distance code code lengths */  | 
844  | 961k  |             while (state->have < state->nlen + state->ndist) { | 
845  | 1.28M  |                 for (;;) { | 
846  | 1.28M  |                     here = state->lencode[BITS(state->lenbits)];  | 
847  | 1.28M  |                     if (here.bits <= bits) break;  | 
848  | 332k  |                     PULLBYTE();  | 
849  | 332k  |                 }  | 
850  | 955k  |                 if (here.val < 16) { | 
851  | 769k  |                     DROPBITS(here.bits);  | 
852  | 769k  |                     state->lens[state->have++] = here.val;  | 
853  | 769k  |                 } else { | 
854  | 185k  |                     if (here.val == 16) { | 
855  | 147k  |                         NEEDBITS(here.bits + 2);  | 
856  | 147k  |                         DROPBITS(here.bits);  | 
857  | 147k  |                         if (state->have == 0) { | 
858  | 0  |                             SET_BAD("invalid bit length repeat"); | 
859  | 0  |                             break;  | 
860  | 0  |                         }  | 
861  | 147k  |                         len = state->lens[state->have - 1];  | 
862  | 147k  |                         copy = 3 + BITS(2);  | 
863  | 147k  |                         DROPBITS(2);  | 
864  | 147k  |                     } else if (here.val == 17) { | 
865  | 27.9k  |                         NEEDBITS(here.bits + 3);  | 
866  | 27.9k  |                         DROPBITS(here.bits);  | 
867  | 27.9k  |                         len = 0;  | 
868  | 27.9k  |                         copy = 3 + BITS(3);  | 
869  | 27.9k  |                         DROPBITS(3);  | 
870  | 27.9k  |                     } else { | 
871  | 10.6k  |                         NEEDBITS(here.bits + 7);  | 
872  | 10.6k  |                         DROPBITS(here.bits);  | 
873  | 10.6k  |                         len = 0;  | 
874  | 10.6k  |                         copy = 11 + BITS(7);  | 
875  | 10.6k  |                         DROPBITS(7);  | 
876  | 10.6k  |                     }  | 
877  | 185k  |                     if (state->have + copy > state->nlen + state->ndist) { | 
878  | 0  |                         SET_BAD("invalid bit length repeat"); | 
879  | 0  |                         break;  | 
880  | 0  |                     }  | 
881  | 1.46M  |                     while (copy) { | 
882  | 1.27M  |                         --copy;  | 
883  | 1.27M  |                         state->lens[state->have++] = (uint16_t)len;  | 
884  | 1.27M  |                     }  | 
885  | 185k  |                 }  | 
886  | 955k  |             }  | 
887  |  |  | 
888  |  |             /* handle error breaks in while */  | 
889  | 6.78k  |             if (state->mode == BAD)  | 
890  | 0  |                 break;  | 
891  |  |  | 
892  |  |             /* check for end-of-block code (better have one) */  | 
893  | 6.78k  |             if (state->lens[256] == 0) { | 
894  | 0  |                 SET_BAD("invalid code -- missing end-of-block"); | 
895  | 0  |                 break;  | 
896  | 0  |             }  | 
897  |  |  | 
898  |  |             /* build code tables -- note: do not change the lenbits or distbits  | 
899  |  |                values here (10 and 9) without reading the comments in inftrees.h  | 
900  |  |                concerning the ENOUGH constants, which depend on those values */  | 
901  | 6.78k  |             state->next = state->codes;  | 
902  | 6.78k  |             state->lencode = (const code *)(state->next);  | 
903  | 6.78k  |             state->lenbits = 10;  | 
904  | 6.78k  |             ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);  | 
905  | 6.78k  |             if (ret) { | 
906  | 0  |                 SET_BAD("invalid literal/lengths set"); | 
907  | 0  |                 break;  | 
908  | 0  |             }  | 
909  | 6.78k  |             state->distcode = (const code *)(state->next);  | 
910  | 6.78k  |             state->distbits = 9;  | 
911  | 6.78k  |             ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,  | 
912  | 6.78k  |                             &(state->next), &(state->distbits), state->work);  | 
913  | 6.78k  |             if (ret) { | 
914  | 0  |                 SET_BAD("invalid distances set"); | 
915  | 0  |                 break;  | 
916  | 0  |             }  | 
917  | 6.78k  |             Tracev((stderr, "inflate:       codes ok\n"));  | 
918  | 6.78k  |             state->mode = LEN_;  | 
919  | 6.78k  |             if (flush == Z_TREES)  | 
920  | 0  |                 goto inf_leave;  | 
921  | 6.78k  |             Z_FALLTHROUGH;  | 
922  |  |  | 
923  | 8.21k  |         case LEN_:  | 
924  | 8.21k  |             state->mode = LEN;  | 
925  | 8.21k  |             Z_FALLTHROUGH;  | 
926  |  |  | 
927  | 197k  |         case LEN:  | 
928  |  |             /* use inflate_fast() if we have enough input and output */  | 
929  | 197k  |             if (have >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_LEFT) { | 
930  | 7.35k  |                 RESTORE();  | 
931  | 7.35k  |                 FUNCTABLE_CALL(inflate_fast)(strm, out);  | 
932  | 7.35k  |                 LOAD();  | 
933  | 7.35k  |                 if (state->mode == TYPE)  | 
934  | 5.43k  |                     state->back = -1;  | 
935  | 7.35k  |                 break;  | 
936  | 7.35k  |             }  | 
937  | 190k  |             state->back = 0;  | 
938  |  |  | 
939  |  |             /* get a literal, length, or end-of-block code */  | 
940  | 348k  |             for (;;) { | 
941  | 348k  |                 here = state->lencode[BITS(state->lenbits)];  | 
942  | 348k  |                 if (here.bits <= bits)  | 
943  | 190k  |                     break;  | 
944  | 157k  |                 PULLBYTE();  | 
945  | 157k  |             }  | 
946  | 190k  |             if (here.op && (here.op & 0xf0) == 0) { | 
947  | 4.27k  |                 last = here;  | 
948  | 5.28k  |                 for (;;) { | 
949  | 5.28k  |                     here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];  | 
950  | 5.28k  |                     if ((unsigned)last.bits + (unsigned)here.bits <= bits)  | 
951  | 4.27k  |                         break;  | 
952  | 1.01k  |                     PULLBYTE();  | 
953  | 1.01k  |                 }  | 
954  | 4.27k  |                 DROPBITS(last.bits);  | 
955  | 4.27k  |                 state->back += last.bits;  | 
956  | 4.27k  |             }  | 
957  | 190k  |             DROPBITS(here.bits);  | 
958  | 190k  |             state->back += here.bits;  | 
959  | 190k  |             state->length = here.val;  | 
960  |  |  | 
961  |  |             /* process literal */  | 
962  | 190k  |             if ((int)(here.op) == 0) { | 
963  | 169k  |                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?  | 
964  | 169k  |                         "inflate:         literal '%c'\n" :  | 
965  | 169k  |                         "inflate:         literal 0x%02x\n", here.val));  | 
966  | 169k  |                 state->mode = LIT;  | 
967  | 169k  |                 break;  | 
968  | 169k  |             }  | 
969  |  |  | 
970  |  |             /* process end of block */  | 
971  | 20.9k  |             if (here.op & 32) { | 
972  | 2.78k  |                 Tracevv((stderr, "inflate:         end of block\n"));  | 
973  | 2.78k  |                 state->back = -1;  | 
974  | 2.78k  |                 state->mode = TYPE;  | 
975  | 2.78k  |                 break;  | 
976  | 2.78k  |             }  | 
977  |  |  | 
978  |  |             /* invalid code */  | 
979  | 18.1k  |             if (here.op & 64) { | 
980  | 0  |                 SET_BAD("invalid literal/length code"); | 
981  | 0  |                 break;  | 
982  | 0  |             }  | 
983  |  |  | 
984  |  |             /* length code */  | 
985  | 18.1k  |             state->extra = (here.op & MAX_BITS);  | 
986  | 18.1k  |             state->mode = LENEXT;  | 
987  | 18.1k  |             Z_FALLTHROUGH;  | 
988  |  |  | 
989  | 18.1k  |         case LENEXT:  | 
990  |  |             /* get extra bits, if any */  | 
991  | 18.1k  |             if (state->extra) { | 
992  | 4.19k  |                 NEEDBITS(state->extra);  | 
993  | 4.19k  |                 state->length += BITS(state->extra);  | 
994  | 4.19k  |                 DROPBITS(state->extra);  | 
995  | 4.19k  |                 state->back += state->extra;  | 
996  | 4.19k  |             }  | 
997  | 18.1k  |             Tracevv((stderr, "inflate:         length %u\n", state->length));  | 
998  | 18.1k  |             state->was = state->length;  | 
999  | 18.1k  |             state->mode = DIST;  | 
1000  | 18.1k  |             Z_FALLTHROUGH;  | 
1001  |  |  | 
1002  | 18.1k  |         case DIST:  | 
1003  |  |             /* get distance code */  | 
1004  | 27.1k  |             for (;;) { | 
1005  | 27.1k  |                 here = state->distcode[BITS(state->distbits)];  | 
1006  | 27.1k  |                 if (here.bits <= bits)  | 
1007  | 18.1k  |                     break;  | 
1008  | 8.98k  |                 PULLBYTE();  | 
1009  | 8.98k  |             }  | 
1010  | 18.1k  |             if ((here.op & 0xf0) == 0) { | 
1011  | 88  |                 last = here;  | 
1012  | 117  |                 for (;;) { | 
1013  | 117  |                     here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];  | 
1014  | 117  |                     if ((unsigned)last.bits + (unsigned)here.bits <= bits)  | 
1015  | 88  |                         break;  | 
1016  | 29  |                     PULLBYTE();  | 
1017  | 29  |                 }  | 
1018  | 88  |                 DROPBITS(last.bits);  | 
1019  | 88  |                 state->back += last.bits;  | 
1020  | 88  |             }  | 
1021  | 18.1k  |             DROPBITS(here.bits);  | 
1022  | 18.1k  |             state->back += here.bits;  | 
1023  | 18.1k  |             if (here.op & 64) { | 
1024  | 0  |                 SET_BAD("invalid distance code"); | 
1025  | 0  |                 break;  | 
1026  | 0  |             }  | 
1027  | 18.1k  |             state->offset = here.val;  | 
1028  | 18.1k  |             state->extra = (here.op & MAX_BITS);  | 
1029  | 18.1k  |             state->mode = DISTEXT;  | 
1030  | 18.1k  |             Z_FALLTHROUGH;  | 
1031  |  |  | 
1032  | 18.1k  |         case DISTEXT:  | 
1033  |  |             /* get distance extra bits, if any */  | 
1034  | 18.1k  |             if (state->extra) { | 
1035  | 14.8k  |                 NEEDBITS(state->extra);  | 
1036  | 14.8k  |                 state->offset += BITS(state->extra);  | 
1037  | 14.8k  |                 DROPBITS(state->extra);  | 
1038  | 14.8k  |                 state->back += state->extra;  | 
1039  | 14.8k  |             }  | 
1040  |  | #ifdef INFLATE_STRICT  | 
1041  |  |             if (state->offset > state->dmax) { | 
1042  |  |                 SET_BAD("invalid distance too far back"); | 
1043  |  |                 break;  | 
1044  |  |             }  | 
1045  |  | #endif  | 
1046  | 18.1k  |             Tracevv((stderr, "inflate:         distance %u\n", state->offset));  | 
1047  | 18.1k  |             state->mode = MATCH;  | 
1048  | 18.1k  |             Z_FALLTHROUGH;  | 
1049  |  |  | 
1050  | 18.1k  |         case MATCH:  | 
1051  |  |             /* copy match from window to output */  | 
1052  | 18.1k  |             if (left == 0)  | 
1053  | 0  |                 goto inf_leave;  | 
1054  | 18.1k  |             copy = out - left;  | 
1055  | 18.1k  |             if (state->offset > copy) {         /* copy from window */ | 
1056  | 0  |                 copy = state->offset - copy;  | 
1057  | 0  |                 if (copy > state->whave) { | 
1058  |  | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR  | 
1059  |  |                     if (state->sane) { | 
1060  |  |                         SET_BAD("invalid distance too far back"); | 
1061  |  |                         break;  | 
1062  |  |                     }  | 
1063  |  |                     Trace((stderr, "inflate.c too far\n"));  | 
1064  |  |                     copy -= state->whave;  | 
1065  |  |                     copy = MIN(copy, state->length);  | 
1066  |  |                     copy = MIN(copy, left);  | 
1067  |  |                     left -= copy;  | 
1068  |  |                     state->length -= copy;  | 
1069  |  |                     do { | 
1070  |  |                         *put++ = 0;  | 
1071  |  |                     } while (--copy);  | 
1072  |  |                     if (state->length == 0)  | 
1073  |  |                         state->mode = LEN;  | 
1074  |  | #else  | 
1075  | 0  |                     SET_BAD("invalid distance too far back"); | 
1076  | 0  | #endif  | 
1077  | 0  |                     break;  | 
1078  | 0  |                 }  | 
1079  | 0  |                 if (copy > state->wnext) { | 
1080  | 0  |                     copy -= state->wnext;  | 
1081  | 0  |                     from = state->window + (state->wsize - copy);  | 
1082  | 0  |                 } else { | 
1083  | 0  |                     from = state->window + (state->wnext - copy);  | 
1084  | 0  |                 }  | 
1085  | 0  |                 copy = MIN(copy, state->length);  | 
1086  | 0  |                 copy = MIN(copy, left);  | 
1087  |  | 
  | 
1088  | 0  |                 put = chunkcopy_safe(put, from, copy, put + left);  | 
1089  | 18.1k  |             } else { | 
1090  | 18.1k  |                 copy = MIN(state->length, left);  | 
1091  |  |  | 
1092  | 18.1k  |                 put = FUNCTABLE_CALL(chunkmemset_safe)(put, put - state->offset, copy, left);  | 
1093  | 18.1k  |             }  | 
1094  | 18.1k  |             left -= copy;  | 
1095  | 18.1k  |             state->length -= copy;  | 
1096  | 18.1k  |             if (state->length == 0)  | 
1097  | 18.1k  |                 state->mode = LEN;  | 
1098  | 18.1k  |             break;  | 
1099  |  |  | 
1100  | 169k  |         case LIT:  | 
1101  | 169k  |             if (left == 0)  | 
1102  | 0  |                 goto inf_leave;  | 
1103  | 169k  |             *put++ = (unsigned char)(state->length);  | 
1104  | 169k  |             left--;  | 
1105  | 169k  |             state->mode = LEN;  | 
1106  | 169k  |             break;  | 
1107  |  |  | 
1108  | 2.98k  |         case CHECK:  | 
1109  | 2.98k  |             if (state->wrap) { | 
1110  | 2.98k  |                 NEEDBITS(32);  | 
1111  | 2.98k  |                 out -= left;  | 
1112  | 2.98k  |                 strm->total_out += out;  | 
1113  | 2.98k  |                 state->total += out;  | 
1114  |  |  | 
1115  |  |                 /* compute crc32 checksum if not in raw mode */  | 
1116  | 2.98k  |                 if (INFLATE_NEED_CHECKSUM(strm) && state->wrap & 4) { | 
1117  | 0  |                     if (out) { | 
1118  | 0  |                         inf_chksum(strm, put - out, out);  | 
1119  | 0  |                     }  | 
1120  | 0  | #ifdef GUNZIP  | 
1121  | 0  |                     if (state->flags)  | 
1122  | 0  |                         strm->adler = state->check = FUNCTABLE_CALL(crc32_fold_final)(&state->crc_fold);  | 
1123  | 0  | #endif  | 
1124  | 0  |                 }  | 
1125  | 2.98k  |                 out = left;  | 
1126  | 2.98k  |                 if ((state->wrap & 4) && (  | 
1127  | 0  | #ifdef GUNZIP  | 
1128  | 0  |                      state->flags ? hold :  | 
1129  | 0  | #endif  | 
1130  | 0  |                      ZSWAP32((unsigned)hold)) != state->check) { | 
1131  | 0  |                     SET_BAD("incorrect data check"); | 
1132  | 0  |                     break;  | 
1133  | 0  |                 }  | 
1134  | 2.98k  |                 INITBITS();  | 
1135  | 2.98k  |                 Tracev((stderr, "inflate:   check matches trailer\n"));  | 
1136  | 2.98k  |             }  | 
1137  | 2.98k  | #ifdef GUNZIP  | 
1138  | 2.98k  |             state->mode = LENGTH;  | 
1139  | 2.98k  |             Z_FALLTHROUGH;  | 
1140  |  |  | 
1141  | 2.98k  |         case LENGTH:  | 
1142  | 2.98k  |             if (state->wrap && state->flags) { | 
1143  | 0  |                 NEEDBITS(32);  | 
1144  | 0  |                 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) { | 
1145  | 0  |                     SET_BAD("incorrect length check"); | 
1146  | 0  |                     break;  | 
1147  | 0  |                 }  | 
1148  | 0  |                 INITBITS();  | 
1149  | 0  |                 Tracev((stderr, "inflate:   length matches trailer\n"));  | 
1150  | 0  |             }  | 
1151  | 2.98k  | #endif  | 
1152  | 2.98k  |             state->mode = DONE;  | 
1153  | 2.98k  |             Z_FALLTHROUGH;  | 
1154  |  |  | 
1155  | 2.98k  |         case DONE:  | 
1156  |  |             /* inflate stream terminated properly */  | 
1157  | 2.98k  |             ret = Z_STREAM_END;  | 
1158  | 2.98k  |             goto inf_leave;  | 
1159  |  |  | 
1160  | 0  |         case BAD:  | 
1161  | 0  |             ret = Z_DATA_ERROR;  | 
1162  | 0  |             goto inf_leave;  | 
1163  |  |  | 
1164  | 0  |         case SYNC:  | 
1165  |  | 
  | 
1166  | 0  |         default:                 /* can't happen, but makes compilers happy */  | 
1167  | 0  |             return Z_STREAM_ERROR;  | 
1168  | 394k  |         }  | 
1169  |  |  | 
1170  |  |     /*  | 
1171  |  |        Return from inflate(), updating the total counts and the check value.  | 
1172  |  |        If there was no progress during the inflate() call, return a buffer  | 
1173  |  |        error.  Call updatewindow() to create and/or update the window state.  | 
1174  |  |      */  | 
1175  | 5.97k  |   inf_leave:  | 
1176  | 5.97k  |     RESTORE();  | 
1177  | 5.97k  |     uint32_t check_bytes = out - strm->avail_out;  | 
1178  | 5.97k  |     if (INFLATE_NEED_UPDATEWINDOW(strm) &&  | 
1179  | 5.97k  |             (state->wsize || (out != strm->avail_out && state->mode < BAD &&  | 
1180  | 0  |                  (state->mode < CHECK || flush != Z_FINISH)))) { | 
1181  |  |         /* update sliding window with respective checksum if not in "raw" mode */  | 
1182  | 0  |         updatewindow(strm, strm->next_out, check_bytes, state->wrap & 4);  | 
1183  | 0  |     }  | 
1184  | 5.97k  |     in -= strm->avail_in;  | 
1185  | 5.97k  |     out -= strm->avail_out;  | 
1186  | 5.97k  |     strm->total_in += in;  | 
1187  | 5.97k  |     strm->total_out += out;  | 
1188  | 5.97k  |     state->total += out;  | 
1189  |  |  | 
1190  | 5.97k  |     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +  | 
1191  | 5.97k  |                       (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);  | 
1192  | 5.97k  |     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) { | 
1193  |  |         /* when no sliding window is used, hash the output bytes if no CHECK state */  | 
1194  | 0  |         if (INFLATE_NEED_CHECKSUM(strm) && !state->wsize && flush == Z_FINISH) { | 
1195  | 0  |             inf_chksum(strm, put - check_bytes, check_bytes);  | 
1196  | 0  |         }  | 
1197  | 0  |         ret = Z_BUF_ERROR;  | 
1198  | 0  |     }  | 
1199  | 5.97k  |     return ret;  | 
1200  | 5.97k  | }  | 
1201  |  |  | 
1202  | 2.98k  | int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) { | 
1203  | 2.98k  |     if (inflateStateCheck(strm))  | 
1204  | 0  |         return Z_STREAM_ERROR;  | 
1205  |  |  | 
1206  |  |     /* Free allocated buffers */  | 
1207  | 2.98k  |     free_inflate(strm);  | 
1208  |  |  | 
1209  | 2.98k  |     Tracev((stderr, "inflate: end\n"));  | 
1210  | 2.98k  |     return Z_OK;  | 
1211  | 2.98k  | }  | 
1212  |  |  | 
1213  | 0  | int32_t Z_EXPORT PREFIX(inflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) { | 
1214  | 0  |     struct inflate_state *state;  | 
1215  |  |  | 
1216  |  |     /* check state */  | 
1217  | 0  |     if (inflateStateCheck(strm))  | 
1218  | 0  |         return Z_STREAM_ERROR;  | 
1219  | 0  |     state = (struct inflate_state *)strm->state;  | 
1220  |  | 
  | 
1221  | 0  |     INFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */  | 
1222  |  |  | 
1223  |  |     /* copy dictionary */  | 
1224  | 0  |     if (state->whave && dictionary != NULL) { | 
1225  | 0  |         memcpy(dictionary, state->window + state->wnext, state->whave - state->wnext);  | 
1226  | 0  |         memcpy(dictionary + state->whave - state->wnext, state->window, state->wnext);  | 
1227  | 0  |     }  | 
1228  | 0  |     if (dictLength != NULL)  | 
1229  | 0  |         *dictLength = state->whave;  | 
1230  | 0  |     return Z_OK;  | 
1231  | 0  | }  | 
1232  |  |  | 
1233  | 0  | int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) { | 
1234  | 0  |     struct inflate_state *state;  | 
1235  | 0  |     unsigned long dictid;  | 
1236  |  |  | 
1237  |  |     /* check state */  | 
1238  | 0  |     if (inflateStateCheck(strm))  | 
1239  | 0  |         return Z_STREAM_ERROR;  | 
1240  | 0  |     state = (struct inflate_state *)strm->state;  | 
1241  | 0  |     if (state->wrap != 0 && state->mode != DICT)  | 
1242  | 0  |         return Z_STREAM_ERROR;  | 
1243  |  |  | 
1244  |  |     /* check for correct dictionary identifier */  | 
1245  | 0  |     if (state->mode == DICT) { | 
1246  | 0  |         dictid = FUNCTABLE_CALL(adler32)(ADLER32_INITIAL_VALUE, dictionary, dictLength);  | 
1247  | 0  |         if (dictid != state->check)  | 
1248  | 0  |             return Z_DATA_ERROR;  | 
1249  | 0  |     }  | 
1250  |  |  | 
1251  | 0  |     INFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */  | 
1252  |  |  | 
1253  |  |     /* copy dictionary to window using updatewindow(), which will amend the  | 
1254  |  |        existing dictionary if appropriate */  | 
1255  | 0  |     updatewindow(strm, dictionary + dictLength, dictLength, 0);  | 
1256  |  | 
  | 
1257  | 0  |     state->havedict = 1;  | 
1258  | 0  |     Tracev((stderr, "inflate:   dictionary set\n"));  | 
1259  | 0  |     return Z_OK;  | 
1260  | 0  | }  | 
1261  |  |  | 
1262  | 0  | int32_t Z_EXPORT PREFIX(inflateGetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) { | 
1263  | 0  |     struct inflate_state *state;  | 
1264  |  |  | 
1265  |  |     /* check state */  | 
1266  | 0  |     if (inflateStateCheck(strm))  | 
1267  | 0  |         return Z_STREAM_ERROR;  | 
1268  | 0  |     state = (struct inflate_state *)strm->state;  | 
1269  | 0  |     if ((state->wrap & 2) == 0)  | 
1270  | 0  |         return Z_STREAM_ERROR;  | 
1271  |  |  | 
1272  |  |     /* save header structure */  | 
1273  | 0  |     state->head = head;  | 
1274  | 0  |     head->done = 0;  | 
1275  | 0  |     return Z_OK;  | 
1276  | 0  | }  | 
1277  |  |  | 
1278  |  | /*  | 
1279  |  |    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found  | 
1280  |  |    or when out of input.  When called, *have is the number of pattern bytes  | 
1281  |  |    found in order so far, in 0..3.  On return *have is updated to the new  | 
1282  |  |    state.  If on return *have equals four, then the pattern was found and the  | 
1283  |  |    return value is how many bytes were read including the last byte of the  | 
1284  |  |    pattern.  If *have is less than four, then the pattern has not been found  | 
1285  |  |    yet and the return value is len.  In the latter case, syncsearch() can be  | 
1286  |  |    called again with more data and the *have state.  *have is initialized to  | 
1287  |  |    zero for the first call.  | 
1288  |  |  */  | 
1289  | 5.97k  | static uint32_t syncsearch(uint32_t *have, const uint8_t *buf, uint32_t len) { | 
1290  | 5.97k  |     uint32_t got, next;  | 
1291  |  |  | 
1292  | 5.97k  |     got = *have;  | 
1293  | 5.97k  |     next = 0;  | 
1294  | 32.8k  |     while (next < len && got < 4) { | 
1295  | 26.8k  |         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))  | 
1296  | 12.0k  |             got++;  | 
1297  | 14.7k  |         else if (buf[next])  | 
1298  | 10.7k  |             got = 0;  | 
1299  | 3.99k  |         else  | 
1300  | 3.99k  |             got = 4 - got;  | 
1301  | 26.8k  |         next++;  | 
1302  | 26.8k  |     }  | 
1303  | 5.97k  |     *have = got;  | 
1304  | 5.97k  |     return next;  | 
1305  | 5.97k  | }  | 
1306  |  |  | 
1307  | 2.98k  | int32_t Z_EXPORT PREFIX(inflateSync)(PREFIX3(stream) *strm) { | 
1308  | 2.98k  |     struct inflate_state *state;  | 
1309  | 2.98k  |     size_t in, out;             /* temporary to save total_in and total_out */  | 
1310  | 2.98k  |     unsigned len;               /* number of bytes to look at or looked at */  | 
1311  | 2.98k  |     int flags;                  /* temporary to save header status */  | 
1312  | 2.98k  |     unsigned char buf[4];       /* to restore bit buffer to byte string */  | 
1313  |  |  | 
1314  |  |     /* check parameters */  | 
1315  | 2.98k  |     if (inflateStateCheck(strm))  | 
1316  | 0  |         return Z_STREAM_ERROR;  | 
1317  | 2.98k  |     state = (struct inflate_state *)strm->state;  | 
1318  | 2.98k  |     if (strm->avail_in == 0 && state->bits < 8)  | 
1319  | 0  |         return Z_BUF_ERROR;  | 
1320  |  |  | 
1321  |  |     /* if first time, start search in bit buffer */  | 
1322  | 2.98k  |     if (state->mode != SYNC) { | 
1323  | 2.98k  |         state->mode = SYNC;  | 
1324  | 2.98k  |         state->hold >>= state->bits & 7;  | 
1325  | 2.98k  |         state->bits -= state->bits & 7;  | 
1326  | 2.98k  |         len = 0;  | 
1327  | 2.98k  |         while (state->bits >= 8) { | 
1328  | 0  |             buf[len++] = (unsigned char)(state->hold);  | 
1329  | 0  |             state->hold >>= 8;  | 
1330  | 0  |             state->bits -= 8;  | 
1331  | 0  |         }  | 
1332  | 2.98k  |         state->have = 0;  | 
1333  | 2.98k  |         syncsearch(&(state->have), buf, len);  | 
1334  | 2.98k  |     }  | 
1335  |  |  | 
1336  |  |     /* search available input */  | 
1337  | 2.98k  |     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);  | 
1338  | 2.98k  |     strm->avail_in -= len;  | 
1339  | 2.98k  |     strm->next_in += len;  | 
1340  | 2.98k  |     strm->total_in += len;  | 
1341  |  |  | 
1342  |  |     /* return no joy or set up to restart inflate() on a new block */  | 
1343  | 2.98k  |     if (state->have != 4)  | 
1344  | 0  |         return Z_DATA_ERROR;  | 
1345  | 2.98k  |     if (state->flags == -1)  | 
1346  | 0  |         state->wrap = 0;    /* if no header yet, treat as raw */  | 
1347  | 2.98k  |     else  | 
1348  | 2.98k  |         state->wrap &= ~4;  /* no point in computing a check value now */  | 
1349  | 2.98k  |     flags = state->flags;  | 
1350  | 2.98k  |     in = strm->total_in;  | 
1351  | 2.98k  |     out = strm->total_out;  | 
1352  | 2.98k  |     PREFIX(inflateReset)(strm);  | 
1353  | 2.98k  |     strm->total_in = (z_uintmax_t)in; /* Can't use z_size_t here as it will overflow on 64-bit Windows */  | 
1354  | 2.98k  |     strm->total_out = (z_uintmax_t)out;  | 
1355  | 2.98k  |     state->flags = flags;  | 
1356  | 2.98k  |     state->mode = TYPE;  | 
1357  | 2.98k  |     return Z_OK;  | 
1358  | 2.98k  | }  | 
1359  |  |  | 
1360  |  | /*  | 
1361  |  |    Returns true if inflate is currently at the end of a block generated by  | 
1362  |  |    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP  | 
1363  |  |    implementation to provide an additional safety check. PPP uses  | 
1364  |  |    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored  | 
1365  |  |    block. When decompressing, PPP checks that at the end of input packet,  | 
1366  |  |    inflate is waiting for these length bytes.  | 
1367  |  |  */  | 
1368  | 0  | int32_t Z_EXPORT PREFIX(inflateSyncPoint)(PREFIX3(stream) *strm) { | 
1369  | 0  |     struct inflate_state *state;  | 
1370  |  | 
  | 
1371  | 0  |     if (inflateStateCheck(strm))  | 
1372  | 0  |         return Z_STREAM_ERROR;  | 
1373  | 0  |     INFLATE_SYNC_POINT_HOOK(strm);  | 
1374  | 0  |     state = (struct inflate_state *)strm->state;  | 
1375  | 0  |     return state->mode == STORED && state->bits == 0;  | 
1376  | 0  | }  | 
1377  |  |  | 
1378  | 0  | int32_t Z_EXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) { | 
1379  | 0  |     struct inflate_state *state;  | 
1380  | 0  |     struct inflate_state *copy;  | 
1381  |  |  | 
1382  |  |     /* check input */  | 
1383  | 0  |     if (inflateStateCheck(source) || dest == NULL)  | 
1384  | 0  |         return Z_STREAM_ERROR;  | 
1385  | 0  |     state = (struct inflate_state *)source->state;  | 
1386  |  |  | 
1387  |  |     /* copy stream */  | 
1388  | 0  |     memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));  | 
1389  |  |  | 
1390  |  |     /* allocate space */  | 
1391  | 0  |     inflate_allocs *alloc_bufs = alloc_inflate(dest);  | 
1392  | 0  |     if (alloc_bufs == NULL)  | 
1393  | 0  |         return Z_MEM_ERROR;  | 
1394  | 0  |     copy = alloc_bufs->state;  | 
1395  |  |  | 
1396  |  |     /* copy state */  | 
1397  | 0  |     memcpy(copy, state, sizeof(struct inflate_state));  | 
1398  | 0  |     copy->strm = dest;  | 
1399  | 0  |     if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) { | 
1400  | 0  |         copy->lencode = copy->codes + (state->lencode - state->codes);  | 
1401  | 0  |         copy->distcode = copy->codes + (state->distcode - state->codes);  | 
1402  | 0  |     }  | 
1403  | 0  |     copy->next = copy->codes + (state->next - state->codes);  | 
1404  | 0  |     copy->window = alloc_bufs->window;  | 
1405  | 0  |     copy->alloc_bufs = alloc_bufs;  | 
1406  |  |  | 
1407  |  |     /* window */  | 
1408  | 0  |     memcpy(copy->window, state->window, INFLATE_ADJUST_WINDOW_SIZE((size_t)state->wsize));  | 
1409  |  | 
  | 
1410  | 0  |     dest->state = (struct internal_state *)copy;  | 
1411  | 0  |     return Z_OK;  | 
1412  | 0  | }  | 
1413  |  |  | 
1414  | 0  | int32_t Z_EXPORT PREFIX(inflateUndermine)(PREFIX3(stream) *strm, int32_t subvert) { | 
1415  |  | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR  | 
1416  |  |     struct inflate_state *state;  | 
1417  |  |  | 
1418  |  |     if (inflateStateCheck(strm))  | 
1419  |  |         return Z_STREAM_ERROR;  | 
1420  |  |     state = (struct inflate_state *)strm->state;  | 
1421  |  |     state->sane = !subvert;  | 
1422  |  |     return Z_OK;  | 
1423  |  | #else  | 
1424  | 0  |     Z_UNUSED(strm);  | 
1425  | 0  |     Z_UNUSED(subvert);  | 
1426  | 0  |     return Z_DATA_ERROR;  | 
1427  | 0  | #endif  | 
1428  | 0  | }  | 
1429  |  |  | 
1430  | 0  | int32_t Z_EXPORT PREFIX(inflateValidate)(PREFIX3(stream) *strm, int32_t check) { | 
1431  | 0  |     struct inflate_state *state;  | 
1432  |  | 
  | 
1433  | 0  |     if (inflateStateCheck(strm))  | 
1434  | 0  |         return Z_STREAM_ERROR;  | 
1435  | 0  |     state = (struct inflate_state *)strm->state;  | 
1436  | 0  |     if (check && state->wrap)  | 
1437  | 0  |         state->wrap |= 4;  | 
1438  | 0  |     else  | 
1439  | 0  |         state->wrap &= ~4;  | 
1440  | 0  |     return Z_OK;  | 
1441  | 0  | }  | 
1442  |  |  | 
1443  | 0  | long Z_EXPORT PREFIX(inflateMark)(PREFIX3(stream) *strm) { | 
1444  | 0  |     struct inflate_state *state;  | 
1445  |  | 
  | 
1446  | 0  |     if (inflateStateCheck(strm))  | 
1447  | 0  |         return -65536;  | 
1448  | 0  |     INFLATE_MARK_HOOK(strm);  /* hook for IBM Z DFLTCC */  | 
1449  | 0  |     state = (struct inflate_state *)strm->state;  | 
1450  | 0  |     return (long)(((unsigned long)((long)state->back)) << 16) +  | 
1451  | 0  |         (state->mode == COPY ? state->length :  | 
1452  | 0  |             (state->mode == MATCH ? state->was - state->length : 0));  | 
1453  | 0  | }  | 
1454  |  |  | 
1455  | 0  | unsigned long Z_EXPORT PREFIX(inflateCodesUsed)(PREFIX3(stream) *strm) { | 
1456  | 0  |     struct inflate_state *state;  | 
1457  | 0  |     if (strm == NULL || strm->state == NULL)  | 
1458  | 0  |         return (unsigned long)-1;  | 
1459  | 0  |     state = (struct inflate_state *)strm->state;  | 
1460  | 0  |     return (unsigned long)(state->next - state->codes);  | 
1461  | 0  | }  |