00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #include "zutil.h"
00084 #include "inftrees.h"
00085 #include "inflate.h"
00086 #include "inffast.h"
00087
00088 #ifdef MAKEFIXED
00089 # ifndef BUILDFIXED
00090 # define BUILDFIXED
00091 # endif
00092 #endif
00093
00094
00095 local void fixedtables OF((struct inflate_state FAR *state));
00096 local int updatewindow OF((z_streamp strm, unsigned out));
00097 #ifdef BUILDFIXED
00098 void makefixed OF((void));
00099 #endif
00100 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
00101 unsigned len));
00102
00103 int ZEXPORT inflateReset(strm)
00104 z_streamp strm;
00105 {
00106 struct inflate_state FAR *state;
00107
00108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
00109 state = (struct inflate_state FAR *)strm->state;
00110 strm->total_in = strm->total_out = state->total = 0;
00111 strm->msg = Z_NULL;
00112 strm->adler = 1;
00113 state->mode = HEAD;
00114 state->last = 0;
00115 state->havedict = 0;
00116 state->dmax = 32768U;
00117 state->head = Z_NULL;
00118 state->wsize = 0;
00119 state->whave = 0;
00120 state->write = 0;
00121 state->hold = 0;
00122 state->bits = 0;
00123 state->lencode = state->distcode = state->next = state->codes;
00124 Tracev((stderr, "inflate: reset\n"));
00125 return Z_OK;
00126 }
00127
00128 int ZEXPORT inflatePrime(strm, bits, value)
00129 z_streamp strm;
00130 int bits;
00131 int value;
00132 {
00133 struct inflate_state FAR *state;
00134
00135 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
00136 state = (struct inflate_state FAR *)strm->state;
00137 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
00138 value &= (1L << bits) - 1;
00139 state->hold += value << state->bits;
00140 state->bits += bits;
00141 return Z_OK;
00142 }
00143
00144 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
00145 z_streamp strm;
00146 int windowBits;
00147 const char *version;
00148 int stream_size;
00149 {
00150 struct inflate_state FAR *state;
00151
00152 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
00153 stream_size != (int)(sizeof(z_stream)))
00154 return Z_VERSION_ERROR;
00155 if (strm == Z_NULL) return Z_STREAM_ERROR;
00156 strm->msg = Z_NULL;
00157 if (strm->zalloc == (alloc_func)0) {
00158 strm->zalloc = zcalloc;
00159 strm->opaque = (voidpf)0;
00160 }
00161 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
00162 state = (struct inflate_state FAR *)
00163 ZALLOC(strm, 1, sizeof(struct inflate_state));
00164 if (state == Z_NULL) return Z_MEM_ERROR;
00165 Tracev((stderr, "inflate: allocated\n"));
00166 strm->state = (struct internal_state FAR *)state;
00167 if (windowBits < 0) {
00168 state->wrap = 0;
00169 windowBits = -windowBits;
00170 }
00171 else {
00172 state->wrap = (windowBits >> 4) + 1;
00173 #ifdef GUNZIP
00174 if (windowBits < 48) windowBits &= 15;
00175 #endif
00176 }
00177 if (windowBits < 8 || windowBits > 15) {
00178 ZFREE(strm, state);
00179 strm->state = Z_NULL;
00180 return Z_STREAM_ERROR;
00181 }
00182 state->wbits = (unsigned)windowBits;
00183 state->window = Z_NULL;
00184 return inflateReset(strm);
00185 }
00186
00187 int ZEXPORT inflateInit_(strm, version, stream_size)
00188 z_streamp strm;
00189 const char *version;
00190 int stream_size;
00191 {
00192 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 local void fixedtables(state)
00206 struct inflate_state FAR *state;
00207 {
00208 #ifdef BUILDFIXED
00209 static int virgin = 1;
00210 static code *lenfix, *distfix;
00211 static code fixed[544];
00212
00213
00214 if (virgin) {
00215 unsigned sym, bits;
00216 static code *next;
00217
00218
00219 sym = 0;
00220 while (sym < 144) state->lens[sym++] = 8;
00221 while (sym < 256) state->lens[sym++] = 9;
00222 while (sym < 280) state->lens[sym++] = 7;
00223 while (sym < 288) state->lens[sym++] = 8;
00224 next = fixed;
00225 lenfix = next;
00226 bits = 9;
00227 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
00228
00229
00230 sym = 0;
00231 while (sym < 32) state->lens[sym++] = 5;
00232 distfix = next;
00233 bits = 5;
00234 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
00235
00236
00237 virgin = 0;
00238 }
00239 #else
00240 # include "inffixed.h"
00241 #endif
00242 state->lencode = lenfix;
00243 state->lenbits = 9;
00244 state->distcode = distfix;
00245 state->distbits = 5;
00246 }
00247
00248 #ifdef MAKEFIXED
00249 #include <stdio.h>
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 void makefixed()
00270 {
00271 unsigned low, size;
00272 struct inflate_state state;
00273
00274 fixedtables(&state);
00275 puts(" /* inffixed.h -- table for decoding fixed codes");
00276 puts(" * Generated automatically by makefixed().");
00277 puts(" */");
00278 puts("");
00279 puts(" /* WARNING: this file should *not* be used by applications.");
00280 puts(" It is part of the implementation of this library and is");
00281 puts(" subject to change. Applications should only use zlib.h.");
00282 puts(" */");
00283 puts("");
00284 size = 1U << 9;
00285 printf(" static const code lenfix[%u] = {", size);
00286 low = 0;
00287 for (;;) {
00288 if ((low % 7) == 0) printf("\n ");
00289 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
00290 state.lencode[low].val);
00291 if (++low == size) break;
00292 putchar(',');
00293 }
00294 puts("\n };");
00295 size = 1U << 5;
00296 printf("\n static const code distfix[%u] = {", size);
00297 low = 0;
00298 for (;;) {
00299 if ((low % 6) == 0) printf("\n ");
00300 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
00301 state.distcode[low].val);
00302 if (++low == size) break;
00303 putchar(',');
00304 }
00305 puts("\n };");
00306 }
00307 #endif
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 local int updatewindow(strm, out)
00324 z_streamp strm;
00325 unsigned out;
00326 {
00327 struct inflate_state FAR *state;
00328 unsigned copy, dist;
00329
00330 state = (struct inflate_state FAR *)strm->state;
00331
00332
00333 if (state->window == Z_NULL) {
00334 state->window = (unsigned char FAR *)
00335 ZALLOC(strm, 1U << state->wbits,
00336 sizeof(unsigned char));
00337 if (state->window == Z_NULL) return 1;
00338 }
00339
00340
00341 if (state->wsize == 0) {
00342 state->wsize = 1U << state->wbits;
00343 state->write = 0;
00344 state->whave = 0;
00345 }
00346
00347
00348 copy = out - strm->avail_out;
00349 if (copy >= state->wsize) {
00350 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
00351 state->write = 0;
00352 state->whave = state->wsize;
00353 }
00354 else {
00355 dist = state->wsize - state->write;
00356 if (dist > copy) dist = copy;
00357 zmemcpy(state->window + state->write, strm->next_out - copy, dist);
00358 copy -= dist;
00359 if (copy) {
00360 zmemcpy(state->window, strm->next_out - copy, copy);
00361 state->write = copy;
00362 state->whave = state->wsize;
00363 }
00364 else {
00365 state->write += dist;
00366 if (state->write == state->wsize) state->write = 0;
00367 if (state->whave < state->wsize) state->whave += dist;
00368 }
00369 }
00370 return 0;
00371 }
00372
00373
00374
00375
00376 #ifdef GUNZIP
00377 # define UPDATE(check, buf, len) \
00378 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
00379 #else
00380 # define UPDATE(check, buf, len) adler32(check, buf, len)
00381 #endif
00382
00383
00384 #ifdef GUNZIP
00385 # define CRC2(check, word) \
00386 do { \
00387 hbuf[0] = (unsigned char)(word); \
00388 hbuf[1] = (unsigned char)((word) >> 8); \
00389 check = crc32(check, hbuf, 2); \
00390 } while (0)
00391
00392 # define CRC4(check, word) \
00393 do { \
00394 hbuf[0] = (unsigned char)(word); \
00395 hbuf[1] = (unsigned char)((word) >> 8); \
00396 hbuf[2] = (unsigned char)((word) >> 16); \
00397 hbuf[3] = (unsigned char)((word) >> 24); \
00398 check = crc32(check, hbuf, 4); \
00399 } while (0)
00400 #endif
00401
00402
00403 #define LOAD() \
00404 do { \
00405 put = strm->next_out; \
00406 left = strm->avail_out; \
00407 next = strm->next_in; \
00408 have = strm->avail_in; \
00409 hold = state->hold; \
00410 bits = state->bits; \
00411 } while (0)
00412
00413
00414 #define RESTORE() \
00415 do { \
00416 strm->next_out = put; \
00417 strm->avail_out = left; \
00418 strm->next_in = next; \
00419 strm->avail_in = have; \
00420 state->hold = hold; \
00421 state->bits = bits; \
00422 } while (0)
00423
00424
00425 #define INITBITS() \
00426 do { \
00427 hold = 0; \
00428 bits = 0; \
00429 } while (0)
00430
00431
00432
00433 #define PULLBYTE() \
00434 do { \
00435 if (have == 0) goto inf_leave; \
00436 have--; \
00437 hold += (unsigned long)(*next++) << bits; \
00438 bits += 8; \
00439 } while (0)
00440
00441
00442
00443 #define NEEDBITS(n) \
00444 do { \
00445 while (bits < (unsigned)(n)) \
00446 PULLBYTE(); \
00447 } while (0)
00448
00449
00450 #define BITS(n) \
00451 ((unsigned)hold & ((1U << (n)) - 1))
00452
00453
00454 #define DROPBITS(n) \
00455 do { \
00456 hold >>= (n); \
00457 bits -= (unsigned)(n); \
00458 } while (0)
00459
00460
00461 #define BYTEBITS() \
00462 do { \
00463 hold >>= bits & 7; \
00464 bits -= bits & 7; \
00465 } while (0)
00466
00467
00468 #define REVERSE(q) \
00469 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
00470 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 int ZEXPORT inflate(strm, flush)
00555 z_streamp strm;
00556 int flush;
00557 {
00558 struct inflate_state FAR *state;
00559 unsigned char FAR *next;
00560 unsigned char FAR *put;
00561 unsigned have, left;
00562 unsigned long hold;
00563 unsigned bits;
00564 unsigned in, out;
00565 unsigned copy;
00566 unsigned char FAR *from;
00567 code this;
00568 code last;
00569 unsigned len;
00570 int ret;
00571 #ifdef GUNZIP
00572 unsigned char hbuf[4];
00573 #endif
00574 static const unsigned short order[19] =
00575 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
00576
00577 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
00578 (strm->next_in == Z_NULL && strm->avail_in != 0))
00579 return Z_STREAM_ERROR;
00580
00581 state = (struct inflate_state FAR *)strm->state;
00582 if (state->mode == TYPE) state->mode = TYPEDO;
00583 LOAD();
00584 in = have;
00585 out = left;
00586 ret = Z_OK;
00587 for (;;)
00588 switch (state->mode) {
00589 case HEAD:
00590 if (state->wrap == 0) {
00591 state->mode = TYPEDO;
00592 break;
00593 }
00594 NEEDBITS(16);
00595 #ifdef GUNZIP
00596 if ((state->wrap & 2) && hold == 0x8b1f) {
00597 state->check = crc32(0L, Z_NULL, 0);
00598 CRC2(state->check, hold);
00599 INITBITS();
00600 state->mode = FLAGS;
00601 break;
00602 }
00603 state->flags = 0;
00604 if (state->head != Z_NULL)
00605 state->head->done = -1;
00606 if (!(state->wrap & 1) ||
00607 #else
00608 if (
00609 #endif
00610 ((BITS(8) << 8) + (hold >> 8)) % 31) {
00611 strm->msg = (char *)"incorrect header check";
00612 state->mode = BAD;
00613 break;
00614 }
00615 if (BITS(4) != Z_DEFLATED) {
00616 strm->msg = (char *)"unknown compression method";
00617 state->mode = BAD;
00618 break;
00619 }
00620 DROPBITS(4);
00621 len = BITS(4) + 8;
00622 if (len > state->wbits) {
00623 strm->msg = (char *)"invalid window size";
00624 state->mode = BAD;
00625 break;
00626 }
00627 state->dmax = 1U << len;
00628 Tracev((stderr, "inflate: zlib header ok\n"));
00629 strm->adler = state->check = adler32(0L, Z_NULL, 0);
00630 state->mode = hold & 0x200 ? DICTID : TYPE;
00631 INITBITS();
00632 break;
00633 #ifdef GUNZIP
00634 case FLAGS:
00635 NEEDBITS(16);
00636 state->flags = (int)(hold);
00637 if ((state->flags & 0xff) != Z_DEFLATED) {
00638 strm->msg = (char *)"unknown compression method";
00639 state->mode = BAD;
00640 break;
00641 }
00642 if (state->flags & 0xe000) {
00643 strm->msg = (char *)"unknown header flags set";
00644 state->mode = BAD;
00645 break;
00646 }
00647 if (state->head != Z_NULL)
00648 state->head->text = (int)((hold >> 8) & 1);
00649 if (state->flags & 0x0200) CRC2(state->check, hold);
00650 INITBITS();
00651 state->mode = TIME;
00652 case TIME:
00653 NEEDBITS(32);
00654 if (state->head != Z_NULL)
00655 state->head->time = hold;
00656 if (state->flags & 0x0200) CRC4(state->check, hold);
00657 INITBITS();
00658 state->mode = OS;
00659 case OS:
00660 NEEDBITS(16);
00661 if (state->head != Z_NULL) {
00662 state->head->xflags = (int)(hold & 0xff);
00663 state->head->os = (int)(hold >> 8);
00664 }
00665 if (state->flags & 0x0200) CRC2(state->check, hold);
00666 INITBITS();
00667 state->mode = EXLEN;
00668 case EXLEN:
00669 if (state->flags & 0x0400) {
00670 NEEDBITS(16);
00671 state->length = (unsigned)(hold);
00672 if (state->head != Z_NULL)
00673 state->head->extra_len = (unsigned)hold;
00674 if (state->flags & 0x0200) CRC2(state->check, hold);
00675 INITBITS();
00676 }
00677 else if (state->head != Z_NULL)
00678 state->head->extra = Z_NULL;
00679 state->mode = EXTRA;
00680 case EXTRA:
00681 if (state->flags & 0x0400) {
00682 copy = state->length;
00683 if (copy > have) copy = have;
00684 if (copy) {
00685 if (state->head != Z_NULL &&
00686 state->head->extra != Z_NULL) {
00687 len = state->head->extra_len - state->length;
00688 zmemcpy(state->head->extra + len, next,
00689 len + copy > state->head->extra_max ?
00690 state->head->extra_max - len : copy);
00691 }
00692 if (state->flags & 0x0200)
00693 state->check = crc32(state->check, next, copy);
00694 have -= copy;
00695 next += copy;
00696 state->length -= copy;
00697 }
00698 if (state->length) goto inf_leave;
00699 }
00700 state->length = 0;
00701 state->mode = NAME;
00702 case NAME:
00703 if (state->flags & 0x0800) {
00704 if (have == 0) goto inf_leave;
00705 copy = 0;
00706 do {
00707 len = (unsigned)(next[copy++]);
00708 if (state->head != Z_NULL &&
00709 state->head->name != Z_NULL &&
00710 state->length < state->head->name_max)
00711 state->head->name[state->length++] = len;
00712 } while (len && copy < have);
00713 if (state->flags & 0x0200)
00714 state->check = crc32(state->check, next, copy);
00715 have -= copy;
00716 next += copy;
00717 if (len) goto inf_leave;
00718 }
00719 else if (state->head != Z_NULL)
00720 state->head->name = Z_NULL;
00721 state->length = 0;
00722 state->mode = COMMENT;
00723 case COMMENT:
00724 if (state->flags & 0x1000) {
00725 if (have == 0) goto inf_leave;
00726 copy = 0;
00727 do {
00728 len = (unsigned)(next[copy++]);
00729 if (state->head != Z_NULL &&
00730 state->head->comment != Z_NULL &&
00731 state->length < state->head->comm_max)
00732 state->head->comment[state->length++] = len;
00733 } while (len && copy < have);
00734 if (state->flags & 0x0200)
00735 state->check = crc32(state->check, next, copy);
00736 have -= copy;
00737 next += copy;
00738 if (len) goto inf_leave;
00739 }
00740 else if (state->head != Z_NULL)
00741 state->head->comment = Z_NULL;
00742 state->mode = HCRC;
00743 case HCRC:
00744 if (state->flags & 0x0200) {
00745 NEEDBITS(16);
00746 if (hold != (state->check & 0xffff)) {
00747 strm->msg = (char *)"header crc mismatch";
00748 state->mode = BAD;
00749 break;
00750 }
00751 INITBITS();
00752 }
00753 if (state->head != Z_NULL) {
00754 state->head->hcrc = (int)((state->flags >> 9) & 1);
00755 state->head->done = 1;
00756 }
00757 strm->adler = state->check = crc32(0L, Z_NULL, 0);
00758 state->mode = TYPE;
00759 break;
00760 #endif
00761 case DICTID:
00762 NEEDBITS(32);
00763 strm->adler = state->check = REVERSE(hold);
00764 INITBITS();
00765 state->mode = DICT;
00766 case DICT:
00767 if (state->havedict == 0) {
00768 RESTORE();
00769 return Z_NEED_DICT;
00770 }
00771 strm->adler = state->check = adler32(0L, Z_NULL, 0);
00772 state->mode = TYPE;
00773 case TYPE:
00774 if (flush == Z_BLOCK) goto inf_leave;
00775 case TYPEDO:
00776 if (state->last) {
00777 BYTEBITS();
00778 state->mode = CHECK;
00779 break;
00780 }
00781 NEEDBITS(3);
00782 state->last = BITS(1);
00783 DROPBITS(1);
00784 switch (BITS(2)) {
00785 case 0:
00786 Tracev((stderr, "inflate: stored block%s\n",
00787 state->last ? " (last)" : ""));
00788 state->mode = STORED;
00789 break;
00790 case 1:
00791 fixedtables(state);
00792 Tracev((stderr, "inflate: fixed codes block%s\n",
00793 state->last ? " (last)" : ""));
00794 state->mode = LEN;
00795 break;
00796 case 2:
00797 Tracev((stderr, "inflate: dynamic codes block%s\n",
00798 state->last ? " (last)" : ""));
00799 state->mode = TABLE;
00800 break;
00801 case 3:
00802 strm->msg = (char *)"invalid block type";
00803 state->mode = BAD;
00804 }
00805 DROPBITS(2);
00806 break;
00807 case STORED:
00808 BYTEBITS();
00809 NEEDBITS(32);
00810 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
00811 strm->msg = (char *)"invalid stored block lengths";
00812 state->mode = BAD;
00813 break;
00814 }
00815 state->length = (unsigned)hold & 0xffff;
00816 Tracev((stderr, "inflate: stored length %u\n",
00817 state->length));
00818 INITBITS();
00819 state->mode = COPY;
00820 case COPY:
00821 copy = state->length;
00822 if (copy) {
00823 if (copy > have) copy = have;
00824 if (copy > left) copy = left;
00825 if (copy == 0) goto inf_leave;
00826 zmemcpy(put, next, copy);
00827 have -= copy;
00828 next += copy;
00829 left -= copy;
00830 put += copy;
00831 state->length -= copy;
00832 break;
00833 }
00834 Tracev((stderr, "inflate: stored end\n"));
00835 state->mode = TYPE;
00836 break;
00837 case TABLE:
00838 NEEDBITS(14);
00839 state->nlen = BITS(5) + 257;
00840 DROPBITS(5);
00841 state->ndist = BITS(5) + 1;
00842 DROPBITS(5);
00843 state->ncode = BITS(4) + 4;
00844 DROPBITS(4);
00845 #ifndef PKZIP_BUG_WORKAROUND
00846 if (state->nlen > 286 || state->ndist > 30) {
00847 strm->msg = (char *)"too many length or distance symbols";
00848 state->mode = BAD;
00849 break;
00850 }
00851 #endif
00852 Tracev((stderr, "inflate: table sizes ok\n"));
00853 state->have = 0;
00854 state->mode = LENLENS;
00855 case LENLENS:
00856 while (state->have < state->ncode) {
00857 NEEDBITS(3);
00858 state->lens[order[state->have++]] = (unsigned short)BITS(3);
00859 DROPBITS(3);
00860 }
00861 while (state->have < 19)
00862 state->lens[order[state->have++]] = 0;
00863 state->next = state->codes;
00864 state->lencode = (code const FAR *)(state->next);
00865 state->lenbits = 7;
00866 ret = inflate_table(CODES, state->lens, 19, &(state->next),
00867 &(state->lenbits), state->work);
00868 if (ret) {
00869 strm->msg = (char *)"invalid code lengths set";
00870 state->mode = BAD;
00871 break;
00872 }
00873 Tracev((stderr, "inflate: code lengths ok\n"));
00874 state->have = 0;
00875 state->mode = CODELENS;
00876 case CODELENS:
00877 while (state->have < state->nlen + state->ndist) {
00878 for (;;) {
00879 this = state->lencode[BITS(state->lenbits)];
00880 if ((unsigned)(this.bits) <= bits) break;
00881 PULLBYTE();
00882 }
00883 if (this.val < 16) {
00884 NEEDBITS(this.bits);
00885 DROPBITS(this.bits);
00886 state->lens[state->have++] = this.val;
00887 }
00888 else {
00889 if (this.val == 16) {
00890 NEEDBITS(this.bits + 2);
00891 DROPBITS(this.bits);
00892 if (state->have == 0) {
00893 strm->msg = (char *)"invalid bit length repeat";
00894 state->mode = BAD;
00895 break;
00896 }
00897 len = state->lens[state->have - 1];
00898 copy = 3 + BITS(2);
00899 DROPBITS(2);
00900 }
00901 else if (this.val == 17) {
00902 NEEDBITS(this.bits + 3);
00903 DROPBITS(this.bits);
00904 len = 0;
00905 copy = 3 + BITS(3);
00906 DROPBITS(3);
00907 }
00908 else {
00909 NEEDBITS(this.bits + 7);
00910 DROPBITS(this.bits);
00911 len = 0;
00912 copy = 11 + BITS(7);
00913 DROPBITS(7);
00914 }
00915 if (state->have + copy > state->nlen + state->ndist) {
00916 strm->msg = (char *)"invalid bit length repeat";
00917 state->mode = BAD;
00918 break;
00919 }
00920 while (copy--)
00921 state->lens[state->have++] = (unsigned short)len;
00922 }
00923 }
00924
00925
00926 if (state->mode == BAD) break;
00927
00928
00929 state->next = state->codes;
00930 state->lencode = (code const FAR *)(state->next);
00931 state->lenbits = 9;
00932 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
00933 &(state->lenbits), state->work);
00934 if (ret) {
00935 strm->msg = (char *)"invalid literal/lengths set";
00936 state->mode = BAD;
00937 break;
00938 }
00939 state->distcode = (code const FAR *)(state->next);
00940 state->distbits = 6;
00941 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
00942 &(state->next), &(state->distbits), state->work);
00943 if (ret) {
00944 strm->msg = (char *)"invalid distances set";
00945 state->mode = BAD;
00946 break;
00947 }
00948 Tracev((stderr, "inflate: codes ok\n"));
00949 state->mode = LEN;
00950 case LEN:
00951 if (have >= 6 && left >= 258) {
00952 RESTORE();
00953 inflate_fast(strm, out);
00954 LOAD();
00955 break;
00956 }
00957 for (;;) {
00958 this = state->lencode[BITS(state->lenbits)];
00959 if ((unsigned)(this.bits) <= bits) break;
00960 PULLBYTE();
00961 }
00962 if (this.op && (this.op & 0xf0) == 0) {
00963 last = this;
00964 for (;;) {
00965 this = state->lencode[last.val +
00966 (BITS(last.bits + last.op) >> last.bits)];
00967 if ((unsigned)(last.bits + this.bits) <= bits) break;
00968 PULLBYTE();
00969 }
00970 DROPBITS(last.bits);
00971 }
00972 DROPBITS(this.bits);
00973 state->length = (unsigned)this.val;
00974 if ((int)(this.op) == 0) {
00975 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
00976 "inflate: literal '%c'\n" :
00977 "inflate: literal 0x%02x\n", this.val));
00978 state->mode = LIT;
00979 break;
00980 }
00981 if (this.op & 32) {
00982 Tracevv((stderr, "inflate: end of block\n"));
00983 state->mode = TYPE;
00984 break;
00985 }
00986 if (this.op & 64) {
00987 strm->msg = (char *)"invalid literal/length code";
00988 state->mode = BAD;
00989 break;
00990 }
00991 state->extra = (unsigned)(this.op) & 15;
00992 state->mode = LENEXT;
00993 case LENEXT:
00994 if (state->extra) {
00995 NEEDBITS(state->extra);
00996 state->length += BITS(state->extra);
00997 DROPBITS(state->extra);
00998 }
00999 Tracevv((stderr, "inflate: length %u\n", state->length));
01000 state->mode = DIST;
01001 case DIST:
01002 for (;;) {
01003 this = state->distcode[BITS(state->distbits)];
01004 if ((unsigned)(this.bits) <= bits) break;
01005 PULLBYTE();
01006 }
01007 if ((this.op & 0xf0) == 0) {
01008 last = this;
01009 for (;;) {
01010 this = state->distcode[last.val +
01011 (BITS(last.bits + last.op) >> last.bits)];
01012 if ((unsigned)(last.bits + this.bits) <= bits) break;
01013 PULLBYTE();
01014 }
01015 DROPBITS(last.bits);
01016 }
01017 DROPBITS(this.bits);
01018 if (this.op & 64) {
01019 strm->msg = (char *)"invalid distance code";
01020 state->mode = BAD;
01021 break;
01022 }
01023 state->offset = (unsigned)this.val;
01024 state->extra = (unsigned)(this.op) & 15;
01025 state->mode = DISTEXT;
01026 case DISTEXT:
01027 if (state->extra) {
01028 NEEDBITS(state->extra);
01029 state->offset += BITS(state->extra);
01030 DROPBITS(state->extra);
01031 }
01032 #ifdef INFLATE_STRICT
01033 if (state->offset > state->dmax) {
01034 strm->msg = (char *)"invalid distance too far back";
01035 state->mode = BAD;
01036 break;
01037 }
01038 #endif
01039 if (state->offset > state->whave + out - left) {
01040 strm->msg = (char *)"invalid distance too far back";
01041 state->mode = BAD;
01042 break;
01043 }
01044 Tracevv((stderr, "inflate: distance %u\n", state->offset));
01045 state->mode = MATCH;
01046 case MATCH:
01047 if (left == 0) goto inf_leave;
01048 copy = out - left;
01049 if (state->offset > copy) {
01050 copy = state->offset - copy;
01051 if (copy > state->write) {
01052 copy -= state->write;
01053 from = state->window + (state->wsize - copy);
01054 }
01055 else
01056 from = state->window + (state->write - copy);
01057 if (copy > state->length) copy = state->length;
01058 }
01059 else {
01060 from = put - state->offset;
01061 copy = state->length;
01062 }
01063 if (copy > left) copy = left;
01064 left -= copy;
01065 state->length -= copy;
01066 do {
01067 *put++ = *from++;
01068 } while (--copy);
01069 if (state->length == 0) state->mode = LEN;
01070 break;
01071 case LIT:
01072 if (left == 0) goto inf_leave;
01073 *put++ = (unsigned char)(state->length);
01074 left--;
01075 state->mode = LEN;
01076 break;
01077 case CHECK:
01078 if (state->wrap) {
01079 NEEDBITS(32);
01080 out -= left;
01081 strm->total_out += out;
01082 state->total += out;
01083 if (out)
01084 strm->adler = state->check =
01085 UPDATE(state->check, put - out, out);
01086 out = left;
01087 if ((
01088 #ifdef GUNZIP
01089 state->flags ? hold :
01090 #endif
01091 REVERSE(hold)) != state->check) {
01092 strm->msg = (char *)"incorrect data check";
01093 state->mode = BAD;
01094 break;
01095 }
01096 INITBITS();
01097 Tracev((stderr, "inflate: check matches trailer\n"));
01098 }
01099 #ifdef GUNZIP
01100 state->mode = LENGTH;
01101 case LENGTH:
01102 if (state->wrap && state->flags) {
01103 NEEDBITS(32);
01104 if (hold != (state->total & 0xffffffffUL)) {
01105 strm->msg = (char *)"incorrect length check";
01106 state->mode = BAD;
01107 break;
01108 }
01109 INITBITS();
01110 Tracev((stderr, "inflate: length matches trailer\n"));
01111 }
01112 #endif
01113 state->mode = DONE;
01114 case DONE:
01115 ret = Z_STREAM_END;
01116 goto inf_leave;
01117 case BAD:
01118 ret = Z_DATA_ERROR;
01119 goto inf_leave;
01120 case MEM:
01121 return Z_MEM_ERROR;
01122 case SYNC:
01123 default:
01124 return Z_STREAM_ERROR;
01125 }
01126
01127
01128
01129
01130
01131
01132
01133 inf_leave:
01134 RESTORE();
01135 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
01136 if (updatewindow(strm, out)) {
01137 state->mode = MEM;
01138 return Z_MEM_ERROR;
01139 }
01140 in -= strm->avail_in;
01141 out -= strm->avail_out;
01142 strm->total_in += in;
01143 strm->total_out += out;
01144 state->total += out;
01145 if (state->wrap && out)
01146 strm->adler = state->check =
01147 UPDATE(state->check, strm->next_out - out, out);
01148 strm->data_type = state->bits + (state->last ? 64 : 0) +
01149 (state->mode == TYPE ? 128 : 0);
01150 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
01151 ret = Z_BUF_ERROR;
01152 return ret;
01153 }
01154
01155 int ZEXPORT inflateEnd(strm)
01156 z_streamp strm;
01157 {
01158 struct inflate_state FAR *state;
01159 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
01160 return Z_STREAM_ERROR;
01161 state = (struct inflate_state FAR *)strm->state;
01162 if (state->window != Z_NULL) ZFREE(strm, state->window);
01163 ZFREE(strm, strm->state);
01164 strm->state = Z_NULL;
01165 Tracev((stderr, "inflate: end\n"));
01166 return Z_OK;
01167 }
01168
01169 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
01170 z_streamp strm;
01171 const Bytef *dictionary;
01172 uInt dictLength;
01173 {
01174 struct inflate_state FAR *state;
01175 unsigned long id;
01176
01177
01178 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01179 state = (struct inflate_state FAR *)strm->state;
01180 if (state->wrap != 0 && state->mode != DICT)
01181 return Z_STREAM_ERROR;
01182
01183
01184 if (state->mode == DICT) {
01185 id = adler32(0L, Z_NULL, 0);
01186 id = adler32(id, dictionary, dictLength);
01187 if (id != state->check)
01188 return Z_DATA_ERROR;
01189 }
01190
01191
01192 if (updatewindow(strm, strm->avail_out)) {
01193 state->mode = MEM;
01194 return Z_MEM_ERROR;
01195 }
01196 if (dictLength > state->wsize) {
01197 zmemcpy(state->window, dictionary + dictLength - state->wsize,
01198 state->wsize);
01199 state->whave = state->wsize;
01200 }
01201 else {
01202 zmemcpy(state->window + state->wsize - dictLength, dictionary,
01203 dictLength);
01204 state->whave = dictLength;
01205 }
01206 state->havedict = 1;
01207 Tracev((stderr, "inflate: dictionary set\n"));
01208 return Z_OK;
01209 }
01210
01211 int ZEXPORT inflateGetHeader(strm, head)
01212 z_streamp strm;
01213 gz_headerp head;
01214 {
01215 struct inflate_state FAR *state;
01216
01217
01218 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01219 state = (struct inflate_state FAR *)strm->state;
01220 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
01221
01222
01223 state->head = head;
01224 head->done = 0;
01225 return Z_OK;
01226 }
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239 local unsigned syncsearch(have, buf, len)
01240 unsigned FAR *have;
01241 unsigned char FAR *buf;
01242 unsigned len;
01243 {
01244 unsigned got;
01245 unsigned next;
01246
01247 got = *have;
01248 next = 0;
01249 while (next < len && got < 4) {
01250 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
01251 got++;
01252 else if (buf[next])
01253 got = 0;
01254 else
01255 got = 4 - got;
01256 next++;
01257 }
01258 *have = got;
01259 return next;
01260 }
01261
01262 int ZEXPORT inflateSync(strm)
01263 z_streamp strm;
01264 {
01265 unsigned len;
01266 unsigned long in, out;
01267 unsigned char buf[4];
01268 struct inflate_state FAR *state;
01269
01270
01271 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01272 state = (struct inflate_state FAR *)strm->state;
01273 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
01274
01275
01276 if (state->mode != SYNC) {
01277 state->mode = SYNC;
01278 state->hold <<= state->bits & 7;
01279 state->bits -= state->bits & 7;
01280 len = 0;
01281 while (state->bits >= 8) {
01282 buf[len++] = (unsigned char)(state->hold);
01283 state->hold >>= 8;
01284 state->bits -= 8;
01285 }
01286 state->have = 0;
01287 syncsearch(&(state->have), buf, len);
01288 }
01289
01290
01291 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
01292 strm->avail_in -= len;
01293 strm->next_in += len;
01294 strm->total_in += len;
01295
01296
01297 if (state->have != 4) return Z_DATA_ERROR;
01298 in = strm->total_in; out = strm->total_out;
01299 inflateReset(strm);
01300 strm->total_in = in; strm->total_out = out;
01301 state->mode = TYPE;
01302 return Z_OK;
01303 }
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313 int ZEXPORT inflateSyncPoint(strm)
01314 z_streamp strm;
01315 {
01316 struct inflate_state FAR *state;
01317
01318 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
01319 state = (struct inflate_state FAR *)strm->state;
01320 return state->mode == STORED && state->bits == 0;
01321 }
01322
01323 int ZEXPORT inflateCopy(dest, source)
01324 z_streamp dest;
01325 z_streamp source;
01326 {
01327 struct inflate_state FAR *state;
01328 struct inflate_state FAR *copy;
01329 unsigned char FAR *window;
01330 unsigned wsize;
01331
01332
01333 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
01334 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
01335 return Z_STREAM_ERROR;
01336 state = (struct inflate_state FAR *)source->state;
01337
01338
01339 copy = (struct inflate_state FAR *)
01340 ZALLOC(source, 1, sizeof(struct inflate_state));
01341 if (copy == Z_NULL) return Z_MEM_ERROR;
01342 window = Z_NULL;
01343 if (state->window != Z_NULL) {
01344 window = (unsigned char FAR *)
01345 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
01346 if (window == Z_NULL) {
01347 ZFREE(source, copy);
01348 return Z_MEM_ERROR;
01349 }
01350 }
01351
01352
01353 zmemcpy(dest, source, sizeof(z_stream));
01354 zmemcpy(copy, state, sizeof(struct inflate_state));
01355 if (state->lencode >= state->codes &&
01356 state->lencode <= state->codes + ENOUGH - 1) {
01357 copy->lencode = copy->codes + (state->lencode - state->codes);
01358 copy->distcode = copy->codes + (state->distcode - state->codes);
01359 }
01360 copy->next = copy->codes + (state->next - state->codes);
01361 if (window != Z_NULL) {
01362 wsize = 1U << state->wbits;
01363 zmemcpy(window, state->window, wsize);
01364 }
01365 copy->window = window;
01366 dest->state = (struct internal_state FAR *)copy;
01367 return Z_OK;
01368 }