Lines Matching +full:next +full:- +full:mode
1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 Mark Adler
18 /* architecture-specific bits */
37 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR; in zlib_inflateReset()
38 state = (struct inflate_state *)strm->state; in zlib_inflateReset()
39 strm->total_in = strm->total_out = state->total = 0; in zlib_inflateReset()
40 strm->msg = NULL; in zlib_inflateReset()
41 strm->adler = 1; /* to support ill-conceived Java test suite */ in zlib_inflateReset()
42 state->mode = HEAD; in zlib_inflateReset()
43 state->last = 0; in zlib_inflateReset()
44 state->havedict = 0; in zlib_inflateReset()
45 state->dmax = 32768U; in zlib_inflateReset()
46 state->hold = 0; in zlib_inflateReset()
47 state->bits = 0; in zlib_inflateReset()
48 state->lencode = state->distcode = state->next = state->codes; in zlib_inflateReset()
51 state->wsize = 1U << state->wbits; in zlib_inflateReset()
52 state->write = 0; in zlib_inflateReset()
53 state->whave = 0; in zlib_inflateReset()
64 strm->msg = NULL; /* in case we return an error */ in zlib_inflateInit2()
66 state = &WS(strm)->inflate_state; in zlib_inflateInit2()
67 strm->state = (struct internal_state *)state; in zlib_inflateInit2()
70 state->wrap = 0; in zlib_inflateInit2()
71 windowBits = -windowBits; in zlib_inflateInit2()
74 state->wrap = (windowBits >> 4) + 1; in zlib_inflateInit2()
79 state->wbits = (unsigned)windowBits; in zlib_inflateInit2()
85 state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE); in zlib_inflateInit2()
87 state->window = &WS(strm)->working_window[0]; in zlib_inflateInit2()
100 state->lencode = lenfix; in zlib_fixedtables()
101 state->lenbits = 9; in zlib_fixedtables()
102 state->distcode = distfix; in zlib_fixedtables()
103 state->distbits = 5; in zlib_fixedtables()
125 state = (struct inflate_state *)strm->state; in zlib_updatewindow()
127 /* copy state->wsize or less output bytes into the circular window */ in zlib_updatewindow()
128 copy = out - strm->avail_out; in zlib_updatewindow()
129 if (copy >= state->wsize) { in zlib_updatewindow()
130 memcpy(state->window, strm->next_out - state->wsize, state->wsize); in zlib_updatewindow()
131 state->write = 0; in zlib_updatewindow()
132 state->whave = state->wsize; in zlib_updatewindow()
135 dist = state->wsize - state->write; in zlib_updatewindow()
137 memcpy(state->window + state->write, strm->next_out - copy, dist); in zlib_updatewindow()
138 copy -= dist; in zlib_updatewindow()
140 memcpy(state->window, strm->next_out - copy, copy); in zlib_updatewindow()
141 state->write = copy; in zlib_updatewindow()
142 state->whave = state->wsize; in zlib_updatewindow()
145 state->write += dist; in zlib_updatewindow()
146 if (state->write == state->wsize) state->write = 0; in zlib_updatewindow()
147 if (state->whave < state->wsize) state->whave += dist; in zlib_updatewindow()
154 * At the end of a Deflate-compressed PPP packet, we expect to have seen
169 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR; in zlib_inflateSyncPacket()
170 state = (struct inflate_state *)strm->state; in zlib_inflateSyncPacket()
172 if (state->mode == STORED && state->bits == 0) { in zlib_inflateSyncPacket()
173 state->mode = TYPE; in zlib_inflateSyncPacket()
187 put = strm->next_out; \
188 left = strm->avail_out; \
189 next = strm->next_in; \
190 have = strm->avail_in; \
191 hold = state->hold; \
192 bits = state->bits; \
198 strm->next_out = put; \
199 strm->avail_out = left; \
200 strm->next_in = next; \
201 strm->avail_in = have; \
202 state->hold = hold; \
203 state->bits = bits; \
218 have--; \
219 hold += (unsigned long)(*next++) << bits; \
233 ((unsigned)hold & ((1U << (n)) - 1))
239 bits -= (unsigned)(n); \
246 bits -= bits & 7; \
267 next state. The NEEDBITS() macro is usually the way the state evaluates
282 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
286 PULLBYTE() directly in order to pull just enough bytes to decode the next
304 As shown above, if the next state is also the next case, then the break
314 Progress is defined as a change in either strm->avail_in or strm->avail_out.
318 output to the window for the next call of inflate().
322 strm->next_out, given the space available and the provided input--the effect
334 const unsigned char *next; /* next input */ in zlib_inflate() local
335 unsigned char *put; /* next output */ in zlib_inflate()
349 /* Do not check for strm->next_out == NULL here as ppc zImage in zlib_inflate()
350 inflates to strm->next_out = 0 */ in zlib_inflate()
352 if (strm == NULL || strm->state == NULL || in zlib_inflate()
353 (strm->next_in == NULL && strm->avail_in != 0)) in zlib_inflate()
356 state = (struct inflate_state *)strm->state; in zlib_inflate()
358 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ in zlib_inflate()
364 switch (state->mode) { in zlib_inflate()
366 if (state->wrap == 0) { in zlib_inflate()
367 state->mode = TYPEDO; in zlib_inflate()
373 strm->msg = (char *)"incorrect header check"; in zlib_inflate()
374 state->mode = BAD; in zlib_inflate()
378 strm->msg = (char *)"unknown compression method"; in zlib_inflate()
379 state->mode = BAD; in zlib_inflate()
384 if (len > state->wbits) { in zlib_inflate()
385 strm->msg = (char *)"invalid window size"; in zlib_inflate()
386 state->mode = BAD; in zlib_inflate()
389 state->dmax = 1U << len; in zlib_inflate()
390 strm->adler = state->check = zlib_adler32(0L, NULL, 0); in zlib_inflate()
391 state->mode = hold & 0x200 ? DICTID : TYPE; in zlib_inflate()
396 strm->adler = state->check = REVERSE(hold); in zlib_inflate()
398 state->mode = DICT; in zlib_inflate()
401 if (state->havedict == 0) { in zlib_inflate()
405 strm->adler = state->check = zlib_adler32(0L, NULL, 0); in zlib_inflate()
406 state->mode = TYPE; in zlib_inflate()
413 if (state->last) { in zlib_inflate()
415 state->mode = CHECK; in zlib_inflate()
419 state->last = BITS(1); in zlib_inflate()
423 state->mode = STORED; in zlib_inflate()
427 state->mode = LEN; /* decode codes */ in zlib_inflate()
430 state->mode = TABLE; in zlib_inflate()
433 strm->msg = (char *)"invalid block type"; in zlib_inflate()
434 state->mode = BAD; in zlib_inflate()
442 strm->msg = (char *)"invalid stored block lengths"; in zlib_inflate()
443 state->mode = BAD; in zlib_inflate()
446 state->length = (unsigned)hold & 0xffff; in zlib_inflate()
448 state->mode = COPY; in zlib_inflate()
451 copy = state->length; in zlib_inflate()
456 memcpy(put, next, copy); in zlib_inflate()
457 have -= copy; in zlib_inflate()
458 next += copy; in zlib_inflate()
459 left -= copy; in zlib_inflate()
461 state->length -= copy; in zlib_inflate()
464 state->mode = TYPE; in zlib_inflate()
468 state->nlen = BITS(5) + 257; in zlib_inflate()
470 state->ndist = BITS(5) + 1; in zlib_inflate()
472 state->ncode = BITS(4) + 4; in zlib_inflate()
475 if (state->nlen > 286 || state->ndist > 30) { in zlib_inflate()
476 strm->msg = (char *)"too many length or distance symbols"; in zlib_inflate()
477 state->mode = BAD; in zlib_inflate()
481 state->have = 0; in zlib_inflate()
482 state->mode = LENLENS; in zlib_inflate()
485 while (state->have < state->ncode) { in zlib_inflate()
487 state->lens[order[state->have++]] = (unsigned short)BITS(3); in zlib_inflate()
490 while (state->have < 19) in zlib_inflate()
491 state->lens[order[state->have++]] = 0; in zlib_inflate()
492 state->next = state->codes; in zlib_inflate()
493 state->lencode = (code const *)(state->next); in zlib_inflate()
494 state->lenbits = 7; in zlib_inflate()
495 ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next), in zlib_inflate()
496 &(state->lenbits), state->work); in zlib_inflate()
498 strm->msg = (char *)"invalid code lengths set"; in zlib_inflate()
499 state->mode = BAD; in zlib_inflate()
502 state->have = 0; in zlib_inflate()
503 state->mode = CODELENS; in zlib_inflate()
506 while (state->have < state->nlen + state->ndist) { in zlib_inflate()
508 this = state->lencode[BITS(state->lenbits)]; in zlib_inflate()
515 state->lens[state->have++] = this.val; in zlib_inflate()
521 if (state->have == 0) { in zlib_inflate()
522 strm->msg = (char *)"invalid bit length repeat"; in zlib_inflate()
523 state->mode = BAD; in zlib_inflate()
526 len = state->lens[state->have - 1]; in zlib_inflate()
544 if (state->have + copy > state->nlen + state->ndist) { in zlib_inflate()
545 strm->msg = (char *)"invalid bit length repeat"; in zlib_inflate()
546 state->mode = BAD; in zlib_inflate()
549 while (copy--) in zlib_inflate()
550 state->lens[state->have++] = (unsigned short)len; in zlib_inflate()
555 if (state->mode == BAD) break; in zlib_inflate()
558 state->next = state->codes; in zlib_inflate()
559 state->lencode = (code const *)(state->next); in zlib_inflate()
560 state->lenbits = 9; in zlib_inflate()
561 ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next), in zlib_inflate()
562 &(state->lenbits), state->work); in zlib_inflate()
564 strm->msg = (char *)"invalid literal/lengths set"; in zlib_inflate()
565 state->mode = BAD; in zlib_inflate()
568 state->distcode = (code const *)(state->next); in zlib_inflate()
569 state->distbits = 6; in zlib_inflate()
570 ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist, in zlib_inflate()
571 &(state->next), &(state->distbits), state->work); in zlib_inflate()
573 strm->msg = (char *)"invalid distances set"; in zlib_inflate()
574 state->mode = BAD; in zlib_inflate()
577 state->mode = LEN; in zlib_inflate()
587 this = state->lencode[BITS(state->lenbits)]; in zlib_inflate()
594 this = state->lencode[last.val + in zlib_inflate()
602 state->length = (unsigned)this.val; in zlib_inflate()
604 state->mode = LIT; in zlib_inflate()
608 state->mode = TYPE; in zlib_inflate()
612 strm->msg = (char *)"invalid literal/length code"; in zlib_inflate()
613 state->mode = BAD; in zlib_inflate()
616 state->extra = (unsigned)(this.op) & 15; in zlib_inflate()
617 state->mode = LENEXT; in zlib_inflate()
620 if (state->extra) { in zlib_inflate()
621 NEEDBITS(state->extra); in zlib_inflate()
622 state->length += BITS(state->extra); in zlib_inflate()
623 DROPBITS(state->extra); in zlib_inflate()
625 state->mode = DIST; in zlib_inflate()
629 this = state->distcode[BITS(state->distbits)]; in zlib_inflate()
636 this = state->distcode[last.val + in zlib_inflate()
645 strm->msg = (char *)"invalid distance code"; in zlib_inflate()
646 state->mode = BAD; in zlib_inflate()
649 state->offset = (unsigned)this.val; in zlib_inflate()
650 state->extra = (unsigned)(this.op) & 15; in zlib_inflate()
651 state->mode = DISTEXT; in zlib_inflate()
654 if (state->extra) { in zlib_inflate()
655 NEEDBITS(state->extra); in zlib_inflate()
656 state->offset += BITS(state->extra); in zlib_inflate()
657 DROPBITS(state->extra); in zlib_inflate()
660 if (state->offset > state->dmax) { in zlib_inflate()
661 strm->msg = (char *)"invalid distance too far back"; in zlib_inflate()
662 state->mode = BAD; in zlib_inflate()
666 if (state->offset > state->whave + out - left) { in zlib_inflate()
667 strm->msg = (char *)"invalid distance too far back"; in zlib_inflate()
668 state->mode = BAD; in zlib_inflate()
671 state->mode = MATCH; in zlib_inflate()
675 copy = out - left; in zlib_inflate()
676 if (state->offset > copy) { /* copy from window */ in zlib_inflate()
677 copy = state->offset - copy; in zlib_inflate()
678 if (copy > state->write) { in zlib_inflate()
679 copy -= state->write; in zlib_inflate()
680 from = state->window + (state->wsize - copy); in zlib_inflate()
683 from = state->window + (state->write - copy); in zlib_inflate()
684 if (copy > state->length) copy = state->length; in zlib_inflate()
687 from = put - state->offset; in zlib_inflate()
688 copy = state->length; in zlib_inflate()
691 left -= copy; in zlib_inflate()
692 state->length -= copy; in zlib_inflate()
695 } while (--copy); in zlib_inflate()
696 if (state->length == 0) state->mode = LEN; in zlib_inflate()
700 *put++ = (unsigned char)(state->length); in zlib_inflate()
701 left--; in zlib_inflate()
702 state->mode = LEN; in zlib_inflate()
705 if (state->wrap) { in zlib_inflate()
707 out -= left; in zlib_inflate()
708 strm->total_out += out; in zlib_inflate()
709 state->total += out; in zlib_inflate()
711 strm->adler = state->check = in zlib_inflate()
712 UPDATE(state->check, put - out, out); in zlib_inflate()
715 REVERSE(hold)) != state->check) { in zlib_inflate()
716 strm->msg = (char *)"incorrect data check"; in zlib_inflate()
717 state->mode = BAD; in zlib_inflate()
722 state->mode = DONE; in zlib_inflate()
745 (state->wsize || (state->mode < CHECK && out != strm->avail_out))) in zlib_inflate()
748 in -= strm->avail_in; in zlib_inflate()
749 out -= strm->avail_out; in zlib_inflate()
750 strm->total_in += in; in zlib_inflate()
751 strm->total_out += out; in zlib_inflate()
752 state->total += out; in zlib_inflate()
753 if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out) in zlib_inflate()
754 strm->adler = state->check = in zlib_inflate()
755 UPDATE(state->check, strm->next_out - out, out); in zlib_inflate()
757 strm->data_type = state->bits + (state->last ? 64 : 0) + in zlib_inflate()
758 (state->mode == TYPE ? 128 : 0); in zlib_inflate()
761 strm->avail_out != 0 && strm->avail_in == 0) in zlib_inflate()
772 if (strm == NULL || strm->state == NULL) in zlib_inflateEnd()
781 * be waiting on the start of a block (i.e. mode == TYPE or HEAD). On exit,
787 struct inflate_state *state = (struct inflate_state *)z->state; in zlib_inflateIncomp()
788 Byte *saved_no = z->next_out; in zlib_inflateIncomp()
789 uInt saved_ao = z->avail_out; in zlib_inflateIncomp()
791 if (state->mode != TYPE && state->mode != HEAD) in zlib_inflateIncomp()
795 z->avail_out = 0; in zlib_inflateIncomp()
796 z->next_out = (unsigned char*)z->next_in + z->avail_in; in zlib_inflateIncomp()
798 zlib_updatewindow(z, z->avail_in); in zlib_inflateIncomp()
801 z->avail_out = saved_ao; in zlib_inflateIncomp()
802 z->next_out = saved_no; in zlib_inflateIncomp()
804 z->adler = state->check = in zlib_inflateIncomp()
805 UPDATE(state->check, z->next_in, z->avail_in); in zlib_inflateIncomp()
807 z->total_out += z->avail_in; in zlib_inflateIncomp()
808 z->total_in += z->avail_in; in zlib_inflateIncomp()
809 z->next_in += z->avail_in; in zlib_inflateIncomp()
810 state->total += z->avail_in; in zlib_inflateIncomp()
811 z->avail_in = 0; in zlib_inflateIncomp()