forked from vivien/i3blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
398 lines (319 loc) · 7.15 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
/*
* json.c - flat JSON parsing
* Copyright (C) 2014 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 <stdlib.h>
#include <string.h>
#include "json.h"
#include "line.h"
#include "log.h"
#include "sys.h"
struct json {
char *name;
size_t name_len;
char *value;
size_t value_len;
json_pair_cb_t *pair_cb;
void *data;
};
static int json_pair(struct json *json)
{
if (!json->pair_cb)
return 0;
*(json->name + json->name_len) = '\0';
*(json->value + json->value_len) = '\0';
return json->pair_cb(json->name, json->value, json->data);
}
/* Return the length of the parsed string, 0 if it is invalid */
static size_t json_parse_string(const char *line)
{
const char *end = line;
int hex;
if (*line != '"')
return 0;
while (*++end != '"') {
/* control character or end-of-line? */
if (iscntrl(*end) || *end == '\0')
return 0;
/* any Unicode character except " or \ or control character? */
if (*end != '\\')
continue;
/* backslash escape */
switch (*++end) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
break;
case 'u':
for (hex = 0; hex < 4; hex++)
if (!isxdigit(*++end))
return 0;
break;
default:
return 0;
}
}
return ++end - line;
}
/* Return the length of the parsed number, 0 if it is invalid */
static size_t json_parse_number(const char *line)
{
char *end;
strtoul(line, &end, 10);
return end - line;
}
/* Return the length of the parsed literal, 0 if it is invalid */
static size_t json_parse_literal(const char *line, const char *literal)
{
const size_t len = strlen(literal);
if (strncmp(line, literal, len) != 0)
return 0;
return len;
}
/* A value can be a string, number, object, array, true, false, or null */
static size_t json_parse_value(struct json *json, char *line)
{
json->value = line;
json->value_len = json_parse_string(line);
if (json->value_len)
return json->value_len;
json->value_len = json_parse_number(line);
if (json->value_len)
return json->value_len;
json->value_len = json_parse_literal(line, "true");
if (json->value_len)
return json->value_len;
json->value_len = json_parse_literal(line, "false");
if (json->value_len)
return json->value_len;
json->value_len = json_parse_literal(line, "null");
if (json->value_len)
return json->value_len;
/* nested arrays or objects are not supported */
json->value = NULL;
return 0;
}
/* Return the length of ':' optionally enclosed by whitespaces, 0 otherwise */
static size_t json_parse_colon(const char *line)
{
size_t len = 0;
while (isspace(*line))
line++, len++;
if (*line != ':')
return 0;
line++;
len++;
while (isspace(*line))
line++, len++;
return len;
}
/* Parse and store (unquoted) the name string */
static size_t json_parse_name(struct json *json, char *line)
{
size_t len;
len = json_parse_string(line);
if (!len)
return 0;
json->name = line + 1;
json->name_len = len - 2;
return len;
}
/* Parse an inline ["name"][\s+:\s+][value] name-value pair */
static size_t json_parse_pair(struct json *json, char *line)
{
size_t pair_len = 0;
size_t len;
len = json_parse_name(json, line);
if (!len)
return 0;
pair_len += len;
line += len;
len = json_parse_colon(line);
if (!len)
return 0;
pair_len += len;
line += len;
len = json_parse_value(json, line);
if (!len)
return 0;
pair_len += len;
return pair_len;
}
static int json_parse_line(char *line, size_t num, void *data)
{
size_t len;
int err;
for (;;) {
/* Only support flat objects per line at the moment */
while (*line == '[' || *line == ']' || *line == ',' ||
*line == '{' || *line == '}' || isspace(*line))
line++;
if (*line == '\0')
break;
len = json_parse_pair(data, line);
if (!len)
return -EINVAL;
line += len;
/* valid delimiters after a pair */
if (*line != ',' && *line != '}' && *line != '\0' &&
!isspace(*line))
return -EINVAL;
if (*line != '\0')
line++;
err = json_pair(data);
if (err)
return err;
}
return 0;
}
int json_read(int fd, size_t count, json_pair_cb_t *pair_cb, void *data)
{
struct json json = {
.pair_cb = pair_cb,
.data = data,
};
return line_read(fd, count, json_parse_line, &json);
}
bool json_is_string(const char *str)
{
size_t len;
len = json_parse_string(str);
if (!len || str[len] != '\0')
return false;
return true;
}
static bool json_is_number(const char *str)
{
char *end;
strtoul(str, &end, 10);
/* not a valid number if end is not a null character */
return !(*str == 0 || *end != 0);
}
static bool json_is_literal(const char *str)
{
return strcmp(str, "true") == 0 || strcmp(str, "false") == 0 ||
strcmp(str, "null") == 0;
}
bool json_is_valid(const char *str)
{
return json_is_string(str) || json_is_number(str) ||
json_is_literal(str);
}
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:
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++;
} while (null--);
return 0;
}
int json_unescape(const char *str, char *buf, size_t size)
{
bool escaped = false;
const char *end;
int len;
char c;
if (json_is_string(str)) {
end = strrchr(str, '"');
if (!end)
return -EINVAL; /* Unlikely */
while (++str < end) {
c = *str;
if (escaped) {
switch (c) {
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;
}
escaped = false;
} else if (*str == '\\') {
escaped = true;
continue;
}
len = snprintf(buf, size, "%c", c);
if (len < 0 || len >= size)
return -ENOSPC;
size -= len;
buf += len;
}
return 0;
}
if (json_is_number(str) || json_is_literal(str)) {
strncpy(buf, str, size);
if (buf[size - 1] != '\0')
return -ENOSPC;
return 0;
}
return -EINVAL;
}