1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
|
/* The C part is broken into three pieces, "json-common.c",
"json-perl.c", and "json-entry-points.c". This file contains the
"Perl" stuff, for example if we have a string, the stuff to convert
it into a Perl hash key or a Perl scalar is in this file. */
/* There are two routes through the code, the PERLING route and the
non-PERLING route. If we go via the non-PERLING route, we never
create or alter any Perl-related stuff, we just parse each byte and
possibly throw an error. This makes validation faster. */
#ifdef PERLING
/* We are creating Perl structures from the JSON. */
#define PREFIX(x) x
#define SVPTR SV *
#define SETVALUE value =
#elif defined(TOKENING)
/* We are just tokenizing the JSON. */
#define PREFIX(x) tokenize_ ## x
#define SVPTR json_token_t *
#define SETVALUE value =
#else /* not def PERLING/TOKENING */
/* Turn off everything to do with creating Perl things. */
#define PREFIX(x) valid_ ## x
#define SVPTR void
#define SETVALUE
#endif /* def PERLING */
/*
This is what INT_MAX_DIGITS is, but #defining it like this causes huge
amounts of unnecessary calculation, so this is commented out.
#define INT_MAX_DIGITS ((int) (log (INT_MAX) / log (10)) - 1)
*/
/* The maximum digits we allow an integer before throwing in the towel
and returning a Perl string type. */
#define INT_MAX_DIGITS 8
#define USEDIGIT guess = guess * 10 + (c - '0')
static INLINE SVPTR
PREFIX (number) (json_parse_t * parser)
{
/* End marker for strtod. */
char * end;
/* Start marker for strtod. */
char * start;
/* A guess for integer numbers. */
int guess;
/* The parsed character itself, the cause of our motion. */
unsigned char c;
/* If it has exp or dot in it. */
double d;
/* Negative number. */
int minus;
/* When this is called, it means that a byte indicating a number
was found. We need to re-examine that byte as a number. */
parser->end--;
start = (char *) parser->end;
#define FAILNUMBER(err) \
if (STRINGEND && \
parser->top_level_value && \
c == '\0') { \
goto exp_number_end; \
} \
parser->bad_byte = parser->end - 1; \
parser->error = json_error_ ## err; \
parser->bad_type = json_number; \
parser->bad_beginning = \
(unsigned char*) start; \
failbadinput (parser)
#define NUMBEREND \
WHITESPACE: \
case ']': \
case '}': \
case ','
#define XNUMBEREND (XCOMMA|XWHITESPACE|parser->end_expected)
guess = 0;
minus = 0;
switch (NEXTBYTE) {
case DIGIT19:
guess = c - '0';
goto leading_digit19;
case '0':
goto leading_zero;
case '-':
minus = 1;
goto leading_minus;
default:
parser->expected = XDIGIT | XMINUS;
FAILNUMBER (unexpected_character);
}
leading_digit19:
switch (NEXTBYTE) {
case DIGIT:
USEDIGIT;
goto leading_digit19;
case '.':
goto dot;
case 'e':
case 'E':
goto exp;
case NUMBEREND:
goto int_number_end;
default:
parser->expected = XDIGIT | XDOT | XEXPONENTIAL | XNUMBEREND;
if (parser->top_level_value) {
parser->expected &= ~XCOMMA;
}
FAILNUMBER (unexpected_character);
}
leading_zero:
switch (NEXTBYTE) {
case '.':
/* "0." */
goto dot;
case 'e':
case 'E':
/* "0e" */
goto exp;
case NUMBEREND:
/* "0" */
goto int_number_end;
default:
parser->expected = XDOT | XEXPONENTIAL | XNUMBEREND;
if (parser->top_level_value) {
parser->expected &= ~XCOMMA;
}
FAILNUMBER (unexpected_character);
}
leading_minus:
switch (NEXTBYTE) {
case DIGIT19:
USEDIGIT;
goto leading_digit19;
case '0':
goto leading_zero;
default:
parser->expected = XDIGIT;
FAILNUMBER (unexpected_character);
}
/* Things like "5." are not allowed so there is no NUMBEREND
here. */
dot:
switch (NEXTBYTE) {
case DIGIT:
goto dot_digits;
default:
parser->expected = XDIGIT;
FAILNUMBER (unexpected_character);
}
/* We have as much as 5.5 so we can stop. */
dot_digits:
switch (NEXTBYTE) {
case DIGIT:
goto dot_digits;
case 'e':
case 'E':
goto exp;
case NUMBEREND:
goto exp_number_end;
default:
parser->expected = XDIGIT | XNUMBEREND | XEXPONENTIAL;
if (parser->top_level_value) {
parser->expected &= ~XCOMMA;
}
FAILNUMBER (unexpected_character);
}
/* Things like "10E" are not allowed so there is no NUMBEREND
here. */
exp:
switch (NEXTBYTE) {
case '-':
case '+':
goto exp_sign;
case DIGIT:
goto exp_digits;
default:
parser->expected = XDIGIT | XMINUS | XPLUS;
FAILNUMBER (unexpected_character);
}
exp_sign:
switch (NEXTBYTE) {
case DIGIT:
goto exp_digits;
default:
parser->expected = XDIGIT;
FAILNUMBER (unexpected_character);
}
/* We have as much as "3.0e1" or similar. */
exp_digits:
switch (NEXTBYTE) {
case DIGIT:
goto exp_digits;
case NUMBEREND:
goto exp_number_end;
default:
parser->expected = XDIGIT | XNUMBEREND;
if (parser->top_level_value) {
parser->expected &= ~XCOMMA;
}
FAILNUMBER (unexpected_character);
}
exp_number_end:
parser->end--;
#ifdef PERLING
d = strtod (start, & end);
#else
strtod (start, & end);
#endif
if ((unsigned char *) end == parser->end) {
/* Success, strtod worked as planned. */
#ifdef PERLING
return newSVnv (d);
#elif defined (TOKENING)
return json_token_new (parser, (unsigned char *) start,
parser->end,
json_token_number);
#else
return;
#endif
}
else {
/* Failure, strtod rejected the number. */
goto string_number_end;
}
int_number_end:
parser->end--;
if (parser->end - (unsigned char *) start < INT_MAX_DIGITS + minus) {
if (minus) {
guess = -guess;
}
/*
printf ("number debug: '%.*s': %d\n",
parser->end - (unsigned char *) start, start, guess);
*/
#ifdef PERLING
return newSViv (guess);
#elif defined (TOKENING)
return json_token_new (parser, (unsigned char *) start,
parser->end - 1, json_token_number);
#else
return;
#endif
}
else {
goto string_number_end;
}
string_number_end:
/* We could not convert this number using a number conversion
routine, so we are going to convert it to a string. This might
happen with ridiculously long numbers or something. The JSON
standard doesn't explicitly disallow integers with a million
digits. */
#ifdef PERLING
return newSVpv (start, (STRLEN) ((char *) parser->end - start));
#elif defined (TOKENING)
return json_token_new (parser, (unsigned char *) start,
parser->end - 1, json_token_number);
#else
return;
#endif
}
#ifdef PERLING
/* This copies our on-stack buffer "buffer" of size "size" into the
end of a Perl SV called "string". */
#define COPYBUFFER { \
if (! string) { \
string = newSVpvn ((char *) buffer, size); \
} \
else { \
char * svbuf; \
STRLEN cur = SvCUR (string); \
if (SvLEN (string) <= cur + size) { \
SvGROW (string, cur + size); \
} \
svbuf = SvPVX (string); \
memcpy (svbuf + cur, buffer, size); \
SvCUR_set (string, cur + size); \
} \
}
/* The size of the on-stack buffer. */
#define BUFSIZE 0x1000
/* We need a safety margin when dealing with the buffer, for example
if we hit a Unicode \uabcd escape which needs to be decoded, we
need to have enough bytes to write into the buffer. */
#define MARGIN 0x10
/* Speedup hack, a special "get_string" for Perl parsing which doesn't
use parser->buffer but its own buffer on the stack. */
static INLINE SV *
perl_get_string (json_parse_t * parser, STRLEN prefixlen)
{
unsigned char * b;
unsigned char c;
unsigned char * start;
unsigned char buffer[BUFSIZE];
STRLEN size;
SV * string;
string = 0;
start = parser->end;
b = buffer;
if (prefixlen > 0) {
/* The string from parser->end to parser->end + prefixlen has
already been checked and found not to contain the end of
the string or any escapes, so we just copy the memory
straight into the buffer. This was supposed to speed things
up, but it didn't seem to. However this presumably cannot
hurt either. */
if (prefixlen > BUFSIZE - MARGIN) {
/* This is to account for the very unlikely case that the
key of the JSON object is more than BUFSIZE - MARGIN
bytes long and has an escape after more than BUFSIZE -
MARGIN bytes. */
prefixlen = BUFSIZE - MARGIN;
}
memcpy (buffer, parser->end, prefixlen);
start += prefixlen;
}
string_start:
size = b - buffer;
if (size >= BUFSIZE - MARGIN) {
/* Spot-check for an overflow. */
if (STRINGEND) {
STRINGFAIL (unexpected_end_of_input);
}
/* "string_start" is a label for a goto which is applied until
we get to the end of the string, so size keeps getting
larger and larger. Now the string being parsed has proved
to be too big for our puny BUFSIZE buffer, so we copy the
contents of the buffer into the nice Perl scalar. */
COPYBUFFER;
/* Set the point of copying bytes back to the beginning of
buffer. We don't reset the memory in buffer. */
b = buffer;
size = b - buffer;
}
NEXTBYTE;
/* "if" statements seem to compile to something marginally faster
than "switch" statements, for some reason. */
if (c < 0x20) {
ILLEGALBYTE;
}
else if (c >= 0x20 && c <= 0x80) {
/* For some reason or another, putting the following "if"
statements after the above one results in about 4% faster
code than putting them before it. */
if (c == '"') {
goto string_end;
}
if (c == '\\') {
HANDLE_ESCAPES (parser->end, start - 1);
goto string_start;
}
* b++ = c;
goto string_start;
}
else {
/* Resort to switch statements for the UTF-8 stuff. This
actually also contains statements to handle ASCII but they
will never be executed. */
switch (c) {
#define ADDBYTE * b = c; b++
#define startofutf8string start
#include "utf8-byte-one.c"
default:
/* We have to give up, this byte is too mysterious for our
weak minds. */
ILLEGALBYTE;
}
}
string_end:
if (STRINGEND) {
STRINGFAIL (unexpected_end_of_input);
}
COPYBUFFER;
return string;
/* The rest of the UTF-8 stuff goes in here. */
#include "utf8-next-byte.c"
#undef ADDBYTE
goto string_end;
}
#endif /* PERLING */
static SVPTR
PREFIX (string) (json_parse_t * parser)
{
unsigned char c;
#ifdef PERLING
SV * string;
STRLEN len;
STRLEN prefixlen;
#elif defined (TOKENING)
json_token_t * string;
int len;
#else
int len;
#endif
unsigned char * start;
start = parser->end;
len = 0;
/* First of all, we examine the string to work out how long it is
and to look for escapes. If we find them, we go to "contains_escapes"
and go back and do all the hard work of converting the escapes
into the right things. If we don't find any escapes, we just
use "start" and "len" and copy the string from inside
"input". This is a trick to increase the speed of
processing. */
string_start:
switch (NEXTBYTE) {
case '"':
goto string_end;
case '\\':
goto contains_escapes;
#define ADDBYTE len++
#include "utf8-byte-one.c"
/* Not a fall through. */
case BADBYTES:
ILLEGALBYTE;
}
/* Parsing of the string ended due to a \0 byte flipping the
"while" switch and we dropped into this section before
reaching the string's end. */
ILLEGALBYTE;
#include "utf8-next-byte.c"
#undef ADDBYTE
string_end:
#ifdef PERLING
/* Our string didn't contain any escape sequences, so we can just
make a new SV * by copying the string from "start", the old
position within the thing we're parsing to start + len. */
string = newSVpvn ((char *) start, len);
#elif defined (TOKENING)
string = json_token_new (parser, start - 1,
start + len,
json_token_string);
#endif
goto string_done;
contains_escapes:
#ifdef PERLING
/* Use "perl_get_string" which keeps the buffer on the
stack. Results in a minor speed increase. */
parser->end = start;
prefixlen = (STRLEN) (parser->end - start);
string = perl_get_string (parser, prefixlen);
#elif defined (TOKENING)
/* Don't use "len" here since it subtracts the escapes. */
parser->end = start;
len = get_string (parser);
string = json_token_new (parser,
/* Location of first quote. */
start - 1,
/* Location of last quote. */
parser->end - 1,
json_token_string);
#else
parser->end = start;
len = get_string (parser);
#endif
string_done:
#ifdef PERLING
if (parser->unicode || parser->force_unicode || parser->upgrade_utf8) {
SvUTF8_on (string);
parser->force_unicode = 0;
}
#endif
#if defined (PERLING) || defined (TOKENING)
return string;
#else
return;
#endif
}
#define FAILLITERAL(c) \
parser->expected = XIN_LITERAL; \
parser->literal_char = c; \
parser->bad_beginning = start; \
parser->error = json_error_unexpected_character; \
parser->bad_type = json_literal; \
parser->bad_byte = parser->end - 1; \
failbadinput (parser)
static SVPTR
PREFIX (literal_true) (json_parse_t * parser)
{
unsigned char * start;
start = parser->end - 1;
if (* parser->end++ == 'r') {
if (* parser->end++ == 'u') {
if (* parser->end++ == 'e') {
#ifdef PERLING
if (parser->user_true) {
return newSVsv (parser->user_true);
}
else if (parser->copy_literals) {
return newSVsv (&PL_sv_yes);
}
else {
return &PL_sv_yes;
}
#elif defined (TOKENING)
return json_token_new (parser, start, parser->end - 1,
json_token_literal);
#else
return;
#endif
}
FAILLITERAL ('e');
}
FAILLITERAL ('u');
}
FAILLITERAL ('r');
}
static SVPTR
PREFIX (literal_false) (json_parse_t * parser)
{
unsigned char * start;
start = parser->end - 1;
if (* parser->end++ == 'a') {
if (* parser->end++ == 'l') {
if (* parser->end++ == 's') {
if (* parser->end++ == 'e') {
#ifdef PERLING
if (parser->user_false) {
return newSVsv (parser->user_false);
}
else if (parser->copy_literals) {
return newSVsv (&PL_sv_no);
}
else {
return &PL_sv_no;
}
#elif defined (TOKENING)
return json_token_new (parser, start, parser->end - 1,
json_token_literal);
#else
return;
#endif
}
FAILLITERAL ('e');
}
FAILLITERAL ('s');
}
FAILLITERAL ('l');
}
FAILLITERAL ('a');
}
static SVPTR
PREFIX (literal_null) (json_parse_t * parser)
{
unsigned char * start;
start = parser->end - 1;
if (* parser->end++ == 'u') {
if (* parser->end++ == 'l') {
if (* parser->end++ == 'l') {
#ifdef PERLING
if (parser->user_null) {
return newSVsv (parser->user_null);
}
else if (parser->copy_literals) {
return newSVsv (&PL_sv_undef);
}
else {
SvREFCNT_inc (json_null);
return json_null;
}
#elif defined (TOKENING)
return json_token_new (parser, start, parser-> end - 1,
json_token_literal);
#else
return;
#endif
}
FAILLITERAL ('l');
}
FAILLITERAL ('l');
}
FAILLITERAL ('u');
}
static SVPTR PREFIX (object) (json_parse_t * parser);
/* Given one character, decide what to do next. This goes in the
switch statement in both "object ()" and "array ()". */
#define PARSE(start,expected) \
\
case WHITESPACE: \
goto start; \
\
case '"': \
SETVALUE PREFIX (string) (parser); \
break; \
\
case '-': \
case DIGIT: \
parser->end_expected = expected; \
SETVALUE PREFIX (number) (parser); \
break; \
\
case '{': \
INCDEPTH; \
SETVALUE PREFIX (object) (parser); \
break; \
\
case '[': \
INCDEPTH; \
SETVALUE PREFIX (array) (parser); \
break; \
\
case 'f': \
SETVALUE PREFIX (literal_false) (parser); \
break; \
\
case 'n': \
SETVALUE PREFIX (literal_null) (parser); \
break; \
\
case 't': \
SETVALUE PREFIX (literal_true) (parser); \
break
#define FAILARRAY(err) \
parser->bad_byte = parser->end - 1; \
parser->bad_type = json_array; \
parser->bad_beginning = start; \
parser->error = json_error_ ## err; \
failbadinput (parser)
/* We have seen "[", so now deal with the contents of an array. At the
end of this routine, "parser->end" is pointing one beyond the final
"]" of the array. */
static SVPTR
PREFIX (array) (json_parse_t * parser)
{
unsigned char c;
unsigned char * start;
#ifdef PERLING
AV * av;
SV * value = & PL_sv_undef;
#elif defined (TOKENING)
json_token_t * av;
json_token_t * prev;
json_token_t * value;
#endif
start = parser->end - 1;
#ifdef PERLING
av = newAV ();
#elif defined (TOKENING)
av = json_token_new (parser, start, 0, json_token_array);
prev = 0;
#endif
array_start:
switch (NEXTBYTE) {
PARSE (array_start, XARRAY_END);
case ']':
goto array_end;
default:
parser->expected = VALUE_START | XWHITESPACE | XARRAY_END;
FAILARRAY (unexpected_character);
}
#ifdef PERLING
av_push (av, value);
#elif defined (TOKENING)
prev = json_token_set_child (parser, av, value);
#endif
/* Accept either a comma or whitespace or the end of the array. */
array_middle:
switch (NEXTBYTE) {
case WHITESPACE:
goto array_middle;
case ',':
#ifdef TOKENING
value = json_token_new (parser, parser->end - 1,
parser->end - 1,
json_token_comma);
prev = json_token_set_next (prev, value);
#endif
goto array_next;
case ']':
/* Array with at least one element. */
goto array_end;
default:
parser->expected = XWHITESPACE | XCOMMA | XARRAY_END;
FAILARRAY (unexpected_character);
}
array_next:
switch (NEXTBYTE) {
PARSE (array_next, XARRAY_END);
default:
parser->expected = VALUE_START | XWHITESPACE;
FAILARRAY (unexpected_character);
}
#ifdef PERLING
av_push (av, value);
#elif defined (TOKENING)
prev = json_token_set_next (prev, value);
#endif
goto array_middle;
array_end:
DECDEPTH;
#ifdef PERLING
return newRV_noinc ((SV *) av);
#elif defined (TOKENING)
/* We didn't know where the end was until now. */
json_token_set_end (parser, av, parser->end - 1);
return av;
#else
return;
#endif
}
#define FAILOBJECT(err) \
parser->bad_byte = parser->end - 1; \
parser->bad_type = json_object; \
parser->bad_beginning = start; \
parser->error = json_error_ ## err; \
failbadinput (parser)
/* We have seen "{", so now deal with the contents of an object. At
the end of this routine, "parser->end" is pointing one beyond the
final "}" of the object. */
static SVPTR
PREFIX (object) (json_parse_t * parser)
{
char c;
#ifdef PERLING
HV * hv;
SV * value;
/* This is set to -1 if we want a Unicode key. See "perldoc
perlapi" under "hv_store". */
int uniflag;
#elif defined (TOKENING)
json_token_t * hv;
json_token_t * value;
json_token_t * prev;
#endif
string_t key;
/* Start of parsing. */
unsigned char * start;
start = parser->end - 1;
#ifdef PERLING
if (parser->unicode || parser->upgrade_utf8) {
/* Keys are unicode. */
uniflag = -1;
}
else {
/* Keys are not unicode. */
uniflag = 1;
}
hv = newHV ();
#elif defined (TOKENING)
hv = json_token_new (parser, start, 0, json_token_object);
prev = 0;
#endif
hash_start:
switch (NEXTBYTE) {
case WHITESPACE:
goto hash_start;
case '}':
goto hash_end;
case '"':
#ifdef TOKENING
value = json_token_new (parser, parser->end - 1, 0,
json_token_string);
/* We only come past the label "hash_start" once, so we don't
need to check that there is not already a child. */
json_token_set_child (parser, hv, value);
prev = value;
#endif
get_key_string (parser, & key);
#ifdef TOKENING
/* We didn't know where the end of the string was until now so
we wait until after "get_key_string" to set the end. */
json_token_set_end (parser, value, parser->end - 1);
#endif
goto hash_next;
default:
parser->expected = XWHITESPACE | XSTRING_START | XOBJECT_END;
FAILOBJECT (unexpected_character);
}
hash_middle:
/* We are in the middle of a hash. We have seen a key:value pair,
and now we're looking for either a comma and then another
key-value pair, or a closing curly brace and the end of the
hash. */
switch (NEXTBYTE) {
case WHITESPACE:
goto hash_middle;
case '}':
goto hash_end;
case ',':
#ifdef TOKENING
value = json_token_new (parser, parser->end - 1,
parser->end - 1,
json_token_comma);
prev = json_token_set_next (prev, value);
#endif
goto hash_key;
default:
parser->expected = XWHITESPACE | XCOMMA | XOBJECT_END;
FAILOBJECT (unexpected_character);
}
hash_key:
/* We're looking for a key in the hash, which is a string starting
with a double quotation mark. */
switch (NEXTBYTE) {
case WHITESPACE:
goto hash_key;
case '"':
#ifdef TOKENING
value = json_token_new (parser, parser->end - 1, 0,
json_token_string);
prev = json_token_set_next (prev, value);
#endif
get_key_string (parser, & key);
#ifdef TOKENING
/* We didn't know where the end of the string was until now so
we wait until after "get_key_string" to set the end. */
json_token_set_end (parser, value, parser->end - 1);
#endif
goto hash_next;
default:
parser->expected = XWHITESPACE | XSTRING_START;
FAILOBJECT (unexpected_character);
}
hash_next:
/* We've seen a key, now we're looking for a colon. */
switch (NEXTBYTE) {
case WHITESPACE:
goto hash_next;
case ':':
#ifdef TOKENING
value = json_token_new (parser, parser->end - 1,
parser->end - 1,
json_token_colon);
prev = json_token_set_next (prev, value);
#endif
goto hash_value;
default:
parser->expected = XWHITESPACE | XVALUE_SEPARATOR;
FAILOBJECT (unexpected_character);
}
hash_value:
/* We've seen a colon, now we're looking for a value, which can be
anything at all, including another hash. Most of the cases are
dealt with in the PARSE macro. */
switch (NEXTBYTE) {
PARSE (hash_value, XOBJECT_END);
default:
parser->expected = XWHITESPACE | VALUE_START;
FAILOBJECT (unexpected_character);
}
if (key.contains_escapes) {
/* The key had something like "\n" in it, so we can't just
copy the value but have to process it to remove the
escapes. */
#ifdef PERLING
int klen;
klen = resolve_string (parser, & key);
key.start = parser->buffer;
key.length = klen;
#else
resolve_string (parser, & key);
#endif
}
#ifdef PERLING
if (parser->detect_collisions) {
/* Look in hv for an existing key with our values. */
SV ** sv_ptr;
sv_ptr = hv_fetch (hv, (char *) key.start, key.length * uniflag, 0);
if (sv_ptr) {
parser->bad_byte = key.start;
parser->bad_length = key.length;
parser->bad_type = json_object;
parser->bad_beginning = start;
parser->error = json_error_name_is_not_unique;
failbadinput (parser);
}
}
(void) hv_store (hv, (char *) key.start, key.length * uniflag, value, 0);
#endif
#if defined(TOKENING)
prev = json_token_set_next (prev, value);
#endif
goto hash_middle;
hash_end:
DECDEPTH;
#ifdef PERLING
return newRV_noinc ((SV *) hv);
#elif defined (TOKENING)
json_token_set_end (parser, hv, parser->end - 1);
return hv;
#else
return;
#endif
}
#undef PREFIX
#undef SVPTR
#undef SETVALUE
#ifdef PERLING
/* Set and delete user-defined literals. */
static void
json_parse_delete_true (json_parse_t * parser)
{
if (parser->user_true) {
SvREFCNT_dec (parser->user_true);
parser->user_true = 0;
}
}
static void
json_parse_set_true (json_parse_t * parser, SV * user_true)
{
json_parse_delete_true (parser);
if (! SvTRUE (user_true) && ! parser->no_warn_literals) {
warn ("User-defined value for JSON true evaluates as false");
}
if (parser->copy_literals && ! parser->no_warn_literals) {
warn ("User-defined value overrules copy_literals");
}
parser->user_true = user_true;
SvREFCNT_inc (user_true);
}
static void
json_parse_delete_false (json_parse_t * parser)
{
if (parser->user_false) {
SvREFCNT_dec (parser->user_false);
parser->user_false = 0;
}
}
static void
json_parse_set_false (json_parse_t * parser, SV * user_false)
{
json_parse_delete_false (parser);
if (SvTRUE (user_false) && ! parser->no_warn_literals) {
warn ("User-defined value for JSON false evaluates as true");
}
if (parser->copy_literals && ! parser->no_warn_literals) {
warn ("User-defined value overrules copy_literals");
}
parser->user_false = user_false;
SvREFCNT_inc (user_false);
}
static void
json_parse_delete_null (json_parse_t * parser)
{
if (parser->user_null) {
SvREFCNT_dec (parser->user_null);
parser->user_null = 0;
}
}
static void
json_parse_set_null (json_parse_t * parser, SV * user_null)
{
if (parser->copy_literals && ! parser->no_warn_literals) {
warn ("User-defined value overrules copy_literals");
}
json_parse_delete_null (parser);
parser->user_null = user_null;
SvREFCNT_inc (user_null);
}
static void
json_parse_free (json_parse_t * parser)
{
/* We can get here with depth > 0 if the parser fails and then the
error is caught. */
if (parser->depth < 0) {
warn ("Parser depth underflow %d", parser->depth);
}
json_parse_delete_true (parser);
json_parse_delete_false (parser);
json_parse_delete_null (parser);
Safefree (parser);
}
static void
json_parse_copy_literals (json_parse_t * parser, SV * onoff)
{
if (! parser->no_warn_literals &&
(parser->user_true || parser->user_false || parser->user_null)) {
warn ("User-defined value overrules copy_literals");
}
parser->copy_literals = SvTRUE (onoff) ? 1 : 0;
}
#endif /* def PERLING */
|