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 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
25 * Copyright (C) 1992-1993 Jean-loup Gailly
26 * The unzip code was written and put in the public domain by Mark Adler.
27 * Portions of the lzw code are derived from the public domain 'compress'
28 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
29 * Ken Turkowski, Dave Mack and Peter Jannesen.
31 * See the file algorithm.doc for the compression algorithms and file formats.
33 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
37 #include "unarchive.h"
39 typedef struct huft_s {
40 unsigned char e; /* number of extra bits or operation */
41 unsigned char b; /* number of bits in this code or subcode */
43 unsigned short n; /* literal, length base, or distance base */
44 struct huft_s *t; /* pointer to next level of table */
48 static int gunzip_src_fd;
49 unsigned int gunzip_bytes_out; /* number of output bytes */
50 static unsigned int gunzip_outbuf_count; /* bytes in output buffer */
52 /* gunzip_window size--must be a power of two, and
53 * at least 32K for zip's deflate method */
54 enum { gunzip_wsize = 0x8000 };
55 static unsigned char *gunzip_window;
57 static uint32_t *gunzip_crc_table;
60 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
61 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
62 #define N_MAX 288 /* maximum number of codes in any set */
65 static unsigned int gunzip_bb; /* bit buffer */
66 static unsigned char gunzip_bk; /* bits in bit buffer */
68 /* These control the size of the bytebuffer */
69 static unsigned int bytebuffer_max = 0x8000;
70 static unsigned char *bytebuffer = NULL;
71 static unsigned int bytebuffer_offset = 0;
72 static unsigned int bytebuffer_size = 0;
74 static const unsigned short mask_bits[] = {
75 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
76 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
79 /* Copy lengths for literal codes 257..285 */
80 static const unsigned short cplens[] = {
81 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
82 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
85 /* note: see note #13 above about the 258 in this list. */
86 /* Extra bits for literal codes 257..285 */
87 static const unsigned char cplext[] = {
88 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,
92 /* Copy offsets for distance codes 0..29 */
93 static const unsigned short cpdist[] = {
94 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
95 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
98 /* Extra bits for distance codes */
99 static const unsigned char cpdext[] = {
100 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
101 11, 11, 12, 12, 13, 13
104 /* Tables for deflate from PKZIP's appnote.txt. */
105 /* Order of the bit length code lengths */
106 static const unsigned char border[] = {
107 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
110 static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
112 while (*current < required) {
113 if (bytebuffer_offset >= bytebuffer_size) {
114 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
115 * to the front of the bytebuffer, leave 4 bytes free at end of tail
116 * so we can easily top up buffer in check_trailer_gzip() */
117 if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8)))
118 bb_error_msg_and_die("unexpected end of file");
119 bytebuffer_size += 4;
120 bytebuffer_offset = 4;
122 bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
130 * Free the malloc'ed tables built by huft_build(), which makes a linked
131 * list of the tables it made, with the links in a dummy first entry of
135 static int huft_free(huft_t * t)
140 /* Go through linked list, freeing from the malloced (t[-1]) address. */
142 while (p != (huft_t *) NULL) {
150 /* Given a list of code lengths and a maximum table size, make a set of
151 * tables to decode that set of codes. Return zero on success, one if
152 * the given code set is incomplete (the tables are still built in this
153 * case), two if the input is invalid (all zero length codes or an
154 * oversubscribed set of lengths), and three if not enough memory.
156 * b: code lengths in bits (all assumed <= BMAX)
157 * n: number of codes (assumed <= N_MAX)
158 * s: number of simple-valued codes (0..s-1)
159 * d: list of base values for non-simple codes
160 * e: list of extra bits for non-simple codes
161 * t: result: starting table
162 * m: maximum lookup bits, returns actual
165 int huft_build(unsigned int *b, const unsigned int n,
166 const unsigned int s, const unsigned short *d,
167 const unsigned char *e, huft_t ** t, unsigned int *m)
169 unsigned a; /* counter for codes of length k */
170 unsigned c[BMAX + 1]; /* bit length count table */
171 unsigned eob_len; /* length of end-of-block code (value 256) */
172 unsigned f; /* i repeats in table every f entries */
173 int g; /* maximum code length */
174 int htl; /* table level */
175 unsigned i; /* counter, current code */
176 unsigned j; /* counter */
177 int k; /* number of bits in current code */
178 unsigned *p; /* pointer into c[], b[], or v[] */
179 huft_t *q; /* points to current table */
180 huft_t r; /* table entry for structure assignment */
181 huft_t *u[BMAX]; /* table stack */
182 unsigned v[N_MAX]; /* values in order of bit length */
183 int ws[BMAX+1]; /* bits decoded stack */
184 int w; /* bits decoded */
185 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
186 unsigned *xp; /* pointer into x */
187 int y; /* number of dummy codes added */
188 unsigned z; /* number of entries in current table */
190 /* Length of EOB code, if any */
191 eob_len = n > 256 ? b[256] : BMAX;
193 /* Generate counts for each bit length */
194 memset((void *)c, 0, sizeof(c));
198 c[*p]++; /* assume all entries <= BMAX */
199 p++; /* Can't combine with above line (Solaris bug) */
201 if (c[0] == n) { /* null input--all zero length codes */
202 *t = (huft_t *) NULL;
207 /* Find minimum and maximum length, bound *m by those */
208 for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
209 k = j; /* minimum code length */
210 for (i = BMAX; (c[i] == 0) && i; i--);
211 g = i; /* maximum code length */
212 *m = (*m < j) ? j : ((*m > i) ? i : *m);
214 /* Adjust last length count to fill out codes, if needed */
215 for (y = 1 << j; j < i; j++, y <<= 1) {
216 if ((y -= c[j]) < 0) {
217 return 2; /* bad input: more codes than bits */
220 if ((y -= c[i]) < 0) {
225 /* Generate starting offsets into the value table for each length */
229 while (--i) { /* note that i == g from above */
233 /* Make a table of values in order of bit lengths */
237 if ((j = *p++) != 0) {
242 /* Generate the Huffman codes and for each, make the table entries */
243 x[0] = i = 0; /* first Huffman code is zero */
244 p = v; /* grab values in bit order */
245 htl = -1; /* no tables yet--level -1 */
246 w = ws[0] = 0; /* bits decoded */
247 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
248 q = (huft_t *) NULL; /* ditto */
251 /* go through the bit lengths (k already is bits in shortest code) */
252 for (; k <= g; k++) {
255 /* here i is the Huffman code of length k bits for value *p */
256 /* make tables up to required level */
257 while (k > ws[htl + 1]) {
260 /* compute minimum size table less than or equal to *m bits */
261 z = (z = g - w) > *m ? *m : z; /* upper limit on table size */
262 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */
263 /* too few codes for k-w bit table */
264 f -= a + 1; /* deduct codes from patterns left */
266 while (++j < z) { /* try smaller tables up to z bits */
267 if ((f <<= 1) <= *++xp) {
268 break; /* enough codes to use up j bits */
270 f -= *xp; /* else deduct codes from patterns */
273 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
274 z = 1 << j; /* table entries for j-bit table */
275 ws[htl+1] = w + j; /* set bits decoded in stack */
277 /* allocate and link in new table */
278 q = xzalloc((z + 1) * sizeof(huft_t));
279 *t = q + 1; /* link to list for huft_free() */
281 u[htl] = ++q; /* table starts after link */
283 /* connect to last table, if there is one */
285 x[htl] = i; /* save pattern for backing up */
286 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
287 r.e = (unsigned char) (16 + j); /* bits in this table */
288 r.v.t = q; /* pointer to this table */
289 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
290 u[htl - 1][j] = r; /* connect to last table */
294 /* set up table entry in r */
295 r.b = (unsigned char) (k - w);
297 r.e = 99; /* out of values--invalid code */
299 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
300 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
302 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
306 /* fill code-like entries with r */
308 for (j = i >> w; j < z; j += f) {
312 /* backwards increment the k-bit code i */
313 for (j = 1 << (k - 1); i & j; j >>= 1) {
318 /* backup over finished tables */
319 while ((i & ((1 << w) - 1)) != x[htl]) {
325 /* return actual size of base table */
328 /* Return true (1) if we were given an incomplete table */
329 return y != 0 && g != 1;
333 * inflate (decompress) the codes in a deflated (compressed) block.
334 * Return an error code or zero if it all goes ok.
336 * tl, td: literal/length and distance decoder tables
337 * bl, bd: number of bits decoded by tl[] and td[]
339 static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
341 static unsigned int e; /* table entry flag/number of extra bits */
342 static unsigned int n, d; /* length and index for copy */
343 static unsigned int w; /* current gunzip_window position */
344 static huft_t *t; /* pointer to table entry */
345 static unsigned int ml, md; /* masks for bl and bd bits */
346 static unsigned int b; /* bit buffer */
347 static unsigned int k; /* number of bits in bit buffer */
348 static huft_t *tl, *td;
349 static unsigned int bl, bd;
350 static int resumeCopy = 0;
352 if (setup) { // 1st time we are called, copy in variables
357 /* make local copies of globals */
358 b = gunzip_bb; /* initialize bit buffer */
360 w = gunzip_outbuf_count; /* initialize gunzip_window position */
362 /* inflate the coded data */
363 ml = mask_bits[bl]; /* precompute masks for speed */
365 return 0; // Don't actually do anything the first time
368 if (resumeCopy) goto do_copy;
370 while (1) { /* do until end of block */
371 b = fill_bitbuffer(b, &k, bl);
372 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
375 bb_error_msg_and_die("inflate_codes error 1");
380 b = fill_bitbuffer(b, &k, e);
382 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
385 if (e == 16) { /* then it's a literal */
386 gunzip_window[w++] = (unsigned char) t->v.n;
387 if (w == gunzip_wsize) {
388 gunzip_outbuf_count = (w);
389 //flush_gunzip_window();
391 return 1; // We have a block to read
393 } else { /* it's an EOB or a length */
395 /* exit if end of block */
400 /* get length of block to copy */
401 b = fill_bitbuffer(b, &k, e);
402 n = t->v.n + ((unsigned) b & mask_bits[e]);
406 /* decode distance of block to copy */
407 b = fill_bitbuffer(b, &k, bd);
408 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
411 bb_error_msg_and_die("inflate_codes error 2");
415 b = fill_bitbuffer(b, &k, e);
418 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
421 b = fill_bitbuffer(b, &k, e);
422 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
430 gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
431 /* copy to new buffer to prevent possible overwrite */
432 if (w - d >= e) { /* (this test assumes unsigned comparison) */
433 memcpy(gunzip_window + w, gunzip_window + d, e);
437 /* do it slow to avoid memcpy() overlap */
440 gunzip_window[w++] = gunzip_window[d++];
443 if (w == gunzip_wsize) {
444 gunzip_outbuf_count = (w);
445 if (n) resumeCopy = 1;
447 //flush_gunzip_window();
456 /* restore the globals from the locals */
457 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
458 gunzip_bb = b; /* restore global bit buffer */
461 /* normally just after call to inflate_codes, but save code by putting it here */
462 /* free the decoding tables, return */
470 static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
472 static unsigned int n, b_stored, k_stored, w;
475 b_stored = my_b_stored;
476 k_stored = my_k_stored;
477 w = gunzip_outbuf_count; /* initialize gunzip_window position */
478 return 0; // Don't do anything first time
481 /* read and output the compressed data */
483 b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
484 gunzip_window[w++] = (unsigned char) b_stored;
485 if (w == gunzip_wsize) {
486 gunzip_outbuf_count = (w);
487 //flush_gunzip_window();
491 return 1; // We have a block
497 /* restore the globals from the locals */
498 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
499 gunzip_bb = b_stored; /* restore global bit buffer */
500 gunzip_bk = k_stored;
501 return 0; // Finished
505 * decompress an inflated block
508 * GLOBAL VARIABLES: bb, kk,
510 // Return values: -1 = inflate_stored, -2 = inflate_codes
511 static int inflate_block(int *e)
513 unsigned t; /* block type */
514 unsigned int b; /* bit buffer */
515 unsigned int k; /* number of bits in bit buffer */
517 /* make local bit buffer */
522 /* read in last block bit */
523 b = fill_bitbuffer(b, &k, 1);
528 /* read in block type */
529 b = fill_bitbuffer(b, &k, 2);
530 t = (unsigned) b & 3;
534 /* restore the global bit buffer */
538 /* inflate that block type */
540 case 0: /* Inflate stored */
542 unsigned int n; /* number of bytes in block */
543 unsigned int b_stored; /* bit buffer */
544 unsigned int k_stored; /* number of bits in bit buffer */
546 /* make local copies of globals */
547 b_stored = gunzip_bb; /* initialize bit buffer */
548 k_stored = gunzip_bk;
550 /* go to byte boundary */
555 /* get the length and its complement */
556 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
557 n = ((unsigned) b_stored & 0xffff);
561 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
562 if (n != (unsigned) ((~b_stored) & 0xffff)) {
563 return 1; /* error in compressed data */
568 inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
571 case 1: /* Inflate fixed
572 * decompress an inflated type 1 (fixed Huffman codes) block. We should
573 * either replace this with a custom decoder, or at least precompute the
577 int i; /* temporary variable */
578 huft_t *tl; /* literal/length code table */
579 huft_t *td; /* distance code table */
580 unsigned int bl; /* lookup bits for tl */
581 unsigned int bd; /* lookup bits for td */
582 unsigned int l[288]; /* length list for huft_build */
584 /* set up literal table */
585 for (i = 0; i < 144; i++) {
588 for (; i < 256; i++) {
591 for (; i < 280; i++) {
594 for (; i < 288; i++) { /* make a complete, but wrong code set */
598 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
602 /* set up distance table */
603 for (i = 0; i < 30; i++) { /* make an incomplete code set */
607 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
612 /* decompress until an end-of-block code */
613 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
615 /* huft_free code moved into inflate_codes */
619 case 2: /* Inflate dynamic */
621 const int dbits = 6; /* bits in base distance lookup table */
622 const int lbits = 9; /* bits in base literal/length lookup table */
624 huft_t *tl; /* literal/length code table */
625 huft_t *td; /* distance code table */
626 unsigned int i; /* temporary variables */
628 unsigned int l; /* last length */
629 unsigned int m; /* mask for bit lengths table */
630 unsigned int n; /* number of lengths to get */
631 unsigned int bl; /* lookup bits for tl */
632 unsigned int bd; /* lookup bits for td */
633 unsigned int nb; /* number of bit length codes */
634 unsigned int nl; /* number of literal/length codes */
635 unsigned int nd; /* number of distance codes */
637 unsigned int ll[286 + 30]; /* literal/length and distance code lengths */
638 unsigned int b_dynamic; /* bit buffer */
639 unsigned int k_dynamic; /* number of bits in bit buffer */
641 /* make local bit buffer */
642 b_dynamic = gunzip_bb;
643 k_dynamic = gunzip_bk;
645 /* read in table lengths */
646 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
647 nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */
651 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
652 nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
656 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
657 nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */
661 if (nl > 286 || nd > 30) {
662 return 1; /* bad lengths */
665 /* read in bit-length-code lengths */
666 for (j = 0; j < nb; j++) {
667 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
668 ll[border[j]] = (unsigned int) b_dynamic & 7;
672 for (; j < 19; j++) {
676 /* build decoding table for trees--single level, 7 bit lookup */
678 i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
683 return i; /* incomplete code set */
686 /* read in literal and distance code lengths */
690 while ((unsigned int) i < n) {
691 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
692 j = (td = tl + ((unsigned int) b_dynamic & m))->b;
696 if (j < 16) { /* length of code in bits (0..15) */
697 ll[i++] = l = j; /* save last length in l */
698 } else if (j == 16) { /* repeat last length 3 to 6 times */
699 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
700 j = 3 + ((unsigned int) b_dynamic & 3);
703 if ((unsigned int) i + j > n) {
709 } else if (j == 17) { /* 3 to 10 zero length codes */
710 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
711 j = 3 + ((unsigned int) b_dynamic & 7);
714 if ((unsigned int) i + j > n) {
721 } else { /* j == 18: 11 to 138 zero length codes */
722 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
723 j = 11 + ((unsigned int) b_dynamic & 0x7f);
726 if ((unsigned int) i + j > n) {
736 /* free decoding table for trees */
739 /* restore the global bit buffer */
740 gunzip_bb = b_dynamic;
741 gunzip_bk = k_dynamic;
743 /* build the decoding tables for literal/length and distance codes */
746 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
748 bb_error_msg_and_die("incomplete literal tree");
751 return i; /* incomplete code set */
755 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
757 bb_error_msg_and_die("incomplete distance tree");
761 return i; /* incomplete code set */
764 /* decompress until an end-of-block code */
765 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
767 /* huft_free code moved into inflate_codes */
773 bb_error_msg_and_die("bad block type %d", t);
777 static void calculate_gunzip_crc(void)
780 for (n = 0; n < gunzip_outbuf_count; n++) {
781 gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
783 gunzip_bytes_out += gunzip_outbuf_count;
786 static int inflate_get_next_window(void)
788 static int method = -1; // Method == -1 for stored, -2 for codes
790 static int needAnotherBlock = 1;
792 gunzip_outbuf_count = 0;
797 if (needAnotherBlock) {
799 calculate_gunzip_crc();
801 needAnotherBlock = 1;
804 method = inflate_block(&e);
805 needAnotherBlock = 0;
809 case -1: ret = inflate_stored(0,0,0,0);
811 case -2: ret = inflate_codes(0,0,0,0,0);
813 default: bb_error_msg_and_die("inflate error %d", method);
817 calculate_gunzip_crc();
818 return 1; // More data left
819 } else needAnotherBlock = 1; // End of that block
821 /* Doesnt get here */
824 /* Initialise bytebuffer, be careful not to overfill the buffer */
825 void inflate_init(unsigned int bufsize)
827 /* Set the bytebuffer size, default is same as gunzip_wsize */
828 bytebuffer_max = bufsize + 8;
829 bytebuffer_offset = 4;
833 void inflate_cleanup(void)
838 USE_DESKTOP(long long) int
839 inflate_unzip(int in, int out)
841 USE_DESKTOP(long long total = 0;)
843 typedef void (*sig_type) (int);
845 /* Allocate all global buffers (for DYN_ALLOC option) */
846 gunzip_window = xmalloc(gunzip_wsize);
847 gunzip_outbuf_count = 0;
848 gunzip_bytes_out = 0;
851 /* initialize gunzip_window, bit buffer */
855 /* Create the crc table */
856 gunzip_crc_table = crc32_filltable(0);
859 /* Allocate space for buffer */
860 bytebuffer = xmalloc(bytebuffer_max);
863 int ret = inflate_get_next_window();
864 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
866 bb_perror_msg("write");
869 USE_DESKTOP(total += nwrote;)
875 free(gunzip_crc_table);
877 /* Store unused bytes in a global buffer so calling applets can access it */
878 if (gunzip_bk >= 8) {
879 /* Undo too much lookahead. The next read will be byte aligned
880 * so we can discard unused bits in the last meaningful byte. */
882 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
886 return USE_DESKTOP(total) + 0;
889 USE_DESKTOP(long long) int
890 inflate_gunzip(int in, int out)
892 uint32_t stored_crc = 0;
894 USE_DESKTOP(long long total = )inflate_unzip(in, out);
896 USE_DESKTOP(if (total < 0) return total;)
898 /* top up the input buffer with the rest of the trailer */
899 count = bytebuffer_size - bytebuffer_offset;
901 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
902 bytebuffer_size += 8 - count;
904 for (count = 0; count != 4; count++) {
905 stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
909 /* Validate decompression - crc */
910 if (stored_crc != (~gunzip_crc)) {
911 bb_error_msg("crc error");
915 /* Validate decompression - size */
916 if (gunzip_bytes_out !=
917 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
918 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
919 bb_error_msg("incorrect length");
923 return USE_DESKTOP(total) + 0;