-
Notifications
You must be signed in to change notification settings - Fork 182
/
json.c
467 lines (379 loc) · 8.25 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* json.c - flat JSON parsing
* Copyright (C) 2014-2019 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "line.h"
#include "log.h"
#include "map.h"
#include "sys.h"
/* Return the number of UTF-8 bytes on success, 0 if it is invalid */
static size_t json_parse_codepoint(const char *str, char *buf, size_t size)
{
uint16_t codepoint = 0;
char utf8[3];
size_t len;
int hex;
char c;
int i;
for (i = 0; i < 4; i++) {
c = str[i];
if (!isxdigit(c))
return 0;
if (c >= '0' && c <= '9')
hex = c - '0';
else if (c >= 'a' && c <= 'f')
hex = c - 'a' + 10;
else
hex = c - 'A' + 10;
codepoint |= hex << (12 - i * 4);
}
/* Support Only a single surrogate at the moment */
if (codepoint <= 0x7f) {
len = 1;
utf8[0] = codepoint;
} else if (codepoint <= 0x7ff) {
len = 2;
utf8[0] = 0xc0 | (codepoint >> 6);
utf8[1] = 0x80 | (codepoint & 0x3f);
} else {
len = 3;
utf8[0] = (0xe0 | (codepoint >> 12));
utf8[1] = (0x80 | ((codepoint >> 6) & 0x3f));
utf8[2] = (0x80 | (codepoint & 0x3f));
}
if (buf) {
if (size < len)
return 0;
memcpy(buf, utf8, len);
}
return len;
}
/* Return the length of the parsed string, 0 if it is invalid */
static size_t json_parse_string(const char *str, char *buf, size_t size)
{
const char *end = str;
size_t len;
char c;
if (*end != '"')
return 0;
do {
len = 0;
switch (*++end) {
case '\0':
return 0;
case '"':
c = '\0';
break;
case '\\':
switch (*++end) {
case '"':
c = '"';
break;
case '\\':
c = '\\';
break;
case '/':
c = '/';
break;
case 'b':
c = '\b';
break;
case 'f':
c = '\f';
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'u':
len = json_parse_codepoint(++end, buf, size);
if (!len)
return 0;
end += 3; /* jump to last hex digit */
break;
default:
return 0;
}
break;
default:
if (iscntrl(*end))
return 0;
c = *end;
break;
}
if (buf) {
if (!len) {
if (size < 1)
return 0;
*buf = c;
len = 1;
}
buf += len;
size -= len;
}
} while (c);
return ++end - str;
}
/* Return the length of the parsed non scalar (with open/close delimiter), 0 if it is invalid */
static size_t json_parse_nested_struct(const char *str, char open, char close,
char *buf, size_t size)
{
const char *end = str;
size_t len;
int nested;
if (*str != open)
return 0;
nested = 1;
while (nested) {
++end;
/* control character or end-of-line? */
if (iscntrl(*end) || *end == '\0')
return 0;
if (*end == open)
nested++;
else if (*end == close)
nested--;
}
len = ++end - str;
if (!len)
return 0;
if (buf) {
if (size <= len)
return 0;
strncpy(buf, str, len);
buf[len] = '\0';
}
return len;
}
/* Return the length of the parsed array, 0 if it is invalid */
static size_t json_parse_nested_array(const char *str, char *buf, size_t size)
{
return json_parse_nested_struct(str, '[', ']', buf, size);
}
/* Return the length of the parsed object, 0 if it is invalid */
static size_t json_parse_nested_object(const char *str, char *buf, size_t size)
{
return json_parse_nested_struct(str, '{', '}', buf, size);
}
/* Return the length of the parsed number, 0 if it is invalid */
static size_t json_parse_number(const char *str, char *buf, size_t size)
{
size_t len;
char *end;
strtoul(str, &end, 10);
len = end - str;
if (!len)
return 0;
if (buf) {
if (size <= len)
return 0;
strncpy(buf, str, len);
buf[len] = '\0';
}
return len;
}
/* Return the length of the parsed literal, 0 if it is invalid */
static size_t json_parse_literal(const char *str, const char *literal,
char *buf, size_t size)
{
const size_t len = strlen(literal);
if (strncmp(str, literal, len) != 0)
return 0;
if (buf) {
strncpy(buf, literal, size);
if (buf[size - 1] != '\0')
return 0;
}
return len;
}
/* A value can be a string, number, object, array, true, false, or null */
static size_t json_parse_value(const char *str, char *buf, size_t size)
{
size_t len;
len = json_parse_string(str, buf, size);
if (len)
return len;
len = json_parse_number(str, buf, size);
if (len)
return len;
len = json_parse_nested_object(str, buf, size);
if (len)
return len;
len = json_parse_nested_array(str, buf, size);
if (len)
return len;
len = json_parse_literal(str, "true", buf, size);
if (len)
return len;
len = json_parse_literal(str, "false", buf, size);
if (len)
return len;
len = json_parse_literal(str, "null", buf, size);
if (len)
return len;
return 0;
}
/* Return the length of a separator optionally enclosed by whitespaces, 0 otherwise */
static size_t json_parse_sep(const char *str, char sep)
{
size_t len = 0;
while (isspace(*str))
str++, len++;
if (*str != sep)
return 0;
str++;
len++;
while (isspace(*str))
str++, len++;
return len;
}
/* Parse an inline ["name"][\s+:\s+][value] name-value pair */
static size_t json_parse_pair(const char *str, char *name, size_t namesiz,
char *val, size_t valsiz)
{
size_t pair_len = 0;
size_t len;
len = json_parse_string(str, name, namesiz);
if (!len)
return 0;
pair_len += len;
str += len;
len = json_parse_sep(str, ':');
if (!len)
return 0;
pair_len += len;
str += len;
len = json_parse_value(str, val, valsiz);
if (!len)
return 0;
pair_len += len;
return pair_len;
}
static int json_line_cb(char *line, size_t num, void *data)
{
struct map *map = data;
char name[BUFSIZ];
char val[BUFSIZ];
size_t len;
int err;
for (;;) {
/* Only support inline flattened structures at the moment */
while (*line == '[' || *line == ']' || *line == ',' ||
*line == '{' || *line == '}' || isspace(*line))
line++;
if (*line == '\0')
break;
memset(name, 0, sizeof(name));
memset(val, 0, sizeof(val));
len = json_parse_pair(line, name, sizeof(name), val,
sizeof(val));
if (!len)
return -EINVAL;
line += len;
/* valid delimiters after a pair */
if (*line != ',' && *line != '}' && *line != '\0' &&
!isspace(*line))
return -EINVAL;
if (*line != '\0')
line++;
if (map) {
err = map_set(map, name, val);
if (err)
return err;
}
}
return 0;
}
int json_read(int fd, size_t count, struct map *map)
{
return line_read(fd, count, json_line_cb, map);
}
bool json_is_string(const char *str)
{
size_t len;
len = strlen(str);
if (!len)
return false;
return json_parse_string(str, NULL, 0) == len;
}
bool json_is_valid(const char *str)
{
size_t len;
len = strlen(str);
if (!len)
return false;
return json_parse_value(str, NULL, 0) == len;
}
int json_escape(const char *str, char *buf, size_t size)
{
size_t null = strlen(str) + 1;
char c = '\0';
int len;
do {
switch (c) {
case '\0':
len = snprintf(buf, size, "\"");
break;
case '\b':
len = snprintf(buf, size, "\\b");
break;
case '\f':
len = snprintf(buf, size, "\\f");
break;
case '\n':
len = snprintf(buf, size, "\\n");
break;
case '\r':
len = snprintf(buf, size, "\\r");
break;
case '\t':
len = snprintf(buf, size, "\\t");
break;
case '\\':
len = snprintf(buf, size, "\\\\");
break;
case '"':
len = snprintf(buf, size, "\\\"");
break;
default:
if (iscntrl(c))
len = snprintf(buf, size, "\\u%04x", c);
else
len = snprintf(buf, size, "%c", c);
break;
}
/* Ensure the result was not truncated */
if (len < 0 || len >= size)
return -ENOSPC;
size -= len;
buf += len;
c = *str;
if (c)
str++;
} while (null--);
return 0;
}