1 /* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2022 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_(strm, windowBits, window, version, stream_size)
31 unsigned char FAR *window;
35 struct inflate_state FAR *state;
37 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38 stream_size != (int)(sizeof(z_stream)))
39 return Z_VERSION_ERROR;
40 if (strm == Z_NULL || window == Z_NULL ||
41 windowBits < 8 || windowBits > 15)
42 return Z_STREAM_ERROR;
43 strm->msg = Z_NULL; /* in case we return an error */
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));
60 if (state == Z_NULL) return Z_MEM_ERROR;
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;
74 Return state with length and distance decoding tables and index sizes set to
75 fixed code decoding. Normally this returns fixed tables from inffixed.h.
76 If BUILDFIXED is defined, then instead this routine builds the tables the
77 first time it's called, and returns those tables the first time and
78 thereafter. This reduces the size of the code by about 2K bytes, in
79 exchange for a little execution time. However, BUILDFIXED should not be
80 used for threaded applications, since the rewriting of the tables and virgin
81 may not be thread-safe.
83 local void fixedtables(state)
84 struct inflate_state FAR *state;
87 static int virgin = 1;
88 static code *lenfix, *distfix;
89 static code fixed[544];
91 /* build fixed huffman tables if first call (may not be thread safe) */
96 /* literal/length table */
98 while (sym < 144) state->lens[sym++] = 8;
99 while (sym < 256) state->lens[sym++] = 9;
100 while (sym < 280) state->lens[sym++] = 7;
101 while (sym < 288) state->lens[sym++] = 8;
105 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
109 while (sym < 32) state->lens[sym++] = 5;
112 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
114 /* do this just once */
117 #else /* !BUILDFIXED */
118 # include "inffixed.h"
119 #endif /* BUILDFIXED */
120 state->lencode = lenfix;
122 state->distcode = distfix;
126 /* Macros for inflateBack(): */
128 /* Load returned state from inflate_fast() */
131 put = strm->next_out; \
132 left = strm->avail_out; \
133 next = strm->next_in; \
134 have = strm->avail_in; \
135 hold = state->hold; \
136 bits = state->bits; \
139 /* Set state from registers for inflate_fast() */
142 strm->next_out = put; \
143 strm->avail_out = left; \
144 strm->next_in = next; \
145 strm->avail_in = have; \
146 state->hold = hold; \
147 state->bits = bits; \
150 /* Clear the input bit accumulator */
157 /* Assure that some input is available. If input is requested, but denied,
158 then return a Z_BUF_ERROR from inflateBack(). */
162 have = in(in_desc, &next); \
171 /* Get a byte of input into the bit accumulator, or return from inflateBack()
172 with an error if there is no input available. */
177 hold += (unsigned long)(*next++) << bits; \
181 /* Assure that there are at least n bits in the bit accumulator. If there is
182 not enough available input to do that, then return from inflateBack() with
184 #define NEEDBITS(n) \
186 while (bits < (unsigned)(n)) \
190 /* Return the low n bits of the bit accumulator (n < 16) */
192 ((unsigned)hold & ((1U << (n)) - 1))
194 /* Remove n bits from the bit accumulator */
195 #define DROPBITS(n) \
198 bits -= (unsigned)(n); \
201 /* Remove zero to seven bits as needed to go to a byte boundary */
208 /* Assure that some output space is available, by writing out the window
209 if it's full. If the write fails, return from inflateBack() with a
214 put = state->window; \
215 left = state->wsize; \
216 state->whave = left; \
217 if (out(out_desc, put, left)) { \
225 strm provides the memory allocation functions and window buffer on input,
226 and provides information on the unused input on return. For Z_DATA_ERROR
227 returns, strm will also provide an error message.
229 in() and out() are the call-back input and output functions. When
230 inflateBack() needs more input, it calls in(). When inflateBack() has
231 filled the window with output, or when it completes with data in the
232 window, it calls out() to write out the data. The application must not
233 change the provided input until in() is called again or inflateBack()
234 returns. The application must not change the window/output buffer until
235 inflateBack() returns.
237 in() and out() are called with a descriptor parameter provided in the
238 inflateBack() call. This parameter can be a structure that provides the
239 information required to do the read or write, as well as accumulated
240 information on the input and output such as totals and check values.
242 in() should return zero on failure. out() should return non-zero on
243 failure. If either in() or out() fails, than inflateBack() returns a
244 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
245 was in() or out() that caused in the error. Otherwise, inflateBack()
246 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
247 error, or Z_MEM_ERROR if it could not allocate memory for the state.
248 inflateBack() can also return Z_STREAM_ERROR if the input parameters
249 are not correct, i.e. strm is Z_NULL or the state was not initialized.
251 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
258 struct inflate_state FAR *state;
259 z_const unsigned char FAR *next; /* next input */
260 unsigned char FAR *put; /* next output */
261 unsigned have, left; /* available input and output */
262 unsigned long hold; /* bit buffer */
263 unsigned bits; /* bits in bit buffer */
264 unsigned copy; /* number of stored or match bytes to copy */
265 unsigned char FAR *from; /* where to copy match bytes from */
266 code here; /* current decoding table entry */
267 code last; /* parent table entry */
268 unsigned len; /* length to copy for repeats, bits to drop */
269 int ret; /* return code */
270 static const unsigned short order[19] = /* permutation of code lengths */
271 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
273 /* Check that the strm exists and that the state was initialized */
274 if (strm == Z_NULL || strm->state == Z_NULL)
275 return Z_STREAM_ERROR;
276 state = (struct inflate_state FAR *)strm->state;
278 /* Reset the state */
283 next = strm->next_in;
284 have = next != Z_NULL ? strm->avail_in : 0;
290 /* Inflate until end of block marked as last */
292 switch (state->mode) {
294 /* determine and dispatch block type */
301 state->last = BITS(1);
304 case 0: /* stored block */
305 Tracev((stderr, "inflate: stored block%s\n",
306 state->last ? " (last)" : ""));
307 state->mode = STORED;
309 case 1: /* fixed block */
311 Tracev((stderr, "inflate: fixed codes block%s\n",
312 state->last ? " (last)" : ""));
313 state->mode = LEN; /* decode codes */
315 case 2: /* dynamic block */
316 Tracev((stderr, "inflate: dynamic codes block%s\n",
317 state->last ? " (last)" : ""));
321 strm->msg = (char *)"invalid block type";
328 /* get and verify stored block length */
329 BYTEBITS(); /* go to byte boundary */
331 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
332 strm->msg = (char *)"invalid stored block lengths";
336 state->length = (unsigned)hold & 0xffff;
337 Tracev((stderr, "inflate: stored length %u\n",
341 /* copy stored block from input to output */
342 while (state->length != 0) {
343 copy = state->length;
346 if (copy > have) copy = have;
347 if (copy > left) copy = left;
348 zmemcpy(put, next, copy);
353 state->length -= copy;
355 Tracev((stderr, "inflate: stored end\n"));
360 /* get dynamic table entries descriptor */
362 state->nlen = BITS(5) + 257;
364 state->ndist = BITS(5) + 1;
366 state->ncode = BITS(4) + 4;
368 #ifndef PKZIP_BUG_WORKAROUND
369 if (state->nlen > 286 || state->ndist > 30) {
370 strm->msg = (char *)"too many length or distance symbols";
375 Tracev((stderr, "inflate: table sizes ok\n"));
377 /* get code length code lengths (not a typo) */
379 while (state->have < state->ncode) {
381 state->lens[order[state->have++]] = (unsigned short)BITS(3);
384 while (state->have < 19)
385 state->lens[order[state->have++]] = 0;
386 state->next = state->codes;
387 state->lencode = (code const FAR *)(state->next);
389 ret = inflate_table(CODES, state->lens, 19, &(state->next),
390 &(state->lenbits), state->work);
392 strm->msg = (char *)"invalid code lengths set";
396 Tracev((stderr, "inflate: code lengths ok\n"));
398 /* get length and distance code code lengths */
400 while (state->have < state->nlen + state->ndist) {
402 here = state->lencode[BITS(state->lenbits)];
403 if ((unsigned)(here.bits) <= bits) break;
408 state->lens[state->have++] = here.val;
411 if (here.val == 16) {
412 NEEDBITS(here.bits + 2);
414 if (state->have == 0) {
415 strm->msg = (char *)"invalid bit length repeat";
419 len = (unsigned)(state->lens[state->have - 1]);
423 else if (here.val == 17) {
424 NEEDBITS(here.bits + 3);
431 NEEDBITS(here.bits + 7);
437 if (state->have + copy > state->nlen + state->ndist) {
438 strm->msg = (char *)"invalid bit length repeat";
443 state->lens[state->have++] = (unsigned short)len;
447 /* handle error breaks in while */
448 if (state->mode == BAD) break;
450 /* check for end-of-block code (better have one) */
451 if (state->lens[256] == 0) {
452 strm->msg = (char *)"invalid code -- missing end-of-block";
457 /* build code tables -- note: do not change the lenbits or distbits
458 values here (9 and 6) without reading the comments in inftrees.h
459 concerning the ENOUGH constants, which depend on those values */
460 state->next = state->codes;
461 state->lencode = (code const FAR *)(state->next);
463 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
464 &(state->lenbits), state->work);
466 strm->msg = (char *)"invalid literal/lengths set";
470 state->distcode = (code const FAR *)(state->next);
472 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
473 &(state->next), &(state->distbits), state->work);
475 strm->msg = (char *)"invalid distances set";
479 Tracev((stderr, "inflate: codes ok\n"));
484 /* use inflate_fast() if we have enough input and output */
485 if (have >= 6 && left >= 258) {
487 if (state->whave < state->wsize)
488 state->whave = state->wsize - left;
489 inflate_fast(strm, state->wsize);
494 /* get a literal, length, or end-of-block code */
496 here = state->lencode[BITS(state->lenbits)];
497 if ((unsigned)(here.bits) <= bits) break;
500 if (here.op && (here.op & 0xf0) == 0) {
503 here = state->lencode[last.val +
504 (BITS(last.bits + last.op) >> last.bits)];
505 if ((unsigned)(last.bits + here.bits) <= bits) break;
511 state->length = (unsigned)here.val;
513 /* process literal */
515 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
516 "inflate: literal '%c'\n" :
517 "inflate: literal 0x%02x\n", here.val));
519 *put++ = (unsigned char)(state->length);
525 /* process end of block */
527 Tracevv((stderr, "inflate: end of block\n"));
534 strm->msg = (char *)"invalid literal/length code";
539 /* length code -- get extra bits, if any */
540 state->extra = (unsigned)(here.op) & 15;
541 if (state->extra != 0) {
542 NEEDBITS(state->extra);
543 state->length += BITS(state->extra);
544 DROPBITS(state->extra);
546 Tracevv((stderr, "inflate: length %u\n", state->length));
548 /* get distance code */
550 here = state->distcode[BITS(state->distbits)];
551 if ((unsigned)(here.bits) <= bits) break;
554 if ((here.op & 0xf0) == 0) {
557 here = state->distcode[last.val +
558 (BITS(last.bits + last.op) >> last.bits)];
559 if ((unsigned)(last.bits + here.bits) <= bits) break;
566 strm->msg = (char *)"invalid distance code";
570 state->offset = (unsigned)here.val;
572 /* get distance extra bits, if any */
573 state->extra = (unsigned)(here.op) & 15;
574 if (state->extra != 0) {
575 NEEDBITS(state->extra);
576 state->offset += BITS(state->extra);
577 DROPBITS(state->extra);
579 if (state->offset > state->wsize - (state->whave < state->wsize ?
581 strm->msg = (char *)"invalid distance too far back";
585 Tracevv((stderr, "inflate: distance %u\n", state->offset));
587 /* copy match from window to output */
590 copy = state->wsize - state->offset;
596 from = put - state->offset;
599 if (copy > state->length) copy = state->length;
600 state->length -= copy;
605 } while (state->length != 0);
609 /* inflate stream terminated properly */
618 /* can't happen, but makes compilers happy */
619 ret = Z_STREAM_ERROR;
623 /* Write leftover output and return unused input */
625 if (left < state->wsize) {
626 if (out(out_desc, state->window, state->wsize - left) &&
630 strm->next_in = next;
631 strm->avail_in = have;
635 int ZEXPORT inflateBackEnd(strm)
638 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
639 return Z_STREAM_ERROR;
640 ZFREE(strm, strm->state);
641 strm->state = Z_NULL;
642 Tracev((stderr, "inflate: end\n"));