Coverage Report

Created: 2026-01-17 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/lib/ccan-json.c
Line
Count
Source
1
/*
2
  Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
3
  All rights reserved.
4
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
  of this software and associated documentation files (the "Software"), to deal
7
  in the Software without restriction, including without limitation the rights
8
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
  copies of the Software, and to permit persons to whom the Software is
10
  furnished to do so, subject to the following conditions:
11
12
  The above copyright notice and this permission notice shall be included in
13
  all copies or substantial portions of the Software.
14
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
  THE SOFTWARE.
22
*/
23
24
#include "ccan-json.h"
25
26
#include <assert.h>
27
#include <stdint.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
32
/* ProFTPD: stdbool.h equivalents */
33
#ifndef TRUE
34
212k
# define TRUE 1
35
#endif
36
37
#ifndef FALSE
38
20.7k
# define FALSE  0
39
#endif
40
41
/* Out-of-memory error handling. */
42
static void default_oom(void)
43
0
{
44
0
  fprintf(stderr, "%s", "Out of memory!\n");
45
0
  exit(EXIT_FAILURE);
46
0
}
47
48
static void (*json_oom)(void) = default_oom;
49
50
/* Sadly, strdup is not portable. */
51
static char *json_strdup(const char *str)
52
0
{
53
0
  char *ret = (char*) malloc(strlen(str) + 1);
54
0
  if (ret == NULL) {
55
0
    json_oom();
56
0
    return NULL;
57
0
  }
58
0
  strcpy(ret, str);
59
0
  return ret;
60
0
}
61
62
/* String buffer */
63
64
typedef struct
65
{
66
  char *cur;
67
  char *end;
68
  char *start;
69
} SB;
70
71
static void sb_init(SB *sb)
72
29.1k
{
73
29.1k
  sb->start = (char*) malloc(17);
74
29.1k
  if (sb->start == NULL)
75
0
    json_oom();
76
29.1k
  sb->cur = sb->start;
77
29.1k
  sb->end = sb->start + 16;
78
29.1k
}
79
80
/* sb and need may be evaluated multiple times. */
81
1.20M
#define sb_need(sb, need) do {                  \
82
1.20M
    if ((sb)->end - (sb)->cur < (need))     \
83
1.20M
      sb_grow(sb, need);                  \
84
1.20M
  } while (0)
85
86
static void sb_grow(SB *sb, int need)
87
522
{
88
522
  size_t length = sb->cur - sb->start;
89
522
  size_t alloc = sb->end - sb->start;
90
  
91
522
  do {
92
522
    alloc *= 2;
93
522
  } while (alloc < length + need);
94
  
95
522
  sb->start = (char*) realloc(sb->start, alloc + 1);
96
522
  if (sb->start == NULL)
97
0
    json_oom();
98
522
  sb->cur = sb->start + length;
99
522
  sb->end = sb->start + alloc;
100
522
}
101
102
static void sb_put(SB *sb, const char *bytes, int count)
103
0
{
104
0
  sb_need(sb, count);
105
0
  memcpy(sb->cur, bytes, count);
106
0
  sb->cur += count;
107
0
}
108
109
0
#define sb_putc(sb, c) do {         \
110
0
    if ((sb)->cur >= (sb)->end) \
111
0
      sb_grow(sb, 1);         \
112
0
    *(sb)->cur++ = (c);         \
113
0
  } while (0)
114
115
static void sb_puts(SB *sb, const char *str)
116
0
{
117
0
  sb_put(sb, str, strlen(str));
118
0
}
119
120
static char *sb_finish(SB *sb)
121
29.1k
{
122
29.1k
  *sb->cur = 0;
123
29.1k
  assert(sb->start <= sb->cur && strlen(sb->start) == (size_t)(sb->cur - sb->start));
124
29.1k
  return sb->start;
125
29.1k
}
126
127
static void sb_free(SB *sb)
128
0
{
129
0
  free(sb->start);
130
0
}
131
132
/*
133
 * Unicode helper functions
134
 *
135
 * These are taken from the ccan/charset module and customized a bit.
136
 * Putting them here means the compiler can (choose to) inline them,
137
 * and it keeps ccan/json from having a dependency.
138
 */
139
140
#if 0
141
/* PROFTPD NOTE: This has been commented out, since it will cause
142
 * problems on platforms where `uchar_t' is already defined.  Instead,
143
 * all ocurrences of `uchar_t' in the original code have been replaced
144
 * with `uint32_t'.
145
 */
146
147
/*
148
 * Type for Unicode codepoints.
149
 * We need our own because wchar_t might be 16 bits.
150
 */
151
typedef uint32_t uchar_t;
152
#endif
153
154
/*
155
 * Validate a single UTF-8 character starting at @s.
156
 * The string must be null-terminated.
157
 *
158
 * If it's valid, return its length (1 thru 4).
159
 * If it's invalid or clipped, return 0.
160
 *
161
 * This function implements the syntax given in RFC3629, which is
162
 * the same as that given in The Unicode Standard, Version 6.0.
163
 *
164
 * It has the following properties:
165
 *
166
 *  * All codepoints U+0000..U+10FFFF may be encoded,
167
 *    except for U+D800..U+DFFF, which are reserved
168
 *    for UTF-16 surrogate pair encoding.
169
 *  * UTF-8 byte sequences longer than 4 bytes are not permitted,
170
 *    as they exceed the range of Unicode.
171
 *  * The sixty-six Unicode "non-characters" are permitted
172
 *    (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF).
173
 */
