ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.26 by root, Fri Nov 22 09:40:13 2013 UTC vs.
Revision 1.27 by root, Fri Nov 22 15:28:38 2013 UTC

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
83static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: 83static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
84static SV *types_true, *types_false, *types_error, *sv_cbor; 84static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter;
85 85
86typedef struct { 86typedef 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
92ecb_inline void 93ecb_inline void
93cbor_init (CBOR *cbor) 94cbor_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
100ecb_inline void
101cbor_free (CBOR *cbor)
102{
103 SvREFCNT_dec (cbor->filter);
97} 104}
98 105
99///////////////////////////////////////////////////////////////////////////// 106/////////////////////////////////////////////////////////////////////////////
100// utility functions 107// utility functions
101 108
542 CBOR cbor; 549 CBOR cbor;
543 U32 depth; // recursion depth 550 U32 depth; // recursion depth
544 U32 maxdepth; // recursion depth limit 551 U32 maxdepth; // recursion depth limit
545 AV *shareable; 552 AV *shareable;
546 AV *stringref; 553 AV *stringref;
554 SV *decode_tagged;
547} dec_t; 555} dec_t;
548 556
549#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
550 558
551#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")
905 913
906 default: 914 default:
907 { 915 {
908 sv = decode_sv (dec); 916 sv = decode_sv (dec);
909 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 {
910 AV *av = newAV (); 941 AV *av = newAV ();
911 av_push (av, newSVuv (tag)); 942 av_push (av, newSVuv (tag));
912 av_push (av, sv); 943 av_push (av, sv);
913 944
914 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 945 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
915 ? cbor_tagged_stash 946 ? cbor_tagged_stash
916 : gv_stashpv ("CBOR::XS::Tagged" , 1); 947 : gv_stashpv ("CBOR::XS::Tagged" , 1);
917
918 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;
919 } 954 }
920 break; 955 break;
921 } 956 }
922 957
923 return sv; 958 return sv;
1069 1104
1070 types_true = get_bool ("Types::Serialiser::true" ); 1105 types_true = get_bool ("Types::Serialiser::true" );
1071 types_false = get_bool ("Types::Serialiser::false"); 1106 types_false = get_bool ("Types::Serialiser::false");
1072 types_error = get_bool ("Types::Serialiser::error"); 1107 types_error = get_bool ("Types::Serialiser::error");
1073 1108
1109 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1110
1074 sv_cbor = newSVpv ("CBOR", 0); 1111 sv_cbor = newSVpv ("CBOR", 0);
1075 SvREADONLY_on (sv_cbor); 1112 SvREADONLY_on (sv_cbor);
1076} 1113}
1077 1114
1078PROTOTYPES: DISABLE 1115PROTOTYPES: DISABLE
1141 CODE: 1178 CODE:
1142 RETVAL = self->max_size; 1179 RETVAL = self->max_size;
1143 OUTPUT: 1180 OUTPUT:
1144 RETVAL 1181 RETVAL
1145 1182
1183void 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
1189SV *get_filter (CBOR *self)
1190 CODE:
1191 RETVAL = self->filter ? self->filter : NEWSV (0, 0);
1192 OUTPUT:
1193 RETVAL
1194
1146void encode (CBOR *self, SV *scalar) 1195void encode (CBOR *self, SV *scalar)
1147 PPCODE: 1196 PPCODE:
1148 PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; 1197 PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN;
1149 XPUSHs (scalar); 1198 XPUSHs (scalar);
1150 1199
1162 EXTEND (SP, 2); 1211 EXTEND (SP, 2);
1163 PUSHs (sv); 1212 PUSHs (sv);
1164 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1213 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1165} 1214}
1166 1215
1216void DESTROY (CBOR *self)
1217 PPCODE:
1218 cbor_free (self);
1219
1167PROTOTYPES: ENABLE 1220PROTOTYPES: ENABLE
1168 1221
1169void encode_cbor (SV *scalar) 1222void encode_cbor (SV *scalar)
1170 PPCODE: 1223 PPCODE:
1171{ 1224{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines