… | |
… | |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
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 | |
|
|
14 | // compatibility with perl <5.18 |
|
|
15 | #ifndef HvNAMELEN_get |
|
|
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
17 | #endif |
|
|
18 | #ifndef HvNAMELEN |
|
|
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
20 | #endif |
|
|
21 | #ifndef HvNAMEUTF8 |
|
|
22 | # define HvNAMEUTF8(hv) 0 |
|
|
23 | #endif |
13 | |
24 | |
14 | // known tags |
25 | // known tags |
15 | enum cbor_tag |
26 | enum cbor_tag |
16 | { |
27 | { |
17 | // inofficial extensions (pending iana registration) |
28 | // inofficial extensions (pending iana registration) |
… | |
… | |
61 | # define CBOR_SLOW 0 |
72 | # define CBOR_SLOW 0 |
62 | # define CBOR_STASH cbor_stash |
73 | # define CBOR_STASH cbor_stash |
63 | #endif |
74 | #endif |
64 | |
75 | |
65 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
76 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
66 | static SV *types_true, *types_false, *types_error; |
77 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
67 | |
78 | |
68 | typedef struct { |
79 | typedef struct { |
69 | U32 flags; |
80 | U32 flags; |
70 | U32 max_depth; |
81 | U32 max_depth; |
71 | STRLEN max_size; |
82 | STRLEN max_size; |
… | |
… | |
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
280 | : gv_stashpv ("Types::Serialiser::Error", 1); |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
281 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
271 | ? cbor_tagged_stash |
282 | ? cbor_tagged_stash |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
283 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
273 | |
284 | |
|
|
285 | HV *stash = SvSTASH (sv); |
|
|
286 | GV *method; |
|
|
287 | |
274 | if (SvSTASH (sv) == boolean_stash) |
288 | if (stash == boolean_stash) |
275 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
289 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
276 | else if (SvSTASH (sv) == error_stash) |
290 | else if (stash == error_stash) |
277 | encode_ch (enc, 0xe0 | 23); |
291 | encode_ch (enc, 0xe0 | 23); |
278 | else if (SvSTASH (sv) == tagged_stash) |
292 | else if (stash == tagged_stash) |
279 | { |
293 | { |
280 | if (svt != SVt_PVAV) |
294 | if (svt != SVt_PVAV) |
281 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
295 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
282 | |
296 | |
283 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
297 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
284 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
298 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
285 | } |
299 | } |
|
|
300 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
301 | { |
|
|
302 | dSP; |
|
|
303 | |
|
|
304 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
305 | // we re-bless the reference to get overload and other niceties right |
|
|
306 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
307 | |
|
|
308 | PUTBACK; |
|
|
309 | // G_SCALAR ensures that return value is 1 |
|
|
310 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
311 | SPAGAIN; |
|
|
312 | |
|
|
313 | // catch this surprisingly common error |
|
|
314 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
315 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
316 | |
|
|
317 | encode_sv (enc, POPs); |
|
|
318 | |
|
|
319 | PUTBACK; |
|
|
320 | |
|
|
321 | FREETMPS; LEAVE; |
|
|
322 | } |
|
|
323 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
324 | { |
|
|
325 | dSP; |
|
|
326 | |
|
|
327 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
328 | EXTEND (SP, 2); |
|
|
329 | // we re-bless the reference to get overload and other niceties right |
|
|
330 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
331 | PUSHs (sv_cbor); |
|
|
332 | |
|
|
333 | PUTBACK; |
|
|
334 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
335 | SPAGAIN; |
|
|
336 | |
|
|
337 | // catch this surprisingly common error |
|
|
338 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
339 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
340 | |
|
|
341 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
|
|
342 | encode_uint (enc, 0x80, count + 1); |
|
|
343 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
344 | |
|
|
345 | while (count) |
|
|
346 | encode_sv (enc, SP[1 - count--]); |
|
|
347 | |
|
|
348 | PUTBACK; |
|
|
349 | |
|
|
350 | FREETMPS; LEAVE; |
|
|
351 | } |
286 | else |
352 | else |
287 | { |
|
|
288 | // we re-bless the reference to get overload and other niceties right |
|
|
289 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
290 | |
|
|
291 | if (to_cbor) |
|
|
292 | { |
|
|
293 | dSP; |
|
|
294 | |
|
|
295 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
296 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
297 | |
|
|
298 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
299 | PUTBACK; |
|
|
300 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | // catch this surprisingly common error |
|
|
304 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
305 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
306 | |
|
|
307 | sv = POPs; |
|
|
308 | PUTBACK; |
|
|
309 | |
|
|
310 | encode_sv (enc, sv); |
|
|
311 | |
|
|
312 | FREETMPS; LEAVE; |
|
|
313 | } |
|
|
314 | else |
|
|
315 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
353 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
316 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
354 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
317 | } |
|
|
318 | } |
355 | } |
319 | else if (svt == SVt_PVHV) |
356 | else if (svt == SVt_PVHV) |
320 | encode_hv (enc, (HV *)sv); |
357 | encode_hv (enc, (HV *)sv); |
321 | else if (svt == SVt_PVAV) |
358 | else if (svt == SVt_PVAV) |
322 | encode_av (enc, (AV *)sv); |
359 | encode_av (enc, (AV *)sv); |
… | |
… | |
572 | |
609 | |
573 | SV *k = decode_sv (dec); |
610 | SV *k = decode_sv (dec); |
574 | SV *v = decode_sv (dec); |
611 | SV *v = decode_sv (dec); |
575 | |
612 | |
576 | hv_store_ent (hv, k, v, 0); |
613 | hv_store_ent (hv, k, v, 0); |
|
|
614 | SvREFCNT_dec (k); |
577 | } |
615 | } |
578 | } |
616 | } |
579 | else |
617 | else |
580 | { |
618 | { |
581 | int len = decode_uint (dec); |
619 | int len = decode_uint (dec); |
… | |
… | |
584 | { |
622 | { |
585 | SV *k = decode_sv (dec); |
623 | SV *k = decode_sv (dec); |
586 | SV *v = decode_sv (dec); |
624 | SV *v = decode_sv (dec); |
587 | |
625 | |
588 | hv_store_ent (hv, k, v, 0); |
626 | hv_store_ent (hv, k, v, 0); |
|
|
627 | SvREFCNT_dec (k); |
589 | } |
628 | } |
590 | } |
629 | } |
591 | |
630 | |
592 | DEC_DEC_DEPTH; |
631 | DEC_DEC_DEPTH; |
593 | return newRV_noinc ((SV *)hv); |
632 | return newRV_noinc ((SV *)hv); |
… | |
… | |
648 | UV tag = decode_uint (dec); |
687 | UV tag = decode_uint (dec); |
649 | SV *sv = decode_sv (dec); |
688 | SV *sv = decode_sv (dec); |
650 | |
689 | |
651 | if (tag == CBOR_TAG_MAGIC) |
690 | if (tag == CBOR_TAG_MAGIC) |
652 | return sv; |
691 | return sv; |
653 | |
|
|
654 | if (tag == CBOR_TAG_PERL_OBJECT) |
692 | else if (tag == CBOR_TAG_PERL_OBJECT) |
655 | { |
693 | { |
656 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
694 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
657 | ERR ("corrupted CBOR data (non-array perl object)"); |
695 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
696 | |
|
|
697 | AV *av = (AV *)SvRV (sv); |
|
|
698 | int len = av_len (av) + 1; |
|
|
699 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
700 | |
|
|
701 | if (!stash) |
|
|
702 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
703 | |
|
|
704 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
658 | |
705 | |
659 | // TODO |
706 | if (!method) |
|
|
707 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
708 | |
|
|
709 | dSP; |
|
|
710 | |
|
|
711 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
712 | EXTEND (SP, len + 1); |
|
|
713 | // we re-bless the reference to get overload and other niceties right |
|
|
714 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
715 | PUSHs (sv_cbor); |
|
|
716 | |
|
|
717 | int i; |
|
|
718 | |
|
|
719 | for (i = 1; i < len; ++i) |
|
|
720 | PUSHs (*av_fetch (av, i, 1)); |
|
|
721 | |
|
|
722 | PUTBACK; |
|
|
723 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
724 | SPAGAIN; |
|
|
725 | |
|
|
726 | SvREFCNT_dec (sv); |
|
|
727 | sv = SvREFCNT_inc (POPs); |
|
|
728 | |
|
|
729 | PUTBACK; |
|
|
730 | |
|
|
731 | FREETMPS; LEAVE; |
|
|
732 | |
|
|
733 | return sv; |
|
|
734 | } |
|
|
735 | else |
660 | } |
736 | { |
661 | |
|
|
662 | AV *av = newAV (); |
737 | AV *av = newAV (); |
663 | av_push (av, newSVuv (tag)); |
738 | av_push (av, newSVuv (tag)); |
664 | av_push (av, sv); |
739 | av_push (av, sv); |
665 | |
740 | |
666 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
741 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
667 | ? cbor_tagged_stash |
742 | ? cbor_tagged_stash |
668 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
743 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
669 | |
744 | |
670 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
745 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
746 | } |
671 | |
747 | |
672 | fail: |
748 | fail: |
673 | SvREFCNT_dec (sv); |
749 | SvREFCNT_dec (sv); |
674 | return &PL_sv_undef; |
750 | return &PL_sv_undef; |
675 | } |
751 | } |
… | |
… | |
846 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
922 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
847 | |
923 | |
848 | types_true = get_bool ("Types::Serialiser::true" ); |
924 | types_true = get_bool ("Types::Serialiser::true" ); |
849 | types_false = get_bool ("Types::Serialiser::false"); |
925 | types_false = get_bool ("Types::Serialiser::false"); |
850 | types_error = get_bool ("Types::Serialiser::error"); |
926 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
927 | |
|
|
928 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
929 | SvREADONLY_on (sv_cbor); |
851 | } |
930 | } |
852 | |
931 | |
853 | PROTOTYPES: DISABLE |
932 | PROTOTYPES: DISABLE |
854 | |
933 | |
855 | void CLONE (...) |
934 | void CLONE (...) |