[7a80925] | 1 | /* vim: set et ts=3 sw=3 sts=3 ft=c: |
---|
[696dc9e] | 2 | * |
---|
[7a80925] | 3 | * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved. |
---|
[696dc9e] | 4 | * https://github.com/udp/json-parser |
---|
| 5 | * |
---|
| 6 | * Redistribution and use in source and binary forms, with or without |
---|
| 7 | * modification, are permitted provided that the following conditions |
---|
| 8 | * are met: |
---|
| 9 | * |
---|
| 10 | * 1. Redistributions of source code must retain the above copyright |
---|
| 11 | * notice, this list of conditions and the following disclaimer. |
---|
| 12 | * |
---|
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer in the |
---|
| 15 | * documentation and/or other materials provided with the distribution. |
---|
| 16 | * |
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 27 | * SUCH DAMAGE. |
---|
| 28 | */ |
---|
| 29 | |
---|
[7a80925] | 30 | #include <glib.h> |
---|
| 31 | |
---|
[696dc9e] | 32 | #include "json.h" |
---|
| 33 | |
---|
| 34 | #ifdef _MSC_VER |
---|
| 35 | #ifndef _CRT_SECURE_NO_WARNINGS |
---|
| 36 | #define _CRT_SECURE_NO_WARNINGS |
---|
| 37 | #endif |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #ifdef __cplusplus |
---|
[5ebff60] | 41 | const struct _json_value json_value_none; /* zero-d by ctor */ |
---|
[696dc9e] | 42 | #else |
---|
[5ebff60] | 43 | const struct _json_value json_value_none = { 0 }; |
---|
[696dc9e] | 44 | #endif |
---|
| 45 | |
---|
| 46 | #include <stdio.h> |
---|
| 47 | #include <string.h> |
---|
| 48 | #include <ctype.h> |
---|
[7a80925] | 49 | #include <math.h> |
---|
[696dc9e] | 50 | |
---|
[b4b8f1e] | 51 | typedef unsigned int json_uchar; |
---|
[696dc9e] | 52 | |
---|
[5ebff60] | 53 | static unsigned char hex_value(json_char c) |
---|
[696dc9e] | 54 | { |
---|
[5ebff60] | 55 | if (g_ascii_isdigit(c)) { |
---|
| 56 | return c - '0'; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | switch (c) { |
---|
| 60 | case 'a': case 'A': return 0x0A; |
---|
| 61 | case 'b': case 'B': return 0x0B; |
---|
| 62 | case 'c': case 'C': return 0x0C; |
---|
| 63 | case 'd': case 'D': return 0x0D; |
---|
| 64 | case 'e': case 'E': return 0x0E; |
---|
| 65 | case 'f': case 'F': return 0x0F; |
---|
| 66 | default: return 0xFF; |
---|
| 67 | } |
---|
[696dc9e] | 68 | } |
---|
| 69 | |
---|
[5ebff60] | 70 | typedef struct { |
---|
| 71 | unsigned long used_memory; |
---|
[696dc9e] | 72 | |
---|
[5ebff60] | 73 | unsigned int uint_max; |
---|
| 74 | unsigned long ulong_max; |
---|
[696dc9e] | 75 | |
---|
[5ebff60] | 76 | json_settings settings; |
---|
| 77 | int first_pass; |
---|
[7a80925] | 78 | |
---|
[696dc9e] | 79 | } json_state; |
---|
| 80 | |
---|
[5ebff60] | 81 | static void * default_alloc(size_t size, int zero, void * user_data) |
---|
[7a80925] | 82 | { |
---|
[5ebff60] | 83 | return zero ? calloc(1, size) : malloc(size); |
---|
[7a80925] | 84 | } |
---|
| 85 | |
---|
[5ebff60] | 86 | static void default_free(void * ptr, void * user_data) |
---|
[696dc9e] | 87 | { |
---|
[5ebff60] | 88 | free(ptr); |
---|
[7a80925] | 89 | } |
---|
[696dc9e] | 90 | |
---|
[5ebff60] | 91 | static void * json_alloc(json_state * state, unsigned long size, int zero) |
---|
[7a80925] | 92 | { |
---|
[5ebff60] | 93 | if ((state->ulong_max - state->used_memory) < size) { |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
[696dc9e] | 96 | |
---|
[5ebff60] | 97 | if (state->settings.max_memory |
---|
| 98 | && (state->used_memory += size) > state->settings.max_memory) { |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
[696dc9e] | 101 | |
---|
[5ebff60] | 102 | return state->settings.mem_alloc(size, zero, state->settings.user_data); |
---|
[696dc9e] | 103 | } |
---|
| 104 | |
---|
| 105 | static int new_value |
---|
[5ebff60] | 106 | (json_state * state, json_value ** top, json_value ** root, json_value ** alloc, json_type type) |
---|
[696dc9e] | 107 | { |
---|
[5ebff60] | 108 | json_value * value; |
---|
| 109 | int values_size; |
---|
[696dc9e] | 110 | |
---|
[5ebff60] | 111 | if (!state->first_pass) { |
---|
| 112 | value = *top = *alloc; |
---|
| 113 | *alloc = (*alloc)->_reserved.next_alloc; |
---|
[696dc9e] | 114 | |
---|
[5ebff60] | 115 | if (!*root) { |
---|
| 116 | *root = value; |
---|
| 117 | } |
---|
[696dc9e] | 118 | |
---|
[5ebff60] | 119 | switch (value->type) { |
---|
| 120 | case json_array: |
---|
[696dc9e] | 121 | |
---|
[5ebff60] | 122 | if (!(value->u.array.values = (json_value **) json_alloc |
---|
| 123 | (state, value->u.array.length * sizeof(json_value *), |
---|
| 124 | 0))) { |
---|
| 125 | return 0; |
---|
| 126 | } |
---|
[696dc9e] | 127 | |
---|
[5ebff60] | 128 | value->u.array.length = 0; |
---|
| 129 | break; |
---|
[696dc9e] | 130 | |
---|
[5ebff60] | 131 | case json_object: |
---|
[696dc9e] | 132 | |
---|
[5ebff60] | 133 | values_size = sizeof(*value->u.object.values) * value->u.object.length; |
---|
[696dc9e] | 134 | |
---|
[5ebff60] | 135 | if (!((*(void **) &value->u.object.values) = json_alloc |
---|
| 136 | (state, values_size + |
---|
| 137 | ((unsigned long) value->u.object.values), |
---|
| 138 | 0))) { |
---|
| 139 | return 0; |
---|
| 140 | } |
---|
[696dc9e] | 141 | |
---|
[b86f37d] | 142 | value->_reserved.object_mem = (void *) (((char *) value->u.object.values) + values_size); |
---|
[696dc9e] | 143 | |
---|
[5ebff60] | 144 | value->u.object.length = 0; |
---|
| 145 | break; |
---|
[696dc9e] | 146 | |
---|
[5ebff60] | 147 | case json_string: |
---|
[696dc9e] | 148 | |
---|
[5ebff60] | 149 | if (!(value->u.string.ptr = (json_char *) json_alloc |
---|
| 150 | (state, (value->u.string.length + 1) * sizeof(json_char), |
---|
| 151 | 0))) { |
---|
| 152 | return 0; |
---|
| 153 | } |
---|
[696dc9e] | 154 | |
---|
[5ebff60] | 155 | value->u.string.length = 0; |
---|
| 156 | break; |
---|
[696dc9e] | 157 | |
---|
[5ebff60] | 158 | default: |
---|
| 159 | break; |
---|
| 160 | } |
---|
| 161 | ; |
---|
[696dc9e] | 162 | |
---|
[5ebff60] | 163 | return 1; |
---|
| 164 | } |
---|
[696dc9e] | 165 | |
---|
[5ebff60] | 166 | value = (json_value *) json_alloc(state, sizeof(json_value), 1); |
---|
[696dc9e] | 167 | |
---|
[5ebff60] | 168 | if (!value) { |
---|
| 169 | return 0; |
---|
| 170 | } |
---|
[696dc9e] | 171 | |
---|
[5ebff60] | 172 | if (!*root) { |
---|
| 173 | *root = value; |
---|
| 174 | } |
---|
[696dc9e] | 175 | |
---|
[5ebff60] | 176 | value->type = type; |
---|
| 177 | value->parent = *top; |
---|
[696dc9e] | 178 | |
---|
[5ebff60] | 179 | if (*alloc) { |
---|
| 180 | (*alloc)->_reserved.next_alloc = value; |
---|
| 181 | } |
---|
[696dc9e] | 182 | |
---|
[5ebff60] | 183 | *alloc = *top = value; |
---|
[696dc9e] | 184 | |
---|
[5ebff60] | 185 | return 1; |
---|
[696dc9e] | 186 | } |
---|
| 187 | |
---|
| 188 | #define e_off \ |
---|
[5ebff60] | 189 | ((int) (i - cur_line_begin)) |
---|
[696dc9e] | 190 | |
---|
| 191 | #define whitespace \ |
---|
[5ebff60] | 192 | case '\n': ++cur_line; cur_line_begin = i; \ |
---|
| 193 | case ' ': case '\t': case '\r' |
---|
[696dc9e] | 194 | |
---|
| 195 | #define string_add(b) \ |
---|
[5ebff60] | 196 | do { if (!state.first_pass) { string [string_length] = b; } ++string_length; } while (0); |
---|
[696dc9e] | 197 | |
---|
[7a80925] | 198 | static const long |
---|
[5ebff60] | 199 | flag_next = 1 << 0, |
---|
| 200 | flag_reproc = 1 << 1, |
---|
| 201 | flag_need_comma = 1 << 2, |
---|
| 202 | flag_seek_value = 1 << 3, |
---|
| 203 | flag_escaped = 1 << 4, |
---|
| 204 | flag_string = 1 << 5, |
---|
| 205 | flag_need_colon = 1 << 6, |
---|
| 206 | flag_done = 1 << 7, |
---|
| 207 | flag_num_negative = 1 << 8, |
---|
| 208 | flag_num_zero = 1 << 9, |
---|
| 209 | flag_num_e = 1 << 10, |
---|
| 210 | flag_num_e_got_sign = 1 << 11, |
---|
| 211 | flag_num_e_negative = 1 << 12, |
---|
| 212 | flag_line_comment = 1 << 13, |
---|
| 213 | flag_block_comment = 1 << 14; |
---|
| 214 | |
---|
| 215 | json_value * json_parse_ex(json_settings * settings, |
---|
| 216 | const json_char * json, |
---|
| 217 | size_t length, |
---|
| 218 | char * error_buf) |
---|
[696dc9e] | 219 | { |
---|
[5ebff60] | 220 | json_char error [json_error_max]; |
---|
| 221 | unsigned int cur_line; |
---|
| 222 | const json_char * cur_line_begin, * i, * end; |
---|
| 223 | json_value * top, * root, * alloc = 0; |
---|
| 224 | json_state state = { 0 }; |
---|
| 225 | long flags; |
---|
| 226 | long num_digits = 0, num_e = 0; |
---|
| 227 | json_int_t num_fraction = 0; |
---|
| 228 | |
---|
| 229 | /* Skip UTF-8 BOM |
---|
| 230 | */ |
---|
| 231 | if (length >= 3 && ((unsigned char) json [0]) == 0xEF |
---|
| 232 | && ((unsigned char) json [1]) == 0xBB |
---|
| 233 | && ((unsigned char) json [2]) == 0xBF) { |
---|
| 234 | json += 3; |
---|
| 235 | length -= 3; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | error[0] = '\0'; |
---|
| 239 | end = (json + length); |
---|
| 240 | |
---|
| 241 | memcpy(&state.settings, settings, sizeof(json_settings)); |
---|
| 242 | |
---|
| 243 | if (!state.settings.mem_alloc) { |
---|
| 244 | state.settings.mem_alloc = default_alloc; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | if (!state.settings.mem_free) { |
---|
| 248 | state.settings.mem_free = default_free; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | memset(&state.uint_max, 0xFF, sizeof(state.uint_max)); |
---|
| 252 | memset(&state.ulong_max, 0xFF, sizeof(state.ulong_max)); |
---|
| 253 | |
---|
| 254 | state.uint_max -= 8; /* limit of how much can be added before next check */ |
---|
| 255 | state.ulong_max -= 8; |
---|
| 256 | |
---|
| 257 | for (state.first_pass = 1; state.first_pass >= 0; --state.first_pass) { |
---|
| 258 | json_uchar uchar; |
---|
| 259 | unsigned char uc_b1, uc_b2, uc_b3, uc_b4; |
---|
| 260 | json_char * string = 0; |
---|
| 261 | unsigned int string_length = 0; |
---|
| 262 | |
---|
| 263 | top = root = 0; |
---|
| 264 | flags = flag_seek_value; |
---|
| 265 | |
---|
| 266 | cur_line = 1; |
---|
| 267 | cur_line_begin = json; |
---|
| 268 | |
---|
| 269 | for (i = json;; ++i) { |
---|
| 270 | json_char b = (i == end ? 0 : *i); |
---|
| 271 | |
---|
| 272 | if (flags & flag_string) { |
---|
| 273 | if (!b) { |
---|
| 274 | sprintf(error, "Unexpected EOF in string (at %d:%d)", cur_line, e_off); |
---|
| 275 | goto e_failed; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | if (string_length > state.uint_max) { |
---|
| 279 | goto e_overflow; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if (flags & flag_escaped) { |
---|
| 283 | flags &= ~flag_escaped; |
---|
| 284 | |
---|
| 285 | switch (b) { |
---|
| 286 | case 'b': string_add('\b'); break; |
---|
| 287 | case 'f': string_add('\f'); break; |
---|
| 288 | case 'n': string_add('\n'); break; |
---|
| 289 | case 'r': string_add('\r'); break; |
---|
| 290 | case 't': string_add('\t'); break; |
---|
| 291 | case 'u': |
---|
| 292 | |
---|
| 293 | if (end - i < 4 || |
---|
| 294 | (uc_b1 = hex_value(*++i)) == 0xFF || (uc_b2 = hex_value(*++i)) == |
---|
| 295 | 0xFF |
---|
| 296 | || (uc_b3 = hex_value(*++i)) == 0xFF || (uc_b4 = hex_value(*++i)) == |
---|
| 297 | 0xFF) { |
---|
| 298 | sprintf(error, "Invalid character value `%c` (at %d:%d)", b, |
---|
| 299 | cur_line, e_off); |
---|
| 300 | goto e_failed; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | uc_b1 = (uc_b1 << 4) | uc_b2; |
---|
| 304 | uc_b2 = (uc_b3 << 4) | uc_b4; |
---|
| 305 | uchar = (uc_b1 << 8) | uc_b2; |
---|
| 306 | |
---|
| 307 | if ((uchar & 0xF800) == 0xD800) { |
---|
| 308 | json_uchar uchar2; |
---|
| 309 | |
---|
| 310 | if (end - i < 6 || (*++i) != '\\' || (*++i) != 'u' || |
---|
| 311 | (uc_b1 = hex_value(*++i)) == 0xFF || |
---|
| 312 | (uc_b2 = hex_value(*++i)) == 0xFF |
---|
| 313 | || (uc_b3 = hex_value(*++i)) == 0xFF || |
---|
| 314 | (uc_b4 = hex_value(*++i)) == 0xFF) { |
---|
| 315 | sprintf(error, |
---|
| 316 | "Invalid character value `%c` (at %d:%d)", b, |
---|
| 317 | cur_line, e_off); |
---|
| 318 | goto e_failed; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | uc_b1 = (uc_b1 << 4) | uc_b2; |
---|
| 322 | uc_b2 = (uc_b3 << 4) | uc_b4; |
---|
| 323 | uchar2 = (uc_b1 << 8) | uc_b2; |
---|
| 324 | |
---|
| 325 | uchar = 0x010000 | ((uchar & 0x3FF) << 10) | (uchar2 & 0x3FF); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | if (sizeof(json_char) >= sizeof(json_uchar) || (uchar <= 0x7F)) { |
---|
| 329 | string_add((json_char) uchar); |
---|
| 330 | break; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | if (uchar <= 0x7FF) { |
---|
| 334 | if (state.first_pass) { |
---|
| 335 | string_length += 2; |
---|
| 336 | } else { string [string_length++] = 0xC0 | |
---|
| 337 | (uchar >> |
---|
| 338 | 6); |
---|
| 339 | string [string_length++] = 0x80 | |
---|
| 340 | (uchar & |
---|
| 341 | 0x3F); } |
---|
| 342 | |
---|
| 343 | break; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if (uchar <= 0xFFFF) { |
---|
| 347 | if (state.first_pass) { |
---|
| 348 | string_length += 3; |
---|
| 349 | } else { string [string_length++] = 0xE0 | |
---|
| 350 | (uchar >> |
---|
| 351 | 12); |
---|
| 352 | string [string_length++] = 0x80 | |
---|
| 353 | ((uchar |
---|
| 354 | >> 6) & |
---|
| 355 | 0x3F); |
---|
| 356 | string [string_length++] = 0x80 | |
---|
| 357 | (uchar & |
---|
| 358 | 0x3F); } |
---|
| 359 | |
---|
| 360 | break; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | if (state.first_pass) { |
---|
| 364 | string_length += 4; |
---|
| 365 | } else { string [string_length++] = 0xF0 | (uchar >> 18); |
---|
| 366 | string [string_length++] = 0x80 | |
---|
| 367 | ((uchar >> |
---|
| 368 | 12) & 0x3F); |
---|
| 369 | string [string_length++] = 0x80 | |
---|
| 370 | ((uchar >> |
---|
| 371 | 6) & 0x3F); |
---|
| 372 | string [string_length++] = 0x80 | |
---|
| 373 | (uchar & 0x3F); } |
---|
| 374 | |
---|
| 375 | break; |
---|
| 376 | |
---|
| 377 | default: |
---|
| 378 | string_add(b); |
---|
| 379 | } |
---|
| 380 | ; |
---|
| 381 | |
---|
| 382 | continue; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | if (b == '\\') { |
---|
| 386 | flags |= flag_escaped; |
---|
| 387 | continue; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | if (b == '"') { |
---|
| 391 | if (!state.first_pass) { |
---|
| 392 | string [string_length] = 0; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | flags &= ~flag_string; |
---|
| 396 | string = 0; |
---|
| 397 | |
---|
| 398 | switch (top->type) { |
---|
| 399 | case json_string: |
---|
| 400 | |
---|
| 401 | top->u.string.length = string_length; |
---|
| 402 | flags |= flag_next; |
---|
| 403 | |
---|
| 404 | break; |
---|
| 405 | |
---|
| 406 | case json_object: |
---|
| 407 | |
---|
| 408 | if (state.first_pass) { |
---|
[b86f37d] | 409 | json_char **chars = (json_char **) &top->u.object.values; |
---|
| 410 | chars[0] += string_length + 1; |
---|
[5ebff60] | 411 | } else { |
---|
| 412 | top->u.object.values [top->u.object.length].name |
---|
| 413 | = (json_char *) top->_reserved.object_mem; |
---|
| 414 | |
---|
| 415 | top->u.object.values [top->u.object.length].name_length |
---|
| 416 | = string_length; |
---|
| 417 | |
---|
| 418 | (*(json_char **) &top->_reserved.object_mem) += string_length + |
---|
| 419 | 1; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | flags |= flag_seek_value | flag_need_colon; |
---|
| 423 | continue; |
---|
| 424 | |
---|
| 425 | default: |
---|
| 426 | break; |
---|
| 427 | } |
---|
| 428 | ; |
---|
| 429 | } else { |
---|
| 430 | string_add(b); |
---|
| 431 | continue; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | if (state.settings.settings & json_enable_comments) { |
---|
| 436 | if (flags & (flag_line_comment | flag_block_comment)) { |
---|
| 437 | if (flags & flag_line_comment) { |
---|
| 438 | if (b == '\r' || b == '\n' || !b) { |
---|
| 439 | flags &= ~flag_line_comment; |
---|
| 440 | --i; /* so null can be reproc'd */ |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | continue; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | if (flags & flag_block_comment) { |
---|
| 447 | if (!b) { |
---|
| 448 | sprintf(error, "%d:%d: Unexpected EOF in block comment", |
---|
| 449 | cur_line, e_off); |
---|
| 450 | goto e_failed; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | if (b == '*' && i < (end - 1) && i [1] == '/') { |
---|
| 454 | flags &= ~flag_block_comment; |
---|
| 455 | ++i; /* skip closing sequence */ |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | continue; |
---|
| 459 | } |
---|
| 460 | } else if (b == '/') { |
---|
| 461 | if (!(flags & (flag_seek_value | flag_done)) && top->type != json_object) { |
---|
| 462 | sprintf(error, "%d:%d: Comment not allowed here", cur_line, e_off); |
---|
| 463 | goto e_failed; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | if (++i == end) { |
---|
| 467 | sprintf(error, "%d:%d: EOF unexpected", cur_line, e_off); |
---|
| 468 | goto e_failed; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | switch (b = *i) { |
---|
| 472 | case '/': |
---|
| 473 | flags |= flag_line_comment; |
---|
| 474 | continue; |
---|
| 475 | |
---|
| 476 | case '*': |
---|
| 477 | flags |= flag_block_comment; |
---|
| 478 | continue; |
---|
| 479 | |
---|
| 480 | default: |
---|
| 481 | sprintf(error, "%d:%d: Unexpected `%c` in comment opening sequence", |
---|
| 482 | cur_line, e_off, b); |
---|
| 483 | goto e_failed; |
---|
| 484 | } |
---|
| 485 | ; |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | if (flags & flag_done) { |
---|
| 490 | if (!b) { |
---|
| 491 | break; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | switch (b) { |
---|
| 495 | whitespace: |
---|
| 496 | continue; |
---|
| 497 | |
---|
| 498 | default: |
---|
| 499 | sprintf(error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b); |
---|
| 500 | goto e_failed; |
---|
| 501 | } |
---|
| 502 | ; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | if (flags & flag_seek_value) { |
---|
| 506 | switch (b) { |
---|
| 507 | whitespace: |
---|
| 508 | continue; |
---|
| 509 | |
---|
| 510 | case ']': |
---|
| 511 | |
---|
[77ed9b7] | 512 | if (top && top->type == json_array) { |
---|
[5ebff60] | 513 | flags = (flags & ~(flag_need_comma | flag_seek_value)) | flag_next; |
---|
| 514 | } else { sprintf(error, "%d:%d: Unexpected ]", cur_line, e_off); |
---|
| 515 | goto e_failed; } |
---|
| 516 | |
---|
| 517 | break; |
---|
| 518 | |
---|
| 519 | default: |
---|
| 520 | |
---|
| 521 | if (flags & flag_need_comma) { |
---|
| 522 | if (b == ',') { |
---|
| 523 | flags &= ~flag_need_comma; |
---|
| 524 | continue; |
---|
| 525 | } else { sprintf(error, "%d:%d: Expected , before %c", cur_line, e_off, |
---|
| 526 | b); |
---|
| 527 | goto e_failed; } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | if (flags & flag_need_colon) { |
---|
| 531 | if (b == ':') { |
---|
| 532 | flags &= ~flag_need_colon; |
---|
| 533 | continue; |
---|
| 534 | } else { sprintf(error, "%d:%d: Expected : before %c", cur_line, e_off, |
---|
| 535 | b); |
---|
| 536 | goto e_failed; } |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | flags &= ~flag_seek_value; |
---|
[696dc9e] | 540 | |
---|
[5ebff60] | 541 | switch (b) { |
---|
| 542 | case '{': |
---|
[696dc9e] | 543 | |
---|
[5ebff60] | 544 | if (!new_value(&state, &top, &root, &alloc, json_object)) { |
---|
| 545 | goto e_alloc_failure; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | continue; |
---|
[696dc9e] | 549 | |
---|
[5ebff60] | 550 | case '[': |
---|
[696dc9e] | 551 | |
---|
[5ebff60] | 552 | if (!new_value(&state, &top, &root, &alloc, json_array)) { |
---|
| 553 | goto e_alloc_failure; |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | flags |= flag_seek_value; |
---|
| 557 | continue; |
---|
| 558 | |
---|
| 559 | case '"': |
---|
| 560 | |
---|
| 561 | if (!new_value(&state, &top, &root, &alloc, json_string)) { |
---|
| 562 | goto e_alloc_failure; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | flags |= flag_string; |
---|
| 566 | |
---|
| 567 | string = top->u.string.ptr; |
---|
| 568 | string_length = 0; |
---|
[696dc9e] | 569 | |
---|
[5ebff60] | 570 | continue; |
---|
| 571 | |
---|
| 572 | case 't': |
---|
[696dc9e] | 573 | |
---|
[5ebff60] | 574 | if ((end - i) < 3 || *(++i) != 'r' || *(++i) != 'u' || *(++i) != 'e') { |
---|
| 575 | goto e_unknown_value; |
---|
| 576 | } |
---|
[696dc9e] | 577 | |
---|
[5ebff60] | 578 | if (!new_value(&state, &top, &root, &alloc, json_boolean)) { |
---|
| 579 | goto e_alloc_failure; |
---|
| 580 | } |
---|
[696dc9e] | 581 | |
---|
[5ebff60] | 582 | top->u.boolean = 1; |
---|
[7a80925] | 583 | |
---|
[5ebff60] | 584 | flags |= flag_next; |
---|
| 585 | break; |
---|
[7a80925] | 586 | |
---|
[5ebff60] | 587 | case 'f': |
---|
[7a80925] | 588 | |
---|
[5ebff60] | 589 | if ((end - i) < 4 || *(++i) != 'a' || *(++i) != 'l' || *(++i) != 's' || |
---|
| 590 | *(++i) != 'e') { |
---|
| 591 | goto e_unknown_value; |
---|
| 592 | } |
---|
[7a80925] | 593 | |
---|
[5ebff60] | 594 | if (!new_value(&state, &top, &root, &alloc, json_boolean)) { |
---|
| 595 | goto e_alloc_failure; |
---|
| 596 | } |
---|
[7a80925] | 597 | |
---|
[5ebff60] | 598 | flags |= flag_next; |
---|
| 599 | break; |
---|
[7a80925] | 600 | |
---|
[5ebff60] | 601 | case 'n': |
---|
[696dc9e] | 602 | |
---|
[5ebff60] | 603 | if ((end - i) < 3 || *(++i) != 'u' || *(++i) != 'l' || *(++i) != 'l') { |
---|
| 604 | goto e_unknown_value; |
---|
| 605 | } |
---|
[696dc9e] | 606 | |
---|
[5ebff60] | 607 | if (!new_value(&state, &top, &root, &alloc, json_null)) { |
---|
| 608 | goto e_alloc_failure; |
---|
| 609 | } |
---|
[696dc9e] | 610 | |
---|
[5ebff60] | 611 | flags |= flag_next; |
---|
| 612 | break; |
---|
[696dc9e] | 613 | |
---|
[5ebff60] | 614 | default: |
---|
[696dc9e] | 615 | |
---|
[5ebff60] | 616 | if (g_ascii_isdigit(b) || b == '-') { |
---|
| 617 | if (!new_value(&state, &top, &root, &alloc, json_integer)) { |
---|
| 618 | goto e_alloc_failure; |
---|
| 619 | } |
---|
[696dc9e] | 620 | |
---|
[5ebff60] | 621 | if (!state.first_pass) { |
---|
| 622 | while (g_ascii_isdigit(b) || b == '+' || b == '-' |
---|
| 623 | || b == 'e' || b == 'E' || b == '.') { |
---|
| 624 | if ((++i) == end) { |
---|
| 625 | b = 0; |
---|
| 626 | break; |
---|
| 627 | } |
---|
[696dc9e] | 628 | |
---|
[5ebff60] | 629 | b = *i; |
---|
| 630 | } |
---|
[696dc9e] | 631 | |
---|
[5ebff60] | 632 | flags |= flag_next | flag_reproc; |
---|
| 633 | break; |
---|
| 634 | } |
---|
[696dc9e] | 635 | |
---|
[5ebff60] | 636 | flags &= ~(flag_num_negative | flag_num_e | |
---|
| 637 | flag_num_e_got_sign | flag_num_e_negative | |
---|
| 638 | flag_num_zero); |
---|
[696dc9e] | 639 | |
---|
[5ebff60] | 640 | num_digits = 0; |
---|
| 641 | num_fraction = 0; |
---|
| 642 | num_e = 0; |
---|
[696dc9e] | 643 | |
---|
[5ebff60] | 644 | if (b != '-') { |
---|
| 645 | flags |= flag_reproc; |
---|
| 646 | break; |
---|
| 647 | } |
---|
[696dc9e] | 648 | |
---|
[5ebff60] | 649 | flags |= flag_num_negative; |
---|
| 650 | continue; |
---|
| 651 | } else { sprintf(error, "%d:%d: Unexpected %c when seeking value", |
---|
| 652 | cur_line, e_off, b); |
---|
| 653 | goto e_failed; } |
---|
| 654 | } |
---|
| 655 | ; |
---|
| 656 | } |
---|
| 657 | ; |
---|
| 658 | } else { |
---|
| 659 | switch (top->type) { |
---|
| 660 | case json_object: |
---|
| 661 | |
---|
| 662 | switch (b) { |
---|
| 663 | whitespace: |
---|
| 664 | continue; |
---|
| 665 | |
---|
| 666 | case '"': |
---|
| 667 | |
---|
| 668 | if (flags & flag_need_comma) { |
---|
| 669 | sprintf(error, "%d:%d: Expected , before \"", cur_line, e_off); |
---|
| 670 | goto e_failed; |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | flags |= flag_string; |
---|
| 674 | |
---|
| 675 | string = (json_char *) top->_reserved.object_mem; |
---|
| 676 | string_length = 0; |
---|
| 677 | |
---|
| 678 | break; |
---|
| 679 | |
---|
| 680 | case '}': |
---|
| 681 | |
---|
| 682 | flags = (flags & ~flag_need_comma) | flag_next; |
---|
| 683 | break; |
---|
| 684 | |
---|
| 685 | case ',': |
---|
| 686 | |
---|
| 687 | if (flags & flag_need_comma) { |
---|
| 688 | flags &= ~flag_need_comma; |
---|
| 689 | break; |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | default: |
---|
| 693 | |
---|
| 694 | sprintf(error, "%d:%d: Unexpected `%c` in object", cur_line, e_off, b); |
---|
| 695 | goto e_failed; |
---|
| 696 | } |
---|
| 697 | ; |
---|
| 698 | |
---|
| 699 | break; |
---|
| 700 | |
---|
| 701 | case json_integer: |
---|
| 702 | case json_double: |
---|
| 703 | |
---|
| 704 | if (g_ascii_isdigit(b)) { |
---|
| 705 | ++num_digits; |
---|
| 706 | |
---|
| 707 | if (top->type == json_integer || flags & flag_num_e) { |
---|
| 708 | if (!(flags & flag_num_e)) { |
---|
| 709 | if (flags & flag_num_zero) { |
---|
| 710 | sprintf(error, |
---|
| 711 | "%d:%d: Unexpected `0` before `%c`", |
---|
| 712 | cur_line, e_off, b); |
---|
| 713 | goto e_failed; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | if (num_digits == 1 && b == '0') { |
---|
| 717 | flags |= flag_num_zero; |
---|
| 718 | } |
---|
| 719 | } else { |
---|
| 720 | flags |= flag_num_e_got_sign; |
---|
| 721 | num_e = (num_e * 10) + (b - '0'); |
---|
| 722 | continue; |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | top->u.integer = (top->u.integer * 10) + (b - '0'); |
---|
| 726 | continue; |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | num_fraction = (num_fraction * 10) + (b - '0'); |
---|
| 730 | continue; |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | if (b == '+' || b == '-') { |
---|
| 734 | if ((flags & flag_num_e) && !(flags & flag_num_e_got_sign)) { |
---|
| 735 | flags |= flag_num_e_got_sign; |
---|
| 736 | |
---|
| 737 | if (b == '-') { |
---|
| 738 | flags |= flag_num_e_negative; |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | continue; |
---|
| 742 | } |
---|
| 743 | } else if (b == '.' && top->type == json_integer) { |
---|
| 744 | if (!num_digits) { |
---|
| 745 | sprintf(error, "%d:%d: Expected digit before `.`", cur_line, |
---|
| 746 | e_off); |
---|
| 747 | goto e_failed; |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | top->type = json_double; |
---|
| 751 | top->u.dbl = (double) top->u.integer; |
---|
| 752 | |
---|
| 753 | num_digits = 0; |
---|
| 754 | continue; |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | if (!(flags & flag_num_e)) { |
---|
| 758 | if (top->type == json_double) { |
---|
| 759 | if (!num_digits) { |
---|
| 760 | sprintf(error, "%d:%d: Expected digit after `.`", |
---|
| 761 | cur_line, e_off); |
---|
| 762 | goto e_failed; |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | top->u.dbl += ((double) num_fraction) / |
---|
| 766 | (pow(10, (double) num_digits)); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | if (b == 'e' || b == 'E') { |
---|
| 770 | flags |= flag_num_e; |
---|
| 771 | |
---|
| 772 | if (top->type == json_integer) { |
---|
| 773 | top->type = json_double; |
---|
| 774 | top->u.dbl = (double) top->u.integer; |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | num_digits = 0; |
---|
| 778 | flags &= ~flag_num_zero; |
---|
| 779 | |
---|
| 780 | continue; |
---|
| 781 | } |
---|
| 782 | } else { |
---|
| 783 | if (!num_digits) { |
---|
| 784 | sprintf(error, "%d:%d: Expected digit after `e`", cur_line, |
---|
| 785 | e_off); |
---|
| 786 | goto e_failed; |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | top->u.dbl *= |
---|
| 790 | pow(10, |
---|
| 791 | (double) (flags & flag_num_e_negative ? -num_e : num_e)); |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | if (flags & flag_num_negative) { |
---|
| 795 | if (top->type == json_integer) { |
---|
| 796 | top->u.integer = -top->u.integer; |
---|
| 797 | } else { |
---|
| 798 | top->u.dbl = -top->u.dbl; |
---|
| 799 | } |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | flags |= flag_next | flag_reproc; |
---|
| 803 | break; |
---|
| 804 | |
---|
| 805 | default: |
---|
| 806 | break; |
---|
| 807 | } |
---|
| 808 | ; |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | if (flags & flag_reproc) { |
---|
| 812 | flags &= ~flag_reproc; |
---|
| 813 | --i; |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | if (flags & flag_next) { |
---|
| 817 | flags = (flags & ~flag_next) | flag_need_comma; |
---|
| 818 | |
---|
| 819 | if (!top->parent) { |
---|
| 820 | /* root value done */ |
---|
| 821 | |
---|
| 822 | flags |= flag_done; |
---|
| 823 | continue; |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | if (top->parent->type == json_array) { |
---|
| 827 | flags |= flag_seek_value; |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | if (!state.first_pass) { |
---|
| 831 | json_value * parent = top->parent; |
---|
| 832 | |
---|
| 833 | switch (parent->type) { |
---|
| 834 | case json_object: |
---|
| 835 | |
---|
| 836 | parent->u.object.values |
---|
| 837 | [parent->u.object.length].value = top; |
---|
| 838 | |
---|
| 839 | break; |
---|
| 840 | |
---|
| 841 | case json_array: |
---|
| 842 | |
---|
| 843 | parent->u.array.values |
---|
| 844 | [parent->u.array.length] = top; |
---|
[696dc9e] | 845 | |
---|
[5ebff60] | 846 | break; |
---|
[696dc9e] | 847 | |
---|
[5ebff60] | 848 | default: |
---|
| 849 | break; |
---|
| 850 | } |
---|
| 851 | ; |
---|
| 852 | } |
---|
[696dc9e] | 853 | |
---|
[5ebff60] | 854 | if ((++top->parent->u.array.length) > state.uint_max) { |
---|
| 855 | goto e_overflow; |
---|
| 856 | } |
---|
[696dc9e] | 857 | |
---|
[5ebff60] | 858 | top = top->parent; |
---|
[696dc9e] | 859 | |
---|
[5ebff60] | 860 | continue; |
---|
| 861 | } |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | alloc = root; |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | return root; |
---|
| 868 | |
---|
| 869 | e_unknown_value: |
---|
| 870 | |
---|
| 871 | sprintf(error, "%d:%d: Unknown value", cur_line, e_off); |
---|
| 872 | goto e_failed; |
---|
| 873 | |
---|
| 874 | e_alloc_failure: |
---|
| 875 | |
---|
| 876 | strcpy(error, "Memory allocation failure"); |
---|
| 877 | goto e_failed; |
---|
| 878 | |
---|
| 879 | e_overflow: |
---|
| 880 | |
---|
| 881 | sprintf(error, "%d:%d: Too long (caught overflow)", cur_line, e_off); |
---|
| 882 | goto e_failed; |
---|
| 883 | |
---|
| 884 | e_failed: |
---|
| 885 | |
---|
| 886 | if (error_buf) { |
---|
| 887 | if (*error) { |
---|
| 888 | strcpy(error_buf, error); |
---|
| 889 | } else { |
---|
| 890 | strcpy(error_buf, "Unknown error"); |
---|
| 891 | } |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | if (state.first_pass) { |
---|
| 895 | alloc = root; |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | while (alloc) { |
---|
| 899 | top = alloc->_reserved.next_alloc; |
---|
| 900 | state.settings.mem_free(alloc, state.settings.user_data); |
---|
| 901 | alloc = top; |
---|
| 902 | } |
---|
| 903 | |
---|
| 904 | if (!state.first_pass) { |
---|
| 905 | json_value_free_ex(&state.settings, root); |
---|
| 906 | } |
---|
| 907 | |
---|
| 908 | return 0; |
---|
[696dc9e] | 909 | } |
---|
| 910 | |
---|
[5ebff60] | 911 | json_value * json_parse(const json_char * json, size_t length) |
---|
[696dc9e] | 912 | { |
---|
[5ebff60] | 913 | json_settings settings = { 0 }; |
---|
| 914 | |
---|
| 915 | return json_parse_ex(&settings, json, length, 0); |
---|
[696dc9e] | 916 | } |
---|
| 917 | |
---|
[5ebff60] | 918 | void json_value_free_ex(json_settings * settings, json_value * value) |
---|
[696dc9e] | 919 | { |
---|
[5ebff60] | 920 | json_value * cur_value; |
---|
[696dc9e] | 921 | |
---|
[5ebff60] | 922 | if (!value) { |
---|
| 923 | return; |
---|
| 924 | } |
---|
[696dc9e] | 925 | |
---|
[5ebff60] | 926 | value->parent = 0; |
---|
[696dc9e] | 927 | |
---|
[5ebff60] | 928 | while (value) { |
---|
| 929 | switch (value->type) { |
---|
| 930 | case json_array: |
---|
[696dc9e] | 931 | |
---|
[5ebff60] | 932 | if (!value->u.array.length) { |
---|
| 933 | settings->mem_free(value->u.array.values, settings->user_data); |
---|
| 934 | break; |
---|
| 935 | } |
---|
[696dc9e] | 936 | |
---|
[5ebff60] | 937 | value = value->u.array.values [--value->u.array.length]; |
---|
| 938 | continue; |
---|
[696dc9e] | 939 | |
---|
[5ebff60] | 940 | case json_object: |
---|
[696dc9e] | 941 | |
---|
[5ebff60] | 942 | if (!value->u.object.length) { |
---|
| 943 | settings->mem_free(value->u.object.values, settings->user_data); |
---|
| 944 | break; |
---|
| 945 | } |
---|
[696dc9e] | 946 | |
---|
[5ebff60] | 947 | value = value->u.object.values [--value->u.object.length].value; |
---|
| 948 | continue; |
---|
[696dc9e] | 949 | |
---|
[5ebff60] | 950 | case json_string: |
---|
[696dc9e] | 951 | |
---|
[5ebff60] | 952 | settings->mem_free(value->u.string.ptr, settings->user_data); |
---|
| 953 | break; |
---|
[696dc9e] | 954 | |
---|
[5ebff60] | 955 | default: |
---|
| 956 | break; |
---|
| 957 | } |
---|
| 958 | ; |
---|
[696dc9e] | 959 | |
---|
[5ebff60] | 960 | cur_value = value; |
---|
| 961 | value = value->parent; |
---|
| 962 | settings->mem_free(cur_value, settings->user_data); |
---|
| 963 | } |
---|
[696dc9e] | 964 | } |
---|
| 965 | |
---|
[5ebff60] | 966 | void json_value_free(json_value * value) |
---|
[7a80925] | 967 | { |
---|
[5ebff60] | 968 | json_settings settings = { 0 }; |
---|
| 969 | |
---|
| 970 | settings.mem_free = default_free; |
---|
| 971 | json_value_free_ex(&settings, value); |
---|
[7a80925] | 972 | } |
---|
[696dc9e] | 973 | |
---|