-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice_z.h
269 lines (205 loc) · 6.5 KB
/
slice_z.h
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
// Classes for managing slice compression (deflation) and slice decompress (inflation)
#ifndef __SLICE_Z
#define __SLICE_Z
#include <fstream>
#include <zlib.h>
#define MAX_COMPRESSED_BYTES 256 // (i.e. NUM_GROUP_CHUNK/( 8 bits per byte) )
#define ZLIB_WINDOW_BITS -9 // No zlib or gzip header
template<unsigned int MAX_BUFFER_SIZE>
class InflateSlice
{
private:
z_stream engine;
unsigned char *z_src_buffer;
unsigned char *z_dst_buffer;
unsigned int z_src_len;
unsigned int z_dst_len;
public:
InflateSlice()
{
engine.zalloc = Z_NULL;
engine.zfree = Z_NULL;
engine.opaque = Z_NULL;
engine.avail_in = 0;
engine.next_in = NULL;
engine.avail_out = MAX_BUFFER_SIZE;
engine.next_out = NULL;
z_src_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_src_buffer == NULL){
throw __FILE__ ":InflateSlice: Unable to allocate z_src_buffer";
};
z_dst_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_dst_buffer == NULL){
throw __FILE__ ":InflateSlice: Unable to allocate z_src_buffer";
};
if( Z_OK != inflateInit2(&engine, ZLIB_WINDOW_BITS) ){
throw __FILE__ ":InflateSlice: Error calling inflateInit2";
}
};
InflateSlice(const InflateSlice &m_rhs)
{
z_src_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_src_buffer == NULL){
throw __FILE__ ":InflateSlice: Unable to allocate z_src_buffer";
};
z_dst_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_dst_buffer == NULL){
throw __FILE__ ":InflateSlice: Unable to allocate z_src_buffer";
};
*this = m_rhs;
};
~InflateSlice()
{
if(z_src_buffer != NULL){
delete [] z_src_buffer;
z_src_buffer = NULL;
}
if(z_dst_buffer != NULL){
delete [] z_dst_buffer;
z_dst_buffer = NULL;
}
if( Z_OK != inflateEnd(&engine) ){
throw __FILE__ ":~InflateSlice: Error in inflateEnd";
}
};
void inflate(std::ifstream &m_fin, unsigned int m_len)
{
m_fin.read( (char*)z_src_buffer, m_len);
if(!m_fin){
throw __FILE__":InflateSlice::inflate(fstream): Error reading compressed slice from file";
}
engine.next_in = z_src_buffer;
engine.avail_in = m_len;
engine.next_out = z_dst_buffer;
engine.avail_out = MAX_COMPRESSED_BYTES;
if( Z_STREAM_END != ::inflate(&engine, Z_FINISH) ){
throw __FILE__ ":InflateSlice::inflate(fstream): Error in inflate (1)";
}
if( Z_OK != inflateReset(&engine) ){
throw __FILE__ ":InflateSlice::inflate(fstream): Error in inflateReset (1)";
}
};
void inflate(unsigned char* m_buffer, unsigned int m_len)
{
engine.next_in = m_buffer;
engine.avail_in = m_len;
engine.next_out = z_dst_buffer;
engine.avail_out = MAX_COMPRESSED_BYTES;
if( Z_STREAM_END != ::inflate(&engine, Z_FINISH) ){
throw __FILE__ ":InflateSlice::inflate(unsigned char*): Error in inflate (1)";
}
if( Z_OK != inflateReset(&engine) ){
throw __FILE__ ":InflateSlice::inflate(unsigned char*): Error in inflateReset (1)";
}
};
unsigned char* ptr() const
{
return z_dst_buffer;
};
inline unsigned int size() const
{
return MAX_BUFFER_SIZE - engine.avail_out;
};
// Do not allow Inflate slice to be copied (copying will require
// some work to manage the decompression engine state)!
InflateSlice& operator=(const InflateSlice &m_rhs)
{
memcpy( z_src_buffer, m_rhs.z_src_buffer, MAX_BUFFER_SIZE*sizeof(unsigned char) );
memcpy( z_dst_buffer, m_rhs.z_dst_buffer, MAX_BUFFER_SIZE*sizeof(unsigned char) );
z_src_len = m_rhs.z_src_len;
z_dst_len = m_rhs.z_dst_len;
inflateCopy(&engine, (z_stream*)&m_rhs.engine);
return *this;
};
};
template<unsigned int MAX_BUFFER_SIZE>
class CompressSlice
{
private:
z_stream engine;
unsigned char *z_buffer;
unsigned int z_buffer_len;
public:
// Smaller value is better
// Z_RLE -> 0.757901
// Z_FILTERED -> 0.763826
// Z_HUFFMAN_ONLY -> 0.785642
// Z_FIXED -> 0.752979
// Z_DEFAULT_STRATEGY -> 0.748315 <-- best
CompressSlice(const int &m_level = 9, const int &m_memLevel = 9)
{
engine.zalloc = Z_NULL;
engine.zfree = Z_NULL;
engine.opaque = Z_NULL;
engine.avail_in = 0;
engine.next_in = NULL;
engine.avail_out = MAX_BUFFER_SIZE;
engine.next_out = NULL;
// Setting the data type to Z_BINARY has no apparent effect!
//engine.data_type = Z_BINARY;
z_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_buffer == NULL){
throw __FILE__ ":CompressSlice: Unable to allocate z_buffer";
};
if( Z_OK != deflateInit2(&engine,
m_level,
Z_DEFLATED,
ZLIB_WINDOW_BITS,
m_memLevel,
Z_DEFAULT_STRATEGY/*Z_RLE*/) ){ // Only Z_RLE is supported for now
throw __FILE__ ":CompressSlice: Error calling deflateInit2";
}
};
CompressSlice(const CompressSlice &m_rhs)
{
z_buffer = new unsigned char[MAX_BUFFER_SIZE];
if(z_buffer == NULL){
throw __FILE__ ":CompressSlice: Unable to allocate z_buffer";
};
*this = m_rhs;
};
~CompressSlice()
{
if(z_buffer != NULL){
delete [] z_buffer;
z_buffer = NULL;
}
// Don't check the return value of deflateEnd, since
// we may have discarded some output when choosing not
// to use compression.
deflateEnd(&engine);
};
unsigned char* ptr() const
{
return z_buffer;
};
inline unsigned int size() const
{
return MAX_BUFFER_SIZE - engine.avail_out;
};
inline bool compress(unsigned char* m_ptr, unsigned int m_len,
const unsigned char* m_dictionary = NULL, const unsigned int &m_dictionary_len = 0)
{
engine.avail_in = m_len;
engine.next_in = m_ptr;
engine.avail_out = MAX_BUFFER_SIZE;
engine.next_out = z_buffer;
if(m_dictionary != Z_NULL){
deflateSetDictionary(&engine, m_dictionary, m_dictionary_len);
}
const int zlib_ret = deflate(&engine, Z_FINISH);
if( Z_OK != deflateReset(&engine) ){
throw __FILE__ ":CompressSlice::compress: Error in deflateReset";
}
// Were we able to compresss?
return ( (Z_STREAM_END == zlib_ret) && (size() < m_len) );
};
CompressSlice& operator=(const CompressSlice &m_rhs)
{
memcpy( z_buffer, m_rhs.z_buffer, MAX_BUFFER_SIZE*sizeof(unsigned char) );
z_buffer_len = m_rhs.z_buffer_len;
deflateCopy( &engine, (z_stream*)&(m_rhs.engine) );
return *this;
};
};
#endif // __SLICE_Z