1
2
3
4
5
6
7
8
9
10
11
12#include <linux/zutil.h>
13#include "inftrees.h"
14#include "inflate.h"
15#include "inffast.h"
16#include "infutil.h"
17
18int zlib_inflate_workspacesize(void)
19{
20 return sizeof(struct inflate_workspace);
21}
22
23int zlib_inflateReset(z_streamp strm)
24{
25 struct inflate_state *state;
26
27 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
28 state = (struct inflate_state *)strm->state;
29 strm->total_in = strm->total_out = state->total = 0;
30 strm->msg = NULL;
31 strm->adler = 1;
32 state->mode = HEAD;
33 state->last = 0;
34 state->havedict = 0;
35 state->dmax = 32768U;
36 state->hold = 0;
37 state->bits = 0;
38 state->lencode = state->distcode = state->next = state->codes;
39
40
41 state->wsize = 1U << state->wbits;
42 state->write = 0;
43 state->whave = 0;
44
45 return Z_OK;
46}
47
48int zlib_inflateInit2(z_streamp strm, int windowBits)
49{
50 struct inflate_state *state;
51
52 if (strm == NULL) return Z_STREAM_ERROR;
53 strm->msg = NULL;
54
55 state = &WS(strm)->inflate_state;
56 strm->state = (struct internal_state *)state;
57
58 if (windowBits < 0) {
59 state->wrap = 0;
60 windowBits = -windowBits;
61 }
62 else {
63 state->wrap = (windowBits >> 4) + 1;
64 }
65 if (windowBits < 8 || windowBits > 15) {
66 return Z_STREAM_ERROR;
67 }
68 state->wbits = (unsigned)windowBits;
69 state->window = &WS(strm)->working_window[0];
70
71 return zlib_inflateReset(strm);
72}
73
74
75
76
77
78static void zlib_fixedtables(struct inflate_state *state)
79{
80# include "inffixed.h"
81 state->lencode = lenfix;
82 state->lenbits = 9;
83 state->distcode = distfix;
84 state->distbits = 5;
85}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101static void zlib_updatewindow(z_streamp strm, unsigned out)
102{
103 struct inflate_state *state;
104 unsigned copy, dist;
105
106 state = (struct inflate_state *)strm->state;
107
108
109 copy = out - strm->avail_out;
110 if (copy >= state->wsize) {
111 memcpy(state->window, strm->next_out - state->wsize, state->wsize);
112 state->write = 0;
113 state->whave = state->wsize;
114 }
115 else {
116 dist = state->wsize - state->write;
117 if (dist > copy) dist = copy;
118 memcpy(state->window + state->write, strm->next_out - copy, dist);
119 copy -= dist;
120 if (copy) {
121 memcpy(state->window, strm->next_out - copy, copy);
122 state->write = copy;
123 state->whave = state->wsize;
124 }
125 else {
126 state->write += dist;
127 if (state->write == state->wsize) state->write = 0;
128 if (state->whave < state->wsize) state->whave += dist;
129 }
130 }
131}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146static int zlib_inflateSyncPacket(z_streamp strm)
147{
148 struct inflate_state *state;
149
150 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
151 state = (struct inflate_state *)strm->state;
152
153 if (state->mode == STORED && state->bits == 0) {
154 state->mode = TYPE;
155 return Z_OK;
156 }
157 return Z_DATA_ERROR;
158}
159
160
161
162
163#define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
164
165
166#define LOAD() \
167 do { \
168 put = strm->next_out; \
169 left = strm->avail_out; \
170 next = strm->next_in; \
171 have = strm->avail_in; \
172 hold = state->hold; \
173 bits = state->bits; \
174 } while (0)
175
176
177#define RESTORE() \
178 do { \
179 strm->next_out = put; \
180 strm->avail_out = left; \
181 strm->next_in = next; \
182 strm->avail_in = have; \
183 state->hold = hold; \
184 state->bits = bits; \
185 } while (0)
186
187
188#define INITBITS() \
189 do { \
190 hold = 0; \
191 bits = 0; \
192 } while (0)
193
194
195
196#define PULLBYTE() \
197 do { \
198 if (have == 0) goto inf_leave; \
199 have--; \
200 hold += (unsigned long)(*next++) << bits; \
201 bits += 8; \
202 } while (0)
203
204
205
206#define NEEDBITS(n) \
207 do { \
208 while (bits < (unsigned)(n)) \
209 PULLBYTE(); \
210 } while (0)
211
212
213#define BITS(n) \
214 ((unsigned)hold & ((1U << (n)) - 1))
215
216
217#define DROPBITS(n) \
218 do { \
219 hold >>= (n); \
220 bits -= (unsigned)(n); \
221 } while (0)
222
223
224#define BYTEBITS() \
225 do { \
226 hold >>= bits & 7; \
227 bits -= bits & 7; \
228 } while (0)
229
230
231#define REVERSE(q) \
232 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
233 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317int zlib_inflate(z_streamp strm, int flush)
318{
319 struct inflate_state *state;
320 const unsigned char *next;
321 unsigned char *put;
322 unsigned have, left;
323 unsigned long hold;
324 unsigned bits;
325 unsigned in, out;
326 unsigned copy;
327 unsigned char *from;
328 code this;
329 code last;
330 unsigned len;
331 int ret;
332 static const unsigned short order[19] =
333 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
334
335
336
337
338 if (strm == NULL || strm->state == NULL ||
339 (strm->next_in == NULL && strm->avail_in != 0))
340 return Z_STREAM_ERROR;
341
342 state = (struct inflate_state *)strm->state;
343
344 if (state->mode == TYPE) state->mode = TYPEDO;
345 LOAD();
346 in = have;
347 out = left;
348 ret = Z_OK;
349 for (;;)
350 switch (state->mode) {
351 case HEAD:
352 if (state->wrap == 0) {
353 state->mode = TYPEDO;
354 break;
355 }
356 NEEDBITS(16);
357 if (
358 ((BITS(8) << 8) + (hold >> 8)) % 31) {
359 strm->msg = (char *)"incorrect header check";
360 state->mode = BAD;
361 break;
362 }
363 if (BITS(4) != Z_DEFLATED) {
364 strm->msg = (char *)"unknown compression method";
365 state->mode = BAD;
366 break;
367 }
368 DROPBITS(4);
369 len = BITS(4) + 8;
370 if (len > state->wbits) {
371 strm->msg = (char *)"invalid window size";
372 state->mode = BAD;
373 break;
374 }
375 state->dmax = 1U << len;
376 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
377 state->mode = hold & 0x200 ? DICTID : TYPE;
378 INITBITS();
379 break;
380 case DICTID:
381 NEEDBITS(32);
382 strm->adler = state->check = REVERSE(hold);
383 INITBITS();
384 state->mode = DICT;
385 case DICT:
386 if (state->havedict == 0) {
387 RESTORE();
388 return Z_NEED_DICT;
389 }
390 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
391 state->mode = TYPE;
392 case TYPE:
393 if (flush == Z_BLOCK) goto inf_leave;
394 case TYPEDO:
395 if (state->last) {
396 BYTEBITS();
397 state->mode = CHECK;
398 break;
399 }
400 NEEDBITS(3);
401 state->last = BITS(1);
402 DROPBITS(1);
403 switch (BITS(2)) {
404 case 0:
405 state->mode = STORED;
406 break;
407 case 1:
408 zlib_fixedtables(state);
409 state->mode = LEN;
410 break;
411 case 2:
412 state->mode = TABLE;
413 break;
414 case 3:
415 strm->msg = (char *)"invalid block type";
416 state->mode = BAD;
417 }
418 DROPBITS(2);
419 break;
420 case STORED:
421 BYTEBITS();
422 NEEDBITS(32);
423 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
424 strm->msg = (char *)"invalid stored block lengths";
425 state->mode = BAD;
426 break;
427 }
428 state->length = (unsigned)hold & 0xffff;
429 INITBITS();
430 state->mode = COPY;
431 case COPY:
432 copy = state->length;
433 if (copy) {
434 if (copy > have) copy = have;
435 if (copy > left) copy = left;
436 if (copy == 0) goto inf_leave;
437 memcpy(put, next, copy);
438 have -= copy;
439 next += copy;
440 left -= copy;
441 put += copy;
442 state->length -= copy;
443 break;
444 }
445 state->mode = TYPE;
446 break;
447 case TABLE:
448 NEEDBITS(14);
449 state->nlen = BITS(5) + 257;
450 DROPBITS(5);
451 state->ndist = BITS(5) + 1;
452 DROPBITS(5);
453 state->ncode = BITS(4) + 4;
454 DROPBITS(4);
455#ifndef PKZIP_BUG_WORKAROUND
456 if (state->nlen > 286 || state->ndist > 30) {
457 strm->msg = (char *)"too many length or distance symbols";
458 state->mode = BAD;
459 break;
460 }
461#endif
462 state->have = 0;
463 state->mode = LENLENS;
464 case LENLENS:
465 while (state->have < state->ncode) {
466 NEEDBITS(3);
467 state->lens[order[state->have++]] = (unsigned short)BITS(3);
468 DROPBITS(3);
469 }
470 while (state->have < 19)
471 state->lens[order[state->have++]] = 0;
472 state->next = state->codes;
473 state->lencode = (code const *)(state->next);
474 state->lenbits = 7;
475 ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
476 &(state->lenbits), state->work);
477 if (ret) {
478 strm->msg = (char *)"invalid code lengths set";
479 state->mode = BAD;
480 break;
481 }
482 state->have = 0;
483 state->mode = CODELENS;
484 case CODELENS:
485 while (state->have < state->nlen + state->ndist) {
486 for (;;) {
487 this = state->lencode[BITS(state->lenbits)];
488 if ((unsigned)(this.bits) <= bits) break;
489 PULLBYTE();
490 }
491 if (this.val < 16) {
492 NEEDBITS(this.bits);
493 DROPBITS(this.bits);
494 state->lens[state->have++] = this.val;
495 }
496 else {
497 if (this.val == 16) {
498 NEEDBITS(this.bits + 2);
499 DROPBITS(this.bits);
500 if (state->have == 0) {
501 strm->msg = (char *)"invalid bit length repeat";
502 state->mode = BAD;
503 break;
504 }
505 len = state->lens[state->have - 1];
506 copy = 3 + BITS(2);
507 DROPBITS(2);
508 }
509 else if (this.val == 17) {
510 NEEDBITS(this.bits + 3);
511 DROPBITS(this.bits);
512 len = 0;
513 copy = 3 + BITS(3);
514 DROPBITS(3);
515 }
516 else {
517 NEEDBITS(this.bits + 7);
518 DROPBITS(this.bits);
519 len = 0;
520 copy = 11 + BITS(7);
521 DROPBITS(7);
522 }
523 if (state->have + copy > state->nlen + state->ndist) {
524 strm->msg = (char *)"invalid bit length repeat";
525 state->mode = BAD;
526 break;
527 }
528 while (copy--)
529 state->lens[state->have++] = (unsigned short)len;
530 }
531 }
532
533
534 if (state->mode == BAD) break;
535
536
537 state->next = state->codes;
538 state->lencode = (code const *)(state->next);
539 state->lenbits = 9;
540 ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
541 &(state->lenbits), state->work);
542 if (ret) {
543 strm->msg = (char *)"invalid literal/lengths set";
544 state->mode = BAD;
545 break;
546 }
547 state->distcode = (code const *)(state->next);
548 state->distbits = 6;
549 ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
550 &(state->next), &(state->distbits), state->work);
551 if (ret) {
552 strm->msg = (char *)"invalid distances set";
553 state->mode = BAD;
554 break;
555 }
556 state->mode = LEN;
557 case LEN:
558 if (have >= 6 && left >= 258) {
559 RESTORE();
560 inflate_fast(strm, out);
561 LOAD();
562 break;
563 }
564 for (;;) {
565 this = state->lencode[BITS(state->lenbits)];
566 if ((unsigned)(this.bits) <= bits) break;
567 PULLBYTE();
568 }
569 if (this.op && (this.op & 0xf0) == 0) {
570 last = this;
571 for (;;) {
572 this = state->lencode[last.val +
573 (BITS(last.bits + last.op) >> last.bits)];
574 if ((unsigned)(last.bits + this.bits) <= bits) break;
575 PULLBYTE();
576 }
577 DROPBITS(last.bits);
578 }
579 DROPBITS(this.bits);
580 state->length = (unsigned)this.val;
581 if ((int)(this.op) == 0) {
582 state->mode = LIT;
583 break;
584 }
585 if (this.op & 32) {
586 state->mode = TYPE;
587 break;
588 }
589 if (this.op & 64) {
590 strm->msg = (char *)"invalid literal/length code";
591 state->mode = BAD;
592 break;
593 }
594 state->extra = (unsigned)(this.op) & 15;
595 state->mode = LENEXT;
596 case LENEXT:
597 if (state->extra) {
598 NEEDBITS(state->extra);
599 state->length += BITS(state->extra);
600 DROPBITS(state->extra);
601 }
602 state->mode = DIST;
603 case DIST:
604 for (;;) {
605 this = state->distcode[BITS(state->distbits)];
606 if ((unsigned)(this.bits) <= bits) break;
607 PULLBYTE();
608 }
609 if ((this.op & 0xf0) == 0) {
610 last = this;
611 for (;;) {
612 this = state->distcode[last.val +
613 (BITS(last.bits + last.op) >> last.bits)];
614 if ((unsigned)(last.bits + this.bits) <= bits) break;
615 PULLBYTE();
616 }
617 DROPBITS(last.bits);
618 }
619 DROPBITS(this.bits);
620 if (this.op & 64) {
621 strm->msg = (char *)"invalid distance code";
622 state->mode = BAD;
623 break;
624 }
625 state->offset = (unsigned)this.val;
626 state->extra = (unsigned)(this.op) & 15;
627 state->mode = DISTEXT;
628 case DISTEXT:
629 if (state->extra) {
630 NEEDBITS(state->extra);
631 state->offset += BITS(state->extra);
632 DROPBITS(state->extra);
633 }
634#ifdef INFLATE_STRICT
635 if (state->offset > state->dmax) {
636 strm->msg = (char *)"invalid distance too far back";
637 state->mode = BAD;
638 break;
639 }
640#endif
641 if (state->offset > state->whave + out - left) {
642 strm->msg = (char *)"invalid distance too far back";
643 state->mode = BAD;
644 break;
645 }
646 state->mode = MATCH;
647 case MATCH:
648 if (left == 0) goto inf_leave;
649 copy = out - left;
650 if (state->offset > copy) {
651 copy = state->offset - copy;
652 if (copy > state->write) {
653 copy -= state->write;
654 from = state->window + (state->wsize - copy);
655 }
656 else
657 from = state->window + (state->write - copy);
658 if (copy > state->length) copy = state->length;
659 }
660 else {
661 from = put - state->offset;
662 copy = state->length;
663 }
664 if (copy > left) copy = left;
665 left -= copy;
666 state->length -= copy;
667 do {
668 *put++ = *from++;
669 } while (--copy);
670 if (state->length == 0) state->mode = LEN;
671 break;
672 case LIT:
673 if (left == 0) goto inf_leave;
674 *put++ = (unsigned char)(state->length);
675 left--;
676 state->mode = LEN;
677 break;
678 case CHECK:
679 if (state->wrap) {
680 NEEDBITS(32);
681 out -= left;
682 strm->total_out += out;
683 state->total += out;
684 if (out)
685 strm->adler = state->check =
686 UPDATE(state->check, put - out, out);
687 out = left;
688 if ((
689 REVERSE(hold)) != state->check) {
690 strm->msg = (char *)"incorrect data check";
691 state->mode = BAD;
692 break;
693 }
694 INITBITS();
695 }
696 state->mode = DONE;
697 case DONE:
698 ret = Z_STREAM_END;
699 goto inf_leave;
700 case BAD:
701 ret = Z_DATA_ERROR;
702 goto inf_leave;
703 case MEM:
704 return Z_MEM_ERROR;
705 case SYNC:
706 default:
707 return Z_STREAM_ERROR;
708 }
709
710
711
712
713
714
715 inf_leave:
716 RESTORE();
717 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
718 zlib_updatewindow(strm, out);
719
720 in -= strm->avail_in;
721 out -= strm->avail_out;
722 strm->total_in += in;
723 strm->total_out += out;
724 state->total += out;
725 if (state->wrap && out)
726 strm->adler = state->check =
727 UPDATE(state->check, strm->next_out - out, out);
728
729 strm->data_type = state->bits + (state->last ? 64 : 0) +
730 (state->mode == TYPE ? 128 : 0);
731
732 if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
733 strm->avail_out != 0 && strm->avail_in == 0)
734 return zlib_inflateSyncPacket(strm);
735
736 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
737 ret = Z_BUF_ERROR;
738
739 return ret;
740}
741
742int zlib_inflateEnd(z_streamp strm)
743{
744 if (strm == NULL || strm->state == NULL)
745 return Z_STREAM_ERROR;
746 return Z_OK;
747}
748
749
750
751
752
753
754
755
756
757int zlib_inflateIncomp(z_stream *z)
758{
759 struct inflate_state *state = (struct inflate_state *)z->state;
760 Byte *saved_no = z->next_out;
761 uInt saved_ao = z->avail_out;
762
763 if (state->mode != TYPE && state->mode != HEAD)
764 return Z_DATA_ERROR;
765
766
767 z->avail_out = 0;
768 z->next_out = (unsigned char*)z->next_in + z->avail_in;
769
770 zlib_updatewindow(z, z->avail_in);
771
772
773 z->avail_out = saved_ao;
774 z->next_out = saved_no;
775
776 z->adler = state->check =
777 UPDATE(state->check, z->next_in, z->avail_in);
778
779 z->total_out += z->avail_in;
780 z->total_in += z->avail_in;
781 z->next_in += z->avail_in;
782 state->total += z->avail_in;
783 z->avail_in = 0;
784
785 return Z_OK;
786}
787