"Fossies" - the Fresh Open Source Software Archive 
Member "rufus-3.13/src/bled/decompress_gunzip.c" (20 Nov 2020, 36109 Bytes) of package /linux/misc/rufus-3.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "decompress_gunzip.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
3.12_vs_3.13.
1 /* vi: set sw=4 ts=4: */
2 /*
3 * gunzip implementation for busybox
4 *
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
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.
13 *
14 * General cleanup to better adhere to the style guide and make use of standard
15 * busybox functions by Glenn McGrath
16 *
17 * read_gz interface + associated hacking by Laurence Anderson
18 *
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
23 *
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.
30 *
31 * See the file algorithm.doc for the compression algorithms and file formats.
32 *
33 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
34 */
35
36 #include <setjmp.h>
37 #include "libbb.h"
38 #include "bb_archive.h"
39
40 typedef struct huft_t {
41 unsigned char e; /* number of extra bits or operation */
42 unsigned char b; /* number of bits in this code or subcode */
43 union {
44 unsigned short n; /* literal, length base, or distance base */
45 struct huft_t *t; /* pointer to next level of table */
46 } v;
47 } huft_t;
48
49 enum {
50 /* gunzip_window size--must be a power of two, and
51 * at least 32K for zip's deflate method */
52 GUNZIP_WSIZE = BB_BUFSIZE,
53 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
54 BMAX = 16, /* maximum bit length of any code (16 for explode) */
55 N_MAX = 288, /* maximum number of codes in any set */
56 };
57
58
59 /* This is somewhat complex-looking arrangement, but it allows
60 * to place decompressor state either in bss or in
61 * malloc'ed space simply by changing #defines below.
62 * Sizes on i386:
63 * text data bss dec hex
64 * 5256 0 108 5364 14f4 - bss
65 * 4915 0 0 4915 1333 - malloc
66 */
67 #define STATE_IN_BSS 0
68 #define STATE_IN_MALLOC 1
69
70
71 typedef struct state_t {
72 off_t gunzip_bytes_out; /* number of output bytes */
73 uint32_t gunzip_crc;
74
75 int gunzip_src_fd;
76 unsigned gunzip_outbuf_count; /* bytes in output buffer */
77
78 unsigned char *gunzip_window;
79
80 uint32_t *gunzip_crc_table;
81
82 /* bitbuffer */
83 unsigned gunzip_bb; /* bit buffer */
84 unsigned char gunzip_bk; /* bits in bit buffer */
85
86 /* input (compressed) data */
87 unsigned char *bytebuffer; /* buffer itself */
88 off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
89 // unsigned bytebuffer_max; /* buffer size */
90 unsigned bytebuffer_offset; /* buffer position */
91 unsigned bytebuffer_size; /* how much data is there (size <= max) */
92
93 /* private data of inflate_codes() */
94 unsigned inflate_codes_ml; /* masks for bl and bd bits */
95 unsigned inflate_codes_md; /* masks for bl and bd bits */
96 unsigned inflate_codes_bb; /* bit buffer */
97 unsigned inflate_codes_k; /* number of bits in bit buffer */
98 unsigned inflate_codes_w; /* current gunzip_window position */
99 huft_t *inflate_codes_tl;
100 huft_t *inflate_codes_td;
101 unsigned inflate_codes_bl;
102 unsigned inflate_codes_bd;
103 unsigned inflate_codes_nn; /* length and index for copy */
104 unsigned inflate_codes_dd;
105
106 smallint resume_copy;
107
108 /* private data of inflate_get_next_window() */
109 smallint method; /* method == -1 for stored, -2 for codes */
110 smallint need_another_block;
111 smallint end_reached;
112
113 /* private data of inflate_stored() */
114 unsigned inflate_stored_n;
115 unsigned inflate_stored_b;
116 unsigned inflate_stored_k;
117 unsigned inflate_stored_w;
118
119 const char *error_msg;
120 jmp_buf error_jmp;
121 } state_t;
122 #define gunzip_bytes_out (S()gunzip_bytes_out )
123 #define gunzip_crc (S()gunzip_crc )
124 #define gunzip_src_fd (S()gunzip_src_fd )
125 #define gunzip_outbuf_count (S()gunzip_outbuf_count)
126 #define gunzip_window (S()gunzip_window )
127 #define gunzip_crc_table (S()gunzip_crc_table )
128 #define gunzip_bb (S()gunzip_bb )
129 #define gunzip_bk (S()gunzip_bk )
130 #define to_read (S()to_read )
131 #define bytebuffer_max BB_BUFSIZE
132 #define bytebuffer (S()bytebuffer )
133 #define bytebuffer_offset (S()bytebuffer_offset )
134 #define bytebuffer_size (S()bytebuffer_size )
135 #define inflate_codes_ml (S()inflate_codes_ml )
136 #define inflate_codes_md (S()inflate_codes_md )
137 #define inflate_codes_bb (S()inflate_codes_bb )
138 #define inflate_codes_k (S()inflate_codes_k )
139 #define inflate_codes_w (S()inflate_codes_w )
140 #define inflate_codes_tl (S()inflate_codes_tl )
141 #define inflate_codes_td (S()inflate_codes_td )
142 #define inflate_codes_bl (S()inflate_codes_bl )
143 #define inflate_codes_bd (S()inflate_codes_bd )
144 #define inflate_codes_nn (S()inflate_codes_nn )
145 #define inflate_codes_dd (S()inflate_codes_dd )
146 #define resume_copy (S()resume_copy )
147 #define method (S()method )
148 #define need_another_block (S()need_another_block )
149 #define end_reached (S()end_reached )
150 #define inflate_stored_n (S()inflate_stored_n )
151 #define inflate_stored_b (S()inflate_stored_b )
152 #define inflate_stored_k (S()inflate_stored_k )
153 #define inflate_stored_w (S()inflate_stored_w )
154 #define error_msg (S()error_msg )
155 #define error_jmp (S()error_jmp )
156
157 /* This is a generic part */
158 #if STATE_IN_BSS /* Use global data segment */
159 #define DECLARE_STATE /*nothing*/
160 #define ALLOC_STATE /*nothing*/
161 #define DEALLOC_STATE ((void)0)
162 #define S() state.
163 #define PASS_STATE /*nothing*/
164 #define PASS_STATE_ONLY /*nothing*/
165 #define STATE_PARAM /*nothing*/
166 #define STATE_PARAM_ONLY void
167 static state_t state;
168 #endif
169
170 #if STATE_IN_MALLOC /* Use malloc space */
171 #define DECLARE_STATE state_t *state
172 #define ALLOC_STATE (state = xzalloc(sizeof(*state)))
173 #define DEALLOC_STATE free(state)
174 #define S() state->
175 #define PASS_STATE state,
176 #define PASS_STATE_ONLY state
177 #define STATE_PARAM state_t *state,
178 #define STATE_PARAM_ONLY state_t *state
179 #endif
180
181
182 static const uint16_t mask_bits[] ALIGN2 = {
183 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
184 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
185 };
186
187 /* Copy lengths for literal codes 257..285 */
188 static const uint16_t cplens[] ALIGN2 = {
189 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
190 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
191 };
192
193 /* note: see note #13 above about the 258 in this list. */
194 /* Extra bits for literal codes 257..285 */
195 static const uint8_t cplext[] ALIGN1 = {
196 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,
197 5, 5, 5, 0, 99, 99
198 }; /* 99 == invalid */
199
200 /* Copy offsets for distance codes 0..29 */
201 static const uint16_t cpdist[] ALIGN2 = {
202 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
203 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
204 };
205
206 /* Extra bits for distance codes */
207 static const uint8_t cpdext[] ALIGN1 = {
208 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
209 11, 11, 12, 12, 13, 13
210 };
211
212 /* Tables for deflate from PKZIP's appnote.txt. */
213 /* Order of the bit length code lengths */
214 static const uint8_t border[] ALIGN1 = {
215 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
216 };
217
218
219 /*
220 * Free the malloc'ed tables built by huft_build(), which makes a linked
221 * list of the tables it made, with the links in a dummy first entry of
222 * each table.
223 * t: table to free
224 */
225 static void huft_free(huft_t *p)
226 {
227 huft_t *q;
228
229 /* Go through linked list, freeing from the malloced (t[-1]) address. */
230 while (p) {
231 q = (--p)->v.t;
232 free(p);
233 p = q;
234 }
235 }
236
237 static void huft_free_all(STATE_PARAM_ONLY)
238 {
239 huft_free(inflate_codes_tl);
240 huft_free(inflate_codes_td);
241 inflate_codes_tl = NULL;
242 inflate_codes_td = NULL;
243 }
244
245 static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
246 static void abort_unzip(STATE_PARAM_ONLY)
247 {
248 huft_free_all(PASS_STATE_ONLY);
249 longjmp(error_jmp, 1);
250 }
251
252 static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
253 {
254 while (*current < required) {
255 if (bytebuffer_offset >= bytebuffer_size) {
256 unsigned sz = bytebuffer_max - 4;
257 if (to_read >= 0 && to_read < sz) /* unzip only */
258 sz = (unsigned)to_read;
259 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
260 * to the front of the bytebuffer */
261 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
262 if ((int)bytebuffer_size < 1) {
263 error_msg = "unexpected end of file";
264 abort_unzip(PASS_STATE_ONLY);
265 }
266 if (to_read >= 0) /* unzip only */
267 to_read -= bytebuffer_size;
268 bytebuffer_size += 4;
269 bytebuffer_offset = 4;
270 }
271 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
272 bytebuffer_offset++;
273 *current += 8;
274 }
275 return bitbuffer;
276 }
277
278
279 /* Given a list of code lengths and a maximum table size, make a set of
280 * tables to decode that set of codes. Return zero on success, one if
281 * the given code set is incomplete (the tables are still built in this
282 * case), two if the input is invalid (all zero length codes or an
283 * oversubscribed set of lengths) - in this case stores NULL in *t.
284 *
285 * b: code lengths in bits (all assumed <= BMAX)
286 * n: number of codes (assumed <= N_MAX)
287 * s: number of simple-valued codes (0..s-1)
288 * d: list of base values for non-simple codes
289 * e: list of extra bits for non-simple codes
290 * t: result: starting table
291 * m: maximum lookup bits, returns actual
292 */
293 static int huft_build(const unsigned *b, const unsigned n,
294 const unsigned s, const unsigned short *d,
295 const unsigned char *e, huft_t **t, unsigned *m)
296 {
297 unsigned a; /* counter for codes of length k */
298 unsigned c[BMAX + 1]; /* bit length count table */
299 unsigned eob_len; /* length of end-of-block code (value 256) */
300 unsigned f; /* i repeats in table every f entries */
301 int g; /* maximum code length */
302 int htl; /* table level */
303 unsigned i; /* counter, current code */
304 unsigned j; /* counter */
305 int k; /* number of bits in current code */
306 unsigned *p; /* pointer into c[], b[], or v[] */
307 huft_t *q; /* points to current table */
308 huft_t r; /* table entry for structure assignment */
309 huft_t *u[BMAX]; /* table stack */
310 unsigned v[N_MAX]; /* values in order of bit length */
311 int ws[BMAX + 1]; /* bits decoded stack */
312 int w; /* bits decoded */
313 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
314 unsigned *xp; /* pointer into x */
315 int y; /* number of dummy codes added */
316 unsigned z; /* number of entries in current table */
317
318 /* Length of EOB code, if any */
319 eob_len = n > 256 ? b[256] : BMAX;
320
321 *t = NULL;
322
323 /* Generate counts for each bit length */
324 memset(c, 0, sizeof(c));
325 p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */
326 i = n;
327 do {
328 c[*p]++; /* assume all entries <= BMAX */
329 p++; /* can't combine with above line (Solaris bug) */
330 } while (--i);
331 if (c[0] == n) { /* null input - all zero length codes */
332 *m = 0;
333 return 2;
334 }
335
336 /* Find minimum and maximum length, bound *m by those */
337 for (j = 1; (j <= BMAX) && (c[j] == 0); j++)
338 continue;
339 k = j; /* minimum code length */
340 for (i = BMAX; (c[i] == 0) && i; i--)
341 continue;
342 g = i; /* maximum code length */
343 *m = (*m < j) ? j : ((*m > i) ? i : *m);
344
345 /* Adjust last length count to fill out codes, if needed */
346 for (y = 1 << j; j < i; j++, y <<= 1) {
347 y -= c[j];
348 if (y < 0)
349 return 2; /* bad input: more codes than bits */
350 }
351 y -= c[i];
352 if (y < 0)
353 return 2;
354 c[i] += y;
355
356 /* Generate starting offsets into the value table for each length */
357 x[1] = j = 0;
358 p = c + 1;
359 xp = x + 2;
360 while (--i) { /* note that i == g from above */
361 j += *p++;
362 *xp++ = j;
363 }
364
365 /* Make a table of values in order of bit lengths */
366 p = (unsigned *) b;
367 i = 0;
368 do {
369 j = *p++;
370 if (j != 0) {
371 v[x[j]++] = i;
372 }
373 } while (++i < n);
374
375 /* Generate the Huffman codes and for each, make the table entries */
376 x[0] = i = 0; /* first Huffman code is zero */
377 p = v; /* grab values in bit order */
378 htl = -1; /* no tables yet--level -1 */
379 w = ws[0] = 0; /* bits decoded */
380 u[0] = NULL; /* just to keep compilers happy */
381 q = NULL; /* ditto */
382 z = 0; /* ditto */
383
384 /* go through the bit lengths (k already is bits in shortest code) */
385 for (; k <= g; k++) {
386 a = c[k];
387 while (a--) {
388 /* here i is the Huffman code of length k bits for value *p */
389 /* make tables up to required level */
390 while (k > ws[htl + 1]) {
391 w = ws[++htl];
392
393 /* compute minimum size table less than or equal to *m bits */
394 z = g - w;
395 z = z > *m ? *m : z; /* upper limit on table size */
396 j = k - w;
397 f = 1 << j;
398 if (f > a + 1) { /* try a k-w bit table */
399 /* too few codes for k-w bit table */
400 f -= a + 1; /* deduct codes from patterns left */
401 xp = c + k;
402 while (++j < z) { /* try smaller tables up to z bits */
403 f <<= 1;
404 if (f <= *++xp) {
405 break; /* enough codes to use up j bits */
406 }
407 f -= *xp; /* else deduct codes from patterns */
408 }
409 }
410 j = ((w + j) > (int)eob_len && w < (int)eob_len) ? eob_len - w : j; /* make EOB code end at table */
411 z = 1 << j; /* table entries for j-bit table */
412 ws[htl+1] = w + j; /* set bits decoded in stack */
413
414 /* allocate and link in new table */
415 q = xzalloc((z + 1) * sizeof(huft_t));
416 *t = q + 1; /* link to list for huft_free() */
417 t = &(q->v.t);
418 u[htl] = ++q; /* table starts after link */
419
420 /* connect to last table, if there is one */
421 if (htl) {
422 x[htl] = i; /* save pattern for backing up */
423 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
424 r.e = (unsigned char) (16 + j); /* bits in this table */
425 r.v.t = q; /* pointer to this table */
426 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
427 u[htl - 1][j] = r; /* connect to last table */
428 }
429 }
430
431 /* set up table entry in r */
432 r.b = (unsigned char) (k - w);
433 if (p >= v + n) {
434 r.e = 99; /* out of values--invalid code */
435 } else if (*p < s) {
436 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
437 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
438 } else {
439 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
440 r.v.n = d[*p++ - s];
441 }
442
443 /* fill code-like entries with r */
444 f = 1 << (k - w);
445 for (j = i >> w; j < z; j += f) {
446 q[j] = r;
447 }
448
449 /* backwards increment the k-bit code i */
450 for (j = 1 << (k - 1); i & j; j >>= 1) {
451 i ^= j;
452 }
453 i ^= j;
454
455 /* backup over finished tables */
456 while ((i & ((1 << w) - 1)) != x[htl]) {
457 w = ws[--htl];
458 }
459 }
460 }
461
462 /* return actual size of base table */
463 *m = ws[1];
464
465 /* Return 1 if we were given an incomplete table */
466 return y != 0 && g != 1;
467 }
468
469
470 /*
471 * inflate (decompress) the codes in a deflated (compressed) block.
472 * Return an error code or zero if it all goes ok.
473 *
474 * tl, td: literal/length and distance decoder tables
475 * bl, bd: number of bits decoded by tl[] and td[]
476 */
477 /* called once from inflate_block */
478
479 /* map formerly local static variables to globals */
480 #define ml inflate_codes_ml
481 #define md inflate_codes_md
482 #define bb inflate_codes_bb
483 #define k inflate_codes_k
484 #define w inflate_codes_w
485 #define tl inflate_codes_tl
486 #define td inflate_codes_td
487 #define bl inflate_codes_bl
488 #define bd inflate_codes_bd
489 #define nn inflate_codes_nn
490 #define dd inflate_codes_dd
491 static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
492 {
493 bl = my_bl;
494 bd = my_bd;
495 /* make local copies of globals */
496 bb = gunzip_bb; /* initialize bit buffer */
497 k = gunzip_bk;
498 w = gunzip_outbuf_count; /* initialize gunzip_window position */
499 /* inflate the coded data */
500 ml = mask_bits[bl]; /* precompute masks for speed */
501 md = mask_bits[bd];
502 }
503 /* called once from inflate_get_next_window */
504 static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
505 {
506 unsigned e; /* table entry flag/number of extra bits */
507 huft_t *t; /* pointer to table entry */
508
509 if (resume_copy)
510 goto do_copy;
511
512 while (1) { /* do until end of block */
513 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
514 t = tl + ((unsigned) bb & ml);
515 e = t->e;
516 if (e > 16)
517 do {
518 if (e == 99)
519 abort_unzip(PASS_STATE_ONLY);;
520 bb >>= t->b;
521 k -= t->b;
522 e -= 16;
523 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
524 t = t->v.t + ((unsigned) bb & mask_bits[e]);
525 e = t->e;
526 } while (e > 16);
527 bb >>= t->b;
528 k -= t->b;
529 if (e == 16) { /* then it's a literal */
530 gunzip_window[w++] = (unsigned char) t->v.n;
531 if (w == GUNZIP_WSIZE) {
532 gunzip_outbuf_count = w;
533 //flush_gunzip_window();
534 w = 0;
535 return 1; // We have a block to read
536 }
537 } else { /* it's an EOB or a length */
538 /* exit if end of block */
539 if (e == 15) {
540 break;
541 }
542
543 /* get length of block to copy */
544 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
545 nn = t->v.n + ((unsigned) bb & mask_bits[e]);
546 bb >>= e;
547 k -= e;
548
549 /* decode distance of block to copy */
550 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
551 t = td + ((unsigned) bb & md);
552 e = t->e;
553 if (e > 16)
554 do {
555 if (e == 99)
556 abort_unzip(PASS_STATE_ONLY);
557 bb >>= t->b;
558 k -= t->b;
559 e -= 16;
560 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
561 t = t->v.t + ((unsigned) bb & mask_bits[e]);
562 e = t->e;
563 } while (e > 16);
564 bb >>= t->b;
565 k -= t->b;
566 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
567 dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
568 bb >>= e;
569 k -= e;
570
571 /* do the copy */
572 do_copy:
573 do {
574 /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
575 /* Who wrote THAT?? rewritten as: */
576 unsigned delta;
577
578 dd &= GUNZIP_WSIZE - 1;
579 e = GUNZIP_WSIZE - (dd > w ? dd : w);
580 delta = w > dd ? w - dd : dd - w;
581 if (e > nn) e = nn;
582 nn -= e;
583
584 /* copy to new buffer to prevent possible overwrite */
585 if (delta >= e) {
586 memcpy(gunzip_window + w, gunzip_window + dd, e);
587 w += e;
588 dd += e;
589 } else {
590 /* do it slow to avoid memcpy() overlap */
591 /* !NOMEMCPY */
592 do {
593 gunzip_window[w++] = gunzip_window[dd++];
594 } while (--e);
595 }
596 if (w == GUNZIP_WSIZE) {
597 gunzip_outbuf_count = w;
598 resume_copy = (nn != 0);
599 //flush_gunzip_window();
600 w = 0;
601 return 1;
602 }
603 } while (nn);
604 resume_copy = 0;
605 }
606 }
607
608 /* restore the globals from the locals */
609 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
610 gunzip_bb = bb; /* restore global bit buffer */
611 gunzip_bk = k;
612
613 /* normally just after call to inflate_codes, but save code by putting it here */
614 /* free the decoding tables (tl and td), return */
615 huft_free_all(PASS_STATE_ONLY);
616
617 /* done */
618 return 0;
619 }
620 #undef ml
621 #undef md
622 #undef bb
623 #undef k
624 #undef w
625 #undef tl
626 #undef td
627 #undef bl
628 #undef bd
629 #undef nn
630 #undef dd
631
632
633 /* called once from inflate_block */
634 static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
635 {
636 inflate_stored_n = my_n;
637 inflate_stored_b = my_b;
638 inflate_stored_k = my_k;
639 /* initialize gunzip_window position */
640 inflate_stored_w = gunzip_outbuf_count;
641 }
642 /* called once from inflate_get_next_window */
643 static int inflate_stored(STATE_PARAM_ONLY)
644 {
645 /* read and output the compressed data */
646 while (inflate_stored_n--) {
647 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
648 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
649 if (inflate_stored_w == GUNZIP_WSIZE) {
650 gunzip_outbuf_count = inflate_stored_w;
651 //flush_gunzip_window();
652 inflate_stored_w = 0;
653 inflate_stored_b >>= 8;
654 inflate_stored_k -= 8;
655 return 1; /* We have a block */
656 }
657 inflate_stored_b >>= 8;
658 inflate_stored_k -= 8;
659 }
660
661 /* restore the globals from the locals */
662 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
663 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
664 gunzip_bk = inflate_stored_k;
665 return 0; /* Finished */
666 }
667
668
669 /*
670 * decompress an inflated block
671 * e: last block flag
672 *
673 * GLOBAL VARIABLES: bb, kk,
674 */
675 /* Return values: -1 = inflate_stored, -2 = inflate_codes */
676 /* One callsite in inflate_get_next_window */
677 static int inflate_block(STATE_PARAM smallint *e)
678 {
679 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
680 unsigned t; /* block type */
681 unsigned b; /* bit buffer */
682 unsigned k; /* number of bits in bit buffer */
683
684 /* make local bit buffer */
685
686 b = gunzip_bb;
687 k = gunzip_bk;
688
689 /* read in last block bit */
690 b = fill_bitbuffer(PASS_STATE b, &k, 1);
691 *e = b & 1;
692 b >>= 1;
693 k -= 1;
694
695 /* read in block type */
696 b = fill_bitbuffer(PASS_STATE b, &k, 2);
697 t = (unsigned) b & 3;
698 b >>= 2;
699 k -= 2;
700
701 /* restore the global bit buffer */
702 gunzip_bb = b;
703 gunzip_bk = k;
704
705 /* Do we see block type 1 often? Yes!
706 * TODO: fix performance problem (see below) */
707 //bb_error_msg("blktype %d", t);
708
709 /* inflate that block type */
710 switch (t) {
711 case 0: /* Inflate stored */
712 {
713 unsigned n; /* number of bytes in block */
714 unsigned b_stored; /* bit buffer */
715 unsigned k_stored; /* number of bits in bit buffer */
716
717 /* make local copies of globals */
718 b_stored = gunzip_bb; /* initialize bit buffer */
719 k_stored = gunzip_bk;
720
721 /* go to byte boundary */
722 n = k_stored & 7;
723 b_stored >>= n;
724 k_stored -= n;
725
726 /* get the length and its complement */
727 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
728 n = ((unsigned) b_stored & 0xffff);
729 b_stored >>= 16;
730 k_stored -= 16;
731
732 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
733 if (n != (unsigned) ((~b_stored) & 0xffff)) {
734 abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
735 }
736 b_stored >>= 16;
737 k_stored -= 16;
738
739 inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
740
741 return -1;
742 }
743 case 1:
744 /* Inflate fixed
745 * decompress an inflated type 1 (fixed Huffman codes) block. We should
746 * either replace this with a custom decoder, or at least precompute the
747 * Huffman tables. TODO */
748 {
749 int i; /* temporary variable */
750 unsigned bl; /* lookup bits for tl */
751 unsigned bd; /* lookup bits for td */
752 /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
753 //unsigned ll[288]; /* length list for huft_build */
754
755 /* set up literal table */
756 for (i = 0; i < 144; i++)
757 ll[i] = 8;
758 for (; i < 256; i++)
759 ll[i] = 9;
760 for (; i < 280; i++)
761 ll[i] = 7;
762 for (; i < 288; i++) /* make a complete, but wrong code set */
763 ll[i] = 8;
764 bl = 7;
765 huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
766 /* huft_build() never return nonzero - we use known data */
767
768 /* set up distance table */
769 for (i = 0; i < 30; i++) /* make an incomplete code set */
770 ll[i] = 5;
771 bd = 5;
772 huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
773
774 /* set up data for inflate_codes() */
775 inflate_codes_setup(PASS_STATE bl, bd);
776
777 /* huft_free code moved into inflate_codes */
778
779 return -2;
780 }
781 case 2: /* Inflate dynamic */
782 {
783 enum { dbits = 6 }; /* bits in base distance lookup table */
784 enum { lbits = 9 }; /* bits in base literal/length lookup table */
785
786 huft_t *td; /* distance code table */
787 unsigned i; /* temporary variables */
788 unsigned j;
789 unsigned l; /* last length */
790 unsigned m; /* mask for bit lengths table */
791 unsigned n; /* number of lengths to get */
792 unsigned bl; /* lookup bits for tl */
793 unsigned bd; /* lookup bits for td */
794 unsigned nb; /* number of bit length codes */
795 unsigned nl; /* number of literal/length codes */
796 unsigned nd; /* number of distance codes */
797
798 //unsigned ll[286 + 30];/* literal/length and distance code lengths */
799 unsigned b_dynamic; /* bit buffer */
800 unsigned k_dynamic; /* number of bits in bit buffer */
801
802 /* make local bit buffer */
803 b_dynamic = gunzip_bb;
804 k_dynamic = gunzip_bk;
805
806 /* read in table lengths */
807 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
808 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
809
810 b_dynamic >>= 5;
811 k_dynamic -= 5;
812 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
813 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
814
815 b_dynamic >>= 5;
816 k_dynamic -= 5;
817 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
818 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
819
820 b_dynamic >>= 4;
821 k_dynamic -= 4;
822 if (nl > 286 || nd > 30)
823 abort_unzip(PASS_STATE_ONLY); /* bad lengths */
824
825 /* read in bit-length-code lengths */
826 for (j = 0; j < nb; j++) {
827 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
828 ll[border[j]] = (unsigned) b_dynamic & 7;
829 b_dynamic >>= 3;
830 k_dynamic -= 3;
831 }
832 for (; j < 19; j++)
833 ll[border[j]] = 0;
834
835 /* build decoding table for trees - single level, 7 bit lookup */
836 bl = 7;
837 i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
838 if (i != 0) {
839 abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */
840 }
841
842 /* read in literal and distance code lengths */
843 n = nl + nd;
844 m = mask_bits[bl];
845 i = l = 0;
846 while ((unsigned) i < n) {
847 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
848 td = inflate_codes_tl + ((unsigned) b_dynamic & m);
849 j = td->b;
850 b_dynamic >>= j;
851 k_dynamic -= j;
852 j = td->v.n;
853 if (j < 16) { /* length of code in bits (0..15) */
854 ll[i++] = l = j; /* save last length in l */
855 } else if (j == 16) { /* repeat last length 3 to 6 times */
856 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
857 j = 3 + ((unsigned) b_dynamic & 3);
858 b_dynamic >>= 2;
859 k_dynamic -= 2;
860 if ((unsigned) i + j > n) {
861 abort_unzip(PASS_STATE_ONLY); //return 1;
862 }
863 while (j--) {
864 ll[i++] = l;
865 }
866 } else if (j == 17) { /* 3 to 10 zero length codes */
867 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
868 j = 3 + ((unsigned) b_dynamic & 7);
869 b_dynamic >>= 3;
870 k_dynamic -= 3;
871 if ((unsigned) i + j > n) {
872 abort_unzip(PASS_STATE_ONLY); //return 1;
873 }
874 while (j--) {
875 ll[i++] = 0;
876 }
877 l = 0;
878 } else { /* j == 18: 11 to 138 zero length codes */
879 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
880 j = 11 + ((unsigned) b_dynamic & 0x7f);
881 b_dynamic >>= 7;
882 k_dynamic -= 7;
883 if ((unsigned) i + j > n) {
884 abort_unzip(PASS_STATE_ONLY); //return 1;
885 }
886 while (j--) {
887 ll[i++] = 0;
888 }
889 l = 0;
890 }
891 }
892
893 /* free decoding table for trees */
894 huft_free(inflate_codes_tl);
895
896 /* restore the global bit buffer */
897 gunzip_bb = b_dynamic;
898 gunzip_bk = k_dynamic;
899
900 /* build the decoding tables for literal/length and distance codes */
901 bl = lbits;
902
903 i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
904 if (i != 0)
905 abort_unzip(PASS_STATE_ONLY);
906 bd = dbits;
907 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
908 if (i != 0)
909 abort_unzip(PASS_STATE_ONLY);
910
911 /* set up data for inflate_codes() */
912 inflate_codes_setup(PASS_STATE bl, bd);
913
914 /* huft_free code moved into inflate_codes */
915
916 return -2;
917 }
918 default:
919 abort_unzip(PASS_STATE_ONLY);
920 return 0;
921 }
922 }
923
924 /* Two callsites, both in inflate_get_next_window */
925 static void calculate_gunzip_crc(STATE_PARAM_ONLY)
926 {
927 gunzip_crc = crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table);
928 gunzip_bytes_out += gunzip_outbuf_count;
929 }
930
931 /* One callsite in inflate_unzip_internal */
932 static int inflate_get_next_window(STATE_PARAM_ONLY)
933 {
934 gunzip_outbuf_count = 0;
935
936 while (1) {
937 int ret = 0;
938
939 if (need_another_block) {
940 if (end_reached) {
941 calculate_gunzip_crc(PASS_STATE_ONLY);
942 end_reached = 0;
943 /* NB: need_another_block is still set */
944 return 0; /* Last block */
945 }
946 method = inflate_block(PASS_STATE &end_reached);
947 need_another_block = 0;
948 }
949
950 switch (method) {
951 case -1:
952 ret = inflate_stored(PASS_STATE_ONLY);
953 break;
954 case -2:
955 ret = inflate_codes(PASS_STATE_ONLY);
956 break;
957 default: /* cannot happen */
958 abort_unzip(PASS_STATE_ONLY);
959 }
960
961 if (ret == 1) {
962 calculate_gunzip_crc(PASS_STATE_ONLY);
963 return 1; /* more data left */
964 }
965 need_another_block = 1; /* end of that block */
966 }
967 /* Doesnt get here */
968 }
969
970
971 /* Called from unpack_gz_stream() and inflate_unzip() */
972 static IF_DESKTOP(long long) int
973 inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
974 {
975 IF_DESKTOP(long long) int n = 0;
976 ssize_t nwrote;
977
978 /* Allocate all global buffers (for DYN_ALLOC option) */
979 gunzip_window = xmalloc(GUNZIP_WSIZE);
980 gunzip_outbuf_count = 0;
981 gunzip_bytes_out = 0;
982 gunzip_src_fd = xstate->src_fd;
983
984 /* (re) initialize state */
985 method = -1;
986 need_another_block = 1;
987 resume_copy = 0;
988 gunzip_bk = 0;
989 gunzip_bb = 0;
990
991 /* Create the crc table */
992 gunzip_crc_table = crc32_filltable(NULL, 0);
993 gunzip_crc = ~0;
994
995 error_msg = "corrupted data";
996 if (setjmp(error_jmp)) {
997 /* Error from deep inside zip machinery */
998 n = -1;
999 goto ret;
1000 }
1001
1002 while (1) {
1003 int r = inflate_get_next_window(PASS_STATE_ONLY);
1004 nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
1005 if (nwrote != (ssize_t)gunzip_outbuf_count) {
1006 huft_free_all(PASS_STATE_ONLY);
1007 n = (nwrote <0)?nwrote:-1;
1008 goto ret;
1009 }
1010 IF_DESKTOP(n += nwrote;)
1011 if (r == 0) break;
1012 }
1013
1014 /* Store unused bytes in a global buffer so calling applets can access it */
1015 if (gunzip_bk >= 8) {
1016 /* Undo too much lookahead. The next read will be byte aligned
1017 * so we can discard unused bits in the last meaningful byte. */
1018 bytebuffer_offset--;
1019 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
1020 gunzip_bb >>= 8;
1021 gunzip_bk -= 8;
1022 }
1023 ret:
1024 /* Cleanup */
1025 free(gunzip_window);
1026 free(gunzip_crc_table);
1027 return n;
1028 }
1029
1030
1031 /* External entry points */
1032
1033 /* For unzip */
1034
1035 IF_DESKTOP(long long) int FAST_FUNC
1036 inflate_unzip(transformer_state_t *xstate)
1037 {
1038 IF_DESKTOP(long long) int n;
1039 DECLARE_STATE;
1040
1041 ALLOC_STATE;
1042 if (state == NULL)
1043 return -1;
1044
1045 to_read = xstate->bytes_in;
1046 // bytebuffer_max = 0x8000;
1047 bytebuffer_offset = 4;
1048 bytebuffer = xmalloc(bytebuffer_max);
1049 n = inflate_unzip_internal(PASS_STATE xstate);
1050 free(bytebuffer);
1051
1052 xstate->crc32 = gunzip_crc;
1053 xstate->bytes_out = gunzip_bytes_out;
1054 DEALLOC_STATE;
1055 return n;
1056 }
1057
1058
1059 /* For gunzip */
1060
1061 /* helpers first */
1062
1063 /* Top up the input buffer with at least n bytes. */
1064 static int top_up(STATE_PARAM unsigned n)
1065 {
1066 int count = bytebuffer_size - bytebuffer_offset;
1067
1068 if (count < (int)n) {
1069 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1070 bytebuffer_offset = 0;
1071 bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1072 if ((int)bytebuffer_size < 0) {
1073 bb_error_msg(bb_msg_read_error);
1074 return 0;
1075 }
1076 bytebuffer_size += count;
1077 if (bytebuffer_size < n)
1078 return 0;
1079 }
1080 return 1;
1081 }
1082
1083 static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
1084 {
1085 uint16_t res;
1086 #if BB_LITTLE_ENDIAN
1087 move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]);
1088 #else
1089 res = bytebuffer[bytebuffer_offset];
1090 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1091 #endif
1092 bytebuffer_offset += 2;
1093 return res;
1094 }
1095
1096 static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
1097 {
1098 uint32_t res;
1099 #if BB_LITTLE_ENDIAN
1100 move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]);
1101 #else
1102 res = bytebuffer[bytebuffer_offset];
1103 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1104 res |= bytebuffer[bytebuffer_offset + 2] << 16;
1105 res |= bytebuffer[bytebuffer_offset + 3] << 24;
1106 #endif
1107 bytebuffer_offset += 4;
1108 return res;
1109 }
1110
1111 static int check_header_gzip(STATE_PARAM transformer_state_t *xstate)
1112 {
1113 PRAGMA_BEGIN_PACKED
1114 union {
1115 unsigned char raw[8];
1116 struct {
1117 uint8_t gz_method;
1118 uint8_t flags;
1119 uint32_t mtime;
1120 uint8_t xtra_flags_UNUSED;
1121 uint8_t os_flags_UNUSED;
1122 } PACKED formatted;
1123 } header;
1124 PRAGMA_END_PACKED
1125 struct BUG_header {
1126 char BUG_header[sizeof(header) == 8 ? 1 : -1];
1127 };
1128
1129 /*
1130 * Rewind bytebuffer. We use the beginning because the header has 8
1131 * bytes, leaving enough for unwinding afterwards.
1132 */
1133 bytebuffer_size -= bytebuffer_offset;
1134 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1135 bytebuffer_offset = 0;
1136
1137 if (!top_up(PASS_STATE 8))
1138 return 0;
1139 memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1140 bytebuffer_offset += 8;
1141
1142 /* Check the compression method */
1143 if (header.formatted.gz_method != 8) {
1144 return 0;
1145 }
1146
1147 if (header.formatted.flags & 0x04) {
1148 /* bit 2 set: extra field present */
1149 unsigned extra_short;
1150
1151 if (!top_up(PASS_STATE 2))
1152 return 0;
1153 extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
1154 if (!top_up(PASS_STATE extra_short))
1155 return 0;
1156 /* Ignore extra field */
1157 bytebuffer_offset += extra_short;
1158 }
1159
1160 /* Discard original name and file comment if any */
1161 /* bit 3 set: original file name present */
1162 /* bit 4 set: file comment present */
1163 if (header.formatted.flags & 0x18) {
1164 while (1) {
1165 do {
1166 if (!top_up(PASS_STATE 1))
1167 return 0;
1168 } while (bytebuffer[bytebuffer_offset++] != 0);
1169 if ((header.formatted.flags & 0x18) != 0x18)
1170 break;
1171 header.formatted.flags &= ~0x18;
1172 }
1173 }
1174
1175 xstate->mtime = SWAP_LE32(header.formatted.mtime);
1176
1177 /* Read the header checksum */
1178 if (header.formatted.flags & 0x02) {
1179 if (!top_up(PASS_STATE 2))
1180 return 0;
1181 bytebuffer_offset += 2;
1182 }
1183 return 1;
1184 }
1185
1186 IF_DESKTOP(long long) int FAST_FUNC
1187 unpack_gz_stream(transformer_state_t *xstate)
1188 {
1189 uint32_t v32;
1190 IF_DESKTOP(long long) int total, n;
1191 DECLARE_STATE;
1192
1193 #if !ENABLE_FEATURE_SEAMLESS_Z
1194 if (check_signature16(xstate, GZIP_MAGIC))
1195 return -1;
1196 #else
1197 if (xstate->check_signature) {
1198 uint16_t magic2;
1199
1200 if (full_read(xstate->src_fd, &magic2, 2) != 2) {
1201 bad_magic:
1202 bb_error_msg("invalid magic");
1203 return -1;
1204 }
1205 if (magic2 == COMPRESS_MAGIC) {
1206 xstate->check_signature = 0;
1207 return unpack_Z_stream(xstate);
1208 }
1209 if (magic2 != GZIP_MAGIC)
1210 goto bad_magic;
1211 }
1212 #endif
1213
1214 total = 0;
1215
1216 ALLOC_STATE;
1217 if (state == NULL) {
1218 bb_error_msg("alloc error");
1219 return -1;
1220 }
1221 to_read = -1;
1222 // bytebuffer_max = 0x8000;
1223 bytebuffer = xmalloc(bytebuffer_max);
1224 if (bytebuffer == NULL) {
1225 bb_error_msg("alloc error");
1226 total = -1;
1227 goto ret;
1228 }
1229 gunzip_src_fd = xstate->src_fd;
1230
1231 again:
1232 if (!check_header_gzip(PASS_STATE xstate)) {
1233 bb_error_msg("corrupted data");
1234 total = -1;
1235 goto ret;
1236 }
1237
1238 n = inflate_unzip_internal(PASS_STATE xstate);
1239 if (n < 0) {
1240 total = (n == -ENOSPC)?xstate->mem_output_size_max:n;
1241 goto ret;
1242 }
1243 total += n;
1244
1245 if (!top_up(PASS_STATE 8)) {
1246 bb_error_msg("corrupted data");
1247 total = -1;
1248 goto ret;
1249 }
1250
1251 /* Validate decompression - crc */
1252 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1253 if ((~gunzip_crc) != v32) {
1254 bb_error_msg("crc error");
1255 total = -1;
1256 goto ret;
1257 }
1258
1259 /* Validate decompression - size */
1260 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1261 if ((uint32_t)gunzip_bytes_out != v32) {
1262 bb_error_msg("incorrect length");
1263 total = -1;
1264 }
1265
1266 if (!top_up(PASS_STATE 2))
1267 goto ret; /* EOF */
1268
1269 if (bytebuffer[bytebuffer_offset] == 0x1f
1270 && bytebuffer[bytebuffer_offset + 1] == 0x8b
1271 ) {
1272 bytebuffer_offset += 2;
1273 goto again;
1274 }
1275 /* GNU gzip says: */
1276 /*bb_error_msg("decompression OK, trailing garbage ignored");*/
1277
1278 ret:
1279 free(bytebuffer);
1280 DEALLOC_STATE;
1281 return total;
1282 }