96 local
int updatewindow OF((z_streamp strm,
unsigned out));
98 void makefixed OF((
void));
100 local
unsigned syncsearch OF((
unsigned FAR *have,
unsigned char FAR *
buf,
103 int ZEXPORT inflateReset(strm)
108 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
110 strm->total_in = strm->total_out = state->total = 0;
116 state->dmax = 32768
U;
117 state->head = Z_NULL;
123 state->lencode = state->distcode = state->next = state->codes;
124 Tracev((stderr,
"inflate: reset\n"));
128 int ZEXPORT inflatePrime(strm, bits, value)
135 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
137 if (bits > 16 || state->bits + bits > 32)
return Z_STREAM_ERROR;
138 value &= (1L << bits) - 1;
139 state->hold += value << state->bits;
144 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
152 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
153 stream_size != (
int)(
sizeof(
z_stream)))
154 return Z_VERSION_ERROR;
155 if (strm == Z_NULL)
return Z_STREAM_ERROR;
157 if (strm->zalloc == (alloc_func)0) {
158 strm->zalloc = zcalloc;
159 strm->opaque = (voidpf)0;
161 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
164 if (state == Z_NULL)
return Z_MEM_ERROR;
165 Tracev((stderr,
"inflate: allocated\n"));
167 if (windowBits < 0) {
169 windowBits = -windowBits;
172 state->wrap = (windowBits >> 4) + 1;
174 if (windowBits < 48) windowBits &= 15;
177 if (windowBits < 8 || windowBits > 15) {
179 strm->state = Z_NULL;
180 return Z_STREAM_ERROR;
182 state->wbits = (unsigned)windowBits;
183 state->window = Z_NULL;
184 return inflateReset(strm);
187 int ZEXPORT inflateInit_(strm, version, stream_size)
192 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
205 local
void fixedtables(state)
209 static int virgin = 1;
210 static code *lenfix, *distfix;
211 static code fixed[544];
220 while (sym < 144) state->lens[sym++] = 8;
221 while (sym < 256) state->lens[sym++] = 9;
222 while (sym < 280) state->lens[sym++] = 7;
223 while (sym < 288) state->lens[sym++] = 8;
227 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
231 while (sym < 32) state->lens[sym++] = 5;
234 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
240 # include "inffixed.h"
242 state->lencode = lenfix;
244 state->distcode = distfix;
275 puts(
" /* inffixed.h -- table for decoding fixed codes");
276 puts(
" * Generated automatically by makefixed().");
279 puts(
" /* WARNING: this file should *not* be used by applications.");
280 puts(
" It is part of the implementation of this library and is");
281 puts(
" subject to change. Applications should only use zlib.h.");
285 printf(
" static const code lenfix[%u] = {", size);
288 if ((low % 7) == 0) printf(
"\n ");
289 printf(
"{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
290 state.lencode[low].val);
291 if (++low == size)
break;
296 printf(
"\n static const code distfix[%u] = {", size);
299 if ((low % 6) == 0) printf(
"\n ");
300 printf(
"{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
301 state.distcode[low].val);
302 if (++low == size)
break;
323 local
int updatewindow(strm, out)
333 if (state->window == Z_NULL) {
334 state->window = (
unsigned char FAR *)
335 ZALLOC(strm, 1
U << state->wbits,
336 sizeof(
unsigned char));
337 if (state->window == Z_NULL)
return 1;
341 if (state->wsize == 0) {
342 state->wsize = 1
U << state->wbits;
348 copy = out - strm->avail_out;
349 if (copy >= state->wsize) {
350 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
352 state->whave = state->wsize;
355 dist = state->wsize - state->write;
356 if (dist > copy) dist = copy;
357 zmemcpy(state->window + state->write, strm->next_out - copy, dist);
360 zmemcpy(state->window, strm->next_out - copy, copy);
362 state->whave = state->wsize;
365 state->write += dist;
366 if (state->write == state->wsize) state->write = 0;
367 if (state->whave < state->wsize) state->whave += dist;
377 # define UPDATE(check, buf, len) \
378 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
380 # define UPDATE(check, buf, len) adler32(check, buf, len)
385 # define CRC2(check, word) \
387 hbuf[0] = (unsigned char)(word); \
388 hbuf[1] = (unsigned char)((word) >> 8); \
389 check = crc32(check, hbuf, 2); \
392 # define CRC4(check, word) \
394 hbuf[0] = (unsigned char)(word); \
395 hbuf[1] = (unsigned char)((word) >> 8); \
396 hbuf[2] = (unsigned char)((word) >> 16); \
397 hbuf[3] = (unsigned char)((word) >> 24); \
398 check = crc32(check, hbuf, 4); \
405 put = strm->next_out; \
406 left = strm->avail_out; \
407 next = strm->next_in; \
408 have = strm->avail_in; \
409 hold = state->hold; \
410 bits = state->bits; \
416 strm->next_out = put; \
417 strm->avail_out = left; \
418 strm->next_in = next; \
419 strm->avail_in = have; \
420 state->hold = hold; \
421 state->bits = bits; \
435 if (have == 0) goto inf_leave; \
437 hold += (unsigned long)(*next++) << bits; \
443 #define NEEDBITS(n) \
445 while (bits < (unsigned)(n)) \
451 ((unsigned)hold & ((1U << (n)) - 1))
454 #define DROPBITS(n) \
457 bits -= (unsigned)(n); \
469 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
470 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
554 int ZEXPORT inflate(strm, flush)
559 unsigned char FAR *next;
560 unsigned char FAR *put;
566 unsigned char FAR *from;
572 unsigned char hbuf[4];
574 static const unsigned short order[19] =
575 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
582 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
583 (strm->next_in == Z_NULL && strm->avail_in != 0))
584 return Z_STREAM_ERROR;
587 if (state->mode == TYPE) state->mode = TYPEDO;
593 switch (state->mode) {
595 if (state->wrap == 0) {
596 state->mode = TYPEDO;
601 if ((state->wrap & 2) && hold == 0x8b1f) {
602 state->check = crc32(0L, Z_NULL, 0);
603 CRC2(state->check, hold);
609 if (state->head != Z_NULL)
610 state->head->done = -1;
611 if (!(state->wrap & 1) ||
615 ((BITS(8) << 8) + (hold >> 8)) % 31) {
616 strm->msg = (
char *)
"incorrect header check";
620 if (BITS(4) != Z_DEFLATED) {
621 strm->msg = (
char *)
"unknown compression method";
627 if (len > state->wbits) {
628 strm->msg = (
char *)
"invalid window size";
632 state->dmax = 1
U << len;
633 Tracev((stderr,
"inflate: zlib header ok\n"));
634 strm->adler = state->check = adler32(0L, Z_NULL, 0);
635 state->mode = hold & 0x200 ? DICTID : TYPE;
641 state->flags = (int)(hold);
642 if ((state->flags & 0xff) != Z_DEFLATED) {
643 strm->msg = (
char *)
"unknown compression method";
647 if (state->flags & 0xe000) {
648 strm->msg = (
char *)
"unknown header flags set";
652 if (state->head != Z_NULL)
653 state->head->text = (int)((hold >> 8) & 1);
654 if (state->flags & 0x0200) CRC2(state->check, hold);
659 if (state->head != Z_NULL)
660 state->head->time = hold;
661 if (state->flags & 0x0200) CRC4(state->check, hold);
666 if (state->head != Z_NULL) {
667 state->head->xflags = (int)(hold & 0xff);
668 state->head->os = (int)(hold >> 8);
670 if (state->flags & 0x0200) CRC2(state->check, hold);
674 if (state->flags & 0x0400) {
676 state->length = (unsigned)(hold);
677 if (state->head != Z_NULL)
678 state->head->extra_len = (unsigned)hold;
679 if (state->flags & 0x0200) CRC2(state->check, hold);
682 else if (state->head != Z_NULL)
683 state->head->extra = Z_NULL;
686 if (state->flags & 0x0400) {
687 copy = state->length;
688 if (copy > have) copy = have;
690 if (state->head != Z_NULL &&
691 state->head->extra != Z_NULL) {
692 len = state->head->extra_len - state->length;
693 zmemcpy(state->head->extra + len, next,
694 len + copy > state->head->extra_max ?
695 state->head->extra_max - len : copy);
697 if (state->flags & 0x0200)
698 state->check = crc32(state->check, next, copy);
701 state->length -= copy;
703 if (state->length)
goto inf_leave;
708 if (state->flags & 0x0800) {
709 if (have == 0)
goto inf_leave;
712 len = (unsigned)(next[copy++]);
713 if (state->head != Z_NULL &&
714 state->head->name != Z_NULL &&
715 state->length < state->head->name_max)
716 state->head->name[state->length++] = len;
717 }
while (len && copy < have);
718 if (state->flags & 0x0200)
719 state->check = crc32(state->check, next, copy);
722 if (len)
goto inf_leave;
724 else if (state->head != Z_NULL)
725 state->head->name = Z_NULL;
727 state->mode = COMMENT;
729 if (state->flags & 0x1000) {
730 if (have == 0)
goto inf_leave;
733 len = (unsigned)(next[copy++]);
734 if (state->head != Z_NULL &&
735 state->head->comment != Z_NULL &&
736 state->length < state->head->comm_max)
737 state->head->comment[state->length++] = len;
738 }
while (len && copy < have);
739 if (state->flags & 0x0200)
740 state->check = crc32(state->check, next, copy);
743 if (len)
goto inf_leave;
745 else if (state->head != Z_NULL)
746 state->head->comment = Z_NULL;
749 if (state->flags & 0x0200) {
751 if (hold != (state->check & 0xffff)) {
752 strm->msg = (
char *)
"header crc mismatch";
758 if (state->head != Z_NULL) {
759 state->head->hcrc = (int)((state->flags >> 9) & 1);
760 state->head->done = 1;
762 strm->adler = state->check = crc32(0L, Z_NULL, 0);
768 strm->adler = state->check = REVERSE(hold);
772 if (state->havedict == 0) {
776 strm->adler = state->check = adler32(0L, Z_NULL, 0);
779 if (flush == Z_BLOCK)
goto inf_leave;
787 state->last = BITS(1);
791 Tracev((stderr,
"inflate: stored block%s\n",
792 state->last ?
" (last)" :
""));
793 state->mode = STORED;
797 Tracev((stderr,
"inflate: fixed codes block%s\n",
798 state->last ?
" (last)" :
""));
802 Tracev((stderr,
"inflate: dynamic codes block%s\n",
803 state->last ?
" (last)" :
""));
807 strm->msg = (
char *)
"invalid block type";
815 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
816 strm->msg = (
char *)
"invalid stored block lengths";
820 state->length = (unsigned)hold & 0xffff;
821 Tracev((stderr,
"inflate: stored length %u\n",
826 copy = state->length;
828 if (copy > have) copy = have;
829 if (copy > left) copy = left;
830 if (copy == 0)
goto inf_leave;
832 Assert ( put != NULL,
"put is uninitialized" );
833 zmemcpy(put, next, copy);
838 state->length -= copy;
841 Tracev((stderr,
"inflate: stored end\n"));
846 state->nlen = BITS(5) + 257;
848 state->ndist = BITS(5) + 1;
850 state->ncode = BITS(4) + 4;
852 #ifndef PKZIP_BUG_WORKAROUND
853 if (state->nlen > 286 || state->ndist > 30) {
854 strm->msg = (
char *)
"too many length or distance symbols";
859 Tracev((stderr,
"inflate: table sizes ok\n"));
861 state->mode = LENLENS;
863 while (state->have < state->ncode) {
865 state->lens[order[state->have++]] = (
unsigned short)BITS(3);
868 while (state->have < 19)
869 state->lens[order[state->have++]] = 0;
870 state->next = state->codes;
871 state->lencode = (
code const FAR *)(state->next);
873 ret = inflate_table(CODES, state->lens, 19, &(state->next),
874 &(state->lenbits), state->work);
876 strm->msg = (
char *)
"invalid code lengths set";
880 Tracev((stderr,
"inflate: code lengths ok\n"));
882 state->mode = CODELENS;
884 while (state->have < state->nlen + state->ndist) {
886 this = state->lencode[BITS(state->lenbits)];
887 if ((
unsigned)(this.bits) <= bits)
break;
893 state->lens[state->have++] = this.val;
896 if (this.val == 16) {
897 NEEDBITS(this.bits + 2);
899 if (state->have == 0) {
900 strm->msg = (
char *)
"invalid bit length repeat";
904 len = state->lens[state->have - 1];
908 else if (this.val == 17) {
909 NEEDBITS(this.bits + 3);
916 NEEDBITS(this.bits + 7);
922 if (state->have + copy > state->nlen + state->ndist) {
923 strm->msg = (
char *)
"invalid bit length repeat";
928 state->lens[state->have++] = (
unsigned short)len;
933 if (state->mode == BAD)
break;
936 state->next = state->codes;
937 state->lencode = (
code const FAR *)(state->next);
939 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
940 &(state->lenbits), state->work);
942 strm->msg = (
char *)
"invalid literal/lengths set";
946 state->distcode = (
code const FAR *)(state->next);
948 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
949 &(state->next), &(state->distbits), state->work);
951 strm->msg = (
char *)
"invalid distances set";
955 Tracev((stderr,
"inflate: codes ok\n"));
958 if (have >= 6 && left >= 258) {
960 inflate_fast(strm, out);
965 this = state->lencode[BITS(state->lenbits)];
966 if ((
unsigned)(this.bits) <= bits)
break;
969 if (this.op && (this.op & 0xf0) == 0) {
972 this = state->lencode[last.val +
973 (BITS(last.bits + last.op) >> last.bits)];
974 if ((
unsigned)(last.bits + this.bits) <= bits)
break;
980 state->length = (unsigned)this.val;
981 if ((
int)(this.op) == 0) {
982 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
983 "inflate: literal '%c'\n" :
984 "inflate: literal 0x%02x\n", this.val));
989 Tracevv((stderr,
"inflate: end of block\n"));
994 strm->msg = (
char *)
"invalid literal/length code";
998 state->extra = (unsigned)(this.op) & 15;
999 state->mode = LENEXT;
1002 NEEDBITS(state->extra);
1003 state->length += BITS(state->extra);
1004 DROPBITS(state->extra);
1006 Tracevv((stderr,
"inflate: length %u\n", state->length));
1010 this = state->distcode[BITS(state->distbits)];
1011 if ((
unsigned)(this.bits) <= bits)
break;
1014 if ((this.op & 0xf0) == 0) {
1017 this = state->distcode[last.val +
1018 (BITS(last.bits + last.op) >> last.bits)];
1019 if ((
unsigned)(last.bits + this.bits) <= bits)
break;
1022 DROPBITS(last.bits);
1024 DROPBITS(this.bits);
1026 strm->msg = (
char *)
"invalid distance code";
1030 state->offset = (unsigned)this.val;
1031 state->extra = (unsigned)(this.op) & 15;
1032 state->mode = DISTEXT;
1035 NEEDBITS(state->extra);
1036 state->offset += BITS(state->extra);
1037 DROPBITS(state->extra);
1039 #ifdef INFLATE_STRICT
1040 if (state->offset > state->dmax) {
1041 strm->msg = (
char *)
"invalid distance too far back";
1046 if (state->offset > state->whave + out - left) {
1047 strm->msg = (
char *)
"invalid distance too far back";
1051 Tracevv((stderr,
"inflate: distance %u\n", state->offset));
1052 state->mode = MATCH;
1054 if (left == 0)
goto inf_leave;
1056 if (state->offset > copy) {
1057 copy = state->offset - copy;
1058 if (copy > state->write) {
1059 copy -= state->write;
1060 from = state->window + (state->wsize - copy);
1063 from = state->window + (state->write - copy);
1064 if (copy > state->length) copy = state->length;
1068 Assert ( put != NULL,
"put is uninitialized" );
1069 from = put - state->offset;
1070 copy = state->length;
1072 if (copy > left) copy = left;
1074 state->length -= copy;
1078 if (state->length == 0) state->mode = LEN;
1081 if (left == 0)
goto inf_leave;
1082 *put++ = (
unsigned char)(state->length);
1090 strm->total_out += out;
1091 state->total += out;
1094 Assert ( put != NULL,
"put is uninitialized" );
1095 strm->adler = state->check =
1096 UPDATE(state->check, put - out, out);
1101 state->flags ? hold :
1103 REVERSE(hold)) != state->check) {
1104 strm->msg = (
char *)
"incorrect data check";
1109 Tracev((stderr,
"inflate: check matches trailer\n"));
1112 state->mode = LENGTH;
1114 if (state->wrap && state->flags) {
1116 if (hold != (state->total & 0xffffffffUL)) {
1117 strm->msg = (
char *)
"incorrect length check";
1122 Tracev((stderr,
"inflate: length matches trailer\n"));
1136 return Z_STREAM_ERROR;
1147 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1148 if (updatewindow(strm, out)) {
1152 in -= strm->avail_in;
1153 out -= strm->avail_out;
1154 strm->total_in += in;
1155 strm->total_out += out;
1156 state->total += out;
1157 if (state->wrap && out)
1158 strm->adler = state->check =
1159 UPDATE(state->check, strm->next_out - out, out);
1160 strm->data_type = state->bits + (state->last ? 64 : 0) +
1161 (state->mode == TYPE ? 128 : 0);
1162 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1167 int ZEXPORT inflateEnd(strm)
1171 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
1172 return Z_STREAM_ERROR;
1174 if (state->window != Z_NULL) ZFREE(strm, state->window);
1175 ZFREE(strm, strm->state);
1176 strm->state = Z_NULL;
1177 Tracev((stderr,
"inflate: end\n"));
1181 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1183 const Bytef *dictionary;
1190 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
1192 if (state->wrap != 0 && state->mode != DICT)
1193 return Z_STREAM_ERROR;
1196 if (state->mode == DICT) {
1197 id = adler32(0L, Z_NULL, 0);
1198 id = adler32(
id, dictionary, dictLength);
1199 if (
id != state->check)
1200 return Z_DATA_ERROR;
1204 if (updatewindow(strm, strm->avail_out)) {
1208 if (dictLength > state->wsize) {
1209 zmemcpy(state->window, dictionary + dictLength - state->wsize,
1211 state->whave = state->wsize;
1214 zmemcpy(state->window + state->wsize - dictLength, dictionary,
1216 state->whave = dictLength;
1218 state->havedict = 1;
1219 Tracev((stderr,
"inflate: dictionary set\n"));
1223 int ZEXPORT inflateGetHeader(strm, head)
1230 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
1232 if ((state->wrap & 2) == 0)
return Z_STREAM_ERROR;
1251 local
unsigned syncsearch(have,
buf, len)
1253 unsigned char FAR *
buf;
1261 while (next < len && got < 4) {
1262 if ((
int)(buf[next]) == (got < 2 ? 0 : 0xff))
1274 int ZEXPORT inflateSync(strm)
1278 unsigned long in, out;
1279 unsigned char buf[4];
1283 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
1285 if (strm->avail_in == 0 && state->bits < 8)
return Z_BUF_ERROR;
1288 if (state->mode != SYNC) {
1290 state->hold <<= state->bits & 7;
1291 state->bits -= state->bits & 7;
1293 while (state->bits >= 8) {
1294 buf[len++] = (
unsigned char)(state->hold);
1299 syncsearch(&(state->have), buf, len);
1303 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1304 strm->avail_in -= len;
1305 strm->next_in += len;
1306 strm->total_in += len;
1309 if (state->have != 4)
return Z_DATA_ERROR;
1310 in = strm->total_in; out = strm->total_out;
1312 strm->total_in = in; strm->total_out = out;
1325 int ZEXPORT inflateSyncPoint(strm)
1330 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
1332 return state->mode == STORED && state->bits == 0;
1335 int ZEXPORT inflateCopy(dest, source)
1341 unsigned char FAR *window;
1345 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1346 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
1347 return Z_STREAM_ERROR;
1353 if (copy == Z_NULL)
return Z_MEM_ERROR;
1355 if (state->window != Z_NULL) {
1356 window = (
unsigned char FAR *)
1357 ZALLOC(source, 1
U << state->wbits,
sizeof(
unsigned char));
1358 if (window == Z_NULL) {
1359 ZFREE(source, copy);
1365 zmemcpy(dest, source,
sizeof(
z_stream));
1367 if (state->lencode >= state->codes &&
1368 state->lencode <= state->codes + ENOUGH - 1) {
1369 copy->lencode = copy->codes + (state->lencode - state->codes);
1370 copy->distcode = copy->codes + (state->distcode - state->codes);
1372 copy->next = copy->codes + (state->next - state->codes);
1373 if (window != Z_NULL) {
1374 wsize = 1
U << state->wbits;
1375 zmemcpy(window, state->window, wsize);
1377 copy->window = window;