… | |
… | |
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 | |
8 | |
9 | #define F_ASCII 0x00000001 |
9 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100UL |
|
|
17 | #define F_MAXDEPTH 0xf8000000UL |
|
|
18 | #define S_MAXDEPTH 27 |
|
|
19 | |
|
|
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
21 | |
|
|
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
24 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
20 | |
27 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
29 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
30 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
25 | #define SHORT_STRING_LEN 256 // special-case strings of up to this size |
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
26 | |
33 | |
27 | #define SB do { |
34 | #define SB do { |
28 | #define SE } while (0) |
35 | #define SE } while (0) |
29 | |
36 | |
30 | static HV *json_stash; // JSON::XS:: |
37 | static HV *json_stash; // JSON::XS:: |
… | |
… | |
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
60 | SvPV_renew (sv, SvCUR (sv) + 1); |
54 | #endif |
61 | #endif |
55 | } |
62 | } |
56 | } |
63 | } |
57 | |
64 | |
|
|
65 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
66 | // case of an error. |
|
|
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
68 | // but use the very good perl function to parse anything else. |
|
|
69 | // note that we never call this function for a ascii codepoints |
|
|
70 | static UV |
|
|
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
72 | { |
|
|
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
76 | { |
|
|
77 | *clen = 2; |
|
|
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
79 | } |
|
|
80 | else |
|
|
81 | return (UV)-1; |
|
|
82 | } |
|
|
83 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
84 | ///////////////////////////////////////////////////////////////////////////// |
59 | // encoder |
85 | // encoder |
60 | |
86 | |
61 | // structure used for encoding JSON |
87 | // structure used for encoding JSON |
62 | typedef struct |
88 | typedef struct |
63 | { |
89 | { |
64 | char *cur; // SvPVX (sv) + current output position |
90 | char *cur; // SvPVX (sv) + current output position |
65 | char *end; // SvEND (sv) |
91 | char *end; // SvEND (sv) |
66 | SV *sv; // result scalar |
92 | SV *sv; // result scalar |
67 | UV flags; // F_* |
93 | U32 flags; // F_* |
68 | int indent; // indentation level |
94 | U32 indent; // indentation level |
69 | int max_depth; // max. recursion level |
95 | U32 maxdepth; // max. indentation/recursion level |
70 | } enc_t; |
96 | } enc_t; |
71 | |
97 | |
72 | static void |
98 | static void |
73 | need (enc_t *enc, STRLEN len) |
99 | need (enc_t *enc, STRLEN len) |
74 | { |
100 | { |
… | |
… | |
133 | STRLEN clen; |
159 | STRLEN clen; |
134 | UV uch; |
160 | UV uch; |
135 | |
161 | |
136 | if (is_utf8) |
162 | if (is_utf8) |
137 | { |
163 | { |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
164 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
165 | uch = decode_utf8 (str, end - str, &clen); |
139 | if (clen == (STRLEN)-1) |
166 | if (clen == (STRLEN)-1) |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
167 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
141 | } |
168 | } |
142 | else |
169 | else |
143 | { |
170 | { |
… | |
… | |
424 | } |
451 | } |
425 | else if (SvROK (sv)) |
452 | else if (SvROK (sv)) |
426 | { |
453 | { |
427 | SV *rv = SvRV (sv); |
454 | SV *rv = SvRV (sv); |
428 | |
455 | |
429 | if (enc->indent >= enc->max_depth) |
456 | if (enc->indent >= enc->maxdepth) |
430 | croak ("data structure too deep (hit recursion limit)"); |
457 | croak ("data structure too deep (hit recursion limit)"); |
431 | |
458 | |
432 | switch (SvTYPE (rv)) |
459 | switch (SvTYPE (rv)) |
433 | { |
460 | { |
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
… | |
… | |
445 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
446 | SvPV_nolen (sv), SvFLAGS (sv)); |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
447 | } |
474 | } |
448 | |
475 | |
449 | static SV * |
476 | static SV * |
450 | encode_json (SV *scalar, UV flags) |
477 | encode_json (SV *scalar, U32 flags) |
451 | { |
478 | { |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
454 | |
481 | |
455 | enc_t enc; |
482 | enc_t enc; |
456 | enc.flags = flags; |
483 | enc.flags = flags; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
485 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
460 | enc.indent = 0; |
487 | enc.indent = 0; |
461 | enc.max_depth = 0x7fffffffUL; |
488 | enc.maxdepth = DEC_DEPTH (flags); |
462 | |
489 | |
463 | SvPOK_only (enc.sv); |
490 | SvPOK_only (enc.sv); |
464 | encode_sv (&enc, scalar); |
491 | encode_sv (&enc, scalar); |
465 | |
492 | |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
481 | typedef struct |
508 | typedef struct |
482 | { |
509 | { |
483 | char *cur; // current parser pointer |
510 | char *cur; // current parser pointer |
484 | char *end; // end of input string |
511 | char *end; // end of input string |
485 | const char *err; // parse error, if != 0 |
512 | const char *err; // parse error, if != 0 |
486 | UV flags; // F_* |
513 | U32 flags; // F_* |
|
|
514 | U32 depth; // recursion depth |
|
|
515 | U32 maxdepth; // recursion depth limit |
487 | } dec_t; |
516 | } dec_t; |
488 | |
517 | |
489 | static void |
518 | static void |
490 | decode_ws (dec_t *dec) |
519 | decode_ws (dec_t *dec) |
491 | { |
520 | { |
… | |
… | |
500 | ++dec->cur; |
529 | ++dec->cur; |
501 | } |
530 | } |
502 | } |
531 | } |
503 | |
532 | |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
533 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
534 | |
505 | #define EXPECT_CH(ch) SB \ |
535 | #define EXPECT_CH(ch) SB \ |
506 | if (*dec->cur != ch) \ |
536 | if (*dec->cur != ch) \ |
507 | ERR (# ch " expected"); \ |
537 | ERR (# ch " expected"); \ |
508 | ++dec->cur; \ |
538 | ++dec->cur; \ |
509 | SE |
539 | SE |
|
|
540 | |
|
|
541 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
542 | #define DEC_DEC_DEPTH --dec->depth |
510 | |
543 | |
511 | static SV *decode_sv (dec_t *dec); |
544 | static SV *decode_sv (dec_t *dec); |
512 | |
545 | |
513 | static signed char decode_hexdigit[256]; |
546 | static signed char decode_hexdigit[256]; |
514 | |
547 | |
… | |
… | |
619 | else if (ch >= 0x80) |
652 | else if (ch >= 0x80) |
620 | { |
653 | { |
621 | --dec->cur; |
654 | --dec->cur; |
622 | |
655 | |
623 | STRLEN clen; |
656 | STRLEN clen; |
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
625 | if (clen == (STRLEN)-1) |
658 | if (clen == (STRLEN)-1) |
626 | ERR ("malformed UTF-8 character in JSON string"); |
659 | ERR ("malformed UTF-8 character in JSON string"); |
627 | |
660 | |
628 | do |
661 | do |
629 | { |
662 | { |
… | |
… | |
758 | static SV * |
791 | static SV * |
759 | decode_av (dec_t *dec) |
792 | decode_av (dec_t *dec) |
760 | { |
793 | { |
761 | AV *av = newAV (); |
794 | AV *av = newAV (); |
762 | |
795 | |
|
|
796 | DEC_INC_DEPTH; |
763 | decode_ws (dec); |
797 | decode_ws (dec); |
|
|
798 | |
764 | if (*dec->cur == ']') |
799 | if (*dec->cur == ']') |
765 | ++dec->cur; |
800 | ++dec->cur; |
766 | else |
801 | else |
767 | for (;;) |
802 | for (;;) |
768 | { |
803 | { |
… | |
… | |
786 | ERR (", or ] expected while parsing array"); |
821 | ERR (", or ] expected while parsing array"); |
787 | |
822 | |
788 | ++dec->cur; |
823 | ++dec->cur; |
789 | } |
824 | } |
790 | |
825 | |
|
|
826 | DEC_DEC_DEPTH; |
791 | return newRV_noinc ((SV *)av); |
827 | return newRV_noinc ((SV *)av); |
792 | |
828 | |
793 | fail: |
829 | fail: |
794 | SvREFCNT_dec (av); |
830 | SvREFCNT_dec (av); |
|
|
831 | DEC_DEC_DEPTH; |
795 | return 0; |
832 | return 0; |
796 | } |
833 | } |
797 | |
834 | |
798 | static SV * |
835 | static SV * |
799 | decode_hv (dec_t *dec) |
836 | decode_hv (dec_t *dec) |
800 | { |
837 | { |
801 | HV *hv = newHV (); |
838 | HV *hv = newHV (); |
802 | |
839 | |
|
|
840 | DEC_INC_DEPTH; |
803 | decode_ws (dec); |
841 | decode_ws (dec); |
|
|
842 | |
804 | if (*dec->cur == '}') |
843 | if (*dec->cur == '}') |
805 | ++dec->cur; |
844 | ++dec->cur; |
806 | else |
845 | else |
807 | for (;;) |
846 | for (;;) |
808 | { |
847 | { |
… | |
… | |
821 | { |
860 | { |
822 | SvREFCNT_dec (key); |
861 | SvREFCNT_dec (key); |
823 | goto fail; |
862 | goto fail; |
824 | } |
863 | } |
825 | |
864 | |
826 | //TODO: optimise |
|
|
827 | hv_store_ent (hv, key, value, 0); |
865 | hv_store_ent (hv, key, value, 0); |
|
|
866 | SvREFCNT_dec (key); |
828 | |
867 | |
829 | decode_ws (dec); |
868 | decode_ws (dec); |
830 | |
869 | |
831 | if (*dec->cur == '}') |
870 | if (*dec->cur == '}') |
832 | { |
871 | { |
… | |
… | |
838 | ERR (", or } expected while parsing object/hash"); |
877 | ERR (", or } expected while parsing object/hash"); |
839 | |
878 | |
840 | ++dec->cur; |
879 | ++dec->cur; |
841 | } |
880 | } |
842 | |
881 | |
|
|
882 | DEC_DEC_DEPTH; |
843 | return newRV_noinc ((SV *)hv); |
883 | return newRV_noinc ((SV *)hv); |
844 | |
884 | |
845 | fail: |
885 | fail: |
846 | SvREFCNT_dec (hv); |
886 | SvREFCNT_dec (hv); |
|
|
887 | DEC_DEC_DEPTH; |
847 | return 0; |
888 | return 0; |
848 | } |
889 | } |
849 | |
890 | |
850 | static SV * |
891 | static SV * |
851 | decode_sv (dec_t *dec) |
892 | decode_sv (dec_t *dec) |
… | |
… | |
903 | fail: |
944 | fail: |
904 | return 0; |
945 | return 0; |
905 | } |
946 | } |
906 | |
947 | |
907 | static SV * |
948 | static SV * |
908 | decode_json (SV *string, UV flags) |
949 | decode_json (SV *string, U32 flags) |
909 | { |
950 | { |
910 | SV *sv; |
951 | SV *sv; |
911 | |
952 | |
912 | if (flags & F_UTF8) |
953 | if (flags & F_UTF8) |
913 | sv_utf8_downgrade (string, 0); |
954 | sv_utf8_downgrade (string, 0); |
… | |
… | |
915 | sv_utf8_upgrade (string); |
956 | sv_utf8_upgrade (string); |
916 | |
957 | |
917 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
958 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
918 | |
959 | |
919 | dec_t dec; |
960 | dec_t dec; |
920 | dec.flags = flags; |
961 | dec.flags = flags; |
921 | dec.cur = SvPVX (string); |
962 | dec.cur = SvPVX (string); |
922 | dec.end = SvEND (string); |
963 | dec.end = SvEND (string); |
923 | dec.err = 0; |
964 | dec.err = 0; |
|
|
965 | dec.depth = 0; |
|
|
966 | dec.maxdepth = DEC_DEPTH (dec.flags); |
924 | |
967 | |
|
|
968 | *SvEND (string) = 0; // this should basically be a nop, too |
925 | sv = decode_sv (&dec); |
969 | sv = decode_sv (&dec); |
926 | |
970 | |
927 | if (!sv) |
971 | if (!sv) |
928 | { |
972 | { |
929 | IV offset = dec.flags & F_UTF8 |
973 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
962 | BOOT: |
1006 | BOOT: |
963 | { |
1007 | { |
964 | int i; |
1008 | int i; |
965 | |
1009 | |
966 | memset (decode_hexdigit, 0xff, 256); |
1010 | memset (decode_hexdigit, 0xff, 256); |
|
|
1011 | |
967 | for (i = 10; i--; ) |
1012 | for (i = 0; i < 256; ++i) |
968 | decode_hexdigit ['0' + i] = i; |
1013 | decode_hexdigit [i] = |
969 | |
1014 | i >= '0' && i <= '9' ? i - '0' |
970 | for (i = 7; i--; ) |
1015 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
971 | { |
1016 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
972 | decode_hexdigit ['a' + i] = 10 + i; |
1017 | : -1; |
973 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
974 | } |
|
|
975 | |
1018 | |
976 | json_stash = gv_stashpv ("JSON::XS", 1); |
1019 | json_stash = gv_stashpv ("JSON::XS", 1); |
977 | } |
1020 | } |
978 | |
1021 | |
979 | PROTOTYPES: DISABLE |
1022 | PROTOTYPES: DISABLE |
… | |
… | |
1006 | RETVAL = newSVsv (self); |
1049 | RETVAL = newSVsv (self); |
1007 | } |
1050 | } |
1008 | OUTPUT: |
1051 | OUTPUT: |
1009 | RETVAL |
1052 | RETVAL |
1010 | |
1053 | |
|
|
1054 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
|
|
1055 | CODE: |
|
|
1056 | { |
|
|
1057 | UV *uv = SvJSON (self); |
|
|
1058 | UV log2 = 0; |
|
|
1059 | |
|
|
1060 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1061 | |
|
|
1062 | while ((1UL << log2) < max_depth) |
|
|
1063 | ++log2; |
|
|
1064 | |
|
|
1065 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1066 | |
|
|
1067 | RETVAL = newSVsv (self); |
|
|
1068 | } |
|
|
1069 | OUTPUT: |
|
|
1070 | RETVAL |
|
|
1071 | |
1011 | void encode (SV *self, SV *scalar) |
1072 | void encode (SV *self, SV *scalar) |
1012 | PPCODE: |
1073 | PPCODE: |
1013 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1074 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1014 | |
1075 | |
1015 | void decode (SV *self, SV *jsonstr) |
1076 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1017 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1078 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1018 | |
1079 | |
1019 | PROTOTYPES: ENABLE |
1080 | PROTOTYPES: ENABLE |
1020 | |
1081 | |
1021 | void to_json (SV *scalar) |
1082 | void to_json (SV *scalar) |
|
|
1083 | ALIAS: |
|
|
1084 | objToJson = 0 |
1022 | PPCODE: |
1085 | PPCODE: |
1023 | XPUSHs (encode_json (scalar, F_UTF8)); |
1086 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1024 | |
1087 | |
1025 | void from_json (SV *jsonstr) |
1088 | void from_json (SV *jsonstr) |
|
|
1089 | ALIAS: |
|
|
1090 | jsonToObj = 0 |
1026 | PPCODE: |
1091 | PPCODE: |
1027 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1092 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1028 | |
1093 | |