1 /* infback.c -- inflate using a call-back interface
\r
2 * Copyright (C) 1995-2009 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 = (struct internal_state FAR *)state;
\r
54 state->dmax = 32768U;
\r
55 state->wbits = windowBits;
\r
56 state->wsize = 1U << windowBits;
\r
57 state->window = window;
\r
64 Return state with length and distance decoding tables and index sizes set to
\r
65 fixed code decoding. Normally this returns fixed tables from inffixed.h.
\r
66 If BUILDFIXED is defined, then instead this routine builds the tables the
\r
67 first time it's called, and returns those tables the first time and
\r
68 thereafter. This reduces the size of the code by about 2K bytes, in
\r
69 exchange for a little execution time. However, BUILDFIXED should not be
\r
70 used for threaded applications, since the rewriting of the tables and virgin
\r
71 may not be thread-safe.
\r
73 local void fixedtables(state)
\r
74 struct inflate_state FAR *state;
\r
77 static int virgin = 1;
\r
78 static code *lenfix, *distfix;
\r
79 static code fixed[544];
\r
81 /* build fixed huffman tables if first call (may not be thread safe) */
\r
86 /* literal/length table */
\r
88 while (sym < 144) state->lens[sym++] = 8;
\r
89 while (sym < 256) state->lens[sym++] = 9;
\r
90 while (sym < 280) state->lens[sym++] = 7;
\r
91 while (sym < 288) state->lens[sym++] = 8;
\r
95 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
\r
97 /* distance table */
\r
99 while (sym < 32) state->lens[sym++] = 5;
\r
102 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
\r
104 /* do this just once */
\r
107 #else /* !BUILDFIXED */
\r
108 # include "inffixed.h"
\r
109 #endif /* BUILDFIXED */
\r
110 state->lencode = lenfix;
\r
111 state->lenbits = 9;
\r
112 state->distcode = distfix;
\r
113 state->distbits = 5;
\r
116 /* Macros for inflateBack(): */
\r
118 /* Load returned state from inflate_fast() */
\r
121 put = strm->next_out; \
\r
122 left = strm->avail_out; \
\r
123 next = strm->next_in; \
\r
124 have = strm->avail_in; \
\r
125 hold = state->hold; \
\r
126 bits = state->bits; \
\r
129 /* Set state from registers for inflate_fast() */
\r
130 #define RESTORE() \
\r
132 strm->next_out = put; \
\r
133 strm->avail_out = left; \
\r
134 strm->next_in = next; \
\r
135 strm->avail_in = have; \
\r
136 state->hold = hold; \
\r
137 state->bits = bits; \
\r
140 /* Clear the input bit accumulator */
\r
141 #define INITBITS() \
\r
147 /* Assure that some input is available. If input is requested, but denied,
\r
148 then return a Z_BUF_ERROR from inflateBack(). */
\r
152 have = in(in_desc, &next); \
\r
155 ret = Z_BUF_ERROR; \
\r
161 /* Get a byte of input into the bit accumulator, or return from inflateBack()
\r
162 with an error if there is no input available. */
\r
163 #define PULLBYTE() \
\r
167 hold += (unsigned long)(*next++) << bits; \
\r
171 /* Assure that there are at least n bits in the bit accumulator. If there is
\r
172 not enough available input to do that, then return from inflateBack() with
\r
174 #define NEEDBITS(n) \
\r
176 while (bits < (unsigned)(n)) \
\r
180 /* Return the low n bits of the bit accumulator (n < 16) */
\r
182 ((unsigned)hold & ((1U << (n)) - 1))
\r
184 /* Remove n bits from the bit accumulator */
\r
185 #define DROPBITS(n) \
\r
188 bits -= (unsigned)(n); \
\r
191 /* Remove zero to seven bits as needed to go to a byte boundary */
\r
192 #define BYTEBITS() \
\r
194 hold >>= bits & 7; \
\r
195 bits -= bits & 7; \
\r
198 /* Assure that some output space is available, by writing out the window
\r
199 if it's full. If the write fails, return from inflateBack() with a
\r
204 put = state->window; \
\r
205 left = state->wsize; \
\r
206 state->whave = left; \
\r
207 if (out(out_desc, put, left)) { \
\r
208 ret = Z_BUF_ERROR; \
\r
215 strm provides the memory allocation functions and window buffer on input,
\r
216 and provides information on the unused input on return. For Z_DATA_ERROR
\r
217 returns, strm will also provide an error message.
\r
219 in() and out() are the call-back input and output functions. When
\r
220 inflateBack() needs more input, it calls in(). When inflateBack() has
\r
221 filled the window with output, or when it completes with data in the
\r
222 window, it calls out() to write out the data. The application must not
\r
223 change the provided input until in() is called again or inflateBack()
\r
224 returns. The application must not change the window/output buffer until
\r
225 inflateBack() returns.
\r
227 in() and out() are called with a descriptor parameter provided in the
\r
228 inflateBack() call. This parameter can be a structure that provides the
\r
229 information required to do the read or write, as well as accumulated
\r
230 information on the input and output such as totals and check values.
\r
232 in() should return zero on failure. out() should return non-zero on
\r
233 failure. If either in() or out() fails, than inflateBack() returns a
\r
234 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
\r
235 was in() or out() that caused in the error. Otherwise, inflateBack()
\r
236 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
\r
237 error, or Z_MEM_ERROR if it could not allocate memory for the state.
\r
238 inflateBack() can also return Z_STREAM_ERROR if the input parameters
\r
239 are not correct, i.e. strm is Z_NULL or the state was not initialized.
\r
241 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
\r
246 void FAR *out_desc;
\r
248 struct inflate_state FAR *state;
\r
249 unsigned char FAR *next; /* next input */
\r
250 unsigned char FAR *put; /* next output */
\r
251 unsigned have, left; /* available input and output */
\r
252 unsigned long hold; /* bit buffer */
\r
253 unsigned bits; /* bits in bit buffer */
\r
254 unsigned copy; /* number of stored or match bytes to copy */
\r
255 unsigned char FAR *from; /* where to copy match bytes from */
\r
256 code here; /* current decoding table entry */
\r
257 code last; /* parent table entry */
\r
258 unsigned len; /* length to copy for repeats, bits to drop */
\r
259 int ret; /* return code */
\r
260 static const unsigned short order[19] = /* permutation of code lengths */
\r
261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
\r
263 /* Check that the strm exists and that the state was initialized */
\r
264 if (strm == Z_NULL || strm->state == Z_NULL)
\r
265 return Z_STREAM_ERROR;
\r
266 state = (struct inflate_state FAR *)strm->state;
\r
268 /* Reset the state */
\r
269 strm->msg = Z_NULL;
\r
270 state->mode = TYPE;
\r
273 next = strm->next_in;
\r
274 have = next != Z_NULL ? strm->avail_in : 0;
\r
277 put = state->window;
\r
278 left = state->wsize;
\r
280 /* Inflate until end of block marked as last */
\r
282 switch (state->mode) {
\r
284 /* determine and dispatch block type */
\r
287 state->mode = DONE;
\r
291 state->last = BITS(1);
\r
294 case 0: /* stored block */
\r
295 Tracev((stderr, "inflate: stored block%s\n",
\r
296 state->last ? " (last)" : ""));
\r
297 state->mode = STORED;
\r
299 case 1: /* fixed block */
\r
300 fixedtables(state);
\r
301 Tracev((stderr, "inflate: fixed codes block%s\n",
\r
302 state->last ? " (last)" : ""));
\r
303 state->mode = LEN; /* decode codes */
\r
305 case 2: /* dynamic block */
\r
306 Tracev((stderr, "inflate: dynamic codes block%s\n",
\r
307 state->last ? " (last)" : ""));
\r
308 state->mode = TABLE;
\r
311 strm->msg = (char *)"invalid block type";
\r
318 /* get and verify stored block length */
\r
319 BYTEBITS(); /* go to byte boundary */
\r
321 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
\r
322 strm->msg = (char *)"invalid stored block lengths";
\r
326 state->length = (unsigned)hold & 0xffff;
\r
327 Tracev((stderr, "inflate: stored length %u\n",
\r
331 /* copy stored block from input to output */
\r
332 while (state->length != 0) {
\r
333 copy = state->length;
\r
336 if (copy > have) copy = have;
\r
337 if (copy > left) copy = left;
\r
338 zmemcpy(put, next, copy);
\r
343 state->length -= copy;
\r
345 Tracev((stderr, "inflate: stored end\n"));
\r
346 state->mode = TYPE;
\r
350 /* get dynamic table entries descriptor */
\r
352 state->nlen = BITS(5) + 257;
\r
354 state->ndist = BITS(5) + 1;
\r
356 state->ncode = BITS(4) + 4;
\r
358 #ifndef PKZIP_BUG_WORKAROUND
\r
359 if (state->nlen > 286 || state->ndist > 30) {
\r
360 strm->msg = (char *)"too many length or distance symbols";
\r
365 Tracev((stderr, "inflate: table sizes ok\n"));
\r
367 /* get code length code lengths (not a typo) */
\r
369 while (state->have < state->ncode) {
\r
371 state->lens[order[state->have++]] = (unsigned short)BITS(3);
\r
374 while (state->have < 19)
\r
375 state->lens[order[state->have++]] = 0;
\r
376 state->next = state->codes;
\r
377 state->lencode = (code const FAR *)(state->next);
\r
378 state->lenbits = 7;
\r
379 ret = inflate_table(CODES, state->lens, 19, &(state->next),
\r
380 &(state->lenbits), state->work);
\r
382 strm->msg = (char *)"invalid code lengths set";
\r
386 Tracev((stderr, "inflate: code lengths ok\n"));
\r
388 /* get length and distance code code lengths */
\r
390 while (state->have < state->nlen + state->ndist) {
\r
392 here = state->lencode[BITS(state->lenbits)];
\r
393 if ((unsigned)(here.bits) <= bits) break;
\r
396 if (here.val < 16) {
\r
397 NEEDBITS(here.bits);
\r
398 DROPBITS(here.bits);
\r
399 state->lens[state->have++] = here.val;
\r
402 if (here.val == 16) {
\r
403 NEEDBITS(here.bits + 2);
\r
404 DROPBITS(here.bits);
\r
405 if (state->have == 0) {
\r
406 strm->msg = (char *)"invalid bit length repeat";
\r
410 len = (unsigned)(state->lens[state->have - 1]);
\r
411 copy = 3 + BITS(2);
\r
414 else if (here.val == 17) {
\r
415 NEEDBITS(here.bits + 3);
\r
416 DROPBITS(here.bits);
\r
418 copy = 3 + BITS(3);
\r
422 NEEDBITS(here.bits + 7);
\r
423 DROPBITS(here.bits);
\r
425 copy = 11 + BITS(7);
\r
428 if (state->have + copy > state->nlen + state->ndist) {
\r
429 strm->msg = (char *)"invalid bit length repeat";
\r
434 state->lens[state->have++] = (unsigned short)len;
\r
438 /* handle error breaks in while */
\r
439 if (state->mode == BAD) break;
\r
441 /* check for end-of-block code (better have one) */
\r
442 if (state->lens[256] == 0) {
\r
443 strm->msg = (char *)"invalid code -- missing end-of-block";
\r
448 /* build code tables -- note: do not change the lenbits or distbits
\r
449 values here (9 and 6) without reading the comments in inftrees.h
\r
450 concerning the ENOUGH constants, which depend on those values */
\r
451 state->next = state->codes;
\r
452 state->lencode = (code const FAR *)(state->next);
\r
453 state->lenbits = 9;
\r
454 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
\r
455 &(state->lenbits), state->work);
\r
457 strm->msg = (char *)"invalid literal/lengths set";
\r
461 state->distcode = (code const FAR *)(state->next);
\r
462 state->distbits = 6;
\r
463 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
\r
464 &(state->next), &(state->distbits), state->work);
\r
466 strm->msg = (char *)"invalid distances set";
\r
470 Tracev((stderr, "inflate: codes ok\n"));
\r
474 /* use inflate_fast() if we have enough input and output */
\r
475 if (have >= 6 && left >= 258) {
\r
477 if (state->whave < state->wsize)
\r
478 state->whave = state->wsize - left;
\r
479 inflate_fast(strm, state->wsize);
\r
484 /* get a literal, length, or end-of-block code */
\r
486 here = state->lencode[BITS(state->lenbits)];
\r
487 if ((unsigned)(here.bits) <= bits) break;
\r
490 if (here.op && (here.op & 0xf0) == 0) {
\r
493 here = state->lencode[last.val +
\r
494 (BITS(last.bits + last.op) >> last.bits)];
\r
495 if ((unsigned)(last.bits + here.bits) <= bits) break;
\r
498 DROPBITS(last.bits);
\r
500 DROPBITS(here.bits);
\r
501 state->length = (unsigned)here.val;
\r
503 /* process literal */
\r
504 if (here.op == 0) {
\r
505 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
\r
506 "inflate: literal '%c'\n" :
\r
507 "inflate: literal 0x%02x\n", here.val));
\r
509 *put++ = (unsigned char)(state->length);
\r
515 /* process end of block */
\r
516 if (here.op & 32) {
\r
517 Tracevv((stderr, "inflate: end of block\n"));
\r
518 state->mode = TYPE;
\r
523 if (here.op & 64) {
\r
524 strm->msg = (char *)"invalid literal/length code";
\r
529 /* length code -- get extra bits, if any */
\r
530 state->extra = (unsigned)(here.op) & 15;
\r
531 if (state->extra != 0) {
\r
532 NEEDBITS(state->extra);
\r
533 state->length += BITS(state->extra);
\r
534 DROPBITS(state->extra);
\r
536 Tracevv((stderr, "inflate: length %u\n", state->length));
\r
538 /* get distance code */
\r
540 here = state->distcode[BITS(state->distbits)];
\r
541 if ((unsigned)(here.bits) <= bits) break;
\r
544 if ((here.op & 0xf0) == 0) {
\r
547 here = state->distcode[last.val +
\r
548 (BITS(last.bits + last.op) >> last.bits)];
\r
549 if ((unsigned)(last.bits + here.bits) <= bits) break;
\r
552 DROPBITS(last.bits);
\r
554 DROPBITS(here.bits);
\r
555 if (here.op & 64) {
\r
556 strm->msg = (char *)"invalid distance code";
\r
560 state->offset = (unsigned)here.val;
\r
562 /* get distance extra bits, if any */
\r
563 state->extra = (unsigned)(here.op) & 15;
\r
564 if (state->extra != 0) {
\r
565 NEEDBITS(state->extra);
\r
566 state->offset += BITS(state->extra);
\r
567 DROPBITS(state->extra);
\r
569 if (state->offset > state->wsize - (state->whave < state->wsize ?
\r
571 strm->msg = (char *)"invalid distance too far back";
\r
575 Tracevv((stderr, "inflate: distance %u\n", state->offset));
\r
577 /* copy match from window to output */
\r
580 copy = state->wsize - state->offset;
\r
583 copy = left - copy;
\r
586 from = put - state->offset;
\r
589 if (copy > state->length) copy = state->length;
\r
590 state->length -= copy;
\r
595 } while (state->length != 0);
\r
599 /* inflate stream terminated properly -- write leftover output */
\r
600 ret = Z_STREAM_END;
\r
601 if (left < state->wsize) {
\r
602 if (out(out_desc, state->window, state->wsize - left))
\r
608 ret = Z_DATA_ERROR;
\r
611 default: /* can't happen, but makes compilers happy */
\r
612 ret = Z_STREAM_ERROR;
\r
616 /* Return unused input */
\r
618 strm->next_in = next;
\r
619 strm->avail_in = have;
\r
623 int ZEXPORT inflateBackEnd(strm)
\r
626 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
\r
627 return Z_STREAM_ERROR;
\r
628 ZFREE(strm, strm->state);
\r
629 strm->state = Z_NULL;
\r
630 Tracev((stderr, "inflate: end\n"));
\r