diff --git a/bzip2.c b/bzip2.c index d95d280619..0c4445cd3e 100644 --- a/bzip2.c +++ b/bzip2.c @@ -150,6 +150,8 @@ ERROR_IF_MINUS_ONE ( retVal ); \ } while ( 0 ) +# define STDERR_FILENO _fileno(stderr) + #endif /* BZ_LCCWIN32 */ @@ -815,10 +817,9 @@ void mySignalCatcher ( IntNative n ) static void mySIGSEGVorSIGBUScatcher ( IntNative n ) { + const char *msg; if (opMode == OM_Z) - fprintf ( - stderr, - "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n" + msg = ": Caught a SIGSEGV or SIGBUS whilst compressing.\n" "\n" " Possible causes are (most likely first):\n" " (1) This computer has unreliable memory or cache hardware\n" @@ -834,12 +835,9 @@ void mySIGSEGVorSIGBUScatcher ( IntNative n ) " bug report should have. If the manual is available on your\n" " system, please try and read it before mailing me. If you don't\n" " have the manual or can't be bothered to read it, mail me anyway.\n" - "\n", - progName ); - else - fprintf ( - stderr, - "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n" + "\n"; + else + msg = ": Caught a SIGSEGV or SIGBUS whilst decompressing.\n" "\n" " Possible causes are (most likely first):\n" " (1) The compressed data is corrupted, and bzip2's usual checks\n" @@ -857,13 +855,25 @@ void mySIGSEGVorSIGBUScatcher ( IntNative n ) " bug report should have. If the manual is available on your\n" " system, please try and read it before mailing me. If you don't\n" " have the manual or can't be bothered to read it, mail me anyway.\n" - "\n", - progName ); + "\n"; + write ( STDERR_FILENO, "\n", 1 ); + write ( STDERR_FILENO, progName, strlen ( progName ) ); + write ( STDERR_FILENO, msg, strlen ( msg ) ); - showFileNames(); - if (opMode == OM_Z) - cleanUpAndFail( 3 ); else - { cadvise(); cleanUpAndFail( 2 ); } + msg = "\tInput file = "; + write ( STDERR_FILENO, msg, strlen (msg) ); + write ( STDERR_FILENO, inName, strlen (inName) ); + write ( STDERR_FILENO, "\n", 1 ); + msg = "\tOutput file = "; + write ( STDERR_FILENO, msg, strlen (msg) ); + write ( STDERR_FILENO, outName, strlen (outName) ); + write ( STDERR_FILENO, "\n", 1 ); + + /* Don't call cleanupAndFail. If we ended up here something went + terribly wrong. Trying to clean up might fail spectacularly. */ + + if (opMode == OM_Z) setExit(3); else setExit(2); + _exit(exitValue); } @@ -1757,8 +1767,8 @@ void addFlagsFromEnvVar ( Cell** argList, Char* varName ) if (p[i] == 0) break; p += i; i = 0; - while (isspace((Int32)(p[0]))) p++; - while (p[i] != 0 && !isspace((Int32)(p[i]))) i++; + while (isspace((UChar)(p[0]))) p++; + while (p[i] != 0 && !isspace((UChar)(p[i]))) i++; if (i > 0) { k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10; for (j = 0; j < k; j++) tmpName[j] = p[j]; @@ -1815,7 +1825,11 @@ IntNative main ( IntNative argc, Char *argv[] ) copyFileName ( inName, (Char*)"(none)" ); copyFileName ( outName, (Char*)"(none)" ); - copyFileName ( progNameReally, argv[0] ); + if (argc >= 1 && argv[0] != NULL) + copyFileName ( progNameReally, argv[0] ); + else + copyFileName ( progNameReally, (Char*)"bzip2" ); + progName = &progNameReally[0]; for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++) if (*tmp == PATH_SEP) progName = tmp + 1; diff --git a/bzip2recover.c b/bzip2recover.c index a8131e0611..e6c0a99875 100644 --- a/bzip2recover.c +++ b/bzip2recover.c @@ -37,7 +37,7 @@ */ #ifdef __GNUC__ typedef unsigned long long int MaybeUInt64; -# define MaybeUInt64_FMT "%Lu" +# define MaybeUInt64_FMT "%llu" #else #ifdef _MSC_VER typedef unsigned __int64 MaybeUInt64; @@ -202,7 +202,9 @@ static Int32 bsGetBit ( BitStream* bs ) bs->buffLive --; return ( ((bs->buffer) >> (bs->buffLive)) & 0x1 ); } else { - Int32 retVal = getc ( bs->handle ); + Int32 retVal; + errno = 0; + retVal = getc ( bs->handle ); if ( retVal == EOF ) { if (errno != 0) readError(); return 2; @@ -309,7 +311,10 @@ Int32 main ( Int32 argc, Char** argv ) UInt32 buffHi, buffLo, blockCRC; Char* p; - strncpy ( progName, argv[0], BZ_MAX_FILENAME-1); + if (argc >= 1 && argv[0] != NULL) + strncpy ( progName, argv[0], BZ_MAX_FILENAME-1); + else + strncpy ( progName, "bzip2recover", BZ_MAX_FILENAME-1); progName[BZ_MAX_FILENAME-1]='\0'; inFileName[0] = outFileName[0] = 0; @@ -394,6 +399,7 @@ Int32 main ( Int32 argc, Char** argv ) bEnd[currBlock] = 0; } if (currBlock > 0 && + bEnd[currBlock] >= bStart[currBlock] && (bEnd[currBlock] - bStart[currBlock]) >= 130) { fprintf ( stderr, " block %d runs from " MaybeUInt64_FMT " to " MaybeUInt64_FMT "\n", @@ -402,7 +408,7 @@ Int32 main ( Int32 argc, Char** argv ) rbEnd[rbCtr] = bEnd[currBlock]; rbCtr++; } - if (currBlock >= BZ_MAX_HANDLED_BLOCKS) + if (currBlock >= BZ_MAX_HANDLED_BLOCKS - 1) tooManyBlocks(BZ_MAX_HANDLED_BLOCKS); currBlock++; @@ -438,7 +444,12 @@ Int32 main ( Int32 argc, Char** argv ) wrBlock = 0; while (True) { b = bsGetBit(bsIn); - if (b == 2) break; + if (b == 2) { + if (outFile != NULL) { + bsClose(bsWr); + } + break; + } buffHi = (buffHi << 1) | (buffLo >> 31); buffLo = (buffLo << 1) | (b & 1); if (bitsRead == 47+rbStart[wrBlock]) @@ -479,7 +490,7 @@ Int32 main ( Int32 argc, Char** argv ) } /* Now split points to the start of the basename. */ ofs = split - outFileName; - sprintf (split, "rec%5d", wrBlock+1); + snprintf (split, BZ_MAX_FILENAME - ofs, "rec%5d", wrBlock+1); for (p = split; *p != 0; p++) if (*p == ' ') *p = '0'; strcat (outFileName, inFileName + ofs); @@ -505,6 +516,8 @@ Int32 main ( Int32 argc, Char** argv ) } } + bsClose ( bsIn ); + fprintf ( stderr, "%s: finished\n", progName ); return 0; } diff --git a/bzlib.c b/bzlib.c index 21786551b6..100873c797 100644 --- a/bzlib.c +++ b/bzlib.c @@ -1408,7 +1408,7 @@ BZFILE * bzopen_or_bzdopen case 's': smallMode = 1; break; default: - if (isdigit((int)(*mode))) { + if (isdigit((unsigned char)(*mode))) { blockSize100k = *mode-BZ_HDR_0; } } diff --git a/bzlib.h b/bzlib.h index 8966a6c580..ac3f1baff0 100644 --- a/bzlib.h +++ b/bzlib.h @@ -22,6 +22,15 @@ #ifndef _BZLIB_H #define _BZLIB_H +#ifndef BZ_NO_STDIO +/* Need a definitition for FILE */ +#include +#endif + +#ifdef _WIN32 +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -70,13 +79,7 @@ typedef #define BZ_EXPORT #endif -#ifndef BZ_NO_STDIO -/* Need a definitition for FILE */ -#include -#endif - #ifdef _WIN32 -# include # ifdef small /* windows.h define small to char */ # undef small diff --git a/compress.c b/compress.c index 5dfa00231b..2dc5dc12f7 100644 --- a/compress.c +++ b/compress.c @@ -321,7 +321,7 @@ void sendMTFValues ( EState* s ) ---*/ for (iter = 0; iter < BZ_N_ITERS; iter++) { - for (t = 0; t < nGroups; t++) fave[t] = 0; + for (t = 0; t < BZ_N_GROUPS; t++) fave[t] = 0; for (t = 0; t < nGroups; t++) for (v = 0; v < alphaSize; v++) @@ -353,7 +353,7 @@ void sendMTFValues ( EState* s ) Calculate the cost of this group as coded by each of the coding tables. --*/ - for (t = 0; t < nGroups; t++) cost[t] = 0; + for (t = 0; t < BZ_N_GROUPS; t++) cost[t] = 0; if (nGroups == 6 && 50 == ge-gs+1) { /*--- fast track the common case ---*/