174
static int utf8_validate_cz(const char *s)
175
4.38M
{
176
4.38M
  unsigned char c = *s++;
177
  
178
4.38M
  if (c <= 0x7F) {        /* 00..7F */
179
4.33M
    return 1;
180
4.33M
  } else if (c <= 0xC1) { /* 80..C1 */
181
    /* Disallow overlong 2-byte sequence. */
182
1.76k
    return 0;
183
47.0k
  } else if (c <= 0xDF) { /* C2..DF */
184
    /* Make sure subsequent byte is in the range 0x80..0xBF. */
185
28.1k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
186
13
      return 0;
187
    
188
28.1k
    return 2;
189
28.1k
  } else if (c <= 0xEF) { /* E0..EF */
190
    /* Disallow overlong 3-byte sequence. */
191
2.71k
    if (c == 0xE0 && (unsigned char)*s < 0xA0)
192
13
      return 0;
193
    
194
    /* Disallow U+D800..U+DFFF. */
195
2.70k
    if (c == 0xED && (unsigned char)*s > 0x9F)
196
2
      return 0;
197
    
198
    /* Make sure subsequent bytes are in the range 0x80..0xBF. */
199
2.70k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
200
20
      return 0;
201
2.68k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
202
15
      return 0;
203
    
204
2.66k
    return 3;
205
16.1k
  } else if (c <= 0xF4) { /* F0..F4 */
206
    /* Disallow overlong 4-byte sequence. */
207
16.1k
    if (c == 0xF0 && (unsigned char)*s < 0x90)
208
10
      return 0;
209
    
210
    /* Disallow codepoints beyond U+10FFFF. */
211
16.1k
    if (c == 0xF4 && (unsigned char)*s > 0x8F)
212
2
      return 0;
213
    
214
    /* Make sure subsequent bytes are in the range 0x80..0xBF. */
215
16.1k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
216
15
      return 0;
217
16.1k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
218
19
      return 0;
219
16.1k
    if (((unsigned char)*s++ & 0xC0) != 0x80)
220
10
      return 0;
221
    
222
16.0k
    return 4;
223
16.1k
  } else {                /* F5..FF */
224
2
    return 0;
225
2
  }
226
4.38M
}
227
228
/* Validate a null-terminated UTF-8 string. */
229
static int utf8_validate(const char *s)
230
0
{
231
0
  int len;
232
  
233
0
  for (; *s != 0; s += len) {
234
0
    len = utf8_validate_cz(s);
235
0
    if (len == 0)
236
0
      return FALSE;
237
0
  }
238
  
239
0
  return TRUE;
240
0
}
241
242
/*
243
 * Read a single UTF-8 character starting at @s,
244
 * returning the length, in bytes, of the character read.
245
 *
246
 * This function assumes input is valid UTF-8,
247
 * and that there are enough characters in front of @s.
248
 */
249
static int utf8_read_char(const char *s, uint32_t *out)
250
0
{
251
0
  const unsigned char *c = (const unsigned char*) s;
252
  
253
0
  assert(utf8_validate_cz(s));
254
255
0
  if (c[0] <= 0x7F) {
256
    /* 00..7F */
257
0
    *out = c[0];
258
0
    return 1;
259
0
  } else if (c[0] <= 0xDF) {
260
    /* C2..DF (unless input is invalid) */
261
0
    *out = ((uint32_t)c[0] & 0x1F) << 6 |
262
0
           ((uint32_t)c[1] & 0x3F);
263
0
    return 2;
264
0
  } else if (c[0] <= 0xEF) {
265
    /* E0..EF */
266
0
    *out = ((uint32_t)c[0] &  0xF) << 12 |
267
0
           ((uint32_t)c[1] & 0x3F) << 6  |
268
0
           ((uint32_t)c[2] & 0x3F);
269
0
    return 3;
270
0
  } else {
271
    /* F0..F4 (unless input is invalid) */
272
0
    *out = ((uint32_t)c[0] &  0x7) << 18 |
273
0
           ((uint32_t)c[1] & 0x3F) << 12 |
274
0
           ((uint32_t)c[2] & 0x3F) << 6  |
275
0
           ((uint32_t)c[3] & 0x3F);
276
0
    return 4;
277
0
  }
278
0
}
279
280
/*
281
 * Write a single UTF-8 character to @s,
282
 * returning the length, in bytes, of the character written.
283
 *
284
 * @unicode must be U+0000..U+10FFFF, but not U+D800..U+DFFF.
285
 *
286
 * This function will write up to 4 bytes to @out.
287
 */
288
static int utf8_write_char(uint32_t unicode, char *out)
289
4.15k
{
290
4.15k
  unsigned char *o = (unsigned char*) out;
291
  
292
4.15k
  assert(unicode <= 0x10FFFF && !(unicode >= 0xD800 && unicode <= 0xDFFF));
293
294
4.15k
  if (unicode <= 0x7F) {
295
    /* U+0000..U+007F */
296
2.73k
    *o++ = unicode;
297
2.73k
    return 1;
298
2.73k
  } else if (unicode <= 0x7FF) {
299
    /* U+0080..U+07FF */
300
207
    *o++ = 0xC0 | unicode >> 6;
301
207
    *o++ = 0x80 | (unicode & 0x3F);
302
207
    return 2;
303
1.20k
  } else if (unicode <= 0xFFFF) {
304
    /* U+0800..U+FFFF */
305
714
    *o++ = 0xE0 | unicode >> 12;
306
714
    *o++ = 0x80 | (unicode >> 6 & 0x3F);
307
714
    *o++ = 0x80 | (unicode & 0x3F);
308
714
    return 3;
309
714
  } else {
310
    /* U+10000..U+10FFFF */
311
492
    *o++ = 0xF0 | unicode >> 18;
312
492
    *o++ = 0x80 | (unicode >> 12 & 0x3F);
313
492
    *o++ = 0x80 | (unicode >> 6 & 0x3F);
314
492
    *o++ = 0x80 | (unicode & 0x3F);
315
492
    return 4;
316
492
  }
317
4.15k
}
318
319
/*
320
 * Compute the Unicode codepoint of a UTF-16 surrogate pair.
321
 *
322
 * @uc should be 0xD800..0xDBFF, and @lc should be 0xDC00..0xDFFF.
323
 * If they aren't, this function returns false.
324
 */
