1 /* vi: set sw=4 ts=4: */
3 * gunzip implementation for busybox
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
10 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support
11 * files as well as stdin/stdout, and to generally behave itself wrt
12 * command line handling.
14 * General cleanup to better adhere to the style guide and make use of standard
15 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
17 * read_gz interface + associated hacking by Laurence Anderson
19 * Fixed huft_build() so decoding end-of-block code does not grab more bits
20 * than necessary (this is required by unzip applet), added inflate_cleanup()
21 * to free leaked bytebuffer memory (used in unzip.c), and some minor style
22 * guide cleanups by Ed Clark
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
40 * Copyright (C) 1992-1993 Jean-loup Gailly
41 * The unzip code was written and put in the public domain by Mark Adler.
42 * Portions of the lzw code are derived from the public domain 'compress'
43 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
44 * Ken Turkowski, Dave Mack and Peter Jannesen.
46 * See the license_msg below and the file COPYING for the software license.
47 * See the file algorithm.doc for the compression algorithms and file formats.
51 static char *license_msg[] = {
52 " Copyright (C) 1992-1993 Jean-loup Gailly",
53 " This program is free software; you can redistribute it and/or modify",
54 " it under the terms of the GNU General Public License as published by",
55 " the Free Software Foundation; either version 2, or (at your option)",
56 " any later version.",
58 " This program is distributed in the hope that it will be useful,",
59 " but WITHOUT ANY WARRANTY; without even the implied warranty of",
60 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
61 " GNU General Public License for more details.",
63 " You should have received a copy of the GNU General Public License",
64 " along with this program; if not, write to the Free Software",
65 " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
70 #include <sys/types.h>
78 #include "unarchive.h"
80 typedef struct huft_s {
81 unsigned char e; /* number of extra bits or operation */
82 unsigned char b; /* number of bits in this code or subcode */
84 unsigned short n; /* literal, length base, or distance base */
85 struct huft_s *t; /* pointer to next level of table */
89 static int gunzip_src_fd;
90 unsigned int gunzip_bytes_out; /* number of output bytes */
91 static unsigned int gunzip_outbuf_count; /* bytes in output buffer */
93 /* gunzip_window size--must be a power of two, and
94 * at least 32K for zip's deflate method */
95 enum { gunzip_wsize = 0x8000 };
96 static unsigned char *gunzip_window;
98 static unsigned int *gunzip_crc_table;
99 unsigned int gunzip_crc;
101 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
102 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
103 #define N_MAX 288 /* maximum number of codes in any set */
106 static unsigned int gunzip_bb; /* bit buffer */
107 static unsigned char gunzip_bk; /* bits in bit buffer */
109 /* These control the size of the bytebuffer */
110 static unsigned int bytebuffer_max = 0x8000;
111 static unsigned char *bytebuffer = NULL;
112 static unsigned int bytebuffer_offset = 0;
113 static unsigned int bytebuffer_size = 0;
115 static const unsigned short mask_bits[] = {
116 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
117 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
120 /* Copy lengths for literal codes 257..285 */
121 static const unsigned short cplens[] = {
122 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
123 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
126 /* note: see note #13 above about the 258 in this list. */
127 /* Extra bits for literal codes 257..285 */
128 static const unsigned char cplext[] = {
129 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
133 /* Copy offsets for distance codes 0..29 */
134 static const unsigned short cpdist[] = {
135 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
136 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
139 /* Extra bits for distance codes */
140 static const unsigned char cpdext[] = {
141 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
142 11, 11, 12, 12, 13, 13
145 /* Tables for deflate from PKZIP's appnote.txt. */
146 /* Order of the bit length code lengths */
147 static const unsigned char border[] = {
148 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
151 static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
153 while (*current < required) {
154 if (bytebuffer_offset >= bytebuffer_size) {
155 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
156 * to the front of the bytebuffer, leave 4 bytes free at end of tail
157 * so we can easily top up buffer in check_trailer_gzip() */
158 if (!(bytebuffer_size = bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) {
159 bb_error_msg_and_die("unexpected end of file");
161 bytebuffer_size += 4;
162 bytebuffer_offset = 4;
164 bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
171 static void make_gunzip_crc_table(void)
173 const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */
174 unsigned short i; /* counter for all possible eight bit values */
176 /* initial shift register value */
177 gunzip_crc = 0xffffffffL;
178 gunzip_crc_table = (unsigned int *) xmalloc(256 * sizeof(unsigned int));
180 /* Compute and print table of CRC's, five per line */
181 for (i = 0; i < 256; i++) {
182 unsigned int table_entry; /* crc shift register */
183 unsigned char k; /* byte being shifted into crc apparatus */
186 /* The idea to initialize the register with the byte instead of
187 * zero was stolen from Haruhiko Okumura's ar002
189 for (k = 8; k; k--) {
190 if (table_entry & 1) {
191 table_entry = (table_entry >> 1) ^ poly;
196 gunzip_crc_table[i] = table_entry;
201 * Free the malloc'ed tables built by huft_build(), which makes a linked
202 * list of the tables it made, with the links in a dummy first entry of
206 static int huft_free(huft_t * t)
211 /* Go through linked list, freeing from the malloced (t[-1]) address. */
213 while (p != (huft_t *) NULL) {
221 /* Given a list of code lengths and a maximum table size, make a set of
222 * tables to decode that set of codes. Return zero on success, one if
223 * the given code set is incomplete (the tables are still built in this
224 * case), two if the input is invalid (all zero length codes or an
225 * oversubscribed set of lengths), and three if not enough memory.
227 * b: code lengths in bits (all assumed <= BMAX)
228 * n: number of codes (assumed <= N_MAX)
229 * s: number of simple-valued codes (0..s-1)
230 * d: list of base values for non-simple codes
231 * e: list of extra bits for non-simple codes
232 * t: result: starting table
233 * m: maximum lookup bits, returns actual
235 int huft_build(unsigned int *b, const unsigned int n,
236 const unsigned int s, const unsigned short *d,
237 const unsigned char *e, huft_t ** t, unsigned int *m)
239 unsigned a; /* counter for codes of length k */
240 unsigned c[BMAX + 1]; /* bit length count table */
241 unsigned eob_len; /* length of end-of-block code (value 256) */
242 unsigned f; /* i repeats in table every f entries */
243 int g; /* maximum code length */
244 int h; /* table level */
245 register unsigned i; /* counter, current code */
246 register unsigned j; /* counter */
247 register int k; /* number of bits in current code */
248 register unsigned *p; /* pointer into c[], b[], or v[] */
249 register huft_t *q; /* points to current table */
250 huft_t r; /* table entry for structure assignment */
251 huft_t *u[BMAX]; /* table stack */
252 unsigned v[N_MAX]; /* values in order of bit length */
253 int ws[BMAX+1]; /* bits decoded stack */
254 register int w; /* bits decoded */
255 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
256 unsigned *xp; /* pointer into x */
257 int y; /* number of dummy codes added */
258 unsigned z; /* number of entries in current table */
260 /* Length of EOB code, if any */
261 eob_len = n > 256 ? b[256] : BMAX;
263 /* Generate counts for each bit length */
264 memset((void *)c, 0, sizeof(c));
268 c[*p]++; /* assume all entries <= BMAX */
269 p++; /* Can't combine with above line (Solaris bug) */
271 if (c[0] == n) { /* null input--all zero length codes */
272 *t = (huft_t *) NULL;
277 /* Find minimum and maximum length, bound *m by those */
278 for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
279 k = j; /* minimum code length */
280 for (i = BMAX; (c[i] == 0) && i; i--);
281 g = i; /* maximum code length */
282 *m = (*m < j) ? j : ((*m > i) ? i : *m);
284 /* Adjust last length count to fill out codes, if needed */
285 for (y = 1 << j; j < i; j++, y <<= 1) {
286 if ((y -= c[j]) < 0) {
287 return 2; /* bad input: more codes than bits */
290 if ((y -= c[i]) < 0) {
295 /* Generate starting offsets into the value table for each length */
299 while (--i) { /* note that i == g from above */
303 /* Make a table of values in order of bit lengths */
307 if ((j = *p++) != 0) {
312 /* Generate the Huffman codes and for each, make the table entries */
313 x[0] = i = 0; /* first Huffman code is zero */
314 p = v; /* grab values in bit order */
315 h = -1; /* no tables yet--level -1 */
316 w = ws[0] = 0; /* bits decoded */
317 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
318 q = (huft_t *) NULL; /* ditto */
321 /* go through the bit lengths (k already is bits in shortest code) */
322 for (; k <= g; k++) {
325 /* here i is the Huffman code of length k bits for value *p */
326 /* make tables up to required level */
327 while (k > ws[h + 1]) {
330 /* compute minimum size table less than or equal to *m bits */
331 z = (z = g - w) > *m ? *m : z; /* upper limit on table size */
332 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */
333 /* too few codes for k-w bit table */
334 f -= a + 1; /* deduct codes from patterns left */
336 while (++j < z) { /* try smaller tables up to z bits */
337 if ((f <<= 1) <= *++xp) {
338 break; /* enough codes to use up j bits */
340 f -= *xp; /* else deduct codes from patterns */
343 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
344 z = 1 << j; /* table entries for j-bit table */
345 ws[h+1] = w + j; /* set bits decoded in stack */
347 /* allocate and link in new table */
348 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
349 *t = q + 1; /* link to list for huft_free() */
350 *(t = &(q->v.t)) = NULL;
351 u[h] = ++q; /* table starts after link */
353 /* connect to last table, if there is one */
355 x[h] = i; /* save pattern for backing up */
356 r.b = (unsigned char) (w - ws[h - 1]); /* bits to dump before this table */
357 r.e = (unsigned char) (16 + j); /* bits in this table */
358 r.v.t = q; /* pointer to this table */
359 j = (i & ((1 << w) - 1)) >> ws[h - 1];
360 u[h - 1][j] = r; /* connect to last table */
364 /* set up table entry in r */
365 r.b = (unsigned char) (k - w);
367 r.e = 99; /* out of values--invalid code */
369 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
370 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
372 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
376 /* fill code-like entries with r */
378 for (j = i >> w; j < z; j += f) {
382 /* backwards increment the k-bit code i */
383 for (j = 1 << (k - 1); i & j; j >>= 1) {
388 /* backup over finished tables */
389 while ((i & ((1 << w) - 1)) != x[h]) {
395 /* return actual size of base table */
398 /* Return true (1) if we were given an incomplete table */
399 return y != 0 && g != 1;
403 * inflate (decompress) the codes in a deflated (compressed) block.
404 * Return an error code or zero if it all goes ok.
406 * tl, td: literal/length and distance decoder tables
407 * bl, bd: number of bits decoded by tl[] and td[]
409 static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
411 static unsigned int e; /* table entry flag/number of extra bits */
412 static unsigned int n, d; /* length and index for copy */
413 static unsigned int w; /* current gunzip_window position */
414 static huft_t *t; /* pointer to table entry */
415 static unsigned int ml, md; /* masks for bl and bd bits */
416 static unsigned int b; /* bit buffer */
417 static unsigned int k; /* number of bits in bit buffer */
418 static huft_t *tl, *td;
419 static unsigned int bl, bd;
420 static int resumeCopy = 0;
422 if (setup) { // 1st time we are called, copy in variables
427 /* make local copies of globals */
428 b = gunzip_bb; /* initialize bit buffer */
430 w = gunzip_outbuf_count; /* initialize gunzip_window position */
432 /* inflate the coded data */
433 ml = mask_bits[bl]; /* precompute masks for speed */
435 return 0; // Don't actually do anything the first time
438 if (resumeCopy) goto do_copy;
440 while (1) { /* do until end of block */
441 b = fill_bitbuffer(b, &k, bl);
442 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
445 bb_error_msg_and_die("inflate_codes error 1");
450 b = fill_bitbuffer(b, &k, e);
452 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
455 if (e == 16) { /* then it's a literal */
456 gunzip_window[w++] = (unsigned char) t->v.n;
457 if (w == gunzip_wsize) {
458 gunzip_outbuf_count = (w);
459 //flush_gunzip_window();
461 return 1; // We have a block to read
463 } else { /* it's an EOB or a length */
465 /* exit if end of block */
470 /* get length of block to copy */
471 b = fill_bitbuffer(b, &k, e);
472 n = t->v.n + ((unsigned) b & mask_bits[e]);
476 /* decode distance of block to copy */
477 b = fill_bitbuffer(b, &k, bd);
478 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
481 bb_error_msg_and_die("inflate_codes error 2");
485 b = fill_bitbuffer(b, &k, e);
488 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
491 b = fill_bitbuffer(b, &k, e);
492 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
500 gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
501 /* copy to new buffer to prevent possible overwrite */
502 if (w - d >= e) { /* (this test assumes unsigned comparison) */
503 memcpy(gunzip_window + w, gunzip_window + d, e);
507 /* do it slow to avoid memcpy() overlap */
510 gunzip_window[w++] = gunzip_window[d++];
513 if (w == gunzip_wsize) {
514 gunzip_outbuf_count = (w);
515 if (n) resumeCopy = 1;
517 //flush_gunzip_window();
526 /* restore the globals from the locals */
527 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
528 gunzip_bb = b; /* restore global bit buffer */
531 /* normally just after call to inflate_codes, but save code by putting it here */
532 /* free the decoding tables, return */
540 static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
542 static unsigned int n, b_stored, k_stored, w;
545 b_stored = my_b_stored;
546 k_stored = my_k_stored;
547 w = gunzip_outbuf_count; /* initialize gunzip_window position */
548 return 0; // Don't do anything first time
551 /* read and output the compressed data */
553 b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
554 gunzip_window[w++] = (unsigned char) b_stored;
555 if (w == gunzip_wsize) {
556 gunzip_outbuf_count = (w);
557 //flush_gunzip_window();
561 return 1; // We have a block
567 /* restore the globals from the locals */
568 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
569 gunzip_bb = b_stored; /* restore global bit buffer */
570 gunzip_bk = k_stored;
571 return 0; // Finished
575 * decompress an inflated block
578 * GLOBAL VARIABLES: bb, kk,
580 // Return values: -1 = inflate_stored, -2 = inflate_codes
581 static int inflate_block(int *e)
583 unsigned t; /* block type */
584 register unsigned int b; /* bit buffer */
585 unsigned int k; /* number of bits in bit buffer */
587 /* make local bit buffer */
592 /* read in last block bit */
593 b = fill_bitbuffer(b, &k, 1);
598 /* read in block type */
599 b = fill_bitbuffer(b, &k, 2);
600 t = (unsigned) b & 3;
604 /* restore the global bit buffer */
608 /* inflate that block type */
610 case 0: /* Inflate stored */
612 unsigned int n; /* number of bytes in block */
613 unsigned int b_stored; /* bit buffer */
614 unsigned int k_stored; /* number of bits in bit buffer */
616 /* make local copies of globals */
617 b_stored = gunzip_bb; /* initialize bit buffer */
618 k_stored = gunzip_bk;
620 /* go to byte boundary */
625 /* get the length and its complement */
626 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
627 n = ((unsigned) b_stored & 0xffff);
631 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
632 if (n != (unsigned) ((~b_stored) & 0xffff)) {
633 return 1; /* error in compressed data */
638 inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
641 case 1: /* Inflate fixed
642 * decompress an inflated type 1 (fixed Huffman codes) block. We should
643 * either replace this with a custom decoder, or at least precompute the
647 int i; /* temporary variable */
648 huft_t *tl; /* literal/length code table */
649 huft_t *td; /* distance code table */
650 unsigned int bl; /* lookup bits for tl */
651 unsigned int bd; /* lookup bits for td */
652 unsigned int l[288]; /* length list for huft_build */
654 /* set up literal table */
655 for (i = 0; i < 144; i++) {
658 for (; i < 256; i++) {
661 for (; i < 280; i++) {
664 for (; i < 288; i++) { /* make a complete, but wrong code set */
668 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
672 /* set up distance table */
673 for (i = 0; i < 30; i++) { /* make an incomplete code set */
677 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
682 /* decompress until an end-of-block code */
683 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
685 /* huft_free code moved into inflate_codes */
689 case 2: /* Inflate dynamic */
691 const int dbits = 6; /* bits in base distance lookup table */
692 const int lbits = 9; /* bits in base literal/length lookup table */
694 huft_t *tl; /* literal/length code table */
695 huft_t *td; /* distance code table */
696 unsigned int i; /* temporary variables */
698 unsigned int l; /* last length */
699 unsigned int m; /* mask for bit lengths table */
700 unsigned int n; /* number of lengths to get */
701 unsigned int bl; /* lookup bits for tl */
702 unsigned int bd; /* lookup bits for td */
703 unsigned int nb; /* number of bit length codes */
704 unsigned int nl; /* number of literal/length codes */
705 unsigned int nd; /* number of distance codes */
707 unsigned int ll[286 + 30]; /* literal/length and distance code lengths */
708 unsigned int b_dynamic; /* bit buffer */
709 unsigned int k_dynamic; /* number of bits in bit buffer */
711 /* make local bit buffer */
712 b_dynamic = gunzip_bb;
713 k_dynamic = gunzip_bk;
715 /* read in table lengths */
716 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
717 nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */
721 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
722 nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
726 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
727 nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */
731 if (nl > 286 || nd > 30) {
732 return 1; /* bad lengths */
735 /* read in bit-length-code lengths */
736 for (j = 0; j < nb; j++) {
737 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
738 ll[border[j]] = (unsigned int) b_dynamic & 7;
742 for (; j < 19; j++) {
746 /* build decoding table for trees--single level, 7 bit lookup */
748 i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
753 return i; /* incomplete code set */
756 /* read in literal and distance code lengths */
760 while ((unsigned int) i < n) {
761 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
762 j = (td = tl + ((unsigned int) b_dynamic & m))->b;
766 if (j < 16) { /* length of code in bits (0..15) */
767 ll[i++] = l = j; /* save last length in l */
768 } else if (j == 16) { /* repeat last length 3 to 6 times */
769 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
770 j = 3 + ((unsigned int) b_dynamic & 3);
773 if ((unsigned int) i + j > n) {
779 } else if (j == 17) { /* 3 to 10 zero length codes */
780 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
781 j = 3 + ((unsigned int) b_dynamic & 7);
784 if ((unsigned int) i + j > n) {
791 } else { /* j == 18: 11 to 138 zero length codes */
792 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
793 j = 11 + ((unsigned int) b_dynamic & 0x7f);
796 if ((unsigned int) i + j > n) {
806 /* free decoding table for trees */
809 /* restore the global bit buffer */
810 gunzip_bb = b_dynamic;
811 gunzip_bk = k_dynamic;
813 /* build the decoding tables for literal/length and distance codes */
816 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
818 bb_error_msg_and_die("Incomplete literal tree");
821 return i; /* incomplete code set */
825 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
827 bb_error_msg_and_die("incomplete distance tree");
831 return i; /* incomplete code set */
834 /* decompress until an end-of-block code */
835 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
837 /* huft_free code moved into inflate_codes */
843 bb_error_msg_and_die("bad block type %d\n", t);
847 static void calculate_gunzip_crc(void)
850 for (n = 0; n < gunzip_outbuf_count; n++) {
851 gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
853 gunzip_bytes_out += gunzip_outbuf_count;
856 static int inflate_get_next_window(void)
858 static int method = -1; // Method == -1 for stored, -2 for codes
860 static int needAnotherBlock = 1;
862 gunzip_outbuf_count = 0;
867 if (needAnotherBlock) {
869 calculate_gunzip_crc();
871 needAnotherBlock = 1;
874 method = inflate_block(&e);
875 needAnotherBlock = 0;
879 case -1: ret = inflate_stored(0,0,0,0);
881 case -2: ret = inflate_codes(0,0,0,0,0);
883 default: bb_error_msg_and_die("inflate error %d", method);
887 calculate_gunzip_crc();
888 return 1; // More data left
889 } else needAnotherBlock = 1; // End of that block
891 /* Doesnt get here */
894 /* Initialise bytebuffer, be careful not to overfill the buffer */
895 void inflate_init(unsigned int bufsize)
897 /* Set the bytebuffer size, default is same as gunzip_wsize */
898 bytebuffer_max = bufsize + 8;
899 bytebuffer_offset = 4;
903 void inflate_cleanup(void)
908 int inflate_unzip(int in, int out)
911 typedef void (*sig_type) (int);
913 /* Allocate all global buffers (for DYN_ALLOC option) */
914 gunzip_window = xmalloc(gunzip_wsize);
915 gunzip_outbuf_count = 0;
916 gunzip_bytes_out = 0;
919 /* initialize gunzip_window, bit buffer */
923 /* Create the crc table */
924 make_gunzip_crc_table();
926 /* Allocate space for buffer */
927 bytebuffer = xmalloc(bytebuffer_max);
930 int ret = inflate_get_next_window();
931 nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
933 bb_perror_msg("write");
941 free(gunzip_crc_table);
943 /* Store unused bytes in a global buffer so calling applets can access it */
944 if (gunzip_bk >= 8) {
945 /* Undo too much lookahead. The next read will be byte aligned
946 * so we can discard unused bits in the last meaningful byte. */
948 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
955 int inflate_gunzip(int in, int out)
957 unsigned int stored_crc = 0;
960 inflate_unzip(in, out);
962 /* top up the input buffer with the rest of the trailer */
963 count = bytebuffer_size - bytebuffer_offset;
965 bb_xread_all(in, &bytebuffer[bytebuffer_size], 8 - count);
966 bytebuffer_size += 8 - count;
968 for (count = 0; count != 4; count++) {
969 stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
973 /* Validate decompression - crc */
974 if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
975 bb_error_msg("crc error");
979 /* Validate decompression - size */
980 if (gunzip_bytes_out !=
981 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
982 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
983 bb_error_msg("Incorrect length");