Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/zlib/inflate.c

Issue 8806004: Update zlib to 1.2.5. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/zlib/inflate.h ('k') | third_party/zlib/inftrees.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* inflate.c -- zlib decompression 1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 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 25 matching lines...) Expand all
38 * - Use local copies of stream next and avail values, as well as local bit 38 * - Use local copies of stream next and avail values, as well as local bit
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used 39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
40 * 40 *
41 * 1.2.beta4 1 Jan 2003 41 * 1.2.beta4 1 Jan 2003
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings 42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c 43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
44 * - Add comments in inffast.c to introduce the inflate_fast() routine 44 * - Add comments in inffast.c to introduce the inflate_fast() routine
45 * - Rearrange window copies in inflate_fast() for speed and simplification 45 * - Rearrange window copies in inflate_fast() for speed and simplification
46 * - Unroll last copy for window match in inflate_fast() 46 * - Unroll last copy for window match in inflate_fast()
47 * - Use local copies of window variables in inflate_fast() for speed 47 * - Use local copies of window variables in inflate_fast() for speed
48 * - Pull out common write == 0 case for speed in inflate_fast() 48 * - Pull out common wnext == 0 case for speed in inflate_fast()
49 * - Make op and len in inflate_fast() unsigned for consistency 49 * - Make op and len in inflate_fast() unsigned for consistency
50 * - Add FAR to lcode and dcode declarations in inflate_fast() 50 * - Add FAR to lcode and dcode declarations in inflate_fast()
51 * - Simplified bad distance check in inflate_fast() 51 * - Simplified bad distance check in inflate_fast()
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new 52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53 * source file infback.c to provide a call-back interface to inflate for 53 * source file infback.c to provide a call-back interface to inflate for
54 * programs like gzip and unzip -- uses window as output buffer to avoid 54 * programs like gzip and unzip -- uses window as output buffer to avoid
55 * window copying 55 * window copying
56 * 56 *
57 * 1.2.beta5 1 Jan 2003 57 * 1.2.beta5 1 Jan 2003
58 * - Improved inflateBack() interface to allow the caller to provide initial 58 * - Improved inflateBack() interface to allow the caller to provide initial
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 strm->total_in = strm->total_out = state->total = 0; 110 strm->total_in = strm->total_out = state->total = 0;
111 strm->msg = Z_NULL; 111 strm->msg = Z_NULL;
112 strm->adler = 1; /* to support ill-conceived Java test suite */ 112 strm->adler = 1; /* to support ill-conceived Java test suite */
113 state->mode = HEAD; 113 state->mode = HEAD;
114 state->last = 0; 114 state->last = 0;
115 state->havedict = 0; 115 state->havedict = 0;
116 state->dmax = 32768U; 116 state->dmax = 32768U;
117 state->head = Z_NULL; 117 state->head = Z_NULL;
118 state->wsize = 0; 118 state->wsize = 0;
119 state->whave = 0; 119 state->whave = 0;
120 state->write = 0; 120 state->wnext = 0;
121 state->hold = 0; 121 state->hold = 0;
122 state->bits = 0; 122 state->bits = 0;
123 state->lencode = state->distcode = state->next = state->codes; 123 state->lencode = state->distcode = state->next = state->codes;
124 state->sane = 1;
125 state->back = -1;
124 Tracev((stderr, "inflate: reset\n")); 126 Tracev((stderr, "inflate: reset\n"));
125 return Z_OK; 127 return Z_OK;
126 } 128 }
127 129
128 int ZEXPORT inflatePrime(strm, bits, value) 130 int ZEXPORT inflateReset2(strm, windowBits)
129 z_streamp strm; 131 z_streamp strm;
130 int bits; 132 int windowBits;
131 int value;
132 { 133 {
134 int wrap;
133 struct inflate_state FAR *state; 135 struct inflate_state FAR *state;
134 136
137 /* get the state */
135 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;
136 state = (struct inflate_state FAR *)strm->state; 139 state = (struct inflate_state FAR *)strm->state;
137 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; 140
138 value &= (1L << bits) - 1; 141 /* extract wrap request from windowBits parameter */
139 state->hold += value << state->bits; 142 if (windowBits < 0) {
140 state->bits += bits; 143 wrap = 0;
141 return Z_OK; 144 windowBits = -windowBits;
145 }
146 else {
147 wrap = (windowBits >> 4) + 1;
148 #ifdef GUNZIP
149 if (windowBits < 48)
150 windowBits &= 15;
151 #endif
152 }
153
154 /* set number of window bits, free window if different */
155 if (windowBits && (windowBits < 8 || windowBits > 15))
156 return Z_STREAM_ERROR;
157 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
158 ZFREE(strm, state->window);
159 state->window = Z_NULL;
160 }
161
162 /* update state and reset the rest of it */
163 state->wrap = wrap;
164 state->wbits = (unsigned)windowBits;
165 return inflateReset(strm);
142 } 166 }
143 167
144 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) 168 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
145 z_streamp strm; 169 z_streamp strm;
146 int windowBits; 170 int windowBits;
147 const char *version; 171 const char *version;
148 int stream_size; 172 int stream_size;
149 { 173 {
174 int ret;
150 struct inflate_state FAR *state; 175 struct inflate_state FAR *state;
151 176
152 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 177 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
153 stream_size != (int)(sizeof(z_stream))) 178 stream_size != (int)(sizeof(z_stream)))
154 return Z_VERSION_ERROR; 179 return Z_VERSION_ERROR;
155 if (strm == Z_NULL) return Z_STREAM_ERROR; 180 if (strm == Z_NULL) return Z_STREAM_ERROR;
156 strm->msg = Z_NULL; /* in case we return an error */ 181 strm->msg = Z_NULL; /* in case we return an error */
157 if (strm->zalloc == (alloc_func)0) { 182 if (strm->zalloc == (alloc_func)0) {
158 strm->zalloc = zcalloc; 183 strm->zalloc = zcalloc;
159 strm->opaque = (voidpf)0; 184 strm->opaque = (voidpf)0;
160 } 185 }
161 if (strm->zfree == (free_func)0) strm->zfree = zcfree; 186 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
162 state = (struct inflate_state FAR *) 187 state = (struct inflate_state FAR *)
163 ZALLOC(strm, 1, sizeof(struct inflate_state)); 188 ZALLOC(strm, 1, sizeof(struct inflate_state));
164 if (state == Z_NULL) return Z_MEM_ERROR; 189 if (state == Z_NULL) return Z_MEM_ERROR;
165 Tracev((stderr, "inflate: allocated\n")); 190 Tracev((stderr, "inflate: allocated\n"));
166 strm->state = (struct internal_state FAR *)state; 191 strm->state = (struct internal_state FAR *)state;
167 if (windowBits < 0) { 192 state->window = Z_NULL;
168 state->wrap = 0; 193 ret = inflateReset2(strm, windowBits);
169 windowBits = -windowBits; 194 if (ret != Z_OK) {
170 }
171 else {
172 state->wrap = (windowBits >> 4) + 1;
173 #ifdef GUNZIP
174 if (windowBits < 48) windowBits &= 15;
175 #endif
176 }
177 if (windowBits < 8 || windowBits > 15) {
178 ZFREE(strm, state); 195 ZFREE(strm, state);
179 strm->state = Z_NULL; 196 strm->state = Z_NULL;
180 return Z_STREAM_ERROR;
181 } 197 }
182 state->wbits = (unsigned)windowBits; 198 return ret;
183 state->window = Z_NULL;
184 return inflateReset(strm);
185 } 199 }
186 200
187 int ZEXPORT inflateInit_(strm, version, stream_size) 201 int ZEXPORT inflateInit_(strm, version, stream_size)
188 z_streamp strm; 202 z_streamp strm;
189 const char *version; 203 const char *version;
190 int stream_size; 204 int stream_size;
191 { 205 {
192 return inflateInit2_(strm, DEF_WBITS, version, stream_size); 206 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
193 } 207 }
194 208
209 int ZEXPORT inflatePrime(strm, bits, value)
210 z_streamp strm;
211 int bits;
212 int value;
213 {
214 struct inflate_state FAR *state;
215
216 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
217 state = (struct inflate_state FAR *)strm->state;
218 if (bits < 0) {
219 state->hold = 0;
220 state->bits = 0;
221 return Z_OK;
222 }
223 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
224 value &= (1L << bits) - 1;
225 state->hold += value << state->bits;
226 state->bits += bits;
227 return Z_OK;
228 }
229
195 /* 230 /*
196 Return state with length and distance decoding tables and index sizes set to 231 Return state with length and distance decoding tables and index sizes set to
197 fixed code decoding. Normally this returns fixed tables from inffixed.h. 232 fixed code decoding. Normally this returns fixed tables from inffixed.h.
198 If BUILDFIXED is defined, then instead this routine builds the tables the 233 If BUILDFIXED is defined, then instead this routine builds the tables the
199 first time it's called, and returns those tables the first time and 234 first time it's called, and returns those tables the first time and
200 thereafter. This reduces the size of the code by about 2K bytes, in 235 thereafter. This reduces the size of the code by about 2K bytes, in
201 exchange for a little execution time. However, BUILDFIXED should not be 236 exchange for a little execution time. However, BUILDFIXED should not be
202 used for threaded applications, since the rewriting of the tables and virgin 237 used for threaded applications, since the rewriting of the tables and virgin
203 may not be thread-safe. 238 may not be thread-safe.
204 */ 239 */
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 if (state->window == Z_NULL) { 368 if (state->window == Z_NULL) {
334 state->window = (unsigned char FAR *) 369 state->window = (unsigned char FAR *)
335 ZALLOC(strm, 1U << state->wbits, 370 ZALLOC(strm, 1U << state->wbits,
336 sizeof(unsigned char)); 371 sizeof(unsigned char));
337 if (state->window == Z_NULL) return 1; 372 if (state->window == Z_NULL) return 1;
338 } 373 }
339 374
340 /* if window not in use yet, initialize */ 375 /* if window not in use yet, initialize */
341 if (state->wsize == 0) { 376 if (state->wsize == 0) {
342 state->wsize = 1U << state->wbits; 377 state->wsize = 1U << state->wbits;
343 state->write = 0; 378 state->wnext = 0;
344 state->whave = 0; 379 state->whave = 0;
345 } 380 }
346 381
347 /* copy state->wsize or less output bytes into the circular window */ 382 /* copy state->wsize or less output bytes into the circular window */
348 copy = out - strm->avail_out; 383 copy = out - strm->avail_out;
349 if (copy >= state->wsize) { 384 if (copy >= state->wsize) {
350 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); 385 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
351 state->write = 0; 386 state->wnext = 0;
352 state->whave = state->wsize; 387 state->whave = state->wsize;
353 } 388 }
354 else { 389 else {
355 dist = state->wsize - state->write; 390 dist = state->wsize - state->wnext;
356 if (dist > copy) dist = copy; 391 if (dist > copy) dist = copy;
357 zmemcpy(state->window + state->write, strm->next_out - copy, dist); 392 zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
358 copy -= dist; 393 copy -= dist;
359 if (copy) { 394 if (copy) {
360 zmemcpy(state->window, strm->next_out - copy, copy); 395 zmemcpy(state->window, strm->next_out - copy, copy);
361 state->write = copy; 396 state->wnext = copy;
362 state->whave = state->wsize; 397 state->whave = state->wsize;
363 } 398 }
364 else { 399 else {
365 state->write += dist; 400 state->wnext += dist;
366 if (state->write == state->wsize) state->write = 0; 401 if (state->wnext == state->wsize) state->wnext = 0;
367 if (state->whave < state->wsize) state->whave += dist; 402 if (state->whave < state->wsize) state->whave += dist;
368 } 403 }
369 } 404 }
370 return 0; 405 return 0;
371 } 406 }
372 407
373 /* Macros for inflate(): */ 408 /* Macros for inflate(): */
374 409
375 /* check function to use adler32() for zlib or crc32() for gzip */ 410 /* check function to use adler32() for zlib or crc32() for gzip */
376 #ifdef GUNZIP 411 #ifdef GUNZIP
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 { 592 {
558 struct inflate_state FAR *state; 593 struct inflate_state FAR *state;
559 unsigned char FAR *next; /* next input */ 594 unsigned char FAR *next; /* next input */
560 unsigned char FAR *put; /* next output */ 595 unsigned char FAR *put; /* next output */
561 unsigned have, left; /* available input and output */ 596 unsigned have, left; /* available input and output */
562 unsigned long hold; /* bit buffer */ 597 unsigned long hold; /* bit buffer */
563 unsigned bits; /* bits in bit buffer */ 598 unsigned bits; /* bits in bit buffer */
564 unsigned in, out; /* save starting available input and output */ 599 unsigned in, out; /* save starting available input and output */
565 unsigned copy; /* number of stored or match bytes to copy */ 600 unsigned copy; /* number of stored or match bytes to copy */
566 unsigned char FAR *from; /* where to copy match bytes from */ 601 unsigned char FAR *from; /* where to copy match bytes from */
567 code this; /* current decoding table entry */ 602 code here; /* current decoding table entry */
568 code last; /* parent table entry */ 603 code last; /* parent table entry */
569 unsigned len; /* length to copy for repeats, bits to drop */ 604 unsigned len; /* length to copy for repeats, bits to drop */
570 int ret; /* return code */ 605 int ret; /* return code */
571 #ifdef GUNZIP 606 #ifdef GUNZIP
572 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ 607 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
573 #endif 608 #endif
574 static const unsigned short order[19] = /* permutation of code lengths */ 609 static const unsigned short order[19] = /* permutation of code lengths */
575 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 610 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
576 611
577 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || 612 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 state->mode = BAD; 647 state->mode = BAD;
613 break; 648 break;
614 } 649 }
615 if (BITS(4) != Z_DEFLATED) { 650 if (BITS(4) != Z_DEFLATED) {
616 strm->msg = (char *)"unknown compression method"; 651 strm->msg = (char *)"unknown compression method";
617 state->mode = BAD; 652 state->mode = BAD;
618 break; 653 break;
619 } 654 }
620 DROPBITS(4); 655 DROPBITS(4);
621 len = BITS(4) + 8; 656 len = BITS(4) + 8;
622 if (len > state->wbits) { 657 if (state->wbits == 0)
658 state->wbits = len;
659 else if (len > state->wbits) {
623 strm->msg = (char *)"invalid window size"; 660 strm->msg = (char *)"invalid window size";
624 state->mode = BAD; 661 state->mode = BAD;
625 break; 662 break;
626 } 663 }
627 state->dmax = 1U << len; 664 state->dmax = 1U << len;
628 Tracev((stderr, "inflate: zlib header ok\n")); 665 Tracev((stderr, "inflate: zlib header ok\n"));
629 strm->adler = state->check = adler32(0L, Z_NULL, 0); 666 strm->adler = state->check = adler32(0L, Z_NULL, 0);
630 state->mode = hold & 0x200 ? DICTID : TYPE; 667 state->mode = hold & 0x200 ? DICTID : TYPE;
631 INITBITS(); 668 INITBITS();
632 break; 669 break;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 INITBITS(); 801 INITBITS();
765 state->mode = DICT; 802 state->mode = DICT;
766 case DICT: 803 case DICT:
767 if (state->havedict == 0) { 804 if (state->havedict == 0) {
768 RESTORE(); 805 RESTORE();
769 return Z_NEED_DICT; 806 return Z_NEED_DICT;
770 } 807 }
771 strm->adler = state->check = adler32(0L, Z_NULL, 0); 808 strm->adler = state->check = adler32(0L, Z_NULL, 0);
772 state->mode = TYPE; 809 state->mode = TYPE;
773 case TYPE: 810 case TYPE:
774 if (flush == Z_BLOCK) goto inf_leave; 811 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
775 case TYPEDO: 812 case TYPEDO:
776 if (state->last) { 813 if (state->last) {
777 BYTEBITS(); 814 BYTEBITS();
778 state->mode = CHECK; 815 state->mode = CHECK;
779 break; 816 break;
780 } 817 }
781 NEEDBITS(3); 818 NEEDBITS(3);
782 state->last = BITS(1); 819 state->last = BITS(1);
783 DROPBITS(1); 820 DROPBITS(1);
784 switch (BITS(2)) { 821 switch (BITS(2)) {
785 case 0: /* stored block */ 822 case 0: /* stored block */
786 Tracev((stderr, "inflate: stored block%s\n", 823 Tracev((stderr, "inflate: stored block%s\n",
787 state->last ? " (last)" : "")); 824 state->last ? " (last)" : ""));
788 state->mode = STORED; 825 state->mode = STORED;
789 break; 826 break;
790 case 1: /* fixed block */ 827 case 1: /* fixed block */
791 fixedtables(state); 828 fixedtables(state);
792 Tracev((stderr, "inflate: fixed codes block%s\n", 829 Tracev((stderr, "inflate: fixed codes block%s\n",
793 state->last ? " (last)" : "")); 830 state->last ? " (last)" : ""));
794 state->mode = LEN; /* decode codes */ 831 state->mode = LEN_; /* decode codes */
832 if (flush == Z_TREES) {
833 DROPBITS(2);
834 goto inf_leave;
835 }
795 break; 836 break;
796 case 2: /* dynamic block */ 837 case 2: /* dynamic block */
797 Tracev((stderr, "inflate: dynamic codes block%s\n", 838 Tracev((stderr, "inflate: dynamic codes block%s\n",
798 state->last ? " (last)" : "")); 839 state->last ? " (last)" : ""));
799 state->mode = TABLE; 840 state->mode = TABLE;
800 break; 841 break;
801 case 3: 842 case 3:
802 strm->msg = (char *)"invalid block type"; 843 strm->msg = (char *)"invalid block type";
803 state->mode = BAD; 844 state->mode = BAD;
804 } 845 }
805 DROPBITS(2); 846 DROPBITS(2);
806 break; 847 break;
807 case STORED: 848 case STORED:
808 BYTEBITS(); /* go to byte boundary */ 849 BYTEBITS(); /* go to byte boundary */
809 NEEDBITS(32); 850 NEEDBITS(32);
810 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { 851 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
811 strm->msg = (char *)"invalid stored block lengths"; 852 strm->msg = (char *)"invalid stored block lengths";
812 state->mode = BAD; 853 state->mode = BAD;
813 break; 854 break;
814 } 855 }
815 state->length = (unsigned)hold & 0xffff; 856 state->length = (unsigned)hold & 0xffff;
816 Tracev((stderr, "inflate: stored length %u\n", 857 Tracev((stderr, "inflate: stored length %u\n",
817 state->length)); 858 state->length));
818 INITBITS(); 859 INITBITS();
860 state->mode = COPY_;
861 if (flush == Z_TREES) goto inf_leave;
862 case COPY_:
819 state->mode = COPY; 863 state->mode = COPY;
820 case COPY: 864 case COPY:
821 copy = state->length; 865 copy = state->length;
822 if (copy) { 866 if (copy) {
823 if (copy > have) copy = have; 867 if (copy > have) copy = have;
824 if (copy > left) copy = left; 868 if (copy > left) copy = left;
825 if (copy == 0) goto inf_leave; 869 if (copy == 0) goto inf_leave;
826 zmemcpy(put, next, copy); 870 zmemcpy(put, next, copy);
827 have -= copy; 871 have -= copy;
828 next += copy; 872 next += copy;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 strm->msg = (char *)"invalid code lengths set"; 913 strm->msg = (char *)"invalid code lengths set";
870 state->mode = BAD; 914 state->mode = BAD;
871 break; 915 break;
872 } 916 }
873 Tracev((stderr, "inflate: code lengths ok\n")); 917 Tracev((stderr, "inflate: code lengths ok\n"));
874 state->have = 0; 918 state->have = 0;
875 state->mode = CODELENS; 919 state->mode = CODELENS;
876 case CODELENS: 920 case CODELENS:
877 while (state->have < state->nlen + state->ndist) { 921 while (state->have < state->nlen + state->ndist) {
878 for (;;) { 922 for (;;) {
879 this = state->lencode[BITS(state->lenbits)]; 923 here = state->lencode[BITS(state->lenbits)];
880 if ((unsigned)(this.bits) <= bits) break; 924 if ((unsigned)(here.bits) <= bits) break;
881 PULLBYTE(); 925 PULLBYTE();
882 } 926 }
883 if (this.val < 16) { 927 if (here.val < 16) {
884 NEEDBITS(this.bits); 928 NEEDBITS(here.bits);
885 DROPBITS(this.bits); 929 DROPBITS(here.bits);
886 state->lens[state->have++] = this.val; 930 state->lens[state->have++] = here.val;
887 } 931 }
888 else { 932 else {
889 if (this.val == 16) { 933 if (here.val == 16) {
890 NEEDBITS(this.bits + 2); 934 NEEDBITS(here.bits + 2);
891 DROPBITS(this.bits); 935 DROPBITS(here.bits);
892 if (state->have == 0) { 936 if (state->have == 0) {
893 strm->msg = (char *)"invalid bit length repeat"; 937 strm->msg = (char *)"invalid bit length repeat";
894 state->mode = BAD; 938 state->mode = BAD;
895 break; 939 break;
896 } 940 }
897 len = state->lens[state->have - 1]; 941 len = state->lens[state->have - 1];
898 copy = 3 + BITS(2); 942 copy = 3 + BITS(2);
899 DROPBITS(2); 943 DROPBITS(2);
900 } 944 }
901 else if (this.val == 17) { 945 else if (here.val == 17) {
902 NEEDBITS(this.bits + 3); 946 NEEDBITS(here.bits + 3);
903 DROPBITS(this.bits); 947 DROPBITS(here.bits);
904 len = 0; 948 len = 0;
905 copy = 3 + BITS(3); 949 copy = 3 + BITS(3);
906 DROPBITS(3); 950 DROPBITS(3);
907 } 951 }
908 else { 952 else {
909 NEEDBITS(this.bits + 7); 953 NEEDBITS(here.bits + 7);
910 DROPBITS(this.bits); 954 DROPBITS(here.bits);
911 len = 0; 955 len = 0;
912 copy = 11 + BITS(7); 956 copy = 11 + BITS(7);
913 DROPBITS(7); 957 DROPBITS(7);
914 } 958 }
915 if (state->have + copy > state->nlen + state->ndist) { 959 if (state->have + copy > state->nlen + state->ndist) {
916 strm->msg = (char *)"invalid bit length repeat"; 960 strm->msg = (char *)"invalid bit length repeat";
917 state->mode = BAD; 961 state->mode = BAD;
918 break; 962 break;
919 } 963 }
920 while (copy--) 964 while (copy--)
921 state->lens[state->have++] = (unsigned short)len; 965 state->lens[state->have++] = (unsigned short)len;
922 } 966 }
923 } 967 }
924 968
925 /* handle error breaks in while */ 969 /* handle error breaks in while */
926 if (state->mode == BAD) break; 970 if (state->mode == BAD) break;
927 971
928 /* build code tables */ 972 /* check for end-of-block code (better have one) */
973 if (state->lens[256] == 0) {
974 strm->msg = (char *)"invalid code -- missing end-of-block";
975 state->mode = BAD;
976 break;
977 }
978
979 /* build code tables -- note: do not change the lenbits or distbits
980 values here (9 and 6) without reading the comments in inftrees.h
981 concerning the ENOUGH constants, which depend on those values */
929 state->next = state->codes; 982 state->next = state->codes;
930 state->lencode = (code const FAR *)(state->next); 983 state->lencode = (code const FAR *)(state->next);
931 state->lenbits = 9; 984 state->lenbits = 9;
932 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), 985 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
933 &(state->lenbits), state->work); 986 &(state->lenbits), state->work);
934 if (ret) { 987 if (ret) {
935 strm->msg = (char *)"invalid literal/lengths set"; 988 strm->msg = (char *)"invalid literal/lengths set";
936 state->mode = BAD; 989 state->mode = BAD;
937 break; 990 break;
938 } 991 }
939 state->distcode = (code const FAR *)(state->next); 992 state->distcode = (code const FAR *)(state->next);
940 state->distbits = 6; 993 state->distbits = 6;
941 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, 994 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
942 &(state->next), &(state->distbits), state->work); 995 &(state->next), &(state->distbits), state->work);
943 if (ret) { 996 if (ret) {
944 strm->msg = (char *)"invalid distances set"; 997 strm->msg = (char *)"invalid distances set";
945 state->mode = BAD; 998 state->mode = BAD;
946 break; 999 break;
947 } 1000 }
948 Tracev((stderr, "inflate: codes ok\n")); 1001 Tracev((stderr, "inflate: codes ok\n"));
1002 state->mode = LEN_;
1003 if (flush == Z_TREES) goto inf_leave;
1004 case LEN_:
949 state->mode = LEN; 1005 state->mode = LEN;
950 case LEN: 1006 case LEN:
951 if (have >= 6 && left >= 258) { 1007 if (have >= 6 && left >= 258) {
952 RESTORE(); 1008 RESTORE();
953 inflate_fast(strm, out); 1009 inflate_fast(strm, out);
954 LOAD(); 1010 LOAD();
1011 if (state->mode == TYPE)
1012 state->back = -1;
955 break; 1013 break;
956 } 1014 }
1015 state->back = 0;
957 for (;;) { 1016 for (;;) {
958 this = state->lencode[BITS(state->lenbits)]; 1017 here = state->lencode[BITS(state->lenbits)];
959 if ((unsigned)(this.bits) <= bits) break; 1018 if ((unsigned)(here.bits) <= bits) break;
960 PULLBYTE(); 1019 PULLBYTE();
961 } 1020 }
962 if (this.op && (this.op & 0xf0) == 0) { 1021 if (here.op && (here.op & 0xf0) == 0) {
963 last = this; 1022 last = here;
964 for (;;) { 1023 for (;;) {
965 this = state->lencode[last.val + 1024 here = state->lencode[last.val +
966 (BITS(last.bits + last.op) >> last.bits)]; 1025 (BITS(last.bits + last.op) >> last.bits)];
967 if ((unsigned)(last.bits + this.bits) <= bits) break; 1026 if ((unsigned)(last.bits + here.bits) <= bits) break;
968 PULLBYTE(); 1027 PULLBYTE();
969 } 1028 }
970 DROPBITS(last.bits); 1029 DROPBITS(last.bits);
1030 state->back += last.bits;
971 } 1031 }
972 DROPBITS(this.bits); 1032 DROPBITS(here.bits);
973 state->length = (unsigned)this.val; 1033 state->back += here.bits;
974 if ((int)(this.op) == 0) { 1034 state->length = (unsigned)here.val;
975 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? 1035 if ((int)(here.op) == 0) {
1036 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
976 "inflate: literal '%c'\n" : 1037 "inflate: literal '%c'\n" :
977 "inflate: literal 0x%02x\n", this.val)); 1038 "inflate: literal 0x%02x\n", here.val));
978 state->mode = LIT; 1039 state->mode = LIT;
979 break; 1040 break;
980 } 1041 }
981 if (this.op & 32) { 1042 if (here.op & 32) {
982 Tracevv((stderr, "inflate: end of block\n")); 1043 Tracevv((stderr, "inflate: end of block\n"));
1044 state->back = -1;
983 state->mode = TYPE; 1045 state->mode = TYPE;
984 break; 1046 break;
985 } 1047 }
986 if (this.op & 64) { 1048 if (here.op & 64) {
987 strm->msg = (char *)"invalid literal/length code"; 1049 strm->msg = (char *)"invalid literal/length code";
988 state->mode = BAD; 1050 state->mode = BAD;
989 break; 1051 break;
990 } 1052 }
991 state->extra = (unsigned)(this.op) & 15; 1053 state->extra = (unsigned)(here.op) & 15;
992 state->mode = LENEXT; 1054 state->mode = LENEXT;
993 case LENEXT: 1055 case LENEXT:
994 if (state->extra) { 1056 if (state->extra) {
995 NEEDBITS(state->extra); 1057 NEEDBITS(state->extra);
996 state->length += BITS(state->extra); 1058 state->length += BITS(state->extra);
997 DROPBITS(state->extra); 1059 DROPBITS(state->extra);
1060 state->back += state->extra;
998 } 1061 }
999 Tracevv((stderr, "inflate: length %u\n", state->length)); 1062 Tracevv((stderr, "inflate: length %u\n", state->length));
1063 state->was = state->length;
1000 state->mode = DIST; 1064 state->mode = DIST;
1001 case DIST: 1065 case DIST:
1002 for (;;) { 1066 for (;;) {
1003 this = state->distcode[BITS(state->distbits)]; 1067 here = state->distcode[BITS(state->distbits)];
1004 if ((unsigned)(this.bits) <= bits) break; 1068 if ((unsigned)(here.bits) <= bits) break;
1005 PULLBYTE(); 1069 PULLBYTE();
1006 } 1070 }
1007 if ((this.op & 0xf0) == 0) { 1071 if ((here.op & 0xf0) == 0) {
1008 last = this; 1072 last = here;
1009 for (;;) { 1073 for (;;) {
1010 this = state->distcode[last.val + 1074 here = state->distcode[last.val +
1011 (BITS(last.bits + last.op) >> last.bits)]; 1075 (BITS(last.bits + last.op) >> last.bits)];
1012 if ((unsigned)(last.bits + this.bits) <= bits) break; 1076 if ((unsigned)(last.bits + here.bits) <= bits) break;
1013 PULLBYTE(); 1077 PULLBYTE();
1014 } 1078 }
1015 DROPBITS(last.bits); 1079 DROPBITS(last.bits);
1080 state->back += last.bits;
1016 } 1081 }
1017 DROPBITS(this.bits); 1082 DROPBITS(here.bits);
1018 if (this.op & 64) { 1083 state->back += here.bits;
1084 if (here.op & 64) {
1019 strm->msg = (char *)"invalid distance code"; 1085 strm->msg = (char *)"invalid distance code";
1020 state->mode = BAD; 1086 state->mode = BAD;
1021 break; 1087 break;
1022 } 1088 }
1023 state->offset = (unsigned)this.val; 1089 state->offset = (unsigned)here.val;
1024 state->extra = (unsigned)(this.op) & 15; 1090 state->extra = (unsigned)(here.op) & 15;
1025 state->mode = DISTEXT; 1091 state->mode = DISTEXT;
1026 case DISTEXT: 1092 case DISTEXT:
1027 if (state->extra) { 1093 if (state->extra) {
1028 NEEDBITS(state->extra); 1094 NEEDBITS(state->extra);
1029 state->offset += BITS(state->extra); 1095 state->offset += BITS(state->extra);
1030 DROPBITS(state->extra); 1096 DROPBITS(state->extra);
1097 state->back += state->extra;
1031 } 1098 }
1032 #ifdef INFLATE_STRICT 1099 #ifdef INFLATE_STRICT
1033 if (state->offset > state->dmax) { 1100 if (state->offset > state->dmax) {
1034 strm->msg = (char *)"invalid distance too far back"; 1101 strm->msg = (char *)"invalid distance too far back";
1035 state->mode = BAD; 1102 state->mode = BAD;
1036 break; 1103 break;
1037 } 1104 }
1038 #endif 1105 #endif
1039 if (state->offset > state->whave + out - left) {
1040 strm->msg = (char *)"invalid distance too far back";
1041 state->mode = BAD;
1042 break;
1043 }
1044 Tracevv((stderr, "inflate: distance %u\n", state->offset)); 1106 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1045 state->mode = MATCH; 1107 state->mode = MATCH;
1046 case MATCH: 1108 case MATCH:
1047 if (left == 0) goto inf_leave; 1109 if (left == 0) goto inf_leave;
1048 copy = out - left; 1110 copy = out - left;
1049 if (state->offset > copy) { /* copy from window */ 1111 if (state->offset > copy) { /* copy from window */
1050 copy = state->offset - copy; 1112 copy = state->offset - copy;
1051 if (copy > state->write) { 1113 if (copy > state->whave) {
1052 copy -= state->write; 1114 if (state->sane) {
1115 strm->msg = (char *)"invalid distance too far back";
1116 state->mode = BAD;
1117 break;
1118 }
1119 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1120 Trace((stderr, "inflate.c too far\n"));
1121 copy -= state->whave;
1122 if (copy > state->length) copy = state->length;
1123 if (copy > left) copy = left;
1124 left -= copy;
1125 state->length -= copy;
1126 do {
1127 *put++ = 0;
1128 } while (--copy);
1129 if (state->length == 0) state->mode = LEN;
1130 break;
1131 #endif
1132 }
1133 if (copy > state->wnext) {
1134 copy -= state->wnext;
1053 from = state->window + (state->wsize - copy); 1135 from = state->window + (state->wsize - copy);
1054 } 1136 }
1055 else 1137 else
1056 from = state->window + (state->write - copy); 1138 from = state->window + (state->wnext - copy);
1057 if (copy > state->length) copy = state->length; 1139 if (copy > state->length) copy = state->length;
1058 } 1140 }
1059 else { /* copy from output */ 1141 else { /* copy from output */
1060 from = put - state->offset; 1142 from = put - state->offset;
1061 copy = state->length; 1143 copy = state->length;
1062 } 1144 }
1063 if (copy > left) copy = left; 1145 if (copy > left) copy = left;
1064 left -= copy; 1146 left -= copy;
1065 state->length -= copy; 1147 state->length -= copy;
1066 do { 1148 do {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1139 } 1221 }
1140 in -= strm->avail_in; 1222 in -= strm->avail_in;
1141 out -= strm->avail_out; 1223 out -= strm->avail_out;
1142 strm->total_in += in; 1224 strm->total_in += in;
1143 strm->total_out += out; 1225 strm->total_out += out;
1144 state->total += out; 1226 state->total += out;
1145 if (state->wrap && out) 1227 if (state->wrap && out)
1146 strm->adler = state->check = 1228 strm->adler = state->check =
1147 UPDATE(state->check, strm->next_out - out, out); 1229 UPDATE(state->check, strm->next_out - out, out);
1148 strm->data_type = state->bits + (state->last ? 64 : 0) + 1230 strm->data_type = state->bits + (state->last ? 64 : 0) +
1149 (state->mode == TYPE ? 128 : 0); 1231 (state->mode == TYPE ? 128 : 0) +
1232 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1150 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) 1233 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1151 ret = Z_BUF_ERROR; 1234 ret = Z_BUF_ERROR;
1152 return ret; 1235 return ret;
1153 } 1236 }
1154 1237
1155 int ZEXPORT inflateEnd(strm) 1238 int ZEXPORT inflateEnd(strm)
1156 z_streamp strm; 1239 z_streamp strm;
1157 { 1240 {
1158 struct inflate_state FAR *state; 1241 struct inflate_state FAR *state;
1159 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)
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
1359 } 1442 }
1360 copy->next = copy->codes + (state->next - state->codes); 1443 copy->next = copy->codes + (state->next - state->codes);
1361 if (window != Z_NULL) { 1444 if (window != Z_NULL) {
1362 wsize = 1U << state->wbits; 1445 wsize = 1U << state->wbits;
1363 zmemcpy(window, state->window, wsize); 1446 zmemcpy(window, state->window, wsize);
1364 } 1447 }
1365 copy->window = window; 1448 copy->window = window;
1366 dest->state = (struct internal_state FAR *)copy; 1449 dest->state = (struct internal_state FAR *)copy;
1367 return Z_OK; 1450 return Z_OK;
1368 } 1451 }
1452
1453 int ZEXPORT inflateUndermine(strm, subvert)
1454 z_streamp strm;
1455 int subvert;
1456 {
1457 struct inflate_state FAR *state;
1458
1459 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1460 state = (struct inflate_state FAR *)strm->state;
1461 state->sane = !subvert;
1462 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1463 return Z_OK;
1464 #else
1465 state->sane = 1;
1466 return Z_DATA_ERROR;
1467 #endif
1468 }
1469
1470 long ZEXPORT inflateMark(strm)
1471 z_streamp strm;
1472 {
1473 struct inflate_state FAR *state;
1474
1475 if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
1476 state = (struct inflate_state FAR *)strm->state;
1477 return ((long)(state->back) << 16) +
1478 (state->mode == COPY ? state->length :
1479 (state->mode == MATCH ? state->was - state->length : 0));
1480 }
OLDNEW
« no previous file with comments | « third_party/zlib/inflate.h ('k') | third_party/zlib/inftrees.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698