Commit 6f9d59b60ded28890b42692c0ba12ab3cbb57162
1 parent
af20c8c8
Fixed compression functions
Showing
3 changed files
with
24 additions
and
12 deletions
compression.c
1 | 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 | 4 | unsigned int increase = compDataLen/2; |
5 | 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 | strm.zalloc = Z_NULL; |
14 | 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 | 24 | fprintf(stderr, "Unable to initialize zlib zstream for decompression\n"); |
18 | 25 | free(uncomp); |
19 | 26 | return -1; |
20 | - } | |
27 | + } | |
21 | 28 | |
22 | - int err = Z_OK; | |
23 | 29 | do { |
24 | 30 | // If our output buffer is too small |
25 | 31 | if(strm.total_out >= uncompLength ) { |
... | ... | @@ -41,7 +47,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { |
41 | 47 | // Inflate another chunk. |
42 | 48 | err = inflate (&strm, Z_SYNC_FLUSH); |
43 | 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 | 51 | inflateEnd(&strm); |
46 | 52 | free(uncomp); |
47 | 53 | return -3; |
... | ... | @@ -59,7 +65,7 @@ ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) { |
59 | 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 | 69 | unsigned int compLength = unCompDataLen; |
64 | 70 | unsigned int increase = compLength/4; |
65 | 71 | char* comp = (char*)calloc(compLength, sizeof(char)); |
... | ... | @@ -70,14 +76,20 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) { |
70 | 76 | strm.total_out = 0; |
71 | 77 | strm.zalloc = Z_NULL; |
72 | 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 | 88 | fprintf(stderr, "Unable to initialize zlib zstream for compression\n"); |
76 | 89 | free(comp); |
77 | 90 | return -1; |
78 | 91 | } |
79 | 92 | |
80 | - int err = Z_OK; | |
81 | 93 | do { |
82 | 94 | // If our output buffer is too small |
83 | 95 | if (strm.total_out >= compLength) { |
... | ... | @@ -99,7 +111,7 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) { |
99 | 111 | // deflate another chunk |
100 | 112 | err = deflate(&strm, Z_FINISH); |
101 | 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 | 115 | deflateEnd(&strm); |
104 | 116 | free(comp); |
105 | 117 | return -3; | ... | ... |
compression.h
... | ... | @@ -7,7 +7,7 @@ |
7 | 7 | |
8 | 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 | 13 | #endif |
14 | 14 | \ No newline at end of file | ... | ... |
nbt.c
... | ... | @@ -48,7 +48,7 @@ ssize_t loadDB(const char* filename, void** data) { |
48 | 48 | |
49 | 49 | if(*(uint16_t*)filedata == GZIP_MAGIC) { |
50 | 50 | void* decompressedFileData; |
51 | - filesize = inflateGzip(filedata,filesize,&decompressedFileData); | |
51 | + filesize = inflateGzip(filedata,filesize,&decompressedFileData,0); | |
52 | 52 | free(filedata); |
53 | 53 | filedata = decompressedFileData; |
54 | 54 | } | ... | ... |