[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 | |
---|
[5ebff60] | 142 | value->_reserved.object_mem = (*(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) { |
---|
| 409 | (*(json_char **) &top->u.object.values) += string_length + 1; |
---|
| 410 | } else { |
---|
| 411 | top->u.object.values [top->u.object.length].name |
---|
| 412 | = (json_char *) top->_reserved.object_mem; |
---|
| 413 | |
---|
| 414 | top->u.object.values [top->u.object.length].name_length |
---|
| 415 | = string_length; |
---|
| 416 | |
---|
| 417 | (*(json_char **) &top->_reserved.object_mem) += string_length + |
---|
| 418 | 1; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | flags |= flag_seek_value | flag_need_colon; |
---|
| 422 | continue; |
---|
| 423 | |
---|
| 424 | default: |
---|
| 425 | break; |
---|
| 426 | } |
---|
| 427 | ; |
---|
| 428 | } else { |
---|
| 429 | string_add(b); |
---|
| 430 | continue; |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | if (state.settings.settings & json_enable_comments) { |
---|
| 435 | if (flags & (flag_line_comment | flag_block_comment)) { |
---|
| 436 | if (flags & flag_line_comment) { |
---|
| 437 | if (b == '\r' || b == '\n' || !b) { |
---|
| 438 | flags &= ~flag_line_comment; |
---|
| 439 | --i; /* so null can be reproc'd */ |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | continue; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | if (flags & flag_block_comment) { |
---|
| 446 | if (!b) { |
---|
| 447 | sprintf(error, "%d:%d: Unexpected EOF in block comment", |
---|
| 448 | cur_line, e_off); |
---|
| 449 | goto e_failed; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | if (b == '*' && i < (end - 1) && i [1] == '/') { |
---|
| 453 | flags &= ~flag_block_comment; |
---|
| 454 | ++i; /* skip closing sequence */ |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | continue; |
---|
| 458 | } |
---|
| 459 | } else if (b == '/') { |
---|
| 460 | if (!(flags & (flag_seek_value | flag_done)) && top->type != json_object) { |
---|
| 461 | sprintf(error, "%d:%d: Comment not allowed here", cur_line, e_off); |
---|
| 462 | goto e_failed; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if (++i == end) { |
---|
| 466 | sprintf(error, "%d:%d: EOF unexpected", cur_line, e_off); |
---|
| 467 | goto e_failed; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | switch (b = *i) { |
---|
| 471 | case '/': |
---|
| 472 | flags |= flag_line_comment; |
---|
| 473 | continue; |
---|
| 474 | |
---|
| 475 | case '*': |
---|
| 476 | flags |= flag_block_comment; |
---|
| 477 | continue; |
---|
| 478 | |
---|
| 479 | default: |
---|
| 480 | sprintf(error, "%d:%d: Unexpected `%c` in comment opening sequence", |
---|
| 481 | cur_line, e_off, b); |
---|
| 482 | goto e_failed; |
---|
| 483 | } |
---|
| 484 | ; |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | if (flags & flag_done) { |
---|
| 489 | if (!b) { |
---|
| 490 | break; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | switch (b) { |
---|
| 494 | whitespace: |
---|
| 495 | continue; |
---|
| 496 | |
---|
| 497 | default: |
---|
| 498 | sprintf(error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b); |
---|
| 499 | goto e_failed; |
---|
| 500 | } |
---|
| 501 | ; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | if (flags & flag_seek_value) { |
---|
| 505 | switch (b) { |
---|
| 506 | whitespace: |
---|
| 507 | continue; |
---|
| 508 | |
---|
| 509 | case ']': |
---|
| 510 | |
---|
[77ed9b7] | 511 | if (top && top->type == json_array) { |
---|
[5ebff60] | 512 | flags = (flags & ~(flag_need_comma | flag_seek_value)) | flag_next; |
---|
| 513 | } else { sprintf(error, "%d:%d: Unexpected ]", cur_line, e_off); |
---|
| 514 | goto e_failed; } |
---|
| 515 | |
---|
| 516 | break; |
---|
| 517 | |
---|
| 518 | default: |
---|
| 519 | |
---|
| 520 | if (flags & flag_need_comma) { |
---|
| 521 | if (b == ',') { |
---|
| 522 | flags &= ~flag_need_comma; |
---|
| 523 | continue; |
---|
| 524 | } else { sprintf(error, "%d:%d: Expected , before %c", cur_line, e_off, |
---|
| 525 | b); |
---|
| 526 | goto e_failed; } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | if (flags & flag_need_colon) { |
---|
| 530 | if (b == ':') { |
---|
| 531 | flags &= ~flag_need_colon; |
---|
| 532 | continue; |
---|
| 533 | } else { sprintf(error, "%d:%d: Expected : before %c", cur_line, e_off, |
---|
| 534 | b); |
---|
| 535 | goto e_failed; } |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | flags &= ~flag_seek_value; |
---|
[696dc9e] | 539 | |
---|
[5ebff60] | 540 | switch (b) { |
---|
| 541 | case '{': |
---|
[696dc9e] | 542 | |
---|
[5ebff60] | 543 | if (!new_value(&state, &top, &root, &alloc, json_object)) { |
---|
| 544 | goto e_alloc_failure; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | continue; |
---|
[696dc9e] | 548 | |
---|
[5ebff60] | 549 | case '[': |
---|
[696dc9e] | 550 | |
---|
[5ebff60] | 551 | if (!new_value(&state, &top, &root, &alloc, json_array)) { |
---|
| 552 | goto e_alloc_failure; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | flags |= flag_seek_value; |
---|
| 556 | continue; |
---|
| 557 | |
---|
| 558 | case '"': |
---|
| 559 | |
---|
| 560 | if (!new_value(&state, &top, &root, &alloc, json_string)) { |
---|
| 561 | goto e_alloc_failure; |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | flags |= flag_string; |
---|
| 565 | |
---|
| 566 | string = top->u.string.ptr; |
---|
| 567 | string_length = 0; |
---|
[696dc9e] | 568 | |
---|
[5ebff60] | 569 | continue; |
---|
| 570 | |
---|
| 571 | case 't': |
---|
[696dc9e] | 572 | |
---|
[5ebff60] | 573 | if ((end - i) < 3 || *(++i) != 'r' || *(++i) != 'u' || *(++i) != 'e') { |
---|
| 574 | goto e_unknown_value; |
---|
| 575 | } |
---|
[696dc9e] | 576 | |
---|
[5ebff60] | 577 | if (!new_value(&state, &top, &root, &alloc, json_boolean)) { |
---|
| 578 | goto e_alloc_failure; |
---|
| 579 | } |
---|
[696dc9e] | 580 | |
---|
[5ebff60] | 581 | top->u.boolean = 1; |
---|
[7a80925] | 582 | |
---|
[5ebff60] | 583 | flags |= flag_next; |
---|
| 584 | break; |
---|
[7a80925] | 585 | |
---|
[5ebff60] | 586 | case 'f': |
---|
[7a80925] | 587 | |
---|
[5ebff60] | 588 | if ((end - i) < 4 || *(++i) != 'a' || *(++i) != 'l' || *(++i) != 's' || |
---|
| 589 | *(++i) != 'e') { |
---|
| 590 | goto e_unknown_value; |
---|
| 591 | } |
---|
[7a80925] | 592 | |
---|
[5ebff60] | 593 | if (!new_value(&state, &top, &root, &alloc, json_boolean)) { |
---|
| 594 | goto e_alloc_failure; |
---|
| 595 | } |
---|
[7a80925] | 596 | |
---|
[5ebff60] | 597 | flags |= flag_next; |
---|
| 598 | break; |
---|
[7a80925] | 599 | |
---|
[5ebff60] | 600 | case 'n': |
---|
[696dc9e] | 601 | |
---|
[5ebff60] | 602 | if ((end - i) < 3 || *(++i) != 'u' || *(++i) != 'l' || *(++i) != 'l') { |
---|
| 603 | goto e_unknown_value; |
---|
| 604 | } |
---|
[696dc9e] | 605 | |
---|
[5ebff60] | 606 | if (!new_value(&state, &top, &root, &alloc, json_null)) { |
---|
| 607 | goto e_alloc_failure; |
---|
| 608 | } |
---|
[696dc9e] | 609 | |
---|
[5ebff60] | 610 | flags |= flag_next; |
---|
| 611 | break; |
---|
[696dc9e] | 612 | |
---|
[5ebff60] | 613 | default: |
---|
[696dc9e] | 614 | |
---|
[5ebff60] | 615 | if (g_ascii_isdigit(b) || b == '-') { |
---|
| 616 | if (!new_value(&state, &top, &root, &alloc, json_integer)) { |
---|
| 617 | goto e_alloc_failure; |
---|
| 618 | } |
---|
[696dc9e] | 619 | |
---|
[5ebff60] | 620 | if (!state.first_pass) { |
---|
| 621 | while (g_ascii_isdigit(b) || b == '+' || b == '-' |
---|
| 622 | || b == 'e' || b == 'E' || b == '.') { |
---|
| 623 | if ((++i) == end) { |
---|
| 624 | b = 0; |
---|
| 625 | break; |
---|
| 626 | } |
---|
[696dc9e] | 627 | |
---|
[5ebff60] | 628 | b = *i; |
---|
| 629 | } |
---|
[696dc9e] | 630 | |
---|
[5ebff60] | 631 | flags |= flag_next | flag_reproc; |
---|
| 632 | break; |
---|
| 633 | } |
---|
[696dc9e] | 634 | |
---|
[5ebff60] | 635 | flags &= ~(flag_num_negative | flag_num_e | |
---|
| 636 | flag_num_e_got_sign | flag_num_e_negative | |
---|
| 637 | flag_num_zero); |
---|
[696dc9e] | 638 | |
---|
[5ebff60] | 639 | num_digits = 0; |
---|
| 640 | num_fraction = 0; |
---|
| 641 | num_e = 0; |
---|
[696dc9e] | 642 | |
---|
[5ebff60] | 643 | if (b != '-') { |
---|
| 644 | flags |= flag_reproc; |
---|
| 645 | break; |
---|
| 646 | } |
---|
[696dc9e] | 647 | |
---|
[5ebff60] | 648 | flags |= flag_num_negative; |
---|
| 649 | continue; |
---|
| 650 | } else { sprintf(error, "%d:%d: Unexpected %c when seeking value", |
---|
| 651 | cur_line, e_off, b); |
---|
| 652 | goto e_failed; } |
---|
| 653 | } |
---|
| 654 | ; |
---|
| 655 | } |
---|
| 656 | ; |
---|
| 657 | } else { |
---|
| 658 | switch (top->type) { |
---|
| 659 | case json_object: |
---|
| 660 | |
---|
| 661 | switch (b) { |
---|
| 662 | whitespace: |
---|
| 663 | continue; |
---|
| 664 | |
---|
| 665 | case '"': |
---|
| 666 | |
---|
| 667 | if (flags & flag_need_comma) { |
---|
| 668 | sprintf(error, "%d:%d: Expected , before \"", cur_line, e_off); |
---|
| 669 | goto e_failed; |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | flags |= flag_string; |
---|
| 673 | |
---|
| 674 | string = (json_char *) top->_reserved.object_mem; |
---|
| 675 | string_length = 0; |
---|
| 676 | |
---|
| 677 | break; |
---|
| 678 | |
---|
| 679 | case '}': |
---|
| 680 | |
---|
| 681 | flags = (flags & ~flag_need_comma) | flag_next; |
---|
| 682 | break; |
---|
| 683 | |
---|
| 684 | case ',': |
---|
| 685 | |
---|
| 686 | if (flags & flag_need_comma) { |
---|
| 687 | flags &= ~flag_need_comma; |
---|
| 688 | break; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | default: |
---|
| 692 | |
---|
| 693 | sprintf(error, "%d:%d: Unexpected `%c` in object", cur_line, e_off, b); |
---|
| 694 | goto e_failed; |
---|
| 695 | } |
---|
| 696 | ; |
---|
| 697 | |
---|
| 698 | break; |
---|
| 699 | |
---|
| 700 | case json_integer: |
---|
| 701 | case json_double: |
---|
| 702 | |
---|
| 703 | if (g_ascii_isdigit(b)) { |
---|
| 704 | ++num_digits; |
---|
| 705 | |
---|
| 706 | if (top->type == json_integer || flags & flag_num_e) { |
---|
| 707 | if (!(flags & flag_num_e)) { |
---|
| 708 | if (flags & flag_num_zero) { |
---|
| 709 | sprintf(error, |
---|
| 710 | "%d:%d: Unexpected `0` before `%c`", |
---|
| 711 | cur_line, e_off, b); |
---|
| 712 | goto e_failed; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | if (num_digits == 1 && b == '0') { |
---|
| 716 | flags |= flag_num_zero; |
---|
| 717 | } |
---|
| 718 | } else { |
---|
| 719 | flags |= flag_num_e_got_sign; |
---|
| 720 | num_e = (num_e * 10) + (b - '0'); |
---|
| 721 | continue; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | top->u.integer = (top->u.integer * 10) + (b - '0'); |
---|
| 725 | continue; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | num_fraction = (num_fraction * 10) + (b - '0'); |
---|
| 729 | continue; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | if (b == '+' || b == '-') { |
---|
| 733 | if ((flags & flag_num_e) && !(flags & flag_num_e_got_sign)) { |
---|
| 734 | flags |= flag_num_e_got_sign; |
---|
| 735 | |
---|
| 736 | if (b == '-') { |
---|
| 737 | flags |= flag_num_e_negative; |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | continue; |
---|
| 741 | } |
---|
| 742 | } else if (b == '.' && top->type == json_integer) { |
---|
| 743 | if (!num_digits) { |
---|
| 744 | sprintf(error, "%d:%d: Expected digit before `.`", cur_line, |
---|
| 745 | e_off); |
---|
| 746 | goto e_failed; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | top->type = json_double; |
---|
| 750 | top->u.dbl = (double) top->u.integer; |
---|
| 751 | |
---|
| 752 | num_digits = 0; |
---|
| 753 | continue; |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | if (!(flags & flag_num_e)) { |
---|
| 757 | if (top->type == json_double) { |
---|
| 758 | if (!num_digits) { |
---|
| 759 | sprintf(error, "%d:%d: Expected digit after `.`", |
---|
| 760 | cur_line, e_off); |
---|
| 761 | goto e_failed; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | top->u.dbl += ((double) num_fraction) / |
---|
| 765 | (pow(10, (double) num_digits)); |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | if (b == 'e' || b == 'E') { |
---|
| 769 | flags |= flag_num_e; |
---|
| 770 | |
---|
| 771 | if (top->type == json_integer) { |
---|
| 772 | top->type = json_double; |
---|
| 773 | top->u.dbl = (double) top->u.integer; |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | num_digits = 0; |
---|
| 777 | flags &= ~flag_num_zero; |
---|
| 778 | |
---|
| 779 | continue; |
---|
| 780 | } |
---|
| 781 | } else { |
---|
| 782 | if (!num_digits) { |
---|
| 783 | sprintf(error, "%d:%d: Expected digit after `e`", cur_line, |
---|
| 784 | e_off); |
---|
| 785 | goto e_failed; |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | top->u.dbl *= |
---|
| 789 | pow(10, |
---|
| 790 | (double) (flags & flag_num_e_negative ? -num_e : num_e)); |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | if (flags & flag_num_negative) { |
---|
| 794 | if (top->type == json_integer) { |
---|
| 795 | top->u.integer = -top->u.integer; |
---|
| 796 | } else { |
---|
| 797 | top->u.dbl = -top->u.dbl; |
---|
| 798 | } |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | flags |= flag_next | flag_reproc; |
---|
| 802 | break; |
---|
| 803 | |
---|
| 804 | default: |
---|
| 805 | break; |
---|
| 806 | } |
---|
| 807 | ; |
---|
| 808 | } |
---|
| 809 | |
---|
| 810 | if (flags & flag_reproc) { |
---|
| 811 | flags &= ~flag_reproc; |
---|
| 812 | --i; |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | if (flags & flag_next) { |
---|
| 816 | flags = (flags & ~flag_next) | flag_need_comma; |
---|
| 817 | |
---|
| 818 | if (!top->parent) { |
---|
| 819 | /* root value done */ |
---|
| 820 | |
---|
| 821 | flags |= flag_done; |
---|
| 822 | continue; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | if (top->parent->type == json_array) { |
---|
| 826 | flags |= flag_seek_value; |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | if (!state.first_pass) { |
---|
| 830 | json_value * parent = top->parent; |
---|
| 831 | |
---|
| 832 | switch (parent->type) { |
---|
| 833 | case json_object: |
---|
| 834 | |
---|
| 835 | parent->u.object.values |
---|
| 836 | [parent->u.object.length].value = top; |
---|
| 837 | |
---|
| 838 | break; |
---|
| 839 | |
---|
| 840 | case json_array: |
---|
| 841 | |
---|
| 842 | parent->u.array.values |
---|
| 843 | [parent->u.array.length] = top; |
---|
[696dc9e] | 844 | |
---|
[5ebff60] | 845 | break; |
---|
[696dc9e] | 846 | |
---|
[5ebff60] | 847 | default: |
---|
| 848 | break; |
---|
| 849 | } |
---|
| 850 | ; |
---|
| 851 | } |
---|
[696dc9e] | 852 | |
---|
[5ebff60] | 853 | if ((++top->parent->u.array.length) > state.uint_max) { |
---|
| 854 | goto e_overflow; |
---|
| 855 | } |
---|
[696dc9e] | 856 | |
---|
[5ebff60] | 857 | top = top->parent; |
---|
[696dc9e] | 858 | |
---|
[5ebff60] | 859 | continue; |
---|
| 860 | } |
---|
| 861 | } |
---|
| 862 | |
---|
| 863 | alloc = root; |
---|
| 864 | } |
---|
| 865 | |
---|
| 866 | return root; |
---|
| 867 | |
---|
| 868 | e_unknown_value: |
---|
| 869 | |
---|
| 870 | sprintf(error, "%d:%d: Unknown value", cur_line, e_off); |
---|
| 871 | goto e_failed; |
---|
| 872 | |
---|
| 873 | e_alloc_failure: |
---|
| 874 | |
---|
| 875 | strcpy(error, "Memory allocation failure"); |
---|
| 876 | goto e_failed; |
---|
| 877 | |
---|
| 878 | e_overflow: |
---|
| 879 | |
---|
| 880 | sprintf(error, "%d:%d: Too long (caught overflow)", cur_line, e_off); |
---|
| 881 | goto e_failed; |
---|
| 882 | |
---|
| 883 | e_failed: |
---|
| 884 | |
---|
| 885 | if (error_buf) { |
---|
| 886 | if (*error) { |
---|
| 887 | strcpy(error_buf, error); |
---|
| 888 | } else { |
---|
| 889 | strcpy(error_buf, "Unknown error"); |
---|
| 890 | } |
---|
| 891 | } |
---|
| 892 | |
---|
| 893 | if (state.first_pass) { |
---|
| 894 | alloc = root; |
---|
| 895 | } |
---|
| 896 | |
---|
| 897 | while (alloc) { |
---|
| 898 | top = alloc->_reserved.next_alloc; |
---|
| 899 | state.settings.mem_free(alloc, state.settings.user_data); |
---|
| 900 | alloc = top; |
---|
| 901 | } |
---|
| 902 | |
---|
| 903 | if (!state.first_pass) { |
---|
| 904 | json_value_free_ex(&state.settings, root); |
---|
| 905 | } |
---|
| 906 | |
---|
| 907 | return 0; |
---|
[696dc9e] | 908 | } |
---|
| 909 | |
---|
[5ebff60] | 910 | json_value * json_parse(const json_char * json, size_t length) |
---|
[696dc9e] | 911 | { |
---|
[5ebff60] | 912 | json_settings settings = { 0 }; |
---|
| 913 | |
---|
| 914 | return json_parse_ex(&settings, json, length, 0); |
---|
[696dc9e] | 915 | } |
---|
| 916 | |
---|
[5ebff60] | 917 | void json_value_free_ex(json_settings * settings, json_value * value) |
---|
[696dc9e] | 918 | { |
---|
[5ebff60] | 919 | json_value * cur_value; |
---|
[696dc9e] | 920 | |
---|
[5ebff60] | 921 | if (!value) { |
---|
| 922 | return; |
---|
| 923 | } |
---|
[696dc9e] | 924 | |
---|
[5ebff60] | 925 | value->parent = 0; |
---|
[696dc9e] | 926 | |
---|
[5ebff60] | 927 | while (value) { |
---|
| 928 | switch (value->type) { |
---|
| 929 | case json_array: |
---|
[696dc9e] | 930 | |
---|
[5ebff60] | 931 | if (!value->u.array.length) { |
---|
| 932 | settings->mem_free(value->u.array.values, settings->user_data); |
---|
| 933 | break; |
---|
| 934 | } |
---|
[696dc9e] | 935 | |
---|
[5ebff60] | 936 | value = value->u.array.values [--value->u.array.length]; |
---|
| 937 | continue; |
---|
[696dc9e] | 938 | |
---|
[5ebff60] | 939 | case json_object: |
---|
[696dc9e] | 940 | |
---|
[5ebff60] | 941 | if (!value->u.object.length) { |
---|
| 942 | settings->mem_free(value->u.object.values, settings->user_data); |
---|
| 943 | break; |
---|
| 944 | } |
---|
[696dc9e] | 945 | |
---|
[5ebff60] | 946 | value = value->u.object.values [--value->u.object.length].value; |
---|
| 947 | continue; |
---|
[696dc9e] | 948 | |
---|
[5ebff60] | 949 | case json_string: |
---|
[696dc9e] | 950 | |
---|
[5ebff60] | 951 | settings->mem_free(value->u.string.ptr, settings->user_data); |
---|
| 952 | break; |
---|
[696dc9e] | 953 | |
---|
[5ebff60] | 954 | default: |
---|
| 955 | break; |
---|
| 956 | } |
---|
| 957 | ; |
---|
[696dc9e] | 958 | |
---|
[5ebff60] | 959 | cur_value = value; |
---|
| 960 | value = value->parent; |
---|
| 961 | settings->mem_free(cur_value, settings->user_data); |
---|
| 962 | } |
---|
[696dc9e] | 963 | } |
---|
| 964 | |
---|
[5ebff60] | 965 | void json_value_free(json_value * value) |
---|
[7a80925] | 966 | { |
---|
[5ebff60] | 967 | json_settings settings = { 0 }; |
---|
| 968 | |
---|
| 969 | settings.mem_free = default_free; |
---|
| 970 | json_value_free_ex(&settings, value); |
---|
[7a80925] | 971 | } |
---|
[696dc9e] | 972 | |
---|