1 /* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2011 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
7 This code is largely copied from inflate.c. Normally either infback.o or
8 inflate.o would be linked into an application--not both. The interface
9 with inffast.c is retained so that optimized assembler-coded versions of
10 inflate_fast() can be used with either inflate.c or infback.c.
18 /* function prototypes */
19 local void fixedtables OF((struct inflate_state FAR *state));
22 strm provides memory allocation functions in zalloc and zfree, or
23 Z_NULL to use the library memory allocation functions.
25 windowBits is in the range 8..15, and window is a user-supplied
26 window and output buffer that is 2**windowBits bytes.
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;
38 strm->msg = Z_NULL; /* in case we return an error */
39 if (strm->zalloc == (alloc_func)0) {
41 return Z_STREAM_ERROR;
43 strm->zalloc = zcalloc;
44 strm->opaque = (voidpf)0;
47 if (strm->zfree == Z_NULL)
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"));
57 strm->state = (struct internal_state FAR *)state;
59 state->wbits = windowBits;
60 state->wsize = 1U << windowBits;
61 state->window = window;
68 Return state with length and distance decoding tables and index sizes set to
69 fixed code decoding. Normally this returns fixed tables from inffixed.h.
70 If BUILDFIXED is defined, then instead this routine builds the tables the
71 first time it's called, and returns those tables the first time and
72 thereafter. This reduces the size of the code by about 2K bytes, in
73 exchange for a little execution time. However, BUILDFIXED should not be
74 used for threaded applications, since the rewriting of the tables and virgin
75 may not be thread-safe.
77 local void fixedtables(struct inflate_state FAR *state)
80 static int virgin = 1;
81 static code *lenfix, *distfix;
82 static code fixed[544];
84 /* build fixed huffman tables if first call (may not be thread safe) */
89 /* literal/length table */
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);
107 /* do this just once */
110 #else /* !BUILDFIXED */
111 # include "inffixed.h"
112 #endif /* BUILDFIXED */
113 state->lencode = lenfix;
115 state->distcode = distfix;
119 /* Macros for inflateBack(): */
121 /* Load returned state from inflate_fast() */
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; \
132 /* Set state from registers for inflate_fast() */
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; \
143 /* Clear the input bit accumulator */
150 /* Assure that some input is available. If input is requested, but denied,
151 then return a Z_BUF_ERROR from inflateBack(). */
155 have = in(in_desc, &next); \
164 /* Get a byte of input into the bit accumulator, or return from inflateBack()
165 with an error if there is no input available. */
170 hold += (unsigned long)(*next++) << bits; \
174 /* Assure that there are at least n bits in the bit accumulator. If there is
175 not enough available input to do that, then return from inflateBack() with
177 #define NEEDBITS(n) \
179 while (bits < (unsigned)(n)) \
183 /* Return the low n bits of the bit accumulator (n < 16) */
185 ((unsigned)hold & ((1U << (n)) - 1))
187 /* Remove n bits from the bit accumulator */
188 #define DROPBITS(n) \
191 bits -= (unsigned)(n); \
194 /* Remove zero to seven bits as needed to go to a byte boundary */
201 /* Assure that some output space is available, by writing out the window
202 if it's full. If the write fails, return from inflateBack() with a
207 put = state->window; \
208 left = state->wsize; \
209 state->whave = left; \
210 if (out(out_desc, put, left)) { \
218 strm provides the memory allocation functions and window buffer on input,
219 and provides information on the unused input on return. For Z_DATA_ERROR
220 returns, strm will also provide an error message.
222 in() and out() are the call-back input and output functions. When
223 inflateBack() needs more input, it calls in(). When inflateBack() has
224 filled the window with output, or when it completes with data in the
225 window, it calls out() to write out the data. The application must not
226 change the provided input until in() is called again or inflateBack()
227 returns. The application must not change the window/output buffer until
228 inflateBack() returns.
230 in() and out() are called with a descriptor parameter provided in the
231 inflateBack() call. This parameter can be a structure that provides the
232 information required to do the read or write, as well as accumulated
233 information on the input and output such as totals and check values.
235 in() should return zero on failure. out() should return non-zero on
236 failure. If either in() or out() fails, than inflateBack() returns a
237 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
238 was in() or out() that caused in the error. Otherwise, inflateBack()
239 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
240 error, or Z_MEM_ERROR if it could not allocate memory for the state.
241 inflateBack() can also return Z_STREAM_ERROR if the input parameters
242 are not correct, i.e. strm is Z_NULL or the state was not initialized.
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; /* next input */
248 unsigned char FAR *put; /* next output */
249 unsigned have, left; /* available input and output */
250 unsigned long hold; /* bit buffer */
251 unsigned bits; /* bits in bit buffer */
252 unsigned copy; /* number of stored or match bytes to copy */
253 unsigned char FAR *from; /* where to copy match bytes from */
254 code here; /* current decoding table entry */
255 code last; /* parent table entry */
256 unsigned len; /* length to copy for repeats, bits to drop */
257 int ret; /* return code */
258 static const unsigned short order[19] = /* permutation of code lengths */
259 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
261 /* Check that the strm exists and that the state was initialized */
262 if (strm == Z_NULL || strm->state == Z_NULL)
263 return Z_STREAM_ERROR;
264 state = (struct inflate_state FAR *)strm->state;
266 /* Reset the state */
271 next = strm->next_in;
272 have = next != Z_NULL ? strm->avail_in : 0;
278 /* Inflate until end of block marked as last */
280 switch (state->mode) {
282 /* determine and dispatch block type */
289 state->last = BITS(1);
292 case 0: /* stored block */
293 Tracev((stderr, "inflate: stored block%s\n",
294 state->last ? " (last)" : ""));
295 state->mode = STORED;
297 case 1: /* fixed block */
299 Tracev((stderr, "inflate: fixed codes block%s\n",
300 state->last ? " (last)" : ""));
301 state->mode = LEN; /* decode codes */
303 case 2: /* dynamic block */
304 Tracev((stderr, "inflate: dynamic codes block%s\n",
305 state->last ? " (last)" : ""));
309 strm->msg = (char *)"invalid block type";
316 /* get and verify stored block length */
317 BYTEBITS(); /* go to byte boundary */
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",
329 /* copy stored block from input to output */
330 while (state->length != 0) {
331 copy = state->length;
334 if (copy > have) copy = have;
335 if (copy > left) copy = left;
336 zmemcpy(put, next, copy);
341 state->length -= copy;
343 Tracev((stderr, "inflate: stored end\n"));
348 /* get dynamic table entries descriptor */
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"));
365 /* get code length code lengths (not a typo) */
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);
377 ret = inflate_table(CODES, state->lens, 19, &(state->next),
378 &(state->lenbits), state->work);
380 strm->msg = (char *)"invalid code lengths set";
384 Tracev((stderr, "inflate: code lengths ok\n"));
386 /* get length and distance code code lengths */
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) {
400 NEEDBITS(here.bits + 2);
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) {
412 NEEDBITS(here.bits + 3);
419 NEEDBITS(here.bits + 7);
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;
435 /* handle error breaks in while */
436 if (state->mode == BAD) break;
438 /* check for end-of-block code (better have one) */
439 if (state->lens[256] == 0) {
440 strm->msg = (char *)"invalid code -- missing end-of-block";
445 /* build code tables -- note: do not change the lenbits or distbits
446 values here (9 and 6) without reading the comments in inftrees.h
447 concerning the ENOUGH constants, which depend on those values */
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"));
471 /* use inflate_fast() if we have enough input and output */
472 if (have >= 6 && left >= 258) {
474 if (state->whave < state->wsize)
475 state->whave = state->wsize - left;
476 inflate_fast(strm, state->wsize);
481 /* get a literal, length, or end-of-block code */
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;
500 /* process literal */
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);
512 /* process end of block */
514 Tracevv((stderr, "inflate: end of block\n"));
521 strm->msg = (char *)"invalid literal/length code";
526 /* length code -- get extra bits, if any */
527 state->extra = (unsigned)(here.op) & 15;
528 if (state->extra != 0) {
529 NEEDBITS(state->extra);
530 state->length += BITS(state->extra);
531 DROPBITS(state->extra);
533 Tracevv((stderr, "inflate: length %u\n", state->length));
535 /* get distance code */
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;
559 /* get distance extra bits, if any */
560 state->extra = (unsigned)(here.op) & 15;
561 if (state->extra != 0) {
562 NEEDBITS(state->extra);
563 state->offset += BITS(state->extra);
564 DROPBITS(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));
574 /* copy match from window to output */
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);
596 /* inflate stream terminated properly -- write leftover output */
598 if (left < state->wsize) {
599 if (out(out_desc, state->window, state->wsize - left))
608 default: /* can't happen, but makes compilers happy */
609 ret = Z_STREAM_ERROR;
613 /* Return unused input */
615 strm->next_in = next;
616 strm->avail_in = have;
620 int ZEXPORT inflateBackEnd(z_streamp strm)
622 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == Z_NULL)
623 return Z_STREAM_ERROR;
624 ZFREE(strm, strm->state);
625 strm->state = Z_NULL;
626 Tracev((stderr, "inflate: end\n"));