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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*-
* Copyright (c) 2018 Christos Zoulas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Parse JSON object serialization format (RFC-7159)
*/
#ifndef TEST
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: is_json.c,v 1.30 2022/09/27 19:12:40 christos Exp $")
#endif
#include "magic.h"
#else
#include <stdio.h>
#include <stddef.h>
#endif
#include <string.h>
#ifdef DEBUG
#include <stdio.h>
#define DPRINTF(a, b, c) \
printf("%*s%s [%.2x/%c] %.*s\n", (int)lvl, "", (a), *(b), *(b), \
(int)(b - c), (const char *)(c))
#define __file_debugused
#else
#define DPRINTF(a, b, c) do { } while (/*CONSTCOND*/0)
#define __file_debugused __attribute__((__unused__))
#endif
#define JSON_ARRAY 0
#define JSON_CONSTANT 1
#define JSON_NUMBER 2
#define JSON_OBJECT 3
#define JSON_STRING 4
#define JSON_ARRAYN 5
#define JSON_MAX 6
/*
* if JSON_COUNT != 0:
* count all the objects, require that we have the whole data file
* otherwise:
* stop if we find an object or an array
*/
#ifndef JSON_COUNT
#define JSON_COUNT 0
#endif
static int json_parse(const unsigned char **, const unsigned char *, size_t *,
size_t);
static int
json_isspace(const unsigned char uc)
{
switch (uc) {
case ' ':
case '\n':
case '\r':
case '\t':
return 1;
default:
return 0;
}
}
static int
json_isdigit(unsigned char uc)
{
switch (uc) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return 1;
default:
return 0;
}
}
static int
json_isxdigit(unsigned char uc)
{
if (json_isdigit(uc))
return 1;
switch (uc) {
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
return 1;
default:
return 0;
}
}
static const unsigned char *
json_skip_space(const unsigned char *uc, const unsigned char *ue)
{
while (uc < ue && json_isspace(*uc))
uc++;
return uc;
}
/*ARGSUSED*/
static int
json_parse_string(const unsigned char **ucp, const unsigned char *ue,
size_t lvl __file_debugused)
{
const unsigned char *uc = *ucp;
size_t i;
DPRINTF("Parse string: ", uc, *ucp);
while (uc < ue) {
switch (*uc++) {
case '\0':
goto out;
case '\\':
if (uc == ue)
goto out;
switch (*uc++) {
case '\0':
goto out;
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
continue;
case 'u':
if (ue - uc < 4) {
uc = ue;
goto out;
}
for (i = 0; i < 4; i++)
if (!json_isxdigit(*uc++))
goto out;
continue;
default:
goto out;
}
case '"':
DPRINTF("Good string: ", uc, *ucp);
*ucp = uc;
return 1;
default:
continue;
}
}
out:
DPRINTF("Bad string: ", uc, *ucp);
*ucp = uc;
return 0;
}
static int
json_parse_array(const unsigned char **ucp, const unsigned char *ue,
size_t *st, size_t lvl)
{
const unsigned char *uc = *ucp;
DPRINTF("Parse array: ", uc, *ucp);
while (uc < ue) {
uc = json_skip_space(uc, ue);
if (uc == ue)
goto out;
if (*uc == ']')
goto done;
if (!json_parse(&uc, ue, st, lvl + 1))
goto out;
if (uc == ue)
goto out;
switch (*uc) {
case ',':
uc++;
continue;
case ']':
done:
st[JSON_ARRAYN]++;
DPRINTF("Good array: ", uc, *ucp);
*ucp = uc + 1;
return 1;
default:
goto out;
}
}
out:
DPRINTF("Bad array: ", uc, *ucp);
*ucp = uc;
return 0;
}
static int
json_parse_object(const unsigned char **ucp, const unsigned char *ue,
size_t *st, size_t lvl)
{
const unsigned char *uc = *ucp;
DPRINTF("Parse object: ", uc, *ucp);
while (uc < ue) {
uc = json_skip_space(uc, ue);
if (uc == ue)
goto out;
if (*uc == '}') {
uc++;
goto done;
}
if (*uc++ != '"') {
DPRINTF("not string", uc, *ucp);
goto out;
}
DPRINTF("next field", uc, *ucp);
if (!json_parse_string(&uc, ue, lvl)) {
DPRINTF("not string", uc, *ucp);
goto out;
}
uc = json_skip_space(uc, ue);
if (uc == ue)
goto out;
if (*uc++ != ':') {
DPRINTF("not colon", uc, *ucp);
goto out;
}
if (!json_parse(&uc, ue, st, lvl + 1)) {
DPRINTF("not json", uc, *ucp);
goto out;
}
if (uc == ue)
goto out;
switch (*uc++) {
case ',':
continue;
case '}': /* { */
done:
DPRINTF("Good object: ", uc, *ucp);
*ucp = uc;
return 1;
default:
DPRINTF("not more", uc, *ucp);
*ucp = uc - 1;
goto out;
}
}
out:
DPRINTF("Bad object: ", uc, *ucp);
*ucp = uc;
return 0;
}
/*ARGSUSED*/
static int
json_parse_number(const unsigned char **ucp, const unsigned char *ue,
size_t lvl __file_debugused)
{
const unsigned char *uc = *ucp;
int got = 0;
DPRINTF("Parse number: ", uc, *ucp);
if (uc == ue)
return 0;
if (*uc == '-')
uc++;
for (; uc < ue; uc++) {
if (!json_isdigit(*uc))
break;
got = 1;
}
if (uc == ue)
goto out;
if (*uc == '.')
uc++;
for (; uc < ue; uc++) {
if (!json_isdigit(*uc))
break;
got = 1;
}
if (uc == ue)
goto out;
if (got && (*uc == 'e' || *uc == 'E')) {
uc++;
got = 0;
if (uc == ue)
goto out;
if (*uc == '+' || *uc == '-')
uc++;
for (; uc < ue; uc++) {
if (!json_isdigit(*uc))
break;
got = 1;
}
}
out:
if (!got)
DPRINTF("Bad number: ", uc, *ucp);
else
DPRINTF("Good number: ", uc, *ucp);
*ucp = uc;
return got;
}
/*ARGSUSED*/
static int
json_parse_const(const unsigned char **ucp, const unsigned char *ue,
const char *str, size_t len, size_t lvl __file_debugused)
{
const unsigned char *uc = *ucp;
DPRINTF("Parse const: ", uc, *ucp);
*ucp += --len - 1;
if (*ucp > ue)
*ucp = ue;
for (; uc < ue && --len;) {
if (*uc++ != *++str) {
DPRINTF("Bad const: ", uc, *ucp);
return 0;
}
}
DPRINTF("Good const: ", uc, *ucp);
return 1;
}
static int
json_parse(const unsigned char **ucp, const unsigned char *ue,
size_t *st, size_t lvl)
{
const unsigned char *uc, *ouc;
int rv = 0;
int t;
ouc = uc = json_skip_space(*ucp, ue);
if (uc == ue)
goto out;
// Avoid recursion
if (lvl > 500) {
DPRINTF("Too many levels", uc, *ucp);
return 0;
}
#if JSON_COUNT
/* bail quickly if not counting */
if (lvl > 1 && (st[JSON_OBJECT] || st[JSON_ARRAYN]))
return 1;
#endif
DPRINTF("Parse general: ", uc, *ucp);
switch (*uc++) {
case '"':
rv = json_parse_string(&uc, ue, lvl + 1);
t = JSON_STRING;
break;
case '[':
rv = json_parse_array(&uc, ue, st, lvl + 1);
t = JSON_ARRAY;
break;
case '{': /* '}' */
rv = json_parse_object(&uc, ue, st, lvl + 1);
t = JSON_OBJECT;
break;
case 't':
rv = json_parse_const(&uc, ue, "true", sizeof("true"), lvl + 1);
t = JSON_CONSTANT;
break;
case 'f':
rv = json_parse_const(&uc, ue, "false", sizeof("false"),
lvl + 1);
t = JSON_CONSTANT;
break;
case 'n':
rv = json_parse_const(&uc, ue, "null", sizeof("null"), lvl + 1);
t = JSON_CONSTANT;
break;
default:
--uc;
rv = json_parse_number(&uc, ue, lvl + 1);
t = JSON_NUMBER;
break;
}
if (rv)
st[t]++;
uc = json_skip_space(uc, ue);
out:
DPRINTF("End general: ", uc, *ucp);
*ucp = uc;
if (lvl == 0) {
if (!rv)
return 0;
if (uc == ue)
return (st[JSON_ARRAYN] || st[JSON_OBJECT]) ? 1 : 0;
if (*ouc == *uc && json_parse(&uc, ue, st, 1))
return (st[JSON_ARRAYN] || st[JSON_OBJECT]) ? 2 : 0;
else
return 0;
}
return rv;
}
#ifndef TEST
int
file_is_json(struct magic_set *ms, const struct buffer *b)
{
const unsigned char *uc = CAST(const unsigned char *, b->fbuf);
const unsigned char *ue = uc + b->flen;
size_t st[JSON_MAX];
int mime = ms->flags & MAGIC_MIME;
int jt;
if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
return 0;
memset(st, 0, sizeof(st));
if ((jt = json_parse(&uc, ue, st, 0)) == 0)
return 0;
if (mime == MAGIC_MIME_ENCODING)
return 1;
if (mime) {
if (file_printf(ms, "application/%s",
jt == 1 ? "json" : "x-ndjson") == -1)
return -1;
return 1;
}
if (file_printf(ms, "%sJSON text data",
jt == 1 ? "" : "New Line Delimited ") == -1)
return -1;
#if JSON_COUNT
#define P(n) st[n], st[n] > 1 ? "s" : ""
if (file_printf(ms, " (%" SIZE_T_FORMAT "u object%s, %" SIZE_T_FORMAT
"u array%s, %" SIZE_T_FORMAT "u string%s, %" SIZE_T_FORMAT
"u constant%s, %" SIZE_T_FORMAT "u number%s, %" SIZE_T_FORMAT
"u >1array%s)",
P(JSON_OBJECT), P(JSON_ARRAY), P(JSON_STRING), P(JSON_CONSTANT),
P(JSON_NUMBER), P(JSON_ARRAYN))
== -1)
return -1;
#endif
return 1;
}
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <err.h>
int
main(int argc, char *argv[])
{
int fd;
struct stat st;
unsigned char *p;
size_t stats[JSON_MAX];
if ((fd = open(argv[1], O_RDONLY)) == -1)
err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
if (fstat(fd, &st) == -1)
err(EXIT_FAILURE, "Can't stat `%s'", argv[1]);
if ((p = CAST(char *, malloc(st.st_size))) == NULL)
err(EXIT_FAILURE, "Can't allocate %jd bytes",
(intmax_t)st.st_size);
if (read(fd, p, st.st_size) != st.st_size)
err(EXIT_FAILURE, "Can't read %jd bytes",
(intmax_t)st.st_size);
memset(stats, 0, sizeof(stats));
printf("is json %d\n", json_parse((const unsigned char **)&p,
p + st.st_size, stats, 0));
return 0;
}
#endif
|