]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/infback.c
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* infback.c -- inflate using a call-back interface
29 * Copyright (C) 1995-2005 Mark Adler
30 * For conditions of distribution and use, see copyright notice in zlib.h
34 This code is largely copied from inflate.c. Normally either infback.o or
35 inflate.o would be linked into an application--not both. The interface
36 with inffast.c is retained so that optimized assembler-coded versions of
37 inflate_fast() can be used with either inflate.c or infback.c.
45 /* function prototypes */
46 local
void fixedtables
OF((struct inflate_state FAR
*state
));
49 strm provides memory allocation functions in zalloc and zfree, or
50 Z_NULL to use the library memory allocation functions.
52 windowBits is in the range 8..15, and window is a user-supplied
53 window and output buffer that is 2**windowBits bytes.
56 inflateBackInit_(z_streamp strm
, int windowBits
, unsigned char FAR
*window
,
57 const char *version
, int stream_size
)
59 struct inflate_state FAR
*state
;
61 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
62 stream_size
!= (int)(sizeof(z_stream
)))
63 return Z_VERSION_ERROR
;
64 if (strm
== Z_NULL
|| window
== Z_NULL
||
65 windowBits
< 8 || windowBits
> 15)
66 return Z_STREAM_ERROR
;
67 strm
->msg
= Z_NULL
; /* in case we return an error */
69 if (strm
->zalloc
== (alloc_func
)0) {
70 strm
->zalloc
= zcalloc
;
71 strm
->opaque
= (voidpf
)0;
73 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
74 #endif /* NO_ZCFUNCS */
75 state
= (struct inflate_state FAR
*)ZALLOC(strm
, 1,
76 sizeof(struct inflate_state
));
77 if (state
== Z_NULL
) return Z_MEM_ERROR
;
78 Tracev((stderr
, "inflate: allocated\n"));
79 strm
->state
= (struct internal_state FAR
*)state
;
81 state
->wbits
= windowBits
;
82 state
->wsize
= 1U << windowBits
;
83 state
->window
= window
;
90 Return state with length and distance decoding tables and index sizes set to
91 fixed code decoding. Normally this returns fixed tables from inffixed.h.
92 If BUILDFIXED is defined, then instead this routine builds the tables the
93 first time it's called, and returns those tables the first time and
94 thereafter. This reduces the size of the code by about 2K bytes, in
95 exchange for a little execution time. However, BUILDFIXED should not be
96 used for threaded applications, since the rewriting of the tables and virgin
97 may not be thread-safe.
100 fixedtables(struct inflate_state FAR
*state
)
103 static int virgin
= 1;
104 static code
*lenfix
, *distfix
;
105 static code fixed
[544];
107 /* build fixed huffman tables if first call (may not be thread safe) */
112 /* literal/length table */
114 while (sym
< 144) state
->lens
[sym
++] = 8;
115 while (sym
< 256) state
->lens
[sym
++] = 9;
116 while (sym
< 280) state
->lens
[sym
++] = 7;
117 while (sym
< 288) state
->lens
[sym
++] = 8;
121 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
125 while (sym
< 32) state
->lens
[sym
++] = 5;
128 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
130 /* do this just once */
133 #else /* !BUILDFIXED */
134 # include "inffixed.h"
135 #endif /* BUILDFIXED */
136 state
->lencode
= lenfix
;
138 state
->distcode
= distfix
;
142 /* Macros for inflateBack(): */
144 /* Load returned state from inflate_fast() */
147 put = strm->next_out; \
148 left = strm->avail_out; \
149 next = strm->next_in; \
150 have = strm->avail_in; \
151 hold = state->hold; \
152 bits = state->bits; \
155 /* Set state from registers for inflate_fast() */
158 strm->next_out = put; \
159 strm->avail_out = left; \
160 strm->next_in = next; \
161 strm->avail_in = have; \
162 state->hold = hold; \
163 state->bits = bits; \
166 /* Clear the input bit accumulator */
173 /* Assure that some input is available. If input is requested, but denied,
174 then return a Z_BUF_ERROR from inflateBack(). */
178 have = in(in_desc, &next); \
187 /* Get a byte of input into the bit accumulator, or return from inflateBack()
188 with an error if there is no input available. */
193 hold += (unsigned long)(*next++) << bits; \
197 /* Assure that there are at least n bits in the bit accumulator. If there is
198 not enough available input to do that, then return from inflateBack() with
200 #define NEEDBITS(n) \
202 while (bits < (unsigned)(n)) \
206 /* Return the low n bits of the bit accumulator (n < 16) */
208 ((unsigned)hold & ((1U << (n)) - 1))
210 /* Remove n bits from the bit accumulator */
211 #define DROPBITS(n) \
214 bits -= (unsigned)(n); \
217 /* Remove zero to seven bits as needed to go to a byte boundary */
224 /* Assure that some output space is available, by writing out the window
225 if it's full. If the write fails, return from inflateBack() with a
230 put = state->window; \
231 left = state->wsize; \
232 state->whave = left; \
233 if (out(out_desc, put, left)) { \
241 strm provides the memory allocation functions and window buffer on input,
242 and provides information on the unused input on return. For Z_DATA_ERROR
243 returns, strm will also provide an error message.
245 in() and out() are the call-back input and output functions. When
246 inflateBack() needs more input, it calls in(). When inflateBack() has
247 filled the window with output, or when it completes with data in the
248 window, it calls out() to write out the data. The application must not
249 change the provided input until in() is called again or inflateBack()
250 returns. The application must not change the window/output buffer until
251 inflateBack() returns.
253 in() and out() are called with a descriptor parameter provided in the
254 inflateBack() call. This parameter can be a structure that provides the
255 information required to do the read or write, as well as accumulated
256 information on the input and output such as totals and check values.
258 in() should return zero on failure. out() should return non-zero on
259 failure. If either in() or out() fails, than inflateBack() returns a
260 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
261 was in() or out() that caused in the error. Otherwise, inflateBack()
262 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
263 error, or Z_MEM_ERROR if it could not allocate memory for the state.
264 inflateBack() can also return Z_STREAM_ERROR if the input parameters
265 are not correct, i.e. strm is Z_NULL or the state was not initialized.
268 inflateBack(z_streamp strm
, in_func in
, void FAR
*in_desc
, out_func out
,
271 struct inflate_state FAR
*state
;
272 unsigned char FAR
*next
; /* next input */
273 unsigned char FAR
*put
; /* next output */
274 unsigned have
, left
; /* available input and output */
275 unsigned long hold
; /* bit buffer */
276 unsigned bits
; /* bits in bit buffer */
277 unsigned copy
; /* number of stored or match bytes to copy */
278 unsigned char FAR
*from
; /* where to copy match bytes from */
279 code
this; /* current decoding table entry */
280 code last
; /* parent table entry */
281 unsigned len
; /* length to copy for repeats, bits to drop */
282 int ret
; /* return code */
283 static const unsigned short order
[19] = /* permutation of code lengths */
284 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
286 /* Check that the strm exists and that the state was initialized */
287 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
)
288 return Z_STREAM_ERROR
;
289 state
= (struct inflate_state FAR
*)strm
->state
;
291 /* Reset the state */
296 next
= strm
->next_in
;
297 have
= next
!= Z_NULL
? strm
->avail_in
: 0;
303 /* Inflate until end of block marked as last */
305 switch (state
->mode
) {
307 /* determine and dispatch block type */
314 state
->last
= BITS(1);
317 case 0: /* stored block */
318 Tracev((stderr
, "inflate: stored block%s\n",
319 state
->last
? " (last)" : ""));
320 state
->mode
= STORED
;
322 case 1: /* fixed block */
324 Tracev((stderr
, "inflate: fixed codes block%s\n",
325 state
->last
? " (last)" : ""));
326 state
->mode
= LEN
; /* decode codes */
328 case 2: /* dynamic block */
329 Tracev((stderr
, "inflate: dynamic codes block%s\n",
330 state
->last
? " (last)" : ""));
334 strm
->msg
= (char *)"invalid block type";
341 /* get and verify stored block length */
342 BYTEBITS(); /* go to byte boundary */
344 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
345 strm
->msg
= (char *)"invalid stored block lengths";
349 state
->length
= (unsigned)hold
& 0xffff;
350 Tracev((stderr
, "inflate: stored length %u\n",
354 /* copy stored block from input to output */
355 while (state
->length
!= 0) {
356 copy
= state
->length
;
359 if (copy
> have
) copy
= have
;
360 if (copy
> left
) copy
= left
;
361 zmemcpy(put
, next
, copy
);
366 state
->length
-= copy
;
368 Tracev((stderr
, "inflate: stored end\n"));
373 /* get dynamic table entries descriptor */
375 state
->nlen
= BITS(5) + 257;
377 state
->ndist
= BITS(5) + 1;
379 state
->ncode
= BITS(4) + 4;
381 #ifndef PKZIP_BUG_WORKAROUND
382 if (state
->nlen
> 286 || state
->ndist
> 30) {
383 strm
->msg
= (char *)"too many length or distance symbols";
388 Tracev((stderr
, "inflate: table sizes ok\n"));
390 /* get code length code lengths (not a typo) */
392 while (state
->have
< state
->ncode
) {
394 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
397 while (state
->have
< 19)
398 state
->lens
[order
[state
->have
++]] = 0;
399 state
->next
= state
->codes
;
400 state
->lencode
= (code
const FAR
*)(state
->next
);
402 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
403 &(state
->lenbits
), state
->work
);
405 strm
->msg
= (char *)"invalid code lengths set";
409 Tracev((stderr
, "inflate: code lengths ok\n"));
411 /* get length and distance code code lengths */
413 while (state
->have
< state
->nlen
+ state
->ndist
) {
415 this = state
->lencode
[BITS(state
->lenbits
)];
416 if ((unsigned)(this.bits
) <= bits
) break;
422 state
->lens
[state
->have
++] = this.val
;
425 if (this.val
== 16) {
426 NEEDBITS(this.bits
+ 2);
428 if (state
->have
== 0) {
429 strm
->msg
= (char *)"invalid bit length repeat";
433 len
= (unsigned)(state
->lens
[state
->have
- 1]);
437 else if (this.val
== 17) {
438 NEEDBITS(this.bits
+ 3);
445 NEEDBITS(this.bits
+ 7);
451 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
452 strm
->msg
= (char *)"invalid bit length repeat";
457 state
->lens
[state
->have
++] = (unsigned short)len
;
461 /* handle error breaks in while */
462 if (state
->mode
== BAD
) break;
464 /* build code tables */
465 state
->next
= state
->codes
;
466 state
->lencode
= (code
const FAR
*)(state
->next
);
468 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
469 &(state
->lenbits
), state
->work
);
471 strm
->msg
= (char *)"invalid literal/lengths set";
475 state
->distcode
= (code
const FAR
*)(state
->next
);
477 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
478 &(state
->next
), &(state
->distbits
), state
->work
);
480 strm
->msg
= (char *)"invalid distances set";
484 Tracev((stderr
, "inflate: codes ok\n"));
488 /* use inflate_fast() if we have enough input and output */
489 if (have
>= 6 && left
>= 258) {
491 if (state
->whave
< state
->wsize
)
492 state
->whave
= state
->wsize
- left
;
493 inflate_fast(strm
, state
->wsize
);
498 /* get a literal, length, or end-of-block code */
500 this = state
->lencode
[BITS(state
->lenbits
)];
501 if ((unsigned)(this.bits
) <= bits
) break;
504 if (this.op
&& (this.op
& 0xf0) == 0) {
507 this = state
->lencode
[last
.val
+
508 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
509 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
515 state
->length
= (unsigned)this.val
;
517 /* process literal */
519 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
520 "inflate: literal '%c'\n" :
521 "inflate: literal 0x%02x\n", this.val
));
523 *put
++ = (unsigned char)(state
->length
);
529 /* process end of block */
531 Tracevv((stderr
, "inflate: end of block\n"));
538 strm
->msg
= (char *)"invalid literal/length code";
543 /* length code -- get extra bits, if any */
544 state
->extra
= (unsigned)(this.op
) & 15;
545 if (state
->extra
!= 0) {
546 NEEDBITS(state
->extra
);
547 state
->length
+= BITS(state
->extra
);
548 DROPBITS(state
->extra
);
550 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
552 /* get distance code */
554 this = state
->distcode
[BITS(state
->distbits
)];
555 if ((unsigned)(this.bits
) <= bits
) break;
558 if ((this.op
& 0xf0) == 0) {
561 this = state
->distcode
[last
.val
+
562 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
563 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
570 strm
->msg
= (char *)"invalid distance code";
574 state
->offset
= (unsigned)this.val
;
576 /* get distance extra bits, if any */
577 state
->extra
= (unsigned)(this.op
) & 15;
578 if (state
->extra
!= 0) {
579 NEEDBITS(state
->extra
);
580 state
->offset
+= BITS(state
->extra
);
581 DROPBITS(state
->extra
);
583 if (state
->offset
> state
->wsize
- (state
->whave
< state
->wsize
?
585 strm
->msg
= (char *)"invalid distance too far back";
589 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
591 /* copy match from window to output */
594 copy
= state
->wsize
- state
->offset
;
600 from
= put
- state
->offset
;
603 if (copy
> state
->length
) copy
= state
->length
;
604 state
->length
-= copy
;
609 } while (state
->length
!= 0);
613 /* inflate stream terminated properly -- write leftover output */
615 if (left
< state
->wsize
) {
616 if (out(out_desc
, state
->window
, state
->wsize
- left
))
625 default: /* can't happen, but makes compilers happy */
626 ret
= Z_STREAM_ERROR
;
630 /* Return unused input */
632 strm
->next_in
= next
;
633 strm
->avail_in
= have
;
638 inflateBackEnd(z_streamp strm
)
640 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
641 return Z_STREAM_ERROR
;
642 ZFREE(strm
, strm
->state
);
643 strm
->state
= Z_NULL
;
644 Tracev((stderr
, "inflate: end\n"));