31 unsigned char FAR *window;
35 struct inflate_state
FAR *state;
38 stream_size != (
int)(
sizeof(z_stream)))
41 windowBits < 8 || windowBits > 15)
44 if (strm->zalloc == (alloc_func)0) {
46 return Z_STREAM_ERROR;
48 strm->zalloc = zcalloc;
49 strm->opaque = (voidpf)0;
52 if (strm->zfree == (free_func)0)
54 return Z_STREAM_ERROR;
58 state = (
struct inflate_state
FAR *)
ZALLOC(strm, 1,
59 sizeof(
struct inflate_state));
61 Tracev((stderr,
"inflate: allocated\n"));
62 strm->state = (
struct internal_state
FAR *)state;
64 state->wbits = (uInt)windowBits;
65 state->wsize = 1U << windowBits;
66 state->window = window;
86 static int virgin = 1;
87 static code *lenfix, *distfix;
88 static code fixed[544];
97 while (sym < 144) state->lens[sym++] = 8;
98 while (sym < 256) state->lens[sym++] = 9;
99 while (sym < 280) state->lens[sym++] = 7;
100 while (sym < 288) state->lens[sym++] = 8;
104 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
108 while (sym < 32) state->lens[sym++] = 5;
111 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
119 state->lencode = lenfix;
121 state->distcode = distfix;
130 put = strm->next_out; 131 left = strm->avail_out; 132 next = strm->next_in; 133 have = strm->avail_in; 141 strm->next_out = put; 142 strm->avail_out = left; 143 strm->next_in = next; 144 strm->avail_in = have; 161 have = in(in_desc, &next); 176 hold += (unsigned long)(*next++) << bits; 185 while (bits < (unsigned)(n)) 191 ((unsigned)hold & ((1U
<< (n)) - 1
)) 197 bits -= (unsigned)(n); 216 if (out(out_desc, put, left)) { 257 struct inflate_state
FAR *state;
259 unsigned char FAR *put;
264 unsigned char FAR *from;
269 static const unsigned short order[19] =
270 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
275 state = (
struct inflate_state
FAR *)strm->state;
282 next = strm->next_in;
283 have = next !=
Z_NULL ? strm->avail_in : 0;
291 switch (state->mode) {
300 state->last =
BITS(1);
304 Tracev((stderr,
"inflate: stored block%s\n",
305 state->last ?
" (last)" :
""));
306 state->mode = STORED;
310 Tracev((stderr,
"inflate: fixed codes block%s\n",
311 state->last ?
" (last)" :
""));
315 Tracev((stderr,
"inflate: dynamic codes block%s\n",
316 state->last ?
" (last)" :
""));
320 strm->msg = (
char *)
"invalid block type";
330 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
331 strm->msg = (
char *)
"invalid stored block lengths";
335 state->length = (
unsigned)hold & 0xffff;
336 Tracev((stderr,
"inflate: stored length %u\n",
341 while (state->length != 0) {
342 copy = state->length;
345 if (copy > have) copy = have;
346 if (copy > left) copy = left;
347 zmemcpy(put, next, copy);
352 state->length -= copy;
354 Tracev((stderr,
"inflate: stored end\n"));
361 state->nlen =
BITS(5) + 257;
363 state->ndist =
BITS(5) + 1;
365 state->ncode =
BITS(4) + 4;
367 #ifndef PKZIP_BUG_WORKAROUND 368 if (state->nlen > 286 || state->ndist > 30) {
369 strm->msg = (
char *)
"too many length or distance symbols";
374 Tracev((stderr,
"inflate: table sizes ok\n"));
378 while (state->have < state->ncode) {
380 state->lens[order[state->have++]] = (
unsigned short)
BITS(3);
383 while (state->have < 19)
384 state->lens[order[state->have++]] = 0;
385 state->next = state->codes;
386 state->lencode = (code
const FAR *)(state->next);
388 ret = inflate_table(CODES, state->lens, 19, &(state->next),
389 &(state->lenbits), state->work);
391 strm->msg = (
char *)
"invalid code lengths set";
395 Tracev((stderr,
"inflate: code lengths ok\n"));
399 while (state->have < state->nlen + state->ndist) {
401 here = state->lencode[
BITS(state->lenbits)];
402 if ((
unsigned)(here.bits) <= bits)
break;
407 state->lens[state->have++] = here.val;
410 if (here.val == 16) {
413 if (state->have == 0) {
414 strm->msg = (
char *)
"invalid bit length repeat";
418 len = (
unsigned)(state->lens[state->have - 1]);
422 else if (here.val == 17) {
436 if (state->have + copy > state->nlen + state->ndist) {
437 strm->msg = (
char *)
"invalid bit length repeat";
442 state->lens[state->have++] = (
unsigned short)len;
447 if (state->mode == BAD)
break;
450 if (state->lens[256] == 0) {
451 strm->msg = (
char *)
"invalid code -- missing end-of-block";
459 state->next = state->codes;
460 state->lencode = (code
const FAR *)(state->next);
462 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
463 &(state->lenbits), state->work);
465 strm->msg = (
char *)
"invalid literal/lengths set";
469 state->distcode = (code
const FAR *)(state->next);
471 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
472 &(state->next), &(state->distbits), state->work);
474 strm->msg = (
char *)
"invalid distances set";
478 Tracev((stderr,
"inflate: codes ok\n"));
483 if (have >= 6 && left >= 258) {
485 if (state->whave < state->wsize)
486 state->whave = state->wsize - left;
487 inflate_fast(strm, state->wsize);
494 here = state->lencode[
BITS(state->lenbits)];
495 if ((
unsigned)(here.bits) <= bits)
break;
498 if (here.op && (here.op & 0xf0) == 0) {
501 here = state->lencode[last.val +
502 (
BITS(last.bits + last.op) >> last.bits)];
503 if ((
unsigned)(last.bits + here.bits) <= bits)
break;
509 state->length = (
unsigned)here.val;
513 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
514 "inflate: literal '%c'\n" :
515 "inflate: literal 0x%02x\n", here.val));
517 *put++ = (
unsigned char)(state->length);
525 Tracevv((stderr,
"inflate: end of block\n"));
532 strm->msg = (
char *)
"invalid literal/length code";
538 state->extra = (
unsigned)(here.op) & 15;
539 if (state->extra != 0) {
541 state->length +=
BITS(state->extra);
544 Tracevv((stderr,
"inflate: length %u\n", state->length));
548 here = state->distcode[
BITS(state->distbits)];
549 if ((
unsigned)(here.bits) <= bits)
break;
552 if ((here.op & 0xf0) == 0) {
555 here = state->distcode[last.val +
556 (
BITS(last.bits + last.op) >> last.bits)];
557 if ((
unsigned)(last.bits + here.bits) <= bits)
break;
564 strm->msg = (
char *)
"invalid distance code";
568 state->offset = (
unsigned)here.val;
571 state->extra = (
unsigned)(here.op) & 15;
572 if (state->extra != 0) {
574 state->offset +=
BITS(state->extra);
577 if (state->offset > state->wsize - (state->whave < state->wsize ?
579 strm->msg = (
char *)
"invalid distance too far back";
583 Tracevv((stderr,
"inflate: distance %u\n", state->offset));
588 copy = state->wsize - state->offset;
594 from = put - state->offset;
597 if (copy > state->length) copy = state->length;
598 state->length -= copy;
603 }
while (state->length != 0);
609 if (left < state->wsize) {
610 if (out(out_desc, state->window, state->wsize - left))
626 strm->next_in = next;
627 strm->avail_in = have;
634 if (strm ==
Z_NULL || strm->state ==
Z_NULL || strm->zfree == (free_func)0)
636 ZFREE(strm, strm->state);
638 Tracev((stderr,
"inflate: end\n"));
int ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc, out_func out, void FAR *out_desc)
int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits, unsigned char FAR *window, const char *version, int stream_size)
#define ZALLOC(strm, items, size)
#define ZFREE(strm, addr)
int ZEXPORT inflateBackEnd(z_streamp strm)
local void fixedtables(struct inflate_state FAR *state)