]>
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.
46 /* function prototypes */
47 local
void fixedtables
OF((struct inflate_state FAR
*state
));
50 strm provides memory allocation functions in zalloc and zfree, or
51 Z_NULL to use the library memory allocation functions.
53 windowBits is in the range 8..15, and window is a user-supplied
54 window and output buffer that is 2**windowBits bytes.
57 inflateBackInit_(z_streamp strm
, int windowBits
, unsigned char FAR
*window
,
58 const char *version
, int stream_size
)
60 struct inflate_state FAR
*state
;
62 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
63 stream_size
!= (int)(sizeof(z_stream
)))
64 return Z_VERSION_ERROR
;
65 if (strm
== Z_NULL
|| window
== Z_NULL
||
66 windowBits
< 8 || windowBits
> 15)
67 return Z_STREAM_ERROR
;
68 strm
->msg
= Z_NULL
; /* in case we return an error */
70 if (strm
->zalloc
== (alloc_func
)0) {
71 strm
->zalloc
= zcalloc
;
72 strm
->opaque
= (voidpf
)0;
74 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
75 #endif /* NO_ZCFUNCS */
76 state
= (struct inflate_state FAR
*)ZALLOC(strm
, 1,
77 sizeof(struct inflate_state
));
78 if (state
== Z_NULL
) return Z_MEM_ERROR
;
79 Tracev((stderr
, "inflate: allocated\n"));
80 strm
->state
= (struct internal_state FAR
*)state
;
82 state
->wbits
= windowBits
;
83 state
->wsize
= 1U << windowBits
;
84 state
->window
= window
;
91 Return state with length and distance decoding tables and index sizes set to
92 fixed code decoding. Normally this returns fixed tables from inffixed.h.
93 If BUILDFIXED is defined, then instead this routine builds the tables the
94 first time it's called, and returns those tables the first time and
95 thereafter. This reduces the size of the code by about 2K bytes, in
96 exchange for a little execution time. However, BUILDFIXED should not be
97 used for threaded applications, since the rewriting of the tables and virgin
98 may not be thread-safe.
101 fixedtables(struct inflate_state FAR
*state
)
104 static int virgin
= 1;
105 static code
*lenfix
, *distfix
;
106 static code fixed
[544];
108 /* build fixed huffman tables if first call (may not be thread safe) */
113 /* literal/length table */
115 while (sym
< 144) state
->lens
[sym
++] = 8;
116 while (sym
< 256) state
->lens
[sym
++] = 9;
117 while (sym
< 280) state
->lens
[sym
++] = 7;
118 while (sym
< 288) state
->lens
[sym
++] = 8;
122 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
126 while (sym
< 32) state
->lens
[sym
++] = 5;
129 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
131 /* do this just once */
134 #else /* !BUILDFIXED */
135 # include "inffixed.h"
136 #endif /* BUILDFIXED */
137 state
->lencode
= lenfix
;
139 state
->distcode
= distfix
;
143 /* Macros for inflateBack(): */
145 /* Load returned state from inflate_fast() */
148 put = strm->next_out; \
149 left = strm->avail_out; \
150 next = strm->next_in; \
151 have = strm->avail_in; \
152 hold = state->hold; \
153 bits = state->bits; \
156 /* Set state from registers for inflate_fast() */
159 strm->next_out = put; \
160 strm->avail_out = left; \
161 strm->next_in = next; \
162 strm->avail_in = have; \
163 state->hold = hold; \
164 state->bits = bits; \
167 /* Clear the input bit accumulator */
174 /* Assure that some input is available. If input is requested, but denied,
175 then return a Z_BUF_ERROR from inflateBack(). */
179 have = in(in_desc, &next); \
188 /* Get a byte of input into the bit accumulator, or return from inflateBack()
189 with an error if there is no input available. */
194 hold += (unsigned long)(*next++) << bits; \
198 /* Assure that there are at least n bits in the bit accumulator. If there is
199 not enough available input to do that, then return from inflateBack() with
201 #define NEEDBITS(n) \
203 while (bits < (unsigned)(n)) \
207 /* Return the low n bits of the bit accumulator (n < 16) */
209 ((unsigned)hold & ((1U << (n)) - 1))
211 /* Remove n bits from the bit accumulator */
212 #define DROPBITS(n) \
215 bits -= (unsigned)(n); \
218 /* Remove zero to seven bits as needed to go to a byte boundary */
225 /* Assure that some output space is available, by writing out the window
226 if it's full. If the write fails, return from inflateBack() with a
231 put = state->window; \
232 left = state->wsize; \
233 state->whave = left; \
234 if (out(out_desc, put, left)) { \
242 strm provides the memory allocation functions and window buffer on input,
243 and provides information on the unused input on return. For Z_DATA_ERROR
244 returns, strm will also provide an error message.
246 in() and out() are the call-back input and output functions. When
247 inflateBack() needs more input, it calls in(). When inflateBack() has
248 filled the window with output, or when it completes with data in the
249 window, it calls out() to write out the data. The application must not
250 change the provided input until in() is called again or inflateBack()
251 returns. The application must not change the window/output buffer until
252 inflateBack() returns.
254 in() and out() are called with a descriptor parameter provided in the
255 inflateBack() call. This parameter can be a structure that provides the
256 information required to do the read or write, as well as accumulated
257 information on the input and output such as totals and check values.
259 in() should return zero on failure. out() should return non-zero on
260 failure. If either in() or out() fails, than inflateBack() returns a
261 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
262 was in() or out() that caused in the error. Otherwise, inflateBack()
263 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
264 error, or Z_MEM_ERROR if it could not allocate memory for the state.
265 inflateBack() can also return Z_STREAM_ERROR if the input parameters
266 are not correct, i.e. strm is Z_NULL or the state was not initialized.
269 inflateBack(z_streamp strm
, in_func in
, void FAR
*in_desc
, out_func out
,
272 struct inflate_state FAR
*state
;
273 unsigned char FAR
*next
; /* next input */
274 unsigned char FAR
*put
; /* next output */
275 unsigned have
, left
; /* available input and output */
276 unsigned long hold
; /* bit buffer */
277 unsigned bits
; /* bits in bit buffer */
278 unsigned copy
; /* number of stored or match bytes to copy */
279 unsigned char FAR
*from
; /* where to copy match bytes from */
280 code
this; /* current decoding table entry */
281 code last
; /* parent table entry */
282 unsigned len
; /* length to copy for repeats, bits to drop */
283 int ret
; /* return code */
284 static const unsigned short order
[19] = /* permutation of code lengths */
285 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
287 /* Check that the strm exists and that the state was initialized */
288 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
)
289 return Z_STREAM_ERROR
;
290 state
= (struct inflate_state FAR
*)strm
->state
;
292 /* Reset the state */
297 next
= strm
->next_in
;
298 have
= next
!= Z_NULL
? strm
->avail_in
: 0;
304 /* Inflate until end of block marked as last */
306 switch (state
->mode
) {
308 /* determine and dispatch block type */
315 state
->last
= BITS(1);
318 case 0: /* stored block */
319 Tracev((stderr
, "inflate: stored block%s\n",
320 state
->last
? " (last)" : ""));
321 state
->mode
= STORED
;
323 case 1: /* fixed block */
325 Tracev((stderr
, "inflate: fixed codes block%s\n",
326 state
->last
? " (last)" : ""));
327 state
->mode
= LEN
; /* decode codes */
329 case 2: /* dynamic block */
330 Tracev((stderr
, "inflate: dynamic codes block%s\n",
331 state
->last
? " (last)" : ""));
335 strm
->msg
= (char *)"invalid block type";
342 /* get and verify stored block length */
343 BYTEBITS(); /* go to byte boundary */
345 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
346 strm
->msg
= (char *)"invalid stored block lengths";
350 state
->length
= (unsigned)hold
& 0xffff;
351 Tracev((stderr
, "inflate: stored length %u\n",
355 /* copy stored block from input to output */
356 while (state
->length
!= 0) {
357 copy
= state
->length
;
360 if (copy
> have
) copy
= have
;
361 if (copy
> left
) copy
= left
;
362 zmemcpy(put
, next
, copy
);
367 state
->length
-= copy
;
369 Tracev((stderr
, "inflate: stored end\n"));
374 /* get dynamic table entries descriptor */
376 state
->nlen
= BITS(5) + 257;
378 state
->ndist
= BITS(5) + 1;
380 state
->ncode
= BITS(4) + 4;
382 #ifndef PKZIP_BUG_WORKAROUND
383 if (state
->nlen
> 286 || state
->ndist
> 30) {
384 strm
->msg
= (char *)"too many length or distance symbols";
389 Tracev((stderr
, "inflate: table sizes ok\n"));
391 /* get code length code lengths (not a typo) */
393 while (state
->have
< state
->ncode
) {
395 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
398 while (state
->have
< 19)
399 state
->lens
[order
[state
->have
++]] = 0;
400 state
->next
= state
->codes
;
401 state
->lencode
= (code
const FAR
*)(state
->next
);
403 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
404 &(state
->lenbits
), state
->work
);
406 strm
->msg
= (char *)"invalid code lengths set";
410 Tracev((stderr
, "inflate: code lengths ok\n"));
412 /* get length and distance code code lengths */
414 while (state
->have
< state
->nlen
+ state
->ndist
) {
416 this = state
->lencode
[BITS(state
->lenbits
)];
417 if ((unsigned)(this.bits
) <= bits
) break;
423 state
->lens
[state
->have
++] = this.val
;
426 if (this.val
== 16) {
427 NEEDBITS(this.bits
+ 2);
429 if (state
->have
== 0) {
430 strm
->msg
= (char *)"invalid bit length repeat";
434 len
= (unsigned)(state
->lens
[state
->have
- 1]);
438 else if (this.val
== 17) {
439 NEEDBITS(this.bits
+ 3);
446 NEEDBITS(this.bits
+ 7);
452 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
453 strm
->msg
= (char *)"invalid bit length repeat";
458 state
->lens
[state
->have
++] = (unsigned short)len
;
462 /* handle error breaks in while */
463 if (state
->mode
== BAD
) break;
465 /* build code tables */
466 state
->next
= state
->codes
;
467 state
->lencode
= (code
const FAR
*)(state
->next
);
469 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
470 &(state
->lenbits
), state
->work
);
472 strm
->msg
= (char *)"invalid literal/lengths set";
476 state
->distcode
= (code
const FAR
*)(state
->next
);
478 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
479 &(state
->next
), &(state
->distbits
), state
->work
);
481 strm
->msg
= (char *)"invalid distances set";
485 Tracev((stderr
, "inflate: codes ok\n"));
490 /* use inflate_fast() if we have enough input and output */
491 if (have
>= 6 && left
>= 258) {
493 if (state
->whave
< state
->wsize
)
494 state
->whave
= state
->wsize
- left
;
495 inflate_fast(strm
, state
->wsize
);
500 /* get a literal, length, or end-of-block code */
502 this = state
->lencode
[BITS(state
->lenbits
)];
503 if ((unsigned)(this.bits
) <= bits
) break;
506 if (this.op
&& (this.op
& 0xf0) == 0) {
509 this = state
->lencode
[last
.val
+
510 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
511 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
517 state
->length
= (unsigned)this.val
;
519 /* process literal */
521 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
522 "inflate: literal '%c'\n" :
523 "inflate: literal 0x%02x\n", this.val
));
525 *put
++ = (unsigned char)(state
->length
);
531 /* process end of block */
533 Tracevv((stderr
, "inflate: end of block\n"));
540 strm
->msg
= (char *)"invalid literal/length code";
545 /* length code -- get extra bits, if any */
546 state
->extra
= (unsigned)(this.op
) & 15;
547 if (state
->extra
!= 0) {
548 NEEDBITS(state
->extra
);
549 state
->length
+= BITS(state
->extra
);
550 DROPBITS(state
->extra
);
552 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
554 /* get distance code */
556 this = state
->distcode
[BITS(state
->distbits
)];
557 if ((unsigned)(this.bits
) <= bits
) break;
560 if ((this.op
& 0xf0) == 0) {
563 this = state
->distcode
[last
.val
+
564 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
565 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
572 strm
->msg
= (char *)"invalid distance code";
576 state
->offset
= (unsigned)this.val
;
578 /* get distance extra bits, if any */
579 state
->extra
= (unsigned)(this.op
) & 15;
580 if (state
->extra
!= 0) {
581 NEEDBITS(state
->extra
);
582 state
->offset
+= BITS(state
->extra
);
583 DROPBITS(state
->extra
);
585 if (state
->offset
> state
->wsize
- (state
->whave
< state
->wsize
?
587 strm
->msg
= (char *)"invalid distance too far back";
591 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
593 /* copy match from window to output */
596 copy
= state
->wsize
- state
->offset
;
602 from
= put
- state
->offset
;
605 if (copy
> state
->length
) copy
= state
->length
;
606 state
->length
-= copy
;
611 } while (state
->length
!= 0);
615 /* inflate stream terminated properly -- write leftover output */
617 if (left
< state
->wsize
) {
618 if (out(out_desc
, state
->window
, state
->wsize
- left
))
627 default: /* can't happen, but makes compilers happy */
628 ret
= Z_STREAM_ERROR
;
632 /* Return unused input */
634 strm
->next_in
= next
;
635 strm
->avail_in
= have
;
640 inflateBackEnd(z_streamp strm
)
642 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
643 return Z_STREAM_ERROR
;
644 ZFREE(strm
, strm
->state
);
645 strm
->state
= Z_NULL
;
646 Tracev((stderr
, "inflate: end\n"));