Lines Matching +full:input +full:- +full:mode

1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 Mark Adler
12 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; in inflateReset()
13 state = (struct inflate_state FAR *)strm->state; in inflateReset()
14 strm->total_in = strm->total_out = state->total = 0; in inflateReset()
15 strm->msg = Z_NULL; in inflateReset()
16 strm->adler = 1; /* to support ill-conceived Java test suite */ in inflateReset()
17 state->mode = HEAD; in inflateReset()
18 state->last = 0; in inflateReset()
19 state->havedict = 0; in inflateReset()
20 state->dmax = 32768U; in inflateReset()
21 state->head = Z_NULL; in inflateReset()
22 state->wsize = 0; in inflateReset()
23 state->whave = 0; in inflateReset()
24 state->write = 0; in inflateReset()
25 state->hold = 0; in inflateReset()
26 state->bits = 0; in inflateReset()
27 state->lencode = state->distcode = state->next = state->codes; in inflateReset()
42 strm->msg = Z_NULL; /* in case we return an error */ in inflateInit2_()
43 if (strm->zalloc == (alloc_func)0) { in inflateInit2_()
44 strm->zalloc = zcalloc; in inflateInit2_()
45 strm->opaque = (voidpf)0; in inflateInit2_()
47 if (strm->zfree == (free_func)0) strm->zfree = zcfree; in inflateInit2_()
52 strm->state = (struct internal_state FAR *)state; in inflateInit2_()
54 state->wrap = 0; in inflateInit2_()
55 windowBits = -windowBits; in inflateInit2_()
58 state->wrap = (windowBits >> 4) + 1; in inflateInit2_()
65 strm->state = Z_NULL; in inflateInit2_()
68 state->wbits = (unsigned)windowBits; in inflateInit2_()
69 state->window = Z_NULL; in inflateInit2_()
80 state->lencode = lenfix; in fixedtables()
81 state->lenbits = 9; in fixedtables()
82 state->distcode = distfix; in fixedtables()
83 state->distbits = 5; in fixedtables()
105 state = (struct inflate_state FAR *)strm->state; in updatewindow()
108 if (state->window == Z_NULL) { in updatewindow()
109 state->window = (unsigned char FAR *) in updatewindow()
110 ZALLOC(strm, 1U << state->wbits, in updatewindow()
112 if (state->window == Z_NULL) return 1; in updatewindow()
116 if (state->wsize == 0) { in updatewindow()
117 state->wsize = 1U << state->wbits; in updatewindow()
118 state->write = 0; in updatewindow()
119 state->whave = 0; in updatewindow()
122 /* copy state->wsize or less output bytes into the circular window */ in updatewindow()
123 copy = out - strm->avail_out; in updatewindow()
124 if (copy >= state->wsize) { in updatewindow()
125 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); in updatewindow()
126 state->write = 0; in updatewindow()
127 state->whave = state->wsize; in updatewindow()
130 dist = state->wsize - state->write; in updatewindow()
132 zmemcpy(state->window + state->write, strm->next_out - copy, dist); in updatewindow()
133 copy -= dist; in updatewindow()
135 zmemcpy(state->window, strm->next_out - copy, copy); in updatewindow()
136 state->write = copy; in updatewindow()
137 state->whave = state->wsize; in updatewindow()
140 state->write += dist; in updatewindow()
141 if (state->write == state->wsize) state->write = 0; in updatewindow()
142 if (state->whave < state->wsize) state->whave += dist; in updatewindow()
153 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
180 put = strm->next_out; \
181 left = strm->avail_out; \
182 next = strm->next_in; \
183 have = strm->avail_in; \
184 hold = state->hold; \
185 bits = state->bits; \
191 strm->next_out = put; \
192 strm->avail_out = left; \
193 strm->next_in = next; \
194 strm->avail_in = have; \
195 state->hold = hold; \
196 state->bits = bits; \
199 /* Clear the input bit accumulator */
206 /* Get a byte of input into the bit accumulator, or return from inflate()
207 if there is no input available. */
211 have--; \
217 not enough available input to do that, then return from inflate(). */
226 ((unsigned)hold & ((1U << (n)) - 1))
232 bits -= (unsigned)(n); \
239 bits -= bits & 7; \
242 /* Reverse the bytes in a 32-bit value */
248 inflate() uses a state machine to process as much input data and generate as
255 if (not enough input data or output space to make progress)
275 input left to load n bits into the accumulator, or it continues. BITS(n)
282 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
283 if there is no input available. The decoding of variable length codes uses
287 Some states loop until they get enough input, making sure that enough
312 Progress is defined as a change in either strm->avail_in or strm->avail_out.
320 strm->next_out, given the space available and the provided input--the effect
323 provides the effect documented in zlib.h for Z_FINISH when the entire input
331 unsigned char FAR *next; /* next input */ in inflate()
333 unsigned have, left; /* available input and output */ in inflate()
336 unsigned in, out; /* save starting available input and output */ in inflate()
349 if (strm == Z_NULL || strm->state == Z_NULL || in inflate()
350 (strm->next_in == Z_NULL && strm->avail_in != 0)) in inflate()
353 state = (struct inflate_state FAR *)strm->state; in inflate()
354 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ in inflate()
360 switch (state->mode) { in inflate()
362 if (state->wrap == 0) { in inflate()
363 state->mode = TYPEDO; in inflate()
368 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ in inflate()
369 state->check = crc32(0L, Z_NULL, 0); in inflate()
370 CRC2(state->check, hold); in inflate()
372 state->mode = FLAGS; in inflate()
375 state->flags = 0; /* expect zlib header */ in inflate()
376 if (state->head != Z_NULL) in inflate()
377 state->head->done = -1; in inflate()
378 if (!(state->wrap & 1) || /* check if zlib header allowed */ in inflate()
383 strm->msg = (char *)"incorrect header check"; in inflate()
384 state->mode = BAD; in inflate()
388 strm->msg = (char *)"unknown compression method"; in inflate()
389 state->mode = BAD; in inflate()
394 if (len > state->wbits) { in inflate()
395 strm->msg = (char *)"invalid window size"; in inflate()
396 state->mode = BAD; in inflate()
399 state->dmax = 1U << len; in inflate()
401 strm->adler = state->check = adler32(0L, Z_NULL, 0); in inflate()
402 state->mode = hold & 0x200 ? DICTID : TYPE; in inflate()
408 state->flags = (int)(hold); in inflate()
409 if ((state->flags & 0xff) != Z_DEFLATED) { in inflate()
410 strm->msg = (char *)"unknown compression method"; in inflate()
411 state->mode = BAD; in inflate()
414 if (state->flags & 0xe000) { in inflate()
415 strm->msg = (char *)"unknown header flags set"; in inflate()
416 state->mode = BAD; in inflate()
419 if (state->head != Z_NULL) in inflate()
420 state->head->text = (int)((hold >> 8) & 1); in inflate()
421 if (state->flags & 0x0200) CRC2(state->check, hold); in inflate()
423 state->mode = TIME; in inflate()
426 if (state->head != Z_NULL) in inflate()
427 state->head->time = hold; in inflate()
428 if (state->flags & 0x0200) CRC4(state->check, hold); in inflate()
430 state->mode = OS; in inflate()
433 if (state->head != Z_NULL) { in inflate()
434 state->head->xflags = (int)(hold & 0xff); in inflate()
435 state->head->os = (int)(hold >> 8); in inflate()
437 if (state->flags & 0x0200) CRC2(state->check, hold); in inflate()
439 state->mode = EXLEN; in inflate()
441 if (state->flags & 0x0400) { in inflate()
443 state->length = (unsigned)(hold); in inflate()
444 if (state->head != Z_NULL) in inflate()
445 state->head->extra_len = (unsigned)hold; in inflate()
446 if (state->flags & 0x0200) CRC2(state->check, hold); in inflate()
449 else if (state->head != Z_NULL) in inflate()
450 state->head->extra = Z_NULL; in inflate()
451 state->mode = EXTRA; in inflate()
453 if (state->flags & 0x0400) { in inflate()
454 copy = state->length; in inflate()
457 if (state->head != Z_NULL && in inflate()
458 state->head->extra != Z_NULL) { in inflate()
459 len = state->head->extra_len - state->length; in inflate()
460 zmemcpy(state->head->extra + len, next, in inflate()
461 len + copy > state->head->extra_max ? in inflate()
462 state->head->extra_max - len : copy); in inflate()
464 if (state->flags & 0x0200) in inflate()
465 state->check = crc32(state->check, next, copy); in inflate()
466 have -= copy; in inflate()
468 state->length -= copy; in inflate()
470 if (state->length) goto inf_leave; in inflate()
472 state->length = 0; in inflate()
473 state->mode = NAME; in inflate()
475 if (state->flags & 0x0800) { in inflate()
480 if (state->head != Z_NULL && in inflate()
481 state->head->name != Z_NULL && in inflate()
482 state->length < state->head->name_max) in inflate()
483 state->head->name[state->length++] = len; in inflate()
485 if (state->flags & 0x0200) in inflate()
486 state->check = crc32(state->check, next, copy); in inflate()
487 have -= copy; in inflate()
491 else if (state->head != Z_NULL) in inflate()
492 state->head->name = Z_NULL; in inflate()
493 state->length = 0; in inflate()
494 state->mode = COMMENT; in inflate()
496 if (state->flags & 0x1000) { in inflate()
501 if (state->head != Z_NULL && in inflate()
502 state->head->comment != Z_NULL && in inflate()
503 state->length < state->head->comm_max) in inflate()
504 state->head->comment[state->length++] = len; in inflate()
506 if (state->flags & 0x0200) in inflate()
507 state->check = crc32(state->check, next, copy); in inflate()
508 have -= copy; in inflate()
512 else if (state->head != Z_NULL) in inflate()
513 state->head->comment = Z_NULL; in inflate()
514 state->mode = HCRC; in inflate()
516 if (state->flags & 0x0200) { in inflate()
518 if (hold != (state->check & 0xffff)) { in inflate()
519 strm->msg = (char *)"header crc mismatch"; in inflate()
520 state->mode = BAD; in inflate()
525 if (state->head != Z_NULL) { in inflate()
526 state->head->hcrc = (int)((state->flags >> 9) & 1); in inflate()
527 state->head->done = 1; in inflate()
529 strm->adler = state->check = crc32(0L, Z_NULL, 0); in inflate()
530 state->mode = TYPE; in inflate()
535 strm->adler = state->check = REVERSE(hold); in inflate()
537 state->mode = DICT; in inflate()
539 if (state->havedict == 0) { in inflate()
543 strm->adler = state->check = adler32(0L, Z_NULL, 0); in inflate()
544 state->mode = TYPE; in inflate()
549 if (state->last) { in inflate()
551 state->mode = CHECK; in inflate()
555 state->last = BITS(1); in inflate()
560 state->last ? " (last)" : "")); in inflate()
561 state->mode = STORED; in inflate()
566 state->last ? " (last)" : "")); in inflate()
567 state->mode = LEN; /* decode codes */ in inflate()
571 state->last ? " (last)" : "")); in inflate()
572 state->mode = TABLE; in inflate()
575 strm->msg = (char *)"invalid block type"; in inflate()
576 state->mode = BAD; in inflate()
584 strm->msg = (char *)"invalid stored block lengths"; in inflate()
585 state->mode = BAD; in inflate()
588 state->length = (unsigned)hold & 0xffff; in inflate()
590 state->length)); in inflate()
592 state->mode = COPY; in inflate()
594 copy = state->length; in inflate()
600 have -= copy; in inflate()
602 left -= copy; in inflate()
604 state->length -= copy; in inflate()
608 state->mode = TYPE; in inflate()
612 state->nlen = BITS(5) + 257; in inflate()
614 state->ndist = BITS(5) + 1; in inflate()
616 state->ncode = BITS(4) + 4; in inflate()
619 if (state->nlen > 286 || state->ndist > 30) { in inflate()
620 strm->msg = (char *)"too many length or distance symbols"; in inflate()
621 state->mode = BAD; in inflate()
626 state->have = 0; in inflate()
627 state->mode = LENLENS; in inflate()
629 while (state->have < state->ncode) { in inflate()
631 state->lens[order[state->have++]] = (unsigned short)BITS(3); in inflate()
634 while (state->have < 19) in inflate()
635 state->lens[order[state->have++]] = 0; in inflate()
636 state->next = state->codes; in inflate()
637 state->lencode = (code const FAR *)(state->next); in inflate()
638 state->lenbits = 7; in inflate()
639 ret = inflate_table(CODES, state->lens, 19, &(state->next), in inflate()
640 &(state->lenbits), state->work); in inflate()
642 strm->msg = (char *)"invalid code lengths set"; in inflate()
643 state->mode = BAD; in inflate()
647 state->have = 0; in inflate()
648 state->mode = CODELENS; in inflate()
650 while (state->have < state->nlen + state->ndist) { in inflate()
652 this = state->lencode[BITS(state->lenbits)]; in inflate()
659 state->lens[state->have++] = this.val; in inflate()
665 if (state->have == 0) { in inflate()
666 strm->msg = (char *)"invalid bit length repeat"; in inflate()
667 state->mode = BAD; in inflate()
670 len = state->lens[state->have - 1]; in inflate()
688 if (state->have + copy > state->nlen + state->ndist) { in inflate()
689 strm->msg = (char *)"invalid bit length repeat"; in inflate()
690 state->mode = BAD; in inflate()
693 while (copy--) in inflate()
694 state->lens[state->have++] = (unsigned short)len; in inflate()
699 if (state->mode == BAD) break; in inflate()
702 state->next = state->codes; in inflate()
703 state->lencode = (code const FAR *)(state->next); in inflate()
704 state->lenbits = 9; in inflate()
705 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), in inflate()
706 &(state->lenbits), state->work); in inflate()
708 strm->msg = (char *)"invalid literal/lengths set"; in inflate()
709 state->mode = BAD; in inflate()
712 state->distcode = (code const FAR *)(state->next); in inflate()
713 state->distbits = 6; in inflate()
714 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, in inflate()
715 &(state->next), &(state->distbits), state->work); in inflate()
717 strm->msg = (char *)"invalid distances set"; in inflate()
718 state->mode = BAD; in inflate()
722 state->mode = LEN; in inflate()
732 this = state->lencode[BITS(state->lenbits)]; in inflate()
739 this = state->lencode[last.val + in inflate()
747 state->length = (unsigned)this.val; in inflate()
752 state->mode = LIT; in inflate()
757 state->mode = TYPE; in inflate()
761 strm->msg = (char *)"invalid literal/length code"; in inflate()
762 state->mode = BAD; in inflate()
765 state->extra = (unsigned)(this.op) & 15; in inflate()
766 state->mode = LENEXT; in inflate()
768 if (state->extra) { in inflate()
769 NEEDBITS(state->extra); in inflate()
770 state->length += BITS(state->extra); in inflate()
771 DROPBITS(state->extra); in inflate()
773 Tracevv((stderr, "inflate: length %u\n", state->length)); in inflate()
774 state->mode = DIST; in inflate()
777 this = state->distcode[BITS(state->distbits)]; in inflate()
784 this = state->distcode[last.val + in inflate()
793 strm->msg = (char *)"invalid distance code"; in inflate()
794 state->mode = BAD; in inflate()
797 state->offset = (unsigned)this.val; in inflate()
798 state->extra = (unsigned)(this.op) & 15; in inflate()
799 state->mode = DISTEXT; in inflate()
801 if (state->extra) { in inflate()
802 NEEDBITS(state->extra); in inflate()
803 state->offset += BITS(state->extra); in inflate()
804 DROPBITS(state->extra); in inflate()
807 if (state->offset > state->dmax) { in inflate()
808 strm->msg = (char *)"invalid distance too far back"; in inflate()
809 state->mode = BAD; in inflate()
813 if (state->offset > state->whave + out - left) { in inflate()
814 strm->msg = (char *)"invalid distance too far back"; in inflate()
815 state->mode = BAD; in inflate()
818 Tracevv((stderr, "inflate: distance %u\n", state->offset)); in inflate()
819 state->mode = MATCH; in inflate()
822 copy = out - left; in inflate()
823 if (state->offset > copy) { /* copy from window */ in inflate()
824 copy = state->offset - copy; in inflate()
825 if (copy > state->write) { in inflate()
826 copy -= state->write; in inflate()
827 from = state->window + (state->wsize - copy); in inflate()
830 from = state->window + (state->write - copy); in inflate()
831 if (copy > state->length) copy = state->length; in inflate()
834 from = put - state->offset; in inflate()
835 copy = state->length; in inflate()
838 left -= copy; in inflate()
839 state->length -= copy; in inflate()
842 } while (--copy); in inflate()
843 if (state->length == 0) state->mode = LEN; in inflate()
847 *put++ = (unsigned char)(state->length); in inflate()
848 left--; in inflate()
849 state->mode = LEN; in inflate()
852 if (state->wrap) { in inflate()
854 out -= left; in inflate()
855 strm->total_out += out; in inflate()
856 state->total += out; in inflate()
858 strm->adler = state->check = in inflate()
859 UPDATE(state->check, put - out, out); in inflate()
863 state->flags ? hold : in inflate()
865 REVERSE(hold)) != state->check) { in inflate()
866 strm->msg = (char *)"incorrect data check"; in inflate()
867 state->mode = BAD; in inflate()
874 state->mode = LENGTH; in inflate()
876 if (state->wrap && state->flags) { in inflate()
878 if (hold != (state->total & 0xffffffffUL)) { in inflate()
879 strm->msg = (char *)"incorrect length check"; in inflate()
880 state->mode = BAD; in inflate()
887 state->mode = DONE; in inflate()
905 Note: a memory error from inflate() is non-recoverable. in inflate()
909 if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) in inflate()
911 state->mode = MEM; in inflate()
914 in -= strm->avail_in; in inflate()
915 out -= strm->avail_out; in inflate()
916 strm->total_in += in; in inflate()
917 strm->total_out += out; in inflate()
918 state->total += out; in inflate()
919 if (state->wrap && out) in inflate()
920 strm->adler = state->check = in inflate()
921 UPDATE(state->check, strm->next_out - out, out); in inflate()
922 strm->data_type = state->bits + (state->last ? 64 : 0) + in inflate()
923 (state->mode == TYPE ? 128 : 0); in inflate()
932 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) in inflateEnd()
934 state = (struct inflate_state FAR *)strm->state; in inflateEnd()
935 if (state->window != Z_NULL) { in inflateEnd()
937 ZFREE(strm, state->window); in inflateEnd()
939 ZFREE(strm, strm->state); in inflateEnd()
940 strm->state = Z_NULL; in inflateEnd()