-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fusionado en feature/zlib_streams_compression (solicitud de integración
#4) Feature/zlib streams compression
- Loading branch information
Showing
7 changed files
with
517 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <stdexcept> | ||
#include "compressor.h" | ||
|
||
compressor::compressor(sector_tools_compression mode, bool is_compression, int8_t comp_level) { | ||
comp_mode = mode; | ||
compression = is_compression; | ||
// Class initialzer | ||
if (mode == C_DATA_ZLIB) { | ||
strm.zalloc = Z_NULL; | ||
strm.zfree = Z_NULL; | ||
strm.opaque = Z_NULL; | ||
|
||
int ret; | ||
if (is_compression) { | ||
ret = deflateInit(&strm, comp_level); | ||
} | ||
else { | ||
ret = inflateInit(&strm); | ||
} | ||
|
||
if (ret != Z_OK) { | ||
//throw std::runtime_error("Error initializing the zlib compressor/decompressor."); | ||
} | ||
} | ||
} | ||
|
||
|
||
int8_t compressor::set_input(uint8_t* in, size_t &in_size){ | ||
if (!compression) { | ||
if (comp_mode == C_DATA_ZLIB) { | ||
strm.avail_in = in_size; | ||
strm.next_in = in; | ||
|
||
return 0; | ||
} | ||
|
||
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) { | ||
if (comp_mode == C_DATA_ZLIB) { | ||
strm.avail_out = out_size; | ||
strm.next_out = out; | ||
|
||
return 0; | ||
} | ||
|
||
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 flusmode){ | ||
if (compression) { | ||
if (comp_mode == C_DATA_ZLIB) { | ||
strm.avail_in = in_size; | ||
strm.next_in = in; | ||
|
||
int8_t return_code = deflate(&strm, flusmode); | ||
|
||
out_size = strm.avail_out; | ||
return return_code; | ||
} | ||
|
||
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) { | ||
if (comp_mode == C_DATA_ZLIB) { | ||
strm.avail_out = out_size; | ||
strm.next_out = out; | ||
int8_t return_code = inflate(&strm, flusmode); | ||
|
||
in_size = strm.avail_in; | ||
return return_code; | ||
} | ||
|
||
return 0; | ||
} | ||
else { | ||
//throw std::runtime_error("Trying to use a compression object to decompress."); | ||
return -1; | ||
} | ||
} | ||
|
||
int8_t compressor::close(){ | ||
if (compression) { | ||
(void)deflateEnd(&strm); | ||
} | ||
else { | ||
(void)inflateEnd(&strm); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdint.h> | ||
#include "zlib.h" | ||
|
||
// | ||
// Stream types detectable by the class | ||
// | ||
enum sector_tools_compression : uint8_t { | ||
C_NONE = 0, | ||
C_AUDIO_FLAC, | ||
C_AUDIO_APE, | ||
C_AUDIO_WAVPACK, | ||
C_DATA_ZLIB = 1, | ||
C_DATA_LZMA, | ||
C_DATA_BZ2, | ||
C_DATA_LZ4 | ||
}; | ||
|
||
// | ||
// compressor Class | ||
// | ||
class compressor { | ||
public: | ||
// Public methods | ||
compressor(sector_tools_compression mode, bool is_compression, int8_t comp_level = -1); | ||
|
||
int8_t set_input(uint8_t* in, size_t &in_size); | ||
int8_t set_output(uint8_t* out, size_t &out_size); | ||
int8_t compress(size_t &out_size, uint8_t* in, size_t in_size, uint8_t flusmode = Z_NO_FLUSH); | ||
int8_t decompress(uint8_t* out, size_t out_size, size_t &in_size, uint8_t flusmode); | ||
size_t data_left_in() { return strm.avail_in; }; | ||
size_t data_left_out() {return strm.avail_out; }; | ||
|
||
int8_t close(); | ||
|
||
private: | ||
// zlib object | ||
z_stream strm; | ||
sector_tools_compression comp_mode; | ||
bool compression; | ||
}; |
Oops, something went wrong.