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