… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
17 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define F_MAXDEPTH 0xf8000000UL |
18 | #define S_MAXDEPTH 27 |
24 | #define S_MAXDEPTH 27 |
19 | |
25 | |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 | |
27 | |
… | |
… | |
26 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
33 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
36 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 | |
38 | |
34 | #define SB do { |
39 | #define SB do { |
35 | #define SE } while (0) |
40 | #define SE } while (0) |
36 | |
41 | |
… | |
… | |
104 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
105 | { |
110 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
115 | } |
111 | } |
116 | } |
112 | |
117 | |
113 | static void |
118 | static void |
114 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
176 | } |
181 | } |
177 | |
182 | |
178 | if (uch > 0x10FFFFUL) |
183 | if (uch > 0x10FFFFUL) |
179 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
180 | |
185 | |
181 | if (uch < 0x80 || enc->flags & F_ASCII) |
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
182 | { |
187 | { |
183 | if (uch > 0xFFFFUL) |
188 | if (uch > 0xFFFFUL) |
184 | { |
189 | { |
185 | need (enc, len += 11); |
190 | need (enc, len += 11); |
186 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
200 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
201 | } |
206 | } |
202 | |
207 | |
203 | str += clen; |
208 | str += clen; |
204 | } |
209 | } |
|
|
210 | else if (enc->flags & F_LATIN1) |
|
|
211 | { |
|
|
212 | *enc->cur++ = uch; |
|
|
213 | str += clen; |
|
|
214 | } |
205 | else if (is_utf8) |
215 | else if (is_utf8) |
206 | { |
216 | { |
207 | need (enc, len += clen); |
217 | need (enc, len += clen); |
208 | do |
218 | do |
209 | { |
219 | { |
… | |
… | |
211 | } |
221 | } |
212 | while (--clen); |
222 | while (--clen); |
213 | } |
223 | } |
214 | else |
224 | else |
215 | { |
225 | { |
216 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
226 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
217 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
227 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
218 | ++str; |
228 | ++str; |
219 | } |
229 | } |
220 | } |
230 | } |
221 | } |
231 | } |
… | |
… | |
510 | enc.maxdepth = DEC_DEPTH (flags); |
520 | enc.maxdepth = DEC_DEPTH (flags); |
511 | |
521 | |
512 | SvPOK_only (enc.sv); |
522 | SvPOK_only (enc.sv); |
513 | encode_sv (&enc, scalar); |
523 | encode_sv (&enc, scalar); |
514 | |
524 | |
|
|
525 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
526 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
527 | |
515 | if (!(flags & (F_ASCII | F_UTF8))) |
528 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
516 | SvUTF8_on (enc.sv); |
529 | SvUTF8_on (enc.sv); |
517 | |
|
|
518 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
519 | |
530 | |
520 | if (enc.flags & F_SHRINK) |
531 | if (enc.flags & F_SHRINK) |
521 | shrink (enc.sv); |
532 | shrink (enc.sv); |
522 | |
533 | |
523 | return enc.sv; |
534 | return enc.sv; |
… | |
… | |
595 | SV *sv = 0; |
606 | SV *sv = 0; |
596 | int utf8 = 0; |
607 | int utf8 = 0; |
597 | |
608 | |
598 | do |
609 | do |
599 | { |
610 | { |
600 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
601 | char *cur = buf; |
612 | char *cur = buf; |
602 | |
613 | |
603 | do |
614 | do |
604 | { |
615 | { |
605 | unsigned char ch = *(unsigned char *)dec->cur++; |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
972 | fail: |
983 | fail: |
973 | return 0; |
984 | return 0; |
974 | } |
985 | } |
975 | |
986 | |
976 | static SV * |
987 | static SV * |
977 | decode_json (SV *string, U32 flags) |
988 | decode_json (SV *string, U32 flags, UV *offset_return) |
978 | { |
989 | { |
979 | dec_t dec; |
990 | dec_t dec; |
|
|
991 | UV offset; |
980 | SV *sv; |
992 | SV *sv; |
981 | |
993 | |
|
|
994 | SvGETMAGIC (string); |
982 | SvUPGRADE (string, SVt_PV); |
995 | SvUPGRADE (string, SVt_PV); |
983 | |
996 | |
984 | if (flags & F_UTF8) |
997 | if (flags & F_UTF8) |
985 | sv_utf8_downgrade (string, 0); |
998 | sv_utf8_downgrade (string, 0); |
986 | else |
999 | else |
… | |
… | |
993 | dec.end = SvEND (string); |
1006 | dec.end = SvEND (string); |
994 | dec.err = 0; |
1007 | dec.err = 0; |
995 | dec.depth = 0; |
1008 | dec.depth = 0; |
996 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1009 | dec.maxdepth = DEC_DEPTH (dec.flags); |
997 | |
1010 | |
998 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1011 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
999 | sv = decode_sv (&dec); |
1012 | sv = decode_sv (&dec); |
1000 | |
1013 | |
|
|
1014 | if (!(offset_return || !sv)) |
|
|
1015 | { |
|
|
1016 | // check for trailing garbage |
|
|
1017 | decode_ws (&dec); |
|
|
1018 | |
|
|
1019 | if (*dec.cur) |
|
|
1020 | { |
|
|
1021 | dec.err = "garbage after JSON object"; |
|
|
1022 | SvREFCNT_dec (sv); |
|
|
1023 | sv = 0; |
|
|
1024 | } |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | if (offset_return || !sv) |
|
|
1028 | { |
|
|
1029 | offset = dec.flags & F_UTF8 |
|
|
1030 | ? dec.cur - SvPVX (string) |
|
|
1031 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1032 | |
|
|
1033 | if (offset_return) |
|
|
1034 | *offset_return = offset; |
|
|
1035 | } |
|
|
1036 | |
1001 | if (!sv) |
1037 | if (!sv) |
1002 | { |
1038 | { |
1003 | IV offset = dec.flags & F_UTF8 |
|
|
1004 | ? dec.cur - SvPVX (string) |
|
|
1005 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1006 | SV *uni = sv_newmortal (); |
1039 | SV *uni = sv_newmortal (); |
1007 | |
1040 | |
1008 | // horrible hack to silence warning inside pv_uni_display |
1041 | // horrible hack to silence warning inside pv_uni_display |
1009 | COP cop = *PL_curcop; |
1042 | COP cop = *PL_curcop; |
1010 | cop.cop_warnings = pWARN_NONE; |
1043 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1058 | RETVAL |
1091 | RETVAL |
1059 | |
1092 | |
1060 | SV *ascii (SV *self, int enable = 1) |
1093 | SV *ascii (SV *self, int enable = 1) |
1061 | ALIAS: |
1094 | ALIAS: |
1062 | ascii = F_ASCII |
1095 | ascii = F_ASCII |
|
|
1096 | latin1 = F_LATIN1 |
1063 | utf8 = F_UTF8 |
1097 | utf8 = F_UTF8 |
1064 | indent = F_INDENT |
1098 | indent = F_INDENT |
1065 | canonical = F_CANONICAL |
1099 | canonical = F_CANONICAL |
1066 | space_before = F_SPACE_BEFORE |
1100 | space_before = F_SPACE_BEFORE |
1067 | space_after = F_SPACE_AFTER |
1101 | space_after = F_SPACE_AFTER |
… | |
… | |
1103 | PPCODE: |
1137 | PPCODE: |
1104 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1138 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1105 | |
1139 | |
1106 | void decode (SV *self, SV *jsonstr) |
1140 | void decode (SV *self, SV *jsonstr) |
1107 | PPCODE: |
1141 | PPCODE: |
1108 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1142 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1143 | |
|
|
1144 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1145 | PPCODE: |
|
|
1146 | { |
|
|
1147 | UV offset; |
|
|
1148 | EXTEND (SP, 2); |
|
|
1149 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1150 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1151 | } |
1109 | |
1152 | |
1110 | PROTOTYPES: ENABLE |
1153 | PROTOTYPES: ENABLE |
1111 | |
1154 | |
1112 | void to_json (SV *scalar) |
1155 | void to_json (SV *scalar) |
1113 | ALIAS: |
1156 | ALIAS: |
… | |
… | |
1117 | |
1160 | |
1118 | void from_json (SV *jsonstr) |
1161 | void from_json (SV *jsonstr) |
1119 | ALIAS: |
1162 | ALIAS: |
1120 | jsonToObj = 0 |
1163 | jsonToObj = 0 |
1121 | PPCODE: |
1164 | PPCODE: |
1122 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1165 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1123 | |
1166 | |