aboutsummaryrefslogtreecommitdiff
path: root/archival/libarchive
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-02-13 09:44:44 +0000
committerRon Yorston <rmy@pobox.com>2018-02-13 09:44:44 +0000
commitdc19a361bd6c6df30338371532691bbc7f7126bb (patch)
tree1fb2cd646d54b5f8e425c4f11f3e09fc21d1966b /archival/libarchive
parent096aee2bb468d1ab044de36e176ed1f6c7e3674d (diff)
parent3459024bf404af814cacfe90a0deb719e282ae62 (diff)
downloadbusybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.tar.gz
busybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.tar.bz2
busybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'archival/libarchive')
-rw-r--r--archival/libarchive/bz/blocksort.c311
-rw-r--r--archival/libarchive/bz/bzlib.c20
-rw-r--r--archival/libarchive/bz/bzlib_private.h28
-rw-r--r--archival/libarchive/bz/compress.c329
-rw-r--r--archival/libarchive/bz/huffman.c4
-rw-r--r--archival/libarchive/decompress_gunzip.c17
-rw-r--r--archival/libarchive/decompress_unxz.c2
-rw-r--r--archival/libarchive/get_header_ar.c4
-rw-r--r--archival/libarchive/get_header_tar.c49
-rw-r--r--archival/libarchive/lzo1x_d.c3
10 files changed, 430 insertions, 337 deletions
diff --git a/archival/libarchive/bz/blocksort.c b/archival/libarchive/bz/blocksort.c
index e600cb7a7..92d6d8251 100644
--- a/archival/libarchive/bz/blocksort.c
+++ b/archival/libarchive/bz/blocksort.c
@@ -113,9 +113,8 @@ void fallbackQSort3(uint32_t* fmap,
113 int32_t loSt, 113 int32_t loSt,
114 int32_t hiSt) 114 int32_t hiSt)
115{ 115{
116 int32_t unLo, unHi, ltLo, gtHi, n, m; 116 int32_t sp;
117 int32_t sp, lo, hi; 117 uint32_t r;
118 uint32_t med, r, r3;
119 int32_t stackLo[FALLBACK_QSORT_STACK_SIZE]; 118 int32_t stackLo[FALLBACK_QSORT_STACK_SIZE];
120 int32_t stackHi[FALLBACK_QSORT_STACK_SIZE]; 119 int32_t stackHi[FALLBACK_QSORT_STACK_SIZE];
121 120
@@ -125,6 +124,11 @@ void fallbackQSort3(uint32_t* fmap,
125 fpush(loSt, hiSt); 124 fpush(loSt, hiSt);
126 125
127 while (sp > 0) { 126 while (sp > 0) {
127 int32_t unLo, unHi, ltLo, gtHi, n, m;
128 int32_t lo, hi;
129 uint32_t med;
130 uint32_t r3;
131
128 AssertH(sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004); 132 AssertH(sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004);
129 133
130 fpop(lo, hi); 134 fpop(lo, hi);
@@ -161,7 +165,7 @@ void fallbackQSort3(uint32_t* fmap,
161 ltLo++; 165 ltLo++;
162 unLo++; 166 unLo++;
163 continue; 167 continue;
164 }; 168 }
165 if (n > 0) break; 169 if (n > 0) break;
166 unLo++; 170 unLo++;
167 } 171 }
@@ -172,7 +176,7 @@ void fallbackQSort3(uint32_t* fmap,
172 mswap(fmap[unHi], fmap[gtHi]); 176 mswap(fmap[unHi], fmap[gtHi]);
173 gtHi--; unHi--; 177 gtHi--; unHi--;
174 continue; 178 continue;
175 }; 179 }
176 if (n < 0) break; 180 if (n < 0) break;
177 unHi--; 181 unHi--;
178 } 182 }
@@ -227,17 +231,19 @@ void fallbackQSort3(uint32_t* fmap,
227#define UNALIGNED_BH(zz) ((zz) & 0x01f) 231#define UNALIGNED_BH(zz) ((zz) & 0x01f)
228 232
229static 233static
230void fallbackSort(uint32_t* fmap, 234void fallbackSort(EState* state)
231 uint32_t* eclass,
232 uint32_t* bhtab,
233 int32_t nblock)
234{ 235{
235 int32_t ftab[257]; 236 int32_t ftab[257];
236 int32_t ftabCopy[256]; 237 int32_t ftabCopy[256];
237 int32_t H, i, j, k, l, r, cc, cc1; 238 int32_t H, i, j, k, l, r, cc, cc1;
238 int32_t nNotDone; 239 int32_t nNotDone;
239 int32_t nBhtab; 240 int32_t nBhtab;
240 uint8_t* eclass8 = (uint8_t*)eclass; 241 /* params */
242 uint32_t *const fmap = state->arr1;
243 uint32_t *const eclass = state->arr2;
244#define eclass8 ((uint8_t*)eclass)
245 uint32_t *const bhtab = state->ftab;
246 const int32_t nblock = state->nblock;
241 247
242 /* 248 /*
243 * Initial 1-char radix sort to generate 249 * Initial 1-char radix sort to generate
@@ -326,7 +332,7 @@ void fallbackSort(uint32_t* fmap,
326 if (cc != cc1) { 332 if (cc != cc1) {
327 SET_BH(i); 333 SET_BH(i);
328 cc = cc1; 334 cc = cc1;
329 }; 335 }
330 } 336 }
331 } 337 }
332 } 338 }
@@ -349,6 +355,7 @@ void fallbackSort(uint32_t* fmap,
349 eclass8[fmap[i]] = (uint8_t)j; 355 eclass8[fmap[i]] = (uint8_t)j;
350 } 356 }
351 AssertH(j < 256, 1005); 357 AssertH(j < 256, 1005);
358#undef eclass8
352} 359}
353 360
354#undef SET_BH 361#undef SET_BH
@@ -367,25 +374,25 @@ void fallbackSort(uint32_t* fmap,
367/*---------------------------------------------*/ 374/*---------------------------------------------*/
368static 375static
369NOINLINE 376NOINLINE
370int mainGtU( 377int mainGtU(EState* state,
371 uint32_t i1, 378 uint32_t i1,
372 uint32_t i2, 379 uint32_t i2)
373 uint8_t* block,
374 uint16_t* quadrant,
375 uint32_t nblock,
376 int32_t* budget)
377{ 380{
378 int32_t k; 381 int32_t k;
379 uint8_t c1, c2; 382 uint8_t c1, c2;
380 uint16_t s1, s2; 383 uint16_t s1, s2;
381 384
385 uint8_t *const block = state->block;
386 uint16_t *const quadrant = state->quadrant;
387 const int32_t nblock = state->nblock;
388
382/* Loop unrolling here is actually very useful 389/* Loop unrolling here is actually very useful
383 * (generated code is much simpler), 390 * (generated code is much simpler),
384 * code size increase is only 270 bytes (i386) 391 * code size increase is only 270 bytes (i386)
385 * but speeds up compression 10% overall 392 * but speeds up compression 10% overall
386 */ 393 */
387 394
388#if CONFIG_BZIP2_FAST >= 1 395#if BZIP2_SPEED >= 1
389 396
390#define TIMES_8(code) \ 397#define TIMES_8(code) \
391 code; code; code; code; \ 398 code; code; code; code; \
@@ -435,7 +442,7 @@ int mainGtU(
435 if (i1 >= nblock) i1 -= nblock; 442 if (i1 >= nblock) i1 -= nblock;
436 if (i2 >= nblock) i2 -= nblock; 443 if (i2 >= nblock) i2 -= nblock;
437 444
438 (*budget)--; 445 state->budget--;
439 k -= 8; 446 k -= 8;
440 } while (k >= 0); 447 } while (k >= 0);
441 448
@@ -452,42 +459,45 @@ int mainGtU(
452 * usually small, typically <= 20. 459 * usually small, typically <= 20.
453 */ 460 */
454static 461static
455const int32_t incs[14] = { 462const uint32_t incs[14] = {
456 1, 4, 13, 40, 121, 364, 1093, 3280, 463 1, 4, 13, 40, 121, 364, 1093, 3280,
457 9841, 29524, 88573, 265720, 464 9841, 29524, 88573, 265720,
458 797161, 2391484 465 797161, 2391484
459}; 466};
460 467
461static 468static
462void mainSimpleSort(uint32_t* ptr, 469void mainSimpleSort(EState* state,
463 uint8_t* block,
464 uint16_t* quadrant,
465 int32_t nblock,
466 int32_t lo, 470 int32_t lo,
467 int32_t hi, 471 int32_t hi,
468 int32_t d, 472 int32_t d)
469 int32_t* budget)
470{ 473{
471 int32_t i, j, h, bigN, hp; 474 uint32_t *const ptr = state->ptr;
472 uint32_t v;
473
474 bigN = hi - lo + 1;
475 if (bigN < 2) return;
476 475
477 hp = 0; 476 /* At which increment to start? */
478 while (incs[hp] < bigN) hp++; 477 int hp = 0;
479 hp--; 478 {
479 int bigN = hi - lo;
480 if (bigN <= 0)
481 return;
482 while (incs[hp] <= bigN)
483 hp++;
484 hp--;
485 }
480 486
481 for (; hp >= 0; hp--) { 487 for (; hp >= 0; hp--) {
482 h = incs[hp]; 488 int32_t i;
489 unsigned h;
483 490
491 h = incs[hp];
484 i = lo + h; 492 i = lo + h;
485 while (1) { 493 while (1) {
486 /*-- copy 1 --*/ 494 unsigned j;
495 unsigned v;
496
487 if (i > hi) break; 497 if (i > hi) break;
488 v = ptr[i]; 498 v = ptr[i];
489 j = i; 499 j = i;
490 while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) { 500 while (mainGtU(state, ptr[j-h]+d, v+d)) {
491 ptr[j] = ptr[j-h]; 501 ptr[j] = ptr[j-h];
492 j = j - h; 502 j = j - h;
493 if (j <= (lo + h - 1)) break; 503 if (j <= (lo + h - 1)) break;
@@ -496,24 +506,23 @@ void mainSimpleSort(uint32_t* ptr,
496 i++; 506 i++;
497 507
498/* 1.5% overall speedup, +290 bytes */ 508/* 1.5% overall speedup, +290 bytes */
499#if CONFIG_BZIP2_FAST >= 3 509#if BZIP2_SPEED >= 3
500 /*-- copy 2 --*/ 510 /*-- copy 2 --*/
501 if (i > hi) break; 511 if (i > hi) break;
502 v = ptr[i]; 512 v = ptr[i];
503 j = i; 513 j = i;
504 while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) { 514 while (mainGtU(state, ptr[j-h]+d, v+d)) {
505 ptr[j] = ptr[j-h]; 515 ptr[j] = ptr[j-h];
506 j = j - h; 516 j = j - h;
507 if (j <= (lo + h - 1)) break; 517 if (j <= (lo + h - 1)) break;
508 } 518 }
509 ptr[j] = v; 519 ptr[j] = v;
510 i++; 520 i++;
511
512 /*-- copy 3 --*/ 521 /*-- copy 3 --*/
513 if (i > hi) break; 522 if (i > hi) break;
514 v = ptr[i]; 523 v = ptr[i];
515 j = i; 524 j = i;
516 while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) { 525 while (mainGtU(state, ptr[j-h]+d, v+d)) {
517 ptr[j] = ptr[j-h]; 526 ptr[j] = ptr[j-h];
518 j = j - h; 527 j = j - h;
519 if (j <= (lo + h - 1)) break; 528 if (j <= (lo + h - 1)) break;
@@ -521,7 +530,7 @@ void mainSimpleSort(uint32_t* ptr,
521 ptr[j] = v; 530 ptr[j] = v;
522 i++; 531 i++;
523#endif 532#endif
524 if (*budget < 0) return; 533 if (state->budget < 0) return;
525 } 534 }
526 } 535 }
527} 536}
@@ -545,7 +554,7 @@ uint8_t mmed3(uint8_t a, uint8_t b, uint8_t c)
545 t = a; 554 t = a;
546 a = b; 555 a = b;
547 b = t; 556 b = t;
548 }; 557 }
549 /* here b >= a */ 558 /* here b >= a */
550 if (b > c) { 559 if (b > c) {
551 b = c; 560 b = c;
@@ -586,15 +595,12 @@ uint8_t mmed3(uint8_t a, uint8_t b, uint8_t c)
586#define MAIN_QSORT_STACK_SIZE 100 595#define MAIN_QSORT_STACK_SIZE 100
587 596
588static NOINLINE 597static NOINLINE
589void mainQSort3(uint32_t* ptr, 598void mainQSort3(EState* state,
590 uint8_t* block,
591 uint16_t* quadrant,
592 int32_t nblock,
593 int32_t loSt, 599 int32_t loSt,
594 int32_t hiSt, 600 int32_t hiSt
595 int32_t dSt, 601 /*int32_t dSt*/)
596 int32_t* budget)
597{ 602{
603 enum { dSt = BZ_N_RADIX };
598 int32_t unLo, unHi, ltLo, gtHi, n, m, med; 604 int32_t unLo, unHi, ltLo, gtHi, n, m, med;
599 int32_t sp, lo, hi, d; 605 int32_t sp, lo, hi, d;
600 606
@@ -606,6 +612,9 @@ void mainQSort3(uint32_t* ptr,
606 int32_t nextHi[3]; 612 int32_t nextHi[3];
607 int32_t nextD [3]; 613 int32_t nextD [3];
608 614
615 uint32_t *const ptr = state->ptr;
616 uint8_t *const block = state->block;
617
609 sp = 0; 618 sp = 0;
610 mpush(loSt, hiSt, dSt); 619 mpush(loSt, hiSt, dSt);
611 620
@@ -616,8 +625,8 @@ void mainQSort3(uint32_t* ptr,
616 if (hi - lo < MAIN_QSORT_SMALL_THRESH 625 if (hi - lo < MAIN_QSORT_SMALL_THRESH
617 || d > MAIN_QSORT_DEPTH_THRESH 626 || d > MAIN_QSORT_DEPTH_THRESH
618 ) { 627 ) {
619 mainSimpleSort(ptr, block, quadrant, nblock, lo, hi, d, budget); 628 mainSimpleSort(state, lo, hi, d);
620 if (*budget < 0) 629 if (state->budget < 0)
621 return; 630 return;
622 continue; 631 continue;
623 } 632 }
@@ -638,8 +647,8 @@ void mainQSort3(uint32_t* ptr,
638 ltLo++; 647 ltLo++;
639 unLo++; 648 unLo++;
640 continue; 649 continue;
641 }; 650 }
642 if (n > 0) break; 651 if (n > 0) break;
643 unLo++; 652 unLo++;
644 } 653 }
645 while (1) { 654 while (1) {
@@ -651,8 +660,8 @@ void mainQSort3(uint32_t* ptr,
651 gtHi--; 660 gtHi--;
652 unHi--; 661 unHi--;
653 continue; 662 continue;
654 }; 663 }
655 if (n < 0) break; 664 if (n < 0) break;
656 unHi--; 665 unHi--;
657 } 666 }
658 if (unLo > unHi) 667 if (unLo > unHi)
@@ -721,28 +730,24 @@ void mainQSort3(uint32_t* ptr,
721#define CLEARMASK (~(SETMASK)) 730#define CLEARMASK (~(SETMASK))
722 731
723static NOINLINE 732static NOINLINE
724void mainSort(EState* state, 733void mainSort(EState* state)
725 uint32_t* ptr,
726 uint8_t* block,
727 uint16_t* quadrant,
728 uint32_t* ftab,
729 int32_t nblock,
730 int32_t* budget)
731{ 734{
732 int32_t i, j, k, ss, sb; 735 int32_t i, j;
733 uint8_t c1;
734 int32_t numQSorted;
735 uint16_t s;
736 Bool bigDone[256]; 736 Bool bigDone[256];
737 uint8_t runningOrder[256];
737 /* bbox: moved to EState to save stack 738 /* bbox: moved to EState to save stack
738 int32_t runningOrder[256];
739 int32_t copyStart[256]; 739 int32_t copyStart[256];
740 int32_t copyEnd [256]; 740 int32_t copyEnd [256];
741 */ 741 */
742#define runningOrder (state->mainSort__runningOrder)
743#define copyStart (state->mainSort__copyStart) 742#define copyStart (state->mainSort__copyStart)
744#define copyEnd (state->mainSort__copyEnd) 743#define copyEnd (state->mainSort__copyEnd)
745 744
745 uint32_t *const ptr = state->ptr;
746 uint8_t *const block = state->block;
747 uint32_t *const ftab = state->ftab;
748 const int32_t nblock = state->nblock;
749 uint16_t *const quadrant = state->quadrant;
750
746 /*-- set up the 2-byte frequency table --*/ 751 /*-- set up the 2-byte frequency table --*/
747 /* was: for (i = 65536; i >= 0; i--) ftab[i] = 0; */ 752 /* was: for (i = 65536; i >= 0; i--) ftab[i] = 0; */
748 memset(ftab, 0, 65537 * sizeof(ftab[0])); 753 memset(ftab, 0, 65537 * sizeof(ftab[0]));
@@ -750,25 +755,25 @@ void mainSort(EState* state,
750 j = block[0] << 8; 755 j = block[0] << 8;
751 i = nblock - 1; 756 i = nblock - 1;
752/* 3%, +300 bytes */ 757/* 3%, +300 bytes */
753#if CONFIG_BZIP2_FAST >= 2 758#if BZIP2_SPEED >= 2
754 for (; i >= 3; i -= 4) { 759 for (; i >= 3; i -= 4) {
755 quadrant[i] = 0; 760 quadrant[i] = 0;
756 j = (j >> 8) | (((uint16_t)block[i]) << 8); 761 j = (j >> 8) | (((unsigned)block[i]) << 8);
757 ftab[j]++; 762 ftab[j]++;
758 quadrant[i-1] = 0; 763 quadrant[i-1] = 0;
759 j = (j >> 8) | (((uint16_t)block[i-1]) << 8); 764 j = (j >> 8) | (((unsigned)block[i-1]) << 8);
760 ftab[j]++; 765 ftab[j]++;
761 quadrant[i-2] = 0; 766 quadrant[i-2] = 0;
762 j = (j >> 8) | (((uint16_t)block[i-2]) << 8); 767 j = (j >> 8) | (((unsigned)block[i-2]) << 8);
763 ftab[j]++; 768 ftab[j]++;
764 quadrant[i-3] = 0; 769 quadrant[i-3] = 0;
765 j = (j >> 8) | (((uint16_t)block[i-3]) << 8); 770 j = (j >> 8) | (((unsigned)block[i-3]) << 8);
766 ftab[j]++; 771 ftab[j]++;
767 } 772 }
768#endif 773#endif
769 for (; i >= 0; i--) { 774 for (; i >= 0; i--) {
770 quadrant[i] = 0; 775 quadrant[i] = 0;
771 j = (j >> 8) | (((uint16_t)block[i]) << 8); 776 j = (j >> 8) | (((unsigned)block[i]) << 8);
772 ftab[j]++; 777 ftab[j]++;
773 } 778 }
774 779
@@ -785,33 +790,36 @@ void mainSort(EState* state,
785 ftab[i] = j; 790 ftab[i] = j;
786 } 791 }
787 792
788 s = block[0] << 8; 793 {
789 i = nblock - 1; 794 unsigned s;
790#if CONFIG_BZIP2_FAST >= 2 795 s = block[0] << 8;
791 for (; i >= 3; i -= 4) { 796 i = nblock - 1;
792 s = (s >> 8) | (block[i] << 8); 797#if BZIP2_SPEED >= 2
793 j = ftab[s] - 1; 798 for (; i >= 3; i -= 4) {
794 ftab[s] = j; 799 s = (s >> 8) | (block[i] << 8);
795 ptr[j] = i; 800 j = ftab[s] - 1;
796 s = (s >> 8) | (block[i-1] << 8); 801 ftab[s] = j;
797 j = ftab[s] - 1; 802 ptr[j] = i;
798 ftab[s] = j; 803 s = (s >> 8) | (block[i-1] << 8);
799 ptr[j] = i-1; 804 j = ftab[s] - 1;
800 s = (s >> 8) | (block[i-2] << 8); 805 ftab[s] = j;
801 j = ftab[s] - 1; 806 ptr[j] = i-1;
802 ftab[s] = j; 807 s = (s >> 8) | (block[i-2] << 8);
803 ptr[j] = i-2; 808 j = ftab[s] - 1;
804 s = (s >> 8) | (block[i-3] << 8); 809 ftab[s] = j;
805 j = ftab[s] - 1; 810 ptr[j] = i-2;
806 ftab[s] = j; 811 s = (s >> 8) | (block[i-3] << 8);
807 ptr[j] = i-3; 812 j = ftab[s] - 1;
808 } 813 ftab[s] = j;
814 ptr[j] = i-3;
815 }
809#endif 816#endif
810 for (; i >= 0; i--) { 817 for (; i >= 0; i--) {
811 s = (s >> 8) | (block[i] << 8); 818 s = (s >> 8) | (block[i] << 8);
812 j = ftab[s] - 1; 819 j = ftab[s] - 1;
813 ftab[s] = j; 820 ftab[s] = j;
814 ptr[j] = i; 821 ptr[j] = i;
822 }
815 } 823 }
816 824
817 /* 825 /*
@@ -825,24 +833,23 @@ void mainSort(EState* state,
825 } 833 }
826 834
827 { 835 {
828 int32_t vv;
829 /* bbox: was: int32_t h = 1; */ 836 /* bbox: was: int32_t h = 1; */
830 /* do h = 3 * h + 1; while (h <= 256); */ 837 /* do h = 3 * h + 1; while (h <= 256); */
831 uint32_t h = 364; 838 unsigned h = 364;
832 839
833 do { 840 do {
834 /*h = h / 3;*/ 841 /*h = h / 3;*/
835 h = (h * 171) >> 9; /* bbox: fast h/3 */ 842 h = (h * 171) >> 9; /* bbox: fast h/3 */
836 for (i = h; i <= 255; i++) { 843 for (i = h; i <= 255; i++) {
837 vv = runningOrder[i]; 844 unsigned vv, jh;
845 vv = runningOrder[i]; /* uint8[] */
838 j = i; 846 j = i;
839 while (BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv)) { 847 while (jh = j - h, BIGFREQ(runningOrder[jh]) > BIGFREQ(vv)) {
840 runningOrder[j] = runningOrder[j-h]; 848 runningOrder[j] = runningOrder[jh];
841 j = j - h; 849 j = jh;
842 if (j <= (h - 1)) 850 if (j < h)
843 goto zero; 851 break;
844 } 852 }
845 zero:
846 runningOrder[j] = vv; 853 runningOrder[j] = vv;
847 } 854 }
848 } while (h != 1); 855 } while (h != 1);
@@ -852,9 +859,8 @@ void mainSort(EState* state,
852 * The main sorting loop. 859 * The main sorting loop.
853 */ 860 */
854 861
855 numQSorted = 0; 862 for (i = 0; /*i <= 255*/; i++) {
856 863 unsigned ss;
857 for (i = 0; i <= 255; i++) {
858 864
859 /* 865 /*
860 * Process big buckets, starting with the least full. 866 * Process big buckets, starting with the least full.
@@ -874,17 +880,14 @@ void mainSort(EState* state,
874 */ 880 */
875 for (j = 0; j <= 255; j++) { 881 for (j = 0; j <= 255; j++) {
876 if (j != ss) { 882 if (j != ss) {
883 unsigned sb;
877 sb = (ss << 8) + j; 884 sb = (ss << 8) + j;
878 if (!(ftab[sb] & SETMASK)) { 885 if (!(ftab[sb] & SETMASK)) {
879 int32_t lo = ftab[sb] & CLEARMASK; 886 int32_t lo = ftab[sb] /*& CLEARMASK (redundant)*/;
880 int32_t hi = (ftab[sb+1] & CLEARMASK) - 1; 887 int32_t hi = (ftab[sb+1] & CLEARMASK) - 1;
881 if (hi > lo) { 888 if (hi > lo) {
882 mainQSort3( 889 mainQSort3(state, lo, hi /*,BZ_N_RADIX*/);
883 ptr, block, quadrant, nblock, 890 if (state->budget < 0) return;
884 lo, hi, BZ_N_RADIX, budget
885 );
886 if (*budget < 0) return;
887 numQSorted += (hi - lo + 1);
888 } 891 }
889 } 892 }
890 ftab[sb] |= SETMASK; 893 ftab[sb] |= SETMASK;
@@ -906,6 +909,8 @@ void mainSort(EState* state,
906 copyEnd [j] = (ftab[(j << 8) + ss + 1] & CLEARMASK) - 1; 909 copyEnd [j] = (ftab[(j << 8) + ss + 1] & CLEARMASK) - 1;
907 } 910 }
908 for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) { 911 for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) {
912 unsigned c1;
913 int32_t k;
909 k = ptr[j] - 1; 914 k = ptr[j] - 1;
910 if (k < 0) 915 if (k < 0)
911 k += nblock; 916 k += nblock;
@@ -914,6 +919,8 @@ void mainSort(EState* state,
914 ptr[copyStart[c1]++] = k; 919 ptr[copyStart[c1]++] = k;
915 } 920 }
916 for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) { 921 for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) {
922 unsigned c1;
923 int32_t k;
917 k = ptr[j]-1; 924 k = ptr[j]-1;
918 if (k < 0) 925 if (k < 0)
919 k += nblock; 926 k += nblock;
@@ -933,6 +940,9 @@ void mainSort(EState* state,
933 for (j = 0; j <= 255; j++) 940 for (j = 0; j <= 255; j++)
934 ftab[(j << 8) + ss] |= SETMASK; 941 ftab[(j << 8) + ss] |= SETMASK;
935 942
943 if (i == 255)
944 break;
945
936 /* 946 /*
937 * Step 3: 947 * Step 3:
938 * The [ss] big bucket is now done. Record this fact, 948 * The [ss] big bucket is now done. Record this fact,
@@ -974,15 +984,15 @@ void mainSort(EState* state,
974 */ 984 */
975 bigDone[ss] = True; 985 bigDone[ss] = True;
976 986
977 if (i < 255) { 987 {
978 int32_t bbStart = ftab[ss << 8] & CLEARMASK; 988 unsigned bbStart = ftab[ss << 8] & CLEARMASK;
979 int32_t bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart; 989 unsigned bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
980 int32_t shifts = 0; 990 unsigned shifts = 0;
981 991
982 while ((bbSize >> shifts) > 65534) shifts++; 992 while ((bbSize >> shifts) > 65534) shifts++;
983 993
984 for (j = bbSize-1; j >= 0; j--) { 994 for (j = bbSize-1; j >= 0; j--) {
985 int32_t a2update = ptr[bbStart + j]; 995 unsigned a2update = ptr[bbStart + j]; /* uint32[] */
986 uint16_t qVal = (uint16_t)(j >> shifts); 996 uint16_t qVal = (uint16_t)(j >> shifts);
987 quadrant[a2update] = qVal; 997 quadrant[a2update] = qVal;
988 if (a2update < BZ_N_OVERSHOOT) 998 if (a2update < BZ_N_OVERSHOOT)
@@ -1015,31 +1025,24 @@ void mainSort(EState* state,
1015 * arr1[0 .. nblock-1] holds sorted order 1025 * arr1[0 .. nblock-1] holds sorted order
1016 */ 1026 */
1017static NOINLINE 1027static NOINLINE
1018void BZ2_blockSort(EState* s) 1028int32_t BZ2_blockSort(EState* state)
1019{ 1029{
1020 /* In original bzip2 1.0.4, it's a parameter, but 30 1030 /* In original bzip2 1.0.4, it's a parameter, but 30
1021 * (which was the default) should work ok. */ 1031 * (which was the default) should work ok. */
1022 enum { wfact = 30 }; 1032 enum { wfact = 30 };
1033 unsigned i;
1034 int32_t origPtr = origPtr;
1023 1035
1024 uint32_t* ptr = s->ptr; 1036 if (state->nblock >= 10000) {
1025 uint8_t* block = s->block;
1026 uint32_t* ftab = s->ftab;
1027 int32_t nblock = s->nblock;
1028 uint16_t* quadrant;
1029 int32_t budget;
1030 int32_t i;
1031
1032 if (nblock < 10000) {
1033 fallbackSort(s->arr1, s->arr2, ftab, nblock);
1034 } else {
1035 /* Calculate the location for quadrant, remembering to get 1037 /* Calculate the location for quadrant, remembering to get
1036 * the alignment right. Assumes that &(block[0]) is at least 1038 * the alignment right. Assumes that &(block[0]) is at least
1037 * 2-byte aligned -- this should be ok since block is really 1039 * 2-byte aligned -- this should be ok since block is really
1038 * the first section of arr2. 1040 * the first section of arr2.
1039 */ 1041 */
1040 i = nblock + BZ_N_OVERSHOOT; 1042 i = state->nblock + BZ_N_OVERSHOOT;
1041 if (i & 1) i++; 1043 if (i & 1)
1042 quadrant = (uint16_t*)(&(block[i])); 1044 i++;
1045 state->quadrant = (uint16_t*) &(state->block[i]);
1043 1046
1044 /* (wfact-1) / 3 puts the default-factor-30 1047 /* (wfact-1) / 3 puts the default-factor-30
1045 * transition point at very roughly the same place as 1048 * transition point at very roughly the same place as
@@ -1048,22 +1051,26 @@ void BZ2_blockSort(EState* s)
1048 * resulting compressed stream is now the same regardless 1051 * resulting compressed stream is now the same regardless
1049 * of whether or not we use the main sort or fallback sort. 1052 * of whether or not we use the main sort or fallback sort.
1050 */ 1053 */
1051 budget = nblock * ((wfact-1) / 3); 1054 state->budget = state->nblock * ((wfact-1) / 3);
1052 1055 mainSort(state);
1053 mainSort(s, ptr, block, quadrant, ftab, nblock, &budget); 1056 if (state->budget >= 0)
1054 if (budget < 0) { 1057 goto good;
1055 fallbackSort(s->arr1, s->arr2, ftab, nblock);
1056 }
1057 } 1058 }
1059 fallbackSort(state);
1060 good:
1058 1061
1059 s->origPtr = -1; 1062#if BZ_LIGHT_DEBUG
1060 for (i = 0; i < s->nblock; i++) 1063 origPtr = -1;
1061 if (ptr[i] == 0) { 1064#endif
1062 s->origPtr = i; 1065 for (i = 0; i < state->nblock; i++) {
1066 if (state->ptr[i] == 0) {
1067 origPtr = i;
1063 break; 1068 break;
1064 }; 1069 }
1070 }
1065 1071
1066 AssertH(s->origPtr != -1, 1003); 1072 AssertH(origPtr != -1, 1003);
1073 return origPtr;
1067} 1074}
1068 1075
1069 1076
diff --git a/archival/libarchive/bz/bzlib.c b/archival/libarchive/bz/bzlib.c
index 5f7db747a..9af2f026d 100644
--- a/archival/libarchive/bz/bzlib.c
+++ b/archival/libarchive/bz/bzlib.c
@@ -55,8 +55,9 @@ void prepare_new_block(EState* s)
55{ 55{
56 int i; 56 int i;
57 s->nblock = 0; 57 s->nblock = 0;
58 s->numZ = 0; 58 //indexes into s->zbits[], initialzation moved to init of s->zbits
59 s->state_out_pos = 0; 59 //s->posZ = s->zbits; // was: s->numZ = 0;
60 //s->state_out_pos = s->zbits;
60 BZ_INITIALISE_CRC(s->blockCRC); 61 BZ_INITIALISE_CRC(s->blockCRC);
61 /* inlined memset would be nice to have here */ 62 /* inlined memset would be nice to have here */
62 for (i = 0; i < 256; i++) 63 for (i = 0; i < 256; i++)
@@ -86,7 +87,7 @@ int isempty_RL(EState* s)
86static 87static
87void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k) 88void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k)
88{ 89{
89 int32_t n; 90 unsigned n;
90 EState* s; 91 EState* s;
91 92
92 s = xzalloc(sizeof(EState)); 93 s = xzalloc(sizeof(EState));
@@ -237,11 +238,10 @@ void /*Bool*/ copy_output_until_stop(EState* s)
237 if (s->strm->avail_out == 0) break; 238 if (s->strm->avail_out == 0) break;
238 239
239 /*-- block done? --*/ 240 /*-- block done? --*/
240 if (s->state_out_pos >= s->numZ) break; 241 if (s->state_out_pos >= s->posZ) break;
241 242
242 /*progress_out = True;*/ 243 /*progress_out = True;*/
243 *(s->strm->next_out) = s->zbits[s->state_out_pos]; 244 *(s->strm->next_out) = *s->state_out_pos++;
244 s->state_out_pos++;
245 s->strm->avail_out--; 245 s->strm->avail_out--;
246 s->strm->next_out++; 246 s->strm->next_out++;
247 s->strm->total_out++; 247 s->strm->total_out++;
@@ -261,7 +261,7 @@ void /*Bool*/ handle_compress(bz_stream *strm)
261 while (1) { 261 while (1) {
262 if (s->state == BZ_S_OUTPUT) { 262 if (s->state == BZ_S_OUTPUT) {
263 /*progress_out |=*/ copy_output_until_stop(s); 263 /*progress_out |=*/ copy_output_until_stop(s);
264 if (s->state_out_pos < s->numZ) break; 264 if (s->state_out_pos < s->posZ) break;
265 if (s->mode == BZ_M_FINISHING 265 if (s->mode == BZ_M_FINISHING
266 //# && s->avail_in_expect == 0 266 //# && s->avail_in_expect == 0
267 && s->strm->avail_in == 0 267 && s->strm->avail_in == 0
@@ -336,7 +336,7 @@ int BZ2_bzCompress(bz_stream *strm, int action)
336 /*if (s->avail_in_expect != s->strm->avail_in) 336 /*if (s->avail_in_expect != s->strm->avail_in)
337 return BZ_SEQUENCE_ERROR;*/ 337 return BZ_SEQUENCE_ERROR;*/
338 /*progress =*/ handle_compress(strm); 338 /*progress =*/ handle_compress(strm);
339 if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) 339 if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
340 return BZ_FLUSH_OK; 340 return BZ_FLUSH_OK;
341 s->mode = BZ_M_RUNNING; 341 s->mode = BZ_M_RUNNING;
342 return BZ_RUN_OK; 342 return BZ_RUN_OK;
@@ -349,9 +349,9 @@ int BZ2_bzCompress(bz_stream *strm, int action)
349 return BZ_SEQUENCE_ERROR;*/ 349 return BZ_SEQUENCE_ERROR;*/
350 /*progress =*/ handle_compress(strm); 350 /*progress =*/ handle_compress(strm);
351 /*if (!progress) return BZ_SEQUENCE_ERROR;*/ 351 /*if (!progress) return BZ_SEQUENCE_ERROR;*/
352 //#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) 352 //#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
353 //# return BZ_FINISH_OK; 353 //# return BZ_FINISH_OK;
354 if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) 354 if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
355 return BZ_FINISH_OK; 355 return BZ_FINISH_OK;
356 /*s->mode = BZ_M_IDLE;*/ 356 /*s->mode = BZ_M_IDLE;*/
357 return BZ_STREAM_END; 357 return BZ_STREAM_END;
diff --git a/archival/libarchive/bz/bzlib_private.h b/archival/libarchive/bz/bzlib_private.h
index 43e674bec..ea0f29b7c 100644
--- a/archival/libarchive/bz/bzlib_private.h
+++ b/archival/libarchive/bz/bzlib_private.h
@@ -120,8 +120,11 @@ typedef struct EState {
120 120
121 /* mode this stream is in, and whether inputting */ 121 /* mode this stream is in, and whether inputting */
122 /* or outputting data */ 122 /* or outputting data */
123 int32_t mode; 123 uint8_t mode;
124 int32_t state; 124 uint8_t state;
125
126 /* misc administratium */
127 uint8_t blockSize100k;
125 128
126 /* remembers avail_in when flush/finish requested */ 129 /* remembers avail_in when flush/finish requested */
127/* bbox: not needed, strm->avail_in always has the same value */ 130/* bbox: not needed, strm->avail_in always has the same value */
@@ -129,20 +132,19 @@ typedef struct EState {
129 /* uint32_t avail_in_expect; */ 132 /* uint32_t avail_in_expect; */
130 133
131 /* for doing the block sorting */ 134 /* for doing the block sorting */
132 int32_t origPtr;
133 uint32_t *arr1; 135 uint32_t *arr1;
134 uint32_t *arr2; 136 uint32_t *arr2;
135 uint32_t *ftab; 137 uint32_t *ftab;
136 138
139 uint16_t *quadrant;
140 int32_t budget;
141
137 /* aliases for arr1 and arr2 */ 142 /* aliases for arr1 and arr2 */
138 uint32_t *ptr; 143 uint32_t *ptr;
139 uint8_t *block; 144 uint8_t *block;
140 uint16_t *mtfv; 145 uint16_t *mtfv;
141 uint8_t *zbits; 146 uint8_t *zbits;
142 147
143 /* guess what */
144 uint32_t *crc32table;
145
146 /* run-length-encoding of the input */ 148 /* run-length-encoding of the input */
147 uint32_t state_in_ch; 149 uint32_t state_in_ch;
148 int32_t state_in_len; 150 int32_t state_in_len;
@@ -150,20 +152,23 @@ typedef struct EState {
150 /* input and output limits and current posns */ 152 /* input and output limits and current posns */
151 int32_t nblock; 153 int32_t nblock;
152 int32_t nblockMAX; 154 int32_t nblockMAX;
153 int32_t numZ; 155 //int32_t numZ; // index into s->zbits[], replaced by pointer:
154 int32_t state_out_pos; 156 uint8_t *posZ;
157 uint8_t *state_out_pos;
155 158
156 /* the buffer for bit stream creation */ 159 /* the buffer for bit stream creation */
157 uint32_t bsBuff; 160 uint32_t bsBuff;
158 int32_t bsLive; 161 int32_t bsLive;
159 162
163 /* guess what */
164 uint32_t *crc32table;
165
160 /* block and combined CRCs */ 166 /* block and combined CRCs */
161 uint32_t blockCRC; 167 uint32_t blockCRC;
162 uint32_t combinedCRC; 168 uint32_t combinedCRC;
163 169
164 /* misc administratium */ 170 /* misc administratium */
165 int32_t blockNo; 171 int32_t blockNo;
166 int32_t blockSize100k;
167 172
168 /* stuff for coding the MTF values */ 173 /* stuff for coding the MTF values */
169 int32_t nMTF; 174 int32_t nMTF;
@@ -183,7 +188,7 @@ typedef struct EState {
183 /* stack-saving measures: these can be local, but they are too big */ 188 /* stack-saving measures: these can be local, but they are too big */
184 int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; 189 int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
185 int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; 190 int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
186#if CONFIG_BZIP2_FAST >= 5 191#if BZIP2_SPEED >= 5
187 /* second dimension: only 3 needed; 4 makes index calculations faster */ 192 /* second dimension: only 3 needed; 4 makes index calculations faster */
188 uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4]; 193 uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4];
189#endif 194#endif
@@ -191,7 +196,6 @@ typedef struct EState {
191 int32_t BZ2_hbMakeCodeLengths__weight[BZ_MAX_ALPHA_SIZE * 2]; 196 int32_t BZ2_hbMakeCodeLengths__weight[BZ_MAX_ALPHA_SIZE * 2];
192 int32_t BZ2_hbMakeCodeLengths__parent[BZ_MAX_ALPHA_SIZE * 2]; 197 int32_t BZ2_hbMakeCodeLengths__parent[BZ_MAX_ALPHA_SIZE * 2];
193 198
194 int32_t mainSort__runningOrder[256];
195 int32_t mainSort__copyStart[256]; 199 int32_t mainSort__copyStart[256];
196 int32_t mainSort__copyEnd[256]; 200 int32_t mainSort__copyEnd[256];
197} EState; 201} EState;
@@ -199,7 +203,7 @@ typedef struct EState {
199 203
200/*-- compression. --*/ 204/*-- compression. --*/
201 205
202static void 206static int32_t
203BZ2_blockSort(EState*); 207BZ2_blockSort(EState*);
204 208
205static void 209static void
diff --git a/archival/libarchive/bz/compress.c b/archival/libarchive/bz/compress.c
index 2d994685c..539ab927e 100644
--- a/archival/libarchive/bz/compress.c
+++ b/archival/libarchive/bz/compress.c
@@ -32,6 +32,12 @@ in the file LICENSE.
32 32
33/* #include "bzlib_private.h" */ 33/* #include "bzlib_private.h" */
34 34
35#if BZIP2_SPEED >= 5
36# define ALWAYS_INLINE_5 ALWAYS_INLINE
37#else
38# define ALWAYS_INLINE_5 /*nothing*/
39#endif
40
35/*---------------------------------------------------*/ 41/*---------------------------------------------------*/
36/*--- Bit stream I/O ---*/ 42/*--- Bit stream I/O ---*/
37/*---------------------------------------------------*/ 43/*---------------------------------------------------*/
@@ -50,8 +56,7 @@ static NOINLINE
50void bsFinishWrite(EState* s) 56void bsFinishWrite(EState* s)
51{ 57{
52 while (s->bsLive > 0) { 58 while (s->bsLive > 0) {
53 s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24); 59 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
54 s->numZ++;
55 s->bsBuff <<= 8; 60 s->bsBuff <<= 8;
56 s->bsLive -= 8; 61 s->bsLive -= 8;
57 } 62 }
@@ -61,39 +66,74 @@ void bsFinishWrite(EState* s)
61/*---------------------------------------------------*/ 66/*---------------------------------------------------*/
62static 67static
63/* Helps only on level 5, on other levels hurts. ? */ 68/* Helps only on level 5, on other levels hurts. ? */
64#if CONFIG_BZIP2_FAST >= 5 69ALWAYS_INLINE_5
65ALWAYS_INLINE
66#endif
67void bsW(EState* s, int32_t n, uint32_t v) 70void bsW(EState* s, int32_t n, uint32_t v)
68{ 71{
69 while (s->bsLive >= 8) { 72 while (s->bsLive >= 8) {
70 s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24); 73 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
71 s->numZ++;
72 s->bsBuff <<= 8; 74 s->bsBuff <<= 8;
73 s->bsLive -= 8; 75 s->bsLive -= 8;
74 } 76 }
75 s->bsBuff |= (v << (32 - s->bsLive - n)); 77 s->bsBuff |= (v << (32 - s->bsLive - n));
76 s->bsLive += n; 78 s->bsLive += n;
77} 79}
80/* Same with n == 16: */
81static
82ALWAYS_INLINE_5
83void bsW16(EState* s, uint32_t v)
84{
85 while (s->bsLive >= 8) {
86 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
87 s->bsBuff <<= 8;
88 s->bsLive -= 8;
89 }
90 s->bsBuff |= (v << (16 - s->bsLive));
91 s->bsLive += 16;
92}
93/* Same with n == 1: */
94static
95ALWAYS_INLINE /* one callsite */
96void bsW1_1(EState* s)
97{
98 /* need space for only 1 bit, no need for loop freeing > 8 bits */
99 if (s->bsLive >= 8) {
100 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
101 s->bsBuff <<= 8;
102 s->bsLive -= 8;
103 }
104 s->bsBuff |= (1 << (31 - s->bsLive));
105 s->bsLive += 1;
106}
107static
108ALWAYS_INLINE_5
109void bsW1_0(EState* s)
110{
111 /* need space for only 1 bit, no need for loop freeing > 8 bits */
112 if (s->bsLive >= 8) {
113 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
114 s->bsBuff <<= 8;
115 s->bsLive -= 8;
116 }
117 //s->bsBuff |= (0 << (31 - s->bsLive));
118 s->bsLive += 1;
119}
78 120
79 121
80/*---------------------------------------------------*/ 122/*---------------------------------------------------*/
81static 123static ALWAYS_INLINE
82void bsPutU32(EState* s, unsigned u) 124void bsPutU16(EState* s, unsigned u)
83{ 125{
84 bsW(s, 8, (u >> 24) & 0xff); 126 bsW16(s, u);
85 bsW(s, 8, (u >> 16) & 0xff);
86 bsW(s, 8, (u >> 8) & 0xff);
87 bsW(s, 8, u & 0xff);
88} 127}
89 128
90 129
91/*---------------------------------------------------*/ 130/*---------------------------------------------------*/
92static 131static
93void bsPutU16(EState* s, unsigned u) 132void bsPutU32(EState* s, unsigned u)
94{ 133{
95 bsW(s, 8, (u >> 8) & 0xff); 134 //bsW(s, 32, u); // can't use: may try "uint32 << -n"
96 bsW(s, 8, u & 0xff); 135 bsW16(s, (u >> 16) & 0xffff);
136 bsW16(s, u & 0xffff);
97} 137}
98 138
99 139
@@ -106,25 +146,57 @@ static
106void makeMaps_e(EState* s) 146void makeMaps_e(EState* s)
107{ 147{
108 int i; 148 int i;
109 s->nInUse = 0; 149 unsigned cnt = 0;
110 for (i = 0; i < 256; i++) { 150 for (i = 0; i < 256; i++) {
111 if (s->inUse[i]) { 151 if (s->inUse[i]) {
112 s->unseqToSeq[i] = s->nInUse; 152 s->unseqToSeq[i] = cnt;
113 s->nInUse++; 153 cnt++;
114 } 154 }
115 } 155 }
156 s->nInUse = cnt;
116} 157}
117 158
118 159
119/*---------------------------------------------------*/ 160/*---------------------------------------------------*/
161/*
162 * This bit of code is performance-critical.
163 * On 32bit x86, gcc-6.3.0 was observed to spill ryy_j to stack,
164 * resulting in abysmal performance (x3 slowdown).
165 * Forcing it into a separate function alleviates register pressure,
166 * and spillage no longer happens.
167 * Other versions of gcc do not exhibit this problem, but out-of-line code
168 * seems to be helping them too (code is both smaller and faster).
169 * Therefore NOINLINE is enabled for the entire 32bit x86 arch for now,
170 * without a check for gcc version.
171 */
172static
173#if defined __i386__
174NOINLINE
175#endif
176int inner_loop(uint8_t *yy, uint8_t ll_i)
177{
178 register uint8_t rtmp;
179 register uint8_t* ryy_j;
180 rtmp = yy[1];
181 yy[1] = yy[0];
182 ryy_j = &(yy[1]);
183 while (ll_i != rtmp) {
184 register uint8_t rtmp2;
185 ryy_j++;
186 rtmp2 = rtmp;
187 rtmp = *ryy_j;
188 *ryy_j = rtmp2;
189 }
190 yy[0] = rtmp;
191 return ryy_j - &(yy[0]);
192}
120static NOINLINE 193static NOINLINE
121void generateMTFValues(EState* s) 194void generateMTFValues(EState* s)
122{ 195{
123 uint8_t yy[256]; 196 uint8_t yy[256];
124 int32_t i, j; 197 int i;
125 int32_t zPend; 198 int zPend;
126 int32_t wr; 199 int32_t wr;
127 int32_t EOB;
128 200
129 /* 201 /*
130 * After sorting (eg, here), 202 * After sorting (eg, here),
@@ -148,95 +220,74 @@ void generateMTFValues(EState* s)
148 * compressBlock(). 220 * compressBlock().
149 */ 221 */
150 uint32_t* ptr = s->ptr; 222 uint32_t* ptr = s->ptr;
151 uint8_t* block = s->block;
152 uint16_t* mtfv = s->mtfv;
153 223
154 makeMaps_e(s); 224 makeMaps_e(s);
155 EOB = s->nInUse+1;
156
157 for (i = 0; i <= EOB; i++)
158 s->mtfFreq[i] = 0;
159 225
160 wr = 0; 226 wr = 0;
161 zPend = 0; 227 zPend = 0;
228 for (i = 0; i <= s->nInUse+1; i++)
229 s->mtfFreq[i] = 0;
230
162 for (i = 0; i < s->nInUse; i++) 231 for (i = 0; i < s->nInUse; i++)
163 yy[i] = (uint8_t) i; 232 yy[i] = (uint8_t) i;
164 233
165 for (i = 0; i < s->nblock; i++) { 234 for (i = 0; i < s->nblock; i++) {
166 uint8_t ll_i; 235 uint8_t ll_i = ll_i; /* gcc 4.3.1 thinks it may be used w/o init */
236 int32_t j;
237
167 AssertD(wr <= i, "generateMTFValues(1)"); 238 AssertD(wr <= i, "generateMTFValues(1)");
168 j = ptr[i] - 1; 239 j = ptr[i] - 1;
169 if (j < 0) 240 if (j < 0)
170 j += s->nblock; 241 j += s->nblock;
171 ll_i = s->unseqToSeq[block[j]]; 242 ll_i = s->unseqToSeq[s->block[j]];
172 AssertD(ll_i < s->nInUse, "generateMTFValues(2a)"); 243 AssertD(ll_i < s->nInUse, "generateMTFValues(2a)");
173 244
174 if (yy[0] == ll_i) { 245 if (yy[0] == ll_i) {
175 zPend++; 246 zPend++;
176 } else { 247 continue;
177 if (zPend > 0) {
178 zPend--;
179 while (1) {
180 if (zPend & 1) {
181 mtfv[wr] = BZ_RUNB; wr++;
182 s->mtfFreq[BZ_RUNB]++;
183 } else {
184 mtfv[wr] = BZ_RUNA; wr++;
185 s->mtfFreq[BZ_RUNA]++;
186 }
187 if (zPend < 2) break;
188 zPend = (uint32_t)(zPend - 2) / 2;
189 /* bbox: unsigned div is easier */
190 };
191 zPend = 0;
192 }
193 {
194 register uint8_t rtmp;
195 register uint8_t* ryy_j;
196 register uint8_t rll_i;
197 rtmp = yy[1];
198 yy[1] = yy[0];
199 ryy_j = &(yy[1]);
200 rll_i = ll_i;
201 while (rll_i != rtmp) {
202 register uint8_t rtmp2;
203 ryy_j++;
204 rtmp2 = rtmp;
205 rtmp = *ryy_j;
206 *ryy_j = rtmp2;
207 };
208 yy[0] = rtmp;
209 j = ryy_j - &(yy[0]);
210 mtfv[wr] = j+1;
211 wr++;
212 s->mtfFreq[j+1]++;
213 }
214 } 248 }
215 }
216 249
217 if (zPend > 0) { 250 if (zPend > 0) {
218 zPend--; 251 process_zPend:
219 while (1) { 252 zPend--;
220 if (zPend & 1) { 253 while (1) {
221 mtfv[wr] = BZ_RUNB; 254#if 0
222 wr++; 255 if (zPend & 1) {
223 s->mtfFreq[BZ_RUNB]++; 256 s->mtfv[wr] = BZ_RUNB; wr++;
224 } else { 257 s->mtfFreq[BZ_RUNB]++;
225 mtfv[wr] = BZ_RUNA; 258 } else {
259 s->mtfv[wr] = BZ_RUNA; wr++;
260 s->mtfFreq[BZ_RUNA]++;
261 }
262#else /* same as above, since BZ_RUNA is 0 and BZ_RUNB is 1 */
263 unsigned run = zPend & 1;
264 s->mtfv[wr] = run;
226 wr++; 265 wr++;
227 s->mtfFreq[BZ_RUNA]++; 266 s->mtfFreq[run]++;
267#endif
268 zPend -= 2;
269 if (zPend < 0)
270 break;
271 zPend = (unsigned)zPend / 2;
272 /* bbox: unsigned div is easier */
228 } 273 }
229 if (zPend < 2) 274 if (i < 0) /* came via "goto process_zPend"? exit */
230 break; 275 goto end;
231 zPend = (uint32_t)(zPend - 2) / 2; 276 zPend = 0;
232 /* bbox: unsigned div is easier */ 277 }
233 }; 278 j = inner_loop(yy, ll_i);
234 zPend = 0; 279 s->mtfv[wr] = j+1;
280 wr++;
281 s->mtfFreq[j+1]++;
235 } 282 }
236 283
237 mtfv[wr] = EOB; 284 i = -1;
285 if (zPend > 0)
286 goto process_zPend; /* "process it and come back here" */
287 end:
288 s->mtfv[wr] = s->nInUse+1;
238 wr++; 289 wr++;
239 s->mtfFreq[EOB]++; 290 s->mtfFreq[s->nInUse+1]++;
240 291
241 s->nMTF = wr; 292 s->nMTF = wr;
242} 293}
@@ -249,8 +300,11 @@ void generateMTFValues(EState* s)
249static NOINLINE 300static NOINLINE
250void sendMTFValues(EState* s) 301void sendMTFValues(EState* s)
251{ 302{
252 int32_t v, t, i, j, gs, ge, bt, bc, iter; 303 int32_t t, i;
253 int32_t nSelectors, alphaSize, minLen, maxLen, selCtr; 304 unsigned iter;
305 unsigned gs;
306 int32_t alphaSize;
307 unsigned nSelectors, selCtr;
254 int32_t nGroups; 308 int32_t nGroups;
255 309
256 /* 310 /*
@@ -266,39 +320,49 @@ void sendMTFValues(EState* s)
266#define rfreq sendMTFValues__rfreq 320#define rfreq sendMTFValues__rfreq
267#define len_pack sendMTFValues__len_pack 321#define len_pack sendMTFValues__len_pack
268 322
269 uint16_t cost[BZ_N_GROUPS]; 323 unsigned /*uint16_t*/ cost[BZ_N_GROUPS];
270 int32_t fave[BZ_N_GROUPS];
271 324
272 uint16_t* mtfv = s->mtfv; 325 uint16_t* mtfv = s->mtfv;
273 326
274 alphaSize = s->nInUse + 2; 327 alphaSize = s->nInUse + 2;
275 for (t = 0; t < BZ_N_GROUPS; t++) 328 for (t = 0; t < BZ_N_GROUPS; t++) {
329 unsigned v;
276 for (v = 0; v < alphaSize; v++) 330 for (v = 0; v < alphaSize; v++)
277 s->len[t][v] = BZ_GREATER_ICOST; 331 s->len[t][v] = BZ_GREATER_ICOST;
332 }
278 333
279 /*--- Decide how many coding tables to use ---*/ 334 /*--- Decide how many coding tables to use ---*/
280 AssertH(s->nMTF > 0, 3001); 335 AssertH(s->nMTF > 0, 3001);
281 if (s->nMTF < 200) nGroups = 2; else 336 // 1..199 = 2
282 if (s->nMTF < 600) nGroups = 3; else 337 // 200..599 = 3
283 if (s->nMTF < 1200) nGroups = 4; else 338 // 600..1199 = 4
284 if (s->nMTF < 2400) nGroups = 5; else 339 // 1200..2399 = 5
285 nGroups = 6; 340 // 2400..99999 = 6
341 nGroups = 2;
342 nGroups += (s->nMTF >= 200);
343 nGroups += (s->nMTF >= 600);
344 nGroups += (s->nMTF >= 1200);
345 nGroups += (s->nMTF >= 2400);
286 346
287 /*--- Generate an initial set of coding tables ---*/ 347 /*--- Generate an initial set of coding tables ---*/
288 { 348 {
289 int32_t nPart, remF, tFreq, aFreq; 349 unsigned nPart, remF;
290 350
291 nPart = nGroups; 351 nPart = nGroups;
292 remF = s->nMTF; 352 remF = s->nMTF;
293 gs = 0; 353 gs = 0;
294 while (nPart > 0) { 354 while (nPart > 0) {
355 unsigned v;
356 unsigned ge;
357 unsigned tFreq, aFreq;
358
295 tFreq = remF / nPart; 359 tFreq = remF / nPart;
296 ge = gs - 1; 360 ge = gs;
297 aFreq = 0; 361 aFreq = 0;
298 while (aFreq < tFreq && ge < alphaSize-1) { 362 while (aFreq < tFreq && ge < alphaSize) {
299 ge++; 363 aFreq += s->mtfFreq[ge++];
300 aFreq += s->mtfFreq[ge];
301 } 364 }
365 ge--;
302 366
303 if (ge > gs 367 if (ge > gs
304 && nPart != nGroups && nPart != 1 368 && nPart != nGroups && nPart != 1
@@ -324,19 +388,19 @@ void sendMTFValues(EState* s)
324 * Iterate up to BZ_N_ITERS times to improve the tables. 388 * Iterate up to BZ_N_ITERS times to improve the tables.
325 */ 389 */
326 for (iter = 0; iter < BZ_N_ITERS; iter++) { 390 for (iter = 0; iter < BZ_N_ITERS; iter++) {
327 for (t = 0; t < nGroups; t++) 391 for (t = 0; t < nGroups; t++) {
328 fave[t] = 0; 392 unsigned v;
329
330 for (t = 0; t < nGroups; t++)
331 for (v = 0; v < alphaSize; v++) 393 for (v = 0; v < alphaSize; v++)
332 s->rfreq[t][v] = 0; 394 s->rfreq[t][v] = 0;
395 }
333 396
334#if CONFIG_BZIP2_FAST >= 5 397#if BZIP2_SPEED >= 5
335 /* 398 /*
336 * Set up an auxiliary length table which is used to fast-track 399 * Set up an auxiliary length table which is used to fast-track
337 * the common case (nGroups == 6). 400 * the common case (nGroups == 6).
338 */ 401 */
339 if (nGroups == 6) { 402 if (nGroups == 6) {
403 unsigned v;
340 for (v = 0; v < alphaSize; v++) { 404 for (v = 0; v < alphaSize; v++) {
341 s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v]; 405 s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
342 s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v]; 406 s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
@@ -347,6 +411,9 @@ void sendMTFValues(EState* s)
347 nSelectors = 0; 411 nSelectors = 0;
348 gs = 0; 412 gs = 0;
349 while (1) { 413 while (1) {
414 unsigned ge;
415 unsigned bt, bc;
416
350 /*--- Set group start & end marks. --*/ 417 /*--- Set group start & end marks. --*/
351 if (gs >= s->nMTF) 418 if (gs >= s->nMTF)
352 break; 419 break;
@@ -360,7 +427,7 @@ void sendMTFValues(EState* s)
360 */ 427 */
361 for (t = 0; t < nGroups; t++) 428 for (t = 0; t < nGroups; t++)
362 cost[t] = 0; 429 cost[t] = 0;
363#if CONFIG_BZIP2_FAST >= 5 430#if BZIP2_SPEED >= 5
364 if (nGroups == 6 && 50 == ge-gs+1) { 431 if (nGroups == 6 && 50 == ge-gs+1) {
365 /*--- fast track the common case ---*/ 432 /*--- fast track the common case ---*/
366 register uint32_t cost01, cost23, cost45; 433 register uint32_t cost01, cost23, cost45;
@@ -390,7 +457,7 @@ void sendMTFValues(EState* s)
390 { 457 {
391 /*--- slow version which correctly handles all situations ---*/ 458 /*--- slow version which correctly handles all situations ---*/
392 for (i = gs; i <= ge; i++) { 459 for (i = gs; i <= ge; i++) {
393 uint16_t icv = mtfv[i]; 460 unsigned /*uint16_t*/ icv = mtfv[i];
394 for (t = 0; t < nGroups; t++) 461 for (t = 0; t < nGroups; t++)
395 cost[t] += s->len[t][icv]; 462 cost[t] += s->len[t][icv];
396 } 463 }
@@ -409,7 +476,6 @@ void sendMTFValues(EState* s)
409 bt = t; 476 bt = t;
410 } 477 }
411 } 478 }
412 fave[bt]++;
413 s->selector[nSelectors] = bt; 479 s->selector[nSelectors] = bt;
414 nSelectors++; 480 nSelectors++;
415 481
@@ -417,7 +483,7 @@ void sendMTFValues(EState* s)
417 * Increment the symbol frequencies for the selected table. 483 * Increment the symbol frequencies for the selected table.
418 */ 484 */
419/* 1% faster compress. +800 bytes */ 485/* 1% faster compress. +800 bytes */
420#if CONFIG_BZIP2_FAST >= 4 486#if BZIP2_SPEED >= 4
421 if (nGroups == 6 && 50 == ge-gs+1) { 487 if (nGroups == 6 && 50 == ge-gs+1) {
422 /*--- fast track the common case ---*/ 488 /*--- fast track the common case ---*/
423#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++ 489#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++
@@ -464,6 +530,7 @@ void sendMTFValues(EState* s)
464 for (i = 0; i < nGroups; i++) 530 for (i = 0; i < nGroups; i++)
465 pos[i] = i; 531 pos[i] = i;
466 for (i = 0; i < nSelectors; i++) { 532 for (i = 0; i < nSelectors; i++) {
533 unsigned j;
467 ll_i = s->selector[i]; 534 ll_i = s->selector[i];
468 j = 0; 535 j = 0;
469 tmp = pos[j]; 536 tmp = pos[j];
@@ -472,16 +539,16 @@ void sendMTFValues(EState* s)
472 tmp2 = tmp; 539 tmp2 = tmp;
473 tmp = pos[j]; 540 tmp = pos[j];
474 pos[j] = tmp2; 541 pos[j] = tmp2;
475 }; 542 }
476 pos[0] = tmp; 543 pos[0] = tmp;
477 s->selectorMtf[i] = j; 544 s->selectorMtf[i] = j;
478 } 545 }
479 }; 546 }
480 547
481 /*--- Assign actual codes for the tables. --*/ 548 /*--- Assign actual codes for the tables. --*/
482 for (t = 0; t < nGroups; t++) { 549 for (t = 0; t < nGroups; t++) {
483 minLen = 32; 550 unsigned minLen = 32; //todo: s->len[t][0];
484 maxLen = 0; 551 unsigned maxLen = 0; //todo: s->len[t][0];
485 for (i = 0; i < alphaSize; i++) { 552 for (i = 0; i < alphaSize; i++) {
486 if (s->len[t][i] > maxLen) maxLen = s->len[t][i]; 553 if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
487 if (s->len[t][i] < minLen) minLen = s->len[t][i]; 554 if (s->len[t][i] < minLen) minLen = s->len[t][i];
@@ -509,15 +576,16 @@ void sendMTFValues(EState* s)
509 } 576 }
510 } 577 }
511 578
512 bsW(s, 16, inUse16); 579 bsW16(s, inUse16);
513 580
514 inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */ 581 inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */
515 for (i = 0; i < 16; i++) { 582 for (i = 0; i < 16; i++) {
516 if (inUse16 < 0) { 583 if (inUse16 < 0) {
517 unsigned v16 = 0; 584 unsigned v16 = 0;
585 unsigned j;
518 for (j = 0; j < 16; j++) 586 for (j = 0; j < 16; j++)
519 v16 = v16*2 + s->inUse[i * 16 + j]; 587 v16 = v16*2 + s->inUse[i * 16 + j];
520 bsW(s, 16, v16); 588 bsW16(s, v16);
521 } 589 }
522 inUse16 <<= 1; 590 inUse16 <<= 1;
523 } 591 }
@@ -527,19 +595,20 @@ void sendMTFValues(EState* s)
527 bsW(s, 3, nGroups); 595 bsW(s, 3, nGroups);
528 bsW(s, 15, nSelectors); 596 bsW(s, 15, nSelectors);
529 for (i = 0; i < nSelectors; i++) { 597 for (i = 0; i < nSelectors; i++) {
598 unsigned j;
530 for (j = 0; j < s->selectorMtf[i]; j++) 599 for (j = 0; j < s->selectorMtf[i]; j++)
531 bsW(s, 1, 1); 600 bsW1_1(s);
532 bsW(s, 1, 0); 601 bsW1_0(s);
533 } 602 }
534 603
535 /*--- Now the coding tables. ---*/ 604 /*--- Now the coding tables. ---*/
536 for (t = 0; t < nGroups; t++) { 605 for (t = 0; t < nGroups; t++) {
537 int32_t curr = s->len[t][0]; 606 unsigned curr = s->len[t][0];
538 bsW(s, 5, curr); 607 bsW(s, 5, curr);
539 for (i = 0; i < alphaSize; i++) { 608 for (i = 0; i < alphaSize; i++) {
540 while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ }; 609 while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ }
541 while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ }; 610 while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ }
542 bsW(s, 1, 0); 611 bsW1_0(s);
543 } 612 }
544 } 613 }
545 614
@@ -547,6 +616,8 @@ void sendMTFValues(EState* s)
547 selCtr = 0; 616 selCtr = 0;
548 gs = 0; 617 gs = 0;
549 while (1) { 618 while (1) {
619 unsigned ge;
620
550 if (gs >= s->nMTF) 621 if (gs >= s->nMTF)
551 break; 622 break;
552 ge = gs + BZ_G_SIZE - 1; 623 ge = gs + BZ_G_SIZE - 1;
@@ -605,17 +676,21 @@ void sendMTFValues(EState* s)
605static 676static
606void BZ2_compressBlock(EState* s, int is_last_block) 677void BZ2_compressBlock(EState* s, int is_last_block)
607{ 678{
679 int32_t origPtr = origPtr;
680
608 if (s->nblock > 0) { 681 if (s->nblock > 0) {
609 BZ_FINALISE_CRC(s->blockCRC); 682 BZ_FINALISE_CRC(s->blockCRC);
610 s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31); 683 s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
611 s->combinedCRC ^= s->blockCRC; 684 s->combinedCRC ^= s->blockCRC;
612 if (s->blockNo > 1) 685 if (s->blockNo > 1)
613 s->numZ = 0; 686 s->posZ = s->zbits; // was: s->numZ = 0;
614 687
615 BZ2_blockSort(s); 688 origPtr = BZ2_blockSort(s);
616 } 689 }
617 690
618 s->zbits = &((uint8_t*)s->arr2)[s->nblock]; 691 s->zbits = &((uint8_t*)s->arr2)[s->nblock];
692 s->posZ = s->zbits;
693 s->state_out_pos = s->zbits;
619 694
620 /*-- If this is the first block, create the stream header. --*/ 695 /*-- If this is the first block, create the stream header. --*/
621 if (s->blockNo == 1) { 696 if (s->blockNo == 1) {
@@ -649,9 +724,9 @@ void BZ2_compressBlock(EState* s, int is_last_block)
649 * so as to maintain backwards compatibility with 724 * so as to maintain backwards compatibility with
650 * older versions of bzip2. 725 * older versions of bzip2.
651 */ 726 */
652 bsW(s, 1, 0); 727 bsW1_0(s);
653 728
654 bsW(s, 24, s->origPtr); 729 bsW(s, 24, origPtr);
655 generateMTFValues(s); 730 generateMTFValues(s);
656 sendMTFValues(s); 731 sendMTFValues(s);
657 } 732 }
diff --git a/archival/libarchive/bz/huffman.c b/archival/libarchive/bz/huffman.c
index bbec11adb..dc851cd3f 100644
--- a/archival/libarchive/bz/huffman.c
+++ b/archival/libarchive/bz/huffman.c
@@ -48,7 +48,7 @@ in the file LICENSE.
48 48
49 49
50/* 90 bytes, 0.3% of overall compress speed */ 50/* 90 bytes, 0.3% of overall compress speed */
51#if CONFIG_BZIP2_FAST >= 1 51#if BZIP2_SPEED >= 1
52 52
53/* macro works better than inline (gcc 4.2.1) */ 53/* macro works better than inline (gcc 4.2.1) */
54#define DOWNHEAP1(heap, weight, Heap) \ 54#define DOWNHEAP1(heap, weight, Heap) \
@@ -217,7 +217,7 @@ void BZ2_hbAssignCodes(int32_t *code,
217 if (length[i] == n) { 217 if (length[i] == n) {
218 code[i] = vec; 218 code[i] = vec;
219 vec++; 219 vec++;
220 }; 220 }
221 } 221 }
222 vec <<= 1; 222 vec <<= 1;
223 } 223 }
diff --git a/archival/libarchive/decompress_gunzip.c b/archival/libarchive/decompress_gunzip.c
index 14a901792..7483a2cea 100644
--- a/archival/libarchive/decompress_gunzip.c
+++ b/archival/libarchive/decompress_gunzip.c
@@ -280,8 +280,8 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current
280/* Given a list of code lengths and a maximum table size, make a set of 280/* Given a list of code lengths and a maximum table size, make a set of
281 * tables to decode that set of codes. Return zero on success, one if 281 * tables to decode that set of codes. Return zero on success, one if
282 * the given code set is incomplete (the tables are still built in this 282 * the given code set is incomplete (the tables are still built in this
283 * case), two if the input is invalid (all zero length codes or an 283 * case), two if the input is invalid (an oversubscribed set of lengths)
284 * oversubscribed set of lengths) - in this case stores NULL in *t. 284 * - in this case stores NULL in *t.
285 * 285 *
286 * b: code lengths in bits (all assumed <= BMAX) 286 * b: code lengths in bits (all assumed <= BMAX)
287 * n: number of codes (assumed <= N_MAX) 287 * n: number of codes (assumed <= N_MAX)
@@ -330,8 +330,15 @@ static int huft_build(const unsigned *b, const unsigned n,
330 p++; /* can't combine with above line (Solaris bug) */ 330 p++; /* can't combine with above line (Solaris bug) */
331 } while (--i); 331 } while (--i);
332 if (c[0] == n) { /* null input - all zero length codes */ 332 if (c[0] == n) { /* null input - all zero length codes */
333 *m = 0; 333 q = xzalloc(3 * sizeof(*q));
334 return 2; 334 //q[0].v.t = NULL;
335 q[1].e = 99; /* invalid code marker */
336 q[1].b = 1;
337 q[2].e = 99; /* invalid code marker */
338 q[2].b = 1;
339 *t = q + 1;
340 *m = 1;
341 return 0;
335 } 342 }
336 343
337 /* Find minimum and maximum length, bound *m by those */ 344 /* Find minimum and maximum length, bound *m by those */
@@ -1000,7 +1007,7 @@ inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
1000 gunzip_bb = 0; 1007 gunzip_bb = 0;
1001 1008
1002 /* Create the crc table */ 1009 /* Create the crc table */
1003 gunzip_crc_table = crc32_filltable(NULL, 0); 1010 gunzip_crc_table = crc32_new_table_le();
1004 gunzip_crc = ~0; 1011 gunzip_crc = ~0;
1005 1012
1006 error_msg = "corrupted data"; 1013 error_msg = "corrupted data";
diff --git a/archival/libarchive/decompress_unxz.c b/archival/libarchive/decompress_unxz.c
index 0be85500c..8ae7a275b 100644
--- a/archival/libarchive/decompress_unxz.c
+++ b/archival/libarchive/decompress_unxz.c
@@ -52,7 +52,7 @@ unpack_xz_stream(transformer_state_t *xstate)
52 IF_DESKTOP(long long) int total = 0; 52 IF_DESKTOP(long long) int total = 0;
53 53
54 if (!global_crc32_table) 54 if (!global_crc32_table)
55 global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0); 55 global_crc32_new_table_le();
56 56
57 memset(&iobuf, 0, sizeof(iobuf)); 57 memset(&iobuf, 0, sizeof(iobuf));
58 membuf = xmalloc(2 * BUFSIZ); 58 membuf = xmalloc(2 * BUFSIZ);
diff --git a/archival/libarchive/get_header_ar.c b/archival/libarchive/get_header_ar.c
index 1809ec396..93e071c9f 100644
--- a/archival/libarchive/get_header_ar.c
+++ b/archival/libarchive/get_header_ar.c
@@ -83,7 +83,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
83 */ 83 */
84 ar_long_name_size = size; 84 ar_long_name_size = size;
85 free(ar_long_names); 85 free(ar_long_names);
86 ar_long_names = xmalloc(size); 86 ar_long_names = xzalloc(size + 1);
87 xread(archive_handle->src_fd, ar_long_names, size); 87 xread(archive_handle->src_fd, ar_long_names, size);
88 archive_handle->offset += size; 88 archive_handle->offset += size;
89 /* Return next header */ 89 /* Return next header */
@@ -107,7 +107,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
107 unsigned long_offset; 107 unsigned long_offset;
108 108
109 /* The number after the '/' indicates the offset in the ar data section 109 /* The number after the '/' indicates the offset in the ar data section
110 * (saved in ar_long_names) that conatains the real filename */ 110 * (saved in ar_long_names) that contains the real filename */
111 long_offset = read_num(&ar.formatted.name[1], 10, 111 long_offset = read_num(&ar.formatted.name[1], 10,
112 sizeof(ar.formatted.name) - 1); 112 sizeof(ar.formatted.name) - 1);
113 if (long_offset >= ar_long_name_size) { 113 if (long_offset >= ar_long_name_size) {
diff --git a/archival/libarchive/get_header_tar.c b/archival/libarchive/get_header_tar.c
index aeb54190f..5c495e14e 100644
--- a/archival/libarchive/get_header_tar.c
+++ b/archival/libarchive/get_header_tar.c
@@ -152,6 +152,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
152 file_header_t *file_header = archive_handle->file_header; 152 file_header_t *file_header = archive_handle->file_header;
153 struct tar_header_t tar; 153 struct tar_header_t tar;
154 char *cp; 154 char *cp;
155 int tar_typeflag; /* can be "char", "int" seems give smaller code */
155 int i, sum_u, sum; 156 int i, sum_u, sum;
156#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY 157#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
157 int sum_s; 158 int sum_s;
@@ -253,10 +254,10 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
253 * POSIX says that checksum is done on unsigned bytes, but 254 * POSIX says that checksum is done on unsigned bytes, but
254 * Sun and HP-UX gets it wrong... more details in 255 * Sun and HP-UX gets it wrong... more details in
255 * GNU tar source. */ 256 * GNU tar source. */
257 sum_u = ' ' * sizeof(tar.chksum);
256#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY 258#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
257 sum_s = ' ' * sizeof(tar.chksum); 259 sum_s = sum_u;
258#endif 260#endif
259 sum_u = ' ' * sizeof(tar.chksum);
260 for (i = 0; i < 148; i++) { 261 for (i = 0; i < 148; i++) {
261 sum_u += ((unsigned char*)&tar)[i]; 262 sum_u += ((unsigned char*)&tar)[i];
262#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY 263#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
@@ -269,27 +270,22 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
269 sum_s += ((signed char*)&tar)[i]; 270 sum_s += ((signed char*)&tar)[i];
270#endif 271#endif
271 } 272 }
272 /* This field does not need special treatment (getOctal) */ 273 /* Most tarfiles have tar.chksum NUL or space terminated, but
273 { 274 * github.com decided to be "special" and have unterminated field:
274 char *endp; /* gcc likes temp var for &endp */ 275 * 0090: 30343300 30303031 33323731 30000000 |043.000132710...|
275 sum = strtoul(tar.chksum, &endp, 8); 276 * ^^^^^^^^|
276 if ((*endp != '\0' && *endp != ' ') 277 * Need to use GET_OCTAL. This overwrites tar.typeflag ---+
277 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) 278 * (the '0' char immediately after chksum in example above) with NUL.
278 ) { 279 */
279 bb_error_msg_and_die("invalid tar header checksum"); 280 tar_typeflag = (uint8_t)tar.typeflag; /* save it */
280 } 281 sum = GET_OCTAL(tar.chksum);
281 } 282 if (sum_u != sum
282 /* don't use xstrtoul, tar.chksum may have leading spaces */ 283 IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)
283 sum = strtoul(tar.chksum, NULL, 8); 284 ) {
284 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
285 bb_error_msg_and_die("invalid tar header checksum"); 285 bb_error_msg_and_die("invalid tar header checksum");
286 } 286 }
287 287
288 /* 0 is reserved for high perf file, treat as normal file */ 288 /* GET_OCTAL trashes subsequent field, therefore we call it
289 if (!tar.typeflag) tar.typeflag = '0';
290 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
291
292 /* getOctal trashes subsequent field, therefore we call it
293 * on fields in reverse order */ 289 * on fields in reverse order */
294 if (tar.devmajor[0]) { 290 if (tar.devmajor[0]) {
295 char t = tar.prefix[0]; 291 char t = tar.prefix[0];
@@ -299,6 +295,11 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
299 file_header->device = makedev(major, minor); 295 file_header->device = makedev(major, minor);
300 tar.prefix[0] = t; 296 tar.prefix[0] = t;
301 } 297 }
298
299 /* 0 is reserved for high perf file, treat as normal file */
300 if (tar_typeflag == '\0') tar_typeflag = '0';
301 parse_names = (tar_typeflag >= '0' && tar_typeflag <= '7');
302
302 file_header->link_target = NULL; 303 file_header->link_target = NULL;
303 if (!p_linkname && parse_names && tar.linkname[0]) { 304 if (!p_linkname && parse_names && tar.linkname[0]) {
304 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname)); 305 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
@@ -332,7 +333,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
332 333
333 /* Set bits 12-15 of the files mode */ 334 /* Set bits 12-15 of the files mode */
334 /* (typeflag was not trashed because chksum does not use getOctal) */ 335 /* (typeflag was not trashed because chksum does not use getOctal) */
335 switch (tar.typeflag) { 336 switch (tar_typeflag) {
336 case '1': /* hardlink */ 337 case '1': /* hardlink */
337 /* we mark hardlinks as regular files with zero size and a link name */ 338 /* we mark hardlinks as regular files with zero size and a link name */
338 file_header->mode |= S_IFREG; 339 file_header->mode |= S_IFREG;
@@ -381,7 +382,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
381 case 'x': { /* pax extended header */ 382 case 'x': { /* pax extended header */
382 if ((uoff_t)file_header->size > 0xfffff) /* paranoia */ 383 if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
383 goto skip_ext_hdr; 384 goto skip_ext_hdr;
384 process_pax_hdr(archive_handle, file_header->size, (tar.typeflag == 'g')); 385 process_pax_hdr(archive_handle, file_header->size, (tar_typeflag == 'g'));
385 goto again_after_align; 386 goto again_after_align;
386#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS 387#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
387/* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */ 388/* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */
@@ -419,7 +420,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
419 skip_ext_hdr: 420 skip_ext_hdr:
420 { 421 {
421 off_t sz; 422 off_t sz;
422 bb_error_msg("warning: skipping header '%c'", tar.typeflag); 423 bb_error_msg("warning: skipping header '%c'", tar_typeflag);
423 sz = (file_header->size + 511) & ~(off_t)511; 424 sz = (file_header->size + 511) & ~(off_t)511;
424 archive_handle->offset += sz; 425 archive_handle->offset += sz;
425 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */ 426 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
@@ -429,7 +430,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
429 goto again_after_align; 430 goto again_after_align;
430 } 431 }
431 default: 432 default:
432 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag); 433 bb_error_msg_and_die("unknown typeflag: 0x%x", tar_typeflag);
433 } 434 }
434 435
435#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS 436#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
diff --git a/archival/libarchive/lzo1x_d.c b/archival/libarchive/lzo1x_d.c
index 40b167e68..43cf4a04e 100644
--- a/archival/libarchive/lzo1x_d.c
+++ b/archival/libarchive/lzo1x_d.c
@@ -31,8 +31,7 @@
31************************************************************************/ 31************************************************************************/
32/* safe decompression with overrun testing */ 32/* safe decompression with overrun testing */
33int lzo1x_decompress_safe(const uint8_t* in, unsigned in_len, 33int lzo1x_decompress_safe(const uint8_t* in, unsigned in_len,
34 uint8_t* out, unsigned* out_len, 34 uint8_t* out, unsigned* out_len /*, void* wrkmem */)
35 void* wrkmem UNUSED_PARAM)
36{ 35{
37 register uint8_t* op; 36 register uint8_t* op;
38 register const uint8_t* ip; 37 register const uint8_t* ip;