Lines Matching +full:- +full:state
1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2022 Mark Adler
10 * - First version -- complete rewrite of inflate to simplify code, avoid
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
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)
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
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
58 * - Improved inflateBack() interface to allow the caller to provide initial
60 * - Fixed stored blocks bug in inflateBack()
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
67 * - Changed type of window in inflateBackInit() to unsigned char *
70 * - Changed many types to unsigned or unsigned short to avoid warnings
71 * - Added inflateCopy() function
74 * - Changed inflateBack() interface to provide separate opaque descriptors
76 * - Changed inflateBack() argument and in_func typedef to swap the length
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
95 struct inflate_state FAR *state; in inflateStateCheck() local
97 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) in inflateStateCheck()
99 state = (struct inflate_state FAR *)strm->state; in inflateStateCheck()
100 if (state == Z_NULL || state->strm != strm || in inflateStateCheck()
101 state->mode < HEAD || state->mode > SYNC) in inflateStateCheck()
107 struct inflate_state FAR *state; in inflateResetKeep() local
110 state = (struct inflate_state FAR *)strm->state; in inflateResetKeep()
111 strm->total_in = strm->total_out = state->total = 0; in inflateResetKeep()
112 strm->msg = Z_NULL; in inflateResetKeep()
113 if (state->wrap) /* to support ill-conceived Java test suite */ in inflateResetKeep()
114 strm->adler = state->wrap & 1; in inflateResetKeep()
115 state->mode = HEAD; in inflateResetKeep()
116 state->last = 0; in inflateResetKeep()
117 state->havedict = 0; in inflateResetKeep()
118 state->flags = -1; in inflateResetKeep()
119 state->dmax = 32768U; in inflateResetKeep()
120 state->head = Z_NULL; in inflateResetKeep()
121 state->hold = 0; in inflateResetKeep()
122 state->bits = 0; in inflateResetKeep()
123 state->lencode = state->distcode = state->next = state->codes; in inflateResetKeep()
124 state->sane = 1; in inflateResetKeep()
125 state->back = -1; in inflateResetKeep()
131 struct inflate_state FAR *state; in inflateReset() local
134 state = (struct inflate_state FAR *)strm->state; in inflateReset()
135 state->wsize = 0; in inflateReset()
136 state->whave = 0; in inflateReset()
137 state->wnext = 0; in inflateReset()
143 struct inflate_state FAR *state; in inflateReset2() local
145 /* get the state */ in inflateReset2()
147 state = (struct inflate_state FAR *)strm->state; in inflateReset2()
151 if (windowBits < -15) in inflateReset2()
154 windowBits = -windowBits; in inflateReset2()
167 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { in inflateReset2()
168 ZFREE(strm, state->window); in inflateReset2()
169 state->window = Z_NULL; in inflateReset2()
172 /* update state and reset the rest of it */ in inflateReset2()
173 state->wrap = wrap; in inflateReset2()
174 state->wbits = (unsigned)windowBits; in inflateReset2()
181 struct inflate_state FAR *state; in inflateInit2_() local
187 strm->msg = Z_NULL; /* in case we return an error */ in inflateInit2_()
188 if (strm->zalloc == (alloc_func)0) { in inflateInit2_()
192 strm->zalloc = zcalloc; in inflateInit2_()
193 strm->opaque = (voidpf)0; in inflateInit2_()
196 if (strm->zfree == (free_func)0) in inflateInit2_()
200 strm->zfree = zcfree; in inflateInit2_()
202 state = (struct inflate_state FAR *) in inflateInit2_()
204 if (state == Z_NULL) return Z_MEM_ERROR; in inflateInit2_()
206 strm->state = (struct internal_state FAR *)state; in inflateInit2_()
207 state->strm = strm; in inflateInit2_()
208 state->window = Z_NULL; in inflateInit2_()
209 state->mode = HEAD; /* to pass state test in inflateReset2() */ in inflateInit2_()
212 ZFREE(strm, state); in inflateInit2_()
213 strm->state = Z_NULL; in inflateInit2_()
224 struct inflate_state FAR *state; in inflatePrime() local
229 state = (struct inflate_state FAR *)strm->state; in inflatePrime()
231 state->hold = 0; in inflatePrime()
232 state->bits = 0; in inflatePrime()
235 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; in inflatePrime()
236 value &= (1L << bits) - 1; in inflatePrime()
237 state->hold += (unsigned)value << state->bits; in inflatePrime()
238 state->bits += (uInt)bits; in inflatePrime()
243 Return state with length and distance decoding tables and index sizes set to
250 may not be thread-safe.
252 local void fixedtables(struct inflate_state FAR *state) { in fixedtables() argument
265 while (sym < 144) state->lens[sym++] = 8; in fixedtables()
266 while (sym < 256) state->lens[sym++] = 9; in fixedtables()
267 while (sym < 280) state->lens[sym++] = 7; in fixedtables()
268 while (sym < 288) state->lens[sym++] = 8; in fixedtables()
272 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); in fixedtables()
276 while (sym < 32) state->lens[sym++] = 5; in fixedtables()
279 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); in fixedtables()
287 state->lencode = lenfix; in fixedtables()
288 state->lenbits = 9; in fixedtables()
289 state->distcode = distfix; in fixedtables()
290 state->distbits = 5; in fixedtables()
317 struct inflate_state state; in makefixed() local
319 fixedtables(&state); in makefixed()
320 puts(" /* inffixed.h -- table for decoding fixed codes"); in makefixed()
334 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, in makefixed()
335 state.lencode[low].bits, state.lencode[low].val); in makefixed()
345 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, in makefixed()
346 state.distcode[low].val); in makefixed()
369 struct inflate_state FAR *state; in updatewindow() local
372 state = (struct inflate_state FAR *)strm->state; in updatewindow()
375 if (state->window == Z_NULL) { in updatewindow()
376 state->window = (unsigned char FAR *) in updatewindow()
377 ZALLOC(strm, 1U << state->wbits, in updatewindow()
379 if (state->window == Z_NULL) return 1; in updatewindow()
383 if (state->wsize == 0) { in updatewindow()
384 state->wsize = 1U << state->wbits; in updatewindow()
385 state->wnext = 0; in updatewindow()
386 state->whave = 0; in updatewindow()
389 /* copy state->wsize or less output bytes into the circular window */ in updatewindow()
390 if (copy >= state->wsize) { in updatewindow()
391 zmemcpy(state->window, end - state->wsize, state->wsize); in updatewindow()
392 state->wnext = 0; in updatewindow()
393 state->whave = state->wsize; in updatewindow()
396 dist = state->wsize - state->wnext; in updatewindow()
398 zmemcpy(state->window + state->wnext, end - copy, dist); in updatewindow()
399 copy -= dist; in updatewindow()
401 zmemcpy(state->window, end - copy, copy); in updatewindow()
402 state->wnext = copy; in updatewindow()
403 state->whave = state->wsize; in updatewindow()
406 state->wnext += dist; in updatewindow()
407 if (state->wnext == state->wsize) state->wnext = 0; in updatewindow()
408 if (state->whave < state->wsize) state->whave += dist; in updatewindow()
419 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
443 /* Load registers with state in inflate() for speed */
446 put = strm->next_out; \
447 left = strm->avail_out; \
448 next = strm->next_in; \
449 have = strm->avail_in; \
450 hold = state->hold; \
451 bits = state->bits; \
454 /* Restore state from registers in inflate() */
457 strm->next_out = put; \
458 strm->avail_out = left; \
459 strm->next_in = next; \
460 strm->avail_in = have; \
461 state->hold = hold; \
462 state->bits = bits; \
477 have--; \
492 ((unsigned)hold & ((1U << (n)) - 1))
498 bits -= (unsigned)(n); \
505 bits -= bits & 7; \
509 inflate() uses a state machine to process as much input data and generate as
510 much output data as possible before returning. The state machine is
513 for (;;) switch (state) {
519 state = STATEm;
526 next state. The NEEDBITS() macro is usually the way the state evaluates
549 state information is maintained to continue the loop where it left off
551 would all have to actually be part of the saved state in case NEEDBITS()
560 state = STATEx;
563 As shown above, if the next state is also the next case, then the break
566 A state may also return if there is not enough output space available to
567 complete that state. Those states are copying stored data, writing a
573 Progress is defined as a change in either strm->avail_in or strm->avail_out.
581 strm->next_out, given the space available and the provided input--the effect
591 struct inflate_state FAR *state; in inflate() local
610 if (inflateStateCheck(strm) || strm->next_out == Z_NULL || in inflate()
611 (strm->next_in == Z_NULL && strm->avail_in != 0)) in inflate()
614 state = (struct inflate_state FAR *)strm->state; in inflate()
615 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ in inflate()
621 switch (state->mode) { in inflate()
623 if (state->wrap == 0) { in inflate()
624 state->mode = TYPEDO; in inflate()
629 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ in inflate()
630 if (state->wbits == 0) in inflate()
631 state->wbits = 15; in inflate()
632 state->check = crc32(0L, Z_NULL, 0); in inflate()
633 CRC2(state->check, hold); in inflate()
635 state->mode = FLAGS; in inflate()
638 if (state->head != Z_NULL) in inflate()
639 state->head->done = -1; in inflate()
640 if (!(state->wrap & 1) || /* check if zlib header allowed */ in inflate()
645 strm->msg = (char *)"incorrect header check"; in inflate()
646 state->mode = BAD; in inflate()
650 strm->msg = (char *)"unknown compression method"; in inflate()
651 state->mode = BAD; in inflate()
656 if (state->wbits == 0) in inflate()
657 state->wbits = len; in inflate()
658 if (len > 15 || len > state->wbits) { in inflate()
659 strm->msg = (char *)"invalid window size"; in inflate()
660 state->mode = BAD; in inflate()
663 state->dmax = 1U << len; in inflate()
664 state->flags = 0; /* indicate zlib header */ in inflate()
666 strm->adler = state->check = adler32(0L, Z_NULL, 0); in inflate()
667 state->mode = hold & 0x200 ? DICTID : TYPE; in inflate()
673 state->flags = (int)(hold); in inflate()
674 if ((state->flags & 0xff) != Z_DEFLATED) { in inflate()
675 strm->msg = (char *)"unknown compression method"; in inflate()
676 state->mode = BAD; in inflate()
679 if (state->flags & 0xe000) { in inflate()
680 strm->msg = (char *)"unknown header flags set"; in inflate()
681 state->mode = BAD; in inflate()
684 if (state->head != Z_NULL) in inflate()
685 state->head->text = (int)((hold >> 8) & 1); in inflate()
686 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
687 CRC2(state->check, hold); in inflate()
689 state->mode = TIME; in inflate()
693 if (state->head != Z_NULL) in inflate()
694 state->head->time = hold; in inflate()
695 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
696 CRC4(state->check, hold); in inflate()
698 state->mode = OS; in inflate()
702 if (state->head != Z_NULL) { in inflate()
703 state->head->xflags = (int)(hold & 0xff); in inflate()
704 state->head->os = (int)(hold >> 8); in inflate()
706 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
707 CRC2(state->check, hold); in inflate()
709 state->mode = EXLEN; in inflate()
712 if (state->flags & 0x0400) { in inflate()
714 state->length = (unsigned)(hold); in inflate()
715 if (state->head != Z_NULL) in inflate()
716 state->head->extra_len = (unsigned)hold; in inflate()
717 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
718 CRC2(state->check, hold); in inflate()
721 else if (state->head != Z_NULL) in inflate()
722 state->head->extra = Z_NULL; in inflate()
723 state->mode = EXTRA; in inflate()
726 if (state->flags & 0x0400) { in inflate()
727 copy = state->length; in inflate()
730 if (state->head != Z_NULL && in inflate()
731 state->head->extra != Z_NULL && in inflate()
732 (len = state->head->extra_len - state->length) < in inflate()
733 state->head->extra_max) { in inflate()
734 zmemcpy(state->head->extra + len, next, in inflate()
735 len + copy > state->head->extra_max ? in inflate()
736 state->head->extra_max - len : copy); in inflate()
738 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
739 state->check = crc32(state->check, next, copy); in inflate()
740 have -= copy; in inflate()
742 state->length -= copy; in inflate()
744 if (state->length) goto inf_leave; in inflate()
746 state->length = 0; in inflate()
747 state->mode = NAME; in inflate()
750 if (state->flags & 0x0800) { in inflate()
755 if (state->head != Z_NULL && in inflate()
756 state->head->name != Z_NULL && in inflate()
757 state->length < state->head->name_max) in inflate()
758 state->head->name[state->length++] = (Bytef)len; in inflate()
760 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
761 state->check = crc32(state->check, next, copy); in inflate()
762 have -= copy; in inflate()
766 else if (state->head != Z_NULL) in inflate()
767 state->head->name = Z_NULL; in inflate()
768 state->length = 0; in inflate()
769 state->mode = COMMENT; in inflate()
772 if (state->flags & 0x1000) { in inflate()
777 if (state->head != Z_NULL && in inflate()
778 state->head->comment != Z_NULL && in inflate()
779 state->length < state->head->comm_max) in inflate()
780 state->head->comment[state->length++] = (Bytef)len; in inflate()
782 if ((state->flags & 0x0200) && (state->wrap & 4)) in inflate()
783 state->check = crc32(state->check, next, copy); in inflate()
784 have -= copy; in inflate()
788 else if (state->head != Z_NULL) in inflate()
789 state->head->comment = Z_NULL; in inflate()
790 state->mode = HCRC; in inflate()
793 if (state->flags & 0x0200) { in inflate()
795 if ((state->wrap & 4) && hold != (state->check & 0xffff)) { in inflate()
796 strm->msg = (char *)"header crc mismatch"; in inflate()
797 state->mode = BAD; in inflate()
802 if (state->head != Z_NULL) { in inflate()
803 state->head->hcrc = (int)((state->flags >> 9) & 1); in inflate()
804 state->head->done = 1; in inflate()
806 strm->adler = state->check = crc32(0L, Z_NULL, 0); in inflate()
807 state->mode = TYPE; in inflate()
812 strm->adler = state->check = ZSWAP32(hold); in inflate()
814 state->mode = DICT; in inflate()
817 if (state->havedict == 0) { in inflate()
821 strm->adler = state->check = adler32(0L, Z_NULL, 0); in inflate()
822 state->mode = TYPE; in inflate()
828 if (state->last) { in inflate()
830 state->mode = CHECK; in inflate()
834 state->last = BITS(1); in inflate()
839 state->last ? " (last)" : "")); in inflate()
840 state->mode = STORED; in inflate()
843 fixedtables(state); in inflate()
845 state->last ? " (last)" : "")); in inflate()
846 state->mode = LEN_; /* decode codes */ in inflate()
854 state->last ? " (last)" : "")); in inflate()
855 state->mode = TABLE; in inflate()
858 strm->msg = (char *)"invalid block type"; in inflate()
859 state->mode = BAD; in inflate()
867 strm->msg = (char *)"invalid stored block lengths"; in inflate()
868 state->mode = BAD; in inflate()
871 state->length = (unsigned)hold & 0xffff; in inflate()
873 state->length)); in inflate()
875 state->mode = COPY_; in inflate()
879 state->mode = COPY; in inflate()
882 copy = state->length; in inflate()
888 have -= copy; in inflate()
890 left -= copy; in inflate()
892 state->length -= copy; in inflate()
896 state->mode = TYPE; in inflate()
900 state->nlen = BITS(5) + 257; in inflate()
902 state->ndist = BITS(5) + 1; in inflate()
904 state->ncode = BITS(4) + 4; in inflate()
907 if (state->nlen > 286 || state->ndist > 30) { in inflate()
908 strm->msg = (char *)"too many length or distance symbols"; in inflate()
909 state->mode = BAD; in inflate()
914 state->have = 0; in inflate()
915 state->mode = LENLENS; in inflate()
918 while (state->have < state->ncode) { in inflate()
920 state->lens[order[state->have++]] = (unsigned short)BITS(3); in inflate()
923 while (state->have < 19) in inflate()
924 state->lens[order[state->have++]] = 0; in inflate()
925 state->next = state->codes; in inflate()
926 state->lencode = (const code FAR *)(state->next); in inflate()
927 state->lenbits = 7; in inflate()
928 ret = inflate_table(CODES, state->lens, 19, &(state->next), in inflate()
929 &(state->lenbits), state->work); in inflate()
931 strm->msg = (char *)"invalid code lengths set"; in inflate()
932 state->mode = BAD; in inflate()
936 state->have = 0; in inflate()
937 state->mode = CODELENS; in inflate()
940 while (state->have < state->nlen + state->ndist) { in inflate()
942 here = state->lencode[BITS(state->lenbits)]; in inflate()
948 state->lens[state->have++] = here.val; in inflate()
954 if (state->have == 0) { in inflate()
955 strm->msg = (char *)"invalid bit length repeat"; in inflate()
956 state->mode = BAD; in inflate()
959 len = state->lens[state->have - 1]; in inflate()
977 if (state->have + copy > state->nlen + state->ndist) { in inflate()
978 strm->msg = (char *)"invalid bit length repeat"; in inflate()
979 state->mode = BAD; in inflate()
982 while (copy--) in inflate()
983 state->lens[state->have++] = (unsigned short)len; in inflate()
988 if (state->mode == BAD) break; in inflate()
990 /* check for end-of-block code (better have one) */ in inflate()
991 if (state->lens[256] == 0) { in inflate()
992 strm->msg = (char *)"invalid code -- missing end-of-block"; in inflate()
993 state->mode = BAD; in inflate()
997 /* build code tables -- note: do not change the lenbits or distbits in inflate()
1000 state->next = state->codes; in inflate()
1001 state->lencode = (const code FAR *)(state->next); in inflate()
1002 state->lenbits = 9; in inflate()
1003 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), in inflate()
1004 &(state->lenbits), state->work); in inflate()
1006 strm->msg = (char *)"invalid literal/lengths set"; in inflate()
1007 state->mode = BAD; in inflate()
1010 state->distcode = (const code FAR *)(state->next); in inflate()
1011 state->distbits = 6; in inflate()
1012 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, in inflate()
1013 &(state->next), &(state->distbits), state->work); in inflate()
1015 strm->msg = (char *)"invalid distances set"; in inflate()
1016 state->mode = BAD; in inflate()
1020 state->mode = LEN_; in inflate()
1024 state->mode = LEN; in inflate()
1031 if (state->mode == TYPE) in inflate()
1032 state->back = -1; in inflate()
1035 state->back = 0; in inflate()
1037 here = state->lencode[BITS(state->lenbits)]; in inflate()
1044 here = state->lencode[last.val + in inflate()
1050 state->back += last.bits; in inflate()
1053 state->back += here.bits; in inflate()
1054 state->length = (unsigned)here.val; in inflate()
1059 state->mode = LIT; in inflate()
1064 state->back = -1; in inflate()
1065 state->mode = TYPE; in inflate()
1069 strm->msg = (char *)"invalid literal/length code"; in inflate()
1070 state->mode = BAD; in inflate()
1073 state->extra = (unsigned)(here.op) & 15; in inflate()
1074 state->mode = LENEXT; in inflate()
1077 if (state->extra) { in inflate()
1078 NEEDBITS(state->extra); in inflate()
1079 state->length += BITS(state->extra); in inflate()
1080 DROPBITS(state->extra); in inflate()
1081 state->back += state->extra; in inflate()
1083 Tracevv((stderr, "inflate: length %u\n", state->length)); in inflate()
1084 state->was = state->length; in inflate()
1085 state->mode = DIST; in inflate()
1089 here = state->distcode[BITS(state->distbits)]; in inflate()
1096 here = state->distcode[last.val + in inflate()
1102 state->back += last.bits; in inflate()
1105 state->back += here.bits; in inflate()
1107 strm->msg = (char *)"invalid distance code"; in inflate()
1108 state->mode = BAD; in inflate()
1111 state->offset = (unsigned)here.val; in inflate()
1112 state->extra = (unsigned)(here.op) & 15; in inflate()
1113 state->mode = DISTEXT; in inflate()
1116 if (state->extra) { in inflate()
1117 NEEDBITS(state->extra); in inflate()
1118 state->offset += BITS(state->extra); in inflate()
1119 DROPBITS(state->extra); in inflate()
1120 state->back += state->extra; in inflate()
1123 if (state->offset > state->dmax) { in inflate()
1124 strm->msg = (char *)"invalid distance too far back"; in inflate()
1125 state->mode = BAD; in inflate()
1129 Tracevv((stderr, "inflate: distance %u\n", state->offset)); in inflate()
1130 state->mode = MATCH; in inflate()
1134 copy = out - left; in inflate()
1135 if (state->offset > copy) { /* copy from window */ in inflate()
1136 copy = state->offset - copy; in inflate()
1137 if (copy > state->whave) { in inflate()
1138 if (state->sane) { in inflate()
1139 strm->msg = (char *)"invalid distance too far back"; in inflate()
1140 state->mode = BAD; in inflate()
1145 copy -= state->whave; in inflate()
1146 if (copy > state->length) copy = state->length; in inflate()
1148 left -= copy; in inflate()
1149 state->length -= copy; in inflate()
1152 } while (--copy); in inflate()
1153 if (state->length == 0) state->mode = LEN; in inflate()
1157 if (copy > state->wnext) { in inflate()
1158 copy -= state->wnext; in inflate()
1159 from = state->window + (state->wsize - copy); in inflate()
1162 from = state->window + (state->wnext - copy); in inflate()
1163 if (copy > state->length) copy = state->length; in inflate()
1166 from = put - state->offset; in inflate()
1167 copy = state->length; in inflate()
1170 left -= copy; in inflate()
1171 state->length -= copy; in inflate()
1174 } while (--copy); in inflate()
1175 if (state->length == 0) state->mode = LEN; in inflate()
1179 *put++ = (unsigned char)(state->length); in inflate()
1180 left--; in inflate()
1181 state->mode = LEN; in inflate()
1184 if (state->wrap) { in inflate()
1186 out -= left; in inflate()
1187 strm->total_out += out; in inflate()
1188 state->total += out; in inflate()
1189 if ((state->wrap & 4) && out) in inflate()
1190 strm->adler = state->check = in inflate()
1191 UPDATE_CHECK(state->check, put - out, out); in inflate()
1193 if ((state->wrap & 4) && ( in inflate()
1195 state->flags ? hold : in inflate()
1197 ZSWAP32(hold)) != state->check) { in inflate()
1198 strm->msg = (char *)"incorrect data check"; in inflate()
1199 state->mode = BAD; in inflate()
1206 state->mode = LENGTH; in inflate()
1209 if (state->wrap && state->flags) { in inflate()
1211 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) { in inflate()
1212 strm->msg = (char *)"incorrect length check"; in inflate()
1213 state->mode = BAD; in inflate()
1220 state->mode = DONE; in inflate()
1239 error. Call updatewindow() to create and/or update the window state. in inflate()
1240 Note: a memory error from inflate() is non-recoverable. in inflate()
1244 if (state->wsize || (out != strm->avail_out && state->mode < BAD && in inflate()
1245 (state->mode < CHECK || flush != Z_FINISH))) in inflate()
1246 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { in inflate()
1247 state->mode = MEM; in inflate()
1250 in -= strm->avail_in; in inflate()
1251 out -= strm->avail_out; in inflate()
1252 strm->total_in += in; in inflate()
1253 strm->total_out += out; in inflate()
1254 state->total += out; in inflate()
1255 if ((state->wrap & 4) && out) in inflate()
1256 strm->adler = state->check = in inflate()
1257 UPDATE_CHECK(state->check, strm->next_out - out, out); in inflate()
1258 strm->data_type = (int)state->bits + (state->last ? 64 : 0) + in inflate()
1259 (state->mode == TYPE ? 128 : 0) + in inflate()
1260 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); in inflate()
1267 struct inflate_state FAR *state; in inflateEnd() local
1270 state = (struct inflate_state FAR *)strm->state; in inflateEnd()
1271 if (state->window != Z_NULL) ZFREE(strm, state->window); in inflateEnd()
1272 ZFREE(strm, strm->state); in inflateEnd()
1273 strm->state = Z_NULL; in inflateEnd()
1280 struct inflate_state FAR *state; in inflateGetDictionary() local
1282 /* check state */ in inflateGetDictionary()
1284 state = (struct inflate_state FAR *)strm->state; in inflateGetDictionary()
1287 if (state->whave && dictionary != Z_NULL) { in inflateGetDictionary()
1288 zmemcpy(dictionary, state->window + state->wnext, in inflateGetDictionary()
1289 state->whave - state->wnext); in inflateGetDictionary()
1290 zmemcpy(dictionary + state->whave - state->wnext, in inflateGetDictionary()
1291 state->window, state->wnext); in inflateGetDictionary()
1294 *dictLength = state->whave; in inflateGetDictionary()
1300 struct inflate_state FAR *state; in inflateSetDictionary() local
1304 /* check state */ in inflateSetDictionary()
1306 state = (struct inflate_state FAR *)strm->state; in inflateSetDictionary()
1307 if (state->wrap != 0 && state->mode != DICT) in inflateSetDictionary()
1311 if (state->mode == DICT) { in inflateSetDictionary()
1314 if (dictid != state->check) in inflateSetDictionary()
1322 state->mode = MEM; in inflateSetDictionary()
1325 state->havedict = 1; in inflateSetDictionary()
1331 struct inflate_state FAR *state; in inflateGetHeader() local
1333 /* check state */ in inflateGetHeader()
1335 state = (struct inflate_state FAR *)strm->state; in inflateGetHeader()
1336 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; in inflateGetHeader()
1339 state->head = head; in inflateGetHeader()
1340 head->done = 0; in inflateGetHeader()
1345 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1348 state. If on return *have equals four, then the pattern was found and the
1352 called again with more data and the *have state. *have is initialized to
1368 got = 4 - got; in syncsearch()
1380 struct inflate_state FAR *state; in inflateSync() local
1384 state = (struct inflate_state FAR *)strm->state; in inflateSync()
1385 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; in inflateSync()
1388 if (state->mode != SYNC) { in inflateSync()
1389 state->mode = SYNC; in inflateSync()
1390 state->hold >>= state->bits & 7; in inflateSync()
1391 state->bits -= state->bits & 7; in inflateSync()
1393 while (state->bits >= 8) { in inflateSync()
1394 buf[len++] = (unsigned char)(state->hold); in inflateSync()
1395 state->hold >>= 8; in inflateSync()
1396 state->bits -= 8; in inflateSync()
1398 state->have = 0; in inflateSync()
1399 syncsearch(&(state->have), buf, len); in inflateSync()
1403 len = syncsearch(&(state->have), strm->next_in, strm->avail_in); in inflateSync()
1404 strm->avail_in -= len; in inflateSync()
1405 strm->next_in += len; in inflateSync()
1406 strm->total_in += len; in inflateSync()
1409 if (state->have != 4) return Z_DATA_ERROR; in inflateSync()
1410 if (state->flags == -1) in inflateSync()
1411 state->wrap = 0; /* if no header yet, treat as raw */ in inflateSync()
1413 state->wrap &= ~4; /* no point in computing a check value now */ in inflateSync()
1414 flags = state->flags; in inflateSync()
1415 in = strm->total_in; out = strm->total_out; in inflateSync()
1417 strm->total_in = in; strm->total_out = out; in inflateSync()
1418 state->flags = flags; in inflateSync()
1419 state->mode = TYPE; in inflateSync()
1432 struct inflate_state FAR *state; in inflateSyncPoint() local
1435 state = (struct inflate_state FAR *)strm->state; in inflateSyncPoint()
1436 return state->mode == STORED && state->bits == 0; in inflateSyncPoint()
1440 struct inflate_state FAR *state; in inflateCopy() local
1448 state = (struct inflate_state FAR *)source->state; in inflateCopy()
1455 if (state->window != Z_NULL) { in inflateCopy()
1457 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); in inflateCopy()
1464 /* copy state */ in inflateCopy()
1466 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); in inflateCopy()
1467 copy->strm = dest; in inflateCopy()
1468 if (state->lencode >= state->codes && in inflateCopy()
1469 state->lencode <= state->codes + ENOUGH - 1) { in inflateCopy()
1470 copy->lencode = copy->codes + (state->lencode - state->codes); in inflateCopy()
1471 copy->distcode = copy->codes + (state->distcode - state->codes); in inflateCopy()
1473 copy->next = copy->codes + (state->next - state->codes); in inflateCopy()
1475 wsize = 1U << state->wbits; in inflateCopy()
1476 zmemcpy(window, state->window, wsize); in inflateCopy()
1478 copy->window = window; in inflateCopy()
1479 dest->state = (struct internal_state FAR *)copy; in inflateCopy()
1484 struct inflate_state FAR *state; in inflateUndermine() local
1487 state = (struct inflate_state FAR *)strm->state; in inflateUndermine()
1489 state->sane = !subvert; in inflateUndermine()
1493 state->sane = 1; in inflateUndermine()
1499 struct inflate_state FAR *state; in inflateValidate() local
1502 state = (struct inflate_state FAR *)strm->state; in inflateValidate()
1503 if (check && state->wrap) in inflateValidate()
1504 state->wrap |= 4; in inflateValidate()
1506 state->wrap &= ~4; in inflateValidate()
1511 struct inflate_state FAR *state; in inflateMark() local
1514 return -(1L << 16); in inflateMark()
1515 state = (struct inflate_state FAR *)strm->state; in inflateMark()
1516 return (long)(((unsigned long)((long)state->back)) << 16) + in inflateMark()
1517 (state->mode == COPY ? state->length : in inflateMark()
1518 (state->mode == MATCH ? state->was - state->length : 0)); in inflateMark()
1522 struct inflate_state FAR *state; in inflateCodesUsed() local
1523 if (inflateStateCheck(strm)) return (unsigned long)-1; in inflateCodesUsed()
1524 state = (struct inflate_state FAR *)strm->state; in inflateCodesUsed()
1525 return (unsigned long)(state->next - state->codes); in inflateCodesUsed()