… | |
… | |
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 |
15 | enum cbor_tag |
15 | enum 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 | |
60 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
65 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
61 | static SV *cbor_true, *cbor_false; |
66 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
62 | |
67 | |
63 | typedef struct { |
68 | typedef 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); |
… | |
… | |
562 | |
598 | |
563 | SV *k = decode_sv (dec); |
599 | SV *k = decode_sv (dec); |
564 | SV *v = decode_sv (dec); |
600 | SV *v = decode_sv (dec); |
565 | |
601 | |
566 | hv_store_ent (hv, k, v, 0); |
602 | hv_store_ent (hv, k, v, 0); |
|
|
603 | SvREFCNT_dec (k); |
567 | } |
604 | } |
568 | } |
605 | } |
569 | else |
606 | else |
570 | { |
607 | { |
571 | int len = decode_uint (dec); |
608 | int len = decode_uint (dec); |
… | |
… | |
574 | { |
611 | { |
575 | SV *k = decode_sv (dec); |
612 | SV *k = decode_sv (dec); |
576 | SV *v = decode_sv (dec); |
613 | SV *v = decode_sv (dec); |
577 | |
614 | |
578 | hv_store_ent (hv, k, v, 0); |
615 | hv_store_ent (hv, k, v, 0); |
|
|
616 | SvREFCNT_dec (k); |
579 | } |
617 | } |
580 | } |
618 | } |
581 | |
619 | |
582 | DEC_DEC_DEPTH; |
620 | DEC_DEC_DEPTH; |
583 | return newRV_noinc ((SV *)hv); |
621 | return newRV_noinc ((SV *)hv); |
… | |
… | |
636 | decode_tagged (dec_t *dec) |
674 | decode_tagged (dec_t *dec) |
637 | { |
675 | { |
638 | UV tag = decode_uint (dec); |
676 | UV tag = decode_uint (dec); |
639 | SV *sv = decode_sv (dec); |
677 | SV *sv = decode_sv (dec); |
640 | |
678 | |
641 | if (tag == CBOR_TAG_MAGIC) // 2.4.5 Self-Describe CBOR |
679 | if (tag == CBOR_TAG_MAGIC) |
642 | return sv; |
680 | return sv; |
|
|
681 | else if (tag == CBOR_TAG_PERL_OBJECT) |
|
|
682 | { |
|
|
683 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
684 | ERR ("corrupted CBOR data (non-array perl object)"); |
643 | |
685 | |
|
|
686 | AV *av = (AV *)SvRV (sv); |
|
|
687 | int len = av_len (av) + 1; |
|
|
688 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
689 | |
|
|
690 | if (!stash) |
|
|
691 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
692 | |
|
|
693 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
694 | |
|
|
695 | if (!method) |
|
|
696 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
697 | |
|
|
698 | dSP; |
|
|
699 | |
|
|
700 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
701 | EXTEND (SP, len + 1); |
|
|
702 | // we re-bless the reference to get overload and other niceties right |
|
|
703 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
704 | PUSHs (sv_cbor); |
|
|
705 | |
|
|
706 | int i; |
|
|
707 | |
|
|
708 | for (i = 1; i < len; ++i) |
|
|
709 | PUSHs (*av_fetch (av, i, 1)); |
|
|
710 | |
|
|
711 | PUTBACK; |
|
|
712 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
713 | SPAGAIN; |
|
|
714 | |
|
|
715 | SvREFCNT_dec (sv); |
|
|
716 | sv = SvREFCNT_inc (POPs); |
|
|
717 | |
|
|
718 | PUTBACK; |
|
|
719 | |
|
|
720 | FREETMPS; LEAVE; |
|
|
721 | |
|
|
722 | return sv; |
|
|
723 | } |
|
|
724 | else |
|
|
725 | { |
644 | AV *av = newAV (); |
726 | AV *av = newAV (); |
645 | av_push (av, newSVuv (tag)); |
727 | av_push (av, newSVuv (tag)); |
646 | av_push (av, sv); |
728 | av_push (av, sv); |
647 | |
729 | |
648 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
730 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
649 | ? cbor_tagged_stash |
731 | ? cbor_tagged_stash |
650 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
732 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
651 | |
733 | |
652 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
734 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
735 | } |
|
|
736 | |
|
|
737 | fail: |
|
|
738 | SvREFCNT_dec (sv); |
|
|
739 | return &PL_sv_undef; |
653 | } |
740 | } |
654 | |
741 | |
655 | static SV * |
742 | static SV * |
656 | decode_sv (dec_t *dec) |
743 | decode_sv (dec_t *dec) |
657 | { |
744 | { |
… | |
… | |
676 | case 7: // misc |
763 | case 7: // misc |
677 | switch (*dec->cur++ & 31) |
764 | switch (*dec->cur++ & 31) |
678 | { |
765 | { |
679 | case 20: |
766 | case 20: |
680 | #if CBOR_SLOW |
767 | #if CBOR_SLOW |
681 | cbor_false = get_bool ("CBOR::XS::false"); |
768 | types_false = get_bool ("Types::Serialiser::false"); |
682 | #endif |
769 | #endif |
683 | return newSVsv (cbor_false); |
770 | return newSVsv (types_false); |
684 | case 21: |
771 | case 21: |
685 | #if CBOR_SLOW |
772 | #if CBOR_SLOW |
686 | cbor_true = get_bool ("CBOR::XS::true"); |
773 | types_true = get_bool ("Types::Serialiser::true"); |
687 | #endif |
774 | #endif |
688 | return newSVsv (cbor_true); |
775 | return newSVsv (types_true); |
689 | case 22: |
776 | case 22: |
690 | return newSVsv (&PL_sv_undef); |
777 | return newSVsv (&PL_sv_undef); |
|
|
778 | case 23: |
|
|
779 | #if CBOR_SLOW |
|
|
780 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
781 | #endif |
|
|
782 | return newSVsv (types_error); |
691 | |
783 | |
692 | case 25: |
784 | case 25: |
693 | { |
785 | { |
694 | WANT (2); |
786 | WANT (2); |
695 | |
787 | |
… | |
… | |
811 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
903 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
812 | |
904 | |
813 | BOOT: |
905 | BOOT: |
814 | { |
906 | { |
815 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
907 | 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); |
908 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
818 | |
909 | |
819 | cbor_true = get_bool ("CBOR::XS::true"); |
910 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
820 | cbor_false = get_bool ("CBOR::XS::false"); |
911 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
912 | |
|
|
913 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
914 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
915 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
916 | |
|
|
917 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
918 | SvREADONLY_on (sv_cbor); |
821 | } |
919 | } |
822 | |
920 | |
823 | PROTOTYPES: DISABLE |
921 | PROTOTYPES: DISABLE |
824 | |
922 | |
825 | void CLONE (...) |
923 | void CLONE (...) |
826 | CODE: |
924 | CODE: |
827 | cbor_stash = 0; |
925 | cbor_stash = 0; |
828 | cbor_boolean_stash = 0; |
|
|
829 | cbor_tagged_stash = 0; |
926 | cbor_tagged_stash = 0; |
|
|
927 | types_error_stash = 0; |
|
|
928 | types_boolean_stash = 0; |
830 | |
929 | |
831 | void new (char *klass) |
930 | void new (char *klass) |
832 | PPCODE: |
931 | PPCODE: |
833 | { |
932 | { |
834 | SV *pv = NEWSV (0, sizeof (CBOR)); |
933 | SV *pv = NEWSV (0, sizeof (CBOR)); |