… | |
… | |
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 = 24, // http://cbor.schmorp.de/perl-object |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // 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 |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
34 | CBOR_TAG_STRINGREF = 28, // 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 |
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
36 | |
… | |
… | |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
55 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
57 | }; |
57 | }; |
58 | |
58 | |
59 | #define F_SHRINK 0x00000001UL |
59 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
62 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
|
|
64 | |
63 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
65 | |
67 | #define SB do { |
66 | #define SB do { |
68 | #define SE } while (0) |
67 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
79 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
80 | # define CBOR_STASH cbor_stash |
82 | #endif |
81 | #endif |
83 | |
82 | |
84 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
83 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
85 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
85 | |
87 | typedef struct { |
86 | typedef struct { |
88 | U32 flags; |
87 | U32 flags; |
89 | U32 max_depth; |
88 | U32 max_depth; |
90 | STRLEN max_size; |
89 | STRLEN max_size; |
|
|
90 | SV *filter; |
91 | } CBOR; |
91 | } CBOR; |
92 | |
92 | |
93 | ecb_inline void |
93 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
94 | cbor_init (CBOR *cbor) |
95 | { |
95 | { |
96 | Zero (cbor, 1, CBOR); |
96 | Zero (cbor, 1, CBOR); |
97 | cbor->max_depth = 512; |
97 | cbor->max_depth = 512; |
|
|
98 | } |
|
|
99 | |
|
|
100 | ecb_inline void |
|
|
101 | cbor_free (CBOR *cbor) |
|
|
102 | { |
|
|
103 | SvREFCNT_dec (cbor->filter); |
98 | } |
104 | } |
99 | |
105 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
106 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
107 | // utility functions |
102 | |
108 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
130 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
131 | #endif |
126 | } |
132 | } |
127 | } |
133 | } |
128 | |
134 | |
|
|
135 | // minimum length of a string to be registered for stringref |
|
|
136 | ecb_inline int |
|
|
137 | minimum_string_length (UV idx) |
|
|
138 | { |
|
|
139 | return idx > 23 |
|
|
140 | ? idx > 0xffU |
|
|
141 | ? idx > 0xffffU |
|
|
142 | ? idx > 0xffffffffU |
|
|
143 | ? 7 |
|
|
144 | : 6 |
|
|
145 | : 5 |
|
|
146 | : 4 |
|
|
147 | : 3; |
|
|
148 | } |
|
|
149 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
150 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
151 | // encoder |
131 | |
152 | |
132 | // structure used for encoding CBOR |
153 | // structure used for encoding CBOR |
133 | typedef struct |
154 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
156 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
157 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
158 | SV *sv; // result scalar |
138 | CBOR cbor; |
159 | CBOR cbor; |
139 | U32 depth; // recursion level |
160 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
161 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
162 | UV stringref_idx; |
141 | HV *sharable; // ptr => index, or 0 |
163 | HV *shareable; // ptr => index, or 0 |
142 | HV *sharable_idx; |
164 | UV shareable_idx; |
143 | } enc_t; |
165 | } enc_t; |
144 | |
166 | |
145 | ecb_inline void |
167 | ecb_inline void |
146 | need (enc_t *enc, STRLEN len) |
168 | need (enc_t *enc, STRLEN len) |
147 | { |
169 | { |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
221 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
222 | *enc->cur++ = len; |
201 | } |
223 | } |
202 | } |
224 | } |
203 | |
225 | |
|
|
226 | ecb_inline void |
|
|
227 | encode_tag (enc_t *enc, UV tag) |
|
|
228 | { |
|
|
229 | encode_uint (enc, 0xc0, tag); |
|
|
230 | } |
|
|
231 | |
204 | static void |
232 | static void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
234 | { |
|
|
235 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
|
|
236 | { |
|
|
237 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
238 | |
|
|
239 | if (SvOK (*svp)) |
|
|
240 | { |
|
|
241 | // already registered, use stringref |
|
|
242 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
243 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
244 | return; |
|
|
245 | } |
|
|
246 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
247 | { |
|
|
248 | // register only |
|
|
249 | sv_setuv (*svp, enc->stringref_idx); |
|
|
250 | ++enc->stringref_idx; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
208 | need (enc, len); |
255 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
256 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
257 | enc->cur += len; |
211 | } |
258 | } |
212 | |
259 | |
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static int |
|
|
220 | encode_sharable2 (enc_t *enc, SV *sv) |
|
|
221 | { |
|
|
222 | if (!enc->sharable) |
|
|
223 | enc->sharable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
224 | |
|
|
225 | SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1); |
|
|
226 | |
|
|
227 | if (SvOK (*svp)) |
|
|
228 | { |
|
|
229 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
230 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
231 | |
|
|
232 | return 1; |
|
|
233 | } |
|
|
234 | else |
|
|
235 | { |
|
|
236 | sv_setuv (*svp, enc->sharable_idx++); |
|
|
237 | encode_tag (enc, CBOR_TAG_VALUE_SHARABLE); |
|
|
238 | |
|
|
239 | return 0; |
|
|
240 | } |
|
|
241 | } |
|
|
242 | |
|
|
243 | ecb_inline int |
|
|
244 | encode_sharable (enc_t *enc, SV *sv) |
|
|
245 | { |
|
|
246 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
247 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
248 | return encode_sharable2 (enc, sv); |
|
|
249 | |
|
|
250 | return 0; |
|
|
251 | } |
|
|
252 | |
|
|
253 | static void encode_sv (enc_t *enc, SV *sv); |
260 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
261 | |
255 | static void |
262 | static void |
256 | encode_av (enc_t *enc, AV *av) |
263 | encode_av (enc_t *enc, AV *av) |
257 | { |
264 | { |
… | |
… | |
309 | |
316 | |
310 | // encode objects, arrays and special \0=false and \1=true values. |
317 | // encode objects, arrays and special \0=false and \1=true values. |
311 | static void |
318 | static void |
312 | encode_rv (enc_t *enc, SV *sv) |
319 | encode_rv (enc_t *enc, SV *sv) |
313 | { |
320 | { |
314 | svtype svt; |
|
|
315 | |
|
|
316 | SvGETMAGIC (sv); |
321 | SvGETMAGIC (sv); |
|
|
322 | |
|
|
323 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
324 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
325 | { |
|
|
326 | if (!enc->shareable) |
|
|
327 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
328 | |
|
|
329 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
330 | |
|
|
331 | if (SvOK (*svp)) |
|
|
332 | { |
|
|
333 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
334 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
335 | return; |
|
|
336 | } |
|
|
337 | else |
|
|
338 | { |
|
|
339 | sv_setuv (*svp, enc->shareable_idx); |
|
|
340 | ++enc->shareable_idx; |
|
|
341 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
342 | } |
|
|
343 | } |
|
|
344 | |
317 | svt = SvTYPE (sv); |
345 | svtype svt = SvTYPE (sv); |
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
|
|
321 | |
346 | |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
347 | if (ecb_expect_false (SvOBJECT (sv))) |
323 | { |
348 | { |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
349 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
325 | ? types_boolean_stash |
350 | ? types_boolean_stash |
… | |
… | |
452 | static void |
477 | static void |
453 | encode_sv (enc_t *enc, SV *sv) |
478 | encode_sv (enc_t *enc, SV *sv) |
454 | { |
479 | { |
455 | SvGETMAGIC (sv); |
480 | SvGETMAGIC (sv); |
456 | |
481 | |
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
|
|
459 | |
|
|
460 | if (SvPOKp (sv)) |
482 | if (SvPOKp (sv)) |
461 | { |
483 | { |
462 | STRLEN len; |
484 | STRLEN len; |
463 | char *str = SvPV (sv, len); |
485 | char *str = SvPV (sv, len); |
464 | encode_str (enc, SvUTF8 (sv), str, len); |
486 | encode_str (enc, SvUTF8 (sv), str, len); |
… | |
… | |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
495 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
497 | |
519 | |
498 | SvPOK_only (enc.sv); |
520 | SvPOK_only (enc.sv); |
|
|
521 | |
|
|
522 | if (cbor->flags & F_ALLOW_STRINGREF) |
|
|
523 | { |
|
|
524 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
525 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
526 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
527 | } |
|
|
528 | |
499 | encode_sv (&enc, scalar); |
529 | encode_sv (&enc, scalar); |
500 | |
530 | |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
531 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
502 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
532 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
503 | |
533 | |
… | |
… | |
517 | U8 *end; // end of input string |
547 | U8 *end; // end of input string |
518 | const char *err; // parse error, if != 0 |
548 | const char *err; // parse error, if != 0 |
519 | CBOR cbor; |
549 | CBOR cbor; |
520 | U32 depth; // recursion depth |
550 | U32 depth; // recursion depth |
521 | U32 maxdepth; // recursion depth limit |
551 | U32 maxdepth; // recursion depth limit |
522 | AV *sharable; |
552 | AV *shareable; |
|
|
553 | AV *stringref; |
|
|
554 | SV *decode_tagged; |
523 | } dec_t; |
555 | } dec_t; |
524 | |
556 | |
525 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
557 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
526 | |
558 | |
527 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
559 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
625 | |
657 | |
626 | static void |
658 | static void |
627 | decode_he (dec_t *dec, HV *hv) |
659 | decode_he (dec_t *dec, HV *hv) |
628 | { |
660 | { |
629 | // for speed reasons, we specialcase single-string |
661 | // for speed reasons, we specialcase single-string |
630 | // byte or utf-8 strings as keys. |
662 | // byte or utf-8 strings as keys, but only when !stringref |
631 | |
663 | |
|
|
664 | if (ecb_expect_true (!dec->stringref)) |
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
665 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
633 | { |
666 | { |
634 | I32 len = decode_uint (dec); |
667 | I32 len = decode_uint (dec); |
635 | char *key = (char *)dec->cur; |
668 | char *key = (char *)dec->cur; |
636 | |
669 | |
637 | dec->cur += len; |
670 | dec->cur += len; |
638 | |
671 | |
|
|
672 | if (ecb_expect_false (dec->stringref)) |
|
|
673 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
674 | |
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
675 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
676 | |
|
|
677 | return; |
640 | } |
678 | } |
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
679 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
642 | { |
680 | { |
643 | I32 len = decode_uint (dec); |
681 | I32 len = decode_uint (dec); |
644 | char *key = (char *)dec->cur; |
682 | char *key = (char *)dec->cur; |
645 | |
683 | |
646 | dec->cur += len; |
684 | dec->cur += len; |
647 | |
685 | |
|
|
686 | if (ecb_expect_false (dec->stringref)) |
|
|
687 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
688 | |
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
689 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
690 | |
|
|
691 | return; |
649 | } |
692 | } |
650 | else |
693 | |
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
694 | SV *k = decode_sv (dec); |
653 | SV *v = decode_sv (dec); |
695 | SV *v = decode_sv (dec); |
654 | |
696 | |
655 | hv_store_ent (hv, k, v, 0); |
697 | hv_store_ent (hv, k, v, 0); |
656 | SvREFCNT_dec (k); |
698 | SvREFCNT_dec (k); |
657 | } |
|
|
658 | } |
699 | } |
659 | |
700 | |
660 | static SV * |
701 | static SV * |
661 | decode_hv (dec_t *dec) |
702 | decode_hv (dec_t *dec) |
662 | { |
703 | { |
… | |
… | |
728 | STRLEN len = decode_uint (dec); |
769 | STRLEN len = decode_uint (dec); |
729 | |
770 | |
730 | WANT (len); |
771 | WANT (len); |
731 | sv = newSVpvn (dec->cur, len); |
772 | sv = newSVpvn (dec->cur, len); |
732 | dec->cur += len; |
773 | dec->cur += len; |
|
|
774 | |
|
|
775 | if (ecb_expect_false (dec->stringref) |
|
|
776 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
777 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
733 | } |
778 | } |
734 | |
779 | |
735 | if (utf8) |
780 | if (utf8) |
736 | SvUTF8_on (sv); |
781 | SvUTF8_on (sv); |
737 | |
782 | |
… | |
… | |
743 | } |
788 | } |
744 | |
789 | |
745 | static SV * |
790 | static SV * |
746 | decode_tagged (dec_t *dec) |
791 | decode_tagged (dec_t *dec) |
747 | { |
792 | { |
|
|
793 | SV *sv = 0; |
748 | UV tag = decode_uint (dec); |
794 | UV tag = decode_uint (dec); |
749 | SV *sv = decode_sv (dec); |
795 | |
|
|
796 | WANT (1); |
750 | |
797 | |
751 | switch (tag) |
798 | switch (tag) |
752 | { |
799 | { |
753 | case CBOR_TAG_MAGIC: |
800 | case CBOR_TAG_MAGIC: |
754 | return sv; |
801 | sv = decode_sv (dec); |
|
|
802 | break; |
755 | |
803 | |
756 | case CBOR_TAG_INDIRECTION: |
804 | case CBOR_TAG_INDIRECTION: |
757 | return newRV_noinc (sv); |
805 | sv = newRV_noinc (decode_sv (dec)); |
|
|
806 | break; |
758 | |
807 | |
|
|
808 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
809 | { |
|
|
810 | ENTER; SAVETMPS; |
|
|
811 | |
|
|
812 | SAVESPTR (dec->stringref); |
|
|
813 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
814 | |
|
|
815 | sv = decode_sv (dec); |
|
|
816 | |
|
|
817 | FREETMPS; LEAVE; |
|
|
818 | } |
|
|
819 | break; |
|
|
820 | |
|
|
821 | case CBOR_TAG_STRINGREF: |
|
|
822 | { |
|
|
823 | if ((*dec->cur >> 5) != 0) |
|
|
824 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
825 | |
|
|
826 | UV idx = decode_uint (dec); |
|
|
827 | |
|
|
828 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
829 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
830 | |
|
|
831 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
832 | } |
|
|
833 | break; |
|
|
834 | |
759 | case CBOR_TAG_VALUE_SHARABLE: |
835 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
836 | { |
760 | if (ecb_expect_false (!dec->sharable)) |
837 | if (ecb_expect_false (!dec->shareable)) |
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
838 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
762 | |
839 | |
|
|
840 | sv = newSV (0); |
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
841 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
764 | |
842 | |
765 | return sv; |
843 | SV *osv = decode_sv (dec); |
|
|
844 | sv_setsv (sv, osv); |
|
|
845 | SvREFCNT_dec_NN (osv); |
|
|
846 | } |
|
|
847 | break; |
766 | |
848 | |
767 | case CBOR_TAG_VALUE_SHAREDREF: |
849 | case CBOR_TAG_VALUE_SHAREDREF: |
768 | { |
850 | { |
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
851 | if ((*dec->cur >> 5) != 0) |
770 | UV idx = SvUV (sv); |
852 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
771 | |
853 | |
|
|
854 | UV idx = decode_uint (dec); |
|
|
855 | |
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
856 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
857 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
774 | |
858 | |
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
859 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
778 | } |
860 | } |
|
|
861 | break; |
779 | |
862 | |
780 | case CBOR_TAG_PERL_OBJECT: |
863 | case CBOR_TAG_PERL_OBJECT: |
781 | { |
864 | { |
|
|
865 | sv = decode_sv (dec); |
|
|
866 | |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
867 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
868 | ERR ("corrupted CBOR data (non-array perl object)"); |
784 | |
869 | |
785 | AV *av = (AV *)SvRV (sv); |
870 | AV *av = (AV *)SvRV (sv); |
786 | int len = av_len (av) + 1; |
871 | int len = av_len (av) + 1; |
… | |
… | |
821 | sv = SvREFCNT_inc (POPs); |
906 | sv = SvREFCNT_inc (POPs); |
822 | |
907 | |
823 | PUTBACK; |
908 | PUTBACK; |
824 | |
909 | |
825 | FREETMPS; LEAVE; |
910 | FREETMPS; LEAVE; |
826 | |
|
|
827 | return sv; |
|
|
828 | } |
911 | } |
|
|
912 | break; |
829 | |
913 | |
830 | default: |
914 | default: |
831 | { |
915 | { |
|
|
916 | sv = decode_sv (dec); |
|
|
917 | |
|
|
918 | dSP; |
|
|
919 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
920 | EXTEND (SP, 2); |
|
|
921 | PUSHs (newSVuv (tag)); |
|
|
922 | PUSHs (sv); |
|
|
923 | |
|
|
924 | PUTBACK; |
|
|
925 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
926 | SPAGAIN; |
|
|
927 | |
|
|
928 | if (SvTRUE (ERRSV)) |
|
|
929 | { |
|
|
930 | FREETMPS; LEAVE; |
|
|
931 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
932 | } |
|
|
933 | |
|
|
934 | if (count) |
|
|
935 | { |
|
|
936 | SvREFCNT_dec (sv); |
|
|
937 | sv = SvREFCNT_inc (POPs); |
|
|
938 | } |
|
|
939 | else |
|
|
940 | { |
832 | AV *av = newAV (); |
941 | AV *av = newAV (); |
833 | av_push (av, newSVuv (tag)); |
942 | av_push (av, newSVuv (tag)); |
834 | av_push (av, sv); |
943 | av_push (av, sv); |
835 | |
944 | |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
945 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
837 | ? cbor_tagged_stash |
946 | ? cbor_tagged_stash |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
947 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
839 | |
|
|
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
948 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
841 | } |
949 | } |
|
|
950 | |
|
|
951 | PUTBACK; |
|
|
952 | |
|
|
953 | FREETMPS; LEAVE; |
|
|
954 | } |
|
|
955 | break; |
842 | } |
956 | } |
|
|
957 | |
|
|
958 | return sv; |
843 | |
959 | |
844 | fail: |
960 | fail: |
845 | SvREFCNT_dec (sv); |
961 | SvREFCNT_dec (sv); |
846 | return &PL_sv_undef; |
962 | return &PL_sv_undef; |
847 | } |
963 | } |
… | |
… | |
988 | |
1104 | |
989 | types_true = get_bool ("Types::Serialiser::true" ); |
1105 | types_true = get_bool ("Types::Serialiser::true" ); |
990 | types_false = get_bool ("Types::Serialiser::false"); |
1106 | types_false = get_bool ("Types::Serialiser::false"); |
991 | types_error = get_bool ("Types::Serialiser::error"); |
1107 | types_error = get_bool ("Types::Serialiser::error"); |
992 | |
1108 | |
|
|
1109 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1110 | |
993 | sv_cbor = newSVpv ("CBOR", 0); |
1111 | sv_cbor = newSVpv ("CBOR", 0); |
994 | SvREADONLY_on (sv_cbor); |
1112 | SvREADONLY_on (sv_cbor); |
995 | } |
1113 | } |
996 | |
1114 | |
997 | PROTOTYPES: DISABLE |
1115 | PROTOTYPES: DISABLE |
… | |
… | |
1018 | void shrink (CBOR *self, int enable = 1) |
1136 | void shrink (CBOR *self, int enable = 1) |
1019 | ALIAS: |
1137 | ALIAS: |
1020 | shrink = F_SHRINK |
1138 | shrink = F_SHRINK |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
1022 | allow_sharing = F_ALLOW_SHARING |
1140 | allow_sharing = F_ALLOW_SHARING |
1023 | dedup_keys = F_DEDUP_KEYS |
1141 | allow_stringref = F_ALLOW_STRINGREF |
1024 | dedup_strings = F_DEDUP_STRINGS |
|
|
1025 | PPCODE: |
1142 | PPCODE: |
1026 | { |
1143 | { |
1027 | if (enable) |
1144 | if (enable) |
1028 | self->flags |= ix; |
1145 | self->flags |= ix; |
1029 | else |
1146 | else |
… | |
… | |
1035 | void get_shrink (CBOR *self) |
1152 | void get_shrink (CBOR *self) |
1036 | ALIAS: |
1153 | ALIAS: |
1037 | get_shrink = F_SHRINK |
1154 | get_shrink = F_SHRINK |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
1155 | get_allow_unknown = F_ALLOW_UNKNOWN |
1039 | get_allow_sharing = F_ALLOW_SHARING |
1156 | get_allow_sharing = F_ALLOW_SHARING |
1040 | get_dedup_keys = F_DEDUP_KEYS |
1157 | get_allow_stringref = F_ALLOW_STRINGREF |
1041 | get_dedup_strings = F_DEDUP_STRINGS |
|
|
1042 | PPCODE: |
1158 | PPCODE: |
1043 | XPUSHs (boolSV (self->flags & ix)); |
1159 | XPUSHs (boolSV (self->flags & ix)); |
1044 | |
1160 | |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1046 | PPCODE: |
1162 | PPCODE: |
… | |
… | |
1059 | XPUSHs (ST (0)); |
1175 | XPUSHs (ST (0)); |
1060 | |
1176 | |
1061 | int get_max_size (CBOR *self) |
1177 | int get_max_size (CBOR *self) |
1062 | CODE: |
1178 | CODE: |
1063 | RETVAL = self->max_size; |
1179 | RETVAL = self->max_size; |
|
|
1180 | OUTPUT: |
|
|
1181 | RETVAL |
|
|
1182 | |
|
|
1183 | void filter (CBOR *self, SV *filter = 0) |
|
|
1184 | PPCODE: |
|
|
1185 | SvREFCNT_dec (self->filter); |
|
|
1186 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1187 | XPUSHs (ST (0)); |
|
|
1188 | |
|
|
1189 | SV *get_filter (CBOR *self) |
|
|
1190 | CODE: |
|
|
1191 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1064 | OUTPUT: |
1192 | OUTPUT: |
1065 | RETVAL |
1193 | RETVAL |
1066 | |
1194 | |
1067 | void encode (CBOR *self, SV *scalar) |
1195 | void encode (CBOR *self, SV *scalar) |
1068 | PPCODE: |
1196 | PPCODE: |
… | |
… | |
1083 | EXTEND (SP, 2); |
1211 | EXTEND (SP, 2); |
1084 | PUSHs (sv); |
1212 | PUSHs (sv); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1213 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1086 | } |
1214 | } |
1087 | |
1215 | |
|
|
1216 | void DESTROY (CBOR *self) |
|
|
1217 | PPCODE: |
|
|
1218 | cbor_free (self); |
|
|
1219 | |
1088 | PROTOTYPES: ENABLE |
1220 | PROTOTYPES: ENABLE |
1089 | |
1221 | |
1090 | void encode_cbor (SV *scalar) |
1222 | void encode_cbor (SV *scalar) |
1091 | PPCODE: |
1223 | PPCODE: |
1092 | { |
1224 | { |