… | |
… | |
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_ALLOW_STRINGREF 0x00000008UL //TODO |
56 | |
63 | |
57 | #define INIT_SIZE 32 // initial scalar size to be allocated |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
58 | |
65 | |
59 | #define SB do { |
66 | #define SB do { |
60 | #define SE } while (0) |
67 | #define SE } while (0) |
… | |
… | |
72 | # define CBOR_SLOW 0 |
79 | # define CBOR_SLOW 0 |
73 | # define CBOR_STASH cbor_stash |
80 | # define CBOR_STASH cbor_stash |
74 | #endif |
81 | #endif |
75 | |
82 | |
76 | 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:: |
77 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
78 | |
85 | |
79 | typedef struct { |
86 | typedef struct { |
80 | U32 flags; |
87 | U32 flags; |
81 | U32 max_depth; |
88 | U32 max_depth; |
82 | STRLEN max_size; |
89 | STRLEN max_size; |
|
|
90 | SV *filter; |
83 | } CBOR; |
91 | } CBOR; |
84 | |
92 | |
85 | ecb_inline void |
93 | ecb_inline void |
86 | cbor_init (CBOR *cbor) |
94 | cbor_init (CBOR *cbor) |
87 | { |
95 | { |
88 | Zero (cbor, 1, CBOR); |
96 | Zero (cbor, 1, CBOR); |
89 | 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); |
90 | } |
104 | } |
91 | |
105 | |
92 | ///////////////////////////////////////////////////////////////////////////// |
106 | ///////////////////////////////////////////////////////////////////////////// |
93 | // utility functions |
107 | // utility functions |
94 | |
108 | |
… | |
… | |
116 | SvPV_renew (sv, SvCUR (sv) + 1); |
130 | SvPV_renew (sv, SvCUR (sv) + 1); |
117 | #endif |
131 | #endif |
118 | } |
132 | } |
119 | } |
133 | } |
120 | |
134 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
135 | // minimum length of a string to be registered for stringref |
122 | // fp hell |
136 | ecb_inline int |
123 | |
137 | minimum_string_length (UV idx) |
124 | //TODO |
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 | } |
125 | |
149 | |
126 | ///////////////////////////////////////////////////////////////////////////// |
150 | ///////////////////////////////////////////////////////////////////////////// |
127 | // encoder |
151 | // encoder |
128 | |
152 | |
129 | // structure used for encoding CBOR |
153 | // structure used for encoding CBOR |
… | |
… | |
132 | char *cur; // SvPVX (sv) + current output position |
156 | char *cur; // SvPVX (sv) + current output position |
133 | char *end; // SvEND (sv) |
157 | char *end; // SvEND (sv) |
134 | SV *sv; // result scalar |
158 | SV *sv; // result scalar |
135 | CBOR cbor; |
159 | CBOR cbor; |
136 | U32 depth; // recursion level |
160 | U32 depth; // recursion level |
|
|
161 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
162 | UV stringref_idx; |
|
|
163 | HV *shareable; // ptr => index, or 0 |
|
|
164 | UV shareable_idx; |
137 | } enc_t; |
165 | } enc_t; |
138 | |
166 | |
139 | ecb_inline void |
167 | ecb_inline void |
140 | need (enc_t *enc, STRLEN len) |
168 | need (enc_t *enc, STRLEN len) |
141 | { |
169 | { |
… | |
… | |
193 | *enc->cur++ = len >> 8; |
221 | *enc->cur++ = len >> 8; |
194 | *enc->cur++ = len; |
222 | *enc->cur++ = len; |
195 | } |
223 | } |
196 | } |
224 | } |
197 | |
225 | |
|
|
226 | ecb_inline void |
|
|
227 | encode_tag (enc_t *enc, UV tag) |
|
|
228 | { |
|
|
229 | encode_uint (enc, 0xc0, tag); |
|
|
230 | } |
|
|
231 | |
198 | static void |
232 | static void |
199 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
200 | { |
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 | |
201 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
202 | need (enc, len); |
255 | need (enc, len); |
203 | memcpy (enc->cur, str, len); |
256 | memcpy (enc->cur, str, len); |
204 | enc->cur += len; |
257 | enc->cur += len; |
205 | } |
258 | } |
… | |
… | |
263 | |
316 | |
264 | // encode objects, arrays and special \0=false and \1=true values. |
317 | // encode objects, arrays and special \0=false and \1=true values. |
265 | static void |
318 | static void |
266 | encode_rv (enc_t *enc, SV *sv) |
319 | encode_rv (enc_t *enc, SV *sv) |
267 | { |
320 | { |
268 | svtype svt; |
|
|
269 | |
|
|
270 | 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 | |
271 | svt = SvTYPE (sv); |
345 | svtype svt = SvTYPE (sv); |
272 | |
346 | |
273 | if (ecb_expect_false (SvOBJECT (sv))) |
347 | if (ecb_expect_false (SvOBJECT (sv))) |
274 | { |
348 | { |
275 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
349 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
276 | ? types_boolean_stash |
350 | ? types_boolean_stash |
… | |
… | |
336 | |
410 | |
337 | // catch this surprisingly common error |
411 | // catch this surprisingly common error |
338 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
412 | 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)); |
413 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
340 | |
414 | |
341 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
415 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
342 | encode_uint (enc, 0x80, count + 1); |
416 | encode_uint (enc, 0x80, count + 1); |
343 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
417 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
344 | |
418 | |
345 | while (count) |
419 | while (count) |
346 | encode_sv (enc, SP[1 - count--]); |
420 | encode_sv (enc, SP[1 - count--]); |
… | |
… | |
355 | } |
429 | } |
356 | else if (svt == SVt_PVHV) |
430 | else if (svt == SVt_PVHV) |
357 | encode_hv (enc, (HV *)sv); |
431 | encode_hv (enc, (HV *)sv); |
358 | else if (svt == SVt_PVAV) |
432 | else if (svt == SVt_PVAV) |
359 | encode_av (enc, (AV *)sv); |
433 | 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 |
434 | else |
378 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
435 | { |
379 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
436 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
437 | encode_sv (enc, sv); |
|
|
438 | } |
380 | } |
439 | } |
381 | |
440 | |
382 | static void |
441 | static void |
383 | encode_nv (enc_t *enc, SV *sv) |
442 | encode_nv (enc_t *enc, SV *sv) |
384 | { |
443 | { |
… | |
… | |
449 | } |
508 | } |
450 | |
509 | |
451 | static SV * |
510 | static SV * |
452 | encode_cbor (SV *scalar, CBOR *cbor) |
511 | encode_cbor (SV *scalar, CBOR *cbor) |
453 | { |
512 | { |
454 | enc_t enc; |
513 | enc_t enc = { }; |
455 | |
514 | |
456 | enc.cbor = *cbor; |
515 | enc.cbor = *cbor; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
460 | enc.depth = 0; |
|
|
461 | |
519 | |
462 | 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 | |
463 | encode_sv (&enc, scalar); |
529 | encode_sv (&enc, scalar); |
464 | |
530 | |
465 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
531 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
466 | *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 |
467 | |
533 | |
… | |
… | |
481 | U8 *end; // end of input string |
547 | U8 *end; // end of input string |
482 | const char *err; // parse error, if != 0 |
548 | const char *err; // parse error, if != 0 |
483 | CBOR cbor; |
549 | CBOR cbor; |
484 | U32 depth; // recursion depth |
550 | U32 depth; // recursion depth |
485 | U32 maxdepth; // recursion depth limit |
551 | U32 maxdepth; // recursion depth limit |
|
|
552 | AV *shareable; |
|
|
553 | AV *stringref; |
|
|
554 | SV *decode_tagged; |
486 | } dec_t; |
555 | } dec_t; |
487 | |
556 | |
488 | #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 |
489 | |
558 | |
490 | #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") |
… | |
… | |
584 | SvREFCNT_dec (av); |
653 | SvREFCNT_dec (av); |
585 | DEC_DEC_DEPTH; |
654 | DEC_DEC_DEPTH; |
586 | return &PL_sv_undef; |
655 | return &PL_sv_undef; |
587 | } |
656 | } |
588 | |
657 | |
|
|
658 | static void |
|
|
659 | decode_he (dec_t *dec, HV *hv) |
|
|
660 | { |
|
|
661 | // for speed reasons, we specialcase single-string |
|
|
662 | // byte or utf-8 strings as keys, but only when !stringref |
|
|
663 | |
|
|
664 | if (ecb_expect_true (!dec->stringref)) |
|
|
665 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
|
|
666 | { |
|
|
667 | I32 len = decode_uint (dec); |
|
|
668 | char *key = (char *)dec->cur; |
|
|
669 | |
|
|
670 | dec->cur += len; |
|
|
671 | |
|
|
672 | if (ecb_expect_false (dec->stringref)) |
|
|
673 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
674 | |
|
|
675 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
676 | |
|
|
677 | return; |
|
|
678 | } |
|
|
679 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
|
|
680 | { |
|
|
681 | I32 len = decode_uint (dec); |
|
|
682 | char *key = (char *)dec->cur; |
|
|
683 | |
|
|
684 | dec->cur += len; |
|
|
685 | |
|
|
686 | if (ecb_expect_false (dec->stringref)) |
|
|
687 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
688 | |
|
|
689 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
690 | |
|
|
691 | return; |
|
|
692 | } |
|
|
693 | |
|
|
694 | SV *k = decode_sv (dec); |
|
|
695 | SV *v = decode_sv (dec); |
|
|
696 | |
|
|
697 | hv_store_ent (hv, k, v, 0); |
|
|
698 | SvREFCNT_dec (k); |
|
|
699 | } |
|
|
700 | |
589 | static SV * |
701 | static SV * |
590 | decode_hv (dec_t *dec) |
702 | decode_hv (dec_t *dec) |
591 | { |
703 | { |
592 | HV *hv = newHV (); |
704 | HV *hv = newHV (); |
593 | |
705 | |
… | |
… | |
605 | { |
717 | { |
606 | ++dec->cur; |
718 | ++dec->cur; |
607 | break; |
719 | break; |
608 | } |
720 | } |
609 | |
721 | |
610 | SV *k = decode_sv (dec); |
722 | decode_he (dec, hv); |
611 | SV *v = decode_sv (dec); |
|
|
612 | |
|
|
613 | hv_store_ent (hv, k, v, 0); |
|
|
614 | SvREFCNT_dec (k); |
|
|
615 | } |
723 | } |
616 | } |
724 | } |
617 | else |
725 | else |
618 | { |
726 | { |
619 | int len = decode_uint (dec); |
727 | int pairs = decode_uint (dec); |
620 | |
728 | |
621 | while (len--) |
729 | while (pairs--) |
622 | { |
730 | 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 | } |
731 | } |
630 | |
732 | |
631 | DEC_DEC_DEPTH; |
733 | DEC_DEC_DEPTH; |
632 | return newRV_noinc ((SV *)hv); |
734 | return newRV_noinc ((SV *)hv); |
633 | |
735 | |
… | |
… | |
667 | STRLEN len = decode_uint (dec); |
769 | STRLEN len = decode_uint (dec); |
668 | |
770 | |
669 | WANT (len); |
771 | WANT (len); |
670 | sv = newSVpvn (dec->cur, len); |
772 | sv = newSVpvn (dec->cur, len); |
671 | 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)); |
672 | } |
778 | } |
673 | |
779 | |
674 | if (utf8) |
780 | if (utf8) |
675 | SvUTF8_on (sv); |
781 | SvUTF8_on (sv); |
676 | |
782 | |
… | |
… | |
682 | } |
788 | } |
683 | |
789 | |
684 | static SV * |
790 | static SV * |
685 | decode_tagged (dec_t *dec) |
791 | decode_tagged (dec_t *dec) |
686 | { |
792 | { |
|
|
793 | SV *sv = 0; |
687 | UV tag = decode_uint (dec); |
794 | UV tag = decode_uint (dec); |
|
|
795 | |
|
|
796 | WANT (1); |
|
|
797 | |
|
|
798 | switch (tag) |
|
|
799 | { |
|
|
800 | case CBOR_TAG_MAGIC: |
|
|
801 | sv = decode_sv (dec); |
|
|
802 | break; |
|
|
803 | |
|
|
804 | case CBOR_TAG_INDIRECTION: |
|
|
805 | sv = newRV_noinc (decode_sv (dec)); |
|
|
806 | break; |
|
|
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 | |
|
|
835 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
836 | { |
|
|
837 | if (ecb_expect_false (!dec->shareable)) |
|
|
838 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
839 | |
|
|
840 | sv = newSV (0); |
|
|
841 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
842 | |
688 | SV *sv = decode_sv (dec); |
843 | SV *osv = decode_sv (dec); |
|
|
844 | sv_setsv (sv, osv); |
|
|
845 | SvREFCNT_dec_NN (osv); |
|
|
846 | } |
|
|
847 | break; |
689 | |
848 | |
690 | if (tag == CBOR_TAG_MAGIC) |
849 | case CBOR_TAG_VALUE_SHAREDREF: |
691 | return sv; |
850 | { |
692 | else if (tag == CBOR_TAG_PERL_OBJECT) |
851 | if ((*dec->cur >> 5) != 0) |
693 | { |
852 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
|
|
853 | |
|
|
854 | UV idx = decode_uint (dec); |
|
|
855 | |
|
|
856 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
|
|
857 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
858 | |
|
|
859 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
860 | } |
|
|
861 | break; |
|
|
862 | |
|
|
863 | case CBOR_TAG_PERL_OBJECT: |
|
|
864 | { |
|
|
865 | sv = decode_sv (dec); |
|
|
866 | |
694 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
867 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
695 | ERR ("corrupted CBOR data (non-array perl object)"); |
868 | ERR ("corrupted CBOR data (non-array perl object)"); |
696 | |
869 | |
697 | AV *av = (AV *)SvRV (sv); |
870 | AV *av = (AV *)SvRV (sv); |
698 | int len = av_len (av) + 1; |
871 | int len = av_len (av) + 1; |
699 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
872 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
700 | |
873 | |
701 | if (!stash) |
874 | if (!stash) |
702 | ERR ("cannot decode perl-object (package does not exist)"); |
875 | ERR ("cannot decode perl-object (package does not exist)"); |
703 | |
876 | |
704 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
877 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
705 | |
878 | |
706 | if (!method) |
879 | if (!method) |
707 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
880 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
708 | |
881 | |
709 | dSP; |
882 | dSP; |
710 | |
883 | |
711 | ENTER; SAVETMPS; PUSHMARK (SP); |
884 | ENTER; SAVETMPS; PUSHMARK (SP); |
712 | EXTEND (SP, len + 1); |
885 | EXTEND (SP, len + 1); |
713 | // we re-bless the reference to get overload and other niceties right |
886 | // we re-bless the reference to get overload and other niceties right |
714 | PUSHs (*av_fetch (av, 0, 1)); |
887 | PUSHs (*av_fetch (av, 0, 1)); |
715 | PUSHs (sv_cbor); |
888 | PUSHs (sv_cbor); |
716 | |
889 | |
717 | int i; |
890 | int i; |
718 | |
891 | |
719 | for (i = 1; i < len; ++i) |
892 | for (i = 1; i < len; ++i) |
720 | PUSHs (*av_fetch (av, i, 1)); |
893 | PUSHs (*av_fetch (av, i, 1)); |
721 | |
894 | |
722 | PUTBACK; |
895 | PUTBACK; |
723 | call_sv ((SV *)GvCV (method), G_SCALAR); |
896 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
724 | SPAGAIN; |
897 | SPAGAIN; |
725 | |
898 | |
|
|
899 | if (SvTRUE (ERRSV)) |
|
|
900 | { |
|
|
901 | FREETMPS; LEAVE; |
|
|
902 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
903 | } |
|
|
904 | |
726 | SvREFCNT_dec (sv); |
905 | SvREFCNT_dec (sv); |
727 | sv = SvREFCNT_inc (POPs); |
906 | sv = SvREFCNT_inc (POPs); |
728 | |
907 | |
729 | PUTBACK; |
908 | PUTBACK; |
730 | |
909 | |
731 | FREETMPS; LEAVE; |
910 | FREETMPS; LEAVE; |
|
|
911 | } |
|
|
912 | break; |
732 | |
913 | |
733 | return sv; |
914 | default: |
734 | } |
915 | { |
735 | else |
916 | sv = decode_sv (dec); |
736 | { |
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 | { |
737 | AV *av = newAV (); |
941 | AV *av = newAV (); |
738 | av_push (av, newSVuv (tag)); |
942 | av_push (av, newSVuv (tag)); |
739 | av_push (av, sv); |
943 | av_push (av, sv); |
740 | |
944 | |
741 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
945 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
742 | ? cbor_tagged_stash |
946 | ? cbor_tagged_stash |
743 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
947 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
744 | |
|
|
745 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
948 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
949 | } |
|
|
950 | |
|
|
951 | PUTBACK; |
|
|
952 | |
|
|
953 | FREETMPS; LEAVE; |
|
|
954 | } |
|
|
955 | break; |
746 | } |
956 | } |
|
|
957 | |
|
|
958 | return sv; |
747 | |
959 | |
748 | fail: |
960 | fail: |
749 | SvREFCNT_dec (sv); |
961 | SvREFCNT_dec (sv); |
750 | return &PL_sv_undef; |
962 | return &PL_sv_undef; |
751 | } |
963 | } |
… | |
… | |
842 | } |
1054 | } |
843 | |
1055 | |
844 | static SV * |
1056 | static SV * |
845 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1057 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
846 | { |
1058 | { |
847 | dec_t dec; |
1059 | dec_t dec = { }; |
848 | SV *sv; |
1060 | SV *sv; |
|
|
1061 | STRLEN len; |
|
|
1062 | char *data = SvPVbyte (string, len); |
849 | |
1063 | |
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) |
1064 | 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", |
1065 | 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); |
1066 | (unsigned long)len, (unsigned long)cbor->max_size); |
881 | } |
|
|
882 | |
|
|
883 | sv_utf8_downgrade (string, 0); |
|
|
884 | |
1067 | |
885 | dec.cbor = *cbor; |
1068 | dec.cbor = *cbor; |
886 | dec.cur = (U8 *)SvPVX (string); |
1069 | dec.cur = (U8 *)data; |
887 | dec.end = (U8 *)SvEND (string); |
1070 | dec.end = (U8 *)data + len; |
888 | dec.err = 0; |
|
|
889 | dec.depth = 0; |
|
|
890 | |
1071 | |
891 | sv = decode_sv (&dec); |
1072 | sv = decode_sv (&dec); |
892 | |
1073 | |
893 | if (offset_return) |
1074 | if (offset_return) |
894 | *offset_return = dec.cur; |
1075 | *offset_return = dec.cur; |
… | |
… | |
898 | dec.err = "garbage after CBOR object"; |
1079 | dec.err = "garbage after CBOR object"; |
899 | |
1080 | |
900 | if (dec.err) |
1081 | if (dec.err) |
901 | { |
1082 | { |
902 | SvREFCNT_dec (sv); |
1083 | SvREFCNT_dec (sv); |
903 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
1084 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
904 | } |
1085 | } |
905 | |
1086 | |
906 | sv = sv_2mortal (sv); |
1087 | sv = sv_2mortal (sv); |
907 | |
1088 | |
908 | return sv; |
1089 | return sv; |
… | |
… | |
922 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1103 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
923 | |
1104 | |
924 | types_true = get_bool ("Types::Serialiser::true" ); |
1105 | types_true = get_bool ("Types::Serialiser::true" ); |
925 | types_false = get_bool ("Types::Serialiser::false"); |
1106 | types_false = get_bool ("Types::Serialiser::false"); |
926 | types_error = get_bool ("Types::Serialiser::error"); |
1107 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1108 | |
|
|
1109 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
927 | |
1110 | |
928 | sv_cbor = newSVpv ("CBOR", 0); |
1111 | sv_cbor = newSVpv ("CBOR", 0); |
929 | SvREADONLY_on (sv_cbor); |
1112 | SvREADONLY_on (sv_cbor); |
930 | } |
1113 | } |
931 | |
1114 | |
… | |
… | |
952 | |
1135 | |
953 | void shrink (CBOR *self, int enable = 1) |
1136 | void shrink (CBOR *self, int enable = 1) |
954 | ALIAS: |
1137 | ALIAS: |
955 | shrink = F_SHRINK |
1138 | shrink = F_SHRINK |
956 | allow_unknown = F_ALLOW_UNKNOWN |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1140 | allow_sharing = F_ALLOW_SHARING |
|
|
1141 | allow_stringref = F_ALLOW_STRINGREF |
957 | PPCODE: |
1142 | PPCODE: |
958 | { |
1143 | { |
959 | if (enable) |
1144 | if (enable) |
960 | self->flags |= ix; |
1145 | self->flags |= ix; |
961 | else |
1146 | else |
… | |
… | |
966 | |
1151 | |
967 | void get_shrink (CBOR *self) |
1152 | void get_shrink (CBOR *self) |
968 | ALIAS: |
1153 | ALIAS: |
969 | get_shrink = F_SHRINK |
1154 | get_shrink = F_SHRINK |
970 | get_allow_unknown = F_ALLOW_UNKNOWN |
1155 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1156 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1157 | get_allow_stringref = F_ALLOW_STRINGREF |
971 | PPCODE: |
1158 | PPCODE: |
972 | XPUSHs (boolSV (self->flags & ix)); |
1159 | XPUSHs (boolSV (self->flags & ix)); |
973 | |
1160 | |
974 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
975 | PPCODE: |
1162 | PPCODE: |
… | |
… | |
991 | CODE: |
1178 | CODE: |
992 | RETVAL = self->max_size; |
1179 | RETVAL = self->max_size; |
993 | OUTPUT: |
1180 | OUTPUT: |
994 | RETVAL |
1181 | RETVAL |
995 | |
1182 | |
996 | #if 0 //TODO |
1183 | void filter (CBOR *self, SV *filter = 0) |
997 | |
|
|
998 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
999 | PPCODE: |
1184 | PPCODE: |
1000 | { |
|
|
1001 | SvREFCNT_dec (self->cb_object); |
1185 | SvREFCNT_dec (self->filter); |
1002 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1186 | self->filter = filter ? newSVsv (filter) : filter; |
1003 | |
|
|
1004 | XPUSHs (ST (0)); |
1187 | XPUSHs (ST (0)); |
1005 | } |
|
|
1006 | |
1188 | |
1007 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
1189 | SV *get_filter (CBOR *self) |
1008 | PPCODE: |
1190 | CODE: |
1009 | { |
1191 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1010 | if (!self->cb_sk_object) |
1192 | OUTPUT: |
1011 | self->cb_sk_object = newHV (); |
1193 | RETVAL |
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 | |
1194 | |
1031 | void encode (CBOR *self, SV *scalar) |
1195 | void encode (CBOR *self, SV *scalar) |
1032 | PPCODE: |
1196 | PPCODE: |
1033 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1197 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1034 | XPUSHs (scalar); |
1198 | XPUSHs (scalar); |
… | |
… | |
1047 | EXTEND (SP, 2); |
1211 | EXTEND (SP, 2); |
1048 | PUSHs (sv); |
1212 | PUSHs (sv); |
1049 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1213 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1050 | } |
1214 | } |
1051 | |
1215 | |
1052 | #if 0 |
|
|
1053 | |
|
|
1054 | void DESTROY (CBOR *self) |
1216 | void DESTROY (CBOR *self) |
1055 | CODE: |
1217 | PPCODE: |
1056 | SvREFCNT_dec (self->cb_sk_object); |
1218 | cbor_free (self); |
1057 | SvREFCNT_dec (self->cb_object); |
|
|
1058 | |
|
|
1059 | #endif |
|
|
1060 | |
1219 | |
1061 | PROTOTYPES: ENABLE |
1220 | PROTOTYPES: ENABLE |
1062 | |
1221 | |
1063 | void encode_cbor (SV *scalar) |
1222 | void encode_cbor (SV *scalar) |
1064 | PPCODE: |
1223 | PPCODE: |