-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGzip.cpp
307 lines (233 loc) · 7.47 KB
/
Gzip.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "Gzip.hpp"
#define _UNIT_TEST false
#if _UNIT_TEST
#include "liolib/Test.hpp"
#endif
namespace lio {
// ===== Exception Implementation =====
const char* const
Gzip::Exception::exceptionMessages_[] = {
GZIP_EXCEPTION_MESSAGES
};
#undef GZIP_EXCEPTION_MESSAGES // undef helps reducing unnecessary preprocessing work.
Gzip::Exception::Exception(ExceptionType exceptionType) :
exceptionType_(exceptionType) { }
const char*
Gzip::Exception::what() const noexcept {
return this->exceptionMessages_[(int) this->exceptionType_];
}
const Gzip::ExceptionType
Gzip::Exception::type() const noexcept {
return this->exceptionType_;
}
// ===== Exception Implementation End =====
const size_t Gzip::CHUNK_SIZE = 1024 * 1024;
const int Gzip::WINDOWS_BITS = 15;
const int Gzip::GZIP_ENCODING = 16;
Gzip::Gzip(Config config) :
config_(config),
mp_(nullptr)
{
if (config.useMemoryPool == true) {
this->mp_ = new MemoryPool(config.memoryPoolSize);
}
}
Gzip::~Gzip() {
if (this->mp_ != nullptr) {
delete mp_;
}
}
DataBlock<> Gzip::Compress(const void* source, size_t length, int level) {
DEBUG_FUNC_START;
if (this->config_.useMemoryPool == false) {
DEBUG_cerr <<
"This function requires use of MemoryPool but Gzip is not set to use MP." << endl;
return DataBlock<>();
}
DEBUG_cout << "Original size: " << length << endl;
int ret, flush;
unsigned have;
z_stream strm;
unsigned char* in;// = new unsigned char[length];
//unsigned char* out = new unsigned char[CHUNK_SIZE];
unsigned char* out = nullptr;
try {
out = (unsigned char*) this->mp_->Mpalloc(CHUNK_SIZE);
} catch (MemoryPool::Exception& ex) {
DEBUG_cerr << "Memory Allocation has failed. " << ex.what() << endl;
throw Exception(ExceptionType::COMPRESSION_FAIL);
}
in = (unsigned char*) source;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;
ret = deflateInit2(&strm, level, Z_DEFLATED, WINDOWS_BITS | GZIP_ENCODING, 8, Z_TEXT);
//ret = deflateInit(&strm, level);
if (ret != Z_OK) {
DEBUG_cerr << "deflateInit failed." << endl;
throw Exception(ExceptionType::COMPRESSION_FAIL);
}
strm.avail_in = length;
flush = Z_FINISH;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
ret = deflate(&strm, flush);
assert (ret != Z_STREAM_ERROR);
have = CHUNK_SIZE - strm.avail_out;
} while (strm.avail_out == 0);
if (flush != Z_FINISH) {
DEBUG_cerr << "Not finished" << endl;
}
have = strm.total_out;
(void) deflateEnd(&strm);
DEBUG_cout << "Compressed size: " << have << endl;
this->mp_->MpAllocFit((void*)out, have);
return DataBlock<>((void*)out, 0, have);
}
DataBlock<> Gzip::Decompress(const void* source, size_t length) {
DEBUG_FUNC_START;
if (this->config_.useMemoryPool == false) {
DEBUG_cerr <<
"This function requires use of MemoryPool but Gzip is not set to use MP." << endl;
return DataBlock<>();
}
int ret;
unsigned have;
z_stream strm;
unsigned char* in;// = new unsigned char[length];
//unsigned char* out = new unsigned char[CHUNK_SIZE];
unsigned char* out = (unsigned char*) this->mp_->Mpalloc(CHUNK_SIZE);
in = (unsigned char*) source;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, WINDOWS_BITS | GZIP_ENCODING);
if (ret != Z_OK) {
throw Exception(ExceptionType::DECOMPRESSION_FAIL);
}
strm.avail_in = length;
strm.next_in = in;
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert (ret != Z_STREAM_ERROR);
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
DEBUG_cerr << "decomp fail." << endl;
throw Exception(ExceptionType::DECOMPRESSION_FAIL);
}
have = strm.total_out;
(void) inflateEnd(&strm);
DEBUG_cout << "Decompressed size: " << have << endl;
this->mp_->MpAllocFit((void*)out, have);
return DataBlock<>((void*)out, 0, have);
}
ssize_t Gzip::Compress(const void* source, size_t length, char* dest, size_t maxSize, int level) {
DEBUG_FUNC_START;
DEBUG_cout << "Original size: " << length << endl;
int ret, flush;
unsigned have;
z_stream strm;
unsigned char* in;// = new unsigned char[length];
//unsigned char* out = new unsigned char[CHUNK_SIZE];
unsigned char* out = (unsigned char*) dest;
in = (unsigned char*) source;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;
ret = deflateInit2(&strm, level, Z_DEFLATED, WINDOWS_BITS | GZIP_ENCODING, 8, Z_TEXT);
//ret = deflateInit(&strm, level);
if (ret != Z_OK) {
DEBUG_cerr << "deflateInit failed." << endl;
throw Exception(ExceptionType::COMPRESSION_FAIL);
}
strm.avail_in = length;
flush = Z_FINISH;
do {
strm.avail_out = maxSize;
strm.next_out = out;
ret = deflate(&strm, flush);
assert (ret != Z_STREAM_ERROR);
have = maxSize - strm.avail_out;
} while (strm.avail_out == 0);
have = strm.total_out;
(void) deflateEnd(&strm);
if (flush != Z_FINISH) {
DEBUG_cerr << "Not finished" << endl;
return -1;
}
DEBUG_cout << "Compressed size: " << have << endl;
return have;
}
ssize_t Gzip::Decompress(const void* source, size_t length, char* dest, size_t maxSize) {
DEBUG_FUNC_START;
int ret;
unsigned have;
z_stream strm;
unsigned char* in;// = new unsigned char[length];
//unsigned char* out = new unsigned char[CHUNK_SIZE];
unsigned char* out = (unsigned char*) dest;
in = (unsigned char*) source;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, WINDOWS_BITS | GZIP_ENCODING);
if (ret != Z_OK) {
throw Exception(ExceptionType::DECOMPRESSION_FAIL);
}
strm.avail_in = length;
strm.next_in = in;
strm.avail_out = maxSize;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert (ret != Z_STREAM_ERROR);
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
(void)inflateEnd(&strm);
DEBUG_cerr << "Z_NEED_DICT" << endl;
return -1;
case Z_DATA_ERROR:
(void)inflateEnd(&strm);
DEBUG_cerr << "Z_DATA_ERROR" << endl;
return -1;
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
DEBUG_cerr << "Z_MEM_ERROR" << endl;
DEBUG_cerr << "Decomp failed." << endl;
return -1;
}
have = strm.total_out;
(void) inflateEnd(&strm);
DEBUG_cout << "Decompressed size: " << have << endl;
return have;
}
//Gzip::
//Gzip::
}
#if _UNIT_TEST
#include <iostream>
using namespace lio;
int main() {
Gzip gzip;
string t = "Text to compreesdgsgdsgsss is here. I'm gonna make this string much longer to see how the compression rates differ. Now it only ssaves abodut 3~5% but I expect it tos be way ways way shigher compression rate. :) hehe let's sdgsee how much it will compress the data. :)";
t += "Text to empress is here. I'm gonna make this string much longer to see how the comvpression rates difefer. xNowe it odasgnly seaves aboutd 3~5% buvt I expesgsgcadfvsadft it to be way way wday hvsigher compression rate. :) hehes let'ds seex how msuch it will compress the data. :)\0";
DataBlock<> compressed = gzip.Compress((const void*) t.c_str(), t.length());
DataBlock<> decompressed = gzip.Decompress((const void*) compressed.object, compressed.length);
char* org = (char*)decompressed.object;
std::cout << org << endl;
return 0;
}
#endif
#undef _UNIT_TEST