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 * (http://www.opensource.org/licenses/mit-license.php)
19 #include "math_compat.h"
29 #include "arraylist.h"
30 #include "json_inttypes.h"
31 #include "json_object.h"
32 #include "json_object_private.h"
33 #include "json_tokener.h"
34 #include "json_util.h"
35 #include "strdup_compat.h"
39 #endif /* HAVE_LOCALE_H */
44 #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
46 #if !HAVE_STRNCASECMP && defined(_MSC_VER)
47 /* MSC has the version as _strnicmp */
48 # define strncasecmp _strnicmp
49 #elif !HAVE_STRNCASECMP
50 # error You do not have strncasecmp on your system.
51 #endif /* HAVE_STRNCASECMP */
53 /* Use C99 NAN by default; if not available, nan("") should work too. */
58 static const char json_null_str[] = "null";
59 static const int json_null_str_len = sizeof(json_null_str) - 1;
60 static const char json_inf_str[] = "Infinity";
61 static const char json_inf_str_lower[] = "infinity";
62 static const unsigned int json_inf_str_len = sizeof(json_inf_str) - 1;
63 static const char json_nan_str[] = "NaN";
64 static const int json_nan_str_len = sizeof(json_nan_str) - 1;
65 static const char json_true_str[] = "true";
66 static const int json_true_str_len = sizeof(json_true_str) - 1;
67 static const char json_false_str[] = "false";
68 static const int json_false_str_len = sizeof(json_false_str) - 1;
70 static const char* json_tokener_errors[] = {
74 "unexpected end of data",
75 "unexpected character",
79 "array value separator ',' expected",
80 "quoted object property name expected",
81 "object property name separator ':' expected",
82 "object value separator ',' expected",
83 "invalid string sequence",
85 "buffer size overflow"
88 const char *json_tokener_error_desc(enum json_tokener_error jerr)
90 int jerr_int = (int) jerr;
92 jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
93 return "Unknown error, "
94 "invalid json_tokener_error value passed to json_tokener_error_desc()";
95 return json_tokener_errors[jerr];
98 enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
103 /* Stuff for decoding unicode sequences */
104 #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
105 #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
106 #define DECODE_SURROGATE_PAIR(hi,lo) ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000)
107 static unsigned char utf8_replacement_char[3] = { 0xEF, 0xBF, 0xBD };
109 struct json_tokener* json_tokener_new_ex(int depth)
111 struct json_tokener *tok;
113 tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
114 if (!tok) return NULL;
115 tok->stack = (struct json_tokener_srec *) calloc(depth,
116 sizeof(struct json_tokener_srec));
121 tok->pb = printbuf_new();
122 tok->max_depth = depth;
123 json_tokener_reset(tok);
127 struct json_tokener* json_tokener_new(void)
129 return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
132 void json_tokener_free(struct json_tokener *tok)
134 json_tokener_reset(tok);
135 if (tok->pb) printbuf_free(tok->pb);
140 static void json_tokener_reset_level(struct json_tokener *tok, int depth)
142 tok->stack[depth].state = json_tokener_state_eatws;
143 tok->stack[depth].saved_state = json_tokener_state_start;
144 json_object_put(tok->stack[depth].current);
145 tok->stack[depth].current = NULL;
146 free(tok->stack[depth].obj_field_name);
147 tok->stack[depth].obj_field_name = NULL;
150 void json_tokener_reset(struct json_tokener *tok)
156 for(i = tok->depth; i >= 0; i--)
157 json_tokener_reset_level(tok, i);
159 tok->err = json_tokener_success;
162 struct json_object* json_tokener_parse(const char *str)
164 enum json_tokener_error jerr_ignored;
165 struct json_object* obj;
166 obj = json_tokener_parse_verbose(str, &jerr_ignored);
170 struct json_object* json_tokener_parse_verbose(const char *str,
171 enum json_tokener_error *error)
173 struct json_tokener* tok;
174 struct json_object* obj;
176 tok = json_tokener_new();
179 obj = json_tokener_parse_ex(tok, str, -1);
181 if(tok->err != json_tokener_success) {
183 json_object_put(obj);
187 json_tokener_free(tok);
191 #define state tok->stack[tok->depth].state
192 #define saved_state tok->stack[tok->depth].saved_state
193 #define current tok->stack[tok->depth].current
194 #define obj_field_name tok->stack[tok->depth].obj_field_name
197 * json_tokener_parse_ex() consumed a lot of CPU in its main loop,
198 * iterating character-by character. A large performance boost is
199 * achieved by using tighter loops to locally handle units such as
200 * comments and strings. Loops that handle an entire token within
201 * their scope also gather entire strings and pass them to
202 * printbuf_memappend() in a single call, rather than calling
203 * printbuf_memappend() one char at a time.
205 * PEEK_CHAR() and ADVANCE_CHAR() macros are used for code that is
206 * common to both the main loop and the tighter loops.
209 /* PEEK_CHAR(dest, tok) macro:
210 * Peeks at the current char and stores it in dest.
211 * Returns 1 on success, sets tok->err and returns 0 if no more chars.
212 * Implicit inputs: str, len vars
214 #define PEEK_CHAR(dest, tok) \
215 (((tok)->char_offset == len) ? \
216 (((tok)->depth == 0 && \
217 state == json_tokener_state_eatws && \
218 saved_state == json_tokener_state_finish \
220 (((tok)->err = json_tokener_success), 0) \
222 (((tok)->err = json_tokener_continue), 0) \
224 (((dest) = *str), 1) \
227 /* ADVANCE_CHAR() macro:
228 * Incrementes str & tok->char_offset.
229 * For convenience of existing conditionals, returns the old value of c (0 on eof)
230 * Implicit inputs: c var
232 #define ADVANCE_CHAR(str, tok) \
233 ( ++(str), ((tok)->char_offset)++, c)
236 /* End optimization macro defs */
239 struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
240 const char *str, int len)
242 struct json_object *obj = NULL;
244 #ifdef HAVE_USELOCALE
245 locale_t oldlocale = uselocale(NULL);
247 #elif defined(HAVE_SETLOCALE)
248 char *oldlocale = NULL;
251 tok->char_offset = 0;
252 tok->err = json_tokener_success;
254 /* this interface is presently not 64-bit clean due to the int len argument
255 and the internal printbuf interface that takes 32-bit int len arguments
256 so the function limits the maximum string size to INT32_MAX (2GB).
257 If the function is called with len == -1 then strlen is called to check
258 the string length is less than INT32_MAX (2GB) */
259 if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX)) {
260 tok->err = json_tokener_error_size;
264 #ifdef HAVE_USELOCALE
266 locale_t duploc = duplocale(oldlocale);
267 newloc = newlocale(LC_NUMERIC, "C", duploc);
268 // XXX at least Debian 8.4 has a bug in newlocale where it doesn't
269 // change the decimal separator unless you set LC_TIME!
272 duploc = newloc; // original duploc has been freed by newlocale()
273 newloc = newlocale(LC_TIME, "C", duploc);
282 #elif defined(HAVE_SETLOCALE)
285 tmplocale = setlocale(LC_NUMERIC, NULL);
286 if (tmplocale) oldlocale = strdup(tmplocale);
287 setlocale(LC_NUMERIC, "C");
291 while (PEEK_CHAR(c, tok)) {
296 case json_tokener_state_eatws:
297 /* Advance until we change state */
298 while (isspace((int)c)) {
299 if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
302 if(c == '/' && !(tok->flags & JSON_TOKENER_STRICT)) {
303 printbuf_reset(tok->pb);
304 printbuf_memappend_fast(tok->pb, &c, 1);
305 state = json_tokener_state_comment_start;
312 case json_tokener_state_start:
315 state = json_tokener_state_eatws;
316 saved_state = json_tokener_state_object_field_start;
317 current = json_object_new_object();
322 state = json_tokener_state_eatws;
323 saved_state = json_tokener_state_array;
324 current = json_object_new_array();
330 state = json_tokener_state_inf;
331 printbuf_reset(tok->pb);
336 state = json_tokener_state_null; // or NaN
337 printbuf_reset(tok->pb);
341 if (tok->flags & JSON_TOKENER_STRICT) {
342 /* in STRICT mode only double-quote are allowed */
343 tok->err = json_tokener_error_parse_unexpected;
348 state = json_tokener_state_string;
349 printbuf_reset(tok->pb);
356 state = json_tokener_state_boolean;
357 printbuf_reset(tok->pb);
371 state = json_tokener_state_number;
372 printbuf_reset(tok->pb);
376 tok->err = json_tokener_error_parse_unexpected;
381 case json_tokener_state_finish:
382 if(tok->depth == 0) goto out;
383 obj = json_object_get(current);
384 json_tokener_reset_level(tok, tok->depth);
388 case json_tokener_state_inf: /* aka starts with 'i' (or 'I', or "-i", or "-I") */
390 /* If we were guaranteed to have len set, then we could (usually) handle
391 * the entire "Infinity" check in a single strncmp (strncasecmp), but
392 * since len might be -1 (i.e. "read until \0"), we need to check it
393 * a character at a time.
394 * Trying to handle it both ways would make this code considerably more
395 * complicated with likely little performance benefit.
398 const char *_json_inf_str = json_inf_str;
399 if (!(tok->flags & JSON_TOKENER_STRICT))
400 _json_inf_str = json_inf_str_lower;
402 /* Note: tok->st_pos must be 0 when state is set to json_tokener_state_inf */
403 while (tok->st_pos < (int)json_inf_str_len)
405 char inf_char = *str;
406 if (!(tok->flags & JSON_TOKENER_STRICT))
407 inf_char = tolower((int)*str);
408 if (inf_char != _json_inf_str[tok->st_pos])
410 tok->err = json_tokener_error_parse_unexpected;
414 (void)ADVANCE_CHAR(str, tok);
415 if (!PEEK_CHAR(c, tok))
417 /* out of input chars, for now at least */
421 /* We checked the full length of "Infinity", so create the object.
422 * When handling -Infinity, the number parsing code will have dropped
423 * the "-" into tok->pb for us, so check it now.
425 if (printbuf_length(tok->pb) > 0 && *(tok->pb->buf) == '-')
429 current = json_object_new_double(is_negative
430 ? -INFINITY : INFINITY);
433 saved_state = json_tokener_state_finish;
434 state = json_tokener_state_eatws;
439 case json_tokener_state_null: /* aka starts with 'n' */
443 printbuf_memappend_fast(tok->pb, &c, 1);
444 size = json_min(tok->st_pos+1, json_null_str_len);
445 size_nan = json_min(tok->st_pos+1, json_nan_str_len);
446 if((!(tok->flags & JSON_TOKENER_STRICT) &&
447 strncasecmp(json_null_str, tok->pb->buf, size) == 0)
448 || (strncmp(json_null_str, tok->pb->buf, size) == 0)
450 if (tok->st_pos == json_null_str_len) {
452 saved_state = json_tokener_state_finish;
453 state = json_tokener_state_eatws;
457 else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
458 strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) ||
459 (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0)
462 if (tok->st_pos == json_nan_str_len)
464 current = json_object_new_double(NAN);
467 saved_state = json_tokener_state_finish;
468 state = json_tokener_state_eatws;
472 tok->err = json_tokener_error_parse_null;
479 case json_tokener_state_comment_start:
481 state = json_tokener_state_comment;
482 } else if(c == '/') {
483 state = json_tokener_state_comment_eol;
485 tok->err = json_tokener_error_parse_comment;
488 printbuf_memappend_fast(tok->pb, &c, 1);
491 case json_tokener_state_comment:
493 /* Advance until we change state */
494 const char *case_start = str;
496 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
497 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
501 printbuf_memappend_fast(tok->pb, case_start, 1+str-case_start);
502 state = json_tokener_state_comment_end;
506 case json_tokener_state_comment_eol:
508 /* Advance until we change state */
509 const char *case_start = str;
511 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
512 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
516 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
517 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
518 state = json_tokener_state_eatws;
522 case json_tokener_state_comment_end:
523 printbuf_memappend_fast(tok->pb, &c, 1);
525 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
526 state = json_tokener_state_eatws;
528 state = json_tokener_state_comment;
532 case json_tokener_state_string:
534 /* Advance until we change state */
535 const char *case_start = str;
537 if(c == tok->quote_char) {
538 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
539 current = json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
542 saved_state = json_tokener_state_finish;
543 state = json_tokener_state_eatws;
545 } else if(c == '\\') {
546 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
547 saved_state = json_tokener_state_string;
548 state = json_tokener_state_string_escape;
551 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
552 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
559 case json_tokener_state_string_escape:
564 printbuf_memappend_fast(tok->pb, &c, 1);
572 if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1);
573 else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1);
574 else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1);
575 else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1);
576 else if(c == 'f') printbuf_memappend_fast(tok->pb, "\f", 1);
582 state = json_tokener_state_escape_unicode;
585 tok->err = json_tokener_error_parse_string;
590 case json_tokener_state_escape_unicode:
592 unsigned int got_hi_surrogate = 0;
594 /* Handle a 4-byte sequence, or two sequences if a surrogate pair */
596 if (c && strchr(json_hex_chars, c)) {
597 tok->ucs_char += ((unsigned int)jt_hexdigit(c) << ((3-tok->st_pos++)*4));
598 if(tok->st_pos == 4) {
599 unsigned char unescaped_utf[4];
601 if (got_hi_surrogate) {
602 if (IS_LOW_SURROGATE(tok->ucs_char)) {
603 /* Recalculate the ucs_char, then fall thru to process normally */
604 tok->ucs_char = DECODE_SURROGATE_PAIR(got_hi_surrogate, tok->ucs_char);
606 /* Hi surrogate was not followed by a low surrogate */
607 /* Replace the hi and process the rest normally */
608 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
610 got_hi_surrogate = 0;
613 if (tok->ucs_char < 0x80) {
614 unescaped_utf[0] = tok->ucs_char;
615 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 1);
616 } else if (tok->ucs_char < 0x800) {
617 unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
618 unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
619 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 2);
620 } else if (IS_HIGH_SURROGATE(tok->ucs_char)) {
621 /* Got a high surrogate. Remember it and look for the
622 * the beginning of another sequence, which should be the
625 got_hi_surrogate = tok->ucs_char;
626 /* Not at end, and the next two chars should be "\u" */
627 if ((len == -1 || len > (tok->char_offset + 2)) &&
628 // str[0] != '0' && // implied by json_hex_chars, above.
632 /* Advance through the 16 bit surrogate, and move on to the
633 * next sequence. The next step is to process the following
636 if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) {
637 printbuf_memappend_fast(tok->pb,
638 (char*) utf8_replacement_char, 3);
640 /* Advance to the first char of the next sequence and
641 * continue processing with the next sequence.
643 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
644 printbuf_memappend_fast(tok->pb,
645 (char*) utf8_replacement_char, 3);
650 continue; /* other json_tokener_state_escape_unicode */
652 /* Got a high surrogate without another sequence following
653 * it. Put a replacement char in for the hi surrogate
654 * and pretend we finished.
656 printbuf_memappend_fast(tok->pb,
657 (char*) utf8_replacement_char, 3);
659 } else if (IS_LOW_SURROGATE(tok->ucs_char)) {
660 /* Got a low surrogate not preceded by a high */
661 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
662 } else if (tok->ucs_char < 0x10000) {
663 unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
664 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
665 unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
666 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 3);
667 } else if (tok->ucs_char < 0x110000) {
668 unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07);
669 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
670 unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
671 unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
672 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 4);
674 /* Don't know what we got--insert the replacement char */
675 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
681 tok->err = json_tokener_error_parse_string;
684 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
685 if (got_hi_surrogate) /* Clean up any pending chars */
686 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
693 case json_tokener_state_boolean:
696 printbuf_memappend_fast(tok->pb, &c, 1);
697 size1 = json_min(tok->st_pos+1, json_true_str_len);
698 size2 = json_min(tok->st_pos+1, json_false_str_len);
699 if((!(tok->flags & JSON_TOKENER_STRICT) &&
700 strncasecmp(json_true_str, tok->pb->buf, size1) == 0)
701 || (strncmp(json_true_str, tok->pb->buf, size1) == 0)
703 if(tok->st_pos == json_true_str_len) {
704 current = json_object_new_boolean(1);
707 saved_state = json_tokener_state_finish;
708 state = json_tokener_state_eatws;
711 } else if((!(tok->flags & JSON_TOKENER_STRICT) &&
712 strncasecmp(json_false_str, tok->pb->buf, size2) == 0)
713 || (strncmp(json_false_str, tok->pb->buf, size2) == 0)) {
714 if(tok->st_pos == json_false_str_len) {
715 current = json_object_new_boolean(0);
718 saved_state = json_tokener_state_finish;
719 state = json_tokener_state_eatws;
723 tok->err = json_tokener_error_parse_boolean;
730 case json_tokener_state_number:
732 /* Advance until we change state */
733 const char *case_start = str;
736 int negativesign_next_possible_location=1;
737 while(c && strchr(json_number_chars, c)) {
740 /* non-digit characters checks */
741 /* note: since the main loop condition to get here was
742 an input starting with 0-9 or '-', we are
743 protected from input starting with '.' or
746 if (tok->is_double != 0) {
747 /* '.' can only be found once, and out of the exponent part.
748 Thus, if the input is already flagged as double, it
750 tok->err = json_tokener_error_parse_number;
755 if (c == 'e' || c == 'E') {
756 if (is_exponent != 0) {
757 /* only one exponent possible */
758 tok->err = json_tokener_error_parse_number;
763 /* the exponent part can begin with a negative sign */
764 negativesign_next_possible_location = case_len + 1;
766 if (c == '-' && case_len != negativesign_next_possible_location) {
767 /* If the negative sign is not where expected (ie
768 start of input or start of exponent part), the
770 tok->err = json_tokener_error_parse_number;
774 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
775 printbuf_memappend_fast(tok->pb, case_start, case_len);
780 printbuf_memappend_fast(tok->pb, case_start, case_len);
782 // Check for -Infinity
783 if (tok->pb->buf[0] == '-' && case_len <= 1 &&
784 (c == 'i' || c == 'I'))
786 state = json_tokener_state_inf;
794 if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
795 if (num64 && tok->pb->buf[0]=='0' &&
796 (tok->flags & JSON_TOKENER_STRICT)) {
797 /* in strict mode, number must not start with 0 */
798 tok->err = json_tokener_error_parse_number;
801 current = json_object_new_int64(num64);
805 else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0)
807 current = json_object_new_double_s(numd, tok->pb->buf);
811 tok->err = json_tokener_error_parse_number;
814 saved_state = json_tokener_state_finish;
815 state = json_tokener_state_eatws;
820 case json_tokener_state_array_after_sep:
821 case json_tokener_state_array:
823 if (state == json_tokener_state_array_after_sep &&
824 (tok->flags & JSON_TOKENER_STRICT))
826 tok->err = json_tokener_error_parse_unexpected;
829 saved_state = json_tokener_state_finish;
830 state = json_tokener_state_eatws;
832 if(tok->depth >= tok->max_depth-1) {
833 tok->err = json_tokener_error_depth;
836 state = json_tokener_state_array_add;
838 json_tokener_reset_level(tok, tok->depth);
843 case json_tokener_state_array_add:
844 if( json_object_array_add(current, obj) != 0 )
846 saved_state = json_tokener_state_array_sep;
847 state = json_tokener_state_eatws;
850 case json_tokener_state_array_sep:
852 saved_state = json_tokener_state_finish;
853 state = json_tokener_state_eatws;
854 } else if(c == ',') {
855 saved_state = json_tokener_state_array_after_sep;
856 state = json_tokener_state_eatws;
858 tok->err = json_tokener_error_parse_array;
863 case json_tokener_state_object_field_start:
864 case json_tokener_state_object_field_start_after_sep:
866 if (state == json_tokener_state_object_field_start_after_sep &&
867 (tok->flags & JSON_TOKENER_STRICT))
869 tok->err = json_tokener_error_parse_unexpected;
872 saved_state = json_tokener_state_finish;
873 state = json_tokener_state_eatws;
874 } else if (c == '"' || c == '\'') {
876 printbuf_reset(tok->pb);
877 state = json_tokener_state_object_field;
879 tok->err = json_tokener_error_parse_object_key_name;
884 case json_tokener_state_object_field:
886 /* Advance until we change state */
887 const char *case_start = str;
889 if(c == tok->quote_char) {
890 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
891 obj_field_name = strdup(tok->pb->buf);
892 saved_state = json_tokener_state_object_field_end;
893 state = json_tokener_state_eatws;
895 } else if(c == '\\') {
896 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
897 saved_state = json_tokener_state_object_field;
898 state = json_tokener_state_string_escape;
901 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
902 printbuf_memappend_fast(tok->pb, case_start, str-case_start);
909 case json_tokener_state_object_field_end:
911 saved_state = json_tokener_state_object_value;
912 state = json_tokener_state_eatws;
914 tok->err = json_tokener_error_parse_object_key_sep;
919 case json_tokener_state_object_value:
920 if(tok->depth >= tok->max_depth-1) {
921 tok->err = json_tokener_error_depth;
924 state = json_tokener_state_object_value_add;
926 json_tokener_reset_level(tok, tok->depth);
929 case json_tokener_state_object_value_add:
930 json_object_object_add(current, obj_field_name, obj);
931 free(obj_field_name);
932 obj_field_name = NULL;
933 saved_state = json_tokener_state_object_sep;
934 state = json_tokener_state_eatws;
937 case json_tokener_state_object_sep:
940 saved_state = json_tokener_state_finish;
941 state = json_tokener_state_eatws;
942 } else if(c == ',') {
943 saved_state = json_tokener_state_object_field_start_after_sep;
944 state = json_tokener_state_eatws;
946 tok->err = json_tokener_error_parse_object_value_sep;
952 if (!ADVANCE_CHAR(str, tok))
954 } /* while(PEEK_CHAR) */
958 (state == json_tokener_state_finish) &&
960 (tok->flags & JSON_TOKENER_STRICT)) {
961 /* unexpected char after JSON data */
962 tok->err = json_tokener_error_parse_unexpected;
964 if (!c) { /* We hit an eof char (0) */
965 if(state != json_tokener_state_finish &&
966 saved_state != json_tokener_state_finish)
967 tok->err = json_tokener_error_parse_eof;
970 #ifdef HAVE_USELOCALE
971 uselocale(oldlocale);
973 #elif defined(HAVE_SETLOCALE)
974 setlocale(LC_NUMERIC, oldlocale);
978 if (tok->err == json_tokener_success)
980 json_object *ret = json_object_get(current);
983 /* Partially reset, so we parse additional objects on subsequent calls. */
984 for(ii = tok->depth; ii >= 0; ii--)
985 json_tokener_reset_level(tok, ii);
989 MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n",
990 json_tokener_errors[tok->err], tok->char_offset);
994 void json_tokener_set_flags(struct json_tokener *tok, int flags)