1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
5 * Based on zlib 1.2.3 but modified for the Linux Kernel by
6 * Richard Purdie <richard@openedhand.com>
8 * Changes mainly for static instead of dynamic memory allocation
12 #include <linux/zutil.h>
18 int zlib_inflate_workspacesize(void)
20 return sizeof(struct inflate_workspace
);
23 int zlib_inflateReset(z_streamp strm
)
25 struct inflate_state
*state
;
27 if (strm
== NULL
|| strm
->state
== NULL
) return Z_STREAM_ERROR
;
28 state
= (struct inflate_state
*)strm
->state
;
29 strm
->total_in
= strm
->total_out
= state
->total
= 0;
31 strm
->adler
= 1; /* to support ill-conceived Java test suite */
38 state
->lencode
= state
->distcode
= state
->next
= state
->codes
;
40 /* Initialise Window */
41 state
->wsize
= 1U << state
->wbits
;
48 int zlib_inflateInit2(z_streamp strm
, int windowBits
)
50 struct inflate_state
*state
;
52 if (strm
== NULL
) return Z_STREAM_ERROR
;
53 strm
->msg
= NULL
; /* in case we return an error */
55 state
= &WS(strm
)->inflate_state
;
56 strm
->state
= (struct internal_state
*)state
;
60 windowBits
= -windowBits
;
63 state
->wrap
= (windowBits
>> 4) + 1;
65 if (windowBits
< 8 || windowBits
> 15) {
66 return Z_STREAM_ERROR
;
68 state
->wbits
= (unsigned)windowBits
;
69 state
->window
= &WS(strm
)->working_window
[0];
71 return zlib_inflateReset(strm
);
75 Return state with length and distance decoding tables and index sizes set to
76 fixed code decoding. This returns fixed tables from inffixed.h.
78 static void zlib_fixedtables(struct inflate_state
*state
)
80 # include "inffixed.h"
81 state
->lencode
= lenfix
;
83 state
->distcode
= distfix
;
89 Update the window with the last wsize (normally 32K) bytes written before
90 returning. This is only called when a window is already in use, or when
91 output has been written during this inflate call, but the end of the deflate
92 stream has not been reached yet. It is also called to window dictionary data
93 when a dictionary is loaded.
95 Providing output buffers larger than 32K to inflate() should provide a speed
96 advantage, since only the last 32K of output is copied to the sliding window
97 upon return from inflate(), and since all distances after the first 32K of
98 output will fall in the output data, making match copies simpler and faster.
99 The advantage may be dependent on the size of the processor's data caches.
101 static void zlib_updatewindow(z_streamp strm
, unsigned out
)
103 struct inflate_state
*state
;
106 state
= (struct inflate_state
*)strm
->state
;
108 /* copy state->wsize or less output bytes into the circular window */
109 copy
= out
- strm
->avail_out
;
110 if (copy
>= state
->wsize
) {
111 memcpy(state
->window
, strm
->next_out
- state
->wsize
, state
->wsize
);
113 state
->whave
= state
->wsize
;
116 dist
= state
->wsize
- state
->write
;
117 if (dist
> copy
) dist
= copy
;
118 memcpy(state
->window
+ state
->write
, strm
->next_out
- copy
, dist
);
121 memcpy(state
->window
, strm
->next_out
- copy
, copy
);
123 state
->whave
= state
->wsize
;
126 state
->write
+= dist
;
127 if (state
->write
== state
->wsize
) state
->write
= 0;
128 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
135 * At the end of a Deflate-compressed PPP packet, we expect to have seen
136 * a `stored' block type value but not the (zero) length bytes.
139 Returns true if inflate is currently at the end of a block generated by
140 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
141 implementation to provide an additional safety check. PPP uses
142 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
143 block. When decompressing, PPP checks that at the end of input packet,
144 inflate is waiting for these length bytes.
146 static int zlib_inflateSyncPacket(z_streamp strm
)
148 struct inflate_state
*state
;
150 if (strm
== NULL
|| strm
->state
== NULL
) return Z_STREAM_ERROR
;
151 state
= (struct inflate_state
*)strm
->state
;
153 if (state
->mode
== STORED
&& state
->bits
== 0) {
160 /* Macros for inflate(): */
162 /* check function to use adler32() for zlib or crc32() for gzip */
163 #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
165 /* Load registers with state in inflate() for speed */
168 put = strm->next_out; \
169 left = strm->avail_out; \
170 next = strm->next_in; \
171 have = strm->avail_in; \
172 hold = state->hold; \
173 bits = state->bits; \
176 /* Restore state from registers in inflate() */
179 strm->next_out = put; \
180 strm->avail_out = left; \
181 strm->next_in = next; \
182 strm->avail_in = have; \
183 state->hold = hold; \
184 state->bits = bits; \
187 /* Clear the input bit accumulator */
194 /* Get a byte of input into the bit accumulator, or return from inflate()
195 if there is no input available. */
198 if (have == 0) goto inf_leave; \
200 hold += (unsigned long)(*next++) << bits; \
204 /* Assure that there are at least n bits in the bit accumulator. If there is
205 not enough available input to do that, then return from inflate(). */
206 #define NEEDBITS(n) \
208 while (bits < (unsigned)(n)) \
212 /* Return the low n bits of the bit accumulator (n < 16) */
214 ((unsigned)hold & ((1U << (n)) - 1))
216 /* Remove n bits from the bit accumulator */
217 #define DROPBITS(n) \
220 bits -= (unsigned)(n); \
223 /* Remove zero to seven bits as needed to go to a byte boundary */
230 /* Reverse the bytes in a 32-bit value */
232 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
233 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
236 inflate() uses a state machine to process as much input data and generate as
237 much output data as possible before returning. The state machine is
238 structured roughly as follows:
240 for (;;) switch (state) {
243 if (not enough input data or output space to make progress)
245 ... make progress ...
251 so when inflate() is called again, the same case is attempted again, and
252 if the appropriate resources are provided, the machine proceeds to the
253 next state. The NEEDBITS() macro is usually the way the state evaluates
254 whether it can proceed or should return. NEEDBITS() does the return if
255 the requested bits are not available. The typical use of the BITS macros
259 ... do something with BITS(n) ...
262 where NEEDBITS(n) either returns from inflate() if there isn't enough
263 input left to load n bits into the accumulator, or it continues. BITS(n)
264 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
265 the low n bits off the accumulator. INITBITS() clears the accumulator
266 and sets the number of available bits to zero. BYTEBITS() discards just
267 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
268 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
270 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
271 if there is no input available. The decoding of variable length codes uses
272 PULLBYTE() directly in order to pull just enough bytes to decode the next
275 Some states loop until they get enough input, making sure that enough
276 state information is maintained to continue the loop where it left off
277 if NEEDBITS() returns in the loop. For example, want, need, and keep
278 would all have to actually be part of the saved state in case NEEDBITS()
282 while (want < need) {
284 keep[want++] = BITS(n);
290 As shown above, if the next state is also the next case, then the break
293 A state may also return if there is not enough output space available to
294 complete that state. Those states are copying stored data, writing a
295 literal byte, and copying a matching string.
297 When returning, a "goto inf_leave" is used to update the total counters,
298 update the check value, and determine whether any progress has been made
299 during that inflate() call in order to return the proper return code.
300 Progress is defined as a change in either strm->avail_in or strm->avail_out.
301 When there is a window, goto inf_leave will update the window with the last
302 output written. If a goto inf_leave occurs in the middle of decompression
303 and there is no window currently, goto inf_leave will create one and copy
304 output to the window for the next call of inflate().
306 In this implementation, the flush parameter of inflate() only affects the
307 return code (per zlib.h). inflate() always writes as much as possible to
308 strm->next_out, given the space available and the provided input--the effect
309 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
310 the allocation of and copying into a sliding window until necessary, which
311 provides the effect documented in zlib.h for Z_FINISH when the entire input
312 stream available. So the only thing the flush parameter actually does is:
313 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
314 will return Z_BUF_ERROR if it has not reached the end of the stream.
317 int zlib_inflate(z_streamp strm
, int flush
)
319 struct inflate_state
*state
;
320 const unsigned char *next
; /* next input */
321 unsigned char *put
; /* next output */
322 unsigned have
, left
; /* available input and output */
323 unsigned long hold
; /* bit buffer */
324 unsigned bits
; /* bits in bit buffer */
325 unsigned in
, out
; /* save starting available input and output */
326 unsigned copy
; /* number of stored or match bytes to copy */
327 unsigned char *from
; /* where to copy match bytes from */
328 code
this; /* current decoding table entry */
329 code last
; /* parent table entry */
330 unsigned len
; /* length to copy for repeats, bits to drop */
331 int ret
; /* return code */
332 static const unsigned short order
[19] = /* permutation of code lengths */
333 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
335 /* Do not check for strm->next_out == NULL here as ppc zImage
336 inflates to strm->next_out = 0 */
338 if (strm
== NULL
|| strm
->state
== NULL
||
339 (strm
->next_in
== NULL
&& strm
->avail_in
!= 0))
340 return Z_STREAM_ERROR
;
342 state
= (struct inflate_state
*)strm
->state
;
344 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
350 switch (state
->mode
) {
352 if (state
->wrap
== 0) {
353 state
->mode
= TYPEDO
;
358 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
359 strm
->msg
= (char *)"incorrect header check";
363 if (BITS(4) != Z_DEFLATED
) {
364 strm
->msg
= (char *)"unknown compression method";
370 if (len
> state
->wbits
) {
371 strm
->msg
= (char *)"invalid window size";
375 state
->dmax
= 1U << len
;
376 strm
->adler
= state
->check
= zlib_adler32(0L, NULL
, 0);
377 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
382 strm
->adler
= state
->check
= REVERSE(hold
);
386 if (state
->havedict
== 0) {
390 strm
->adler
= state
->check
= zlib_adler32(0L, NULL
, 0);
393 if (flush
== Z_BLOCK
) goto inf_leave
;
401 state
->last
= BITS(1);
404 case 0: /* stored block */
405 state
->mode
= STORED
;
407 case 1: /* fixed block */
408 zlib_fixedtables(state
);
409 state
->mode
= LEN
; /* decode codes */
411 case 2: /* dynamic block */
415 strm
->msg
= (char *)"invalid block type";
421 BYTEBITS(); /* go to byte boundary */
423 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
424 strm
->msg
= (char *)"invalid stored block lengths";
428 state
->length
= (unsigned)hold
& 0xffff;
432 copy
= state
->length
;
434 if (copy
> have
) copy
= have
;
435 if (copy
> left
) copy
= left
;
436 if (copy
== 0) goto inf_leave
;
437 memcpy(put
, next
, copy
);
442 state
->length
-= copy
;
449 state
->nlen
= BITS(5) + 257;
451 state
->ndist
= BITS(5) + 1;
453 state
->ncode
= BITS(4) + 4;
455 #ifndef PKZIP_BUG_WORKAROUND
456 if (state
->nlen
> 286 || state
->ndist
> 30) {
457 strm
->msg
= (char *)"too many length or distance symbols";
463 state
->mode
= LENLENS
;
465 while (state
->have
< state
->ncode
) {
467 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
470 while (state
->have
< 19)
471 state
->lens
[order
[state
->have
++]] = 0;
472 state
->next
= state
->codes
;
473 state
->lencode
= (code
const *)(state
->next
);
475 ret
= zlib_inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
476 &(state
->lenbits
), state
->work
);
478 strm
->msg
= (char *)"invalid code lengths set";
483 state
->mode
= CODELENS
;
485 while (state
->have
< state
->nlen
+ state
->ndist
) {
487 this = state
->lencode
[BITS(state
->lenbits
)];
488 if ((unsigned)(this.bits
) <= bits
) break;
494 state
->lens
[state
->have
++] = this.val
;
497 if (this.val
== 16) {
498 NEEDBITS(this.bits
+ 2);
500 if (state
->have
== 0) {
501 strm
->msg
= (char *)"invalid bit length repeat";
505 len
= state
->lens
[state
->have
- 1];
509 else if (this.val
== 17) {
510 NEEDBITS(this.bits
+ 3);
517 NEEDBITS(this.bits
+ 7);
523 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
524 strm
->msg
= (char *)"invalid bit length repeat";
529 state
->lens
[state
->have
++] = (unsigned short)len
;
533 /* handle error breaks in while */
534 if (state
->mode
== BAD
) break;
536 /* build code tables */
537 state
->next
= state
->codes
;
538 state
->lencode
= (code
const *)(state
->next
);
540 ret
= zlib_inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
541 &(state
->lenbits
), state
->work
);
543 strm
->msg
= (char *)"invalid literal/lengths set";
547 state
->distcode
= (code
const *)(state
->next
);
549 ret
= zlib_inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
550 &(state
->next
), &(state
->distbits
), state
->work
);
552 strm
->msg
= (char *)"invalid distances set";
558 if (have
>= 6 && left
>= 258) {
560 inflate_fast(strm
, out
);
565 this = state
->lencode
[BITS(state
->lenbits
)];
566 if ((unsigned)(this.bits
) <= bits
) break;
569 if (this.op
&& (this.op
& 0xf0) == 0) {
572 this = state
->lencode
[last
.val
+
573 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
574 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
580 state
->length
= (unsigned)this.val
;
581 if ((int)(this.op
) == 0) {
590 strm
->msg
= (char *)"invalid literal/length code";
594 state
->extra
= (unsigned)(this.op
) & 15;
595 state
->mode
= LENEXT
;
598 NEEDBITS(state
->extra
);
599 state
->length
+= BITS(state
->extra
);
600 DROPBITS(state
->extra
);
605 this = state
->distcode
[BITS(state
->distbits
)];
606 if ((unsigned)(this.bits
) <= bits
) break;
609 if ((this.op
& 0xf0) == 0) {
612 this = state
->distcode
[last
.val
+
613 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
614 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
621 strm
->msg
= (char *)"invalid distance code";
625 state
->offset
= (unsigned)this.val
;
626 state
->extra
= (unsigned)(this.op
) & 15;
627 state
->mode
= DISTEXT
;
630 NEEDBITS(state
->extra
);
631 state
->offset
+= BITS(state
->extra
);
632 DROPBITS(state
->extra
);
634 #ifdef INFLATE_STRICT
635 if (state
->offset
> state
->dmax
) {
636 strm
->msg
= (char *)"invalid distance too far back";
641 if (state
->offset
> state
->whave
+ out
- left
) {
642 strm
->msg
= (char *)"invalid distance too far back";
648 if (left
== 0) goto inf_leave
;
650 if (state
->offset
> copy
) { /* copy from window */
651 copy
= state
->offset
- copy
;
652 if (copy
> state
->write
) {
653 copy
-= state
->write
;
654 from
= state
->window
+ (state
->wsize
- copy
);
657 from
= state
->window
+ (state
->write
- copy
);
658 if (copy
> state
->length
) copy
= state
->length
;
660 else { /* copy from output */
661 from
= put
- state
->offset
;
662 copy
= state
->length
;
664 if (copy
> left
) copy
= left
;
666 state
->length
-= copy
;
670 if (state
->length
== 0) state
->mode
= LEN
;
673 if (left
== 0) goto inf_leave
;
674 *put
++ = (unsigned char)(state
->length
);
682 strm
->total_out
+= out
;
685 strm
->adler
= state
->check
=
686 UPDATE(state
->check
, put
- out
, out
);
689 REVERSE(hold
)) != state
->check
) {
690 strm
->msg
= (char *)"incorrect data check";
707 return Z_STREAM_ERROR
;
711 Return from inflate(), updating the total counts and the check value.
712 If there was no progress during the inflate() call, return a buffer
713 error. Call zlib_updatewindow() to create and/or update the window state.
717 if (state
->wsize
|| (state
->mode
< CHECK
&& out
!= strm
->avail_out
))
718 zlib_updatewindow(strm
, out
);
720 in
-= strm
->avail_in
;
721 out
-= strm
->avail_out
;
722 strm
->total_in
+= in
;
723 strm
->total_out
+= out
;
725 if (state
->wrap
&& out
)
726 strm
->adler
= state
->check
=
727 UPDATE(state
->check
, strm
->next_out
- out
, out
);
729 strm
->data_type
= state
->bits
+ (state
->last
? 64 : 0) +
730 (state
->mode
== TYPE
? 128 : 0);
732 if (flush
== Z_PACKET_FLUSH
&& ret
== Z_OK
&&
733 strm
->avail_out
!= 0 && strm
->avail_in
== 0)
734 return zlib_inflateSyncPacket(strm
);
736 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
742 int zlib_inflateEnd(z_streamp strm
)
744 if (strm
== NULL
|| strm
->state
== NULL
)
745 return Z_STREAM_ERROR
;
750 * This subroutine adds the data at next_in/avail_in to the output history
751 * without performing any output. The output buffer must be "caught up";
752 * i.e. no pending output but this should always be the case. The state must
753 * be waiting on the start of a block (i.e. mode == TYPE or HEAD). On exit,
754 * the output will also be caught up, and the checksum will have been updated
757 int zlib_inflateIncomp(z_stream
*z
)
759 struct inflate_state
*state
= (struct inflate_state
*)z
->state
;
760 Byte
*saved_no
= z
->next_out
;
761 uInt saved_ao
= z
->avail_out
;
763 if (state
->mode
!= TYPE
&& state
->mode
!= HEAD
)
766 /* Setup some variables to allow misuse of updateWindow */
768 z
->next_out
= (unsigned char*)z
->next_in
+ z
->avail_in
;
770 zlib_updatewindow(z
, z
->avail_in
);
772 /* Restore saved variables */
773 z
->avail_out
= saved_ao
;
774 z
->next_out
= saved_no
;
776 z
->adler
= state
->check
=
777 UPDATE(state
->check
, z
->next_in
, z
->avail_in
);
779 z
->total_out
+= z
->avail_in
;
780 z
->total_in
+= z
->avail_in
;
781 z
->next_in
+= z
->avail_in
;
782 state
->total
+= z
->avail_in
;