… | |
… | |
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_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 |
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_ALLOW_STRINGREF 0x00000008UL //TODO |
45 | |
63 | |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
47 | |
65 | |
48 | #define SB do { |
66 | #define SB do { |
49 | #define SE } while (0) |
67 | #define SE } while (0) |
… | |
… | |
61 | # define CBOR_SLOW 0 |
79 | # define CBOR_SLOW 0 |
62 | # define CBOR_STASH cbor_stash |
80 | # define CBOR_STASH cbor_stash |
63 | #endif |
81 | #endif |
64 | |
82 | |
65 | 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:: |
66 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
67 | |
85 | |
68 | typedef struct { |
86 | typedef struct { |
69 | U32 flags; |
87 | U32 flags; |
70 | U32 max_depth; |
88 | U32 max_depth; |
71 | STRLEN max_size; |
89 | STRLEN max_size; |
|
|
90 | SV *filter; |
72 | } CBOR; |
91 | } CBOR; |
73 | |
92 | |
74 | ecb_inline void |
93 | ecb_inline void |
75 | cbor_init (CBOR *cbor) |
94 | cbor_init (CBOR *cbor) |
76 | { |
95 | { |
77 | Zero (cbor, 1, CBOR); |
96 | Zero (cbor, 1, CBOR); |
78 | 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); |
79 | } |
104 | } |
80 | |
105 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
106 | ///////////////////////////////////////////////////////////////////////////// |
82 | // utility functions |
107 | // utility functions |
83 | |
108 | |
… | |
… | |
105 | SvPV_renew (sv, SvCUR (sv) + 1); |
130 | SvPV_renew (sv, SvCUR (sv) + 1); |
106 | #endif |
131 | #endif |
107 | } |
132 | } |
108 | } |
133 | } |
109 | |
134 | |
110 | ///////////////////////////////////////////////////////////////////////////// |
135 | // minimum length of a string to be registered for stringref |
111 | // fp hell |
136 | ecb_inline int |
112 | |
137 | minimum_string_length (UV idx) |
113 | //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 | } |
114 | |
149 | |
115 | ///////////////////////////////////////////////////////////////////////////// |
150 | ///////////////////////////////////////////////////////////////////////////// |
116 | // encoder |
151 | // encoder |
117 | |
152 | |
118 | // structure used for encoding CBOR |
153 | // structure used for encoding CBOR |
… | |
… | |
121 | char *cur; // SvPVX (sv) + current output position |
156 | char *cur; // SvPVX (sv) + current output position |
122 | char *end; // SvEND (sv) |
157 | char *end; // SvEND (sv) |
123 | SV *sv; // result scalar |
158 | SV *sv; // result scalar |
124 | CBOR cbor; |
159 | CBOR cbor; |
125 | 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; |
126 | } enc_t; |
165 | } enc_t; |
127 | |
166 | |
128 | ecb_inline void |
167 | ecb_inline void |
129 | need (enc_t *enc, STRLEN len) |
168 | need (enc_t *enc, STRLEN len) |
130 | { |
169 | { |
… | |
… | |
182 | *enc->cur++ = len >> 8; |
221 | *enc->cur++ = len >> 8; |
183 | *enc->cur++ = len; |
222 | *enc->cur++ = len; |
184 | } |
223 | } |
185 | } |
224 | } |
186 | |
225 | |
|
|
226 | ecb_inline void |
|
|
227 | encode_tag (enc_t *enc, UV tag) |
|
|
228 | { |
|
|
229 | encode_uint (enc, 0xc0, tag); |
|
|
230 | } |
|
|
231 | |
187 | static void |
232 | static void |
188 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
189 | { |
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 | |
190 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
191 | need (enc, len); |
255 | need (enc, len); |
192 | memcpy (enc->cur, str, len); |
256 | memcpy (enc->cur, str, len); |
193 | enc->cur += len; |
257 | enc->cur += len; |
194 | } |
258 | } |
… | |
… | |
252 | |
316 | |
253 | // encode objects, arrays and special \0=false and \1=true values. |
317 | // encode objects, arrays and special \0=false and \1=true values. |
254 | static void |
318 | static void |
255 | encode_rv (enc_t *enc, SV *sv) |
319 | encode_rv (enc_t *enc, SV *sv) |
256 | { |
320 | { |
257 | svtype svt; |
|
|
258 | |
|
|
259 | 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 | |
260 | svt = SvTYPE (sv); |
345 | svtype svt = SvTYPE (sv); |
261 | |
346 | |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
347 | if (ecb_expect_false (SvOBJECT (sv))) |
263 | { |
348 | { |
264 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
349 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
265 | ? types_boolean_stash |
350 | ? types_boolean_stash |
… | |
… | |
325 | |
410 | |
326 | // catch this surprisingly common error |
411 | // catch this surprisingly common error |
327 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
412 | 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)); |
413 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
329 | |
414 | |
330 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
415 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
331 | encode_uint (enc, 0x80, count + 1); |
416 | encode_uint (enc, 0x80, count + 1); |
332 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
417 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
333 | |
418 | |
334 | while (count) |
419 | while (count) |
335 | encode_sv (enc, SP[1 - count--]); |
420 | encode_sv (enc, SP[1 - count--]); |
… | |
… | |
344 | } |
429 | } |
345 | else if (svt == SVt_PVHV) |
430 | else if (svt == SVt_PVHV) |
346 | encode_hv (enc, (HV *)sv); |
431 | encode_hv (enc, (HV *)sv); |
347 | else if (svt == SVt_PVAV) |
432 | else if (svt == SVt_PVAV) |
348 | encode_av (enc, (AV *)sv); |
433 | 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 |
434 | else |
367 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
435 | { |
368 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
436 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
437 | encode_sv (enc, sv); |
|
|
438 | } |
369 | } |
439 | } |
370 | |
440 | |
371 | static void |
441 | static void |
372 | encode_nv (enc_t *enc, SV *sv) |
442 | encode_nv (enc_t *enc, SV *sv) |
373 | { |
443 | { |
… | |
… | |
438 | } |
508 | } |
439 | |
509 | |
440 | static SV * |
510 | static SV * |
441 | encode_cbor (SV *scalar, CBOR *cbor) |
511 | encode_cbor (SV *scalar, CBOR *cbor) |
442 | { |
512 | { |
443 | enc_t enc; |
513 | enc_t enc = { }; |
444 | |
514 | |
445 | enc.cbor = *cbor; |
515 | enc.cbor = *cbor; |
446 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
447 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
448 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
449 | enc.depth = 0; |
|
|
450 | |
519 | |
451 | 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 | |
452 | encode_sv (&enc, scalar); |
529 | encode_sv (&enc, scalar); |
453 | |
530 | |
454 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
531 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
455 | *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 |
456 | |
533 | |
… | |
… | |
470 | U8 *end; // end of input string |
547 | U8 *end; // end of input string |
471 | const char *err; // parse error, if != 0 |
548 | const char *err; // parse error, if != 0 |
472 | CBOR cbor; |
549 | CBOR cbor; |
473 | U32 depth; // recursion depth |
550 | U32 depth; // recursion depth |
474 | U32 maxdepth; // recursion depth limit |
551 | U32 maxdepth; // recursion depth limit |
|
|
552 | AV *shareable; |
|
|
553 | AV *stringref; |
|
|
554 | SV *decode_tagged; |
475 | } dec_t; |
555 | } dec_t; |
476 | |
556 | |
477 | #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 |
478 | |
558 | |
479 | #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") |
… | |
… | |
573 | SvREFCNT_dec (av); |
653 | SvREFCNT_dec (av); |
574 | DEC_DEC_DEPTH; |
654 | DEC_DEC_DEPTH; |
575 | return &PL_sv_undef; |
655 | return &PL_sv_undef; |
576 | } |
656 | } |
577 | |
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 | |
578 | static SV * |
701 | static SV * |
579 | decode_hv (dec_t *dec) |
702 | decode_hv (dec_t *dec) |
580 | { |
703 | { |
581 | HV *hv = newHV (); |
704 | HV *hv = newHV (); |
582 | |
705 | |
… | |
… | |
594 | { |
717 | { |
595 | ++dec->cur; |
718 | ++dec->cur; |
596 | break; |
719 | break; |
597 | } |
720 | } |
598 | |
721 | |
599 | SV *k = decode_sv (dec); |
722 | decode_he (dec, hv); |
600 | SV *v = decode_sv (dec); |
|
|
601 | |
|
|
602 | hv_store_ent (hv, k, v, 0); |
|
|
603 | SvREFCNT_dec (k); |
|
|
604 | } |
723 | } |
605 | } |
724 | } |
606 | else |
725 | else |
607 | { |
726 | { |
608 | int len = decode_uint (dec); |
727 | int pairs = decode_uint (dec); |
609 | |
728 | |
610 | while (len--) |
729 | while (pairs--) |
611 | { |
730 | decode_he (dec, hv); |
612 | SV *k = decode_sv (dec); |
|
|
613 | SV *v = decode_sv (dec); |
|
|
614 | |
|
|
615 | hv_store_ent (hv, k, v, 0); |
|
|
616 | SvREFCNT_dec (k); |
|
|
617 | } |
|
|
618 | } |
731 | } |
619 | |
732 | |
620 | DEC_DEC_DEPTH; |
733 | DEC_DEC_DEPTH; |
621 | return newRV_noinc ((SV *)hv); |
734 | return newRV_noinc ((SV *)hv); |
622 | |
735 | |
… | |
… | |
656 | STRLEN len = decode_uint (dec); |
769 | STRLEN len = decode_uint (dec); |
657 | |
770 | |
658 | WANT (len); |
771 | WANT (len); |
659 | sv = newSVpvn (dec->cur, len); |
772 | sv = newSVpvn (dec->cur, len); |
660 | 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)); |
661 | } |
778 | } |
662 | |
779 | |
663 | if (utf8) |
780 | if (utf8) |
664 | SvUTF8_on (sv); |
781 | SvUTF8_on (sv); |
665 | |
782 | |
… | |
… | |
671 | } |
788 | } |
672 | |
789 | |
673 | static SV * |
790 | static SV * |
674 | decode_tagged (dec_t *dec) |
791 | decode_tagged (dec_t *dec) |
675 | { |
792 | { |
|
|
793 | SV *sv = 0; |
676 | 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 | |
677 | SV *sv = decode_sv (dec); |
843 | SV *osv = decode_sv (dec); |
|
|
844 | sv_setsv (sv, osv); |
|
|
845 | SvREFCNT_dec_NN (osv); |
|
|
846 | } |
|
|
847 | break; |
678 | |
848 | |
679 | if (tag == CBOR_TAG_MAGIC) |
849 | case CBOR_TAG_VALUE_SHAREDREF: |
680 | return sv; |
850 | { |
681 | else if (tag == CBOR_TAG_PERL_OBJECT) |
851 | if ((*dec->cur >> 5) != 0) |
682 | { |
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 | |
683 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
867 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
684 | ERR ("corrupted CBOR data (non-array perl object)"); |
868 | ERR ("corrupted CBOR data (non-array perl object)"); |
685 | |
869 | |
686 | AV *av = (AV *)SvRV (sv); |
870 | AV *av = (AV *)SvRV (sv); |
687 | int len = av_len (av) + 1; |
871 | int len = av_len (av) + 1; |
688 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
872 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
689 | |
873 | |
690 | if (!stash) |
874 | if (!stash) |
691 | ERR ("cannot decode perl-object (package does not exist)"); |
875 | ERR ("cannot decode perl-object (package does not exist)"); |
692 | |
876 | |
693 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
877 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
694 | |
878 | |
695 | if (!method) |
879 | if (!method) |
696 | 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)"); |
697 | |
881 | |
698 | dSP; |
882 | dSP; |
699 | |
883 | |
700 | ENTER; SAVETMPS; PUSHMARK (SP); |
884 | ENTER; SAVETMPS; PUSHMARK (SP); |
701 | EXTEND (SP, len + 1); |
885 | EXTEND (SP, len + 1); |
702 | // 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 |
703 | PUSHs (*av_fetch (av, 0, 1)); |
887 | PUSHs (*av_fetch (av, 0, 1)); |
704 | PUSHs (sv_cbor); |
888 | PUSHs (sv_cbor); |
705 | |
889 | |
706 | int i; |
890 | int i; |
707 | |
891 | |
708 | for (i = 1; i < len; ++i) |
892 | for (i = 1; i < len; ++i) |
709 | PUSHs (*av_fetch (av, i, 1)); |
893 | PUSHs (*av_fetch (av, i, 1)); |
710 | |
894 | |
711 | PUTBACK; |
895 | PUTBACK; |
712 | call_sv ((SV *)GvCV (method), G_SCALAR); |
896 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
713 | SPAGAIN; |
897 | SPAGAIN; |
714 | |
898 | |
|
|
899 | if (SvTRUE (ERRSV)) |
|
|
900 | { |
|
|
901 | FREETMPS; LEAVE; |
|
|
902 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
903 | } |
|
|
904 | |
715 | SvREFCNT_dec (sv); |
905 | SvREFCNT_dec (sv); |
716 | sv = SvREFCNT_inc (POPs); |
906 | sv = SvREFCNT_inc (POPs); |
717 | |
907 | |
718 | PUTBACK; |
908 | PUTBACK; |
719 | |
909 | |
720 | FREETMPS; LEAVE; |
910 | FREETMPS; LEAVE; |
|
|
911 | } |
|
|
912 | break; |
721 | |
913 | |
722 | return sv; |
914 | default: |
723 | } |
915 | { |
724 | else |
916 | sv = decode_sv (dec); |
725 | { |
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 | { |
726 | AV *av = newAV (); |
941 | AV *av = newAV (); |
727 | av_push (av, newSVuv (tag)); |
942 | av_push (av, newSVuv (tag)); |
728 | av_push (av, sv); |
943 | av_push (av, sv); |
729 | |
944 | |
730 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
945 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
731 | ? cbor_tagged_stash |
946 | ? cbor_tagged_stash |
732 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
947 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
733 | |
|
|
734 | 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; |
735 | } |
956 | } |
|
|
957 | |
|
|
958 | return sv; |
736 | |
959 | |
737 | fail: |
960 | fail: |
738 | SvREFCNT_dec (sv); |
961 | SvREFCNT_dec (sv); |
739 | return &PL_sv_undef; |
962 | return &PL_sv_undef; |
740 | } |
963 | } |
… | |
… | |
831 | } |
1054 | } |
832 | |
1055 | |
833 | static SV * |
1056 | static SV * |
834 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1057 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
835 | { |
1058 | { |
836 | dec_t dec; |
1059 | dec_t dec = { }; |
837 | SV *sv; |
1060 | SV *sv; |
|
|
1061 | STRLEN len; |
|
|
1062 | char *data = SvPVbyte (string, len); |
838 | |
1063 | |
839 | /* work around bugs in 5.10 where manipulating magic values |
|
|
840 | * makes perl ignore the magic in subsequent accesses. |
|
|
841 | * also make a copy of non-PV values, to get them into a clean |
|
|
842 | * state (SvPV should do that, but it's buggy, see below). |
|
|
843 | */ |
|
|
844 | /*SvGETMAGIC (string);*/ |
|
|
845 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
846 | string = sv_2mortal (newSVsv (string)); |
|
|
847 | |
|
|
848 | SvUPGRADE (string, SVt_PV); |
|
|
849 | |
|
|
850 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
851 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
852 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
853 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
854 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
855 | * and hope for the best. |
|
|
856 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
857 | * assertion business is seriously broken, try yet another workaround |
|
|
858 | * for the broken -DDEBUGGING. |
|
|
859 | */ |
|
|
860 | { |
|
|
861 | #ifdef DEBUGGING |
|
|
862 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
863 | #else |
|
|
864 | STRLEN offset = SvCUR (string); |
|
|
865 | #endif |
|
|
866 | |
|
|
867 | if (offset > cbor->max_size && cbor->max_size) |
1064 | if (len > cbor->max_size && cbor->max_size) |
868 | 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", |
869 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
1066 | (unsigned long)len, (unsigned long)cbor->max_size); |
870 | } |
|
|
871 | |
|
|
872 | sv_utf8_downgrade (string, 0); |
|
|
873 | |
1067 | |
874 | dec.cbor = *cbor; |
1068 | dec.cbor = *cbor; |
875 | dec.cur = (U8 *)SvPVX (string); |
1069 | dec.cur = (U8 *)data; |
876 | dec.end = (U8 *)SvEND (string); |
1070 | dec.end = (U8 *)data + len; |
877 | dec.err = 0; |
|
|
878 | dec.depth = 0; |
|
|
879 | |
1071 | |
880 | sv = decode_sv (&dec); |
1072 | sv = decode_sv (&dec); |
881 | |
1073 | |
882 | if (offset_return) |
1074 | if (offset_return) |
883 | *offset_return = dec.cur; |
1075 | *offset_return = dec.cur; |
… | |
… | |
887 | dec.err = "garbage after CBOR object"; |
1079 | dec.err = "garbage after CBOR object"; |
888 | |
1080 | |
889 | if (dec.err) |
1081 | if (dec.err) |
890 | { |
1082 | { |
891 | SvREFCNT_dec (sv); |
1083 | SvREFCNT_dec (sv); |
892 | 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); |
893 | } |
1085 | } |
894 | |
1086 | |
895 | sv = sv_2mortal (sv); |
1087 | sv = sv_2mortal (sv); |
896 | |
1088 | |
897 | return sv; |
1089 | return sv; |
… | |
… | |
911 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1103 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
912 | |
1104 | |
913 | types_true = get_bool ("Types::Serialiser::true" ); |
1105 | types_true = get_bool ("Types::Serialiser::true" ); |
914 | types_false = get_bool ("Types::Serialiser::false"); |
1106 | types_false = get_bool ("Types::Serialiser::false"); |
915 | 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); |
916 | |
1110 | |
917 | sv_cbor = newSVpv ("CBOR", 0); |
1111 | sv_cbor = newSVpv ("CBOR", 0); |
918 | SvREADONLY_on (sv_cbor); |
1112 | SvREADONLY_on (sv_cbor); |
919 | } |
1113 | } |
920 | |
1114 | |
… | |
… | |
941 | |
1135 | |
942 | void shrink (CBOR *self, int enable = 1) |
1136 | void shrink (CBOR *self, int enable = 1) |
943 | ALIAS: |
1137 | ALIAS: |
944 | shrink = F_SHRINK |
1138 | shrink = F_SHRINK |
945 | allow_unknown = F_ALLOW_UNKNOWN |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1140 | allow_sharing = F_ALLOW_SHARING |
|
|
1141 | allow_stringref = F_ALLOW_STRINGREF |
946 | PPCODE: |
1142 | PPCODE: |
947 | { |
1143 | { |
948 | if (enable) |
1144 | if (enable) |
949 | self->flags |= ix; |
1145 | self->flags |= ix; |
950 | else |
1146 | else |
… | |
… | |
955 | |
1151 | |
956 | void get_shrink (CBOR *self) |
1152 | void get_shrink (CBOR *self) |
957 | ALIAS: |
1153 | ALIAS: |
958 | get_shrink = F_SHRINK |
1154 | get_shrink = F_SHRINK |
959 | 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 |
960 | PPCODE: |
1158 | PPCODE: |
961 | XPUSHs (boolSV (self->flags & ix)); |
1159 | XPUSHs (boolSV (self->flags & ix)); |
962 | |
1160 | |
963 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
964 | PPCODE: |
1162 | PPCODE: |
… | |
… | |
980 | CODE: |
1178 | CODE: |
981 | RETVAL = self->max_size; |
1179 | RETVAL = self->max_size; |
982 | OUTPUT: |
1180 | OUTPUT: |
983 | RETVAL |
1181 | RETVAL |
984 | |
1182 | |
985 | #if 0 //TODO |
1183 | void filter (CBOR *self, SV *filter = 0) |
986 | |
|
|
987 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
988 | PPCODE: |
1184 | PPCODE: |
989 | { |
|
|
990 | SvREFCNT_dec (self->cb_object); |
1185 | SvREFCNT_dec (self->filter); |
991 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1186 | self->filter = filter ? newSVsv (filter) : filter; |
992 | |
|
|
993 | XPUSHs (ST (0)); |
1187 | XPUSHs (ST (0)); |
994 | } |
|
|
995 | |
1188 | |
996 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
1189 | SV *get_filter (CBOR *self) |
997 | PPCODE: |
1190 | CODE: |
998 | { |
1191 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
999 | if (!self->cb_sk_object) |
1192 | OUTPUT: |
1000 | self->cb_sk_object = newHV (); |
1193 | RETVAL |
1001 | |
|
|
1002 | if (SvOK (cb)) |
|
|
1003 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1004 | else |
|
|
1005 | { |
|
|
1006 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1007 | |
|
|
1008 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1009 | { |
|
|
1010 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1011 | self->cb_sk_object = 0; |
|
|
1012 | } |
|
|
1013 | } |
|
|
1014 | |
|
|
1015 | XPUSHs (ST (0)); |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | #endif |
|
|
1019 | |
1194 | |
1020 | void encode (CBOR *self, SV *scalar) |
1195 | void encode (CBOR *self, SV *scalar) |
1021 | PPCODE: |
1196 | PPCODE: |
1022 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1197 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1023 | XPUSHs (scalar); |
1198 | XPUSHs (scalar); |
… | |
… | |
1036 | EXTEND (SP, 2); |
1211 | EXTEND (SP, 2); |
1037 | PUSHs (sv); |
1212 | PUSHs (sv); |
1038 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1213 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1039 | } |
1214 | } |
1040 | |
1215 | |
1041 | #if 0 |
|
|
1042 | |
|
|
1043 | void DESTROY (CBOR *self) |
1216 | void DESTROY (CBOR *self) |
1044 | CODE: |
1217 | PPCODE: |
1045 | SvREFCNT_dec (self->cb_sk_object); |
1218 | cbor_free (self); |
1046 | SvREFCNT_dec (self->cb_object); |
|
|
1047 | |
|
|
1048 | #endif |
|
|
1049 | |
1219 | |
1050 | PROTOTYPES: ENABLE |
1220 | PROTOTYPES: ENABLE |
1051 | |
1221 | |
1052 | void encode_cbor (SV *scalar) |
1222 | void encode_cbor (SV *scalar) |
1053 | PPCODE: |
1223 | PPCODE: |