1 /* infback9.c -- inflate deflate64 data using a call-back interface
2 * Copyright (C) 1995-2008 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
14 strm provides memory allocation functions in zalloc and zfree, or
15 Z_NULL to use the library memory allocation functions.
17 window is a user-supplied window and output buffer that is 64K bytes.
19 int ZEXPORT inflateBack9Init_(z_stream FAR *strm, unsigned char FAR *window,
20 const char *version, int stream_size) {
21 struct inflate_state FAR *state;
23 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
24 stream_size != (int)(sizeof(z_stream)))
25 return Z_VERSION_ERROR;
26 if (strm == Z_NULL || window == Z_NULL)
27 return Z_STREAM_ERROR;
28 strm->msg = Z_NULL; /* in case we return an error */
29 if (strm->zalloc == (alloc_func)0) {
30 strm->zalloc = zcalloc;
31 strm->opaque = (voidpf)0;
33 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
34 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
35 sizeof(struct inflate_state));
36 if (state == Z_NULL) return Z_MEM_ERROR;
37 Tracev((stderr, "inflate: allocated\n"));
38 strm->state = (voidpf)state;
39 state->window = window;
44 Build and output length and distance decoding tables for fixed code
50 void makefixed9(void) {
51 unsigned sym, bits, low, size;
52 code *next, *lenfix, *distfix;
53 struct inflate_state state;
56 /* literal/length table */
58 while (sym < 144) state.lens[sym++] = 8;
59 while (sym < 256) state.lens[sym++] = 9;
60 while (sym < 280) state.lens[sym++] = 7;
61 while (sym < 288) state.lens[sym++] = 8;
65 inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
69 while (sym < 32) state.lens[sym++] = 5;
72 inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
75 puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
76 puts(" * Generated automatically by makefixed9().");
79 puts(" /* WARNING: this file should *not* be used by applications.");
80 puts(" It is part of the implementation of this library and is");
81 puts(" subject to change. Applications should only use zlib.h.");
85 printf(" static const code lenfix[%u] = {", size);
88 if ((low % 6) == 0) printf("\n ");
89 printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
91 if (++low == size) break;
96 printf("\n static const code distfix[%u] = {", size);
99 if ((low % 5) == 0) printf("\n ");
100 printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
102 if (++low == size) break;
107 #endif /* MAKEFIXED */
109 /* Macros for inflateBack(): */
111 /* Clear the input bit accumulator */
118 /* Assure that some input is available. If input is requested, but denied,
119 then return a Z_BUF_ERROR from inflateBack(). */
123 have = in(in_desc, &next); \
132 /* Get a byte of input into the bit accumulator, or return from inflateBack()
133 with an error if there is no input available. */
138 hold += (unsigned long)(*next++) << bits; \
142 /* Assure that there are at least n bits in the bit accumulator. If there is
143 not enough available input to do that, then return from inflateBack() with
145 #define NEEDBITS(n) \
147 while (bits < (unsigned)(n)) \
151 /* Return the low n bits of the bit accumulator (n <= 16) */
153 ((unsigned)hold & ((1U << (n)) - 1))
155 /* Remove n bits from the bit accumulator */
156 #define DROPBITS(n) \
159 bits -= (unsigned)(n); \
162 /* Remove zero to seven bits as needed to go to a byte boundary */
169 /* Assure that some output space is available, by writing out the window
170 if it's full. If the write fails, return from inflateBack() with a
178 if (out(out_desc, put, (unsigned)left)) { \
186 strm provides the memory allocation functions and window buffer on input,
187 and provides information on the unused input on return. For Z_DATA_ERROR
188 returns, strm will also provide an error message.
190 in() and out() are the call-back input and output functions. When
191 inflateBack() needs more input, it calls in(). When inflateBack() has
192 filled the window with output, or when it completes with data in the
193 window, it calls out() to write out the data. The application must not
194 change the provided input until in() is called again or inflateBack()
195 returns. The application must not change the window/output buffer until
196 inflateBack() returns.
198 in() and out() are called with a descriptor parameter provided in the
199 inflateBack() call. This parameter can be a structure that provides the
200 information required to do the read or write, as well as accumulated
201 information on the input and output such as totals and check values.
203 in() should return zero on failure. out() should return non-zero on
204 failure. If either in() or out() fails, than inflateBack() returns a
205 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
206 was in() or out() that caused in the error. Otherwise, inflateBack()
207 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
208 error, or Z_MEM_ERROR if it could not allocate memory for the state.
209 inflateBack() can also return Z_STREAM_ERROR if the input parameters
210 are not correct, i.e. strm is Z_NULL or the state was not initialized.
212 int ZEXPORT inflateBack9(z_stream FAR *strm, in_func in, void FAR *in_desc,
213 out_func out, void FAR *out_desc) {
214 struct inflate_state FAR *state;
215 z_const unsigned char FAR *next; /* next input */
216 unsigned char FAR *put; /* next output */
217 unsigned have; /* available input */
218 unsigned long left; /* available output */
219 inflate_mode mode; /* current inflate mode */
220 int lastblock; /* true if processing last block */
221 int wrap; /* true if the window has wrapped */
222 unsigned char FAR *window; /* allocated sliding window, if needed */
223 unsigned long hold; /* bit buffer */
224 unsigned bits; /* bits in bit buffer */
225 unsigned extra; /* extra bits needed */
226 unsigned long length; /* literal or length of data to copy */
227 unsigned long offset; /* distance back to copy string from */
228 unsigned long copy; /* number of stored or match bytes to copy */
229 unsigned char FAR *from; /* where to copy match bytes from */
230 code const FAR *lencode; /* starting table for length/literal codes */
231 code const FAR *distcode; /* starting table for distance codes */
232 unsigned lenbits; /* index bits for lencode */
233 unsigned distbits; /* index bits for distcode */
234 code here; /* current decoding table entry */
235 code last; /* parent table entry */
236 unsigned len; /* length to copy for repeats, bits to drop */
237 int ret; /* return code */
238 static const unsigned short order[19] = /* permutation of code lengths */
239 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
242 /* Check that the strm exists and that the state was initialized */
243 if (strm == Z_NULL || strm->state == Z_NULL)
244 return Z_STREAM_ERROR;
245 state = (struct inflate_state FAR *)strm->state;
247 /* Reset the state */
252 window = state->window;
253 next = strm->next_in;
254 have = next != Z_NULL ? strm->avail_in : 0;
262 /* Inflate until end of block marked as last */
266 /* determine and dispatch block type */
276 case 0: /* stored block */
277 Tracev((stderr, "inflate: stored block%s\n",
278 lastblock ? " (last)" : ""));
281 case 1: /* fixed block */
286 Tracev((stderr, "inflate: fixed codes block%s\n",
287 lastblock ? " (last)" : ""));
288 mode = LEN; /* decode codes */
290 case 2: /* dynamic block */
291 Tracev((stderr, "inflate: dynamic codes block%s\n",
292 lastblock ? " (last)" : ""));
296 strm->msg = (char *)"invalid block type";
303 /* get and verify stored block length */
304 BYTEBITS(); /* go to byte boundary */
306 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
307 strm->msg = (char *)"invalid stored block lengths";
311 length = (unsigned)hold & 0xffff;
312 Tracev((stderr, "inflate: stored length %lu\n",
316 /* copy stored block from input to output */
317 while (length != 0) {
321 if (copy > have) copy = have;
322 if (copy > left) copy = left;
323 zmemcpy(put, next, copy);
330 Tracev((stderr, "inflate: stored end\n"));
335 /* get dynamic table entries descriptor */
337 state->nlen = BITS(5) + 257;
339 state->ndist = BITS(5) + 1;
341 state->ncode = BITS(4) + 4;
343 if (state->nlen > 286) {
344 strm->msg = (char *)"too many length symbols";
348 Tracev((stderr, "inflate: table sizes ok\n"));
350 /* get code length code lengths (not a typo) */
352 while (state->have < state->ncode) {
354 state->lens[order[state->have++]] = (unsigned short)BITS(3);
357 while (state->have < 19)
358 state->lens[order[state->have++]] = 0;
359 state->next = state->codes;
360 lencode = (code const FAR *)(state->next);
362 ret = inflate_table9(CODES, state->lens, 19, &(state->next),
363 &(lenbits), state->work);
365 strm->msg = (char *)"invalid code lengths set";
369 Tracev((stderr, "inflate: code lengths ok\n"));
371 /* get length and distance code code lengths */
373 while (state->have < state->nlen + state->ndist) {
375 here = lencode[BITS(lenbits)];
376 if ((unsigned)(here.bits) <= bits) break;
382 state->lens[state->have++] = here.val;
385 if (here.val == 16) {
386 NEEDBITS(here.bits + 2);
388 if (state->have == 0) {
389 strm->msg = (char *)"invalid bit length repeat";
393 len = (unsigned)(state->lens[state->have - 1]);
397 else if (here.val == 17) {
398 NEEDBITS(here.bits + 3);
405 NEEDBITS(here.bits + 7);
411 if (state->have + copy > state->nlen + state->ndist) {
412 strm->msg = (char *)"invalid bit length repeat";
417 state->lens[state->have++] = (unsigned short)len;
421 /* handle error breaks in while */
422 if (mode == BAD) break;
424 /* check for end-of-block code (better have one) */
425 if (state->lens[256] == 0) {
426 strm->msg = (char *)"invalid code -- missing end-of-block";
431 /* build code tables -- note: do not change the lenbits or distbits
432 values here (9 and 6) without reading the comments in inftree9.h
433 concerning the ENOUGH constants, which depend on those values */
434 state->next = state->codes;
435 lencode = (code const FAR *)(state->next);
437 ret = inflate_table9(LENS, state->lens, state->nlen,
438 &(state->next), &(lenbits), state->work);
440 strm->msg = (char *)"invalid literal/lengths set";
444 distcode = (code const FAR *)(state->next);
446 ret = inflate_table9(DISTS, state->lens + state->nlen,
447 state->ndist, &(state->next), &(distbits),
450 strm->msg = (char *)"invalid distances set";
454 Tracev((stderr, "inflate: codes ok\n"));
458 /* get a literal, length, or end-of-block code */
460 here = lencode[BITS(lenbits)];
461 if ((unsigned)(here.bits) <= bits) break;
464 if (here.op && (here.op & 0xf0) == 0) {
467 here = lencode[last.val +
468 (BITS(last.bits + last.op) >> last.bits)];
469 if ((unsigned)(last.bits + here.bits) <= bits) break;
475 length = (unsigned)here.val;
477 /* process literal */
479 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
480 "inflate: literal '%c'\n" :
481 "inflate: literal 0x%02x\n", here.val));
483 *put++ = (unsigned char)(length);
489 /* process end of block */
491 Tracevv((stderr, "inflate: end of block\n"));
498 strm->msg = (char *)"invalid literal/length code";
503 /* length code -- get extra bits, if any */
504 extra = (unsigned)(here.op) & 31;
507 length += BITS(extra);
510 Tracevv((stderr, "inflate: length %lu\n", length));
512 /* get distance code */
514 here = distcode[BITS(distbits)];
515 if ((unsigned)(here.bits) <= bits) break;
518 if ((here.op & 0xf0) == 0) {
521 here = distcode[last.val +
522 (BITS(last.bits + last.op) >> last.bits)];
523 if ((unsigned)(last.bits + here.bits) <= bits) break;
530 strm->msg = (char *)"invalid distance code";
534 offset = (unsigned)here.val;
536 /* get distance extra bits, if any */
537 extra = (unsigned)(here.op) & 15;
540 offset += BITS(extra);
543 if (offset > WSIZE - (wrap ? 0: left)) {
544 strm->msg = (char *)"invalid distance too far back";
548 Tracevv((stderr, "inflate: distance %lu\n", offset));
550 /* copy match from window to output */
553 copy = WSIZE - offset;
562 if (copy > length) copy = length;
568 } while (length != 0);
572 /* inflate stream terminated properly -- write leftover output */
575 if (out(out_desc, window, (unsigned)(WSIZE - left)))
584 default: /* can't happen, but makes compilers happy */
585 ret = Z_STREAM_ERROR;
589 /* Return unused input */
591 strm->next_in = next;
592 strm->avail_in = have;
596 int ZEXPORT inflateBack9End(z_stream FAR *strm) {
597 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
598 return Z_STREAM_ERROR;
599 ZFREE(strm, strm->state);
600 strm->state = Z_NULL;
601 Tracev((stderr, "inflate: end\n"));