Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 2 | * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 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. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 10 | * |
| 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) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include <string.h> |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 23 | #include <limits.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 24 | |
| 25 | #include "bits.h" |
| 26 | #include "debug.h" |
| 27 | #include "printbuf.h" |
| 28 | #include "arraylist.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 29 | #include "json_inttypes.h" |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 30 | #include "json_object.h" |
| 31 | #include "json_tokener.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 32 | #include "json_util.h" |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 33 | |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 34 | #if !HAVE_STRNCASECMP && defined(_MSC_VER) |
| 35 | /* MSC has the version as _strnicmp */ |
| 36 | # define strncasecmp _strnicmp |
| 37 | #elif !HAVE_STRNCASECMP |
| 38 | # error You do not have strncasecmp on your system. |
| 39 | #endif /* HAVE_STRNCASECMP */ |
| 40 | |
| 41 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 42 | static const char* json_null_str = "null"; |
| 43 | static const char* json_true_str = "true"; |
| 44 | static const char* json_false_str = "false"; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 45 | |
Eric Haszlakiewicz | 2f9091f | 2012-02-22 08:24:40 -0600 | [diff] [blame] | 46 | // XXX after v0.10 this array will become static: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 47 | const char* json_tokener_errors[] = { |
| 48 | "success", |
| 49 | "continue", |
Eric Haszlakiewicz | 3620cba | 2012-03-31 12:52:59 -0500 | [diff] [blame] | 50 | "nesting too deep", |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 51 | "unexpected end of data", |
| 52 | "unexpected character", |
| 53 | "null expected", |
| 54 | "boolean expected", |
| 55 | "number expected", |
| 56 | "array value separator ',' expected", |
| 57 | "quoted object property name expected", |
| 58 | "object property name separator ':' expected", |
| 59 | "object value separator ',' expected", |
| 60 | "invalid string sequence", |
| 61 | "expected comment", |
| 62 | }; |
| 63 | |
Eric Haszlakiewicz | 2f9091f | 2012-02-22 08:24:40 -0600 | [diff] [blame] | 64 | const char *json_tokener_error_desc(enum json_tokener_error jerr) |
| 65 | { |
| 66 | if (jerr < 0 || jerr > sizeof(json_tokener_errors)) |
| 67 | return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()"; |
| 68 | return json_tokener_errors[jerr]; |
| 69 | } |
| 70 | |
| 71 | enum json_tokener_error json_tokener_get_error(json_tokener *tok) |
| 72 | { |
| 73 | return tok->err; |
| 74 | } |
| 75 | |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 76 | /* Stuff for decoding unicode sequences */ |
| 77 | #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800) |
| 78 | #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00) |
| 79 | #define DECODE_SURROGATE_PAIR(hi,lo) ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000) |
| 80 | static unsigned char utf8_replacement_char[3] = { 0xEF, 0xBF, 0xBD }; |
| 81 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 82 | |
Michael Clark | e8de078 | 2009-02-25 01:45:00 +0000 | [diff] [blame] | 83 | struct json_tokener* json_tokener_new(void) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 84 | { |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 85 | struct json_tokener *tok; |
| 86 | |
| 87 | tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener)); |
Christopher Watford | 543bb14 | 2009-07-08 03:46:10 +0000 | [diff] [blame] | 88 | if (!tok) return NULL; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 89 | tok->pb = printbuf_new(); |
| 90 | json_tokener_reset(tok); |
| 91 | return tok; |
| 92 | } |
| 93 | |
| 94 | void json_tokener_free(struct json_tokener *tok) |
| 95 | { |
| 96 | json_tokener_reset(tok); |
| 97 | if(tok) printbuf_free(tok->pb); |
| 98 | free(tok); |
| 99 | } |
| 100 | |
| 101 | static void json_tokener_reset_level(struct json_tokener *tok, int depth) |
| 102 | { |
| 103 | tok->stack[depth].state = json_tokener_state_eatws; |
| 104 | tok->stack[depth].saved_state = json_tokener_state_start; |
| 105 | json_object_put(tok->stack[depth].current); |
| 106 | tok->stack[depth].current = NULL; |
| 107 | free(tok->stack[depth].obj_field_name); |
| 108 | tok->stack[depth].obj_field_name = NULL; |
| 109 | } |
| 110 | |
| 111 | void json_tokener_reset(struct json_tokener *tok) |
| 112 | { |
| 113 | int i; |
Michael Clark | 22dee7c | 2009-02-25 01:51:40 +0000 | [diff] [blame] | 114 | if (!tok) |
| 115 | return; |
| 116 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 117 | for(i = tok->depth; i >= 0; i--) |
| 118 | json_tokener_reset_level(tok, i); |
| 119 | tok->depth = 0; |
| 120 | tok->err = json_tokener_success; |
| 121 | } |
| 122 | |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 123 | struct json_object* json_tokener_parse(const char *str) |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 124 | { |
Eric Haszlakiewicz | 3620cba | 2012-03-31 12:52:59 -0500 | [diff] [blame] | 125 | enum json_tokener_error jerr_ignored; |
| 126 | struct json_object* obj; |
| 127 | obj = json_tokener_parse_verbose(str, &jerr_ignored); |
| 128 | return obj; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 131 | struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) |
| 132 | { |
| 133 | struct json_tokener* tok; |
| 134 | struct json_object* obj; |
| 135 | |
| 136 | tok = json_tokener_new(); |
Eric Haszlakiewicz | 3620cba | 2012-03-31 12:52:59 -0500 | [diff] [blame] | 137 | if (!tok) |
| 138 | return NULL; |
Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 139 | obj = json_tokener_parse_ex(tok, str, -1); |
| 140 | *error = tok->err; |
| 141 | if(tok->err != json_tokener_success) { |
Eric Haszlakiewicz | 3620cba | 2012-03-31 12:52:59 -0500 | [diff] [blame] | 142 | if (obj != NULL) |
| 143 | json_object_put(obj); |
Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 144 | obj = NULL; |
| 145 | } |
| 146 | |
| 147 | json_tokener_free(tok); |
| 148 | return obj; |
| 149 | } |
| 150 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 151 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 152 | #if !HAVE_STRNDUP |
| 153 | /* CAW: compliant version of strndup() */ |
| 154 | char* strndup(const char* str, size_t n) |
| 155 | { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 156 | if(str) { |
| 157 | size_t len = strlen(str); |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 158 | size_t nn = json_min(len,n); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 159 | char* s = (char*)malloc(sizeof(char) * (nn + 1)); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 160 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 161 | if(s) { |
| 162 | memcpy(s, str, nn); |
| 163 | s[nn] = '\0'; |
| 164 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 165 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 166 | return s; |
| 167 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 168 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 169 | return NULL; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 170 | } |
| 171 | #endif |
| 172 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 173 | |
| 174 | #define state tok->stack[tok->depth].state |
| 175 | #define saved_state tok->stack[tok->depth].saved_state |
| 176 | #define current tok->stack[tok->depth].current |
| 177 | #define obj_field_name tok->stack[tok->depth].obj_field_name |
| 178 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 179 | /* Optimization: |
| 180 | * json_tokener_parse_ex() consumed a lot of CPU in its main loop, |
| 181 | * iterating character-by character. A large performance boost is |
| 182 | * achieved by using tighter loops to locally handle units such as |
| 183 | * comments and strings. Loops that handle an entire token within |
| 184 | * their scope also gather entire strings and pass them to |
| 185 | * printbuf_memappend() in a single call, rather than calling |
| 186 | * printbuf_memappend() one char at a time. |
| 187 | * |
| 188 | * POP_CHAR() and ADVANCE_CHAR() macros are used for code that is |
| 189 | * common to both the main loop and the tighter loops. |
| 190 | */ |
| 191 | |
| 192 | /* POP_CHAR(dest, tok) macro: |
| 193 | * Not really a pop()...peeks at the current char and stores it in dest. |
| 194 | * Returns 1 on success, sets tok->err and returns 0 if no more chars. |
| 195 | * Implicit inputs: str, len vars |
| 196 | */ |
| 197 | #define POP_CHAR(dest, tok) \ |
| 198 | (((tok)->char_offset == len) ? \ |
| 199 | (((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \ |
| 200 | (((tok)->err = json_tokener_success), 0) \ |
| 201 | : \ |
| 202 | (((tok)->err = json_tokener_continue), 0) \ |
| 203 | ) : \ |
| 204 | (((dest) = *str), 1) \ |
| 205 | ) |
| 206 | |
| 207 | /* ADVANCE_CHAR() macro: |
| 208 | * Incrementes str & tok->char_offset. |
| 209 | * For convenience of existing conditionals, returns the old value of c (0 on eof) |
| 210 | * Implicit inputs: c var |
| 211 | */ |
| 212 | #define ADVANCE_CHAR(str, tok) \ |
| 213 | ( ++(str), ((tok)->char_offset)++, c) |
| 214 | |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 215 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 216 | /* End optimization macro defs */ |
| 217 | |
| 218 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 219 | struct json_object* json_tokener_parse_ex(struct json_tokener *tok, |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 220 | const char *str, int len) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 221 | { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 222 | struct json_object *obj = NULL; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 223 | char c = '\1'; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 224 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 225 | tok->char_offset = 0; |
| 226 | tok->err = json_tokener_success; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 227 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 228 | while (POP_CHAR(c, tok)) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 229 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 230 | redo_char: |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 231 | switch(state) { |
| 232 | |
| 233 | case json_tokener_state_eatws: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 234 | /* Advance until we change state */ |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 235 | while (isspace((int)c)) { |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 236 | if ((!ADVANCE_CHAR(str, tok)) || (!POP_CHAR(c, tok))) |
| 237 | goto out; |
| 238 | } |
| 239 | if(c == '/') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 240 | printbuf_reset(tok->pb); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 241 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 242 | state = json_tokener_state_comment_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 243 | } else { |
| 244 | state = saved_state; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 245 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | |
| 249 | case json_tokener_state_start: |
| 250 | switch(c) { |
| 251 | case '{': |
| 252 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 253 | saved_state = json_tokener_state_object_field_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 254 | current = json_object_new_object(); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 255 | break; |
| 256 | case '[': |
| 257 | state = json_tokener_state_eatws; |
| 258 | saved_state = json_tokener_state_array; |
| 259 | current = json_object_new_array(); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 260 | break; |
| 261 | case 'N': |
| 262 | case 'n': |
| 263 | state = json_tokener_state_null; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 264 | printbuf_reset(tok->pb); |
| 265 | tok->st_pos = 0; |
| 266 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 267 | case '"': |
| 268 | case '\'': |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 269 | state = json_tokener_state_string; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 270 | printbuf_reset(tok->pb); |
| 271 | tok->quote_char = c; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 272 | break; |
| 273 | case 'T': |
| 274 | case 't': |
| 275 | case 'F': |
| 276 | case 'f': |
| 277 | state = json_tokener_state_boolean; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 278 | printbuf_reset(tok->pb); |
| 279 | tok->st_pos = 0; |
| 280 | goto redo_char; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 281 | #if defined(__GNUC__) |
| 282 | case '0' ... '9': |
| 283 | #else |
| 284 | case '0': |
| 285 | case '1': |
| 286 | case '2': |
| 287 | case '3': |
| 288 | case '4': |
| 289 | case '5': |
| 290 | case '6': |
| 291 | case '7': |
| 292 | case '8': |
| 293 | case '9': |
| 294 | #endif |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 295 | case '-': |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 296 | state = json_tokener_state_number; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 297 | printbuf_reset(tok->pb); |
| 298 | tok->is_double = 0; |
| 299 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 300 | default: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 301 | tok->err = json_tokener_error_parse_unexpected; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 302 | goto out; |
| 303 | } |
| 304 | break; |
| 305 | |
| 306 | case json_tokener_state_finish: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 307 | if(tok->depth == 0) goto out; |
| 308 | obj = json_object_get(current); |
| 309 | json_tokener_reset_level(tok, tok->depth); |
| 310 | tok->depth--; |
| 311 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 312 | |
| 313 | case json_tokener_state_null: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 314 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 315 | if(strncasecmp(json_null_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 316 | json_min(tok->st_pos+1, strlen(json_null_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 317 | if(tok->st_pos == strlen(json_null_str)) { |
| 318 | current = NULL; |
| 319 | saved_state = json_tokener_state_finish; |
| 320 | state = json_tokener_state_eatws; |
| 321 | goto redo_char; |
| 322 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 323 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 324 | tok->err = json_tokener_error_parse_null; |
| 325 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 326 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 327 | tok->st_pos++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | case json_tokener_state_comment_start: |
| 331 | if(c == '*') { |
| 332 | state = json_tokener_state_comment; |
| 333 | } else if(c == '/') { |
| 334 | state = json_tokener_state_comment_eol; |
| 335 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 336 | tok->err = json_tokener_error_parse_comment; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 337 | goto out; |
| 338 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 339 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 340 | break; |
| 341 | |
| 342 | case json_tokener_state_comment: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 343 | { |
| 344 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 345 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 346 | while(c != '*') { |
| 347 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 348 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 349 | goto out; |
| 350 | } |
| 351 | } |
| 352 | printbuf_memappend_fast(tok->pb, case_start, 1+str-case_start); |
| 353 | state = json_tokener_state_comment_end; |
| 354 | } |
| 355 | break; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 356 | |
| 357 | case json_tokener_state_comment_eol: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 358 | { |
| 359 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 360 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 361 | while(c != '\n') { |
| 362 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 363 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 364 | goto out; |
| 365 | } |
| 366 | } |
| 367 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 368 | MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 369 | state = json_tokener_state_eatws; |
| 370 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 371 | break; |
| 372 | |
| 373 | case json_tokener_state_comment_end: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 374 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 375 | if(c == '/') { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 376 | MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 377 | state = json_tokener_state_eatws; |
| 378 | } else { |
| 379 | state = json_tokener_state_comment; |
| 380 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | case json_tokener_state_string: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 384 | { |
| 385 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 386 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 387 | while(1) { |
| 388 | if(c == tok->quote_char) { |
| 389 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 390 | current = json_object_new_string(tok->pb->buf); |
| 391 | saved_state = json_tokener_state_finish; |
| 392 | state = json_tokener_state_eatws; |
| 393 | break; |
| 394 | } else if(c == '\\') { |
| 395 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 396 | saved_state = json_tokener_state_string; |
| 397 | state = json_tokener_state_string_escape; |
| 398 | break; |
| 399 | } |
| 400 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 401 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 402 | goto out; |
| 403 | } |
| 404 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 405 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case json_tokener_state_string_escape: |
| 409 | switch(c) { |
| 410 | case '"': |
| 411 | case '\\': |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 412 | case '/': |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 413 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 414 | state = saved_state; |
| 415 | break; |
| 416 | case 'b': |
| 417 | case 'n': |
| 418 | case 'r': |
| 419 | case 't': |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 420 | if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1); |
| 421 | else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1); |
| 422 | else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1); |
| 423 | else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 424 | state = saved_state; |
| 425 | break; |
| 426 | case 'u': |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 427 | tok->ucs_char = 0; |
| 428 | tok->st_pos = 0; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 429 | state = json_tokener_state_escape_unicode; |
| 430 | break; |
| 431 | default: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 432 | tok->err = json_tokener_error_parse_string; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 433 | goto out; |
| 434 | } |
| 435 | break; |
| 436 | |
| 437 | case json_tokener_state_escape_unicode: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 438 | { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 439 | unsigned int got_hi_surrogate = 0; |
| 440 | |
| 441 | /* Handle a 4-byte sequence, or two sequences if a surrogate pair */ |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 442 | while(1) { |
| 443 | if(strchr(json_hex_chars, c)) { |
| 444 | tok->ucs_char += ((unsigned int)hexdigit(c) << ((3-tok->st_pos++)*4)); |
| 445 | if(tok->st_pos == 4) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 446 | unsigned char unescaped_utf[4]; |
| 447 | |
| 448 | if (got_hi_surrogate) { |
| 449 | if (IS_LOW_SURROGATE(tok->ucs_char)) { |
| 450 | /* Recalculate the ucs_char, then fall thru to process normally */ |
| 451 | tok->ucs_char = DECODE_SURROGATE_PAIR(got_hi_surrogate, tok->ucs_char); |
| 452 | } else { |
| 453 | /* Hi surrogate was not followed by a low surrogate */ |
| 454 | /* Replace the hi and process the rest normally */ |
| 455 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 456 | } |
| 457 | got_hi_surrogate = 0; |
| 458 | } |
| 459 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 460 | if (tok->ucs_char < 0x80) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 461 | unescaped_utf[0] = tok->ucs_char; |
| 462 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 1); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 463 | } else if (tok->ucs_char < 0x800) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 464 | unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6); |
| 465 | unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f); |
| 466 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 2); |
| 467 | } else if (IS_HIGH_SURROGATE(tok->ucs_char)) { |
| 468 | /* Got a high surrogate. Remember it and look for the |
| 469 | * the beginning of another sequence, which should be the |
| 470 | * low surrogate. |
| 471 | */ |
| 472 | got_hi_surrogate = tok->ucs_char; |
| 473 | /* Not at end, and the next two chars should be "\u" */ |
| 474 | if ((tok->char_offset+1 != len) && |
| 475 | (tok->char_offset+2 != len) && |
| 476 | (str[1] == '\\') && |
| 477 | (str[2] == 'u')) |
| 478 | { |
| 479 | ADVANCE_CHAR(str, tok); |
| 480 | ADVANCE_CHAR(str, tok); |
| 481 | |
| 482 | /* Advance to the first char of the next sequence and |
| 483 | * continue processing with the next sequence. |
| 484 | */ |
| 485 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 486 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 487 | goto out; |
| 488 | } |
| 489 | tok->ucs_char = 0; |
| 490 | tok->st_pos = 0; |
| 491 | continue; /* other json_tokener_state_escape_unicode */ |
| 492 | } else { |
| 493 | /* Got a high surrogate without another sequence following |
| 494 | * it. Put a replacement char in for the hi surrogate |
| 495 | * and pretend we finished. |
| 496 | */ |
| 497 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 498 | } |
| 499 | } else if (IS_LOW_SURROGATE(tok->ucs_char)) { |
| 500 | /* Got a low surrogate not preceded by a high */ |
| 501 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 502 | } else if (tok->ucs_char < 0x10000) { |
| 503 | unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12); |
| 504 | unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); |
| 505 | unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f); |
| 506 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 3); |
| 507 | } else if (tok->ucs_char < 0x110000) { |
| 508 | unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07); |
| 509 | unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f); |
| 510 | unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); |
| 511 | unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f); |
| 512 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 4); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 513 | } else { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 514 | /* Don't know what we got--insert the replacement char */ |
| 515 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 516 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 517 | state = saved_state; |
| 518 | break; |
| 519 | } |
| 520 | } else { |
| 521 | tok->err = json_tokener_error_parse_string; |
| 522 | goto out; |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 523 | } |
| 524 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 525 | if (got_hi_surrogate) /* Clean up any pending chars */ |
| 526 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 527 | goto out; |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 528 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 529 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 530 | } |
| 531 | break; |
| 532 | |
| 533 | case json_tokener_state_boolean: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 534 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 535 | if(strncasecmp(json_true_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 536 | json_min(tok->st_pos+1, strlen(json_true_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 537 | if(tok->st_pos == strlen(json_true_str)) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 538 | current = json_object_new_boolean(1); |
| 539 | saved_state = json_tokener_state_finish; |
| 540 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 541 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 542 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 543 | } else if(strncasecmp(json_false_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 544 | json_min(tok->st_pos+1, strlen(json_false_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 545 | if(tok->st_pos == strlen(json_false_str)) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 546 | current = json_object_new_boolean(0); |
| 547 | saved_state = json_tokener_state_finish; |
| 548 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 549 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 550 | } |
| 551 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 552 | tok->err = json_tokener_error_parse_boolean; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 553 | goto out; |
| 554 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 555 | tok->st_pos++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | case json_tokener_state_number: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 559 | { |
| 560 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 561 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 562 | int case_len=0; |
| 563 | while(c && strchr(json_number_chars, c)) { |
| 564 | ++case_len; |
| 565 | if(c == '.' || c == 'e') tok->is_double = 1; |
| 566 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 567 | printbuf_memappend_fast(tok->pb, case_start, case_len); |
| 568 | goto out; |
| 569 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 570 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 571 | if (case_len>0) |
| 572 | printbuf_memappend_fast(tok->pb, case_start, case_len); |
| 573 | } |
| 574 | { |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 575 | int64_t num64; |
| 576 | double numd; |
| 577 | if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) { |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 578 | current = json_object_new_int64(num64); |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 579 | } else if(tok->is_double && sscanf(tok->pb->buf, "%lf", &numd) == 1) { |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 580 | current = json_object_new_double(numd); |
| 581 | } else { |
| 582 | tok->err = json_tokener_error_parse_number; |
| 583 | goto out; |
| 584 | } |
| 585 | saved_state = json_tokener_state_finish; |
| 586 | state = json_tokener_state_eatws; |
| 587 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 588 | } |
| 589 | break; |
| 590 | |
| 591 | case json_tokener_state_array: |
| 592 | if(c == ']') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 593 | saved_state = json_tokener_state_finish; |
| 594 | state = json_tokener_state_eatws; |
| 595 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 596 | if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) { |
| 597 | tok->err = json_tokener_error_depth; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 598 | goto out; |
| 599 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 600 | state = json_tokener_state_array_add; |
| 601 | tok->depth++; |
| 602 | json_tokener_reset_level(tok, tok->depth); |
| 603 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 604 | } |
| 605 | break; |
| 606 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 607 | case json_tokener_state_array_add: |
| 608 | json_object_array_add(current, obj); |
| 609 | saved_state = json_tokener_state_array_sep; |
| 610 | state = json_tokener_state_eatws; |
| 611 | goto redo_char; |
| 612 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 613 | case json_tokener_state_array_sep: |
| 614 | if(c == ']') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 615 | saved_state = json_tokener_state_finish; |
| 616 | state = json_tokener_state_eatws; |
| 617 | } else if(c == ',') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 618 | saved_state = json_tokener_state_array; |
| 619 | state = json_tokener_state_eatws; |
| 620 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 621 | tok->err = json_tokener_error_parse_array; |
| 622 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 623 | } |
| 624 | break; |
| 625 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 626 | case json_tokener_state_object_field_start: |
| 627 | if(c == '}') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 628 | saved_state = json_tokener_state_finish; |
| 629 | state = json_tokener_state_eatws; |
| 630 | } else if (c == '"' || c == '\'') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 631 | tok->quote_char = c; |
| 632 | printbuf_reset(tok->pb); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 633 | state = json_tokener_state_object_field; |
Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 634 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 635 | tok->err = json_tokener_error_parse_object_key_name; |
Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 636 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 637 | } |
| 638 | break; |
| 639 | |
| 640 | case json_tokener_state_object_field: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 641 | { |
| 642 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 643 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 644 | while(1) { |
| 645 | if(c == tok->quote_char) { |
| 646 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 647 | obj_field_name = strdup(tok->pb->buf); |
| 648 | saved_state = json_tokener_state_object_field_end; |
| 649 | state = json_tokener_state_eatws; |
| 650 | break; |
| 651 | } else if(c == '\\') { |
| 652 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 653 | saved_state = json_tokener_state_object_field; |
| 654 | state = json_tokener_state_string_escape; |
| 655 | break; |
| 656 | } |
| 657 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 658 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 659 | goto out; |
| 660 | } |
| 661 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 662 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | case json_tokener_state_object_field_end: |
| 666 | if(c == ':') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 667 | saved_state = json_tokener_state_object_value; |
| 668 | state = json_tokener_state_eatws; |
| 669 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 670 | tok->err = json_tokener_error_parse_object_key_sep; |
| 671 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 672 | } |
| 673 | break; |
| 674 | |
| 675 | case json_tokener_state_object_value: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 676 | if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) { |
| 677 | tok->err = json_tokener_error_depth; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 678 | goto out; |
| 679 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 680 | state = json_tokener_state_object_value_add; |
| 681 | tok->depth++; |
| 682 | json_tokener_reset_level(tok, tok->depth); |
| 683 | goto redo_char; |
| 684 | |
| 685 | case json_tokener_state_object_value_add: |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 686 | json_object_object_add(current, obj_field_name, obj); |
| 687 | free(obj_field_name); |
| 688 | obj_field_name = NULL; |
| 689 | saved_state = json_tokener_state_object_sep; |
| 690 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 691 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 692 | |
| 693 | case json_tokener_state_object_sep: |
| 694 | if(c == '}') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 695 | saved_state = json_tokener_state_finish; |
| 696 | state = json_tokener_state_eatws; |
| 697 | } else if(c == ',') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 698 | saved_state = json_tokener_state_object_field_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 699 | state = json_tokener_state_eatws; |
| 700 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 701 | tok->err = json_tokener_error_parse_object_value_sep; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 702 | goto out; |
| 703 | } |
| 704 | break; |
| 705 | |
| 706 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 707 | if (!ADVANCE_CHAR(str, tok)) |
| 708 | goto out; |
| 709 | } /* while(POP_CHAR) */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 710 | |
| 711 | out: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 712 | if (!c) { /* We hit an eof char (0) */ |
| 713 | if(state != json_tokener_state_finish && |
| 714 | saved_state != json_tokener_state_finish) |
| 715 | tok->err = json_tokener_error_parse_eof; |
| 716 | } |
| 717 | |
Eric Haszlakiewicz | d809fa6 | 2012-03-31 22:53:43 -0500 | [diff] [blame^] | 718 | if (tok->err == json_tokener_success) |
| 719 | { |
| 720 | json_object *ret = json_object_get(current); |
| 721 | int ii; |
| 722 | |
| 723 | /* Partially reset, so we parse additional objects on subsequent calls. */ |
| 724 | for(ii = tok->depth; ii >= 0; ii--) |
| 725 | json_tokener_reset_level(tok, ii); |
| 726 | return ret; |
| 727 | } |
| 728 | |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 729 | MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 730 | json_tokener_errors[tok->err], tok->char_offset); |
| 731 | return NULL; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 732 | } |