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_(strm
, window
, version
, stream_size
)
21 unsigned char FAR
*window
;
25 struct inflate_state FAR
*state
;
27 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
28 stream_size
!= (int)(sizeof(z_stream
)))
29 return Z_VERSION_ERROR
;
30 if (strm
== Z_NULL
|| window
== Z_NULL
)
31 return Z_STREAM_ERROR
;
32 strm
->msg
= Z_NULL
; /* in case we return an error */
33 if (strm
->zalloc
== (alloc_func
)0) {
34 strm
->zalloc
= zcalloc
;
35 strm
->opaque
= (voidpf
)0;
37 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
38 state
= (struct inflate_state FAR
*)ZALLOC(strm
, 1,
39 sizeof(struct inflate_state
));
40 if (state
== Z_NULL
) return Z_MEM_ERROR
;
41 Tracev((stderr
, "inflate: allocated\n"));
42 strm
->state
= (voidpf
)state
;
43 state
->window
= window
;
48 Build and output length and distance decoding tables for fixed code
56 unsigned sym
, bits
, low
, size
;
57 code
*next
, *lenfix
, *distfix
;
58 struct inflate_state state
;
61 /* literal/length table */
63 while (sym
< 144) state
.lens
[sym
++] = 8;
64 while (sym
< 256) state
.lens
[sym
++] = 9;
65 while (sym
< 280) state
.lens
[sym
++] = 7;
66 while (sym
< 288) state
.lens
[sym
++] = 8;
70 inflate_table9(LENS
, state
.lens
, 288, &(next
), &(bits
), state
.work
);
74 while (sym
< 32) state
.lens
[sym
++] = 5;
77 inflate_table9(DISTS
, state
.lens
, 32, &(next
), &(bits
), state
.work
);
80 puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
81 puts(" * Generated automatically by makefixed9().");
84 puts(" /* WARNING: this file should *not* be used by applications.");
85 puts(" It is part of the implementation of this library and is");
86 puts(" subject to change. Applications should only use zlib.h.");
90 printf(" static const code lenfix[%u] = {", size
);
93 if ((low
% 6) == 0) printf("\n ");
94 printf("{%u,%u,%d}", lenfix
[low
].op
, lenfix
[low
].bits
,
96 if (++low
== size
) break;
101 printf("\n static const code distfix[%u] = {", size
);
104 if ((low
% 5) == 0) printf("\n ");
105 printf("{%u,%u,%d}", distfix
[low
].op
, distfix
[low
].bits
,
107 if (++low
== size
) break;
112 #endif /* MAKEFIXED */
114 /* Macros for inflateBack(): */
116 /* Clear the input bit accumulator */
123 /* Assure that some input is available. If input is requested, but denied,
124 then return a Z_BUF_ERROR from inflateBack(). */
128 have = in(in_desc, &next); \
137 /* Get a byte of input into the bit accumulator, or return from inflateBack()
138 with an error if there is no input available. */
143 hold += (unsigned long)(*next++) << bits; \
147 /* Assure that there are at least n bits in the bit accumulator. If there is
148 not enough available input to do that, then return from inflateBack() with
150 #define NEEDBITS(n) \
152 while (bits < (unsigned)(n)) \
156 /* Return the low n bits of the bit accumulator (n <= 16) */
158 ((unsigned)hold & ((1U << (n)) - 1))
160 /* Remove n bits from the bit accumulator */
161 #define DROPBITS(n) \
164 bits -= (unsigned)(n); \
167 /* Remove zero to seven bits as needed to go to a byte boundary */
174 /* Assure that some output space is available, by writing out the window
175 if it's full. If the write fails, return from inflateBack() with a
183 if (out(out_desc, put, (unsigned)left)) { \
191 strm provides the memory allocation functions and window buffer on input,
192 and provides information on the unused input on return. For Z_DATA_ERROR
193 returns, strm will also provide an error message.
195 in() and out() are the call-back input and output functions. When
196 inflateBack() needs more input, it calls in(). When inflateBack() has
197 filled the window with output, or when it completes with data in the
198 window, it calls out() to write out the data. The application must not
199 change the provided input until in() is called again or inflateBack()
200 returns. The application must not change the window/output buffer until
201 inflateBack() returns.
203 in() and out() are called with a descriptor parameter provided in the
204 inflateBack() call. This parameter can be a structure that provides the
205 information required to do the read or write, as well as accumulated
206 information on the input and output such as totals and check values.
208 in() should return zero on failure. out() should return non-zero on
209 failure. If either in() or out() fails, than inflateBack() returns a
210 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
211 was in() or out() that caused in the error. Otherwise, inflateBack()
212 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
213 error, or Z_MEM_ERROR if it could not allocate memory for the state.
214 inflateBack() can also return Z_STREAM_ERROR if the input parameters
215 are not correct, i.e. strm is Z_NULL or the state was not initialized.
217 int ZEXPORT
inflateBack9(strm
, in
, in_desc
, out
, out_desc
)
224 struct inflate_state FAR
*state
;
225 z_const
unsigned char FAR
*next
; /* next input */
226 unsigned char FAR
*put
; /* next output */
227 unsigned have
; /* available input */
228 unsigned long left
; /* available output */
229 inflate_mode mode
; /* current inflate mode */
230 int lastblock
; /* true if processing last block */
231 int wrap
; /* true if the window has wrapped */
232 unsigned char FAR
*window
; /* allocated sliding window, if needed */
233 unsigned long hold
; /* bit buffer */
234 unsigned bits
; /* bits in bit buffer */
235 unsigned extra
; /* extra bits needed */
236 unsigned long length
; /* literal or length of data to copy */
237 unsigned long offset
; /* distance back to copy string from */
238 unsigned long copy
; /* number of stored or match bytes to copy */
239 unsigned char FAR
*from
; /* where to copy match bytes from */
240 code
const FAR
*lencode
; /* starting table for length/literal codes */
241 code
const FAR
*distcode
; /* starting table for distance codes */
242 unsigned lenbits
; /* index bits for lencode */
243 unsigned distbits
; /* index bits for distcode */
244 code here
; /* current decoding table entry */
245 code last
; /* parent table entry */
246 unsigned len
; /* length to copy for repeats, bits to drop */
247 int ret
; /* return code */
248 static const unsigned short order
[19] = /* permutation of code lengths */
249 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
252 /* Check that the strm exists and that the state was initialized */
253 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
)
254 return Z_STREAM_ERROR
;
255 state
= (struct inflate_state FAR
*)strm
->state
;
257 /* Reset the state */
262 window
= state
->window
;
263 next
= strm
->next_in
;
264 have
= next
!= Z_NULL
? strm
->avail_in
: 0;
272 /* Inflate until end of block marked as last */
276 /* determine and dispatch block type */
286 case 0: /* stored block */
287 Tracev((stderr
, "inflate: stored block%s\n",
288 lastblock
? " (last)" : ""));
291 case 1: /* fixed block */
296 Tracev((stderr
, "inflate: fixed codes block%s\n",
297 lastblock
? " (last)" : ""));
298 mode
= LEN
; /* decode codes */
300 case 2: /* dynamic block */
301 Tracev((stderr
, "inflate: dynamic codes block%s\n",
302 lastblock
? " (last)" : ""));
306 strm
->msg
= (char *)"invalid block type";
313 /* get and verify stored block length */
314 BYTEBITS(); /* go to byte boundary */
316 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
317 strm
->msg
= (char *)"invalid stored block lengths";
321 length
= (unsigned)hold
& 0xffff;
322 Tracev((stderr
, "inflate: stored length %lu\n",
326 /* copy stored block from input to output */
327 while (length
!= 0) {
331 if (copy
> have
) copy
= have
;
332 if (copy
> left
) copy
= left
;
333 zmemcpy(put
, next
, copy
);
340 Tracev((stderr
, "inflate: stored end\n"));
345 /* get dynamic table entries descriptor */
347 state
->nlen
= BITS(5) + 257;
349 state
->ndist
= BITS(5) + 1;
351 state
->ncode
= BITS(4) + 4;
353 if (state
->nlen
> 286) {
354 strm
->msg
= (char *)"too many length symbols";
358 Tracev((stderr
, "inflate: table sizes ok\n"));
360 /* get code length code lengths (not a typo) */
362 while (state
->have
< state
->ncode
) {
364 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
367 while (state
->have
< 19)
368 state
->lens
[order
[state
->have
++]] = 0;
369 state
->next
= state
->codes
;
370 lencode
= (code
const FAR
*)(state
->next
);
372 ret
= inflate_table9(CODES
, state
->lens
, 19, &(state
->next
),
373 &(lenbits
), state
->work
);
375 strm
->msg
= (char *)"invalid code lengths set";
379 Tracev((stderr
, "inflate: code lengths ok\n"));
381 /* get length and distance code code lengths */
383 while (state
->have
< state
->nlen
+ state
->ndist
) {
385 here
= lencode
[BITS(lenbits
)];
386 if ((unsigned)(here
.bits
) <= bits
) break;
392 state
->lens
[state
->have
++] = here
.val
;
395 if (here
.val
== 16) {
396 NEEDBITS(here
.bits
+ 2);
398 if (state
->have
== 0) {
399 strm
->msg
= (char *)"invalid bit length repeat";
403 len
= (unsigned)(state
->lens
[state
->have
- 1]);
407 else if (here
.val
== 17) {
408 NEEDBITS(here
.bits
+ 3);
415 NEEDBITS(here
.bits
+ 7);
421 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
422 strm
->msg
= (char *)"invalid bit length repeat";
427 state
->lens
[state
->have
++] = (unsigned short)len
;
431 /* handle error breaks in while */
432 if (mode
== BAD
) break;
434 /* check for end-of-block code (better have one) */
435 if (state
->lens
[256] == 0) {
436 strm
->msg
= (char *)"invalid code -- missing end-of-block";
441 /* build code tables -- note: do not change the lenbits or distbits
442 values here (9 and 6) without reading the comments in inftree9.h
443 concerning the ENOUGH constants, which depend on those values */
444 state
->next
= state
->codes
;
445 lencode
= (code
const FAR
*)(state
->next
);
447 ret
= inflate_table9(LENS
, state
->lens
, state
->nlen
,
448 &(state
->next
), &(lenbits
), state
->work
);
450 strm
->msg
= (char *)"invalid literal/lengths set";
454 distcode
= (code
const FAR
*)(state
->next
);
456 ret
= inflate_table9(DISTS
, state
->lens
+ state
->nlen
,
457 state
->ndist
, &(state
->next
), &(distbits
),
460 strm
->msg
= (char *)"invalid distances set";
464 Tracev((stderr
, "inflate: codes ok\n"));
468 /* get a literal, length, or end-of-block code */
470 here
= lencode
[BITS(lenbits
)];
471 if ((unsigned)(here
.bits
) <= bits
) break;
474 if (here
.op
&& (here
.op
& 0xf0) == 0) {
477 here
= lencode
[last
.val
+
478 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
479 if ((unsigned)(last
.bits
+ here
.bits
) <= bits
) break;
485 length
= (unsigned)here
.val
;
487 /* process literal */
489 Tracevv((stderr
, here
.val
>= 0x20 && here
.val
< 0x7f ?
490 "inflate: literal '%c'\n" :
491 "inflate: literal 0x%02x\n", here
.val
));
493 *put
++ = (unsigned char)(length
);
499 /* process end of block */
501 Tracevv((stderr
, "inflate: end of block\n"));
508 strm
->msg
= (char *)"invalid literal/length code";
513 /* length code -- get extra bits, if any */
514 extra
= (unsigned)(here
.op
) & 31;
517 length
+= BITS(extra
);
520 Tracevv((stderr
, "inflate: length %lu\n", length
));
522 /* get distance code */
524 here
= distcode
[BITS(distbits
)];
525 if ((unsigned)(here
.bits
) <= bits
) break;
528 if ((here
.op
& 0xf0) == 0) {
531 here
= distcode
[last
.val
+
532 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
533 if ((unsigned)(last
.bits
+ here
.bits
) <= bits
) break;
540 strm
->msg
= (char *)"invalid distance code";
544 offset
= (unsigned)here
.val
;
546 /* get distance extra bits, if any */
547 extra
= (unsigned)(here
.op
) & 15;
550 offset
+= BITS(extra
);
553 if (offset
> WSIZE
- (wrap
? 0: left
)) {
554 strm
->msg
= (char *)"invalid distance too far back";
558 Tracevv((stderr
, "inflate: distance %lu\n", offset
));
560 /* copy match from window to output */
563 copy
= WSIZE
- offset
;
572 if (copy
> length
) copy
= length
;
578 } while (length
!= 0);
582 /* inflate stream terminated properly -- write leftover output */
585 if (out(out_desc
, window
, (unsigned)(WSIZE
- left
)))
594 default: /* can't happen, but makes compilers happy */
595 ret
= Z_STREAM_ERROR
;
599 /* Return unused input */
601 strm
->next_in
= next
;
602 strm
->avail_in
= have
;
606 int ZEXPORT
inflateBack9End(strm
)
609 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
610 return Z_STREAM_ERROR
;
611 ZFREE(strm
, strm
->state
);
612 strm
->state
= Z_NULL
;
613 Tracev((stderr
, "inflate: end\n"));