0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/zutil.h>
0013 #include "inftrees.h"
0014 #include "inflate.h"
0015 #include "inffast.h"
0016 #include "infutil.h"
0017
0018
0019 #ifdef CONFIG_ZLIB_DFLTCC
0020 # include "../zlib_dfltcc/dfltcc.h"
0021 #else
0022 #define INFLATE_RESET_HOOK(strm) do {} while (0)
0023 #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
0024 #define INFLATE_NEED_UPDATEWINDOW(strm) 1
0025 #define INFLATE_NEED_CHECKSUM(strm) 1
0026 #endif
0027
0028 int zlib_inflate_workspacesize(void)
0029 {
0030 return sizeof(struct inflate_workspace);
0031 }
0032
0033 int zlib_inflateReset(z_streamp strm)
0034 {
0035 struct inflate_state *state;
0036
0037 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
0038 state = (struct inflate_state *)strm->state;
0039 strm->total_in = strm->total_out = state->total = 0;
0040 strm->msg = NULL;
0041 strm->adler = 1;
0042 state->mode = HEAD;
0043 state->last = 0;
0044 state->havedict = 0;
0045 state->dmax = 32768U;
0046 state->hold = 0;
0047 state->bits = 0;
0048 state->lencode = state->distcode = state->next = state->codes;
0049
0050
0051 state->wsize = 1U << state->wbits;
0052 state->write = 0;
0053 state->whave = 0;
0054
0055 INFLATE_RESET_HOOK(strm);
0056 return Z_OK;
0057 }
0058
0059 int zlib_inflateInit2(z_streamp strm, int windowBits)
0060 {
0061 struct inflate_state *state;
0062
0063 if (strm == NULL) return Z_STREAM_ERROR;
0064 strm->msg = NULL;
0065
0066 state = &WS(strm)->inflate_state;
0067 strm->state = (struct internal_state *)state;
0068
0069 if (windowBits < 0) {
0070 state->wrap = 0;
0071 windowBits = -windowBits;
0072 }
0073 else {
0074 state->wrap = (windowBits >> 4) + 1;
0075 }
0076 if (windowBits < 8 || windowBits > 15) {
0077 return Z_STREAM_ERROR;
0078 }
0079 state->wbits = (unsigned)windowBits;
0080 #ifdef CONFIG_ZLIB_DFLTCC
0081
0082
0083
0084
0085 state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
0086 #else
0087 state->window = &WS(strm)->working_window[0];
0088 #endif
0089
0090 return zlib_inflateReset(strm);
0091 }
0092
0093
0094
0095
0096
0097 static void zlib_fixedtables(struct inflate_state *state)
0098 {
0099 # include "inffixed.h"
0100 state->lencode = lenfix;
0101 state->lenbits = 9;
0102 state->distcode = distfix;
0103 state->distbits = 5;
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 static void zlib_updatewindow(z_streamp strm, unsigned out)
0121 {
0122 struct inflate_state *state;
0123 unsigned copy, dist;
0124
0125 state = (struct inflate_state *)strm->state;
0126
0127
0128 copy = out - strm->avail_out;
0129 if (copy >= state->wsize) {
0130 memcpy(state->window, strm->next_out - state->wsize, state->wsize);
0131 state->write = 0;
0132 state->whave = state->wsize;
0133 }
0134 else {
0135 dist = state->wsize - state->write;
0136 if (dist > copy) dist = copy;
0137 memcpy(state->window + state->write, strm->next_out - copy, dist);
0138 copy -= dist;
0139 if (copy) {
0140 memcpy(state->window, strm->next_out - copy, copy);
0141 state->write = copy;
0142 state->whave = state->wsize;
0143 }
0144 else {
0145 state->write += dist;
0146 if (state->write == state->wsize) state->write = 0;
0147 if (state->whave < state->wsize) state->whave += dist;
0148 }
0149 }
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 static int zlib_inflateSyncPacket(z_streamp strm)
0166 {
0167 struct inflate_state *state;
0168
0169 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
0170 state = (struct inflate_state *)strm->state;
0171
0172 if (state->mode == STORED && state->bits == 0) {
0173 state->mode = TYPE;
0174 return Z_OK;
0175 }
0176 return Z_DATA_ERROR;
0177 }
0178
0179
0180
0181
0182 #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
0183
0184
0185 #define LOAD() \
0186 do { \
0187 put = strm->next_out; \
0188 left = strm->avail_out; \
0189 next = strm->next_in; \
0190 have = strm->avail_in; \
0191 hold = state->hold; \
0192 bits = state->bits; \
0193 } while (0)
0194
0195
0196 #define RESTORE() \
0197 do { \
0198 strm->next_out = put; \
0199 strm->avail_out = left; \
0200 strm->next_in = next; \
0201 strm->avail_in = have; \
0202 state->hold = hold; \
0203 state->bits = bits; \
0204 } while (0)
0205
0206
0207 #define INITBITS() \
0208 do { \
0209 hold = 0; \
0210 bits = 0; \
0211 } while (0)
0212
0213
0214
0215 #define PULLBYTE() \
0216 do { \
0217 if (have == 0) goto inf_leave; \
0218 have--; \
0219 hold += (unsigned long)(*next++) << bits; \
0220 bits += 8; \
0221 } while (0)
0222
0223
0224
0225 #define NEEDBITS(n) \
0226 do { \
0227 while (bits < (unsigned)(n)) \
0228 PULLBYTE(); \
0229 } while (0)
0230
0231
0232 #define BITS(n) \
0233 ((unsigned)hold & ((1U << (n)) - 1))
0234
0235
0236 #define DROPBITS(n) \
0237 do { \
0238 hold >>= (n); \
0239 bits -= (unsigned)(n); \
0240 } while (0)
0241
0242
0243 #define BYTEBITS() \
0244 do { \
0245 hold >>= bits & 7; \
0246 bits -= bits & 7; \
0247 } while (0)
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 int zlib_inflate(z_streamp strm, int flush)
0332 {
0333 struct inflate_state *state;
0334 const unsigned char *next;
0335 unsigned char *put;
0336 unsigned have, left;
0337 unsigned long hold;
0338 unsigned bits;
0339 unsigned in, out;
0340 unsigned copy;
0341 unsigned char *from;
0342 code this;
0343 code last;
0344 unsigned len;
0345 int ret;
0346 static const unsigned short order[19] =
0347 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0348
0349
0350
0351
0352 if (strm == NULL || strm->state == NULL ||
0353 (strm->next_in == NULL && strm->avail_in != 0))
0354 return Z_STREAM_ERROR;
0355
0356 state = (struct inflate_state *)strm->state;
0357
0358 if (state->mode == TYPE) state->mode = TYPEDO;
0359 LOAD();
0360 in = have;
0361 out = left;
0362 ret = Z_OK;
0363 for (;;)
0364 switch (state->mode) {
0365 case HEAD:
0366 if (state->wrap == 0) {
0367 state->mode = TYPEDO;
0368 break;
0369 }
0370 NEEDBITS(16);
0371 if (
0372 ((BITS(8) << 8) + (hold >> 8)) % 31) {
0373 strm->msg = (char *)"incorrect header check";
0374 state->mode = BAD;
0375 break;
0376 }
0377 if (BITS(4) != Z_DEFLATED) {
0378 strm->msg = (char *)"unknown compression method";
0379 state->mode = BAD;
0380 break;
0381 }
0382 DROPBITS(4);
0383 len = BITS(4) + 8;
0384 if (len > state->wbits) {
0385 strm->msg = (char *)"invalid window size";
0386 state->mode = BAD;
0387 break;
0388 }
0389 state->dmax = 1U << len;
0390 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
0391 state->mode = hold & 0x200 ? DICTID : TYPE;
0392 INITBITS();
0393 break;
0394 case DICTID:
0395 NEEDBITS(32);
0396 strm->adler = state->check = REVERSE(hold);
0397 INITBITS();
0398 state->mode = DICT;
0399 fallthrough;
0400 case DICT:
0401 if (state->havedict == 0) {
0402 RESTORE();
0403 return Z_NEED_DICT;
0404 }
0405 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
0406 state->mode = TYPE;
0407 fallthrough;
0408 case TYPE:
0409 if (flush == Z_BLOCK) goto inf_leave;
0410 fallthrough;
0411 case TYPEDO:
0412 INFLATE_TYPEDO_HOOK(strm, flush);
0413 if (state->last) {
0414 BYTEBITS();
0415 state->mode = CHECK;
0416 break;
0417 }
0418 NEEDBITS(3);
0419 state->last = BITS(1);
0420 DROPBITS(1);
0421 switch (BITS(2)) {
0422 case 0:
0423 state->mode = STORED;
0424 break;
0425 case 1:
0426 zlib_fixedtables(state);
0427 state->mode = LEN;
0428 break;
0429 case 2:
0430 state->mode = TABLE;
0431 break;
0432 case 3:
0433 strm->msg = (char *)"invalid block type";
0434 state->mode = BAD;
0435 }
0436 DROPBITS(2);
0437 break;
0438 case STORED:
0439 BYTEBITS();
0440 NEEDBITS(32);
0441 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
0442 strm->msg = (char *)"invalid stored block lengths";
0443 state->mode = BAD;
0444 break;
0445 }
0446 state->length = (unsigned)hold & 0xffff;
0447 INITBITS();
0448 state->mode = COPY;
0449 fallthrough;
0450 case COPY:
0451 copy = state->length;
0452 if (copy) {
0453 if (copy > have) copy = have;
0454 if (copy > left) copy = left;
0455 if (copy == 0) goto inf_leave;
0456 memcpy(put, next, copy);
0457 have -= copy;
0458 next += copy;
0459 left -= copy;
0460 put += copy;
0461 state->length -= copy;
0462 break;
0463 }
0464 state->mode = TYPE;
0465 break;
0466 case TABLE:
0467 NEEDBITS(14);
0468 state->nlen = BITS(5) + 257;
0469 DROPBITS(5);
0470 state->ndist = BITS(5) + 1;
0471 DROPBITS(5);
0472 state->ncode = BITS(4) + 4;
0473 DROPBITS(4);
0474 #ifndef PKZIP_BUG_WORKAROUND
0475 if (state->nlen > 286 || state->ndist > 30) {
0476 strm->msg = (char *)"too many length or distance symbols";
0477 state->mode = BAD;
0478 break;
0479 }
0480 #endif
0481 state->have = 0;
0482 state->mode = LENLENS;
0483 fallthrough;
0484 case LENLENS:
0485 while (state->have < state->ncode) {
0486 NEEDBITS(3);
0487 state->lens[order[state->have++]] = (unsigned short)BITS(3);
0488 DROPBITS(3);
0489 }
0490 while (state->have < 19)
0491 state->lens[order[state->have++]] = 0;
0492 state->next = state->codes;
0493 state->lencode = (code const *)(state->next);
0494 state->lenbits = 7;
0495 ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
0496 &(state->lenbits), state->work);
0497 if (ret) {
0498 strm->msg = (char *)"invalid code lengths set";
0499 state->mode = BAD;
0500 break;
0501 }
0502 state->have = 0;
0503 state->mode = CODELENS;
0504 fallthrough;
0505 case CODELENS:
0506 while (state->have < state->nlen + state->ndist) {
0507 for (;;) {
0508 this = state->lencode[BITS(state->lenbits)];
0509 if ((unsigned)(this.bits) <= bits) break;
0510 PULLBYTE();
0511 }
0512 if (this.val < 16) {
0513 NEEDBITS(this.bits);
0514 DROPBITS(this.bits);
0515 state->lens[state->have++] = this.val;
0516 }
0517 else {
0518 if (this.val == 16) {
0519 NEEDBITS(this.bits + 2);
0520 DROPBITS(this.bits);
0521 if (state->have == 0) {
0522 strm->msg = (char *)"invalid bit length repeat";
0523 state->mode = BAD;
0524 break;
0525 }
0526 len = state->lens[state->have - 1];
0527 copy = 3 + BITS(2);
0528 DROPBITS(2);
0529 }
0530 else if (this.val == 17) {
0531 NEEDBITS(this.bits + 3);
0532 DROPBITS(this.bits);
0533 len = 0;
0534 copy = 3 + BITS(3);
0535 DROPBITS(3);
0536 }
0537 else {
0538 NEEDBITS(this.bits + 7);
0539 DROPBITS(this.bits);
0540 len = 0;
0541 copy = 11 + BITS(7);
0542 DROPBITS(7);
0543 }
0544 if (state->have + copy > state->nlen + state->ndist) {
0545 strm->msg = (char *)"invalid bit length repeat";
0546 state->mode = BAD;
0547 break;
0548 }
0549 while (copy--)
0550 state->lens[state->have++] = (unsigned short)len;
0551 }
0552 }
0553
0554
0555 if (state->mode == BAD) break;
0556
0557
0558 state->next = state->codes;
0559 state->lencode = (code const *)(state->next);
0560 state->lenbits = 9;
0561 ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
0562 &(state->lenbits), state->work);
0563 if (ret) {
0564 strm->msg = (char *)"invalid literal/lengths set";
0565 state->mode = BAD;
0566 break;
0567 }
0568 state->distcode = (code const *)(state->next);
0569 state->distbits = 6;
0570 ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
0571 &(state->next), &(state->distbits), state->work);
0572 if (ret) {
0573 strm->msg = (char *)"invalid distances set";
0574 state->mode = BAD;
0575 break;
0576 }
0577 state->mode = LEN;
0578 fallthrough;
0579 case LEN:
0580 if (have >= 6 && left >= 258) {
0581 RESTORE();
0582 inflate_fast(strm, out);
0583 LOAD();
0584 break;
0585 }
0586 for (;;) {
0587 this = state->lencode[BITS(state->lenbits)];
0588 if ((unsigned)(this.bits) <= bits) break;
0589 PULLBYTE();
0590 }
0591 if (this.op && (this.op & 0xf0) == 0) {
0592 last = this;
0593 for (;;) {
0594 this = state->lencode[last.val +
0595 (BITS(last.bits + last.op) >> last.bits)];
0596 if ((unsigned)(last.bits + this.bits) <= bits) break;
0597 PULLBYTE();
0598 }
0599 DROPBITS(last.bits);
0600 }
0601 DROPBITS(this.bits);
0602 state->length = (unsigned)this.val;
0603 if ((int)(this.op) == 0) {
0604 state->mode = LIT;
0605 break;
0606 }
0607 if (this.op & 32) {
0608 state->mode = TYPE;
0609 break;
0610 }
0611 if (this.op & 64) {
0612 strm->msg = (char *)"invalid literal/length code";
0613 state->mode = BAD;
0614 break;
0615 }
0616 state->extra = (unsigned)(this.op) & 15;
0617 state->mode = LENEXT;
0618 fallthrough;
0619 case LENEXT:
0620 if (state->extra) {
0621 NEEDBITS(state->extra);
0622 state->length += BITS(state->extra);
0623 DROPBITS(state->extra);
0624 }
0625 state->mode = DIST;
0626 fallthrough;
0627 case DIST:
0628 for (;;) {
0629 this = state->distcode[BITS(state->distbits)];
0630 if ((unsigned)(this.bits) <= bits) break;
0631 PULLBYTE();
0632 }
0633 if ((this.op & 0xf0) == 0) {
0634 last = this;
0635 for (;;) {
0636 this = state->distcode[last.val +
0637 (BITS(last.bits + last.op) >> last.bits)];
0638 if ((unsigned)(last.bits + this.bits) <= bits) break;
0639 PULLBYTE();
0640 }
0641 DROPBITS(last.bits);
0642 }
0643 DROPBITS(this.bits);
0644 if (this.op & 64) {
0645 strm->msg = (char *)"invalid distance code";
0646 state->mode = BAD;
0647 break;
0648 }
0649 state->offset = (unsigned)this.val;
0650 state->extra = (unsigned)(this.op) & 15;
0651 state->mode = DISTEXT;
0652 fallthrough;
0653 case DISTEXT:
0654 if (state->extra) {
0655 NEEDBITS(state->extra);
0656 state->offset += BITS(state->extra);
0657 DROPBITS(state->extra);
0658 }
0659 #ifdef INFLATE_STRICT
0660 if (state->offset > state->dmax) {
0661 strm->msg = (char *)"invalid distance too far back";
0662 state->mode = BAD;
0663 break;
0664 }
0665 #endif
0666 if (state->offset > state->whave + out - left) {
0667 strm->msg = (char *)"invalid distance too far back";
0668 state->mode = BAD;
0669 break;
0670 }
0671 state->mode = MATCH;
0672 fallthrough;
0673 case MATCH:
0674 if (left == 0) goto inf_leave;
0675 copy = out - left;
0676 if (state->offset > copy) {
0677 copy = state->offset - copy;
0678 if (copy > state->write) {
0679 copy -= state->write;
0680 from = state->window + (state->wsize - copy);
0681 }
0682 else
0683 from = state->window + (state->write - copy);
0684 if (copy > state->length) copy = state->length;
0685 }
0686 else {
0687 from = put - state->offset;
0688 copy = state->length;
0689 }
0690 if (copy > left) copy = left;
0691 left -= copy;
0692 state->length -= copy;
0693 do {
0694 *put++ = *from++;
0695 } while (--copy);
0696 if (state->length == 0) state->mode = LEN;
0697 break;
0698 case LIT:
0699 if (left == 0) goto inf_leave;
0700 *put++ = (unsigned char)(state->length);
0701 left--;
0702 state->mode = LEN;
0703 break;
0704 case CHECK:
0705 if (state->wrap) {
0706 NEEDBITS(32);
0707 out -= left;
0708 strm->total_out += out;
0709 state->total += out;
0710 if (INFLATE_NEED_CHECKSUM(strm) && out)
0711 strm->adler = state->check =
0712 UPDATE(state->check, put - out, out);
0713 out = left;
0714 if ((
0715 REVERSE(hold)) != state->check) {
0716 strm->msg = (char *)"incorrect data check";
0717 state->mode = BAD;
0718 break;
0719 }
0720 INITBITS();
0721 }
0722 state->mode = DONE;
0723 fallthrough;
0724 case DONE:
0725 ret = Z_STREAM_END;
0726 goto inf_leave;
0727 case BAD:
0728 ret = Z_DATA_ERROR;
0729 goto inf_leave;
0730 case MEM:
0731 return Z_MEM_ERROR;
0732 case SYNC:
0733 default:
0734 return Z_STREAM_ERROR;
0735 }
0736
0737
0738
0739
0740
0741
0742 inf_leave:
0743 RESTORE();
0744 if (INFLATE_NEED_UPDATEWINDOW(strm) &&
0745 (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
0746 zlib_updatewindow(strm, out);
0747
0748 in -= strm->avail_in;
0749 out -= strm->avail_out;
0750 strm->total_in += in;
0751 strm->total_out += out;
0752 state->total += out;
0753 if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
0754 strm->adler = state->check =
0755 UPDATE(state->check, strm->next_out - out, out);
0756
0757 strm->data_type = state->bits + (state->last ? 64 : 0) +
0758 (state->mode == TYPE ? 128 : 0);
0759
0760 if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
0761 strm->avail_out != 0 && strm->avail_in == 0)
0762 return zlib_inflateSyncPacket(strm);
0763
0764 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
0765 ret = Z_BUF_ERROR;
0766
0767 return ret;
0768 }
0769
0770 int zlib_inflateEnd(z_streamp strm)
0771 {
0772 if (strm == NULL || strm->state == NULL)
0773 return Z_STREAM_ERROR;
0774 return Z_OK;
0775 }
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785 int zlib_inflateIncomp(z_stream *z)
0786 {
0787 struct inflate_state *state = (struct inflate_state *)z->state;
0788 Byte *saved_no = z->next_out;
0789 uInt saved_ao = z->avail_out;
0790
0791 if (state->mode != TYPE && state->mode != HEAD)
0792 return Z_DATA_ERROR;
0793
0794
0795 z->avail_out = 0;
0796 z->next_out = (unsigned char*)z->next_in + z->avail_in;
0797
0798 zlib_updatewindow(z, z->avail_in);
0799
0800
0801 z->avail_out = saved_ao;
0802 z->next_out = saved_no;
0803
0804 z->adler = state->check =
0805 UPDATE(state->check, z->next_in, z->avail_in);
0806
0807 z->total_out += z->avail_in;
0808 z->total_in += z->avail_in;
0809 z->next_in += z->avail_in;
0810 state->total += z->avail_in;
0811 z->avail_in = 0;
0812
0813 return Z_OK;
0814 }