-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMiniz.hpp
213 lines (195 loc) · 4.56 KB
/
SMiniz.hpp
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
/*
* C++ Wrapper for Miniz compression library
* https://github.com/richgel999/miniz
*/
#pragma once
#include "miniz.h"
#include <algorithm>
#define BUF_SIZE (1048576)
typedef std::function<bool(const unsigned char* data, unsigned int length)> WriteFunc;
class SMiniz
{
private:
int _compressionLevel = Z_BEST_COMPRESSION;
z_stream _stream;
unsigned char _outBuf[BUF_SIZE];
bool _inCompression = false;
WriteFunc _writeFunc = nullptr;
public:
SMiniz(WriteFunc writeFunc):_writeFunc(writeFunc)
{
memset(&_stream, 0, sizeof(_stream));
_stream.avail_out = BUF_SIZE;
_stream.next_out = _outBuf;
_stream.avail_in = 0;
}
~SMiniz()
{
if (_inCompression)
{
// ending the compression
try
{
if (_writeFunc)
compressData(reinterpret_cast<const unsigned char*>(""), 0, Z_FINISH);
endCompression();
}
catch (std::exception& ex)
{
// can't throw in destructor .. so just log
std::string errMsg = "Failed to finalize the deflate because of ";
errMsg += ex.what();
std::cout << errMsg << std::endl;
}
}
else
{
// ending decompression.
try
{
endDecompression();
}
catch (std::exception& ex)
{
// can't throw in destructor .. so just log
std::string errMsg = "Failed to finalize the deflate because of ";
errMsg += ex.what();
std::cout << errMsg << std::endl;
}
}
}
void initiateCompression()
{
_inCompression = true;
if (deflateInit(&_stream, _compressionLevel) != Z_OK)
{
throw std::runtime_error("deflateInit() failed!");
}
}
void initiateDecompression()
{
if (inflateInit(&_stream))
{
throw std::runtime_error("inflateInit() failed!");
}
}
void endCompression()
{
if (deflateEnd(&_stream) != Z_OK)
{
throw std::runtime_error("deflateEnd() failed!");
}
}
void endDecompression()
{
if (inflateEnd(&_stream) != Z_OK)
{
throw std::runtime_error("inflateEnd() failed!\n");
}
}
int writeDeflate(int flush)
{
int status = deflate(&_stream, flush);
if ((status == Z_STREAM_END) || (!_stream.avail_out))
{
unsigned int writenData = BUF_SIZE - _stream.avail_out;
_writeFunc(_outBuf, writenData);
_stream.next_out = _outBuf;
_stream.avail_out = BUF_SIZE;
}
else if (status != Z_OK)
{
throw std::runtime_error("Deflate() failed with status [" + std::to_string(status) + "]");
}
return status;
}
int writeInflate(int flush)
{
int status = inflate(&_stream, flush);
if ((status == Z_STREAM_END) || (!_stream.avail_out))
{
unsigned int writenData = BUF_SIZE - _stream.avail_out;
_writeFunc(_outBuf, writenData);
_stream.next_out = _outBuf;
_stream.avail_out = BUF_SIZE;
}
else if (status != Z_OK)
{
throw std::runtime_error("Inflate() failed with status [" + std::to_string(status) + "]");
}
return status;
}
void compressData(const unsigned char* srcData, unsigned int length, int flush = Z_NO_FLUSH)
{
if (flush == Z_FINISH)
{
_stream.next_in = reinterpret_cast<const unsigned char*>("");
_stream.avail_in = 0;
int status = Z_OK;
do
{
// flush everything out
status = writeDeflate(flush);
}while(status != Z_STREAM_END);
}
else
{
unsigned int index = 0;
while(_stream.avail_in || (index < length))
{
if (!_stream.avail_in)
{
// now read data
if ((length - index) > BUF_SIZE)
{
_stream.next_in = srcData + index;
index += BUF_SIZE;
_stream.avail_in = BUF_SIZE;
}
else
{
_stream.next_in = srcData + index;
_stream.avail_in = (length - index);
index += (length - index);
}
}
writeDeflate(flush);
}
}
}
void printStat(int bufLength, unsigned int index, unsigned int length, int flush)
{
std::cout << ((flush == Z_FINISH) ? "Finish:" : "Buffered:") << bufLength << ":" << index << ":" << length << ":" << _stream.avail_in << ":" <<_stream.next_in << std::endl;
}
void decompressData(const unsigned char* srcData, unsigned int length)
{
unsigned int index = 0;
int status = Z_OK;
while((status != Z_STREAM_END) || (_stream.avail_in || (index < length)))
{
if (!_stream.avail_in)
{
if (index < length)
{
if ((length - index) > BUF_SIZE)
{
_stream.next_in = srcData + index;
index += BUF_SIZE;
_stream.avail_in = BUF_SIZE;
}
else
{
_stream.next_in = srcData + index;
_stream.avail_in = (length - index);
index += (length - index);
}
}
else
{
break;
}
}
status = writeInflate(Z_SYNC_FLUSH);
}
}
};