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