summaryrefslogtreecommitdiffstats
path: root/bzip2.c
diff options
context:
space:
mode:
Diffstat (limited to 'bzip2.c')
-rw-r--r--bzip2.c158
1 files changed, 83 insertions, 75 deletions
diff --git a/bzip2.c b/bzip2.c
index 0fb45fb..53ce10d 100644
--- a/bzip2.c
+++ b/bzip2.c
@@ -5,7 +5,7 @@
5 5
6/*-- 6/*--
7 This program is bzip2, a lossless, block-sorting data compressor, 7 This program is bzip2, a lossless, block-sorting data compressor,
8 version 0.1pl0, dated 17-Aug-1997. 8 version 0.1pl2, dated 29-Aug-1997.
9 9
10 Copyright (C) 1996, 1997 by Julian Seward. 10 Copyright (C) 1996, 1997 by Julian Seward.
11 Guildford, Surrey, UK 11 Guildford, Surrey, UK
@@ -71,6 +71,12 @@
71 That is not to say this program is inherently unreliable. 71 That is not to say this program is inherently unreliable.
72 Indeed, I very much hope the opposite is true. bzip2 has been 72 Indeed, I very much hope the opposite is true. bzip2 has been
73 carefully constructed and extensively tested. 73 carefully constructed and extensively tested.
74
75 PATENTS:
76 To the best of my knowledge, bzip2 does not use any patented
77 algorithms. However, I do not have the resources available to
78 carry out a full patent search. Therefore I cannot give any
79 guarantee of the above statement.
74--*/ 80--*/
75 81
76 82
@@ -111,7 +117,6 @@
111#endif 117#endif
112#include <string.h> 118#include <string.h>
113#include <signal.h> 119#include <signal.h>
114#include <errno.h>
115#include <math.h> 120#include <math.h>
116 121
117#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); } 122#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
@@ -125,6 +130,7 @@
125--*/ 130--*/
126 131
127#if BZ_UNIX 132#if BZ_UNIX
133 #include <sys/types.h>
128 #include <utime.h> 134 #include <utime.h>
129 #include <unistd.h> 135 #include <unistd.h>
130 #include <malloc.h> 136 #include <malloc.h>
@@ -512,77 +518,77 @@ UInt32 crc32Table[256] = {
512 518
513 /*-- Ugly, innit? --*/ 519 /*-- Ugly, innit? --*/
514 520
515 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L, 521 0x00000000UL, 0x04c11db7UL, 0x09823b6eUL, 0x0d4326d9UL,
516 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L, 522 0x130476dcUL, 0x17c56b6bUL, 0x1a864db2UL, 0x1e475005UL,
517 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L, 523 0x2608edb8UL, 0x22c9f00fUL, 0x2f8ad6d6UL, 0x2b4bcb61UL,
518 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL, 524 0x350c9b64UL, 0x31cd86d3UL, 0x3c8ea00aUL, 0x384fbdbdUL,
519 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L, 525 0x4c11db70UL, 0x48d0c6c7UL, 0x4593e01eUL, 0x4152fda9UL,
520 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L, 526 0x5f15adacUL, 0x5bd4b01bUL, 0x569796c2UL, 0x52568b75UL,
521 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L, 527 0x6a1936c8UL, 0x6ed82b7fUL, 0x639b0da6UL, 0x675a1011UL,
522 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL, 528 0x791d4014UL, 0x7ddc5da3UL, 0x709f7b7aUL, 0x745e66cdUL,
523 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L, 529 0x9823b6e0UL, 0x9ce2ab57UL, 0x91a18d8eUL, 0x95609039UL,
524 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L, 530 0x8b27c03cUL, 0x8fe6dd8bUL, 0x82a5fb52UL, 0x8664e6e5UL,
525 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L, 531 0xbe2b5b58UL, 0xbaea46efUL, 0xb7a96036UL, 0xb3687d81UL,
526 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL, 532 0xad2f2d84UL, 0xa9ee3033UL, 0xa4ad16eaUL, 0xa06c0b5dUL,
527 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L, 533 0xd4326d90UL, 0xd0f37027UL, 0xddb056feUL, 0xd9714b49UL,
528 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L, 534 0xc7361b4cUL, 0xc3f706fbUL, 0xceb42022UL, 0xca753d95UL,
529 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L, 535 0xf23a8028UL, 0xf6fb9d9fUL, 0xfbb8bb46UL, 0xff79a6f1UL,
530 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL, 536 0xe13ef6f4UL, 0xe5ffeb43UL, 0xe8bccd9aUL, 0xec7dd02dUL,
531 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL, 537 0x34867077UL, 0x30476dc0UL, 0x3d044b19UL, 0x39c556aeUL,
532 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L, 538 0x278206abUL, 0x23431b1cUL, 0x2e003dc5UL, 0x2ac12072UL,
533 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L, 539 0x128e9dcfUL, 0x164f8078UL, 0x1b0ca6a1UL, 0x1fcdbb16UL,
534 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL, 540 0x018aeb13UL, 0x054bf6a4UL, 0x0808d07dUL, 0x0cc9cdcaUL,
535 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL, 541 0x7897ab07UL, 0x7c56b6b0UL, 0x71159069UL, 0x75d48ddeUL,
536 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L, 542 0x6b93dddbUL, 0x6f52c06cUL, 0x6211e6b5UL, 0x66d0fb02UL,
537 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L, 543 0x5e9f46bfUL, 0x5a5e5b08UL, 0x571d7dd1UL, 0x53dc6066UL,
538 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL, 544 0x4d9b3063UL, 0x495a2dd4UL, 0x44190b0dUL, 0x40d816baUL,
539 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL, 545 0xaca5c697UL, 0xa864db20UL, 0xa527fdf9UL, 0xa1e6e04eUL,
540 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L, 546 0xbfa1b04bUL, 0xbb60adfcUL, 0xb6238b25UL, 0xb2e29692UL,
541 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L, 547 0x8aad2b2fUL, 0x8e6c3698UL, 0x832f1041UL, 0x87ee0df6UL,
542 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL, 548 0x99a95df3UL, 0x9d684044UL, 0x902b669dUL, 0x94ea7b2aUL,
543 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL, 549 0xe0b41de7UL, 0xe4750050UL, 0xe9362689UL, 0xedf73b3eUL,
544 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L, 550 0xf3b06b3bUL, 0xf771768cUL, 0xfa325055UL, 0xfef34de2UL,
545 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L, 551 0xc6bcf05fUL, 0xc27dede8UL, 0xcf3ecb31UL, 0xcbffd686UL,
546 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL, 552 0xd5b88683UL, 0xd1799b34UL, 0xdc3abdedUL, 0xd8fba05aUL,
547 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L, 553 0x690ce0eeUL, 0x6dcdfd59UL, 0x608edb80UL, 0x644fc637UL,
548 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL, 554 0x7a089632UL, 0x7ec98b85UL, 0x738aad5cUL, 0x774bb0ebUL,
549 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL, 555 0x4f040d56UL, 0x4bc510e1UL, 0x46863638UL, 0x42472b8fUL,
550 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L, 556 0x5c007b8aUL, 0x58c1663dUL, 0x558240e4UL, 0x51435d53UL,
551 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L, 557 0x251d3b9eUL, 0x21dc2629UL, 0x2c9f00f0UL, 0x285e1d47UL,
552 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL, 558 0x36194d42UL, 0x32d850f5UL, 0x3f9b762cUL, 0x3b5a6b9bUL,
553 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL, 559 0x0315d626UL, 0x07d4cb91UL, 0x0a97ed48UL, 0x0e56f0ffUL,
554 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L, 560 0x1011a0faUL, 0x14d0bd4dUL, 0x19939b94UL, 0x1d528623UL,
555 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L, 561 0xf12f560eUL, 0xf5ee4bb9UL, 0xf8ad6d60UL, 0xfc6c70d7UL,
556 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL, 562 0xe22b20d2UL, 0xe6ea3d65UL, 0xeba91bbcUL, 0xef68060bUL,
557 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL, 563 0xd727bbb6UL, 0xd3e6a601UL, 0xdea580d8UL, 0xda649d6fUL,
558 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L, 564 0xc423cd6aUL, 0xc0e2d0ddUL, 0xcda1f604UL, 0xc960ebb3UL,
559 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L, 565 0xbd3e8d7eUL, 0xb9ff90c9UL, 0xb4bcb610UL, 0xb07daba7UL,
560 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL, 566 0xae3afba2UL, 0xaafbe615UL, 0xa7b8c0ccUL, 0xa379dd7bUL,
561 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL, 567 0x9b3660c6UL, 0x9ff77d71UL, 0x92b45ba8UL, 0x9675461fUL,
562 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L, 568 0x8832161aUL, 0x8cf30badUL, 0x81b02d74UL, 0x857130c3UL,
563 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L, 569 0x5d8a9099UL, 0x594b8d2eUL, 0x5408abf7UL, 0x50c9b640UL,
564 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL, 570 0x4e8ee645UL, 0x4a4ffbf2UL, 0x470cdd2bUL, 0x43cdc09cUL,
565 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L, 571 0x7b827d21UL, 0x7f436096UL, 0x7200464fUL, 0x76c15bf8UL,
566 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L, 572 0x68860bfdUL, 0x6c47164aUL, 0x61043093UL, 0x65c52d24UL,
567 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L, 573 0x119b4be9UL, 0x155a565eUL, 0x18197087UL, 0x1cd86d30UL,
568 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL, 574 0x029f3d35UL, 0x065e2082UL, 0x0b1d065bUL, 0x0fdc1becUL,
569 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L, 575 0x3793a651UL, 0x3352bbe6UL, 0x3e119d3fUL, 0x3ad08088UL,
570 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L, 576 0x2497d08dUL, 0x2056cd3aUL, 0x2d15ebe3UL, 0x29d4f654UL,
571 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L, 577 0xc5a92679UL, 0xc1683bceUL, 0xcc2b1d17UL, 0xc8ea00a0UL,
572 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL, 578 0xd6ad50a5UL, 0xd26c4d12UL, 0xdf2f6bcbUL, 0xdbee767cUL,
573 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L, 579 0xe3a1cbc1UL, 0xe760d676UL, 0xea23f0afUL, 0xeee2ed18UL,
574 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L, 580 0xf0a5bd1dUL, 0xf464a0aaUL, 0xf9278673UL, 0xfde69bc4UL,
575 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L, 581 0x89b8fd09UL, 0x8d79e0beUL, 0x803ac667UL, 0x84fbdbd0UL,
576 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL, 582 0x9abc8bd5UL, 0x9e7d9662UL, 0x933eb0bbUL, 0x97ffad0cUL,
577 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L, 583 0xafb010b1UL, 0xab710d06UL, 0xa6322bdfUL, 0xa2f33668UL,
578 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L 584 0xbcb4666dUL, 0xb8757bdaUL, 0xb5365d03UL, 0xb1f740b4UL
579}; 585};
580 586
581 587
582/*---------------------------------------------*/ 588/*---------------------------------------------*/
583void initialiseCRC ( void ) 589void initialiseCRC ( void )
584{ 590{
585 globalCrc = 0xffffffffL; 591 globalCrc = 0xffffffffUL;
586} 592}
587 593
588 594
@@ -2579,7 +2585,7 @@ INLINE Int32 getRLEpair ( FILE* src )
2579 2585
2580 /*--- Because I have no idea what kind of a value EOF is. ---*/ 2586 /*--- Because I have no idea what kind of a value EOF is. ---*/
2581 if (ch == EOF) { 2587 if (ch == EOF) {
2582 ERROR_IF_NOT_ZERO ( errno ); 2588 ERROR_IF_NOT_ZERO ( ferror(src));
2583 return (1 << 16) | MY_EOF; 2589 return (1 << 16) | MY_EOF;
2584 } 2590 }
2585 2591
@@ -2595,7 +2601,7 @@ INLINE Int32 getRLEpair ( FILE* src )
2595 if ( ungetc ( chLatest, src ) == EOF ) 2601 if ( ungetc ( chLatest, src ) == EOF )
2596 panic ( "getRLEpair: ungetc failed" ); 2602 panic ( "getRLEpair: ungetc failed" );
2597 } else { 2603 } else {
2598 ERROR_IF_NOT_ZERO ( errno ); 2604 ERROR_IF_NOT_ZERO ( ferror(src) );
2599 } 2605 }
2600 2606
2601 /*--- Conditional is just a speedup hack. ---*/ 2607 /*--- Conditional is just a speedup hack. ---*/
@@ -3442,7 +3448,6 @@ void compress ( Char *name )
3442 } 3448 }
3443 3449
3444 /*--- Now the input and output handles are sane. Do the Biz. ---*/ 3450 /*--- Now the input and output handles are sane. Do the Biz. ---*/
3445 errno = 0;
3446 outputHandleJustInCase = outStr; 3451 outputHandleJustInCase = outStr;
3447 compressStream ( inStr, outStr ); 3452 compressStream ( inStr, outStr );
3448 outputHandleJustInCase = NULL; 3453 outputHandleJustInCase = NULL;
@@ -3559,7 +3564,6 @@ void uncompress ( Char *name )
3559 } 3564 }
3560 3565
3561 /*--- Now the input and output handles are sane. Do the Biz. ---*/ 3566 /*--- Now the input and output handles are sane. Do the Biz. ---*/
3562 errno = 0;
3563 outputHandleJustInCase = outStr; 3567 outputHandleJustInCase = outStr;
3564 magicNumberOK = uncompressStream ( inStr, outStr ); 3568 magicNumberOK = uncompressStream ( inStr, outStr );
3565 outputHandleJustInCase = NULL; 3569 outputHandleJustInCase = NULL;
@@ -3667,7 +3671,6 @@ void testf ( Char *name )
3667 } 3671 }
3668 3672
3669 /*--- Now the input handle is sane. Do the Biz. ---*/ 3673 /*--- Now the input handle is sane. Do the Biz. ---*/
3670 errno = 0;
3671 allOK = testStream ( inStr ); 3674 allOK = testStream ( inStr );
3672 3675
3673 if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" ); 3676 if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
@@ -3681,7 +3684,7 @@ void license ( void )
3681 fprintf ( stderr, 3684 fprintf ( stderr,
3682 3685
3683 "bzip2, a block-sorting file compressor. " 3686 "bzip2, a block-sorting file compressor. "
3684 "Version 0.1pl0, 17-Aug-97.\n" 3687 "Version 0.1pl2, 29-Aug-97.\n"
3685 " \n" 3688 " \n"
3686 " Copyright (C) 1996, 1997 by Julian Seward.\n" 3689 " Copyright (C) 1996, 1997 by Julian Seward.\n"
3687 " \n" 3690 " \n"
@@ -3711,7 +3714,7 @@ void usage ( Char *fullProgName )
3711 fprintf ( 3714 fprintf (
3712 stderr, 3715 stderr,
3713 "bzip2, a block-sorting file compressor. " 3716 "bzip2, a block-sorting file compressor. "
3714 "Version 0.1pl0, 17-Aug-97.\n" 3717 "Version 0.1pl2, 29-Aug-97.\n"
3715 "\n usage: %s [flags and input files in any order]\n" 3718 "\n usage: %s [flags and input files in any order]\n"
3716 "\n" 3719 "\n"
3717 " -h --help print this message\n" 3720 " -h --help print this message\n"
@@ -3864,7 +3867,6 @@ IntNative main ( IntNative argc, Char *argv[] )
3864 tt = NULL; 3867 tt = NULL;
3865 block = NULL; 3868 block = NULL;
3866 zptr = NULL; 3869 zptr = NULL;
3867 errno = 0;
3868 smallMode = False; 3870 smallMode = False;
3869 keepInputFiles = False; 3871 keepInputFiles = False;
3870 verbosity = 0; 3872 verbosity = 0;
@@ -3980,6 +3982,12 @@ IntNative main ( IntNative argc, Char *argv[] )
3980 exit ( 1 ); 3982 exit ( 1 );
3981 } 3983 }
3982 3984
3985 if (srcMode == SM_F2O && numFileNames == 0) {
3986 fprintf ( stderr, "%s: -c expects at least one filename.\n",
3987 progName );
3988 exit ( 1 );
3989 }
3990
3983 if (opMode == OM_TEST && srcMode == SM_F2O) { 3991 if (opMode == OM_TEST && srcMode == SM_F2O) {
3984 fprintf ( stderr, "%s: -c and -t cannot be used together.\n", 3992 fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
3985 progName ); 3993 progName );