-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMiniz.cpp
206 lines (188 loc) · 4.5 KB
/
SMiniz.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
#include "SMiniz.h"
#include <functional>
namespace util
{
// base wrapper starts here
SMiniz::SMiniz(WriteFunc writeFunc) :_writeFunc(writeFunc)
{
memset(&_stream, 0, sizeof(_stream));
_stream.avail_out = SMINIZ_BUF_SIZE;
_stream.next_out = _outBuf;
_stream.avail_in = 0;
}
void SMiniz::printStat(int bufLength, size_t index, size_t length, int flush)
{
std::cout << ((flush == Z_FINISH) ? "Finish:" : "Buffered:") << bufLength << ":" << index << ":" << length << ":" << _stream.avail_in << ":" << _stream.next_in << std::endl;
}
// Compression wrapper starts here
SMinizCompress::~SMinizCompress()
{
if (!_isInitialized)
return;
// ending the compression
try
{
if (_writeFunc)
compressData(reinterpret_cast<const unsigned char*>(""), 0, Z_FINISH);
end();
}
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::cerr << errMsg << std::endl;
}
}
void SMinizCompress::initialize(int compressionLevel)
{
_isInitialized = true;
if (deflateInit(&_stream, compressionLevel) != Z_OK)
{
throw std::runtime_error("deflateInit() failed!");
}
}
void SMinizCompress::end()
{
if (deflateEnd(&_stream) != Z_OK)
{
throw std::runtime_error("deflateEnd() failed!");
}
}
int SMinizCompress::writeDeflate(int flush)
{
int status = deflate(&_stream, flush);
if ((status == Z_STREAM_END) || (!_stream.avail_out))
{
size_t writenData = SMINIZ_BUF_SIZE - _stream.avail_out;
_writeFunc(_outBuf, writenData);
_stream.next_out = _outBuf;
_stream.avail_out = SMINIZ_BUF_SIZE;
}
else if (status != Z_OK)
{
throw std::runtime_error("Deflate() failed with status [" + std::to_string(status) + "]");
}
return status;
}
void SMinizCompress::compressData(const unsigned char* srcData, size_t 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
{
size_t index = 0;
while (_stream.avail_in || (index < length))
{
if (!_stream.avail_in)
{
// now read data
if ((length - index) > SMINIZ_BUF_SIZE)
{
_stream.next_in = srcData + index;
index += SMINIZ_BUF_SIZE;
_stream.avail_in = SMINIZ_BUF_SIZE;
}
else
{
_stream.next_in = srcData + index;
_stream.avail_in = (length - index);
index += (length - index);
}
}
writeDeflate(flush);
}
}
}
// Decompression wrapper starts here
SMinizDecompress::~SMinizDecompress()
{
if (!_isInitialized)
return;
// ending decompression.
try
{
end();
}
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::cerr << errMsg << std::endl;
}
}
void SMinizDecompress::initialize()
{
_isInitialized = true;
if (inflateInit(&_stream))
{
throw std::runtime_error("inflateInit() failed!");
}
}
void SMinizDecompress::end()
{
if (inflateEnd(&_stream) != Z_OK)
{
throw std::runtime_error("inflateEnd() failed!\n");
}
}
int SMinizDecompress::writeInflate(int flush)
{
int status = inflate(&_stream, flush);
if ((status == Z_STREAM_END) || (!_stream.avail_out))
{
size_t writenData = SMINIZ_BUF_SIZE - _stream.avail_out;
_writeFunc(_outBuf, writenData);
_stream.next_out = _outBuf;
_stream.avail_out = SMINIZ_BUF_SIZE;
}
else if (status != Z_OK)
{
throw std::runtime_error("Inflate() failed with status [" + std::to_string(status) + "]");
}
return status;
}
void SMinizDecompress::decompressData(const unsigned char* srcData, size_t length)
{
size_t index = 0;
int status = Z_OK;
while ((status != Z_STREAM_END) || (_stream.avail_in || (index < length)))
{
if (!_stream.avail_in)
{
if (index < length)
{
// now read data if available
if ((length - index) > SMINIZ_BUF_SIZE)
{
_stream.next_in = srcData + index;
index += SMINIZ_BUF_SIZE;
_stream.avail_in = SMINIZ_BUF_SIZE;
}
else
{
_stream.next_in = srcData + index;
_stream.avail_in = (length - index);
index += (length - index);
}
}
else
{
// probably need more data so break off
break;
}
}
status = writeInflate(Z_SYNC_FLUSH);
}
}
}