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