OLD | NEW |
1 /* inflate.c -- zlib decompression | 1 /* inflate.c -- zlib decompression |
2 * Copyright (C) 1995-2012 Mark Adler | 2 * Copyright (C) 1995-2016 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
4 */ | 4 */ |
5 | 5 |
6 /* | 6 /* |
7 * Change history: | 7 * Change history: |
8 * | 8 * |
9 * 1.2.beta0 24 Nov 2002 | 9 * 1.2.beta0 24 Nov 2002 |
10 * - First version -- complete rewrite of inflate to simplify code, avoid | 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 | 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 | 12 * needed, make inffast.c even faster, implement gzip decoding, and to |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 #include "inflate.h" | 85 #include "inflate.h" |
86 #include "inffast.h" | 86 #include "inffast.h" |
87 | 87 |
88 #ifdef MAKEFIXED | 88 #ifdef MAKEFIXED |
89 # ifndef BUILDFIXED | 89 # ifndef BUILDFIXED |
90 # define BUILDFIXED | 90 # define BUILDFIXED |
91 # endif | 91 # endif |
92 #endif | 92 #endif |
93 | 93 |
94 /* function prototypes */ | 94 /* function prototypes */ |
| 95 local int inflateStateCheck OF((z_streamp strm)); |
95 local void fixedtables OF((struct inflate_state FAR *state)); | 96 local void fixedtables OF((struct inflate_state FAR *state)); |
96 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, | 97 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, |
97 unsigned copy)); | 98 unsigned copy)); |
98 #ifdef BUILDFIXED | 99 #ifdef BUILDFIXED |
99 void makefixed OF((void)); | 100 void makefixed OF((void)); |
100 #endif | 101 #endif |
101 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, | 102 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, |
102 unsigned len)); | 103 unsigned len)); |
103 | 104 |
| 105 local int inflateStateCheck(strm) |
| 106 z_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 |
104 int ZEXPORT inflateResetKeep(strm) | 119 int ZEXPORT inflateResetKeep(strm) |
105 z_streamp strm; | 120 z_streamp strm; |
106 { | 121 { |
107 struct inflate_state FAR *state; | 122 struct inflate_state FAR *state; |
108 | 123 |
109 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 124 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
110 state = (struct inflate_state FAR *)strm->state; | 125 state = (struct inflate_state FAR *)strm->state; |
111 strm->total_in = strm->total_out = state->total = 0; | 126 strm->total_in = strm->total_out = state->total = 0; |
112 strm->msg = Z_NULL; | 127 strm->msg = Z_NULL; |
113 if (state->wrap) /* to support ill-conceived Java test suite */ | 128 if (state->wrap) /* to support ill-conceived Java test suite */ |
114 strm->adler = state->wrap & 1; | 129 strm->adler = state->wrap & 1; |
115 state->mode = HEAD; | 130 state->mode = HEAD; |
116 state->last = 0; | 131 state->last = 0; |
117 state->havedict = 0; | 132 state->havedict = 0; |
118 state->dmax = 32768U; | 133 state->dmax = 32768U; |
119 state->head = Z_NULL; | 134 state->head = Z_NULL; |
120 state->hold = 0; | 135 state->hold = 0; |
121 state->bits = 0; | 136 state->bits = 0; |
122 state->lencode = state->distcode = state->next = state->codes; | 137 state->lencode = state->distcode = state->next = state->codes; |
123 state->sane = 1; | 138 state->sane = 1; |
124 state->back = -1; | 139 state->back = -1; |
125 Tracev((stderr, "inflate: reset\n")); | 140 Tracev((stderr, "inflate: reset\n")); |
126 return Z_OK; | 141 return Z_OK; |
127 } | 142 } |
128 | 143 |
129 int ZEXPORT inflateReset(strm) | 144 int ZEXPORT inflateReset(strm) |
130 z_streamp strm; | 145 z_streamp strm; |
131 { | 146 { |
132 struct inflate_state FAR *state; | 147 struct inflate_state FAR *state; |
133 | 148 |
134 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 149 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
135 state = (struct inflate_state FAR *)strm->state; | 150 state = (struct inflate_state FAR *)strm->state; |
136 state->wsize = 0; | 151 state->wsize = 0; |
137 state->whave = 0; | 152 state->whave = 0; |
138 state->wnext = 0; | 153 state->wnext = 0; |
139 return inflateResetKeep(strm); | 154 return inflateResetKeep(strm); |
140 } | 155 } |
141 | 156 |
142 int ZEXPORT inflateReset2(strm, windowBits) | 157 int ZEXPORT inflateReset2(strm, windowBits) |
143 z_streamp strm; | 158 z_streamp strm; |
144 int windowBits; | 159 int windowBits; |
145 { | 160 { |
146 int wrap; | 161 int wrap; |
147 struct inflate_state FAR *state; | 162 struct inflate_state FAR *state; |
148 | 163 |
149 /* get the state */ | 164 /* get the state */ |
150 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 165 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
151 state = (struct inflate_state FAR *)strm->state; | 166 state = (struct inflate_state FAR *)strm->state; |
152 | 167 |
153 /* extract wrap request from windowBits parameter */ | 168 /* extract wrap request from windowBits parameter */ |
154 if (windowBits < 0) { | 169 if (windowBits < 0) { |
155 wrap = 0; | 170 wrap = 0; |
156 windowBits = -windowBits; | 171 windowBits = -windowBits; |
157 } | 172 } |
158 else { | 173 else { |
159 wrap = (windowBits >> 4) + 1; | 174 wrap = (windowBits >> 4) + 5; |
160 #ifdef GUNZIP | 175 #ifdef GUNZIP |
161 if (windowBits < 48) | 176 if (windowBits < 48) |
162 windowBits &= 15; | 177 windowBits &= 15; |
163 #endif | 178 #endif |
164 } | 179 } |
165 | 180 |
166 /* set number of window bits, free window if different */ | 181 /* set number of window bits, free window if different */ |
167 if (windowBits && (windowBits < 8 || windowBits > 15)) | 182 if (windowBits && (windowBits < 8 || windowBits > 15)) |
168 return Z_STREAM_ERROR; | 183 return Z_STREAM_ERROR; |
169 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { | 184 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 #ifdef Z_SOLO | 218 #ifdef Z_SOLO |
204 return Z_STREAM_ERROR; | 219 return Z_STREAM_ERROR; |
205 #else | 220 #else |
206 strm->zfree = zcfree; | 221 strm->zfree = zcfree; |
207 #endif | 222 #endif |
208 state = (struct inflate_state FAR *) | 223 state = (struct inflate_state FAR *) |
209 ZALLOC(strm, 1, sizeof(struct inflate_state)); | 224 ZALLOC(strm, 1, sizeof(struct inflate_state)); |
210 if (state == Z_NULL) return Z_MEM_ERROR; | 225 if (state == Z_NULL) return Z_MEM_ERROR; |
211 Tracev((stderr, "inflate: allocated\n")); | 226 Tracev((stderr, "inflate: allocated\n")); |
212 strm->state = (struct internal_state FAR *)state; | 227 strm->state = (struct internal_state FAR *)state; |
| 228 state->strm = strm; |
213 state->window = Z_NULL; | 229 state->window = Z_NULL; |
| 230 state->mode = HEAD; /* to pass state test in inflateReset2() */ |
214 ret = inflateReset2(strm, windowBits); | 231 ret = inflateReset2(strm, windowBits); |
215 if (ret != Z_OK) { | 232 if (ret != Z_OK) { |
216 ZFREE(strm, state); | 233 ZFREE(strm, state); |
217 strm->state = Z_NULL; | 234 strm->state = Z_NULL; |
218 } | 235 } |
219 return ret; | 236 return ret; |
220 } | 237 } |
221 | 238 |
222 int ZEXPORT inflateInit_(strm, version, stream_size) | 239 int ZEXPORT inflateInit_(strm, version, stream_size) |
223 z_streamp strm; | 240 z_streamp strm; |
224 const char *version; | 241 const char *version; |
225 int stream_size; | 242 int stream_size; |
226 { | 243 { |
227 return inflateInit2_(strm, DEF_WBITS, version, stream_size); | 244 return inflateInit2_(strm, DEF_WBITS, version, stream_size); |
228 } | 245 } |
229 | 246 |
230 int ZEXPORT inflatePrime(strm, bits, value) | 247 int ZEXPORT inflatePrime(strm, bits, value) |
231 z_streamp strm; | 248 z_streamp strm; |
232 int bits; | 249 int bits; |
233 int value; | 250 int value; |
234 { | 251 { |
235 struct inflate_state FAR *state; | 252 struct inflate_state FAR *state; |
236 | 253 |
237 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 254 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
238 state = (struct inflate_state FAR *)strm->state; | 255 state = (struct inflate_state FAR *)strm->state; |
239 if (bits < 0) { | 256 if (bits < 0) { |
240 state->hold = 0; | 257 state->hold = 0; |
241 state->bits = 0; | 258 state->bits = 0; |
242 return Z_OK; | 259 return Z_OK; |
243 } | 260 } |
244 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; | 261 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; |
245 value &= (1L << bits) - 1; | 262 value &= (1L << bits) - 1; |
246 state->hold += value << state->bits; | 263 state->hold += (unsigned)value << state->bits; |
247 state->bits += bits; | 264 state->bits += (uInt)bits; |
248 return Z_OK; | 265 return Z_OK; |
249 } | 266 } |
250 | 267 |
251 /* | 268 /* |
252 Return state with length and distance decoding tables and index sizes set to | 269 Return state with length and distance decoding tables and index sizes set to |
253 fixed code decoding. Normally this returns fixed tables from inffixed.h. | 270 fixed code decoding. Normally this returns fixed tables from inffixed.h. |
254 If BUILDFIXED is defined, then instead this routine builds the tables the | 271 If BUILDFIXED is defined, then instead this routine builds the tables the |
255 first time it's called, and returns those tables the first time and | 272 first time it's called, and returns those tables the first time and |
256 thereafter. This reduces the size of the code by about 2K bytes, in | 273 thereafter. This reduces the size of the code by about 2K bytes, in |
257 exchange for a little execution time. However, BUILDFIXED should not be | 274 exchange for a little execution time. However, BUILDFIXED should not be |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 code here; /* current decoding table entry */ | 635 code here; /* current decoding table entry */ |
619 code last; /* parent table entry */ | 636 code last; /* parent table entry */ |
620 unsigned len; /* length to copy for repeats, bits to drop */ | 637 unsigned len; /* length to copy for repeats, bits to drop */ |
621 int ret; /* return code */ | 638 int ret; /* return code */ |
622 #ifdef GUNZIP | 639 #ifdef GUNZIP |
623 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ | 640 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ |
624 #endif | 641 #endif |
625 static const unsigned short order[19] = /* permutation of code lengths */ | 642 static const unsigned short order[19] = /* permutation of code lengths */ |
626 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 643 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
627 | 644 |
628 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || | 645 if (inflateStateCheck(strm) || strm->next_out == Z_NULL || |
629 (strm->next_in == Z_NULL && strm->avail_in != 0)) | 646 (strm->next_in == Z_NULL && strm->avail_in != 0)) |
630 return Z_STREAM_ERROR; | 647 return Z_STREAM_ERROR; |
631 | 648 |
632 state = (struct inflate_state FAR *)strm->state; | 649 state = (struct inflate_state FAR *)strm->state; |
633 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ | 650 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ |
634 LOAD(); | 651 LOAD(); |
635 in = have; | 652 in = have; |
636 out = left; | 653 out = left; |
637 ret = Z_OK; | 654 ret = Z_OK; |
638 for (;;) | 655 for (;;) |
639 switch (state->mode) { | 656 switch (state->mode) { |
640 case HEAD: | 657 case HEAD: |
641 if (state->wrap == 0) { | 658 if (state->wrap == 0) { |
642 state->mode = TYPEDO; | 659 state->mode = TYPEDO; |
643 break; | 660 break; |
644 } | 661 } |
645 NEEDBITS(16); | 662 NEEDBITS(16); |
646 #ifdef GUNZIP | 663 #ifdef GUNZIP |
647 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ | 664 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ |
| 665 if (state->wbits == 0) |
| 666 state->wbits = 15; |
648 state->check = crc32(0L, Z_NULL, 0); | 667 state->check = crc32(0L, Z_NULL, 0); |
649 CRC2(state->check, hold); | 668 CRC2(state->check, hold); |
650 INITBITS(); | 669 INITBITS(); |
651 state->mode = FLAGS; | 670 state->mode = FLAGS; |
652 break; | 671 break; |
653 } | 672 } |
654 state->flags = 0; /* expect zlib header */ | 673 state->flags = 0; /* expect zlib header */ |
655 if (state->head != Z_NULL) | 674 if (state->head != Z_NULL) |
656 state->head->done = -1; | 675 state->head->done = -1; |
657 if (!(state->wrap & 1) || /* check if zlib header allowed */ | 676 if (!(state->wrap & 1) || /* check if zlib header allowed */ |
658 #else | 677 #else |
659 if ( | 678 if ( |
660 #endif | 679 #endif |
661 ((BITS(8) << 8) + (hold >> 8)) % 31) { | 680 ((BITS(8) << 8) + (hold >> 8)) % 31) { |
662 strm->msg = (char *)"incorrect header check"; | 681 strm->msg = (char *)"incorrect header check"; |
663 state->mode = BAD; | 682 state->mode = BAD; |
664 break; | 683 break; |
665 } | 684 } |
666 if (BITS(4) != Z_DEFLATED) { | 685 if (BITS(4) != Z_DEFLATED) { |
667 strm->msg = (char *)"unknown compression method"; | 686 strm->msg = (char *)"unknown compression method"; |
668 state->mode = BAD; | 687 state->mode = BAD; |
669 break; | 688 break; |
670 } | 689 } |
671 DROPBITS(4); | 690 DROPBITS(4); |
672 len = BITS(4) + 8; | 691 len = BITS(4) + 8; |
673 if (state->wbits == 0) | 692 if (state->wbits == 0) |
674 state->wbits = len; | 693 state->wbits = len; |
675 else if (len > state->wbits) { | 694 if (len > 15 || len > state->wbits) { |
676 strm->msg = (char *)"invalid window size"; | 695 strm->msg = (char *)"invalid window size"; |
677 state->mode = BAD; | 696 state->mode = BAD; |
678 break; | 697 break; |
679 } | 698 } |
680 state->dmax = 1U << len; | 699 state->dmax = 1U << len; |
681 Tracev((stderr, "inflate: zlib header ok\n")); | 700 Tracev((stderr, "inflate: zlib header ok\n")); |
682 strm->adler = state->check = adler32(0L, Z_NULL, 0); | 701 strm->adler = state->check = adler32(0L, Z_NULL, 0); |
683 state->mode = hold & 0x200 ? DICTID : TYPE; | 702 state->mode = hold & 0x200 ? DICTID : TYPE; |
684 INITBITS(); | 703 INITBITS(); |
685 break; | 704 break; |
686 #ifdef GUNZIP | 705 #ifdef GUNZIP |
687 case FLAGS: | 706 case FLAGS: |
688 NEEDBITS(16); | 707 NEEDBITS(16); |
689 state->flags = (int)(hold); | 708 state->flags = (int)(hold); |
690 if ((state->flags & 0xff) != Z_DEFLATED) { | 709 if ((state->flags & 0xff) != Z_DEFLATED) { |
691 strm->msg = (char *)"unknown compression method"; | 710 strm->msg = (char *)"unknown compression method"; |
692 state->mode = BAD; | 711 state->mode = BAD; |
693 break; | 712 break; |
694 } | 713 } |
695 if (state->flags & 0xe000) { | 714 if (state->flags & 0xe000) { |
696 strm->msg = (char *)"unknown header flags set"; | 715 strm->msg = (char *)"unknown header flags set"; |
697 state->mode = BAD; | 716 state->mode = BAD; |
698 break; | 717 break; |
699 } | 718 } |
700 if (state->head != Z_NULL) | 719 if (state->head != Z_NULL) |
701 state->head->text = (int)((hold >> 8) & 1); | 720 state->head->text = (int)((hold >> 8) & 1); |
702 if (state->flags & 0x0200) CRC2(state->check, hold); | 721 if ((state->flags & 0x0200) && (state->wrap & 4)) |
| 722 CRC2(state->check, hold); |
703 INITBITS(); | 723 INITBITS(); |
704 state->mode = TIME; | 724 state->mode = TIME; |
705 case TIME: | 725 case TIME: |
706 NEEDBITS(32); | 726 NEEDBITS(32); |
707 if (state->head != Z_NULL) | 727 if (state->head != Z_NULL) |
708 state->head->time = hold; | 728 state->head->time = hold; |
709 if (state->flags & 0x0200) CRC4(state->check, hold); | 729 if ((state->flags & 0x0200) && (state->wrap & 4)) |
| 730 CRC4(state->check, hold); |
710 INITBITS(); | 731 INITBITS(); |
711 state->mode = OS; | 732 state->mode = OS; |
712 case OS: | 733 case OS: |
713 NEEDBITS(16); | 734 NEEDBITS(16); |
714 if (state->head != Z_NULL) { | 735 if (state->head != Z_NULL) { |
715 state->head->xflags = (int)(hold & 0xff); | 736 state->head->xflags = (int)(hold & 0xff); |
716 state->head->os = (int)(hold >> 8); | 737 state->head->os = (int)(hold >> 8); |
717 } | 738 } |
718 if (state->flags & 0x0200) CRC2(state->check, hold); | 739 if ((state->flags & 0x0200) && (state->wrap & 4)) |
| 740 CRC2(state->check, hold); |
719 INITBITS(); | 741 INITBITS(); |
720 state->mode = EXLEN; | 742 state->mode = EXLEN; |
721 case EXLEN: | 743 case EXLEN: |
722 if (state->flags & 0x0400) { | 744 if (state->flags & 0x0400) { |
723 NEEDBITS(16); | 745 NEEDBITS(16); |
724 state->length = (unsigned)(hold); | 746 state->length = (unsigned)(hold); |
725 if (state->head != Z_NULL) | 747 if (state->head != Z_NULL) |
726 state->head->extra_len = (unsigned)hold; | 748 state->head->extra_len = (unsigned)hold; |
727 if (state->flags & 0x0200) CRC2(state->check, hold); | 749 if ((state->flags & 0x0200) && (state->wrap & 4)) |
| 750 CRC2(state->check, hold); |
728 INITBITS(); | 751 INITBITS(); |
729 } | 752 } |
730 else if (state->head != Z_NULL) | 753 else if (state->head != Z_NULL) |
731 state->head->extra = Z_NULL; | 754 state->head->extra = Z_NULL; |
732 state->mode = EXTRA; | 755 state->mode = EXTRA; |
733 case EXTRA: | 756 case EXTRA: |
734 if (state->flags & 0x0400) { | 757 if (state->flags & 0x0400) { |
735 copy = state->length; | 758 copy = state->length; |
736 if (copy > have) copy = have; | 759 if (copy > have) copy = have; |
737 if (copy) { | 760 if (copy) { |
738 if (state->head != Z_NULL && | 761 if (state->head != Z_NULL && |
739 state->head->extra != Z_NULL) { | 762 state->head->extra != Z_NULL) { |
740 len = state->head->extra_len - state->length; | 763 len = state->head->extra_len - state->length; |
741 zmemcpy(state->head->extra + len, next, | 764 zmemcpy(state->head->extra + len, next, |
742 len + copy > state->head->extra_max ? | 765 len + copy > state->head->extra_max ? |
743 state->head->extra_max - len : copy); | 766 state->head->extra_max - len : copy); |
744 } | 767 } |
745 if (state->flags & 0x0200) | 768 if ((state->flags & 0x0200) && (state->wrap & 4)) |
746 state->check = crc32(state->check, next, copy); | 769 state->check = crc32(state->check, next, copy); |
747 have -= copy; | 770 have -= copy; |
748 next += copy; | 771 next += copy; |
749 state->length -= copy; | 772 state->length -= copy; |
750 } | 773 } |
751 if (state->length) goto inf_leave; | 774 if (state->length) goto inf_leave; |
752 } | 775 } |
753 state->length = 0; | 776 state->length = 0; |
754 state->mode = NAME; | 777 state->mode = NAME; |
755 case NAME: | 778 case NAME: |
756 if (state->flags & 0x0800) { | 779 if (state->flags & 0x0800) { |
757 if (have == 0) goto inf_leave; | 780 if (have == 0) goto inf_leave; |
758 copy = 0; | 781 copy = 0; |
759 do { | 782 do { |
760 len = (unsigned)(next[copy++]); | 783 len = (unsigned)(next[copy++]); |
761 if (state->head != Z_NULL && | 784 if (state->head != Z_NULL && |
762 state->head->name != Z_NULL && | 785 state->head->name != Z_NULL && |
763 state->length < state->head->name_max) | 786 state->length < state->head->name_max) |
764 state->head->name[state->length++] = len; | 787 state->head->name[state->length++] = (Bytef)len; |
765 } while (len && copy < have); | 788 } while (len && copy < have); |
766 if (state->flags & 0x0200) | 789 if ((state->flags & 0x0200) && (state->wrap & 4)) |
767 state->check = crc32(state->check, next, copy); | 790 state->check = crc32(state->check, next, copy); |
768 have -= copy; | 791 have -= copy; |
769 next += copy; | 792 next += copy; |
770 if (len) goto inf_leave; | 793 if (len) goto inf_leave; |
771 } | 794 } |
772 else if (state->head != Z_NULL) | 795 else if (state->head != Z_NULL) |
773 state->head->name = Z_NULL; | 796 state->head->name = Z_NULL; |
774 state->length = 0; | 797 state->length = 0; |
775 state->mode = COMMENT; | 798 state->mode = COMMENT; |
776 case COMMENT: | 799 case COMMENT: |
777 if (state->flags & 0x1000) { | 800 if (state->flags & 0x1000) { |
778 if (have == 0) goto inf_leave; | 801 if (have == 0) goto inf_leave; |
779 copy = 0; | 802 copy = 0; |
780 do { | 803 do { |
781 len = (unsigned)(next[copy++]); | 804 len = (unsigned)(next[copy++]); |
782 if (state->head != Z_NULL && | 805 if (state->head != Z_NULL && |
783 state->head->comment != Z_NULL && | 806 state->head->comment != Z_NULL && |
784 state->length < state->head->comm_max) | 807 state->length < state->head->comm_max) |
785 state->head->comment[state->length++] = len; | 808 state->head->comment[state->length++] = (Bytef)len; |
786 } while (len && copy < have); | 809 } while (len && copy < have); |
787 if (state->flags & 0x0200) | 810 if ((state->flags & 0x0200) && (state->wrap & 4)) |
788 state->check = crc32(state->check, next, copy); | 811 state->check = crc32(state->check, next, copy); |
789 have -= copy; | 812 have -= copy; |
790 next += copy; | 813 next += copy; |
791 if (len) goto inf_leave; | 814 if (len) goto inf_leave; |
792 } | 815 } |
793 else if (state->head != Z_NULL) | 816 else if (state->head != Z_NULL) |
794 state->head->comment = Z_NULL; | 817 state->head->comment = Z_NULL; |
795 state->mode = HCRC; | 818 state->mode = HCRC; |
796 case HCRC: | 819 case HCRC: |
797 if (state->flags & 0x0200) { | 820 if (state->flags & 0x0200) { |
798 NEEDBITS(16); | 821 NEEDBITS(16); |
799 if (hold != (state->check & 0xffff)) { | 822 if ((state->wrap & 4) && hold != (state->check & 0xffff)) { |
800 strm->msg = (char *)"header crc mismatch"; | 823 strm->msg = (char *)"header crc mismatch"; |
801 state->mode = BAD; | 824 state->mode = BAD; |
802 break; | 825 break; |
803 } | 826 } |
804 INITBITS(); | 827 INITBITS(); |
805 } | 828 } |
806 if (state->head != Z_NULL) { | 829 if (state->head != Z_NULL) { |
807 state->head->hcrc = (int)((state->flags >> 9) & 1); | 830 state->head->hcrc = (int)((state->flags >> 9) & 1); |
808 state->head->done = 1; | 831 state->head->done = 1; |
809 } | 832 } |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1170 *put++ = (unsigned char)(state->length); | 1193 *put++ = (unsigned char)(state->length); |
1171 left--; | 1194 left--; |
1172 state->mode = LEN; | 1195 state->mode = LEN; |
1173 break; | 1196 break; |
1174 case CHECK: | 1197 case CHECK: |
1175 if (state->wrap) { | 1198 if (state->wrap) { |
1176 NEEDBITS(32); | 1199 NEEDBITS(32); |
1177 out -= left; | 1200 out -= left; |
1178 strm->total_out += out; | 1201 strm->total_out += out; |
1179 state->total += out; | 1202 state->total += out; |
1180 if (out) | 1203 if ((state->wrap & 4) && out) |
1181 strm->adler = state->check = | 1204 strm->adler = state->check = |
1182 UPDATE(state->check, put - out, out); | 1205 UPDATE(state->check, put - out, out); |
1183 out = left; | 1206 out = left; |
1184 if (( | 1207 if ((state->wrap & 4) && ( |
1185 #ifdef GUNZIP | 1208 #ifdef GUNZIP |
1186 state->flags ? hold : | 1209 state->flags ? hold : |
1187 #endif | 1210 #endif |
1188 ZSWAP32(hold)) != state->check) { | 1211 ZSWAP32(hold)) != state->check) { |
1189 strm->msg = (char *)"incorrect data check"; | 1212 strm->msg = (char *)"incorrect data check"; |
1190 state->mode = BAD; | 1213 state->mode = BAD; |
1191 break; | 1214 break; |
1192 } | 1215 } |
1193 INITBITS(); | 1216 INITBITS(); |
1194 Tracev((stderr, "inflate: check matches trailer\n")); | 1217 Tracev((stderr, "inflate: check matches trailer\n")); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1233 (state->mode < CHECK || flush != Z_FINISH))) | 1256 (state->mode < CHECK || flush != Z_FINISH))) |
1234 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { | 1257 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { |
1235 state->mode = MEM; | 1258 state->mode = MEM; |
1236 return Z_MEM_ERROR; | 1259 return Z_MEM_ERROR; |
1237 } | 1260 } |
1238 in -= strm->avail_in; | 1261 in -= strm->avail_in; |
1239 out -= strm->avail_out; | 1262 out -= strm->avail_out; |
1240 strm->total_in += in; | 1263 strm->total_in += in; |
1241 strm->total_out += out; | 1264 strm->total_out += out; |
1242 state->total += out; | 1265 state->total += out; |
1243 if (state->wrap && out) | 1266 if ((state->wrap & 4) && out) |
1244 strm->adler = state->check = | 1267 strm->adler = state->check = |
1245 UPDATE(state->check, strm->next_out - out, out); | 1268 UPDATE(state->check, strm->next_out - out, out); |
1246 strm->data_type = state->bits + (state->last ? 64 : 0) + | 1269 strm->data_type = (int)state->bits + (state->last ? 64 : 0) + |
1247 (state->mode == TYPE ? 128 : 0) + | 1270 (state->mode == TYPE ? 128 : 0) + |
1248 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); | 1271 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); |
1249 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) | 1272 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
1250 ret = Z_BUF_ERROR; | 1273 ret = Z_BUF_ERROR; |
1251 return ret; | 1274 return ret; |
1252 } | 1275 } |
1253 | 1276 |
1254 int ZEXPORT inflateEnd(strm) | 1277 int ZEXPORT inflateEnd(strm) |
1255 z_streamp strm; | 1278 z_streamp strm; |
1256 { | 1279 { |
1257 struct inflate_state FAR *state; | 1280 struct inflate_state FAR *state; |
1258 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) | 1281 if (inflateStateCheck(strm)) |
1259 return Z_STREAM_ERROR; | 1282 return Z_STREAM_ERROR; |
1260 state = (struct inflate_state FAR *)strm->state; | 1283 state = (struct inflate_state FAR *)strm->state; |
1261 if (state->window != Z_NULL) ZFREE(strm, state->window); | 1284 if (state->window != Z_NULL) ZFREE(strm, state->window); |
1262 ZFREE(strm, strm->state); | 1285 ZFREE(strm, strm->state); |
1263 strm->state = Z_NULL; | 1286 strm->state = Z_NULL; |
1264 Tracev((stderr, "inflate: end\n")); | 1287 Tracev((stderr, "inflate: end\n")); |
1265 return Z_OK; | 1288 return Z_OK; |
1266 } | 1289 } |
1267 | 1290 |
1268 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) | 1291 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) |
1269 z_streamp strm; | 1292 z_streamp strm; |
1270 Bytef *dictionary; | 1293 Bytef *dictionary; |
1271 uInt *dictLength; | 1294 uInt *dictLength; |
1272 { | 1295 { |
1273 struct inflate_state FAR *state; | 1296 struct inflate_state FAR *state; |
1274 | 1297 |
1275 /* check state */ | 1298 /* check state */ |
1276 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1299 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1277 state = (struct inflate_state FAR *)strm->state; | 1300 state = (struct inflate_state FAR *)strm->state; |
1278 | 1301 |
1279 /* copy dictionary */ | 1302 /* copy dictionary */ |
1280 if (state->whave && dictionary != Z_NULL) { | 1303 if (state->whave && dictionary != Z_NULL) { |
1281 zmemcpy(dictionary, state->window + state->wnext, | 1304 zmemcpy(dictionary, state->window + state->wnext, |
1282 state->whave - state->wnext); | 1305 state->whave - state->wnext); |
1283 zmemcpy(dictionary + state->whave - state->wnext, | 1306 zmemcpy(dictionary + state->whave - state->wnext, |
1284 state->window, state->wnext); | 1307 state->window, state->wnext); |
1285 } | 1308 } |
1286 if (dictLength != Z_NULL) | 1309 if (dictLength != Z_NULL) |
1287 *dictLength = state->whave; | 1310 *dictLength = state->whave; |
1288 return Z_OK; | 1311 return Z_OK; |
1289 } | 1312 } |
1290 | 1313 |
1291 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) | 1314 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) |
1292 z_streamp strm; | 1315 z_streamp strm; |
1293 const Bytef *dictionary; | 1316 const Bytef *dictionary; |
1294 uInt dictLength; | 1317 uInt dictLength; |
1295 { | 1318 { |
1296 struct inflate_state FAR *state; | 1319 struct inflate_state FAR *state; |
1297 unsigned long dictid; | 1320 unsigned long dictid; |
1298 int ret; | 1321 int ret; |
1299 | 1322 |
1300 /* check state */ | 1323 /* check state */ |
1301 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1324 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1302 state = (struct inflate_state FAR *)strm->state; | 1325 state = (struct inflate_state FAR *)strm->state; |
1303 if (state->wrap != 0 && state->mode != DICT) | 1326 if (state->wrap != 0 && state->mode != DICT) |
1304 return Z_STREAM_ERROR; | 1327 return Z_STREAM_ERROR; |
1305 | 1328 |
1306 /* check for correct dictionary identifier */ | 1329 /* check for correct dictionary identifier */ |
1307 if (state->mode == DICT) { | 1330 if (state->mode == DICT) { |
1308 dictid = adler32(0L, Z_NULL, 0); | 1331 dictid = adler32(0L, Z_NULL, 0); |
1309 dictid = adler32(dictid, dictionary, dictLength); | 1332 dictid = adler32(dictid, dictionary, dictLength); |
1310 if (dictid != state->check) | 1333 if (dictid != state->check) |
1311 return Z_DATA_ERROR; | 1334 return Z_DATA_ERROR; |
(...skipping 11 matching lines...) Expand all Loading... |
1323 return Z_OK; | 1346 return Z_OK; |
1324 } | 1347 } |
1325 | 1348 |
1326 int ZEXPORT inflateGetHeader(strm, head) | 1349 int ZEXPORT inflateGetHeader(strm, head) |
1327 z_streamp strm; | 1350 z_streamp strm; |
1328 gz_headerp head; | 1351 gz_headerp head; |
1329 { | 1352 { |
1330 struct inflate_state FAR *state; | 1353 struct inflate_state FAR *state; |
1331 | 1354 |
1332 /* check state */ | 1355 /* check state */ |
1333 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1356 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1334 state = (struct inflate_state FAR *)strm->state; | 1357 state = (struct inflate_state FAR *)strm->state; |
1335 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; | 1358 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; |
1336 | 1359 |
1337 /* save header structure */ | 1360 /* save header structure */ |
1338 state->head = head; | 1361 state->head = head; |
1339 head->done = 0; | 1362 head->done = 0; |
1340 return Z_OK; | 1363 return Z_OK; |
1341 } | 1364 } |
1342 | 1365 |
1343 /* | 1366 /* |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1376 | 1399 |
1377 int ZEXPORT inflateSync(strm) | 1400 int ZEXPORT inflateSync(strm) |
1378 z_streamp strm; | 1401 z_streamp strm; |
1379 { | 1402 { |
1380 unsigned len; /* number of bytes to look at or looked at */ | 1403 unsigned len; /* number of bytes to look at or looked at */ |
1381 unsigned long in, out; /* temporary to save total_in and total_out */ | 1404 unsigned long in, out; /* temporary to save total_in and total_out */ |
1382 unsigned char buf[4]; /* to restore bit buffer to byte string */ | 1405 unsigned char buf[4]; /* to restore bit buffer to byte string */ |
1383 struct inflate_state FAR *state; | 1406 struct inflate_state FAR *state; |
1384 | 1407 |
1385 /* check parameters */ | 1408 /* check parameters */ |
1386 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1409 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1387 state = (struct inflate_state FAR *)strm->state; | 1410 state = (struct inflate_state FAR *)strm->state; |
1388 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; | 1411 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; |
1389 | 1412 |
1390 /* if first time, start search in bit buffer */ | 1413 /* if first time, start search in bit buffer */ |
1391 if (state->mode != SYNC) { | 1414 if (state->mode != SYNC) { |
1392 state->mode = SYNC; | 1415 state->mode = SYNC; |
1393 state->hold <<= state->bits & 7; | 1416 state->hold <<= state->bits & 7; |
1394 state->bits -= state->bits & 7; | 1417 state->bits -= state->bits & 7; |
1395 len = 0; | 1418 len = 0; |
1396 while (state->bits >= 8) { | 1419 while (state->bits >= 8) { |
(...skipping 26 matching lines...) Expand all Loading... |
1423 implementation to provide an additional safety check. PPP uses | 1446 implementation to provide an additional safety check. PPP uses |
1424 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored | 1447 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored |
1425 block. When decompressing, PPP checks that at the end of input packet, | 1448 block. When decompressing, PPP checks that at the end of input packet, |
1426 inflate is waiting for these length bytes. | 1449 inflate is waiting for these length bytes. |
1427 */ | 1450 */ |
1428 int ZEXPORT inflateSyncPoint(strm) | 1451 int ZEXPORT inflateSyncPoint(strm) |
1429 z_streamp strm; | 1452 z_streamp strm; |
1430 { | 1453 { |
1431 struct inflate_state FAR *state; | 1454 struct inflate_state FAR *state; |
1432 | 1455 |
1433 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1456 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1434 state = (struct inflate_state FAR *)strm->state; | 1457 state = (struct inflate_state FAR *)strm->state; |
1435 return state->mode == STORED && state->bits == 0; | 1458 return state->mode == STORED && state->bits == 0; |
1436 } | 1459 } |
1437 | 1460 |
1438 int ZEXPORT inflateCopy(dest, source) | 1461 int ZEXPORT inflateCopy(dest, source) |
1439 z_streamp dest; | 1462 z_streamp dest; |
1440 z_streamp source; | 1463 z_streamp source; |
1441 { | 1464 { |
1442 struct inflate_state FAR *state; | 1465 struct inflate_state FAR *state; |
1443 struct inflate_state FAR *copy; | 1466 struct inflate_state FAR *copy; |
1444 unsigned char FAR *window; | 1467 unsigned char FAR *window; |
1445 unsigned wsize; | 1468 unsigned wsize; |
1446 | 1469 |
1447 /* check input */ | 1470 /* check input */ |
1448 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || | 1471 if (inflateStateCheck(source) || dest == Z_NULL) |
1449 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) | |
1450 return Z_STREAM_ERROR; | 1472 return Z_STREAM_ERROR; |
1451 state = (struct inflate_state FAR *)source->state; | 1473 state = (struct inflate_state FAR *)source->state; |
1452 | 1474 |
1453 /* allocate space */ | 1475 /* allocate space */ |
1454 copy = (struct inflate_state FAR *) | 1476 copy = (struct inflate_state FAR *) |
1455 ZALLOC(source, 1, sizeof(struct inflate_state)); | 1477 ZALLOC(source, 1, sizeof(struct inflate_state)); |
1456 if (copy == Z_NULL) return Z_MEM_ERROR; | 1478 if (copy == Z_NULL) return Z_MEM_ERROR; |
1457 window = Z_NULL; | 1479 window = Z_NULL; |
1458 if (state->window != Z_NULL) { | 1480 if (state->window != Z_NULL) { |
1459 window = (unsigned char FAR *) | 1481 window = (unsigned char FAR *) |
1460 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); | 1482 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); |
1461 if (window == Z_NULL) { | 1483 if (window == Z_NULL) { |
1462 ZFREE(source, copy); | 1484 ZFREE(source, copy); |
1463 return Z_MEM_ERROR; | 1485 return Z_MEM_ERROR; |
1464 } | 1486 } |
1465 } | 1487 } |
1466 | 1488 |
1467 /* copy state */ | 1489 /* copy state */ |
1468 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); | 1490 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
1469 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); | 1491 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); |
| 1492 copy->strm = dest; |
1470 if (state->lencode >= state->codes && | 1493 if (state->lencode >= state->codes && |
1471 state->lencode <= state->codes + ENOUGH - 1) { | 1494 state->lencode <= state->codes + ENOUGH - 1) { |
1472 copy->lencode = copy->codes + (state->lencode - state->codes); | 1495 copy->lencode = copy->codes + (state->lencode - state->codes); |
1473 copy->distcode = copy->codes + (state->distcode - state->codes); | 1496 copy->distcode = copy->codes + (state->distcode - state->codes); |
1474 } | 1497 } |
1475 copy->next = copy->codes + (state->next - state->codes); | 1498 copy->next = copy->codes + (state->next - state->codes); |
1476 if (window != Z_NULL) { | 1499 if (window != Z_NULL) { |
1477 wsize = 1U << state->wbits; | 1500 wsize = 1U << state->wbits; |
1478 zmemcpy(window, state->window, wsize); | 1501 zmemcpy(window, state->window, wsize); |
1479 } | 1502 } |
1480 copy->window = window; | 1503 copy->window = window; |
1481 dest->state = (struct internal_state FAR *)copy; | 1504 dest->state = (struct internal_state FAR *)copy; |
1482 return Z_OK; | 1505 return Z_OK; |
1483 } | 1506 } |
1484 | 1507 |
1485 int ZEXPORT inflateUndermine(strm, subvert) | 1508 int ZEXPORT inflateUndermine(strm, subvert) |
1486 z_streamp strm; | 1509 z_streamp strm; |
1487 int subvert; | 1510 int subvert; |
1488 { | 1511 { |
1489 struct inflate_state FAR *state; | 1512 struct inflate_state FAR *state; |
1490 | 1513 |
1491 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1514 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
1492 state = (struct inflate_state FAR *)strm->state; | 1515 state = (struct inflate_state FAR *)strm->state; |
| 1516 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
1493 state->sane = !subvert; | 1517 state->sane = !subvert; |
1494 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR | |
1495 return Z_OK; | 1518 return Z_OK; |
1496 #else | 1519 #else |
| 1520 (void)subvert; |
1497 state->sane = 1; | 1521 state->sane = 1; |
1498 return Z_DATA_ERROR; | 1522 return Z_DATA_ERROR; |
1499 #endif | 1523 #endif |
1500 } | 1524 } |
1501 | 1525 |
| 1526 int ZEXPORT inflateValidate(strm, check) |
| 1527 z_streamp strm; |
| 1528 int check; |
| 1529 { |
| 1530 struct inflate_state FAR *state; |
| 1531 |
| 1532 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 1533 state = (struct inflate_state FAR *)strm->state; |
| 1534 if (check) |
| 1535 state->wrap |= 4; |
| 1536 else |
| 1537 state->wrap &= ~4; |
| 1538 return Z_OK; |
| 1539 } |
| 1540 |
1502 long ZEXPORT inflateMark(strm) | 1541 long ZEXPORT inflateMark(strm) |
1503 z_streamp strm; | 1542 z_streamp strm; |
1504 { | 1543 { |
1505 struct inflate_state FAR *state; | 1544 struct inflate_state FAR *state; |
1506 | 1545 |
1507 if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; | 1546 if (inflateStateCheck(strm)) |
| 1547 return -(1L << 16); |
1508 state = (struct inflate_state FAR *)strm->state; | 1548 state = (struct inflate_state FAR *)strm->state; |
1509 return ((long)(state->back) << 16) + | 1549 return (long)(((unsigned long)((long)state->back)) << 16) + |
1510 (state->mode == COPY ? state->length : | 1550 (state->mode == COPY ? state->length : |
1511 (state->mode == MATCH ? state->was - state->length : 0)); | 1551 (state->mode == MATCH ? state->was - state->length : 0)); |
1512 } | 1552 } |
| 1553 |
| 1554 unsigned long ZEXPORT inflateCodesUsed(strm) |
| 1555 z_streamp strm; |
| 1556 { |
| 1557 struct inflate_state FAR *state; |
| 1558 if (inflateStateCheck(strm)) return (unsigned long)-1; |
| 1559 state = (struct inflate_state FAR *)strm->state; |
| 1560 return (unsigned long)(state->next - state->codes); |
| 1561 } |
OLD | NEW |