46 bool deflate(
const uint8_t *data, uint32_t size, std::vector<uint8_t>& deflated)
48 static int BLOCK_SIZE_100K = 5;
52 stream.next_in =
reinterpret_cast<char *
>(
const_cast<uint8_t *
>(data));
53 stream.avail_in = size;
54 stream.bzalloc = NULL;
58 if (BZ2_bzCompressInit(&stream, BLOCK_SIZE_100K, 0, 0) != BZ_OK) {
62 deflated.resize(CHUNK_SIZE);
63 stream.next_out =
reinterpret_cast<char *
>(deflated.data());
64 stream.avail_out = deflated.size();
66 int result = BZ_RUN_OK;
68 while(result == BZ_RUN_OK || result == BZ_FLUSH_OK || result == BZ_FINISH_OK) {
69 if (stream.avail_in == 0) {
73 if (stream.avail_out == 0) {
75 stream.next_out =
reinterpret_cast<char *
>(deflated.data() + deflated.size() -
CHUNK_SIZE);
79 result = BZ2_bzCompress(&stream, state);
82 if (result != BZ_STREAM_END) {
84 BZ2_bzCompressEnd(&stream);
88 deflated.resize(deflated.size() - stream.avail_out);
89 BZ2_bzCompressEnd(&stream);
93 bool inflate(
const uint8_t *data, uint32_t size, std::vector<uint8_t>& inflated)
95 static const int SMALL = 1;
99 stream.next_in =
reinterpret_cast<char *
>(
const_cast<uint8_t *
>(data));
100 stream.avail_in = size;
101 stream.bzalloc = NULL;
102 stream.bzfree = NULL;
103 stream.opaque = NULL;
105 if (BZ2_bzDecompressInit(&stream, VERBOSITY, SMALL) != BZ_OK) {
109 inflated.resize(CHUNK_SIZE);
110 stream.next_out =
reinterpret_cast<char *
>(inflated.data());
111 stream.avail_out = inflated.size();
114 while(result == BZ_OK || result == BZ_FLUSH_OK || result == BZ_FINISH_OK) {
115 if (stream.avail_out == 0) {
116 inflated.resize(inflated.size() +
CHUNK_SIZE);
117 stream.next_out =
reinterpret_cast<char *
>(inflated.data() + inflated.size() -
CHUNK_SIZE);
121 result = BZ2_bzDecompress(&stream);
124 if (result != BZ_STREAM_END) {
126 BZ2_bzDecompressEnd(&stream);
130 inflated.resize(inflated.size() - stream.avail_out);
131 BZ2_bzDecompressEnd(&stream);
142 bool deflate(
const uint8_t *data, uint32_t size, std::vector<uint8_t>& deflated) {
return false; }
143 bool inflate(
const uint8_t *data, uint32_t size, std::vector<uint8_t>& inflated) {
return false; }
static const std::size_t CHUNK_SIZE
bool inflate(const uint8_t *data, uint32_t size, std::vector< uint8_t > &inflated)
bool compressionAvailable()
static const int VERBOSITY
bool deflate(const uint8_t *data, uint32_t size, std::vector< uint8_t > &deflated)