OLD | NEW |
1 /* inflate.c -- zlib decompression | 1 /* inflate.c -- zlib decompression |
2 * Copyright (C) 1995-2010 Mark Adler | 2 * Copyright (C) 1995-2012 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 void fixedtables OF((struct inflate_state FAR *state)); | 95 local void fixedtables OF((struct inflate_state FAR *state)); |
96 local int updatewindow OF((z_streamp strm, unsigned out)); | 96 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, |
| 97 unsigned copy)); |
97 #ifdef BUILDFIXED | 98 #ifdef BUILDFIXED |
98 void makefixed OF((void)); | 99 void makefixed OF((void)); |
99 #endif | 100 #endif |
100 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, | 101 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, |
101 unsigned len)); | 102 unsigned len)); |
102 | 103 |
103 int ZEXPORT inflateReset(strm) | 104 int ZEXPORT inflateResetKeep(strm) |
104 z_streamp strm; | 105 z_streamp strm; |
105 { | 106 { |
106 struct inflate_state FAR *state; | 107 struct inflate_state FAR *state; |
107 | 108 |
108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 109 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
109 state = (struct inflate_state FAR *)strm->state; | 110 state = (struct inflate_state FAR *)strm->state; |
110 strm->total_in = strm->total_out = state->total = 0; | 111 strm->total_in = strm->total_out = state->total = 0; |
111 strm->msg = Z_NULL; | 112 strm->msg = Z_NULL; |
112 strm->adler = 1; /* to support ill-conceived Java test suite */ | 113 if (state->wrap) /* to support ill-conceived Java test suite */ |
| 114 strm->adler = state->wrap & 1; |
113 state->mode = HEAD; | 115 state->mode = HEAD; |
114 state->last = 0; | 116 state->last = 0; |
115 state->havedict = 0; | 117 state->havedict = 0; |
116 state->dmax = 32768U; | 118 state->dmax = 32768U; |
117 state->head = Z_NULL; | 119 state->head = Z_NULL; |
118 state->wsize = 0; | |
119 state->whave = 0; | |
120 state->wnext = 0; | |
121 state->hold = 0; | 120 state->hold = 0; |
122 state->bits = 0; | 121 state->bits = 0; |
123 state->lencode = state->distcode = state->next = state->codes; | 122 state->lencode = state->distcode = state->next = state->codes; |
124 state->sane = 1; | 123 state->sane = 1; |
125 state->back = -1; | 124 state->back = -1; |
126 Tracev((stderr, "inflate: reset\n")); | 125 Tracev((stderr, "inflate: reset\n")); |
127 return Z_OK; | 126 return Z_OK; |
128 } | 127 } |
129 | 128 |
| 129 int ZEXPORT inflateReset(strm) |
| 130 z_streamp strm; |
| 131 { |
| 132 struct inflate_state FAR *state; |
| 133 |
| 134 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 135 state = (struct inflate_state FAR *)strm->state; |
| 136 state->wsize = 0; |
| 137 state->whave = 0; |
| 138 state->wnext = 0; |
| 139 return inflateResetKeep(strm); |
| 140 } |
| 141 |
130 int ZEXPORT inflateReset2(strm, windowBits) | 142 int ZEXPORT inflateReset2(strm, windowBits) |
131 z_streamp strm; | 143 z_streamp strm; |
132 int windowBits; | 144 int windowBits; |
133 { | 145 { |
134 int wrap; | 146 int wrap; |
135 struct inflate_state FAR *state; | 147 struct inflate_state FAR *state; |
136 | 148 |
137 /* get the state */ | 149 /* get the state */ |
138 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 150 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
139 state = (struct inflate_state FAR *)strm->state; | 151 state = (struct inflate_state FAR *)strm->state; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 { | 185 { |
174 int ret; | 186 int ret; |
175 struct inflate_state FAR *state; | 187 struct inflate_state FAR *state; |
176 | 188 |
177 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || | 189 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
178 stream_size != (int)(sizeof(z_stream))) | 190 stream_size != (int)(sizeof(z_stream))) |
179 return Z_VERSION_ERROR; | 191 return Z_VERSION_ERROR; |
180 if (strm == Z_NULL) return Z_STREAM_ERROR; | 192 if (strm == Z_NULL) return Z_STREAM_ERROR; |
181 strm->msg = Z_NULL; /* in case we return an error */ | 193 strm->msg = Z_NULL; /* in case we return an error */ |
182 if (strm->zalloc == (alloc_func)0) { | 194 if (strm->zalloc == (alloc_func)0) { |
| 195 #ifdef Z_SOLO |
| 196 return Z_STREAM_ERROR; |
| 197 #else |
183 strm->zalloc = zcalloc; | 198 strm->zalloc = zcalloc; |
184 strm->opaque = (voidpf)0; | 199 strm->opaque = (voidpf)0; |
| 200 #endif |
185 } | 201 } |
186 if (strm->zfree == (free_func)0) strm->zfree = zcfree; | 202 if (strm->zfree == (free_func)0) |
| 203 #ifdef Z_SOLO |
| 204 return Z_STREAM_ERROR; |
| 205 #else |
| 206 strm->zfree = zcfree; |
| 207 #endif |
187 state = (struct inflate_state FAR *) | 208 state = (struct inflate_state FAR *) |
188 ZALLOC(strm, 1, sizeof(struct inflate_state)); | 209 ZALLOC(strm, 1, sizeof(struct inflate_state)); |
189 if (state == Z_NULL) return Z_MEM_ERROR; | 210 if (state == Z_NULL) return Z_MEM_ERROR; |
190 Tracev((stderr, "inflate: allocated\n")); | 211 Tracev((stderr, "inflate: allocated\n")); |
191 strm->state = (struct internal_state FAR *)state; | 212 strm->state = (struct internal_state FAR *)state; |
192 state->window = Z_NULL; | 213 state->window = Z_NULL; |
193 ret = inflateReset2(strm, windowBits); | 214 ret = inflateReset2(strm, windowBits); |
194 if (ret != Z_OK) { | 215 if (ret != Z_OK) { |
195 ZFREE(strm, state); | 216 ZFREE(strm, state); |
196 strm->state = Z_NULL; | 217 strm->state = Z_NULL; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 puts(" /* WARNING: this file should *not* be used by applications."); | 335 puts(" /* WARNING: this file should *not* be used by applications."); |
315 puts(" It is part of the implementation of this library and is"); | 336 puts(" It is part of the implementation of this library and is"); |
316 puts(" subject to change. Applications should only use zlib.h."); | 337 puts(" subject to change. Applications should only use zlib.h."); |
317 puts(" */"); | 338 puts(" */"); |
318 puts(""); | 339 puts(""); |
319 size = 1U << 9; | 340 size = 1U << 9; |
320 printf(" static const code lenfix[%u] = {", size); | 341 printf(" static const code lenfix[%u] = {", size); |
321 low = 0; | 342 low = 0; |
322 for (;;) { | 343 for (;;) { |
323 if ((low % 7) == 0) printf("\n "); | 344 if ((low % 7) == 0) printf("\n "); |
324 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, | 345 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, |
325 state.lencode[low].val); | 346 state.lencode[low].bits, state.lencode[low].val); |
326 if (++low == size) break; | 347 if (++low == size) break; |
327 putchar(','); | 348 putchar(','); |
328 } | 349 } |
329 puts("\n };"); | 350 puts("\n };"); |
330 size = 1U << 5; | 351 size = 1U << 5; |
331 printf("\n static const code distfix[%u] = {", size); | 352 printf("\n static const code distfix[%u] = {", size); |
332 low = 0; | 353 low = 0; |
333 for (;;) { | 354 for (;;) { |
334 if ((low % 6) == 0) printf("\n "); | 355 if ((low % 6) == 0) printf("\n "); |
335 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, | 356 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, |
(...skipping 12 matching lines...) Expand all Loading... |
348 inflate call, but the end of the deflate stream has not been reached yet. | 369 inflate call, but the end of the deflate stream has not been reached yet. |
349 It is also called to create a window for dictionary data when a dictionary | 370 It is also called to create a window for dictionary data when a dictionary |
350 is loaded. | 371 is loaded. |
351 | 372 |
352 Providing output buffers larger than 32K to inflate() should provide a speed | 373 Providing output buffers larger than 32K to inflate() should provide a speed |
353 advantage, since only the last 32K of output is copied to the sliding window | 374 advantage, since only the last 32K of output is copied to the sliding window |
354 upon return from inflate(), and since all distances after the first 32K of | 375 upon return from inflate(), and since all distances after the first 32K of |
355 output will fall in the output data, making match copies simpler and faster. | 376 output will fall in the output data, making match copies simpler and faster. |
356 The advantage may be dependent on the size of the processor's data caches. | 377 The advantage may be dependent on the size of the processor's data caches. |
357 */ | 378 */ |
358 local int updatewindow(strm, out) | 379 local int updatewindow(strm, end, copy) |
359 z_streamp strm; | 380 z_streamp strm; |
360 unsigned out; | 381 const Bytef *end; |
| 382 unsigned copy; |
361 { | 383 { |
362 struct inflate_state FAR *state; | 384 struct inflate_state FAR *state; |
363 unsigned copy, dist; | 385 unsigned dist; |
364 | 386 |
365 state = (struct inflate_state FAR *)strm->state; | 387 state = (struct inflate_state FAR *)strm->state; |
366 | 388 |
367 /* if it hasn't been done already, allocate space for the window */ | 389 /* if it hasn't been done already, allocate space for the window */ |
368 if (state->window == Z_NULL) { | 390 if (state->window == Z_NULL) { |
369 state->window = (unsigned char FAR *) | 391 state->window = (unsigned char FAR *) |
370 ZALLOC(strm, 1U << state->wbits, | 392 ZALLOC(strm, 1U << state->wbits, |
371 sizeof(unsigned char)); | 393 sizeof(unsigned char)); |
372 if (state->window == Z_NULL) return 1; | 394 if (state->window == Z_NULL) return 1; |
373 } | 395 } |
374 | 396 |
375 /* if window not in use yet, initialize */ | 397 /* if window not in use yet, initialize */ |
376 if (state->wsize == 0) { | 398 if (state->wsize == 0) { |
377 state->wsize = 1U << state->wbits; | 399 state->wsize = 1U << state->wbits; |
378 state->wnext = 0; | 400 state->wnext = 0; |
379 state->whave = 0; | 401 state->whave = 0; |
380 } | 402 } |
381 | 403 |
382 /* copy state->wsize or less output bytes into the circular window */ | 404 /* copy state->wsize or less output bytes into the circular window */ |
383 copy = out - strm->avail_out; | |
384 if (copy >= state->wsize) { | 405 if (copy >= state->wsize) { |
385 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); | 406 zmemcpy(state->window, end - state->wsize, state->wsize); |
386 state->wnext = 0; | 407 state->wnext = 0; |
387 state->whave = state->wsize; | 408 state->whave = state->wsize; |
388 } | 409 } |
389 else { | 410 else { |
390 dist = state->wsize - state->wnext; | 411 dist = state->wsize - state->wnext; |
391 if (dist > copy) dist = copy; | 412 if (dist > copy) dist = copy; |
392 zmemcpy(state->window + state->wnext, strm->next_out - copy, dist); | 413 zmemcpy(state->window + state->wnext, end - copy, dist); |
393 copy -= dist; | 414 copy -= dist; |
394 if (copy) { | 415 if (copy) { |
395 zmemcpy(state->window, strm->next_out - copy, copy); | 416 zmemcpy(state->window, end - copy, copy); |
396 state->wnext = copy; | 417 state->wnext = copy; |
397 state->whave = state->wsize; | 418 state->whave = state->wsize; |
398 } | 419 } |
399 else { | 420 else { |
400 state->wnext += dist; | 421 state->wnext += dist; |
401 if (state->wnext == state->wsize) state->wnext = 0; | 422 if (state->wnext == state->wsize) state->wnext = 0; |
402 if (state->whave < state->wsize) state->whave += dist; | 423 if (state->whave < state->wsize) state->whave += dist; |
403 } | 424 } |
404 } | 425 } |
405 return 0; | 426 return 0; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 bits -= (unsigned)(n); \ | 513 bits -= (unsigned)(n); \ |
493 } while (0) | 514 } while (0) |
494 | 515 |
495 /* Remove zero to seven bits as needed to go to a byte boundary */ | 516 /* Remove zero to seven bits as needed to go to a byte boundary */ |
496 #define BYTEBITS() \ | 517 #define BYTEBITS() \ |
497 do { \ | 518 do { \ |
498 hold >>= bits & 7; \ | 519 hold >>= bits & 7; \ |
499 bits -= bits & 7; \ | 520 bits -= bits & 7; \ |
500 } while (0) | 521 } while (0) |
501 | 522 |
502 /* Reverse the bytes in a 32-bit value */ | |
503 #define REVERSE(q) \ | |
504 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ | |
505 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) | |
506 | |
507 /* | 523 /* |
508 inflate() uses a state machine to process as much input data and generate as | 524 inflate() uses a state machine to process as much input data and generate as |
509 much output data as possible before returning. The state machine is | 525 much output data as possible before returning. The state machine is |
510 structured roughly as follows: | 526 structured roughly as follows: |
511 | 527 |
512 for (;;) switch (state) { | 528 for (;;) switch (state) { |
513 ... | 529 ... |
514 case STATEn: | 530 case STATEn: |
515 if (not enough input data or output space to make progress) | 531 if (not enough input data or output space to make progress) |
516 return; | 532 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 stream available. So the only thing the flush parameter actually does is: | 600 stream available. So the only thing the flush parameter actually does is: |
585 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it | 601 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
586 will return Z_BUF_ERROR if it has not reached the end of the stream. | 602 will return Z_BUF_ERROR if it has not reached the end of the stream. |
587 */ | 603 */ |
588 | 604 |
589 int ZEXPORT inflate(strm, flush) | 605 int ZEXPORT inflate(strm, flush) |
590 z_streamp strm; | 606 z_streamp strm; |
591 int flush; | 607 int flush; |
592 { | 608 { |
593 struct inflate_state FAR *state; | 609 struct inflate_state FAR *state; |
594 unsigned char FAR *next; /* next input */ | 610 z_const unsigned char FAR *next; /* next input */ |
595 unsigned char FAR *put; /* next output */ | 611 unsigned char FAR *put; /* next output */ |
596 unsigned have, left; /* available input and output */ | 612 unsigned have, left; /* available input and output */ |
597 unsigned long hold; /* bit buffer */ | 613 unsigned long hold; /* bit buffer */ |
598 unsigned bits; /* bits in bit buffer */ | 614 unsigned bits; /* bits in bit buffer */ |
599 unsigned in, out; /* save starting available input and output */ | 615 unsigned in, out; /* save starting available input and output */ |
600 unsigned copy; /* number of stored or match bytes to copy */ | 616 unsigned copy; /* number of stored or match bytes to copy */ |
601 unsigned char FAR *from; /* where to copy match bytes from */ | 617 unsigned char FAR *from; /* where to copy match bytes from */ |
602 code here; /* current decoding table entry */ | 618 code here; /* current decoding table entry */ |
603 code last; /* parent table entry */ | 619 code last; /* parent table entry */ |
604 unsigned len; /* length to copy for repeats, bits to drop */ | 620 unsigned len; /* length to copy for repeats, bits to drop */ |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 if (state->head != Z_NULL) { | 806 if (state->head != Z_NULL) { |
791 state->head->hcrc = (int)((state->flags >> 9) & 1); | 807 state->head->hcrc = (int)((state->flags >> 9) & 1); |
792 state->head->done = 1; | 808 state->head->done = 1; |
793 } | 809 } |
794 strm->adler = state->check = crc32(0L, Z_NULL, 0); | 810 strm->adler = state->check = crc32(0L, Z_NULL, 0); |
795 state->mode = TYPE; | 811 state->mode = TYPE; |
796 break; | 812 break; |
797 #endif | 813 #endif |
798 case DICTID: | 814 case DICTID: |
799 NEEDBITS(32); | 815 NEEDBITS(32); |
800 strm->adler = state->check = REVERSE(hold); | 816 strm->adler = state->check = ZSWAP32(hold); |
801 INITBITS(); | 817 INITBITS(); |
802 state->mode = DICT; | 818 state->mode = DICT; |
803 case DICT: | 819 case DICT: |
804 if (state->havedict == 0) { | 820 if (state->havedict == 0) { |
805 RESTORE(); | 821 RESTORE(); |
806 return Z_NEED_DICT; | 822 return Z_NEED_DICT; |
807 } | 823 } |
808 strm->adler = state->check = adler32(0L, Z_NULL, 0); | 824 strm->adler = state->check = adler32(0L, Z_NULL, 0); |
809 state->mode = TYPE; | 825 state->mode = TYPE; |
810 case TYPE: | 826 case TYPE: |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 state->mode = LENLENS; | 914 state->mode = LENLENS; |
899 case LENLENS: | 915 case LENLENS: |
900 while (state->have < state->ncode) { | 916 while (state->have < state->ncode) { |
901 NEEDBITS(3); | 917 NEEDBITS(3); |
902 state->lens[order[state->have++]] = (unsigned short)BITS(3); | 918 state->lens[order[state->have++]] = (unsigned short)BITS(3); |
903 DROPBITS(3); | 919 DROPBITS(3); |
904 } | 920 } |
905 while (state->have < 19) | 921 while (state->have < 19) |
906 state->lens[order[state->have++]] = 0; | 922 state->lens[order[state->have++]] = 0; |
907 state->next = state->codes; | 923 state->next = state->codes; |
908 state->lencode = (code const FAR *)(state->next); | 924 state->lencode = (const code FAR *)(state->next); |
909 state->lenbits = 7; | 925 state->lenbits = 7; |
910 ret = inflate_table(CODES, state->lens, 19, &(state->next), | 926 ret = inflate_table(CODES, state->lens, 19, &(state->next), |
911 &(state->lenbits), state->work); | 927 &(state->lenbits), state->work); |
912 if (ret) { | 928 if (ret) { |
913 strm->msg = (char *)"invalid code lengths set"; | 929 strm->msg = (char *)"invalid code lengths set"; |
914 state->mode = BAD; | 930 state->mode = BAD; |
915 break; | 931 break; |
916 } | 932 } |
917 Tracev((stderr, "inflate: code lengths ok\n")); | 933 Tracev((stderr, "inflate: code lengths ok\n")); |
918 state->have = 0; | 934 state->have = 0; |
919 state->mode = CODELENS; | 935 state->mode = CODELENS; |
920 case CODELENS: | 936 case CODELENS: |
921 while (state->have < state->nlen + state->ndist) { | 937 while (state->have < state->nlen + state->ndist) { |
922 for (;;) { | 938 for (;;) { |
923 here = state->lencode[BITS(state->lenbits)]; | 939 here = state->lencode[BITS(state->lenbits)]; |
924 if ((unsigned)(here.bits) <= bits) break; | 940 if ((unsigned)(here.bits) <= bits) break; |
925 PULLBYTE(); | 941 PULLBYTE(); |
926 } | 942 } |
927 if (here.val < 16) { | 943 if (here.val < 16) { |
928 NEEDBITS(here.bits); | |
929 DROPBITS(here.bits); | 944 DROPBITS(here.bits); |
930 state->lens[state->have++] = here.val; | 945 state->lens[state->have++] = here.val; |
931 } | 946 } |
932 else { | 947 else { |
933 if (here.val == 16) { | 948 if (here.val == 16) { |
934 NEEDBITS(here.bits + 2); | 949 NEEDBITS(here.bits + 2); |
935 DROPBITS(here.bits); | 950 DROPBITS(here.bits); |
936 if (state->have == 0) { | 951 if (state->have == 0) { |
937 strm->msg = (char *)"invalid bit length repeat"; | 952 strm->msg = (char *)"invalid bit length repeat"; |
938 state->mode = BAD; | 953 state->mode = BAD; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 if (state->lens[256] == 0) { | 988 if (state->lens[256] == 0) { |
974 strm->msg = (char *)"invalid code -- missing end-of-block"; | 989 strm->msg = (char *)"invalid code -- missing end-of-block"; |
975 state->mode = BAD; | 990 state->mode = BAD; |
976 break; | 991 break; |
977 } | 992 } |
978 | 993 |
979 /* build code tables -- note: do not change the lenbits or distbits | 994 /* build code tables -- note: do not change the lenbits or distbits |
980 values here (9 and 6) without reading the comments in inftrees.h | 995 values here (9 and 6) without reading the comments in inftrees.h |
981 concerning the ENOUGH constants, which depend on those values */ | 996 concerning the ENOUGH constants, which depend on those values */ |
982 state->next = state->codes; | 997 state->next = state->codes; |
983 state->lencode = (code const FAR *)(state->next); | 998 state->lencode = (const code FAR *)(state->next); |
984 state->lenbits = 9; | 999 state->lenbits = 9; |
985 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), | 1000 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
986 &(state->lenbits), state->work); | 1001 &(state->lenbits), state->work); |
987 if (ret) { | 1002 if (ret) { |
988 strm->msg = (char *)"invalid literal/lengths set"; | 1003 strm->msg = (char *)"invalid literal/lengths set"; |
989 state->mode = BAD; | 1004 state->mode = BAD; |
990 break; | 1005 break; |
991 } | 1006 } |
992 state->distcode = (code const FAR *)(state->next); | 1007 state->distcode = (const code FAR *)(state->next); |
993 state->distbits = 6; | 1008 state->distbits = 6; |
994 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, | 1009 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, |
995 &(state->next), &(state->distbits), state->work); | 1010 &(state->next), &(state->distbits), state->work); |
996 if (ret) { | 1011 if (ret) { |
997 strm->msg = (char *)"invalid distances set"; | 1012 strm->msg = (char *)"invalid distances set"; |
998 state->mode = BAD; | 1013 state->mode = BAD; |
999 break; | 1014 break; |
1000 } | 1015 } |
1001 Tracev((stderr, "inflate: codes ok\n")); | 1016 Tracev((stderr, "inflate: codes ok\n")); |
1002 state->mode = LEN_; | 1017 state->mode = LEN_; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1163 strm->total_out += out; | 1178 strm->total_out += out; |
1164 state->total += out; | 1179 state->total += out; |
1165 if (out) | 1180 if (out) |
1166 strm->adler = state->check = | 1181 strm->adler = state->check = |
1167 UPDATE(state->check, put - out, out); | 1182 UPDATE(state->check, put - out, out); |
1168 out = left; | 1183 out = left; |
1169 if (( | 1184 if (( |
1170 #ifdef GUNZIP | 1185 #ifdef GUNZIP |
1171 state->flags ? hold : | 1186 state->flags ? hold : |
1172 #endif | 1187 #endif |
1173 REVERSE(hold)) != state->check) { | 1188 ZSWAP32(hold)) != state->check) { |
1174 strm->msg = (char *)"incorrect data check"; | 1189 strm->msg = (char *)"incorrect data check"; |
1175 state->mode = BAD; | 1190 state->mode = BAD; |
1176 break; | 1191 break; |
1177 } | 1192 } |
1178 INITBITS(); | 1193 INITBITS(); |
1179 Tracev((stderr, "inflate: check matches trailer\n")); | 1194 Tracev((stderr, "inflate: check matches trailer\n")); |
1180 } | 1195 } |
1181 #ifdef GUNZIP | 1196 #ifdef GUNZIP |
1182 state->mode = LENGTH; | 1197 state->mode = LENGTH; |
1183 case LENGTH: | 1198 case LENGTH: |
(...skipping 23 matching lines...) Expand all Loading... |
1207 } | 1222 } |
1208 | 1223 |
1209 /* | 1224 /* |
1210 Return from inflate(), updating the total counts and the check value. | 1225 Return from inflate(), updating the total counts and the check value. |
1211 If there was no progress during the inflate() call, return a buffer | 1226 If there was no progress during the inflate() call, return a buffer |
1212 error. Call updatewindow() to create and/or update the window state. | 1227 error. Call updatewindow() to create and/or update the window state. |
1213 Note: a memory error from inflate() is non-recoverable. | 1228 Note: a memory error from inflate() is non-recoverable. |
1214 */ | 1229 */ |
1215 inf_leave: | 1230 inf_leave: |
1216 RESTORE(); | 1231 RESTORE(); |
1217 if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) | 1232 if (state->wsize || (out != strm->avail_out && state->mode < BAD && |
1218 if (updatewindow(strm, out)) { | 1233 (state->mode < CHECK || flush != Z_FINISH))) |
| 1234 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { |
1219 state->mode = MEM; | 1235 state->mode = MEM; |
1220 return Z_MEM_ERROR; | 1236 return Z_MEM_ERROR; |
1221 } | 1237 } |
1222 in -= strm->avail_in; | 1238 in -= strm->avail_in; |
1223 out -= strm->avail_out; | 1239 out -= strm->avail_out; |
1224 strm->total_in += in; | 1240 strm->total_in += in; |
1225 strm->total_out += out; | 1241 strm->total_out += out; |
1226 state->total += out; | 1242 state->total += out; |
1227 if (state->wrap && out) | 1243 if (state->wrap && out) |
1228 strm->adler = state->check = | 1244 strm->adler = state->check = |
(...skipping 13 matching lines...) Expand all Loading... |
1242 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) | 1258 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
1243 return Z_STREAM_ERROR; | 1259 return Z_STREAM_ERROR; |
1244 state = (struct inflate_state FAR *)strm->state; | 1260 state = (struct inflate_state FAR *)strm->state; |
1245 if (state->window != Z_NULL) ZFREE(strm, state->window); | 1261 if (state->window != Z_NULL) ZFREE(strm, state->window); |
1246 ZFREE(strm, strm->state); | 1262 ZFREE(strm, strm->state); |
1247 strm->state = Z_NULL; | 1263 strm->state = Z_NULL; |
1248 Tracev((stderr, "inflate: end\n")); | 1264 Tracev((stderr, "inflate: end\n")); |
1249 return Z_OK; | 1265 return Z_OK; |
1250 } | 1266 } |
1251 | 1267 |
| 1268 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) |
| 1269 z_streamp strm; |
| 1270 Bytef *dictionary; |
| 1271 uInt *dictLength; |
| 1272 { |
| 1273 struct inflate_state FAR *state; |
| 1274 |
| 1275 /* check state */ |
| 1276 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 1277 state = (struct inflate_state FAR *)strm->state; |
| 1278 |
| 1279 /* copy dictionary */ |
| 1280 if (state->whave && dictionary != Z_NULL) { |
| 1281 zmemcpy(dictionary, state->window + state->wnext, |
| 1282 state->whave - state->wnext); |
| 1283 zmemcpy(dictionary + state->whave - state->wnext, |
| 1284 state->window, state->wnext); |
| 1285 } |
| 1286 if (dictLength != Z_NULL) |
| 1287 *dictLength = state->whave; |
| 1288 return Z_OK; |
| 1289 } |
| 1290 |
1252 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) | 1291 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) |
1253 z_streamp strm; | 1292 z_streamp strm; |
1254 const Bytef *dictionary; | 1293 const Bytef *dictionary; |
1255 uInt dictLength; | 1294 uInt dictLength; |
1256 { | 1295 { |
1257 struct inflate_state FAR *state; | 1296 struct inflate_state FAR *state; |
1258 unsigned long id; | 1297 unsigned long dictid; |
| 1298 int ret; |
1259 | 1299 |
1260 /* check state */ | 1300 /* check state */ |
1261 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1301 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
1262 state = (struct inflate_state FAR *)strm->state; | 1302 state = (struct inflate_state FAR *)strm->state; |
1263 if (state->wrap != 0 && state->mode != DICT) | 1303 if (state->wrap != 0 && state->mode != DICT) |
1264 return Z_STREAM_ERROR; | 1304 return Z_STREAM_ERROR; |
1265 | 1305 |
1266 /* check for correct dictionary id */ | 1306 /* check for correct dictionary identifier */ |
1267 if (state->mode == DICT) { | 1307 if (state->mode == DICT) { |
1268 id = adler32(0L, Z_NULL, 0); | 1308 dictid = adler32(0L, Z_NULL, 0); |
1269 id = adler32(id, dictionary, dictLength); | 1309 dictid = adler32(dictid, dictionary, dictLength); |
1270 if (id != state->check) | 1310 if (dictid != state->check) |
1271 return Z_DATA_ERROR; | 1311 return Z_DATA_ERROR; |
1272 } | 1312 } |
1273 | 1313 |
1274 /* copy dictionary to window */ | 1314 /* copy dictionary to window using updatewindow(), which will amend the |
1275 if (updatewindow(strm, strm->avail_out)) { | 1315 existing dictionary if appropriate */ |
| 1316 ret = updatewindow(strm, dictionary + dictLength, dictLength); |
| 1317 if (ret) { |
1276 state->mode = MEM; | 1318 state->mode = MEM; |
1277 return Z_MEM_ERROR; | 1319 return Z_MEM_ERROR; |
1278 } | 1320 } |
1279 if (dictLength > state->wsize) { | |
1280 zmemcpy(state->window, dictionary + dictLength - state->wsize, | |
1281 state->wsize); | |
1282 state->whave = state->wsize; | |
1283 } | |
1284 else { | |
1285 zmemcpy(state->window + state->wsize - dictLength, dictionary, | |
1286 dictLength); | |
1287 state->whave = dictLength; | |
1288 } | |
1289 state->havedict = 1; | 1321 state->havedict = 1; |
1290 Tracev((stderr, "inflate: dictionary set\n")); | 1322 Tracev((stderr, "inflate: dictionary set\n")); |
1291 return Z_OK; | 1323 return Z_OK; |
1292 } | 1324 } |
1293 | 1325 |
1294 int ZEXPORT inflateGetHeader(strm, head) | 1326 int ZEXPORT inflateGetHeader(strm, head) |
1295 z_streamp strm; | 1327 z_streamp strm; |
1296 gz_headerp head; | 1328 gz_headerp head; |
1297 { | 1329 { |
1298 struct inflate_state FAR *state; | 1330 struct inflate_state FAR *state; |
(...skipping 15 matching lines...) Expand all Loading... |
1314 found in order so far, in 0..3. On return *have is updated to the new | 1346 found in order so far, in 0..3. On return *have is updated to the new |
1315 state. If on return *have equals four, then the pattern was found and the | 1347 state. If on return *have equals four, then the pattern was found and the |
1316 return value is how many bytes were read including the last byte of the | 1348 return value is how many bytes were read including the last byte of the |
1317 pattern. If *have is less than four, then the pattern has not been found | 1349 pattern. If *have is less than four, then the pattern has not been found |
1318 yet and the return value is len. In the latter case, syncsearch() can be | 1350 yet and the return value is len. In the latter case, syncsearch() can be |
1319 called again with more data and the *have state. *have is initialized to | 1351 called again with more data and the *have state. *have is initialized to |
1320 zero for the first call. | 1352 zero for the first call. |
1321 */ | 1353 */ |
1322 local unsigned syncsearch(have, buf, len) | 1354 local unsigned syncsearch(have, buf, len) |
1323 unsigned FAR *have; | 1355 unsigned FAR *have; |
1324 unsigned char FAR *buf; | 1356 const unsigned char FAR *buf; |
1325 unsigned len; | 1357 unsigned len; |
1326 { | 1358 { |
1327 unsigned got; | 1359 unsigned got; |
1328 unsigned next; | 1360 unsigned next; |
1329 | 1361 |
1330 got = *have; | 1362 got = *have; |
1331 next = 0; | 1363 next = 0; |
1332 while (next < len && got < 4) { | 1364 while (next < len && got < 4) { |
1333 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) | 1365 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) |
1334 got++; | 1366 got++; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1426 if (state->window != Z_NULL) { | 1458 if (state->window != Z_NULL) { |
1427 window = (unsigned char FAR *) | 1459 window = (unsigned char FAR *) |
1428 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); | 1460 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); |
1429 if (window == Z_NULL) { | 1461 if (window == Z_NULL) { |
1430 ZFREE(source, copy); | 1462 ZFREE(source, copy); |
1431 return Z_MEM_ERROR; | 1463 return Z_MEM_ERROR; |
1432 } | 1464 } |
1433 } | 1465 } |
1434 | 1466 |
1435 /* copy state */ | 1467 /* copy state */ |
1436 zmemcpy(dest, source, sizeof(z_stream)); | 1468 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
1437 zmemcpy(copy, state, sizeof(struct inflate_state)); | 1469 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); |
1438 if (state->lencode >= state->codes && | 1470 if (state->lencode >= state->codes && |
1439 state->lencode <= state->codes + ENOUGH - 1) { | 1471 state->lencode <= state->codes + ENOUGH - 1) { |
1440 copy->lencode = copy->codes + (state->lencode - state->codes); | 1472 copy->lencode = copy->codes + (state->lencode - state->codes); |
1441 copy->distcode = copy->codes + (state->distcode - state->codes); | 1473 copy->distcode = copy->codes + (state->distcode - state->codes); |
1442 } | 1474 } |
1443 copy->next = copy->codes + (state->next - state->codes); | 1475 copy->next = copy->codes + (state->next - state->codes); |
1444 if (window != Z_NULL) { | 1476 if (window != Z_NULL) { |
1445 wsize = 1U << state->wbits; | 1477 wsize = 1U << state->wbits; |
1446 zmemcpy(window, state->window, wsize); | 1478 zmemcpy(window, state->window, wsize); |
1447 } | 1479 } |
(...skipping 23 matching lines...) Expand all Loading... |
1471 z_streamp strm; | 1503 z_streamp strm; |
1472 { | 1504 { |
1473 struct inflate_state FAR *state; | 1505 struct inflate_state FAR *state; |
1474 | 1506 |
1475 if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; | 1507 if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; |
1476 state = (struct inflate_state FAR *)strm->state; | 1508 state = (struct inflate_state FAR *)strm->state; |
1477 return ((long)(state->back) << 16) + | 1509 return ((long)(state->back) << 16) + |
1478 (state->mode == COPY ? state->length : | 1510 (state->mode == COPY ? state->length : |
1479 (state->mode == MATCH ? state->was - state->length : 0)); | 1511 (state->mode == MATCH ? state->was - state->length : 0)); |
1480 } | 1512 } |
OLD | NEW |