… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
20 | #endif |
21 | #ifndef HvNAMEUTF8 |
21 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
24 | |
27 | |
25 | // known tags |
28 | // known tags |
26 | enum cbor_tag |
29 | enum cbor_tag |
27 | { |
30 | { |
28 | // inofficial extensions (pending iana registration) |
31 | // extensions |
|
|
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
37 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
39 | |
37 | // rfc7049 |
40 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
… | |
… | |
79 | # define CBOR_SLOW 0 |
82 | # define CBOR_SLOW 0 |
80 | # define CBOR_STASH cbor_stash |
83 | # define CBOR_STASH cbor_stash |
81 | #endif |
84 | #endif |
82 | |
85 | |
83 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
86 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
87 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
85 | |
88 | |
86 | typedef struct { |
89 | typedef struct { |
87 | U32 flags; |
90 | U32 flags; |
88 | U32 max_depth; |
91 | U32 max_depth; |
89 | STRLEN max_size; |
92 | STRLEN max_size; |
|
|
93 | SV *filter; |
90 | } CBOR; |
94 | } CBOR; |
91 | |
95 | |
92 | ecb_inline void |
96 | ecb_inline void |
93 | cbor_init (CBOR *cbor) |
97 | cbor_init (CBOR *cbor) |
94 | { |
98 | { |
95 | Zero (cbor, 1, CBOR); |
99 | Zero (cbor, 1, CBOR); |
96 | cbor->max_depth = 512; |
100 | cbor->max_depth = 512; |
|
|
101 | } |
|
|
102 | |
|
|
103 | ecb_inline void |
|
|
104 | cbor_free (CBOR *cbor) |
|
|
105 | { |
|
|
106 | SvREFCNT_dec (cbor->filter); |
97 | } |
107 | } |
98 | |
108 | |
99 | ///////////////////////////////////////////////////////////////////////////// |
109 | ///////////////////////////////////////////////////////////////////////////// |
100 | // utility functions |
110 | // utility functions |
101 | |
111 | |
… | |
… | |
131 | { |
141 | { |
132 | return idx > 23 |
142 | return idx > 23 |
133 | ? idx > 0xffU |
143 | ? idx > 0xffU |
134 | ? idx > 0xffffU |
144 | ? idx > 0xffffU |
135 | ? idx > 0xffffffffU |
145 | ? idx > 0xffffffffU |
|
|
146 | ? 11 |
136 | ? 7 |
147 | : 7 |
137 | : 6 |
|
|
138 | : 5 |
148 | : 5 |
139 | : 4 |
149 | : 4 |
140 | : 3; |
150 | : 3; |
141 | } |
151 | } |
142 | |
152 | |
… | |
… | |
542 | CBOR cbor; |
552 | CBOR cbor; |
543 | U32 depth; // recursion depth |
553 | U32 depth; // recursion depth |
544 | U32 maxdepth; // recursion depth limit |
554 | U32 maxdepth; // recursion depth limit |
545 | AV *shareable; |
555 | AV *shareable; |
546 | AV *stringref; |
556 | AV *stringref; |
|
|
557 | SV *decode_tagged; |
547 | } dec_t; |
558 | } dec_t; |
548 | |
559 | |
549 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
550 | |
561 | |
551 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
562 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
905 | |
916 | |
906 | default: |
917 | default: |
907 | { |
918 | { |
908 | sv = decode_sv (dec); |
919 | sv = decode_sv (dec); |
909 | |
920 | |
|
|
921 | dSP; |
|
|
922 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
923 | EXTEND (SP, 2); |
|
|
924 | PUSHs (newSVuv (tag)); |
|
|
925 | PUSHs (sv); |
|
|
926 | |
|
|
927 | PUTBACK; |
|
|
928 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
929 | SPAGAIN; |
|
|
930 | |
|
|
931 | if (SvTRUE (ERRSV)) |
|
|
932 | { |
|
|
933 | FREETMPS; LEAVE; |
|
|
934 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
935 | } |
|
|
936 | |
|
|
937 | if (count) |
|
|
938 | { |
|
|
939 | SvREFCNT_dec (sv); |
|
|
940 | sv = SvREFCNT_inc (POPs); |
|
|
941 | } |
|
|
942 | else |
|
|
943 | { |
910 | AV *av = newAV (); |
944 | AV *av = newAV (); |
911 | av_push (av, newSVuv (tag)); |
945 | av_push (av, newSVuv (tag)); |
912 | av_push (av, sv); |
946 | av_push (av, sv); |
913 | |
947 | |
914 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
948 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
915 | ? cbor_tagged_stash |
949 | ? cbor_tagged_stash |
916 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
950 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
917 | |
|
|
918 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
951 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
952 | } |
|
|
953 | |
|
|
954 | PUTBACK; |
|
|
955 | |
|
|
956 | FREETMPS; LEAVE; |
919 | } |
957 | } |
920 | break; |
958 | break; |
921 | } |
959 | } |
922 | |
960 | |
923 | return sv; |
961 | return sv; |
… | |
… | |
1069 | |
1107 | |
1070 | types_true = get_bool ("Types::Serialiser::true" ); |
1108 | types_true = get_bool ("Types::Serialiser::true" ); |
1071 | types_false = get_bool ("Types::Serialiser::false"); |
1109 | types_false = get_bool ("Types::Serialiser::false"); |
1072 | types_error = get_bool ("Types::Serialiser::error"); |
1110 | types_error = get_bool ("Types::Serialiser::error"); |
1073 | |
1111 | |
|
|
1112 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1113 | |
1074 | sv_cbor = newSVpv ("CBOR", 0); |
1114 | sv_cbor = newSVpv ("CBOR", 0); |
1075 | SvREADONLY_on (sv_cbor); |
1115 | SvREADONLY_on (sv_cbor); |
1076 | } |
1116 | } |
1077 | |
1117 | |
1078 | PROTOTYPES: DISABLE |
1118 | PROTOTYPES: DISABLE |
… | |
… | |
1141 | CODE: |
1181 | CODE: |
1142 | RETVAL = self->max_size; |
1182 | RETVAL = self->max_size; |
1143 | OUTPUT: |
1183 | OUTPUT: |
1144 | RETVAL |
1184 | RETVAL |
1145 | |
1185 | |
|
|
1186 | void filter (CBOR *self, SV *filter = 0) |
|
|
1187 | PPCODE: |
|
|
1188 | SvREFCNT_dec (self->filter); |
|
|
1189 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1190 | XPUSHs (ST (0)); |
|
|
1191 | |
|
|
1192 | SV *get_filter (CBOR *self) |
|
|
1193 | CODE: |
|
|
1194 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
|
|
1195 | OUTPUT: |
|
|
1196 | RETVAL |
|
|
1197 | |
1146 | void encode (CBOR *self, SV *scalar) |
1198 | void encode (CBOR *self, SV *scalar) |
1147 | PPCODE: |
1199 | PPCODE: |
1148 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1200 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1149 | XPUSHs (scalar); |
1201 | XPUSHs (scalar); |
1150 | |
1202 | |
… | |
… | |
1162 | EXTEND (SP, 2); |
1214 | EXTEND (SP, 2); |
1163 | PUSHs (sv); |
1215 | PUSHs (sv); |
1164 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1216 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1165 | } |
1217 | } |
1166 | |
1218 | |
|
|
1219 | void DESTROY (CBOR *self) |
|
|
1220 | PPCODE: |
|
|
1221 | cbor_free (self); |
|
|
1222 | |
1167 | PROTOTYPES: ENABLE |
1223 | PROTOTYPES: ENABLE |
1168 | |
1224 | |
1169 | void encode_cbor (SV *scalar) |
1225 | void encode_cbor (SV *scalar) |
1170 | PPCODE: |
1226 | PPCODE: |
1171 | { |
1227 | { |