… | |
… | |
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 | // known tags |
|
|
15 | enum cbor_tag |
|
|
16 | { |
|
|
17 | // inofficial extensions (pending iana registration) |
|
|
18 | CBOR_TAG_PERL_OBJECT = 256, |
|
|
19 | CBOR_TAG_GENERIC_OBJECT = 257, |
|
|
20 | |
|
|
21 | // rfc7049 |
|
|
22 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
|
|
23 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
|
|
24 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
|
|
25 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
|
|
26 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
|
|
27 | CBOR_TAG_BIGFLOAT = 5, // array |
|
|
28 | |
|
|
29 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
|
|
30 | CBOR_TAG_CONV_B64 = 22, // base64, any |
|
|
31 | CBOR_TAG_CONV_HEX = 23, // base16, any |
|
|
32 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
|
|
33 | |
|
|
34 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
|
|
35 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
|
|
36 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
|
|
37 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
|
|
38 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
|
|
39 | |
|
|
40 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
|
|
41 | }; |
13 | |
42 | |
14 | #define F_SHRINK 0x00000200UL |
43 | #define F_SHRINK 0x00000200UL |
15 | #define F_ALLOW_UNKNOWN 0x00002000UL |
44 | #define F_ALLOW_UNKNOWN 0x00002000UL |
16 | |
45 | |
17 | #define INIT_SIZE 32 // initial scalar size to be allocated |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
31 | #else |
60 | #else |
32 | # define CBOR_SLOW 0 |
61 | # define CBOR_SLOW 0 |
33 | # define CBOR_STASH cbor_stash |
62 | # define CBOR_STASH cbor_stash |
34 | #endif |
63 | #endif |
35 | |
64 | |
36 | 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:: |
37 | static SV *cbor_true, *cbor_false; |
66 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
38 | |
67 | |
39 | typedef struct { |
68 | typedef struct { |
40 | U32 flags; |
69 | U32 flags; |
41 | U32 max_depth; |
70 | U32 max_depth; |
42 | STRLEN max_size; |
71 | STRLEN max_size; |
… | |
… | |
230 | SvGETMAGIC (sv); |
259 | SvGETMAGIC (sv); |
231 | svt = SvTYPE (sv); |
260 | svt = SvTYPE (sv); |
232 | |
261 | |
233 | if (ecb_expect_false (SvOBJECT (sv))) |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
234 | { |
263 | { |
235 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
264 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
236 | ? cbor_boolean_stash |
265 | ? types_boolean_stash |
237 | : 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); |
238 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
239 | ? cbor_tagged_stash |
271 | ? cbor_tagged_stash |
240 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
241 | |
273 | |
|
|
274 | HV *stash = SvSTASH (sv); |
|
|
275 | GV *method; |
|
|
276 | |
242 | if (SvSTASH (sv) == boolean_stash) |
277 | if (stash == boolean_stash) |
243 | 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); |
244 | else if (SvSTASH (sv) == tagged_stash) |
281 | else if (stash == tagged_stash) |
245 | { |
282 | { |
246 | if (svt != SVt_PVAV) |
283 | if (svt != SVt_PVAV) |
247 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
284 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
248 | |
285 | |
249 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
286 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
250 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
287 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
251 | } |
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 | } |
252 | else |
341 | else |
253 | { |
|
|
254 | // we re-bless the reference to get overload and other niceties right |
|
|
255 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
256 | |
|
|
257 | if (to_cbor) |
|
|
258 | { |
|
|
259 | dSP; |
|
|
260 | |
|
|
261 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
262 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
263 | |
|
|
264 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
265 | PUTBACK; |
|
|
266 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
267 | SPAGAIN; |
|
|
268 | |
|
|
269 | // catch this surprisingly common error |
|
|
270 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
271 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
272 | |
|
|
273 | sv = POPs; |
|
|
274 | PUTBACK; |
|
|
275 | |
|
|
276 | encode_sv (enc, sv); |
|
|
277 | |
|
|
278 | FREETMPS; LEAVE; |
|
|
279 | } |
|
|
280 | else |
|
|
281 | 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", |
282 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
343 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
283 | } |
|
|
284 | } |
344 | } |
285 | else if (svt == SVt_PVHV) |
345 | else if (svt == SVt_PVHV) |
286 | encode_hv (enc, (HV *)sv); |
346 | encode_hv (enc, (HV *)sv); |
287 | else if (svt == SVt_PVAV) |
347 | else if (svt == SVt_PVAV) |
288 | encode_av (enc, (AV *)sv); |
348 | encode_av (enc, (AV *)sv); |
… | |
… | |
612 | decode_tagged (dec_t *dec) |
672 | decode_tagged (dec_t *dec) |
613 | { |
673 | { |
614 | UV tag = decode_uint (dec); |
674 | UV tag = decode_uint (dec); |
615 | SV *sv = decode_sv (dec); |
675 | SV *sv = decode_sv (dec); |
616 | |
676 | |
617 | if (tag == 55799) // 2.4.5 Self-Describe CBOR |
677 | if (tag == CBOR_TAG_MAGIC) |
618 | 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)"); |
619 | |
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 | { |
620 | AV *av = newAV (); |
723 | AV *av = newAV (); |
621 | av_push (av, newSVuv (tag)); |
724 | av_push (av, newSVuv (tag)); |
622 | av_push (av, sv); |
725 | av_push (av, sv); |
623 | return newRV_noinc ((SV *)av); |
726 | |
|
|
727 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
728 | ? cbor_tagged_stash |
|
|
729 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
730 | |
|
|
731 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
732 | } |
|
|
733 | |
|
|
734 | fail: |
|
|
735 | SvREFCNT_dec (sv); |
|
|
736 | return &PL_sv_undef; |
624 | } |
737 | } |
625 | |
738 | |
626 | static SV * |
739 | static SV * |
627 | decode_sv (dec_t *dec) |
740 | decode_sv (dec_t *dec) |
628 | { |
741 | { |
… | |
… | |
647 | case 7: // misc |
760 | case 7: // misc |
648 | switch (*dec->cur++ & 31) |
761 | switch (*dec->cur++ & 31) |
649 | { |
762 | { |
650 | case 20: |
763 | case 20: |
651 | #if CBOR_SLOW |
764 | #if CBOR_SLOW |
652 | cbor_false = get_bool ("CBOR::XS::false"); |
765 | types_false = get_bool ("Types::Serialiser::false"); |
653 | #endif |
766 | #endif |
654 | return newSVsv (cbor_false); |
767 | return newSVsv (types_false); |
655 | case 21: |
768 | case 21: |
656 | #if CBOR_SLOW |
769 | #if CBOR_SLOW |
657 | cbor_true = get_bool ("CBOR::XS::true"); |
770 | types_true = get_bool ("Types::Serialiser::true"); |
658 | #endif |
771 | #endif |
659 | return newSVsv (cbor_true); |
772 | return newSVsv (types_true); |
660 | case 22: |
773 | case 22: |
661 | 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); |
662 | |
780 | |
663 | case 25: |
781 | case 25: |
664 | { |
782 | { |
665 | WANT (2); |
783 | WANT (2); |
666 | |
784 | |
… | |
… | |
782 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
900 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
783 | |
901 | |
784 | BOOT: |
902 | BOOT: |
785 | { |
903 | { |
786 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
904 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
787 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
788 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
905 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
789 | |
906 | |
790 | cbor_true = get_bool ("CBOR::XS::true"); |
907 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
791 | 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); |
792 | } |
916 | } |
793 | |
917 | |
794 | PROTOTYPES: DISABLE |
918 | PROTOTYPES: DISABLE |
795 | |
919 | |
796 | void CLONE (...) |
920 | void CLONE (...) |
797 | CODE: |
921 | CODE: |
798 | cbor_stash = 0; |
922 | cbor_stash = 0; |
799 | cbor_boolean_stash = 0; |
|
|
800 | cbor_tagged_stash = 0; |
923 | cbor_tagged_stash = 0; |
|
|
924 | types_error_stash = 0; |
|
|
925 | types_boolean_stash = 0; |
801 | |
926 | |
802 | void new (char *klass) |
927 | void new (char *klass) |
803 | PPCODE: |
928 | PPCODE: |
804 | { |
929 | { |
805 | SV *pv = NEWSV (0, sizeof (CBOR)); |
930 | SV *pv = NEWSV (0, sizeof (CBOR)); |