… | |
… | |
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 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
28 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
29 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
30 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << 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 | } |
… | |
… | |
362 | // actually, this is mostly due to the stupid so-called |
372 | // actually, this is mostly due to the stupid so-called |
363 | // security workaround added somewhere in 5.8.x. |
373 | // security workaround added somewhere in 5.8.x. |
364 | // that randomises hash orderings |
374 | // that randomises hash orderings |
365 | if (enc->flags & F_CANONICAL) |
375 | if (enc->flags & F_CANONICAL) |
366 | { |
376 | { |
367 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
368 | int fast = 1; |
377 | int fast = 1; |
|
|
378 | HE *he; |
|
|
379 | #if WIN32 |
|
|
380 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
381 | #else |
|
|
382 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
383 | #endif |
369 | |
384 | |
370 | i = 0; |
385 | i = 0; |
371 | while ((he = hv_iternext (hv))) |
386 | while ((he = hv_iternext (hv))) |
372 | { |
387 | { |
373 | hes [i++] = he; |
388 | hes [i++] = he; |
… | |
… | |
408 | |
423 | |
409 | encode_nl (enc); |
424 | encode_nl (enc); |
410 | } |
425 | } |
411 | else |
426 | else |
412 | { |
427 | { |
413 | SV *sv; |
|
|
414 | HE *he = hv_iternext (hv); |
428 | HE *he = hv_iternext (hv); |
415 | |
429 | |
416 | for (;;) |
430 | for (;;) |
417 | { |
431 | { |
418 | encode_indent (enc); |
432 | encode_indent (enc); |
… | |
… | |
433 | |
447 | |
434 | // encode objects, arrays and special \0=false and \1=true values. |
448 | // encode objects, arrays and special \0=false and \1=true values. |
435 | static void |
449 | static void |
436 | encode_rv (enc_t *enc, SV *sv) |
450 | encode_rv (enc_t *enc, SV *sv) |
437 | { |
451 | { |
|
|
452 | svtype svt; |
|
|
453 | |
438 | SvGETMAGIC (sv); |
454 | SvGETMAGIC (sv); |
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
455 | svt = SvTYPE (sv); |
441 | |
456 | |
442 | if (svt == SVt_PVHV) |
457 | if (svt == SVt_PVHV) |
443 | encode_hv (enc, (HV *)sv); |
458 | encode_hv (enc, (HV *)sv); |
444 | else if (svt == SVt_PVAV) |
459 | else if (svt == SVt_PVAV) |
445 | encode_av (enc, (AV *)sv); |
460 | encode_av (enc, (AV *)sv); |
… | |
… | |
495 | } |
510 | } |
496 | |
511 | |
497 | static SV * |
512 | static SV * |
498 | encode_json (SV *scalar, U32 flags) |
513 | encode_json (SV *scalar, U32 flags) |
499 | { |
514 | { |
|
|
515 | enc_t enc; |
|
|
516 | |
500 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
517 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
501 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
518 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
502 | |
519 | |
503 | enc_t enc; |
|
|
504 | enc.flags = flags; |
520 | enc.flags = flags; |
505 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
521 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.cur = SvPVX (enc.sv); |
522 | enc.cur = SvPVX (enc.sv); |
507 | enc.end = SvEND (enc.sv); |
523 | enc.end = SvEND (enc.sv); |
508 | enc.indent = 0; |
524 | enc.indent = 0; |
509 | enc.maxdepth = DEC_DEPTH (flags); |
525 | enc.maxdepth = DEC_DEPTH (flags); |
510 | |
526 | |
511 | SvPOK_only (enc.sv); |
527 | SvPOK_only (enc.sv); |
512 | encode_sv (&enc, scalar); |
528 | encode_sv (&enc, scalar); |
513 | |
529 | |
|
|
530 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
531 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
532 | |
514 | if (!(flags & (F_ASCII | F_UTF8))) |
533 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
515 | SvUTF8_on (enc.sv); |
534 | SvUTF8_on (enc.sv); |
516 | |
|
|
517 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
518 | |
535 | |
519 | if (enc.flags & F_SHRINK) |
536 | if (enc.flags & F_SHRINK) |
520 | shrink (enc.sv); |
537 | shrink (enc.sv); |
521 | |
538 | |
522 | return enc.sv; |
539 | return enc.sv; |
… | |
… | |
594 | SV *sv = 0; |
611 | SV *sv = 0; |
595 | int utf8 = 0; |
612 | int utf8 = 0; |
596 | |
613 | |
597 | do |
614 | do |
598 | { |
615 | { |
599 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
616 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
600 | char *cur = buf; |
617 | char *cur = buf; |
601 | |
618 | |
602 | do |
619 | do |
603 | { |
620 | { |
604 | unsigned char ch = *(unsigned char *)dec->cur++; |
621 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
670 | } |
687 | } |
671 | else if (ch >= 0x20 && ch <= 0x7f) |
688 | else if (ch >= 0x20 && ch <= 0x7f) |
672 | *cur++ = ch; |
689 | *cur++ = ch; |
673 | else if (ch >= 0x80) |
690 | else if (ch >= 0x80) |
674 | { |
691 | { |
|
|
692 | STRLEN clen; |
|
|
693 | UV uch; |
|
|
694 | |
675 | --dec->cur; |
695 | --dec->cur; |
676 | |
696 | |
677 | STRLEN clen; |
|
|
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
697 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
679 | if (clen == (STRLEN)-1) |
698 | if (clen == (STRLEN)-1) |
680 | ERR ("malformed UTF-8 character in JSON string"); |
699 | ERR ("malformed UTF-8 character in JSON string"); |
681 | |
700 | |
682 | do |
701 | do |
683 | *cur++ = *dec->cur++; |
702 | *cur++ = *dec->cur++; |
… | |
… | |
688 | else |
707 | else |
689 | { |
708 | { |
690 | --dec->cur; |
709 | --dec->cur; |
691 | |
710 | |
692 | if (!ch) |
711 | if (!ch) |
693 | ERR ("unexpected end of string while parsing json string"); |
712 | ERR ("unexpected end of string while parsing JSON string"); |
694 | else |
713 | else |
695 | ERR ("invalid character encountered while parsing json string"); |
714 | ERR ("invalid character encountered while parsing JSON string"); |
696 | } |
715 | } |
697 | } |
716 | } |
698 | while (cur < buf + SHORT_STRING_LEN); |
717 | while (cur < buf + SHORT_STRING_LEN); |
699 | |
718 | |
|
|
719 | { |
700 | STRLEN len = cur - buf; |
720 | STRLEN len = cur - buf; |
701 | |
721 | |
702 | if (sv) |
722 | if (sv) |
703 | { |
723 | { |
704 | SvGROW (sv, SvCUR (sv) + len + 1); |
724 | SvGROW (sv, SvCUR (sv) + len + 1); |
705 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
725 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
706 | SvCUR_set (sv, SvCUR (sv) + len); |
726 | SvCUR_set (sv, SvCUR (sv) + len); |
707 | } |
727 | } |
708 | else |
728 | else |
709 | sv = newSVpvn (buf, len); |
729 | sv = newSVpvn (buf, len); |
|
|
730 | } |
710 | } |
731 | } |
711 | while (*dec->cur != '"'); |
732 | while (*dec->cur != '"'); |
712 | |
733 | |
713 | ++dec->cur; |
734 | ++dec->cur; |
714 | |
735 | |
… | |
… | |
958 | ERR ("'null' expected"); |
979 | ERR ("'null' expected"); |
959 | |
980 | |
960 | break; |
981 | break; |
961 | |
982 | |
962 | default: |
983 | default: |
963 | ERR ("malformed json string, neither array, object, number, string or atom"); |
984 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
964 | break; |
985 | break; |
965 | } |
986 | } |
966 | |
987 | |
967 | fail: |
988 | fail: |
968 | return 0; |
989 | return 0; |
969 | } |
990 | } |
970 | |
991 | |
971 | static SV * |
992 | static SV * |
972 | decode_json (SV *string, U32 flags) |
993 | decode_json (SV *string, U32 flags, UV *offset_return) |
973 | { |
994 | { |
|
|
995 | dec_t dec; |
|
|
996 | UV offset; |
974 | SV *sv; |
997 | SV *sv; |
975 | |
998 | |
|
|
999 | SvGETMAGIC (string); |
976 | SvUPGRADE (string, SVt_PV); |
1000 | SvUPGRADE (string, SVt_PV); |
977 | |
1001 | |
978 | if (flags & F_UTF8) |
1002 | if (flags & F_UTF8) |
979 | sv_utf8_downgrade (string, 0); |
1003 | sv_utf8_downgrade (string, 0); |
980 | else |
1004 | else |
981 | sv_utf8_upgrade (string); |
1005 | sv_utf8_upgrade (string); |
982 | |
1006 | |
983 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1007 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
984 | |
1008 | |
985 | dec_t dec; |
|
|
986 | dec.flags = flags; |
1009 | dec.flags = flags; |
987 | dec.cur = SvPVX (string); |
1010 | dec.cur = SvPVX (string); |
988 | dec.end = SvEND (string); |
1011 | dec.end = SvEND (string); |
989 | dec.err = 0; |
1012 | dec.err = 0; |
990 | dec.depth = 0; |
1013 | dec.depth = 0; |
991 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1014 | dec.maxdepth = DEC_DEPTH (dec.flags); |
992 | |
1015 | |
993 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1016 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
994 | sv = decode_sv (&dec); |
1017 | sv = decode_sv (&dec); |
995 | |
1018 | |
|
|
1019 | if (!(offset_return || !sv)) |
|
|
1020 | { |
|
|
1021 | // check for trailing garbage |
|
|
1022 | decode_ws (&dec); |
|
|
1023 | |
|
|
1024 | if (*dec.cur) |
|
|
1025 | { |
|
|
1026 | dec.err = "garbage after JSON object"; |
|
|
1027 | SvREFCNT_dec (sv); |
|
|
1028 | sv = 0; |
|
|
1029 | } |
|
|
1030 | } |
|
|
1031 | |
|
|
1032 | if (offset_return || !sv) |
|
|
1033 | { |
|
|
1034 | offset = dec.flags & F_UTF8 |
|
|
1035 | ? dec.cur - SvPVX (string) |
|
|
1036 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1037 | |
|
|
1038 | if (offset_return) |
|
|
1039 | *offset_return = offset; |
|
|
1040 | } |
|
|
1041 | |
996 | if (!sv) |
1042 | if (!sv) |
997 | { |
1043 | { |
998 | IV offset = dec.flags & F_UTF8 |
|
|
999 | ? dec.cur - SvPVX (string) |
|
|
1000 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1001 | SV *uni = sv_newmortal (); |
1044 | SV *uni = sv_newmortal (); |
1002 | |
1045 | |
1003 | // horrible hack to silence warning inside pv_uni_display |
1046 | // horrible hack to silence warning inside pv_uni_display |
1004 | COP cop = *PL_curcop; |
1047 | COP cop = *PL_curcop; |
1005 | cop.cop_warnings = pWARN_NONE; |
1048 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1053 | RETVAL |
1096 | RETVAL |
1054 | |
1097 | |
1055 | SV *ascii (SV *self, int enable = 1) |
1098 | SV *ascii (SV *self, int enable = 1) |
1056 | ALIAS: |
1099 | ALIAS: |
1057 | ascii = F_ASCII |
1100 | ascii = F_ASCII |
|
|
1101 | latin1 = F_LATIN1 |
1058 | utf8 = F_UTF8 |
1102 | utf8 = F_UTF8 |
1059 | indent = F_INDENT |
1103 | indent = F_INDENT |
1060 | canonical = F_CANONICAL |
1104 | canonical = F_CANONICAL |
1061 | space_before = F_SPACE_BEFORE |
1105 | space_before = F_SPACE_BEFORE |
1062 | space_after = F_SPACE_AFTER |
1106 | space_after = F_SPACE_AFTER |
… | |
… | |
1074 | RETVAL = newSVsv (self); |
1118 | RETVAL = newSVsv (self); |
1075 | } |
1119 | } |
1076 | OUTPUT: |
1120 | OUTPUT: |
1077 | RETVAL |
1121 | RETVAL |
1078 | |
1122 | |
1079 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1123 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1080 | CODE: |
1124 | CODE: |
1081 | { |
1125 | { |
1082 | UV *uv = SvJSON (self); |
1126 | UV *uv = SvJSON (self); |
1083 | UV log2 = 0; |
1127 | UV log2 = 0; |
1084 | |
1128 | |
… | |
… | |
1098 | PPCODE: |
1142 | PPCODE: |
1099 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1143 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1100 | |
1144 | |
1101 | void decode (SV *self, SV *jsonstr) |
1145 | void decode (SV *self, SV *jsonstr) |
1102 | PPCODE: |
1146 | PPCODE: |
1103 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1147 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1148 | |
|
|
1149 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1150 | PPCODE: |
|
|
1151 | { |
|
|
1152 | UV offset; |
|
|
1153 | EXTEND (SP, 2); |
|
|
1154 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1155 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1156 | } |
1104 | |
1157 | |
1105 | PROTOTYPES: ENABLE |
1158 | PROTOTYPES: ENABLE |
1106 | |
1159 | |
1107 | void to_json (SV *scalar) |
1160 | void to_json (SV *scalar) |
1108 | ALIAS: |
1161 | ALIAS: |
… | |
… | |
1112 | |
1165 | |
1113 | void from_json (SV *jsonstr) |
1166 | void from_json (SV *jsonstr) |
1114 | ALIAS: |
1167 | ALIAS: |
1115 | jsonToObj = 0 |
1168 | jsonToObj = 0 |
1116 | PPCODE: |
1169 | PPCODE: |
1117 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1170 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1118 | |
1171 | |