-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson.c
293 lines (261 loc) · 6.24 KB
/
json.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
/*
Copyright (c) 2014
vurtun <polygone@gmx.net>
MIT license
*/
#include <stdlib.h>
#include <string.h>
#include "json.h"
/* remove gcc warning for static init*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverride-init"
struct json_iter
json_begin(const json_char* str, json_size len)
{
struct json_iter iter;
memset(&iter, 0, sizeof iter);
iter.src = str;
iter.len = len;
return iter;
}
struct json_iter
json_read(const struct json_iter* prev, struct json_token *obj)
{
static const void *go_struct[] = {
[0 ... 255] = &&l_fail,
['\t'] = &&l_loop,
['\r'] = &&l_loop,
['\n'] = &&l_loop,
[' '] = &&l_loop,
['"'] = &&l_qup,
[':'] = &&l_loop,
['='] = &&l_loop,
[','] = &&l_loop,
['['] = &&l_up,
[']'] = &&l_down,
['{'] = &&l_up,
['}'] = &&l_down,
['-'] = &&l_bare,
[49 ... 57] = &&l_bare,
['t'] = &&l_bare,
['f'] = &&l_bare,
['n'] = &&l_bare,
};
static const void *go_bare[] = {
[0 ... 255] = &&l_fail,
[32 ... 126]= &&l_loop,
['\t'] = &&l_unbare,
['\r'] = &&l_unbare,
['\n'] = &&l_unbare,
[','] = &&l_unbare,
[']'] = &&l_unbare,
['}'] = &&l_unbare,
};
static const void *go_string[] = {
[0 ... 31] = &&l_fail,
[32 ... 126] = &&l_loop,
[127] = &&l_fail,
['\\'] = &&l_esc,
['"'] = &&l_qdown,
[128 ... 191] = &&l_fail,
[192 ... 223] = &&l_utf8_2,
[224 ... 239] = &&l_utf8_3,
[240 ... 247] = &&l_utf8_4,
[248 ... 255] = &&l_fail,
};
static const void *go_utf8[] = {
[0 ... 127] = &&l_fail,
[128 ... 191] = &&l_utf_next,
[192 ... 255] = &&l_fail
};
static const void *go_esc[] = {
[0 ... 255] = &&l_fail,
['"'] = &&l_unesc,
['\\'] = &&l_unesc,
['/'] = &&l_unesc,
['b'] = &&l_unesc,
['f'] = &&l_unesc,
['n'] = &&l_unesc,
['r'] = &&l_unesc,
['t'] = &&l_unesc,
['u'] = &&l_unesc
};
if (!prev || !obj || !prev->src || !prev->len || prev->err) {
struct json_iter it;
memset(&it, 0, sizeof it);
memset(&obj, 0, sizeof(*obj));
it.err = 1;
return it;
}
struct json_iter iter;
memset(obj, 0, sizeof(*obj));
memcpy(&iter, prev, sizeof iter);
iter.err = 0;
if (!iter.go)
iter.go = go_struct;
int len = iter.len;
const json_char *cur;
int utf8_remain = 0;
for (cur = iter.src; len; cur++, len--) {
goto *iter.go[*cur];
l_loop:;
}
if (!iter.depth) {
iter.src = NULL;
iter.len = 0;
if (obj->str)
obj->len = (cur-1) - obj->str;
return iter;
}
l_fail:
iter.err = 1;
return iter;
l_up:
if (iter.depth++ == 1)
obj->str = cur;
goto l_loop;
l_down:
if (--iter.depth == 1) {
obj->len = cur - obj->str;
goto l_yield;
}
goto l_loop;
l_qup:
iter.go = go_string;
if (iter.depth == 1)
obj->str = cur;
goto l_loop;
l_qdown:
iter.go = go_struct;
if (iter.depth == 1) {
obj->len = (cur - obj->str);
goto l_yield;
}
goto l_loop;
l_esc:
iter.go = go_esc;
goto l_loop;
l_unesc:
iter.go = go_string;
goto l_loop;
l_bare:
if (iter.depth == 1)
obj->str = cur;
iter.go = go_bare;
goto l_loop;
l_unbare:
iter.go = go_struct;
if (iter.depth == 1) {
obj->len = (cur - obj->str);
iter.src = cur;
iter.len = len;
return iter;
}
goto *iter.go[*cur];
l_utf8_2:
iter.go = go_utf8;
utf8_remain = 1;
goto l_loop;
l_utf8_3:
iter.go = go_utf8;
utf8_remain = 2;
goto l_loop;
l_utf8_4:
iter.go = go_utf8;
utf8_remain = 3;
goto l_loop;
l_utf_next:
if (!--utf8_remain)
iter.go = go_string;
goto l_loop;
l_yield:
if (iter.depth != 1 || !obj->str)
goto l_loop;
iter.src = cur + 1;
iter.len = len - 1;
return iter;
}
struct json_iter
json_parse(const struct json_iter* it, json_pair p)
{
struct json_iter next;
next = json_read(it, &p[JSON_NAME]);
if (next.err)
return next;
return json_read(&next, &p[JSON_VALUE]);
}
json_char*
json_dup(struct json_token* tok, void*(*alloc)(size_t))
{
if (!tok || !alloc)
return NULL;
json_char *str = alloc(tok->len + 1);
if (!str)
return NULL;
memcpy(str, tok->str, tok->len);
str[tok->len] = '\0';
return str;
}
int
json_cpy(json_char *dst, json_size max, struct json_token* tok)
{
if (!dst || !max || !tok)
return -1;
int result;
json_size *siz;
if (max <= (tok->len + 1)) {
result = max;
max--;
siz = &max;
} else {
result = tok->len;
siz = &tok->len;
}
memcpy(dst, tok->str, *siz);
dst[*siz] = '\0';
return result;
}
int
json_cmp(const struct json_token* tok, const json_char* str)
{
if (!tok || !str)
return 1;
json_size i;
for (i = 0; (i < tok->len && *str); i++, str++){
if (tok->str[i] != *str)
return 1;
}
return 0;
}
enum json_typ
json_type(const struct json_token *tok)
{
if (!tok || !tok->str || !tok->len)
return JSON_NONE;
if (tok->str[0] == '{') {
if (tok->str[tok->len] == '}')
return JSON_OBJECT;
else
return JSON_NONE;
}
if (tok->str[0] == '[') {
if (tok->str[tok->len] == ']')
return JSON_ARRAY;
else
return JSON_NONE;
}
if (tok->str[0] == '\"') {
if (tok->str[tok->len] == '\"')
return JSON_STRING;
else
return JSON_NONE;
}
if (!json_cmp(tok, (json_char*)"true"))
return JSON_TRUE;
if (!json_cmp(tok, (json_char*)"false"))
return JSON_FALSE;
if (!json_cmp(tok, (json_char*)"null"))
return JSON_NULL;
return JSON_NUMBER;
}
#pragma GCC diagnostic pop