1 #ifndef _POSIX_C_SOURCE
2 #  define _POSIX_C_SOURCE 200112L
3 #elif _POSIX_C_SOURCE < 200112L
4 #  error incompatible _POSIX_C_SOURCE level
5 #endif
6 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 
11 #ifndef PDJSON_H
12 #  include "pdjson.h"
13 #endif
14 
15 #define JSON_FLAG_ERROR      (1u << 0)
16 #define JSON_FLAG_STREAMING  (1u << 1)
17 
18 #if defined(_MSC_VER) && (_MSC_VER < 1900)
19 
20 #define json_error(json, format, ...)                             \
21     if (!(json->flags & JSON_FLAG_ERROR)) {                       \
22         json->flags |= JSON_FLAG_ERROR;                           \
23         _snprintf_s(json->errmsg, sizeof(json->errmsg),           \
24                  _TRUNCATE,                                       \
25                  format,                                          \
26                  __VA_ARGS__);                                    \
27     }                                                             \
28 
29 #else
30 
31 #define json_error(json, format, ...)                             \
32     if (!(json->flags & JSON_FLAG_ERROR)) {                       \
33         json->flags |= JSON_FLAG_ERROR;                           \
34         snprintf(json->errmsg, sizeof(json->errmsg),              \
35                  format,                                          \
36                  __VA_ARGS__);                                    \
37     }                                                             \
38 
39 #endif /* _MSC_VER */
40 
41 /* See also PDJSON_STACK_MAX below. */
42 #ifndef PDJSON_STACK_INC
43 #  define PDJSON_STACK_INC 4
44 #endif
45 
46 struct json_stack {
47     enum json_type type;
48     long count;
49 };
50 
51 static enum json_type
push(json_stream * json,enum json_type type)52 push(json_stream *json, enum json_type type)
53 {
54     json->stack_top++;
55 
56 #ifdef PDJSON_STACK_MAX
57     if (json->stack_top > PDJSON_STACK_MAX) {
58         json_error(json, "%s", "maximum depth of nesting reached");
59         return JSON_ERROR;
60     }
61 #endif
62 
63     if (json->stack_top >= json->stack_size) {
64         struct json_stack *stack;
65         size_t size = (json->stack_size + PDJSON_STACK_INC) * sizeof(*json->stack);
66         stack = (struct json_stack *)json->alloc.realloc(json->stack, size);
67         if (stack == NULL) {
68             json_error(json, "%s", "out of memory");
69             return JSON_ERROR;
70         }
71 
72         json->stack_size += PDJSON_STACK_INC;
73         json->stack = stack;
74     }
75 
76     json->stack[json->stack_top].type = type;
77     json->stack[json->stack_top].count = 0;
78 
79     return type;
80 }
81 
82 static enum json_type
pop(json_stream * json,int c,enum json_type expected)83 pop(json_stream *json, int c, enum json_type expected)
84 {
85     if (json->stack == NULL || json->stack[json->stack_top].type != expected) {
86         json_error(json, "unexpected byte '%c'", c);
87         return JSON_ERROR;
88     }
89     json->stack_top--;
90     return expected == JSON_ARRAY ? JSON_ARRAY_END : JSON_OBJECT_END;
91 }
92 
buffer_peek(struct json_source * source)93 static int buffer_peek(struct json_source *source)
94 {
95     if (source->position < source->source.buffer.length)
96         return source->source.buffer.buffer[source->position];
97     else
98         return EOF;
99 }
100 
buffer_get(struct json_source * source)101 static int buffer_get(struct json_source *source)
102 {
103     int c = source->peek(source);
104     source->position++;
105     return c;
106 }
107 
stream_get(struct json_source * source)108 static int stream_get(struct json_source *source)
109 {
110     source->position++;
111     return fgetc(source->source.stream.stream);
112 }
113 
stream_peek(struct json_source * source)114 static int stream_peek(struct json_source *source)
115 {
116     int c = fgetc(source->source.stream.stream);
117     ungetc(c, source->source.stream.stream);
118     return c;
119 }
120 
init(json_stream * json)121 static void init(json_stream *json)
122 {
123     json->lineno = 1;
124     json->flags = JSON_FLAG_STREAMING;
125     json->errmsg[0] = '\0';
126     json->ntokens = 0;
127     json->next = (enum json_type)0;
128 
129     json->stack = NULL;
130     json->stack_top = -1;
131     json->stack_size = 0;
132 
133     json->data.string = NULL;
134     json->data.string_size = 0;
135     json->data.string_fill = 0;
136     json->source.position = 0;
137 
138     json->alloc.malloc = malloc;
139     json->alloc.realloc = realloc;
140     json->alloc.free = free;
141 }
142 
143 static enum json_type
is_match(json_stream * json,const char * pattern,enum json_type type)144 is_match(json_stream *json, const char *pattern, enum json_type type)
145 {
146     int c;
147     for (const char *p = pattern; *p; p++) {
148         if (*p != (c = json->source.get(&json->source))) {
149             json_error(json, "expected '%c' instead of byte '%c'", *p, c);
150             return JSON_ERROR;
151         }
152     }
153     return type;
154 }
155 
pushchar(json_stream * json,int c)156 static int pushchar(json_stream *json, int c)
157 {
158     if (json->data.string_fill == json->data.string_size) {
159         size_t size = json->data.string_size * 2;
160         char *buffer = (char *)json->alloc.realloc(json->data.string, size);
161         if (buffer == NULL) {
162             json_error(json, "%s", "out of memory");
163             return -1;
164         } else {
165             json->data.string_size = size;
166             json->data.string = buffer;
167         }
168     }
169     json->data.string[json->data.string_fill++] = c;
170     return 0;
171 }
172 
init_string(json_stream * json)173 static int init_string(json_stream *json)
174 {
175     json->data.string_fill = 0;
176     if (json->data.string == NULL) {
177         json->data.string_size = 1024;
178         json->data.string = (char *)json->alloc.malloc(json->data.string_size);
179         if (json->data.string == NULL) {
180             json_error(json, "%s", "out of memory");
181             return -1;
182         }
183     }
184     json->data.string[0] = '\0';
185     return 0;
186 }
187 
encode_utf8(json_stream * json,unsigned long c)188 static int encode_utf8(json_stream *json, unsigned long c)
189 {
190     if (c < 0x80UL) {
191         return pushchar(json, c);
192     } else if (c < 0x0800UL) {
193         return !((pushchar(json, (c >> 6 & 0x1F) | 0xC0) == 0) &&
194                  (pushchar(json, (c >> 0 & 0x3F) | 0x80) == 0));
195     } else if (c < 0x010000UL) {
196         if (c >= 0xd800 && c <= 0xdfff) {
197             json_error(json, "invalid codepoint %06lx", c);
198             return -1;
199         }
200         return !((pushchar(json, (c >> 12 & 0x0F) | 0xE0) == 0) &&
201                  (pushchar(json, (c >>  6 & 0x3F) | 0x80) == 0) &&
202                  (pushchar(json, (c >>  0 & 0x3F) | 0x80) == 0));
203     } else if (c < 0x110000UL) {
204         return !((pushchar(json, (c >> 18 & 0x07) | 0xF0) == 0) &&
205                 (pushchar(json, (c >> 12 & 0x3F) | 0x80) == 0) &&
206                 (pushchar(json, (c >> 6  & 0x3F) | 0x80) == 0) &&
207                 (pushchar(json, (c >> 0  & 0x3F) | 0x80) == 0));
208     } else {
209         json_error(json, "unable to encode %06lx as UTF-8", c);
210         return -1;
211     }
212 }
213 
hexchar(int c)214 static int hexchar(int c)
215 {
216     switch (c) {
217     case '0': return 0;
218     case '1': return 1;
219     case '2': return 2;
220     case '3': return 3;
221     case '4': return 4;
222     case '5': return 5;
223     case '6': return 6;
224     case '7': return 7;
225     case '8': return 8;
226     case '9': return 9;
227     case 'a':
228     case 'A': return 10;
229     case 'b':
230     case 'B': return 11;
231     case 'c':
232     case 'C': return 12;
233     case 'd':
234     case 'D': return 13;
235     case 'e':
236     case 'E': return 14;
237     case 'f':
238     case 'F': return 15;
239     default:
240         return -1;
241     }
242 }
243 
244 static long
read_unicode_cp(json_stream * json)245 read_unicode_cp(json_stream *json)
246 {
247     long cp = 0;
248     int shift = 12;
249 
250     for (size_t i = 0; i < 4; i++) {
251         int c = json->source.get(&json->source);
252         int hc;
253 
254         if (c == EOF) {
255             json_error(json, "%s", "unterminated string literal in Unicode");
256             return -1;
257         } else if ((hc = hexchar(c)) == -1) {
258             json_error(json, "invalid escape Unicode byte '%c'", c);
259             return -1;
260         }
261 
262         cp += hc * (1 << shift);
263         shift -= 4;
264     }
265 
266 
267     return cp;
268 }
269 
read_unicode(json_stream * json)270 static int read_unicode(json_stream *json)
271 {
272     long cp, h, l;
273 
274     if ((cp = read_unicode_cp(json)) == -1) {
275         return -1;
276     }
277 
278     if (cp >= 0xd800 && cp <= 0xdbff) {
279         /* This is the high portion of a surrogate pair; we need to read the
280          * lower portion to get the codepoint
281          */
282         h = cp;
283 
284         int c = json->source.get(&json->source);
285         if (c == EOF) {
286             json_error(json, "%s", "unterminated string literal in Unicode");
287             return -1;
288         } else if (c != '\\') {
289             json_error(json, "invalid continuation for surrogate pair '%c', "
290                              "expected '\\'", c);
291             return -1;
292         }
293 
294         c = json->source.get(&json->source);
295         if (c == EOF) {
296             json_error(json, "%s", "unterminated string literal in Unicode");
297             return -1;
298         } else if (c != 'u') {
299             json_error(json, "invalid continuation for surrogate pair '%c', "
300                              "expected 'u'", c);
301             return -1;
302         }
303 
304         if ((l = read_unicode_cp(json)) == -1) {
305             return -1;
306         }
307 
308         if (l < 0xdc00 || l > 0xdfff) {
309             json_error(json, "surrogate pair continuation \\u%04lx out "
310                              "of range (dc00-dfff)", l);
311             return -1;
312         }
313 
314         cp = ((h - 0xd800) * 0x400) + ((l - 0xdc00) + 0x10000);
315     } else if (cp >= 0xdc00 && cp <= 0xdfff) {
316             json_error(json, "dangling surrogate \\u%04lx", cp);
317             return -1;
318     }
319 
320     return encode_utf8(json, cp);
321 }
322 
323 static int
read_escaped(json_stream * json)324 read_escaped(json_stream *json)
325 {
326     int c = json->source.get(&json->source);
327     if (c == EOF) {
328         json_error(json, "%s", "unterminated string literal in escape");
329         return -1;
330     } else if (c == 'u') {
331         if (read_unicode(json) != 0)
332             return -1;
333     } else {
334         switch (c) {
335         case '\\':
336         case 'b':
337         case 'f':
338         case 'n':
339         case 'r':
340         case 't':
341         case '/':
342         case '"':
343             {
344                 const char *codes = "\\bfnrt/\"";
345                 const char *p = strchr(codes, c);
346                 if (pushchar(json, "\\\b\f\n\r\t/\""[p - codes]) != 0)
347                     return -1;
348             }
349             break;
350         default:
351             json_error(json, "invalid escaped byte '%c'", c);
352             return -1;
353         }
354     }
355     return 0;
356 }
357 
358 static int
char_needs_escaping(int c)359 char_needs_escaping(int c)
360 {
361     if ((c >= 0) && (c < 0x20 || c == 0x22 || c == 0x5c)) {
362         return 1;
363     }
364 
365     return 0;
366 }
367 
368 static int
utf8_seq_length(char byte)369 utf8_seq_length(char byte)
370 {
371     unsigned char u = (unsigned char) byte;
372     if (u < 0x80) return 1;
373 
374     if (0x80 <= u && u <= 0xBF)
375     {
376         // second, third or fourth byte of a multi-byte
377         // sequence, i.e. a "continuation byte"
378         return 0;
379     }
380     else if (u == 0xC0 || u == 0xC1)
381     {
382         // overlong encoding of an ASCII byte
383         return 0;
384     }
385     else if (0xC2 <= u && u <= 0xDF)
386     {
387         // 2-byte sequence
388         return 2;
389     }
390     else if (0xE0 <= u && u <= 0xEF)
391     {
392         // 3-byte sequence
393         return 3;
394     }
395     else if (0xF0 <= u && u <= 0xF4)
396     {
397         // 4-byte sequence
398         return 4;
399     }
400     else
401     {
402         // u >= 0xF5
403         // Restricted (start of 4-, 5- or 6-byte sequence) or invalid UTF-8
404         return 0;
405     }
406 }
407 
408 static int
is_legal_utf8(const unsigned char * bytes,int length)409 is_legal_utf8(const unsigned char *bytes, int length)
410 {
411     if (0 == bytes || 0 == length) return 0;
412 
413     unsigned char a;
414     const unsigned char* srcptr = bytes + length;
415     switch (length)
416     {
417     default:
418         return 0;
419         // Everything else falls through when true.
420     case 4:
421         if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
422         /* FALLTHRU */
423     case 3:
424         if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
425         /* FALLTHRU */
426     case 2:
427         a = (*--srcptr);
428         switch (*bytes)
429         {
430         case 0xE0:
431             if (a < 0xA0 || a > 0xBF) return 0;
432             break;
433         case 0xED:
434             if (a < 0x80 || a > 0x9F) return 0;
435             break;
436         case 0xF0:
437             if (a < 0x90 || a > 0xBF) return 0;
438             break;
439         case 0xF4:
440             if (a < 0x80 || a > 0x8F) return 0;
441             break;
442         default:
443             if (a < 0x80 || a > 0xBF) return 0;
444             break;
445         }
446         /* FALLTHRU */
447     case 1:
448         if (*bytes >= 0x80 && *bytes < 0xC2) return 0;
449     }
450     return *bytes <= 0xF4;
451 }
452 
453 static int
read_utf8(json_stream * json,int next_char)454 read_utf8(json_stream* json, int next_char)
455 {
456     int count = utf8_seq_length(next_char);
457     if (!count)
458     {
459         json_error(json, "%s", "invalid UTF-8 character");
460         return -1;
461     }
462 
463     char buffer[4];
464     buffer[0] = next_char;
465     int i;
466     for (i = 1; i < count; ++i)
467     {
468         buffer[i] = json->source.get(&json->source);;
469     }
470 
471     if (!is_legal_utf8((unsigned char*) buffer, count))
472     {
473         json_error(json, "%s", "invalid UTF-8 text");
474         return -1;
475     }
476 
477     for (i = 0; i < count; ++i)
478     {
479         if (pushchar(json, buffer[i]) != 0)
480             return -1;
481     }
482     return 0;
483 }
484 
485 static enum json_type
read_string(json_stream * json)486 read_string(json_stream *json)
487 {
488     if (init_string(json) != 0)
489         return JSON_ERROR;
490     while (1) {
491         int c = json->source.get(&json->source);
492         if (c == EOF) {
493             json_error(json, "%s", "unterminated string literal");
494             return JSON_ERROR;
495         } else if (c == '"') {
496             if (pushchar(json, '\0') == 0)
497                 return JSON_STRING;
498             else
499                 return JSON_ERROR;
500         } else if (c == '\\') {
501             if (read_escaped(json) != 0)
502                 return JSON_ERROR;
503         } else if ((unsigned) c >= 0x80) {
504             if (read_utf8(json, c) != 0)
505                 return JSON_ERROR;
506         } else {
507             if (char_needs_escaping(c)) {
508                 json_error(json, "%s", "unescaped control character in string");
509                 return JSON_ERROR;
510             }
511 
512             if (pushchar(json, c) != 0)
513                 return JSON_ERROR;
514         }
515     }
516     return JSON_ERROR;
517 }
518 
519 static int
is_digit(int c)520 is_digit(int c)
521 {
522     return c >= 48 /*0*/ && c <= 57 /*9*/;
523 }
524 
525 static int
read_digits(json_stream * json)526 read_digits(json_stream *json)
527 {
528     int c;
529     unsigned nread = 0;
530     while (is_digit(c = json->source.peek(&json->source))) {
531         if (pushchar(json, json->source.get(&json->source)) != 0)
532             return -1;
533 
534         nread++;
535     }
536 
537     if (nread == 0) {
538         json_error(json, "expected digit instead of byte '%c'", c);
539         return -1;
540     }
541 
542     return 0;
543 }
544 
545 static enum json_type
read_number(json_stream * json,int c)546 read_number(json_stream *json, int c)
547 {
548     if (pushchar(json, c) != 0)
549         return JSON_ERROR;
550     if (c == '-') {
551         c = json->source.get(&json->source);
552         if (is_digit(c)) {
553             return read_number(json, c);
554         } else {
555             json_error(json, "unexpected byte '%c' in number", c);
556             return JSON_ERROR;
557         }
558     } else if (strchr("123456789", c) != NULL) {
559         c = json->source.peek(&json->source);
560         if (is_digit(c)) {
561             if (read_digits(json) != 0)
562                 return JSON_ERROR;
563         }
564     }
565     /* Up to decimal or exponent has been read. */
566     c = json->source.peek(&json->source);
567     if (strchr(".eE", c) == NULL) {
568         if (pushchar(json, '\0') != 0)
569             return JSON_ERROR;
570         else
571             return JSON_NUMBER;
572     }
573     if (c == '.') {
574         json->source.get(&json->source); // consume .
575         if (pushchar(json, c) != 0)
576             return JSON_ERROR;
577         if (read_digits(json) != 0)
578             return JSON_ERROR;
579     }
580     /* Check for exponent. */
581     c = json->source.peek(&json->source);
582     if (c == 'e' || c == 'E') {
583         json->source.get(&json->source); // consume e/E
584         if (pushchar(json, c) != 0)
585             return JSON_ERROR;
586         c = json->source.peek(&json->source);
587         if (c == '+' || c == '-') {
588             json->source.get(&json->source); // consume
589             if (pushchar(json, c) != 0)
590                 return JSON_ERROR;
591             if (read_digits(json) != 0)
592                 return JSON_ERROR;
593         } else if (is_digit(c)) {
594             if (read_digits(json) != 0)
595                 return JSON_ERROR;
596         } else {
597             json_error(json, "unexpected byte '%c' in number", c);
598             return JSON_ERROR;
599         }
600     }
601     if (pushchar(json, '\0') != 0)
602         return JSON_ERROR;
603     else
604         return JSON_NUMBER;
605 }
606 
607 bool
json_isspace(int c)608 json_isspace(int c)
609 {
610     switch (c) {
611     case 0x09:
612     case 0x0a:
613     case 0x0d:
614     case 0x20:
615         return true;
616     }
617 
618     return false;
619 }
620 
621 /* Returns the next non-whitespace character in the stream. */
next(json_stream * json)622 static int next(json_stream *json)
623 {
624    int c;
625    while (json_isspace(c = json->source.get(&json->source)))
626        if (c == '\n')
627            json->lineno++;
628    return c;
629 }
630 
631 static enum json_type
read_value(json_stream * json,int c)632 read_value(json_stream *json, int c)
633 {
634     json->ntokens++;
635     switch (c) {
636     case EOF:
637         json_error(json, "%s", "unexpected end of text");
638         return JSON_ERROR;
639     case '{':
640         return push(json, JSON_OBJECT);
641     case '[':
642         return push(json, JSON_ARRAY);
643     case '"':
644         return read_string(json);
645     case 'n':
646         return is_match(json, "ull", JSON_NULL);
647     case 'f':
648         return is_match(json, "alse", JSON_FALSE);
649     case 't':
650         return is_match(json, "rue", JSON_TRUE);
651     case '0':
652     case '1':
653     case '2':
654     case '3':
655     case '4':
656     case '5':
657     case '6':
658     case '7':
659     case '8':
660     case '9':
661     case '-':
662         if (init_string(json) != 0)
663             return JSON_ERROR;
664         return read_number(json, c);
665     default:
666         json_error(json, "unexpected byte '%c' in value", c);
667         return JSON_ERROR;
668     }
669 }
670 
json_peek(json_stream * json)671 enum json_type json_peek(json_stream *json)
672 {
673     enum json_type next;
674     if (json->next)
675         next = json->next;
676     else
677         next = json->next = json_next(json);
678     return next;
679 }
680 
json_next(json_stream * json)681 enum json_type json_next(json_stream *json)
682 {
683     if (json->flags & JSON_FLAG_ERROR)
684         return JSON_ERROR;
685     if (json->next != 0) {
686         enum json_type next = json->next;
687         json->next = (enum json_type)0;
688         return next;
689     }
690     if (json->ntokens > 0 && json->stack_top == (size_t)-1) {
691 
692         /* In the streaming mode leave any trailing whitespaces in the stream.
693          * This allows the user to validate any desired separation between
694          * values (such as newlines) using json_source_get/peek() with any
695          * remaining whitespaces ignored as leading when we parse the next
696          * value. */
697         if (!(json->flags & JSON_FLAG_STREAMING)) {
698             int c;
699 
700             do {
701                 c = json->source.peek(&json->source);
702                 if (json_isspace(c)) {
703                     c = json->source.get(&json->source);
704                 }
705             } while (json_isspace(c));
706 
707             if (c != EOF) {
708                 json_error(json, "expected end of text instead of byte '%c'", c);
709                 return JSON_ERROR;
710             }
711         }
712 
713         return JSON_DONE;
714     }
715     int c = next(json);
716     if (json->stack_top == (size_t)-1) {
717         if (c == EOF && (json->flags & JSON_FLAG_STREAMING))
718             return JSON_DONE;
719 
720         return read_value(json, c);
721     }
722     if (json->stack[json->stack_top].type == JSON_ARRAY) {
723         if (json->stack[json->stack_top].count == 0) {
724             if (c == ']') {
725                 return pop(json, c, JSON_ARRAY);
726             }
727             json->stack[json->stack_top].count++;
728             return read_value(json, c);
729         } else if (c == ',') {
730             json->stack[json->stack_top].count++;
731             return read_value(json, next(json));
732         } else if (c == ']') {
733             return pop(json, c, JSON_ARRAY);
734         } else {
735             json_error(json, "unexpected byte '%c'", c);
736             return JSON_ERROR;
737         }
738     } else if (json->stack[json->stack_top].type == JSON_OBJECT) {
739         if (json->stack[json->stack_top].count == 0) {
740             if (c == '}') {
741                 return pop(json, c, JSON_OBJECT);
742             }
743 
744             /* No member name/value pairs yet. */
745             enum json_type value = read_value(json, c);
746             if (value != JSON_STRING) {
747                 if (value != JSON_ERROR)
748                     json_error(json, "%s", "expected member name or '}'");
749                 return JSON_ERROR;
750             } else {
751                 json->stack[json->stack_top].count++;
752                 return value;
753             }
754         } else if ((json->stack[json->stack_top].count % 2) == 0) {
755             /* Expecting comma followed by member name. */
756             if (c != ',' && c != '}') {
757                 json_error(json, "%s", "expected ',' or '}' after member value");
758                 return JSON_ERROR;
759             } else if (c == '}') {
760                 return pop(json, c, JSON_OBJECT);
761             } else {
762                 enum json_type value = read_value(json, next(json));
763                 if (value != JSON_STRING) {
764                     if (value != JSON_ERROR)
765                         json_error(json, "%s", "expected member name");
766                     return JSON_ERROR;
767                 } else {
768                     json->stack[json->stack_top].count++;
769                     return value;
770                 }
771             }
772         } else if ((json->stack[json->stack_top].count % 2) == 1) {
773             /* Expecting colon followed by value. */
774             if (c != ':') {
775                 json_error(json, "%s", "expected ':' after member name");
776                 return JSON_ERROR;
777             } else {
778                 json->stack[json->stack_top].count++;
779                 return read_value(json, next(json));
780             }
781         }
782     }
783     json_error(json, "%s", "invalid parser state");
784     return JSON_ERROR;
785 }
786 
json_reset(json_stream * json)787 void json_reset(json_stream *json)
788 {
789     json->stack_top = -1;
790     json->ntokens = 0;
791     json->flags &= ~JSON_FLAG_ERROR;
792     json->errmsg[0] = '\0';
793 }
794 
json_skip(json_stream * json)795 enum json_type json_skip(json_stream *json)
796 {
797     enum json_type type = json_next(json);
798     size_t cnt_arr = 0;
799     size_t cnt_obj = 0;
800 
801     for (enum json_type skip = type; ; skip = json_next(json)) {
802         if (skip == JSON_ERROR || skip == JSON_DONE)
803             return skip;
804 
805         if (skip == JSON_ARRAY) {
806             ++cnt_arr;
807         } else if (skip == JSON_ARRAY_END && cnt_arr > 0) {
808             --cnt_arr;
809         } else if (skip == JSON_OBJECT) {
810             ++cnt_obj;
811         } else if (skip == JSON_OBJECT_END && cnt_obj > 0) {
812             --cnt_obj;
813         }
814 
815         if (!cnt_arr && !cnt_obj)
816             break;
817     }
818 
819     return type;
820 }
821 
json_skip_until(json_stream * json,enum json_type type)822 enum json_type json_skip_until(json_stream *json, enum json_type type)
823 {
824     while (1) {
825         enum json_type skip = json_skip(json);
826 
827         if (skip == JSON_ERROR || skip == JSON_DONE)
828             return skip;
829 
830         if (skip == type)
831             break;
832     }
833 
834     return type;
835 }
836 
json_get_string(json_stream * json,size_t * length)837 const char *json_get_string(json_stream *json, size_t *length)
838 {
839     if (length != NULL)
840         *length = json->data.string_fill;
841     if (json->data.string == NULL)
842         return "";
843     else
844         return json->data.string;
845 }
846 
json_get_number(json_stream * json)847 double json_get_number(json_stream *json)
848 {
849     char *p = json->data.string;
850     return p == NULL ? 0 : strtod(p, NULL);
851 }
852 
json_get_error(json_stream * json)853 const char *json_get_error(json_stream *json)
854 {
855     return json->flags & JSON_FLAG_ERROR ? json->errmsg : NULL;
856 }
857 
json_get_lineno(json_stream * json)858 size_t json_get_lineno(json_stream *json)
859 {
860     return json->lineno;
861 }
862 
json_get_position(json_stream * json)863 size_t json_get_position(json_stream *json)
864 {
865     return json->source.position;
866 }
867 
json_get_depth(json_stream * json)868 size_t json_get_depth(json_stream *json)
869 {
870     return json->stack_top + 1;
871 }
872 
873 /* Return the current parsing context, that is, JSON_OBJECT if we are inside
874    an object, JSON_ARRAY if we are inside an array, and JSON_DONE if we are
875    not yet/anymore in either.
876 
877    Additionally, for the first two cases, also return the number of parsing
878    events that have already been observed at this level with json_next/peek().
879    In particular, inside an object, an odd number would indicate that the just
880    observed JSON_STRING event is a member name.
881 */
json_get_context(json_stream * json,size_t * count)882 enum json_type json_get_context(json_stream *json, size_t *count)
883 {
884     if (json->stack_top == (size_t)-1)
885         return JSON_DONE;
886 
887     if (count != NULL)
888         *count = json->stack[json->stack_top].count;
889 
890     return json->stack[json->stack_top].type;
891 }
892 
json_source_get(json_stream * json)893 int json_source_get(json_stream *json)
894 {
895     int c = json->source.get(&json->source);
896     if (c == '\n')
897         json->lineno++;
898     return c;
899 }
900 
json_source_peek(json_stream * json)901 int json_source_peek(json_stream *json)
902 {
903     return json->source.peek(&json->source);
904 }
905 
json_open_buffer(json_stream * json,const void * buffer,size_t size)906 void json_open_buffer(json_stream *json, const void *buffer, size_t size)
907 {
908     init(json);
909     json->source.get = buffer_get;
910     json->source.peek = buffer_peek;
911     json->source.source.buffer.buffer = (const char *)buffer;
912     json->source.source.buffer.length = size;
913 }
914 
json_open_string(json_stream * json,const char * string)915 void json_open_string(json_stream *json, const char *string)
916 {
917     json_open_buffer(json, string, strlen(string));
918 }
919 
json_open_stream(json_stream * json,FILE * stream)920 void json_open_stream(json_stream *json, FILE * stream)
921 {
922     init(json);
923     json->source.get = stream_get;
924     json->source.peek = stream_peek;
925     json->source.source.stream.stream = stream;
926 }
927 
user_get(struct json_source * json)928 static int user_get(struct json_source *json)
929 {
930     return json->source.user.get(json->source.user.ptr);
931 }
932 
user_peek(struct json_source * json)933 static int user_peek(struct json_source *json)
934 {
935     return json->source.user.peek(json->source.user.ptr);
936 }
937 
json_open_user(json_stream * json,json_user_io get,json_user_io peek,void * user)938 void json_open_user(json_stream *json, json_user_io get, json_user_io peek, void *user)
939 {
940     init(json);
941     json->source.get = user_get;
942     json->source.peek = user_peek;
943     json->source.source.user.ptr = user;
944     json->source.source.user.get = get;
945     json->source.source.user.peek = peek;
946 }
947 
json_set_allocator(json_stream * json,json_allocator * a)948 void json_set_allocator(json_stream *json, json_allocator *a)
949 {
950     json->alloc = *a;
951 }
952 
json_set_streaming(json_stream * json,bool streaming)953 void json_set_streaming(json_stream *json, bool streaming)
954 {
955     if (streaming)
956         json->flags |= JSON_FLAG_STREAMING;
957     else
958         json->flags &= ~JSON_FLAG_STREAMING;
959 }
960 
json_close(json_stream * json)961 void json_close(json_stream *json)
962 {
963     json->alloc.free(json->stack);
964     json->alloc.free(json->data.string);
965 }
966