This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
json.c
413 lines (376 loc) · 10.2 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
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "json.h"
#include "util.h"
#include "utf8.h"
#define UTF8_FULL 0
enum json_state {
VALUE = (1<<0),
ARRAY_EMPTY = (1<<1),
ARRAY_NONEMPTY = (1<<2),
OBJECT_EMPTY = (1<<3),
OBJECT_NONEMPTY = (1<<4),
STRING = (1<<5),
IOERR = (1<<6),
};
struct json_context {
struct json_context* previous;
enum json_state state;
};
struct json_writer {
FILE* out;
struct json_writer_config config;
struct json_context* context;
uint32_t utf8_state;
uint8_t utf8_buf[5];
uint8_t utf8_bufsz;
};
static const char*
json_state_name(enum json_state state)
{
#ifndef NDEBUG
switch (state) {
case VALUE: return "VALUE";
case ARRAY_EMPTY: return "ARRAY_EMPTY";
case ARRAY_NONEMPTY: return "ARRAY_NONEMPTY";
case OBJECT_EMPTY: return "OBJECT_EMPTY";
case OBJECT_NONEMPTY: return "OBJECT_NONEMPTY";
case STRING: return "STRING";
case IOERR: return "IOERR";
}
return "?"; // Keep outside switch to preserve compiler warnings
#else
return xaprintf("0x%08X", (unsigned) state);
#endif
}
static const char*
json_state_names(unsigned states)
{
#ifndef NDEBUG
unsigned statebit = 0;
const char* set = "{";
int nstate = 0;
while (states != 0) {
if (states & 1) {
enum json_state state = (1<<statebit);
set = xaprintf("%s%s%s",
set,
nstate++ > 0 ? "," : "",
json_state_name(state));
}
states >>= 1;
statebit++;
}
return xaprintf("%s}", set);
#else
return xaprintf("0x%08X", (unsigned) states);
#endif
}
static void
json_push_context(struct json_writer* writer, enum json_state state)
{
struct json_context* context = malloc(sizeof (*context));
if (context == NULL)
die_oom();
context->state = state;
context->previous = writer->context;
writer->context = context;
}
static void
json_pop_context(struct json_writer* writer)
{
struct json_context* context = writer->context;
writer->context = context->previous;
free(context);
}
static void
json_writer_cleanup(void* data)
{
struct json_writer* writer = data;
while (writer->context)
json_pop_context(writer);
free(writer);
}
static enum json_state
json_writer_state(struct json_writer* writer)
{
if (writer->context == NULL)
die(EINVAL, "json writer terminated");
return writer->context->state;
}
static void
json_check_state(struct json_writer* writer, unsigned allowed_states)
{
if ((json_writer_state(writer) & allowed_states) == 0)
die(EINVAL,
"json writer in illegal state %s: need one in %s",
json_state_name(json_writer_state(writer)),
json_state_names(allowed_states));
}
static void
json_emitc(struct json_writer* writer, char c)
{
if (putc(c, writer->out) == EOF) {
writer->context->state = IOERR;
die_errno("putc");
}
}
static void
json_emits(struct json_writer* writer, const char* s)
{
char c;
while ((c = *s++) != '\0')
json_emitc(writer, c);
}
static void
json_value_start(struct json_writer* writer)
{
json_check_state(writer, VALUE | ARRAY_EMPTY | ARRAY_NONEMPTY);
if (json_writer_state(writer) == ARRAY_NONEMPTY)
json_emitc(writer, ',');
else if (json_writer_state(writer) == ARRAY_EMPTY)
writer->context->state = ARRAY_NONEMPTY;
}
static void
json_value_end(struct json_writer* writer)
{
if (json_writer_state(writer) == VALUE)
json_pop_context(writer);
}
struct json_writer*
json_writer_create(FILE* out)
{
struct cleanup* cl = cleanup_allocate();
struct json_writer* writer = calloc(1, sizeof (*writer));
if (writer == NULL)
die_oom();
cleanup_commit(cl, json_writer_cleanup, writer);
writer->out = out;
writer->config.bad_utf8_mode = JSON_WRITER_BAD_UTF8_REPLACE;
writer->config.bad_utf8_replacement = "\\uFFFD";
json_push_context(writer, VALUE);
return writer;
}
void
json_begin_array(struct json_writer* writer)
{
json_value_start(writer);
json_push_context(writer, ARRAY_EMPTY);
json_emitc(writer, '[');
}
void
json_end_array(struct json_writer* writer)
{
json_check_state(writer, ARRAY_EMPTY | ARRAY_NONEMPTY);
json_pop_context(writer);
json_emitc(writer, ']');
json_value_end(writer);
}
void
json_begin_object(struct json_writer* writer)
{
json_value_start(writer);
json_push_context(writer, OBJECT_EMPTY);
json_emitc(writer, '{');
}
void
json_end_object(struct json_writer* writer)
{
json_check_state(writer, OBJECT_EMPTY | OBJECT_NONEMPTY);
json_pop_context(writer);
json_emitc(writer, '}');
json_value_end(writer);
}
static void
json_emit_ascii(struct json_writer* writer, uint8_t c)
{
if (c == '"' || c == '\\') {
json_emitc(writer, '\\');
json_emitc(writer, c);
} else if (c == '\n') {
json_emits(writer, "\\n");
} else if (c == '\r') {
json_emits(writer, "\\r");
} else if (c == '\t') {
json_emits(writer, "\\t");
} else if (c == '\f') {
json_emits(writer, "\\f");
} else if (c <= 0x1F || c == 0x7F) {
char buf[sizeof ("\\uFFFF")];
sprintf(buf, "\\u%04x", (unsigned) c);
json_emits(writer, buf);
} else {
json_emitc(writer, c);
}
}
static void
json_bad_utf8(struct json_writer* writer)
{
switch (writer->config.bad_utf8_mode) {
case JSON_WRITER_BAD_UTF8_DELETE:
break;
case JSON_WRITER_BAD_UTF8_DIE:
die(EINVAL, "invalid UTF-8 sequence");
case JSON_WRITER_BAD_UTF8_REPLACE:
json_emits(writer, writer->config.bad_utf8_replacement);
break;
}
}
void
json_emit_string_part(struct json_writer* writer,
const char* s,
size_t n)
{
json_check_state(writer, STRING);
for (size_t i = 0; i < n; ++i) {
uint8_t c = s[i];
assert(writer->utf8_bufsz < sizeof (writer->utf8_buf));
writer->utf8_buf[writer->utf8_bufsz++] = c;
switch (utf8_decode(&writer->utf8_state, c)) {
case UTF8_ACCEPT: {
if (writer->utf8_bufsz == 1) {
json_emit_ascii(writer, c);
} else {
for (uint8_t j = 0; j < writer->utf8_bufsz; ++j) {
json_emitc(writer, writer->utf8_buf[j]);
}
}
writer->utf8_bufsz = 0;
writer->utf8_state = UTF8_ACCEPT;
break;
}
case UTF8_REJECT: {
uint8_t utf8_bufsz;
utf8_bufsz = writer->utf8_bufsz;
writer->utf8_bufsz = 0;
writer->utf8_state = UTF8_ACCEPT;
json_bad_utf8(writer);
if (utf8_bufsz > 1) {
i -= 1; // Reconsider individually
}
break;
}
default: {
break;
}
}
}
}
static void
json_begin_string_no_check(struct json_writer* writer)
{
json_push_context(writer, STRING);
json_emitc(writer, '"');
writer->utf8_state = UTF8_ACCEPT;
writer->utf8_bufsz = 0;
}
void
json_begin_string(struct json_writer* writer)
{
json_value_start(writer);
json_begin_string_no_check(writer);
}
void
json_end_string(struct json_writer* writer)
{
json_check_state(writer, STRING);
if (writer->utf8_state != UTF8_ACCEPT)
json_bad_utf8(writer);
json_pop_context(writer);
json_emitc(writer, '"');
json_value_end(writer);
}
void
json_begin_field(struct json_writer* writer, const char* name)
{
json_check_state(writer, OBJECT_EMPTY | OBJECT_NONEMPTY);
if (json_writer_state(writer) == OBJECT_NONEMPTY)
json_emitc(writer, ',');
else if (json_writer_state(writer) == OBJECT_EMPTY)
writer->context->state = OBJECT_NONEMPTY;
json_begin_string_no_check(writer);
json_emit_string_part(writer, name, strlen(name));
json_end_string(writer);
json_emitc(writer, ':');
json_push_context(writer, VALUE);
}
void
json_emit_string_n(struct json_writer* writer,
const char* string,
size_t n)
{
json_begin_string(writer);
json_emit_string_part(writer, string, n);
json_end_string(writer);
}
void
json_emit_string(struct json_writer* writer, const char* string)
{
json_emit_string_n(writer, string, strlen(string));
}
void
json_emit_i64(struct json_writer* writer, int64_t number)
{
char buf[sizeof ("-18446744073709551616")];
sprintf(buf, "%lld", (long long) number);
json_value_start(writer);
json_emits(writer, buf);
json_value_end(writer);
}
void
json_emit_u64(struct json_writer* writer, uint64_t number)
{
char buf[sizeof ("18446744073709551615")];
sprintf(buf, "%llu", (unsigned long long) number);
json_value_start(writer);
json_emits(writer, buf);
json_value_end(writer);
}
void
json_emit_null(struct json_writer* writer)
{
json_value_start(writer);
json_emits(writer, "null");
json_value_end(writer);
}
void
json_emit_bool(struct json_writer* writer, bool b)
{
json_value_start(writer);
json_emits(writer, b ? "true" : "false");
json_value_end(writer);
}
const struct json_context*
json_save_context(struct json_writer* writer)
{
return writer->context;
}
void
json_pop_to_saved_context(struct json_writer* writer,
const struct json_context* saved_context)
{
while (writer->context && writer->context != saved_context) {
switch (writer->context->state) {
case VALUE:
json_emit_null(writer);
break;
case ARRAY_EMPTY:
case ARRAY_NONEMPTY:
json_end_array(writer);
break;
case OBJECT_EMPTY:
case OBJECT_NONEMPTY:
json_end_object(writer);
break;
case STRING:
json_end_string(writer);
break;
case IOERR:
die(EINVAL, "json object saw IO error");
}
}
if (writer->context == NULL) {
die(EINVAL, "saved state mismatch");
}
}