325
static int from_surrogate_pair(uint16_t uc, uint16_t lc, uint32_t *unicode)
326
521
{
327
521
  if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF) {
328
492
    *unicode = 0x10000 + ((((uint32_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
329
492
    return TRUE;
330
492
  } else {
331
29
    return FALSE;
332
29
  }
333
521
}
334
335
/*
336
 * Construct a UTF-16 surrogate pair given a Unicode codepoint.
337
 *
338
 * @unicode must be U+10000..U+10FFFF.
339
 */
340
static void to_surrogate_pair(uint32_t unicode, uint16_t *uc, uint16_t *lc)
341
0
{
342
0
  uint32_t n;
343
  
344
0
  assert(unicode >= 0x10000 && unicode <= 0x10FFFF);
345
  
346
0
  n = unicode - 0x10000;
347
0
  *uc = ((n >> 10) & 0x3FF) | 0xD800;
348
0
  *lc = (n & 0x3FF) | 0xDC00;
349
0
}
350
351
322k
#define is_space(c) ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
352
20.7k
#define is_digit(c) ((c) >= '0' && (c) <= '9')
353
354
static int parse_value      (const char **sp, JsonNode        **out, unsigned int depth);
355
static int parse_string     (const char **sp, char            **out);
356
static int parse_number     (const char **sp, double           *out);
357
static int parse_array      (const char **sp, JsonNode        **out, unsigned int depth);
358
static int parse_object     (const char **sp, JsonNode        **out, unsigned int depth);
359
static int parse_hex16      (const char **sp, uint16_t         *out);
360
361
static int expect_literal   (const char **sp, const char *str);
362
static void skip_space      (const char **sp);
363
364
static void emit_value              (SB *out, const JsonNode *node);
365
static void emit_value_indented     (SB *out, const JsonNode *node, const char *space, int indent_level);
366
static void emit_string             (SB *out, const char *str);
367
static void emit_number             (SB *out, double num);
368
static void emit_array              (SB *out, const JsonNode *array);
369
static void emit_array_indented     (SB *out, const JsonNode *array, const char *space, int indent_level);
370
static void emit_object             (SB *out, const JsonNode *object);
371
static void emit_object_indented    (SB *out, const JsonNode *object, const char *space, int indent_level);
372
373
static int write_hex16(char *out, uint16_t val);
374
375
static JsonNode *mknode(JsonTag tag);
376
static void append_node(JsonNode *parent, JsonNode *child);
377
static void prepend_node(JsonNode *parent, JsonNode *child);
378
static void append_member(JsonNode *object, char *key, JsonNode *value);
379
380
/* Assertion-friendly validity checks */
381
static int tag_is_valid(unsigned int tag);
382
static int number_is_valid(const char *num);
383
384
3.50k
void json_set_oom(void (*oom)(void)) {
385
3.50k
  json_oom = (oom != NULL ? oom : default_oom);
386
3.50k
}
387
388
JsonNode *json_decode(const char *json)
389
4.17k
{
390
4.17k
  const char *s = json;
391
4.17k
  JsonNode *ret;
392
  
393
4.17k
  skip_space(&s);
394
4.17k
  if (!parse_value(&s, &ret, 0))
395
0
    return NULL;
396
  
397
4.17k
  skip_space(&s);
398
4.17k
  if (*s != 0) {
399
0
    json_delete(ret);
400
0
    return NULL;
401
0
  }
402
  
403
4.17k
  return ret;
404
4.17k
}
405
406
char *json_encode(const JsonNode *node)
407
0
{
408
0
  return json_stringify(node, NULL);
409
0
}
410
411
char *json_encode_string(const char *str)
412
0
{
413
0
  char *encoded_str;
414
0
  SB sb;
415
0
  sb_init(&sb);
416
  
417
0
  emit_string(&sb, str);
418
  
419
0
  encoded_str = json_strdup(sb_finish(&sb));
420
0
  sb_free(&sb);
421
0
  return encoded_str;
422
0
}
423
424
char *json_stringify(const JsonNode *node, const char *space)
425
0
{
426
0
  char *str;
427
0
  SB sb;
428
0
  sb_init(&sb);
429
  
430
0
  if (space != NULL)
431
0
    emit_value_indented(&sb, node, space, 0);
432
0
  else
433
0
    emit_value(&sb, node);
434
  
435
0
  str = json_strdup(sb_finish(&sb));
436
0
  sb_free(&sb);
437
0
  return str;
438
0
}
439
440
void json_delete(JsonNode *node)
441
37.7k
{
442
37.7k
  if (node != NULL) {
443
33.2k
    json_remove_from_parent(node);
444
    
445
33.2k
    switch (node->tag) {
446
11.5k
      case JSON_STRING:
447
11.5k
        free(node->string_);
448
11.5k
        break;
449
2.07k
      case JSON_ARRAY:
450
13.5k
      case JSON_OBJECT:
451
13.5k
      {
452
13.5k
        JsonNode *child, *next;
453
42.5k
        for (child = node->children.head; child != NULL; child = next) {
454
29.0k
          next = child->next;
455
29.0k
          json_delete(child);
456
29.0k
        }
457
13.5k
        break;
458
2.07k
      }
459
8.20k
      default:;
460
33.2k
    }
461
    
462
33.2k
    free(node);
463
33.2k
  }
464
37.7k
}
465
466
int json_validate(const char *json)
467
8.77k
{
468
8.77k
  const char *s = json;
469
  
470
8.77k
  skip_space(&s);
471
8.77k
  if (!parse_value(&s, NULL, 0))
472
4.51k
    return FALSE;
473
  
474
4.25k
  skip_space(&s);
475
4.25k
  if (*s != 0)
476
78
    return FALSE;
477
  
478
4.17k
  return TRUE;
479
4.25k
}
480
481
JsonNode *json_find_element(JsonNode *array, unsigned int idx)
482
0
{
483
0
  JsonNode *element;
484
0
  unsigned int i = 0;
485
  
486
0
  if (array == NULL || array->tag != JSON_ARRAY)
487
0
    return NULL;
488
  
489
0
  json_foreach(element, array) {
490
0
    if (i == idx)
491
0
      return element;
492
0
    i++;
493
0
  }
494
  
495
0
  return NULL;
496
0
}
497
498
JsonNode *json_find_member(JsonNode *object, const char *name)
499
0
{
500
0
  JsonNode *member;
501
  
502
0
  if (object == NULL || object->tag != JSON_OBJECT)
503
0
    return NULL;
504
  
505
0
  json_foreach(member, object)
506
0
    if (strcmp(member->key, name) == 0)
507
0
      return member;
508
  
509
0
  return NULL;
510
0
}
511
512
JsonNode *json_first_child(const JsonNode *node)
513
3.63k
{
514
3.63k
  if (node != NULL && (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT))
515
3.63k
    return node->children.head;
516
0
  return NULL;
517
3.63k
}
518
519
static JsonNode *mknode(JsonTag tag)
520
33.2k
{
521
33.2k
  JsonNode *ret = (JsonNode*) calloc(1, sizeof(JsonNode));
522
33.2k
  if (ret == NULL) {
523
0
    json_oom();
524
0
    return NULL;
525
0
  }
526
33.2k
  ret->tag = tag;
527
33.2k
  return ret;
528
33.2k
}
529
530
JsonNode *json_mknull(void)
531
258
{
532
258
  return mknode(JSON_NULL);
533
258
}
534
535
JsonNode *json_mkbool(int b)
536
529
{
537
529
  JsonNode *ret = mknode(JSON_BOOL);
538
529
  ret->bool_ = b;
539
529
  return ret;
540
529
}
541
542
static JsonNode *mkstring(char *s)
543
11.5k
{
544
11.5k
  JsonNode *ret = mknode(JSON_STRING);
545
11.5k
  ret->string_ = s;
546
11.5k
  return ret;
547
11.5k
}
548
549
JsonNode *json_mkstring(const char *s)
550
0
{
551
0
  return mkstring(json_strdup(s));
552
0
}
553
554
JsonNode *json_mknumber(double n)
555
7.41k
{
556
7.41k
  JsonNode *node = mknode(JSON_NUMBER);
557
7.41k
  node->number_ = n;
558
7.41k
  return node;
559
7.41k
}
560
561
JsonNode *json_mkarray(void)
562
2.07k
{
563
2.07k
  return mknode(JSON_ARRAY);
564
2.07k
}
565
566
JsonNode *json_mkobject(void)
567
11.4k
{
568
11.4k
  return mknode(JSON_OBJECT);
569
11.4k
}
570
571
static void append_node(JsonNode *parent, JsonNode *child)
572
29.0k
{
573
29.0k
  child->parent = parent;
574
29.0k
  child->prev = parent->children.tail;
575
29.0k
  child->next = NULL;
576
  
577
29.0k
  if (parent->children.tail != NULL)
578
17.1k
    parent->children.tail->next = child;
579
11.8k
  else
580
11.8k
    parent->children.head = child;
581
29.0k
  parent->children.tail = child;
582
29.0k
}
583
584
static void prepend_node(JsonNode *parent, JsonNode *child)
585
0
{
586
0
  child->parent = parent;
587
0
  child->prev = NULL;
588
0
  child->next = parent->children.head;
589
  
590
0
  if (parent->children.head != NULL)
591
0
    parent->children.head->prev = child;
592
0
  else
593
0
    parent->children.tail = child;
594
0
  parent->children.head = child;
595
0
}
596
597
static void append_member(JsonNode *object, char *key, JsonNode *value)
598
17.6k
{
599
17.6k
  value->key = key;
600
17.6k
  append_node(object, value);
601
17.6k
}
602
603
void json_append_element(JsonNode *array, JsonNode *element)
604
11.4k
{
605
11.4k
  assert(array->tag == JSON_ARRAY);
606
11.4k
  assert(element->parent == NULL);
607
  
608
11.4k
  append_node(array, element);
609
11.4k
}
610
611
void json_prepend_element(JsonNode *array, JsonNode *element)
612
0
{
613
0
  assert(array->tag == JSON_ARRAY);
614
0
  assert(element->parent == NULL);
615
  
616
0
  prepend_node(array, element);
617
0
}
618
619
void json_append_member(JsonNode *object, const char *key, JsonNode *value)
620
0
{
621
0
  assert(object->tag == JSON_OBJECT);
622
0
  assert(value->parent == NULL);
623
  
624
0
  append_member(object, json_strdup(key), value);
625
0
}
626
627
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
628
0
{
629
0
  assert(object->tag == JSON_OBJECT);
630
0
  assert(value->parent == NULL);
631
  
632
0
  value->key = json_strdup(key);
633
0
  prepend_node(object, value);
634
0
}
635
636
void json_remove_from_parent(JsonNode *node)
637
33.2k
{
638
33.2k
  JsonNode *parent = node->parent;
639
  
640
33.2k
  if (parent != NULL) {
641
29.0k
    if (node->prev != NULL)
642
0
      node->prev->next = node->next;
643
29.0k
    else
644
29.0k
      parent->children.head = node->next;
645
29.0k
    if (node->next != NULL)
646
17.1k
      node->next->prev = node->prev;
647
11.8k
    else
648
11.8k
      parent->children.tail = node->prev;
649
    
650
29.0k
    free(node->key);
651
    
652
29.0k
    node->parent = NULL;
653
29.0k
    node->prev = node->next = NULL;
654
29.0k
    node->key = NULL;
655
29.0k
  }
656
33.2k
}
657
658
static int parse_value(const char **sp, JsonNode **out, unsigned int depth)
659
87.3k
{
660
87.3k
  const char *s = *sp;
661
662
87.3k
  if (depth > CCAN_JSON_MAX_DEPTH) {
663
13
    return FALSE;
664
13
  }
665
666
87.3k
  switch (*s) {
667
713
    case 'n':
668
713
      if (expect_literal(&s, "null")) {
669
710
        if (out)
670
258
          *out = json_mknull();
671
710
        *sp = s;
672
710
        return TRUE;
673
710
      }
674
3
      return FALSE;
675
    
676
740
    case 'f':
677
740
      if (expect_literal(&s, "false")) {
678
736
        if (out)
679
271
          *out = json_mkbool(FALSE);
680
736
        *sp = s;
681
736
        return TRUE;
682
736
      }
683
4
      return FALSE;
684
    
685
713
    case 't':
686
713
      if (expect_literal(&s, "true")) {
687
710
        if (out)
688
258
          *out = json_mkbool(TRUE);
689
710
        *sp = s;
690
710
        return TRUE;
691
710
      }
692
3
      return FALSE;
693
    
694
31.7k
    case '"': {
695
31.7k
      char *str;
696
31.7k
      if (parse_string(&s, out ? &str : NULL)) {
697
29.4k
        if (out)
698
11.5k
          *out = mkstring(str);
699
29.4k
        *sp = s;
700
29.4k
        return TRUE;
701
29.4k
      }
702
2.21k
      return FALSE;
703
31.7k
    }
704
    
705
4.84k
    case '[':
706
4.84k
      if (parse_array(&s, out, depth)) {
707
4.35k
        *sp = s;
708
4.35k
        return TRUE;
709
4.35k
      }
710
483
      return FALSE;
711
    
712
30.2k
    case '{':
713
30.2k
      if (parse_object(&s, out, depth)) {
714
26.2k
        *sp = s;
715
26.2k
        return TRUE;
716
26.2k
      }
717
3.99k
      return FALSE;
718
    
719
18.3k
    default: {
720
18.3k
      double num;
721
18.3k
      if (parse_number(&s, out ? &num : NULL)) {
722
18.1k
        if (out)
723
7.41k
          *out = json_mknumber(num);
724
18.1k
        *sp = s;
725
18.1k
        return TRUE;
726
18.1k
      }
727
254
      return FALSE;
728
18.3k
    }
729
87.3k
  }
730
87.3k
}
731
732
static int parse_array(const char **sp, JsonNode **out, unsigned int depth)
733
4.84k
{
734
4.84k
  const char *s = *sp;
735
4.84k
  JsonNode *ret = out ? json_mkarray() : NULL;
736
4.84k
  JsonNode *element;
737
  
738
4.84k
  depth++;
739
4.84k
  if (*s++ != '[')
740
0
    goto failure;
741
4.84k
  skip_space(&s);
742
  
743
4.84k
  if (*s == ']') {
744
617
    s++;
745
617
    goto success;
746
617
  }
747
  
748
32.0k
  for (;;) {
749
32.0k
    if (!parse_value(&s, out ? &element : NULL, depth))
750
388
      goto failure;
751
31.6k
    skip_space(&s);
752
    
753
31.6k
    if (out)
754
11.4k
      json_append_element(ret, element);
755
    
756
31.6k
    if (*s == ']') {
757
3.74k
      s++;
758
3.74k
      goto success;
759
3.74k
    }
760
    
761
27.8k
    if (*s++ != ',')
762
95
      goto failure;
763
27.8k
    skip_space(&s);
764
27.8k
  }
765
  
766
4.35k
success:
767
4.35k
  depth--;
768
4.35k
  *sp = s;
769
4.35k
  if (out)
770
2.07k
    *out = ret;
771
4.35k
  return TRUE;
772
773
483
failure:
774
483
  depth--;
775
483
  json_delete(ret);
776
483
  return FALSE;
777
4.22k
}
778
779
static int parse_object(const char **sp, JsonNode **out, unsigned int depth)
780
30.2k
{
781
30.2k
  const char *s = *sp;
782
30.2k
  JsonNode *ret = out ? json_mkobject() : NULL;
783
30.2k
  char *key;
784
30.2k
  JsonNode *value;
785
  
786
30.2k
  depth++;
787
30.2k
  if (*s++ != '{')
788
0
    goto failure;
789
30.2k
  skip_space(&s);
790
  
791
30.2k
  if (*s == '}') {
792
5.05k
    s++;
793
5.05k
    goto success;
794
5.05k
  }
795
  
796
44.2k
  for (;;) {
797
44.2k
    if (!parse_string(&s, out ? &key : NULL))
798
1.85k
      goto failure;
799
42.4k
    skip_space(&s);
800
    
801
42.4k
    if (*s++ != ':')
802
49
      goto failure_free_key;
803
42.3k
    skip_space(&s);
804
    
805
42.3k
    if (!parse_value(&s, out ? &value : NULL, depth))
806
2.06k
      goto failure_free_key;
807
40.3k
    skip_space(&s);
808
    
809
40.3k
    if (out)
810
17.6k
      append_member(ret, key, value);
811
    
812
40.3k
    if (*s == '}') {
813
21.2k
      s++;
814
21.2k
      goto success;
815
21.2k
    }
816
    
817
19.0k
    if (*s++ != ',')
818
33
      goto failure;
819
19.0k
    skip_space(&s);
820
19.0k
  }
821
  
822
26.2k
success:
823
26.2k
  depth--;
824
26.2k
  *sp = s;
825
26.2k
  if (out)
826
11.4k
    *out = ret;
827
26.2k
  return TRUE;
828
829
2.11k
failure_free_key:
830
2.11k
  if (out)
831
0
    free(key);
832
3.99k
failure:
833
3.99k
  depth--;
834
3.99k
  json_delete(ret);
835
3.99k
  return FALSE;
836
2.11k
}
837
838
int parse_string(const char **sp, char **out)
839
75.9k
{
840
75.9k
  const char *s = *sp;
841
75.9k
  SB sb;
842
75.9k
  char throwaway_buffer[4];
843
    /* enough space for a UTF-8 character */
844
75.9k
  char *b;
845
  
846
75.9k
  if (*s++ != '"')
847
1.84k
    return FALSE;
848
  
849
74.1k
  if (out) {
850
29.1k
    sb_init(&sb);
851
29.1k
    sb_need(&sb, 4);
852
29.1k
    b = sb.cur;
853
44.9k
  } else {
854
44.9k
    b = throwaway_buffer;
855
44.9k
  }
856
  
857
4.46M
  while (*s != '"') {
858
4.39M
    unsigned char c = *s++;
859
    
860
    /* Parse next character, and write it to b. */
861
4.39M
    if (c == '\\') {
862
6.49k
      c = *s++;
863
6.49k
      switch (c) {
864
511
        case '"':
865
705
        case '\\':
866
899
        case '/':
867
899
          *b++ = c;
868
899
          break;
869
194
        case 'b':
870
194
          *b++ = '\b';
871
194
          break;
872
194
        case 'f':
873
194
          *b++ = '\f';
874
194
          break;
875
511
        case 'n':
876
511
          *b++ = '\n';
877
511
          break;
878
202
        case 'r':
879
202
          *b++ = '\r';
880
202
          break;
881
194
        case 't':
882
194
          *b++ = '\t';
883
194
          break;
884
4.30k
        case 'u':
885
4.30k
        {
886
4.30k
          uint16_t uc, lc;
887
4.30k
          uint32_t unicode;
888
          
889
4.30k
          if (!parse_hex16(&s, &uc))
890
53
            goto failed;
891
          
892
4.25k
          if (uc >= 0xD800 && uc <= 0xDFFF) {
893
            /* Handle UTF-16 surrogate pair. */
894
591
            if (*s++ != '\\' || *s++ != 'u' || !parse_hex16(&s, &lc))
895
70
              goto failed; /* Incomplete surrogate pair. */
896
521
            if (!from_surrogate_pair(uc, lc, &unicode))
897
29
              goto failed; /* Invalid surrogate pair. */
898
3.65k
          } else if (uc == 0) {
899
            /* Disallow "\u0000". */
900
1
            goto failed;
901
3.65k
          } else {
902
3.65k
            unicode = uc;
903
3.65k
          }
904
          
905
4.15k
          b += utf8_write_char(unicode, b);
906
4.15k
          break;
907
4.25k
        }
908
1
        default:
909
          /* Invalid escape */
910
1
          goto failed;
911
6.49k
      }
912
4.38M
    } else if (c <= 0x1F) {
913
      /* Control characters are not allowed in string literals. */
914
173
      goto failed;
915
4.38M
    } else {
916
      /* Validate and echo a UTF-8 character. */
917
4.38M
      int len;
918
      
919
4.38M
      s--;
920
4.38M
      len = utf8_validate_cz(s);
921
4.38M
      if (len == 0)
922
1.88k
        goto failed; /* Invalid UTF-8 character. */
923
      
924
8.84M
      while (len--)
925
4.46M
        *b++ = *s++;
926
4.38M
    }
927
    
928
    /*
929
     * Update sb to know about the new bytes,
930
     * and set up b to write another character.
931
     */
932
4.39M
    if (out) {
933
1.18M
      sb.cur = b;
934
1.18M
      sb_need(&sb, 4);
935
1.18M
      b = sb.cur;
936
3.20M
    } else {
937
3.20M
      b = throwaway_buffer;
938
3.20M
    }
939
4.39M
  }
940
71.9k
  s++;
941
  
942
71.9k
  if (out)
943
29.1k
    *out = sb_finish(&sb);
944
71.9k
  *sp = s;
945
71.9k
  return TRUE;
946
947
2.21k
failed:
948
2.21k
  if (out)
949
0
    sb_free(&sb);
950
2.21k
  return FALSE;
951
74.1k
}
952
953
/*
954
 * The JSON spec says that a number shall follow this precise pattern
955
 * (spaces and quotes added for readability):
956
 *   '-'? (0 | [1-9][0-9]*) ('.' [0-9]+)? ([Ee] [+-]? [0-9]+)?
957
 *
958
 * However, some JSON parsers are more liberal.  For instance, PHP accepts
959
 * '.5' and '1.'.  JSON.parse accepts '+3'.
960
 *
961
 * This function takes the strict approach.
962
 */
963
int parse_number(const char **sp, double *out)
964
18.3k
{
965
18.3k
  const char *s = *sp;
966
967
  /* '-'? */
968
18.3k
  if (*s == '-')
969
194
    s++;
970
971
  /* (0 | [1-9][0-9]*) */
972
18.3k
  if (*s == '0') {
973
9.51k
    s++;
974
9.51k
  } else {
975
8.84k
    if (!is_digit(*s))
976
207
      return FALSE;
977
8.83k
    do {
978
8.83k
      s++;
979
8.83k
    } while (is_digit(*s));
980
8.63k
  }
981
982
  /* ('.' [0-9]+)? */
983
18.1k
  if (*s == '.') {
984
310
    s++;
985
310
    if (!is_digit(*s))
986
14
      return FALSE;
987
500
    do {
988
500
      s++;
989
500
    } while (is_digit(*s));
990
296
  }
991
992
  /* ([Ee] [+-]? [0-9]+)? */
993
18.1k
  if (*s == 'E' || *s == 'e') {
994
1.03k
    s++;
995
1.03k
    if (*s == '+' || *s == '-')
996
388
      s++;
997
1.03k
    if (!is_digit(*s))
998
33
      return FALSE;
999
1.20k
    do {
1000
1.20k
      s++;
1001
1.20k
    } while (is_digit(*s));
1002
1.00k
  }
1003
1004
18.1k
  if (out)
1005
7.41k
    *out = strtod(*sp, NULL);
1006
1007
18.1k
  *sp = s;
1008
18.1k
  return TRUE;
1009
18.1k
}
1010
1011
static void skip_space(const char **sp)
1012
260k
{
1013
260k
  const char *s = *sp;
1014
322k
  while (is_space(*s))
1015
62.0k
    s++;
1016
260k
  *sp = s;
1017
260k
}
1018
1019
static void emit_value(SB *out, const JsonNode *node)
1020
0
{
1021
0
  assert(tag_is_valid(node->tag));
1022
0
  switch (node->tag) {
1023
0
    case JSON_NULL:
1024
0
      sb_puts(out, "null");
1025
0
      break;
1026
0
    case JSON_BOOL:
1027
0
      sb_puts(out, node->bool_ ? "true" : "false");
1028
0
      break;
1029
0
    case JSON_STRING:
1030
0
      emit_string(out, node->string_);
1031
0
      break;
1032
0
    case JSON_NUMBER:
1033
0
      emit_number(out, node->number_);
1034
0
      break;
1035
0
    case JSON_ARRAY:
1036
0
      emit_array(out, node);
1037
0
      break;
1038
0
    case JSON_OBJECT:
1039
0
      emit_object(out, node);
1040
0
      break;
1041
0
    default:
1042
0
      assert(FALSE);
1043
0
  }
1044
0
}
1045
1046
void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level)
1047
0
{
1048
0
  assert(tag_is_valid(node->tag));
1049
0
  switch (node->tag) {
1050
0
    case JSON_NULL:
1051
0
      sb_puts(out, "null");
1052
0
      break;
1053
0
    case JSON_BOOL:
1054
0
      sb_puts(out, node->bool_ ? "true" : "false");
1055
0
      break;
1056
0
    case JSON_STRING:
1057
0
      emit_string(out, node->string_);
1058
0
      break;
1059
0
    case JSON_NUMBER:
1060
0
      emit_number(out, node->number_);
1061
0
      break;
1062
0
    case JSON_ARRAY:
1063
0
      emit_array_indented(out, node, space, indent_level);
1064
0
      break;
1065
0
    case JSON_OBJECT:
1066
0
      emit_object_indented(out, node, space, indent_level);
1067
0
      break;
1068
0
    default:
1069
0
      assert(FALSE);
1070
0
  }
1071
0
}
1072
1073
static void emit_array(SB *out, const JsonNode *array)
1074
0
{
1075
0
  const JsonNode *element;
1076
  
1077
0
  sb_putc(out, '[');
1078
0
  json_foreach(element, array) {
1079
0
    emit_value(out, element);
1080
0
    if (element->next != NULL)
1081
0
      sb_putc(out, ',');
1082
0
  }
1083
0
  sb_putc(out, ']');
1084
0
}
1085
1086
static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level)
1087
0
{
1088
0
  const JsonNode *element = array->children.head;
1089
0
  int i;
1090
  
1091
0
  if (element == NULL) {
1092
0
    sb_puts(out, "[]");
1093
0
    return;
1094
0
  }
1095
  
1096
0
  sb_puts(out, "[\n");
1097
0
  while (element != NULL) {
1098
0
    for (i = 0; i < indent_level + 1; i++)
1099
0
      sb_puts(out, space);
1100
0
    emit_value_indented(out, element, space, indent_level + 1);
1101
    
1102
0
    element = element->next;
1103
0
    sb_puts(out, element != NULL ? ",\n" : "\n");
1104
0
  }
1105
0
  for (i = 0; i < indent_level; i++)
1106
0
    sb_puts(out, space);
1107
0
  sb_putc(out, ']');
1108
0
}
1109
1110
static void emit_object(SB *out, const JsonNode *object)
1111
0
{
1112
0
  const JsonNode *member;
1113
  
1114
0
  sb_putc(out, '{');
1115
0
  json_foreach(member, object) {
1116
0
    emit_string(out, member->key);
1117
0
    sb_putc(out, ':');
1118
0
    emit_value(out, member);
1119
0
    if (member->next != NULL)
1120
0
      sb_putc(out, ',');
1121
0
  }
1122
0
  sb_putc(out, '}');
1123
0
}
1124
1125
static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level)
1126
0
{
1127
0
  const JsonNode *member = object->children.head;
1128
0
  int i;
1129
  
1130
0
  if (member == NULL) {
1131
0
    sb_puts(out, "{}");
1132
0
    return;
1133
0
  }
1134
  
1135
0
  sb_puts(out, "{\n");
1136
0
  while (member != NULL) {
1137
0
    for (i = 0; i < indent_level + 1; i++)
1138
0
      sb_puts(out, space);
1139
0
    emit_string(out, member->key);
1140
0
    sb_puts(out, ": ");
1141
0
    emit_value_indented(out, member, space, indent_level + 1);
1142
    
1143
0
    member = member->next;
1144
0
    sb_puts(out, member != NULL ? ",\n" : "\n");
1145
0
  }
1146
0
  for (i = 0; i < indent_level; i++)
1147
0
    sb_puts(out, space);
1148
0
  sb_putc(out, '}');
1149
0
}
1150
1151
void emit_string(SB *out, const char *str)
1152
0
{
1153
0
  const char *s = str;
1154
0
  char *b;
1155
  
1156
0
  assert(utf8_validate(str));
1157
  
1158
  /*
1159
   * 14 bytes is enough space to write up to two
1160
   * \uXXXX escapes and two quotation marks.
1161
   */
1162
0
  sb_need(out, 14);
1163
0
  b = out->cur;
1164
  
1165
0
  *b++ = '"';
1166
0
  while (*s != 0) {
1167
0
    unsigned char c = *s++;
1168
    
1169
    /* Encode the next character, and write it to b. */
1170
0
    switch (c) {
1171
0
      case '"':
1172
0
        *b++ = '\\';
1173
0
        *b++ = '"';
1174
0
        break;
1175
0
      case '\\':
1176
0
        *b++ = '\\';
1177
0
        *b++ = '\\';
1178
0
        break;
1179
0
      case '\b':
1180
0
        *b++ = '\\';
1181
0
        *b++ = 'b';
1182
0
        break;
1183
0
      case '\f':
1184
0
        *b++ = '\\';
1185
0
        *b++ = 'f';
1186
0
        break;
1187
0
      case '\n':
1188
0
        *b++ = '\\';
1189
0
        *b++ = 'n';
1190
0
        break;
1191
0
      case '\r':
1192
0
        *b++ = '\\';
1193
0
        *b++ = 'r';
1194
0
        break;
1195
0
      case '\t':
1196
0
        *b++ = '\\';
1197
0
        *b++ = 't';
1198
0
        break;
1199
0
      default: {
1200
0
        int len;
1201
        
1202
0
        s--;
1203
0
        len = utf8_validate_cz(s);
1204
        
1205
0
        if (len == 0) {
1206
          /*
1207
           * Handle invalid UTF-8 character gracefully in production
1208
           * by writing a replacement character (U+FFFD)
1209
           * and skipping a single byte.
1210
           *
1211
           * This should never happen when assertions are enabled
1212
           * due to the assertion at the beginning of this function.
1213
           */
1214
0
          assert(FALSE);
1215
0
          *b++ = 0xEF;
1216
0
          *b++ = 0xBF;
1217
0
          *b++ = 0xBD;
1218
0
          s++;
1219
0
        } else if (c < 0x1F) {
1220
          /* Encode using \u.... */
1221
0
          uint32_t unicode;
1222
          
1223
0
          s += utf8_read_char(s, &unicode);
1224
          
1225
0
          if (unicode <= 0xFFFF) {
1226
0
            *b++ = '\\';
1227
0
            *b++ = 'u';
1228
0
            b += write_hex16(b, unicode);
1229
0
          } else {
1230
            /* Produce a surrogate pair. */
1231
0
            uint16_t uc, lc;
1232
0
            assert(unicode <= 0x10FFFF);
1233
0
            to_surrogate_pair(unicode, &uc, &lc);
1234
0
            *b++ = '\\';
1235
0
            *b++ = 'u';
1236
0
            b += write_hex16(b, uc);
1237
0
            *b++ = '\\';
1238
0
            *b++ = 'u';
1239
0
            b += write_hex16(b, lc);
1240
0
          }
1241
0
        } else {
1242
          /* Write the character directly. */
1243
0
          while (len--)
1244
0
            *b++ = *s++;
1245
0
        }
1246
        
1247
0
        break;
1248
0
      }
1249
0
    }
1250
  
1251
    /*
1252
     * Update *out to know about the new bytes,
1253
     * and set up b to write another encoded character.
1254
     */
1255
0
    out->cur = b;
1256
0
    sb_need(out, 14);
1257
0
    b = out->cur;
1258
0
  }
1259
0
  *b++ = '"';
1260
  
1261
0
  out->cur = b;
1262
0
}
1263
1264
static void emit_number(SB *out, double num)
1265
0
{
1266
  /*
1267
   * This isn't exactly how JavaScript renders numbers,
1268
   * but it should produce valid JSON for reasonable numbers
1269
   * preserve precision well enough, and avoid some oddities
1270
   * like 0.3 -> 0.299999999999999988898 .
1271
   */
1272
0
  char buf[64];
1273
0
  snprintf(buf, sizeof(buf)-1, "%.16g", num);
1274
0
        buf[sizeof(buf)-1] = '\0';
1275
  
1276
0
  if (number_is_valid(buf))
1277
0
    sb_puts(out, buf);
1278
0
  else
1279
0
    sb_puts(out, "null");
1280
0
}
1281
1282
static int tag_is_valid(unsigned int tag)
1283
0
{
1284
0
  return (/* tag >= JSON_NULL && */ tag <= JSON_OBJECT);
1285
0
}
1286
1287
static int number_is_valid(const char *num)
1288
0
{
1289
0
  return (parse_number(&num, NULL) && *num == '\0');
1290
0
}
1291
1292
static int expect_literal(const char **sp, const char *str)
1293
2.16k
{
1294
2.16k
  const char *s = *sp;
1295
  
1296
11.5k
  while (*str != '\0')
1297
9.39k
    if (*s++ != *str++)
1298
10
      return FALSE;
1299
  
1300
2.15k
  *sp = s;
1301
2.15k
  return TRUE;
1302
2.16k
}
1303
1304
/*
1305
 * Parses exactly 4 hex characters (capital or lowercase).
1306
 * Fails if any input chars are not [0-9A-Fa-f].
1307
 */
