comparison zlib/inflate.c @ 2690:9ef72ee5c0b0

Update vendored zlib to 1.3.1
author Michael Pavone <pavone@retrodev.com>
date Sun, 15 Jun 2025 15:39:33 -0700
parents 00d788dac91a
children
comparison
equal deleted inserted replaced
2689:bd6e33de0972 2690:9ef72ee5c0b0
1 /* inflate.c -- zlib decompression 1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2016 Mark Adler 2 * Copyright (C) 1995-2022 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:
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 local int inflateStateCheck(z_streamp strm) {
95 local int inflateStateCheck OF((z_streamp strm));
96 local void fixedtables OF((struct inflate_state FAR *state));
97 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
98 unsigned copy));
99 #ifdef BUILDFIXED
100 void makefixed OF((void));
101 #endif
102 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
103 unsigned len));
104
105 local int inflateStateCheck(strm)
106 z_streamp strm;
107 {
108 struct inflate_state FAR *state; 95 struct inflate_state FAR *state;
109 if (strm == Z_NULL || 96 if (strm == Z_NULL ||
110 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 97 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
111 return 1; 98 return 1;
112 state = (struct inflate_state FAR *)strm->state; 99 state = (struct inflate_state FAR *)strm->state;
114 state->mode < HEAD || state->mode > SYNC) 101 state->mode < HEAD || state->mode > SYNC)
115 return 1; 102 return 1;
116 return 0; 103 return 0;
117 } 104 }
118 105
119 int ZEXPORT inflateResetKeep(strm) 106 int ZEXPORT inflateResetKeep(z_streamp strm) {
120 z_streamp strm;
121 {
122 struct inflate_state FAR *state; 107 struct inflate_state FAR *state;
123 108
124 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 109 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
125 state = (struct inflate_state FAR *)strm->state; 110 state = (struct inflate_state FAR *)strm->state;
126 strm->total_in = strm->total_out = state->total = 0; 111 strm->total_in = strm->total_out = state->total = 0;
128 if (state->wrap) /* to support ill-conceived Java test suite */ 113 if (state->wrap) /* to support ill-conceived Java test suite */
129 strm->adler = state->wrap & 1; 114 strm->adler = state->wrap & 1;
130 state->mode = HEAD; 115 state->mode = HEAD;
131 state->last = 0; 116 state->last = 0;
132 state->havedict = 0; 117 state->havedict = 0;
118 state->flags = -1;
133 state->dmax = 32768U; 119 state->dmax = 32768U;
134 state->head = Z_NULL; 120 state->head = Z_NULL;
135 state->hold = 0; 121 state->hold = 0;
136 state->bits = 0; 122 state->bits = 0;
137 state->lencode = state->distcode = state->next = state->codes; 123 state->lencode = state->distcode = state->next = state->codes;
139 state->back = -1; 125 state->back = -1;
140 Tracev((stderr, "inflate: reset\n")); 126 Tracev((stderr, "inflate: reset\n"));
141 return Z_OK; 127 return Z_OK;
142 } 128 }
143 129
144 int ZEXPORT inflateReset(strm) 130 int ZEXPORT inflateReset(z_streamp strm) {
145 z_streamp strm;
146 {
147 struct inflate_state FAR *state; 131 struct inflate_state FAR *state;
148 132
149 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 133 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
150 state = (struct inflate_state FAR *)strm->state; 134 state = (struct inflate_state FAR *)strm->state;
151 state->wsize = 0; 135 state->wsize = 0;
152 state->whave = 0; 136 state->whave = 0;
153 state->wnext = 0; 137 state->wnext = 0;
154 return inflateResetKeep(strm); 138 return inflateResetKeep(strm);
155 } 139 }
156 140
157 int ZEXPORT inflateReset2(strm, windowBits) 141 int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
158 z_streamp strm;
159 int windowBits;
160 {
161 int wrap; 142 int wrap;
162 struct inflate_state FAR *state; 143 struct inflate_state FAR *state;
163 144
164 /* get the state */ 145 /* get the state */
165 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 146 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
166 state = (struct inflate_state FAR *)strm->state; 147 state = (struct inflate_state FAR *)strm->state;
167 148
168 /* extract wrap request from windowBits parameter */ 149 /* extract wrap request from windowBits parameter */
169 if (windowBits < 0) { 150 if (windowBits < 0) {
151 if (windowBits < -15)
152 return Z_STREAM_ERROR;
170 wrap = 0; 153 wrap = 0;
171 windowBits = -windowBits; 154 windowBits = -windowBits;
172 } 155 }
173 else { 156 else {
174 wrap = (windowBits >> 4) + 5; 157 wrap = (windowBits >> 4) + 5;
190 state->wrap = wrap; 173 state->wrap = wrap;
191 state->wbits = (unsigned)windowBits; 174 state->wbits = (unsigned)windowBits;
192 return inflateReset(strm); 175 return inflateReset(strm);
193 } 176 }
194 177
195 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) 178 int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
196 z_streamp strm; 179 const char *version, int stream_size) {
197 int windowBits;
198 const char *version;
199 int stream_size;
200 {
201 int ret; 180 int ret;
202 struct inflate_state FAR *state; 181 struct inflate_state FAR *state;
203 182
204 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 183 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
205 stream_size != (int)(sizeof(z_stream))) 184 stream_size != (int)(sizeof(z_stream)))
234 strm->state = Z_NULL; 213 strm->state = Z_NULL;
235 } 214 }
236 return ret; 215 return ret;
237 } 216 }
238 217
239 int ZEXPORT inflateInit_(strm, version, stream_size) 218 int ZEXPORT inflateInit_(z_streamp strm, const char *version,
240 z_streamp strm; 219 int stream_size) {
241 const char *version;
242 int stream_size;
243 {
244 return inflateInit2_(strm, DEF_WBITS, version, stream_size); 220 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
245 } 221 }
246 222
247 int ZEXPORT inflatePrime(strm, bits, value) 223 int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
248 z_streamp strm;
249 int bits;
250 int value;
251 {
252 struct inflate_state FAR *state; 224 struct inflate_state FAR *state;
253 225
254 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 226 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
227 if (bits == 0)
228 return Z_OK;
255 state = (struct inflate_state FAR *)strm->state; 229 state = (struct inflate_state FAR *)strm->state;
256 if (bits < 0) { 230 if (bits < 0) {
257 state->hold = 0; 231 state->hold = 0;
258 state->bits = 0; 232 state->bits = 0;
259 return Z_OK; 233 return Z_OK;
273 thereafter. This reduces the size of the code by about 2K bytes, in 247 thereafter. This reduces the size of the code by about 2K bytes, in
274 exchange for a little execution time. However, BUILDFIXED should not be 248 exchange for a little execution time. However, BUILDFIXED should not be
275 used for threaded applications, since the rewriting of the tables and virgin 249 used for threaded applications, since the rewriting of the tables and virgin
276 may not be thread-safe. 250 may not be thread-safe.
277 */ 251 */
278 local void fixedtables(state) 252 local void fixedtables(struct inflate_state FAR *state) {
279 struct inflate_state FAR *state;
280 {
281 #ifdef BUILDFIXED 253 #ifdef BUILDFIXED
282 static int virgin = 1; 254 static int virgin = 1;
283 static code *lenfix, *distfix; 255 static code *lenfix, *distfix;
284 static code fixed[544]; 256 static code fixed[544];
285 257
337 309
338 Then that can be linked with zlib built with MAKEFIXED defined and run: 310 Then that can be linked with zlib built with MAKEFIXED defined and run:
339 311
340 a.out > inffixed.h 312 a.out > inffixed.h
341 */ 313 */
342 void makefixed() 314 void makefixed(void)
343 { 315 {
344 unsigned low, size; 316 unsigned low, size;
345 struct inflate_state state; 317 struct inflate_state state;
346 318
347 fixedtables(&state); 319 fixedtables(&state);
391 advantage, since only the last 32K of output is copied to the sliding window 363 advantage, since only the last 32K of output is copied to the sliding window
392 upon return from inflate(), and since all distances after the first 32K of 364 upon return from inflate(), and since all distances after the first 32K of
393 output will fall in the output data, making match copies simpler and faster. 365 output will fall in the output data, making match copies simpler and faster.
394 The advantage may be dependent on the size of the processor's data caches. 366 The advantage may be dependent on the size of the processor's data caches.
395 */ 367 */
396 local int updatewindow(strm, end, copy) 368 local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
397 z_streamp strm;
398 const Bytef *end;
399 unsigned copy;
400 {
401 struct inflate_state FAR *state; 369 struct inflate_state FAR *state;
402 unsigned dist; 370 unsigned dist;
403 371
404 state = (struct inflate_state FAR *)strm->state; 372 state = (struct inflate_state FAR *)strm->state;
405 373
445 413
446 /* Macros for inflate(): */ 414 /* Macros for inflate(): */
447 415
448 /* check function to use adler32() for zlib or crc32() for gzip */ 416 /* check function to use adler32() for zlib or crc32() for gzip */
449 #ifdef GUNZIP 417 #ifdef GUNZIP
450 # define UPDATE(check, buf, len) \ 418 # define UPDATE_CHECK(check, buf, len) \
451 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) 419 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
452 #else 420 #else
453 # define UPDATE(check, buf, len) adler32(check, buf, len) 421 # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
454 #endif 422 #endif
455 423
456 /* check macros for header crc */ 424 /* check macros for header crc */
457 #ifdef GUNZIP 425 #ifdef GUNZIP
458 # define CRC2(check, word) \ 426 # define CRC2(check, word) \
617 stream available. So the only thing the flush parameter actually does is: 585 stream available. So the only thing the flush parameter actually does is:
618 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it 586 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
619 will return Z_BUF_ERROR if it has not reached the end of the stream. 587 will return Z_BUF_ERROR if it has not reached the end of the stream.
620 */ 588 */
621 589
622 int ZEXPORT inflate(strm, flush) 590 int ZEXPORT inflate(z_streamp strm, int flush) {
623 z_streamp strm;
624 int flush;
625 {
626 struct inflate_state FAR *state; 591 struct inflate_state FAR *state;
627 z_const unsigned char FAR *next; /* next input */ 592 z_const unsigned char FAR *next; /* next input */
628 unsigned char FAR *put; /* next output */ 593 unsigned char FAR *put; /* next output */
629 unsigned have, left; /* available input and output */ 594 unsigned have, left; /* available input and output */
630 unsigned long hold; /* bit buffer */ 595 unsigned long hold; /* bit buffer */
668 CRC2(state->check, hold); 633 CRC2(state->check, hold);
669 INITBITS(); 634 INITBITS();
670 state->mode = FLAGS; 635 state->mode = FLAGS;
671 break; 636 break;
672 } 637 }
673 state->flags = 0; /* expect zlib header */
674 if (state->head != Z_NULL) 638 if (state->head != Z_NULL)
675 state->head->done = -1; 639 state->head->done = -1;
676 if (!(state->wrap & 1) || /* check if zlib header allowed */ 640 if (!(state->wrap & 1) || /* check if zlib header allowed */
677 #else 641 #else
678 if ( 642 if (
695 strm->msg = (char *)"invalid window size"; 659 strm->msg = (char *)"invalid window size";
696 state->mode = BAD; 660 state->mode = BAD;
697 break; 661 break;
698 } 662 }
699 state->dmax = 1U << len; 663 state->dmax = 1U << len;
664 state->flags = 0; /* indicate zlib header */
700 Tracev((stderr, "inflate: zlib header ok\n")); 665 Tracev((stderr, "inflate: zlib header ok\n"));
701 strm->adler = state->check = adler32(0L, Z_NULL, 0); 666 strm->adler = state->check = adler32(0L, Z_NULL, 0);
702 state->mode = hold & 0x200 ? DICTID : TYPE; 667 state->mode = hold & 0x200 ? DICTID : TYPE;
703 INITBITS(); 668 INITBITS();
704 break; 669 break;
720 state->head->text = (int)((hold >> 8) & 1); 685 state->head->text = (int)((hold >> 8) & 1);
721 if ((state->flags & 0x0200) && (state->wrap & 4)) 686 if ((state->flags & 0x0200) && (state->wrap & 4))
722 CRC2(state->check, hold); 687 CRC2(state->check, hold);
723 INITBITS(); 688 INITBITS();
724 state->mode = TIME; 689 state->mode = TIME;
690 /* fallthrough */
725 case TIME: 691 case TIME:
726 NEEDBITS(32); 692 NEEDBITS(32);
727 if (state->head != Z_NULL) 693 if (state->head != Z_NULL)
728 state->head->time = hold; 694 state->head->time = hold;
729 if ((state->flags & 0x0200) && (state->wrap & 4)) 695 if ((state->flags & 0x0200) && (state->wrap & 4))
730 CRC4(state->check, hold); 696 CRC4(state->check, hold);
731 INITBITS(); 697 INITBITS();
732 state->mode = OS; 698 state->mode = OS;
699 /* fallthrough */
733 case OS: 700 case OS:
734 NEEDBITS(16); 701 NEEDBITS(16);
735 if (state->head != Z_NULL) { 702 if (state->head != Z_NULL) {
736 state->head->xflags = (int)(hold & 0xff); 703 state->head->xflags = (int)(hold & 0xff);
737 state->head->os = (int)(hold >> 8); 704 state->head->os = (int)(hold >> 8);
738 } 705 }
739 if ((state->flags & 0x0200) && (state->wrap & 4)) 706 if ((state->flags & 0x0200) && (state->wrap & 4))
740 CRC2(state->check, hold); 707 CRC2(state->check, hold);
741 INITBITS(); 708 INITBITS();
742 state->mode = EXLEN; 709 state->mode = EXLEN;
710 /* fallthrough */
743 case EXLEN: 711 case EXLEN:
744 if (state->flags & 0x0400) { 712 if (state->flags & 0x0400) {
745 NEEDBITS(16); 713 NEEDBITS(16);
746 state->length = (unsigned)(hold); 714 state->length = (unsigned)(hold);
747 if (state->head != Z_NULL) 715 if (state->head != Z_NULL)
751 INITBITS(); 719 INITBITS();
752 } 720 }
753 else if (state->head != Z_NULL) 721 else if (state->head != Z_NULL)
754 state->head->extra = Z_NULL; 722 state->head->extra = Z_NULL;
755 state->mode = EXTRA; 723 state->mode = EXTRA;
724 /* fallthrough */
756 case EXTRA: 725 case EXTRA:
757 if (state->flags & 0x0400) { 726 if (state->flags & 0x0400) {
758 copy = state->length; 727 copy = state->length;
759 if (copy > have) copy = have; 728 if (copy > have) copy = have;
760 if (copy) { 729 if (copy) {
761 if (state->head != Z_NULL && 730 if (state->head != Z_NULL &&
762 state->head->extra != Z_NULL) { 731 state->head->extra != Z_NULL &&
763 len = state->head->extra_len - state->length; 732 (len = state->head->extra_len - state->length) <
733 state->head->extra_max) {
764 zmemcpy(state->head->extra + len, next, 734 zmemcpy(state->head->extra + len, next,
765 len + copy > state->head->extra_max ? 735 len + copy > state->head->extra_max ?
766 state->head->extra_max - len : copy); 736 state->head->extra_max - len : copy);
767 } 737 }
768 if ((state->flags & 0x0200) && (state->wrap & 4)) 738 if ((state->flags & 0x0200) && (state->wrap & 4))
773 } 743 }
774 if (state->length) goto inf_leave; 744 if (state->length) goto inf_leave;
775 } 745 }
776 state->length = 0; 746 state->length = 0;
777 state->mode = NAME; 747 state->mode = NAME;
748 /* fallthrough */
778 case NAME: 749 case NAME:
779 if (state->flags & 0x0800) { 750 if (state->flags & 0x0800) {
780 if (have == 0) goto inf_leave; 751 if (have == 0) goto inf_leave;
781 copy = 0; 752 copy = 0;
782 do { 753 do {
794 } 765 }
795 else if (state->head != Z_NULL) 766 else if (state->head != Z_NULL)
796 state->head->name = Z_NULL; 767 state->head->name = Z_NULL;
797 state->length = 0; 768 state->length = 0;
798 state->mode = COMMENT; 769 state->mode = COMMENT;
770 /* fallthrough */
799 case COMMENT: 771 case COMMENT:
800 if (state->flags & 0x1000) { 772 if (state->flags & 0x1000) {
801 if (have == 0) goto inf_leave; 773 if (have == 0) goto inf_leave;
802 copy = 0; 774 copy = 0;
803 do { 775 do {
814 if (len) goto inf_leave; 786 if (len) goto inf_leave;
815 } 787 }
816 else if (state->head != Z_NULL) 788 else if (state->head != Z_NULL)
817 state->head->comment = Z_NULL; 789 state->head->comment = Z_NULL;
818 state->mode = HCRC; 790 state->mode = HCRC;
791 /* fallthrough */
819 case HCRC: 792 case HCRC:
820 if (state->flags & 0x0200) { 793 if (state->flags & 0x0200) {
821 NEEDBITS(16); 794 NEEDBITS(16);
822 if ((state->wrap & 4) && hold != (state->check & 0xffff)) { 795 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
823 strm->msg = (char *)"header crc mismatch"; 796 strm->msg = (char *)"header crc mismatch";
837 case DICTID: 810 case DICTID:
838 NEEDBITS(32); 811 NEEDBITS(32);
839 strm->adler = state->check = ZSWAP32(hold); 812 strm->adler = state->check = ZSWAP32(hold);
840 INITBITS(); 813 INITBITS();
841 state->mode = DICT; 814 state->mode = DICT;
815 /* fallthrough */
842 case DICT: 816 case DICT:
843 if (state->havedict == 0) { 817 if (state->havedict == 0) {
844 RESTORE(); 818 RESTORE();
845 return Z_NEED_DICT; 819 return Z_NEED_DICT;
846 } 820 }
847 strm->adler = state->check = adler32(0L, Z_NULL, 0); 821 strm->adler = state->check = adler32(0L, Z_NULL, 0);
848 state->mode = TYPE; 822 state->mode = TYPE;
823 /* fallthrough */
849 case TYPE: 824 case TYPE:
850 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; 825 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
826 /* fallthrough */
851 case TYPEDO: 827 case TYPEDO:
852 if (state->last) { 828 if (state->last) {
853 BYTEBITS(); 829 BYTEBITS();
854 state->mode = CHECK; 830 state->mode = CHECK;
855 break; 831 break;
896 Tracev((stderr, "inflate: stored length %u\n", 872 Tracev((stderr, "inflate: stored length %u\n",
897 state->length)); 873 state->length));
898 INITBITS(); 874 INITBITS();
899 state->mode = COPY_; 875 state->mode = COPY_;
900 if (flush == Z_TREES) goto inf_leave; 876 if (flush == Z_TREES) goto inf_leave;
877 /* fallthrough */
901 case COPY_: 878 case COPY_:
902 state->mode = COPY; 879 state->mode = COPY;
880 /* fallthrough */
903 case COPY: 881 case COPY:
904 copy = state->length; 882 copy = state->length;
905 if (copy) { 883 if (copy) {
906 if (copy > have) copy = have; 884 if (copy > have) copy = have;
907 if (copy > left) copy = left; 885 if (copy > left) copy = left;
933 } 911 }
934 #endif 912 #endif
935 Tracev((stderr, "inflate: table sizes ok\n")); 913 Tracev((stderr, "inflate: table sizes ok\n"));
936 state->have = 0; 914 state->have = 0;
937 state->mode = LENLENS; 915 state->mode = LENLENS;
916 /* fallthrough */
938 case LENLENS: 917 case LENLENS:
939 while (state->have < state->ncode) { 918 while (state->have < state->ncode) {
940 NEEDBITS(3); 919 NEEDBITS(3);
941 state->lens[order[state->have++]] = (unsigned short)BITS(3); 920 state->lens[order[state->have++]] = (unsigned short)BITS(3);
942 DROPBITS(3); 921 DROPBITS(3);
954 break; 933 break;
955 } 934 }
956 Tracev((stderr, "inflate: code lengths ok\n")); 935 Tracev((stderr, "inflate: code lengths ok\n"));
957 state->have = 0; 936 state->have = 0;
958 state->mode = CODELENS; 937 state->mode = CODELENS;
938 /* fallthrough */
959 case CODELENS: 939 case CODELENS:
960 while (state->have < state->nlen + state->ndist) { 940 while (state->have < state->nlen + state->ndist) {
961 for (;;) { 941 for (;;) {
962 here = state->lencode[BITS(state->lenbits)]; 942 here = state->lencode[BITS(state->lenbits)];
963 if ((unsigned)(here.bits) <= bits) break; 943 if ((unsigned)(here.bits) <= bits) break;
1037 break; 1017 break;
1038 } 1018 }
1039 Tracev((stderr, "inflate: codes ok\n")); 1019 Tracev((stderr, "inflate: codes ok\n"));
1040 state->mode = LEN_; 1020 state->mode = LEN_;
1041 if (flush == Z_TREES) goto inf_leave; 1021 if (flush == Z_TREES) goto inf_leave;
1022 /* fallthrough */
1042 case LEN_: 1023 case LEN_:
1043 state->mode = LEN; 1024 state->mode = LEN;
1025 /* fallthrough */
1044 case LEN: 1026 case LEN:
1045 if (have >= 6 && left >= 258) { 1027 if (have >= 6 && left >= 258) {
1046 RESTORE(); 1028 RESTORE();
1047 inflate_fast(strm, out); 1029 inflate_fast(strm, out);
1048 LOAD(); 1030 LOAD();
1088 state->mode = BAD; 1070 state->mode = BAD;
1089 break; 1071 break;
1090 } 1072 }
1091 state->extra = (unsigned)(here.op) & 15; 1073 state->extra = (unsigned)(here.op) & 15;
1092 state->mode = LENEXT; 1074 state->mode = LENEXT;
1075 /* fallthrough */
1093 case LENEXT: 1076 case LENEXT:
1094 if (state->extra) { 1077 if (state->extra) {
1095 NEEDBITS(state->extra); 1078 NEEDBITS(state->extra);
1096 state->length += BITS(state->extra); 1079 state->length += BITS(state->extra);
1097 DROPBITS(state->extra); 1080 DROPBITS(state->extra);
1098 state->back += state->extra; 1081 state->back += state->extra;
1099 } 1082 }
1100 Tracevv((stderr, "inflate: length %u\n", state->length)); 1083 Tracevv((stderr, "inflate: length %u\n", state->length));
1101 state->was = state->length; 1084 state->was = state->length;
1102 state->mode = DIST; 1085 state->mode = DIST;
1086 /* fallthrough */
1103 case DIST: 1087 case DIST:
1104 for (;;) { 1088 for (;;) {
1105 here = state->distcode[BITS(state->distbits)]; 1089 here = state->distcode[BITS(state->distbits)];
1106 if ((unsigned)(here.bits) <= bits) break; 1090 if ((unsigned)(here.bits) <= bits) break;
1107 PULLBYTE(); 1091 PULLBYTE();
1125 break; 1109 break;
1126 } 1110 }
1127 state->offset = (unsigned)here.val; 1111 state->offset = (unsigned)here.val;
1128 state->extra = (unsigned)(here.op) & 15; 1112 state->extra = (unsigned)(here.op) & 15;
1129 state->mode = DISTEXT; 1113 state->mode = DISTEXT;
1114 /* fallthrough */
1130 case DISTEXT: 1115 case DISTEXT:
1131 if (state->extra) { 1116 if (state->extra) {
1132 NEEDBITS(state->extra); 1117 NEEDBITS(state->extra);
1133 state->offset += BITS(state->extra); 1118 state->offset += BITS(state->extra);
1134 DROPBITS(state->extra); 1119 DROPBITS(state->extra);
1141 break; 1126 break;
1142 } 1127 }
1143 #endif 1128 #endif
1144 Tracevv((stderr, "inflate: distance %u\n", state->offset)); 1129 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1145 state->mode = MATCH; 1130 state->mode = MATCH;
1131 /* fallthrough */
1146 case MATCH: 1132 case MATCH:
1147 if (left == 0) goto inf_leave; 1133 if (left == 0) goto inf_leave;
1148 copy = out - left; 1134 copy = out - left;
1149 if (state->offset > copy) { /* copy from window */ 1135 if (state->offset > copy) { /* copy from window */
1150 copy = state->offset - copy; 1136 copy = state->offset - copy;
1200 out -= left; 1186 out -= left;
1201 strm->total_out += out; 1187 strm->total_out += out;
1202 state->total += out; 1188 state->total += out;
1203 if ((state->wrap & 4) && out) 1189 if ((state->wrap & 4) && out)
1204 strm->adler = state->check = 1190 strm->adler = state->check =
1205 UPDATE(state->check, put - out, out); 1191 UPDATE_CHECK(state->check, put - out, out);
1206 out = left; 1192 out = left;
1207 if ((state->wrap & 4) && ( 1193 if ((state->wrap & 4) && (
1208 #ifdef GUNZIP 1194 #ifdef GUNZIP
1209 state->flags ? hold : 1195 state->flags ? hold :
1210 #endif 1196 #endif
1216 INITBITS(); 1202 INITBITS();
1217 Tracev((stderr, "inflate: check matches trailer\n")); 1203 Tracev((stderr, "inflate: check matches trailer\n"));
1218 } 1204 }
1219 #ifdef GUNZIP 1205 #ifdef GUNZIP
1220 state->mode = LENGTH; 1206 state->mode = LENGTH;
1207 /* fallthrough */
1221 case LENGTH: 1208 case LENGTH:
1222 if (state->wrap && state->flags) { 1209 if (state->wrap && state->flags) {
1223 NEEDBITS(32); 1210 NEEDBITS(32);
1224 if (hold != (state->total & 0xffffffffUL)) { 1211 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1225 strm->msg = (char *)"incorrect length check"; 1212 strm->msg = (char *)"incorrect length check";
1226 state->mode = BAD; 1213 state->mode = BAD;
1227 break; 1214 break;
1228 } 1215 }
1229 INITBITS(); 1216 INITBITS();
1230 Tracev((stderr, "inflate: length matches trailer\n")); 1217 Tracev((stderr, "inflate: length matches trailer\n"));
1231 } 1218 }
1232 #endif 1219 #endif
1233 state->mode = DONE; 1220 state->mode = DONE;
1221 /* fallthrough */
1234 case DONE: 1222 case DONE:
1235 ret = Z_STREAM_END; 1223 ret = Z_STREAM_END;
1236 goto inf_leave; 1224 goto inf_leave;
1237 case BAD: 1225 case BAD:
1238 ret = Z_DATA_ERROR; 1226 ret = Z_DATA_ERROR;
1239 goto inf_leave; 1227 goto inf_leave;
1240 case MEM: 1228 case MEM:
1241 return Z_MEM_ERROR; 1229 return Z_MEM_ERROR;
1242 case SYNC: 1230 case SYNC:
1231 /* fallthrough */
1243 default: 1232 default:
1244 return Z_STREAM_ERROR; 1233 return Z_STREAM_ERROR;
1245 } 1234 }
1246 1235
1247 /* 1236 /*
1263 strm->total_in += in; 1252 strm->total_in += in;
1264 strm->total_out += out; 1253 strm->total_out += out;
1265 state->total += out; 1254 state->total += out;
1266 if ((state->wrap & 4) && out) 1255 if ((state->wrap & 4) && out)
1267 strm->adler = state->check = 1256 strm->adler = state->check =
1268 UPDATE(state->check, strm->next_out - out, out); 1257 UPDATE_CHECK(state->check, strm->next_out - out, out);
1269 strm->data_type = (int)state->bits + (state->last ? 64 : 0) + 1258 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1270 (state->mode == TYPE ? 128 : 0) + 1259 (state->mode == TYPE ? 128 : 0) +
1271 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); 1260 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1272 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) 1261 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1273 ret = Z_BUF_ERROR; 1262 ret = Z_BUF_ERROR;
1274 return ret; 1263 return ret;
1275 } 1264 }
1276 1265
1277 int ZEXPORT inflateEnd(strm) 1266 int ZEXPORT inflateEnd(z_streamp strm) {
1278 z_streamp strm;
1279 {
1280 struct inflate_state FAR *state; 1267 struct inflate_state FAR *state;
1281 if (inflateStateCheck(strm)) 1268 if (inflateStateCheck(strm))
1282 return Z_STREAM_ERROR; 1269 return Z_STREAM_ERROR;
1283 state = (struct inflate_state FAR *)strm->state; 1270 state = (struct inflate_state FAR *)strm->state;
1284 if (state->window != Z_NULL) ZFREE(strm, state->window); 1271 if (state->window != Z_NULL) ZFREE(strm, state->window);
1286 strm->state = Z_NULL; 1273 strm->state = Z_NULL;
1287 Tracev((stderr, "inflate: end\n")); 1274 Tracev((stderr, "inflate: end\n"));
1288 return Z_OK; 1275 return Z_OK;
1289 } 1276 }
1290 1277
1291 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) 1278 int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1292 z_streamp strm; 1279 uInt *dictLength) {
1293 Bytef *dictionary;
1294 uInt *dictLength;
1295 {
1296 struct inflate_state FAR *state; 1280 struct inflate_state FAR *state;
1297 1281
1298 /* check state */ 1282 /* check state */
1299 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1283 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1300 state = (struct inflate_state FAR *)strm->state; 1284 state = (struct inflate_state FAR *)strm->state;
1309 if (dictLength != Z_NULL) 1293 if (dictLength != Z_NULL)
1310 *dictLength = state->whave; 1294 *dictLength = state->whave;
1311 return Z_OK; 1295 return Z_OK;
1312 } 1296 }
1313 1297
1314 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) 1298 int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1315 z_streamp strm; 1299 uInt dictLength) {
1316 const Bytef *dictionary;
1317 uInt dictLength;
1318 {
1319 struct inflate_state FAR *state; 1300 struct inflate_state FAR *state;
1320 unsigned long dictid; 1301 unsigned long dictid;
1321 int ret; 1302 int ret;
1322 1303
1323 /* check state */ 1304 /* check state */
1344 state->havedict = 1; 1325 state->havedict = 1;
1345 Tracev((stderr, "inflate: dictionary set\n")); 1326 Tracev((stderr, "inflate: dictionary set\n"));
1346 return Z_OK; 1327 return Z_OK;
1347 } 1328 }
1348 1329
1349 int ZEXPORT inflateGetHeader(strm, head) 1330 int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1350 z_streamp strm;
1351 gz_headerp head;
1352 {
1353 struct inflate_state FAR *state; 1331 struct inflate_state FAR *state;
1354 1332
1355 /* check state */ 1333 /* check state */
1356 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1334 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1357 state = (struct inflate_state FAR *)strm->state; 1335 state = (struct inflate_state FAR *)strm->state;
1372 pattern. If *have is less than four, then the pattern has not been found 1350 pattern. If *have is less than four, then the pattern has not been found
1373 yet and the return value is len. In the latter case, syncsearch() can be 1351 yet and the return value is len. In the latter case, syncsearch() can be
1374 called again with more data and the *have state. *have is initialized to 1352 called again with more data and the *have state. *have is initialized to
1375 zero for the first call. 1353 zero for the first call.
1376 */ 1354 */
1377 local unsigned syncsearch(have, buf, len) 1355 local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1378 unsigned FAR *have; 1356 unsigned len) {
1379 const unsigned char FAR *buf;
1380 unsigned len;
1381 {
1382 unsigned got; 1357 unsigned got;
1383 unsigned next; 1358 unsigned next;
1384 1359
1385 got = *have; 1360 got = *have;
1386 next = 0; 1361 next = 0;
1395 } 1370 }
1396 *have = got; 1371 *have = got;
1397 return next; 1372 return next;
1398 } 1373 }
1399 1374
1400 int ZEXPORT inflateSync(strm) 1375 int ZEXPORT inflateSync(z_streamp strm) {
1401 z_streamp strm;
1402 {
1403 unsigned len; /* number of bytes to look at or looked at */ 1376 unsigned len; /* number of bytes to look at or looked at */
1377 int flags; /* temporary to save header status */
1404 unsigned long in, out; /* temporary to save total_in and total_out */ 1378 unsigned long in, out; /* temporary to save total_in and total_out */
1405 unsigned char buf[4]; /* to restore bit buffer to byte string */ 1379 unsigned char buf[4]; /* to restore bit buffer to byte string */
1406 struct inflate_state FAR *state; 1380 struct inflate_state FAR *state;
1407 1381
1408 /* check parameters */ 1382 /* check parameters */
1411 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; 1385 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1412 1386
1413 /* if first time, start search in bit buffer */ 1387 /* if first time, start search in bit buffer */
1414 if (state->mode != SYNC) { 1388 if (state->mode != SYNC) {
1415 state->mode = SYNC; 1389 state->mode = SYNC;
1416 state->hold <<= state->bits & 7; 1390 state->hold >>= state->bits & 7;
1417 state->bits -= state->bits & 7; 1391 state->bits -= state->bits & 7;
1418 len = 0; 1392 len = 0;
1419 while (state->bits >= 8) { 1393 while (state->bits >= 8) {
1420 buf[len++] = (unsigned char)(state->hold); 1394 buf[len++] = (unsigned char)(state->hold);
1421 state->hold >>= 8; 1395 state->hold >>= 8;
1431 strm->next_in += len; 1405 strm->next_in += len;
1432 strm->total_in += len; 1406 strm->total_in += len;
1433 1407
1434 /* return no joy or set up to restart inflate() on a new block */ 1408 /* return no joy or set up to restart inflate() on a new block */
1435 if (state->have != 4) return Z_DATA_ERROR; 1409 if (state->have != 4) return Z_DATA_ERROR;
1410 if (state->flags == -1)
1411 state->wrap = 0; /* if no header yet, treat as raw */
1412 else
1413 state->wrap &= ~4; /* no point in computing a check value now */
1414 flags = state->flags;
1436 in = strm->total_in; out = strm->total_out; 1415 in = strm->total_in; out = strm->total_out;
1437 inflateReset(strm); 1416 inflateReset(strm);
1438 strm->total_in = in; strm->total_out = out; 1417 strm->total_in = in; strm->total_out = out;
1418 state->flags = flags;
1439 state->mode = TYPE; 1419 state->mode = TYPE;
1440 return Z_OK; 1420 return Z_OK;
1441 } 1421 }
1442 1422
1443 /* 1423 /*
1446 implementation to provide an additional safety check. PPP uses 1426 implementation to provide an additional safety check. PPP uses
1447 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored 1427 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1448 block. When decompressing, PPP checks that at the end of input packet, 1428 block. When decompressing, PPP checks that at the end of input packet,
1449 inflate is waiting for these length bytes. 1429 inflate is waiting for these length bytes.
1450 */ 1430 */
1451 int ZEXPORT inflateSyncPoint(strm) 1431 int ZEXPORT inflateSyncPoint(z_streamp strm) {
1452 z_streamp strm;
1453 {
1454 struct inflate_state FAR *state; 1432 struct inflate_state FAR *state;
1455 1433
1456 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1434 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1457 state = (struct inflate_state FAR *)strm->state; 1435 state = (struct inflate_state FAR *)strm->state;
1458 return state->mode == STORED && state->bits == 0; 1436 return state->mode == STORED && state->bits == 0;
1459 } 1437 }
1460 1438
1461 int ZEXPORT inflateCopy(dest, source) 1439 int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1462 z_streamp dest;
1463 z_streamp source;
1464 {
1465 struct inflate_state FAR *state; 1440 struct inflate_state FAR *state;
1466 struct inflate_state FAR *copy; 1441 struct inflate_state FAR *copy;
1467 unsigned char FAR *window; 1442 unsigned char FAR *window;
1468 unsigned wsize; 1443 unsigned wsize;
1469 1444
1503 copy->window = window; 1478 copy->window = window;
1504 dest->state = (struct internal_state FAR *)copy; 1479 dest->state = (struct internal_state FAR *)copy;
1505 return Z_OK; 1480 return Z_OK;
1506 } 1481 }
1507 1482
1508 int ZEXPORT inflateUndermine(strm, subvert) 1483 int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1509 z_streamp strm;
1510 int subvert;
1511 {
1512 struct inflate_state FAR *state; 1484 struct inflate_state FAR *state;
1513 1485
1514 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1486 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1515 state = (struct inflate_state FAR *)strm->state; 1487 state = (struct inflate_state FAR *)strm->state;
1516 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 1488 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1521 state->sane = 1; 1493 state->sane = 1;
1522 return Z_DATA_ERROR; 1494 return Z_DATA_ERROR;
1523 #endif 1495 #endif
1524 } 1496 }
1525 1497
1526 int ZEXPORT inflateValidate(strm, check) 1498 int ZEXPORT inflateValidate(z_streamp strm, int check) {
1527 z_streamp strm;
1528 int check;
1529 {
1530 struct inflate_state FAR *state; 1499 struct inflate_state FAR *state;
1531 1500
1532 if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1501 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1533 state = (struct inflate_state FAR *)strm->state; 1502 state = (struct inflate_state FAR *)strm->state;
1534 if (check) 1503 if (check && state->wrap)
1535 state->wrap |= 4; 1504 state->wrap |= 4;
1536 else 1505 else
1537 state->wrap &= ~4; 1506 state->wrap &= ~4;
1538 return Z_OK; 1507 return Z_OK;
1539 } 1508 }
1540 1509
1541 long ZEXPORT inflateMark(strm) 1510 long ZEXPORT inflateMark(z_streamp strm) {
1542 z_streamp strm;
1543 {
1544 struct inflate_state FAR *state; 1511 struct inflate_state FAR *state;
1545 1512
1546 if (inflateStateCheck(strm)) 1513 if (inflateStateCheck(strm))
1547 return -(1L << 16); 1514 return -(1L << 16);
1548 state = (struct inflate_state FAR *)strm->state; 1515 state = (struct inflate_state FAR *)strm->state;
1549 return (long)(((unsigned long)((long)state->back)) << 16) + 1516 return (long)(((unsigned long)((long)state->back)) << 16) +
1550 (state->mode == COPY ? state->length : 1517 (state->mode == COPY ? state->length :
1551 (state->mode == MATCH ? state->was - state->length : 0)); 1518 (state->mode == MATCH ? state->was - state->length : 0));
1552 } 1519 }
1553 1520
1554 unsigned long ZEXPORT inflateCodesUsed(strm) 1521 unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1555 z_streamp strm;
1556 {
1557 struct inflate_state FAR *state; 1522 struct inflate_state FAR *state;
1558 if (inflateStateCheck(strm)) return (unsigned long)-1; 1523 if (inflateStateCheck(strm)) return (unsigned long)-1;
1559 state = (struct inflate_state FAR *)strm->state; 1524 state = (struct inflate_state FAR *)strm->state;
1560 return (unsigned long)(state->next - state->codes); 1525 return (unsigned long)(state->next - state->codes);
1561 } 1526 }