/
hs_snappy.cpp
105 lines (87 loc) · 2.28 KB
/
hs_snappy.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
#include "hs_snappy.h"
#include "snappy.h"
#include "snappy-sinksource.h"
using namespace snappy;
size_t _hsnappy_MaxCompressedLength(size_t n)
{
return MaxCompressedLength(n);
}
void _hsnappy_RawCompress(const char *input, size_t input_length,
char *compressed, size_t *compressed_length)
{
RawCompress(input, input_length, compressed, compressed_length);
}
int _hsnappy_GetUncompressedLength(const char *compressed,
size_t compressed_length,
size_t *result)
{
return GetUncompressedLength(compressed, compressed_length, result);
}
int _hsnappy_RawUncompress(const char *compressed, size_t compressed_length,
char *uncompressed)
{
return RawUncompress(compressed, compressed_length, uncompressed);
}
class BSSource : public Source
{
public:
BSSource(BS *chunks, size_t nchunks, size_t size)
: chunks_(chunks), nchunks_(nchunks), size_(size), cur_(chunks),
left_(size) { }
size_t Available() const { return left_; }
const char *Peek(size_t *len) {
if (left_ > 0) {
*len = cur_->len - cur_->off;
return cur_->ptr + cur_->off;
} else {
*len = 0;
return NULL;
}
}
void Skip(size_t n) {
if (n > 0) {
left_ -= n;
cur_->off += n;
if (cur_->off == cur_->len)
cur_++;
}
}
void Rewind() {
left_ = size_;
cur_ = chunks_;
for (size_t i = 0; i < nchunks_ && chunks_[i].off > 0; i++)
chunks_[i].off = 0;
}
private:
BS *chunks_;
const size_t nchunks_;
const size_t size_;
BS *cur_;
size_t left_;
};
void _hsnappy_CompressChunks(BS *chunks, size_t nchunks, size_t length,
char *compressed, size_t *compressed_length)
{
BSSource reader(chunks, nchunks, length);
UncheckedByteArraySink writer(compressed);
Compress(&reader, &writer);
*compressed_length = writer.CurrentDestination() - compressed;
}
BSSource *_hsnappy_NewSource(BS *chunks, size_t nchunks, size_t length)
{
return new BSSource(chunks, nchunks, length);
}
void _hsnappy_DeleteSource(BSSource *src)
{
delete src;
}
int _hsnappy_UncompressChunks(BSSource *reader, char *uncompressed)
{
return RawUncompress(reader, uncompressed);
}
int _hsnappy_GetUncompressedLengthChunks(BSSource *reader, uint32_t *result)
{
int n = GetUncompressedLength(reader, result);
reader->Rewind();
return n;
}