… | |
… | |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
8 | #include "stdio.h" |
8 | #include "stdio.h" |
9 | |
9 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
12 | #endif |
18 | #endif |
13 | |
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
… | |
… | |
49 | |
55 | |
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
52 | |
58 | |
53 | static HV *json_stash; // JSON::XS:: |
59 | static HV *json_stash; // JSON::XS:: |
|
|
60 | static SV *json_true, *json_false; |
54 | |
61 | |
55 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
56 | // utility functions |
63 | // utility functions |
57 | |
64 | |
58 | static UV * |
65 | static UV * |
… | |
… | |
178 | STRLEN clen; |
185 | STRLEN clen; |
179 | UV uch; |
186 | UV uch; |
180 | |
187 | |
181 | if (is_utf8) |
188 | if (is_utf8) |
182 | { |
189 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
190 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
191 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
192 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
193 | } |
188 | else |
194 | else |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
503 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
504 | encode_ch (enc, '"'); |
499 | } |
505 | } |
500 | else if (SvNOKp (sv)) |
506 | else if (SvNOKp (sv)) |
501 | { |
507 | { |
|
|
508 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
509 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
510 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
511 | enc->cur += strlen (enc->cur); |
505 | } |
512 | } |
506 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
507 | { |
514 | { |
508 | // we assume we can always read an IV as a UV |
515 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
516 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
517 | { |
|
|
518 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, sizeof (UV) * 3); |
519 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
520 | enc->cur += |
513 | SvIsUV(sv) |
521 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
522 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
523 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
… | |
… | |
518 | { |
526 | { |
519 | // optimise the "small number case" |
527 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
528 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
529 | I32 i = SvIV (sv); |
522 | U32 u; |
530 | U32 u; |
|
|
531 | char digit, nz = 0; |
523 | |
532 | |
524 | need (enc, 6); |
533 | need (enc, 6); |
525 | |
534 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
535 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
536 | u = i < 0 ? -i : i; |
528 | |
537 | |
529 | // convert to 4.28 fixed-point representation |
538 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
539 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
540 | |
532 | char digit, nz = 0; |
541 | // now output digit by digit, each time masking out the integer part |
533 | |
542 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
543 | // resulting in a net multiplication by 10. |
|
|
544 | // we always write the digit to memory but conditionally increment |
|
|
545 | // the pointer, to ease the usage of conditional move instructions. |
534 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
546 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
535 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
547 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
536 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
548 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
537 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
549 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
538 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; |
550 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
551 | } |
540 | } |
552 | } |
541 | else if (SvROK (sv)) |
553 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
554 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
555 | else if (!SvOK (sv)) |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
637 | decode_4hex (dec_t *dec) |
626 | { |
638 | { |
627 | signed char d1, d2, d3, d4; |
639 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
640 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
641 | |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
642 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
643 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
644 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
645 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
634 | |
646 | |
635 | dec->cur += 4; |
647 | dec->cur += 4; |
636 | |
648 | |
637 | return ((UV)d1) << 12 |
649 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
650 | | ((UV)d2) << 8 |
… | |
… | |
861 | { |
873 | { |
862 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
874 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
863 | if (*start == '-') |
875 | if (*start == '-') |
864 | switch (dec->cur - start) |
876 | switch (dec->cur - start) |
865 | { |
877 | { |
866 | case 2: return newSViv (-( start [1] - '0' )); |
878 | case 2: return newSViv (-( start [1] - '0' * 1)); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
879 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
868 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
880 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
869 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
881 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
870 | } |
882 | } |
871 | else |
883 | else |
872 | switch (dec->cur - start) |
884 | switch (dec->cur - start) |
873 | { |
885 | { |
874 | case 1: return newSViv ( start [0] - '0' ); |
886 | case 1: return newSViv ( start [0] - '0' * 1); |
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
887 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
876 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
888 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
877 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
889 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
878 | } |
890 | } |
879 | |
891 | |
… | |
… | |
886 | if (uv < (UV)IV_MIN) |
898 | if (uv < (UV)IV_MIN) |
887 | return newSViv (-(IV)uv); |
899 | return newSViv (-(IV)uv); |
888 | } |
900 | } |
889 | else |
901 | else |
890 | return newSVuv (uv); |
902 | return newSVuv (uv); |
|
|
903 | |
|
|
904 | // here would likely be the place for bigint support |
891 | } |
905 | } |
892 | } |
906 | } |
893 | |
907 | |
|
|
908 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
894 | return newSVnv (Atof (start)); |
909 | return newSVnv (Atof (start)); |
895 | |
910 | |
896 | fail: |
911 | fail: |
897 | return 0; |
912 | return 0; |
898 | } |
913 | } |
… | |
… | |
999 | |
1014 | |
1000 | static SV * |
1015 | static SV * |
1001 | decode_sv (dec_t *dec) |
1016 | decode_sv (dec_t *dec) |
1002 | { |
1017 | { |
1003 | decode_ws (dec); |
1018 | decode_ws (dec); |
|
|
1019 | |
|
|
1020 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1021 | // to parse anything. |
1004 | switch (*dec->cur) |
1022 | switch (*dec->cur) |
1005 | { |
1023 | { |
1006 | case '"': ++dec->cur; return decode_str (dec); |
1024 | case '"': ++dec->cur; return decode_str (dec); |
1007 | case '[': ++dec->cur; return decode_av (dec); |
1025 | case '[': ++dec->cur; return decode_av (dec); |
1008 | case '{': ++dec->cur; return decode_hv (dec); |
1026 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1014 | |
1032 | |
1015 | case 't': |
1033 | case 't': |
1016 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1034 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1017 | { |
1035 | { |
1018 | dec->cur += 4; |
1036 | dec->cur += 4; |
1019 | return newSViv (1); |
1037 | return SvREFCNT_inc (json_true); |
1020 | } |
1038 | } |
1021 | else |
1039 | else |
1022 | ERR ("'true' expected"); |
1040 | ERR ("'true' expected"); |
1023 | |
1041 | |
1024 | break; |
1042 | break; |
1025 | |
1043 | |
1026 | case 'f': |
1044 | case 'f': |
1027 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1045 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1028 | { |
1046 | { |
1029 | dec->cur += 5; |
1047 | dec->cur += 5; |
1030 | return newSViv (0); |
1048 | return SvREFCNT_inc (json_false); |
1031 | } |
1049 | } |
1032 | else |
1050 | else |
1033 | ERR ("'false' expected"); |
1051 | ERR ("'false' expected"); |
1034 | |
1052 | |
1035 | break; |
1053 | break; |
… | |
… | |
1138 | |
1156 | |
1139 | BOOT: |
1157 | BOOT: |
1140 | { |
1158 | { |
1141 | int i; |
1159 | int i; |
1142 | |
1160 | |
1143 | memset (decode_hexdigit, 0xff, 256); |
|
|
1144 | |
|
|
1145 | for (i = 0; i < 256; ++i) |
1161 | for (i = 0; i < 256; ++i) |
1146 | decode_hexdigit [i] = |
1162 | decode_hexdigit [i] = |
1147 | i >= '0' && i <= '9' ? i - '0' |
1163 | i >= '0' && i <= '9' ? i - '0' |
1148 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1164 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1149 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1165 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1150 | : -1; |
1166 | : -1; |
1151 | |
1167 | |
1152 | json_stash = gv_stashpv ("JSON::XS", 1); |
1168 | json_stash = gv_stashpv ("JSON::XS", 1); |
|
|
1169 | |
|
|
1170 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1171 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1153 | } |
1172 | } |
1154 | |
1173 | |
1155 | PROTOTYPES: DISABLE |
1174 | PROTOTYPES: DISABLE |
1156 | |
1175 | |
1157 | SV *new (char *dummy) |
1176 | SV *new (char *dummy) |