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