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