File indexing completed on 2025-05-11 08:24:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 #include "zutil.h"
0084 #include "inftrees.h"
0085 #include "inflate.h"
0086 #include "inffast.h"
0087
0088 #ifdef MAKEFIXED
0089 # ifndef BUILDFIXED
0090 # define BUILDFIXED
0091 # endif
0092 #endif
0093
0094
0095 local int inflateStateCheck OF((z_streamp strm));
0096 local void fixedtables OF((struct inflate_state FAR *state));
0097 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
0098 unsigned copy));
0099 #ifdef BUILDFIXED
0100 void makefixed OF((void));
0101 #endif
0102 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
0103 unsigned len));
0104
0105 local int inflateStateCheck(strm)
0106 z_streamp strm;
0107 {
0108 struct inflate_state FAR *state;
0109 if (strm == Z_NULL ||
0110 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
0111 return 1;
0112 state = (struct inflate_state FAR *)strm->state;
0113 if (state == Z_NULL || state->strm != strm ||
0114 state->mode < HEAD || state->mode > SYNC)
0115 return 1;
0116 return 0;
0117 }
0118
0119 int ZEXPORT inflateResetKeep(strm)
0120 z_streamp strm;
0121 {
0122 struct inflate_state FAR *state;
0123
0124 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
0125 state = (struct inflate_state FAR *)strm->state;
0126 strm->total_in = strm->total_out = state->total = 0;
0127 strm->msg = Z_NULL;
0128 if (state->wrap)
0129 strm->adler = state->wrap & 1;
0130 state->mode = HEAD;
0131 state->last = 0;
0132 state->havedict = 0;
0133 state->flags = -1;
0134 state->dmax = 32768U;
0135 state->head = Z_NULL;
0136 state->hold = 0;
0137 state->bits = 0;
0138 state->lencode = state->distcode = state->next = state->codes;
0139 state->sane = 1;
0140 state->back = -1;
0141 Tracev((stderr, "inflate: reset\n"));
0142 return Z_OK;
0143 }
0144
0145 int ZEXPORT inflateReset(strm)
0146 z_streamp strm;
0147 {
0148 struct inflate_state FAR *state;
0149
0150 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
0151 state = (struct inflate_state FAR *)strm->state;
0152 state->wsize = 0;
0153 state->whave = 0;
0154 state->wnext = 0;
0155 return inflateResetKeep(strm);
0156 }
0157
0158 int ZEXPORT inflateReset2(strm, windowBits)
0159 z_streamp strm;
0160 int windowBits;
0161 {
0162 int wrap;
0163 struct inflate_state FAR *state;
0164
0165
0166 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
0167 state = (struct inflate_state FAR *)strm->state;
0168
0169
0170 if (windowBits < 0) {
0171 if (windowBits < -15)
0172 return Z_STREAM_ERROR;
0173 wrap = 0;
0174 windowBits = -windowBits;
0175 }
0176 else {
0177 wrap = (windowBits >> 4) + 5;
0178 #ifdef GUNZIP
0179 if (windowBits < 48)
0180 windowBits &= 15;
0181 #endif
0182 }
0183
0184
0185 if (windowBits && (windowBits < 8 || windowBits > 15))
0186 return Z_STREAM_ERROR;
0187 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
0188 ZFREE(strm, state->window);
0189 state->window = Z_NULL;
0190 }
0191
0192
0193 state->wrap = wrap;
0194 state->wbits = (unsigned)windowBits;
0195 return inflateReset(strm);
0196 }
0197
0198 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
0199 z_streamp strm;
0200 int windowBits;
0201 const char *version;
0202 int stream_size;
0203 {
0204 int ret;
0205 struct inflate_state FAR *state;
0206
0207 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
0208 stream_size != (int)(sizeof(z_stream)))
0209 return Z_VERSION_ERROR;
0210 if (strm == Z_NULL) return Z_STREAM_ERROR;
0211 strm->msg = Z_NULL;
0212 if (strm->zalloc == (alloc_func)0) {
0213 #ifdef Z_SOLO
0214 return Z_STREAM_ERROR;
0215 #else
0216 strm->zalloc = zcalloc;
0217 strm->opaque = (voidpf)0;
0218 #endif
0219 }
0220 if (strm->zfree == (free_func)0)
0221 #ifdef Z_SOLO
0222 return Z_STREAM_ERROR;
0223 #else
0224 strm->zfree = zcfree;
0225 #endif
0226 state = (struct inflate_state FAR *)
0227 ZALLOC(strm, 1, sizeof(struct inflate_state));
0228 if (state == Z_NULL) return Z_MEM_ERROR;
0229 Tracev((stderr, "inflate: allocated\n"));
0230 strm->state = (struct internal_state FAR *)state;
0231 state->strm = strm;
0232 state->window = Z_NULL;
0233 state->mode = HEAD;
0234 ret = inflateReset2(strm, windowBits);
0235 if (ret != Z_OK) {
0236 ZFREE(strm, state);
0237 strm->state = Z_NULL;
0238 }
0239 return ret;
0240 }
0241
0242 int ZEXPORT inflateInit_(strm, version, stream_size)
0243 z_streamp strm;
0244 const char *version;
0245 int stream_size;
0246 {
0247 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
0248 }
0249
0250 int ZEXPORT inflatePrime(strm, bits, value)
0251 z_streamp strm;
0252 int bits;
0253 int value;
0254 {
0255 struct inflate_state FAR *state;
0256
0257 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
0258 state = (struct inflate_state FAR *)strm->state;
0259 if (bits < 0) {
0260 state->hold = 0;
0261 state->bits = 0;
0262 return Z_OK;
0263 }
0264 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
0265 value &= (1L << bits) - 1;
0266 state->hold += (unsigned)value << state->bits;
0267 state->bits += (uInt)bits;
0268 return Z_OK;
0269 }
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 local void fixedtables(state)
0282 struct inflate_state FAR *state;
0283 {
0284 #ifdef BUILDFIXED
0285 static int virgin = 1;
0286 static code *lenfix, *distfix;
0287 static code fixed[544];
0288
0289
0290 if (virgin) {
0291 unsigned sym, bits;
0292 static code *next;
0293
0294
0295 sym = 0;
0296 while (sym < 144) state->lens[sym++] = 8;
0297 while (sym < 256) state->lens[sym++] = 9;
0298 while (sym < 280) state->lens[sym++] = 7;
0299 while (sym < 288) state->lens[sym++] = 8;
0300 next = fixed;
0301 lenfix = next;
0302 bits = 9;
0303 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
0304
0305
0306 sym = 0;
0307 while (sym < 32) state->lens[sym++] = 5;
0308 distfix = next;
0309 bits = 5;
0310 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
0311
0312
0313 virgin = 0;
0314 }
0315 #else
0316 # include "inffixed.h"
0317 #endif
0318 state->lencode = lenfix;
0319 state->lenbits = 9;
0320 state->distcode = distfix;
0321 state->distbits = 5;
0322 }
0323
0324 #ifdef MAKEFIXED
0325 #include <stdio.h>
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 void makefixed()
0346 {
0347 unsigned low, size;
0348 struct inflate_state state;
0349
0350 fixedtables(&state);
0351 puts(" /* inffixed.h -- table for decoding fixed codes");
0352 puts(" * Generated automatically by makefixed().");
0353 puts(" */");
0354 puts("");
0355 puts(" /* WARNING: this file should *not* be used by applications.");
0356 puts(" It is part of the implementation of this library and is");
0357 puts(" subject to change. Applications should only use zlib.h.");
0358 puts(" */");
0359 puts("");
0360 size = 1U << 9;
0361 printf(" static const code lenfix[%u] = {", size);
0362 low = 0;
0363 for (;;) {
0364 if ((low % 7) == 0) printf("\n ");
0365 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
0366 state.lencode[low].bits, state.lencode[low].val);
0367 if (++low == size) break;
0368 putchar(',');
0369 }
0370 puts("\n };");
0371 size = 1U << 5;
0372 printf("\n static const code distfix[%u] = {", size);
0373 low = 0;
0374 for (;;) {
0375 if ((low % 6) == 0) printf("\n ");
0376 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
0377 state.distcode[low].val);
0378 if (++low == size) break;
0379 putchar(',');
0380 }
0381 puts("\n };");
0382 }
0383 #endif
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399 local int updatewindow(strm, end, copy)
0400 z_streamp strm;
0401 const Bytef *end;
0402 unsigned copy;
0403 {
0404 struct inflate_state FAR *state;
0405 unsigned dist;
0406
0407 state = (struct inflate_state FAR *)strm->state;
0408
0409
0410 if (state->window == Z_NULL) {
0411 state->window = (unsigned char FAR *)
0412 ZALLOC(strm, 1U << state->wbits,
0413 sizeof(unsigned char));
0414 if (state->window == Z_NULL) return 1;
0415 }
0416
0417
0418 if (state->wsize == 0) {
0419 state->wsize = 1U << state->wbits;
0420 state->wnext = 0;
0421 state->whave = 0;
0422 }
0423
0424
0425 if (copy >= state->wsize) {
0426 zmemcpy(state->window, end - state->wsize, state->wsize);
0427 state->wnext = 0;
0428 state->whave = state->wsize;
0429 }
0430 else {
0431 dist = state->wsize - state->wnext;
0432 if (dist > copy) dist = copy;
0433 zmemcpy(state->window + state->wnext, end - copy, dist);
0434 copy -= dist;
0435 if (copy) {
0436 zmemcpy(state->window, end - copy, copy);
0437 state->wnext = copy;
0438 state->whave = state->wsize;
0439 }
0440 else {
0441 state->wnext += dist;
0442 if (state->wnext == state->wsize) state->wnext = 0;
0443 if (state->whave < state->wsize) state->whave += dist;
0444 }
0445 }
0446 return 0;
0447 }
0448
0449
0450
0451
0452 #ifdef GUNZIP
0453 # define UPDATE_CHECK(check, buf, len) \
0454 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
0455 #else
0456 # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
0457 #endif
0458
0459
0460 #ifdef GUNZIP
0461 # define CRC2(check, word) \
0462 do { \
0463 hbuf[0] = (unsigned char)(word); \
0464 hbuf[1] = (unsigned char)((word) >> 8); \
0465 check = crc32(check, hbuf, 2); \
0466 } while (0)
0467
0468 # define CRC4(check, word) \
0469 do { \
0470 hbuf[0] = (unsigned char)(word); \
0471 hbuf[1] = (unsigned char)((word) >> 8); \
0472 hbuf[2] = (unsigned char)((word) >> 16); \
0473 hbuf[3] = (unsigned char)((word) >> 24); \
0474 check = crc32(check, hbuf, 4); \
0475 } while (0)
0476 #endif
0477
0478
0479 #define LOAD() \
0480 do { \
0481 put = strm->next_out; \
0482 left = strm->avail_out; \
0483 next = strm->next_in; \
0484 have = strm->avail_in; \
0485 hold = state->hold; \
0486 bits = state->bits; \
0487 } while (0)
0488
0489
0490 #define RESTORE() \
0491 do { \
0492 strm->next_out = put; \
0493 strm->avail_out = left; \
0494 strm->next_in = next; \
0495 strm->avail_in = have; \
0496 state->hold = hold; \
0497 state->bits = bits; \
0498 } while (0)
0499
0500
0501 #define INITBITS() \
0502 do { \
0503 hold = 0; \
0504 bits = 0; \
0505 } while (0)
0506
0507
0508
0509 #define PULLBYTE() \
0510 do { \
0511 if (have == 0) goto inf_leave; \
0512 have--; \
0513 hold += (unsigned long)(*next++) << bits; \
0514 bits += 8; \
0515 } while (0)
0516
0517
0518
0519 #define NEEDBITS(n) \
0520 do { \
0521 while (bits < (unsigned)(n)) \
0522 PULLBYTE(); \
0523 } while (0)
0524
0525
0526 #define BITS(n) \
0527 ((unsigned)hold & ((1U << (n)) - 1))
0528
0529
0530 #define DROPBITS(n) \
0531 do { \
0532 hold >>= (n); \
0533 bits -= (unsigned)(n); \
0534 } while (0)
0535
0536
0537 #define BYTEBITS() \
0538 do { \
0539 hold >>= bits & 7; \
0540 bits -= bits & 7; \
0541 } while (0)
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625 int ZEXPORT inflate(strm, flush)
0626 z_streamp strm;
0627 int flush;
0628 {
0629 struct inflate_state FAR *state;
0630 z_const unsigned char FAR *next;
0631 unsigned char FAR *put;
0632 unsigned have, left;
0633 unsigned long hold;
0634 unsigned bits;
0635 unsigned in, out;
0636 unsigned copy;
0637 unsigned char FAR *from;
0638 code here;
0639 code last;
0640 unsigned len;
0641 int ret;
0642 #ifdef GUNZIP
0643 unsigned char hbuf[4];
0644 #endif
0645 static const unsigned short order[19] =
0646 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0647
0648 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
0649 (strm->next_in == Z_NULL && strm->avail_in != 0))
0650 return Z_STREAM_ERROR;
0651
0652 state = (struct inflate_state FAR *)strm->state;
0653 if (state->mode == TYPE) state->mode = TYPEDO;
0654 LOAD();
0655 in = have;
0656 out = left;
0657 ret = Z_OK;
0658 for (;;)
0659 switch (state->mode) {
0660 case HEAD:
0661 if (state->wrap == 0) {
0662 state->mode = TYPEDO;
0663 break;
0664 }
0665 NEEDBITS(16);
0666 #ifdef GUNZIP
0667 if ((state->wrap & 2) && hold == 0x8b1f) {
0668 if (state->wbits == 0)
0669 state->wbits = 15;
0670 state->check = crc32(0L, Z_NULL, 0);
0671 CRC2(state->check, hold);
0672 INITBITS();
0673 state->mode = FLAGS;
0674 break;
0675 }
0676 if (state->head != Z_NULL)
0677 state->head->done = -1;
0678 if (!(state->wrap & 1) ||
0679 #else
0680 if (
0681 #endif
0682 ((BITS(8) << 8) + (hold >> 8)) % 31) {
0683 strm->msg = (char *)"incorrect header check";
0684 state->mode = BAD;
0685 break;
0686 }
0687 if (BITS(4) != Z_DEFLATED) {
0688 strm->msg = (char *)"unknown compression method";
0689 state->mode = BAD;
0690 break;
0691 }
0692 DROPBITS(4);
0693 len = BITS(4) + 8;
0694 if (state->wbits == 0)
0695 state->wbits = len;
0696 if (len > 15 || len > state->wbits) {
0697 strm->msg = (char *)"invalid window size";
0698 state->mode = BAD;
0699 break;
0700 }
0701 state->dmax = 1U << len;
0702 state->flags = 0;
0703 Tracev((stderr, "inflate: zlib header ok\n"));
0704 strm->adler = state->check = adler32(0L, Z_NULL, 0);
0705 state->mode = hold & 0x200 ? DICTID : TYPE;
0706 INITBITS();
0707 break;
0708 #ifdef GUNZIP
0709 case FLAGS:
0710 NEEDBITS(16);
0711 state->flags = (int)(hold);
0712 if ((state->flags & 0xff) != Z_DEFLATED) {
0713 strm->msg = (char *)"unknown compression method";
0714 state->mode = BAD;
0715 break;
0716 }
0717 if (state->flags & 0xe000) {
0718 strm->msg = (char *)"unknown header flags set";
0719 state->mode = BAD;
0720 break;
0721 }
0722 if (state->head != Z_NULL)
0723 state->head->text = (int)((hold >> 8) & 1);
0724 if ((state->flags & 0x0200) && (state->wrap & 4))
0725 CRC2(state->check, hold);
0726 INITBITS();
0727 state->mode = TIME;
0728
0729 case TIME:
0730 NEEDBITS(32);
0731 if (state->head != Z_NULL)
0732 state->head->time = hold;
0733 if ((state->flags & 0x0200) && (state->wrap & 4))
0734 CRC4(state->check, hold);
0735 INITBITS();
0736 state->mode = OS;
0737
0738 case OS:
0739 NEEDBITS(16);
0740 if (state->head != Z_NULL) {
0741 state->head->xflags = (int)(hold & 0xff);
0742 state->head->os = (int)(hold >> 8);
0743 }
0744 if ((state->flags & 0x0200) && (state->wrap & 4))
0745 CRC2(state->check, hold);
0746 INITBITS();
0747 state->mode = EXLEN;
0748
0749 case EXLEN:
0750 if (state->flags & 0x0400) {
0751 NEEDBITS(16);
0752 state->length = (unsigned)(hold);
0753 if (state->head != Z_NULL)
0754 state->head->extra_len = (unsigned)hold;
0755 if ((state->flags & 0x0200) && (state->wrap & 4))
0756 CRC2(state->check, hold);
0757 INITBITS();
0758 }
0759 else if (state->head != Z_NULL)
0760 state->head->extra = Z_NULL;
0761 state->mode = EXTRA;
0762
0763 case EXTRA:
0764 if (state->flags & 0x0400) {
0765 copy = state->length;
0766 if (copy > have) copy = have;
0767 if (copy) {
0768 if (state->head != Z_NULL &&
0769 state->head->extra != Z_NULL &&
0770 (len = state->head->extra_len - state->length) <
0771 state->head->extra_max) {
0772 zmemcpy(state->head->extra + len, next,
0773 len + copy > state->head->extra_max ?
0774 state->head->extra_max - len : copy);
0775 }
0776 if ((state->flags & 0x0200) && (state->wrap & 4))
0777 state->check = crc32(state->check, next, copy);
0778 have -= copy;
0779 next += copy;
0780 state->length -= copy;
0781 }
0782 if (state->length) goto inf_leave;
0783 }
0784 state->length = 0;
0785 state->mode = NAME;
0786
0787 case NAME:
0788 if (state->flags & 0x0800) {
0789 if (have == 0) goto inf_leave;
0790 copy = 0;
0791 do {
0792 len = (unsigned)(next[copy++]);
0793 if (state->head != Z_NULL &&
0794 state->head->name != Z_NULL &&
0795 state->length < state->head->name_max)
0796 state->head->name[state->length++] = (Bytef)len;
0797 } while (len && copy < have);
0798 if ((state->flags & 0x0200) && (state->wrap & 4))
0799 state->check = crc32(state->check, next, copy);
0800 have -= copy;
0801 next += copy;
0802 if (len) goto inf_leave;
0803 }
0804 else if (state->head != Z_NULL)
0805 state->head->name = Z_NULL;
0806 state->length = 0;
0807 state->mode = COMMENT;
0808
0809 case COMMENT:
0810 if (state->flags & 0x1000) {
0811 if (have == 0) goto inf_leave;
0812 copy = 0;
0813 do {
0814 len = (unsigned)(next[copy++]);
0815 if (state->head != Z_NULL &&
0816 state->head->comment != Z_NULL &&
0817 state->length < state->head->comm_max)
0818 state->head->comment[state->length++] = (Bytef)len;
0819 } while (len && copy < have);
0820 if ((state->flags & 0x0200) && (state->wrap & 4))
0821 state->check = crc32(state->check, next, copy);
0822 have -= copy;
0823 next += copy;
0824 if (len) goto inf_leave;
0825 }
0826 else if (state->head != Z_NULL)
0827 state->head->comment = Z_NULL;
0828 state->mode = HCRC;
0829
0830 case HCRC:
0831 if (state->flags & 0x0200) {
0832 NEEDBITS(16);
0833 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
0834 strm->msg = (char *)"header crc mismatch";
0835 state->mode = BAD;
0836 break;
0837 }
0838 INITBITS();
0839 }
0840 if (state->head != Z_NULL) {
0841 state->head->hcrc = (int)((state->flags >> 9) & 1);
0842 state->head->done = 1;
0843 }
0844 strm->adler = state->check = crc32(0L, Z_NULL, 0);
0845 state->mode = TYPE;
0846 break;
0847 #endif
0848 case DICTID:
0849 NEEDBITS(32);
0850 strm->adler = state->check = ZSWAP32(hold);
0851 INITBITS();
0852 state->mode = DICT;
0853
0854 case DICT:
0855 if (state->havedict == 0) {
0856 RESTORE();
0857 return Z_NEED_DICT;
0858 }
0859 strm->adler = state->check = adler32(0L, Z_NULL, 0);
0860 state->mode = TYPE;
0861
0862 case TYPE:
0863 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
0864
0865 case TYPEDO:
0866 if (state->last) {
0867 BYTEBITS();
0868 state->mode = CHECK;
0869 break;
0870 }
0871 NEEDBITS(3);
0872 state->last = BITS(1);
0873 DROPBITS(1);
0874 switch (BITS(2)) {
0875 case 0:
0876 Tracev((stderr, "inflate: stored block%s\n",
0877 state->last ? " (last)" : ""));
0878 state->mode = STORED;
0879 break;
0880 case 1:
0881 fixedtables(state);
0882 Tracev((stderr, "inflate: fixed codes block%s\n",
0883 state->last ? " (last)" : ""));
0884 state->mode = LEN_;
0885 if (flush == Z_TREES) {
0886 DROPBITS(2);
0887 goto inf_leave;
0888 }
0889 break;
0890 case 2:
0891 Tracev((stderr, "inflate: dynamic codes block%s\n",
0892 state->last ? " (last)" : ""));
0893 state->mode = TABLE;
0894 break;
0895 case 3:
0896 strm->msg = (char *)"invalid block type";
0897 state->mode = BAD;
0898 }
0899 DROPBITS(2);
0900 break;
0901 case STORED:
0902 BYTEBITS();
0903 NEEDBITS(32);
0904 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
0905 strm->msg = (char *)"invalid stored block lengths";
0906 state->mode = BAD;
0907 break;
0908 }
0909 state->length = (unsigned)hold & 0xffff;
0910 Tracev((stderr, "inflate: stored length %u\n",
0911 state->length));
0912 INITBITS();
0913 state->mode = COPY_;
0914 if (flush == Z_TREES) goto inf_leave;
0915
0916 case COPY_:
0917 state->mode = COPY;
0918
0919 case COPY:
0920 copy = state->length;
0921 if (copy) {
0922 if (copy > have) copy = have;
0923 if (copy > left) copy = left;
0924 if (copy == 0) goto inf_leave;
0925 zmemcpy(put, next, copy);
0926 have -= copy;
0927 next += copy;
0928 left -= copy;
0929 put += copy;
0930 state->length -= copy;
0931 break;
0932 }
0933 Tracev((stderr, "inflate: stored end\n"));
0934 state->mode = TYPE;
0935 break;
0936 case TABLE:
0937 NEEDBITS(14);
0938 state->nlen = BITS(5) + 257;
0939 DROPBITS(5);
0940 state->ndist = BITS(5) + 1;
0941 DROPBITS(5);
0942 state->ncode = BITS(4) + 4;
0943 DROPBITS(4);
0944 #ifndef PKZIP_BUG_WORKAROUND
0945 if (state->nlen > 286 || state->ndist > 30) {
0946 strm->msg = (char *)"too many length or distance symbols";
0947 state->mode = BAD;
0948 break;
0949 }
0950 #endif
0951 Tracev((stderr, "inflate: table sizes ok\n"));
0952 state->have = 0;
0953 state->mode = LENLENS;
0954
0955 case LENLENS:
0956 while (state->have < state->ncode) {
0957 NEEDBITS(3);
0958 state->lens[order[state->have++]] = (unsigned short)BITS(3);
0959 DROPBITS(3);
0960 }
0961 while (state->have < 19)
0962 state->lens[order[state->have++]] = 0;
0963 state->next = state->codes;
0964 state->lencode = (const code FAR *)(state->next);
0965 state->lenbits = 7;
0966 ret = inflate_table(CODES, state->lens, 19, &(state->next),
0967 &(state->lenbits), state->work);
0968 if (ret) {
0969 strm->msg = (char *)"invalid code lengths set";
0970 state->mode = BAD;
0971 break;
0972 }
0973 Tracev((stderr, "inflate: code lengths ok\n"));
0974 state->have = 0;
0975 state->mode = CODELENS;
0976
0977 case CODELENS:
0978 while (state->have < state->nlen + state->ndist) {
0979 for (;;) {
0980 here = state->lencode[BITS(state->lenbits)];
0981 if ((unsigned)(here.bits) <= bits) break;
0982 PULLBYTE();
0983 }
0984 if (here.val < 16) {
0985 DROPBITS(here.bits);
0986 state->lens[state->have++] = here.val;
0987 }
0988 else {
0989 if (here.val == 16) {
0990 NEEDBITS(here.bits + 2);
0991 DROPBITS(here.bits);
0992 if (state->have == 0) {
0993 strm->msg = (char *)"invalid bit length repeat";
0994 state->mode = BAD;
0995 break;
0996 }
0997 len = state->lens[state->have - 1];
0998 copy = 3 + BITS(2);
0999 DROPBITS(2);
1000 }
1001 else if (here.val == 17) {
1002 NEEDBITS(here.bits + 3);
1003 DROPBITS(here.bits);
1004 len = 0;
1005 copy = 3 + BITS(3);
1006 DROPBITS(3);
1007 }
1008 else {
1009 NEEDBITS(here.bits + 7);
1010 DROPBITS(here.bits);
1011 len = 0;
1012 copy = 11 + BITS(7);
1013 DROPBITS(7);
1014 }
1015 if (state->have + copy > state->nlen + state->ndist) {
1016 strm->msg = (char *)"invalid bit length repeat";
1017 state->mode = BAD;
1018 break;
1019 }
1020 while (copy--)
1021 state->lens[state->have++] = (unsigned short)len;
1022 }
1023 }
1024
1025
1026 if (state->mode == BAD) break;
1027
1028
1029 if (state->lens[256] == 0) {
1030 strm->msg = (char *)"invalid code -- missing end-of-block";
1031 state->mode = BAD;
1032 break;
1033 }
1034
1035
1036
1037
1038 state->next = state->codes;
1039 state->lencode = (const code FAR *)(state->next);
1040 state->lenbits = 9;
1041 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1042 &(state->lenbits), state->work);
1043 if (ret) {
1044 strm->msg = (char *)"invalid literal/lengths set";
1045 state->mode = BAD;
1046 break;
1047 }
1048 state->distcode = (const code FAR *)(state->next);
1049 state->distbits = 6;
1050 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1051 &(state->next), &(state->distbits), state->work);
1052 if (ret) {
1053 strm->msg = (char *)"invalid distances set";
1054 state->mode = BAD;
1055 break;
1056 }
1057 Tracev((stderr, "inflate: codes ok\n"));
1058 state->mode = LEN_;
1059 if (flush == Z_TREES) goto inf_leave;
1060
1061 case LEN_:
1062 state->mode = LEN;
1063
1064 case LEN:
1065 if (have >= 6 && left >= 258) {
1066 RESTORE();
1067 inflate_fast(strm, out);
1068 LOAD();
1069 if (state->mode == TYPE)
1070 state->back = -1;
1071 break;
1072 }
1073 state->back = 0;
1074 for (;;) {
1075 here = state->lencode[BITS(state->lenbits)];
1076 if ((unsigned)(here.bits) <= bits) break;
1077 PULLBYTE();
1078 }
1079 if (here.op && (here.op & 0xf0) == 0) {
1080 last = here;
1081 for (;;) {
1082 here = state->lencode[last.val +
1083 (BITS(last.bits + last.op) >> last.bits)];
1084 if ((unsigned)(last.bits + here.bits) <= bits) break;
1085 PULLBYTE();
1086 }
1087 DROPBITS(last.bits);
1088 state->back += last.bits;
1089 }
1090 DROPBITS(here.bits);
1091 state->back += here.bits;
1092 state->length = (unsigned)here.val;
1093 if ((int)(here.op) == 0) {
1094 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1095 "inflate: literal '%c'\n" :
1096 "inflate: literal 0x%02x\n", here.val));
1097 state->mode = LIT;
1098 break;
1099 }
1100 if (here.op & 32) {
1101 Tracevv((stderr, "inflate: end of block\n"));
1102 state->back = -1;
1103 state->mode = TYPE;
1104 break;
1105 }
1106 if (here.op & 64) {
1107 strm->msg = (char *)"invalid literal/length code";
1108 state->mode = BAD;
1109 break;
1110 }
1111 state->extra = (unsigned)(here.op) & 15;
1112 state->mode = LENEXT;
1113
1114 case LENEXT:
1115 if (state->extra) {
1116 NEEDBITS(state->extra);
1117 state->length += BITS(state->extra);
1118 DROPBITS(state->extra);
1119 state->back += state->extra;
1120 }
1121 Tracevv((stderr, "inflate: length %u\n", state->length));
1122 state->was = state->length;
1123 state->mode = DIST;
1124
1125 case DIST:
1126 for (;;) {
1127 here = state->distcode[BITS(state->distbits)];
1128 if ((unsigned)(here.bits) <= bits) break;
1129 PULLBYTE();
1130 }
1131 if ((here.op & 0xf0) == 0) {
1132 last = here;
1133 for (;;) {
1134 here = state->distcode[last.val +
1135 (BITS(last.bits + last.op) >> last.bits)];
1136 if ((unsigned)(last.bits + here.bits) <= bits) break;
1137 PULLBYTE();
1138 }
1139 DROPBITS(last.bits);
1140 state->back += last.bits;
1141 }
1142 DROPBITS(here.bits);
1143 state->back += here.bits;
1144 if (here.op & 64) {
1145 strm->msg = (char *)"invalid distance code";
1146 state->mode = BAD;
1147 break;
1148 }
1149 state->offset = (unsigned)here.val;
1150 state->extra = (unsigned)(here.op) & 15;
1151 state->mode = DISTEXT;
1152
1153 case DISTEXT:
1154 if (state->extra) {
1155 NEEDBITS(state->extra);
1156 state->offset += BITS(state->extra);
1157 DROPBITS(state->extra);
1158 state->back += state->extra;
1159 }
1160 #ifdef INFLATE_STRICT
1161 if (state->offset > state->dmax) {
1162 strm->msg = (char *)"invalid distance too far back";
1163 state->mode = BAD;
1164 break;
1165 }
1166 #endif
1167 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1168 state->mode = MATCH;
1169
1170 case MATCH:
1171 if (left == 0) goto inf_leave;
1172 copy = out - left;
1173 if (state->offset > copy) {
1174 copy = state->offset - copy;
1175 if (copy > state->whave) {
1176 if (state->sane) {
1177 strm->msg = (char *)"invalid distance too far back";
1178 state->mode = BAD;
1179 break;
1180 }
1181 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1182 Trace((stderr, "inflate.c too far\n"));
1183 copy -= state->whave;
1184 if (copy > state->length) copy = state->length;
1185 if (copy > left) copy = left;
1186 left -= copy;
1187 state->length -= copy;
1188 do {
1189 *put++ = 0;
1190 } while (--copy);
1191 if (state->length == 0) state->mode = LEN;
1192 break;
1193 #endif
1194 }
1195 if (copy > state->wnext) {
1196 copy -= state->wnext;
1197 from = state->window + (state->wsize - copy);
1198 }
1199 else
1200 from = state->window + (state->wnext - copy);
1201 if (copy > state->length) copy = state->length;
1202 }
1203 else {
1204 from = put - state->offset;
1205 copy = state->length;
1206 }
1207 if (copy > left) copy = left;
1208 left -= copy;
1209 state->length -= copy;
1210 do {
1211 *put++ = *from++;
1212 } while (--copy);
1213 if (state->length == 0) state->mode = LEN;
1214 break;
1215 case LIT:
1216 if (left == 0) goto inf_leave;
1217 *put++ = (unsigned char)(state->length);
1218 left--;
1219 state->mode = LEN;
1220 break;
1221 case CHECK:
1222 if (state->wrap) {
1223 NEEDBITS(32);
1224 out -= left;
1225 strm->total_out += out;
1226 state->total += out;
1227 if ((state->wrap & 4) && out)
1228 strm->adler = state->check =
1229 UPDATE_CHECK(state->check, put - out, out);
1230 out = left;
1231 if ((state->wrap & 4) && (
1232 #ifdef GUNZIP
1233 state->flags ? hold :
1234 #endif
1235 ZSWAP32(hold)) != state->check) {
1236 strm->msg = (char *)"incorrect data check";
1237 state->mode = BAD;
1238 break;
1239 }
1240 INITBITS();
1241 Tracev((stderr, "inflate: check matches trailer\n"));
1242 }
1243 #ifdef GUNZIP
1244 state->mode = LENGTH;
1245
1246 case LENGTH:
1247 if (state->wrap && state->flags) {
1248 NEEDBITS(32);
1249 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1250 strm->msg = (char *)"incorrect length check";
1251 state->mode = BAD;
1252 break;
1253 }
1254 INITBITS();
1255 Tracev((stderr, "inflate: length matches trailer\n"));
1256 }
1257 #endif
1258 state->mode = DONE;
1259
1260 case DONE:
1261 ret = Z_STREAM_END;
1262 goto inf_leave;
1263 case BAD:
1264 ret = Z_DATA_ERROR;
1265 goto inf_leave;
1266 case MEM:
1267 return Z_MEM_ERROR;
1268 case SYNC:
1269
1270 default:
1271 return Z_STREAM_ERROR;
1272 }
1273
1274
1275
1276
1277
1278
1279
1280 inf_leave:
1281 RESTORE();
1282 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1283 (state->mode < CHECK || flush != Z_FINISH)))
1284 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1285 state->mode = MEM;
1286 return Z_MEM_ERROR;
1287 }
1288 in -= strm->avail_in;
1289 out -= strm->avail_out;
1290 strm->total_in += in;
1291 strm->total_out += out;
1292 state->total += out;
1293 if ((state->wrap & 4) && out)
1294 strm->adler = state->check =
1295 UPDATE_CHECK(state->check, strm->next_out - out, out);
1296 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1297 (state->mode == TYPE ? 128 : 0) +
1298 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1299 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1300 ret = Z_BUF_ERROR;
1301 return ret;
1302 }
1303
1304 int ZEXPORT inflateEnd(strm)
1305 z_streamp strm;
1306 {
1307 struct inflate_state FAR *state;
1308 if (inflateStateCheck(strm))
1309 return Z_STREAM_ERROR;
1310 state = (struct inflate_state FAR *)strm->state;
1311 if (state->window != Z_NULL) ZFREE(strm, state->window);
1312 ZFREE(strm, strm->state);
1313 strm->state = Z_NULL;
1314 Tracev((stderr, "inflate: end\n"));
1315 return Z_OK;
1316 }
1317
1318 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1319 z_streamp strm;
1320 Bytef *dictionary;
1321 uInt *dictLength;
1322 {
1323 struct inflate_state FAR *state;
1324
1325
1326 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1327 state = (struct inflate_state FAR *)strm->state;
1328
1329
1330 if (state->whave && dictionary != Z_NULL) {
1331 zmemcpy(dictionary, state->window + state->wnext,
1332 state->whave - state->wnext);
1333 zmemcpy(dictionary + state->whave - state->wnext,
1334 state->window, state->wnext);
1335 }
1336 if (dictLength != Z_NULL)
1337 *dictLength = state->whave;
1338 return Z_OK;
1339 }
1340
1341 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1342 z_streamp strm;
1343 const Bytef *dictionary;
1344 uInt dictLength;
1345 {
1346 struct inflate_state FAR *state;
1347 unsigned long dictid;
1348 int ret;
1349
1350
1351 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1352 state = (struct inflate_state FAR *)strm->state;
1353 if (state->wrap != 0 && state->mode != DICT)
1354 return Z_STREAM_ERROR;
1355
1356
1357 if (state->mode == DICT) {
1358 dictid = adler32(0L, Z_NULL, 0);
1359 dictid = adler32(dictid, dictionary, dictLength);
1360 if (dictid != state->check)
1361 return Z_DATA_ERROR;
1362 }
1363
1364
1365
1366 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1367 if (ret) {
1368 state->mode = MEM;
1369 return Z_MEM_ERROR;
1370 }
1371 state->havedict = 1;
1372 Tracev((stderr, "inflate: dictionary set\n"));
1373 return Z_OK;
1374 }
1375
1376 int ZEXPORT inflateGetHeader(strm, head)
1377 z_streamp strm;
1378 gz_headerp head;
1379 {
1380 struct inflate_state FAR *state;
1381
1382
1383 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1384 state = (struct inflate_state FAR *)strm->state;
1385 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1386
1387
1388 state->head = head;
1389 head->done = 0;
1390 return Z_OK;
1391 }
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 local unsigned syncsearch(have, buf, len)
1405 unsigned FAR *have;
1406 const unsigned char FAR *buf;
1407 unsigned len;
1408 {
1409 unsigned got;
1410 unsigned next;
1411
1412 got = *have;
1413 next = 0;
1414 while (next < len && got < 4) {
1415 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1416 got++;
1417 else if (buf[next])
1418 got = 0;
1419 else
1420 got = 4 - got;
1421 next++;
1422 }
1423 *have = got;
1424 return next;
1425 }
1426
1427 int ZEXPORT inflateSync(strm)
1428 z_streamp strm;
1429 {
1430 unsigned len;
1431 int flags;
1432 unsigned long in, out;
1433 unsigned char buf[4];
1434 struct inflate_state FAR *state;
1435
1436
1437 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1438 state = (struct inflate_state FAR *)strm->state;
1439 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1440
1441
1442 if (state->mode != SYNC) {
1443 state->mode = SYNC;
1444 state->hold <<= state->bits & 7;
1445 state->bits -= state->bits & 7;
1446 len = 0;
1447 while (state->bits >= 8) {
1448 buf[len++] = (unsigned char)(state->hold);
1449 state->hold >>= 8;
1450 state->bits -= 8;
1451 }
1452 state->have = 0;
1453 syncsearch(&(state->have), buf, len);
1454 }
1455
1456
1457 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1458 strm->avail_in -= len;
1459 strm->next_in += len;
1460 strm->total_in += len;
1461
1462
1463 if (state->have != 4) return Z_DATA_ERROR;
1464 if (state->flags == -1)
1465 state->wrap = 0;
1466 else
1467 state->wrap &= ~4;
1468 flags = state->flags;
1469 in = strm->total_in; out = strm->total_out;
1470 inflateReset(strm);
1471 strm->total_in = in; strm->total_out = out;
1472 state->flags = flags;
1473 state->mode = TYPE;
1474 return Z_OK;
1475 }
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485 int ZEXPORT inflateSyncPoint(strm)
1486 z_streamp strm;
1487 {
1488 struct inflate_state FAR *state;
1489
1490 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1491 state = (struct inflate_state FAR *)strm->state;
1492 return state->mode == STORED && state->bits == 0;
1493 }
1494
1495 int ZEXPORT inflateCopy(dest, source)
1496 z_streamp dest;
1497 z_streamp source;
1498 {
1499 struct inflate_state FAR *state;
1500 struct inflate_state FAR *copy;
1501 unsigned char FAR *window;
1502 unsigned wsize;
1503
1504
1505 if (inflateStateCheck(source) || dest == Z_NULL)
1506 return Z_STREAM_ERROR;
1507 state = (struct inflate_state FAR *)source->state;
1508
1509
1510 copy = (struct inflate_state FAR *)
1511 ZALLOC(source, 1, sizeof(struct inflate_state));
1512 if (copy == Z_NULL) return Z_MEM_ERROR;
1513 window = Z_NULL;
1514 if (state->window != Z_NULL) {
1515 window = (unsigned char FAR *)
1516 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1517 if (window == Z_NULL) {
1518 ZFREE(source, copy);
1519 return Z_MEM_ERROR;
1520 }
1521 }
1522
1523
1524 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1525 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1526 copy->strm = dest;
1527 if (state->lencode >= state->codes &&
1528 state->lencode <= state->codes + ENOUGH - 1) {
1529 copy->lencode = copy->codes + (state->lencode - state->codes);
1530 copy->distcode = copy->codes + (state->distcode - state->codes);
1531 }
1532 copy->next = copy->codes + (state->next - state->codes);
1533 if (window != Z_NULL) {
1534 wsize = 1U << state->wbits;
1535 zmemcpy(window, state->window, wsize);
1536 }
1537 copy->window = window;
1538 dest->state = (struct internal_state FAR *)copy;
1539 return Z_OK;
1540 }
1541
1542 int ZEXPORT inflateUndermine(strm, subvert)
1543 z_streamp strm;
1544 int subvert;
1545 {
1546 struct inflate_state FAR *state;
1547
1548 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1549 state = (struct inflate_state FAR *)strm->state;
1550 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1551 state->sane = !subvert;
1552 return Z_OK;
1553 #else
1554 (void)subvert;
1555 state->sane = 1;
1556 return Z_DATA_ERROR;
1557 #endif
1558 }
1559
1560 int ZEXPORT inflateValidate(strm, check)
1561 z_streamp strm;
1562 int check;
1563 {
1564 struct inflate_state FAR *state;
1565
1566 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1567 state = (struct inflate_state FAR *)strm->state;
1568 if (check && state->wrap)
1569 state->wrap |= 4;
1570 else
1571 state->wrap &= ~4;
1572 return Z_OK;
1573 }
1574
1575 long ZEXPORT inflateMark(strm)
1576 z_streamp strm;
1577 {
1578 struct inflate_state FAR *state;
1579
1580 if (inflateStateCheck(strm))
1581 return -(1L << 16);
1582 state = (struct inflate_state FAR *)strm->state;
1583 return (long)(((unsigned long)((long)state->back)) << 16) +
1584 (state->mode == COPY ? state->length :
1585 (state->mode == MATCH ? state->was - state->length : 0));
1586 }
1587
1588 unsigned long ZEXPORT inflateCodesUsed(strm)
1589 z_streamp strm;
1590 {
1591 struct inflate_state FAR *state;
1592 if (inflateStateCheck(strm)) return (unsigned long)-1;
1593 state = (struct inflate_state FAR *)strm->state;
1594 return (unsigned long)(state->next - state->codes);
1595 }