aboutsummaryrefslogtreecommitdiff
path: root/archival/libarchive
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2020-01-08 12:30:49 +0000
committerRon Yorston <rmy@pobox.com>2020-01-08 12:30:49 +0000
commita9271a8e97e6e7be5285330d5f19352decabf807 (patch)
treebf3c4464c369a15a46454792dac167505f74769f /archival/libarchive
parentb0b7ab792bc1f45963f4b84b94faaf05054e1613 (diff)
parent9ec836c033fc6e55e80f3309b3e05acdf09bb297 (diff)
downloadbusybox-w32-a9271a8e97e6e7be5285330d5f19352decabf807.tar.gz
busybox-w32-a9271a8e97e6e7be5285330d5f19352decabf807.tar.bz2
busybox-w32-a9271a8e97e6e7be5285330d5f19352decabf807.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'archival/libarchive')
-rw-r--r--archival/libarchive/decompress_gunzip.c111
-rw-r--r--archival/libarchive/decompress_unxz.c20
-rw-r--r--archival/libarchive/unxz/xz_stream.h2
3 files changed, 79 insertions, 54 deletions
diff --git a/archival/libarchive/decompress_gunzip.c b/archival/libarchive/decompress_gunzip.c
index cf07f71df..c0332d414 100644
--- a/archival/libarchive/decompress_gunzip.c
+++ b/archival/libarchive/decompress_gunzip.c
@@ -39,7 +39,8 @@ typedef struct huft_t {
39 unsigned char e; /* number of extra bits or operation */ 39 unsigned char e; /* number of extra bits or operation */
40 unsigned char b; /* number of bits in this code or subcode */ 40 unsigned char b; /* number of bits in this code or subcode */
41 union { 41 union {
42 unsigned short n; /* literal, length base, or distance base */ 42 unsigned n; /* literal, length base, or distance base */
43 /* ^^^^^ was "unsigned short", but that results in larger code */
43 struct huft_t *t; /* pointer to next level of table */ 44 struct huft_t *t; /* pointer to next level of table */
44 } v; 45 } v;
45} huft_t; 46} huft_t;
@@ -184,29 +185,26 @@ static const uint16_t mask_bits[] ALIGN2 = {
184 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 185 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
185}; 186};
186 187
187/* Copy lengths for literal codes 257..285 */ 188/* Put lengths/offsets and extra bits in a struct of arrays
188static const uint16_t cplens[] ALIGN2 = { 189 * to make calls to huft_build() have one fewer parameter.
189 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 190 */
190 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 191struct cp_ext {
192 uint16_t cp[31];
193 uint8_t ext[31];
191}; 194};
192 195/* Copy lengths and extra bits for literal codes 257..285 */
193/* note: see note #13 above about the 258 in this list. */ 196/* note: see note #13 above about the 258 in this list. */
194/* Extra bits for literal codes 257..285 */ 197static const struct cp_ext lit = {
195static const uint8_t cplext[] ALIGN1 = { 198 /*257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 */
196 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 199 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 */
197 5, 5, 5, 0, 99, 99 200 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 },
198}; /* 99 == invalid */ 201 { 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, 5, 5, 5, 0, 99, 99 } /* 99 == invalid */
199
200/* Copy offsets for distance codes 0..29 */
201static const uint16_t cpdist[] ALIGN2 = {
202 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
203 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
204}; 202};
205 203/* Copy offsets and extra bits for distance codes 0..29 */
206/* Extra bits for distance codes */ 204static const struct cp_ext dist = {
207static const uint8_t cpdext[] ALIGN1 = { 205 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */
208 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 206 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 },
209 11, 11, 12, 12, 13, 13 207 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }
210}; 208};
211 209
212/* Tables for deflate from PKZIP's appnote.txt. */ 210/* Tables for deflate from PKZIP's appnote.txt. */
@@ -277,22 +275,25 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current
277 275
278 276
279/* Given a list of code lengths and a maximum table size, make a set of 277/* Given a list of code lengths and a maximum table size, make a set of
280 * tables to decode that set of codes. Return zero on success, one if 278 * tables to decode that set of codes.
281 * the given code set is incomplete (the tables are still built in this
282 * case), two if the input is invalid (an oversubscribed set of lengths)
283 * - in this case stores NULL in *t.
284 * 279 *
285 * b: code lengths in bits (all assumed <= BMAX) 280 * b: code lengths in bits (all assumed <= BMAX)
286 * n: number of codes (assumed <= N_MAX) 281 * n: number of codes (assumed <= N_MAX)
287 * s: number of simple-valued codes (0..s-1) 282 * s: number of simple-valued codes (0..s-1)
288 * d: list of base values for non-simple codes 283 * cp_ext->cp,ext: list of base values/extra bits for non-simple codes
289 * e: list of extra bits for non-simple codes
290 * t: result: starting table
291 * m: maximum lookup bits, returns actual 284 * m: maximum lookup bits, returns actual
285 * result: starting table
286 *
287 * On error, returns a value with lowest-bit set on error.
288 * It can be just the value of 0x1,
289 * or a valid pointer to a Huffman table, ORed with 0x1 if incompete table
290 * is given: "fixed inflate" decoder feeds us such data.
292 */ 291 */
293static int huft_build(const unsigned *b, const unsigned n, 292#define BAD_HUFT(p) ((uintptr_t)(p) & 1)
294 const unsigned s, const unsigned short *d, 293#define ERR_RET ((huft_t*)(uintptr_t)1)
295 const unsigned char *e, huft_t **t, unsigned *m) 294static huft_t* huft_build(const unsigned *b, const unsigned n,
295 const unsigned s, const struct cp_ext *cp_ext,
296 unsigned *m)
296{ 297{
297 unsigned a; /* counter for codes of length k */ 298 unsigned a; /* counter for codes of length k */
298 unsigned c[BMAX + 1]; /* bit length count table */ 299 unsigned c[BMAX + 1]; /* bit length count table */
@@ -314,12 +315,12 @@ static int huft_build(const unsigned *b, const unsigned n,
314 unsigned *xp; /* pointer into x */ 315 unsigned *xp; /* pointer into x */
315 int y; /* number of dummy codes added */ 316 int y; /* number of dummy codes added */
316 unsigned z; /* number of entries in current table */ 317 unsigned z; /* number of entries in current table */
318 huft_t *result;
319 huft_t **t;
317 320
318 /* Length of EOB code, if any */ 321 /* Length of EOB code, if any */
319 eob_len = n > 256 ? b[256] : BMAX; 322 eob_len = n > 256 ? b[256] : BMAX;
320 323
321 *t = NULL;
322
323 /* Generate counts for each bit length */ 324 /* Generate counts for each bit length */
324 memset(c, 0, sizeof(c)); 325 memset(c, 0, sizeof(c));
325 p = b; 326 p = b;
@@ -335,9 +336,8 @@ static int huft_build(const unsigned *b, const unsigned n,
335 q[1].b = 1; 336 q[1].b = 1;
336 q[2].e = 99; /* invalid code marker */ 337 q[2].e = 99; /* invalid code marker */
337 q[2].b = 1; 338 q[2].b = 1;
338 *t = q + 1;
339 *m = 1; 339 *m = 1;
340 return 0; 340 return q + 1;
341 } 341 }
342 342
343 /* Find minimum and maximum length, bound *m by those */ 343 /* Find minimum and maximum length, bound *m by those */
@@ -353,11 +353,11 @@ static int huft_build(const unsigned *b, const unsigned n,
353 for (y = 1 << j; j < i; j++, y <<= 1) { 353 for (y = 1 << j; j < i; j++, y <<= 1) {
354 y -= c[j]; 354 y -= c[j];
355 if (y < 0) 355 if (y < 0)
356 return 2; /* bad input: more codes than bits */ 356 return ERR_RET; /* bad input: more codes than bits */
357 } 357 }
358 y -= c[i]; 358 y -= c[i];
359 if (y < 0) 359 if (y < 0)
360 return 2; 360 return ERR_RET;
361 c[i] += y; 361 c[i] += y;
362 362
363 /* Generate starting offsets into the value table for each length */ 363 /* Generate starting offsets into the value table for each length */
@@ -384,6 +384,8 @@ static int huft_build(const unsigned *b, const unsigned n,
384 } while (++i < n); 384 } while (++i < n);
385 385
386 /* Generate the Huffman codes and for each, make the table entries */ 386 /* Generate the Huffman codes and for each, make the table entries */
387 result = ERR_RET;
388 t = &result;
387 x[0] = i = 0; /* first Huffman code is zero */ 389 x[0] = i = 0; /* first Huffman code is zero */
388 p = v; /* grab values in bit order */ 390 p = v; /* grab values in bit order */
389 htl = -1; /* no tables yet--level -1 */ 391 htl = -1; /* no tables yet--level -1 */
@@ -449,8 +451,8 @@ static int huft_build(const unsigned *b, const unsigned n,
449 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ 451 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
450 r.v.n = (unsigned short) (*p++); /* simple code is just the value */ 452 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
451 } else { 453 } else {
452 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ 454 r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */
453 r.v.n = d[*p++ - s]; 455 r.v.n = cp_ext->cp[*p++ - s];
454 } 456 }
455 457
456 /* fill code-like entries with r */ 458 /* fill code-like entries with r */
@@ -475,8 +477,11 @@ static int huft_build(const unsigned *b, const unsigned n,
475 /* return actual size of base table */ 477 /* return actual size of base table */
476 *m = ws[1]; 478 *m = ws[1];
477 479
478 /* Return 1 if we were given an incomplete table */ 480 if (y != 0 && g != 1) /* we were given an incomplete table */
479 return y != 0 && g != 1; 481 /* return "result" ORed with 1 */
482 return (void*)((uintptr_t)result | 1);
483
484 return result;
480} 485}
481 486
482 487
@@ -777,14 +782,17 @@ static int inflate_block(STATE_PARAM smallint *e)
777 for (; i < 288; i++) /* make a complete, but wrong code set */ 782 for (; i < 288; i++) /* make a complete, but wrong code set */
778 ll[i] = 8; 783 ll[i] = 8;
779 bl = 7; 784 bl = 7;
780 huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl); 785 inflate_codes_tl = huft_build(ll, 288, 257, &lit, &bl);
781 /* huft_build() never return nonzero - we use known data */ 786 /* ^^^ never returns error here - we use known data */
782 787
783 /* set up distance table */ 788 /* set up distance table */
784 for (i = 0; i < 30; i++) /* make an incomplete code set */ 789 for (i = 0; i < 30; i++) /* make an incomplete code set */
785 ll[i] = 5; 790 ll[i] = 5;
786 bd = 5; 791 bd = 5;
787 huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd); 792 inflate_codes_td = huft_build(ll, 30, 0, &dist, &bd);
793 /* ^^^ does return error here! (lsb bit is set) - we gave it incomplete code set */
794 /* clearing error bit: */
795 inflate_codes_td = (void*)((uintptr_t)inflate_codes_td & ~(uintptr_t)1);
788 796
789 /* set up data for inflate_codes() */ 797 /* set up data for inflate_codes() */
790 inflate_codes_setup(PASS_STATE bl, bd); 798 inflate_codes_setup(PASS_STATE bl, bd);
@@ -850,9 +858,9 @@ static int inflate_block(STATE_PARAM smallint *e)
850 858
851 /* build decoding table for trees - single level, 7 bit lookup */ 859 /* build decoding table for trees - single level, 7 bit lookup */
852 bl = 7; 860 bl = 7;
853 i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl); 861 inflate_codes_tl = huft_build(ll, 19, 19, NULL, &bl);
854 if (i != 0) { 862 if (BAD_HUFT(inflate_codes_tl)) {
855 abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */ 863 abort_unzip(PASS_STATE_ONLY); /* incomplete code set */
856 } 864 }
857 865
858 /* read in literal and distance code lengths */ 866 /* read in literal and distance code lengths */
@@ -915,14 +923,13 @@ static int inflate_block(STATE_PARAM smallint *e)
915 923
916 /* build the decoding tables for literal/length and distance codes */ 924 /* build the decoding tables for literal/length and distance codes */
917 bl = lbits; 925 bl = lbits;
918 926 inflate_codes_tl = huft_build(ll, nl, 257, &lit, &bl);
919 i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl); 927 if (BAD_HUFT(inflate_codes_tl)) {
920 if (i != 0) {
921 abort_unzip(PASS_STATE_ONLY); 928 abort_unzip(PASS_STATE_ONLY);
922 } 929 }
923 bd = dbits; 930 bd = dbits;
924 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd); 931 inflate_codes_td = huft_build(ll + nl, nd, 0, &dist, &bd);
925 if (i != 0) { 932 if (BAD_HUFT(inflate_codes_td)) {
926 abort_unzip(PASS_STATE_ONLY); 933 abort_unzip(PASS_STATE_ONLY);
927 } 934 }
928 935
diff --git a/archival/libarchive/decompress_unxz.c b/archival/libarchive/decompress_unxz.c
index f03341384..3dd9bbf49 100644
--- a/archival/libarchive/decompress_unxz.c
+++ b/archival/libarchive/decompress_unxz.c
@@ -96,6 +96,24 @@ unpack_xz_stream(transformer_state_t *xstate)
96 */ 96 */
97 do { 97 do {
98 if (membuf[iobuf.in_pos] != 0) { 98 if (membuf[iobuf.in_pos] != 0) {
99 /* There is more data, but is it XZ data?
100 * Example: dpkg-deb -f busybox_1.30.1-4_amd64.deb
101 * reads control.tar.xz "control" file
102 * inside the ar archive, but tar.xz
103 * extraction code reaches end of xz data,
104 * reached this code and reads the beginning
105 * of data.tar.xz's ar header, which isn't xz data,
106 * and prints "corrupted data".
107 * The correct solution is to not read
108 * past nested archive (to simulate EOF).
109 * This is a workaround:
110 */
111 if (membuf[iobuf.in_pos] != 0xfd) {
112 /* It's definitely not a xz signature
113 * (which is 0xfd,"7zXZ",0x00).
114 */
115 goto end;
116 }
99 xz_dec_reset(state); 117 xz_dec_reset(state);
100 goto do_run; 118 goto do_run;
101 } 119 }
@@ -128,7 +146,7 @@ unpack_xz_stream(transformer_state_t *xstate)
128 break; 146 break;
129 } 147 }
130 } 148 }
131 149 end:
132 xz_dec_end(state); 150 xz_dec_end(state);
133 free(membuf); 151 free(membuf);
134 152
diff --git a/archival/libarchive/unxz/xz_stream.h b/archival/libarchive/unxz/xz_stream.h
index 66cb5a705..45056e42c 100644
--- a/archival/libarchive/unxz/xz_stream.h
+++ b/archival/libarchive/unxz/xz_stream.h
@@ -25,7 +25,7 @@
25 25
26#define STREAM_HEADER_SIZE 12 26#define STREAM_HEADER_SIZE 12
27 27
28#define HEADER_MAGIC "\3757zXZ" 28#define HEADER_MAGIC "\375""7zXZ"
29#define HEADER_MAGIC_SIZE 6 29#define HEADER_MAGIC_SIZE 6
30 30
31#define FOOTER_MAGIC "YZ" 31#define FOOTER_MAGIC "YZ"