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
48#if 0
49int zlib_inflatePrime(z_streamp strm, int bits, int value)
50{
51 struct inflate_state *state;
52
53 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
54 state = (struct inflate_state *)strm->state;
55 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
56 value &= (1L << bits) - 1;
57 state->hold += value << state->bits;
58 state->bits += bits;
59 return Z_OK;
60}
61#endif
62
63int zlib_inflateInit2(z_streamp strm, int windowBits)
64{
65 struct inflate_state *state;
66
67 if (strm == NULL) return Z_STREAM_ERROR;
68 strm->msg = NULL;
69
70 state = &WS(strm)->inflate_state;
71 strm->state = (struct internal_state *)state;
72
73 if (windowBits < 0) {
74 state->wrap = 0;
75 windowBits = -windowBits;
76 }
77 else {
78 state->wrap = (windowBits >> 4) + 1;
79 }
80 if (windowBits < 8 || windowBits > 15) {
81 return Z_STREAM_ERROR;
82 }
83 state->wbits = (unsigned)windowBits;
84 state->window = &WS(strm)->working_window[0];
85
86 return zlib_inflateReset(strm);
87}
88
89
90
91
92
93static void zlib_fixedtables(struct inflate_state *state)
94{
95# include "inffixed.h"
96 state->lencode = lenfix;
97 state->lenbits = 9;
98 state->distcode = distfix;
99 state->distbits = 5;
100}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116static void zlib_updatewindow(z_streamp strm, unsigned out)
117{
118 struct inflate_state *state;
119 unsigned copy, dist;
120
121 state = (struct inflate_state *)strm->state;
122
123
124 copy = out - strm->avail_out;
125 if (copy >= state->wsize) {
126 memcpy(state->window, strm->next_out - state->wsize, state->wsize);
127 state->write = 0;
128 state->whave = state->wsize;
129 }
130 else {
131 dist = state->wsize - state->write;
132 if (dist > copy) dist = copy;
133 memcpy(state->window + state->write, strm->next_out - copy, dist);
134 copy -= dist;
135 if (copy) {
136 memcpy(state->window, strm->next_out - copy, copy);
137 state->write = copy;
138 state->whave = state->wsize;
139 }
140 else {
141 state->write += dist;
142 if (state->write == state->wsize) state->write = 0;
143 if (state->whave < state->wsize) state->whave += dist;
144 }
145 }
146}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161static int zlib_inflateSyncPacket(z_streamp strm)
162{
163 struct inflate_state *state;
164
165 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
166 state = (struct inflate_state *)strm->state;
167
168 if (state->mode == STORED && state->bits == 0) {
169 state->mode = TYPE;
170 return Z_OK;
171 }
172 return Z_DATA_ERROR;
173}
174
175
176
177
178#define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
179
180
181#define LOAD() \
182 do { \
183 put = strm->next_out; \
184 left = strm->avail_out; \
185 next = strm->next_in; \
186 have = strm->avail_in; \
187 hold = state->hold; \
188 bits = state->bits; \
189 } while (0)
190
191
192#define RESTORE() \
193 do { \
194 strm->next_out = put; \
195 strm->avail_out = left; \
196 strm->next_in = next; \
197 strm->avail_in = have; \
198 state->hold = hold; \
199 state->bits = bits; \
200 } while (0)
201
202
203#define INITBITS() \
204 do { \
205 hold = 0; \
206 bits = 0; \
207 } while (0)
208
209
210
211#define PULLBYTE() \
212 do { \
213 if (have == 0) goto inf_leave; \
214 have--; \
215 hold += (unsigned long)(*next++) << bits; \
216 bits += 8; \
217 } while (0)
218
219
220
221#define NEEDBITS(n) \
222 do { \
223 while (bits < (unsigned)(n)) \
224 PULLBYTE(); \
225 } while (0)
226
227
228#define BITS(n) \
229 ((unsigned)hold & ((1U << (n)) - 1))
230
231
232#define DROPBITS(n) \
233 do { \
234 hold >>= (n); \
235 bits -= (unsigned)(n); \
236 } while (0)
237
238
239#define BYTEBITS() \
240 do { \
241 hold >>= bits & 7; \
242 bits -= bits & 7; \
243 } while (0)
244
245
246#define REVERSE(q) \
247 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
248 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332int zlib_inflate(z_streamp strm, int flush)
333{
334 struct inflate_state *state;
335 const unsigned char *next;
336 unsigned char *put;
337 unsigned have, left;
338 unsigned long hold;
339 unsigned bits;
340 unsigned in, out;
341 unsigned copy;
342 unsigned char *from;
343 code this;
344 code last;
345 unsigned len;
346 int ret;
347 static const unsigned short order[19] =
348 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
349
350
351
352
353 if (strm == NULL || strm->state == NULL ||
354 (strm->next_in == NULL && strm->avail_in != 0))
355 return Z_STREAM_ERROR;
356
357 state = (struct inflate_state *)strm->state;
358
359 if (state->mode == TYPE) state->mode = TYPEDO;
360 LOAD();
361 in = have;
362 out = left;
363 ret = Z_OK;
364 for (;;)
365 switch (state->mode) {
366 case HEAD:
367 if (state->wrap == 0) {
368 state->mode = TYPEDO;
369 break;
370 }
371 NEEDBITS(16);
372 if (
373 ((BITS(8) << 8) + (hold >> 8)) % 31) {
374 strm->msg = (char *)"incorrect header check";
375 state->mode = BAD;
376 break;
377 }
378 if (BITS(4) != Z_DEFLATED) {
379 strm->msg = (char *)"unknown compression method";
380 state->mode = BAD;
381 break;
382 }
383 DROPBITS(4);
384 len = BITS(4) + 8;
385 if (len > state->wbits) {
386 strm->msg = (char *)"invalid window size";
387 state->mode = BAD;
388 break;
389 }
390 state->dmax = 1U << len;
391 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
392 state->mode = hold & 0x200 ? DICTID : TYPE;
393 INITBITS();
394 break;
395 case DICTID:
396 NEEDBITS(32);
397 strm->adler = state->check = REVERSE(hold);
398 INITBITS();
399 state->mode = DICT;
400 case DICT:
401 if (state->havedict == 0) {
402 RESTORE();
403 return Z_NEED_DICT;
404 }
405 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
406 state->mode = TYPE;
407 case TYPE:
408 if (flush == Z_BLOCK) goto inf_leave;
409 case TYPEDO:
410 if (state->last) {
411 BYTEBITS();
412 state->mode = CHECK;
413 break;
414 }
415 NEEDBITS(3);
416 state->last = BITS(1);
417 DROPBITS(1);
418 switch (BITS(2)) {
419 case 0:
420 state->mode = STORED;
421 break;
422 case 1:
423 zlib_fixedtables(state);
424 state->mode = LEN;
425 break;
426 case 2:
427 state->mode = TABLE;
428 break;
429 case 3:
430 strm->msg = (char *)"invalid block type";
431 state->mode = BAD;
432 }
433 DROPBITS(2);
434 break;
435 case STORED:
436 BYTEBITS();
437 NEEDBITS(32);
438 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
439 strm->msg = (char *)"invalid stored block lengths";
440 state->mode = BAD;
441 break;
442 }
443 state->length = (unsigned)hold & 0xffff;
444 INITBITS();
445 state->mode = COPY;
446 case COPY:
447 copy = state->length;
448 if (copy) {
449 if (copy > have) copy = have;
450 if (copy > left) copy = left;
451 if (copy == 0) goto inf_leave;
452 memcpy(put, next, copy);
453 have -= copy;
454 next += copy;
455 left -= copy;
456 put += copy;
457 state->length -= copy;
458 break;
459 }
460 state->mode = TYPE;
461 break;
462 case TABLE:
463 NEEDBITS(14);
464 state->nlen = BITS(5) + 257;
465 DROPBITS(5);
466 state->ndist = BITS(5) + 1;
467 DROPBITS(5);
468 state->ncode = BITS(4) + 4;
469 DROPBITS(4);
470#ifndef PKZIP_BUG_WORKAROUND
471 if (state->nlen > 286 || state->ndist > 30) {
472 strm->msg = (char *)"too many length or distance symbols";
473 state->mode = BAD;
474 break;
475 }
476#endif
477 state->have = 0;
478 state->mode = LENLENS;
479 case LENLENS:
480 while (state->have < state->ncode) {
481 NEEDBITS(3);
482 state->lens[order[state->have++]] = (unsigned short)BITS(3);
483 DROPBITS(3);
484 }
485 while (state->have < 19)
486 state->lens[order[state->have++]] = 0;
487 state->next = state->codes;
488 state->lencode = (code const *)(state->next);
489 state->lenbits = 7;
490 ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
491 &(state->lenbits), state->work);
492 if (ret) {
493 strm->msg = (char *)"invalid code lengths set";
494 state->mode = BAD;
495 break;
496 }
497 state->have = 0;
498 state->mode = CODELENS;
499 case CODELENS:
500 while (state->have < state->nlen + state->ndist) {
501 for (;;) {
502 this = state->lencode[BITS(state->lenbits)];
503 if ((unsigned)(this.bits) <= bits) break;
504 PULLBYTE();
505 }
506 if (this.val < 16) {
507 NEEDBITS(this.bits);
508 DROPBITS(this.bits);
509 state->lens[state->have++] = this.val;
510 }
511 else {
512 if (this.val == 16) {
513 NEEDBITS(this.bits + 2);
514 DROPBITS(this.bits);
515 if (state->have == 0) {
516 strm->msg = (char *)"invalid bit length repeat";
517 state->mode = BAD;
518 break;
519 }
520 len = state->lens[state->have - 1];
521 copy = 3 + BITS(2);
522 DROPBITS(2);
523 }
524 else if (this.val == 17) {
525 NEEDBITS(this.bits + 3);
526 DROPBITS(this.bits);
527 len = 0;
528 copy = 3 + BITS(3);
529 DROPBITS(3);
530 }
531 else {
532 NEEDBITS(this.bits + 7);
533 DROPBITS(this.bits);
534 len = 0;
535 copy = 11 + BITS(7);
536 DROPBITS(7);
537 }
538 if (state->have + copy > state->nlen + state->ndist) {
539 strm->msg = (char *)"invalid bit length repeat";
540 state->mode = BAD;
541 break;
542 }
543 while (copy--)
544 state->lens[state->have++] = (unsigned short)len;
545 }
546 }
547
548
549 if (state->mode == BAD) break;
550
551
552 state->next = state->codes;
553 state->lencode = (code const *)(state->next);
554 state->lenbits = 9;
555 ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
556 &(state->lenbits), state->work);
557 if (ret) {
558 strm->msg = (char *)"invalid literal/lengths set";
559 state->mode = BAD;
560 break;
561 }
562 state->distcode = (code const *)(state->next);
563 state->distbits = 6;
564 ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
565 &(state->next), &(state->distbits), state->work);
566 if (ret) {
567 strm->msg = (char *)"invalid distances set";
568 state->mode = BAD;
569 break;
570 }
571 state->mode = LEN;
572 case LEN:
573 if (have >= 6 && left >= 258) {
574 RESTORE();
575 inflate_fast(strm, out);
576 LOAD();
577 break;
578 }
579 for (;;) {
580 this = state->lencode[BITS(state->lenbits)];
581 if ((unsigned)(this.bits) <= bits) break;
582 PULLBYTE();
583 }
584 if (this.op && (this.op & 0xf0) == 0) {
585 last = this;
586 for (;;) {
587 this = state->lencode[last.val +
588 (BITS(last.bits + last.op) >> last.bits)];
589 if ((unsigned)(last.bits + this.bits) <= bits) break;
590 PULLBYTE();
591 }
592 DROPBITS(last.bits);
593 }
594 DROPBITS(this.bits);
595 state->length = (unsigned)this.val;
596 if ((int)(this.op) == 0) {
597 state->mode = LIT;
598 break;
599 }
600 if (this.op & 32) {
601 state->mode = TYPE;
602 break;
603 }
604 if (this.op & 64) {
605 strm->msg = (char *)"invalid literal/length code";
606 state->mode = BAD;
607 break;
608 }
609 state->extra = (unsigned)(this.op) & 15;
610 state->mode = LENEXT;
611 case LENEXT:
612 if (state->extra) {
613 NEEDBITS(state->extra);
614 state->length += BITS(state->extra);
615 DROPBITS(state->extra);
616 }
617 state->mode = DIST;
618 case DIST:
619 for (;;) {
620 this = state->distcode[BITS(state->distbits)];
621 if ((unsigned)(this.bits) <= bits) break;
622 PULLBYTE();
623 }
624 if ((this.op & 0xf0) == 0) {
625 last = this;
626 for (;;) {
627 this = state->distcode[last.val +
628 (BITS(last.bits + last.op) >> last.bits)];
629 if ((unsigned)(last.bits + this.bits) <= bits) break;
630 PULLBYTE();
631 }
632 DROPBITS(last.bits);
633 }
634 DROPBITS(this.bits);
635 if (this.op & 64) {
636 strm->msg = (char *)"invalid distance code";
637 state->mode = BAD;
638 break;
639 }
640 state->offset = (unsigned)this.val;
641 state->extra = (unsigned)(this.op) & 15;
642 state->mode = DISTEXT;
643 case DISTEXT:
644 if (state->extra) {
645 NEEDBITS(state->extra);
646 state->offset += BITS(state->extra);
647 DROPBITS(state->extra);
648 }
649#ifdef INFLATE_STRICT
650 if (state->offset > state->dmax) {
651 strm->msg = (char *)"invalid distance too far back";
652 state->mode = BAD;
653 break;
654 }
655#endif
656 if (state->offset > state->whave + out - left) {
657 strm->msg = (char *)"invalid distance too far back";
658 state->mode = BAD;
659 break;
660 }
661 state->mode = MATCH;
662 case MATCH:
663 if (left == 0) goto inf_leave;
664 copy = out - left;
665 if (state->offset > copy) {
666 copy = state->offset - copy;
667 if (copy > state->write) {
668 copy -= state->write;
669 from = state->window + (state->wsize - copy);
670 }
671 else
672 from = state->window + (state->write - copy);
673 if (copy > state->length) copy = state->length;
674 }
675 else {
676 from = put - state->offset;
677 copy = state->length;
678 }
679 if (copy > left) copy = left;
680 left -= copy;
681 state->length -= copy;
682 do {
683 *put++ = *from++;
684 } while (--copy);
685 if (state->length == 0) state->mode = LEN;
686 break;
687 case LIT:
688 if (left == 0) goto inf_leave;
689 *put++ = (unsigned char)(state->length);
690 left--;
691 state->mode = LEN;
692 break;
693 case CHECK:
694 if (state->wrap) {
695 NEEDBITS(32);
696 out -= left;
697 strm->total_out += out;
698 state->total += out;
699 if (out)
700 strm->adler = state->check =
701 UPDATE(state->check, put - out, out);
702 out = left;
703 if ((
704 REVERSE(hold)) != state->check) {
705 strm->msg = (char *)"incorrect data check";
706 state->mode = BAD;
707 break;
708 }
709 INITBITS();
710 }
711 state->mode = DONE;
712 case DONE:
713 ret = Z_STREAM_END;
714 goto inf_leave;
715 case BAD:
716 ret = Z_DATA_ERROR;
717 goto inf_leave;
718 case MEM:
719 return Z_MEM_ERROR;
720 case SYNC:
721 default:
722 return Z_STREAM_ERROR;
723 }
724
725
726
727
728
729
730 inf_leave:
731 RESTORE();
732 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
733 zlib_updatewindow(strm, out);
734
735 in -= strm->avail_in;
736 out -= strm->avail_out;
737 strm->total_in += in;
738 strm->total_out += out;
739 state->total += out;
740 if (state->wrap && out)
741 strm->adler = state->check =
742 UPDATE(state->check, strm->next_out - out, out);
743
744 strm->data_type = state->bits + (state->last ? 64 : 0) +
745 (state->mode == TYPE ? 128 : 0);
746
747 if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
748 strm->avail_out != 0 && strm->avail_in == 0)
749 return zlib_inflateSyncPacket(strm);
750
751 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
752 ret = Z_BUF_ERROR;
753
754 return ret;
755}
756
757int zlib_inflateEnd(z_streamp strm)
758{
759 if (strm == NULL || strm->state == NULL)
760 return Z_STREAM_ERROR;
761 return Z_OK;
762}
763
764#if 0
765int zlib_inflateSetDictionary(z_streamp strm, const Byte *dictionary,
766 uInt dictLength)
767{
768 struct inflate_state *state;
769 unsigned long id;
770
771
772 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
773 state = (struct inflate_state *)strm->state;
774 if (state->wrap != 0 && state->mode != DICT)
775 return Z_STREAM_ERROR;
776
777
778 if (state->mode == DICT) {
779 id = zlib_adler32(0L, NULL, 0);
780 id = zlib_adler32(id, dictionary, dictLength);
781 if (id != state->check)
782 return Z_DATA_ERROR;
783 }
784
785
786 zlib_updatewindow(strm, strm->avail_out);
787
788 if (dictLength > state->wsize) {
789 memcpy(state->window, dictionary + dictLength - state->wsize,
790 state->wsize);
791 state->whave = state->wsize;
792 }
793 else {
794 memcpy(state->window + state->wsize - dictLength, dictionary,
795 dictLength);
796 state->whave = dictLength;
797 }
798 state->havedict = 1;
799 return Z_OK;
800}
801#endif
802
803#if 0
804
805
806
807
808
809
810
811
812
813
814
815static unsigned zlib_syncsearch(unsigned *have, unsigned char *buf,
816 unsigned len)
817{
818 unsigned got;
819 unsigned next;
820
821 got = *have;
822 next = 0;
823 while (next < len && got < 4) {
824 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
825 got++;
826 else if (buf[next])
827 got = 0;
828 else
829 got = 4 - got;
830 next++;
831 }
832 *have = got;
833 return next;
834}
835#endif
836
837#if 0
838int zlib_inflateSync(z_streamp strm)
839{
840 unsigned len;
841 unsigned long in, out;
842 unsigned char buf[4];
843 struct inflate_state *state;
844
845
846 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
847 state = (struct inflate_state *)strm->state;
848 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
849
850
851 if (state->mode != SYNC) {
852 state->mode = SYNC;
853 state->hold <<= state->bits & 7;
854 state->bits -= state->bits & 7;
855 len = 0;
856 while (state->bits >= 8) {
857 buf[len++] = (unsigned char)(state->hold);
858 state->hold >>= 8;
859 state->bits -= 8;
860 }
861 state->have = 0;
862 zlib_syncsearch(&(state->have), buf, len);
863 }
864
865
866 len = zlib_syncsearch(&(state->have), strm->next_in, strm->avail_in);
867 strm->avail_in -= len;
868 strm->next_in += len;
869 strm->total_in += len;
870
871
872 if (state->have != 4) return Z_DATA_ERROR;
873 in = strm->total_in; out = strm->total_out;
874 zlib_inflateReset(strm);
875 strm->total_in = in; strm->total_out = out;
876 state->mode = TYPE;
877 return Z_OK;
878}
879#endif
880
881
882
883
884
885
886
887
888
889int zlib_inflateIncomp(z_stream *z)
890{
891 struct inflate_state *state = (struct inflate_state *)z->state;
892 Byte *saved_no = z->next_out;
893 uInt saved_ao = z->avail_out;
894
895 if (state->mode != TYPE && state->mode != HEAD)
896 return Z_DATA_ERROR;
897
898
899 z->avail_out = 0;
900 z->next_out = (unsigned char*)z->next_in + z->avail_in;
901
902 zlib_updatewindow(z, z->avail_in);
903
904
905 z->avail_out = saved_ao;
906 z->next_out = saved_no;
907
908 z->adler = state->check =
909 UPDATE(state->check, z->next_in, z->avail_in);
910
911 z->total_out += z->avail_in;
912 z->total_in += z->avail_in;
913 z->next_in += z->avail_in;
914 state->total += z->avail_in;
915 z->avail_in = 0;
916
917 return Z_OK;
918}
919