diff options
author | Paul Fox <pgf@brightstareng.com> | 2005-07-20 20:26:49 +0000 |
---|---|---|
committer | Paul Fox <pgf@brightstareng.com> | 2005-07-20 20:26:49 +0000 |
commit | 0840b76602900f8236f444b68da16d5c8d57ac3d (patch) | |
tree | 9e4bfeefb4a93867bbee1f2a48f77e7a44389397 /archival/libunarchive | |
parent | f2ddc05ee77a2f5ab9a2be318c694d2f3d4e852c (diff) | |
download | busybox-w32-0840b76602900f8236f444b68da16d5c8d57ac3d.tar.gz busybox-w32-0840b76602900f8236f444b68da16d5c8d57ac3d.tar.bz2 busybox-w32-0840b76602900f8236f444b68da16d5c8d57ac3d.zip |
applying fixes from:
0000142: unzip enhancements
Diffstat (limited to 'archival/libunarchive')
-rw-r--r-- | archival/libunarchive/decompress_unzip.c | 160 |
1 files changed, 81 insertions, 79 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index b17065d92..883aaac46 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c | |||
@@ -16,6 +16,11 @@ | |||
16 | * | 16 | * |
17 | * read_gz interface + associated hacking by Laurence Anderson | 17 | * read_gz interface + associated hacking by Laurence Anderson |
18 | * | 18 | * |
19 | * Fixed huft_build() so decoding end-of-block code does not grab more bits | ||
20 | * than necessary (this is required by unzip applet), added inflate_cleanup() | ||
21 | * to free leaked bytebuffer memory (used in unzip.c), and some minor style | ||
22 | * guide cleanups by Ed Clark | ||
23 | * | ||
19 | * This program is free software; you can redistribute it and/or modify | 24 | * This program is free software; you can redistribute it and/or modify |
20 | * it under the terms of the GNU General Public License as published by | 25 | * it under the terms of the GNU General Public License as published by |
21 | * the Free Software Foundation; either version 2 of the License, or | 26 | * the Free Software Foundation; either version 2 of the License, or |
@@ -116,26 +121,26 @@ static const unsigned short mask_bits[] = { | |||
116 | /* Copy lengths for literal codes 257..285 */ | 121 | /* Copy lengths for literal codes 257..285 */ |
117 | static const unsigned short cplens[] = { | 122 | static const unsigned short cplens[] = { |
118 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, | 123 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, |
119 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 | 124 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
120 | }; | 125 | }; |
121 | 126 | ||
122 | /* note: see note #13 above about the 258 in this list. */ | 127 | /* note: see note #13 above about the 258 in this list. */ |
123 | /* Extra bits for literal codes 257..285 */ | 128 | /* Extra bits for literal codes 257..285 */ |
124 | static const unsigned char cplext[] = { | 129 | static const unsigned char cplext[] = { |
125 | 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, | 130 | 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, |
126 | 5, 5, 5, 0, 99, 99 | 131 | 5, 5, 5, 0, 99, 99 |
127 | }; /* 99==invalid */ | 132 | }; /* 99==invalid */ |
128 | 133 | ||
129 | /* Copy offsets for distance codes 0..29 */ | 134 | /* Copy offsets for distance codes 0..29 */ |
130 | static const unsigned short cpdist[] = { | 135 | static const unsigned short cpdist[] = { |
131 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, | 136 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, |
132 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 | 137 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 |
133 | }; | 138 | }; |
134 | 139 | ||
135 | /* Extra bits for distance codes */ | 140 | /* Extra bits for distance codes */ |
136 | static const unsigned char cpdext[] = { | 141 | static const unsigned char cpdext[] = { |
137 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, | 142 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, |
138 | 11, 11, 12, 12, 13, 13 | 143 | 11, 11, 12, 12, 13, 13 |
139 | }; | 144 | }; |
140 | 145 | ||
141 | /* Tables for deflate from PKZIP's appnote.txt. */ | 146 | /* Tables for deflate from PKZIP's appnote.txt. */ |
@@ -187,8 +192,8 @@ static void make_gunzip_crc_table(void) | |||
187 | table_entry = (table_entry >> 1) ^ poly; | 192 | table_entry = (table_entry >> 1) ^ poly; |
188 | } else { | 193 | } else { |
189 | table_entry >>= 1; | 194 | table_entry >>= 1; |
190 | } | 195 | } |
191 | } | 196 | } |
192 | gunzip_crc_table[i] = table_entry; | 197 | gunzip_crc_table[i] = table_entry; |
193 | } | 198 | } |
194 | } | 199 | } |
@@ -228,70 +233,59 @@ static int huft_free(huft_t * t) | |||
228 | * t: result: starting table | 233 | * t: result: starting table |
229 | * m: maximum lookup bits, returns actual | 234 | * m: maximum lookup bits, returns actual |
230 | */ | 235 | */ |
231 | static int huft_build(unsigned int *b, const unsigned int n, | 236 | int huft_build(unsigned int *b, const unsigned int n, |
232 | const unsigned int s, const unsigned short *d, | 237 | const unsigned int s, const unsigned short *d, |
233 | const unsigned char *e, huft_t ** t, int *m) | 238 | const unsigned char *e, huft_t ** t, int *m) |
234 | { | 239 | { |
235 | unsigned a; /* counter for codes of length k */ | 240 | unsigned a; /* counter for codes of length k */ |
236 | unsigned c[BMAX + 1]; /* bit length count table */ | 241 | unsigned c[BMAX + 1]; /* bit length count table */ |
237 | unsigned f; /* i repeats in table every f entries */ | 242 | unsigned eob_len; /* length of end-of-block code (value 256) */ |
238 | int g; /* maximum code length */ | 243 | unsigned f; /* i repeats in table every f entries */ |
239 | int h; /* table level */ | 244 | int g; /* maximum code length */ |
245 | int h; /* table level */ | ||
240 | register unsigned i; /* counter, current code */ | 246 | register unsigned i; /* counter, current code */ |
241 | register unsigned j; /* counter */ | 247 | register unsigned j; /* counter */ |
242 | register int k; /* number of bits in current code */ | 248 | register int k; /* number of bits in current code */ |
243 | int l; /* bits per table (returned in m) */ | ||
244 | register unsigned *p; /* pointer into c[], b[], or v[] */ | 249 | register unsigned *p; /* pointer into c[], b[], or v[] */ |
245 | register huft_t *q; /* points to current table */ | 250 | register huft_t *q; /* points to current table */ |
246 | huft_t r; /* table entry for structure assignment */ | 251 | huft_t r; /* table entry for structure assignment */ |
247 | huft_t *u[BMAX]; /* table stack */ | 252 | huft_t *u[BMAX]; /* table stack */ |
248 | unsigned v[N_MAX]; /* values in order of bit length */ | 253 | unsigned v[N_MAX]; /* values in order of bit length */ |
249 | register int w; /* bits before this table == (l * h) */ | 254 | int ws[BMAX+1]; /* bits decoded stack */ |
255 | register int w; /* bits decoded */ | ||
250 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ | 256 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
251 | unsigned *xp; /* pointer into x */ | 257 | unsigned *xp; /* pointer into x */ |
252 | int y; /* number of dummy codes added */ | 258 | int y; /* number of dummy codes added */ |
253 | unsigned z; /* number of entries in current table */ | 259 | unsigned z; /* number of entries in current table */ |
260 | |||
261 | /* Length of EOB code, if any */ | ||
262 | eob_len = n > 256 ? b[256] : BMAX; | ||
254 | 263 | ||
255 | /* Generate counts for each bit length */ | 264 | /* Generate counts for each bit length */ |
256 | memset((void *) (c), 0, sizeof(c)); | 265 | memset((void *)c, 0, sizeof(c)); |
257 | p = b; | 266 | p = b; |
258 | i = n; | 267 | i = n; |
259 | do { | 268 | do { |
260 | c[*p]++; /* assume all entries <= BMAX */ | 269 | c[*p]++; /* assume all entries <= BMAX */ |
261 | p++; /* Can't combine with above line (Solaris bug) */ | 270 | p++; /* Can't combine with above line (Solaris bug) */ |
262 | } while (--i); | 271 | } while (--i); |
263 | if (c[0] == n) { /* null input--all zero length codes */ | 272 | if (c[0] == n) { /* null input--all zero length codes */ |
264 | *t = (huft_t *) NULL; | 273 | *t = (huft_t *) NULL; |
265 | *m = 0; | 274 | *m = 0; |
266 | return 0; | 275 | return 0; |
267 | } | 276 | } |
268 | 277 | ||
269 | /* Find minimum and maximum length, bound *m by those */ | 278 | /* Find minimum and maximum length, bound *m by those */ |
270 | l = *m; | 279 | for (j = 1; (c[j] == 0) && (j <= BMAX); j++); |
271 | for (j = 1; j <= BMAX; j++) { | 280 | k = j; /* minimum code length */ |
272 | if (c[j]) { | 281 | for (i = BMAX; (c[i] == 0) && i; i--); |
273 | break; | 282 | g = i; /* maximum code length */ |
274 | } | 283 | *m = (*m < j) ? j : ((*m > i) ? i : *m); |
275 | } | ||
276 | k = j; /* minimum code length */ | ||
277 | if ((unsigned) l < j) { | ||
278 | l = j; | ||
279 | } | ||
280 | for (i = BMAX; i; i--) { | ||
281 | if (c[i]) { | ||
282 | break; | ||
283 | } | ||
284 | } | ||
285 | g = i; /* maximum code length */ | ||
286 | if ((unsigned) l > i) { | ||
287 | l = i; | ||
288 | } | ||
289 | *m = l; | ||
290 | 284 | ||
291 | /* Adjust last length count to fill out codes, if needed */ | 285 | /* Adjust last length count to fill out codes, if needed */ |
292 | for (y = 1 << j; j < i; j++, y <<= 1) { | 286 | for (y = 1 << j; j < i; j++, y <<= 1) { |
293 | if ((y -= c[j]) < 0) { | 287 | if ((y -= c[j]) < 0) { |
294 | return 2; /* bad input: more codes than bits */ | 288 | return 2; /* bad input: more codes than bits */ |
295 | } | 289 | } |
296 | } | 290 | } |
297 | if ((y -= c[i]) < 0) { | 291 | if ((y -= c[i]) < 0) { |
@@ -303,7 +297,7 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
303 | x[1] = j = 0; | 297 | x[1] = j = 0; |
304 | p = c + 1; | 298 | p = c + 1; |
305 | xp = x + 2; | 299 | xp = x + 2; |
306 | while (--i) { /* note that i == g from above */ | 300 | while (--i) { /* note that i == g from above */ |
307 | *xp++ = (j += *p++); | 301 | *xp++ = (j += *p++); |
308 | } | 302 | } |
309 | 303 | ||
@@ -317,13 +311,13 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
317 | } while (++i < n); | 311 | } while (++i < n); |
318 | 312 | ||
319 | /* Generate the Huffman codes and for each, make the table entries */ | 313 | /* Generate the Huffman codes and for each, make the table entries */ |
320 | x[0] = i = 0; /* first Huffman code is zero */ | 314 | x[0] = i = 0; /* first Huffman code is zero */ |
321 | p = v; /* grab values in bit order */ | 315 | p = v; /* grab values in bit order */ |
322 | h = -1; /* no tables yet--level -1 */ | 316 | h = -1; /* no tables yet--level -1 */ |
323 | w = -l; /* bits decoded == (l * h) */ | 317 | w = ws[0] = 0; /* bits decoded */ |
324 | u[0] = (huft_t *) NULL; /* just to keep compilers happy */ | 318 | u[0] = (huft_t *) NULL; /* just to keep compilers happy */ |
325 | q = (huft_t *) NULL; /* ditto */ | 319 | q = (huft_t *) NULL; /* ditto */ |
326 | z = 0; /* ditto */ | 320 | z = 0; /* ditto */ |
327 | 321 | ||
328 | /* go through the bit lengths (k already is bits in shortest code) */ | 322 | /* go through the bit lengths (k already is bits in shortest code) */ |
329 | for (; k <= g; k++) { | 323 | for (; k <= g; k++) { |
@@ -331,52 +325,52 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
331 | while (a--) { | 325 | while (a--) { |
332 | /* here i is the Huffman code of length k bits for value *p */ | 326 | /* here i is the Huffman code of length k bits for value *p */ |
333 | /* make tables up to required level */ | 327 | /* make tables up to required level */ |
334 | while (k > w + l) { | 328 | while (k > ws[h + 1]) { |
335 | h++; | 329 | w = ws[++h]; |
336 | w += l; /* previous table always l bits */ | 330 | |
337 | 331 | /* compute minimum size table less than or equal to *m bits */ | |
338 | /* compute minimum size table less than or equal to l bits */ | 332 | z = (z = g - w) > *m ? *m : z; /* upper limit on table size */ |
339 | z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ | 333 | if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */ |
340 | if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */ | 334 | /* too few codes for k-w bit table */ |
341 | f -= a + 1; /* deduct codes from patterns left */ | 335 | f -= a + 1; /* deduct codes from patterns left */ |
342 | xp = c + k; | 336 | xp = c + k; |
343 | while (++j < z) { /* try smaller tables up to z bits */ | 337 | while (++j < z) { /* try smaller tables up to z bits */ |
344 | if ((f <<= 1) <= *++xp) { | 338 | if ((f <<= 1) <= *++xp) { |
345 | break; /* enough codes to use up j bits */ | 339 | break; /* enough codes to use up j bits */ |
346 | } | 340 | } |
347 | f -= *xp; /* else deduct codes from patterns */ | 341 | f -= *xp; /* else deduct codes from patterns */ |
348 | } | 342 | } |
349 | } | 343 | } |
344 | j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */ | ||
350 | z = 1 << j; /* table entries for j-bit table */ | 345 | z = 1 << j; /* table entries for j-bit table */ |
346 | ws[h+1] = w + j; /* set bits decoded in stack */ | ||
351 | 347 | ||
352 | /* allocate and link in new table */ | 348 | /* allocate and link in new table */ |
353 | q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); | 349 | q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); |
354 | |||
355 | *t = q + 1; /* link to list for huft_free() */ | 350 | *t = q + 1; /* link to list for huft_free() */ |
356 | *(t = &(q->v.t)) = NULL; | 351 | *(t = &(q->v.t)) = NULL; |
357 | u[h] = ++q; /* table starts after link */ | 352 | u[h] = ++q; /* table starts after link */ |
358 | 353 | ||
359 | /* connect to last table, if there is one */ | 354 | /* connect to last table, if there is one */ |
360 | if (h) { | 355 | if (h) { |
361 | x[h] = i; /* save pattern for backing up */ | 356 | x[h] = i; /* save pattern for backing up */ |
362 | r.b = (unsigned char) l; /* bits to dump before this table */ | 357 | r.b = (unsigned char) (w - ws[h - 1]); /* bits to dump before this table */ |
363 | r.e = (unsigned char) (16 + j); /* bits in this table */ | 358 | r.e = (unsigned char) (16 + j); /* bits in this table */ |
364 | r.v.t = q; /* pointer to this table */ | 359 | r.v.t = q; /* pointer to this table */ |
365 | j = i >> (w - l); /* (get around Turbo C bug) */ | 360 | j = (i & ((1 << w) - 1)) >> ws[h - 1]; |
366 | u[h - 1][j] = r; /* connect to last table */ | 361 | u[h - 1][j] = r; /* connect to last table */ |
367 | } | 362 | } |
368 | } | 363 | } |
369 | 364 | ||
370 | /* set up table entry in r */ | 365 | /* set up table entry in r */ |
371 | r.b = (unsigned char) (k - w); | 366 | r.b = (unsigned char) (k - w); |
372 | if (p >= v + n) { | 367 | if (p >= v + n) { |
373 | r.e = 99; /* out of values--invalid code */ | 368 | r.e = 99; /* out of values--invalid code */ |
374 | } else if (*p < s) { | 369 | } else if (*p < s) { |
375 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ | 370 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ |
376 | r.v.n = (unsigned short) (*p); /* simple code is just the value */ | 371 | r.v.n = (unsigned short) (*p++); /* simple code is just the value */ |
377 | p++; /* one compiler does not like *p++ */ | ||
378 | } else { | 372 | } else { |
379 | r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ | 373 | r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ |
380 | r.v.n = d[*p++ - s]; | 374 | r.v.n = d[*p++ - s]; |
381 | } | 375 | } |
382 | 376 | ||
@@ -394,11 +388,14 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
394 | 388 | ||
395 | /* backup over finished tables */ | 389 | /* backup over finished tables */ |
396 | while ((i & ((1 << w) - 1)) != x[h]) { | 390 | while ((i & ((1 << w) - 1)) != x[h]) { |
397 | h--; /* don't need to update q */ | 391 | w = ws[--h]; |
398 | w -= l; | ||
399 | } | 392 | } |
400 | } | 393 | } |
401 | } | 394 | } |
395 | |||
396 | /* return actual size of base table */ | ||
397 | *m = ws[1]; | ||
398 | |||
402 | /* Return true (1) if we were given an incomplete table */ | 399 | /* Return true (1) if we were given an incomplete table */ |
403 | return y != 0 && g != 1; | 400 | return y != 0 && g != 1; |
404 | } | 401 | } |
@@ -904,6 +901,11 @@ extern void inflate_init(unsigned int bufsize) | |||
904 | bytebuffer_size = 0; | 901 | bytebuffer_size = 0; |
905 | } | 902 | } |
906 | 903 | ||
904 | extern void inflate_cleanup(void) | ||
905 | { | ||
906 | free(bytebuffer); | ||
907 | } | ||
908 | |||
907 | extern int inflate_unzip(int in, int out) | 909 | extern int inflate_unzip(int in, int out) |
908 | { | 910 | { |
909 | ssize_t nwrote; | 911 | ssize_t nwrote; |