forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_compress.cc
445 lines (406 loc) · 10.8 KB
/
tkrzw_compress.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*************************************************************************************************
* Data compression functions
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#include "tkrzw_sys_config.h"
#include "tkrzw_compress.h"
#include "tkrzw_hash_util.h"
#include "tkrzw_lib_common.h"
#if _TKRZW_COMP_ZLIB
extern "C" {
#include <zlib.h>
}
#endif
#if _TKRZW_COMP_ZSTD
extern "C" {
#include <zstd.h>
}
#endif
#if _TKRZW_COMP_LZ4
extern "C" {
#include <lz4.h>
}
#endif
#if _TKRZW_COMP_LZMA
extern "C" {
#include <lzma.h>
}
#endif
namespace tkrzw {
DummyCompressor::DummyCompressor(bool checksum) : checksum_(checksum) {}
DummyCompressor::~DummyCompressor() {}
bool DummyCompressor::IsSupported() const {
return true;
}
char* DummyCompressor::Compress(const void* buf, size_t size, size_t* sp) {
if (checksum_) {
const uint32_t crc = HashCRC32(buf, size);
char* zbuf = static_cast<char*>(xmalloc(sizeof(uint32_t) + size));
WriteFixNum(zbuf, crc, sizeof(crc));
std::memcpy(zbuf + sizeof(crc), buf, size);
*sp = sizeof(crc) + size;
return zbuf;
}
char* zbuf = static_cast<char*>(xmalloc(size + 1));
std::memcpy(zbuf, buf, size);
*sp = size;
return zbuf;
}
char* DummyCompressor::Decompress(const void* buf, size_t size, size_t* sp) {
if (checksum_) {
if (size < sizeof(uint32_t)) {
return nullptr;
}
const char* rp = static_cast<const char*>(buf);
const uint32_t crc = ReadFixNum(rp, sizeof(uint32_t));
rp += sizeof(crc);
size -= sizeof(crc);
if (HashCRC32(rp, size) != crc) {
return nullptr;
}
char* zbuf = static_cast<char*>(xmalloc(size + 1));
std::memcpy(zbuf, rp, size);
*sp = size;
return zbuf;
}
char* zbuf = static_cast<char*>(xmalloc(size + 1));
std::memcpy(zbuf, buf, size);
*sp = size;
return zbuf;
}
std::unique_ptr<Compressor> DummyCompressor::MakeCompressor() const {
return std::make_unique<DummyCompressor>(checksum_);
}
ZLibCompressor::ZLibCompressor(int32_t level, MetadataMode metadata_mode)
: level_(level), metadata_mode_(metadata_mode) {
assert(level >= 0 && level <= 9);
}
ZLibCompressor::~ZLibCompressor() {}
bool ZLibCompressor::IsSupported() const {
#if _TKRZW_COMP_ZLIB
return true;
#else
return false;
#endif
}
char* ZLibCompressor::Compress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_ZLIB
assert(buf != nullptr && size <= MAX_MEMORY_SIZE && sp != nullptr);
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
switch (metadata_mode_) {
default:
if (deflateInit2(&zs, level_, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
return nullptr;
}
break;
case METADATA_ADLER32:
if (deflateInit2(&zs, level_, Z_DEFLATED, 15, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
return nullptr;
}
break;
case METADATA_CRC32:
if (deflateInit2(&zs, level_, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
return nullptr;
}
break;
}
const char* rp = (const char*)buf;
size_t zsiz = size + size / 8 + 32;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
char* wp = zbuf;
zs.next_in = (Bytef*)rp;
zs.avail_in = size;
zs.next_out = (Bytef*)wp;
zs.avail_out = zsiz;
if (deflate(&zs, Z_FINISH) != Z_STREAM_END) {
xfree(zbuf);
deflateEnd(&zs);
return nullptr;
}
deflateEnd(&zs);
zsiz -= zs.avail_out;
*sp = zsiz;
return zbuf;
#else
return nullptr;
#endif
}
char* ZLibCompressor::Decompress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_ZLIB
assert(buf != nullptr && size <= MAX_MEMORY_SIZE && sp != nullptr);
size_t zsiz = size * 8 + 32;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
while (true) {
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
switch (metadata_mode_) {
default:
if (inflateInit2(&zs, -15) != Z_OK) {
xfree(zbuf);
return nullptr;
}
break;
case METADATA_ADLER32:
if (inflateInit2(&zs, 15) != Z_OK) {
xfree(zbuf);
return nullptr;
}
break;
case METADATA_CRC32:
if (inflateInit2(&zs, 15 + 16) != Z_OK) {
xfree(zbuf);
return nullptr;
}
break;
}
zs.next_in = (Bytef*)buf;
zs.avail_in = size;
zs.next_out = (Bytef*)zbuf;
zs.avail_out = zsiz;
int32_t rv = inflate(&zs, Z_FINISH);
inflateEnd(&zs);
if (rv == Z_STREAM_END) {
zsiz -= zs.avail_out;
*sp = zsiz;
return zbuf;
} else if (rv == Z_BUF_ERROR) {
if (zsiz >= INT32MAX / 2) {
break;
}
zsiz *= 2;
zbuf = static_cast<char*>(xrealloc(zbuf, zsiz));
} else {
break;
}
}
xfree(zbuf);
return nullptr;
#else
return nullptr;
#endif
}
std::unique_ptr<Compressor> ZLibCompressor::MakeCompressor() const {
return std::make_unique<ZLibCompressor>(level_, metadata_mode_);
}
ZStdCompressor::ZStdCompressor(int32_t level) : level_(level) {
assert(level >= -1 && level <= 19);
}
ZStdCompressor::~ZStdCompressor() {
}
bool ZStdCompressor::IsSupported() const {
#if _TKRZW_COMP_ZSTD
return true;
#else
return false;
#endif
}
char* ZStdCompressor::Compress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_ZSTD
int32_t zsiz = ZSTD_compressBound(size);
char* zbuf = static_cast<char*>(xmalloc(zsiz));
const size_t rsiz = ZSTD_compress(zbuf, zsiz, static_cast<const char*>(buf), size, level_);
if (ZSTD_isError(rsiz)) {
xfree(zbuf);
return zbuf;
}
*sp = rsiz;
return zbuf;
#else
return nullptr;
#endif
}
char* ZStdCompressor::Decompress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_ZSTD
int32_t zsiz = size * 8 + 32;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
while (true) {
const size_t rsiz = ZSTD_decompress(zbuf, zsiz, static_cast<const char*>(buf), size);
if (!ZSTD_isError(rsiz)) {
*sp = rsiz;
return zbuf;
}
if (zsiz >= INT32MAX / 2) {
break;
}
zsiz *= 2;
zbuf = static_cast<char*>(xrealloc(zbuf, zsiz));
}
xfree(zbuf);
return nullptr;
#else
return nullptr;
#endif
}
std::unique_ptr<Compressor> ZStdCompressor::MakeCompressor() const {
return std::make_unique<ZStdCompressor>(level_);
}
LZ4Compressor::LZ4Compressor(int32_t accelaration) : acceleration_(accelaration) {
assert(accelaration > 0);
}
LZ4Compressor::~LZ4Compressor() {
}
bool LZ4Compressor::IsSupported() const {
#if _TKRZW_COMP_LZ4
return true;
#else
return false;
#endif
}
char* LZ4Compressor::Compress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_LZ4
int32_t zsiz = LZ4_compressBound(size);
char* zbuf = static_cast<char*>(xmalloc(zsiz));
const int32_t rsiz =
LZ4_compress_fast(static_cast<const char*>(buf), zbuf, size, zsiz, acceleration_);
if (rsiz < 1) {
xfree(zbuf);
return zbuf;
}
*sp = rsiz;
return zbuf;
#else
return nullptr;
#endif
}
char* LZ4Compressor::Decompress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_LZ4
int32_t zsiz = size * 4 + 32;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
while (true) {
const int32_t rsiz = LZ4_decompress_safe(static_cast<const char*>(buf), zbuf, size, zsiz);
if (rsiz >= 0) {
*sp = rsiz;
return zbuf;
}
if (zsiz >= INT32MAX / 2) {
break;
}
zsiz *= 2;
zbuf = static_cast<char*>(xrealloc(zbuf, zsiz));
}
xfree(zbuf);
return nullptr;
#else
return nullptr;
#endif
}
std::unique_ptr<Compressor> LZ4Compressor::MakeCompressor() const {
return std::make_unique<LZ4Compressor>(acceleration_);
}
LZMACompressor::LZMACompressor(int32_t level, MetadataMode metadata_mode)
: level_(level), metadata_mode_(metadata_mode) {
assert(level >= 0 && level <= 9);
}
LZMACompressor::~LZMACompressor() {}
bool LZMACompressor::IsSupported() const {
#if _TKRZW_COMP_LZMA
return true;
#else
return false;
#endif
}
char* LZMACompressor::Compress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_LZMA
assert(buf != nullptr && size <= MAX_MEMORY_SIZE && sp != nullptr);
lzma_stream zs = LZMA_STREAM_INIT;
const char* rp = (const char*)buf;
size_t zsiz = size + 1024;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
char* wp = zbuf;
zs.next_in = (const uint8_t*)rp;
zs.avail_in = size;
zs.next_out = (uint8_t*)wp;
zs.avail_out = zsiz;
switch (metadata_mode_) {
default: {
if (lzma_easy_encoder(&zs, level_, LZMA_CHECK_NONE) != LZMA_OK) {
xfree(zbuf);
return nullptr;
}
break;
}
case METADATA_CRC32: {
if (lzma_easy_encoder(&zs, level_, LZMA_CHECK_CRC32) != LZMA_OK) {
xfree(zbuf);
return nullptr;
}
break;
}
case METADATA_SHA256: {
if (lzma_easy_encoder(&zs, level_, LZMA_CHECK_SHA256) != LZMA_OK) {
xfree(zbuf);
return nullptr;
}
break;
}
}
if (lzma_code(&zs, LZMA_FINISH) != LZMA_STREAM_END) {
xfree(zbuf);
lzma_end(&zs);
return nullptr;
}
lzma_end(&zs);
zsiz -= zs.avail_out;
*sp = zsiz;
return zbuf;
#else
return nullptr;
#endif
}
char* LZMACompressor::Decompress(const void* buf, size_t size, size_t* sp) {
#if _TKRZW_COMP_LZMA
assert(buf != nullptr && size <= MAX_MEMORY_SIZE && sp != nullptr);
size_t zsiz = size * 12 + 32;
char* zbuf = static_cast<char*>(xmalloc(zsiz));
while (true) {
lzma_stream zs = LZMA_STREAM_INIT;
const char* rp = (const char*)buf;
char* wp = zbuf;
zs.next_in = (const uint8_t*)rp;
zs.avail_in = size;
zs.next_out = (uint8_t*)wp;
zs.avail_out = zsiz;
if (lzma_auto_decoder(&zs, 1ULL << 30, 0) != LZMA_OK) {
xfree(zbuf);
return nullptr;
}
int32_t rv = lzma_code(&zs, LZMA_FINISH);
lzma_end(&zs);
if (rv == LZMA_STREAM_END) {
zsiz -= zs.avail_out;
*sp = zsiz;
return zbuf;
} else if (rv == LZMA_OK) {
if (zsiz >= INT32MAX / 2) {
break;
}
zsiz *= 2;
zbuf = static_cast<char*>(xrealloc(zbuf, zsiz));
} else {
break;
}
}
xfree(zbuf);
return nullptr;
#else
return nullptr;
#endif
}
std::unique_ptr<Compressor> LZMACompressor::MakeCompressor() const {
return std::make_unique<LZMACompressor>(level_, metadata_mode_);
}
} // namespace tkrzw
// END OF FILE