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