… | |
… | |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
12 | #include "ecb.h" |
12 | #include "ecb.h" |
13 | |
13 | |
|
|
14 | // compatibility with perl <5.18 |
|
|
15 | #ifndef HvNAMELEN_get |
|
|
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
17 | #endif |
|
|
18 | #ifndef HvNAMELEN |
|
|
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
20 | #endif |
|
|
21 | #ifndef HvNAMEUTF8 |
|
|
22 | # define HvNAMEUTF8(hv) 0 |
|
|
23 | #endif |
|
|
24 | |
14 | // known tags |
25 | // known tags |
15 | enum cbor_tag |
26 | enum cbor_tag |
16 | { |
27 | { |
17 | // inofficial extensions (pending iana registration) |
28 | // inofficial extensions (pending iana registration) |
18 | CBOR_TAG_PERL_OBJECT = 256, |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
19 | CBOR_TAG_GENERIC_OBJECT = 257, |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
|
|
31 | CBOR_TAG_VALUE_SHARABLE = 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 |
20 | |
36 | |
21 | // rfc7049 |
37 | // rfc7049 |
22 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
23 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
24 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
… | |
… | |
38 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
39 | |
55 | |
40 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
41 | }; |
57 | }; |
42 | |
58 | |
43 | #define F_SHRINK 0x00000200UL |
59 | #define F_SHRINK 0x00000001UL |
44 | #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 |
45 | |
64 | |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
47 | |
66 | |
48 | #define SB do { |
67 | #define SB do { |
49 | #define SE } while (0) |
68 | #define SE } while (0) |
… | |
… | |
106 | #endif |
125 | #endif |
107 | } |
126 | } |
108 | } |
127 | } |
109 | |
128 | |
110 | ///////////////////////////////////////////////////////////////////////////// |
129 | ///////////////////////////////////////////////////////////////////////////// |
111 | // fp hell |
|
|
112 | |
|
|
113 | //TODO |
|
|
114 | |
|
|
115 | ///////////////////////////////////////////////////////////////////////////// |
|
|
116 | // encoder |
130 | // encoder |
117 | |
131 | |
118 | // structure used for encoding CBOR |
132 | // structure used for encoding CBOR |
119 | typedef struct |
133 | typedef struct |
120 | { |
134 | { |
121 | char *cur; // SvPVX (sv) + current output position |
135 | char *cur; // SvPVX (sv) + current output position |
122 | char *end; // SvEND (sv) |
136 | char *end; // SvEND (sv) |
123 | SV *sv; // result scalar |
137 | SV *sv; // result scalar |
124 | CBOR cbor; |
138 | CBOR cbor; |
125 | U32 depth; // recursion level |
139 | U32 depth; // recursion level |
|
|
140 | HV *stringref; // string => index, or 0 |
|
|
141 | HV *sharable; // ptr => index, or 0 |
|
|
142 | HV *sharable_idx; |
126 | } enc_t; |
143 | } enc_t; |
127 | |
144 | |
128 | ecb_inline void |
145 | ecb_inline void |
129 | need (enc_t *enc, STRLEN len) |
146 | need (enc_t *enc, STRLEN len) |
130 | { |
147 | { |
… | |
… | |
191 | need (enc, len); |
208 | need (enc, len); |
192 | memcpy (enc->cur, str, len); |
209 | memcpy (enc->cur, str, len); |
193 | enc->cur += len; |
210 | enc->cur += len; |
194 | } |
211 | } |
195 | |
212 | |
|
|
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 | |
196 | static void encode_sv (enc_t *enc, SV *sv); |
253 | static void encode_sv (enc_t *enc, SV *sv); |
197 | |
254 | |
198 | static void |
255 | static void |
199 | encode_av (enc_t *enc, AV *av) |
256 | encode_av (enc_t *enc, AV *av) |
200 | { |
257 | { |
… | |
… | |
256 | { |
313 | { |
257 | svtype svt; |
314 | svtype svt; |
258 | |
315 | |
259 | SvGETMAGIC (sv); |
316 | SvGETMAGIC (sv); |
260 | svt = SvTYPE (sv); |
317 | svt = SvTYPE (sv); |
|
|
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
261 | |
321 | |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
263 | { |
323 | { |
264 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
265 | ? types_boolean_stash |
325 | ? types_boolean_stash |
… | |
… | |
325 | |
385 | |
326 | // catch this surprisingly common error |
386 | // catch this surprisingly common error |
327 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
387 | 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)); |
388 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
329 | |
389 | |
330 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
390 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
331 | encode_uint (enc, 0x80, count + 1); |
391 | encode_uint (enc, 0x80, count + 1); |
332 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
392 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
333 | |
393 | |
334 | while (count) |
394 | while (count) |
335 | encode_sv (enc, SP[1 - count--]); |
395 | encode_sv (enc, SP[1 - count--]); |
… | |
… | |
344 | } |
404 | } |
345 | else if (svt == SVt_PVHV) |
405 | else if (svt == SVt_PVHV) |
346 | encode_hv (enc, (HV *)sv); |
406 | encode_hv (enc, (HV *)sv); |
347 | else if (svt == SVt_PVAV) |
407 | else if (svt == SVt_PVAV) |
348 | encode_av (enc, (AV *)sv); |
408 | encode_av (enc, (AV *)sv); |
349 | else if (svt < SVt_PVAV) |
|
|
350 | { |
|
|
351 | STRLEN len = 0; |
|
|
352 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
353 | |
|
|
354 | if (len == 1 && *pv == '1') |
|
|
355 | encode_ch (enc, 0xe0 | 21); |
|
|
356 | else if (len == 1 && *pv == '0') |
|
|
357 | encode_ch (enc, 0xe0 | 20); |
|
|
358 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
359 | encode_ch (enc, 0xe0 | 23); |
|
|
360 | else |
|
|
361 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
362 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
363 | } |
|
|
364 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
365 | encode_ch (enc, 0xe0 | 23); |
|
|
366 | else |
409 | else |
367 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
410 | { |
368 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
411 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
412 | encode_sv (enc, sv); |
|
|
413 | } |
369 | } |
414 | } |
370 | |
415 | |
371 | static void |
416 | static void |
372 | encode_nv (enc_t *enc, SV *sv) |
417 | encode_nv (enc_t *enc, SV *sv) |
373 | { |
418 | { |
… | |
… | |
406 | |
451 | |
407 | static void |
452 | static void |
408 | encode_sv (enc_t *enc, SV *sv) |
453 | encode_sv (enc_t *enc, SV *sv) |
409 | { |
454 | { |
410 | SvGETMAGIC (sv); |
455 | SvGETMAGIC (sv); |
|
|
456 | |
|
|
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
411 | |
459 | |
412 | if (SvPOKp (sv)) |
460 | if (SvPOKp (sv)) |
413 | { |
461 | { |
414 | STRLEN len; |
462 | STRLEN len; |
415 | char *str = SvPV (sv, len); |
463 | char *str = SvPV (sv, len); |
… | |
… | |
438 | } |
486 | } |
439 | |
487 | |
440 | static SV * |
488 | static SV * |
441 | encode_cbor (SV *scalar, CBOR *cbor) |
489 | encode_cbor (SV *scalar, CBOR *cbor) |
442 | { |
490 | { |
443 | enc_t enc; |
491 | enc_t enc = { }; |
444 | |
492 | |
445 | enc.cbor = *cbor; |
493 | enc.cbor = *cbor; |
446 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
447 | enc.cur = SvPVX (enc.sv); |
495 | enc.cur = SvPVX (enc.sv); |
448 | enc.end = SvEND (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
449 | enc.depth = 0; |
|
|
450 | |
497 | |
451 | SvPOK_only (enc.sv); |
498 | SvPOK_only (enc.sv); |
452 | encode_sv (&enc, scalar); |
499 | encode_sv (&enc, scalar); |
453 | |
500 | |
454 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
470 | U8 *end; // end of input string |
517 | U8 *end; // end of input string |
471 | const char *err; // parse error, if != 0 |
518 | const char *err; // parse error, if != 0 |
472 | CBOR cbor; |
519 | CBOR cbor; |
473 | U32 depth; // recursion depth |
520 | U32 depth; // recursion depth |
474 | U32 maxdepth; // recursion depth limit |
521 | U32 maxdepth; // recursion depth limit |
|
|
522 | AV *sharable; |
475 | } dec_t; |
523 | } dec_t; |
476 | |
524 | |
477 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
525 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
478 | |
526 | |
479 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
527 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
573 | SvREFCNT_dec (av); |
621 | SvREFCNT_dec (av); |
574 | DEC_DEC_DEPTH; |
622 | DEC_DEC_DEPTH; |
575 | return &PL_sv_undef; |
623 | return &PL_sv_undef; |
576 | } |
624 | } |
577 | |
625 | |
|
|
626 | static void |
|
|
627 | decode_he (dec_t *dec, HV *hv) |
|
|
628 | { |
|
|
629 | // for speed reasons, we specialcase single-string |
|
|
630 | // byte or utf-8 strings as keys. |
|
|
631 | |
|
|
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
|
|
633 | { |
|
|
634 | I32 len = decode_uint (dec); |
|
|
635 | char *key = (char *)dec->cur; |
|
|
636 | |
|
|
637 | dec->cur += len; |
|
|
638 | |
|
|
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
640 | } |
|
|
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
|
|
642 | { |
|
|
643 | I32 len = decode_uint (dec); |
|
|
644 | char *key = (char *)dec->cur; |
|
|
645 | |
|
|
646 | dec->cur += len; |
|
|
647 | |
|
|
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
649 | } |
|
|
650 | else |
|
|
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
|
|
653 | SV *v = decode_sv (dec); |
|
|
654 | |
|
|
655 | hv_store_ent (hv, k, v, 0); |
|
|
656 | SvREFCNT_dec (k); |
|
|
657 | } |
|
|
658 | } |
|
|
659 | |
578 | static SV * |
660 | static SV * |
579 | decode_hv (dec_t *dec) |
661 | decode_hv (dec_t *dec) |
580 | { |
662 | { |
581 | HV *hv = newHV (); |
663 | HV *hv = newHV (); |
582 | |
664 | |
… | |
… | |
594 | { |
676 | { |
595 | ++dec->cur; |
677 | ++dec->cur; |
596 | break; |
678 | break; |
597 | } |
679 | } |
598 | |
680 | |
599 | SV *k = decode_sv (dec); |
681 | decode_he (dec, hv); |
600 | SV *v = decode_sv (dec); |
|
|
601 | |
|
|
602 | hv_store_ent (hv, k, v, 0); |
|
|
603 | } |
682 | } |
604 | } |
683 | } |
605 | else |
684 | else |
606 | { |
685 | { |
607 | int len = decode_uint (dec); |
686 | int pairs = decode_uint (dec); |
608 | |
687 | |
609 | while (len--) |
688 | while (pairs--) |
610 | { |
689 | decode_he (dec, hv); |
611 | SV *k = decode_sv (dec); |
|
|
612 | SV *v = decode_sv (dec); |
|
|
613 | |
|
|
614 | hv_store_ent (hv, k, v, 0); |
|
|
615 | } |
|
|
616 | } |
690 | } |
617 | |
691 | |
618 | DEC_DEC_DEPTH; |
692 | DEC_DEC_DEPTH; |
619 | return newRV_noinc ((SV *)hv); |
693 | return newRV_noinc ((SV *)hv); |
620 | |
694 | |
… | |
… | |
672 | decode_tagged (dec_t *dec) |
746 | decode_tagged (dec_t *dec) |
673 | { |
747 | { |
674 | UV tag = decode_uint (dec); |
748 | UV tag = decode_uint (dec); |
675 | SV *sv = decode_sv (dec); |
749 | SV *sv = decode_sv (dec); |
676 | |
750 | |
677 | if (tag == CBOR_TAG_MAGIC) |
751 | switch (tag) |
|
|
752 | { |
|
|
753 | case CBOR_TAG_MAGIC: |
678 | return sv; |
754 | return sv; |
679 | else if (tag == CBOR_TAG_PERL_OBJECT) |
755 | |
680 | { |
756 | case CBOR_TAG_INDIRECTION: |
|
|
757 | return newRV_noinc (sv); |
|
|
758 | |
|
|
759 | case CBOR_TAG_VALUE_SHARABLE: |
|
|
760 | if (ecb_expect_false (!dec->sharable)) |
|
|
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
762 | |
|
|
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
|
|
764 | |
|
|
765 | return sv; |
|
|
766 | |
|
|
767 | case CBOR_TAG_VALUE_SHAREDREF: |
|
|
768 | { |
|
|
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
|
|
770 | UV idx = SvUV (sv); |
|
|
771 | |
|
|
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
|
|
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
774 | |
|
|
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
|
|
778 | } |
|
|
779 | |
|
|
780 | case CBOR_TAG_PERL_OBJECT: |
|
|
781 | { |
681 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
682 | ERR ("corrupted CBOR data (non-array perl object)"); |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
683 | |
784 | |
684 | AV *av = (AV *)SvRV (sv); |
785 | AV *av = (AV *)SvRV (sv); |
685 | int len = av_len (av) + 1; |
786 | int len = av_len (av) + 1; |
686 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
787 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
687 | |
788 | |
688 | if (!stash) |
789 | if (!stash) |
689 | ERR ("cannot decode perl-object (package does not exist)"); |
790 | ERR ("cannot decode perl-object (package does not exist)"); |
690 | |
791 | |
691 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
792 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
692 | |
793 | |
693 | if (!method) |
794 | if (!method) |
694 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
795 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
695 | |
796 | |
696 | dSP; |
797 | dSP; |
697 | |
798 | |
698 | ENTER; SAVETMPS; PUSHMARK (SP); |
799 | ENTER; SAVETMPS; PUSHMARK (SP); |
699 | EXTEND (SP, len + 1); |
800 | EXTEND (SP, len + 1); |
700 | // we re-bless the reference to get overload and other niceties right |
801 | // we re-bless the reference to get overload and other niceties right |
701 | PUSHs (*av_fetch (av, 0, 1)); |
802 | PUSHs (*av_fetch (av, 0, 1)); |
702 | PUSHs (sv_cbor); |
803 | PUSHs (sv_cbor); |
703 | |
804 | |
704 | int i; |
805 | int i; |
705 | |
806 | |
706 | for (i = 1; i < len; ++i) |
807 | for (i = 1; i < len; ++i) |
707 | PUSHs (*av_fetch (av, i, 1)); |
808 | PUSHs (*av_fetch (av, i, 1)); |
708 | |
809 | |
709 | PUTBACK; |
810 | PUTBACK; |
710 | call_sv ((SV *)GvCV (method), G_SCALAR); |
811 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
711 | SPAGAIN; |
812 | SPAGAIN; |
712 | |
813 | |
|
|
814 | if (SvTRUE (ERRSV)) |
|
|
815 | { |
|
|
816 | FREETMPS; LEAVE; |
|
|
817 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
818 | } |
|
|
819 | |
|
|
820 | SvREFCNT_dec (sv); |
713 | sv = SvREFCNT_inc (POPs); |
821 | sv = SvREFCNT_inc (POPs); |
714 | |
822 | |
715 | PUTBACK; |
823 | PUTBACK; |
716 | |
824 | |
717 | FREETMPS; LEAVE; |
825 | FREETMPS; LEAVE; |
718 | |
826 | |
719 | return sv; |
827 | return sv; |
720 | } |
828 | } |
721 | else |
829 | |
722 | { |
830 | default: |
|
|
831 | { |
723 | AV *av = newAV (); |
832 | AV *av = newAV (); |
724 | av_push (av, newSVuv (tag)); |
833 | av_push (av, newSVuv (tag)); |
725 | av_push (av, sv); |
834 | av_push (av, sv); |
726 | |
835 | |
727 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
728 | ? cbor_tagged_stash |
837 | ? cbor_tagged_stash |
729 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
730 | |
839 | |
731 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
841 | } |
732 | } |
842 | } |
733 | |
843 | |
734 | fail: |
844 | fail: |
735 | SvREFCNT_dec (sv); |
845 | SvREFCNT_dec (sv); |
736 | return &PL_sv_undef; |
846 | return &PL_sv_undef; |
… | |
… | |
828 | } |
938 | } |
829 | |
939 | |
830 | static SV * |
940 | static SV * |
831 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
941 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
832 | { |
942 | { |
833 | dec_t dec; |
943 | dec_t dec = { }; |
834 | SV *sv; |
944 | SV *sv; |
|
|
945 | STRLEN len; |
|
|
946 | char *data = SvPVbyte (string, len); |
835 | |
947 | |
836 | /* work around bugs in 5.10 where manipulating magic values |
|
|
837 | * makes perl ignore the magic in subsequent accesses. |
|
|
838 | * also make a copy of non-PV values, to get them into a clean |
|
|
839 | * state (SvPV should do that, but it's buggy, see below). |
|
|
840 | */ |
|
|
841 | /*SvGETMAGIC (string);*/ |
|
|
842 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
843 | string = sv_2mortal (newSVsv (string)); |
|
|
844 | |
|
|
845 | SvUPGRADE (string, SVt_PV); |
|
|
846 | |
|
|
847 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
848 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
849 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
850 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
851 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
852 | * and hope for the best. |
|
|
853 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
854 | * assertion business is seriously broken, try yet another workaround |
|
|
855 | * for the broken -DDEBUGGING. |
|
|
856 | */ |
|
|
857 | { |
|
|
858 | #ifdef DEBUGGING |
|
|
859 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
860 | #else |
|
|
861 | STRLEN offset = SvCUR (string); |
|
|
862 | #endif |
|
|
863 | |
|
|
864 | if (offset > cbor->max_size && cbor->max_size) |
948 | if (len > cbor->max_size && cbor->max_size) |
865 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
949 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
866 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
950 | (unsigned long)len, (unsigned long)cbor->max_size); |
867 | } |
|
|
868 | |
|
|
869 | sv_utf8_downgrade (string, 0); |
|
|
870 | |
951 | |
871 | dec.cbor = *cbor; |
952 | dec.cbor = *cbor; |
872 | dec.cur = (U8 *)SvPVX (string); |
953 | dec.cur = (U8 *)data; |
873 | dec.end = (U8 *)SvEND (string); |
954 | dec.end = (U8 *)data + len; |
874 | dec.err = 0; |
|
|
875 | dec.depth = 0; |
|
|
876 | |
955 | |
877 | sv = decode_sv (&dec); |
956 | sv = decode_sv (&dec); |
878 | |
957 | |
879 | if (offset_return) |
958 | if (offset_return) |
880 | *offset_return = dec.cur; |
959 | *offset_return = dec.cur; |
… | |
… | |
884 | dec.err = "garbage after CBOR object"; |
963 | dec.err = "garbage after CBOR object"; |
885 | |
964 | |
886 | if (dec.err) |
965 | if (dec.err) |
887 | { |
966 | { |
888 | SvREFCNT_dec (sv); |
967 | SvREFCNT_dec (sv); |
889 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
968 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
890 | } |
969 | } |
891 | |
970 | |
892 | sv = sv_2mortal (sv); |
971 | sv = sv_2mortal (sv); |
893 | |
972 | |
894 | return sv; |
973 | return sv; |
… | |
… | |
938 | |
1017 | |
939 | void shrink (CBOR *self, int enable = 1) |
1018 | void shrink (CBOR *self, int enable = 1) |
940 | ALIAS: |
1019 | ALIAS: |
941 | shrink = F_SHRINK |
1020 | shrink = F_SHRINK |
942 | allow_unknown = F_ALLOW_UNKNOWN |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1022 | allow_sharing = F_ALLOW_SHARING |
|
|
1023 | dedup_keys = F_DEDUP_KEYS |
|
|
1024 | dedup_strings = F_DEDUP_STRINGS |
943 | PPCODE: |
1025 | PPCODE: |
944 | { |
1026 | { |
945 | if (enable) |
1027 | if (enable) |
946 | self->flags |= ix; |
1028 | self->flags |= ix; |
947 | else |
1029 | else |
… | |
… | |
952 | |
1034 | |
953 | void get_shrink (CBOR *self) |
1035 | void get_shrink (CBOR *self) |
954 | ALIAS: |
1036 | ALIAS: |
955 | get_shrink = F_SHRINK |
1037 | get_shrink = F_SHRINK |
956 | get_allow_unknown = F_ALLOW_UNKNOWN |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1039 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1040 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1041 | get_dedup_strings = F_DEDUP_STRINGS |
957 | PPCODE: |
1042 | PPCODE: |
958 | XPUSHs (boolSV (self->flags & ix)); |
1043 | XPUSHs (boolSV (self->flags & ix)); |
959 | |
1044 | |
960 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
961 | PPCODE: |
1046 | PPCODE: |
… | |
… | |
976 | int get_max_size (CBOR *self) |
1061 | int get_max_size (CBOR *self) |
977 | CODE: |
1062 | CODE: |
978 | RETVAL = self->max_size; |
1063 | RETVAL = self->max_size; |
979 | OUTPUT: |
1064 | OUTPUT: |
980 | RETVAL |
1065 | RETVAL |
981 | |
|
|
982 | #if 0 //TODO |
|
|
983 | |
|
|
984 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
985 | PPCODE: |
|
|
986 | { |
|
|
987 | SvREFCNT_dec (self->cb_object); |
|
|
988 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
989 | |
|
|
990 | XPUSHs (ST (0)); |
|
|
991 | } |
|
|
992 | |
|
|
993 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
994 | PPCODE: |
|
|
995 | { |
|
|
996 | if (!self->cb_sk_object) |
|
|
997 | self->cb_sk_object = newHV (); |
|
|
998 | |
|
|
999 | if (SvOK (cb)) |
|
|
1000 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1001 | else |
|
|
1002 | { |
|
|
1003 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1004 | |
|
|
1005 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1006 | { |
|
|
1007 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1008 | self->cb_sk_object = 0; |
|
|
1009 | } |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | XPUSHs (ST (0)); |
|
|
1013 | } |
|
|
1014 | |
|
|
1015 | #endif |
|
|
1016 | |
1066 | |
1017 | void encode (CBOR *self, SV *scalar) |
1067 | void encode (CBOR *self, SV *scalar) |
1018 | PPCODE: |
1068 | PPCODE: |
1019 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1069 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1020 | XPUSHs (scalar); |
1070 | XPUSHs (scalar); |
… | |
… | |
1033 | EXTEND (SP, 2); |
1083 | EXTEND (SP, 2); |
1034 | PUSHs (sv); |
1084 | PUSHs (sv); |
1035 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1036 | } |
1086 | } |
1037 | |
1087 | |
1038 | #if 0 |
|
|
1039 | |
|
|
1040 | void DESTROY (CBOR *self) |
|
|
1041 | CODE: |
|
|
1042 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1043 | SvREFCNT_dec (self->cb_object); |
|
|
1044 | |
|
|
1045 | #endif |
|
|
1046 | |
|
|
1047 | PROTOTYPES: ENABLE |
1088 | PROTOTYPES: ENABLE |
1048 | |
1089 | |
1049 | void encode_cbor (SV *scalar) |
1090 | void encode_cbor (SV *scalar) |
1050 | PPCODE: |
1091 | PPCODE: |
1051 | { |
1092 | { |