Skip to content

Commit d20ea3c

Browse files
committed
Merge pull request #56 from MrKrzYch00/original
Various fixes
2 parents 6ff3ba2 + 2c34d0d commit d20ea3c

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/zopfli/cache.c

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) {
3131
lmc->dist = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
3232
/* Rather large amount of memory. */
3333
lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize);
34+
if(lmc->sublen == NULL) {
35+
fprintf(stderr,"Error: Out of memory. Tried allocating %lu bytes of memory.\n",(unsigned long)(ZOPFLI_CACHE_LENGTH * 3 * blocksize));
36+
exit (EXIT_FAILURE);
37+
}
3438

3539
/* length > 0 and dist 0 is invalid combination, which indicates on purpose
3640
that this cache value is not filled in yet. */

src/zopfli/deflate.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final,
844844
void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
845845
const unsigned char* in, size_t insize,
846846
unsigned char* bp, unsigned char** out, size_t* outsize) {
847+
size_t offset = *outsize;
847848
#if ZOPFLI_MASTER_BLOCK_SIZE == 0
848849
ZopfliDeflatePart(options, btype, final, in, 0, insize, bp, out, outsize);
849850
#else
@@ -860,7 +861,7 @@ void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
860861
if (options->verbose) {
861862
fprintf(stderr,
862863
"Original Size: %d, Deflate: %d, Compression: %f%% Removed\n",
863-
(int)insize, (int)*outsize,
864-
100.0 * (double)(insize - *outsize) / (double)insize);
864+
(int)insize, (int)(*outsize - offset),
865+
100.0 * (double)(insize - (*outsize - offset)) / (double)insize);
865866
}
866867
}

src/zopfli/zopfli_bin.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ decompressor.
3333
#include "gzip_container.h"
3434
#include "zlib_container.h"
3535

36+
/* Windows workaround for stdout output. */
37+
#if _WIN32
38+
#include <fcntl.h>
39+
#endif
40+
3641
/*
3742
Loads a file into a memory array.
3843
*/
@@ -47,6 +52,10 @@ static void LoadFile(const char* filename,
4752

4853
fseek(file , 0 , SEEK_END);
4954
*outsize = ftell(file);
55+
if(*outsize > 2147483647) {
56+
fprintf(stderr,"Files larger than 2GB are not supported.\n");
57+
exit(EXIT_FAILURE);
58+
}
5059
rewind(file);
5160

5261
*out = (unsigned char*)malloc(*outsize);
@@ -71,6 +80,10 @@ Saves a file from a memory array, overwriting the file if it existed.
7180
static void SaveFile(const char* filename,
7281
const unsigned char* in, size_t insize) {
7382
FILE* file = fopen(filename, "wb" );
83+
if (file == NULL) {
84+
fprintf(stderr,"Error: Cannot write to output file, terminating.\n");
85+
exit (EXIT_FAILURE);
86+
}
7487
assert(file);
7588
fwrite((char*)in, 1, insize, file);
7689
fclose(file);
@@ -99,10 +112,17 @@ static void CompressFile(const ZopfliOptions* options,
99112
SaveFile(outfilename, out, outsize);
100113
} else {
101114
size_t i;
115+
/* Windows workaround for stdout output. */
116+
#if _WIN32
117+
_setmode(_fileno(stdout), _O_BINARY);
118+
#endif
102119
for (i = 0; i < outsize; i++) {
103120
/* Works only if terminal does not convert newlines. */
104121
printf("%c", out[i]);
105122
}
123+
#if _WIN32
124+
_setmode(_fileno(stdout), _O_TEXT);
125+
#endif
106126
}
107127

108128
free(out);
@@ -168,7 +188,7 @@ int main(int argc, char* argv[]) {
168188
}
169189

170190
if (options.numiterations < 1) {
171-
fprintf(stderr, "Error: must have 1 or more iterations");
191+
fprintf(stderr, "Error: must have 1 or more iterations\n");
172192
return 0;
173193
}
174194

0 commit comments

Comments
 (0)