forked from benoitc/eep0018
/
decode.c
296 lines (254 loc) · 6.76 KB
/
decode.c
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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "erl_nif.h"
#include "erl_nif_compat.h"
#include "yajl/yajl_parse.h"
#include "yajl/yajl_parser.h"
#include "yajl/yajl_lex.h"
typedef struct {
ERL_NIF_TERM head;
ErlNifEnv* env;
} decode_ctx;
#define ENV(ctxarg) (((decode_ctx*)ctxarg)->env)
#define CONTINUE 1
#define CANCEL 0
static ERL_NIF_TERM
make_error(yajl_handle handle, ErlNifEnv* env)
{
char* yajlError = (char*) yajl_get_error(handle, 0, NULL, 0);
ERL_NIF_TERM errMsg;
if(yajlError != NULL)
{
errMsg = enif_make_string(env, yajlError, ERL_NIF_LATIN1);
yajl_free_error(handle, (unsigned char*) yajlError);
}
else
{
errMsg = enif_make_string(env, "unknown parse error", ERL_NIF_LATIN1);
}
return enif_make_tuple(env, 2,
enif_make_atom(env, "error"),
enif_make_tuple(env, 2,
enif_make_uint(env, handle->bytesConsumed),
errMsg
)
);
}
static void
add_to_head(void* vctx, ERL_NIF_TERM newhead)
{
decode_ctx* ctx = (decode_ctx*)vctx;
ctx->head = enif_make_list_cell(ctx->env, newhead, ctx->head);
}
static int
decode_null(void* ctx)
{
add_to_head(ctx, enif_make_atom(ENV(ctx), "null"));
return CONTINUE;
}
static int
decode_boolean(void* ctx, int val)
{
add_to_head(ctx, enif_make_atom(ENV(ctx), val ? "true" : "false"));
return CONTINUE;
}
static int
decode_number(void * ctx, const char * numberVal, unsigned int numberLen)
{
// scan in the input to see if it's a float or int
int numberType = 0; // 0 means integer, 1 means float
unsigned int i;
ErlNifBinary bin;
int missingDot = 1;
unsigned int expPos;
for(i=0; i<numberLen; i++) {
switch (numberVal[i]) {
case '.':
missingDot = 0;
numberType = 1; // it's a float
goto loopend;
case 'E':
case 'e':
expPos = i;
numberType = 1; // it's a float
goto loopend;
}
}
loopend:
if ((numberType == 1) && missingDot)
{
if(!enif_alloc_binary_compat(ENV(ctx), numberLen + 2, &bin))
{
return CANCEL;
}
memcpy(bin.data, numberVal, expPos);
bin.data[expPos] = '.';
bin.data[expPos + 1] = '0';
memcpy(bin.data + expPos + 2, numberVal + expPos, numberLen - expPos);
}
else
{
if(!enif_alloc_binary_compat(ENV(ctx), numberLen, &bin))
{
return CANCEL;
}
memcpy(bin.data, numberVal, numberLen);
}
add_to_head(ctx, enif_make_tuple(ENV(ctx), 2,
enif_make_int(ENV(ctx), numberType),
enif_make_binary(ENV(ctx), &bin)));
return CONTINUE;
}
static int
decode_string(void* ctx, const unsigned char* data, unsigned int size)
{
ErlNifBinary bin;
if(!enif_alloc_binary_compat(ENV(ctx), size, &bin))
{
return CANCEL;
}
memcpy(bin.data, data, size);
add_to_head(ctx, enif_make_binary(ENV(ctx), &bin));
return CONTINUE;
}
static int
decode_start_array(void* ctx)
{
add_to_head(ctx, enif_make_int(ENV(ctx), 0));
return CONTINUE;
}
static int
decode_end_array(void* ctx)
{
add_to_head(ctx, enif_make_int(ENV(ctx), 1));
return CONTINUE;
}
static int
decode_start_map(void* ctx)
{
add_to_head(ctx, enif_make_int(ENV(ctx), 2));
return CONTINUE;
}
static int
decode_end_map(void* ctx)
{
add_to_head(ctx, enif_make_int(ENV(ctx), 3));
return CONTINUE;
}
static int
decode_map_key(void* ctx, const unsigned char* data, unsigned int size)
{
ErlNifBinary bin;
if(!enif_alloc_binary_compat(ENV(ctx), size, &bin))
{
return CANCEL;
}
memcpy(bin.data, data, size);
add_to_head(ctx, enif_make_tuple(ENV(ctx), 2,
enif_make_int(ENV(ctx), 3),
enif_make_binary(ENV(ctx), &bin)));
return CONTINUE;
}
static yajl_callbacks
decoder_callbacks = {
decode_null,
decode_boolean,
NULL,
NULL,
decode_number,
decode_string,
decode_start_map,
decode_map_key,
decode_end_map,
decode_start_array,
decode_end_array
};
static int
check_rest(unsigned char* data, unsigned int size, unsigned int used)
{
unsigned int i = 0;
for(i = used; i < size; i++)
{
switch(data[i])
{
case ' ':
case '\t':
case '\r':
case '\n':
continue;
default:
return CANCEL;
}
}
return CONTINUE;
}
ERL_NIF_TERM
reverse_tokens(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
decode_ctx ctx;
yajl_parser_config conf = {0, 1}; // No comments, check utf8
yajl_handle handle = yajl_alloc(&decoder_callbacks, &conf, NULL, &ctx);
yajl_status status;
unsigned int used;
ErlNifBinary bin;
ERL_NIF_TERM ret;
ctx.env = env;
ctx.head = enif_make_list_from_array(env, NULL, 0);
if(!enif_inspect_iolist_as_binary(env, argv[0], &bin))
{
ret = enif_make_badarg(env);
goto done;
}
status = yajl_parse(handle, bin.data, bin.size);
used = handle->bytesConsumed;
// Parsing something like "2.0" (without quotes) will
// cause a spurious semi-error. We add the extra size
// check so that "2008-20-10" doesn't pass.
if(status == yajl_status_insufficient_data && used == bin.size)
{
status = yajl_parse_complete(handle);
}
if(status == yajl_status_ok && used != bin.size)
{
if(check_rest(bin.data, bin.size, used) == CANCEL)
{
ret = enif_make_tuple(env, 2,
enif_make_atom(env, "error"),
enif_make_atom(env, "garbage_after_value")
);
goto done;
}
}
switch(status)
{
case yajl_status_ok:
ret = enif_make_tuple(env, 2, enif_make_atom(env, "ok"), ctx.head);
goto done;
case yajl_status_error:
ret = make_error(handle, env);
goto done;
case yajl_status_insufficient_data:
ret = enif_make_tuple(env, 2,
enif_make_atom(env, "error"),
enif_make_atom(env, "insufficient_data")
);
goto done;
case yajl_status_client_canceled:
/* the only time we do this is when we can't allocate a binary. */
ret = enif_make_tuple(env, 2,
enif_make_atom(env, "error"),
enif_make_atom(env, "insufficient_memory")
);
goto done;
default:
ret = enif_make_tuple(env, 2,
enif_make_atom(env, "error"),
enif_make_atom(env, "unknown")
);
goto done;
}
done:
if(handle != NULL) yajl_free(handle);
return ret;
}