1308
static int parse_hex16(const char **sp, uint16_t *out)
1309
4.86k
{
1310
4.86k
  const char *s = *sp;
1311
4.86k
  uint16_t ret = 0;
1312
4.86k
  uint16_t i;
1313
4.86k
  uint16_t tmp;
1314
4.86k
  char c;
1315
1316
24.0k
  for (i = 0; i < 4; i++) {
1317
19.2k
    c = *s++;
1318
19.2k
    if (c >= '0' && c <= '9')
1319
11.9k
      tmp = c - '0';
1320
7.27k
    else if (c >= 'A' && c <= 'F')
1321
2.78k
      tmp = c - 'A' + 10;
1322
4.49k
    else if (c >= 'a' && c <= 'f')
1323
4.39k
      tmp = c - 'a' + 10;
1324
98
    else
1325
98
      return FALSE;
1326
1327
19.1k
    ret <<= 4;
1328
19.1k
    ret += tmp;
1329
19.1k
  }
1330
  
1331
4.77k
  if (out)
1332
4.77k
    *out = ret;
1333
4.77k
  *sp = s;
1334
4.77k
  return TRUE;
1335
4.86k
}
1336
1337
/*
1338
 * Encodes a 16-bit number into hexadecimal,
1339
 * writing exactly 4 hex chars.
1340
 */
