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