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