compression.c
3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "compression.h"
ssize_t inflateGzip(void* compData, size_t compDataLen, void** unCompData) {
unsigned int increase = compDataLen/2;
unsigned int uncompLength = compDataLen; // Later to be increased
char* uncomp = (char*)calloc(uncompLength, sizeof(char));
z_stream strm;
strm.next_in = (Bytef*) compData;
strm.avail_in = compDataLen;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if (inflateInit2(&strm, (16+MAX_WBITS)) != Z_OK) {
fprintf(stderr, "Unable to initialize zlib zstream for decompression\n");
free(uncomp);
return -1;
}
int err = Z_OK;
do {
// If our output buffer is too small
if(strm.total_out >= uncompLength ) {
// Increase size of output buffer
void* newptr = realloc(uncomp,uncompLength + increase);
if(newptr == NULL) {
fprintf(stderr,"Unable to request memory realloc\n");
inflateEnd(&strm);
free(uncomp);
return -2;
}
uncomp = newptr;
uncompLength += increase;
}
strm.next_out = (Bytef *) (uncomp + strm.total_out);
strm.avail_out = uncompLength - strm.total_out;
// Inflate another chunk.
err = inflate (&strm, Z_SYNC_FLUSH);
if(err != Z_OK && err != Z_STREAM_END) {
fprintf(stderr, "zlib error: %d.\n", err);
inflateEnd(&strm);
free(uncomp);
return -3;
}
} while(err != Z_STREAM_END);
uncompLength = strm.total_out;
if(inflateEnd(&strm) != Z_OK) {
fprintf(stderr,"Error while deallocating libz zstream\n");
free(uncomp);
return -4;
}
*unCompData = uncomp;
return uncompLength;
}
ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData) {
unsigned int compLength = unCompDataLen;
unsigned int increase = compLength/4;
char* comp = (char*)calloc(compLength, sizeof(char));
z_stream strm;
strm.next_in = (Bytef*) unCompData;
strm.avail_in = unCompDataLen;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if(deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (16+MAX_WBITS),8 , Z_DEFAULT_STRATEGY) != Z_OK) {
fprintf(stderr, "Unable to initialize zlib zstream for compression\n");
free(comp);
return -1;
}
int err = Z_OK;
do {
// If our output buffer is too small
if (strm.total_out >= compLength) {
// Increase size of output buffer
void* newptr = realloc(comp, compLength + increase);
if(newptr == NULL) {
fprintf(stderr,"Unable to request memory realloc\n");
deflateEnd(&strm);
free(comp);
return -2;
}
comp = newptr;
compLength += increase;
}
strm.next_out = (Bytef*) (comp + strm.total_out);
strm.avail_out = compLength - strm.total_out;
// deflate another chunk
err = deflate(&strm, Z_FINISH);
if(err != Z_OK && err != Z_STREAM_END) {
fprintf(stderr, "Error while deflating buffer\n");
deflateEnd(&strm);
free(comp);
return -3;
}
} while(err != Z_STREAM_END);
compLength = strm.total_out;
if(deflateEnd(&strm) != Z_OK) {
fprintf(stderr,"Error while deallocating libz zstream\n");
free(comp);
return -4;
}
// Set OS Flag to 0x00: "FAT filesystem (MS-DOS, OS/2, NT/Win32)"
comp[OS_FLAG_OFFSET] = 0x00;
*compData = comp;
return compLength;
}