… | |
… | |
79 | # define CBOR_SLOW 0 |
79 | # define CBOR_SLOW 0 |
80 | # define CBOR_STASH cbor_stash |
80 | # define CBOR_STASH cbor_stash |
81 | #endif |
81 | #endif |
82 | |
82 | |
83 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
83 | 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; |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
85 | |
85 | |
86 | typedef struct { |
86 | typedef struct { |
87 | U32 flags; |
87 | U32 flags; |
88 | U32 max_depth; |
88 | U32 max_depth; |
89 | STRLEN max_size; |
89 | STRLEN max_size; |
|
|
90 | SV *filter; |
90 | } CBOR; |
91 | } CBOR; |
91 | |
92 | |
92 | ecb_inline void |
93 | ecb_inline void |
93 | cbor_init (CBOR *cbor) |
94 | cbor_init (CBOR *cbor) |
94 | { |
95 | { |
95 | Zero (cbor, 1, CBOR); |
96 | Zero (cbor, 1, CBOR); |
96 | cbor->max_depth = 512; |
97 | cbor->max_depth = 512; |
|
|
98 | } |
|
|
99 | |
|
|
100 | ecb_inline void |
|
|
101 | cbor_free (CBOR *cbor) |
|
|
102 | { |
|
|
103 | SvREFCNT_dec (cbor->filter); |
97 | } |
104 | } |
98 | |
105 | |
99 | ///////////////////////////////////////////////////////////////////////////// |
106 | ///////////////////////////////////////////////////////////////////////////// |
100 | // utility functions |
107 | // utility functions |
101 | |
108 | |
… | |
… | |
271 | } |
278 | } |
272 | |
279 | |
273 | --enc->depth; |
280 | --enc->depth; |
274 | } |
281 | } |
275 | |
282 | |
276 | ecb_inline void |
|
|
277 | encode_he (enc_t *enc, HE *he) |
|
|
278 | { |
|
|
279 | } |
|
|
280 | |
|
|
281 | static void |
283 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
284 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
285 | { |
284 | HE *he; |
286 | HE *he; |
285 | |
287 | |
… | |
… | |
547 | CBOR cbor; |
549 | CBOR cbor; |
548 | U32 depth; // recursion depth |
550 | U32 depth; // recursion depth |
549 | U32 maxdepth; // recursion depth limit |
551 | U32 maxdepth; // recursion depth limit |
550 | AV *shareable; |
552 | AV *shareable; |
551 | AV *stringref; |
553 | AV *stringref; |
|
|
554 | SV *decode_tagged; |
552 | } dec_t; |
555 | } dec_t; |
553 | |
556 | |
554 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
557 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
555 | |
558 | |
556 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
559 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
766 | STRLEN len = decode_uint (dec); |
769 | STRLEN len = decode_uint (dec); |
767 | |
770 | |
768 | WANT (len); |
771 | WANT (len); |
769 | sv = newSVpvn (dec->cur, len); |
772 | sv = newSVpvn (dec->cur, len); |
770 | dec->cur += len; |
773 | dec->cur += len; |
|
|
774 | |
|
|
775 | if (ecb_expect_false (dec->stringref) |
|
|
776 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
777 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
771 | } |
778 | } |
772 | |
779 | |
773 | if (utf8) |
780 | if (utf8) |
774 | SvUTF8_on (sv); |
781 | SvUTF8_on (sv); |
775 | |
|
|
776 | if (ecb_expect_false (dec->stringref) |
|
|
777 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
778 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
779 | |
782 | |
780 | return sv; |
783 | return sv; |
781 | |
784 | |
782 | fail: |
785 | fail: |
783 | SvREFCNT_dec (sv); |
786 | SvREFCNT_dec (sv); |
… | |
… | |
910 | |
913 | |
911 | default: |
914 | default: |
912 | { |
915 | { |
913 | sv = decode_sv (dec); |
916 | sv = decode_sv (dec); |
914 | |
917 | |
|
|
918 | dSP; |
|
|
919 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
920 | EXTEND (SP, 2); |
|
|
921 | PUSHs (newSVuv (tag)); |
|
|
922 | PUSHs (sv); |
|
|
923 | |
|
|
924 | PUTBACK; |
|
|
925 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
926 | SPAGAIN; |
|
|
927 | |
|
|
928 | if (SvTRUE (ERRSV)) |
|
|
929 | { |
|
|
930 | FREETMPS; LEAVE; |
|
|
931 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
932 | } |
|
|
933 | |
|
|
934 | if (count) |
|
|
935 | { |
|
|
936 | SvREFCNT_dec (sv); |
|
|
937 | sv = SvREFCNT_inc (POPs); |
|
|
938 | } |
|
|
939 | else |
|
|
940 | { |
915 | AV *av = newAV (); |
941 | AV *av = newAV (); |
916 | av_push (av, newSVuv (tag)); |
942 | av_push (av, newSVuv (tag)); |
917 | av_push (av, sv); |
943 | av_push (av, sv); |
918 | |
944 | |
919 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
945 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
920 | ? cbor_tagged_stash |
946 | ? cbor_tagged_stash |
921 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
947 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
922 | |
|
|
923 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
948 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
949 | } |
|
|
950 | |
|
|
951 | PUTBACK; |
|
|
952 | |
|
|
953 | FREETMPS; LEAVE; |
924 | } |
954 | } |
925 | break; |
955 | break; |
926 | } |
956 | } |
927 | |
957 | |
928 | return sv; |
958 | return sv; |
… | |
… | |
1074 | |
1104 | |
1075 | types_true = get_bool ("Types::Serialiser::true" ); |
1105 | types_true = get_bool ("Types::Serialiser::true" ); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
1106 | types_false = get_bool ("Types::Serialiser::false"); |
1077 | types_error = get_bool ("Types::Serialiser::error"); |
1107 | types_error = get_bool ("Types::Serialiser::error"); |
1078 | |
1108 | |
|
|
1109 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1110 | |
1079 | sv_cbor = newSVpv ("CBOR", 0); |
1111 | sv_cbor = newSVpv ("CBOR", 0); |
1080 | SvREADONLY_on (sv_cbor); |
1112 | SvREADONLY_on (sv_cbor); |
1081 | } |
1113 | } |
1082 | |
1114 | |
1083 | PROTOTYPES: DISABLE |
1115 | PROTOTYPES: DISABLE |
… | |
… | |
1146 | CODE: |
1178 | CODE: |
1147 | RETVAL = self->max_size; |
1179 | RETVAL = self->max_size; |
1148 | OUTPUT: |
1180 | OUTPUT: |
1149 | RETVAL |
1181 | RETVAL |
1150 | |
1182 | |
|
|
1183 | void filter (CBOR *self, SV *filter = 0) |
|
|
1184 | PPCODE: |
|
|
1185 | SvREFCNT_dec (self->filter); |
|
|
1186 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1187 | XPUSHs (ST (0)); |
|
|
1188 | |
|
|
1189 | SV *get_filter (CBOR *self) |
|
|
1190 | CODE: |
|
|
1191 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
|
|
1192 | OUTPUT: |
|
|
1193 | RETVAL |
|
|
1194 | |
1151 | void encode (CBOR *self, SV *scalar) |
1195 | void encode (CBOR *self, SV *scalar) |
1152 | PPCODE: |
1196 | PPCODE: |
1153 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1197 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1154 | XPUSHs (scalar); |
1198 | XPUSHs (scalar); |
1155 | |
1199 | |
… | |
… | |
1167 | EXTEND (SP, 2); |
1211 | EXTEND (SP, 2); |
1168 | PUSHs (sv); |
1212 | PUSHs (sv); |
1169 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1213 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1170 | } |
1214 | } |
1171 | |
1215 | |
|
|
1216 | void DESTROY (CBOR *self) |
|
|
1217 | PPCODE: |
|
|
1218 | cbor_free (self); |
|
|
1219 | |
1172 | PROTOTYPES: ENABLE |
1220 | PROTOTYPES: ENABLE |
1173 | |
1221 | |
1174 | void encode_cbor (SV *scalar) |
1222 | void encode_cbor (SV *scalar) |
1175 | PPCODE: |
1223 | PPCODE: |
1176 | { |
1224 | { |