Fossil SCM
merged in JSON_parser license change (went to the wrong branch).
Commit
10666aadddbd5cce69b76acf705c42ac65078d5d
Parent
3e90ef61f302f25…
1 file changed
+126
-124
+126
-124
| --- src/cson_amalgamation.c | ||
| +++ src/cson_amalgamation.c | ||
| @@ -26,15 +26,11 @@ | ||
| 26 | 26 | #else |
| 27 | 27 | # define JSON_PARSER_DLL_API |
| 28 | 28 | #endif |
| 29 | 29 | |
| 30 | 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | -#ifdef _WIN32 | |
| 32 | -typedef __int64 JSON_int_t; | |
| 33 | -#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%I64d" | |
| 34 | -#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d" | |
| 35 | -#elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) | |
| 31 | +#if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1 | |
| 36 | 32 | typedef long long JSON_int_t; |
| 37 | 33 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 38 | 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 39 | 35 | #else |
| 40 | 36 | typedef long JSON_int_t; |
| @@ -103,11 +99,11 @@ | ||
| 103 | 99 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 104 | 100 | as zero-terminated C strings. |
| 105 | 101 | |
| 106 | 102 | \return Non-zero if parsing should continue, else zero. |
| 107 | 103 | */ |
| 108 | -typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value); | |
| 104 | +typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value); | |
| 109 | 105 | |
| 110 | 106 | |
| 111 | 107 | /** |
| 112 | 108 | A typedef for allocator functions semantically compatible with malloc(). |
| 113 | 109 | */ |
| @@ -233,11 +229,11 @@ | ||
| 233 | 229 | |
| 234 | 230 | #endif /* JSON_PARSER_H */ |
| 235 | 231 | /* end file parser/JSON_parser.h */ |
| 236 | 232 | /* begin file parser/JSON_parser.c */ |
| 237 | 233 | /* |
| 238 | -Copyright (c) 2005 JSON.org | |
| 234 | +Copyright (c) 2007-2013 Jean Gressmann ([email protected]) | |
| 239 | 235 | |
| 240 | 236 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 241 | 237 | of this software and associated documentation files (the "Software"), to deal |
| 242 | 238 | in the Software without restriction, including without limitation the rights |
| 243 | 239 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| @@ -245,12 +241,10 @@ | ||
| 245 | 241 | furnished to do so, subject to the following conditions: |
| 246 | 242 | |
| 247 | 243 | The above copyright notice and this permission notice shall be included in all |
| 248 | 244 | copies or substantial portions of the Software. |
| 249 | 245 | |
| 250 | -The Software shall be used for Good, not Evil. | |
| 251 | - | |
| 252 | 246 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 253 | 247 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 254 | 248 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 255 | 249 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 256 | 250 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| @@ -257,49 +251,52 @@ | ||
| 257 | 251 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 258 | 252 | SOFTWARE. |
| 259 | 253 | */ |
| 260 | 254 | |
| 261 | 255 | /* |
| 262 | - Callbacks, comments, Unicode handling by Jean Gressmann ([email protected]), 2007-2010. | |
| 263 | - | |
| 264 | - | |
| 265 | 256 | Changelog: |
| 257 | + 2013-09-08 | |
| 258 | + Updated license to to be compatible with Debian license requirements. | |
| 259 | + | |
| 260 | + 2012-06-06 | |
| 261 | + Fix for invalid UTF16 characters and some comment fixex ([email protected]). | |
| 262 | + | |
| 266 | 263 | 2010-11-25 |
| 267 | 264 | Support for custom memory allocation ([email protected]). |
| 268 | - | |
| 265 | + | |
| 269 | 266 | 2010-05-07 |
| 270 | - Added error handling for memory allocation failure ([email protected]). | |
| 267 | + Added error handling for memory allocation failure ([email protected]). | |
| 271 | 268 | Added diagnosis errors for invalid JSON. |
| 272 | - | |
| 269 | + | |
| 273 | 270 | 2010-03-25 |
| 274 | 271 | Fixed buffer overrun in grow_parse_buffer & cleaned up code. |
| 275 | - | |
| 272 | + | |
| 276 | 273 | 2009-10-19 |
| 277 | - Replaced long double in JSON_value_struct with double after reports | |
| 274 | + Replaced long double in JSON_value_struct with double after reports | |
| 278 | 275 | of strtold being broken on some platforms ([email protected]). |
| 279 | - | |
| 280 | - 2009-05-17 | |
| 276 | + | |
| 277 | + 2009-05-17 | |
| 281 | 278 | Incorporated [email protected] fix for UTF16 decoding. |
| 282 | - | |
| 283 | - 2009-05-14 | |
| 279 | + | |
| 280 | + 2009-05-14 | |
| 284 | 281 | Fixed float parsing bug related to a locale being set that didn't |
| 285 | 282 | use '.' as decimal point character ([email protected]). |
| 286 | - | |
| 287 | - 2008-10-14 | |
| 283 | + | |
| 284 | + 2008-10-14 | |
| 288 | 285 | Renamed states.IN to states.IT to avoid name clash which IN macro |
| 289 | 286 | defined in windef.h ([email protected]) |
| 290 | - | |
| 291 | - 2008-07-19 | |
| 287 | + | |
| 288 | + 2008-07-19 | |
| 292 | 289 | Removed some duplicate code & debugging variable ([email protected]) |
| 293 | - | |
| 294 | - 2008-05-28 | |
| 295 | - Made JSON_value structure ansi C compliant. This bug was report by | |
| 290 | + | |
| 291 | + 2008-05-28 | |
| 292 | + Made JSON_value structure ansi C compliant. This bug was report by | |
| 296 | 293 | [email protected] |
| 297 | - | |
| 298 | - 2008-05-20 | |
| 299 | - Fixed bug reported by [email protected] where the switching | |
| 300 | - from static to dynamic parse buffer did not copy the static parse | |
| 294 | + | |
| 295 | + 2008-05-20 | |
| 296 | + Fixed bug reported by [email protected] where the switching | |
| 297 | + from static to dynamic parse buffer did not copy the static parse | |
| 301 | 298 | buffer's content. |
| 302 | 299 | */ |
| 303 | 300 | |
| 304 | 301 | |
| 305 | 302 | |
| @@ -362,11 +359,11 @@ | ||
| 362 | 359 | char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE]; |
| 363 | 360 | JSON_malloc_t malloc; |
| 364 | 361 | JSON_free_t free; |
| 365 | 362 | }; |
| 366 | 363 | |
| 367 | -#define COUNTOF(x) (sizeof(x)/sizeof(x[0])) | |
| 364 | +#define COUNTOF(x) (sizeof(x)/sizeof(x[0])) | |
| 368 | 365 | |
| 369 | 366 | /* |
| 370 | 367 | Characters are mapped into these character classes. This allows for |
| 371 | 368 | a significant reduction in the size of the state transition table. |
| 372 | 369 | */ |
| @@ -403,11 +400,11 @@ | ||
| 403 | 400 | C_LOW_T, /* t */ |
| 404 | 401 | C_LOW_U, /* u */ |
| 405 | 402 | C_ABCDF, /* ABCDF */ |
| 406 | 403 | C_E, /* E */ |
| 407 | 404 | C_ETC, /* everything else */ |
| 408 | - C_STAR, /* * */ | |
| 405 | + C_STAR, /* * */ | |
| 409 | 406 | NR_CLASSES |
| 410 | 407 | }; |
| 411 | 408 | |
| 412 | 409 | static const signed char ascii_class[128] = { |
| 413 | 410 | /* |
| @@ -419,11 +416,11 @@ | ||
| 419 | 416 | __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __, |
| 420 | 417 | __, __, __, __, __, __, __, __, |
| 421 | 418 | __, __, __, __, __, __, __, __, |
| 422 | 419 | |
| 423 | 420 | C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 424 | - C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH, | |
| 421 | + C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH, | |
| 425 | 422 | C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, |
| 426 | 423 | C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 427 | 424 | |
| 428 | 425 | C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC, |
| 429 | 426 | C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| @@ -536,11 +533,11 @@ | ||
| 536 | 533 | /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__}, |
| 537 | 534 | /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__}, |
| 538 | 535 | /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__}, |
| 539 | 536 | /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__}, |
| 540 | 537 | /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2}, |
| 541 | -/*/* C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, | |
| 538 | +/*/star C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, | |
| 542 | 539 | /** C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, |
| 543 | 540 | /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__}, |
| 544 | 541 | /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__}, |
| 545 | 542 | /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__}, |
| 546 | 543 | }; |
| @@ -548,27 +545,27 @@ | ||
| 548 | 545 | |
| 549 | 546 | /* |
| 550 | 547 | These modes can be pushed on the stack. |
| 551 | 548 | */ |
| 552 | 549 | enum modes { |
| 553 | - MODE_ARRAY = 1, | |
| 554 | - MODE_DONE = 2, | |
| 555 | - MODE_KEY = 3, | |
| 550 | + MODE_ARRAY = 1, | |
| 551 | + MODE_DONE = 2, | |
| 552 | + MODE_KEY = 3, | |
| 556 | 553 | MODE_OBJECT = 4 |
| 557 | 554 | }; |
| 558 | 555 | |
| 559 | 556 | static void set_error(JSON_parser jc) |
| 560 | 557 | { |
| 561 | 558 | switch (jc->state) { |
| 562 | 559 | case GO: |
| 563 | 560 | switch (jc->current_char) { |
| 564 | - case '{': case '}': case '[': case ']': | |
| 561 | + case '{': case '}': case '[': case ']': | |
| 565 | 562 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 566 | 563 | break; |
| 567 | 564 | default: |
| 568 | 565 | jc->error = JSON_E_INVALID_CHAR; |
| 569 | - break; | |
| 566 | + break; | |
| 570 | 567 | } |
| 571 | 568 | break; |
| 572 | 569 | case OB: |
| 573 | 570 | jc->error = JSON_E_EXPECTED_KEY; |
| 574 | 571 | break; |
| @@ -604,11 +601,11 @@ | ||
| 604 | 601 | { |
| 605 | 602 | /* |
| 606 | 603 | Push a mode onto the stack. Return false if there is overflow. |
| 607 | 604 | */ |
| 608 | 605 | assert(jc->top <= jc->stack_capacity); |
| 609 | - | |
| 606 | + | |
| 610 | 607 | if (jc->depth < 0) { |
| 611 | 608 | if (jc->top == jc->stack_capacity) { |
| 612 | 609 | const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]); |
| 613 | 610 | const size_t new_capacity = jc->stack_capacity * 2; |
| 614 | 611 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]); |
| @@ -653,17 +650,17 @@ | ||
| 653 | 650 | #define parse_buffer_clear(jc) \ |
| 654 | 651 | do {\ |
| 655 | 652 | jc->parse_buffer_count = 0;\ |
| 656 | 653 | jc->parse_buffer[0] = 0;\ |
| 657 | 654 | } while (0) |
| 658 | - | |
| 655 | + | |
| 659 | 656 | #define parse_buffer_pop_back_char(jc)\ |
| 660 | 657 | do {\ |
| 661 | 658 | assert(jc->parse_buffer_count >= 1);\ |
| 662 | 659 | --jc->parse_buffer_count;\ |
| 663 | 660 | jc->parse_buffer[jc->parse_buffer_count] = 0;\ |
| 664 | - } while (0) | |
| 661 | + } while (0) | |
| 665 | 662 | |
| 666 | 663 | |
| 667 | 664 | |
| 668 | 665 | void delete_JSON_parser(JSON_parser jc) |
| 669 | 666 | { |
| @@ -673,25 +670,25 @@ | ||
| 673 | 670 | } |
| 674 | 671 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 675 | 672 | jc->free((void*)jc->parse_buffer); |
| 676 | 673 | } |
| 677 | 674 | jc->free((void*)jc); |
| 678 | - } | |
| 675 | + } | |
| 679 | 676 | } |
| 680 | 677 | |
| 681 | 678 | int JSON_parser_reset(JSON_parser jc) |
| 682 | 679 | { |
| 683 | 680 | if (NULL == jc) { |
| 684 | 681 | return false; |
| 685 | 682 | } |
| 686 | - | |
| 683 | + | |
| 687 | 684 | jc->state = GO; |
| 688 | 685 | jc->top = -1; |
| 689 | 686 | |
| 690 | 687 | /* parser has been used previously? */ |
| 691 | 688 | if (NULL == jc->parse_buffer) { |
| 692 | - | |
| 689 | + | |
| 693 | 690 | /* Do we want non-bound stack? */ |
| 694 | 691 | if (jc->depth > 0) { |
| 695 | 692 | jc->stack_capacity = jc->depth; |
| 696 | 693 | if (jc->depth <= (int)COUNTOF(jc->static_stack)) { |
| 697 | 694 | jc->stack = &jc->static_stack[0]; |
| @@ -705,20 +702,20 @@ | ||
| 705 | 702 | } else { |
| 706 | 703 | jc->stack_capacity = (int)COUNTOF(jc->static_stack); |
| 707 | 704 | jc->depth = -1; |
| 708 | 705 | jc->stack = &jc->static_stack[0]; |
| 709 | 706 | } |
| 710 | - | |
| 707 | + | |
| 711 | 708 | /* set up the parse buffer */ |
| 712 | 709 | jc->parse_buffer = &jc->static_parse_buffer[0]; |
| 713 | 710 | jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer); |
| 714 | 711 | } |
| 715 | - | |
| 712 | + | |
| 716 | 713 | /* set parser to start */ |
| 717 | 714 | push(jc, MODE_DONE); |
| 718 | 715 | parse_buffer_clear(jc); |
| 719 | - | |
| 716 | + | |
| 720 | 717 | return true; |
| 721 | 718 | } |
| 722 | 719 | |
| 723 | 720 | JSON_parser |
| 724 | 721 | new_JSON_parser(JSON_config const * config) |
| @@ -735,29 +732,29 @@ | ||
| 735 | 732 | |
| 736 | 733 | int use_std_malloc = false; |
| 737 | 734 | JSON_config default_config; |
| 738 | 735 | JSON_parser jc; |
| 739 | 736 | JSON_malloc_t alloc; |
| 740 | - | |
| 737 | + | |
| 741 | 738 | /* set to default configuration if none was provided */ |
| 742 | 739 | if (NULL == config) { |
| 743 | 740 | /* initialize configuration */ |
| 744 | 741 | init_JSON_config(&default_config); |
| 745 | 742 | config = &default_config; |
| 746 | 743 | } |
| 747 | - | |
| 744 | + | |
| 748 | 745 | /* use std malloc if either the allocator or deallocator function isn't set */ |
| 749 | 746 | use_std_malloc = NULL == config->malloc || NULL == config->free; |
| 750 | - | |
| 747 | + | |
| 751 | 748 | alloc = use_std_malloc ? malloc : config->malloc; |
| 752 | - | |
| 753 | - jc = JSON_parser_malloc(alloc, sizeof(*jc), "parser"); | |
| 754 | - | |
| 749 | + | |
| 750 | + jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser"); | |
| 751 | + | |
| 755 | 752 | if (NULL == jc) { |
| 756 | 753 | return NULL; |
| 757 | 754 | } |
| 758 | - | |
| 755 | + | |
| 759 | 756 | /* configure the parser */ |
| 760 | 757 | memset(jc, 0, sizeof(*jc)); |
| 761 | 758 | jc->malloc = alloc; |
| 762 | 759 | jc->free = use_std_malloc ? free : config->free; |
| 763 | 760 | jc->callback = config->callback; |
| @@ -765,42 +762,42 @@ | ||
| 765 | 762 | jc->allow_comments = (signed char)(config->allow_comments != 0); |
| 766 | 763 | jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0); |
| 767 | 764 | jc->decimal_point = *localeconv()->decimal_point; |
| 768 | 765 | /* We need to be able to push at least one object */ |
| 769 | 766 | jc->depth = config->depth == 0 ? 1 : config->depth; |
| 770 | - | |
| 767 | + | |
| 771 | 768 | /* reset the parser */ |
| 772 | 769 | if (!JSON_parser_reset(jc)) { |
| 773 | 770 | jc->free(jc); |
| 774 | 771 | return NULL; |
| 775 | 772 | } |
| 776 | - | |
| 773 | + | |
| 777 | 774 | return jc; |
| 778 | 775 | } |
| 779 | 776 | |
| 780 | 777 | static int parse_buffer_grow(JSON_parser jc) |
| 781 | 778 | { |
| 782 | 779 | const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]); |
| 783 | 780 | const size_t new_capacity = jc->parse_buffer_capacity * 2; |
| 784 | 781 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]); |
| 785 | 782 | void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer"); |
| 786 | - | |
| 783 | + | |
| 787 | 784 | if (mem == NULL) { |
| 788 | 785 | jc->error = JSON_E_OUT_OF_MEMORY; |
| 789 | 786 | return false; |
| 790 | 787 | } |
| 791 | - | |
| 788 | + | |
| 792 | 789 | assert(new_capacity > 0); |
| 793 | 790 | memcpy(mem, jc->parse_buffer, bytes_to_copy); |
| 794 | - | |
| 791 | + | |
| 795 | 792 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 796 | 793 | jc->free(jc->parse_buffer); |
| 797 | 794 | } |
| 798 | - | |
| 795 | + | |
| 799 | 796 | jc->parse_buffer = (char*)mem; |
| 800 | 797 | jc->parse_buffer_capacity = new_capacity; |
| 801 | - | |
| 798 | + | |
| 802 | 799 | return true; |
| 803 | 800 | } |
| 804 | 801 | |
| 805 | 802 | static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars) |
| 806 | 803 | { |
| @@ -808,11 +805,11 @@ | ||
| 808 | 805 | if (!parse_buffer_grow(jc)) { |
| 809 | 806 | assert(jc->error == JSON_E_OUT_OF_MEMORY); |
| 810 | 807 | return false; |
| 811 | 808 | } |
| 812 | 809 | } |
| 813 | - | |
| 810 | + | |
| 814 | 811 | return true; |
| 815 | 812 | } |
| 816 | 813 | |
| 817 | 814 | #define parse_buffer_has_space_for(jc, count) \ |
| 818 | 815 | (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity) |
| @@ -830,27 +827,27 @@ | ||
| 830 | 827 | jc->type == JSON_T_FALSE || \ |
| 831 | 828 | jc->type == JSON_T_TRUE || \ |
| 832 | 829 | jc->type == JSON_T_FLOAT || \ |
| 833 | 830 | jc->type == JSON_T_INTEGER || \ |
| 834 | 831 | jc->type == JSON_T_STRING) |
| 835 | - | |
| 832 | + | |
| 836 | 833 | |
| 837 | 834 | static int parse_parse_buffer(JSON_parser jc) |
| 838 | 835 | { |
| 839 | 836 | if (jc->callback) { |
| 840 | 837 | JSON_value value, *arg = NULL; |
| 841 | - | |
| 838 | + | |
| 842 | 839 | if (jc->type != JSON_T_NONE) { |
| 843 | 840 | assert_is_non_container_type(jc); |
| 844 | - | |
| 841 | + | |
| 845 | 842 | switch(jc->type) { |
| 846 | 843 | case JSON_T_FLOAT: |
| 847 | 844 | arg = &value; |
| 848 | 845 | if (jc->handle_floats_manually) { |
| 849 | 846 | value.vu.str.value = jc->parse_buffer; |
| 850 | 847 | value.vu.str.length = jc->parse_buffer_count; |
| 851 | - } else { | |
| 848 | + } else { | |
| 852 | 849 | /* not checking with end pointer b/c there may be trailing ws */ |
| 853 | 850 | value.vu.float_value = strtod(jc->parse_buffer, NULL); |
| 854 | 851 | } |
| 855 | 852 | break; |
| 856 | 853 | case JSON_T_INTEGER: |
| @@ -861,19 +858,19 @@ | ||
| 861 | 858 | arg = &value; |
| 862 | 859 | value.vu.str.value = jc->parse_buffer; |
| 863 | 860 | value.vu.str.length = jc->parse_buffer_count; |
| 864 | 861 | break; |
| 865 | 862 | } |
| 866 | - | |
| 863 | + | |
| 867 | 864 | if (!(*jc->callback)(jc->ctx, jc->type, arg)) { |
| 868 | 865 | return false; |
| 869 | 866 | } |
| 870 | 867 | } |
| 871 | 868 | } |
| 872 | - | |
| 869 | + | |
| 873 | 870 | parse_buffer_clear(jc); |
| 874 | - | |
| 871 | + | |
| 875 | 872 | return true; |
| 876 | 873 | } |
| 877 | 874 | |
| 878 | 875 | #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800) |
| 879 | 876 | #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00) |
| @@ -884,35 +881,39 @@ | ||
| 884 | 881 | { |
| 885 | 882 | int i; |
| 886 | 883 | unsigned uc = 0; |
| 887 | 884 | char* p; |
| 888 | 885 | int trail_bytes; |
| 889 | - | |
| 886 | + | |
| 890 | 887 | assert(jc->parse_buffer_count >= 6); |
| 891 | - | |
| 888 | + | |
| 892 | 889 | p = &jc->parse_buffer[jc->parse_buffer_count - 4]; |
| 893 | - | |
| 890 | + | |
| 894 | 891 | for (i = 12; i >= 0; i -= 4, ++p) { |
| 895 | 892 | unsigned x = *p; |
| 896 | - | |
| 893 | + | |
| 897 | 894 | if (x >= 'a') { |
| 898 | 895 | x -= ('a' - 10); |
| 899 | 896 | } else if (x >= 'A') { |
| 900 | 897 | x -= ('A' - 10); |
| 901 | 898 | } else { |
| 902 | 899 | x &= ~0x30u; |
| 903 | 900 | } |
| 904 | - | |
| 901 | + | |
| 905 | 902 | assert(x < 16); |
| 906 | - | |
| 903 | + | |
| 907 | 904 | uc |= x << i; |
| 908 | 905 | } |
| 909 | - | |
| 906 | + | |
| 910 | 907 | /* clear UTF-16 char from buffer */ |
| 911 | 908 | jc->parse_buffer_count -= 6; |
| 912 | 909 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 913 | - | |
| 910 | + | |
| 911 | + if (uc == 0xffff || uc == 0xfffe) { | |
| 912 | + return false; | |
| 913 | + } | |
| 914 | + | |
| 914 | 915 | /* attempt decoding ... */ |
| 915 | 916 | if (jc->utf16_high_surrogate) { |
| 916 | 917 | if (IS_LOW_SURROGATE(uc)) { |
| 917 | 918 | uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc); |
| 918 | 919 | trail_bytes = 3; |
| @@ -935,26 +936,26 @@ | ||
| 935 | 936 | return false; |
| 936 | 937 | } else { |
| 937 | 938 | trail_bytes = 2; |
| 938 | 939 | } |
| 939 | 940 | } |
| 940 | - | |
| 941 | + | |
| 941 | 942 | jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]); |
| 942 | - | |
| 943 | + | |
| 943 | 944 | for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) { |
| 944 | 945 | jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80); |
| 945 | 946 | } |
| 946 | 947 | |
| 947 | 948 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 948 | - | |
| 949 | + | |
| 949 | 950 | return true; |
| 950 | 951 | } |
| 951 | 952 | |
| 952 | 953 | static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char) |
| 953 | 954 | { |
| 954 | 955 | assert(parse_buffer_has_space_for(jc, 1)); |
| 955 | - | |
| 956 | + | |
| 956 | 957 | jc->escaped = 0; |
| 957 | 958 | /* remove the backslash */ |
| 958 | 959 | parse_buffer_pop_back_char(jc); |
| 959 | 960 | switch(next_char) { |
| 960 | 961 | case 'b': |
| @@ -996,22 +997,22 @@ | ||
| 996 | 997 | { |
| 997 | 998 | if (!parse_buffer_reserve_for(jc, 1)) { |
| 998 | 999 | assert(JSON_E_OUT_OF_MEMORY == jc->error); |
| 999 | 1000 | return false; |
| 1000 | 1001 | } |
| 1001 | - | |
| 1002 | + | |
| 1002 | 1003 | if (jc->escaped) { |
| 1003 | 1004 | if (!add_escaped_char_to_parse_buffer(jc, next_char)) { |
| 1004 | 1005 | jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE; |
| 1005 | - return false; | |
| 1006 | + return false; | |
| 1006 | 1007 | } |
| 1007 | 1008 | } else if (!jc->comment) { |
| 1008 | 1009 | if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) { |
| 1009 | 1010 | parse_buffer_push_back_char(jc, (char)next_char); |
| 1010 | 1011 | } |
| 1011 | 1012 | } |
| 1012 | - | |
| 1013 | + | |
| 1013 | 1014 | return true; |
| 1014 | 1015 | } |
| 1015 | 1016 | |
| 1016 | 1017 | #define assert_type_isnt_string_null_or_bool(jc) \ |
| 1017 | 1018 | assert(jc->type != JSON_T_FALSE); \ |
| @@ -1031,13 +1032,13 @@ | ||
| 1031 | 1032 | */ |
| 1032 | 1033 | int next_class, next_state; |
| 1033 | 1034 | |
| 1034 | 1035 | /* |
| 1035 | 1036 | Store the current char for error handling |
| 1036 | -*/ | |
| 1037 | +*/ | |
| 1037 | 1038 | jc->current_char = next_char; |
| 1038 | - | |
| 1039 | + | |
| 1039 | 1040 | /* |
| 1040 | 1041 | Determine the character's class. |
| 1041 | 1042 | */ |
| 1042 | 1043 | if (next_char < 0) { |
| 1043 | 1044 | jc->error = JSON_E_INVALID_CHAR; |
| @@ -1050,15 +1051,15 @@ | ||
| 1050 | 1051 | if (next_class <= __) { |
| 1051 | 1052 | set_error(jc); |
| 1052 | 1053 | return false; |
| 1053 | 1054 | } |
| 1054 | 1055 | } |
| 1055 | - | |
| 1056 | + | |
| 1056 | 1057 | if (!add_char_to_parse_buffer(jc, next_char, next_class)) { |
| 1057 | 1058 | return false; |
| 1058 | 1059 | } |
| 1059 | - | |
| 1060 | + | |
| 1060 | 1061 | /* |
| 1061 | 1062 | Get the next state from the state transition table. |
| 1062 | 1063 | */ |
| 1063 | 1064 | next_state = state_transition_table[jc->state][next_class]; |
| 1064 | 1065 | if (next_state >= 0) { |
| @@ -1069,11 +1070,11 @@ | ||
| 1069 | 1070 | } else { |
| 1070 | 1071 | /* |
| 1071 | 1072 | Or perform one of the actions. |
| 1072 | 1073 | */ |
| 1073 | 1074 | switch (next_state) { |
| 1074 | -/* Unicode character */ | |
| 1075 | +/* Unicode character */ | |
| 1075 | 1076 | case UC: |
| 1076 | 1077 | if(!decode_unicode_char(jc)) { |
| 1077 | 1078 | jc->error = JSON_E_INVALID_UNICODE_SEQUENCE; |
| 1078 | 1079 | return false; |
| 1079 | 1080 | } |
| @@ -1091,78 +1092,78 @@ | ||
| 1091 | 1092 | break; |
| 1092 | 1093 | /* integer detected by minus */ |
| 1093 | 1094 | case MX: |
| 1094 | 1095 | jc->type = JSON_T_INTEGER; |
| 1095 | 1096 | jc->state = MI; |
| 1096 | - break; | |
| 1097 | -/* integer detected by zero */ | |
| 1097 | + break; | |
| 1098 | +/* integer detected by zero */ | |
| 1098 | 1099 | case ZX: |
| 1099 | 1100 | jc->type = JSON_T_INTEGER; |
| 1100 | 1101 | jc->state = ZE; |
| 1101 | - break; | |
| 1102 | -/* integer detected by 1-9 */ | |
| 1102 | + break; | |
| 1103 | +/* integer detected by 1-9 */ | |
| 1103 | 1104 | case IX: |
| 1104 | 1105 | jc->type = JSON_T_INTEGER; |
| 1105 | 1106 | jc->state = IT; |
| 1106 | - break; | |
| 1107 | - | |
| 1107 | + break; | |
| 1108 | + | |
| 1108 | 1109 | /* floating point number detected by exponent*/ |
| 1109 | 1110 | case DE: |
| 1110 | 1111 | assert_type_isnt_string_null_or_bool(jc); |
| 1111 | 1112 | jc->type = JSON_T_FLOAT; |
| 1112 | 1113 | jc->state = E1; |
| 1113 | - break; | |
| 1114 | - | |
| 1114 | + break; | |
| 1115 | + | |
| 1115 | 1116 | /* floating point number detected by fraction */ |
| 1116 | 1117 | case DF: |
| 1117 | 1118 | assert_type_isnt_string_null_or_bool(jc); |
| 1118 | 1119 | if (!jc->handle_floats_manually) { |
| 1119 | 1120 | /* |
| 1120 | - Some versions of strtod (which underlies sscanf) don't support converting | |
| 1121 | + Some versions of strtod (which underlies sscanf) don't support converting | |
| 1121 | 1122 | C-locale formated floating point values. |
| 1122 | -*/ | |
| 1123 | +*/ | |
| 1123 | 1124 | assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.'); |
| 1124 | 1125 | jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point; |
| 1125 | - } | |
| 1126 | + } | |
| 1126 | 1127 | jc->type = JSON_T_FLOAT; |
| 1127 | 1128 | jc->state = FX; |
| 1128 | - break; | |
| 1129 | + break; | |
| 1129 | 1130 | /* string begin " */ |
| 1130 | 1131 | case SB: |
| 1131 | 1132 | parse_buffer_clear(jc); |
| 1132 | 1133 | assert(jc->type == JSON_T_NONE); |
| 1133 | 1134 | jc->type = JSON_T_STRING; |
| 1134 | 1135 | jc->state = ST; |
| 1135 | - break; | |
| 1136 | - | |
| 1136 | + break; | |
| 1137 | + | |
| 1137 | 1138 | /* n */ |
| 1138 | 1139 | case NU: |
| 1139 | 1140 | assert(jc->type == JSON_T_NONE); |
| 1140 | 1141 | jc->type = JSON_T_NULL; |
| 1141 | 1142 | jc->state = N1; |
| 1142 | - break; | |
| 1143 | + break; | |
| 1143 | 1144 | /* f */ |
| 1144 | 1145 | case FA: |
| 1145 | 1146 | assert(jc->type == JSON_T_NONE); |
| 1146 | 1147 | jc->type = JSON_T_FALSE; |
| 1147 | 1148 | jc->state = F1; |
| 1148 | - break; | |
| 1149 | + break; | |
| 1149 | 1150 | /* t */ |
| 1150 | 1151 | case TR: |
| 1151 | 1152 | assert(jc->type == JSON_T_NONE); |
| 1152 | 1153 | jc->type = JSON_T_TRUE; |
| 1153 | 1154 | jc->state = T1; |
| 1154 | - break; | |
| 1155 | - | |
| 1155 | + break; | |
| 1156 | + | |
| 1156 | 1157 | /* closing comment */ |
| 1157 | 1158 | case CE: |
| 1158 | 1159 | jc->comment = 0; |
| 1159 | 1160 | assert(jc->parse_buffer_count == 0); |
| 1160 | 1161 | assert(jc->type == JSON_T_NONE); |
| 1161 | 1162 | jc->state = jc->before_comment_state; |
| 1162 | - break; | |
| 1163 | - | |
| 1163 | + break; | |
| 1164 | + | |
| 1164 | 1165 | /* opening comment */ |
| 1165 | 1166 | case CB: |
| 1166 | 1167 | if (!jc->allow_comments) { |
| 1167 | 1168 | return false; |
| 1168 | 1169 | } |
| @@ -1172,11 +1173,11 @@ | ||
| 1172 | 1173 | } |
| 1173 | 1174 | assert(jc->parse_buffer_count == 0); |
| 1174 | 1175 | assert(jc->type != JSON_T_STRING); |
| 1175 | 1176 | switch (jc->stack[jc->top]) { |
| 1176 | 1177 | case MODE_ARRAY: |
| 1177 | - case MODE_OBJECT: | |
| 1178 | + case MODE_OBJECT: | |
| 1178 | 1179 | switch(jc->state) { |
| 1179 | 1180 | case VA: |
| 1180 | 1181 | case AR: |
| 1181 | 1182 | jc->before_comment_state = jc->state; |
| 1182 | 1183 | break; |
| @@ -1192,11 +1193,11 @@ | ||
| 1192 | 1193 | jc->type = JSON_T_NONE; |
| 1193 | 1194 | jc->state = C1; |
| 1194 | 1195 | jc->comment = 1; |
| 1195 | 1196 | break; |
| 1196 | 1197 | /* empty } */ |
| 1197 | - case -9: | |
| 1198 | + case -9: | |
| 1198 | 1199 | parse_buffer_clear(jc); |
| 1199 | 1200 | if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) { |
| 1200 | 1201 | return false; |
| 1201 | 1202 | } |
| 1202 | 1203 | if (!pop(jc, MODE_KEY)) { |
| @@ -1231,11 +1232,11 @@ | ||
| 1231 | 1232 | } |
| 1232 | 1233 | if (!pop(jc, MODE_ARRAY)) { |
| 1233 | 1234 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 1234 | 1235 | return false; |
| 1235 | 1236 | } |
| 1236 | - | |
| 1237 | + | |
| 1237 | 1238 | jc->type = JSON_T_NONE; |
| 1238 | 1239 | jc->state = OK; |
| 1239 | 1240 | break; |
| 1240 | 1241 | |
| 1241 | 1242 | /* { */ case -6: |
| @@ -1267,11 +1268,11 @@ | ||
| 1267 | 1268 | switch (jc->stack[jc->top]) { |
| 1268 | 1269 | case MODE_KEY: |
| 1269 | 1270 | assert(jc->type == JSON_T_STRING); |
| 1270 | 1271 | jc->type = JSON_T_NONE; |
| 1271 | 1272 | jc->state = CO; |
| 1272 | - | |
| 1273 | + | |
| 1273 | 1274 | if (jc->callback) { |
| 1274 | 1275 | JSON_value value; |
| 1275 | 1276 | value.vu.str.value = jc->parse_buffer; |
| 1276 | 1277 | value.vu.str.length = jc->parse_buffer_count; |
| 1277 | 1278 | if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) { |
| @@ -1357,29 +1358,29 @@ | ||
| 1357 | 1358 | |
| 1358 | 1359 | |
| 1359 | 1360 | int JSON_parser_is_legal_white_space_string(const char* s) |
| 1360 | 1361 | { |
| 1361 | 1362 | int c, char_class; |
| 1362 | - | |
| 1363 | + | |
| 1363 | 1364 | if (s == NULL) { |
| 1364 | 1365 | return false; |
| 1365 | 1366 | } |
| 1366 | - | |
| 1367 | - for (; *s; ++s) { | |
| 1367 | + | |
| 1368 | + for (; *s; ++s) { | |
| 1368 | 1369 | c = *s; |
| 1369 | - | |
| 1370 | + | |
| 1370 | 1371 | if (c < 0 || c >= 128) { |
| 1371 | 1372 | return false; |
| 1372 | 1373 | } |
| 1373 | - | |
| 1374 | + | |
| 1374 | 1375 | char_class = ascii_class[c]; |
| 1375 | - | |
| 1376 | + | |
| 1376 | 1377 | if (char_class != C_SPACE && char_class != C_WHITE) { |
| 1377 | 1378 | return false; |
| 1378 | 1379 | } |
| 1379 | 1380 | } |
| 1380 | - | |
| 1381 | + | |
| 1381 | 1382 | return true; |
| 1382 | 1383 | } |
| 1383 | 1384 | |
| 1384 | 1385 | int JSON_parser_get_last_error(JSON_parser jc) |
| 1385 | 1386 | { |
| @@ -1389,16 +1390,17 @@ | ||
| 1389 | 1390 | |
| 1390 | 1391 | void init_JSON_config(JSON_config* config) |
| 1391 | 1392 | { |
| 1392 | 1393 | if (config) { |
| 1393 | 1394 | memset(config, 0, sizeof(*config)); |
| 1394 | - | |
| 1395 | + | |
| 1395 | 1396 | config->depth = JSON_PARSER_STACK_SIZE - 1; |
| 1396 | 1397 | config->malloc = malloc; |
| 1397 | 1398 | config->free = free; |
| 1398 | 1399 | } |
| 1399 | 1400 | } |
| 1401 | + | |
| 1400 | 1402 | /* end file parser/JSON_parser.c */ |
| 1401 | 1403 | /* begin file ./cson.c */ |
| 1402 | 1404 | #include <assert.h> |
| 1403 | 1405 | #include <stdlib.h> /* malloc()/free() */ |
| 1404 | 1406 | #include <string.h> |
| 1405 | 1407 |
| --- src/cson_amalgamation.c | |
| +++ src/cson_amalgamation.c | |
| @@ -26,15 +26,11 @@ | |
| 26 | #else |
| 27 | # define JSON_PARSER_DLL_API |
| 28 | #endif |
| 29 | |
| 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | #ifdef _WIN32 |
| 32 | typedef __int64 JSON_int_t; |
| 33 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%I64d" |
| 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d" |
| 35 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 36 | typedef long long JSON_int_t; |
| 37 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 38 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 39 | #else |
| 40 | typedef long JSON_int_t; |
| @@ -103,11 +99,11 @@ | |
| 103 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 104 | as zero-terminated C strings. |
| 105 | |
| 106 | \return Non-zero if parsing should continue, else zero. |
| 107 | */ |
| 108 | typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value); |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | A typedef for allocator functions semantically compatible with malloc(). |
| 113 | */ |
| @@ -233,11 +229,11 @@ | |
| 233 | |
| 234 | #endif /* JSON_PARSER_H */ |
| 235 | /* end file parser/JSON_parser.h */ |
| 236 | /* begin file parser/JSON_parser.c */ |
| 237 | /* |
| 238 | Copyright (c) 2005 JSON.org |
| 239 | |
| 240 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 241 | of this software and associated documentation files (the "Software"), to deal |
| 242 | in the Software without restriction, including without limitation the rights |
| 243 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| @@ -245,12 +241,10 @@ | |
| 245 | furnished to do so, subject to the following conditions: |
| 246 | |
| 247 | The above copyright notice and this permission notice shall be included in all |
| 248 | copies or substantial portions of the Software. |
| 249 | |
| 250 | The Software shall be used for Good, not Evil. |
| 251 | |
| 252 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 253 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 254 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 255 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 256 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| @@ -257,49 +251,52 @@ | |
| 257 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 258 | SOFTWARE. |
| 259 | */ |
| 260 | |
| 261 | /* |
| 262 | Callbacks, comments, Unicode handling by Jean Gressmann ([email protected]), 2007-2010. |
| 263 | |
| 264 | |
| 265 | Changelog: |
| 266 | 2010-11-25 |
| 267 | Support for custom memory allocation ([email protected]). |
| 268 | |
| 269 | 2010-05-07 |
| 270 | Added error handling for memory allocation failure ([email protected]). |
| 271 | Added diagnosis errors for invalid JSON. |
| 272 | |
| 273 | 2010-03-25 |
| 274 | Fixed buffer overrun in grow_parse_buffer & cleaned up code. |
| 275 | |
| 276 | 2009-10-19 |
| 277 | Replaced long double in JSON_value_struct with double after reports |
| 278 | of strtold being broken on some platforms ([email protected]). |
| 279 | |
| 280 | 2009-05-17 |
| 281 | Incorporated [email protected] fix for UTF16 decoding. |
| 282 | |
| 283 | 2009-05-14 |
| 284 | Fixed float parsing bug related to a locale being set that didn't |
| 285 | use '.' as decimal point character ([email protected]). |
| 286 | |
| 287 | 2008-10-14 |
| 288 | Renamed states.IN to states.IT to avoid name clash which IN macro |
| 289 | defined in windef.h ([email protected]) |
| 290 | |
| 291 | 2008-07-19 |
| 292 | Removed some duplicate code & debugging variable ([email protected]) |
| 293 | |
| 294 | 2008-05-28 |
| 295 | Made JSON_value structure ansi C compliant. This bug was report by |
| 296 | [email protected] |
| 297 | |
| 298 | 2008-05-20 |
| 299 | Fixed bug reported by [email protected] where the switching |
| 300 | from static to dynamic parse buffer did not copy the static parse |
| 301 | buffer's content. |
| 302 | */ |
| 303 | |
| 304 | |
| 305 | |
| @@ -362,11 +359,11 @@ | |
| 362 | char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE]; |
| 363 | JSON_malloc_t malloc; |
| 364 | JSON_free_t free; |
| 365 | }; |
| 366 | |
| 367 | #define COUNTOF(x) (sizeof(x)/sizeof(x[0])) |
| 368 | |
| 369 | /* |
| 370 | Characters are mapped into these character classes. This allows for |
| 371 | a significant reduction in the size of the state transition table. |
| 372 | */ |
| @@ -403,11 +400,11 @@ | |
| 403 | C_LOW_T, /* t */ |
| 404 | C_LOW_U, /* u */ |
| 405 | C_ABCDF, /* ABCDF */ |
| 406 | C_E, /* E */ |
| 407 | C_ETC, /* everything else */ |
| 408 | C_STAR, /* * */ |
| 409 | NR_CLASSES |
| 410 | }; |
| 411 | |
| 412 | static const signed char ascii_class[128] = { |
| 413 | /* |
| @@ -419,11 +416,11 @@ | |
| 419 | __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __, |
| 420 | __, __, __, __, __, __, __, __, |
| 421 | __, __, __, __, __, __, __, __, |
| 422 | |
| 423 | C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 424 | C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH, |
| 425 | C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, |
| 426 | C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 427 | |
| 428 | C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC, |
| 429 | C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| @@ -536,11 +533,11 @@ | |
| 536 | /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__}, |
| 537 | /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__}, |
| 538 | /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__}, |
| 539 | /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__}, |
| 540 | /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2}, |
| 541 | /*/* C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, |
| 542 | /** C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, |
| 543 | /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__}, |
| 544 | /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__}, |
| 545 | /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__}, |
| 546 | }; |
| @@ -548,27 +545,27 @@ | |
| 548 | |
| 549 | /* |
| 550 | These modes can be pushed on the stack. |
| 551 | */ |
| 552 | enum modes { |
| 553 | MODE_ARRAY = 1, |
| 554 | MODE_DONE = 2, |
| 555 | MODE_KEY = 3, |
| 556 | MODE_OBJECT = 4 |
| 557 | }; |
| 558 | |
| 559 | static void set_error(JSON_parser jc) |
| 560 | { |
| 561 | switch (jc->state) { |
| 562 | case GO: |
| 563 | switch (jc->current_char) { |
| 564 | case '{': case '}': case '[': case ']': |
| 565 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 566 | break; |
| 567 | default: |
| 568 | jc->error = JSON_E_INVALID_CHAR; |
| 569 | break; |
| 570 | } |
| 571 | break; |
| 572 | case OB: |
| 573 | jc->error = JSON_E_EXPECTED_KEY; |
| 574 | break; |
| @@ -604,11 +601,11 @@ | |
| 604 | { |
| 605 | /* |
| 606 | Push a mode onto the stack. Return false if there is overflow. |
| 607 | */ |
| 608 | assert(jc->top <= jc->stack_capacity); |
| 609 | |
| 610 | if (jc->depth < 0) { |
| 611 | if (jc->top == jc->stack_capacity) { |
| 612 | const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]); |
| 613 | const size_t new_capacity = jc->stack_capacity * 2; |
| 614 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]); |
| @@ -653,17 +650,17 @@ | |
| 653 | #define parse_buffer_clear(jc) \ |
| 654 | do {\ |
| 655 | jc->parse_buffer_count = 0;\ |
| 656 | jc->parse_buffer[0] = 0;\ |
| 657 | } while (0) |
| 658 | |
| 659 | #define parse_buffer_pop_back_char(jc)\ |
| 660 | do {\ |
| 661 | assert(jc->parse_buffer_count >= 1);\ |
| 662 | --jc->parse_buffer_count;\ |
| 663 | jc->parse_buffer[jc->parse_buffer_count] = 0;\ |
| 664 | } while (0) |
| 665 | |
| 666 | |
| 667 | |
| 668 | void delete_JSON_parser(JSON_parser jc) |
| 669 | { |
| @@ -673,25 +670,25 @@ | |
| 673 | } |
| 674 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 675 | jc->free((void*)jc->parse_buffer); |
| 676 | } |
| 677 | jc->free((void*)jc); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | int JSON_parser_reset(JSON_parser jc) |
| 682 | { |
| 683 | if (NULL == jc) { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | jc->state = GO; |
| 688 | jc->top = -1; |
| 689 | |
| 690 | /* parser has been used previously? */ |
| 691 | if (NULL == jc->parse_buffer) { |
| 692 | |
| 693 | /* Do we want non-bound stack? */ |
| 694 | if (jc->depth > 0) { |
| 695 | jc->stack_capacity = jc->depth; |
| 696 | if (jc->depth <= (int)COUNTOF(jc->static_stack)) { |
| 697 | jc->stack = &jc->static_stack[0]; |
| @@ -705,20 +702,20 @@ | |
| 705 | } else { |
| 706 | jc->stack_capacity = (int)COUNTOF(jc->static_stack); |
| 707 | jc->depth = -1; |
| 708 | jc->stack = &jc->static_stack[0]; |
| 709 | } |
| 710 | |
| 711 | /* set up the parse buffer */ |
| 712 | jc->parse_buffer = &jc->static_parse_buffer[0]; |
| 713 | jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer); |
| 714 | } |
| 715 | |
| 716 | /* set parser to start */ |
| 717 | push(jc, MODE_DONE); |
| 718 | parse_buffer_clear(jc); |
| 719 | |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | JSON_parser |
| 724 | new_JSON_parser(JSON_config const * config) |
| @@ -735,29 +732,29 @@ | |
| 735 | |
| 736 | int use_std_malloc = false; |
| 737 | JSON_config default_config; |
| 738 | JSON_parser jc; |
| 739 | JSON_malloc_t alloc; |
| 740 | |
| 741 | /* set to default configuration if none was provided */ |
| 742 | if (NULL == config) { |
| 743 | /* initialize configuration */ |
| 744 | init_JSON_config(&default_config); |
| 745 | config = &default_config; |
| 746 | } |
| 747 | |
| 748 | /* use std malloc if either the allocator or deallocator function isn't set */ |
| 749 | use_std_malloc = NULL == config->malloc || NULL == config->free; |
| 750 | |
| 751 | alloc = use_std_malloc ? malloc : config->malloc; |
| 752 | |
| 753 | jc = JSON_parser_malloc(alloc, sizeof(*jc), "parser"); |
| 754 | |
| 755 | if (NULL == jc) { |
| 756 | return NULL; |
| 757 | } |
| 758 | |
| 759 | /* configure the parser */ |
| 760 | memset(jc, 0, sizeof(*jc)); |
| 761 | jc->malloc = alloc; |
| 762 | jc->free = use_std_malloc ? free : config->free; |
| 763 | jc->callback = config->callback; |
| @@ -765,42 +762,42 @@ | |
| 765 | jc->allow_comments = (signed char)(config->allow_comments != 0); |
| 766 | jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0); |
| 767 | jc->decimal_point = *localeconv()->decimal_point; |
| 768 | /* We need to be able to push at least one object */ |
| 769 | jc->depth = config->depth == 0 ? 1 : config->depth; |
| 770 | |
| 771 | /* reset the parser */ |
| 772 | if (!JSON_parser_reset(jc)) { |
| 773 | jc->free(jc); |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | return jc; |
| 778 | } |
| 779 | |
| 780 | static int parse_buffer_grow(JSON_parser jc) |
| 781 | { |
| 782 | const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]); |
| 783 | const size_t new_capacity = jc->parse_buffer_capacity * 2; |
| 784 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]); |
| 785 | void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer"); |
| 786 | |
| 787 | if (mem == NULL) { |
| 788 | jc->error = JSON_E_OUT_OF_MEMORY; |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | assert(new_capacity > 0); |
| 793 | memcpy(mem, jc->parse_buffer, bytes_to_copy); |
| 794 | |
| 795 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 796 | jc->free(jc->parse_buffer); |
| 797 | } |
| 798 | |
| 799 | jc->parse_buffer = (char*)mem; |
| 800 | jc->parse_buffer_capacity = new_capacity; |
| 801 | |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars) |
| 806 | { |
| @@ -808,11 +805,11 @@ | |
| 808 | if (!parse_buffer_grow(jc)) { |
| 809 | assert(jc->error == JSON_E_OUT_OF_MEMORY); |
| 810 | return false; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | return true; |
| 815 | } |
| 816 | |
| 817 | #define parse_buffer_has_space_for(jc, count) \ |
| 818 | (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity) |
| @@ -830,27 +827,27 @@ | |
| 830 | jc->type == JSON_T_FALSE || \ |
| 831 | jc->type == JSON_T_TRUE || \ |
| 832 | jc->type == JSON_T_FLOAT || \ |
| 833 | jc->type == JSON_T_INTEGER || \ |
| 834 | jc->type == JSON_T_STRING) |
| 835 | |
| 836 | |
| 837 | static int parse_parse_buffer(JSON_parser jc) |
| 838 | { |
| 839 | if (jc->callback) { |
| 840 | JSON_value value, *arg = NULL; |
| 841 | |
| 842 | if (jc->type != JSON_T_NONE) { |
| 843 | assert_is_non_container_type(jc); |
| 844 | |
| 845 | switch(jc->type) { |
| 846 | case JSON_T_FLOAT: |
| 847 | arg = &value; |
| 848 | if (jc->handle_floats_manually) { |
| 849 | value.vu.str.value = jc->parse_buffer; |
| 850 | value.vu.str.length = jc->parse_buffer_count; |
| 851 | } else { |
| 852 | /* not checking with end pointer b/c there may be trailing ws */ |
| 853 | value.vu.float_value = strtod(jc->parse_buffer, NULL); |
| 854 | } |
| 855 | break; |
| 856 | case JSON_T_INTEGER: |
| @@ -861,19 +858,19 @@ | |
| 861 | arg = &value; |
| 862 | value.vu.str.value = jc->parse_buffer; |
| 863 | value.vu.str.length = jc->parse_buffer_count; |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | if (!(*jc->callback)(jc->ctx, jc->type, arg)) { |
| 868 | return false; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | parse_buffer_clear(jc); |
| 874 | |
| 875 | return true; |
| 876 | } |
| 877 | |
| 878 | #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800) |
| 879 | #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00) |
| @@ -884,35 +881,39 @@ | |
| 884 | { |
| 885 | int i; |
| 886 | unsigned uc = 0; |
| 887 | char* p; |
| 888 | int trail_bytes; |
| 889 | |
| 890 | assert(jc->parse_buffer_count >= 6); |
| 891 | |
| 892 | p = &jc->parse_buffer[jc->parse_buffer_count - 4]; |
| 893 | |
| 894 | for (i = 12; i >= 0; i -= 4, ++p) { |
| 895 | unsigned x = *p; |
| 896 | |
| 897 | if (x >= 'a') { |
| 898 | x -= ('a' - 10); |
| 899 | } else if (x >= 'A') { |
| 900 | x -= ('A' - 10); |
| 901 | } else { |
| 902 | x &= ~0x30u; |
| 903 | } |
| 904 | |
| 905 | assert(x < 16); |
| 906 | |
| 907 | uc |= x << i; |
| 908 | } |
| 909 | |
| 910 | /* clear UTF-16 char from buffer */ |
| 911 | jc->parse_buffer_count -= 6; |
| 912 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 913 | |
| 914 | /* attempt decoding ... */ |
| 915 | if (jc->utf16_high_surrogate) { |
| 916 | if (IS_LOW_SURROGATE(uc)) { |
| 917 | uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc); |
| 918 | trail_bytes = 3; |
| @@ -935,26 +936,26 @@ | |
| 935 | return false; |
| 936 | } else { |
| 937 | trail_bytes = 2; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]); |
| 942 | |
| 943 | for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) { |
| 944 | jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80); |
| 945 | } |
| 946 | |
| 947 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 948 | |
| 949 | return true; |
| 950 | } |
| 951 | |
| 952 | static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char) |
| 953 | { |
| 954 | assert(parse_buffer_has_space_for(jc, 1)); |
| 955 | |
| 956 | jc->escaped = 0; |
| 957 | /* remove the backslash */ |
| 958 | parse_buffer_pop_back_char(jc); |
| 959 | switch(next_char) { |
| 960 | case 'b': |
| @@ -996,22 +997,22 @@ | |
| 996 | { |
| 997 | if (!parse_buffer_reserve_for(jc, 1)) { |
| 998 | assert(JSON_E_OUT_OF_MEMORY == jc->error); |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | if (jc->escaped) { |
| 1003 | if (!add_escaped_char_to_parse_buffer(jc, next_char)) { |
| 1004 | jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE; |
| 1005 | return false; |
| 1006 | } |
| 1007 | } else if (!jc->comment) { |
| 1008 | if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) { |
| 1009 | parse_buffer_push_back_char(jc, (char)next_char); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
| 1016 | #define assert_type_isnt_string_null_or_bool(jc) \ |
| 1017 | assert(jc->type != JSON_T_FALSE); \ |
| @@ -1031,13 +1032,13 @@ | |
| 1031 | */ |
| 1032 | int next_class, next_state; |
| 1033 | |
| 1034 | /* |
| 1035 | Store the current char for error handling |
| 1036 | */ |
| 1037 | jc->current_char = next_char; |
| 1038 | |
| 1039 | /* |
| 1040 | Determine the character's class. |
| 1041 | */ |
| 1042 | if (next_char < 0) { |
| 1043 | jc->error = JSON_E_INVALID_CHAR; |
| @@ -1050,15 +1051,15 @@ | |
| 1050 | if (next_class <= __) { |
| 1051 | set_error(jc); |
| 1052 | return false; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | if (!add_char_to_parse_buffer(jc, next_char, next_class)) { |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | Get the next state from the state transition table. |
| 1062 | */ |
| 1063 | next_state = state_transition_table[jc->state][next_class]; |
| 1064 | if (next_state >= 0) { |
| @@ -1069,11 +1070,11 @@ | |
| 1069 | } else { |
| 1070 | /* |
| 1071 | Or perform one of the actions. |
| 1072 | */ |
| 1073 | switch (next_state) { |
| 1074 | /* Unicode character */ |
| 1075 | case UC: |
| 1076 | if(!decode_unicode_char(jc)) { |
| 1077 | jc->error = JSON_E_INVALID_UNICODE_SEQUENCE; |
| 1078 | return false; |
| 1079 | } |
| @@ -1091,78 +1092,78 @@ | |
| 1091 | break; |
| 1092 | /* integer detected by minus */ |
| 1093 | case MX: |
| 1094 | jc->type = JSON_T_INTEGER; |
| 1095 | jc->state = MI; |
| 1096 | break; |
| 1097 | /* integer detected by zero */ |
| 1098 | case ZX: |
| 1099 | jc->type = JSON_T_INTEGER; |
| 1100 | jc->state = ZE; |
| 1101 | break; |
| 1102 | /* integer detected by 1-9 */ |
| 1103 | case IX: |
| 1104 | jc->type = JSON_T_INTEGER; |
| 1105 | jc->state = IT; |
| 1106 | break; |
| 1107 | |
| 1108 | /* floating point number detected by exponent*/ |
| 1109 | case DE: |
| 1110 | assert_type_isnt_string_null_or_bool(jc); |
| 1111 | jc->type = JSON_T_FLOAT; |
| 1112 | jc->state = E1; |
| 1113 | break; |
| 1114 | |
| 1115 | /* floating point number detected by fraction */ |
| 1116 | case DF: |
| 1117 | assert_type_isnt_string_null_or_bool(jc); |
| 1118 | if (!jc->handle_floats_manually) { |
| 1119 | /* |
| 1120 | Some versions of strtod (which underlies sscanf) don't support converting |
| 1121 | C-locale formated floating point values. |
| 1122 | */ |
| 1123 | assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.'); |
| 1124 | jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point; |
| 1125 | } |
| 1126 | jc->type = JSON_T_FLOAT; |
| 1127 | jc->state = FX; |
| 1128 | break; |
| 1129 | /* string begin " */ |
| 1130 | case SB: |
| 1131 | parse_buffer_clear(jc); |
| 1132 | assert(jc->type == JSON_T_NONE); |
| 1133 | jc->type = JSON_T_STRING; |
| 1134 | jc->state = ST; |
| 1135 | break; |
| 1136 | |
| 1137 | /* n */ |
| 1138 | case NU: |
| 1139 | assert(jc->type == JSON_T_NONE); |
| 1140 | jc->type = JSON_T_NULL; |
| 1141 | jc->state = N1; |
| 1142 | break; |
| 1143 | /* f */ |
| 1144 | case FA: |
| 1145 | assert(jc->type == JSON_T_NONE); |
| 1146 | jc->type = JSON_T_FALSE; |
| 1147 | jc->state = F1; |
| 1148 | break; |
| 1149 | /* t */ |
| 1150 | case TR: |
| 1151 | assert(jc->type == JSON_T_NONE); |
| 1152 | jc->type = JSON_T_TRUE; |
| 1153 | jc->state = T1; |
| 1154 | break; |
| 1155 | |
| 1156 | /* closing comment */ |
| 1157 | case CE: |
| 1158 | jc->comment = 0; |
| 1159 | assert(jc->parse_buffer_count == 0); |
| 1160 | assert(jc->type == JSON_T_NONE); |
| 1161 | jc->state = jc->before_comment_state; |
| 1162 | break; |
| 1163 | |
| 1164 | /* opening comment */ |
| 1165 | case CB: |
| 1166 | if (!jc->allow_comments) { |
| 1167 | return false; |
| 1168 | } |
| @@ -1172,11 +1173,11 @@ | |
| 1172 | } |
| 1173 | assert(jc->parse_buffer_count == 0); |
| 1174 | assert(jc->type != JSON_T_STRING); |
| 1175 | switch (jc->stack[jc->top]) { |
| 1176 | case MODE_ARRAY: |
| 1177 | case MODE_OBJECT: |
| 1178 | switch(jc->state) { |
| 1179 | case VA: |
| 1180 | case AR: |
| 1181 | jc->before_comment_state = jc->state; |
| 1182 | break; |
| @@ -1192,11 +1193,11 @@ | |
| 1192 | jc->type = JSON_T_NONE; |
| 1193 | jc->state = C1; |
| 1194 | jc->comment = 1; |
| 1195 | break; |
| 1196 | /* empty } */ |
| 1197 | case -9: |
| 1198 | parse_buffer_clear(jc); |
| 1199 | if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | if (!pop(jc, MODE_KEY)) { |
| @@ -1231,11 +1232,11 @@ | |
| 1231 | } |
| 1232 | if (!pop(jc, MODE_ARRAY)) { |
| 1233 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | jc->type = JSON_T_NONE; |
| 1238 | jc->state = OK; |
| 1239 | break; |
| 1240 | |
| 1241 | /* { */ case -6: |
| @@ -1267,11 +1268,11 @@ | |
| 1267 | switch (jc->stack[jc->top]) { |
| 1268 | case MODE_KEY: |
| 1269 | assert(jc->type == JSON_T_STRING); |
| 1270 | jc->type = JSON_T_NONE; |
| 1271 | jc->state = CO; |
| 1272 | |
| 1273 | if (jc->callback) { |
| 1274 | JSON_value value; |
| 1275 | value.vu.str.value = jc->parse_buffer; |
| 1276 | value.vu.str.length = jc->parse_buffer_count; |
| 1277 | if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) { |
| @@ -1357,29 +1358,29 @@ | |
| 1357 | |
| 1358 | |
| 1359 | int JSON_parser_is_legal_white_space_string(const char* s) |
| 1360 | { |
| 1361 | int c, char_class; |
| 1362 | |
| 1363 | if (s == NULL) { |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | for (; *s; ++s) { |
| 1368 | c = *s; |
| 1369 | |
| 1370 | if (c < 0 || c >= 128) { |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | char_class = ascii_class[c]; |
| 1375 | |
| 1376 | if (char_class != C_SPACE && char_class != C_WHITE) { |
| 1377 | return false; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
| 1384 | int JSON_parser_get_last_error(JSON_parser jc) |
| 1385 | { |
| @@ -1389,16 +1390,17 @@ | |
| 1389 | |
| 1390 | void init_JSON_config(JSON_config* config) |
| 1391 | { |
| 1392 | if (config) { |
| 1393 | memset(config, 0, sizeof(*config)); |
| 1394 | |
| 1395 | config->depth = JSON_PARSER_STACK_SIZE - 1; |
| 1396 | config->malloc = malloc; |
| 1397 | config->free = free; |
| 1398 | } |
| 1399 | } |
| 1400 | /* end file parser/JSON_parser.c */ |
| 1401 | /* begin file ./cson.c */ |
| 1402 | #include <assert.h> |
| 1403 | #include <stdlib.h> /* malloc()/free() */ |
| 1404 | #include <string.h> |
| 1405 |
| --- src/cson_amalgamation.c | |
| +++ src/cson_amalgamation.c | |
| @@ -26,15 +26,11 @@ | |
| 26 | #else |
| 27 | # define JSON_PARSER_DLL_API |
| 28 | #endif |
| 29 | |
| 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | #if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1 |
| 32 | typedef long long JSON_int_t; |
| 33 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 35 | #else |
| 36 | typedef long JSON_int_t; |
| @@ -103,11 +99,11 @@ | |
| 99 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 100 | as zero-terminated C strings. |
| 101 | |
| 102 | \return Non-zero if parsing should continue, else zero. |
| 103 | */ |
| 104 | typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value); |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | A typedef for allocator functions semantically compatible with malloc(). |
| 109 | */ |
| @@ -233,11 +229,11 @@ | |
| 229 | |
| 230 | #endif /* JSON_PARSER_H */ |
| 231 | /* end file parser/JSON_parser.h */ |
| 232 | /* begin file parser/JSON_parser.c */ |
| 233 | /* |
| 234 | Copyright (c) 2007-2013 Jean Gressmann ([email protected]) |
| 235 | |
| 236 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 237 | of this software and associated documentation files (the "Software"), to deal |
| 238 | in the Software without restriction, including without limitation the rights |
| 239 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| @@ -245,12 +241,10 @@ | |
| 241 | furnished to do so, subject to the following conditions: |
| 242 | |
| 243 | The above copyright notice and this permission notice shall be included in all |
| 244 | copies or substantial portions of the Software. |
| 245 | |
| 246 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 247 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 248 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 249 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 250 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| @@ -257,49 +251,52 @@ | |
| 251 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 252 | SOFTWARE. |
| 253 | */ |
| 254 | |
| 255 | /* |
| 256 | Changelog: |
| 257 | 2013-09-08 |
| 258 | Updated license to to be compatible with Debian license requirements. |
| 259 | |
| 260 | 2012-06-06 |
| 261 | Fix for invalid UTF16 characters and some comment fixex ([email protected]). |
| 262 | |
| 263 | 2010-11-25 |
| 264 | Support for custom memory allocation ([email protected]). |
| 265 | |
| 266 | 2010-05-07 |
| 267 | Added error handling for memory allocation failure ([email protected]). |
| 268 | Added diagnosis errors for invalid JSON. |
| 269 | |
| 270 | 2010-03-25 |
| 271 | Fixed buffer overrun in grow_parse_buffer & cleaned up code. |
| 272 | |
| 273 | 2009-10-19 |
| 274 | Replaced long double in JSON_value_struct with double after reports |
| 275 | of strtold being broken on some platforms ([email protected]). |
| 276 | |
| 277 | 2009-05-17 |
| 278 | Incorporated [email protected] fix for UTF16 decoding. |
| 279 | |
| 280 | 2009-05-14 |
| 281 | Fixed float parsing bug related to a locale being set that didn't |
| 282 | use '.' as decimal point character ([email protected]). |
| 283 | |
| 284 | 2008-10-14 |
| 285 | Renamed states.IN to states.IT to avoid name clash which IN macro |
| 286 | defined in windef.h ([email protected]) |
| 287 | |
| 288 | 2008-07-19 |
| 289 | Removed some duplicate code & debugging variable ([email protected]) |
| 290 | |
| 291 | 2008-05-28 |
| 292 | Made JSON_value structure ansi C compliant. This bug was report by |
| 293 | [email protected] |
| 294 | |
| 295 | 2008-05-20 |
| 296 | Fixed bug reported by [email protected] where the switching |
| 297 | from static to dynamic parse buffer did not copy the static parse |
| 298 | buffer's content. |
| 299 | */ |
| 300 | |
| 301 | |
| 302 | |
| @@ -362,11 +359,11 @@ | |
| 359 | char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE]; |
| 360 | JSON_malloc_t malloc; |
| 361 | JSON_free_t free; |
| 362 | }; |
| 363 | |
| 364 | #define COUNTOF(x) (sizeof(x)/sizeof(x[0])) |
| 365 | |
| 366 | /* |
| 367 | Characters are mapped into these character classes. This allows for |
| 368 | a significant reduction in the size of the state transition table. |
| 369 | */ |
| @@ -403,11 +400,11 @@ | |
| 400 | C_LOW_T, /* t */ |
| 401 | C_LOW_U, /* u */ |
| 402 | C_ABCDF, /* ABCDF */ |
| 403 | C_E, /* E */ |
| 404 | C_ETC, /* everything else */ |
| 405 | C_STAR, /* * */ |
| 406 | NR_CLASSES |
| 407 | }; |
| 408 | |
| 409 | static const signed char ascii_class[128] = { |
| 410 | /* |
| @@ -419,11 +416,11 @@ | |
| 416 | __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __, |
| 417 | __, __, __, __, __, __, __, __, |
| 418 | __, __, __, __, __, __, __, __, |
| 419 | |
| 420 | C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 421 | C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH, |
| 422 | C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, |
| 423 | C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| 424 | |
| 425 | C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC, |
| 426 | C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, |
| @@ -536,11 +533,11 @@ | |
| 533 | /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__}, |
| 534 | /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__}, |
| 535 | /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__}, |
| 536 | /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__}, |
| 537 | /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2}, |
| 538 | /*/star C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, |
| 539 | /** C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3}, |
| 540 | /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__}, |
| 541 | /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__}, |
| 542 | /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__}, |
| 543 | }; |
| @@ -548,27 +545,27 @@ | |
| 545 | |
| 546 | /* |
| 547 | These modes can be pushed on the stack. |
| 548 | */ |
| 549 | enum modes { |
| 550 | MODE_ARRAY = 1, |
| 551 | MODE_DONE = 2, |
| 552 | MODE_KEY = 3, |
| 553 | MODE_OBJECT = 4 |
| 554 | }; |
| 555 | |
| 556 | static void set_error(JSON_parser jc) |
| 557 | { |
| 558 | switch (jc->state) { |
| 559 | case GO: |
| 560 | switch (jc->current_char) { |
| 561 | case '{': case '}': case '[': case ']': |
| 562 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 563 | break; |
| 564 | default: |
| 565 | jc->error = JSON_E_INVALID_CHAR; |
| 566 | break; |
| 567 | } |
| 568 | break; |
| 569 | case OB: |
| 570 | jc->error = JSON_E_EXPECTED_KEY; |
| 571 | break; |
| @@ -604,11 +601,11 @@ | |
| 601 | { |
| 602 | /* |
| 603 | Push a mode onto the stack. Return false if there is overflow. |
| 604 | */ |
| 605 | assert(jc->top <= jc->stack_capacity); |
| 606 | |
| 607 | if (jc->depth < 0) { |
| 608 | if (jc->top == jc->stack_capacity) { |
| 609 | const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]); |
| 610 | const size_t new_capacity = jc->stack_capacity * 2; |
| 611 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]); |
| @@ -653,17 +650,17 @@ | |
| 650 | #define parse_buffer_clear(jc) \ |
| 651 | do {\ |
| 652 | jc->parse_buffer_count = 0;\ |
| 653 | jc->parse_buffer[0] = 0;\ |
| 654 | } while (0) |
| 655 | |
| 656 | #define parse_buffer_pop_back_char(jc)\ |
| 657 | do {\ |
| 658 | assert(jc->parse_buffer_count >= 1);\ |
| 659 | --jc->parse_buffer_count;\ |
| 660 | jc->parse_buffer[jc->parse_buffer_count] = 0;\ |
| 661 | } while (0) |
| 662 | |
| 663 | |
| 664 | |
| 665 | void delete_JSON_parser(JSON_parser jc) |
| 666 | { |
| @@ -673,25 +670,25 @@ | |
| 670 | } |
| 671 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 672 | jc->free((void*)jc->parse_buffer); |
| 673 | } |
| 674 | jc->free((void*)jc); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | int JSON_parser_reset(JSON_parser jc) |
| 679 | { |
| 680 | if (NULL == jc) { |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | jc->state = GO; |
| 685 | jc->top = -1; |
| 686 | |
| 687 | /* parser has been used previously? */ |
| 688 | if (NULL == jc->parse_buffer) { |
| 689 | |
| 690 | /* Do we want non-bound stack? */ |
| 691 | if (jc->depth > 0) { |
| 692 | jc->stack_capacity = jc->depth; |
| 693 | if (jc->depth <= (int)COUNTOF(jc->static_stack)) { |
| 694 | jc->stack = &jc->static_stack[0]; |
| @@ -705,20 +702,20 @@ | |
| 702 | } else { |
| 703 | jc->stack_capacity = (int)COUNTOF(jc->static_stack); |
| 704 | jc->depth = -1; |
| 705 | jc->stack = &jc->static_stack[0]; |
| 706 | } |
| 707 | |
| 708 | /* set up the parse buffer */ |
| 709 | jc->parse_buffer = &jc->static_parse_buffer[0]; |
| 710 | jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer); |
| 711 | } |
| 712 | |
| 713 | /* set parser to start */ |
| 714 | push(jc, MODE_DONE); |
| 715 | parse_buffer_clear(jc); |
| 716 | |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | JSON_parser |
| 721 | new_JSON_parser(JSON_config const * config) |
| @@ -735,29 +732,29 @@ | |
| 732 | |
| 733 | int use_std_malloc = false; |
| 734 | JSON_config default_config; |
| 735 | JSON_parser jc; |
| 736 | JSON_malloc_t alloc; |
| 737 | |
| 738 | /* set to default configuration if none was provided */ |
| 739 | if (NULL == config) { |
| 740 | /* initialize configuration */ |
| 741 | init_JSON_config(&default_config); |
| 742 | config = &default_config; |
| 743 | } |
| 744 | |
| 745 | /* use std malloc if either the allocator or deallocator function isn't set */ |
| 746 | use_std_malloc = NULL == config->malloc || NULL == config->free; |
| 747 | |
| 748 | alloc = use_std_malloc ? malloc : config->malloc; |
| 749 | |
| 750 | jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser"); |
| 751 | |
| 752 | if (NULL == jc) { |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | /* configure the parser */ |
| 757 | memset(jc, 0, sizeof(*jc)); |
| 758 | jc->malloc = alloc; |
| 759 | jc->free = use_std_malloc ? free : config->free; |
| 760 | jc->callback = config->callback; |
| @@ -765,42 +762,42 @@ | |
| 762 | jc->allow_comments = (signed char)(config->allow_comments != 0); |
| 763 | jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0); |
| 764 | jc->decimal_point = *localeconv()->decimal_point; |
| 765 | /* We need to be able to push at least one object */ |
| 766 | jc->depth = config->depth == 0 ? 1 : config->depth; |
| 767 | |
| 768 | /* reset the parser */ |
| 769 | if (!JSON_parser_reset(jc)) { |
| 770 | jc->free(jc); |
| 771 | return NULL; |
| 772 | } |
| 773 | |
| 774 | return jc; |
| 775 | } |
| 776 | |
| 777 | static int parse_buffer_grow(JSON_parser jc) |
| 778 | { |
| 779 | const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]); |
| 780 | const size_t new_capacity = jc->parse_buffer_capacity * 2; |
| 781 | const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]); |
| 782 | void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer"); |
| 783 | |
| 784 | if (mem == NULL) { |
| 785 | jc->error = JSON_E_OUT_OF_MEMORY; |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | assert(new_capacity > 0); |
| 790 | memcpy(mem, jc->parse_buffer, bytes_to_copy); |
| 791 | |
| 792 | if (jc->parse_buffer != &jc->static_parse_buffer[0]) { |
| 793 | jc->free(jc->parse_buffer); |
| 794 | } |
| 795 | |
| 796 | jc->parse_buffer = (char*)mem; |
| 797 | jc->parse_buffer_capacity = new_capacity; |
| 798 | |
| 799 | return true; |
| 800 | } |
| 801 | |
| 802 | static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars) |
| 803 | { |
| @@ -808,11 +805,11 @@ | |
| 805 | if (!parse_buffer_grow(jc)) { |
| 806 | assert(jc->error == JSON_E_OUT_OF_MEMORY); |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | #define parse_buffer_has_space_for(jc, count) \ |
| 815 | (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity) |
| @@ -830,27 +827,27 @@ | |
| 827 | jc->type == JSON_T_FALSE || \ |
| 828 | jc->type == JSON_T_TRUE || \ |
| 829 | jc->type == JSON_T_FLOAT || \ |
| 830 | jc->type == JSON_T_INTEGER || \ |
| 831 | jc->type == JSON_T_STRING) |
| 832 | |
| 833 | |
| 834 | static int parse_parse_buffer(JSON_parser jc) |
| 835 | { |
| 836 | if (jc->callback) { |
| 837 | JSON_value value, *arg = NULL; |
| 838 | |
| 839 | if (jc->type != JSON_T_NONE) { |
| 840 | assert_is_non_container_type(jc); |
| 841 | |
| 842 | switch(jc->type) { |
| 843 | case JSON_T_FLOAT: |
| 844 | arg = &value; |
| 845 | if (jc->handle_floats_manually) { |
| 846 | value.vu.str.value = jc->parse_buffer; |
| 847 | value.vu.str.length = jc->parse_buffer_count; |
| 848 | } else { |
| 849 | /* not checking with end pointer b/c there may be trailing ws */ |
| 850 | value.vu.float_value = strtod(jc->parse_buffer, NULL); |
| 851 | } |
| 852 | break; |
| 853 | case JSON_T_INTEGER: |
| @@ -861,19 +858,19 @@ | |
| 858 | arg = &value; |
| 859 | value.vu.str.value = jc->parse_buffer; |
| 860 | value.vu.str.length = jc->parse_buffer_count; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (!(*jc->callback)(jc->ctx, jc->type, arg)) { |
| 865 | return false; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | parse_buffer_clear(jc); |
| 871 | |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800) |
| 876 | #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00) |
| @@ -884,35 +881,39 @@ | |
| 881 | { |
| 882 | int i; |
| 883 | unsigned uc = 0; |
| 884 | char* p; |
| 885 | int trail_bytes; |
| 886 | |
| 887 | assert(jc->parse_buffer_count >= 6); |
| 888 | |
| 889 | p = &jc->parse_buffer[jc->parse_buffer_count - 4]; |
| 890 | |
| 891 | for (i = 12; i >= 0; i -= 4, ++p) { |
| 892 | unsigned x = *p; |
| 893 | |
| 894 | if (x >= 'a') { |
| 895 | x -= ('a' - 10); |
| 896 | } else if (x >= 'A') { |
| 897 | x -= ('A' - 10); |
| 898 | } else { |
| 899 | x &= ~0x30u; |
| 900 | } |
| 901 | |
| 902 | assert(x < 16); |
| 903 | |
| 904 | uc |= x << i; |
| 905 | } |
| 906 | |
| 907 | /* clear UTF-16 char from buffer */ |
| 908 | jc->parse_buffer_count -= 6; |
| 909 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 910 | |
| 911 | if (uc == 0xffff || uc == 0xfffe) { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | /* attempt decoding ... */ |
| 916 | if (jc->utf16_high_surrogate) { |
| 917 | if (IS_LOW_SURROGATE(uc)) { |
| 918 | uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc); |
| 919 | trail_bytes = 3; |
| @@ -935,26 +936,26 @@ | |
| 936 | return false; |
| 937 | } else { |
| 938 | trail_bytes = 2; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]); |
| 943 | |
| 944 | for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) { |
| 945 | jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80); |
| 946 | } |
| 947 | |
| 948 | jc->parse_buffer[jc->parse_buffer_count] = 0; |
| 949 | |
| 950 | return true; |
| 951 | } |
| 952 | |
| 953 | static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char) |
| 954 | { |
| 955 | assert(parse_buffer_has_space_for(jc, 1)); |
| 956 | |
| 957 | jc->escaped = 0; |
| 958 | /* remove the backslash */ |
| 959 | parse_buffer_pop_back_char(jc); |
| 960 | switch(next_char) { |
| 961 | case 'b': |
| @@ -996,22 +997,22 @@ | |
| 997 | { |
| 998 | if (!parse_buffer_reserve_for(jc, 1)) { |
| 999 | assert(JSON_E_OUT_OF_MEMORY == jc->error); |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | if (jc->escaped) { |
| 1004 | if (!add_escaped_char_to_parse_buffer(jc, next_char)) { |
| 1005 | jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE; |
| 1006 | return false; |
| 1007 | } |
| 1008 | } else if (!jc->comment) { |
| 1009 | if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) { |
| 1010 | parse_buffer_push_back_char(jc, (char)next_char); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | #define assert_type_isnt_string_null_or_bool(jc) \ |
| 1018 | assert(jc->type != JSON_T_FALSE); \ |
| @@ -1031,13 +1032,13 @@ | |
| 1032 | */ |
| 1033 | int next_class, next_state; |
| 1034 | |
| 1035 | /* |
| 1036 | Store the current char for error handling |
| 1037 | */ |
| 1038 | jc->current_char = next_char; |
| 1039 | |
| 1040 | /* |
| 1041 | Determine the character's class. |
| 1042 | */ |
| 1043 | if (next_char < 0) { |
| 1044 | jc->error = JSON_E_INVALID_CHAR; |
| @@ -1050,15 +1051,15 @@ | |
| 1051 | if (next_class <= __) { |
| 1052 | set_error(jc); |
| 1053 | return false; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | if (!add_char_to_parse_buffer(jc, next_char, next_class)) { |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | Get the next state from the state transition table. |
| 1063 | */ |
| 1064 | next_state = state_transition_table[jc->state][next_class]; |
| 1065 | if (next_state >= 0) { |
| @@ -1069,11 +1070,11 @@ | |
| 1070 | } else { |
| 1071 | /* |
| 1072 | Or perform one of the actions. |
| 1073 | */ |
| 1074 | switch (next_state) { |
| 1075 | /* Unicode character */ |
| 1076 | case UC: |
| 1077 | if(!decode_unicode_char(jc)) { |
| 1078 | jc->error = JSON_E_INVALID_UNICODE_SEQUENCE; |
| 1079 | return false; |
| 1080 | } |
| @@ -1091,78 +1092,78 @@ | |
| 1092 | break; |
| 1093 | /* integer detected by minus */ |
| 1094 | case MX: |
| 1095 | jc->type = JSON_T_INTEGER; |
| 1096 | jc->state = MI; |
| 1097 | break; |
| 1098 | /* integer detected by zero */ |
| 1099 | case ZX: |
| 1100 | jc->type = JSON_T_INTEGER; |
| 1101 | jc->state = ZE; |
| 1102 | break; |
| 1103 | /* integer detected by 1-9 */ |
| 1104 | case IX: |
| 1105 | jc->type = JSON_T_INTEGER; |
| 1106 | jc->state = IT; |
| 1107 | break; |
| 1108 | |
| 1109 | /* floating point number detected by exponent*/ |
| 1110 | case DE: |
| 1111 | assert_type_isnt_string_null_or_bool(jc); |
| 1112 | jc->type = JSON_T_FLOAT; |
| 1113 | jc->state = E1; |
| 1114 | break; |
| 1115 | |
| 1116 | /* floating point number detected by fraction */ |
| 1117 | case DF: |
| 1118 | assert_type_isnt_string_null_or_bool(jc); |
| 1119 | if (!jc->handle_floats_manually) { |
| 1120 | /* |
| 1121 | Some versions of strtod (which underlies sscanf) don't support converting |
| 1122 | C-locale formated floating point values. |
| 1123 | */ |
| 1124 | assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.'); |
| 1125 | jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point; |
| 1126 | } |
| 1127 | jc->type = JSON_T_FLOAT; |
| 1128 | jc->state = FX; |
| 1129 | break; |
| 1130 | /* string begin " */ |
| 1131 | case SB: |
| 1132 | parse_buffer_clear(jc); |
| 1133 | assert(jc->type == JSON_T_NONE); |
| 1134 | jc->type = JSON_T_STRING; |
| 1135 | jc->state = ST; |
| 1136 | break; |
| 1137 | |
| 1138 | /* n */ |
| 1139 | case NU: |
| 1140 | assert(jc->type == JSON_T_NONE); |
| 1141 | jc->type = JSON_T_NULL; |
| 1142 | jc->state = N1; |
| 1143 | break; |
| 1144 | /* f */ |
| 1145 | case FA: |
| 1146 | assert(jc->type == JSON_T_NONE); |
| 1147 | jc->type = JSON_T_FALSE; |
| 1148 | jc->state = F1; |
| 1149 | break; |
| 1150 | /* t */ |
| 1151 | case TR: |
| 1152 | assert(jc->type == JSON_T_NONE); |
| 1153 | jc->type = JSON_T_TRUE; |
| 1154 | jc->state = T1; |
| 1155 | break; |
| 1156 | |
| 1157 | /* closing comment */ |
| 1158 | case CE: |
| 1159 | jc->comment = 0; |
| 1160 | assert(jc->parse_buffer_count == 0); |
| 1161 | assert(jc->type == JSON_T_NONE); |
| 1162 | jc->state = jc->before_comment_state; |
| 1163 | break; |
| 1164 | |
| 1165 | /* opening comment */ |
| 1166 | case CB: |
| 1167 | if (!jc->allow_comments) { |
| 1168 | return false; |
| 1169 | } |
| @@ -1172,11 +1173,11 @@ | |
| 1173 | } |
| 1174 | assert(jc->parse_buffer_count == 0); |
| 1175 | assert(jc->type != JSON_T_STRING); |
| 1176 | switch (jc->stack[jc->top]) { |
| 1177 | case MODE_ARRAY: |
| 1178 | case MODE_OBJECT: |
| 1179 | switch(jc->state) { |
| 1180 | case VA: |
| 1181 | case AR: |
| 1182 | jc->before_comment_state = jc->state; |
| 1183 | break; |
| @@ -1192,11 +1193,11 @@ | |
| 1193 | jc->type = JSON_T_NONE; |
| 1194 | jc->state = C1; |
| 1195 | jc->comment = 1; |
| 1196 | break; |
| 1197 | /* empty } */ |
| 1198 | case -9: |
| 1199 | parse_buffer_clear(jc); |
| 1200 | if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) { |
| 1201 | return false; |
| 1202 | } |
| 1203 | if (!pop(jc, MODE_KEY)) { |
| @@ -1231,11 +1232,11 @@ | |
| 1232 | } |
| 1233 | if (!pop(jc, MODE_ARRAY)) { |
| 1234 | jc->error = JSON_E_UNBALANCED_COLLECTION; |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | jc->type = JSON_T_NONE; |
| 1239 | jc->state = OK; |
| 1240 | break; |
| 1241 | |
| 1242 | /* { */ case -6: |
| @@ -1267,11 +1268,11 @@ | |
| 1268 | switch (jc->stack[jc->top]) { |
| 1269 | case MODE_KEY: |
| 1270 | assert(jc->type == JSON_T_STRING); |
| 1271 | jc->type = JSON_T_NONE; |
| 1272 | jc->state = CO; |
| 1273 | |
| 1274 | if (jc->callback) { |
| 1275 | JSON_value value; |
| 1276 | value.vu.str.value = jc->parse_buffer; |
| 1277 | value.vu.str.length = jc->parse_buffer_count; |
| 1278 | if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) { |
| @@ -1357,29 +1358,29 @@ | |
| 1358 | |
| 1359 | |
| 1360 | int JSON_parser_is_legal_white_space_string(const char* s) |
| 1361 | { |
| 1362 | int c, char_class; |
| 1363 | |
| 1364 | if (s == NULL) { |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | for (; *s; ++s) { |
| 1369 | c = *s; |
| 1370 | |
| 1371 | if (c < 0 || c >= 128) { |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
| 1375 | char_class = ascii_class[c]; |
| 1376 | |
| 1377 | if (char_class != C_SPACE && char_class != C_WHITE) { |
| 1378 | return false; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | return true; |
| 1383 | } |
| 1384 | |
| 1385 | int JSON_parser_get_last_error(JSON_parser jc) |
| 1386 | { |
| @@ -1389,16 +1390,17 @@ | |
| 1390 | |
| 1391 | void init_JSON_config(JSON_config* config) |
| 1392 | { |
| 1393 | if (config) { |
| 1394 | memset(config, 0, sizeof(*config)); |
| 1395 | |
| 1396 | config->depth = JSON_PARSER_STACK_SIZE - 1; |
| 1397 | config->malloc = malloc; |
| 1398 | config->free = free; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* end file parser/JSON_parser.c */ |
| 1403 | /* begin file ./cson.c */ |
| 1404 | #include <assert.h> |
| 1405 | #include <stdlib.h> /* malloc()/free() */ |
| 1406 | #include <string.h> |
| 1407 |