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