cannam@128: /* infback.c -- inflate using a call-back interface cannam@128: * Copyright (C) 1995-2011 Mark Adler cannam@128: * For conditions of distribution and use, see copyright notice in zlib.h cannam@128: */ cannam@128: cannam@128: /* cannam@128: This code is largely copied from inflate.c. Normally either infback.o or cannam@128: inflate.o would be linked into an application--not both. The interface cannam@128: with inffast.c is retained so that optimized assembler-coded versions of cannam@128: inflate_fast() can be used with either inflate.c or infback.c. cannam@128: */ cannam@128: cannam@128: #include "zutil.h" cannam@128: #include "inftrees.h" cannam@128: #include "inflate.h" cannam@128: #include "inffast.h" cannam@128: cannam@128: /* function prototypes */ cannam@128: local void fixedtables OF((struct inflate_state FAR *state)); cannam@128: cannam@128: /* cannam@128: strm provides memory allocation functions in zalloc and zfree, or cannam@128: Z_NULL to use the library memory allocation functions. cannam@128: cannam@128: windowBits is in the range 8..15, and window is a user-supplied cannam@128: window and output buffer that is 2**windowBits bytes. cannam@128: */ cannam@128: int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) cannam@128: z_streamp strm; cannam@128: int windowBits; cannam@128: unsigned char FAR *window; cannam@128: const char *version; cannam@128: int stream_size; cannam@128: { cannam@128: struct inflate_state FAR *state; cannam@128: cannam@128: if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || cannam@128: stream_size != (int)(sizeof(z_stream))) cannam@128: return Z_VERSION_ERROR; cannam@128: if (strm == Z_NULL || window == Z_NULL || cannam@128: windowBits < 8 || windowBits > 15) cannam@128: return Z_STREAM_ERROR; cannam@128: strm->msg = Z_NULL; /* in case we return an error */ cannam@128: if (strm->zalloc == (alloc_func)0) { cannam@128: #ifdef Z_SOLO cannam@128: return Z_STREAM_ERROR; cannam@128: #else cannam@128: strm->zalloc = zcalloc; cannam@128: strm->opaque = (voidpf)0; cannam@128: #endif cannam@128: } cannam@128: if (strm->zfree == (free_func)0) cannam@128: #ifdef Z_SOLO cannam@128: return Z_STREAM_ERROR; cannam@128: #else cannam@128: strm->zfree = zcfree; cannam@128: #endif cannam@128: state = (struct inflate_state FAR *)ZALLOC(strm, 1, cannam@128: sizeof(struct inflate_state)); cannam@128: if (state == Z_NULL) return Z_MEM_ERROR; cannam@128: Tracev((stderr, "inflate: allocated\n")); cannam@128: strm->state = (struct internal_state FAR *)state; cannam@128: state->dmax = 32768U; cannam@128: state->wbits = windowBits; cannam@128: state->wsize = 1U << windowBits; cannam@128: state->window = window; cannam@128: state->wnext = 0; cannam@128: state->whave = 0; cannam@128: return Z_OK; cannam@128: } cannam@128: cannam@128: /* cannam@128: Return state with length and distance decoding tables and index sizes set to cannam@128: fixed code decoding. Normally this returns fixed tables from inffixed.h. cannam@128: If BUILDFIXED is defined, then instead this routine builds the tables the cannam@128: first time it's called, and returns those tables the first time and cannam@128: thereafter. This reduces the size of the code by about 2K bytes, in cannam@128: exchange for a little execution time. However, BUILDFIXED should not be cannam@128: used for threaded applications, since the rewriting of the tables and virgin cannam@128: may not be thread-safe. cannam@128: */ cannam@128: local void fixedtables(state) cannam@128: struct inflate_state FAR *state; cannam@128: { cannam@128: #ifdef BUILDFIXED cannam@128: static int virgin = 1; cannam@128: static code *lenfix, *distfix; cannam@128: static code fixed[544]; cannam@128: cannam@128: /* build fixed huffman tables if first call (may not be thread safe) */ cannam@128: if (virgin) { cannam@128: unsigned sym, bits; cannam@128: static code *next; cannam@128: cannam@128: /* literal/length table */ cannam@128: sym = 0; cannam@128: while (sym < 144) state->lens[sym++] = 8; cannam@128: while (sym < 256) state->lens[sym++] = 9; cannam@128: while (sym < 280) state->lens[sym++] = 7; cannam@128: while (sym < 288) state->lens[sym++] = 8; cannam@128: next = fixed; cannam@128: lenfix = next; cannam@128: bits = 9; cannam@128: inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); cannam@128: cannam@128: /* distance table */ cannam@128: sym = 0; cannam@128: while (sym < 32) state->lens[sym++] = 5; cannam@128: distfix = next; cannam@128: bits = 5; cannam@128: inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); cannam@128: cannam@128: /* do this just once */ cannam@128: virgin = 0; cannam@128: } cannam@128: #else /* !BUILDFIXED */ cannam@128: # include "inffixed.h" cannam@128: #endif /* BUILDFIXED */ cannam@128: state->lencode = lenfix; cannam@128: state->lenbits = 9; cannam@128: state->distcode = distfix; cannam@128: state->distbits = 5; cannam@128: } cannam@128: cannam@128: /* Macros for inflateBack(): */ cannam@128: cannam@128: /* Load returned state from inflate_fast() */ cannam@128: #define LOAD() \ cannam@128: do { \ cannam@128: put = strm->next_out; \ cannam@128: left = strm->avail_out; \ cannam@128: next = strm->next_in; \ cannam@128: have = strm->avail_in; \ cannam@128: hold = state->hold; \ cannam@128: bits = state->bits; \ cannam@128: } while (0) cannam@128: cannam@128: /* Set state from registers for inflate_fast() */ cannam@128: #define RESTORE() \ cannam@128: do { \ cannam@128: strm->next_out = put; \ cannam@128: strm->avail_out = left; \ cannam@128: strm->next_in = next; \ cannam@128: strm->avail_in = have; \ cannam@128: state->hold = hold; \ cannam@128: state->bits = bits; \ cannam@128: } while (0) cannam@128: cannam@128: /* Clear the input bit accumulator */ cannam@128: #define INITBITS() \ cannam@128: do { \ cannam@128: hold = 0; \ cannam@128: bits = 0; \ cannam@128: } while (0) cannam@128: cannam@128: /* Assure that some input is available. If input is requested, but denied, cannam@128: then return a Z_BUF_ERROR from inflateBack(). */ cannam@128: #define PULL() \ cannam@128: do { \ cannam@128: if (have == 0) { \ cannam@128: have = in(in_desc, &next); \ cannam@128: if (have == 0) { \ cannam@128: next = Z_NULL; \ cannam@128: ret = Z_BUF_ERROR; \ cannam@128: goto inf_leave; \ cannam@128: } \ cannam@128: } \ cannam@128: } while (0) cannam@128: cannam@128: /* Get a byte of input into the bit accumulator, or return from inflateBack() cannam@128: with an error if there is no input available. */ cannam@128: #define PULLBYTE() \ cannam@128: do { \ cannam@128: PULL(); \ cannam@128: have--; \ cannam@128: hold += (unsigned long)(*next++) << bits; \ cannam@128: bits += 8; \ cannam@128: } while (0) cannam@128: cannam@128: /* Assure that there are at least n bits in the bit accumulator. If there is cannam@128: not enough available input to do that, then return from inflateBack() with cannam@128: an error. */ cannam@128: #define NEEDBITS(n) \ cannam@128: do { \ cannam@128: while (bits < (unsigned)(n)) \ cannam@128: PULLBYTE(); \ cannam@128: } while (0) cannam@128: cannam@128: /* Return the low n bits of the bit accumulator (n < 16) */ cannam@128: #define BITS(n) \ cannam@128: ((unsigned)hold & ((1U << (n)) - 1)) cannam@128: cannam@128: /* Remove n bits from the bit accumulator */ cannam@128: #define DROPBITS(n) \ cannam@128: do { \ cannam@128: hold >>= (n); \ cannam@128: bits -= (unsigned)(n); \ cannam@128: } while (0) cannam@128: cannam@128: /* Remove zero to seven bits as needed to go to a byte boundary */ cannam@128: #define BYTEBITS() \ cannam@128: do { \ cannam@128: hold >>= bits & 7; \ cannam@128: bits -= bits & 7; \ cannam@128: } while (0) cannam@128: cannam@128: /* Assure that some output space is available, by writing out the window cannam@128: if it's full. If the write fails, return from inflateBack() with a cannam@128: Z_BUF_ERROR. */ cannam@128: #define ROOM() \ cannam@128: do { \ cannam@128: if (left == 0) { \ cannam@128: put = state->window; \ cannam@128: left = state->wsize; \ cannam@128: state->whave = left; \ cannam@128: if (out(out_desc, put, left)) { \ cannam@128: ret = Z_BUF_ERROR; \ cannam@128: goto inf_leave; \ cannam@128: } \ cannam@128: } \ cannam@128: } while (0) cannam@128: cannam@128: /* cannam@128: strm provides the memory allocation functions and window buffer on input, cannam@128: and provides information on the unused input on return. For Z_DATA_ERROR cannam@128: returns, strm will also provide an error message. cannam@128: cannam@128: in() and out() are the call-back input and output functions. When cannam@128: inflateBack() needs more input, it calls in(). When inflateBack() has cannam@128: filled the window with output, or when it completes with data in the cannam@128: window, it calls out() to write out the data. The application must not cannam@128: change the provided input until in() is called again or inflateBack() cannam@128: returns. The application must not change the window/output buffer until cannam@128: inflateBack() returns. cannam@128: cannam@128: in() and out() are called with a descriptor parameter provided in the cannam@128: inflateBack() call. This parameter can be a structure that provides the cannam@128: information required to do the read or write, as well as accumulated cannam@128: information on the input and output such as totals and check values. cannam@128: cannam@128: in() should return zero on failure. out() should return non-zero on cannam@128: failure. If either in() or out() fails, than inflateBack() returns a cannam@128: Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it cannam@128: was in() or out() that caused in the error. Otherwise, inflateBack() cannam@128: returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format cannam@128: error, or Z_MEM_ERROR if it could not allocate memory for the state. cannam@128: inflateBack() can also return Z_STREAM_ERROR if the input parameters cannam@128: are not correct, i.e. strm is Z_NULL or the state was not initialized. cannam@128: */ cannam@128: int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) cannam@128: z_streamp strm; cannam@128: in_func in; cannam@128: void FAR *in_desc; cannam@128: out_func out; cannam@128: void FAR *out_desc; cannam@128: { cannam@128: struct inflate_state FAR *state; cannam@128: z_const unsigned char FAR *next; /* next input */ cannam@128: unsigned char FAR *put; /* next output */ cannam@128: unsigned have, left; /* available input and output */ cannam@128: unsigned long hold; /* bit buffer */ cannam@128: unsigned bits; /* bits in bit buffer */ cannam@128: unsigned copy; /* number of stored or match bytes to copy */ cannam@128: unsigned char FAR *from; /* where to copy match bytes from */ cannam@128: code here; /* current decoding table entry */ cannam@128: code last; /* parent table entry */ cannam@128: unsigned len; /* length to copy for repeats, bits to drop */ cannam@128: int ret; /* return code */ cannam@128: static const unsigned short order[19] = /* permutation of code lengths */ cannam@128: {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; cannam@128: cannam@128: /* Check that the strm exists and that the state was initialized */ cannam@128: if (strm == Z_NULL || strm->state == Z_NULL) cannam@128: return Z_STREAM_ERROR; cannam@128: state = (struct inflate_state FAR *)strm->state; cannam@128: cannam@128: /* Reset the state */ cannam@128: strm->msg = Z_NULL; cannam@128: state->mode = TYPE; cannam@128: state->last = 0; cannam@128: state->whave = 0; cannam@128: next = strm->next_in; cannam@128: have = next != Z_NULL ? strm->avail_in : 0; cannam@128: hold = 0; cannam@128: bits = 0; cannam@128: put = state->window; cannam@128: left = state->wsize; cannam@128: cannam@128: /* Inflate until end of block marked as last */ cannam@128: for (;;) cannam@128: switch (state->mode) { cannam@128: case TYPE: cannam@128: /* determine and dispatch block type */ cannam@128: if (state->last) { cannam@128: BYTEBITS(); cannam@128: state->mode = DONE; cannam@128: break; cannam@128: } cannam@128: NEEDBITS(3); cannam@128: state->last = BITS(1); cannam@128: DROPBITS(1); cannam@128: switch (BITS(2)) { cannam@128: case 0: /* stored block */ cannam@128: Tracev((stderr, "inflate: stored block%s\n", cannam@128: state->last ? " (last)" : "")); cannam@128: state->mode = STORED; cannam@128: break; cannam@128: case 1: /* fixed block */ cannam@128: fixedtables(state); cannam@128: Tracev((stderr, "inflate: fixed codes block%s\n", cannam@128: state->last ? " (last)" : "")); cannam@128: state->mode = LEN; /* decode codes */ cannam@128: break; cannam@128: case 2: /* dynamic block */ cannam@128: Tracev((stderr, "inflate: dynamic codes block%s\n", cannam@128: state->last ? " (last)" : "")); cannam@128: state->mode = TABLE; cannam@128: break; cannam@128: case 3: cannam@128: strm->msg = (char *)"invalid block type"; cannam@128: state->mode = BAD; cannam@128: } cannam@128: DROPBITS(2); cannam@128: break; cannam@128: cannam@128: case STORED: cannam@128: /* get and verify stored block length */ cannam@128: BYTEBITS(); /* go to byte boundary */ cannam@128: NEEDBITS(32); cannam@128: if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { cannam@128: strm->msg = (char *)"invalid stored block lengths"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: state->length = (unsigned)hold & 0xffff; cannam@128: Tracev((stderr, "inflate: stored length %u\n", cannam@128: state->length)); cannam@128: INITBITS(); cannam@128: cannam@128: /* copy stored block from input to output */ cannam@128: while (state->length != 0) { cannam@128: copy = state->length; cannam@128: PULL(); cannam@128: ROOM(); cannam@128: if (copy > have) copy = have; cannam@128: if (copy > left) copy = left; cannam@128: zmemcpy(put, next, copy); cannam@128: have -= copy; cannam@128: next += copy; cannam@128: left -= copy; cannam@128: put += copy; cannam@128: state->length -= copy; cannam@128: } cannam@128: Tracev((stderr, "inflate: stored end\n")); cannam@128: state->mode = TYPE; cannam@128: break; cannam@128: cannam@128: case TABLE: cannam@128: /* get dynamic table entries descriptor */ cannam@128: NEEDBITS(14); cannam@128: state->nlen = BITS(5) + 257; cannam@128: DROPBITS(5); cannam@128: state->ndist = BITS(5) + 1; cannam@128: DROPBITS(5); cannam@128: state->ncode = BITS(4) + 4; cannam@128: DROPBITS(4); cannam@128: #ifndef PKZIP_BUG_WORKAROUND cannam@128: if (state->nlen > 286 || state->ndist > 30) { cannam@128: strm->msg = (char *)"too many length or distance symbols"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: #endif cannam@128: Tracev((stderr, "inflate: table sizes ok\n")); cannam@128: cannam@128: /* get code length code lengths (not a typo) */ cannam@128: state->have = 0; cannam@128: while (state->have < state->ncode) { cannam@128: NEEDBITS(3); cannam@128: state->lens[order[state->have++]] = (unsigned short)BITS(3); cannam@128: DROPBITS(3); cannam@128: } cannam@128: while (state->have < 19) cannam@128: state->lens[order[state->have++]] = 0; cannam@128: state->next = state->codes; cannam@128: state->lencode = (code const FAR *)(state->next); cannam@128: state->lenbits = 7; cannam@128: ret = inflate_table(CODES, state->lens, 19, &(state->next), cannam@128: &(state->lenbits), state->work); cannam@128: if (ret) { cannam@128: strm->msg = (char *)"invalid code lengths set"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: Tracev((stderr, "inflate: code lengths ok\n")); cannam@128: cannam@128: /* get length and distance code code lengths */ cannam@128: state->have = 0; cannam@128: while (state->have < state->nlen + state->ndist) { cannam@128: for (;;) { cannam@128: here = state->lencode[BITS(state->lenbits)]; cannam@128: if ((unsigned)(here.bits) <= bits) break; cannam@128: PULLBYTE(); cannam@128: } cannam@128: if (here.val < 16) { cannam@128: DROPBITS(here.bits); cannam@128: state->lens[state->have++] = here.val; cannam@128: } cannam@128: else { cannam@128: if (here.val == 16) { cannam@128: NEEDBITS(here.bits + 2); cannam@128: DROPBITS(here.bits); cannam@128: if (state->have == 0) { cannam@128: strm->msg = (char *)"invalid bit length repeat"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: len = (unsigned)(state->lens[state->have - 1]); cannam@128: copy = 3 + BITS(2); cannam@128: DROPBITS(2); cannam@128: } cannam@128: else if (here.val == 17) { cannam@128: NEEDBITS(here.bits + 3); cannam@128: DROPBITS(here.bits); cannam@128: len = 0; cannam@128: copy = 3 + BITS(3); cannam@128: DROPBITS(3); cannam@128: } cannam@128: else { cannam@128: NEEDBITS(here.bits + 7); cannam@128: DROPBITS(here.bits); cannam@128: len = 0; cannam@128: copy = 11 + BITS(7); cannam@128: DROPBITS(7); cannam@128: } cannam@128: if (state->have + copy > state->nlen + state->ndist) { cannam@128: strm->msg = (char *)"invalid bit length repeat"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: while (copy--) cannam@128: state->lens[state->have++] = (unsigned short)len; cannam@128: } cannam@128: } cannam@128: cannam@128: /* handle error breaks in while */ cannam@128: if (state->mode == BAD) break; cannam@128: cannam@128: /* check for end-of-block code (better have one) */ cannam@128: if (state->lens[256] == 0) { cannam@128: strm->msg = (char *)"invalid code -- missing end-of-block"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: cannam@128: /* build code tables -- note: do not change the lenbits or distbits cannam@128: values here (9 and 6) without reading the comments in inftrees.h cannam@128: concerning the ENOUGH constants, which depend on those values */ cannam@128: state->next = state->codes; cannam@128: state->lencode = (code const FAR *)(state->next); cannam@128: state->lenbits = 9; cannam@128: ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), cannam@128: &(state->lenbits), state->work); cannam@128: if (ret) { cannam@128: strm->msg = (char *)"invalid literal/lengths set"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: state->distcode = (code const FAR *)(state->next); cannam@128: state->distbits = 6; cannam@128: ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, cannam@128: &(state->next), &(state->distbits), state->work); cannam@128: if (ret) { cannam@128: strm->msg = (char *)"invalid distances set"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: Tracev((stderr, "inflate: codes ok\n")); cannam@128: state->mode = LEN; cannam@128: cannam@128: case LEN: cannam@128: /* use inflate_fast() if we have enough input and output */ cannam@128: if (have >= 6 && left >= 258) { cannam@128: RESTORE(); cannam@128: if (state->whave < state->wsize) cannam@128: state->whave = state->wsize - left; cannam@128: inflate_fast(strm, state->wsize); cannam@128: LOAD(); cannam@128: break; cannam@128: } cannam@128: cannam@128: /* get a literal, length, or end-of-block code */ cannam@128: for (;;) { cannam@128: here = state->lencode[BITS(state->lenbits)]; cannam@128: if ((unsigned)(here.bits) <= bits) break; cannam@128: PULLBYTE(); cannam@128: } cannam@128: if (here.op && (here.op & 0xf0) == 0) { cannam@128: last = here; cannam@128: for (;;) { cannam@128: here = state->lencode[last.val + cannam@128: (BITS(last.bits + last.op) >> last.bits)]; cannam@128: if ((unsigned)(last.bits + here.bits) <= bits) break; cannam@128: PULLBYTE(); cannam@128: } cannam@128: DROPBITS(last.bits); cannam@128: } cannam@128: DROPBITS(here.bits); cannam@128: state->length = (unsigned)here.val; cannam@128: cannam@128: /* process literal */ cannam@128: if (here.op == 0) { cannam@128: Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? cannam@128: "inflate: literal '%c'\n" : cannam@128: "inflate: literal 0x%02x\n", here.val)); cannam@128: ROOM(); cannam@128: *put++ = (unsigned char)(state->length); cannam@128: left--; cannam@128: state->mode = LEN; cannam@128: break; cannam@128: } cannam@128: cannam@128: /* process end of block */ cannam@128: if (here.op & 32) { cannam@128: Tracevv((stderr, "inflate: end of block\n")); cannam@128: state->mode = TYPE; cannam@128: break; cannam@128: } cannam@128: cannam@128: /* invalid code */ cannam@128: if (here.op & 64) { cannam@128: strm->msg = (char *)"invalid literal/length code"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: cannam@128: /* length code -- get extra bits, if any */ cannam@128: state->extra = (unsigned)(here.op) & 15; cannam@128: if (state->extra != 0) { cannam@128: NEEDBITS(state->extra); cannam@128: state->length += BITS(state->extra); cannam@128: DROPBITS(state->extra); cannam@128: } cannam@128: Tracevv((stderr, "inflate: length %u\n", state->length)); cannam@128: cannam@128: /* get distance code */ cannam@128: for (;;) { cannam@128: here = state->distcode[BITS(state->distbits)]; cannam@128: if ((unsigned)(here.bits) <= bits) break; cannam@128: PULLBYTE(); cannam@128: } cannam@128: if ((here.op & 0xf0) == 0) { cannam@128: last = here; cannam@128: for (;;) { cannam@128: here = state->distcode[last.val + cannam@128: (BITS(last.bits + last.op) >> last.bits)]; cannam@128: if ((unsigned)(last.bits + here.bits) <= bits) break; cannam@128: PULLBYTE(); cannam@128: } cannam@128: DROPBITS(last.bits); cannam@128: } cannam@128: DROPBITS(here.bits); cannam@128: if (here.op & 64) { cannam@128: strm->msg = (char *)"invalid distance code"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: state->offset = (unsigned)here.val; cannam@128: cannam@128: /* get distance extra bits, if any */ cannam@128: state->extra = (unsigned)(here.op) & 15; cannam@128: if (state->extra != 0) { cannam@128: NEEDBITS(state->extra); cannam@128: state->offset += BITS(state->extra); cannam@128: DROPBITS(state->extra); cannam@128: } cannam@128: if (state->offset > state->wsize - (state->whave < state->wsize ? cannam@128: left : 0)) { cannam@128: strm->msg = (char *)"invalid distance too far back"; cannam@128: state->mode = BAD; cannam@128: break; cannam@128: } cannam@128: Tracevv((stderr, "inflate: distance %u\n", state->offset)); cannam@128: cannam@128: /* copy match from window to output */ cannam@128: do { cannam@128: ROOM(); cannam@128: copy = state->wsize - state->offset; cannam@128: if (copy < left) { cannam@128: from = put + copy; cannam@128: copy = left - copy; cannam@128: } cannam@128: else { cannam@128: from = put - state->offset; cannam@128: copy = left; cannam@128: } cannam@128: if (copy > state->length) copy = state->length; cannam@128: state->length -= copy; cannam@128: left -= copy; cannam@128: do { cannam@128: *put++ = *from++; cannam@128: } while (--copy); cannam@128: } while (state->length != 0); cannam@128: break; cannam@128: cannam@128: case DONE: cannam@128: /* inflate stream terminated properly -- write leftover output */ cannam@128: ret = Z_STREAM_END; cannam@128: if (left < state->wsize) { cannam@128: if (out(out_desc, state->window, state->wsize - left)) cannam@128: ret = Z_BUF_ERROR; cannam@128: } cannam@128: goto inf_leave; cannam@128: cannam@128: case BAD: cannam@128: ret = Z_DATA_ERROR; cannam@128: goto inf_leave; cannam@128: cannam@128: default: /* can't happen, but makes compilers happy */ cannam@128: ret = Z_STREAM_ERROR; cannam@128: goto inf_leave; cannam@128: } cannam@128: cannam@128: /* Return unused input */ cannam@128: inf_leave: cannam@128: strm->next_in = next; cannam@128: strm->avail_in = have; cannam@128: return ret; cannam@128: } cannam@128: cannam@128: int ZEXPORT inflateBackEnd(strm) cannam@128: z_streamp strm; cannam@128: { cannam@128: if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) cannam@128: return Z_STREAM_ERROR; cannam@128: ZFREE(strm, strm->state); cannam@128: strm->state = Z_NULL; cannam@128: Tracev((stderr, "inflate: end\n")); cannam@128: return Z_OK; cannam@128: }