| 1 | /* vim: set et ts=3 sw=3 sts=3 ft=c: |
| 2 | * |
| 3 | * Copyright (C) 2012-2021 the json-parser authors All rights reserved. |
| 4 | * https://github.com/json-parser/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 | |
| 30 | #include "json.h" |
| 31 | |
| 32 | #ifdef _MSC_VER |
| 33 | #ifndef _CRT_SECURE_NO_WARNINGS |
| 34 | #define _CRT_SECURE_NO_WARNINGS |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <ctype.h> |
| 42 | #include <limits.h> |
| 43 | #include <math.h> |
| 44 | |
| 45 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
| 46 | /* C99 might give us uintptr_t and UINTPTR_MAX but they also might not be provided */ |
| 47 | #include <stdint.h> |
| 48 | #endif |
| 49 | |
| 50 | #ifndef JSON_INT_T_OVERRIDDEN |
| 51 | #if defined(_MSC_VER) |
| 52 | /* https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges */ |
| 53 | #define JSON_INT_MAX 9223372036854775807LL |
| 54 | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
| 55 | /* C99 */ |
| 56 | #define JSON_INT_MAX INT_FAST64_MAX |
| 57 | #else |
| 58 | /* C89 */ |
| 59 | #include <limits.h> |
| 60 | #define JSON_INT_MAX LONG_MAX |
| 61 | #endif |
| 62 | #endif |
| 63 | |
| 64 | #ifndef JSON_INT_MAX |
| 65 | #define JSON_INT_MAX (json_int_t)(((unsigned json_int_t)(-1)) / (unsigned json_int_t)2); |
| 66 | #endif |
| 67 | |
| 68 | #ifndef JSON_UINTPTR_T |
| 69 | #ifdef UINTPTR_MAX |
| 70 | /* C99 */ |
| 71 | #define JSON_UINTPTR_T uintptr_t |
| 72 | #else |
| 73 | /* C89 */ |
| 74 | #define JSON_UINTPTR_T size_t |
| 75 | #endif |
| 76 | #endif |
| 77 | |
| 78 | #ifndef JSON_SIZE_MAX |
| 79 | #ifdef SIZE_MAX |
| 80 | /* C99 */ |
| 81 | #define JSON_SIZE_MAX SIZE_MAX |
| 82 | #else |
| 83 | /* C89 */ |
| 84 | #define JSON_SIZE_MAX ((size_t) -1) |
| 85 | #endif |
| 86 | #endif |
| 87 | |
| 88 | typedef char json_size_t_static_assert[(JSON_SIZE_MAX >= UINT_MAX) ? 1 : -1]; /* size_t must be able to hold all values of unsigned int */ |
| 89 | #ifndef JSON_OBJECT_ELEMENTS_MAX |
| 90 | static const unsigned int JSON_OBJECT_ELEMENTS_MAX = UINT_MAX / (sizeof (json_object_entry) + sizeof (json_char)); |
| 91 | #endif |
| 92 | #ifndef JSON_ARRAY_ELEMENTS_MAX |
| 93 | static const unsigned int JSON_ARRAY_ELEMENTS_MAX = UINT_MAX / sizeof (json_value *); |
| 94 | #endif |
| 95 | #ifndef JSON_STRING_LENGTH_MAX |
| 96 | static const unsigned int JSON_STRING_LENGTH_MAX = UINT_MAX / sizeof (json_char) - (unsigned int) 1; |
| 97 | #endif |
| 98 | #ifndef JSON_OBJECT_NAMES_COMBINED_LENGTH_MAX |
| 99 | static const unsigned int JSON_OBJECT_NAMES_COMBINED_LENGTH_MAX = UINT_MAX / sizeof (json_char) - (unsigned int) (1 + sizeof (json_object_entry)); |
| 100 | #endif |
| 101 | |
| 102 | typedef unsigned int json_uchar; |
| 103 | |
| 104 | const struct _json_value json_value_none; |
| 105 | |
| 106 | static unsigned char hex_value (json_char c) |
| 107 | { |
| 108 | if (isdigit((unsigned char)c)) |
| 109 | return c - '0'; |
| 110 | |
| 111 | switch (c) { |
| 112 | case 'a': case 'A': return 0x0A; |
| 113 | case 'b': case 'B': return 0x0B; |
| 114 | case 'c': case 'C': return 0x0C; |
| 115 | case 'd': case 'D': return 0x0D; |
| 116 | case 'e': case 'E': return 0x0E; |
| 117 | case 'f': case 'F': return 0x0F; |
| 118 | default: return 0xFF; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static int would_overflow (json_int_t value, json_char b) |
| 123 | { |
| 124 | return ((JSON_INT_MAX - (b - '0')) / 10 ) < value; |
| 125 | } |
| 126 | |
| 127 | typedef struct |
| 128 | { |
| 129 | size_t used_memory; |
| 130 | |
| 131 | json_settings settings; |
| 132 | int first_pass; |
| 133 | |
| 134 | const json_char * ptr; |
| 135 | unsigned int cur_line, cur_col; |
| 136 | |
| 137 | } json_state; |
| 138 | |
| 139 | static void * default_alloc (size_t size, int zero, void * user_data) |
| 140 | { |
| 141 | (void)user_data; /* ignore unused-parameter warn */ |
| 142 | return zero ? calloc (nmemb: 1, size: size) : malloc (size: size); |
| 143 | } |
| 144 | |
| 145 | static void default_free (void * ptr, void * user_data) |
| 146 | { |
| 147 | (void)user_data; /* ignore unused-parameter warn */ |
| 148 | free (ptr: ptr); |
| 149 | } |
| 150 | |
| 151 | static void * json_alloc (json_state * state, size_t size, int zero) |
| 152 | { |
| 153 | if ((ULONG_MAX - 8 - state->used_memory) < size) |
| 154 | return 0; |
| 155 | |
| 156 | if (state->settings.max_memory |
| 157 | && (state->used_memory += size) > state->settings.max_memory) |
| 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | return state->settings.mem_alloc (size, zero, state->settings.user_data); |
| 163 | } |
| 164 | |
| 165 | static int new_value (json_state * state, |
| 166 | json_value ** top, json_value ** root, json_value ** alloc, |
| 167 | json_type type) |
| 168 | { |
| 169 | json_value * value; |
| 170 | size_t values_size; |
| 171 | JSON_UINTPTR_T names_size; |
| 172 | |
| 173 | if (!state->first_pass) |
| 174 | { |
| 175 | value = *top = *alloc; |
| 176 | *alloc = (*alloc)->_reserved.next_alloc; |
| 177 | |
| 178 | if (!*root) |
| 179 | *root = value; |
| 180 | |
| 181 | switch (value->type) |
| 182 | { |
| 183 | case json_array: |
| 184 | |
| 185 | if (value->u.array.length == 0) |
| 186 | break; |
| 187 | |
| 188 | #ifndef JSON_NO_PEDANTIC_OVERFLOW |
| 189 | /* the first pass should already have handled this */ |
| 190 | if (value->u.array.length > JSON_ARRAY_ELEMENTS_MAX) |
| 191 | { |
| 192 | value->u.array.length = 0; |
| 193 | return 0; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | if (! (value->u.array.values = (json_value **) json_alloc |
| 198 | (state, size: value->u.array.length * sizeof (json_value *), zero: 0)) ) |
| 199 | { |
| 200 | value->u.array.length = 0; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | value->u.array.length = 0; |
| 205 | break; |
| 206 | |
| 207 | case json_object: |
| 208 | |
| 209 | if (value->u.object.length == 0) |
| 210 | break; |
| 211 | |
| 212 | #ifndef JSON_NO_PEDANTIC_OVERFLOW |
| 213 | /* the first pass should already have handled this */ |
| 214 | if (value->u.object.length > JSON_OBJECT_ELEMENTS_MAX) |
| 215 | { |
| 216 | value->u.object.length = 0; |
| 217 | value->u.object.values = 0; |
| 218 | return 0; |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | values_size = sizeof (json_object_entry) * value->u.object.length; |
| 223 | |
| 224 | /* |
| 225 | The `values` pointer holds a size calculated by the first pass. |
| 226 | The size represents the storage space needed for object key names. |
| 227 | Now during the second pass, it's replaced with an actual allocation. |
| 228 | */ |
| 229 | names_size = (JSON_UINTPTR_T) value->u.object.values; |
| 230 | |
| 231 | #ifndef JSON_NO_PEDANTIC_OVERFLOW |
| 232 | /* the first pass should already have handled this */ |
| 233 | if (names_size > JSON_OBJECT_NAMES_COMBINED_LENGTH_MAX |
| 234 | || values_size >= JSON_SIZE_MAX || JSON_SIZE_MAX - values_size < names_size) |
| 235 | { |
| 236 | value->u.object.length = 0; |
| 237 | value->u.object.values = 0; |
| 238 | return 0; |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | if (! (value->u.object.values = (json_object_entry *) json_alloc |
| 243 | (state, size: values_size + names_size, zero: 0)) ) |
| 244 | { |
| 245 | value->u.object.length = 0; |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | value->_reserved.object_mem = (void *) (((char *) value->u.object.values) + values_size); |
| 250 | |
| 251 | value->u.object.length = 0; |
| 252 | break; |
| 253 | |
| 254 | case json_string: |
| 255 | |
| 256 | #ifndef JSON_NO_PEDANTIC_OVERFLOW |
| 257 | /* the first pass should already have handled this */ |
| 258 | if (value->u.string.length > JSON_STRING_LENGTH_MAX) |
| 259 | { |
| 260 | return 0; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | if (! (value->u.string.ptr = (json_char *) json_alloc |
| 265 | (state, size: (value->u.string.length + 1) * sizeof (json_char), zero: 0)) ) |
| 266 | { |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | value->u.string.length = 0; |
| 271 | break; |
| 272 | |
| 273 | default: |
| 274 | break; |
| 275 | }; |
| 276 | |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | if (! (value = (json_value *) json_alloc |
| 281 | (state, size: sizeof (json_value) + state->settings.value_extra, zero: 1))) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | if (!*root) |
| 287 | *root = value; |
| 288 | |
| 289 | value->type = type; |
| 290 | value->parent = *top; |
| 291 | |
| 292 | #ifdef JSON_TRACK_SOURCE |
| 293 | value->line = state->cur_line; |
| 294 | value->col = state->cur_col; |
| 295 | #endif |
| 296 | |
| 297 | if (*alloc) |
| 298 | (*alloc)->_reserved.next_alloc = value; |
| 299 | |
| 300 | *alloc = *top = value; |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | #define whitespace \ |
| 306 | case '\n': ++ state.cur_line; state.cur_col = 0; /* FALLTHRU */ \ |
| 307 | case ' ': /* FALLTHRU */ case '\t': /* FALLTHRU */ case '\r' |
| 308 | |
| 309 | #define string_add(b) \ |
| 310 | do { if (!state.first_pass) string [string_length] = b; ++ string_length; } while (0); |
| 311 | |
| 312 | #define line_and_col \ |
| 313 | state.cur_line, state.cur_col |
| 314 | |
| 315 | static const long |
| 316 | flag_next = 1 << 0, |
| 317 | flag_reproc = 1 << 1, |
| 318 | flag_need_comma = 1 << 2, |
| 319 | flag_seek_value = 1 << 3, |
| 320 | flag_escaped = 1 << 4, |
| 321 | flag_string = 1 << 5, |
| 322 | flag_need_colon = 1 << 6, |
| 323 | flag_done = 1 << 7, |
| 324 | flag_num_negative = 1 << 8, |
| 325 | flag_num_zero = 1 << 9, |
| 326 | flag_num_e = 1 << 10, |
| 327 | flag_num_e_got_sign = 1 << 11, |
| 328 | flag_num_e_negative = 1 << 12, |
| 329 | = 1 << 13, |
| 330 | = 1 << 14, |
| 331 | flag_num_got_decimal = 1 << 15; |
| 332 | |
| 333 | json_value * json_parse_ex (json_settings * settings, |
| 334 | const json_char * json, |
| 335 | size_t length, |
| 336 | char * error_buf) |
| 337 | { |
| 338 | char error [json_error_max]; |
| 339 | const json_char * end; |
| 340 | json_value * top, * root, * alloc = 0; |
| 341 | json_state state = { 0 }; |
| 342 | long flags = 0; |
| 343 | int num_digits = 0; |
| 344 | double num_e = 0, num_fraction = 0; |
| 345 | |
| 346 | /* Skip UTF-8 BOM |
| 347 | */ |
| 348 | if (length >= 3 && ((unsigned char) json [0]) == 0xEF |
| 349 | && ((unsigned char) json [1]) == 0xBB |
| 350 | && ((unsigned char) json [2]) == 0xBF) |
| 351 | { |
| 352 | json += 3; |
| 353 | length -= 3; |
| 354 | } |
| 355 | |
| 356 | error[0] = '\0'; |
| 357 | end = (json + length); |
| 358 | |
| 359 | memcpy (dest: &state.settings, src: settings, n: sizeof (json_settings)); |
| 360 | |
| 361 | if (!state.settings.mem_alloc) |
| 362 | state.settings.mem_alloc = default_alloc; |
| 363 | |
| 364 | if (!state.settings.mem_free) |
| 365 | state.settings.mem_free = default_free; |
| 366 | |
| 367 | for (state.first_pass = 1; state.first_pass >= 0; -- state.first_pass) |
| 368 | { |
| 369 | json_uchar uchar; |
| 370 | unsigned char uc_b1, uc_b2, uc_b3, uc_b4; |
| 371 | json_char * string = 0; |
| 372 | unsigned int string_length = 0; |
| 373 | |
| 374 | top = root = 0; |
| 375 | flags = flag_seek_value; |
| 376 | |
| 377 | state.cur_line = 1; |
| 378 | |
| 379 | for (state.ptr = json ;; ++ state.ptr) |
| 380 | { |
| 381 | json_char b = (state.ptr == end ? 0 : *state.ptr); |
| 382 | |
| 383 | if (flags & flag_string) |
| 384 | { |
| 385 | if (!b) |
| 386 | { sprintf (s: error, format: "%u:%u: Unexpected EOF in string" , line_and_col); |
| 387 | goto e_failed; |
| 388 | } |
| 389 | |
| 390 | if (string_length > JSON_STRING_LENGTH_MAX) |
| 391 | goto e_overflow; |
| 392 | |
| 393 | if (flags & flag_escaped) |
| 394 | { |
| 395 | flags &= ~ flag_escaped; |
| 396 | |
| 397 | switch (b) |
| 398 | { |
| 399 | case 'b': string_add ('\b'); break; |
| 400 | case 'f': string_add ('\f'); break; |
| 401 | case 'n': string_add ('\n'); break; |
| 402 | case 'r': string_add ('\r'); break; |
| 403 | case 't': string_add ('\t'); break; |
| 404 | case 'u': |
| 405 | |
| 406 | if (end - state.ptr <= 4 || |
| 407 | (uc_b1 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 408 | (uc_b2 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 409 | (uc_b3 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 410 | (uc_b4 = hex_value (c: *++ state.ptr)) == 0xFF) |
| 411 | { |
| 412 | sprintf (s: error, format: "%u:%u: Invalid character value `%c`" , line_and_col, b); |
| 413 | goto e_failed; |
| 414 | } |
| 415 | |
| 416 | uc_b1 = (uc_b1 << 4) | uc_b2; |
| 417 | uc_b2 = (uc_b3 << 4) | uc_b4; |
| 418 | uchar = (uc_b1 << 8) | uc_b2; |
| 419 | |
| 420 | if ((uchar & 0xF800) == 0xD800) { |
| 421 | json_uchar uchar2; |
| 422 | |
| 423 | if (end - state.ptr <= 6 || (*++ state.ptr) != '\\' || (*++ state.ptr) != 'u' || |
| 424 | (uc_b1 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 425 | (uc_b2 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 426 | (uc_b3 = hex_value (c: *++ state.ptr)) == 0xFF || |
| 427 | (uc_b4 = hex_value (c: *++ state.ptr)) == 0xFF) |
| 428 | { |
| 429 | sprintf (s: error, format: "%u:%u: Invalid character value `%c`" , line_and_col, b); |
| 430 | goto e_failed; |
| 431 | } |
| 432 | |
| 433 | uc_b1 = (uc_b1 << 4) | uc_b2; |
| 434 | uc_b2 = (uc_b3 << 4) | uc_b4; |
| 435 | uchar2 = (uc_b1 << 8) | uc_b2; |
| 436 | |
| 437 | uchar = 0x010000 | ((uchar & 0x3FF) << 10) | (uchar2 & 0x3FF); |
| 438 | } |
| 439 | |
| 440 | if (sizeof (json_char) >= sizeof (json_uchar) || (uchar <= 0x7F)) |
| 441 | { |
| 442 | string_add ((json_char) uchar); |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | if (uchar <= 0x7FF) |
| 447 | { |
| 448 | if (JSON_STRING_LENGTH_MAX - string_length < 2) |
| 449 | goto e_overflow; |
| 450 | if (state.first_pass) |
| 451 | string_length += 2; |
| 452 | else |
| 453 | { string [string_length ++] = 0xC0 | (uchar >> 6); |
| 454 | string [string_length ++] = 0x80 | (uchar & 0x3F); |
| 455 | } |
| 456 | |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | if (uchar <= 0xFFFF) { |
| 461 | if (JSON_STRING_LENGTH_MAX - string_length < 3) |
| 462 | goto e_overflow; |
| 463 | if (state.first_pass) |
| 464 | string_length += 3; |
| 465 | else |
| 466 | { string [string_length ++] = 0xE0 | (uchar >> 12); |
| 467 | string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F); |
| 468 | string [string_length ++] = 0x80 | (uchar & 0x3F); |
| 469 | } |
| 470 | |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | if (JSON_STRING_LENGTH_MAX - string_length < 4) |
| 475 | goto e_overflow; |
| 476 | if (state.first_pass) |
| 477 | string_length += 4; |
| 478 | else |
| 479 | { string [string_length ++] = 0xF0 | (uchar >> 18); |
| 480 | string [string_length ++] = 0x80 | ((uchar >> 12) & 0x3F); |
| 481 | string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F); |
| 482 | string [string_length ++] = 0x80 | (uchar & 0x3F); |
| 483 | } |
| 484 | |
| 485 | break; |
| 486 | |
| 487 | default: |
| 488 | string_add (b); |
| 489 | }; |
| 490 | |
| 491 | continue; |
| 492 | } |
| 493 | |
| 494 | if (b == '\\') |
| 495 | { |
| 496 | flags |= flag_escaped; |
| 497 | continue; |
| 498 | } |
| 499 | |
| 500 | if (b == '"') |
| 501 | { |
| 502 | if (!state.first_pass) |
| 503 | string [string_length] = 0; |
| 504 | |
| 505 | flags &= ~ flag_string; |
| 506 | string = 0; |
| 507 | |
| 508 | switch (top->type) |
| 509 | { |
| 510 | case json_string: |
| 511 | |
| 512 | top->u.string.length = string_length; |
| 513 | flags |= flag_next; |
| 514 | |
| 515 | break; |
| 516 | |
| 517 | case json_object: |
| 518 | |
| 519 | /* |
| 520 | The `values` pointer holds an increasing size during the first pass. |
| 521 | The size represents the storage space needed for object key names. |
| 522 | It's used by `new_value` when transitioning into the second pass. |
| 523 | From then on, it's a pointer to allocated memory. |
| 524 | */ |
| 525 | if (state.first_pass) { |
| 526 | JSON_UINTPTR_T chars = (JSON_UINTPTR_T) top->u.object.values; |
| 527 | |
| 528 | if (chars >= JSON_OBJECT_NAMES_COMBINED_LENGTH_MAX || JSON_OBJECT_NAMES_COMBINED_LENGTH_MAX - chars <= string_length) |
| 529 | goto e_overflow; |
| 530 | |
| 531 | chars += string_length; |
| 532 | ++chars; /* space for null terminator */ |
| 533 | top->u.object.values = (json_object_entry *) chars; |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | top->u.object.values [top->u.object.length].name |
| 538 | = (json_char *) top->_reserved.object_mem; |
| 539 | |
| 540 | top->u.object.values [top->u.object.length].name_length |
| 541 | = string_length; |
| 542 | |
| 543 | (*(json_char **) &top->_reserved.object_mem) += string_length + 1; |
| 544 | } |
| 545 | |
| 546 | flags |= flag_seek_value | flag_need_colon; |
| 547 | continue; |
| 548 | |
| 549 | default: |
| 550 | break; |
| 551 | }; |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | string_add (b); |
| 556 | continue; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (state.settings.settings & json_enable_comments) |
| 561 | { |
| 562 | if (flags & (flag_line_comment | flag_block_comment)) |
| 563 | { |
| 564 | if (flags & flag_line_comment) |
| 565 | { |
| 566 | if (b == '\r' || b == '\n' || !b) |
| 567 | { |
| 568 | flags &= ~ flag_line_comment; |
| 569 | -- state.ptr; /* so null can be reproc'd */ |
| 570 | } |
| 571 | |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | if (flags & flag_block_comment) |
| 576 | { |
| 577 | if (!b) |
| 578 | { sprintf (s: error, format: "%u:%u: Unexpected EOF in block comment" , line_and_col); |
| 579 | goto e_failed; |
| 580 | } |
| 581 | |
| 582 | if (b == '*' && state.ptr < (end - 1) && state.ptr [1] == '/') |
| 583 | { |
| 584 | flags &= ~ flag_block_comment; |
| 585 | ++ state.ptr; /* skip closing sequence */ |
| 586 | } |
| 587 | |
| 588 | continue; |
| 589 | } |
| 590 | } |
| 591 | else if (b == '/') |
| 592 | { |
| 593 | if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object) |
| 594 | { sprintf (s: error, format: "%u:%u: Comment not allowed here" , line_and_col); |
| 595 | goto e_failed; |
| 596 | } |
| 597 | |
| 598 | if (++ state.ptr == end) |
| 599 | { sprintf (s: error, format: "%u:%u: EOF unexpected" , line_and_col); |
| 600 | goto e_failed; |
| 601 | } |
| 602 | |
| 603 | switch (b = *state.ptr) |
| 604 | { |
| 605 | case '/': |
| 606 | flags |= flag_line_comment; |
| 607 | continue; |
| 608 | |
| 609 | case '*': |
| 610 | flags |= flag_block_comment; |
| 611 | continue; |
| 612 | |
| 613 | default: |
| 614 | sprintf (s: error, format: "%u:%u: Unexpected `%c` in comment opening sequence" , line_and_col, b); |
| 615 | goto e_failed; |
| 616 | }; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (flags & flag_done) |
| 621 | { |
| 622 | if (!b) |
| 623 | break; |
| 624 | |
| 625 | switch (b) |
| 626 | { |
| 627 | whitespace: |
| 628 | continue; |
| 629 | |
| 630 | default: |
| 631 | |
| 632 | sprintf (s: error, format: "%u:%u: Trailing garbage: `%c`" , |
| 633 | line_and_col, b); |
| 634 | |
| 635 | goto e_failed; |
| 636 | }; |
| 637 | } |
| 638 | |
| 639 | if (flags & flag_seek_value) |
| 640 | { |
| 641 | switch (b) |
| 642 | { |
| 643 | whitespace: |
| 644 | continue; |
| 645 | |
| 646 | case ']': |
| 647 | |
| 648 | if (top && top->type == json_array) |
| 649 | flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next; |
| 650 | else |
| 651 | { sprintf (s: error, format: "%u:%u: Unexpected `]`" , line_and_col); |
| 652 | goto e_failed; |
| 653 | } |
| 654 | |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | |
| 659 | if (flags & flag_need_comma) |
| 660 | { |
| 661 | if (b == ',') |
| 662 | { flags &= ~ flag_need_comma; |
| 663 | continue; |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | sprintf (s: error, format: "%u:%u: Expected `,` before `%c`" , |
| 668 | line_and_col, b); |
| 669 | |
| 670 | goto e_failed; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (flags & flag_need_colon) |
| 675 | { |
| 676 | if (b == ':') |
| 677 | { flags &= ~ flag_need_colon; |
| 678 | continue; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | sprintf (s: error, format: "%u:%u: Expected `:` before `%c`" , |
| 683 | line_and_col, b); |
| 684 | |
| 685 | goto e_failed; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | flags &= ~ flag_seek_value; |
| 690 | |
| 691 | switch (b) |
| 692 | { |
| 693 | case '{': |
| 694 | |
| 695 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_object)) |
| 696 | goto e_alloc_failure; |
| 697 | |
| 698 | continue; |
| 699 | |
| 700 | case '[': |
| 701 | |
| 702 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_array)) |
| 703 | goto e_alloc_failure; |
| 704 | |
| 705 | flags |= flag_seek_value; |
| 706 | continue; |
| 707 | |
| 708 | case '"': |
| 709 | |
| 710 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_string)) |
| 711 | goto e_alloc_failure; |
| 712 | |
| 713 | flags |= flag_string; |
| 714 | |
| 715 | string = top->u.string.ptr; |
| 716 | string_length = 0; |
| 717 | |
| 718 | continue; |
| 719 | |
| 720 | case 't': |
| 721 | |
| 722 | if ((end - state.ptr) <= 3 || *(++ state.ptr) != 'r' || |
| 723 | *(++ state.ptr) != 'u' || *(++ state.ptr) != 'e') |
| 724 | { |
| 725 | goto e_unknown_value; |
| 726 | } |
| 727 | |
| 728 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_boolean)) |
| 729 | goto e_alloc_failure; |
| 730 | |
| 731 | top->u.boolean = 1; |
| 732 | |
| 733 | flags |= flag_next; |
| 734 | break; |
| 735 | |
| 736 | case 'f': |
| 737 | |
| 738 | if ((end - state.ptr) <= 4 || *(++ state.ptr) != 'a' || |
| 739 | *(++ state.ptr) != 'l' || *(++ state.ptr) != 's' || |
| 740 | *(++ state.ptr) != 'e') |
| 741 | { |
| 742 | goto e_unknown_value; |
| 743 | } |
| 744 | |
| 745 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_boolean)) |
| 746 | goto e_alloc_failure; |
| 747 | |
| 748 | flags |= flag_next; |
| 749 | break; |
| 750 | |
| 751 | case 'n': |
| 752 | |
| 753 | if ((end - state.ptr) <= 3 || *(++ state.ptr) != 'u' || |
| 754 | *(++ state.ptr) != 'l' || *(++ state.ptr) != 'l') |
| 755 | { |
| 756 | goto e_unknown_value; |
| 757 | } |
| 758 | |
| 759 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_null)) |
| 760 | goto e_alloc_failure; |
| 761 | |
| 762 | flags |= flag_next; |
| 763 | break; |
| 764 | |
| 765 | default: |
| 766 | |
| 767 | if (isdigit ((unsigned char) b) || b == '-') |
| 768 | { |
| 769 | if (!new_value (state: &state, top: &top, root: &root, alloc: &alloc, type: json_integer)) |
| 770 | goto e_alloc_failure; |
| 771 | |
| 772 | if (!state.first_pass) |
| 773 | { |
| 774 | while (isdigit ((unsigned char) b) || b == '+' || b == '-' |
| 775 | || b == 'e' || b == 'E' || b == '.') |
| 776 | { |
| 777 | if ( (++ state.ptr) == end) |
| 778 | { |
| 779 | b = 0; |
| 780 | break; |
| 781 | } |
| 782 | |
| 783 | b = *state.ptr; |
| 784 | } |
| 785 | |
| 786 | flags |= flag_next | flag_reproc; |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | flags &= ~ (flag_num_negative | flag_num_e | |
| 791 | flag_num_e_got_sign | flag_num_e_negative | |
| 792 | flag_num_zero); |
| 793 | |
| 794 | num_digits = 0; |
| 795 | num_fraction = 0; |
| 796 | num_e = 0; |
| 797 | |
| 798 | if (b != '-') |
| 799 | { |
| 800 | flags |= flag_reproc; |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | flags |= flag_num_negative; |
| 805 | continue; |
| 806 | } |
| 807 | else |
| 808 | { sprintf (s: error, format: "%u:%u: Unexpected `%c` when seeking value" , line_and_col, b); |
| 809 | goto e_failed; |
| 810 | } |
| 811 | }; |
| 812 | }; |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | switch (top->type) |
| 817 | { |
| 818 | case json_object: |
| 819 | |
| 820 | switch (b) |
| 821 | { |
| 822 | whitespace: |
| 823 | continue; |
| 824 | |
| 825 | case '"': |
| 826 | |
| 827 | if (flags & flag_need_comma) |
| 828 | { sprintf (s: error, format: "%u:%u: Expected `,` before `\"`" , line_and_col); |
| 829 | goto e_failed; |
| 830 | } |
| 831 | |
| 832 | flags |= flag_string; |
| 833 | |
| 834 | string = (json_char *) top->_reserved.object_mem; |
| 835 | string_length = 0; |
| 836 | |
| 837 | break; |
| 838 | |
| 839 | case '}': |
| 840 | |
| 841 | flags = (flags & ~ flag_need_comma) | flag_next; |
| 842 | break; |
| 843 | |
| 844 | case ',': |
| 845 | |
| 846 | if (flags & flag_need_comma) |
| 847 | { |
| 848 | flags &= ~ flag_need_comma; |
| 849 | break; |
| 850 | } /* FALLTHRU */ |
| 851 | |
| 852 | default: |
| 853 | sprintf (s: error, format: "%u:%u: Unexpected `%c` in object" , line_and_col, b); |
| 854 | goto e_failed; |
| 855 | }; |
| 856 | |
| 857 | break; |
| 858 | |
| 859 | case json_integer: |
| 860 | case json_double: |
| 861 | |
| 862 | if (isdigit ((unsigned char)b)) |
| 863 | { |
| 864 | ++ num_digits; |
| 865 | |
| 866 | if (top->type == json_integer || flags & flag_num_e) |
| 867 | { |
| 868 | if (! (flags & flag_num_e)) |
| 869 | { |
| 870 | if (flags & flag_num_zero) |
| 871 | { sprintf (s: error, format: "%u:%u: Unexpected `0` before `%c`" , line_and_col, b); |
| 872 | goto e_failed; |
| 873 | } |
| 874 | |
| 875 | if (num_digits == 1 && b == '0') |
| 876 | flags |= flag_num_zero; |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | flags |= flag_num_e_got_sign; |
| 881 | num_e = (num_e * 10) + (b - '0'); |
| 882 | continue; |
| 883 | } |
| 884 | |
| 885 | if (would_overflow(value: top->u.integer, b)) |
| 886 | { |
| 887 | json_int_t integer = top->u.integer; |
| 888 | -- num_digits; |
| 889 | -- state.ptr; |
| 890 | top->type = json_double; |
| 891 | top->u.dbl = (double)integer; |
| 892 | continue; |
| 893 | } |
| 894 | |
| 895 | top->u.integer = (top->u.integer * 10) + (b - '0'); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | if (flags & flag_num_got_decimal) |
| 900 | num_fraction = (num_fraction * 10) + (b - '0'); |
| 901 | else |
| 902 | top->u.dbl = (top->u.dbl * 10) + (b - '0'); |
| 903 | |
| 904 | continue; |
| 905 | } |
| 906 | |
| 907 | if (b == '+' || b == '-') |
| 908 | { |
| 909 | if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign)) |
| 910 | { |
| 911 | flags |= flag_num_e_got_sign; |
| 912 | |
| 913 | if (b == '-') |
| 914 | flags |= flag_num_e_negative; |
| 915 | |
| 916 | continue; |
| 917 | } |
| 918 | } |
| 919 | else if (b == '.' && top->type == json_integer) |
| 920 | { |
| 921 | json_int_t integer = top->u.integer; |
| 922 | |
| 923 | if (!num_digits) |
| 924 | { sprintf (s: error, format: "%u:%u: Expected digit before `.`" , line_and_col); |
| 925 | goto e_failed; |
| 926 | } |
| 927 | |
| 928 | top->type = json_double; |
| 929 | top->u.dbl = (double) integer; |
| 930 | |
| 931 | flags |= flag_num_got_decimal; |
| 932 | num_digits = 0; |
| 933 | continue; |
| 934 | } |
| 935 | |
| 936 | if (! (flags & flag_num_e)) |
| 937 | { |
| 938 | if (top->type == json_double) |
| 939 | { |
| 940 | if (!num_digits) |
| 941 | { sprintf (s: error, format: "%u:%u: Expected digit after `.`" , line_and_col); |
| 942 | goto e_failed; |
| 943 | } |
| 944 | |
| 945 | top->u.dbl += num_fraction / pow (x: 10.0, y: num_digits); |
| 946 | } |
| 947 | |
| 948 | if (b == 'e' || b == 'E') |
| 949 | { |
| 950 | flags |= flag_num_e; |
| 951 | |
| 952 | if (top->type == json_integer) |
| 953 | { |
| 954 | json_int_t integer = top->u.integer; |
| 955 | top->type = json_double; |
| 956 | top->u.dbl = (double) integer; |
| 957 | } |
| 958 | |
| 959 | num_digits = 0; |
| 960 | flags &= ~ flag_num_zero; |
| 961 | |
| 962 | continue; |
| 963 | } |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | if (!num_digits) |
| 968 | { sprintf (s: error, format: "%u:%u: Expected digit after `e`" , line_and_col); |
| 969 | goto e_failed; |
| 970 | } |
| 971 | |
| 972 | top->u.dbl *= pow (x: 10.0, y: (flags & flag_num_e_negative ? - num_e : num_e)); |
| 973 | } |
| 974 | |
| 975 | if (flags & flag_num_negative) |
| 976 | { |
| 977 | if (top->type == json_integer) |
| 978 | top->u.integer = - top->u.integer; |
| 979 | else |
| 980 | top->u.dbl = - top->u.dbl; |
| 981 | } |
| 982 | |
| 983 | flags |= flag_next | flag_reproc; |
| 984 | break; |
| 985 | |
| 986 | default: |
| 987 | break; |
| 988 | }; |
| 989 | } |
| 990 | |
| 991 | if (flags & flag_reproc) |
| 992 | { |
| 993 | flags &= ~ flag_reproc; |
| 994 | -- state.ptr; |
| 995 | } |
| 996 | |
| 997 | if (flags & flag_next) |
| 998 | { |
| 999 | flags = (flags & ~ flag_next) | flag_need_comma; |
| 1000 | |
| 1001 | if (!top->parent) |
| 1002 | { |
| 1003 | /* root value done */ |
| 1004 | |
| 1005 | flags |= flag_done; |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | if (top->parent->type == json_array) |
| 1010 | flags |= flag_seek_value; |
| 1011 | |
| 1012 | if (!state.first_pass) |
| 1013 | { |
| 1014 | json_value * parent = top->parent; |
| 1015 | |
| 1016 | switch (parent->type) |
| 1017 | { |
| 1018 | case json_object: |
| 1019 | |
| 1020 | parent->u.object.values |
| 1021 | [parent->u.object.length].value = top; |
| 1022 | |
| 1023 | break; |
| 1024 | |
| 1025 | case json_array: |
| 1026 | |
| 1027 | parent->u.array.values |
| 1028 | [parent->u.array.length] = top; |
| 1029 | |
| 1030 | break; |
| 1031 | |
| 1032 | default: /* unreachable */ |
| 1033 | break; |
| 1034 | }; |
| 1035 | } |
| 1036 | |
| 1037 | switch (top->parent->type) |
| 1038 | { |
| 1039 | case json_object: |
| 1040 | |
| 1041 | if ( (++ top->parent->u.object.length) > JSON_OBJECT_ELEMENTS_MAX) |
| 1042 | goto e_overflow; |
| 1043 | |
| 1044 | /* |
| 1045 | The `values` pointer holds an increasing size during the first pass. |
| 1046 | The size represents the storage space needed for object key names. |
| 1047 | It's used by `new_value` when transitioning into the second pass. |
| 1048 | A single allocation must be able to contain the object entries and names. |
| 1049 | */ |
| 1050 | if (state.first_pass |
| 1051 | && JSON_SIZE_MAX - top->parent->u.object.length * sizeof (json_object_entry) |
| 1052 | < ((JSON_UINTPTR_T) top->parent->u.object.values) ) |
| 1053 | goto e_overflow; |
| 1054 | |
| 1055 | break; |
| 1056 | |
| 1057 | case json_array: |
| 1058 | |
| 1059 | if ( (++ top->parent->u.array.length) > JSON_ARRAY_ELEMENTS_MAX) |
| 1060 | goto e_overflow; |
| 1061 | |
| 1062 | break; |
| 1063 | |
| 1064 | default: /* unreachable */ |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | top = top->parent; |
| 1069 | |
| 1070 | continue; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | alloc = root; |
| 1075 | } |
| 1076 | |
| 1077 | return root; |
| 1078 | |
| 1079 | e_unknown_value: |
| 1080 | |
| 1081 | sprintf (s: error, format: "%u:%u: Unknown value" , line_and_col); |
| 1082 | goto e_failed; |
| 1083 | |
| 1084 | e_alloc_failure: |
| 1085 | |
| 1086 | strcpy (dest: error, src: "Memory allocation failure" ); |
| 1087 | goto e_failed; |
| 1088 | |
| 1089 | e_overflow: |
| 1090 | |
| 1091 | sprintf (s: error, format: "%u:%u: Too long (caught overflow)" , line_and_col); |
| 1092 | goto e_failed; |
| 1093 | |
| 1094 | e_failed: |
| 1095 | |
| 1096 | if (error_buf) |
| 1097 | { |
| 1098 | if (*error) |
| 1099 | strcpy (dest: error_buf, src: error); |
| 1100 | else |
| 1101 | strcpy (dest: error_buf, src: "Unknown error" ); |
| 1102 | } |
| 1103 | |
| 1104 | if (state.first_pass) |
| 1105 | alloc = root; |
| 1106 | |
| 1107 | while (alloc) |
| 1108 | { |
| 1109 | top = alloc->_reserved.next_alloc; |
| 1110 | state.settings.mem_free (alloc, state.settings.user_data); |
| 1111 | alloc = top; |
| 1112 | } |
| 1113 | |
| 1114 | if (!state.first_pass) |
| 1115 | json_value_free_ex (settings: &state.settings, root); |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | json_value * json_parse (const json_char * json, size_t length) |
| 1121 | { |
| 1122 | json_settings settings = { 0 }; |
| 1123 | return json_parse_ex (settings: &settings, json, length, error_buf: 0); |
| 1124 | } |
| 1125 | |
| 1126 | void json_value_free_ex (json_settings * settings, json_value * value) |
| 1127 | { |
| 1128 | json_value * cur_value; |
| 1129 | |
| 1130 | if (!value) |
| 1131 | return; |
| 1132 | |
| 1133 | value->parent = 0; |
| 1134 | |
| 1135 | while (value) |
| 1136 | { |
| 1137 | switch (value->type) |
| 1138 | { |
| 1139 | case json_array: |
| 1140 | |
| 1141 | if (!value->u.array.length) |
| 1142 | { |
| 1143 | settings->mem_free (value->u.array.values, settings->user_data); |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | value = value->u.array.values [-- value->u.array.length]; |
| 1148 | continue; |
| 1149 | |
| 1150 | case json_object: |
| 1151 | |
| 1152 | if (!value->u.object.length) |
| 1153 | { |
| 1154 | settings->mem_free (value->u.object.values, settings->user_data); |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | value = value->u.object.values [-- value->u.object.length].value; |
| 1159 | continue; |
| 1160 | |
| 1161 | case json_string: |
| 1162 | |
| 1163 | settings->mem_free (value->u.string.ptr, settings->user_data); |
| 1164 | break; |
| 1165 | |
| 1166 | default: |
| 1167 | break; |
| 1168 | }; |
| 1169 | |
| 1170 | cur_value = value; |
| 1171 | value = value->parent; |
| 1172 | settings->mem_free (cur_value, settings->user_data); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | void json_value_free (json_value * value) |
| 1177 | { |
| 1178 | json_settings settings = { 0 }; |
| 1179 | settings.mem_free = default_free; |
| 1180 | json_value_free_ex (settings: &settings, value); |
| 1181 | } |
| 1182 | |