diff --git a/src/zopfli/cache.c b/src/zopfli/cache.c index 88a49ac3..e04a1c1c 100644 --- a/src/zopfli/cache.c +++ b/src/zopfli/cache.c @@ -31,6 +31,10 @@ void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) { lmc->dist = (unsigned short*)malloc(sizeof(unsigned short) * blocksize); /* Rather large amount of memory. */ lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize); + if(lmc->sublen == NULL) { + fprintf(stderr,"Error: Out of memory. Tried allocating %lu bytes of memory.\n",(unsigned long)(ZOPFLI_CACHE_LENGTH * 3 * blocksize)); + exit (EXIT_FAILURE); + } /* length > 0 and dist 0 is invalid combination, which indicates on purpose that this cache value is not filled in yet. */ diff --git a/src/zopfli/deflate.c b/src/zopfli/deflate.c index e918f195..2ffe0923 100644 --- a/src/zopfli/deflate.c +++ b/src/zopfli/deflate.c @@ -844,6 +844,7 @@ void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final, void ZopfliDeflate(const ZopfliOptions* options, int btype, int final, const unsigned char* in, size_t insize, unsigned char* bp, unsigned char** out, size_t* outsize) { + size_t offset = *outsize; #if ZOPFLI_MASTER_BLOCK_SIZE == 0 ZopfliDeflatePart(options, btype, final, in, 0, insize, bp, out, outsize); #else @@ -860,7 +861,7 @@ void ZopfliDeflate(const ZopfliOptions* options, int btype, int final, if (options->verbose) { fprintf(stderr, "Original Size: %d, Deflate: %d, Compression: %f%% Removed\n", - (int)insize, (int)*outsize, - 100.0 * (double)(insize - *outsize) / (double)insize); + (int)insize, (int)(*outsize - offset), + 100.0 * (double)(insize - (*outsize - offset)) / (double)insize); } } diff --git a/src/zopfli/zopfli_bin.c b/src/zopfli/zopfli_bin.c index cf0b6917..747828db 100644 --- a/src/zopfli/zopfli_bin.c +++ b/src/zopfli/zopfli_bin.c @@ -33,6 +33,11 @@ decompressor. #include "gzip_container.h" #include "zlib_container.h" +/* Windows workaround for stdout output. */ +#if _WIN32 +#include +#endif + /* Loads a file into a memory array. */ @@ -47,6 +52,10 @@ static void LoadFile(const char* filename, fseek(file , 0 , SEEK_END); *outsize = ftell(file); + if(*outsize > 2147483647) { + fprintf(stderr,"Files larger than 2GB are not supported.\n"); + exit(EXIT_FAILURE); + } rewind(file); *out = (unsigned char*)malloc(*outsize); @@ -71,6 +80,10 @@ Saves a file from a memory array, overwriting the file if it existed. static void SaveFile(const char* filename, const unsigned char* in, size_t insize) { FILE* file = fopen(filename, "wb" ); + if (file == NULL) { + fprintf(stderr,"Error: Cannot write to output file, terminating.\n"); + exit (EXIT_FAILURE); + } assert(file); fwrite((char*)in, 1, insize, file); fclose(file); @@ -99,10 +112,17 @@ static void CompressFile(const ZopfliOptions* options, SaveFile(outfilename, out, outsize); } else { size_t i; +/* Windows workaround for stdout output. */ +#if _WIN32 + _setmode(_fileno(stdout), _O_BINARY); +#endif for (i = 0; i < outsize; i++) { /* Works only if terminal does not convert newlines. */ printf("%c", out[i]); } +#if _WIN32 + _setmode(_fileno(stdout), _O_TEXT); +#endif } free(out); @@ -168,7 +188,7 @@ int main(int argc, char* argv[]) { } if (options.numiterations < 1) { - fprintf(stderr, "Error: must have 1 or more iterations"); + fprintf(stderr, "Error: must have 1 or more iterations\n"); return 0; }