2 * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
11 * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
12 * The copyrights to the contents of this file are licensed under the MIT License
13 * (https://www.opensource.org/licenses/mit-license.php)
18 #include "math_compat.h"
28 #include "json_inttypes.h"
29 #include "json_object.h"
30 #include "json_object_private.h"
31 #include "json_tokener.h"
32 #include "json_util.h"
34 #include "strdup_compat.h"
38 #endif /* HAVE_LOCALE_H */
44 #endif /* HAVE_STRINGS_H */
46 #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x)&7) + 9)
48 #if !HAVE_STRNCASECMP && defined(_MSC_VER)
49 /* MSC has the version as _strnicmp */
50 #define strncasecmp _strnicmp
51 #elif !HAVE_STRNCASECMP
52 #error You do not have strncasecmp on your system.
53 #endif /* HAVE_STRNCASECMP */
55 #if defined(_MSC_VER) && (_MSC_VER <= 1800)
56 /* VS2013 doesn't know about "inline" */
57 #define inline __inline
62 /* The following helper functions are used to speed up parsing. They
63 * are faster than their ctype counterparts because they assume that
64 * the input is in ASCII and that the locale is set to "C". The
65 * compiler will also inline these functions, providing an additional
66 * speedup by saving on function calls.
68 static inline int is_ws_char(char c)
76 static inline int is_hex_char(char c)
78 return (c >= '0' && c <= '9')
79 || (c >= 'A' && c <= 'F')
80 || (c >= 'a' && c <= 'f');
83 /* Use C99 NAN by default; if not available, nan("") should work too. */
88 static const char json_null_str[] = "null";
89 static const int json_null_str_len = sizeof(json_null_str) - 1;
90 static const char json_inf_str[] = "Infinity";
91 /* Swapped case "Infinity" to avoid need to call tolower() on input chars: */
92 static const char json_inf_str_invert[] = "iNFINITY";
93 static const unsigned int json_inf_str_len = sizeof(json_inf_str) - 1;
94 static const char json_nan_str[] = "NaN";
95 static const int json_nan_str_len = sizeof(json_nan_str) - 1;
96 static const char json_true_str[] = "true";
97 static const int json_true_str_len = sizeof(json_true_str) - 1;
98 static const char json_false_str[] = "false";
99 static const int json_false_str_len = sizeof(json_false_str) - 1;
101 /* clang-format off */
102 static const char *json_tokener_errors[] = {
106 "unexpected end of data",
107 "unexpected character",
111 "array value separator ',' expected",
112 "quoted object property name expected",
113 "object property name separator ':' expected",
114 "object value separator ',' expected",
115 "invalid string sequence",
117 "invalid utf-8 string",
118 "buffer size overflow"
120 /* clang-format on */
123 * validete the utf-8 string in strict model.
124 * if not utf-8 format, return err.
126 static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes);
128 static int json_tokener_parse_double(const char *buf, int len, double *retval);
130 const char *json_tokener_error_desc(enum json_tokener_error jerr)
132 int jerr_int = (int)jerr;
134 jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
135 return "Unknown error, "
136 "invalid json_tokener_error value passed to json_tokener_error_desc()";
137 return json_tokener_errors[jerr];
140 enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
145 /* Stuff for decoding unicode sequences */
146 #define IS_HIGH_SURROGATE(uc) (((uc)&0xFC00) == 0xD800)
147 #define IS_LOW_SURROGATE(uc) (((uc)&0xFC00) == 0xDC00)
148 #define DECODE_SURROGATE_PAIR(hi, lo) ((((hi)&0x3FF) << 10) + ((lo)&0x3FF) + 0x10000)
149 static unsigned char utf8_replacement_char[3] = {0xEF, 0xBF, 0xBD};
151 struct json_tokener *json_tokener_new_ex(int depth)
153 struct json_tokener *tok;
155 tok = (struct json_tokener *)calloc(1, sizeof(struct json_tokener));
158 tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
164 tok->pb = printbuf_new();
171 tok->max_depth = depth;
172 json_tokener_reset(tok);
176 struct json_tokener *json_tokener_new(void)
178 return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
181 void json_tokener_free(struct json_tokener *tok)
183 json_tokener_reset(tok);
185 printbuf_free(tok->pb);
190 static void json_tokener_reset_level(struct json_tokener *tok, int depth)
192 tok->stack[depth].state = json_tokener_state_eatws;
193 tok->stack[depth].saved_state = json_tokener_state_start;
194 json_object_put(tok->stack[depth].current);
195 tok->stack[depth].current = NULL;
196 free(tok->stack[depth].obj_field_name);
197 tok->stack[depth].obj_field_name = NULL;
200 void json_tokener_reset(struct json_tokener *tok)
206 for (i = tok->depth; i >= 0; i--)
207 json_tokener_reset_level(tok, i);
209 tok->err = json_tokener_success;
212 struct json_object *json_tokener_parse(const char *str)
214 enum json_tokener_error jerr_ignored;
215 struct json_object *obj;
216 obj = json_tokener_parse_verbose(str, &jerr_ignored);
220 struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokener_error *error)
222 struct json_tokener *tok;
223 struct json_object *obj;
225 tok = json_tokener_new();
228 obj = json_tokener_parse_ex(tok, str, -1);
230 if (tok->err != json_tokener_success
232 /* This would be a more sensible default, and cause parsing
233 * things like "null123" to fail when the caller can't know
234 * where the parsing left off, but starting to fail would
235 * be a notable behaviour change. Save for a 1.0 release.
237 || json_tokener_get_parse_end(tok) != strlen(str)
243 json_object_put(obj);
247 json_tokener_free(tok);
251 #define state tok->stack[tok->depth].state
252 #define saved_state tok->stack[tok->depth].saved_state
253 #define current tok->stack[tok->depth].current
254 #define obj_field_name tok->stack[tok->depth].obj_field_name
257 * json_tokener_parse_ex() consumed a lot of CPU in its main loop,
258 * iterating character-by character. A large performance boost is
259 * achieved by using tighter loops to locally handle units such as
260 * comments and strings. Loops that handle an entire token within
261 * their scope also gather entire strings and pass them to
262 * printbuf_memappend() in a single call, rather than calling
263 * printbuf_memappend() one char at a time.
265 * PEEK_CHAR() and ADVANCE_CHAR() macros are used for code that is
266 * common to both the main loop and the tighter loops.
269 /* PEEK_CHAR(dest, tok) macro:
270 * Peeks at the current char and stores it in dest.
271 * Returns 1 on success, sets tok->err and returns 0 if no more chars.
272 * Implicit inputs: str, len, nBytesp vars
274 #define PEEK_CHAR(dest, tok) \
275 (((tok)->char_offset == len) \
276 ? (((tok)->depth == 0 && state == json_tokener_state_eatws && \
277 saved_state == json_tokener_state_finish) \
278 ? (((tok)->err = json_tokener_success), 0) \
279 : (((tok)->err = json_tokener_continue), 0)) \
280 : (((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && \
281 (!json_tokener_validate_utf8(*str, nBytesp))) \
282 ? ((tok->err = json_tokener_error_parse_utf8_string), 0) \
283 : (((dest) = *str), 1)))
285 /* ADVANCE_CHAR() macro:
286 * Increments str & tok->char_offset.
287 * For convenience of existing conditionals, returns the old value of c (0 on eof)
288 * Implicit inputs: c var
290 #define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
292 /* End optimization macro defs */
294 struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
296 struct json_object *obj = NULL;
298 unsigned int nBytes = 0;
299 unsigned int *nBytesp = &nBytes;
301 #ifdef HAVE_USELOCALE
302 locale_t oldlocale = uselocale(NULL);
304 #elif defined(HAVE_SETLOCALE)
305 char *oldlocale = NULL;
308 tok->char_offset = 0;
309 tok->err = json_tokener_success;
311 /* this interface is presently not 64-bit clean due to the int len argument
312 * and the internal printbuf interface that takes 32-bit int len arguments
313 * so the function limits the maximum string size to INT32_MAX (2GB).
314 * If the function is called with len == -1 then strlen is called to check
315 * the string length is less than INT32_MAX (2GB)
317 if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX))
319 tok->err = json_tokener_error_size;
323 #ifdef HAVE_USELOCALE
325 locale_t duploc = duplocale(oldlocale);
326 newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
334 #elif defined(HAVE_SETLOCALE)
337 tmplocale = setlocale(LC_NUMERIC, NULL);
339 oldlocale = strdup(tmplocale);
340 setlocale(LC_NUMERIC, "C");
344 while (PEEK_CHAR(c, tok)) // Note: c might be '\0' !
351 case json_tokener_state_eatws:
352 /* Advance until we change state */
353 while (is_ws_char(c))
355 if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
358 if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
360 printbuf_reset(tok->pb);
361 printbuf_memappend_fast(tok->pb, &c, 1);
362 state = json_tokener_state_comment_start;
371 case json_tokener_state_start:
375 state = json_tokener_state_eatws;
376 saved_state = json_tokener_state_object_field_start;
377 current = json_object_new_object();
382 state = json_tokener_state_eatws;
383 saved_state = json_tokener_state_array;
384 current = json_object_new_array();
390 state = json_tokener_state_inf;
391 printbuf_reset(tok->pb);
396 state = json_tokener_state_null; // or NaN
397 printbuf_reset(tok->pb);
401 if (tok->flags & JSON_TOKENER_STRICT)
403 /* in STRICT mode only double-quote are allowed */
404 tok->err = json_tokener_error_parse_unexpected;
409 state = json_tokener_state_string;
410 printbuf_reset(tok->pb);
417 state = json_tokener_state_boolean;
418 printbuf_reset(tok->pb);
432 state = json_tokener_state_number;
433 printbuf_reset(tok->pb);
436 default: tok->err = json_tokener_error_parse_unexpected; goto out;
440 case json_tokener_state_finish:
443 obj = json_object_get(current);
444 json_tokener_reset_level(tok, tok->depth);
448 case json_tokener_state_inf: /* aka starts with 'i' (or 'I', or "-i", or "-I") */
450 /* If we were guaranteed to have len set, then we could (usually) handle
451 * the entire "Infinity" check in a single strncmp (strncasecmp), but
452 * since len might be -1 (i.e. "read until \0"), we need to check it
453 * a character at a time.
454 * Trying to handle it both ways would make this code considerably more
455 * complicated with likely little performance benefit.
459 /* Note: tok->st_pos must be 0 when state is set to json_tokener_state_inf */
460 while (tok->st_pos < (int)json_inf_str_len)
462 char inf_char = *str;
463 if (inf_char != json_inf_str[tok->st_pos] &&
464 ((tok->flags & JSON_TOKENER_STRICT) ||
465 inf_char != json_inf_str_invert[tok->st_pos])
468 tok->err = json_tokener_error_parse_unexpected;
472 (void)ADVANCE_CHAR(str, tok);
473 if (!PEEK_CHAR(c, tok))
475 /* out of input chars, for now at least */
479 /* We checked the full length of "Infinity", so create the object.
480 * When handling -Infinity, the number parsing code will have dropped
481 * the "-" into tok->pb for us, so check it now.
483 if (printbuf_length(tok->pb) > 0 && *(tok->pb->buf) == '-')
487 current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
490 saved_state = json_tokener_state_finish;
491 state = json_tokener_state_eatws;
495 case json_tokener_state_null: /* aka starts with 'n' */
499 printbuf_memappend_fast(tok->pb, &c, 1);
500 size = json_min(tok->st_pos + 1, json_null_str_len);
501 size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
502 if ((!(tok->flags & JSON_TOKENER_STRICT) &&
503 strncasecmp(json_null_str, tok->pb->buf, size) == 0) ||
504 (strncmp(json_null_str, tok->pb->buf, size) == 0))
506 if (tok->st_pos == json_null_str_len)
509 saved_state = json_tokener_state_finish;
510 state = json_tokener_state_eatws;
514 else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
515 strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) ||
516 (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0))
518 if (tok->st_pos == json_nan_str_len)
520 current = json_object_new_double(NAN);
523 saved_state = json_tokener_state_finish;
524 state = json_tokener_state_eatws;
530 tok->err = json_tokener_error_parse_null;
537 case json_tokener_state_comment_start:
540 state = json_tokener_state_comment;
544 state = json_tokener_state_comment_eol;
548 tok->err = json_tokener_error_parse_comment;
551 printbuf_memappend_fast(tok->pb, &c, 1);
554 case json_tokener_state_comment:
556 /* Advance until we change state */
557 const char *case_start = str;
560 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
562 printbuf_memappend_fast(tok->pb, case_start,
567 printbuf_memappend_fast(tok->pb, case_start, 1 + str - case_start);
568 state = json_tokener_state_comment_end;
572 case json_tokener_state_comment_eol:
574 /* Advance until we change state */
575 const char *case_start = str;
578 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
580 printbuf_memappend_fast(tok->pb, case_start,
585 printbuf_memappend_fast(tok->pb, case_start, str - case_start);
586 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
587 state = json_tokener_state_eatws;
591 case json_tokener_state_comment_end:
592 printbuf_memappend_fast(tok->pb, &c, 1);
595 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
596 state = json_tokener_state_eatws;
600 state = json_tokener_state_comment;
604 case json_tokener_state_string:
606 /* Advance until we change state */
607 const char *case_start = str;
610 if (c == tok->quote_char)
612 printbuf_memappend_fast(tok->pb, case_start,
615 json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
618 saved_state = json_tokener_state_finish;
619 state = json_tokener_state_eatws;
624 printbuf_memappend_fast(tok->pb, case_start,
626 saved_state = json_tokener_state_string;
627 state = json_tokener_state_string_escape;
630 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
632 printbuf_memappend_fast(tok->pb, case_start,
640 case json_tokener_state_string_escape:
646 printbuf_memappend_fast(tok->pb, &c, 1);
655 printbuf_memappend_fast(tok->pb, "\b", 1);
657 printbuf_memappend_fast(tok->pb, "\n", 1);
659 printbuf_memappend_fast(tok->pb, "\r", 1);
661 printbuf_memappend_fast(tok->pb, "\t", 1);
663 printbuf_memappend_fast(tok->pb, "\f", 1);
669 state = json_tokener_state_escape_unicode;
671 default: tok->err = json_tokener_error_parse_string; goto out;
675 // ===================================================
677 case json_tokener_state_escape_unicode:
679 /* Handle a 4-byte \uNNNN sequence, or two sequences if a surrogate pair */
682 if (!c || !is_hex_char(c))
684 tok->err = json_tokener_error_parse_string;
688 ((unsigned int)jt_hexdigit(c) << ((3 - tok->st_pos) * 4));
690 if (tok->st_pos >= 4)
693 (void)ADVANCE_CHAR(str, tok);
694 if (!PEEK_CHAR(c, tok))
697 * We're out of characters in the current call to
698 * json_tokener_parse(), but a subsequent call might
699 * provide us with more, so leave our current state
700 * as-is (including tok->high_surrogate) and return.
707 /* Now, we have a full \uNNNN sequence in tok->ucs_char */
709 /* If the *previous* sequence was a high surrogate ... */
710 if (tok->high_surrogate)
712 if (IS_LOW_SURROGATE(tok->ucs_char))
714 /* Recalculate the ucs_char, then fall thru to process normally */
715 tok->ucs_char = DECODE_SURROGATE_PAIR(tok->high_surrogate,
720 /* High surrogate was not followed by a low surrogate
721 * Replace the high and process the rest normally
723 printbuf_memappend_fast(tok->pb,
724 (char *)utf8_replacement_char, 3);
726 tok->high_surrogate = 0;
729 if (tok->ucs_char < 0x80)
731 unsigned char unescaped_utf[1];
732 unescaped_utf[0] = tok->ucs_char;
733 printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 1);
735 else if (tok->ucs_char < 0x800)
737 unsigned char unescaped_utf[2];
738 unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
739 unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
740 printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 2);
742 else if (IS_HIGH_SURROGATE(tok->ucs_char))
745 * The next two characters should be \u, HOWEVER,
746 * we can't simply peek ahead here, because the
747 * characters we need might not be passed to us
748 * until a subsequent call to json_tokener_parse.
749 * Instead, transition through a couple of states.
751 * _escape_unicode => _unicode_need_escape
753 * _unicode_need_escape => _unicode_need_u
755 * _unicode_need_u => _escape_unicode
756 * ...and we'll end up back around here.
758 tok->high_surrogate = tok->ucs_char;
760 state = json_tokener_state_escape_unicode_need_escape;
763 else if (IS_LOW_SURROGATE(tok->ucs_char))
765 /* Got a low surrogate not preceded by a high */
766 printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
768 else if (tok->ucs_char < 0x10000)
770 unsigned char unescaped_utf[3];
771 unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
772 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
773 unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
774 printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 3);
776 else if (tok->ucs_char < 0x110000)
778 unsigned char unescaped_utf[4];
779 unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07);
780 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
781 unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
782 unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
783 printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 4);
787 /* Don't know what we got--insert the replacement char */
788 printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
790 state = saved_state; // i.e. _state_string or _state_object_field
794 case json_tokener_state_escape_unicode_need_escape:
795 // We get here after processing a high_surrogate
796 // require a '\\' char
799 /* Got a high surrogate without another sequence following
800 * it. Put a replacement char in for the high surrogate
801 * and pop back up to _state_string or _state_object_field.
803 printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
804 tok->high_surrogate = 0;
810 state = json_tokener_state_escape_unicode_need_u;
813 case json_tokener_state_escape_unicode_need_u:
814 /* We already had a \ char, check that it's \u */
817 /* Got a high surrogate with some non-unicode escape
818 * sequence following it.
819 * Put a replacement char in for the high surrogate
820 * and handle the escape sequence normally.
822 printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
823 tok->high_surrogate = 0;
826 state = json_tokener_state_string_escape;
829 state = json_tokener_state_escape_unicode;
832 // ===================================================
834 case json_tokener_state_boolean:
837 printbuf_memappend_fast(tok->pb, &c, 1);
838 size1 = json_min(tok->st_pos + 1, json_true_str_len);
839 size2 = json_min(tok->st_pos + 1, json_false_str_len);
840 if ((!(tok->flags & JSON_TOKENER_STRICT) &&
841 strncasecmp(json_true_str, tok->pb->buf, size1) == 0) ||
842 (strncmp(json_true_str, tok->pb->buf, size1) == 0))
844 if (tok->st_pos == json_true_str_len)
846 current = json_object_new_boolean(1);
849 saved_state = json_tokener_state_finish;
850 state = json_tokener_state_eatws;
854 else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
855 strncasecmp(json_false_str, tok->pb->buf, size2) == 0) ||
856 (strncmp(json_false_str, tok->pb->buf, size2) == 0))
858 if (tok->st_pos == json_false_str_len)
860 current = json_object_new_boolean(0);
863 saved_state = json_tokener_state_finish;
864 state = json_tokener_state_eatws;
870 tok->err = json_tokener_error_parse_boolean;
877 case json_tokener_state_number:
879 /* Advance until we change state */
880 const char *case_start = str;
885 if (printbuf_length(tok->pb) > 0)
887 /* We don't save all state from the previous incremental parse
888 so we need to re-generate it based on the saved string so far.
890 char *e_loc = strchr(tok->pb->buf, 'e');
892 e_loc = strchr(tok->pb->buf, 'E');
895 char *last_saved_char =
896 &tok->pb->buf[printbuf_length(tok->pb) - 1];
898 pos_sign_ok = neg_sign_ok = 1;
899 /* If the "e" isn't at the end, we can't start with a '-' */
900 if (e_loc != last_saved_char)
905 // else leave it set to 1, i.e. start of the new input
909 while (c && ((c >= '0' && c <= '9') ||
910 (!is_exponent && (c == 'e' || c == 'E')) ||
911 (neg_sign_ok && c == '-') || (pos_sign_ok && c == '+') ||
912 (!tok->is_double && c == '.')))
914 pos_sign_ok = neg_sign_ok = 0;
917 /* non-digit characters checks */
918 /* note: since the main loop condition to get here was
919 * an input starting with 0-9 or '-', we are
920 * protected from input starting with '.' or
930 case 'e': /* FALLTHRU */
934 /* the exponent part can begin with a negative sign */
935 pos_sign_ok = neg_sign_ok = 1;
940 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
942 printbuf_memappend_fast(tok->pb, case_start, case_len);
947 Now we know c isn't a valid number char, but check whether
948 it might have been intended to be, and return a potentially
949 more understandable error right away.
950 However, if we're at the top-level, use the number as-is
951 because c can be part of a new object to parse on the
952 next call to json_tokener_parse().
954 if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
955 c != 'I' && c != 'i' && !is_ws_char(c))
957 tok->err = json_tokener_error_parse_number;
961 printbuf_memappend_fast(tok->pb, case_start, case_len);
963 // Check for -Infinity
964 if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
966 state = json_tokener_state_inf;
970 if (tok->is_double && !(tok->flags & JSON_TOKENER_STRICT))
972 /* Trim some chars off the end, to allow things
973 like "123e+" to parse ok. */
974 while (printbuf_length(tok->pb) > 1)
976 char last_char = tok->pb->buf[printbuf_length(tok->pb) - 1];
977 if (last_char != 'e' && last_char != 'E' &&
978 last_char != '-' && last_char != '+')
982 tok->pb->buf[printbuf_length(tok->pb) - 1] = '\0';
983 printbuf_length(tok->pb)--;
991 if (!tok->is_double && tok->pb->buf[0] == '-' &&
992 json_parse_int64(tok->pb->buf, &num64) == 0)
994 current = json_object_new_int64(num64);
998 else if (!tok->is_double && tok->pb->buf[0] != '-' &&
999 json_parse_uint64(tok->pb->buf, &numuint64) == 0)
1001 if (numuint64 && tok->pb->buf[0] == '0' &&
1002 (tok->flags & JSON_TOKENER_STRICT))
1004 tok->err = json_tokener_error_parse_number;
1007 if (numuint64 <= INT64_MAX)
1009 num64 = (uint64_t)numuint64;
1010 current = json_object_new_int64(num64);
1011 if (current == NULL)
1016 current = json_object_new_uint64(numuint64);
1017 if (current == NULL)
1021 else if (tok->is_double &&
1022 json_tokener_parse_double(
1023 tok->pb->buf, printbuf_length(tok->pb), &numd) == 0)
1025 current = json_object_new_double_s(numd, tok->pb->buf);
1026 if (current == NULL)
1031 tok->err = json_tokener_error_parse_number;
1034 saved_state = json_tokener_state_finish;
1035 state = json_tokener_state_eatws;
1040 case json_tokener_state_array_after_sep:
1041 case json_tokener_state_array:
1044 // Minimize memory usage; assume parsed objs are unlikely to be changed
1045 json_object_array_shrink(current, 0);
1047 if (state == json_tokener_state_array_after_sep &&
1048 (tok->flags & JSON_TOKENER_STRICT))
1050 tok->err = json_tokener_error_parse_unexpected;
1053 saved_state = json_tokener_state_finish;
1054 state = json_tokener_state_eatws;
1058 if (tok->depth >= tok->max_depth - 1)
1060 tok->err = json_tokener_error_depth;
1063 state = json_tokener_state_array_add;
1065 json_tokener_reset_level(tok, tok->depth);
1070 case json_tokener_state_array_add:
1071 if (json_object_array_add(current, obj) != 0)
1073 saved_state = json_tokener_state_array_sep;
1074 state = json_tokener_state_eatws;
1077 case json_tokener_state_array_sep:
1080 // Minimize memory usage; assume parsed objs are unlikely to be changed
1081 json_object_array_shrink(current, 0);
1083 saved_state = json_tokener_state_finish;
1084 state = json_tokener_state_eatws;
1088 saved_state = json_tokener_state_array_after_sep;
1089 state = json_tokener_state_eatws;
1093 tok->err = json_tokener_error_parse_array;
1098 case json_tokener_state_object_field_start:
1099 case json_tokener_state_object_field_start_after_sep:
1102 if (state == json_tokener_state_object_field_start_after_sep &&
1103 (tok->flags & JSON_TOKENER_STRICT))
1105 tok->err = json_tokener_error_parse_unexpected;
1108 saved_state = json_tokener_state_finish;
1109 state = json_tokener_state_eatws;
1111 else if (c == '"' || c == '\'')
1113 tok->quote_char = c;
1114 printbuf_reset(tok->pb);
1115 state = json_tokener_state_object_field;
1119 tok->err = json_tokener_error_parse_object_key_name;
1124 case json_tokener_state_object_field:
1126 /* Advance until we change state */
1127 const char *case_start = str;
1130 if (c == tok->quote_char)
1132 printbuf_memappend_fast(tok->pb, case_start,
1134 obj_field_name = strdup(tok->pb->buf);
1135 saved_state = json_tokener_state_object_field_end;
1136 state = json_tokener_state_eatws;
1141 printbuf_memappend_fast(tok->pb, case_start,
1143 saved_state = json_tokener_state_object_field;
1144 state = json_tokener_state_string_escape;
1147 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
1149 printbuf_memappend_fast(tok->pb, case_start,
1157 case json_tokener_state_object_field_end:
1160 saved_state = json_tokener_state_object_value;
1161 state = json_tokener_state_eatws;
1165 tok->err = json_tokener_error_parse_object_key_sep;
1170 case json_tokener_state_object_value:
1171 if (tok->depth >= tok->max_depth - 1)
1173 tok->err = json_tokener_error_depth;
1176 state = json_tokener_state_object_value_add;
1178 json_tokener_reset_level(tok, tok->depth);
1181 case json_tokener_state_object_value_add:
1182 json_object_object_add(current, obj_field_name, obj);
1183 free(obj_field_name);
1184 obj_field_name = NULL;
1185 saved_state = json_tokener_state_object_sep;
1186 state = json_tokener_state_eatws;
1189 case json_tokener_state_object_sep:
1193 saved_state = json_tokener_state_finish;
1194 state = json_tokener_state_eatws;
1198 saved_state = json_tokener_state_object_field_start_after_sep;
1199 state = json_tokener_state_eatws;
1203 tok->err = json_tokener_error_parse_object_value_sep;
1208 (void)ADVANCE_CHAR(str, tok);
1209 if (!c) // This is the char *before* advancing
1211 } /* while(PEEK_CHAR) */
1214 if ((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && (nBytes != 0))
1216 tok->err = json_tokener_error_parse_utf8_string;
1218 if (c && (state == json_tokener_state_finish) && (tok->depth == 0) &&
1219 (tok->flags & (JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS)) ==
1220 JSON_TOKENER_STRICT)
1222 /* unexpected char after JSON data */
1223 tok->err = json_tokener_error_parse_unexpected;
1227 /* We hit an eof char (0) */
1228 if (state != json_tokener_state_finish && saved_state != json_tokener_state_finish)
1229 tok->err = json_tokener_error_parse_eof;
1232 #ifdef HAVE_USELOCALE
1233 uselocale(oldlocale);
1235 #elif defined(HAVE_SETLOCALE)
1236 setlocale(LC_NUMERIC, oldlocale);
1240 if (tok->err == json_tokener_success)
1242 json_object *ret = json_object_get(current);
1245 /* Partially reset, so we parse additional objects on subsequent calls. */
1246 for (ii = tok->depth; ii >= 0; ii--)
1247 json_tokener_reset_level(tok, ii);
1251 MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", json_tokener_errors[tok->err],
1256 static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes)
1258 unsigned char chr = c;
1263 if ((chr & 0xe0) == 0xc0)
1265 else if ((chr & 0xf0) == 0xe0)
1267 else if ((chr & 0xf8) == 0xf0)
1275 if ((chr & 0xC0) != 0x80)
1282 void json_tokener_set_flags(struct json_tokener *tok, int flags)
1287 size_t json_tokener_get_parse_end(struct json_tokener *tok)
1289 assert(tok->char_offset >= 0); /* Drop this line when char_offset becomes a size_t */
1290 return (size_t)tok->char_offset;
1293 static int json_tokener_parse_double(const char *buf, int len, double *retval)
1296 *retval = strtod(buf, &end);
1297 if (buf + len == end)
1298 return 0; // It worked