Initial commit
[nyanbb.git] / archival / libarchive / decompress_gunzip.c
blob87ebd3fa498e7959f2429ed6f9d27e353b757d0e
1 /* vi: set sw=4 ts=4: */
2 /*
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
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 source tree.
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <setjmp.h>
38 #include <string.h>
40 #include "bb/archival/libarchive/private.h"
42 struct bb_archive_gunzip_huft_t {
43 unsigned char e; /* number of extra bits or operation */
44 unsigned char b; /* number of bits in this code or subcode */
45 union {
46 unsigned n; /* literal, length base, or distance base */
47 /* ^^^^^ was "unsigned short", but that results in larger code */
48 struct bb_archive_gunzip_huft_t *t; /* pointer to next level of table */
49 } v;
52 enum {
53 /* gunzip_window size--must be a power of two, and
54 * at least 32K for zip's deflate method */
55 BB_ARCHIVE_GUNZIP_WSIZE = 0x8000,
56 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
57 BB_ARCHIVE_GUNZIP_BMAX = 16, /* maximum bit length of any code (16 for explode) */
58 BB_ARCHIVE_GUNZIP_N_MAX = 288, /* maximum number of codes in any set */
62 /* This is somewhat complex-looking arrangement, but it allows
63 * to place decompressor state either in bss or in
64 * malloc'ed space simply by changing #defines below.
65 * Sizes on i386:
66 * text data bss dec hex
67 * 5256 0 108 5364 14f4 - bss
68 * 4915 0 0 4915 1333 - malloc
70 #define STATE_IN_BSS 0
71 #define STATE_IN_MALLOC 1
73 struct bb_archive_gunzip_state_t {
74 off_t gunzip_bytes_out; /* number of output bytes */
75 uint32_t gunzip_crc;
77 int gunzip_src_fd;
78 unsigned gunzip_outbuf_count; /* bytes in output buffer */
80 unsigned char *gunzip_window;
82 uint32_t *gunzip_crc_table;
84 /* bitbuffer */
85 unsigned gunzip_bb; /* bit buffer */
86 unsigned char gunzip_bk; /* bits in bit buffer */
88 /* input (compressed) data */
89 unsigned char *bytebuffer; /* buffer itself */
90 off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
91 // unsigned bytebuffer_max; /* buffer size */
92 unsigned bytebuffer_offset; /* buffer position */
93 unsigned bytebuffer_size; /* how much data is there (size <= max) */
95 /* private data of inflate_codes() */
96 unsigned inflate_codes_ml; /* masks for bl and bd bits */
97 unsigned inflate_codes_md; /* masks for bl and bd bits */
98 unsigned inflate_codes_bb; /* bit buffer */
99 unsigned inflate_codes_k; /* number of bits in bit buffer */
100 unsigned inflate_codes_w; /* current gunzip_window position */
101 struct bb_archive_gunzip_huft_t *inflate_codes_tl;
102 struct bb_archive_gunzip_huft_t *inflate_codes_td;
103 unsigned inflate_codes_bl;
104 unsigned inflate_codes_bd;
105 unsigned inflate_codes_nn; /* length and index for copy */
106 unsigned inflate_codes_dd;
108 uint8_t resume_copy;
110 /* private data of inflate_get_next_window() */
111 int8_t method; /* method == -1 for stored, -2 for codes */
112 uint8_t need_another_block;
113 uint8_t end_reached;
115 /* private data of inflate_stored() */
116 unsigned inflate_stored_n;
117 unsigned inflate_stored_b;
118 unsigned inflate_stored_k;
119 unsigned inflate_stored_w;
121 const char *error_msg;
122 jmp_buf error_jmp;
124 #define gunzip_bytes_out (S()gunzip_bytes_out )
125 #define gunzip_crc (S()gunzip_crc )
126 #define gunzip_src_fd (S()gunzip_src_fd )
127 #define gunzip_outbuf_count (S()gunzip_outbuf_count)
128 #define gunzip_window (S()gunzip_window )
129 #define gunzip_crc_table (S()gunzip_crc_table )
130 #define gunzip_bb (S()gunzip_bb )
131 #define gunzip_bk (S()gunzip_bk )
132 #define to_read (S()to_read )
133 // #define bytebuffer_max (S()bytebuffer_max )
134 // Both gunzip and unzip can use constant buffer size now (16k):
135 #define bytebuffer_max 0x4000
136 #define bytebuffer (S()bytebuffer )
137 #define bytebuffer_offset (S()bytebuffer_offset )
138 #define bytebuffer_size (S()bytebuffer_size )
139 #define inflate_codes_ml (S()inflate_codes_ml )
140 #define inflate_codes_md (S()inflate_codes_md )
141 #define inflate_codes_bb (S()inflate_codes_bb )
142 #define inflate_codes_k (S()inflate_codes_k )
143 #define inflate_codes_w (S()inflate_codes_w )
144 #define inflate_codes_tl (S()inflate_codes_tl )
145 #define inflate_codes_td (S()inflate_codes_td )
146 #define inflate_codes_bl (S()inflate_codes_bl )
147 #define inflate_codes_bd (S()inflate_codes_bd )
148 #define inflate_codes_nn (S()inflate_codes_nn )
149 #define inflate_codes_dd (S()inflate_codes_dd )
150 #define resume_copy (S()resume_copy )
151 #define method (S()method )
152 #define need_another_block (S()need_another_block )
153 #define end_reached (S()end_reached )
154 #define inflate_stored_n (S()inflate_stored_n )
155 #define inflate_stored_b (S()inflate_stored_b )
156 #define inflate_stored_k (S()inflate_stored_k )
157 #define inflate_stored_w (S()inflate_stored_w )
158 #define error_msg (S()error_msg )
159 #define error_jmp (S()error_jmp )
161 /* This is a generic part */
162 #if STATE_IN_BSS /* Use global data segment */
163 #define DECLARE_STATE /*nothing*/
164 #define ALLOC_STATE /*nothing*/
165 #define DEALLOC_STATE ((void)0)
166 #define S() state.
167 #define PASS_STATE /*nothing*/
168 #define PASS_STATE_ONLY /*nothing*/
169 #define STATE_PARAM /*nothing*/
170 #define STATE_PARAM_ONLY void
171 BB_STATIC struct bb_archive_gunzip_state_t bb_archive_gunzip_state;
172 #endif
174 #if STATE_IN_MALLOC /* Use malloc space */
175 #define DECLARE_STATE struct bb_archive_gunzip_state_t *bb_archive_gunzip_state
176 #define ALLOC_STATE (bb_archive_gunzip_state = bb_xzalloc(sizeof(*bb_archive_gunzip_state)))
177 #define DEALLOC_STATE free(bb_archive_gunzip_state)
178 #define S() bb_archive_gunzip_state->
179 #define PASS_STATE bb_archive_gunzip_state,
180 #define PASS_STATE_ONLY bb_archive_gunzip_state
181 #define STATE_PARAM struct bb_archive_gunzip_state_t *bb_archive_gunzip_state,
182 #define STATE_PARAM_ONLY struct bb_archive_gunzip_state_t *bb_archive_gunzip_state
183 #endif
186 BB_STATIC uint16_t bb_archive_gunzip_mask_bits[] = {
187 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
188 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
191 /* Put lengths/offsets and extra bits in a struct of arrays
192 * to make calls to huft_build() have one fewer parameter.
194 struct bb_archive_gunzip_cp_ext {
195 uint16_t cp[31];
196 uint8_t ext[31];
198 /* Copy lengths and extra bits for literal codes 257..285 */
199 /* note: see note #13 above about the 258 in this list. */
200 BB_STATIC struct bb_archive_gunzip_cp_ext bb_archive_gunzip_lit = {
201 /*257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 */
202 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 */
203 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 },
204 { 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, 5, 5, 5, 0, 99, 99 } /* 99 == invalid */
206 /* Copy offsets and extra bits for distance codes 0..29 */
207 BB_STATIC struct bb_archive_gunzip_cp_ext bb_archive_gunzip_dist = {
208 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */
209 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 },
210 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }
213 /* Tables for deflate from PKZIP's appnote.txt. */
214 /* Order of the bit length code lengths */
215 BB_STATIC uint8_t bb_archive_gunzip_border[] = {
216 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
221 * Free the malloc'ed tables built by huft_build(), which makes a linked
222 * list of the tables it made, with the links in a dummy first entry of
223 * each table.
224 * t: table to free
226 #define BAD_HUFT(p) ((uintptr_t)(p) & 1)
227 #define ERR_RET ((struct bb_archive_gunzip_huft_t*)(uintptr_t)1)
228 BB_STATIC void bb_archive_gunzip_huft_free(struct bb_archive_gunzip_huft_t *p)
230 struct bb_archive_gunzip_huft_t *q;
233 * If 'p' has the error bit set we have to clear it, otherwise we might run
234 * into a segmentation fault or an invalid pointer to free(p)
236 //if (BAD_HUFT(p)) // commented out, since bit clearing has effect only if condition is true
237 p = (struct bb_archive_gunzip_huft_t*)((uintptr_t)p & ~(uintptr_t)ERR_RET);
239 /* Go through linked list, freeing from the malloced (t[-1]) address. */
240 while (p) {
241 q = (--p)->v.t;
242 free(p);
243 p = q;
247 BB_STATIC void bb_archive_gunzip_huft_free_all(STATE_PARAM_ONLY)
249 bb_archive_gunzip_huft_free(inflate_codes_tl);
250 bb_archive_gunzip_huft_free(inflate_codes_td);
251 inflate_codes_tl = NULL;
252 inflate_codes_td = NULL;
255 BB_STATIC void bb_archive_gunzip_abort_unzip(STATE_PARAM_ONLY)
257 bb_archive_gunzip_huft_free_all(PASS_STATE_ONLY);
258 longjmp(error_jmp, 1);
261 BB_STATIC unsigned bb_archive_gunzip_fill_bitbuffer(STATE_PARAM unsigned bitbuffer,
262 unsigned *current, const unsigned required)
264 while (*current < required) {
265 if (bytebuffer_offset >= bytebuffer_size) {
266 unsigned sz = bytebuffer_max - 4;
267 if (to_read >= 0 && to_read < sz) /* unzip only */
268 sz = to_read;
269 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
270 * to the front of the bytebuffer */
271 bytebuffer_size = bb_safe_read(gunzip_src_fd, &bytebuffer[4], sz);
272 if ((int)bytebuffer_size < 1) {
273 error_msg = "unexpected end of file";
274 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
276 if (to_read >= 0) /* unzip only */
277 to_read -= bytebuffer_size;
278 bytebuffer_size += 4;
279 bytebuffer_offset = 4;
281 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
282 bytebuffer_offset++;
283 *current += 8;
285 return bitbuffer;
289 /* Given a list of code lengths and a maximum table size, make a set of
290 * tables to decode that set of codes.
292 * b: code lengths in bits (all assumed <= BMAX)
293 * n: number of codes (assumed <= N_MAX)
294 * s: number of simple-valued codes (0..s-1)
295 * cp_ext->cp,ext: list of base values/extra bits for non-simple codes
296 * m: maximum lookup bits, returns actual
297 * result: starting table
299 * On error, returns a value with lowest-bit set on error.
300 * It can be just the value of 0x1,
301 * or a valid pointer to a Huffman table, ORed with 0x1 if incompete table
302 * is given: "fixed inflate" decoder feeds us such data.
304 BB_STATIC struct bb_archive_gunzip_huft_t* bb_archive_gunzip_huft_build(const unsigned *b,
305 const unsigned n,
306 const unsigned s, struct bb_archive_gunzip_cp_ext *cp_ext,
307 unsigned *m)
309 unsigned a; /* counter for codes of length k */
310 unsigned c[BB_ARCHIVE_GUNZIP_BMAX + 1]; /* bit length count table */
311 unsigned eob_len; /* length of end-of-block code (value 256) */
312 unsigned f; /* i repeats in table every f entries */
313 int g; /* maximum code length */
314 int htl; /* table level */
315 unsigned i; /* counter, current code */
316 unsigned j; /* counter */
317 int k; /* number of bits in current code */
318 const unsigned *p; /* pointer into c[], b[], or v[] */
319 struct bb_archive_gunzip_huft_t *q; /* points to current table */
320 struct bb_archive_gunzip_huft_t r; /* table entry for structure assignment */
321 struct bb_archive_gunzip_huft_t *u[BB_ARCHIVE_GUNZIP_BMAX]; /* table stack */
322 unsigned v[BB_ARCHIVE_GUNZIP_N_MAX + 1]; /* values in order of bit length. last v[] is never used */
323 int ws[BB_ARCHIVE_GUNZIP_BMAX + 1]; /* bits decoded stack */
324 int w; /* bits decoded */
325 unsigned x[BB_ARCHIVE_GUNZIP_BMAX + 1]; /* bit offsets, then code stack */
326 unsigned *xp; /* pointer into x */
327 int y; /* number of dummy codes added */
328 unsigned z; /* number of entries in current table */
329 struct bb_archive_gunzip_huft_t *result;
330 struct bb_archive_gunzip_huft_t **t;
332 /* Length of EOB code, if any */
333 eob_len = n > 256 ? b[256] : BB_ARCHIVE_GUNZIP_BMAX;
335 /* Generate counts for each bit length */
336 memset(c, 0, sizeof(c));
337 p = b;
338 i = n;
339 do {
340 c[*p]++; /* assume all entries <= BMAX */
341 p++; /* can't combine with above line (Solaris bug) */
342 } while (--i);
343 if (c[0] == n) { /* null input - all zero length codes */
344 q = bb_xzalloc(3 * sizeof(*q));
345 //q[0].v.t = NULL;
346 q[1].e = 99; /* invalid code marker */
347 q[1].b = 1;
348 q[2].e = 99; /* invalid code marker */
349 q[2].b = 1;
350 *m = 1;
351 return q + 1;
354 /* Find minimum and maximum length, bound *m by those */
355 for (j = 1; (j <= BB_ARCHIVE_GUNZIP_BMAX) && (c[j] == 0); j++)
356 continue;
357 k = j; /* minimum code length */
358 for (i = BB_ARCHIVE_GUNZIP_BMAX; (c[i] == 0) && i; i--)
359 continue;
360 g = i; /* maximum code length */
361 *m = (*m < j) ? j : ((*m > i) ? i : *m);
363 /* Adjust last length count to fill out codes, if needed */
364 for (y = 1 << j; j < i; j++, y <<= 1) {
365 y -= c[j];
366 if (y < 0)
367 return ERR_RET; /* bad input: more codes than bits */
369 y -= c[i];
370 if (y < 0)
371 return ERR_RET;
372 c[i] += y;
374 /* Generate starting offsets into the value table for each length */
375 x[1] = j = 0;
376 p = c + 1;
377 xp = x + 2;
378 while (--i) { /* note that i == g from above */
379 j += *p++;
380 *xp++ = j;
383 /* Make a table of values in order of bit lengths.
384 * To detect bad input, unused v[i]'s are set to invalid value UINT_MAX.
385 * In particular, last v[i] is never filled and must not be accessed.
387 memset(v, 0xff, sizeof(v));
388 p = b;
389 i = 0;
390 do {
391 j = *p++;
392 if (j != 0) {
393 v[x[j]++] = i;
395 } while (++i < n);
397 /* Generate the Huffman codes and for each, make the table entries */
398 result = ERR_RET;
399 t = &result;
400 x[0] = i = 0; /* first Huffman code is zero */
401 p = v; /* grab values in bit order */
402 htl = -1; /* no tables yet--level -1 */
403 w = ws[0] = 0; /* bits decoded */
404 u[0] = NULL; /* just to keep compilers happy */
405 q = NULL; /* ditto */
406 z = 0; /* ditto */
408 /* go through the bit lengths (k already is bits in shortest code) */
409 for (; k <= g; k++) {
410 a = c[k];
411 while (a--) {
412 /* here i is the Huffman code of length k bits for value *p */
413 /* make tables up to required level */
414 while (k > ws[htl + 1]) {
415 w = ws[++htl];
417 /* compute minimum size table less than or equal to *m bits */
418 z = g - w;
419 z = z > *m ? *m : z; /* upper limit on table size */
420 j = k - w;
421 f = 1 << j;
422 if (f > a + 1) { /* try a k-w bit table */
423 /* too few codes for k-w bit table */
424 f -= a + 1; /* deduct codes from patterns left */
425 xp = c + k;
426 while (++j < z) { /* try smaller tables up to z bits */
427 f <<= 1;
428 if (f <= *++xp) {
429 break; /* enough codes to use up j bits */
431 f -= *xp; /* else deduct codes from patterns */
434 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
435 z = 1 << j; /* table entries for j-bit table */
436 ws[htl+1] = w + j; /* set bits decoded in stack */
438 /* allocate and link in new table */
439 q = bb_xzalloc((z + 1) * sizeof(struct bb_archive_gunzip_huft_t));
440 *t = q + 1; /* link to list for huft_free() */
441 t = &(q->v.t);
442 u[htl] = ++q; /* table starts after link */
444 /* connect to last table, if there is one */
445 if (htl) {
446 x[htl] = i; /* save pattern for backing up */
447 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
448 r.e = (unsigned char) (16 + j); /* bits in this table */
449 r.v.t = q; /* pointer to this table */
450 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
451 u[htl - 1][j] = r; /* connect to last table */
455 /* set up table entry in r */
456 r.b = (unsigned char) (k - w);
457 if (/*p >= v + n || -- redundant, caught by the second check: */
458 *p == UINT_MAX /* do we access uninited v[i]? (see memset(v))*/
460 r.e = 99; /* out of values--invalid code */
461 } else if (*p < s) {
462 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
463 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
464 } else {
465 r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */
466 r.v.n = cp_ext->cp[*p++ - s];
469 /* fill code-like entries with r */
470 f = 1 << (k - w);
471 for (j = i >> w; j < z; j += f) {
472 q[j] = r;
475 /* backwards increment the k-bit code i */
476 for (j = 1 << (k - 1); i & j; j >>= 1) {
477 i ^= j;
479 i ^= j;
481 /* backup over finished tables */
482 while ((i & ((1 << w) - 1)) != x[htl]) {
483 w = ws[--htl];
488 /* return actual size of base table */
489 *m = ws[1];
491 if (y != 0 && g != 1) /* we were given an incomplete table */
492 /* return "result" ORed with 1 */
493 return (void*)((uintptr_t)result | 1);
495 return result;
500 * inflate (decompress) the codes in a deflated (compressed) block.
501 * Return an error code or zero if it all goes ok.
503 * tl, td: literal/length and distance decoder tables
504 * bl, bd: number of bits decoded by tl[] and td[]
506 /* called once from inflate_block */
508 /* map formerly local static variables to globals */
509 #define ml inflate_codes_ml
510 #define md inflate_codes_md
511 #define bb inflate_codes_bb
512 #define k inflate_codes_k
513 #define w inflate_codes_w
514 #define tl inflate_codes_tl
515 #define td inflate_codes_td
516 #define bl inflate_codes_bl
517 #define bd inflate_codes_bd
518 #define nn inflate_codes_nn
519 #define dd inflate_codes_dd
520 BB_STATIC void bb_archive_gunzip_inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
522 bl = my_bl;
523 bd = my_bd;
524 /* make local copies of globals */
525 bb = gunzip_bb; /* initialize bit buffer */
526 k = gunzip_bk;
527 w = gunzip_outbuf_count; /* initialize gunzip_window position */
528 /* inflate the coded data */
529 ml = bb_archive_gunzip_mask_bits[bl]; /* precompute masks for speed */
530 md = bb_archive_gunzip_mask_bits[bd];
532 /* called once from inflate_get_next_window */
533 BB_STATIC int bb_archive_gunzip_inflate_codes(STATE_PARAM_ONLY)
535 unsigned e; /* table entry flag/number of extra bits */
536 struct bb_archive_gunzip_huft_t *t; /* pointer to table entry */
538 if (resume_copy)
539 goto do_copy;
541 while (1) { /* do until end of block */
542 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, bl);
543 t = tl + ((unsigned) bb & ml);
544 e = t->e;
545 if (e > 16)
546 do {
547 if (e == 99) {
548 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
550 bb >>= t->b;
551 k -= t->b;
552 e -= 16;
553 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, e);
554 t = t->v.t + ((unsigned) bb & bb_archive_gunzip_mask_bits[e]);
555 e = t->e;
556 } while (e > 16);
557 bb >>= t->b;
558 k -= t->b;
559 if (e == 16) { /* then it's a literal */
560 gunzip_window[w++] = (unsigned char) t->v.n;
561 if (w == BB_ARCHIVE_GUNZIP_WSIZE) {
562 gunzip_outbuf_count = w;
563 //flush_gunzip_window();
564 w = 0;
565 return 1; // We have a block to read
567 } else { /* it's an EOB or a length */
568 /* exit if end of block */
569 if (e == 15) {
570 break;
573 /* get length of block to copy */
574 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, e);
575 nn = t->v.n + ((unsigned) bb & bb_archive_gunzip_mask_bits[e]);
576 bb >>= e;
577 k -= e;
579 /* decode distance of block to copy */
580 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, bd);
581 t = td + ((unsigned) bb & md);
582 e = t->e;
583 if (e > 16)
584 do {
585 if (e == 99) {
586 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
588 bb >>= t->b;
589 k -= t->b;
590 e -= 16;
591 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, e);
592 t = t->v.t + ((unsigned) bb & bb_archive_gunzip_mask_bits[e]);
593 e = t->e;
594 } while (e > 16);
595 bb >>= t->b;
596 k -= t->b;
597 bb = bb_archive_gunzip_fill_bitbuffer(PASS_STATE bb, &k, e);
598 dd = w - t->v.n - ((unsigned) bb & bb_archive_gunzip_mask_bits[e]);
599 bb >>= e;
600 k -= e;
602 /* do the copy */
603 do_copy:
604 do {
605 /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
606 /* Who wrote THAT?? rewritten as: */
607 unsigned delta;
609 dd &= BB_ARCHIVE_GUNZIP_WSIZE - 1;
610 e = BB_ARCHIVE_GUNZIP_WSIZE - (dd > w ? dd : w);
611 delta = w > dd ? w - dd : dd - w;
612 if (e > nn) e = nn;
613 nn -= e;
615 /* copy to new buffer to prevent possible overwrite */
616 if (delta >= e) {
617 memcpy(gunzip_window + w, gunzip_window + dd, e);
618 w += e;
619 dd += e;
620 } else {
621 /* do it slow to avoid memcpy() overlap */
622 /* !NOMEMCPY */
623 do {
624 gunzip_window[w++] = gunzip_window[dd++];
625 } while (--e);
627 if (w == BB_ARCHIVE_GUNZIP_WSIZE) {
628 gunzip_outbuf_count = w;
629 resume_copy = (nn != 0);
630 //flush_gunzip_window();
631 w = 0;
632 return 1;
634 } while (nn);
635 resume_copy = 0;
639 /* restore the globals from the locals */
640 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
641 gunzip_bb = bb; /* restore global bit buffer */
642 gunzip_bk = k;
644 /* normally just after call to inflate_codes, but save code by putting it here */
645 /* free the decoding tables (tl and td), return */
646 bb_archive_gunzip_huft_free_all(PASS_STATE_ONLY);
648 /* done */
649 return 0;
651 #undef ml
652 #undef md
653 #undef bb
654 #undef k
655 #undef w
656 #undef tl
657 #undef td
658 #undef bl
659 #undef bd
660 #undef nn
661 #undef dd
664 /* called once from inflate_block */
665 BB_STATIC void bb_archive_gunzip_inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
667 inflate_stored_n = my_n;
668 inflate_stored_b = my_b;
669 inflate_stored_k = my_k;
670 /* initialize gunzip_window position */
671 inflate_stored_w = gunzip_outbuf_count;
673 /* called once from inflate_get_next_window */
674 BB_STATIC int bb_archive_gunzip_inflate_stored(STATE_PARAM_ONLY)
676 /* read and output the compressed data */
677 while (inflate_stored_n--) {
678 inflate_stored_b = bb_archive_gunzip_fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
679 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
680 if (inflate_stored_w == BB_ARCHIVE_GUNZIP_WSIZE) {
681 gunzip_outbuf_count = inflate_stored_w;
682 //flush_gunzip_window();
683 inflate_stored_w = 0;
684 inflate_stored_b >>= 8;
685 inflate_stored_k -= 8;
686 return 1; /* We have a block */
688 inflate_stored_b >>= 8;
689 inflate_stored_k -= 8;
692 /* restore the globals from the locals */
693 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
694 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
695 gunzip_bk = inflate_stored_k;
696 return 0; /* Finished */
701 * decompress an inflated block
702 * e: last block flag
704 * GLOBAL VARIABLES: bb, kk,
706 /* Return values: -1 = inflate_stored, -2 = inflate_codes */
707 /* One callsite in inflate_get_next_window */
708 BB_STATIC int bb_archive_gunzip_inflate_block(STATE_PARAM uint8_t *e)
710 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
711 unsigned t; /* block type */
712 unsigned b; /* bit buffer */
713 unsigned k; /* number of bits in bit buffer */
715 /* make local bit buffer */
717 b = gunzip_bb;
718 k = gunzip_bk;
720 /* read in last block bit */
721 b = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b, &k, 1);
722 *e = b & 1;
723 b >>= 1;
724 k -= 1;
726 /* read in block type */
727 b = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b, &k, 2);
728 t = (unsigned) b & 3;
729 b >>= 2;
730 k -= 2;
732 /* restore the global bit buffer */
733 gunzip_bb = b;
734 gunzip_bk = k;
736 /* Do we see block type 1 often? Yes!
737 * TODO: fix performance problem (see below) */
738 //bb_error_msg("blktype %d", t);
740 /* inflate that block type */
741 switch (t) {
742 case 0: /* Inflate stored */
744 unsigned n; /* number of bytes in block */
745 unsigned b_stored; /* bit buffer */
746 unsigned k_stored; /* number of bits in bit buffer */
748 /* make local copies of globals */
749 b_stored = gunzip_bb; /* initialize bit buffer */
750 k_stored = gunzip_bk;
752 /* go to byte boundary */
753 n = k_stored & 7;
754 b_stored >>= n;
755 k_stored -= n;
757 /* get the length and its complement */
758 b_stored = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
759 n = ((unsigned) b_stored & 0xffff);
760 b_stored >>= 16;
761 k_stored -= 16;
763 b_stored = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
764 if (n != (unsigned) ((~b_stored) & 0xffff)) {
765 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
767 b_stored >>= 16;
768 k_stored -= 16;
770 bb_archive_gunzip_inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
772 return -1;
774 case 1:
775 /* Inflate fixed
776 * decompress an inflated type 1 (fixed Huffman codes) block. We should
777 * either replace this with a custom decoder, or at least precompute the
778 * Huffman tables. TODO */
780 int i; /* temporary variable */
781 unsigned bl; /* lookup bits for tl */
782 unsigned bd; /* lookup bits for td */
783 /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
784 //unsigned ll[288]; /* length list for huft_build */
786 /* set up literal table */
787 for (i = 0; i < 144; i++)
788 ll[i] = 8;
789 for (; i < 256; i++)
790 ll[i] = 9;
791 for (; i < 280; i++)
792 ll[i] = 7;
793 for (; i < 288; i++) /* make a complete, but wrong code set */
794 ll[i] = 8;
795 bl = 7;
796 inflate_codes_tl = bb_archive_gunzip_huft_build(ll, 288, 257, &bb_archive_gunzip_lit, &bl);
797 /* ^^^ never returns error here - we use known data */
799 /* set up distance table */
800 for (i = 0; i < 30; i++) /* make an incomplete code set */
801 ll[i] = 5;
802 bd = 5;
803 inflate_codes_td = bb_archive_gunzip_huft_build(ll, 30, 0, &bb_archive_gunzip_dist, &bd);
804 /* ^^^ does return error here! (lsb bit is set) - we gave it incomplete code set */
805 /* clearing error bit: */
806 inflate_codes_td = (void*)((uintptr_t)inflate_codes_td & ~(uintptr_t)1);
808 /* set up data for inflate_codes() */
809 bb_archive_gunzip_inflate_codes_setup(PASS_STATE bl, bd);
811 /* huft_free code moved into inflate_codes */
813 return -2;
815 case 2: /* Inflate dynamic */
817 enum { dbits = 6 }; /* bits in base distance lookup table */
818 enum { lbits = 9 }; /* bits in base literal/length lookup table */
820 struct bb_archive_gunzip_huft_t *td; /* distance code table */
821 unsigned i; /* temporary variables */
822 unsigned j;
823 unsigned l; /* last length */
824 unsigned m; /* mask for bit lengths table */
825 unsigned n; /* number of lengths to get */
826 unsigned bl; /* lookup bits for tl */
827 unsigned bd; /* lookup bits for td */
828 unsigned nb; /* number of bit length codes */
829 unsigned nl; /* number of literal/length codes */
830 unsigned nd; /* number of distance codes */
832 //unsigned ll[286 + 30];/* literal/length and distance code lengths */
833 unsigned b_dynamic; /* bit buffer */
834 unsigned k_dynamic; /* number of bits in bit buffer */
836 /* make local bit buffer */
837 b_dynamic = gunzip_bb;
838 k_dynamic = gunzip_bk;
840 /* read in table lengths */
841 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
842 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
844 b_dynamic >>= 5;
845 k_dynamic -= 5;
846 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
847 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
849 b_dynamic >>= 5;
850 k_dynamic -= 5;
851 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
852 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
854 b_dynamic >>= 4;
855 k_dynamic -= 4;
856 if (nl > 286 || nd > 30) {
857 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); /* bad lengths */
860 /* read in bit-length-code lengths */
861 for (j = 0; j < nb; j++) {
862 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
863 ll[bb_archive_gunzip_border[j]] = (unsigned) b_dynamic & 7;
864 b_dynamic >>= 3;
865 k_dynamic -= 3;
867 for (; j < 19; j++)
868 ll[bb_archive_gunzip_border[j]] = 0;
870 /* build decoding table for trees - single level, 7 bit lookup */
871 bl = 7;
872 inflate_codes_tl = bb_archive_gunzip_huft_build(ll, 19, 19, NULL, &bl);
873 if (BAD_HUFT(inflate_codes_tl)) {
874 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); /* incomplete code set */
877 /* read in literal and distance code lengths */
878 n = nl + nd;
879 m = bb_archive_gunzip_mask_bits[bl];
880 i = l = 0;
881 while ((unsigned) i < n) {
882 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
883 td = inflate_codes_tl + ((unsigned) b_dynamic & m);
884 j = td->b;
885 b_dynamic >>= j;
886 k_dynamic -= j;
887 j = td->v.n;
888 if (j < 16) { /* length of code in bits (0..15) */
889 ll[i++] = l = j; /* save last length in l */
890 } else if (j == 16) { /* repeat last length 3 to 6 times */
891 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
892 j = 3 + ((unsigned) b_dynamic & 3);
893 b_dynamic >>= 2;
894 k_dynamic -= 2;
895 if ((unsigned) i + j > n) {
896 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); //return 1;
898 while (j--) {
899 ll[i++] = l;
901 } else if (j == 17) { /* 3 to 10 zero length codes */
902 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
903 j = 3 + ((unsigned) b_dynamic & 7);
904 b_dynamic >>= 3;
905 k_dynamic -= 3;
906 if ((unsigned) i + j > n) {
907 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); //return 1;
909 while (j--) {
910 ll[i++] = 0;
912 l = 0;
913 } else { /* j == 18: 11 to 138 zero length codes */
914 b_dynamic = bb_archive_gunzip_fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
915 j = 11 + ((unsigned) b_dynamic & 0x7f);
916 b_dynamic >>= 7;
917 k_dynamic -= 7;
918 if ((unsigned) i + j > n) {
919 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY); //return 1;
921 while (j--) {
922 ll[i++] = 0;
924 l = 0;
928 /* free decoding table for trees */
929 bb_archive_gunzip_huft_free(inflate_codes_tl);
931 /* restore the global bit buffer */
932 gunzip_bb = b_dynamic;
933 gunzip_bk = k_dynamic;
935 /* build the decoding tables for literal/length and distance codes */
936 bl = lbits;
937 inflate_codes_tl = bb_archive_gunzip_huft_build(ll, nl, 257, &bb_archive_gunzip_lit, &bl);
938 if (BAD_HUFT(inflate_codes_tl)) {
939 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
941 bd = dbits;
942 inflate_codes_td = bb_archive_gunzip_huft_build(ll + nl, nd, 0, &bb_archive_gunzip_dist, &bd);
943 if (BAD_HUFT(inflate_codes_td)) {
944 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
947 /* set up data for inflate_codes() */
948 bb_archive_gunzip_inflate_codes_setup(PASS_STATE bl, bd);
950 /* huft_free code moved into inflate_codes */
952 return -2;
954 default:
955 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
959 /* Two callsites, both in inflate_get_next_window */
960 BB_STATIC void bb_archive_gunzip_calculate_gunzip_crc(STATE_PARAM_ONLY)
962 gunzip_crc = bb_crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table);
963 gunzip_bytes_out += gunzip_outbuf_count;
966 /* One callsite in inflate_unzip_internal */
967 BB_STATIC int bb_archive_gunzip_inflate_get_next_window(STATE_PARAM_ONLY)
969 gunzip_outbuf_count = 0;
971 while (1) {
972 int ret;
974 if (need_another_block) {
975 if (end_reached) {
976 bb_archive_gunzip_calculate_gunzip_crc(PASS_STATE_ONLY);
977 end_reached = 0;
978 /* NB: need_another_block is still set */
979 return 0; /* Last block */
981 method = bb_archive_gunzip_inflate_block(PASS_STATE &end_reached);
982 need_another_block = 0;
985 switch (method) {
986 case -1:
987 ret = bb_archive_gunzip_inflate_stored(PASS_STATE_ONLY);
988 break;
989 case -2:
990 ret = bb_archive_gunzip_inflate_codes(PASS_STATE_ONLY);
991 break;
992 default: /* cannot happen */
993 bb_archive_gunzip_abort_unzip(PASS_STATE_ONLY);
996 if (ret == 1) {
997 bb_archive_gunzip_calculate_gunzip_crc(PASS_STATE_ONLY);
998 return 1; /* more data left */
1000 need_another_block = 1; /* end of that block */
1002 /* Doesnt get here */
1006 /* Called from unpack_gz_stream() and inflate_unzip() */
1007 BB_STATIC long bb_archive_gunzip_inflate_unzip_internal(
1008 STATE_PARAM bb_archive_transformer_state_t *xstate)
1010 long n = 0;
1011 ssize_t nwrote;
1013 /* Allocate all global buffers (for DYN_ALLOC option) */
1014 gunzip_window = bb_xmalloc(BB_ARCHIVE_GUNZIP_WSIZE);
1015 gunzip_outbuf_count = 0;
1016 gunzip_bytes_out = 0;
1017 gunzip_src_fd = xstate->src_fd;
1019 /* (re) initialize state */
1020 method = -1;
1021 need_another_block = 1;
1022 resume_copy = 0;
1023 gunzip_bk = 0;
1024 gunzip_bb = 0;
1026 /* Create the crc table */
1027 gunzip_crc_table = bb_crc32_new_table_le();
1028 gunzip_crc = ~0;
1030 error_msg = "corrupted data";
1031 if (setjmp(error_jmp)) {
1032 /* Error from deep inside zip machinery */
1033 bb_simple_error_msg(error_msg);
1034 n = -1;
1035 goto ret;
1038 while (1) {
1039 int r = bb_archive_gunzip_inflate_get_next_window(PASS_STATE_ONLY);
1040 nwrote = bb_archive_transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
1041 if (nwrote == (ssize_t)-1) {
1042 n = -1;
1043 goto ret;
1045 n += nwrote;
1046 if (r == 0) break;
1049 /* Store unused bytes in a global buffer so calling applets can access it */
1050 if (gunzip_bk >= 8) {
1051 /* Undo too much lookahead. The next read will be byte aligned
1052 * so we can discard unused bits in the last meaningful byte. */
1053 bytebuffer_offset--;
1054 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
1055 gunzip_bb >>= 8;
1056 gunzip_bk -= 8;
1058 ret:
1059 /* Cleanup */
1060 free(gunzip_window);
1061 free(gunzip_crc_table);
1062 return n;
1066 /* External entry points */
1068 /* For unzip */
1070 BB_STATIC long bb_archive_gunzip_inflate_unzip(bb_archive_transformer_state_t *xstate)
1072 long n;
1073 DECLARE_STATE;
1075 ALLOC_STATE;
1077 to_read = xstate->bytes_in;
1078 // bytebuffer_max = 0x8000;
1079 bytebuffer_offset = 4;
1080 bytebuffer = bb_xmalloc(bytebuffer_max);
1081 n = bb_archive_gunzip_inflate_unzip_internal(PASS_STATE xstate);
1082 free(bytebuffer);
1084 xstate->crc32 = gunzip_crc;
1085 xstate->bytes_out = gunzip_bytes_out;
1086 DEALLOC_STATE;
1087 return n;
1091 /* For gunzip */
1093 /* helpers first */
1095 /* Top up the input buffer with at least n bytes. */
1096 BB_STATIC int bb_archive_gunzip_top_up(STATE_PARAM unsigned n)
1098 int count = bytebuffer_size - bytebuffer_offset;
1100 if (count < (int)n) {
1101 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1102 bytebuffer_offset = 0;
1103 bytebuffer_size = bb_full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1104 if ((int)bytebuffer_size < 0) {
1105 bb_simple_error_msg(bb_msg_read_error);
1106 return 0;
1108 bytebuffer_size += count;
1109 if (bytebuffer_size < n)
1110 return 0;
1112 return 1;
1115 BB_STATIC uint16_t bb_archive_gunzip_buffer_read_le_u16(STATE_PARAM_ONLY)
1117 uint16_t res = *(uint16_t*)(&bytebuffer[bytebuffer_offset]);
1118 bytebuffer_offset += 2;
1119 return res;
1122 BB_STATIC uint32_t bb_archive_gunzip_buffer_read_le_u32(STATE_PARAM_ONLY)
1124 uint32_t res = *(uint32_t*)(&bytebuffer[bytebuffer_offset]);
1126 bytebuffer_offset += 4;
1127 return res;
1130 BB_STATIC int bb_archive_gunzip_check_header_gzip(STATE_PARAM bb_archive_transformer_state_t *xstate)
1132 union {
1133 unsigned char raw[8];
1134 struct {
1135 uint8_t gz_method_u8;
1136 uint8_t flags_u8;
1137 uint8_t mtime_u32[4];
1138 uint8_t xtra_flags_u8_UNUSED;
1139 uint8_t os_flags_u8_UNUSED;
1140 } formatted; /* PACKED */
1141 } header;
1143 BB_BUILD_BUG_ON(sizeof(header) != 8);
1146 * Rewind bytebuffer. We use the beginning because the header has 8
1147 * bytes, leaving enough for unwinding afterwards.
1149 bytebuffer_size -= bytebuffer_offset;
1150 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1151 bytebuffer_offset = 0;
1153 if (!bb_archive_gunzip_top_up(PASS_STATE 8))
1154 return 0;
1155 memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1156 bytebuffer_offset += 8;
1158 /* Check the compression method */
1159 if (header.formatted.gz_method_u8 != 8) {
1160 return 0;
1163 if (header.formatted.flags_u8 & 0x04) {
1164 /* bit 2 set: extra field present */
1165 unsigned extra_short;
1167 if (!bb_archive_gunzip_top_up(PASS_STATE 2))
1168 return 0;
1169 extra_short = bb_archive_gunzip_buffer_read_le_u16(PASS_STATE_ONLY);
1170 if (!bb_archive_gunzip_top_up(PASS_STATE extra_short))
1171 return 0;
1172 /* Ignore extra field */
1173 bytebuffer_offset += extra_short;
1176 /* Discard original name and file comment if any */
1177 /* bit 3 set: original file name present */
1178 /* bit 4 set: file comment present */
1179 if (header.formatted.flags_u8 & 0x18) {
1180 while (1) {
1181 do {
1182 if (!bb_archive_gunzip_top_up(PASS_STATE 1))
1183 return 0;
1184 } while (bytebuffer[bytebuffer_offset++] != 0);
1185 if ((header.formatted.flags_u8 & 0x18) != 0x18)
1186 break;
1187 header.formatted.flags_u8 &= ~0x18;
1191 xstate->mtime = *(uint32_t*)(header.formatted.mtime_u32);
1193 /* Read the header checksum */
1194 if (header.formatted.flags_u8 & 0x02) {
1195 if (!bb_archive_gunzip_top_up(PASS_STATE 2))
1196 return 0;
1197 bytebuffer_offset += 2;
1199 return 1;
1202 BB_STATIC long bb_archive_unpack_gz_stream(bb_archive_transformer_state_t *xstate)
1204 uint32_t v32;
1205 long total, n;
1206 DECLARE_STATE;
1208 if (!xstate->signature_skipped) {
1209 uint16_t magic2;
1211 if (bb_full_read(xstate->src_fd, &magic2, 2) != 2) {
1212 bad_magic:
1213 bb_simple_error_msg("invalid magic");
1214 return -1;
1216 if (magic2 == BB_COMPRESS_MAGIC) {
1217 xstate->signature_skipped = 2;
1218 return bb_archive_unpack_Z_stream(xstate);
1220 if (magic2 != BB_GZIP_MAGIC)
1221 goto bad_magic;
1224 total = 0;
1226 ALLOC_STATE;
1227 to_read = -1;
1228 // bytebuffer_max = 0x8000;
1229 bytebuffer = bb_xmalloc(bytebuffer_max);
1230 gunzip_src_fd = xstate->src_fd;
1232 again:
1233 if (!bb_archive_gunzip_check_header_gzip(PASS_STATE xstate)) {
1234 bb_simple_error_msg("corrupted data");
1235 total = -1;
1236 goto ret;
1239 n = bb_archive_gunzip_inflate_unzip_internal(PASS_STATE xstate);
1240 if (n < 0) {
1241 total = -1;
1242 goto ret;
1244 total += n;
1246 if (!bb_archive_gunzip_top_up(PASS_STATE 8)) {
1247 bb_simple_error_msg("corrupted data");
1248 total = -1;
1249 goto ret;
1252 /* Validate decompression - crc */
1253 v32 = bb_archive_gunzip_buffer_read_le_u32(PASS_STATE_ONLY);
1254 if ((~gunzip_crc) != v32) {
1255 bb_simple_error_msg("crc error");
1256 total = -1;
1257 goto ret;
1260 /* Validate decompression - size */
1261 v32 = bb_archive_gunzip_buffer_read_le_u32(PASS_STATE_ONLY);
1262 if ((uint32_t)gunzip_bytes_out != v32) {
1263 bb_simple_error_msg("incorrect length");
1264 total = -1;
1267 if (!bb_archive_gunzip_top_up(PASS_STATE 2))
1268 goto ret; /* EOF */
1270 if (bytebuffer[bytebuffer_offset] == 0x1f
1271 && bytebuffer[bytebuffer_offset + 1] == 0x8b
1273 bytebuffer_offset += 2;
1274 goto again;
1276 /* GNU gzip says: */
1277 /*bb_error_msg("decompression OK, trailing garbage ignored");*/
1279 ret:
1280 free(bytebuffer);
1281 DEALLOC_STATE;
1282 return total;
1284 /* cleanup */
1285 #if STATE_IN_BSS
1286 #undef DECLARE_STATE
1287 #undef ALLOC_STATE
1288 #undef DEALLOC_STATE
1289 #undef S
1290 #undef PASS_STATE
1291 #undef PASS_STATE_ONLY
1292 #undef STATE_PARAM
1293 #undef STATE_PARAM_ONLY
1294 #endif
1296 #if STATE_IN_MALLOC
1297 #undef DECLARE_STATE
1298 #undef ALLOC_STATE
1299 #undef DEALLOC_STATE
1300 #undef S
1301 #undef PASS_STATE
1302 #undef PASS_STATE_ONLY
1303 #undef STATE_PARAM
1304 #undef STATE_PARAM_ONLY
1305 #endif
1307 #undef STATE_IN_BSS
1308 #undef STATE_IN_MALLOC
1309 #undef gunzip_bytes_out
1310 #undef gunzip_crc
1311 #undef gunzip_src_fd
1312 #undef gunzip_outbuf_count
1313 #undef gunzip_window
1314 #undef gunzip_crc_table
1315 #undef gunzip_bb
1316 #undef gunzip_bk
1317 #undef to_read
1318 #undef bytebuffer_max
1319 #undef bytebuffer
1320 #undef bytebuffer_offset
1321 #undef bytebuffer_size
1322 #undef inflate_codes_ml
1323 #undef inflate_codes_md
1324 #undef inflate_codes_bb
1325 #undef inflate_codes_k
1326 #undef inflate_codes_w
1327 #undef inflate_codes_tl
1328 #undef inflate_codes_td
1329 #undef inflate_codes_bl
1330 #undef inflate_codes_bd
1331 #undef inflate_codes_nn
1332 #undef inflate_codes_dd
1333 #undef resume_copy
1334 #undef method
1335 #undef need_another_block
1336 #undef end_reached
1337 #undef inflate_stored_n
1338 #undef inflate_stored_b
1339 #undef inflate_stored_k
1340 #undef inflate_stored_w
1341 #undef error_msg
1342 #undef error_jmp
1344 #undef BAD_HUFT
1345 #undef ERR_RET