1 /* $NetBSD: infback.c,v 1.2 2006/01/27 00:45:27 christos Exp $ */
3 /* infback.c -- inflate using a call-back interface
4 * Copyright (C) 1995-2005 Mark Adler
5 * For conditions of distribution and use, see copyright notice in zlib.h
9 This code is largely copied from inflate.c. Normally either infback.o or
10 inflate.o would be linked into an application--not both. The interface
11 with inffast.c is retained so that optimized assembler-coded versions of
12 inflate_fast() can be used with either inflate.c or infback.c.
20 /* function prototypes */
21 local
void fixedtables
OF((struct inflate_state FAR
*state
));
24 strm provides memory allocation functions in zalloc and zfree, or
25 Z_NULL to use the library memory allocation functions.
27 windowBits is in the range 8..15, and window is a user-supplied
28 window and output buffer that is 2**windowBits bytes.
30 int ZEXPORT
inflateBackInit_(strm
, windowBits
, window
, version
, stream_size
)
33 unsigned char FAR
*window
;
37 struct inflate_state FAR
*state
;
39 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
40 stream_size
!= (int)(sizeof(z_stream
)))
41 return Z_VERSION_ERROR
;
42 if (strm
== Z_NULL
|| window
== Z_NULL
||
43 windowBits
< 8 || windowBits
> 15)
44 return Z_STREAM_ERROR
;
45 strm
->msg
= Z_NULL
; /* in case we return an error */
46 if (strm
->zalloc
== (alloc_func
)0) {
47 strm
->zalloc
= zcalloc
;
48 strm
->opaque
= (voidpf
)0;
50 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
51 state
= (struct inflate_state FAR
*)ZALLOC(strm
, 1,
52 sizeof(struct inflate_state
));
53 if (state
== Z_NULL
) return Z_MEM_ERROR
;
54 Tracev((stderr
, "inflate: allocated\n"));
55 strm
->state
= (struct internal_state FAR
*)state
;
57 state
->wbits
= windowBits
;
58 state
->wsize
= 1U << windowBits
;
59 state
->window
= window
;
66 Return state with length and distance decoding tables and index sizes set to
67 fixed code decoding. Normally this returns fixed tables from inffixed.h.
68 If BUILDFIXED is defined, then instead this routine builds the tables the
69 first time it's called, and returns those tables the first time and
70 thereafter. This reduces the size of the code by about 2K bytes, in
71 exchange for a little execution time. However, BUILDFIXED should not be
72 used for threaded applications, since the rewriting of the tables and virgin
73 may not be thread-safe.
75 local
void fixedtables(state
)
76 struct inflate_state FAR
*state
;
79 static int virgin
= 1;
80 static code
*lenfix
, *distfix
;
81 static code fixed
[544];
83 /* build fixed huffman tables if first call (may not be thread safe) */
88 /* literal/length table */
90 while (sym
< 144) state
->lens
[sym
++] = 8;
91 while (sym
< 256) state
->lens
[sym
++] = 9;
92 while (sym
< 280) state
->lens
[sym
++] = 7;
93 while (sym
< 288) state
->lens
[sym
++] = 8;
97 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
101 while (sym
< 32) state
->lens
[sym
++] = 5;
104 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
106 /* do this just once */
109 #else /* !BUILDFIXED */
110 # include "inffixed.h"
111 #endif /* BUILDFIXED */
112 state
->lencode
= lenfix
;
114 state
->distcode
= distfix
;
118 /* Macros for inflateBack(): */
120 /* Load returned state from inflate_fast() */
123 put = strm->next_out; \
124 left = strm->avail_out; \
125 next = strm->next_in; \
126 have = strm->avail_in; \
127 hold = state->hold; \
128 bits = state->bits; \
131 /* Set state from registers for inflate_fast() */
134 strm->next_out = put; \
135 strm->avail_out = left; \
136 strm->next_in = next; \
137 strm->avail_in = have; \
138 state->hold = hold; \
139 state->bits = bits; \
142 /* Clear the input bit accumulator */
149 /* Assure that some input is available. If input is requested, but denied,
150 then return a Z_BUF_ERROR from inflateBack(). */
154 have = in(in_desc, &next); \
163 /* Get a byte of input into the bit accumulator, or return from inflateBack()
164 with an error if there is no input available. */
169 hold += (unsigned long)(*next++) << bits; \
173 /* Assure that there are at least n bits in the bit accumulator. If there is
174 not enough available input to do that, then return from inflateBack() with
176 #define NEEDBITS(n) \
178 while (bits < (unsigned)(n)) \
182 /* Return the low n bits of the bit accumulator (n < 16) */
184 ((unsigned)hold & ((1U << (n)) - 1))
186 /* Remove n bits from the bit accumulator */
187 #define DROPBITS(n) \
190 bits -= (unsigned)(n); \
193 /* Remove zero to seven bits as needed to go to a byte boundary */
200 /* Assure that some output space is available, by writing out the window
201 if it's full. If the write fails, return from inflateBack() with a
206 put = state->window; \
207 left = state->wsize; \
208 state->whave = left; \
209 if (out(out_desc, put, left)) { \
217 strm provides the memory allocation functions and window buffer on input,
218 and provides information on the unused input on return. For Z_DATA_ERROR
219 returns, strm will also provide an error message.
221 in() and out() are the call-back input and output functions. When
222 inflateBack() needs more input, it calls in(). When inflateBack() has
223 filled the window with output, or when it completes with data in the
224 window, it calls out() to write out the data. The application must not
225 change the provided input until in() is called again or inflateBack()
226 returns. The application must not change the window/output buffer until
227 inflateBack() returns.
229 in() and out() are called with a descriptor parameter provided in the
230 inflateBack() call. This parameter can be a structure that provides the
231 information required to do the read or write, as well as accumulated
232 information on the input and output such as totals and check values.
234 in() should return zero on failure. out() should return non-zero on
235 failure. If either in() or out() fails, than inflateBack() returns a
236 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
237 was in() or out() that caused in the error. Otherwise, inflateBack()
238 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
239 error, or Z_MEM_ERROR if it could not allocate memory for the state.
240 inflateBack() can also return Z_STREAM_ERROR if the input parameters
241 are not correct, i.e. strm is Z_NULL or the state was not initialized.
243 int ZEXPORT
inflateBack(strm
, in
, in_desc
, out
, out_desc
)
250 struct inflate_state FAR
*state
;
251 unsigned char FAR
*next
; /* next input */
252 unsigned char FAR
*put
; /* next output */
253 unsigned have
, left
; /* available input and output */
254 unsigned long hold
; /* bit buffer */
255 unsigned bits
; /* bits in bit buffer */
256 unsigned copy
; /* number of stored or match bytes to copy */
257 unsigned char FAR
*from
; /* where to copy match bytes from */
258 code
this; /* current decoding table entry */
259 code last
; /* parent table entry */
260 unsigned len
; /* length to copy for repeats, bits to drop */
261 int ret
; /* return code */
262 static const unsigned short order
[19] = /* permutation of code lengths */
263 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
265 /* Check that the strm exists and that the state was initialized */
266 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
)
267 return Z_STREAM_ERROR
;
268 state
= (struct inflate_state FAR
*)strm
->state
;
270 /* Reset the state */
275 next
= strm
->next_in
;
276 have
= next
!= Z_NULL
? strm
->avail_in
: 0;
282 /* Inflate until end of block marked as last */
284 switch (state
->mode
) {
286 /* determine and dispatch block type */
293 state
->last
= BITS(1);
296 case 0: /* stored block */
297 Tracev((stderr
, "inflate: stored block%s\n",
298 state
->last
? " (last)" : ""));
299 state
->mode
= STORED
;
301 case 1: /* fixed block */
303 Tracev((stderr
, "inflate: fixed codes block%s\n",
304 state
->last
? " (last)" : ""));
305 state
->mode
= LEN
; /* decode codes */
307 case 2: /* dynamic block */
308 Tracev((stderr
, "inflate: dynamic codes block%s\n",
309 state
->last
? " (last)" : ""));
313 strm
->msg
= __UNCONST("invalid block type");
320 /* get and verify stored block length */
321 BYTEBITS(); /* go to byte boundary */
323 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
324 strm
->msg
= __UNCONST("invalid stored block lengths");
328 state
->length
= (unsigned)hold
& 0xffff;
329 Tracev((stderr
, "inflate: stored length %u\n",
333 /* copy stored block from input to output */
334 while (state
->length
!= 0) {
335 copy
= state
->length
;
338 if (copy
> have
) copy
= have
;
339 if (copy
> left
) copy
= left
;
340 zmemcpy(put
, next
, copy
);
345 state
->length
-= copy
;
347 Tracev((stderr
, "inflate: stored end\n"));
352 /* get dynamic table entries descriptor */
354 state
->nlen
= BITS(5) + 257;
356 state
->ndist
= BITS(5) + 1;
358 state
->ncode
= BITS(4) + 4;
360 #ifndef PKZIP_BUG_WORKAROUND
361 if (state
->nlen
> 286 || state
->ndist
> 30) {
362 strm
->msg
= __UNCONST("too many length or distance symbols");
367 Tracev((stderr
, "inflate: table sizes ok\n"));
369 /* get code length code lengths (not a typo) */
371 while (state
->have
< state
->ncode
) {
373 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
376 while (state
->have
< 19)
377 state
->lens
[order
[state
->have
++]] = 0;
378 state
->next
= state
->codes
;
379 state
->lencode
= (code
const FAR
*)(state
->next
);
381 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
382 &(state
->lenbits
), state
->work
);
384 strm
->msg
= __UNCONST("invalid code lengths set");
388 Tracev((stderr
, "inflate: code lengths ok\n"));
390 /* get length and distance code code lengths */
392 while (state
->have
< state
->nlen
+ state
->ndist
) {
394 this = state
->lencode
[BITS(state
->lenbits
)];
395 if ((unsigned)(this.bits
) <= bits
) break;
401 state
->lens
[state
->have
++] = this.val
;
404 if (this.val
== 16) {
405 NEEDBITS(this.bits
+ 2);
407 if (state
->have
== 0) {
408 strm
->msg
= __UNCONST("invalid bit length repeat");
412 len
= (unsigned)(state
->lens
[state
->have
- 1]);
416 else if (this.val
== 17) {
417 NEEDBITS(this.bits
+ 3);
424 NEEDBITS(this.bits
+ 7);
430 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
431 strm
->msg
= __UNCONST("invalid bit length repeat");
436 state
->lens
[state
->have
++] = (unsigned short)len
;
440 /* handle error breaks in while */
441 if (state
->mode
== BAD
) break;
443 /* build code tables */
444 state
->next
= state
->codes
;
445 state
->lencode
= (code
const FAR
*)(state
->next
);
447 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
448 &(state
->lenbits
), state
->work
);
450 strm
->msg
= __UNCONST("invalid literal/lengths set");
454 state
->distcode
= (code
const FAR
*)(state
->next
);
456 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
457 &(state
->next
), &(state
->distbits
), state
->work
);
459 strm
->msg
= __UNCONST("invalid distances set");
463 Tracev((stderr
, "inflate: codes ok\n"));
467 /* use inflate_fast() if we have enough input and output */
468 if (have
>= 6 && left
>= 258) {
470 if (state
->whave
< state
->wsize
)
471 state
->whave
= state
->wsize
- left
;
472 inflate_fast(strm
, state
->wsize
);
477 /* get a literal, length, or end-of-block code */
479 this = state
->lencode
[BITS(state
->lenbits
)];
480 if ((unsigned)(this.bits
) <= bits
) break;
483 if (this.op
&& (this.op
& 0xf0) == 0) {
486 this = state
->lencode
[last
.val
+
487 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
488 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
494 state
->length
= (unsigned)this.val
;
496 /* process literal */
498 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
499 "inflate: literal '%c'\n" :
500 "inflate: literal 0x%02x\n", this.val
));
502 *put
++ = (unsigned char)(state
->length
);
508 /* process end of block */
510 Tracevv((stderr
, "inflate: end of block\n"));
517 strm
->msg
= __UNCONST("invalid literal/length code");
522 /* length code -- get extra bits, if any */
523 state
->extra
= (unsigned)(this.op
) & 15;
524 if (state
->extra
!= 0) {
525 NEEDBITS(state
->extra
);
526 state
->length
+= BITS(state
->extra
);
527 DROPBITS(state
->extra
);
529 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
531 /* get distance code */
533 this = state
->distcode
[BITS(state
->distbits
)];
534 if ((unsigned)(this.bits
) <= bits
) break;
537 if ((this.op
& 0xf0) == 0) {
540 this = state
->distcode
[last
.val
+
541 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
542 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
549 strm
->msg
= __UNCONST("invalid distance code");
553 state
->offset
= (unsigned)this.val
;
555 /* get distance extra bits, if any */
556 state
->extra
= (unsigned)(this.op
) & 15;
557 if (state
->extra
!= 0) {
558 NEEDBITS(state
->extra
);
559 state
->offset
+= BITS(state
->extra
);
560 DROPBITS(state
->extra
);
562 if (state
->offset
> state
->wsize
- (state
->whave
< state
->wsize
?
564 strm
->msg
= __UNCONST("invalid distance too far back");
568 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
570 /* copy match from window to output */
573 copy
= state
->wsize
- state
->offset
;
579 from
= put
- state
->offset
;
582 if (copy
> state
->length
) copy
= state
->length
;
583 state
->length
-= copy
;
588 } while (state
->length
!= 0);
592 /* inflate stream terminated properly -- write leftover output */
594 if (left
< state
->wsize
) {
595 if (out(out_desc
, state
->window
, state
->wsize
- left
))
604 default: /* can't happen, but makes compilers happy */
605 ret
= Z_STREAM_ERROR
;
609 /* Return unused input */
611 strm
->next_in
= next
;
612 strm
->avail_in
= have
;
616 int ZEXPORT
inflateBackEnd(strm
)
619 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
620 return Z_STREAM_ERROR
;
621 ZFREE(strm
, strm
->state
);
622 strm
->state
= Z_NULL
;
623 Tracev((stderr
, "inflate: end\n"));