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