This source file includes following definitions.
- zlib_inflate_workspacesize
- zlib_inflateReset
- zlib_inflateInit2
- zlib_fixedtables
- zlib_updatewindow
- zlib_inflateSyncPacket
- zlib_inflate
- zlib_inflateEnd
- zlib_inflateIncomp
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
18 int zlib_inflate_workspacesize(void)
19 {
20 return sizeof(struct inflate_workspace);
21 }
22
23 int 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 int 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
78 static 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
101 static 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
146 static 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
317 int 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
386 case DICT:
387 if (state->havedict == 0) {
388 RESTORE();
389 return Z_NEED_DICT;
390 }
391 strm->adler = state->check = zlib_adler32(0L, NULL, 0);
392 state->mode = TYPE;
393
394 case TYPE:
395 if (flush == Z_BLOCK) goto inf_leave;
396
397 case TYPEDO:
398 if (state->last) {
399 BYTEBITS();
400 state->mode = CHECK;
401 break;
402 }
403 NEEDBITS(3);
404 state->last = BITS(1);
405 DROPBITS(1);
406 switch (BITS(2)) {
407 case 0:
408 state->mode = STORED;
409 break;
410 case 1:
411 zlib_fixedtables(state);
412 state->mode = LEN;
413 break;
414 case 2:
415 state->mode = TABLE;
416 break;
417 case 3:
418 strm->msg = (char *)"invalid block type";
419 state->mode = BAD;
420 }
421 DROPBITS(2);
422 break;
423 case STORED:
424 BYTEBITS();
425 NEEDBITS(32);
426 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
427 strm->msg = (char *)"invalid stored block lengths";
428 state->mode = BAD;
429 break;
430 }
431 state->length = (unsigned)hold & 0xffff;
432 INITBITS();
433 state->mode = COPY;
434
435 case COPY:
436 copy = state->length;
437 if (copy) {
438 if (copy > have) copy = have;
439 if (copy > left) copy = left;
440 if (copy == 0) goto inf_leave;
441 memcpy(put, next, copy);
442 have -= copy;
443 next += copy;
444 left -= copy;
445 put += copy;
446 state->length -= copy;
447 break;
448 }
449 state->mode = TYPE;
450 break;
451 case TABLE:
452 NEEDBITS(14);
453 state->nlen = BITS(5) + 257;
454 DROPBITS(5);
455 state->ndist = BITS(5) + 1;
456 DROPBITS(5);
457 state->ncode = BITS(4) + 4;
458 DROPBITS(4);
459 #ifndef PKZIP_BUG_WORKAROUND
460 if (state->nlen > 286 || state->ndist > 30) {
461 strm->msg = (char *)"too many length or distance symbols";
462 state->mode = BAD;
463 break;
464 }
465 #endif
466 state->have = 0;
467 state->mode = LENLENS;
468
469 case LENLENS:
470 while (state->have < state->ncode) {
471 NEEDBITS(3);
472 state->lens[order[state->have++]] = (unsigned short)BITS(3);
473 DROPBITS(3);
474 }
475 while (state->have < 19)
476 state->lens[order[state->have++]] = 0;
477 state->next = state->codes;
478 state->lencode = (code const *)(state->next);
479 state->lenbits = 7;
480 ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
481 &(state->lenbits), state->work);
482 if (ret) {
483 strm->msg = (char *)"invalid code lengths set";
484 state->mode = BAD;
485 break;
486 }
487 state->have = 0;
488 state->mode = CODELENS;
489
490 case CODELENS:
491 while (state->have < state->nlen + state->ndist) {
492 for (;;) {
493 this = state->lencode[BITS(state->lenbits)];
494 if ((unsigned)(this.bits) <= bits) break;
495 PULLBYTE();
496 }
497 if (this.val < 16) {
498 NEEDBITS(this.bits);
499 DROPBITS(this.bits);
500 state->lens[state->have++] = this.val;
501 }
502 else {
503 if (this.val == 16) {
504 NEEDBITS(this.bits + 2);
505 DROPBITS(this.bits);
506 if (state->have == 0) {
507 strm->msg = (char *)"invalid bit length repeat";
508 state->mode = BAD;
509 break;
510 }
511 len = state->lens[state->have - 1];
512 copy = 3 + BITS(2);
513 DROPBITS(2);
514 }
515 else if (this.val == 17) {
516 NEEDBITS(this.bits + 3);
517 DROPBITS(this.bits);
518 len = 0;
519 copy = 3 + BITS(3);
520 DROPBITS(3);
521 }
522 else {
523 NEEDBITS(this.bits + 7);
524 DROPBITS(this.bits);
525 len = 0;
526 copy = 11 + BITS(7);
527 DROPBITS(7);
528 }
529 if (state->have + copy > state->nlen + state->ndist) {
530 strm->msg = (char *)"invalid bit length repeat";
531 state->mode = BAD;
532 break;
533 }
534 while (copy--)
535 state->lens[state->have++] = (unsigned short)len;
536 }
537 }
538
539
540 if (state->mode == BAD) break;
541
542
543 state->next = state->codes;
544 state->lencode = (code const *)(state->next);
545 state->lenbits = 9;
546 ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
547 &(state->lenbits), state->work);
548 if (ret) {
549 strm->msg = (char *)"invalid literal/lengths set";
550 state->mode = BAD;
551 break;
552 }
553 state->distcode = (code const *)(state->next);
554 state->distbits = 6;
555 ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
556 &(state->next), &(state->distbits), state->work);
557 if (ret) {
558 strm->msg = (char *)"invalid distances set";
559 state->mode = BAD;
560 break;
561 }
562 state->mode = LEN;
563
564 case LEN:
565 if (have >= 6 && left >= 258) {
566 RESTORE();
567 inflate_fast(strm, out);
568 LOAD();
569 break;
570 }
571 for (;;) {
572 this = state->lencode[BITS(state->lenbits)];
573 if ((unsigned)(this.bits) <= bits) break;
574 PULLBYTE();
575 }
576 if (this.op && (this.op & 0xf0) == 0) {
577 last = this;
578 for (;;) {
579 this = state->lencode[last.val +
580 (BITS(last.bits + last.op) >> last.bits)];
581 if ((unsigned)(last.bits + this.bits) <= bits) break;
582 PULLBYTE();
583 }
584 DROPBITS(last.bits);
585 }
586 DROPBITS(this.bits);
587 state->length = (unsigned)this.val;
588 if ((int)(this.op) == 0) {
589 state->mode = LIT;
590 break;
591 }
592 if (this.op & 32) {
593 state->mode = TYPE;
594 break;
595 }
596 if (this.op & 64) {
597 strm->msg = (char *)"invalid literal/length code";
598 state->mode = BAD;
599 break;
600 }
601 state->extra = (unsigned)(this.op) & 15;
602 state->mode = LENEXT;
603
604 case LENEXT:
605 if (state->extra) {
606 NEEDBITS(state->extra);
607 state->length += BITS(state->extra);
608 DROPBITS(state->extra);
609 }
610 state->mode = DIST;
611
612 case DIST:
613 for (;;) {
614 this = state->distcode[BITS(state->distbits)];
615 if ((unsigned)(this.bits) <= bits) break;
616 PULLBYTE();
617 }
618 if ((this.op & 0xf0) == 0) {
619 last = this;
620 for (;;) {
621 this = state->distcode[last.val +
622 (BITS(last.bits + last.op) >> last.bits)];
623 if ((unsigned)(last.bits + this.bits) <= bits) break;
624 PULLBYTE();
625 }
626 DROPBITS(last.bits);
627 }
628 DROPBITS(this.bits);
629 if (this.op & 64) {
630 strm->msg = (char *)"invalid distance code";
631 state->mode = BAD;
632 break;
633 }
634 state->offset = (unsigned)this.val;
635 state->extra = (unsigned)(this.op) & 15;
636 state->mode = DISTEXT;
637
638 case DISTEXT:
639 if (state->extra) {
640 NEEDBITS(state->extra);
641 state->offset += BITS(state->extra);
642 DROPBITS(state->extra);
643 }
644 #ifdef INFLATE_STRICT
645 if (state->offset > state->dmax) {
646 strm->msg = (char *)"invalid distance too far back";
647 state->mode = BAD;
648 break;
649 }
650 #endif
651 if (state->offset > state->whave + out - left) {
652 strm->msg = (char *)"invalid distance too far back";
653 state->mode = BAD;
654 break;
655 }
656 state->mode = MATCH;
657
658 case MATCH:
659 if (left == 0) goto inf_leave;
660 copy = out - left;
661 if (state->offset > copy) {
662 copy = state->offset - copy;
663 if (copy > state->write) {
664 copy -= state->write;
665 from = state->window + (state->wsize - copy);
666 }
667 else
668 from = state->window + (state->write - copy);
669 if (copy > state->length) copy = state->length;
670 }
671 else {
672 from = put - state->offset;
673 copy = state->length;
674 }
675 if (copy > left) copy = left;
676 left -= copy;
677 state->length -= copy;
678 do {
679 *put++ = *from++;
680 } while (--copy);
681 if (state->length == 0) state->mode = LEN;
682 break;
683 case LIT:
684 if (left == 0) goto inf_leave;
685 *put++ = (unsigned char)(state->length);
686 left--;
687 state->mode = LEN;
688 break;
689 case CHECK:
690 if (state->wrap) {
691 NEEDBITS(32);
692 out -= left;
693 strm->total_out += out;
694 state->total += out;
695 if (out)
696 strm->adler = state->check =
697 UPDATE(state->check, put - out, out);
698 out = left;
699 if ((
700 REVERSE(hold)) != state->check) {
701 strm->msg = (char *)"incorrect data check";
702 state->mode = BAD;
703 break;
704 }
705 INITBITS();
706 }
707 state->mode = DONE;
708
709 case DONE:
710 ret = Z_STREAM_END;
711 goto inf_leave;
712 case BAD:
713 ret = Z_DATA_ERROR;
714 goto inf_leave;
715 case MEM:
716 return Z_MEM_ERROR;
717 case SYNC:
718 default:
719 return Z_STREAM_ERROR;
720 }
721
722
723
724
725
726
727 inf_leave:
728 RESTORE();
729 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
730 zlib_updatewindow(strm, out);
731
732 in -= strm->avail_in;
733 out -= strm->avail_out;
734 strm->total_in += in;
735 strm->total_out += out;
736 state->total += out;
737 if (state->wrap && out)
738 strm->adler = state->check =
739 UPDATE(state->check, strm->next_out - out, out);
740
741 strm->data_type = state->bits + (state->last ? 64 : 0) +
742 (state->mode == TYPE ? 128 : 0);
743
744 if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
745 strm->avail_out != 0 && strm->avail_in == 0)
746 return zlib_inflateSyncPacket(strm);
747
748 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
749 ret = Z_BUF_ERROR;
750
751 return ret;
752 }
753
754 int zlib_inflateEnd(z_streamp strm)
755 {
756 if (strm == NULL || strm->state == NULL)
757 return Z_STREAM_ERROR;
758 return Z_OK;
759 }
760
761
762
763
764
765
766
767
768
769 int zlib_inflateIncomp(z_stream *z)
770 {
771 struct inflate_state *state = (struct inflate_state *)z->state;
772 Byte *saved_no = z->next_out;
773 uInt saved_ao = z->avail_out;
774
775 if (state->mode != TYPE && state->mode != HEAD)
776 return Z_DATA_ERROR;
777
778
779 z->avail_out = 0;
780 z->next_out = (unsigned char*)z->next_in + z->avail_in;
781
782 zlib_updatewindow(z, z->avail_in);
783
784
785 z->avail_out = saved_ao;
786 z->next_out = saved_no;
787
788 z->adler = state->check =
789 UPDATE(state->check, z->next_in, z->avail_in);
790
791 z->total_out += z->avail_in;
792 z->total_in += z->avail_in;
793 z->next_in += z->avail_in;
794 state->total += z->avail_in;
795 z->avail_in = 0;
796
797 return Z_OK;
798 }