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 /* architecture-specific bits */
19 #ifdef CONFIG_ZLIB_DFLTCC
20 # include "../zlib_dfltcc/dfltcc.h"
22 #define INFLATE_RESET_HOOK(strm) do {} while (0)
23 #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
24 #define INFLATE_NEED_UPDATEWINDOW(strm) 1
25 #define INFLATE_NEED_CHECKSUM(strm) 1
28 int zlib_inflate_workspacesize(void)
30 return sizeof(struct inflate_workspace
);
33 int zlib_inflateReset(z_streamp strm
)
35 struct inflate_state
*state
;
37 if (strm
== NULL
|| strm
->state
== NULL
) return Z_STREAM_ERROR
;
38 state
= (struct inflate_state
*)strm
->state
;
39 strm
->total_in
= strm
->total_out
= state
->total
= 0;
41 strm
->adler
= 1; /* to support ill-conceived Java test suite */
48 state
->lencode
= state
->distcode
= state
->next
= state
->codes
;
50 /* Initialise Window */
51 state
->wsize
= 1U << state
->wbits
;
55 INFLATE_RESET_HOOK(strm
);
59 int zlib_inflateInit2(z_streamp strm
, int windowBits
)
61 struct inflate_state
*state
;
63 if (strm
== NULL
) return Z_STREAM_ERROR
;
64 strm
->msg
= NULL
; /* in case we return an error */
66 state
= &WS(strm
)->inflate_state
;
67 strm
->state
= (struct internal_state
*)state
;
71 windowBits
= -windowBits
;
74 state
->wrap
= (windowBits
>> 4) + 1;
76 if (windowBits
< 8 || windowBits
> 15) {
77 return Z_STREAM_ERROR
;
79 state
->wbits
= (unsigned)windowBits
;
80 #ifdef CONFIG_ZLIB_DFLTCC
82 * DFLTCC requires the window to be page aligned.
83 * Thus, we overallocate and take the aligned portion of the buffer.
85 state
->window
= PTR_ALIGN(&WS(strm
)->working_window
[0], PAGE_SIZE
);
87 state
->window
= &WS(strm
)->working_window
[0];
90 return zlib_inflateReset(strm
);
94 Return state with length and distance decoding tables and index sizes set to
95 fixed code decoding. This returns fixed tables from inffixed.h.
97 static void zlib_fixedtables(struct inflate_state
*state
)
99 # include "inffixed.h"
100 state
->lencode
= lenfix
;
102 state
->distcode
= distfix
;
108 Update the window with the last wsize (normally 32K) bytes written before
109 returning. This is only called when a window is already in use, or when
110 output has been written during this inflate call, but the end of the deflate
111 stream has not been reached yet. It is also called to window dictionary data
112 when a dictionary is loaded.
114 Providing output buffers larger than 32K to inflate() should provide a speed
115 advantage, since only the last 32K of output is copied to the sliding window
116 upon return from inflate(), and since all distances after the first 32K of
117 output will fall in the output data, making match copies simpler and faster.
118 The advantage may be dependent on the size of the processor's data caches.
120 static void zlib_updatewindow(z_streamp strm
, unsigned out
)
122 struct inflate_state
*state
;
125 state
= (struct inflate_state
*)strm
->state
;
127 /* copy state->wsize or less output bytes into the circular window */
128 copy
= out
- strm
->avail_out
;
129 if (copy
>= state
->wsize
) {
130 memcpy(state
->window
, strm
->next_out
- state
->wsize
, state
->wsize
);
132 state
->whave
= state
->wsize
;
135 dist
= state
->wsize
- state
->write
;
136 if (dist
> copy
) dist
= copy
;
137 memcpy(state
->window
+ state
->write
, strm
->next_out
- copy
, dist
);
140 memcpy(state
->window
, strm
->next_out
- copy
, copy
);
142 state
->whave
= state
->wsize
;
145 state
->write
+= dist
;
146 if (state
->write
== state
->wsize
) state
->write
= 0;
147 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
154 * At the end of a Deflate-compressed PPP packet, we expect to have seen
155 * a `stored' block type value but not the (zero) length bytes.
158 Returns true if inflate is currently at the end of a block generated by
159 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
160 implementation to provide an additional safety check. PPP uses
161 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
162 block. When decompressing, PPP checks that at the end of input packet,
163 inflate is waiting for these length bytes.
165 static int zlib_inflateSyncPacket(z_streamp strm
)
167 struct inflate_state
*state
;
169 if (strm
== NULL
|| strm
->state
== NULL
) return Z_STREAM_ERROR
;
170 state
= (struct inflate_state
*)strm
->state
;
172 if (state
->mode
== STORED
&& state
->bits
== 0) {
179 /* Macros for inflate(): */
181 /* check function to use adler32() for zlib or crc32() for gzip */
182 #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
184 /* Load registers with state in inflate() for speed */
187 put = strm->next_out; \
188 left = strm->avail_out; \
189 next = strm->next_in; \
190 have = strm->avail_in; \
191 hold = state->hold; \
192 bits = state->bits; \
195 /* Restore state from registers in inflate() */
198 strm->next_out = put; \
199 strm->avail_out = left; \
200 strm->next_in = next; \
201 strm->avail_in = have; \
202 state->hold = hold; \
203 state->bits = bits; \
206 /* Clear the input bit accumulator */
213 /* Get a byte of input into the bit accumulator, or return from inflate()
214 if there is no input available. */
217 if (have == 0) goto inf_leave; \
219 hold += (unsigned long)(*next++) << bits; \
223 /* Assure that there are at least n bits in the bit accumulator. If there is
224 not enough available input to do that, then return from inflate(). */
225 #define NEEDBITS(n) \
227 while (bits < (unsigned)(n)) \
231 /* Return the low n bits of the bit accumulator (n < 16) */
233 ((unsigned)hold & ((1U << (n)) - 1))
235 /* Remove n bits from the bit accumulator */
236 #define DROPBITS(n) \
239 bits -= (unsigned)(n); \
242 /* Remove zero to seven bits as needed to go to a byte boundary */
250 inflate() uses a state machine to process as much input data and generate as
251 much output data as possible before returning. The state machine is
252 structured roughly as follows:
254 for (;;) switch (state) {
257 if (not enough input data or output space to make progress)
259 ... make progress ...
265 so when inflate() is called again, the same case is attempted again, and
266 if the appropriate resources are provided, the machine proceeds to the
267 next state. The NEEDBITS() macro is usually the way the state evaluates
268 whether it can proceed or should return. NEEDBITS() does the return if
269 the requested bits are not available. The typical use of the BITS macros
273 ... do something with BITS(n) ...
276 where NEEDBITS(n) either returns from inflate() if there isn't enough
277 input left to load n bits into the accumulator, or it continues. BITS(n)
278 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
279 the low n bits off the accumulator. INITBITS() clears the accumulator
280 and sets the number of available bits to zero. BYTEBITS() discards just
281 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
282 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
284 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
285 if there is no input available. The decoding of variable length codes uses
286 PULLBYTE() directly in order to pull just enough bytes to decode the next
289 Some states loop until they get enough input, making sure that enough
290 state information is maintained to continue the loop where it left off
291 if NEEDBITS() returns in the loop. For example, want, need, and keep
292 would all have to actually be part of the saved state in case NEEDBITS()
296 while (want < need) {
298 keep[want++] = BITS(n);
304 As shown above, if the next state is also the next case, then the break
307 A state may also return if there is not enough output space available to
308 complete that state. Those states are copying stored data, writing a
309 literal byte, and copying a matching string.
311 When returning, a "goto inf_leave" is used to update the total counters,
312 update the check value, and determine whether any progress has been made
313 during that inflate() call in order to return the proper return code.
314 Progress is defined as a change in either strm->avail_in or strm->avail_out.
315 When there is a window, goto inf_leave will update the window with the last
316 output written. If a goto inf_leave occurs in the middle of decompression
317 and there is no window currently, goto inf_leave will create one and copy
318 output to the window for the next call of inflate().
320 In this implementation, the flush parameter of inflate() only affects the
321 return code (per zlib.h). inflate() always writes as much as possible to
322 strm->next_out, given the space available and the provided input--the effect
323 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
324 the allocation of and copying into a sliding window until necessary, which
325 provides the effect documented in zlib.h for Z_FINISH when the entire input
326 stream available. So the only thing the flush parameter actually does is:
327 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
328 will return Z_BUF_ERROR if it has not reached the end of the stream.
331 int zlib_inflate(z_streamp strm
, int flush
)
333 struct inflate_state
*state
;
334 const unsigned char *next
; /* next input */
335 unsigned char *put
; /* next output */
336 unsigned have
, left
; /* available input and output */
337 unsigned long hold
; /* bit buffer */
338 unsigned bits
; /* bits in bit buffer */
339 unsigned in
, out
; /* save starting available input and output */
340 unsigned copy
; /* number of stored or match bytes to copy */
341 unsigned char *from
; /* where to copy match bytes from */
342 code
this; /* current decoding table entry */
343 code last
; /* parent table entry */
344 unsigned len
; /* length to copy for repeats, bits to drop */
345 int ret
; /* return code */
346 static const unsigned short order
[19] = /* permutation of code lengths */
347 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
349 /* Do not check for strm->next_out == NULL here as ppc zImage
350 inflates to strm->next_out = 0 */
352 if (strm
== NULL
|| strm
->state
== NULL
||
353 (strm
->next_in
== NULL
&& strm
->avail_in
!= 0))
354 return Z_STREAM_ERROR
;
356 state
= (struct inflate_state
*)strm
->state
;
358 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
364 switch (state
->mode
) {
366 if (state
->wrap
== 0) {
367 state
->mode
= TYPEDO
;
372 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
373 strm
->msg
= (char *)"incorrect header check";
377 if (BITS(4) != Z_DEFLATED
) {
378 strm
->msg
= (char *)"unknown compression method";
384 if (len
> state
->wbits
) {
385 strm
->msg
= (char *)"invalid window size";
389 state
->dmax
= 1U << len
;
390 strm
->adler
= state
->check
= zlib_adler32(0L, NULL
, 0);
391 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
396 strm
->adler
= state
->check
= REVERSE(hold
);
401 if (state
->havedict
== 0) {
405 strm
->adler
= state
->check
= zlib_adler32(0L, NULL
, 0);
409 if (flush
== Z_BLOCK
) goto inf_leave
;
412 INFLATE_TYPEDO_HOOK(strm
, flush
);
419 state
->last
= BITS(1);
422 case 0: /* stored block */
423 state
->mode
= STORED
;
425 case 1: /* fixed block */
426 zlib_fixedtables(state
);
427 state
->mode
= LEN
; /* decode codes */
429 case 2: /* dynamic block */
433 strm
->msg
= (char *)"invalid block type";
439 BYTEBITS(); /* go to byte boundary */
441 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
442 strm
->msg
= (char *)"invalid stored block lengths";
446 state
->length
= (unsigned)hold
& 0xffff;
451 copy
= state
->length
;
453 if (copy
> have
) copy
= have
;
454 if (copy
> left
) copy
= left
;
455 if (copy
== 0) goto inf_leave
;
456 memcpy(put
, next
, copy
);
461 state
->length
-= copy
;
468 state
->nlen
= BITS(5) + 257;
470 state
->ndist
= BITS(5) + 1;
472 state
->ncode
= BITS(4) + 4;
474 #ifndef PKZIP_BUG_WORKAROUND
475 if (state
->nlen
> 286 || state
->ndist
> 30) {
476 strm
->msg
= (char *)"too many length or distance symbols";
482 state
->mode
= LENLENS
;
485 while (state
->have
< state
->ncode
) {
487 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
490 while (state
->have
< 19)
491 state
->lens
[order
[state
->have
++]] = 0;
492 state
->next
= state
->codes
;
493 state
->lencode
= (code
const *)(state
->next
);
495 ret
= zlib_inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
496 &(state
->lenbits
), state
->work
);
498 strm
->msg
= (char *)"invalid code lengths set";
503 state
->mode
= CODELENS
;
506 while (state
->have
< state
->nlen
+ state
->ndist
) {
508 this = state
->lencode
[BITS(state
->lenbits
)];
509 if ((unsigned)(this.bits
) <= bits
) break;
515 state
->lens
[state
->have
++] = this.val
;
518 if (this.val
== 16) {
519 NEEDBITS(this.bits
+ 2);
521 if (state
->have
== 0) {
522 strm
->msg
= (char *)"invalid bit length repeat";
526 len
= state
->lens
[state
->have
- 1];
530 else if (this.val
== 17) {
531 NEEDBITS(this.bits
+ 3);
538 NEEDBITS(this.bits
+ 7);
544 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
545 strm
->msg
= (char *)"invalid bit length repeat";
550 state
->lens
[state
->have
++] = (unsigned short)len
;
554 /* handle error breaks in while */
555 if (state
->mode
== BAD
) break;
557 /* build code tables */
558 state
->next
= state
->codes
;
559 state
->lencode
= (code
const *)(state
->next
);
561 ret
= zlib_inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
562 &(state
->lenbits
), state
->work
);
564 strm
->msg
= (char *)"invalid literal/lengths set";
568 state
->distcode
= (code
const *)(state
->next
);
570 ret
= zlib_inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
571 &(state
->next
), &(state
->distbits
), state
->work
);
573 strm
->msg
= (char *)"invalid distances set";
580 if (have
>= 6 && left
>= 258) {
582 inflate_fast(strm
, out
);
587 this = state
->lencode
[BITS(state
->lenbits
)];
588 if ((unsigned)(this.bits
) <= bits
) break;
591 if (this.op
&& (this.op
& 0xf0) == 0) {
594 this = state
->lencode
[last
.val
+
595 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
596 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
602 state
->length
= (unsigned)this.val
;
603 if ((int)(this.op
) == 0) {
612 strm
->msg
= (char *)"invalid literal/length code";
616 state
->extra
= (unsigned)(this.op
) & 15;
617 state
->mode
= LENEXT
;
621 NEEDBITS(state
->extra
);
622 state
->length
+= BITS(state
->extra
);
623 DROPBITS(state
->extra
);
629 this = state
->distcode
[BITS(state
->distbits
)];
630 if ((unsigned)(this.bits
) <= bits
) break;
633 if ((this.op
& 0xf0) == 0) {
636 this = state
->distcode
[last
.val
+
637 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
638 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
645 strm
->msg
= (char *)"invalid distance code";
649 state
->offset
= (unsigned)this.val
;
650 state
->extra
= (unsigned)(this.op
) & 15;
651 state
->mode
= DISTEXT
;
655 NEEDBITS(state
->extra
);
656 state
->offset
+= BITS(state
->extra
);
657 DROPBITS(state
->extra
);
659 #ifdef INFLATE_STRICT
660 if (state
->offset
> state
->dmax
) {
661 strm
->msg
= (char *)"invalid distance too far back";
666 if (state
->offset
> state
->whave
+ out
- left
) {
667 strm
->msg
= (char *)"invalid distance too far back";
674 if (left
== 0) goto inf_leave
;
676 if (state
->offset
> copy
) { /* copy from window */
677 copy
= state
->offset
- copy
;
678 if (copy
> state
->write
) {
679 copy
-= state
->write
;
680 from
= state
->window
+ (state
->wsize
- copy
);
683 from
= state
->window
+ (state
->write
- copy
);
684 if (copy
> state
->length
) copy
= state
->length
;
686 else { /* copy from output */
687 from
= put
- state
->offset
;
688 copy
= state
->length
;
690 if (copy
> left
) copy
= left
;
692 state
->length
-= copy
;
696 if (state
->length
== 0) state
->mode
= LEN
;
699 if (left
== 0) goto inf_leave
;
700 *put
++ = (unsigned char)(state
->length
);
708 strm
->total_out
+= out
;
710 if (INFLATE_NEED_CHECKSUM(strm
) && out
)
711 strm
->adler
= state
->check
=
712 UPDATE(state
->check
, put
- out
, out
);
715 REVERSE(hold
)) != state
->check
) {
716 strm
->msg
= (char *)"incorrect data check";
734 return Z_STREAM_ERROR
;
738 Return from inflate(), updating the total counts and the check value.
739 If there was no progress during the inflate() call, return a buffer
740 error. Call zlib_updatewindow() to create and/or update the window state.
744 if (INFLATE_NEED_UPDATEWINDOW(strm
) &&
745 (state
->wsize
|| (state
->mode
< CHECK
&& out
!= strm
->avail_out
)))
746 zlib_updatewindow(strm
, out
);
748 in
-= strm
->avail_in
;
749 out
-= strm
->avail_out
;
750 strm
->total_in
+= in
;
751 strm
->total_out
+= out
;
753 if (INFLATE_NEED_CHECKSUM(strm
) && state
->wrap
&& out
)
754 strm
->adler
= state
->check
=
755 UPDATE(state
->check
, strm
->next_out
- out
, out
);
757 strm
->data_type
= state
->bits
+ (state
->last
? 64 : 0) +
758 (state
->mode
== TYPE
? 128 : 0);
760 if (flush
== Z_PACKET_FLUSH
&& ret
== Z_OK
&&
761 strm
->avail_out
!= 0 && strm
->avail_in
== 0)
762 return zlib_inflateSyncPacket(strm
);
764 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
770 int zlib_inflateEnd(z_streamp strm
)
772 if (strm
== NULL
|| strm
->state
== NULL
)
773 return Z_STREAM_ERROR
;
778 * This subroutine adds the data at next_in/avail_in to the output history
779 * without performing any output. The output buffer must be "caught up";
780 * i.e. no pending output but this should always be the case. The state must
781 * be waiting on the start of a block (i.e. mode == TYPE or HEAD). On exit,
782 * the output will also be caught up, and the checksum will have been updated
785 int zlib_inflateIncomp(z_stream
*z
)
787 struct inflate_state
*state
= (struct inflate_state
*)z
->state
;
788 Byte
*saved_no
= z
->next_out
;
789 uInt saved_ao
= z
->avail_out
;
791 if (state
->mode
!= TYPE
&& state
->mode
!= HEAD
)
794 /* Setup some variables to allow misuse of updateWindow */
796 z
->next_out
= (unsigned char*)z
->next_in
+ z
->avail_in
;
798 zlib_updatewindow(z
, z
->avail_in
);
800 /* Restore saved variables */
801 z
->avail_out
= saved_ao
;
802 z
->next_out
= saved_no
;
804 z
->adler
= state
->check
=
805 UPDATE(state
->check
, z
->next_in
, z
->avail_in
);
807 z
->total_out
+= z
->avail_in
;
808 z
->total_in
+= z
->avail_in
;
809 z
->next_in
+= z
->avail_in
;
810 state
->total
+= z
->avail_in
;