Commit 6f9d59b60ded28890b42692c0ba12ab3cbb57162

Authored by Imanol-Mikel Barba Sabariego
1 parent af20c8c8

Fixed compression functions

compression.c
1 #include "compression.h" 1 #include "compression.h"
2 2
3 -ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { 3 +ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData, int headerless) {
4 unsigned int increase = compDataLen/2; 4 unsigned int increase = compDataLen/2;
5 unsigned int uncompLength = compDataLen; // Later to be increased 5 unsigned int uncompLength = compDataLen; // Later to be increased
6 6
@@ -13,13 +13,19 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { @@ -13,13 +13,19 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) {
13 strm.zalloc = Z_NULL; 13 strm.zalloc = Z_NULL;
14 strm.zfree = Z_NULL; 14 strm.zfree = Z_NULL;
15 15
16 - if (inflateInit2(&strm, (16+MAX_WBITS)) != Z_OK) { 16 + int err = Z_OK;
  17 + if(headerless) {
  18 + err = inflateInit(&strm);
  19 + } else {
  20 + err = inflateInit2(&strm, 16+MAX_WBITS);
  21 + }
  22 +
  23 + if(err != Z_OK) {
17 fprintf(stderr, "Unable to initialize zlib zstream for decompression\n"); 24 fprintf(stderr, "Unable to initialize zlib zstream for decompression\n");
18 free(uncomp); 25 free(uncomp);
19 return -1; 26 return -1;
20 - } 27 + }
21 28
22 - int err = Z_OK;  
23 do { 29 do {
24 // If our output buffer is too small 30 // If our output buffer is too small
25 if(strm.total_out >= uncompLength ) { 31 if(strm.total_out >= uncompLength ) {
@@ -41,7 +47,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { @@ -41,7 +47,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) {
41 // Inflate another chunk. 47 // Inflate another chunk.
42 err = inflate (&strm, Z_SYNC_FLUSH); 48 err = inflate (&strm, Z_SYNC_FLUSH);
43 if(err != Z_OK && err != Z_STREAM_END) { 49 if(err != Z_OK && err != Z_STREAM_END) {
44 - fprintf(stderr, "zlib error: %d.\n", err); 50 + fprintf(stderr, "Error while inflating buffer: %d - %s.\n", err,strm.msg);
45 inflateEnd(&strm); 51 inflateEnd(&strm);
46 free(uncomp); 52 free(uncomp);
47 return -3; 53 return -3;
@@ -59,7 +65,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { @@ -59,7 +65,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) {
59 return uncompLength; 65 return uncompLength;
60 } 66 }
61 67
62 -ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) { 68 +ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData, int headerless) {
63 unsigned int compLength = unCompDataLen; 69 unsigned int compLength = unCompDataLen;
64 unsigned int increase = compLength/4; 70 unsigned int increase = compLength/4;
65 char* comp = (char*)calloc(compLength, sizeof(char)); 71 char* comp = (char*)calloc(compLength, sizeof(char));
@@ -70,14 +76,20 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) { @@ -70,14 +76,20 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) {
70 strm.total_out = 0; 76 strm.total_out = 0;
71 strm.zalloc = Z_NULL; 77 strm.zalloc = Z_NULL;
72 strm.zfree = Z_NULL; 78 strm.zfree = Z_NULL;
  79 +
  80 + int err = Z_OK;
  81 + if(headerless) {
  82 + err = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
  83 + } else {
  84 + err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 16+MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
  85 + }
73 86
74 - if(deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (16+MAX_WBITS),8 , Z_DEFAULT_STRATEGY) != Z_OK) { 87 + if(err != Z_OK) {
75 fprintf(stderr, "Unable to initialize zlib zstream for compression\n"); 88 fprintf(stderr, "Unable to initialize zlib zstream for compression\n");
76 free(comp); 89 free(comp);
77 return -1; 90 return -1;
78 } 91 }
79 92
80 - int err = Z_OK;  
81 do { 93 do {
82 // If our output buffer is too small 94 // If our output buffer is too small
83 if (strm.total_out >= compLength) { 95 if (strm.total_out >= compLength) {
@@ -99,7 +111,7 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) { @@ -99,7 +111,7 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) {
99 // deflate another chunk 111 // deflate another chunk
100 err = deflate(&strm, Z_FINISH); 112 err = deflate(&strm, Z_FINISH);
101 if(err != Z_OK && err != Z_STREAM_END) { 113 if(err != Z_OK && err != Z_STREAM_END) {
102 - fprintf(stderr, "Error while deflating buffer\n"); 114 + fprintf(stderr, "Error while deflating buffer: %d - %s\n",err,strm.msg);
103 deflateEnd(&strm); 115 deflateEnd(&strm);
104 free(comp); 116 free(comp);
105 return -3; 117 return -3;
compression.h
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 7
8 #define OS_FLAG_OFFSET 0x9 8 #define OS_FLAG_OFFSET 0x9
9 9
10 -ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData);  
11 -ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData); 10 +ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData, int headerless);
  11 +ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData, int headerless);
12 12
13 #endif 13 #endif
14 \ No newline at end of file 14 \ No newline at end of file
@@ -48,7 +48,7 @@ ssize_t loadDB(const char* filename, void** data) { @@ -48,7 +48,7 @@ ssize_t loadDB(const char* filename, void** data) {
48 48
49 if(*(uint16_t*)filedata == GZIP_MAGIC) { 49 if(*(uint16_t*)filedata == GZIP_MAGIC) {
50 void* decompressedFileData; 50 void* decompressedFileData;
51 - filesize = inflateGzip(filedata,filesize,&decompressedFileData); 51 + filesize = inflateGzip(filedata,filesize,&decompressedFileData,0);
52 free(filedata); 52 free(filedata);
53 filedata = decompressedFileData; 53 filedata = decompressedFileData;
54 } 54 }