File indexing completed on 2025-05-11 08:24:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "zutil.h"
0014 #include "inftrees.h"
0015 #include "inflate.h"
0016 #include "inffast.h"
0017
0018
0019 local void fixedtables OF((struct inflate_state FAR *state));
0020
0021
0022
0023
0024
0025
0026
0027
0028 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
0029 z_streamp strm;
0030 int windowBits;
0031 unsigned char FAR *window;
0032 const char *version;
0033 int stream_size;
0034 {
0035 struct inflate_state FAR *state;
0036
0037 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
0038 stream_size != (int)(sizeof(z_stream)))
0039 return Z_VERSION_ERROR;
0040 if (strm == Z_NULL || window == Z_NULL ||
0041 windowBits < 8 || windowBits > 15)
0042 return Z_STREAM_ERROR;
0043 strm->msg = Z_NULL;
0044 if (strm->zalloc == (alloc_func)0) {
0045 #ifdef Z_SOLO
0046 return Z_STREAM_ERROR;
0047 #else
0048 strm->zalloc = zcalloc;
0049 strm->opaque = (voidpf)0;
0050 #endif
0051 }
0052 if (strm->zfree == (free_func)0)
0053 #ifdef Z_SOLO
0054 return Z_STREAM_ERROR;
0055 #else
0056 strm->zfree = zcfree;
0057 #endif
0058 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
0059 sizeof(struct inflate_state));
0060 if (state == Z_NULL) return Z_MEM_ERROR;
0061 Tracev((stderr, "inflate: allocated\n"));
0062 strm->state = (struct internal_state FAR *)state;
0063 state->dmax = 32768U;
0064 state->wbits = (uInt)windowBits;
0065 state->wsize = 1U << windowBits;
0066 state->window = window;
0067 state->wnext = 0;
0068 state->whave = 0;
0069 state->sane = 1;
0070 return Z_OK;
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 local void fixedtables(state)
0084 struct inflate_state FAR *state;
0085 {
0086 #ifdef BUILDFIXED
0087 static int virgin = 1;
0088 static code *lenfix, *distfix;
0089 static code fixed[544];
0090
0091
0092 if (virgin) {
0093 unsigned sym, bits;
0094 static code *next;
0095
0096
0097 sym = 0;
0098 while (sym < 144) state->lens[sym++] = 8;
0099 while (sym < 256) state->lens[sym++] = 9;
0100 while (sym < 280) state->lens[sym++] = 7;
0101 while (sym < 288) state->lens[sym++] = 8;
0102 next = fixed;
0103 lenfix = next;
0104 bits = 9;
0105 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
0106
0107
0108 sym = 0;
0109 while (sym < 32) state->lens[sym++] = 5;
0110 distfix = next;
0111 bits = 5;
0112 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
0113
0114
0115 virgin = 0;
0116 }
0117 #else
0118 # include "inffixed.h"
0119 #endif
0120 state->lencode = lenfix;
0121 state->lenbits = 9;
0122 state->distcode = distfix;
0123 state->distbits = 5;
0124 }
0125
0126
0127
0128
0129 #define LOAD() \
0130 do { \
0131 put = strm->next_out; \
0132 left = strm->avail_out; \
0133 next = strm->next_in; \
0134 have = strm->avail_in; \
0135 hold = state->hold; \
0136 bits = state->bits; \
0137 } while (0)
0138
0139
0140 #define RESTORE() \
0141 do { \
0142 strm->next_out = put; \
0143 strm->avail_out = left; \
0144 strm->next_in = next; \
0145 strm->avail_in = have; \
0146 state->hold = hold; \
0147 state->bits = bits; \
0148 } while (0)
0149
0150
0151 #define INITBITS() \
0152 do { \
0153 hold = 0; \
0154 bits = 0; \
0155 } while (0)
0156
0157
0158
0159 #define PULL() \
0160 do { \
0161 if (have == 0) { \
0162 have = in(in_desc, &next); \
0163 if (have == 0) { \
0164 next = Z_NULL; \
0165 ret = Z_BUF_ERROR; \
0166 goto inf_leave; \
0167 } \
0168 } \
0169 } while (0)
0170
0171
0172
0173 #define PULLBYTE() \
0174 do { \
0175 PULL(); \
0176 have--; \
0177 hold += (unsigned long)(*next++) << bits; \
0178 bits += 8; \
0179 } while (0)
0180
0181
0182
0183
0184 #define NEEDBITS(n) \
0185 do { \
0186 while (bits < (unsigned)(n)) \
0187 PULLBYTE(); \
0188 } while (0)
0189
0190
0191 #define BITS(n) \
0192 ((unsigned)hold & ((1U << (n)) - 1))
0193
0194
0195 #define DROPBITS(n) \
0196 do { \
0197 hold >>= (n); \
0198 bits -= (unsigned)(n); \
0199 } while (0)
0200
0201
0202 #define BYTEBITS() \
0203 do { \
0204 hold >>= bits & 7; \
0205 bits -= bits & 7; \
0206 } while (0)
0207
0208
0209
0210
0211 #define ROOM() \
0212 do { \
0213 if (left == 0) { \
0214 put = state->window; \
0215 left = state->wsize; \
0216 state->whave = left; \
0217 if (out(out_desc, put, left)) { \
0218 ret = Z_BUF_ERROR; \
0219 goto inf_leave; \
0220 } \
0221 } \
0222 } while (0)
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
0252 z_streamp strm;
0253 in_func in;
0254 void FAR *in_desc;
0255 out_func out;
0256 void FAR *out_desc;
0257 {
0258 struct inflate_state FAR *state;
0259 z_const unsigned char FAR *next;
0260 unsigned char FAR *put;
0261 unsigned have, left;
0262 unsigned long hold;
0263 unsigned bits;
0264 unsigned copy;
0265 unsigned char FAR *from;
0266 code here;
0267 code last;
0268 unsigned len;
0269 int ret;
0270 static const unsigned short order[19] =
0271 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0272
0273
0274 if (strm == Z_NULL || strm->state == Z_NULL)
0275 return Z_STREAM_ERROR;
0276 state = (struct inflate_state FAR *)strm->state;
0277
0278
0279 strm->msg = Z_NULL;
0280 state->mode = TYPE;
0281 state->last = 0;
0282 state->whave = 0;
0283 next = strm->next_in;
0284 have = next != Z_NULL ? strm->avail_in : 0;
0285 hold = 0;
0286 bits = 0;
0287 put = state->window;
0288 left = state->wsize;
0289
0290
0291 for (;;)
0292 switch (state->mode) {
0293 case TYPE:
0294
0295 if (state->last) {
0296 BYTEBITS();
0297 state->mode = DONE;
0298 break;
0299 }
0300 NEEDBITS(3);
0301 state->last = BITS(1);
0302 DROPBITS(1);
0303 switch (BITS(2)) {
0304 case 0:
0305 Tracev((stderr, "inflate: stored block%s\n",
0306 state->last ? " (last)" : ""));
0307 state->mode = STORED;
0308 break;
0309 case 1:
0310 fixedtables(state);
0311 Tracev((stderr, "inflate: fixed codes block%s\n",
0312 state->last ? " (last)" : ""));
0313 state->mode = LEN;
0314 break;
0315 case 2:
0316 Tracev((stderr, "inflate: dynamic codes block%s\n",
0317 state->last ? " (last)" : ""));
0318 state->mode = TABLE;
0319 break;
0320 case 3:
0321 strm->msg = (char *)"invalid block type";
0322 state->mode = BAD;
0323 }
0324 DROPBITS(2);
0325 break;
0326
0327 case STORED:
0328
0329 BYTEBITS();
0330 NEEDBITS(32);
0331 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
0332 strm->msg = (char *)"invalid stored block lengths";
0333 state->mode = BAD;
0334 break;
0335 }
0336 state->length = (unsigned)hold & 0xffff;
0337 Tracev((stderr, "inflate: stored length %u\n",
0338 state->length));
0339 INITBITS();
0340
0341
0342 while (state->length != 0) {
0343 copy = state->length;
0344 PULL();
0345 ROOM();
0346 if (copy > have) copy = have;
0347 if (copy > left) copy = left;
0348 zmemcpy(put, next, copy);
0349 have -= copy;
0350 next += copy;
0351 left -= copy;
0352 put += copy;
0353 state->length -= copy;
0354 }
0355 Tracev((stderr, "inflate: stored end\n"));
0356 state->mode = TYPE;
0357 break;
0358
0359 case TABLE:
0360
0361 NEEDBITS(14);
0362 state->nlen = BITS(5) + 257;
0363 DROPBITS(5);
0364 state->ndist = BITS(5) + 1;
0365 DROPBITS(5);
0366 state->ncode = BITS(4) + 4;
0367 DROPBITS(4);
0368 #ifndef PKZIP_BUG_WORKAROUND
0369 if (state->nlen > 286 || state->ndist > 30) {
0370 strm->msg = (char *)"too many length or distance symbols";
0371 state->mode = BAD;
0372 break;
0373 }
0374 #endif
0375 Tracev((stderr, "inflate: table sizes ok\n"));
0376
0377
0378 state->have = 0;
0379 while (state->have < state->ncode) {
0380 NEEDBITS(3);
0381 state->lens[order[state->have++]] = (unsigned short)BITS(3);
0382 DROPBITS(3);
0383 }
0384 while (state->have < 19)
0385 state->lens[order[state->have++]] = 0;
0386 state->next = state->codes;
0387 state->lencode = (code const FAR *)(state->next);
0388 state->lenbits = 7;
0389 ret = inflate_table(CODES, state->lens, 19, &(state->next),
0390 &(state->lenbits), state->work);
0391 if (ret) {
0392 strm->msg = (char *)"invalid code lengths set";
0393 state->mode = BAD;
0394 break;
0395 }
0396 Tracev((stderr, "inflate: code lengths ok\n"));
0397
0398
0399 state->have = 0;
0400 while (state->have < state->nlen + state->ndist) {
0401 for (;;) {
0402 here = state->lencode[BITS(state->lenbits)];
0403 if ((unsigned)(here.bits) <= bits) break;
0404 PULLBYTE();
0405 }
0406 if (here.val < 16) {
0407 DROPBITS(here.bits);
0408 state->lens[state->have++] = here.val;
0409 }
0410 else {
0411 if (here.val == 16) {
0412 NEEDBITS(here.bits + 2);
0413 DROPBITS(here.bits);
0414 if (state->have == 0) {
0415 strm->msg = (char *)"invalid bit length repeat";
0416 state->mode = BAD;
0417 break;
0418 }
0419 len = (unsigned)(state->lens[state->have - 1]);
0420 copy = 3 + BITS(2);
0421 DROPBITS(2);
0422 }
0423 else if (here.val == 17) {
0424 NEEDBITS(here.bits + 3);
0425 DROPBITS(here.bits);
0426 len = 0;
0427 copy = 3 + BITS(3);
0428 DROPBITS(3);
0429 }
0430 else {
0431 NEEDBITS(here.bits + 7);
0432 DROPBITS(here.bits);
0433 len = 0;
0434 copy = 11 + BITS(7);
0435 DROPBITS(7);
0436 }
0437 if (state->have + copy > state->nlen + state->ndist) {
0438 strm->msg = (char *)"invalid bit length repeat";
0439 state->mode = BAD;
0440 break;
0441 }
0442 while (copy--)
0443 state->lens[state->have++] = (unsigned short)len;
0444 }
0445 }
0446
0447
0448 if (state->mode == BAD) break;
0449
0450
0451 if (state->lens[256] == 0) {
0452 strm->msg = (char *)"invalid code -- missing end-of-block";
0453 state->mode = BAD;
0454 break;
0455 }
0456
0457
0458
0459
0460 state->next = state->codes;
0461 state->lencode = (code const FAR *)(state->next);
0462 state->lenbits = 9;
0463 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
0464 &(state->lenbits), state->work);
0465 if (ret) {
0466 strm->msg = (char *)"invalid literal/lengths set";
0467 state->mode = BAD;
0468 break;
0469 }
0470 state->distcode = (code const FAR *)(state->next);
0471 state->distbits = 6;
0472 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
0473 &(state->next), &(state->distbits), state->work);
0474 if (ret) {
0475 strm->msg = (char *)"invalid distances set";
0476 state->mode = BAD;
0477 break;
0478 }
0479 Tracev((stderr, "inflate: codes ok\n"));
0480 state->mode = LEN;
0481
0482
0483 case LEN:
0484
0485 if (have >= 6 && left >= 258) {
0486 RESTORE();
0487 if (state->whave < state->wsize)
0488 state->whave = state->wsize - left;
0489 inflate_fast(strm, state->wsize);
0490 LOAD();
0491 break;
0492 }
0493
0494
0495 for (;;) {
0496 here = state->lencode[BITS(state->lenbits)];
0497 if ((unsigned)(here.bits) <= bits) break;
0498 PULLBYTE();
0499 }
0500 if (here.op && (here.op & 0xf0) == 0) {
0501 last = here;
0502 for (;;) {
0503 here = state->lencode[last.val +
0504 (BITS(last.bits + last.op) >> last.bits)];
0505 if ((unsigned)(last.bits + here.bits) <= bits) break;
0506 PULLBYTE();
0507 }
0508 DROPBITS(last.bits);
0509 }
0510 DROPBITS(here.bits);
0511 state->length = (unsigned)here.val;
0512
0513
0514 if (here.op == 0) {
0515 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
0516 "inflate: literal '%c'\n" :
0517 "inflate: literal 0x%02x\n", here.val));
0518 ROOM();
0519 *put++ = (unsigned char)(state->length);
0520 left--;
0521 state->mode = LEN;
0522 break;
0523 }
0524
0525
0526 if (here.op & 32) {
0527 Tracevv((stderr, "inflate: end of block\n"));
0528 state->mode = TYPE;
0529 break;
0530 }
0531
0532
0533 if (here.op & 64) {
0534 strm->msg = (char *)"invalid literal/length code";
0535 state->mode = BAD;
0536 break;
0537 }
0538
0539
0540 state->extra = (unsigned)(here.op) & 15;
0541 if (state->extra != 0) {
0542 NEEDBITS(state->extra);
0543 state->length += BITS(state->extra);
0544 DROPBITS(state->extra);
0545 }
0546 Tracevv((stderr, "inflate: length %u\n", state->length));
0547
0548
0549 for (;;) {
0550 here = state->distcode[BITS(state->distbits)];
0551 if ((unsigned)(here.bits) <= bits) break;
0552 PULLBYTE();
0553 }
0554 if ((here.op & 0xf0) == 0) {
0555 last = here;
0556 for (;;) {
0557 here = state->distcode[last.val +
0558 (BITS(last.bits + last.op) >> last.bits)];
0559 if ((unsigned)(last.bits + here.bits) <= bits) break;
0560 PULLBYTE();
0561 }
0562 DROPBITS(last.bits);
0563 }
0564 DROPBITS(here.bits);
0565 if (here.op & 64) {
0566 strm->msg = (char *)"invalid distance code";
0567 state->mode = BAD;
0568 break;
0569 }
0570 state->offset = (unsigned)here.val;
0571
0572
0573 state->extra = (unsigned)(here.op) & 15;
0574 if (state->extra != 0) {
0575 NEEDBITS(state->extra);
0576 state->offset += BITS(state->extra);
0577 DROPBITS(state->extra);
0578 }
0579 if (state->offset > state->wsize - (state->whave < state->wsize ?
0580 left : 0)) {
0581 strm->msg = (char *)"invalid distance too far back";
0582 state->mode = BAD;
0583 break;
0584 }
0585 Tracevv((stderr, "inflate: distance %u\n", state->offset));
0586
0587
0588 do {
0589 ROOM();
0590 copy = state->wsize - state->offset;
0591 if (copy < left) {
0592 from = put + copy;
0593 copy = left - copy;
0594 }
0595 else {
0596 from = put - state->offset;
0597 copy = left;
0598 }
0599 if (copy > state->length) copy = state->length;
0600 state->length -= copy;
0601 left -= copy;
0602 do {
0603 *put++ = *from++;
0604 } while (--copy);
0605 } while (state->length != 0);
0606 break;
0607
0608 case DONE:
0609
0610 ret = Z_STREAM_END;
0611 goto inf_leave;
0612
0613 case BAD:
0614 ret = Z_DATA_ERROR;
0615 goto inf_leave;
0616
0617 default:
0618
0619 ret = Z_STREAM_ERROR;
0620 goto inf_leave;
0621 }
0622
0623
0624 inf_leave:
0625 if (left < state->wsize) {
0626 if (out(out_desc, state->window, state->wsize - left) &&
0627 ret == Z_STREAM_END)
0628 ret = Z_BUF_ERROR;
0629 }
0630 strm->next_in = next;
0631 strm->avail_in = have;
0632 return ret;
0633 }
0634
0635 int ZEXPORT inflateBackEnd(strm)
0636 z_streamp strm;
0637 {
0638 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
0639 return Z_STREAM_ERROR;
0640 ZFREE(strm, strm->state);
0641 strm->state = Z_NULL;
0642 Tracev((stderr, "inflate: end\n"));
0643 return Z_OK;
0644 }