/
snappy_nif.cc
265 lines (210 loc) · 5.93 KB
/
snappy_nif.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
/**
* Copyright 2011, Filipe David Manana <fdmanana@apache.org>
* Web: http://github.com/fdmanana/snappy-erlang-nif
*
* 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
*
* http://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 <iostream>
#include <cstring>
#include "erl_nif_compat.h"
#include "google-snappy/snappy.h"
#include "google-snappy/snappy-sinksource.h"
#ifdef OTP_R13B03
#error OTP R13B03 not supported. Upgrade to R13B04 or later.
#endif
#ifdef __cplusplus
#define BEGIN_C extern "C" {
#define END_C }
#else
#define BEGIN_C
#define END_C
#endif
#define SC_PTR(c) reinterpret_cast<char *>(c)
class SnappyNifSink : public snappy::Sink
{
public:
SnappyNifSink(ErlNifEnv* e);
~SnappyNifSink();
void Append(const char* data, size_t n);
char* GetAppendBuffer(size_t len, char* scratch);
ErlNifBinary& getBin();
private:
ErlNifEnv* env;
ErlNifBinary bin;
size_t length;
};
SnappyNifSink::SnappyNifSink(ErlNifEnv* e) : env(e), length(0)
{
if(!enif_alloc_binary_compat(env, 0, &bin)) {
env = NULL;
throw std::bad_alloc();
}
}
SnappyNifSink::~SnappyNifSink()
{
if(env != NULL) {
enif_release_binary_compat(env, &bin);
}
}
void
SnappyNifSink::Append(const char *data, size_t n)
{
if(data != (SC_PTR(bin.data) + length)) {
memcpy(bin.data + length, data, n);
}
length += n;
}
char*
SnappyNifSink::GetAppendBuffer(size_t len, char* scratch)
{
size_t sz;
if((length + len) > bin.size) {
sz = (len * 4) < 8192 ? 8192 : (len * 4);
if(!enif_realloc_binary_compat(env, &bin, bin.size + sz)) {
throw std::bad_alloc();
}
}
return SC_PTR(bin.data) + length;
}
ErlNifBinary&
SnappyNifSink::getBin()
{
if(bin.size > length) {
if(!enif_realloc_binary_compat(env, &bin, length)) {
throw std::bad_alloc();
}
}
return bin;
}
BEGIN_C
static inline ERL_NIF_TERM
make_atom(ErlNifEnv* env, const char* name)
{
ERL_NIF_TERM ret;
if(enif_make_existing_atom_compat(env, name, &ret, ERL_NIF_LATIN1)) {
return ret;
}
return enif_make_atom(env, name);
}
static inline ERL_NIF_TERM
make_ok(ErlNifEnv* env, ERL_NIF_TERM mesg)
{
ERL_NIF_TERM ok = make_atom(env, "ok");
return enif_make_tuple2(env, ok, mesg);
}
static inline ERL_NIF_TERM
make_error(ErlNifEnv* env, const char* mesg)
{
ERL_NIF_TERM error = make_atom(env, "error");
return enif_make_tuple2(env, error, make_atom(env, mesg));
}
ERL_NIF_TERM
snappy_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary input;
if(!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}
try {
snappy::ByteArraySource source(SC_PTR(input.data), input.size);
SnappyNifSink sink(env);
snappy::Compress(&source, &sink);
return make_ok(env, enif_make_binary(env, &sink.getBin()));
} catch(std::bad_alloc e) {
return make_error(env, "insufficient_memory");
} catch(...) {
return make_error(env, "unknown");
}
}
ERL_NIF_TERM
snappy_decompress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
ErlNifBinary ret;
size_t len;
if(!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
return enif_make_badarg(env);
}
try {
if(!snappy::GetUncompressedLength(SC_PTR(bin.data), bin.size, &len)) {
return make_error(env, "data_not_compressed");
}
if(!enif_alloc_binary_compat(env, len, &ret)) {
return make_error(env, "insufficient_memory");
}
if(!snappy::RawUncompress(SC_PTR(bin.data), bin.size,
SC_PTR(ret.data))) {
return make_error(env, "corrupted_data");
}
return make_ok(env, enif_make_binary(env, &ret));
} catch(...) {
return make_error(env, "unknown");
}
}
ERL_NIF_TERM
snappy_uncompressed_length(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
size_t len;
if(!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
return enif_make_badarg(env);
}
try {
if(!snappy::GetUncompressedLength(SC_PTR(bin.data), bin.size, &len)) {
return make_error(env, "data_not_compressed");
}
return make_ok(env, enif_make_ulong(env, len));
} catch(...) {
return make_error(env, "unknown");
}
}
ERL_NIF_TERM
snappy_is_valid(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
if (!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
return enif_make_badarg(env);
}
try {
if(snappy::IsValidCompressedBuffer(SC_PTR(bin.data), bin.size)) {
return make_atom(env, "true");
} else {
return make_atom(env, "false");
}
} catch(...) {
return make_error(env, "unknown");
}
}
int
on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
return 0;
}
int
on_reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
return 0;
}
int
on_upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM info)
{
return 0;
}
static ErlNifFunc nif_functions[] = {
{"compress", 1, snappy_compress},
{"decompress", 1, snappy_decompress},
{"uncompressed_length", 1, snappy_uncompressed_length},
{"is_valid", 1, snappy_is_valid}
};
ERL_NIF_INIT(snappy, nif_functions, &on_load, &on_reload, &on_upgrade, NULL);
END_C