1341
static int write_hex16(char *out, uint16_t val)
1342
0
{
1343
0
  const char *hex = "0123456789ABCDEF";
1344
  
1345
0
  *out++ = hex[(val >> 12) & 0xF];
1346
0
  *out++ = hex[(val >> 8)  & 0xF];
1347
0
  *out++ = hex[(val >> 4)  & 0xF];
1348
0
  *out++ = hex[ val        & 0xF];
1349
  
1350
0
  return 4;
1351
0
}
1352
1353
int json_check(const JsonNode *node, char errmsg[256])
1354
0
{
1355
0
  #define problem(...) do { \
1356
0
      if (errmsg != NULL) \
1357
0
        snprintf(errmsg, 256, __VA_ARGS__); \
1358
0
      return FALSE; \
1359
0
    } while (0)
1360
  
1361
0
  if (node->key != NULL && !utf8_validate(node->key))
1362
0
    problem("key contains invalid UTF-8");
1363
  
1364
0
  if (!tag_is_valid(node->tag))
1365
0
    problem("tag is invalid (%u)", node->tag);
1366
  
1367
0
  if (node->tag == JSON_BOOL) {
1368
0
    if (node->bool_ != FALSE && node->bool_ != TRUE)
1369
0
      problem("bool_ is neither false (%d) nor true (%d)", (int)FALSE, (int)TRUE);
1370
0
  } else if (node->tag == JSON_STRING) {
1371
0
    if (node->string_ == NULL)
1372
0
      problem("string_ is NULL");
1373
0
    if (!utf8_validate(node->string_))
1374
0
      problem("string_ contains invalid UTF-8");
1375
0
  } else if (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT) {
1376
0
    JsonNode *head = node->children.head;
1377
0
    JsonNode *tail = node->children.tail;
1378
    
1379
0
    if (head == NULL || tail == NULL) {
1380
0
      if (head != NULL)
1381
0
        problem("tail is NULL, but head is not");
1382
0
      if (tail != NULL)
1383
0
        problem("head is NULL, but tail is not");
1384
0
    } else {
1385
0
      JsonNode *child;
1386
0
      JsonNode *last = NULL;
1387
      
1388
0
      if (head->prev != NULL)
1389
0
        problem("First child's prev pointer is not NULL");
1390
      
1391
0
      for (child = head; child != NULL; last = child, child = child->next) {
1392
0
        if (child == node)
1393
0
          problem("node is its own child");
1394
0
        if (child->next == child)
1395
0
          problem("child->next == child (cycle)");
1396
0
        if (child->next == head)
1397
0
          problem("child->next == head (cycle)");
1398
        
1399
0
        if (child->parent != node)
1400
0
          problem("child does not point back to parent");
1401
0
        if (child->next != NULL && child->next->prev != child)
1402
0
          problem("child->next does not point back to child");
1403
        
1404
0
        if (node->tag == JSON_ARRAY && child->key != NULL)
1405
0
          problem("Array element's key is not NULL");
1406
0
        if (node->tag == JSON_OBJECT && child->key == NULL)
1407
0
          problem("Object member's key is NULL");
1408
        
1409
0
        if (!json_check(child, errmsg))
1410
0
          return FALSE;
1411
0
      }
1412
      
1413
0
      if (last != tail)
1414
0
        problem("tail does not match pointer found by starting at head and following next links");
1415
0
    }
1416
0
  }
1417
  
1418
0
  return TRUE;
1419
  
1420
0
  #undef problem
1421
0
}