aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archival/libunarchive/decompress_uncompress.c62
-rw-r--r--archival/libunarchive/decompress_unzip.c106
2 files changed, 110 insertions, 58 deletions
diff --git a/archival/libunarchive/decompress_uncompress.c b/archival/libunarchive/decompress_uncompress.c
index ff98254ff..8ce3cba74 100644
--- a/archival/libunarchive/decompress_uncompress.c
+++ b/archival/libunarchive/decompress_uncompress.c
@@ -64,12 +64,6 @@
64#undef MAXSEG_64K 64#undef MAXSEG_64K
65#define MAXCODE(n) (1L << (n)) 65#define MAXCODE(n) (1L << (n))
66 66
67/* Block compress mode -C compatible with 2.0 */
68static int block_mode = BLOCK_MODE;
69
70/* user settable max # bits/code */
71static int maxbits = BITS;
72
73#define htabof(i) htab[i] 67#define htabof(i) htab[i]
74#define codetabof(i) codetab[i] 68#define codetabof(i) codetab[i]
75#define tab_prefixof(i) codetabof(i) 69#define tab_prefixof(i) codetabof(i)
@@ -109,25 +103,38 @@ uncompress(int fd_in, int fd_out)
109 RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048); 103 RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048);
110 unsigned char htab[HSIZE]; 104 unsigned char htab[HSIZE];
111 unsigned short codetab[HSIZE]; 105 unsigned short codetab[HSIZE];
106
107 /* Hmm, these were statics - why?! */
108 /* user settable max # bits/code */
109 int maxbits; /* = BITS; */
110 /* block compress mode -C compatible with 2.0 */
111 int block_mode; /* = BLOCK_MODE; */
112
112 memset(inbuf, 0, IBUFSIZ + 64); 113 memset(inbuf, 0, IBUFSIZ + 64);
113 memset(outbuf, 0, OBUFSIZ + 2048); 114 memset(outbuf, 0, OBUFSIZ + 2048);
114 115
115 insize = 0; 116 insize = 0;
116 117
117 inbuf[0] = xread_char(fd_in); 118 /* xread isn't good here, we have to return - caller may want
119 * to do some cleanup (e.g. delete incomplete unpacked file etc) */
120 if (full_read(fd_in, inbuf, 1) != 1) {
121 bb_error_msg("short read");
122 return -1;
123 }
118 124
119 maxbits = inbuf[0] & BIT_MASK; 125 maxbits = inbuf[0] & BIT_MASK;
120 block_mode = inbuf[0] & BLOCK_MODE; 126 block_mode = inbuf[0] & BLOCK_MODE;
121 maxmaxcode = MAXCODE(maxbits); 127 maxmaxcode = MAXCODE(maxbits);
122 128
123 if (maxbits > BITS) { 129 if (maxbits > BITS) {
124 bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits, 130 bb_error_msg("compressed with %d bits, can only handle "
125 BITS); 131 "%d bits", maxbits, BITS);
126 return -1; 132 return -1;
127 } 133 }
128 134
129 maxcode = MAXCODE(n_bits = INIT_BITS) - 1; 135 n_bits = INIT_BITS;
130 bitmask = (1 << n_bits) - 1; 136 maxcode = MAXCODE(INIT_BITS) - 1;
137 bitmask = (1 << INIT_BITS) - 1;
131 oldcode = -1; 138 oldcode = -1;
132 finchar = 0; 139 finchar = 0;
133 outpos = 0; 140 outpos = 0;
@@ -143,13 +150,14 @@ uncompress(int fd_in, int fd_out)
143 } 150 }
144 151
145 do { 152 do {
146 resetbuf:; 153 resetbuf:
147 { 154 {
148 int i; 155 int i;
149 int e; 156 int e;
150 int o; 157 int o;
151 158
152 e = insize - (o = (posbits >> 3)); 159 o = posbits >> 3;
160 e = insize - o;
153 161
154 for (i = 0; i < e; ++i) 162 for (i = 0; i < e; ++i)
155 inbuf[i] = inbuf[i + o]; 163 inbuf[i] = inbuf[i + o];
@@ -160,6 +168,7 @@ uncompress(int fd_in, int fd_out)
160 168
161 if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) { 169 if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
162 rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ); 170 rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
171//error check??
163 insize += rsize; 172 insize += rsize;
164 } 173 }
165 174
@@ -184,8 +193,8 @@ uncompress(int fd_in, int fd_out)
184 { 193 {
185 unsigned char *p = &inbuf[posbits >> 3]; 194 unsigned char *p = &inbuf[posbits >> 3];
186 195
187 code = ((((long) (p[0])) | ((long) (p[1]) << 8) | 196 code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
188 ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask; 197 ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
189 } 198 }
190 posbits += n_bits; 199 posbits += n_bits;
191 200
@@ -204,8 +213,9 @@ uncompress(int fd_in, int fd_out)
204 ((posbits - 1) + 213 ((posbits - 1) +
205 ((n_bits << 3) - 214 ((n_bits << 3) -
206 (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); 215 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
207 maxcode = MAXCODE(n_bits = INIT_BITS) - 1; 216 n_bits = INIT_BITS;
208 bitmask = (1 << n_bits) - 1; 217 maxcode = MAXCODE(INIT_BITS) - 1;
218 bitmask = (1 << INIT_BITS) - 1;
209 goto resetbuf; 219 goto resetbuf;
210 } 220 }
211 221
@@ -238,13 +248,15 @@ uncompress(int fd_in, int fd_out)
238 code = tab_prefixof(code); 248 code = tab_prefixof(code);
239 } 249 }
240 250
241 *--stackp = (unsigned char) (finchar = tab_suffixof(code)); 251 finchar = tab_suffixof(code);
252 *--stackp = (unsigned char) finchar;
242 253
243 /* And put them out in forward order */ 254 /* And put them out in forward order */
244 { 255 {
245 int i; 256 int i;
246 257
247 if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) { 258 i = de_stack - stackp;
259 if (outpos + i >= OBUFSIZ) {
248 do { 260 do {
249 if (i > OBUFSIZ - outpos) { 261 if (i > OBUFSIZ - outpos) {
250 i = OBUFSIZ - outpos; 262 i = OBUFSIZ - outpos;
@@ -256,12 +268,14 @@ uncompress(int fd_in, int fd_out)
256 } 268 }
257 269
258 if (outpos >= OBUFSIZ) { 270 if (outpos >= OBUFSIZ) {
259 write(fd_out, outbuf, outpos); 271 full_write(fd_out, outbuf, outpos);
272//error check??
260 USE_DESKTOP(total_written += outpos;) 273 USE_DESKTOP(total_written += outpos;)
261 outpos = 0; 274 outpos = 0;
262 } 275 }
263 stackp += i; 276 stackp += i;
264 } while ((i = (de_stack - stackp)) > 0); 277 i = de_stack - stackp;
278 } while (i > 0);
265 } else { 279 } else {
266 memcpy(outbuf + outpos, stackp, i); 280 memcpy(outbuf + outpos, stackp, i);
267 outpos += i; 281 outpos += i;
@@ -269,7 +283,8 @@ uncompress(int fd_in, int fd_out)
269 } 283 }
270 284
271 /* Generate the new entry. */ 285 /* Generate the new entry. */
272 if ((code = free_ent) < maxmaxcode) { 286 code = free_ent;
287 if (code < maxmaxcode) {
273 tab_prefixof(code) = (unsigned short) oldcode; 288 tab_prefixof(code) = (unsigned short) oldcode;
274 tab_suffixof(code) = (unsigned char) finchar; 289 tab_suffixof(code) = (unsigned char) finchar;
275 free_ent = code + 1; 290 free_ent = code + 1;
@@ -282,7 +297,8 @@ uncompress(int fd_in, int fd_out)
282 } while (rsize > 0); 297 } while (rsize > 0);
283 298
284 if (outpos > 0) { 299 if (outpos > 0) {
285 write(fd_out, outbuf, outpos); 300 full_write(fd_out, outbuf, outpos);
301//error check??
286 USE_DESKTOP(total_written += outpos;) 302 USE_DESKTOP(total_written += outpos;)
287 } 303 }
288 304
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index b4d62f16c..7001c70f9 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -116,7 +116,9 @@ static unsigned fill_bitbuffer(unsigned bitbuffer, unsigned *current, const unsi
116 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer 116 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
117 * to the front of the bytebuffer, leave 4 bytes free at end of tail 117 * 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() */ 118 * so we can easily top up buffer in check_trailer_gzip() */
119 if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) 119 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);
120 if (1 > bytebuffer_size)
121//shouldn't we propagate error?
120 bb_error_msg_and_die("unexpected end of file"); 122 bb_error_msg_and_die("unexpected end of file");
121 bytebuffer_size += 4; 123 bytebuffer_size += 4;
122 bytebuffer_offset = 4; 124 bytebuffer_offset = 4;
@@ -193,7 +195,7 @@ int huft_build(unsigned *b, const unsigned n,
193 eob_len = n > 256 ? b[256] : BMAX; 195 eob_len = n > 256 ? b[256] : BMAX;
194 196
195 /* Generate counts for each bit length */ 197 /* Generate counts for each bit length */
196 memset((void *)c, 0, sizeof(c)); 198 memset(c, 0, sizeof(c));
197 p = b; 199 p = b;
198 i = n; 200 i = n;
199 do { 201 do {
@@ -215,11 +217,13 @@ int huft_build(unsigned *b, const unsigned n,
215 217
216 /* Adjust last length count to fill out codes, if needed */ 218 /* Adjust last length count to fill out codes, if needed */
217 for (y = 1 << j; j < i; j++, y <<= 1) { 219 for (y = 1 << j; j < i; j++, y <<= 1) {
218 if ((y -= c[j]) < 0) { 220 y -= c[j];
221 if (y < 0) {
219 return 2; /* bad input: more codes than bits */ 222 return 2; /* bad input: more codes than bits */
220 } 223 }
221 } 224 }
222 if ((y -= c[i]) < 0) { 225 y -= c[i];
226 if (y < 0) {
223 return 2; 227 return 2;
224 } 228 }
225 c[i] += y; 229 c[i] += y;
@@ -229,14 +233,16 @@ int huft_build(unsigned *b, const unsigned n,
229 p = c + 1; 233 p = c + 1;
230 xp = x + 2; 234 xp = x + 2;
231 while (--i) { /* note that i == g from above */ 235 while (--i) { /* note that i == g from above */
232 *xp++ = (j += *p++); 236 j += *p++;
237 *xp++ = j;
233 } 238 }
234 239
235 /* Make a table of values in order of bit lengths */ 240 /* Make a table of values in order of bit lengths */
236 p = b; 241 p = b;
237 i = 0; 242 i = 0;
238 do { 243 do {
239 if ((j = *p++) != 0) { 244 j = *p++;
245 if (j != 0) {
240 v[x[j]++] = i; 246 v[x[j]++] = i;
241 } 247 }
242 } while (++i < n); 248 } while (++i < n);
@@ -260,13 +266,17 @@ int huft_build(unsigned *b, const unsigned n,
260 w = ws[++htl]; 266 w = ws[++htl];
261 267
262 /* compute minimum size table less than or equal to *m bits */ 268 /* compute minimum size table less than or equal to *m bits */
263 z = (z = g - w) > *m ? *m : z; /* upper limit on table size */ 269 z = g - w;
264 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */ 270 z = z > *m ? *m : z; /* upper limit on table size */
271 j = k - w;
272 f = 1 << j;
273 if (f > a + 1) { /* try a k-w bit table */
265 /* too few codes for k-w bit table */ 274 /* too few codes for k-w bit table */
266 f -= a + 1; /* deduct codes from patterns left */ 275 f -= a + 1; /* deduct codes from patterns left */
267 xp = c + k; 276 xp = c + k;
268 while (++j < z) { /* try smaller tables up to z bits */ 277 while (++j < z) { /* try smaller tables up to z bits */
269 if ((f <<= 1) <= *++xp) { 278 f <<= 1;
279 if (f <= *++xp) {
270 break; /* enough codes to use up j bits */ 280 break; /* enough codes to use up j bits */
271 } 281 }
272 f -= *xp; /* else deduct codes from patterns */ 282 f -= *xp; /* else deduct codes from patterns */
@@ -338,6 +348,8 @@ int huft_build(unsigned *b, const unsigned n,
338 * tl, td: literal/length and distance decoder tables 348 * tl, td: literal/length and distance decoder tables
339 * bl, bd: number of bits decoded by tl[] and td[] 349 * bl, bd: number of bits decoded by tl[] and td[]
340 */ 350 */
351/* called with setup==1 once from inflate_block */
352/* called once with setup==0 from inflate_get_next_window */
341static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd, int setup) 353static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd, int setup)
342{ 354{
343 static unsigned e; /* table entry flag/number of extra bits */ 355 static unsigned e; /* table entry flag/number of extra bits */
@@ -371,7 +383,9 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
371 383
372 while (1) { /* do until end of block */ 384 while (1) { /* do until end of block */
373 b = fill_bitbuffer(b, &k, bl); 385 b = fill_bitbuffer(b, &k, bl);
374 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) 386 t = tl + ((unsigned) b & ml);
387 e = t->e;
388 if (e > 16)
375 do { 389 do {
376 if (e == 99) { 390 if (e == 99) {
377 bb_error_msg_and_die("inflate_codes error 1"); 391 bb_error_msg_and_die("inflate_codes error 1");
@@ -380,8 +394,9 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
380 k -= t->b; 394 k -= t->b;
381 e -= 16; 395 e -= 16;
382 b = fill_bitbuffer(b, &k, e); 396 b = fill_bitbuffer(b, &k, e);
383 } while ((e = 397 t = t->v.t + ((unsigned) b & mask_bits[e]);
384 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 398 e = t->e;
399 } while (e > 16);
385 b >>= t->b; 400 b >>= t->b;
386 k -= t->b; 401 k -= t->b;
387 if (e == 16) { /* then it's a literal */ 402 if (e == 16) { /* then it's a literal */
@@ -407,7 +422,9 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
407 422
408 /* decode distance of block to copy */ 423 /* decode distance of block to copy */
409 b = fill_bitbuffer(b, &k, bd); 424 b = fill_bitbuffer(b, &k, bd);
410 if ((e = (t = td + ((unsigned) b & md))->e) > 16) 425 t = td + ((unsigned) b & md);
426 e = t->e;
427 if (e > 16)
411 do { 428 do {
412 if (e == 99) 429 if (e == 99)
413 bb_error_msg_and_die("inflate_codes error 2"); 430 bb_error_msg_and_die("inflate_codes error 2");
@@ -415,9 +432,9 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
415 k -= t->b; 432 k -= t->b;
416 e -= 16; 433 e -= 16;
417 b = fill_bitbuffer(b, &k, e); 434 b = fill_bitbuffer(b, &k, e);
418 } while ((e = 435 t = t->v.t + ((unsigned) b & mask_bits[e]);
419 (t = 436 e = t->e;
420 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 437 } while (e > 16);
421 b >>= t->b; 438 b >>= t->b;
422 k -= t->b; 439 k -= t->b;
423 b = fill_bitbuffer(b, &k, e); 440 b = fill_bitbuffer(b, &k, e);
@@ -426,26 +443,30 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
426 k -= e; 443 k -= e;
427 444
428 /* do the copy */ 445 /* do the copy */
429do_copy: do { 446 do_copy:
430 n -= (e = 447 do {
431 (e = 448 /* Was: n -= (e = (e = gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); */
432 gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); 449 /* Who wrote THAT?? rewritten as: */
433 /* copy to new buffer to prevent possible overwrite */ 450 d &= gunzip_wsize - 1;
451 e = gunzip_wsize - (d > w ? d : w);
452 if (e > n) e = n;
453 n -= e;
454
455 /* copy to new buffer to prevent possible overwrite */
434 if (w - d >= e) { /* (this test assumes unsigned comparison) */ 456 if (w - d >= e) { /* (this test assumes unsigned comparison) */
435 memcpy(gunzip_window + w, gunzip_window + d, e); 457 memcpy(gunzip_window + w, gunzip_window + d, e);
436 w += e; 458 w += e;
437 d += e; 459 d += e;
438 } else { 460 } else {
439 /* do it slow to avoid memcpy() overlap */ 461 /* do it slow to avoid memcpy() overlap */
440 /* !NOMEMCPY */ 462 /* !NOMEMCPY */
441 do { 463 do {
442 gunzip_window[w++] = gunzip_window[d++]; 464 gunzip_window[w++] = gunzip_window[d++];
443 } while (--e); 465 } while (--e);
444 } 466 }
445 if (w == gunzip_wsize) { 467 if (w == gunzip_wsize) {
446 gunzip_outbuf_count = (w); 468 gunzip_outbuf_count = (w);
447 if (n) resumeCopy = 1; 469 resumeCopy = (n != 0);
448 else resumeCopy = 0;
449 //flush_gunzip_window(); 470 //flush_gunzip_window();
450 w = 0; 471 w = 0;
451 return 1; 472 return 1;
@@ -469,9 +490,12 @@ do_copy: do {
469 return 0; 490 return 0;
470} 491}
471 492
493/* called once (setup==1) from inflate_block */
494/* and once (setup==0) from inflate_get_next_window */
472static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) 495static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
473{ 496{
474 static unsigned n, b_stored, k_stored, w; 497 static unsigned n, b_stored, k_stored, w;
498
475 if (setup) { 499 if (setup) {
476 n = my_n; 500 n = my_n;
477 b_stored = my_b_stored; 501 b_stored = my_b_stored;
@@ -509,7 +533,8 @@ static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
509 * 533 *
510 * GLOBAL VARIABLES: bb, kk, 534 * GLOBAL VARIABLES: bb, kk,
511 */ 535 */
512 // Return values: -1 = inflate_stored, -2 = inflate_codes 536/* Return values: -1 = inflate_stored, -2 = inflate_codes */
537/* One callsite in inflate_get_next_window */
513static int inflate_block(int *e) 538static int inflate_block(int *e)
514{ 539{
515 unsigned t; /* block type */ 540 unsigned t; /* block type */
@@ -597,7 +622,8 @@ static int inflate_block(int *e)
597 l[i] = 8; 622 l[i] = 8;
598 } 623 }
599 bl = 7; 624 bl = 7;
600 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { 625 i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl);
626 if (i != 0) {
601 return i; 627 return i;
602 } 628 }
603 629
@@ -606,7 +632,8 @@ static int inflate_block(int *e)
606 l[i] = 5; 632 l[i] = 5;
607 } 633 }
608 bd = 5; 634 bd = 5;
609 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { 635 i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd);
636 if (i > 1) {
610 huft_free(tl); 637 huft_free(tl);
611 return i; 638 return i;
612 } 639 }
@@ -745,19 +772,21 @@ static int inflate_block(int *e)
745 /* build the decoding tables for literal/length and distance codes */ 772 /* build the decoding tables for literal/length and distance codes */
746 bl = lbits; 773 bl = lbits;
747 774
748 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { 775 i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
776 if (i != 0) {
749 if (i == 1) { 777 if (i == 1) {
750 bb_error_msg_and_die("incomplete literal tree"); 778 bb_error_msg_and_die("incomplete literal tree");
751 huft_free(tl); 779 /* huft_free(tl); */
752 } 780 }
753 return i; /* incomplete code set */ 781 return i; /* incomplete code set */
754 } 782 }
755 783
756 bd = dbits; 784 bd = dbits;
757 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { 785 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
786 if (i != 0) {
758 if (i == 1) { 787 if (i == 1) {
759 bb_error_msg_and_die("incomplete distance tree"); 788 bb_error_msg_and_die("incomplete distance tree");
760 huft_free(td); 789 /* huft_free(td); */
761 } 790 }
762 huft_free(tl); 791 huft_free(tl);
763 return i; /* incomplete code set */ 792 return i; /* incomplete code set */
@@ -776,6 +805,7 @@ static int inflate_block(int *e)
776 } 805 }
777} 806}
778 807
808/* Two callsites, both in inflate_get_next_window */
779static void calculate_gunzip_crc(void) 809static void calculate_gunzip_crc(void)
780{ 810{
781 int n; 811 int n;
@@ -785,6 +815,7 @@ static void calculate_gunzip_crc(void)
785 gunzip_bytes_out += gunzip_outbuf_count; 815 gunzip_bytes_out += gunzip_outbuf_count;
786} 816}
787 817
818/* One callsite in inflate_unzip */
788static int inflate_get_next_window(void) 819static int inflate_get_next_window(void)
789{ 820{
790 static int method = -1; // Method == -1 for stored, -2 for codes 821 static int method = -1; // Method == -1 for stored, -2 for codes
@@ -823,7 +854,8 @@ static int inflate_get_next_window(void)
823 /* Doesnt get here */ 854 /* Doesnt get here */
824} 855}
825 856
826/* Initialise bytebuffer, be careful not to overfill the buffer */ 857/* Initialize bytebuffer, be careful not to overfill the buffer */
858/* Called from archival/unzip.c */
827void inflate_init(unsigned bufsize) 859void inflate_init(unsigned bufsize)
828{ 860{
829 /* Set the bytebuffer size, default is same as gunzip_wsize */ 861 /* Set the bytebuffer size, default is same as gunzip_wsize */
@@ -832,11 +864,13 @@ void inflate_init(unsigned bufsize)
832 bytebuffer_size = 0; 864 bytebuffer_size = 0;
833} 865}
834 866
867/* Called from archival/unzip.c */
835void inflate_cleanup(void) 868void inflate_cleanup(void)
836{ 869{
837 free(bytebuffer); 870 free(bytebuffer);
838} 871}
839 872
873/* Called from inflate_gunzip() and archival/unzip.c */
840USE_DESKTOP(long long) int 874USE_DESKTOP(long long) int
841inflate_unzip(int in, int out) 875inflate_unzip(int in, int out)
842{ 876{
@@ -864,7 +898,7 @@ inflate_unzip(int in, int out)
864 while (1) { 898 while (1) {
865 int ret = inflate_get_next_window(); 899 int ret = inflate_get_next_window();
866 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count); 900 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
867 if (nwrote == -1) { 901 if (nwrote != gunzip_outbuf_count) {
868 bb_perror_msg("write"); 902 bb_perror_msg("write");
869 return -1; 903 return -1;
870 } 904 }
@@ -901,6 +935,7 @@ inflate_gunzip(int in, int out)
901 count = bytebuffer_size - bytebuffer_offset; 935 count = bytebuffer_size - bytebuffer_offset;
902 if (count < 8) { 936 if (count < 8) {
903 xread(in, &bytebuffer[bytebuffer_size], 8 - count); 937 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
938//shouldn't we propagate error?
904 bytebuffer_size += 8 - count; 939 bytebuffer_size += 8 - count;
905 } 940 }
906 for (count = 0; count != 4; count++) { 941 for (count = 0; count != 4; count++) {
@@ -917,7 +952,8 @@ inflate_gunzip(int in, int out)
917 /* Validate decompression - size */ 952 /* Validate decompression - size */
918 if (gunzip_bytes_out != 953 if (gunzip_bytes_out !=
919 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | 954 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
920 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) { 955 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))
956 ) {
921 bb_error_msg("incorrect length"); 957 bb_error_msg("incorrect length");
922 return -1; 958 return -1;
923 } 959 }