blob: 8f33e6e6c066bdfab3e881eae29f0a413cfff17d [file] [log] [blame]
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001/* 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 *
Eric Andersencb81e642003-07-14 21:21:08 +000010 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support
Eric Andersendb930942001-12-06 08:20:14 +000011 * files as well as stdin/stdout, and to generally behave itself wrt
Glenn L McGrath7fd92942001-04-11 03:11:33 +000012 * command line handling.
13 *
14 * General cleanup to better adhere to the style guide and make use of standard
Glenn L McGrathc6992fe2004-04-25 05:11:19 +000015 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
Eric Andersenc7bda1c2004-03-15 08:29:22 +000016 *
Glenn L McGrath83bf47c2002-11-20 22:00:31 +000017 * read_gz interface + associated hacking by Laurence Anderson
18 *
Paul Fox0840b762005-07-20 20:26:49 +000019 * 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 *
Glenn L McGrath7fd92942001-04-11 03:11:33 +000024 * 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 *
Glenn L McGrath7fd92942001-04-11 03:11:33 +000031 * See the file algorithm.doc for the compression algorithms and file formats.
Rob Landley11c7a7b2006-06-25 22:39:24 +000032 *
33 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath7fd92942001-04-11 03:11:33 +000034 */
35
Rob Landley11c7a7b2006-06-25 22:39:24 +000036#include "libbb.h"
Glenn L McGrath7fd92942001-04-11 03:11:33 +000037#include <sys/wait.h>
38#include <signal.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039#include "unarchive.h"
Glenn L McGrath7fd92942001-04-11 03:11:33 +000040
41typedef struct huft_s {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000042 unsigned char e; /* number of extra bits or operation */
43 unsigned char b; /* number of bits in this code or subcode */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000044 union {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000045 unsigned short n; /* literal, length base, or distance base */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000046 struct huft_s *t; /* pointer to next level of table */
47 } v;
48} huft_t;
49
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000050static int gunzip_src_fd;
Glenn L McGrath826b48b2003-02-09 12:00:17 +000051unsigned int gunzip_bytes_out; /* number of output bytes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000052static unsigned int gunzip_outbuf_count; /* bytes in output buffer */
53
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000054/* gunzip_window size--must be a power of two, and
55 * at least 32K for zip's deflate method */
Rob Landleybc68cd12006-03-10 19:22:06 +000056enum { gunzip_wsize = 0x8000 };
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057static unsigned char *gunzip_window;
Glenn L McGrathcc616922003-02-09 04:46:34 +000058
Rob Landleyc57ec372006-04-10 17:07:15 +000059static uint32_t *gunzip_crc_table;
60uint32_t gunzip_crc;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000061
62/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
63#define BMAX 16 /* maximum bit length of any code (16 for explode) */
64#define N_MAX 288 /* maximum number of codes in any set */
65
Glenn L McGrathcc616922003-02-09 04:46:34 +000066/* bitbuffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000067static unsigned int gunzip_bb; /* bit buffer */
68static unsigned char gunzip_bk; /* bits in bit buffer */
69
Glenn L McGrathcc616922003-02-09 04:46:34 +000070/* These control the size of the bytebuffer */
Glenn L McGrath5699b852003-11-15 23:19:05 +000071static unsigned int bytebuffer_max = 0x8000;
Glenn L McGrathcc616922003-02-09 04:46:34 +000072static unsigned char *bytebuffer = NULL;
73static unsigned int bytebuffer_offset = 0;
74static unsigned int bytebuffer_size = 0;
75
Eric Andersen044228d2001-07-17 01:12:36 +000076static const unsigned short mask_bits[] = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000077 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
Glenn L McGrath7fd92942001-04-11 03:11:33 +000078 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
79};
80
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081/* Copy lengths for literal codes 257..285 */
82static const unsigned short cplens[] = {
83 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
Paul Fox0840b762005-07-20 20:26:49 +000084 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085};
86
87/* note: see note #13 above about the 258 in this list. */
88/* Extra bits for literal codes 257..285 */
89static const unsigned char cplext[] = {
90 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,
Paul Fox0840b762005-07-20 20:26:49 +000091 5, 5, 5, 0, 99, 99
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000092}; /* 99==invalid */
93
94/* Copy offsets for distance codes 0..29 */
95static const unsigned short cpdist[] = {
96 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
Paul Fox0840b762005-07-20 20:26:49 +000097 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000098};
99
100/* Extra bits for distance codes */
101static const unsigned char cpdext[] = {
102 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
Paul Fox0840b762005-07-20 20:26:49 +0000103 11, 11, 12, 12, 13, 13
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104};
105
106/* Tables for deflate from PKZIP's appnote.txt. */
107/* Order of the bit length code lengths */
108static const unsigned char border[] = {
109 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
110};
111
112static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
113{
114 while (*current < required) {
Glenn L McGrath5699b852003-11-15 23:19:05 +0000115 if (bytebuffer_offset >= bytebuffer_size) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
Glenn L McGrath5699b852003-11-15 23:19:05 +0000117 * to the front of the bytebuffer, leave 4 bytes free at end of tail
118 * so we can easily top up buffer in check_trailer_gzip() */
Rob Landley53437472006-07-16 08:14:35 +0000119 if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8)))
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000120 bb_error_msg_and_die("unexpected end of file");
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000121 bytebuffer_size += 4;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000122 bytebuffer_offset = 4;
123 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000124 bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
125 bytebuffer_offset++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000126 *current += 8;
127 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000128 return(bitbuffer);
129}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000130
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000131/*
132 * Free the malloc'ed tables built by huft_build(), which makes a linked
133 * list of the tables it made, with the links in a dummy first entry of
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000134 * each table.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000135 * t: table to free
136 */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000137static int huft_free(huft_t * t)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000138{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000139 huft_t *p;
140 huft_t *q;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000141
142 /* Go through linked list, freeing from the malloced (t[-1]) address. */
143 p = t;
144 while (p != (huft_t *) NULL) {
145 q = (--p)->v.t;
146 free((char *) p);
147 p = q;
148 }
149 return 0;
150}
151
152/* Given a list of code lengths and a maximum table size, make a set of
153 * tables to decode that set of codes. Return zero on success, one if
154 * the given code set is incomplete (the tables are still built in this
155 * case), two if the input is invalid (all zero length codes or an
156 * oversubscribed set of lengths), and three if not enough memory.
157 *
158 * b: code lengths in bits (all assumed <= BMAX)
159 * n: number of codes (assumed <= N_MAX)
160 * s: number of simple-valued codes (0..s-1)
161 * d: list of base values for non-simple codes
162 * e: list of extra bits for non-simple codes
163 * t: result: starting table
164 * m: maximum lookup bits, returns actual
165 */
Bernhard Reutner-Fischer7ab5f4d2006-04-02 21:23:40 +0000166static
Paul Fox0840b762005-07-20 20:26:49 +0000167int huft_build(unsigned int *b, const unsigned int n,
168 const unsigned int s, const unsigned short *d,
Eric Andersenf55289f2006-01-30 17:27:00 +0000169 const unsigned char *e, huft_t ** t, unsigned int *m)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000170{
Paul Fox0840b762005-07-20 20:26:49 +0000171 unsigned a; /* counter for codes of length k */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000172 unsigned c[BMAX + 1]; /* bit length count table */
Paul Fox0840b762005-07-20 20:26:49 +0000173 unsigned eob_len; /* length of end-of-block code (value 256) */
174 unsigned f; /* i repeats in table every f entries */
175 int g; /* maximum code length */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000176 int htl; /* table level */
177 unsigned i; /* counter, current code */
178 unsigned j; /* counter */
179 int k; /* number of bits in current code */
180 unsigned *p; /* pointer into c[], b[], or v[] */
181 huft_t *q; /* points to current table */
Paul Fox0840b762005-07-20 20:26:49 +0000182 huft_t r; /* table entry for structure assignment */
183 huft_t *u[BMAX]; /* table stack */
184 unsigned v[N_MAX]; /* values in order of bit length */
185 int ws[BMAX+1]; /* bits decoded stack */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000186 int w; /* bits decoded */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000187 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
Paul Fox0840b762005-07-20 20:26:49 +0000188 unsigned *xp; /* pointer into x */
189 int y; /* number of dummy codes added */
190 unsigned z; /* number of entries in current table */
191
192 /* Length of EOB code, if any */
193 eob_len = n > 256 ? b[256] : BMAX;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000194
195 /* Generate counts for each bit length */
Paul Fox0840b762005-07-20 20:26:49 +0000196 memset((void *)c, 0, sizeof(c));
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000197 p = b;
198 i = n;
199 do {
Paul Fox0840b762005-07-20 20:26:49 +0000200 c[*p]++; /* assume all entries <= BMAX */
201 p++; /* Can't combine with above line (Solaris bug) */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000202 } while (--i);
Paul Fox0840b762005-07-20 20:26:49 +0000203 if (c[0] == n) { /* null input--all zero length codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000204 *t = (huft_t *) NULL;
205 *m = 0;
Rob Landleyeb00afb2006-02-20 02:18:03 +0000206 return 2;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000207 }
208
209 /* Find minimum and maximum length, bound *m by those */
Paul Fox0840b762005-07-20 20:26:49 +0000210 for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
211 k = j; /* minimum code length */
212 for (i = BMAX; (c[i] == 0) && i; i--);
213 g = i; /* maximum code length */
214 *m = (*m < j) ? j : ((*m > i) ? i : *m);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000215
216 /* Adjust last length count to fill out codes, if needed */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000217 for (y = 1 << j; j < i; j++, y <<= 1) {
218 if ((y -= c[j]) < 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000219 return 2; /* bad input: more codes than bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000220 }
221 }
222 if ((y -= c[i]) < 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000223 return 2;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000224 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000225 c[i] += y;
226
227 /* Generate starting offsets into the value table for each length */
228 x[1] = j = 0;
229 p = c + 1;
230 xp = x + 2;
Paul Fox0840b762005-07-20 20:26:49 +0000231 while (--i) { /* note that i == g from above */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000232 *xp++ = (j += *p++);
233 }
234
235 /* Make a table of values in order of bit lengths */
236 p = b;
237 i = 0;
238 do {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000239 if ((j = *p++) != 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000240 v[x[j]++] = i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000241 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000242 } while (++i < n);
243
244 /* Generate the Huffman codes and for each, make the table entries */
Paul Fox0840b762005-07-20 20:26:49 +0000245 x[0] = i = 0; /* first Huffman code is zero */
246 p = v; /* grab values in bit order */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000247 htl = -1; /* no tables yet--level -1 */
Paul Fox0840b762005-07-20 20:26:49 +0000248 w = ws[0] = 0; /* bits decoded */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000249 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
250 q = (huft_t *) NULL; /* ditto */
Paul Fox0840b762005-07-20 20:26:49 +0000251 z = 0; /* ditto */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000252
253 /* go through the bit lengths (k already is bits in shortest code) */
254 for (; k <= g; k++) {
255 a = c[k];
256 while (a--) {
257 /* here i is the Huffman code of length k bits for value *p */
258 /* make tables up to required level */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000259 while (k > ws[htl + 1]) {
260 w = ws[++htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000261
Paul Fox0840b762005-07-20 20:26:49 +0000262 /* compute minimum size table less than or equal to *m bits */
263 z = (z = g - w) > *m ? *m : z; /* upper limit on table size */
264 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */
265 /* too few codes for k-w bit table */
266 f -= a + 1; /* deduct codes from patterns left */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000267 xp = c + k;
Paul Fox0840b762005-07-20 20:26:49 +0000268 while (++j < z) { /* try smaller tables up to z bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000269 if ((f <<= 1) <= *++xp) {
Paul Fox0840b762005-07-20 20:26:49 +0000270 break; /* enough codes to use up j bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000271 }
Paul Fox0840b762005-07-20 20:26:49 +0000272 f -= *xp; /* else deduct codes from patterns */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000273 }
274 }
Paul Fox0840b762005-07-20 20:26:49 +0000275 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000276 z = 1 << j; /* table entries for j-bit table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000277 ws[htl+1] = w + j; /* set bits decoded in stack */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000278
279 /* allocate and link in new table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000280 q = (huft_t *) xzalloc((z + 1) * sizeof(huft_t));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000281 *t = q + 1; /* link to list for huft_free() */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000282 t = &(q->v.t);
283 u[htl] = ++q; /* table starts after link */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000284
285 /* connect to last table, if there is one */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000286 if (htl) {
287 x[htl] = i; /* save pattern for backing up */
288 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
Paul Fox0840b762005-07-20 20:26:49 +0000289 r.e = (unsigned char) (16 + j); /* bits in this table */
290 r.v.t = q; /* pointer to this table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000291 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
292 u[htl - 1][j] = r; /* connect to last table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000293 }
294 }
295
296 /* set up table entry in r */
297 r.b = (unsigned char) (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000298 if (p >= v + n) {
Paul Fox0840b762005-07-20 20:26:49 +0000299 r.e = 99; /* out of values--invalid code */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000300 } else if (*p < s) {
Paul Fox0840b762005-07-20 20:26:49 +0000301 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
302 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000303 } else {
Paul Fox0840b762005-07-20 20:26:49 +0000304 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000305 r.v.n = d[*p++ - s];
306 }
307
308 /* fill code-like entries with r */
309 f = 1 << (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000310 for (j = i >> w; j < z; j += f) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000311 q[j] = r;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000312 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000313
314 /* backwards increment the k-bit code i */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000315 for (j = 1 << (k - 1); i & j; j >>= 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000316 i ^= j;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000317 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000318 i ^= j;
319
320 /* backup over finished tables */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000321 while ((i & ((1 << w) - 1)) != x[htl]) {
322 w = ws[--htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000323 }
324 }
325 }
Paul Fox0840b762005-07-20 20:26:49 +0000326
327 /* return actual size of base table */
328 *m = ws[1];
329
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000330 /* Return true (1) if we were given an incomplete table */
331 return y != 0 && g != 1;
332}
333
334/*
335 * inflate (decompress) the codes in a deflated (compressed) block.
336 * Return an error code or zero if it all goes ok.
337 *
338 * tl, td: literal/length and distance decoder tables
339 * bl, bd: number of bits decoded by tl[] and td[]
340 */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000341static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000342{
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000343 static unsigned int e; /* table entry flag/number of extra bits */
344 static unsigned int n, d; /* length and index for copy */
345 static unsigned int w; /* current gunzip_window position */
346 static huft_t *t; /* pointer to table entry */
347 static unsigned int ml, md; /* masks for bl and bd bits */
348 static unsigned int b; /* bit buffer */
349 static unsigned int k; /* number of bits in bit buffer */
350 static huft_t *tl, *td;
351 static unsigned int bl, bd;
352 static int resumeCopy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000353
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000354 if (setup) { // 1st time we are called, copy in variables
355 tl = my_tl;
356 td = my_td;
357 bl = my_bl;
358 bd = my_bd;
359 /* make local copies of globals */
360 b = gunzip_bb; /* initialize bit buffer */
361 k = gunzip_bk;
362 w = gunzip_outbuf_count; /* initialize gunzip_window position */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000363
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000364 /* inflate the coded data */
365 ml = mask_bits[bl]; /* precompute masks for speed */
366 md = mask_bits[bd];
367 return 0; // Don't actually do anything the first time
368 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000369
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000370 if (resumeCopy) goto do_copy;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000371
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000372 while (1) { /* do until end of block */
373 b = fill_bitbuffer(b, &k, bl);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000374 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000375 do {
376 if (e == 99) {
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000377 bb_error_msg_and_die("inflate_codes error 1");
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000378 }
379 b >>= t->b;
380 k -= t->b;
381 e -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000382 b = fill_bitbuffer(b, &k, e);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000383 } while ((e =
384 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000385 b >>= t->b;
386 k -= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000387 if (e == 16) { /* then it's a literal */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000388 gunzip_window[w++] = (unsigned char) t->v.n;
389 if (w == gunzip_wsize) {
390 gunzip_outbuf_count = (w);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000391 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000392 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000393 return 1; // We have a block to read
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000394 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000395 } else { /* it's an EOB or a length */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000396
397 /* exit if end of block */
398 if (e == 15) {
399 break;
400 }
401
402 /* get length of block to copy */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000403 b = fill_bitbuffer(b, &k, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000404 n = t->v.n + ((unsigned) b & mask_bits[e]);
405 b >>= e;
406 k -= e;
407
408 /* decode distance of block to copy */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000409 b = fill_bitbuffer(b, &k, bd);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000410 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
411 do {
412 if (e == 99)
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000413 bb_error_msg_and_die("inflate_codes error 2");
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000414 b >>= t->b;
415 k -= t->b;
416 e -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000417 b = fill_bitbuffer(b, &k, e);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000418 } while ((e =
419 (t =
420 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000421 b >>= t->b;
422 k -= t->b;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000423 b = fill_bitbuffer(b, &k, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000424 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
425 b >>= e;
426 k -= e;
427
428 /* do the copy */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000429do_copy: do {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000430 n -= (e =
431 (e =
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000432 gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
433 /* copy to new buffer to prevent possible overwrite */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000434 if (w - d >= e) { /* (this test assumes unsigned comparison) */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000435 memcpy(gunzip_window + w, gunzip_window + d, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000436 w += e;
437 d += e;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000438 } else {
439 /* do it slow to avoid memcpy() overlap */
440 /* !NOMEMCPY */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000441 do {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000442 gunzip_window[w++] = gunzip_window[d++];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000443 } while (--e);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000444 }
445 if (w == gunzip_wsize) {
446 gunzip_outbuf_count = (w);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000447 if (n) resumeCopy = 1;
448 else resumeCopy = 0;
449 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000450 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000451 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000452 }
453 } while (n);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000454 resumeCopy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000455 }
456 }
457
458 /* restore the globals from the locals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000459 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
460 gunzip_bb = b; /* restore global bit buffer */
461 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000462
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000463 /* normally just after call to inflate_codes, but save code by putting it here */
464 /* free the decoding tables, return */
465 huft_free(tl);
466 huft_free(td);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000467
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000468 /* done */
469 return 0;
470}
471
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000472static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000473{
Eric Andersenf55289f2006-01-30 17:27:00 +0000474 static unsigned int n, b_stored, k_stored, w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000475 if (setup) {
476 n = my_n;
477 b_stored = my_b_stored;
478 k_stored = my_k_stored;
479 w = gunzip_outbuf_count; /* initialize gunzip_window position */
480 return 0; // Don't do anything first time
481 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000482
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000483 /* read and output the compressed data */
484 while (n--) {
485 b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
486 gunzip_window[w++] = (unsigned char) b_stored;
Eric Andersenf55289f2006-01-30 17:27:00 +0000487 if (w == gunzip_wsize) {
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000488 gunzip_outbuf_count = (w);
489 //flush_gunzip_window();
490 w = 0;
491 b_stored >>= 8;
492 k_stored -= 8;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000493 return 1; // We have a block
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000494 }
495 b_stored >>= 8;
496 k_stored -= 8;
497 }
498
499 /* restore the globals from the locals */
500 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
501 gunzip_bb = b_stored; /* restore global bit buffer */
502 gunzip_bk = k_stored;
503 return 0; // Finished
504}
505
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000506/*
507 * decompress an inflated block
508 * e: last block flag
509 *
510 * GLOBAL VARIABLES: bb, kk,
511 */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000512 // Return values: -1 = inflate_stored, -2 = inflate_codes
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000513static int inflate_block(int *e)
514{
515 unsigned t; /* block type */
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000516 unsigned int b; /* bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000517 unsigned int k; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000518
519 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000520
521 b = gunzip_bb;
522 k = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000523
524 /* read in last block bit */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000525 b = fill_bitbuffer(b, &k, 1);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000526 *e = (int) b & 1;
527 b >>= 1;
528 k -= 1;
529
530 /* read in block type */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000531 b = fill_bitbuffer(b, &k, 2);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000532 t = (unsigned) b & 3;
533 b >>= 2;
534 k -= 2;
535
536 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000537 gunzip_bb = b;
538 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000539
540 /* inflate that block type */
541 switch (t) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000542 case 0: /* Inflate stored */
543 {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000544 unsigned int n; /* number of bytes in block */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000545 unsigned int b_stored; /* bit buffer */
546 unsigned int k_stored; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000547
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000548 /* make local copies of globals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000549 b_stored = gunzip_bb; /* initialize bit buffer */
550 k_stored = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000551
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000552 /* go to byte boundary */
553 n = k_stored & 7;
554 b_stored >>= n;
555 k_stored -= n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000556
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000557 /* get the length and its complement */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000558 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000559 n = ((unsigned) b_stored & 0xffff);
560 b_stored >>= 16;
561 k_stored -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000562
563 b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000564 if (n != (unsigned) ((~b_stored) & 0xffff)) {
565 return 1; /* error in compressed data */
566 }
567 b_stored >>= 16;
568 k_stored -= 16;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000569
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000570 inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
571 return -1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000572 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000573 case 1: /* Inflate fixed
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000574 * decompress an inflated type 1 (fixed Huffman codes) block. We should
575 * either replace this with a custom decoder, or at least precompute the
576 * Huffman tables.
577 */
578 {
579 int i; /* temporary variable */
580 huft_t *tl; /* literal/length code table */
581 huft_t *td; /* distance code table */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000582 unsigned int bl; /* lookup bits for tl */
583 unsigned int bd; /* lookup bits for td */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000584 unsigned int l[288]; /* length list for huft_build */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000585
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000586 /* set up literal table */
587 for (i = 0; i < 144; i++) {
588 l[i] = 8;
589 }
590 for (; i < 256; i++) {
591 l[i] = 9;
592 }
593 for (; i < 280; i++) {
594 l[i] = 7;
595 }
596 for (; i < 288; i++) { /* make a complete, but wrong code set */
597 l[i] = 8;
598 }
599 bl = 7;
600 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
601 return i;
602 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000603
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000604 /* set up distance table */
605 for (i = 0; i < 30; i++) { /* make an incomplete code set */
606 l[i] = 5;
607 }
608 bd = 5;
609 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000610 huft_free(tl);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000611 return i;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000612 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000613
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000614 /* decompress until an end-of-block code */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000615 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000616
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000617 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000618
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000619 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000620 }
621 case 2: /* Inflate dynamic */
622 {
623 const int dbits = 6; /* bits in base distance lookup table */
624 const int lbits = 9; /* bits in base literal/length lookup table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000625
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000626 huft_t *tl; /* literal/length code table */
627 huft_t *td; /* distance code table */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000628 unsigned int i; /* temporary variables */
629 unsigned int j;
630 unsigned int l; /* last length */
631 unsigned int m; /* mask for bit lengths table */
632 unsigned int n; /* number of lengths to get */
633 unsigned int bl; /* lookup bits for tl */
634 unsigned int bd; /* lookup bits for td */
635 unsigned int nb; /* number of bit length codes */
636 unsigned int nl; /* number of literal/length codes */
637 unsigned int nd; /* number of distance codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000638
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000639 unsigned int ll[286 + 30]; /* literal/length and distance code lengths */
640 unsigned int b_dynamic; /* bit buffer */
641 unsigned int k_dynamic; /* number of bits in bit buffer */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000642
643 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000644 b_dynamic = gunzip_bb;
645 k_dynamic = gunzip_bk;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000646
647 /* read in table lengths */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000648 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
649 nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */
650
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000651 b_dynamic >>= 5;
652 k_dynamic -= 5;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000653 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
654 nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
655
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000656 b_dynamic >>= 5;
657 k_dynamic -= 5;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000658 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
659 nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */
660
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000661 b_dynamic >>= 4;
662 k_dynamic -= 4;
663 if (nl > 286 || nd > 30) {
664 return 1; /* bad lengths */
665 }
666
667 /* read in bit-length-code lengths */
668 for (j = 0; j < nb; j++) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000669 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
670 ll[border[j]] = (unsigned int) b_dynamic & 7;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000671 b_dynamic >>= 3;
672 k_dynamic -= 3;
673 }
674 for (; j < 19; j++) {
675 ll[border[j]] = 0;
676 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000677
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000678 /* build decoding table for trees--single level, 7 bit lookup */
679 bl = 7;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000680 i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
681 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000682 if (i == 1) {
683 huft_free(tl);
684 }
685 return i; /* incomplete code set */
686 }
687
688 /* read in literal and distance code lengths */
689 n = nl + nd;
690 m = mask_bits[bl];
691 i = l = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000692 while ((unsigned int) i < n) {
693 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
694 j = (td = tl + ((unsigned int) b_dynamic & m))->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000695 b_dynamic >>= j;
696 k_dynamic -= j;
697 j = td->v.n;
698 if (j < 16) { /* length of code in bits (0..15) */
699 ll[i++] = l = j; /* save last length in l */
700 } else if (j == 16) { /* repeat last length 3 to 6 times */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000701 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
702 j = 3 + ((unsigned int) b_dynamic & 3);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000703 b_dynamic >>= 2;
704 k_dynamic -= 2;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000705 if ((unsigned int) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000706 return 1;
707 }
708 while (j--) {
709 ll[i++] = l;
710 }
711 } else if (j == 17) { /* 3 to 10 zero length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000712 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
713 j = 3 + ((unsigned int) b_dynamic & 7);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000714 b_dynamic >>= 3;
715 k_dynamic -= 3;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000716 if ((unsigned int) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000717 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000718 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000719 while (j--) {
720 ll[i++] = 0;
721 }
722 l = 0;
723 } else { /* j == 18: 11 to 138 zero length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000724 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
725 j = 11 + ((unsigned int) b_dynamic & 0x7f);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000726 b_dynamic >>= 7;
727 k_dynamic -= 7;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000728 if ((unsigned int) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000729 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000730 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000731 while (j--) {
732 ll[i++] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000733 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000734 l = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000735 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000736 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000737
738 /* free decoding table for trees */
739 huft_free(tl);
740
741 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000742 gunzip_bb = b_dynamic;
743 gunzip_bk = k_dynamic;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000744
745 /* build the decoding tables for literal/length and distance codes */
746 bl = lbits;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000747
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000748 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
749 if (i == 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750 bb_error_msg_and_die("Incomplete literal tree");
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000751 huft_free(tl);
752 }
753 return i; /* incomplete code set */
754 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000755
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000756 bd = dbits;
757 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
758 if (i == 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000759 bb_error_msg_and_die("incomplete distance tree");
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000760 huft_free(td);
761 }
762 huft_free(tl);
763 return i; /* incomplete code set */
764 }
765
766 /* decompress until an end-of-block code */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000767 inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000768
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000769 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000770
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000771 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000772 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000773 default:
774 /* bad block type */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000775 bb_error_msg_and_die("bad block type %d\n", t);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000776 }
777}
778
779static void calculate_gunzip_crc(void)
780{
781 int n;
782 for (n = 0; n < gunzip_outbuf_count; n++) {
783 gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
784 }
785 gunzip_bytes_out += gunzip_outbuf_count;
786}
787
788static int inflate_get_next_window(void)
789{
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000790 static int method = -1; // Method == -1 for stored, -2 for codes
791 static int e = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000792 static int needAnotherBlock = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000793
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000794 gunzip_outbuf_count = 0;
795
796 while(1) {
797 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000798
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000799 if (needAnotherBlock) {
Glenn L McGratheda4f532002-11-24 06:01:20 +0000800 if(e) {
801 calculate_gunzip_crc();
Glenn L McGrath5699b852003-11-15 23:19:05 +0000802 e = 0;
803 needAnotherBlock = 1;
Glenn L McGratheda4f532002-11-24 06:01:20 +0000804 return 0;
805 } // Last block
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000806 method = inflate_block(&e);
807 needAnotherBlock = 0;
808 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000809
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000810 switch (method) {
811 case -1: ret = inflate_stored(0,0,0,0);
812 break;
813 case -2: ret = inflate_codes(0,0,0,0,0);
814 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000815 default: bb_error_msg_and_die("inflate error %d", method);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000816 }
817
818 if (ret == 1) {
819 calculate_gunzip_crc();
820 return 1; // More data left
821 } else needAnotherBlock = 1; // End of that block
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000822 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000823 /* Doesnt get here */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000824}
825
Eric Andersenaff114c2004-04-14 17:51:38 +0000826/* Initialise bytebuffer, be careful not to overfill the buffer */
Rob Landleydfba7412006-03-06 20:47:33 +0000827void inflate_init(unsigned int bufsize)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000828{
Glenn L McGrath5699b852003-11-15 23:19:05 +0000829 /* Set the bytebuffer size, default is same as gunzip_wsize */
830 bytebuffer_max = bufsize + 8;
831 bytebuffer_offset = 4;
832 bytebuffer_size = 0;
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000833}
834
Rob Landleydfba7412006-03-06 20:47:33 +0000835void inflate_cleanup(void)
Paul Fox0840b762005-07-20 20:26:49 +0000836{
837 free(bytebuffer);
838}
839
Rob Landleydfba7412006-03-06 20:47:33 +0000840int inflate_unzip(int in, int out)
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000841{
Glenn L McGrath5699b852003-11-15 23:19:05 +0000842 ssize_t nwrote;
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000843 typedef void (*sig_type) (int);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000844
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000845 /* Allocate all global buffers (for DYN_ALLOC option) */
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000846 gunzip_window = xmalloc(gunzip_wsize);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000847 gunzip_outbuf_count = 0;
848 gunzip_bytes_out = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000849 gunzip_src_fd = in;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000850
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000851 /* initialize gunzip_window, bit buffer */
852 gunzip_bk = 0;
853 gunzip_bb = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000854
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000855 /* Create the crc table */
Rob Landleyc57ec372006-04-10 17:07:15 +0000856 gunzip_crc_table = bb_crc32_filltable(0);
857 gunzip_crc = ~0;
858
Glenn L McGrath5699b852003-11-15 23:19:05 +0000859 /* Allocate space for buffer */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000860 bytebuffer = xmalloc(bytebuffer_max);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000861
862 while(1) {
863 int ret = inflate_get_next_window();
Rob Landley53437472006-07-16 08:14:35 +0000864 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000865 if (nwrote == -1) {
866 bb_perror_msg("write");
867 return -1;
868 }
869 if (ret == 0) break;
870 }
871
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000872 /* Cleanup */
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000873 free(gunzip_window);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000874 free(gunzip_crc_table);
875
876 /* Store unused bytes in a global buffer so calling applets can access it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000877 if (gunzip_bk >= 8) {
878 /* Undo too much lookahead. The next read will be byte aligned
879 * so we can discard unused bits in the last meaningful byte. */
Glenn L McGratheda4f532002-11-24 06:01:20 +0000880 bytebuffer_offset--;
881 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000882 gunzip_bb >>= 8;
883 gunzip_bk -= 8;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000884 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000885 return 0;
886}
Glenn L McGrathcc616922003-02-09 04:46:34 +0000887
Rob Landleydfba7412006-03-06 20:47:33 +0000888int inflate_gunzip(int in, int out)
Glenn L McGrathcc616922003-02-09 04:46:34 +0000889{
Rob Landleyc57ec372006-04-10 17:07:15 +0000890 uint32_t stored_crc = 0;
Rob Landley29ee76c2005-08-31 22:03:15 +0000891 unsigned int count;
Glenn L McGrathcc616922003-02-09 04:46:34 +0000892
Glenn L McGrath5699b852003-11-15 23:19:05 +0000893 inflate_unzip(in, out);
894
Glenn L McGrathcc616922003-02-09 04:46:34 +0000895 /* top up the input buffer with the rest of the trailer */
896 count = bytebuffer_size - bytebuffer_offset;
897 if (count < 8) {
Rob Landley53437472006-07-16 08:14:35 +0000898 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
Glenn L McGrathcc616922003-02-09 04:46:34 +0000899 bytebuffer_size += 8 - count;
900 }
901 for (count = 0; count != 4; count++) {
902 stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
903 bytebuffer_offset++;
904 }
905
906 /* Validate decompression - crc */
Rob Landleyc57ec372006-04-10 17:07:15 +0000907 if (stored_crc != (~gunzip_crc)) {
Glenn L McGrath5699b852003-11-15 23:19:05 +0000908 bb_error_msg("crc error");
Rob Landley9e4100b2006-01-10 06:19:56 +0000909 return -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +0000910 }
911
912 /* Validate decompression - size */
913 if (gunzip_bytes_out !=
914 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
915 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
Glenn L McGrath5699b852003-11-15 23:19:05 +0000916 bb_error_msg("Incorrect length");
Rob Landley9e4100b2006-01-10 06:19:56 +0000917 return -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +0000918 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000919
Glenn L McGrath5699b852003-11-15 23:19:05 +0000920 return 0;
Glenn L McGrathcc616922003-02-09 04:46:34 +0000921}