-
Notifications
You must be signed in to change notification settings - Fork 2
/
pzsnap.cc
273 lines (231 loc) · 6.31 KB
/
pzsnap.cc
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
270
271
272
273
/* pzsnap.c - fake snappy wrapper to invoke other compression APIs
* Copyright 2014-2015 Howard Chu @ Symas Corp.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
typedef size_t (bfunc)(size_t len);
typedef void (cfunc)(const char *in, size_t inlen, char *out, size_t *outlen);
typedef bool (ufunc)(const char *in, size_t inlen, char *out);
typedef struct codec_t {
bfunc *bound;
cfunc *comp;
ufunc *uncomp;
} codec_t;
typedef struct ctable_t {
const char *name;
const codec_t *cptr;
} ctable_t;
static bfunc def_bfunc;
static cfunc def_cfunc;
static ufunc def_ufunc;
static const codec_t def_codec = { def_bfunc, def_cfunc, def_ufunc };
static codec_t *codec = (codec_t *)&def_codec;
namespace snappy {
/* We always store the original length at head of buffer */
bool GetUncompressedLength(const char *in, size_t inlen, size_t *result) {
if (inlen < sizeof(size_t))
return false;
memcpy(result, in, sizeof(size_t));
return true;
}
void RawCompress(const char *in, size_t inlen, char *out, size_t *outlen) {
memcpy(out, &inlen, sizeof(size_t));
*outlen -= sizeof(size_t);
codec->comp(in, inlen, out+sizeof(size_t), outlen);
*outlen += sizeof(size_t);
}
bool RawUncompress(const char *in, size_t inlen, char *out) {
return codec->uncomp(in+sizeof(size_t), inlen-sizeof(size_t), out);
}
size_t MaxCompressedLength(size_t len){
return codec->bound(len) + sizeof(size_t);
}
}
extern "C" {
int snappy_compress(const char *in, size_t inlen, char *out, size_t *outlen) {
snappy::RawCompress(in, inlen, out, outlen);
return 0;
}
int snappy_uncompress(const char *in, size_t inlen, char *out, size_t *outlen) {
snappy::RawUncompress(in, inlen, out);
return 0;
}
size_t snappy_max_compressed_length(size_t len) {
return snappy::MaxCompressedLength(len);
}
int snappy_uncompressed_length(const char *in, size_t inlen, size_t *outlen) {
snappy::GetUncompressedLength(in, inlen, outlen);
return 0;
}
}
#ifndef USE_BZIP2
#define USE_BZIP2 1
#endif
#if USE_BZIP2
#include <bzlib.h>
static size_t bzip2_bfunc(size_t len) {
return len * 101 / 100 + 600;
}
static void bzip2_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
unsigned int destlen = bzip2_bfunc(inlen);
BZ2_bzBuffToBuffCompress(out, &destlen, (char *)in, inlen, 9, 0, 0);
*outlen = destlen;
}
static bool bzip2_ufunc(const char *in, size_t inlen, char *out) {
size_t origlen;
unsigned int destlen;
int rc;
memcpy(&origlen, in-sizeof(size_t), sizeof(size_t));
destlen = origlen;
rc = BZ2_bzBuffToBuffDecompress(out, &destlen, (char *)in, inlen, 0, 0);
return rc == BZ_OK;
}
static const codec_t c_bzip2 = {
bzip2_bfunc, bzip2_cfunc, bzip2_ufunc
};
#endif
#ifndef USE_LZ4
#define USE_LZ4 1
#endif
#if USE_LZ4
#include <lz4.h>
static size_t lz4_bfunc(size_t len) {
return LZ4_compressBound(len);
}
static void lz4_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
*outlen = LZ4_compress(in, out, inlen);
}
static bool lz4_ufunc(const char *in, size_t inlen, char *out) {
size_t origlen;
memcpy(&origlen, in-sizeof(size_t), sizeof(size_t));
origlen = LZ4_decompress_safe(in, out, inlen, origlen);
return origlen >= 0;
}
static const codec_t c_lz4 = {
lz4_bfunc, lz4_cfunc, lz4_ufunc
};
#include <lz4hc.h>
static void lz4hc_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
*outlen = LZ4_compressHC(in, out, inlen);
}
static const codec_t c_lz4hc = {
lz4_bfunc, lz4hc_cfunc, lz4_ufunc
};
#endif
#ifndef USE_LZMA
#define USE_LZMA 1
#endif
#if USE_LZMA
#include <lzma.h>
static size_t lzma_bfunc(size_t len) {
return lzma_stream_buffer_bound(len);
}
static void lzma_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
size_t outl = lzma_stream_buffer_bound(inlen);
*outlen = 0;
lzma_easy_buffer_encode(2, LZMA_CHECK_NONE, NULL, (uint8_t *)in, inlen,
(uint8_t *)out, outlen, outl);
}
static bool lzma_ufunc(const char *in, size_t inlen, char *out) {
uint64_t memlimit = UINT64_MAX;
size_t inpos = 0;
size_t outpos = 0;
size_t outsize;
memcpy(&outsize, in-sizeof(size_t), sizeof(size_t));
return lzma_stream_buffer_decode(&memlimit, 0, NULL, (uint8_t *)in, &inpos,
inlen, (uint8_t *)out, &outpos, outsize) == 0;
}
static const codec_t c_lzma = {
lzma_bfunc, lzma_cfunc, lzma_ufunc
};
#endif
#ifndef USE_LZO
#define USE_LZO 1
#endif
#if USE_LZO
#include <lzo/lzo1x.h>
static size_t lzo_bfunc(size_t len) {
return len + 16;
}
static void lzo_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
char work[LZO1X_1_MEM_COMPRESS];
lzo_uint dstlen;
lzo1x_1_compress((lzo_bytep)in, inlen, (lzo_bytep)out, &dstlen, work);
*outlen = dstlen;
}
static bool lzo_ufunc(const char *in, size_t inlen, char *out) {
size_t outsize;
lzo_uint dstlen;
memcpy(&outsize, in-sizeof(size_t), sizeof(size_t));
dstlen = outsize;
return lzo1x_decompress((lzo_bytep)in, inlen, (lzo_bytep)out, &dstlen, NULL) == 0;
}
static const codec_t c_lzo = {
lzo_bfunc, lzo_cfunc, lzo_ufunc
};
#endif
#ifndef USE_ZLIB
#define USE_ZLIB 1
#endif
#if USE_ZLIB
#include <zlib.h>
static size_t zlib_bfunc(size_t len) {
return compressBound(len);
}
static void zlib_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
*outlen = compressBound(inlen);
compress((Bytef *)out, outlen, (Bytef *)in, inlen);
}
static bool zlib_ufunc(const char *in, size_t inlen, char *out) {
size_t outlen;
memcpy(&outlen, in-sizeof(size_t), sizeof(size_t));
return uncompress((Bytef *)out, &outlen, (Bytef *)in, inlen) == Z_OK;
}
static const codec_t c_zlib = {
zlib_bfunc, zlib_cfunc, zlib_ufunc
};
#endif
static const ctable_t ctable[] = {
#if USE_BZIP2
{"bzip2", &c_bzip2},
#endif
#if USE_LZ4
{"lz4", &c_lz4},
{"lz4hc", &c_lz4hc},
#endif
#if USE_LZMA
{"lzma", &c_lzma},
#endif
#if USE_LZO
{"lzo", &c_lzo},
#endif
#if USE_ZLIB
{"zlib", &c_zlib},
#endif
{NULL, NULL}
};
static void set_codec() {
char *ptr = getenv("POLYZ");
int i;
if (!ptr) exit(1);
for (i=0; ctable[i].name; i++) {
if (!strcmp(ptr, ctable[i].name)) {
codec = (codec_t *)ctable[i].cptr;
return;
}
}
exit(1);
}
static size_t def_bfunc(size_t len) {
set_codec();
return codec->bound(len);
}
static void def_cfunc(const char *in, size_t inlen, char *out, size_t *outlen) {
set_codec();
codec->comp(in, inlen, out, outlen);
}
static bool def_ufunc(const char *in, size_t inlen, char *out) {
set_codec();
return codec->uncomp(in, inlen, out);
}