28 int ZEXPORT
inflateBackInit_(z_streamp strm,
int windowBits,
unsigned char FAR *window,
const char *version,
int stream_size)
30 struct inflate_state FAR *state;
32 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
33 stream_size != (
int)(
sizeof(z_stream)))
34 return Z_VERSION_ERROR;
35 if (strm == Z_NULL || window == Z_NULL ||
36 windowBits < 8 || windowBits > 15)
37 return Z_STREAM_ERROR;
39 if (strm->zalloc == (alloc_func)0) {
41 return Z_STREAM_ERROR;
44 strm->opaque = (voidpf)0;
47 if (strm->zfree == (free_func)0)
49 return Z_STREAM_ERROR;
53 state = (
struct inflate_state FAR *)ZALLOC(strm, 1,
54 sizeof(
struct inflate_state));
55 if (state == Z_NULL)
return Z_MEM_ERROR;
56 Tracev((stderr,
"inflate: allocated\n"));
59 state->wbits = windowBits;
60 state->wsize = 1U << windowBits;
61 state->window = window;
80 static int virgin = 1;
81 static code *lenfix, *distfix;
82 static code fixed[544];
91 while (sym < 144) state->lens[sym++] = 8;
92 while (sym < 256) state->lens[sym++] = 9;
93 while (sym < 280) state->lens[sym++] = 7;
94 while (sym < 288) state->lens[sym++] = 8;
98 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
102 while (sym < 32) state->lens[sym++] = 5;
105 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
111 # include "inffixed.h"
113 state->lencode = lenfix;
115 state->distcode = distfix;
124 put = strm->next_out; \
125 left = strm->avail_out; \
126 next = strm->next_in; \
127 have = strm->avail_in; \
128 hold = state->hold; \
129 bits = state->bits; \
135 strm->next_out = put; \
136 strm->avail_out = left; \
137 strm->next_in = next; \
138 strm->avail_in = have; \
139 state->hold = hold; \
140 state->bits = bits; \
155 have = in(in_desc, &next); \
170 hold += (unsigned long)(*next++) << bits; \
177 #define NEEDBITS(n) \
179 while (bits < (unsigned)(n)) \
185 ((unsigned)hold & ((1U << (n)) - 1))
188 #define DROPBITS(n) \
191 bits -= (unsigned)(n); \
207 put = state->window; \
208 left = state->wsize; \
209 state->whave = left; \
210 if (out(out_desc, put, left)) { \
244 int ZEXPORT
inflateBack(z_streamp strm, in_func in,
void FAR *in_desc, out_func out,
void FAR *out_desc)
246 struct inflate_state FAR *state;
247 z_const
unsigned char FAR *next;
248 unsigned char FAR *put;
253 unsigned char FAR *from;
258 static const unsigned short order[19] =
259 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
262 if (strm == Z_NULL || strm->state == Z_NULL)
263 return Z_STREAM_ERROR;
264 state = (
struct inflate_state FAR *)strm->state;
271 next = strm->next_in;
272 have = next != Z_NULL ? strm->avail_in : 0;
280 switch (state->mode) {
289 state->last =
BITS(1);
293 Tracev((stderr,
"inflate: stored block%s\n",
294 state->last ?
" (last)" :
""));
295 state->mode = STORED;
299 Tracev((stderr,
"inflate: fixed codes block%s\n",
300 state->last ?
" (last)" :
""));
304 Tracev((stderr,
"inflate: dynamic codes block%s\n",
305 state->last ?
" (last)" :
""));
309 strm->msg = (
char *)
"invalid block type";
319 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
320 strm->msg = (
char *)
"invalid stored block lengths";
324 state->length = (unsigned)hold & 0xffff;
325 Tracev((stderr,
"inflate: stored length %u\n",
330 while (state->length != 0) {
331 copy = state->length;
334 if (copy > have) copy = have;
335 if (copy > left) copy =
left;
341 state->length -= copy;
343 Tracev((stderr,
"inflate: stored end\n"));
350 state->nlen =
BITS(5) + 257;
352 state->ndist =
BITS(5) + 1;
354 state->ncode =
BITS(4) + 4;
356 #ifndef PKZIP_BUG_WORKAROUND
357 if (state->nlen > 286 || state->ndist > 30) {
358 strm->msg = (
char *)
"too many length or distance symbols";
363 Tracev((stderr,
"inflate: table sizes ok\n"));
367 while (state->have < state->ncode) {
369 state->lens[order[state->have++]] = (
unsigned short)
BITS(3);
372 while (state->have < 19)
373 state->lens[order[state->have++]] = 0;
374 state->next = state->codes;
375 state->lencode = (code
const FAR *)(state->next);
378 &(state->lenbits), state->work);
380 strm->msg = (
char *)
"invalid code lengths set";
384 Tracev((stderr,
"inflate: code lengths ok\n"));
388 while (state->have < state->nlen + state->ndist) {
390 here = state->lencode[
BITS(state->lenbits)];
391 if ((
unsigned)(here.bits) <= bits)
break;
396 state->lens[state->have++] = here.val;
399 if (here.val == 16) {
402 if (state->have == 0) {
403 strm->msg = (
char *)
"invalid bit length repeat";
407 len = (unsigned)(state->lens[state->have - 1]);
411 else if (here.val == 17) {
425 if (state->have + copy > state->nlen + state->ndist) {
426 strm->msg = (
char *)
"invalid bit length repeat";
431 state->lens[state->have++] = (
unsigned short)len;
436 if (state->mode == BAD)
break;
439 if (state->lens[256] == 0) {
440 strm->msg = (
char *)
"invalid code -- missing end-of-block";
448 state->next = state->codes;
449 state->lencode = (code
const FAR *)(state->next);
451 ret =
inflate_table(LENS, state->lens, state->nlen, &(state->next),
452 &(state->lenbits), state->work);
454 strm->msg = (
char *)
"invalid literal/lengths set";
458 state->distcode = (code
const FAR *)(state->next);
460 ret =
inflate_table(DISTS, state->lens + state->nlen, state->ndist,
461 &(state->next), &(state->distbits), state->work);
463 strm->msg = (
char *)
"invalid distances set";
467 Tracev((stderr,
"inflate: codes ok\n"));
472 if (have >= 6 && left >= 258) {
474 if (state->whave < state->wsize)
475 state->whave = state->wsize -
left;
483 here = state->lencode[
BITS(state->lenbits)];
484 if ((
unsigned)(here.bits) <= bits)
break;
487 if (here.op && (here.op & 0xf0) == 0) {
490 here = state->lencode[last.val +
491 (
BITS(last.bits + last.op) >> last.bits)];
492 if ((
unsigned)(last.bits + here.bits) <= bits)
break;
498 state->length = (unsigned)here.val;
502 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
503 "inflate: literal '%c'\n" :
504 "inflate: literal 0x%02x\n", here.val));
506 *put++ = (
unsigned char)(state->length);
514 Tracevv((stderr,
"inflate: end of block\n"));
521 strm->msg = (
char *)
"invalid literal/length code";
527 state->extra = (unsigned)(here.op) & 15;
528 if (state->extra != 0) {
530 state->length +=
BITS(state->extra);
533 Tracevv((stderr,
"inflate: length %u\n", state->length));
537 here = state->distcode[
BITS(state->distbits)];
538 if ((
unsigned)(here.bits) <= bits)
break;
541 if ((here.op & 0xf0) == 0) {
544 here = state->distcode[last.val +
545 (
BITS(last.bits + last.op) >> last.bits)];
546 if ((
unsigned)(last.bits + here.bits) <= bits)
break;
553 strm->msg = (
char *)
"invalid distance code";
557 state->offset = (unsigned)here.val;
560 state->extra = (
unsigned)(here.op) & 15;
561 if (state->extra != 0) {
563 state->offset +=
BITS(state->extra);
566 if (state->offset > state->wsize - (state->whave < state->wsize ?
568 strm->msg = (
char *)
"invalid distance too far back";
572 Tracevv((stderr,
"inflate: distance %u\n", state->offset));
577 copy = state->wsize - state->offset;
583 from = put - state->offset;
586 if (copy > state->length) copy = state->length;
587 state->length -= copy;
592 }
while (state->length != 0);
598 if (left < state->wsize) {
599 if (out(out_desc, state->window, state->wsize - left))
609 ret = Z_STREAM_ERROR;
615 strm->next_in = next;
616 strm->avail_in = have;
622 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
623 return Z_STREAM_ERROR;
624 ZFREE(strm, strm->state);
625 strm->state = Z_NULL;
626 Tracev((stderr,
"inflate: end\n"));
int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, unsigned codes, code FAR *FAR *table, unsigned FAR *bits, unsigned short FAR *work)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start)
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
local void fixedtables OF((struct inflate_state FAR *state))
int ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc, out_func out, void FAR *out_desc)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
local void fixedtables(struct inflate_state FAR *state)
int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits, unsigned char FAR *window, const char *version, int stream_size)
int ZEXPORT inflateBackEnd(z_streamp strm)