1 /* infback.c -- inflate using a call-back interface
\r
2 * Copyright (C) 1995-2003 Mark Adler
\r
3 * For conditions of distribution and use, see copyright notice in zlib.h
\r
7 This code is largely copied from inflate.c. Normally either infback.o or
\r
8 inflate.o would be linked into an application--not both. The interface
\r
9 with inffast.c is retained so that optimized assembler-coded versions of
\r
10 inflate_fast() can be used with either inflate.c or infback.c.
\r
14 #include "inftrees.h"
\r
15 #include "inflate.h"
\r
16 #include "inffast.h"
\r
18 /* function prototypes */
\r
19 local void fixedtables OF((struct inflate_state FAR *state));
\r
22 strm provides memory allocation functions in zalloc and zfree, or
\r
23 Z_NULL to use the library memory allocation functions.
\r
25 windowBits is in the range 8..15, and window is a user-supplied
\r
26 window and output buffer that is 2**windowBits bytes.
\r
28 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
\r
31 unsigned char FAR *window;
\r
32 const char *version;
\r
35 struct inflate_state FAR *state;
\r
37 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
\r
38 stream_size != (int)(sizeof(z_stream)))
\r
39 return Z_VERSION_ERROR;
\r
40 if (strm == Z_NULL || window == Z_NULL ||
\r
41 windowBits < 8 || windowBits > 15)
\r
42 return Z_STREAM_ERROR;
\r
43 strm->msg = Z_NULL; /* in case we return an error */
\r
44 if (strm->zalloc == (alloc_func)0) {
\r
45 strm->zalloc = zcalloc;
\r
46 strm->opaque = (voidpf)0;
\r
48 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
\r
49 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
\r
50 sizeof(struct inflate_state));
\r
51 if (state == Z_NULL) return Z_MEM_ERROR;
\r
52 Tracev((stderr, "inflate: allocated\n"));
\r
53 strm->state = (voidpf)state;
\r
54 state->wbits = windowBits;
\r
55 state->wsize = 1U << windowBits;
\r
56 state->window = window;
\r
63 Return state with length and distance decoding tables and index sizes set to
\r
64 fixed code decoding. Normally this returns fixed tables from inffixed.h.
\r
65 If BUILDFIXED is defined, then instead this routine builds the tables the
\r
66 first time it's called, and returns those tables the first time and
\r
67 thereafter. This reduces the size of the code by about 2K bytes, in
\r
68 exchange for a little execution time. However, BUILDFIXED should not be
\r
69 used for threaded applications, since the rewriting of the tables and virgin
\r
70 may not be thread-safe.
\r
72 local void fixedtables(state)
\r
73 struct inflate_state FAR *state;
\r
76 static int virgin = 1;
\r
77 static code *lenfix, *distfix;
\r
78 static code fixed[544];
\r
80 /* build fixed huffman tables if first call (may not be thread safe) */
\r
85 /* literal/length table */
\r
87 while (sym < 144) state->lens[sym++] = 8;
\r
88 while (sym < 256) state->lens[sym++] = 9;
\r
89 while (sym < 280) state->lens[sym++] = 7;
\r
90 while (sym < 288) state->lens[sym++] = 8;
\r
94 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
\r
96 /* distance table */
\r
98 while (sym < 32) state->lens[sym++] = 5;
\r
101 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
\r
103 /* do this just once */
\r
106 #else /* !BUILDFIXED */
\r
107 # include "inffixed.h"
\r
108 #endif /* BUILDFIXED */
\r
109 state->lencode = lenfix;
\r
110 state->lenbits = 9;
\r
111 state->distcode = distfix;
\r
112 state->distbits = 5;
\r
115 /* Macros for inflateBack(): */
\r
117 /* Load returned state from inflate_fast() */
\r
120 put = strm->next_out; \
\r
121 left = strm->avail_out; \
\r
122 next = strm->next_in; \
\r
123 have = strm->avail_in; \
\r
124 hold = state->hold; \
\r
125 bits = state->bits; \
\r
128 /* Set state from registers for inflate_fast() */
\r
129 #define RESTORE() \
\r
131 strm->next_out = put; \
\r
132 strm->avail_out = left; \
\r
133 strm->next_in = next; \
\r
134 strm->avail_in = have; \
\r
135 state->hold = hold; \
\r
136 state->bits = bits; \
\r
139 /* Clear the input bit accumulator */
\r
140 #define INITBITS() \
\r
146 /* Assure that some input is available. If input is requested, but denied,
\r
147 then return a Z_BUF_ERROR from inflateBack(). */
\r
151 have = in(in_desc, &next); \
\r
154 ret = Z_BUF_ERROR; \
\r
160 /* Get a byte of input into the bit accumulator, or return from inflateBack()
\r
161 with an error if there is no input available. */
\r
162 #define PULLBYTE() \
\r
166 hold += (unsigned long)(*next++) << bits; \
\r
170 /* Assure that there are at least n bits in the bit accumulator. If there is
\r
171 not enough available input to do that, then return from inflateBack() with
\r
173 #define NEEDBITS(n) \
\r
175 while (bits < (unsigned)(n)) \
\r
179 /* Return the low n bits of the bit accumulator (n < 16) */
\r
181 ((unsigned)hold & ((1U << (n)) - 1))
\r
183 /* Remove n bits from the bit accumulator */
\r
184 #define DROPBITS(n) \
\r
187 bits -= (unsigned)(n); \
\r
190 /* Remove zero to seven bits as needed to go to a byte boundary */
\r
191 #define BYTEBITS() \
\r
193 hold >>= bits & 7; \
\r
194 bits -= bits & 7; \
\r
197 /* Assure that some output space is available, by writing out the window
\r
198 if it's full. If the write fails, return from inflateBack() with a
\r
203 put = state->window; \
\r
204 left = state->wsize; \
\r
205 state->whave = left; \
\r
206 if (out(out_desc, put, left)) { \
\r
207 ret = Z_BUF_ERROR; \
\r
214 strm provides the memory allocation functions and window buffer on input,
\r
215 and provides information on the unused input on return. For Z_DATA_ERROR
\r
216 returns, strm will also provide an error message.
\r
218 in() and out() are the call-back input and output functions. When
\r
219 inflateBack() needs more input, it calls in(). When inflateBack() has
\r
220 filled the window with output, or when it completes with data in the
\r
221 window, it calls out() to write out the data. The application must not
\r
222 change the provided input until in() is called again or inflateBack()
\r
223 returns. The application must not change the window/output buffer until
\r
224 inflateBack() returns.
\r
226 in() and out() are called with a descriptor parameter provided in the
\r
227 inflateBack() call. This parameter can be a structure that provides the
\r
228 information required to do the read or write, as well as accumulated
\r
229 information on the input and output such as totals and check values.
\r
231 in() should return zero on failure. out() should return non-zero on
\r
232 failure. If either in() or out() fails, than inflateBack() returns a
\r
233 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
\r
234 was in() or out() that caused in the error. Otherwise, inflateBack()
\r
235 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
\r
236 error, or Z_MEM_ERROR if it could not allocate memory for the state.
\r
237 inflateBack() can also return Z_STREAM_ERROR if the input parameters
\r
238 are not correct, i.e. strm is Z_NULL or the state was not initialized.
\r
240 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
\r
241 z_stream FAR *strm;
\r
245 void FAR *out_desc;
\r
247 struct inflate_state FAR *state;
\r
248 unsigned char FAR *next; /* next input */
\r
249 unsigned char FAR *put; /* next output */
\r
250 unsigned have, left; /* available input and output */
\r
251 unsigned long hold; /* bit buffer */
\r
252 unsigned bits; /* bits in bit buffer */
\r
253 unsigned copy; /* number of stored or match bytes to copy */
\r
254 unsigned char FAR *from; /* where to copy match bytes from */
\r
255 code this; /* current decoding table entry */
\r
256 code last; /* parent table entry */
\r
257 unsigned len; /* length to copy for repeats, bits to drop */
\r
258 int ret; /* return code */
\r
259 static const unsigned short order[19] = /* permutation of code lengths */
\r
260 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
\r
262 /* Check that the strm exists and that the state was initialized */
\r
263 if (strm == Z_NULL || strm->state == Z_NULL)
\r
264 return Z_STREAM_ERROR;
\r
265 state = (struct inflate_state FAR *)strm->state;
\r
267 /* Reset the state */
\r
268 strm->msg = Z_NULL;
\r
269 state->mode = TYPE;
\r
272 next = strm->next_in;
\r
273 have = next != Z_NULL ? strm->avail_in : 0;
\r
276 put = state->window;
\r
277 left = state->wsize;
\r
279 /* Inflate until end of block marked as last */
\r
281 switch (state->mode) {
\r
283 /* determine and dispatch block type */
\r
286 state->mode = DONE;
\r
290 state->last = BITS(1);
\r
293 case 0: /* stored block */
\r
294 Tracev((stderr, "inflate: stored block%s\n",
\r
295 state->last ? " (last)" : ""));
\r
296 state->mode = STORED;
\r
298 case 1: /* fixed block */
\r
299 fixedtables(state);
\r
300 Tracev((stderr, "inflate: fixed codes block%s\n",
\r
301 state->last ? " (last)" : ""));
\r
302 state->mode = LEN; /* decode codes */
\r
304 case 2: /* dynamic block */
\r
305 Tracev((stderr, "inflate: dynamic codes block%s\n",
\r
306 state->last ? " (last)" : ""));
\r
307 state->mode = TABLE;
\r
310 strm->msg = (char *)"invalid block type";
\r
317 /* get and verify stored block length */
\r
318 BYTEBITS(); /* go to byte boundary */
\r
320 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
\r
321 strm->msg = (char *)"invalid stored block lengths";
\r
325 state->length = (unsigned)hold & 0xffff;
\r
326 Tracev((stderr, "inflate: stored length %u\n",
\r
330 /* copy stored block from input to output */
\r
331 while (state->length != 0) {
\r
332 copy = state->length;
\r
335 if (copy > have) copy = have;
\r
336 if (copy > left) copy = left;
\r
337 zmemcpy(put, next, copy);
\r
342 state->length -= copy;
\r
344 Tracev((stderr, "inflate: stored end\n"));
\r
345 state->mode = TYPE;
\r
349 /* get dynamic table entries descriptor */
\r
351 state->nlen = BITS(5) + 257;
\r
353 state->ndist = BITS(5) + 1;
\r
355 state->ncode = BITS(4) + 4;
\r
357 #ifndef PKZIP_BUG_WORKAROUND
\r
358 if (state->nlen > 286 || state->ndist > 30) {
\r
359 strm->msg = (char *)"too many length or distance symbols";
\r
364 Tracev((stderr, "inflate: table sizes ok\n"));
\r
366 /* get code length code lengths (not a typo) */
\r
368 while (state->have < state->ncode) {
\r
370 state->lens[order[state->have++]] = (unsigned short)BITS(3);
\r
373 while (state->have < 19)
\r
374 state->lens[order[state->have++]] = 0;
\r
375 state->next = state->codes;
\r
376 state->lencode = (code const FAR *)(state->next);
\r
377 state->lenbits = 7;
\r
378 ret = inflate_table(CODES, state->lens, 19, &(state->next),
\r
379 &(state->lenbits), state->work);
\r
381 strm->msg = (char *)"invalid code lengths set";
\r
385 Tracev((stderr, "inflate: code lengths ok\n"));
\r
387 /* get length and distance code code lengths */
\r
389 while (state->have < state->nlen + state->ndist) {
\r
391 this = state->lencode[BITS(state->lenbits)];
\r
392 if ((unsigned)(this.bits) <= bits) break;
\r
395 if (this.val < 16) {
\r
396 NEEDBITS(this.bits);
\r
397 DROPBITS(this.bits);
\r
398 state->lens[state->have++] = this.val;
\r
401 if (this.val == 16) {
\r
402 NEEDBITS(this.bits + 2);
\r
403 DROPBITS(this.bits);
\r
404 if (state->have == 0) {
\r
405 strm->msg = (char *)"invalid bit length repeat";
\r
409 len = (unsigned)(state->lens[state->have - 1]);
\r
410 copy = 3 + BITS(2);
\r
413 else if (this.val == 17) {
\r
414 NEEDBITS(this.bits + 3);
\r
415 DROPBITS(this.bits);
\r
417 copy = 3 + BITS(3);
\r
421 NEEDBITS(this.bits + 7);
\r
422 DROPBITS(this.bits);
\r
424 copy = 11 + BITS(7);
\r
427 if (state->have + copy > state->nlen + state->ndist) {
\r
428 strm->msg = (char *)"invalid bit length repeat";
\r
433 state->lens[state->have++] = (unsigned short)len;
\r
437 /* build code tables */
\r
438 state->next = state->codes;
\r
439 state->lencode = (code const FAR *)(state->next);
\r
440 state->lenbits = 9;
\r
441 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
\r
442 &(state->lenbits), state->work);
\r
444 strm->msg = (char *)"invalid literal/lengths set";
\r
448 state->distcode = (code const FAR *)(state->next);
\r
449 state->distbits = 6;
\r
450 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
\r
451 &(state->next), &(state->distbits), state->work);
\r
453 strm->msg = (char *)"invalid distances set";
\r
457 Tracev((stderr, "inflate: codes ok\n"));
\r
461 /* use inflate_fast() if we have enough input and output */
\r
462 if (have >= 6 && left >= 258) {
\r
464 if (state->whave < state->wsize)
\r
465 state->whave = state->wsize - left;
\r
466 inflate_fast(strm, state->wsize);
\r
471 /* get a literal, length, or end-of-block code */
\r
473 this = state->lencode[BITS(state->lenbits)];
\r
474 if ((unsigned)(this.bits) <= bits) break;
\r
477 if (this.op && (this.op & 0xf0) == 0) {
\r
480 this = state->lencode[last.val +
\r
481 (BITS(last.bits + last.op) >> last.bits)];
\r
482 if ((unsigned)(last.bits + this.bits) <= bits) break;
\r
485 DROPBITS(last.bits);
\r
487 DROPBITS(this.bits);
\r
488 state->length = (unsigned)this.val;
\r
490 /* process literal */
\r
491 if (this.op == 0) {
\r
492 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
\r
493 "inflate: literal '%c'\n" :
\r
494 "inflate: literal 0x%02x\n", this.val));
\r
496 *put++ = (unsigned char)(state->length);
\r
502 /* process end of block */
\r
503 if (this.op & 32) {
\r
504 Tracevv((stderr, "inflate: end of block\n"));
\r
505 state->mode = TYPE;
\r
510 if (this.op & 64) {
\r
511 strm->msg = (char *)"invalid literal/length code";
\r
516 /* length code -- get extra bits, if any */
\r
517 state->extra = (unsigned)(this.op) & 15;
\r
518 if (state->extra != 0) {
\r
519 NEEDBITS(state->extra);
\r
520 state->length += BITS(state->extra);
\r
521 DROPBITS(state->extra);
\r
523 Tracevv((stderr, "inflate: length %u\n", state->length));
\r
525 /* get distance code */
\r
527 this = state->distcode[BITS(state->distbits)];
\r
528 if ((unsigned)(this.bits) <= bits) break;
\r
531 if ((this.op & 0xf0) == 0) {
\r
534 this = state->distcode[last.val +
\r
535 (BITS(last.bits + last.op) >> last.bits)];
\r
536 if ((unsigned)(last.bits + this.bits) <= bits) break;
\r
539 DROPBITS(last.bits);
\r
541 DROPBITS(this.bits);
\r
542 if (this.op & 64) {
\r
543 strm->msg = (char *)"invalid distance code";
\r
547 state->offset = (unsigned)this.val;
\r
549 /* get distance extra bits, if any */
\r
550 state->extra = (unsigned)(this.op) & 15;
\r
551 if (state->extra != 0) {
\r
552 NEEDBITS(state->extra);
\r
553 state->offset += BITS(state->extra);
\r
554 DROPBITS(state->extra);
\r
556 if (state->offset > state->wsize - (state->whave < state->wsize ?
\r
558 strm->msg = (char *)"invalid distance too far back";
\r
562 Tracevv((stderr, "inflate: distance %u\n", state->offset));
\r
564 /* copy match from window to output */
\r
567 copy = state->wsize - state->offset;
\r
570 copy = left - copy;
\r
573 from = put - state->offset;
\r
576 if (copy > state->length) copy = state->length;
\r
577 state->length -= copy;
\r
582 } while (state->length != 0);
\r
586 /* inflate stream terminated properly -- write leftover output */
\r
587 ret = Z_STREAM_END;
\r
588 if (left < state->wsize) {
\r
589 if (out(out_desc, state->window, state->wsize - left))
\r
595 ret = Z_DATA_ERROR;
\r
598 default: /* can't happen, but makes compilers happy */
\r
599 ret = Z_STREAM_ERROR;
\r
603 /* Return unused input */
\r
605 strm->next_in = next;
\r
606 strm->avail_in = have;
\r
610 int ZEXPORT inflateBackEnd(strm)
\r
611 z_stream FAR *strm;
\r
613 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
\r
614 return Z_STREAM_ERROR;
\r
615 ZFREE(strm, strm->state);
\r
616 strm->state = Z_NULL;
\r
617 Tracev((stderr, "inflate: end\n"));
\r