… | |
… | |
24 | |
24 | |
25 | // known tags |
25 | // known tags |
26 | enum cbor_tag |
26 | enum cbor_tag |
27 | { |
27 | { |
28 | // inofficial extensions (pending iana registration) |
28 | // inofficial extensions (pending iana registration) |
29 | CBOR_TAG_PERL_OBJECT = 256, |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 257, |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
|
|
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
|
|
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
|
|
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
|
|
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
31 | |
36 | |
32 | // rfc7049 |
37 | // rfc7049 |
33 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
34 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
35 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
… | |
… | |
49 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
50 | |
55 | |
51 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
52 | }; |
57 | }; |
53 | |
58 | |
54 | #define F_SHRINK 0x00000200UL |
59 | #define F_SHRINK 0x00000001UL |
55 | #define F_ALLOW_UNKNOWN 0x00002000UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
|
|
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
|
|
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
|
|
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
56 | |
64 | |
57 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
58 | |
66 | |
59 | #define SB do { |
67 | #define SB do { |
60 | #define SE } while (0) |
68 | #define SE } while (0) |
… | |
… | |
117 | #endif |
125 | #endif |
118 | } |
126 | } |
119 | } |
127 | } |
120 | |
128 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
129 | ///////////////////////////////////////////////////////////////////////////// |
122 | // fp hell |
|
|
123 | |
|
|
124 | //TODO |
|
|
125 | |
|
|
126 | ///////////////////////////////////////////////////////////////////////////// |
|
|
127 | // encoder |
130 | // encoder |
128 | |
131 | |
129 | // structure used for encoding CBOR |
132 | // structure used for encoding CBOR |
130 | typedef struct |
133 | typedef struct |
131 | { |
134 | { |
132 | char *cur; // SvPVX (sv) + current output position |
135 | char *cur; // SvPVX (sv) + current output position |
133 | char *end; // SvEND (sv) |
136 | char *end; // SvEND (sv) |
134 | SV *sv; // result scalar |
137 | SV *sv; // result scalar |
135 | CBOR cbor; |
138 | CBOR cbor; |
136 | U32 depth; // recursion level |
139 | U32 depth; // recursion level |
|
|
140 | HV *stringref; // string => index, or 0 |
|
|
141 | HV *shareable; // ptr => index, or 0 |
|
|
142 | UV shareable_idx; |
137 | } enc_t; |
143 | } enc_t; |
138 | |
144 | |
139 | ecb_inline void |
145 | ecb_inline void |
140 | need (enc_t *enc, STRLEN len) |
146 | need (enc_t *enc, STRLEN len) |
141 | { |
147 | { |
… | |
… | |
202 | need (enc, len); |
208 | need (enc, len); |
203 | memcpy (enc->cur, str, len); |
209 | memcpy (enc->cur, str, len); |
204 | enc->cur += len; |
210 | enc->cur += len; |
205 | } |
211 | } |
206 | |
212 | |
|
|
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
207 | static void encode_sv (enc_t *enc, SV *sv); |
219 | static void encode_sv (enc_t *enc, SV *sv); |
208 | |
220 | |
209 | static void |
221 | static void |
210 | encode_av (enc_t *enc, AV *av) |
222 | encode_av (enc_t *enc, AV *av) |
211 | { |
223 | { |
… | |
… | |
263 | |
275 | |
264 | // encode objects, arrays and special \0=false and \1=true values. |
276 | // encode objects, arrays and special \0=false and \1=true values. |
265 | static void |
277 | static void |
266 | encode_rv (enc_t *enc, SV *sv) |
278 | encode_rv (enc_t *enc, SV *sv) |
267 | { |
279 | { |
268 | svtype svt; |
|
|
269 | |
|
|
270 | SvGETMAGIC (sv); |
280 | SvGETMAGIC (sv); |
|
|
281 | |
|
|
282 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
283 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
284 | { |
|
|
285 | if (!enc->shareable) |
|
|
286 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
287 | |
|
|
288 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
289 | |
|
|
290 | if (SvOK (*svp)) |
|
|
291 | { |
|
|
292 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
293 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
294 | return; |
|
|
295 | } |
|
|
296 | else |
|
|
297 | { |
|
|
298 | sv_setuv (*svp, enc->shareable_idx); |
|
|
299 | ++enc->shareable_idx; |
|
|
300 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
271 | svt = SvTYPE (sv); |
304 | svtype svt = SvTYPE (sv); |
272 | |
305 | |
273 | if (ecb_expect_false (SvOBJECT (sv))) |
306 | if (ecb_expect_false (SvOBJECT (sv))) |
274 | { |
307 | { |
275 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
308 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
276 | ? types_boolean_stash |
309 | ? types_boolean_stash |
… | |
… | |
336 | |
369 | |
337 | // catch this surprisingly common error |
370 | // catch this surprisingly common error |
338 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
371 | 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)); |
372 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
340 | |
373 | |
341 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
374 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
342 | encode_uint (enc, 0x80, count + 1); |
375 | encode_uint (enc, 0x80, count + 1); |
343 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
376 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
344 | |
377 | |
345 | while (count) |
378 | while (count) |
346 | encode_sv (enc, SP[1 - count--]); |
379 | encode_sv (enc, SP[1 - count--]); |
… | |
… | |
355 | } |
388 | } |
356 | else if (svt == SVt_PVHV) |
389 | else if (svt == SVt_PVHV) |
357 | encode_hv (enc, (HV *)sv); |
390 | encode_hv (enc, (HV *)sv); |
358 | else if (svt == SVt_PVAV) |
391 | else if (svt == SVt_PVAV) |
359 | encode_av (enc, (AV *)sv); |
392 | encode_av (enc, (AV *)sv); |
360 | else if (svt < SVt_PVAV) |
|
|
361 | { |
|
|
362 | STRLEN len = 0; |
|
|
363 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
364 | |
|
|
365 | if (len == 1 && *pv == '1') |
|
|
366 | encode_ch (enc, 0xe0 | 21); |
|
|
367 | else if (len == 1 && *pv == '0') |
|
|
368 | encode_ch (enc, 0xe0 | 20); |
|
|
369 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
370 | encode_ch (enc, 0xe0 | 23); |
|
|
371 | else |
|
|
372 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
373 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
374 | } |
|
|
375 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
376 | encode_ch (enc, 0xe0 | 23); |
|
|
377 | else |
393 | else |
378 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
394 | { |
379 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
395 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
396 | encode_sv (enc, sv); |
|
|
397 | } |
380 | } |
398 | } |
381 | |
399 | |
382 | static void |
400 | static void |
383 | encode_nv (enc_t *enc, SV *sv) |
401 | encode_nv (enc_t *enc, SV *sv) |
384 | { |
402 | { |
… | |
… | |
449 | } |
467 | } |
450 | |
468 | |
451 | static SV * |
469 | static SV * |
452 | encode_cbor (SV *scalar, CBOR *cbor) |
470 | encode_cbor (SV *scalar, CBOR *cbor) |
453 | { |
471 | { |
454 | enc_t enc; |
472 | enc_t enc = { }; |
455 | |
473 | |
456 | enc.cbor = *cbor; |
474 | enc.cbor = *cbor; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
475 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
476 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
477 | enc.end = SvEND (enc.sv); |
460 | enc.depth = 0; |
|
|
461 | |
478 | |
462 | SvPOK_only (enc.sv); |
479 | SvPOK_only (enc.sv); |
463 | encode_sv (&enc, scalar); |
480 | encode_sv (&enc, scalar); |
464 | |
481 | |
465 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
482 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
481 | U8 *end; // end of input string |
498 | U8 *end; // end of input string |
482 | const char *err; // parse error, if != 0 |
499 | const char *err; // parse error, if != 0 |
483 | CBOR cbor; |
500 | CBOR cbor; |
484 | U32 depth; // recursion depth |
501 | U32 depth; // recursion depth |
485 | U32 maxdepth; // recursion depth limit |
502 | U32 maxdepth; // recursion depth limit |
|
|
503 | AV *shareable; |
486 | } dec_t; |
504 | } dec_t; |
487 | |
505 | |
488 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
506 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
489 | |
507 | |
490 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
508 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
584 | SvREFCNT_dec (av); |
602 | SvREFCNT_dec (av); |
585 | DEC_DEC_DEPTH; |
603 | DEC_DEC_DEPTH; |
586 | return &PL_sv_undef; |
604 | return &PL_sv_undef; |
587 | } |
605 | } |
588 | |
606 | |
|
|
607 | static void |
|
|
608 | decode_he (dec_t *dec, HV *hv) |
|
|
609 | { |
|
|
610 | // for speed reasons, we specialcase single-string |
|
|
611 | // byte or utf-8 strings as keys. |
|
|
612 | |
|
|
613 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
|
|
614 | { |
|
|
615 | I32 len = decode_uint (dec); |
|
|
616 | char *key = (char *)dec->cur; |
|
|
617 | |
|
|
618 | dec->cur += len; |
|
|
619 | |
|
|
620 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
621 | } |
|
|
622 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
|
|
623 | { |
|
|
624 | I32 len = decode_uint (dec); |
|
|
625 | char *key = (char *)dec->cur; |
|
|
626 | |
|
|
627 | dec->cur += len; |
|
|
628 | |
|
|
629 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
630 | } |
|
|
631 | else |
|
|
632 | { |
|
|
633 | SV *k = decode_sv (dec); |
|
|
634 | SV *v = decode_sv (dec); |
|
|
635 | |
|
|
636 | hv_store_ent (hv, k, v, 0); |
|
|
637 | SvREFCNT_dec (k); |
|
|
638 | } |
|
|
639 | } |
|
|
640 | |
589 | static SV * |
641 | static SV * |
590 | decode_hv (dec_t *dec) |
642 | decode_hv (dec_t *dec) |
591 | { |
643 | { |
592 | HV *hv = newHV (); |
644 | HV *hv = newHV (); |
593 | |
645 | |
… | |
… | |
605 | { |
657 | { |
606 | ++dec->cur; |
658 | ++dec->cur; |
607 | break; |
659 | break; |
608 | } |
660 | } |
609 | |
661 | |
610 | SV *k = decode_sv (dec); |
662 | decode_he (dec, hv); |
611 | SV *v = decode_sv (dec); |
|
|
612 | |
|
|
613 | hv_store_ent (hv, k, v, 0); |
|
|
614 | SvREFCNT_dec (k); |
|
|
615 | } |
663 | } |
616 | } |
664 | } |
617 | else |
665 | else |
618 | { |
666 | { |
619 | int len = decode_uint (dec); |
667 | int pairs = decode_uint (dec); |
620 | |
668 | |
621 | while (len--) |
669 | while (pairs--) |
622 | { |
670 | decode_he (dec, hv); |
623 | SV *k = decode_sv (dec); |
|
|
624 | SV *v = decode_sv (dec); |
|
|
625 | |
|
|
626 | hv_store_ent (hv, k, v, 0); |
|
|
627 | SvREFCNT_dec (k); |
|
|
628 | } |
|
|
629 | } |
671 | } |
630 | |
672 | |
631 | DEC_DEC_DEPTH; |
673 | DEC_DEC_DEPTH; |
632 | return newRV_noinc ((SV *)hv); |
674 | return newRV_noinc ((SV *)hv); |
633 | |
675 | |
… | |
… | |
682 | } |
724 | } |
683 | |
725 | |
684 | static SV * |
726 | static SV * |
685 | decode_tagged (dec_t *dec) |
727 | decode_tagged (dec_t *dec) |
686 | { |
728 | { |
|
|
729 | SV *sv = 0; |
687 | UV tag = decode_uint (dec); |
730 | UV tag = decode_uint (dec); |
|
|
731 | |
|
|
732 | WANT (1); |
|
|
733 | |
|
|
734 | switch (tag) |
|
|
735 | { |
|
|
736 | case CBOR_TAG_MAGIC: |
|
|
737 | return decode_sv (dec); |
|
|
738 | |
|
|
739 | case CBOR_TAG_INDIRECTION: |
|
|
740 | return newRV_noinc (decode_sv (dec)); |
|
|
741 | |
|
|
742 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
743 | { |
|
|
744 | if (ecb_expect_false (!dec->shareable)) |
|
|
745 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
746 | |
|
|
747 | sv = newSV (0); |
|
|
748 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
749 | |
688 | SV *sv = decode_sv (dec); |
750 | SV *osv = decode_sv (dec); |
|
|
751 | sv_setsv (sv, osv); |
|
|
752 | SvREFCNT_dec_NN (osv); |
|
|
753 | } |
689 | |
754 | |
690 | if (tag == CBOR_TAG_MAGIC) |
|
|
691 | return sv; |
755 | return sv; |
692 | else if (tag == CBOR_TAG_PERL_OBJECT) |
756 | |
693 | { |
757 | case CBOR_TAG_VALUE_SHAREDREF: |
|
|
758 | { |
|
|
759 | if ((*dec->cur >> 5) != 0) |
|
|
760 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
|
|
761 | |
|
|
762 | UV idx = decode_uint (dec); |
|
|
763 | |
|
|
764 | if (!dec->shareable || idx > AvFILLp (dec->shareable)) |
|
|
765 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
766 | |
|
|
767 | return SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
768 | } |
|
|
769 | |
|
|
770 | case CBOR_TAG_PERL_OBJECT: |
|
|
771 | { |
|
|
772 | sv = decode_sv (dec); |
|
|
773 | |
694 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
774 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
695 | ERR ("corrupted CBOR data (non-array perl object)"); |
775 | ERR ("corrupted CBOR data (non-array perl object)"); |
696 | |
776 | |
697 | AV *av = (AV *)SvRV (sv); |
777 | AV *av = (AV *)SvRV (sv); |
698 | int len = av_len (av) + 1; |
778 | int len = av_len (av) + 1; |
699 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
779 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
700 | |
780 | |
701 | if (!stash) |
781 | if (!stash) |
702 | ERR ("cannot decode perl-object (package does not exist)"); |
782 | ERR ("cannot decode perl-object (package does not exist)"); |
703 | |
783 | |
704 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
784 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
705 | |
785 | |
706 | if (!method) |
786 | if (!method) |
707 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
787 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
708 | |
788 | |
709 | dSP; |
789 | dSP; |
710 | |
790 | |
711 | ENTER; SAVETMPS; PUSHMARK (SP); |
791 | ENTER; SAVETMPS; PUSHMARK (SP); |
712 | EXTEND (SP, len + 1); |
792 | EXTEND (SP, len + 1); |
713 | // we re-bless the reference to get overload and other niceties right |
793 | // we re-bless the reference to get overload and other niceties right |
714 | PUSHs (*av_fetch (av, 0, 1)); |
794 | PUSHs (*av_fetch (av, 0, 1)); |
715 | PUSHs (sv_cbor); |
795 | PUSHs (sv_cbor); |
716 | |
796 | |
717 | int i; |
797 | int i; |
718 | |
798 | |
719 | for (i = 1; i < len; ++i) |
799 | for (i = 1; i < len; ++i) |
720 | PUSHs (*av_fetch (av, i, 1)); |
800 | PUSHs (*av_fetch (av, i, 1)); |
721 | |
801 | |
722 | PUTBACK; |
802 | PUTBACK; |
723 | call_sv ((SV *)GvCV (method), G_SCALAR); |
803 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
724 | SPAGAIN; |
804 | SPAGAIN; |
725 | |
805 | |
|
|
806 | if (SvTRUE (ERRSV)) |
|
|
807 | { |
|
|
808 | FREETMPS; LEAVE; |
|
|
809 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
810 | } |
|
|
811 | |
726 | SvREFCNT_dec (sv); |
812 | SvREFCNT_dec (sv); |
727 | sv = SvREFCNT_inc (POPs); |
813 | sv = SvREFCNT_inc (POPs); |
728 | |
814 | |
729 | PUTBACK; |
815 | PUTBACK; |
730 | |
816 | |
731 | FREETMPS; LEAVE; |
817 | FREETMPS; LEAVE; |
732 | |
818 | |
733 | return sv; |
819 | return sv; |
734 | } |
820 | } |
735 | else |
821 | |
736 | { |
822 | default: |
|
|
823 | { |
|
|
824 | sv = decode_sv (dec); |
|
|
825 | |
737 | AV *av = newAV (); |
826 | AV *av = newAV (); |
738 | av_push (av, newSVuv (tag)); |
827 | av_push (av, newSVuv (tag)); |
739 | av_push (av, sv); |
828 | av_push (av, sv); |
740 | |
829 | |
741 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
830 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
742 | ? cbor_tagged_stash |
831 | ? cbor_tagged_stash |
743 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
832 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
744 | |
833 | |
745 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
834 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
835 | } |
746 | } |
836 | } |
747 | |
837 | |
748 | fail: |
838 | fail: |
749 | SvREFCNT_dec (sv); |
839 | SvREFCNT_dec (sv); |
750 | return &PL_sv_undef; |
840 | return &PL_sv_undef; |
… | |
… | |
842 | } |
932 | } |
843 | |
933 | |
844 | static SV * |
934 | static SV * |
845 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
935 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
846 | { |
936 | { |
847 | dec_t dec; |
937 | dec_t dec = { }; |
848 | SV *sv; |
938 | SV *sv; |
|
|
939 | STRLEN len; |
|
|
940 | char *data = SvPVbyte (string, len); |
849 | |
941 | |
850 | /* work around bugs in 5.10 where manipulating magic values |
|
|
851 | * makes perl ignore the magic in subsequent accesses. |
|
|
852 | * also make a copy of non-PV values, to get them into a clean |
|
|
853 | * state (SvPV should do that, but it's buggy, see below). |
|
|
854 | */ |
|
|
855 | /*SvGETMAGIC (string);*/ |
|
|
856 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
857 | string = sv_2mortal (newSVsv (string)); |
|
|
858 | |
|
|
859 | SvUPGRADE (string, SVt_PV); |
|
|
860 | |
|
|
861 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
862 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
863 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
864 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
865 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
866 | * and hope for the best. |
|
|
867 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
868 | * assertion business is seriously broken, try yet another workaround |
|
|
869 | * for the broken -DDEBUGGING. |
|
|
870 | */ |
|
|
871 | { |
|
|
872 | #ifdef DEBUGGING |
|
|
873 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
874 | #else |
|
|
875 | STRLEN offset = SvCUR (string); |
|
|
876 | #endif |
|
|
877 | |
|
|
878 | if (offset > cbor->max_size && cbor->max_size) |
942 | if (len > cbor->max_size && cbor->max_size) |
879 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
943 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
880 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
944 | (unsigned long)len, (unsigned long)cbor->max_size); |
881 | } |
|
|
882 | |
|
|
883 | sv_utf8_downgrade (string, 0); |
|
|
884 | |
945 | |
885 | dec.cbor = *cbor; |
946 | dec.cbor = *cbor; |
886 | dec.cur = (U8 *)SvPVX (string); |
947 | dec.cur = (U8 *)data; |
887 | dec.end = (U8 *)SvEND (string); |
948 | dec.end = (U8 *)data + len; |
888 | dec.err = 0; |
|
|
889 | dec.depth = 0; |
|
|
890 | |
949 | |
891 | sv = decode_sv (&dec); |
950 | sv = decode_sv (&dec); |
892 | |
951 | |
893 | if (offset_return) |
952 | if (offset_return) |
894 | *offset_return = dec.cur; |
953 | *offset_return = dec.cur; |
… | |
… | |
898 | dec.err = "garbage after CBOR object"; |
957 | dec.err = "garbage after CBOR object"; |
899 | |
958 | |
900 | if (dec.err) |
959 | if (dec.err) |
901 | { |
960 | { |
902 | SvREFCNT_dec (sv); |
961 | SvREFCNT_dec (sv); |
903 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
962 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
904 | } |
963 | } |
905 | |
964 | |
906 | sv = sv_2mortal (sv); |
965 | sv = sv_2mortal (sv); |
907 | |
966 | |
908 | return sv; |
967 | return sv; |
… | |
… | |
952 | |
1011 | |
953 | void shrink (CBOR *self, int enable = 1) |
1012 | void shrink (CBOR *self, int enable = 1) |
954 | ALIAS: |
1013 | ALIAS: |
955 | shrink = F_SHRINK |
1014 | shrink = F_SHRINK |
956 | allow_unknown = F_ALLOW_UNKNOWN |
1015 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1016 | allow_sharing = F_ALLOW_SHARING |
|
|
1017 | dedup_keys = F_DEDUP_KEYS |
|
|
1018 | dedup_strings = F_DEDUP_STRINGS |
957 | PPCODE: |
1019 | PPCODE: |
958 | { |
1020 | { |
959 | if (enable) |
1021 | if (enable) |
960 | self->flags |= ix; |
1022 | self->flags |= ix; |
961 | else |
1023 | else |
… | |
… | |
966 | |
1028 | |
967 | void get_shrink (CBOR *self) |
1029 | void get_shrink (CBOR *self) |
968 | ALIAS: |
1030 | ALIAS: |
969 | get_shrink = F_SHRINK |
1031 | get_shrink = F_SHRINK |
970 | get_allow_unknown = F_ALLOW_UNKNOWN |
1032 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1033 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1034 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1035 | get_dedup_strings = F_DEDUP_STRINGS |
971 | PPCODE: |
1036 | PPCODE: |
972 | XPUSHs (boolSV (self->flags & ix)); |
1037 | XPUSHs (boolSV (self->flags & ix)); |
973 | |
1038 | |
974 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1039 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
975 | PPCODE: |
1040 | PPCODE: |
… | |
… | |
990 | int get_max_size (CBOR *self) |
1055 | int get_max_size (CBOR *self) |
991 | CODE: |
1056 | CODE: |
992 | RETVAL = self->max_size; |
1057 | RETVAL = self->max_size; |
993 | OUTPUT: |
1058 | OUTPUT: |
994 | RETVAL |
1059 | RETVAL |
995 | |
|
|
996 | #if 0 //TODO |
|
|
997 | |
|
|
998 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
999 | PPCODE: |
|
|
1000 | { |
|
|
1001 | SvREFCNT_dec (self->cb_object); |
|
|
1002 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1003 | |
|
|
1004 | XPUSHs (ST (0)); |
|
|
1005 | } |
|
|
1006 | |
|
|
1007 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1008 | PPCODE: |
|
|
1009 | { |
|
|
1010 | if (!self->cb_sk_object) |
|
|
1011 | self->cb_sk_object = newHV (); |
|
|
1012 | |
|
|
1013 | if (SvOK (cb)) |
|
|
1014 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1015 | else |
|
|
1016 | { |
|
|
1017 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1018 | |
|
|
1019 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1020 | { |
|
|
1021 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1022 | self->cb_sk_object = 0; |
|
|
1023 | } |
|
|
1024 | } |
|
|
1025 | |
|
|
1026 | XPUSHs (ST (0)); |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | #endif |
|
|
1030 | |
1060 | |
1031 | void encode (CBOR *self, SV *scalar) |
1061 | void encode (CBOR *self, SV *scalar) |
1032 | PPCODE: |
1062 | PPCODE: |
1033 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1063 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1034 | XPUSHs (scalar); |
1064 | XPUSHs (scalar); |
… | |
… | |
1047 | EXTEND (SP, 2); |
1077 | EXTEND (SP, 2); |
1048 | PUSHs (sv); |
1078 | PUSHs (sv); |
1049 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1079 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1050 | } |
1080 | } |
1051 | |
1081 | |
1052 | #if 0 |
|
|
1053 | |
|
|
1054 | void DESTROY (CBOR *self) |
|
|
1055 | CODE: |
|
|
1056 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1057 | SvREFCNT_dec (self->cb_object); |
|
|
1058 | |
|
|
1059 | #endif |
|
|
1060 | |
|
|
1061 | PROTOTYPES: ENABLE |
1082 | PROTOTYPES: ENABLE |
1062 | |
1083 | |
1063 | void encode_cbor (SV *scalar) |
1084 | void encode_cbor (SV *scalar) |
1064 | PPCODE: |
1085 | PPCODE: |
1065 | { |
1086 | { |