-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.cpp
402 lines (329 loc) · 10.4 KB
/
compressor.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*******************************************************************************
*
* Created by Daniel Carrasco at https://www.electrosoftcloud.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <stdexcept>
#include "compressor.h"
compressor::compressor(sector_tools_compression mode, bool is_compression, int32_t comp_level) {
comp_mode = mode;
compression = is_compression;
compression_level = comp_level;
int ret;
// Class initialzer
switch(mode) {
case C_ZLIB:
strm_zlib.zalloc = Z_NULL;
strm_zlib.zfree = Z_NULL;
strm_zlib.opaque = Z_NULL;
if (is_compression) {
ret = deflateInit(&strm_zlib, comp_level);
}
else {
ret = inflateInit(&strm_zlib);
}
if (ret != Z_OK) {
fprintf(stderr, "There was an error initializing the ZLIB encoder/decoder\n");
//throw std::runtime_error("Error initializing the zlib compressor/decompressor.");
}
break;
case C_LZMA:
strm_lzma = LZMA_STREAM_INIT;
if (is_compression) {
lzma_lzma_preset(&opt_lzma2, comp_level);
lzma_filter filters[] = {
{ LZMA_FILTER_X86, NULL },
{ LZMA_FILTER_LZMA2, &opt_lzma2 },
{ LZMA_VLI_UNKNOWN, NULL },
};
ret = lzma_stream_encoder(&strm_lzma, filters, LZMA_CHECK_NONE); // CRC is already checked
}
else {
ret = lzma_stream_decoder(
&strm_lzma,
UINT64_MAX,
LZMA_IGNORE_CHECK
);
}
// Return successfully if the initialization went fine.
if (ret != LZMA_OK) {
fprintf(stderr, "There was an error initializing LZMA encoder/decoder\n");
//throw std::runtime_error("Error initializing the lzma2 compressor/decompressor.");
}
break;
case C_LZ4:
if (is_compression) {
// We will create blocks of 1Mb and data will not be splitted between blocks.
strm_lz4 = new lzlib4((size_t)1048576, LZLIB4_INPUT_NOSPLIT, (int8_t)(1.34 * comp_level));
}
else {
strm_lz4 = new lzlib4();
}
break;
case C_FLAC:
if (is_compression) {
// We will create blocks of 1Mb and data will not be splitted between blocks.
strm_flac = new flaczlib(true, 2, 16, 44100, 0, comp_level);
}
else {
strm_flac = new flaczlib(false);
}
break;
}
}
// Destructor function that will call close()
compressor::~compressor(void) {
close();
}
int8_t compressor::set_input(uint8_t* in, size_t &in_size){
if (!compression) {
switch(comp_mode) {
case C_ZLIB:
strm_zlib.avail_in = in_size;
strm_zlib.next_in = in;
return 0;
break;
case C_LZMA:
strm_lzma.avail_in = in_size;
strm_lzma.next_in = in;
return 0;
break;
case C_LZ4:
strm_lz4->strm.avail_in = in_size;
strm_lz4->strm.next_in = in;
return 0;
break;
case C_FLAC:
strm_flac->strm->avail_in = in_size;
strm_flac->strm->next_in = in;
return 0;
break;
}
return 0;
}
else {
//throw std::runtime_error("Trying to use a decompression object to compress.");
return -1;
}
}
int8_t compressor::set_output(uint8_t* out, size_t &out_size){
if (compression) {
switch(comp_mode) {
case C_ZLIB:
strm_zlib.avail_out = out_size;
strm_zlib.next_out = out;
return 0;
break;
case C_LZMA:
strm_lzma.avail_out = out_size;
strm_lzma.next_out = out;
return 0;
break;
case C_LZ4:
strm_lz4->strm.avail_out = out_size;
strm_lz4->strm.next_out = out;
return 0;
break;
case C_FLAC:
strm_flac->strm->avail_out = out_size;
strm_flac->strm->next_out = out;
return 0;
break;
}
return 0;
}
else {
//throw std::runtime_error("Trying to use a decompression object to compress.");
return -1;
}
}
int8_t compressor::compress(size_t &out_size, uint8_t* in, size_t in_size, uint8_t flush_mode){
if (compression) {
int8_t return_code;
lzma_action flushmode_lzma = LZMA_RUN;
size_t processed;
switch(comp_mode) {
case C_ZLIB:
strm_zlib.avail_in = in_size;
strm_zlib.next_in = in;
return_code = deflate(&strm_zlib, flush_mode);
// If is the end of the stream, then is OK
if (return_code == Z_STREAM_END) {
return_code = Z_OK;
}
// If the buffer is 0 bytes the compressor will give BUF_ERROR.
// Can be ignored because no data was sent so nothing failed
if (return_code == Z_BUF_ERROR && in_size == 0) {
return_code = Z_OK;
}
out_size = strm_zlib.avail_out;
return return_code;
break;
case C_LZMA:
strm_lzma.avail_in = in_size;
strm_lzma.next_in = in;
switch (flush_mode) {
case Z_FULL_FLUSH:
flushmode_lzma = LZMA_FULL_FLUSH;
break;
case Z_FINISH:
flushmode_lzma = LZMA_FINISH;
break;
}
return_code = lzma_code(&strm_lzma, flushmode_lzma);
// If is the end of the stream, then is OK
if (return_code == LZMA_STREAM_END) {
return_code = LZMA_OK;
}
// If the buffer is 0 bytes the compressor will give BUF_ERROR.
// Can be ignored because no data was sent so nothing failed
if (return_code == LZMA_BUF_ERROR && in_size == 0) {
return_code = LZMA_OK;
}
out_size = strm_lzma.avail_out;
return return_code;
break;
case C_LZ4:
strm_lz4->strm.avail_in = in_size;
strm_lz4->strm.next_in = in;
strm_lz4->compress((lzlib4_flush_mode)flush_mode);
out_size = strm_lz4->strm.avail_out;
break;
case C_FLAC:
strm_flac->strm->avail_in = in_size;
strm_flac->strm->next_in = in;
strm_flac->compress(in_size / 4, (flaczlib_flush_mode)flush_mode);
out_size = strm_flac->strm->avail_out;
break;
}
return 0;
}
else {
//throw std::runtime_error("Trying to use a decompression object to compress.");
return -1;
}
}
int8_t compressor::decompress(uint8_t* out, size_t &out_size, size_t &in_size, uint8_t flusmode){
if (!compression) {
int8_t return_code;
switch(comp_mode) {
case C_ZLIB:
{
strm_zlib.avail_out = out_size;
strm_zlib.next_out = out;
return_code = inflate(&strm_zlib, flusmode);
in_size = strm_zlib.avail_in;
return return_code;
break;
}
case C_LZMA:
{
strm_lzma.avail_out = out_size;
strm_lzma.next_out = out;
return_code = lzma_code(&strm_lzma, LZMA_RUN);
in_size = strm_lzma.avail_in;
return return_code;
break;
}
case C_LZ4:
{
strm_lz4->strm.avail_out = out_size;
strm_lz4->strm.next_out = out;
int return_code = strm_lz4->decompress_partial(false, -1);
in_size = strm_lz4->strm.avail_in;
return return_code;
break;
}
case C_FLAC:
{
strm_flac->strm->avail_out = out_size;
strm_flac->strm->next_out = out;
int return_code = strm_flac->decompress_partial(false, -1);
in_size = strm_flac->strm->avail_in;
return return_code;
break;
}
}
return 0;
}
else {
//throw std::runtime_error("Trying to use a compression object to decompress.");
return -1;
}
}
int8_t compressor::close(){
switch(comp_mode) {
case C_ZLIB:
if (compression) {
(void)deflateEnd(&strm_zlib);
strm_zlib = {};
}
else {
(void)inflateEnd(&strm_zlib);
strm_zlib = {};
}
return 0;
break;
case C_LZMA:
lzma_end(&strm_lzma);
strm_lzma = {};
return 0;
break;
case C_LZ4:
delete strm_lz4; //strm_lz4->close();
return 0;
break;
case C_FLAC:
delete strm_flac; //strm_lz4->close();
return 0;
break;
}
return -1;
}
size_t compressor::data_left_in() {
switch(comp_mode) {
case C_ZLIB:
return strm_zlib.avail_in;
break;
case C_LZMA:
return strm_lzma.avail_in;
break;
case C_LZ4:
return strm_lz4->strm.avail_in;
break;
case C_FLAC:
return strm_flac->strm->avail_in;
break;
}
return -1;
};
size_t compressor::data_left_out() {
switch(comp_mode) {
case C_ZLIB:
return strm_zlib.avail_out;
break;
case C_LZMA:
return strm_lzma.avail_out;
break;
case C_LZ4:
return strm_lz4->strm.avail_out;
break;
case C_FLAC:
return strm_flac->strm->avail_out;
break;
}
return -1;
};