diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-05 15:26:08 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-05 15:26:08 +0000 |
commit | 5dd8a0366578746bb991b0e87a60d8d442bd8d87 (patch) | |
tree | dbfa7391dd67f9c8e85f574a33ef5591b8303cb5 /archival/libunarchive | |
parent | 368a12efc24d9e9c758ee99239f79e5f443e6c0e (diff) | |
download | busybox-w32-5dd8a0366578746bb991b0e87a60d8d442bd8d87.tar.gz busybox-w32-5dd8a0366578746bb991b0e87a60d8d442bd8d87.tar.bz2 busybox-w32-5dd8a0366578746bb991b0e87a60d8d442bd8d87.zip |
gunzip: support concatenated gz files.
text data bss dec hex filename
770988 1029 9552 781569 bed01 busybox.t0/busybox
771105 1029 9552 781686 bed76 busybox.t3/busybox
Diffstat (limited to 'archival/libunarchive')
-rw-r--r-- | archival/libunarchive/Kbuild | 3 | ||||
-rw-r--r-- | archival/libunarchive/decompress_unzip.c | 605 | ||||
-rw-r--r-- | archival/libunarchive/get_header_tar_gz.c | 2 |
3 files changed, 367 insertions, 243 deletions
diff --git a/archival/libunarchive/Kbuild b/archival/libunarchive/Kbuild index d104524e4..a58a84f4b 100644 --- a/archival/libunarchive/Kbuild +++ b/archival/libunarchive/Kbuild | |||
@@ -28,7 +28,8 @@ lib-y:= \ | |||
28 | find_list_entry.o \ | 28 | find_list_entry.o \ |
29 | init_handle.o | 29 | init_handle.o |
30 | 30 | ||
31 | GUNZIP_FILES:= check_header_gzip.o decompress_unzip.o | 31 | GUNZIP_FILES:= decompress_unzip.o |
32 | |||
32 | DPKG_FILES:= \ | 33 | DPKG_FILES:= \ |
33 | get_header_ar.o \ | 34 | get_header_ar.o \ |
34 | unpack_ar_archive.o \ | 35 | unpack_ar_archive.o \ |
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 275f80b33..52be6b25d 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c | |||
@@ -33,21 +33,22 @@ | |||
33 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 33 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include <setjmp.h> | ||
36 | #include "libbb.h" | 37 | #include "libbb.h" |
37 | #include "unarchive.h" | 38 | #include "unarchive.h" |
38 | 39 | ||
39 | typedef struct huft_s { | 40 | typedef struct huft_t { |
40 | unsigned char e; /* number of extra bits or operation */ | 41 | unsigned char e; /* number of extra bits or operation */ |
41 | unsigned char b; /* number of bits in this code or subcode */ | 42 | unsigned char b; /* number of bits in this code or subcode */ |
42 | union { | 43 | union { |
43 | unsigned short n; /* literal, length base, or distance base */ | 44 | unsigned short n; /* literal, length base, or distance base */ |
44 | struct huft_s *t; /* pointer to next level of table */ | 45 | struct huft_t *t; /* pointer to next level of table */ |
45 | } v; | 46 | } v; |
46 | } huft_t; | 47 | } huft_t; |
47 | 48 | ||
48 | enum { | 49 | enum { |
49 | /* gunzip_window size--must be a power of two, and | 50 | /* gunzip_window size--must be a power of two, and |
50 | * at least 32K for zip's deflate method */ | 51 | * at least 32K for zip's deflate method */ |
51 | GUNZIP_WSIZE = 0x8000, | 52 | GUNZIP_WSIZE = 0x8000, |
52 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ | 53 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ |
53 | BMAX = 16, /* maximum bit length of any code (16 for explode) */ | 54 | BMAX = 16, /* maximum bit length of any code (16 for explode) */ |
@@ -82,11 +83,11 @@ typedef struct state_t { | |||
82 | unsigned gunzip_bb; /* bit buffer */ | 83 | unsigned gunzip_bb; /* bit buffer */ |
83 | unsigned char gunzip_bk; /* bits in bit buffer */ | 84 | unsigned char gunzip_bk; /* bits in bit buffer */ |
84 | 85 | ||
85 | /* These control the size of the STATE()bytebuffer */ | 86 | /* input (compressed) data */ |
86 | unsigned bytebuffer_max; | 87 | unsigned char *bytebuffer; /* buffer itself */ |
87 | unsigned char *bytebuffer; | 88 | unsigned bytebuffer_max; /* buffer size */ |
88 | unsigned bytebuffer_offset; | 89 | unsigned bytebuffer_offset; /* buffer position */ |
89 | unsigned bytebuffer_size; | 90 | unsigned bytebuffer_size; /* how much data is there (size <= max) */ |
90 | 91 | ||
91 | /* private data of inflate_codes() */ | 92 | /* private data of inflate_codes() */ |
92 | unsigned inflate_codes_ml; /* masks for bl and bd bits */ | 93 | unsigned inflate_codes_ml; /* masks for bl and bd bits */ |
@@ -100,10 +101,11 @@ typedef struct state_t { | |||
100 | unsigned inflate_codes_bd; | 101 | unsigned inflate_codes_bd; |
101 | unsigned inflate_codes_nn; /* length and index for copy */ | 102 | unsigned inflate_codes_nn; /* length and index for copy */ |
102 | unsigned inflate_codes_dd; | 103 | unsigned inflate_codes_dd; |
104 | |||
103 | smallint resume_copy; | 105 | smallint resume_copy; |
104 | 106 | ||
105 | /* private data of inflate_get_next_window() */ | 107 | /* private data of inflate_get_next_window() */ |
106 | smallint method; /* Method == -1 for stored, -2 for codes */ | 108 | smallint method; /* method == -1 for stored, -2 for codes */ |
107 | smallint need_another_block; | 109 | smallint need_another_block; |
108 | smallint end_reached; | 110 | smallint end_reached; |
109 | 111 | ||
@@ -112,6 +114,9 @@ typedef struct state_t { | |||
112 | unsigned inflate_stored_b; | 114 | unsigned inflate_stored_b; |
113 | unsigned inflate_stored_k; | 115 | unsigned inflate_stored_k; |
114 | unsigned inflate_stored_w; | 116 | unsigned inflate_stored_w; |
117 | |||
118 | const char *error_msg; | ||
119 | jmp_buf error_jmp; | ||
115 | } state_t; | 120 | } state_t; |
116 | #define gunzip_bytes_out (S()gunzip_bytes_out ) | 121 | #define gunzip_bytes_out (S()gunzip_bytes_out ) |
117 | #define gunzip_crc (S()gunzip_crc ) | 122 | #define gunzip_crc (S()gunzip_crc ) |
@@ -144,13 +149,13 @@ typedef struct state_t { | |||
144 | #define inflate_stored_b (S()inflate_stored_b ) | 149 | #define inflate_stored_b (S()inflate_stored_b ) |
145 | #define inflate_stored_k (S()inflate_stored_k ) | 150 | #define inflate_stored_k (S()inflate_stored_k ) |
146 | #define inflate_stored_w (S()inflate_stored_w ) | 151 | #define inflate_stored_w (S()inflate_stored_w ) |
147 | #define INIT_STATE ({ bytebuffer_size = 0; method = -1; need_another_block = 1; }) | 152 | #define error_msg (S()error_msg ) |
153 | #define error_jmp (S()error_jmp ) | ||
148 | 154 | ||
149 | 155 | /* This is a generic part */ | |
150 | /* This is generic part */ | ||
151 | #if STATE_IN_BSS /* Use global data segment */ | 156 | #if STATE_IN_BSS /* Use global data segment */ |
152 | #define DECLARE_STATE /*nothing*/ | 157 | #define DECLARE_STATE /*nothing*/ |
153 | #define ALLOC_STATE (init_state()) | 158 | #define ALLOC_STATE /*nothing*/ |
154 | #define DEALLOC_STATE ((void)0) | 159 | #define DEALLOC_STATE ((void)0) |
155 | #define S() state. | 160 | #define S() state. |
156 | #define PASS_STATE /*nothing*/ | 161 | #define PASS_STATE /*nothing*/ |
@@ -158,86 +163,56 @@ typedef struct state_t { | |||
158 | #define STATE_PARAM /*nothing*/ | 163 | #define STATE_PARAM /*nothing*/ |
159 | #define STATE_PARAM_ONLY void | 164 | #define STATE_PARAM_ONLY void |
160 | static state_t state; | 165 | static state_t state; |
161 | static void init_state(void) | ||
162 | { | ||
163 | INIT_STATE; | ||
164 | } | ||
165 | #endif | 166 | #endif |
166 | 167 | ||
167 | #if STATE_IN_MALLOC /* Use malloc space */ | 168 | #if STATE_IN_MALLOC /* Use malloc space */ |
168 | #define DECLARE_STATE state_t *state | 169 | #define DECLARE_STATE state_t *state |
169 | #define ALLOC_STATE (state = alloc_state()) | 170 | #define ALLOC_STATE (state = xzalloc(sizeof(*state))) |
170 | #define DEALLOC_STATE free(state) | 171 | #define DEALLOC_STATE free(state) |
171 | #define S() state-> | 172 | #define S() state-> |
172 | #define PASS_STATE state, | 173 | #define PASS_STATE state, |
173 | #define PASS_STATE_ONLY state | 174 | #define PASS_STATE_ONLY state |
174 | #define STATE_PARAM state_t *state, | 175 | #define STATE_PARAM state_t *state, |
175 | #define STATE_PARAM_ONLY state_t *state | 176 | #define STATE_PARAM_ONLY state_t *state |
176 | static state_t* alloc_state(void) | ||
177 | { | ||
178 | state_t* state = xzalloc(sizeof(*state)); | ||
179 | INIT_STATE; | ||
180 | return state; | ||
181 | } | ||
182 | #endif | 177 | #endif |
183 | 178 | ||
184 | 179 | ||
185 | static const unsigned short mask_bits[] ALIGN2 = { | 180 | static const uint16_t mask_bits[] ALIGN2 = { |
186 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | 181 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
187 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | 182 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
188 | }; | 183 | }; |
189 | 184 | ||
190 | /* Copy lengths for literal codes 257..285 */ | 185 | /* Copy lengths for literal codes 257..285 */ |
191 | static const unsigned short cplens[] ALIGN2 = { | 186 | static const uint16_t cplens[] ALIGN2 = { |
192 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, | 187 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, |
193 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 | 188 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
194 | }; | 189 | }; |
195 | 190 | ||
196 | /* note: see note #13 above about the 258 in this list. */ | 191 | /* note: see note #13 above about the 258 in this list. */ |
197 | /* Extra bits for literal codes 257..285 */ | 192 | /* Extra bits for literal codes 257..285 */ |
198 | static const unsigned char cplext[] ALIGN1 = { | 193 | static const uint8_t cplext[] ALIGN1 = { |
199 | 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, | 194 | 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, |
200 | 5, 5, 5, 0, 99, 99 | 195 | 5, 5, 5, 0, 99, 99 |
201 | }; /* 99 == invalid */ | 196 | }; /* 99 == invalid */ |
202 | 197 | ||
203 | /* Copy offsets for distance codes 0..29 */ | 198 | /* Copy offsets for distance codes 0..29 */ |
204 | static const unsigned short cpdist[] ALIGN2 = { | 199 | static const uint16_t cpdist[] ALIGN2 = { |
205 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, | 200 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, |
206 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 | 201 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 |
207 | }; | 202 | }; |
208 | 203 | ||
209 | /* Extra bits for distance codes */ | 204 | /* Extra bits for distance codes */ |
210 | static const unsigned char cpdext[] ALIGN1 = { | 205 | static const uint8_t cpdext[] ALIGN1 = { |
211 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, | 206 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, |
212 | 11, 11, 12, 12, 13, 13 | 207 | 11, 11, 12, 12, 13, 13 |
213 | }; | 208 | }; |
214 | 209 | ||
215 | /* Tables for deflate from PKZIP's appnote.txt. */ | 210 | /* Tables for deflate from PKZIP's appnote.txt. */ |
216 | /* Order of the bit length code lengths */ | 211 | /* Order of the bit length code lengths */ |
217 | static const unsigned char border[] ALIGN1 = { | 212 | static const uint8_t border[] ALIGN1 = { |
218 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | 213 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
219 | }; | 214 | }; |
220 | 215 | ||
221 | static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required) | ||
222 | { | ||
223 | while (*current < required) { | ||
224 | if (bytebuffer_offset >= bytebuffer_size) { | ||
225 | /* Leave the first 4 bytes empty so we can always unwind the bitbuffer | ||
226 | * to the front of the bytebuffer, leave 4 bytes free at end of tail | ||
227 | * so we can easily top up buffer in check_trailer_gzip() */ | ||
228 | bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8); | ||
229 | if (1 > bytebuffer_size) | ||
230 | //shouldn't we propagate error? | ||
231 | bb_error_msg_and_die("unexpected end of file"); | ||
232 | bytebuffer_size += 4; | ||
233 | bytebuffer_offset = 4; | ||
234 | } | ||
235 | bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current; | ||
236 | bytebuffer_offset++; | ||
237 | *current += 8; | ||
238 | } | ||
239 | return bitbuffer; | ||
240 | } | ||
241 | 216 | ||
242 | /* | 217 | /* |
243 | * Free the malloc'ed tables built by huft_build(), which makes a linked | 218 | * Free the malloc'ed tables built by huft_build(), which makes a linked |
@@ -245,7 +220,7 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current | |||
245 | * each table. | 220 | * each table. |
246 | * t: table to free | 221 | * t: table to free |
247 | */ | 222 | */ |
248 | static void huft_free(huft_t * p) | 223 | static void huft_free(huft_t *p) |
249 | { | 224 | { |
250 | huft_t *q; | 225 | huft_t *q; |
251 | 226 | ||
@@ -257,11 +232,48 @@ static void huft_free(huft_t * p) | |||
257 | } | 232 | } |
258 | } | 233 | } |
259 | 234 | ||
235 | static void huft_free_all(STATE_PARAM_ONLY) | ||
236 | { | ||
237 | huft_free(inflate_codes_tl); | ||
238 | huft_free(inflate_codes_td); | ||
239 | inflate_codes_tl = NULL; | ||
240 | inflate_codes_td = NULL; | ||
241 | } | ||
242 | |||
243 | static void abort_unzip(STATE_PARAM_ONLY) ATTRIBUTE_NORETURN; | ||
244 | static void abort_unzip(STATE_PARAM_ONLY) | ||
245 | { | ||
246 | huft_free_all(PASS_STATE_ONLY); | ||
247 | longjmp(error_jmp, 1); | ||
248 | } | ||
249 | |||
250 | static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required) | ||
251 | { | ||
252 | while (*current < required) { | ||
253 | if (bytebuffer_offset >= bytebuffer_size) { | ||
254 | /* Leave the first 4 bytes empty so we can always unwind the bitbuffer | ||
255 | * to the front of the bytebuffer */ | ||
256 | bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 4); | ||
257 | if ((int)bytebuffer_size < 1) { | ||
258 | error_msg = "unexpected end of file"; | ||
259 | abort_unzip(PASS_STATE_ONLY); | ||
260 | } | ||
261 | bytebuffer_size += 4; | ||
262 | bytebuffer_offset = 4; | ||
263 | } | ||
264 | bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current; | ||
265 | bytebuffer_offset++; | ||
266 | *current += 8; | ||
267 | } | ||
268 | return bitbuffer; | ||
269 | } | ||
270 | |||
271 | |||
260 | /* Given a list of code lengths and a maximum table size, make a set of | 272 | /* Given a list of code lengths and a maximum table size, make a set of |
261 | * tables to decode that set of codes. Return zero on success, one if | 273 | * tables to decode that set of codes. Return zero on success, one if |
262 | * the given code set is incomplete (the tables are still built in this | 274 | * the given code set is incomplete (the tables are still built in this |
263 | * case), two if the input is invalid (all zero length codes or an | 275 | * case), two if the input is invalid (all zero length codes or an |
264 | * oversubscribed set of lengths), and three if not enough memory. | 276 | * oversubscribed set of lengths) - in this case stores NULL in *t. |
265 | * | 277 | * |
266 | * b: code lengths in bits (all assumed <= BMAX) | 278 | * b: code lengths in bits (all assumed <= BMAX) |
267 | * n: number of codes (assumed <= N_MAX) | 279 | * n: number of codes (assumed <= N_MAX) |
@@ -271,66 +283,67 @@ static void huft_free(huft_t * p) | |||
271 | * t: result: starting table | 283 | * t: result: starting table |
272 | * m: maximum lookup bits, returns actual | 284 | * m: maximum lookup bits, returns actual |
273 | */ | 285 | */ |
274 | static int huft_build(unsigned *b, const unsigned n, | 286 | static int huft_build(const unsigned *b, const unsigned n, |
275 | const unsigned s, const unsigned short *d, | 287 | const unsigned s, const unsigned short *d, |
276 | const unsigned char *e, huft_t ** t, unsigned *m) | 288 | const unsigned char *e, huft_t **t, unsigned *m) |
277 | { | 289 | { |
278 | unsigned a; /* counter for codes of length k */ | 290 | unsigned a; /* counter for codes of length k */ |
279 | unsigned c[BMAX + 1]; /* bit length count table */ | 291 | unsigned c[BMAX + 1]; /* bit length count table */ |
280 | unsigned eob_len; /* length of end-of-block code (value 256) */ | 292 | unsigned eob_len; /* length of end-of-block code (value 256) */ |
281 | unsigned f; /* i repeats in table every f entries */ | 293 | unsigned f; /* i repeats in table every f entries */ |
282 | int g; /* maximum code length */ | 294 | int g; /* maximum code length */ |
283 | int htl; /* table level */ | 295 | int htl; /* table level */ |
284 | unsigned i; /* counter, current code */ | 296 | unsigned i; /* counter, current code */ |
285 | unsigned j; /* counter */ | 297 | unsigned j; /* counter */ |
286 | int k; /* number of bits in current code */ | 298 | int k; /* number of bits in current code */ |
287 | unsigned *p; /* pointer into c[], b[], or v[] */ | 299 | unsigned *p; /* pointer into c[], b[], or v[] */ |
288 | huft_t *q; /* points to current table */ | 300 | huft_t *q; /* points to current table */ |
289 | huft_t r; /* table entry for structure assignment */ | 301 | huft_t r; /* table entry for structure assignment */ |
290 | huft_t *u[BMAX]; /* table stack */ | 302 | huft_t *u[BMAX]; /* table stack */ |
291 | unsigned v[N_MAX]; /* values in order of bit length */ | 303 | unsigned v[N_MAX]; /* values in order of bit length */ |
292 | int ws[BMAX+1]; /* bits decoded stack */ | 304 | int ws[BMAX + 1]; /* bits decoded stack */ |
293 | int w; /* bits decoded */ | 305 | int w; /* bits decoded */ |
294 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ | 306 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
295 | unsigned *xp; /* pointer into x */ | 307 | unsigned *xp; /* pointer into x */ |
296 | int y; /* number of dummy codes added */ | 308 | int y; /* number of dummy codes added */ |
297 | unsigned z; /* number of entries in current table */ | 309 | unsigned z; /* number of entries in current table */ |
298 | 310 | ||
299 | /* Length of EOB code, if any */ | 311 | /* Length of EOB code, if any */ |
300 | eob_len = n > 256 ? b[256] : BMAX; | 312 | eob_len = n > 256 ? b[256] : BMAX; |
301 | 313 | ||
314 | *t = NULL; | ||
315 | |||
302 | /* Generate counts for each bit length */ | 316 | /* Generate counts for each bit length */ |
303 | memset(c, 0, sizeof(c)); | 317 | memset(c, 0, sizeof(c)); |
304 | p = b; | 318 | p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */ |
305 | i = n; | 319 | i = n; |
306 | do { | 320 | do { |
307 | c[*p]++; /* assume all entries <= BMAX */ | 321 | c[*p]++; /* assume all entries <= BMAX */ |
308 | p++; /* Can't combine with above line (Solaris bug) */ | 322 | p++; /* can't combine with above line (Solaris bug) */ |
309 | } while (--i); | 323 | } while (--i); |
310 | if (c[0] == n) { /* null input--all zero length codes */ | 324 | if (c[0] == n) { /* null input - all zero length codes */ |
311 | *t = NULL; | ||
312 | *m = 0; | 325 | *m = 0; |
313 | return 2; | 326 | return 2; |
314 | } | 327 | } |
315 | 328 | ||
316 | /* Find minimum and maximum length, bound *m by those */ | 329 | /* Find minimum and maximum length, bound *m by those */ |
317 | for (j = 1; (c[j] == 0) && (j <= BMAX); j++); | 330 | for (j = 1; (c[j] == 0) && (j <= BMAX); j++) |
331 | continue; | ||
318 | k = j; /* minimum code length */ | 332 | k = j; /* minimum code length */ |
319 | for (i = BMAX; (c[i] == 0) && i; i--); | 333 | for (i = BMAX; (c[i] == 0) && i; i--) |
334 | continue; | ||
320 | g = i; /* maximum code length */ | 335 | g = i; /* maximum code length */ |
321 | *m = (*m < j) ? j : ((*m > i) ? i : *m); | 336 | *m = (*m < j) ? j : ((*m > i) ? i : *m); |
322 | 337 | ||
323 | /* Adjust last length count to fill out codes, if needed */ | 338 | /* Adjust last length count to fill out codes, if needed */ |
324 | for (y = 1 << j; j < i; j++, y <<= 1) { | 339 | for (y = 1 << j; j < i; j++, y <<= 1) { |
325 | y -= c[j]; | 340 | y -= c[j]; |
326 | if (y < 0) { | 341 | if (y < 0) |
327 | return 2; /* bad input: more codes than bits */ | 342 | return 2; /* bad input: more codes than bits */ |
328 | } | ||
329 | } | 343 | } |
330 | y -= c[i]; | 344 | y -= c[i]; |
331 | if (y < 0) { | 345 | if (y < 0) |
332 | return 2; | 346 | return 2; |
333 | } | ||
334 | c[i] += y; | 347 | c[i] += y; |
335 | 348 | ||
336 | /* Generate starting offsets into the value table for each length */ | 349 | /* Generate starting offsets into the value table for each length */ |
@@ -343,7 +356,7 @@ static int huft_build(unsigned *b, const unsigned n, | |||
343 | } | 356 | } |
344 | 357 | ||
345 | /* Make a table of values in order of bit lengths */ | 358 | /* Make a table of values in order of bit lengths */ |
346 | p = b; | 359 | p = (unsigned *) b; |
347 | i = 0; | 360 | i = 0; |
348 | do { | 361 | do { |
349 | j = *p++; | 362 | j = *p++; |
@@ -353,13 +366,13 @@ static int huft_build(unsigned *b, const unsigned n, | |||
353 | } while (++i < n); | 366 | } while (++i < n); |
354 | 367 | ||
355 | /* Generate the Huffman codes and for each, make the table entries */ | 368 | /* Generate the Huffman codes and for each, make the table entries */ |
356 | x[0] = i = 0; /* first Huffman code is zero */ | 369 | x[0] = i = 0; /* first Huffman code is zero */ |
357 | p = v; /* grab values in bit order */ | 370 | p = v; /* grab values in bit order */ |
358 | htl = -1; /* no tables yet--level -1 */ | 371 | htl = -1; /* no tables yet--level -1 */ |
359 | w = ws[0] = 0; /* bits decoded */ | 372 | w = ws[0] = 0; /* bits decoded */ |
360 | u[0] = NULL; /* just to keep compilers happy */ | 373 | u[0] = NULL; /* just to keep compilers happy */ |
361 | q = NULL; /* ditto */ | 374 | q = NULL; /* ditto */ |
362 | z = 0; /* ditto */ | 375 | z = 0; /* ditto */ |
363 | 376 | ||
364 | /* go through the bit lengths (k already is bits in shortest code) */ | 377 | /* go through the bit lengths (k already is bits in shortest code) */ |
365 | for (; k <= g; k++) { | 378 | for (; k <= g; k++) { |
@@ -442,7 +455,7 @@ static int huft_build(unsigned *b, const unsigned n, | |||
442 | /* return actual size of base table */ | 455 | /* return actual size of base table */ |
443 | *m = ws[1]; | 456 | *m = ws[1]; |
444 | 457 | ||
445 | /* Return true (1) if we were given an incomplete table */ | 458 | /* Return 1 if we were given an incomplete table */ |
446 | return y != 0 && g != 1; | 459 | return y != 0 && g != 1; |
447 | } | 460 | } |
448 | 461 | ||
@@ -468,10 +481,8 @@ static int huft_build(unsigned *b, const unsigned n, | |||
468 | #define bd inflate_codes_bd | 481 | #define bd inflate_codes_bd |
469 | #define nn inflate_codes_nn | 482 | #define nn inflate_codes_nn |
470 | #define dd inflate_codes_dd | 483 | #define dd inflate_codes_dd |
471 | static void inflate_codes_setup(STATE_PARAM huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd) | 484 | static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd) |
472 | { | 485 | { |
473 | tl = my_tl; | ||
474 | td = my_td; | ||
475 | bl = my_bl; | 486 | bl = my_bl; |
476 | bd = my_bd; | 487 | bd = my_bd; |
477 | /* make local copies of globals */ | 488 | /* make local copies of globals */ |
@@ -488,7 +499,8 @@ static int inflate_codes(STATE_PARAM_ONLY) | |||
488 | unsigned e; /* table entry flag/number of extra bits */ | 499 | unsigned e; /* table entry flag/number of extra bits */ |
489 | huft_t *t; /* pointer to table entry */ | 500 | huft_t *t; /* pointer to table entry */ |
490 | 501 | ||
491 | if (resume_copy) goto do_copy; | 502 | if (resume_copy) |
503 | goto do_copy; | ||
492 | 504 | ||
493 | while (1) { /* do until end of block */ | 505 | while (1) { /* do until end of block */ |
494 | bb = fill_bitbuffer(PASS_STATE bb, &k, bl); | 506 | bb = fill_bitbuffer(PASS_STATE bb, &k, bl); |
@@ -496,10 +508,8 @@ static int inflate_codes(STATE_PARAM_ONLY) | |||
496 | e = t->e; | 508 | e = t->e; |
497 | if (e > 16) | 509 | if (e > 16) |
498 | do { | 510 | do { |
499 | if (e == 99) { | 511 | if (e == 99) |
500 | //shouldn't we propagate error? | 512 | abort_unzip(PASS_STATE_ONLY);; |
501 | bb_error_msg_and_die("inflate_codes error 1"); | ||
502 | } | ||
503 | bb >>= t->b; | 513 | bb >>= t->b; |
504 | k -= t->b; | 514 | k -= t->b; |
505 | e -= 16; | 515 | e -= 16; |
@@ -536,8 +546,7 @@ static int inflate_codes(STATE_PARAM_ONLY) | |||
536 | if (e > 16) | 546 | if (e > 16) |
537 | do { | 547 | do { |
538 | if (e == 99) | 548 | if (e == 99) |
539 | //shouldn't we propagate error? | 549 | abort_unzip(PASS_STATE_ONLY); |
540 | bb_error_msg_and_die("inflate_codes error 2"); | ||
541 | bb >>= t->b; | 550 | bb >>= t->b; |
542 | k -= t->b; | 551 | k -= t->b; |
543 | e -= 16; | 552 | e -= 16; |
@@ -592,9 +601,8 @@ static int inflate_codes(STATE_PARAM_ONLY) | |||
592 | gunzip_bk = k; | 601 | gunzip_bk = k; |
593 | 602 | ||
594 | /* normally just after call to inflate_codes, but save code by putting it here */ | 603 | /* normally just after call to inflate_codes, but save code by putting it here */ |
595 | /* free the decoding tables, return */ | 604 | /* free the decoding tables (tl and td), return */ |
596 | huft_free(tl); | 605 | huft_free_all(PASS_STATE_ONLY); |
597 | huft_free(td); | ||
598 | 606 | ||
599 | /* done */ | 607 | /* done */ |
600 | return 0; | 608 | return 0; |
@@ -634,7 +642,7 @@ static int inflate_stored(STATE_PARAM_ONLY) | |||
634 | inflate_stored_w = 0; | 642 | inflate_stored_w = 0; |
635 | inflate_stored_b >>= 8; | 643 | inflate_stored_b >>= 8; |
636 | inflate_stored_k -= 8; | 644 | inflate_stored_k -= 8; |
637 | return 1; // We have a block | 645 | return 1; /* We have a block */ |
638 | } | 646 | } |
639 | inflate_stored_b >>= 8; | 647 | inflate_stored_b >>= 8; |
640 | inflate_stored_k -= 8; | 648 | inflate_stored_k -= 8; |
@@ -644,7 +652,7 @@ static int inflate_stored(STATE_PARAM_ONLY) | |||
644 | gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */ | 652 | gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */ |
645 | gunzip_bb = inflate_stored_b; /* restore global bit buffer */ | 653 | gunzip_bb = inflate_stored_b; /* restore global bit buffer */ |
646 | gunzip_bk = inflate_stored_k; | 654 | gunzip_bk = inflate_stored_k; |
647 | return 0; // Finished | 655 | return 0; /* Finished */ |
648 | } | 656 | } |
649 | 657 | ||
650 | 658 | ||
@@ -658,9 +666,10 @@ static int inflate_stored(STATE_PARAM_ONLY) | |||
658 | /* One callsite in inflate_get_next_window */ | 666 | /* One callsite in inflate_get_next_window */ |
659 | static int inflate_block(STATE_PARAM smallint *e) | 667 | static int inflate_block(STATE_PARAM smallint *e) |
660 | { | 668 | { |
661 | unsigned t; /* block type */ | 669 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ |
662 | unsigned b; /* bit buffer */ | 670 | unsigned t; /* block type */ |
663 | unsigned k; /* number of bits in bit buffer */ | 671 | unsigned b; /* bit buffer */ |
672 | unsigned k; /* number of bits in bit buffer */ | ||
664 | 673 | ||
665 | /* make local bit buffer */ | 674 | /* make local bit buffer */ |
666 | 675 | ||
@@ -683,9 +692,13 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
683 | gunzip_bb = b; | 692 | gunzip_bb = b; |
684 | gunzip_bk = k; | 693 | gunzip_bk = k; |
685 | 694 | ||
695 | /* Do we see block type 1 often? Yes! | ||
696 | * TODO: fix performance problem (see below) */ | ||
697 | //bb_error_msg("blktype %d", t); | ||
698 | |||
686 | /* inflate that block type */ | 699 | /* inflate that block type */ |
687 | switch (t) { | 700 | switch (t) { |
688 | case 0: /* Inflate stored */ | 701 | case 0: /* Inflate stored */ |
689 | { | 702 | { |
690 | unsigned n; /* number of bytes in block */ | 703 | unsigned n; /* number of bytes in block */ |
691 | unsigned b_stored; /* bit buffer */ | 704 | unsigned b_stored; /* bit buffer */ |
@@ -708,86 +721,73 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
708 | 721 | ||
709 | b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16); | 722 | b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16); |
710 | if (n != (unsigned) ((~b_stored) & 0xffff)) { | 723 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
711 | return 1; /* error in compressed data */ | 724 | abort_unzip(PASS_STATE_ONLY); /* error in compressed data */ |
712 | } | 725 | } |
713 | b_stored >>= 16; | 726 | b_stored >>= 16; |
714 | k_stored -= 16; | 727 | k_stored -= 16; |
715 | 728 | ||
716 | inflate_stored_setup(PASS_STATE n, b_stored, k_stored); // Setup inflate_stored | 729 | inflate_stored_setup(PASS_STATE n, b_stored, k_stored); |
717 | 730 | ||
718 | return -1; | 731 | return -1; |
719 | } | 732 | } |
720 | case 1: | 733 | case 1: |
721 | /* Inflate fixed | 734 | /* Inflate fixed |
722 | * decompress an inflated type 1 (fixed Huffman codes) block. We should | 735 | * decompress an inflated type 1 (fixed Huffman codes) block. We should |
723 | * either replace this with a custom decoder, or at least precompute the | 736 | * either replace this with a custom decoder, or at least precompute the |
724 | * Huffman tables. */ | 737 | * Huffman tables. TODO */ |
725 | { | 738 | { |
726 | int i; /* temporary variable */ | 739 | int i; /* temporary variable */ |
727 | huft_t *tl; /* literal/length code table */ | 740 | unsigned bl; /* lookup bits for tl */ |
728 | huft_t *td; /* distance code table */ | 741 | unsigned bd; /* lookup bits for td */ |
729 | unsigned bl; /* lookup bits for tl */ | 742 | /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */ |
730 | unsigned bd; /* lookup bits for td */ | 743 | //unsigned ll[288]; /* length list for huft_build */ |
731 | unsigned l[288]; /* length list for huft_build */ | ||
732 | 744 | ||
733 | /* set up literal table */ | 745 | /* set up literal table */ |
734 | for (i = 0; i < 144; i++) { | 746 | for (i = 0; i < 144; i++) |
735 | l[i] = 8; | 747 | ll[i] = 8; |
736 | } | 748 | for (; i < 256; i++) |
737 | for (; i < 256; i++) { | 749 | ll[i] = 9; |
738 | l[i] = 9; | 750 | for (; i < 280; i++) |
739 | } | 751 | ll[i] = 7; |
740 | for (; i < 280; i++) { | 752 | for (; i < 288; i++) /* make a complete, but wrong code set */ |
741 | l[i] = 7; | 753 | ll[i] = 8; |
742 | } | ||
743 | for (; i < 288; i++) { /* make a complete, but wrong code set */ | ||
744 | l[i] = 8; | ||
745 | } | ||
746 | bl = 7; | 754 | bl = 7; |
747 | i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl); | 755 | huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl); |
748 | if (i != 0) { | 756 | /* huft_build() never return nonzero - we use known data */ |
749 | return i; | ||
750 | } | ||
751 | 757 | ||
752 | /* set up distance table */ | 758 | /* set up distance table */ |
753 | for (i = 0; i < 30; i++) { /* make an incomplete code set */ | 759 | for (i = 0; i < 30; i++) /* make an incomplete code set */ |
754 | l[i] = 5; | 760 | ll[i] = 5; |
755 | } | ||
756 | bd = 5; | 761 | bd = 5; |
757 | i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd); | 762 | huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd); |
758 | if (i > 1) { | ||
759 | huft_free(tl); | ||
760 | return i; | ||
761 | } | ||
762 | 763 | ||
763 | /* decompress until an end-of-block code */ | 764 | /* set up data for inflate_codes() */ |
764 | inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes | 765 | inflate_codes_setup(PASS_STATE bl, bd); |
765 | 766 | ||
766 | /* huft_free code moved into inflate_codes */ | 767 | /* huft_free code moved into inflate_codes */ |
767 | 768 | ||
768 | return -2; | 769 | return -2; |
769 | } | 770 | } |
770 | case 2: /* Inflate dynamic */ | 771 | case 2: /* Inflate dynamic */ |
771 | { | 772 | { |
772 | const int dbits = 6; /* bits in base distance lookup table */ | 773 | enum { dbits = 6 }; /* bits in base distance lookup table */ |
773 | const int lbits = 9; /* bits in base literal/length lookup table */ | 774 | enum { lbits = 9 }; /* bits in base literal/length lookup table */ |
774 | 775 | ||
775 | huft_t *tl; /* literal/length code table */ | 776 | huft_t *td; /* distance code table */ |
776 | huft_t *td; /* distance code table */ | 777 | unsigned i; /* temporary variables */ |
777 | unsigned i; /* temporary variables */ | ||
778 | unsigned j; | 778 | unsigned j; |
779 | unsigned l; /* last length */ | 779 | unsigned l; /* last length */ |
780 | unsigned m; /* mask for bit lengths table */ | 780 | unsigned m; /* mask for bit lengths table */ |
781 | unsigned n; /* number of lengths to get */ | 781 | unsigned n; /* number of lengths to get */ |
782 | unsigned bl; /* lookup bits for tl */ | 782 | unsigned bl; /* lookup bits for tl */ |
783 | unsigned bd; /* lookup bits for td */ | 783 | unsigned bd; /* lookup bits for td */ |
784 | unsigned nb; /* number of bit length codes */ | 784 | unsigned nb; /* number of bit length codes */ |
785 | unsigned nl; /* number of literal/length codes */ | 785 | unsigned nl; /* number of literal/length codes */ |
786 | unsigned nd; /* number of distance codes */ | 786 | unsigned nd; /* number of distance codes */ |
787 | 787 | ||
788 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ | 788 | //unsigned ll[286 + 30];/* literal/length and distance code lengths */ |
789 | unsigned b_dynamic; /* bit buffer */ | 789 | unsigned b_dynamic; /* bit buffer */ |
790 | unsigned k_dynamic; /* number of bits in bit buffer */ | 790 | unsigned k_dynamic; /* number of bits in bit buffer */ |
791 | 791 | ||
792 | /* make local bit buffer */ | 792 | /* make local bit buffer */ |
793 | b_dynamic = gunzip_bb; | 793 | b_dynamic = gunzip_bb; |
@@ -809,9 +809,8 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
809 | 809 | ||
810 | b_dynamic >>= 4; | 810 | b_dynamic >>= 4; |
811 | k_dynamic -= 4; | 811 | k_dynamic -= 4; |
812 | if (nl > 286 || nd > 30) { | 812 | if (nl > 286 || nd > 30) |
813 | return 1; /* bad lengths */ | 813 | abort_unzip(PASS_STATE_ONLY); /* bad lengths */ |
814 | } | ||
815 | 814 | ||
816 | /* read in bit-length-code lengths */ | 815 | /* read in bit-length-code lengths */ |
817 | for (j = 0; j < nb; j++) { | 816 | for (j = 0; j < nb; j++) { |
@@ -820,18 +819,14 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
820 | b_dynamic >>= 3; | 819 | b_dynamic >>= 3; |
821 | k_dynamic -= 3; | 820 | k_dynamic -= 3; |
822 | } | 821 | } |
823 | for (; j < 19; j++) { | 822 | for (; j < 19; j++) |
824 | ll[border[j]] = 0; | 823 | ll[border[j]] = 0; |
825 | } | ||
826 | 824 | ||
827 | /* build decoding table for trees--single level, 7 bit lookup */ | 825 | /* build decoding table for trees - single level, 7 bit lookup */ |
828 | bl = 7; | 826 | bl = 7; |
829 | i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl); | 827 | i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl); |
830 | if (i != 0) { | 828 | if (i != 0) { |
831 | if (i == 1) { | 829 | abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */ |
832 | huft_free(tl); | ||
833 | } | ||
834 | return i; /* incomplete code set */ | ||
835 | } | 830 | } |
836 | 831 | ||
837 | /* read in literal and distance code lengths */ | 832 | /* read in literal and distance code lengths */ |
@@ -840,7 +835,8 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
840 | i = l = 0; | 835 | i = l = 0; |
841 | while ((unsigned) i < n) { | 836 | while ((unsigned) i < n) { |
842 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl); | 837 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl); |
843 | j = (td = tl + ((unsigned) b_dynamic & m))->b; | 838 | td = inflate_codes_tl + ((unsigned) b_dynamic & m); |
839 | j = td->b; | ||
844 | b_dynamic >>= j; | 840 | b_dynamic >>= j; |
845 | k_dynamic -= j; | 841 | k_dynamic -= j; |
846 | j = td->v.n; | 842 | j = td->v.n; |
@@ -852,7 +848,7 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
852 | b_dynamic >>= 2; | 848 | b_dynamic >>= 2; |
853 | k_dynamic -= 2; | 849 | k_dynamic -= 2; |
854 | if ((unsigned) i + j > n) { | 850 | if ((unsigned) i + j > n) { |
855 | return 1; | 851 | abort_unzip(PASS_STATE_ONLY); //return 1; |
856 | } | 852 | } |
857 | while (j--) { | 853 | while (j--) { |
858 | ll[i++] = l; | 854 | ll[i++] = l; |
@@ -863,7 +859,7 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
863 | b_dynamic >>= 3; | 859 | b_dynamic >>= 3; |
864 | k_dynamic -= 3; | 860 | k_dynamic -= 3; |
865 | if ((unsigned) i + j > n) { | 861 | if ((unsigned) i + j > n) { |
866 | return 1; | 862 | abort_unzip(PASS_STATE_ONLY); //return 1; |
867 | } | 863 | } |
868 | while (j--) { | 864 | while (j--) { |
869 | ll[i++] = 0; | 865 | ll[i++] = 0; |
@@ -875,7 +871,7 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
875 | b_dynamic >>= 7; | 871 | b_dynamic >>= 7; |
876 | k_dynamic -= 7; | 872 | k_dynamic -= 7; |
877 | if ((unsigned) i + j > n) { | 873 | if ((unsigned) i + j > n) { |
878 | return 1; | 874 | abort_unzip(PASS_STATE_ONLY); //return 1; |
879 | } | 875 | } |
880 | while (j--) { | 876 | while (j--) { |
881 | ll[i++] = 0; | 877 | ll[i++] = 0; |
@@ -885,7 +881,7 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
885 | } | 881 | } |
886 | 882 | ||
887 | /* free decoding table for trees */ | 883 | /* free decoding table for trees */ |
888 | huft_free(tl); | 884 | huft_free(inflate_codes_tl); |
889 | 885 | ||
890 | /* restore the global bit buffer */ | 886 | /* restore the global bit buffer */ |
891 | gunzip_bb = b_dynamic; | 887 | gunzip_bb = b_dynamic; |
@@ -894,39 +890,23 @@ static int inflate_block(STATE_PARAM smallint *e) | |||
894 | /* build the decoding tables for literal/length and distance codes */ | 890 | /* build the decoding tables for literal/length and distance codes */ |
895 | bl = lbits; | 891 | bl = lbits; |
896 | 892 | ||
897 | i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl); | 893 | i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl); |
898 | if (i != 0) { | 894 | if (i != 0) |
899 | if (i == 1) { | 895 | abort_unzip(PASS_STATE_ONLY); |
900 | //shouldn't we propagate error? | ||
901 | bb_error_msg_and_die("incomplete literal tree"); | ||
902 | /* huft_free(tl); */ | ||
903 | } | ||
904 | return i; /* incomplete code set */ | ||
905 | } | ||
906 | |||
907 | bd = dbits; | 896 | bd = dbits; |
908 | i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd); | 897 | i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd); |
909 | if (i != 0) { | 898 | if (i != 0) |
910 | if (i == 1) { | 899 | abort_unzip(PASS_STATE_ONLY); |
911 | //shouldn't we propagate error? | ||
912 | bb_error_msg_and_die("incomplete distance tree"); | ||
913 | /* huft_free(td); */ | ||
914 | } | ||
915 | huft_free(tl); | ||
916 | return i; /* incomplete code set */ | ||
917 | } | ||
918 | 900 | ||
919 | /* decompress until an end-of-block code */ | 901 | /* set up data for inflate_codes() */ |
920 | inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes | 902 | inflate_codes_setup(PASS_STATE bl, bd); |
921 | 903 | ||
922 | /* huft_free code moved into inflate_codes */ | 904 | /* huft_free code moved into inflate_codes */ |
923 | 905 | ||
924 | return -2; | 906 | return -2; |
925 | } | 907 | } |
926 | default: | 908 | default: |
927 | /* bad block type */ | 909 | abort_unzip(PASS_STATE_ONLY); |
928 | //shouldn't we propagate error? | ||
929 | bb_error_msg_and_die("bad block type %d", t); | ||
930 | } | 910 | } |
931 | } | 911 | } |
932 | 912 | ||
@@ -952,7 +932,7 @@ static int inflate_get_next_window(STATE_PARAM_ONLY) | |||
952 | if (end_reached) { | 932 | if (end_reached) { |
953 | calculate_gunzip_crc(PASS_STATE_ONLY); | 933 | calculate_gunzip_crc(PASS_STATE_ONLY); |
954 | end_reached = 0; | 934 | end_reached = 0; |
955 | need_another_block = 1; | 935 | /* NB: need_another_block is still set */ |
956 | return 0; /* Last block */ | 936 | return 0; /* Last block */ |
957 | } | 937 | } |
958 | method = inflate_block(PASS_STATE &end_reached); | 938 | method = inflate_block(PASS_STATE &end_reached); |
@@ -966,23 +946,21 @@ static int inflate_get_next_window(STATE_PARAM_ONLY) | |||
966 | case -2: | 946 | case -2: |
967 | ret = inflate_codes(PASS_STATE_ONLY); | 947 | ret = inflate_codes(PASS_STATE_ONLY); |
968 | break; | 948 | break; |
969 | default: | 949 | default: /* cannot happen */ |
970 | //shouldn't we propagate error? | 950 | abort_unzip(PASS_STATE_ONLY); |
971 | bb_error_msg_and_die("inflate error %d", method); | ||
972 | } | 951 | } |
973 | 952 | ||
974 | if (ret == 1) { | 953 | if (ret == 1) { |
975 | calculate_gunzip_crc(PASS_STATE_ONLY); | 954 | calculate_gunzip_crc(PASS_STATE_ONLY); |
976 | return 1; // More data left | 955 | return 1; /* more data left */ |
977 | } | 956 | } |
978 | need_another_block = 1; // End of that block | 957 | need_another_block = 1; /* end of that block */ |
979 | } | 958 | } |
980 | /* Doesnt get here */ | 959 | /* Doesnt get here */ |
981 | } | 960 | } |
982 | 961 | ||
983 | 962 | ||
984 | /* Called from unpack_gz_stream() and inflate_unzip() */ | 963 | /* Called from unpack_gz_stream() and inflate_unzip() */ |
985 | /* NB: bytebuffer is allocated here but freeing it is left to the caller! */ | ||
986 | static USE_DESKTOP(long long) int | 964 | static USE_DESKTOP(long long) int |
987 | inflate_unzip_internal(STATE_PARAM int in, int out) | 965 | inflate_unzip_internal(STATE_PARAM int in, int out) |
988 | { | 966 | { |
@@ -995,7 +973,10 @@ inflate_unzip_internal(STATE_PARAM int in, int out) | |||
995 | gunzip_bytes_out = 0; | 973 | gunzip_bytes_out = 0; |
996 | gunzip_src_fd = in; | 974 | gunzip_src_fd = in; |
997 | 975 | ||
998 | /* initialize gunzip_window, bit buffer */ | 976 | /* (re) initialize state */ |
977 | method = -1; | ||
978 | need_another_block = 1; | ||
979 | resume_copy = 0; | ||
999 | gunzip_bk = 0; | 980 | gunzip_bk = 0; |
1000 | gunzip_bb = 0; | 981 | gunzip_bb = 0; |
1001 | 982 | ||
@@ -1003,8 +984,12 @@ inflate_unzip_internal(STATE_PARAM int in, int out) | |||
1003 | gunzip_crc_table = crc32_filltable(NULL, 0); | 984 | gunzip_crc_table = crc32_filltable(NULL, 0); |
1004 | gunzip_crc = ~0; | 985 | gunzip_crc = ~0; |
1005 | 986 | ||
1006 | /* Allocate space for buffer */ | 987 | error_msg = "corrupted data"; |
1007 | bytebuffer = xmalloc(bytebuffer_max); | 988 | if (setjmp(error_jmp)) { |
989 | /* Error from deep inside zip machinery */ | ||
990 | n = -1; | ||
991 | goto ret; | ||
992 | } | ||
1008 | 993 | ||
1009 | while (1) { | 994 | while (1) { |
1010 | int r = inflate_get_next_window(PASS_STATE_ONLY); | 995 | int r = inflate_get_next_window(PASS_STATE_ONLY); |
@@ -1035,6 +1020,10 @@ inflate_unzip_internal(STATE_PARAM int in, int out) | |||
1035 | } | 1020 | } |
1036 | 1021 | ||
1037 | 1022 | ||
1023 | /* External entry points */ | ||
1024 | |||
1025 | /* For unzip */ | ||
1026 | |||
1038 | USE_DESKTOP(long long) int | 1027 | USE_DESKTOP(long long) int |
1039 | inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out) | 1028 | inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out) |
1040 | { | 1029 | { |
@@ -1043,60 +1032,196 @@ inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out) | |||
1043 | 1032 | ||
1044 | ALLOC_STATE; | 1033 | ALLOC_STATE; |
1045 | 1034 | ||
1046 | bytebuffer_max = bufsize + 8; | 1035 | bytebuffer_max = bufsize + 4; |
1047 | bytebuffer_offset = 4; | 1036 | bytebuffer_offset = 4; |
1037 | bytebuffer = xmalloc(bytebuffer_max); | ||
1048 | n = inflate_unzip_internal(PASS_STATE in, out); | 1038 | n = inflate_unzip_internal(PASS_STATE in, out); |
1039 | free(bytebuffer); | ||
1049 | 1040 | ||
1050 | res->crc = gunzip_crc; | 1041 | res->crc = gunzip_crc; |
1051 | res->bytes_out = gunzip_bytes_out; | 1042 | res->bytes_out = gunzip_bytes_out; |
1052 | free(bytebuffer); | ||
1053 | DEALLOC_STATE; | 1043 | DEALLOC_STATE; |
1054 | return n; | 1044 | return n; |
1055 | } | 1045 | } |
1056 | 1046 | ||
1057 | 1047 | ||
1048 | /* For gunzip */ | ||
1049 | |||
1050 | /* helpers first */ | ||
1051 | |||
1052 | /* Top up the input buffer with at least n bytes. */ | ||
1053 | static int top_up(STATE_PARAM unsigned n) | ||
1054 | { | ||
1055 | int count = bytebuffer_size - bytebuffer_offset; | ||
1056 | |||
1057 | if (count < n) { | ||
1058 | memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count); | ||
1059 | bytebuffer_offset = 0; | ||
1060 | bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count); | ||
1061 | if ((int)bytebuffer_size < 0) { | ||
1062 | bb_error_msg("read error"); | ||
1063 | return 0; | ||
1064 | } | ||
1065 | bytebuffer_size += count; | ||
1066 | if (bytebuffer_size < n) | ||
1067 | return 0; | ||
1068 | } | ||
1069 | return 1; | ||
1070 | } | ||
1071 | |||
1072 | static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY) | ||
1073 | { | ||
1074 | uint16_t res; | ||
1075 | #if BB_LITTLE_ENDIAN | ||
1076 | /* gcc 4.2.1 is very clever */ | ||
1077 | memcpy(&res, &bytebuffer[bytebuffer_offset], 2); | ||
1078 | #else | ||
1079 | res = bytebuffer[bytebuffer_offset]; | ||
1080 | res |= bytebuffer[bytebuffer_offset + 1] << 8; | ||
1081 | #endif | ||
1082 | bytebuffer_offset += 2; | ||
1083 | return res; | ||
1084 | } | ||
1085 | |||
1086 | static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY) | ||
1087 | { | ||
1088 | uint32_t res; | ||
1089 | #if BB_LITTLE_ENDIAN | ||
1090 | memcpy(&res, &bytebuffer[bytebuffer_offset], 4); | ||
1091 | #else | ||
1092 | res = bytebuffer[bytebuffer_offset]; | ||
1093 | res |= bytebuffer[bytebuffer_offset + 1] << 8; | ||
1094 | res |= bytebuffer[bytebuffer_offset + 2] << 16; | ||
1095 | res |= bytebuffer[bytebuffer_offset + 3] << 24; | ||
1096 | #endif | ||
1097 | bytebuffer_offset += 4; | ||
1098 | return res; | ||
1099 | } | ||
1100 | |||
1101 | static int check_header_gzip(STATE_PARAM_ONLY) | ||
1102 | { | ||
1103 | union { | ||
1104 | unsigned char raw[8]; | ||
1105 | struct { | ||
1106 | uint8_t gz_method; | ||
1107 | uint8_t flags; | ||
1108 | //uint32_t mtime; - unused fields | ||
1109 | //uint8_t xtra_flags; | ||
1110 | //uint8_t os_flags; | ||
1111 | } formatted; /* packed */ | ||
1112 | } header; | ||
1113 | |||
1114 | /* | ||
1115 | * Rewind bytebuffer. We use the beginning because the header has 8 | ||
1116 | * bytes, leaving enough for unwinding afterwards. | ||
1117 | */ | ||
1118 | bytebuffer_size -= bytebuffer_offset; | ||
1119 | memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size); | ||
1120 | bytebuffer_offset = 0; | ||
1121 | |||
1122 | if (!top_up(PASS_STATE 8)) | ||
1123 | return 0; | ||
1124 | memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8); | ||
1125 | bytebuffer_offset += 8; | ||
1126 | |||
1127 | /* Check the compression method */ | ||
1128 | if (header.formatted.gz_method != 8) { | ||
1129 | return 0; | ||
1130 | } | ||
1131 | |||
1132 | if (header.formatted.flags & 0x04) { | ||
1133 | /* bit 2 set: extra field present */ | ||
1134 | unsigned extra_short; | ||
1135 | |||
1136 | if (!top_up(PASS_STATE 2)) | ||
1137 | return 0; | ||
1138 | extra_short = buffer_read_le_u16(PASS_STATE_ONLY); | ||
1139 | if (!top_up(PASS_STATE extra_short)) | ||
1140 | return 0; | ||
1141 | /* Ignore extra field */ | ||
1142 | bytebuffer_offset += extra_short; | ||
1143 | } | ||
1144 | |||
1145 | /* Discard original name and file comment if any */ | ||
1146 | /* bit 3 set: original file name present */ | ||
1147 | /* bit 4 set: file comment present */ | ||
1148 | if (header.formatted.flags & 0x18) { | ||
1149 | while (1) { | ||
1150 | do { | ||
1151 | if (!top_up(PASS_STATE 1)) | ||
1152 | return 0; | ||
1153 | } while (bytebuffer[bytebuffer_offset++] != 0); | ||
1154 | if ((header.formatted.flags & 0x18) != 0x18) | ||
1155 | break; | ||
1156 | header.formatted.flags &= ~0x18; | ||
1157 | } | ||
1158 | } | ||
1159 | |||
1160 | /* Read the header checksum */ | ||
1161 | if (header.formatted.flags & 0x02) { | ||
1162 | if (!top_up(PASS_STATE 2)) | ||
1163 | return 0; | ||
1164 | bytebuffer_offset += 2; | ||
1165 | } | ||
1166 | return 1; | ||
1167 | } | ||
1168 | |||
1058 | USE_DESKTOP(long long) int | 1169 | USE_DESKTOP(long long) int |
1059 | unpack_gz_stream(int in, int out) | 1170 | unpack_gz_stream(int in, int out) |
1060 | { | 1171 | { |
1061 | uint32_t stored_crc = 0; | 1172 | uint32_t v32; |
1062 | unsigned count; | ||
1063 | USE_DESKTOP(long long) int n; | 1173 | USE_DESKTOP(long long) int n; |
1064 | DECLARE_STATE; | 1174 | DECLARE_STATE; |
1065 | 1175 | ||
1066 | ALLOC_STATE; | 1176 | n = 0; |
1067 | 1177 | ||
1178 | ALLOC_STATE; | ||
1068 | bytebuffer_max = 0x8000; | 1179 | bytebuffer_max = 0x8000; |
1069 | n = inflate_unzip_internal(PASS_STATE in, out); | 1180 | bytebuffer = xmalloc(bytebuffer_max); |
1070 | |||
1071 | if (n < 0) goto ret; | ||
1072 | 1181 | ||
1073 | /* top up the input buffer with the rest of the trailer */ | 1182 | again: |
1074 | count = bytebuffer_size - bytebuffer_offset; | 1183 | if (!check_header_gzip(PASS_STATE_ONLY)) { |
1075 | if (count < 8) { | 1184 | bb_error_msg("corrupted data"); |
1076 | xread(in, &bytebuffer[bytebuffer_size], 8 - count); | 1185 | n = -1; |
1077 | //shouldn't we propagate error? | 1186 | goto ret; |
1078 | bytebuffer_size += 8 - count; | ||
1079 | } | 1187 | } |
1080 | for (count = 0; count != 4; count++) { | 1188 | n += inflate_unzip_internal(PASS_STATE in, out); |
1081 | stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8)); | 1189 | if (n < 0) |
1082 | bytebuffer_offset++; | 1190 | goto ret; |
1191 | |||
1192 | if (!top_up(PASS_STATE 8)) { | ||
1193 | bb_error_msg("corrupted data"); | ||
1194 | n = -1; | ||
1195 | goto ret; | ||
1083 | } | 1196 | } |
1084 | 1197 | ||
1085 | /* Validate decompression - crc */ | 1198 | /* Validate decompression - crc */ |
1086 | if (stored_crc != (~gunzip_crc)) { | 1199 | v32 = buffer_read_le_u32(PASS_STATE_ONLY); |
1200 | if ((~gunzip_crc) != v32) { | ||
1087 | bb_error_msg("crc error"); | 1201 | bb_error_msg("crc error"); |
1088 | n = -1; | 1202 | n = -1; |
1089 | goto ret; | 1203 | goto ret; |
1090 | } | 1204 | } |
1091 | 1205 | ||
1092 | /* Validate decompression - size */ | 1206 | /* Validate decompression - size */ |
1093 | if (gunzip_bytes_out != | 1207 | v32 = buffer_read_le_u32(PASS_STATE_ONLY); |
1094 | (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | | 1208 | if ((uint32_t)gunzip_bytes_out != v32) { |
1095 | (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24)) | ||
1096 | ) { | ||
1097 | bb_error_msg("incorrect length"); | 1209 | bb_error_msg("incorrect length"); |
1098 | n = -1; | 1210 | n = -1; |
1099 | } | 1211 | } |
1212 | |||
1213 | if (!top_up(PASS_STATE 2)) | ||
1214 | goto ret; /* EOF */ | ||
1215 | |||
1216 | if (bytebuffer[bytebuffer_offset] == 0x1f | ||
1217 | && bytebuffer[bytebuffer_offset + 1] == 0x8b | ||
1218 | ) { | ||
1219 | bytebuffer_offset += 2; | ||
1220 | goto again; | ||
1221 | } | ||
1222 | /* GNU gzip says: */ | ||
1223 | /*bb_error_msg("decompression OK, trailing garbage ignored");*/ | ||
1224 | |||
1100 | ret: | 1225 | ret: |
1101 | free(bytebuffer); | 1226 | free(bytebuffer); |
1102 | DEALLOC_STATE; | 1227 | DEALLOC_STATE; |
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c index 4b9e793bb..dd655f557 100644 --- a/archival/libunarchive/get_header_tar_gz.c +++ b/archival/libunarchive/get_header_tar_gz.c | |||
@@ -23,8 +23,6 @@ char get_header_tar_gz(archive_handle_t *archive_handle) | |||
23 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { | 23 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { |
24 | bb_error_msg_and_die("invalid gzip magic"); | 24 | bb_error_msg_and_die("invalid gzip magic"); |
25 | } | 25 | } |
26 | |||
27 | check_header_gzip_or_die(archive_handle->src_fd); | ||
28 | #endif | 26 | #endif |
29 | 27 | ||
30 | archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip", "gunzip", "-cf", "-", NULL); | 28 | archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip", "gunzip", "-cf", "-", NULL); |