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.8 by root, Sun Oct 27 10:12:01 2013 UTC vs.
Revision 1.11 by root, Sun Oct 27 22:35:15 2013 UTC

9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11 11
12#include "ecb.h" 12#include "ecb.h"
13 13
14// known tags, rfc7049 14// known tags
15enum cbor_tag 15enum cbor_tag
16{ 16{
17 // inofficial extensions (pending iana registration)
18 CBOR_TAG_PERL_OBJECT = 256,
19 CBOR_TAG_GENERIC_OBJECT = 257,
20
21 // rfc7049
17 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 22 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
18 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any 23 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
19 CBOR_TAG_POS_BIGNUM = 2, // byte string 24 CBOR_TAG_POS_BIGNUM = 2, // byte string
20 CBOR_TAG_NEG_BIGNUM = 3, // byte string 25 CBOR_TAG_NEG_BIGNUM = 3, // byte string
21 CBOR_TAG_DECIMAL = 4, // decimal fraction, array 26 CBOR_TAG_DECIMAL = 4, // decimal fraction, array
22 CBOR_TAG_BIGFLOAT = 5, // array 27 CBOR_TAG_BIGFLOAT = 5, // array
23 28
24 CBOR_TAG_CONV_B64U = 21, // base64url, any 29 CBOR_TAG_CONV_B64U = 21, // base64url, any
25 CBOR_TAG_CONV_B64 = 22, // base64, any 30 CBOR_TAG_CONV_B64 = 22, // base64, any
26 CBOR_TAG_CONV_HEX = 23, // base16, any 31 CBOR_TAG_CONV_HEX = 23, // base16, any
27 CBOR_TAG_CBOR = 24, // embedded cbor, byte string 32 CBOR_TAG_CBOR = 24, // embedded cbor, byte string
28 33
29 CBOR_TAG_URI = 32, // URI rfc3986, utf-8 34 CBOR_TAG_URI = 32, // URI rfc3986, utf-8
30 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 35 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8
31 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 36 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
32 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 37 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
33 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 38 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
34 39
35 CBOR_TAG_MAGIC = 55799 40 CBOR_TAG_MAGIC = 55799 // self-describe cbor
36}; 41};
37 42
38#define F_SHRINK 0x00000200UL 43#define F_SHRINK 0x00000200UL
39#define F_ALLOW_UNKNOWN 0x00002000UL 44#define F_ALLOW_UNKNOWN 0x00002000UL
40 45
55#else 60#else
56# define CBOR_SLOW 0 61# define CBOR_SLOW 0
57# define CBOR_STASH cbor_stash 62# define CBOR_STASH cbor_stash
58#endif 63#endif
59 64
60static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: 65static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
61static SV *cbor_true, *cbor_false; 66static SV *types_true, *types_false, *types_error, *sv_cbor;
62 67
63typedef struct { 68typedef struct {
64 U32 flags; 69 U32 flags;
65 U32 max_depth; 70 U32 max_depth;
66 STRLEN max_size; 71 STRLEN max_size;
254 SvGETMAGIC (sv); 259 SvGETMAGIC (sv);
255 svt = SvTYPE (sv); 260 svt = SvTYPE (sv);
256 261
257 if (ecb_expect_false (SvOBJECT (sv))) 262 if (ecb_expect_false (SvOBJECT (sv)))
258 { 263 {
259 HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash 264 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
260 ? cbor_boolean_stash 265 ? types_boolean_stash
261 : gv_stashpv ("CBOR::XS::Boolean", 1); 266 : gv_stashpv ("Types::Serialiser::Boolean", 1);
267 HV *error_stash = !CBOR_SLOW || types_error_stash
268 ? types_error_stash
269 : gv_stashpv ("Types::Serialiser::Error", 1);
262 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 270 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
263 ? cbor_tagged_stash 271 ? cbor_tagged_stash
264 : gv_stashpv ("CBOR::XS::Tagged" , 1); 272 : gv_stashpv ("CBOR::XS::Tagged" , 1);
265 273
274 HV *stash = SvSTASH (sv);
275 GV *method;
276
266 if (SvSTASH (sv) == boolean_stash) 277 if (stash == boolean_stash)
267 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); 278 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20);
279 else if (stash == error_stash)
280 encode_ch (enc, 0xe0 | 23);
268 else if (SvSTASH (sv) == tagged_stash) 281 else if (stash == tagged_stash)
269 { 282 {
270 if (svt != SVt_PVAV) 283 if (svt != SVt_PVAV)
271 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 284 croak ("encountered CBOR::XS::Tagged object that isn't an array");
272 285
273 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); 286 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1)));
274 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 287 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
275 } 288 }
289 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
290 {
291 dSP;
292
293 ENTER; SAVETMPS; PUSHMARK (SP);
294 // we re-bless the reference to get overload and other niceties right
295 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
296
297 PUTBACK;
298 // G_SCALAR ensures that return value is 1
299 call_sv ((SV *)GvCV (method), G_SCALAR);
300 SPAGAIN;
301
302 // catch this surprisingly common error
303 if (SvROK (TOPs) && SvRV (TOPs) == sv)
304 croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash));
305
306 encode_sv (enc, POPs);
307
308 PUTBACK;
309
310 FREETMPS; LEAVE;
311 }
312 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
313 {
314 dSP;
315
316 ENTER; SAVETMPS; PUSHMARK (SP);
317 EXTEND (SP, 2);
318 // we re-bless the reference to get overload and other niceties right
319 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
320 PUSHs (sv_cbor);
321
322 PUTBACK;
323 int count = call_sv ((SV *)GvCV (method), G_ARRAY);
324 SPAGAIN;
325
326 // catch this surprisingly common error
327 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
328 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
329
330 encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT);
331 encode_uint (enc, 0x80, count + 1);
332 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
333
334 while (count)
335 encode_sv (enc, SP[1 - count--]);
336
337 PUTBACK;
338
339 FREETMPS; LEAVE;
340 }
276 else 341 else
277 {
278 // we re-bless the reference to get overload and other niceties right
279 GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0);
280
281 if (to_cbor)
282 {
283 dSP;
284
285 ENTER; SAVETMPS; PUSHMARK (SP);
286 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
287
288 // calling with G_SCALAR ensures that we always get a 1 return value
289 PUTBACK;
290 call_sv ((SV *)GvCV (to_cbor), G_SCALAR);
291 SPAGAIN;
292
293 // catch this surprisingly common error
294 if (SvROK (TOPs) && SvRV (TOPs) == sv)
295 croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
296
297 sv = POPs;
298 PUTBACK;
299
300 encode_sv (enc, sv);
301
302 FREETMPS; LEAVE;
303 }
304 else
305 croak ("encountered object '%s', but no TO_CBOR method available on it", 342 croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it",
306 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 343 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
307 }
308 } 344 }
309 else if (svt == SVt_PVHV) 345 else if (svt == SVt_PVHV)
310 encode_hv (enc, (HV *)sv); 346 encode_hv (enc, (HV *)sv);
311 else if (svt == SVt_PVAV) 347 else if (svt == SVt_PVAV)
312 encode_av (enc, (AV *)sv); 348 encode_av (enc, (AV *)sv);
636decode_tagged (dec_t *dec) 672decode_tagged (dec_t *dec)
637{ 673{
638 UV tag = decode_uint (dec); 674 UV tag = decode_uint (dec);
639 SV *sv = decode_sv (dec); 675 SV *sv = decode_sv (dec);
640 676
641 if (tag == CBOR_TAG_MAGIC) // 2.4.5 Self-Describe CBOR 677 if (tag == CBOR_TAG_MAGIC)
642 return sv; 678 return sv;
679 else if (tag == CBOR_TAG_PERL_OBJECT)
680 {
681 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
682 ERR ("corrupted CBOR data (non-array perl object)");
643 683
684 AV *av = (AV *)SvRV (sv);
685 int len = av_len (av) + 1;
686 HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0);
687
688 if (!stash)
689 ERR ("cannot decode perl-object (package does not exist)");
690
691 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
692
693 if (!method)
694 ERR ("cannot decode perl-object (package does not have a THAW method)");
695
696 dSP;
697
698 ENTER; SAVETMPS; PUSHMARK (SP);
699 EXTEND (SP, len + 1);
700 // we re-bless the reference to get overload and other niceties right
701 PUSHs (*av_fetch (av, 0, 1));
702 PUSHs (sv_cbor);
703
704 int i;
705
706 for (i = 1; i < len; ++i)
707 PUSHs (*av_fetch (av, i, 1));
708
709 PUTBACK;
710 call_sv ((SV *)GvCV (method), G_SCALAR);
711 SPAGAIN;
712
713 sv = SvREFCNT_inc (POPs);
714
715 PUTBACK;
716
717 FREETMPS; LEAVE;
718
719 return sv;
720 }
721 else
722 {
644 AV *av = newAV (); 723 AV *av = newAV ();
645 av_push (av, newSVuv (tag)); 724 av_push (av, newSVuv (tag));
646 av_push (av, sv); 725 av_push (av, sv);
647 726
648 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 727 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
649 ? cbor_tagged_stash 728 ? cbor_tagged_stash
650 : gv_stashpv ("CBOR::XS::Tagged" , 1); 729 : gv_stashpv ("CBOR::XS::Tagged" , 1);
651 730
652 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 731 return sv_bless (newRV_noinc ((SV *)av), tagged_stash);
732 }
733
734fail:
735 SvREFCNT_dec (sv);
736 return &PL_sv_undef;
653} 737}
654 738
655static SV * 739static SV *
656decode_sv (dec_t *dec) 740decode_sv (dec_t *dec)
657{ 741{
676 case 7: // misc 760 case 7: // misc
677 switch (*dec->cur++ & 31) 761 switch (*dec->cur++ & 31)
678 { 762 {
679 case 20: 763 case 20:
680#if CBOR_SLOW 764#if CBOR_SLOW
681 cbor_false = get_bool ("CBOR::XS::false"); 765 types_false = get_bool ("Types::Serialiser::false");
682#endif 766#endif
683 return newSVsv (cbor_false); 767 return newSVsv (types_false);
684 case 21: 768 case 21:
685#if CBOR_SLOW 769#if CBOR_SLOW
686 cbor_true = get_bool ("CBOR::XS::true"); 770 types_true = get_bool ("Types::Serialiser::true");
687#endif 771#endif
688 return newSVsv (cbor_true); 772 return newSVsv (types_true);
689 case 22: 773 case 22:
690 return newSVsv (&PL_sv_undef); 774 return newSVsv (&PL_sv_undef);
775 case 23:
776#if CBOR_SLOW
777 types_error = get_bool ("Types::Serialiser::error");
778#endif
779 return newSVsv (types_error);
691 780
692 case 25: 781 case 25:
693 { 782 {
694 WANT (2); 783 WANT (2);
695 784
811MODULE = CBOR::XS PACKAGE = CBOR::XS 900MODULE = CBOR::XS PACKAGE = CBOR::XS
812 901
813BOOT: 902BOOT:
814{ 903{
815 cbor_stash = gv_stashpv ("CBOR::XS" , 1); 904 cbor_stash = gv_stashpv ("CBOR::XS" , 1);
816 cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1);
817 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); 905 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
818 906
819 cbor_true = get_bool ("CBOR::XS::true"); 907 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
820 cbor_false = get_bool ("CBOR::XS::false"); 908 types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1);
909
910 types_true = get_bool ("Types::Serialiser::true" );
911 types_false = get_bool ("Types::Serialiser::false");
912 types_error = get_bool ("Types::Serialiser::error");
913
914 sv_cbor = newSVpv ("CBOR", 0);
915 SvREADONLY_on (sv_cbor);
821} 916}
822 917
823PROTOTYPES: DISABLE 918PROTOTYPES: DISABLE
824 919
825void CLONE (...) 920void CLONE (...)
826 CODE: 921 CODE:
827 cbor_stash = 0; 922 cbor_stash = 0;
828 cbor_boolean_stash = 0;
829 cbor_tagged_stash = 0; 923 cbor_tagged_stash = 0;
924 types_error_stash = 0;
925 types_boolean_stash = 0;
830 926
831void new (char *klass) 927void new (char *klass)
832 PPCODE: 928 PPCODE:
833{ 929{
834 SV *pv = NEWSV (0, sizeof (CBOR)); 930 SV *pv = NEWSV (0, sizeof (CBOR));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines