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