Lines Matching +full:out +full:- +full:of +full:- +full:window

1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
8 * Changes mainly for static instead of dynamic memory allocation
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()
50 /* Initialise Window */ 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()
82 * DFLTCC requires the window to be page aligned. in zlib_inflateInit2()
83 * Thus, we overallocate and take the aligned portion of the buffer. 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()
108 Update the window with the last wsize (normally 32K) bytes written before
109 returning. This is only called when a window is already in use, or when
110 output has been written during this inflate call, but the end of the deflate
111 stream has not been reached yet. It is also called to window dictionary data
115 advantage, since only the last 32K of output is copied to the sliding window
116 upon return from inflate(), and since all distances after the first 32K of
118 The advantage may be dependent on the size of the processor's data caches.
120 static void zlib_updatewindow(z_streamp strm, unsigned out) in zlib_updatewindow() argument
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
158 Returns true if inflate is currently at the end of a block generated by
161 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
162 block. When decompressing, PPP checks that at the end of input packet,
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; \
213 /* Get a byte of input into the bit accumulator, or return from inflate()
218 have--; \
231 /* Return the low n bits of the bit accumulator (n < 16) */
233 ((unsigned)hold & ((1U << (n)) - 1))
239 bits -= (unsigned)(n); \
246 bits -= bits & 7; \
269 the requested bits are not available. The typical use of the BITS macros
280 and sets the number of available bits to zero. BYTEBITS() discards just
284 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
285 if there is no input available. The decoding of variable length codes uses
292 would all have to actually be part of the saved state in case NEEDBITS()
314 Progress is defined as a change in either strm->avail_in or strm->avail_out.
315 When there is a window, goto inf_leave will update the window with the last
316 output written. If a goto inf_leave occurs in the middle of decompression
317 and there is no window currently, goto inf_leave will create one and copy
318 output to the window for the next call of inflate().
320 In this implementation, the flush parameter of inflate() only affects the
322 strm->next_out, given the space available and the provided input--the effect
323 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
324 the allocation of and copying into a sliding window until necessary, which
328 will return Z_BUF_ERROR if it has not reached the end of the stream.
339 unsigned in, out; /* save starting available input and output */ in zlib_inflate() local
340 unsigned copy; /* number of stored or match bytes to copy */ in zlib_inflate()
346 static const unsigned short order[19] = /* permutation of code lengths */ 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()
361 out = left; 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()
457 have -= 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()
582 inflate_fast(strm, out); 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()
710 if (INFLATE_NEED_CHECKSUM(strm) && out) in zlib_inflate()
711 strm->adler = state->check = in zlib_inflate()
712 UPDATE(state->check, put - out, out); in zlib_inflate()
713 out = left; 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()
740 error. Call zlib_updatewindow() to create and/or update the window state. in zlib_inflate()
745 (state->wsize || (state->mode < CHECK && out != strm->avail_out))) in zlib_inflate()
746 zlib_updatewindow(strm, 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()
764 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) 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()
794 /* Setup some variables to allow misuse of updateWindow */ 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()