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