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