Skip to content

Commit

Permalink
Merge pull request #56 from MrKrzYch00/original
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
lvandeve committed Aug 21, 2015
2 parents 6ff3ba2 + 2c34d0d commit d20ea3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/zopfli/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
5 changes: 3 additions & 2 deletions src/zopfli/deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
22 changes: 21 additions & 1 deletion src/zopfli/zopfli_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ decompressor.
#include "gzip_container.h"
#include "zlib_container.h"

/* Windows workaround for stdout output. */
#if _WIN32
#include <fcntl.h>
#endif

/*
Loads a file into a memory array.
*/
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit d20ea3c

Please sign in to comment.