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