… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
20 | #endif |
21 | #ifndef HvNAMEUTF8 |
21 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
24 | |
27 | |
25 | // known tags |
28 | // known tags |
26 | enum cbor_tag |
29 | enum cbor_tag |
27 | { |
30 | { |
28 | // inofficial extensions (pending iana registration) |
31 | // extensions |
|
|
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
37 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // 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 |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
39 | |
37 | // rfc7049 |
40 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
… | |
… | |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
57 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
58 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
57 | }; |
60 | }; |
58 | |
61 | |
59 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
65 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
|
|
64 | |
66 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
68 | |
67 | #define SB do { |
69 | #define SB do { |
68 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
82 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
83 | # define CBOR_STASH cbor_stash |
82 | #endif |
84 | #endif |
83 | |
85 | |
84 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
86 | 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; |
87 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
88 | |
87 | typedef struct { |
89 | typedef struct { |
88 | U32 flags; |
90 | U32 flags; |
89 | U32 max_depth; |
91 | U32 max_depth; |
90 | STRLEN max_size; |
92 | STRLEN max_size; |
|
|
93 | SV *filter; |
91 | } CBOR; |
94 | } CBOR; |
92 | |
95 | |
93 | ecb_inline void |
96 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
97 | cbor_init (CBOR *cbor) |
95 | { |
98 | { |
96 | Zero (cbor, 1, CBOR); |
99 | Zero (cbor, 1, CBOR); |
97 | cbor->max_depth = 512; |
100 | cbor->max_depth = 512; |
|
|
101 | } |
|
|
102 | |
|
|
103 | ecb_inline void |
|
|
104 | cbor_free (CBOR *cbor) |
|
|
105 | { |
|
|
106 | SvREFCNT_dec (cbor->filter); |
98 | } |
107 | } |
99 | |
108 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
109 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
110 | // utility functions |
102 | |
111 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
133 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
134 | #endif |
126 | } |
135 | } |
127 | } |
136 | } |
128 | |
137 | |
|
|
138 | // minimum length of a string to be registered for stringref |
|
|
139 | ecb_inline int |
|
|
140 | minimum_string_length (UV idx) |
|
|
141 | { |
|
|
142 | return idx > 23 |
|
|
143 | ? idx > 0xffU |
|
|
144 | ? idx > 0xffffU |
|
|
145 | ? idx > 0xffffffffU |
|
|
146 | ? 11 |
|
|
147 | : 7 |
|
|
148 | : 5 |
|
|
149 | : 4 |
|
|
150 | : 3; |
|
|
151 | } |
|
|
152 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
153 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
154 | // encoder |
131 | |
155 | |
132 | // structure used for encoding CBOR |
156 | // structure used for encoding CBOR |
133 | typedef struct |
157 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
159 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
160 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
161 | SV *sv; // result scalar |
138 | CBOR cbor; |
162 | CBOR cbor; |
139 | U32 depth; // recursion level |
163 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
164 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
165 | UV stringref_idx; |
141 | HV *sharable; // ptr => index, or 0 |
166 | HV *shareable; // ptr => index, or 0 |
142 | HV *sharable_idx; |
167 | UV shareable_idx; |
143 | } enc_t; |
168 | } enc_t; |
144 | |
169 | |
145 | ecb_inline void |
170 | ecb_inline void |
146 | need (enc_t *enc, STRLEN len) |
171 | need (enc_t *enc, STRLEN len) |
147 | { |
172 | { |
… | |
… | |
164 | static void |
189 | static void |
165 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
166 | { |
191 | { |
167 | need (enc, 9); |
192 | need (enc, 9); |
168 | |
193 | |
169 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
170 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
171 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
172 | { |
197 | { |
173 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
174 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
175 | } |
200 | } |
176 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
224 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
225 | *enc->cur++ = len; |
201 | } |
226 | } |
202 | } |
227 | } |
203 | |
228 | |
204 | static void |
229 | ecb_inline void |
|
|
230 | encode_tag (enc_t *enc, UV tag) |
|
|
231 | { |
|
|
232 | encode_uint (enc, 0xc0, tag); |
|
|
233 | } |
|
|
234 | |
|
|
235 | ecb_inline void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
237 | { |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
208 | need (enc, len); |
239 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
240 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
241 | enc->cur += len; |
211 | } |
242 | } |
212 | |
243 | |
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static int |
244 | static void |
220 | encode_sharable2 (enc_t *enc, SV *sv) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
221 | { |
246 | { |
222 | if (!enc->sharable) |
247 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
223 | enc->sharable = (HV *)sv_2mortal ((SV *)newHV ()); |
248 | { |
|
|
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
224 | |
250 | |
225 | SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1); |
|
|
226 | |
|
|
227 | if (SvOK (*svp)) |
251 | if (SvOK (*svp)) |
228 | { |
252 | { |
|
|
253 | // already registered, use stringref |
229 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
254 | encode_tag (enc, CBOR_TAG_STRINGREF); |
230 | encode_uint (enc, 0x00, SvUV (*svp)); |
255 | encode_uint (enc, 0x00, SvUV (*svp)); |
231 | |
|
|
232 | return 1; |
256 | return; |
233 | } |
257 | } |
234 | else |
258 | else if (len >= minimum_string_length (enc->stringref_idx)) |
235 | { |
259 | { |
|
|
260 | // register only |
236 | sv_setuv (*svp, enc->sharable_idx++); |
261 | sv_setuv (*svp, enc->stringref_idx); |
237 | encode_tag (enc, CBOR_TAG_VALUE_SHARABLE); |
262 | ++enc->stringref_idx; |
238 | |
263 | } |
239 | return 0; |
|
|
240 | } |
264 | } |
241 | } |
|
|
242 | |
265 | |
243 | ecb_inline int |
266 | encode_str (enc, utf8, str, len); |
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 | } |
267 | } |
252 | |
268 | |
253 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
270 | |
255 | static void |
271 | static void |
… | |
… | |
294 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
295 | { |
311 | { |
296 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
297 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
298 | else |
314 | else |
299 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
300 | |
316 | |
301 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
317 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
302 | } |
318 | } |
303 | |
319 | |
304 | if (mg) |
320 | if (mg) |
… | |
… | |
309 | |
325 | |
310 | // encode objects, arrays and special \0=false and \1=true values. |
326 | // encode objects, arrays and special \0=false and \1=true values. |
311 | static void |
327 | static void |
312 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
313 | { |
329 | { |
314 | svtype svt; |
|
|
315 | |
|
|
316 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
|
|
331 | |
|
|
332 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
333 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
334 | { |
|
|
335 | if (!enc->shareable) |
|
|
336 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
337 | |
|
|
338 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
339 | |
|
|
340 | if (SvOK (*svp)) |
|
|
341 | { |
|
|
342 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
343 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
344 | return; |
|
|
345 | } |
|
|
346 | else |
|
|
347 | { |
|
|
348 | sv_setuv (*svp, enc->shareable_idx); |
|
|
349 | ++enc->shareable_idx; |
|
|
350 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
351 | } |
|
|
352 | } |
|
|
353 | |
317 | svt = SvTYPE (sv); |
354 | svtype svt = SvTYPE (sv); |
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
|
|
321 | |
355 | |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
356 | if (ecb_expect_false (SvOBJECT (sv))) |
323 | { |
357 | { |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
358 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
325 | ? types_boolean_stash |
359 | ? types_boolean_stash |
… | |
… | |
387 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
421 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
388 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
422 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
389 | |
423 | |
390 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
424 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
391 | encode_uint (enc, 0x80, count + 1); |
425 | encode_uint (enc, 0x80, count + 1); |
392 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
426 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
393 | |
427 | |
394 | while (count) |
428 | while (count) |
395 | encode_sv (enc, SP[1 - count--]); |
429 | encode_sv (enc, SP[1 - count--]); |
396 | |
430 | |
397 | PUTBACK; |
431 | PUTBACK; |
… | |
… | |
452 | static void |
486 | static void |
453 | encode_sv (enc_t *enc, SV *sv) |
487 | encode_sv (enc_t *enc, SV *sv) |
454 | { |
488 | { |
455 | SvGETMAGIC (sv); |
489 | SvGETMAGIC (sv); |
456 | |
490 | |
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
|
|
459 | |
|
|
460 | if (SvPOKp (sv)) |
491 | if (SvPOKp (sv)) |
461 | { |
492 | { |
462 | STRLEN len; |
493 | STRLEN len; |
463 | char *str = SvPV (sv, len); |
494 | char *str = SvPV (sv, len); |
464 | encode_str (enc, SvUTF8 (sv), str, len); |
495 | encode_strref (enc, SvUTF8 (sv), str, len); |
465 | } |
496 | } |
466 | else if (SvNOKp (sv)) |
497 | else if (SvNOKp (sv)) |
467 | encode_nv (enc, sv); |
498 | encode_nv (enc, sv); |
468 | else if (SvIOKp (sv)) |
499 | else if (SvIOKp (sv)) |
469 | { |
500 | { |
… | |
… | |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
525 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
495 | enc.cur = SvPVX (enc.sv); |
526 | enc.cur = SvPVX (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
527 | enc.end = SvEND (enc.sv); |
497 | |
528 | |
498 | SvPOK_only (enc.sv); |
529 | SvPOK_only (enc.sv); |
|
|
530 | |
|
|
531 | if (cbor->flags & F_ALLOW_STRINGREF) |
|
|
532 | { |
|
|
533 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
534 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
535 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
536 | } |
|
|
537 | |
499 | encode_sv (&enc, scalar); |
538 | encode_sv (&enc, scalar); |
500 | |
539 | |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
540 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
502 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
541 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
503 | |
542 | |
… | |
… | |
517 | U8 *end; // end of input string |
556 | U8 *end; // end of input string |
518 | const char *err; // parse error, if != 0 |
557 | const char *err; // parse error, if != 0 |
519 | CBOR cbor; |
558 | CBOR cbor; |
520 | U32 depth; // recursion depth |
559 | U32 depth; // recursion depth |
521 | U32 maxdepth; // recursion depth limit |
560 | U32 maxdepth; // recursion depth limit |
522 | AV *sharable; |
561 | AV *shareable; |
|
|
562 | AV *stringref; |
|
|
563 | SV *decode_tagged; |
523 | } dec_t; |
564 | } dec_t; |
524 | |
565 | |
525 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
566 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
526 | |
567 | |
527 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
568 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
625 | |
666 | |
626 | static void |
667 | static void |
627 | decode_he (dec_t *dec, HV *hv) |
668 | decode_he (dec_t *dec, HV *hv) |
628 | { |
669 | { |
629 | // for speed reasons, we specialcase single-string |
670 | // for speed reasons, we specialcase single-string |
630 | // byte or utf-8 strings as keys. |
671 | // byte or utf-8 strings as keys, but only when !stringref |
631 | |
672 | |
|
|
673 | if (ecb_expect_true (!dec->stringref)) |
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
674 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
633 | { |
675 | { |
634 | I32 len = decode_uint (dec); |
676 | I32 len = decode_uint (dec); |
635 | char *key = (char *)dec->cur; |
677 | char *key = (char *)dec->cur; |
636 | |
678 | |
637 | dec->cur += len; |
679 | dec->cur += len; |
638 | |
680 | |
|
|
681 | if (ecb_expect_false (dec->stringref)) |
|
|
682 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
683 | |
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
684 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
685 | |
|
|
686 | return; |
640 | } |
687 | } |
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
688 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
642 | { |
689 | { |
643 | I32 len = decode_uint (dec); |
690 | I32 len = decode_uint (dec); |
644 | char *key = (char *)dec->cur; |
691 | char *key = (char *)dec->cur; |
645 | |
692 | |
646 | dec->cur += len; |
693 | dec->cur += len; |
647 | |
694 | |
|
|
695 | if (ecb_expect_false (dec->stringref)) |
|
|
696 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
697 | |
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
698 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
699 | |
|
|
700 | return; |
649 | } |
701 | } |
650 | else |
702 | |
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
703 | SV *k = decode_sv (dec); |
653 | SV *v = decode_sv (dec); |
704 | SV *v = decode_sv (dec); |
654 | |
705 | |
655 | hv_store_ent (hv, k, v, 0); |
706 | hv_store_ent (hv, k, v, 0); |
656 | SvREFCNT_dec (k); |
707 | SvREFCNT_dec (k); |
657 | } |
|
|
658 | } |
708 | } |
659 | |
709 | |
660 | static SV * |
710 | static SV * |
661 | decode_hv (dec_t *dec) |
711 | decode_hv (dec_t *dec) |
662 | { |
712 | { |
… | |
… | |
728 | STRLEN len = decode_uint (dec); |
778 | STRLEN len = decode_uint (dec); |
729 | |
779 | |
730 | WANT (len); |
780 | WANT (len); |
731 | sv = newSVpvn (dec->cur, len); |
781 | sv = newSVpvn (dec->cur, len); |
732 | dec->cur += len; |
782 | dec->cur += len; |
|
|
783 | |
|
|
784 | if (ecb_expect_false (dec->stringref) |
|
|
785 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
786 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
733 | } |
787 | } |
734 | |
788 | |
735 | if (utf8) |
789 | if (utf8) |
736 | SvUTF8_on (sv); |
790 | SvUTF8_on (sv); |
737 | |
791 | |
… | |
… | |
743 | } |
797 | } |
744 | |
798 | |
745 | static SV * |
799 | static SV * |
746 | decode_tagged (dec_t *dec) |
800 | decode_tagged (dec_t *dec) |
747 | { |
801 | { |
|
|
802 | SV *sv = 0; |
748 | UV tag = decode_uint (dec); |
803 | UV tag = decode_uint (dec); |
749 | SV *sv = decode_sv (dec); |
804 | |
|
|
805 | WANT (1); |
750 | |
806 | |
751 | switch (tag) |
807 | switch (tag) |
752 | { |
808 | { |
753 | case CBOR_TAG_MAGIC: |
809 | case CBOR_TAG_MAGIC: |
754 | return sv; |
810 | sv = decode_sv (dec); |
|
|
811 | break; |
755 | |
812 | |
756 | case CBOR_TAG_INDIRECTION: |
813 | case CBOR_TAG_INDIRECTION: |
757 | return newRV_noinc (sv); |
814 | sv = newRV_noinc (decode_sv (dec)); |
|
|
815 | break; |
758 | |
816 | |
|
|
817 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
818 | { |
|
|
819 | ENTER; SAVETMPS; |
|
|
820 | |
|
|
821 | SAVESPTR (dec->stringref); |
|
|
822 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
823 | |
|
|
824 | sv = decode_sv (dec); |
|
|
825 | |
|
|
826 | FREETMPS; LEAVE; |
|
|
827 | } |
|
|
828 | break; |
|
|
829 | |
|
|
830 | case CBOR_TAG_STRINGREF: |
|
|
831 | { |
|
|
832 | if ((*dec->cur >> 5) != 0) |
|
|
833 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
834 | |
|
|
835 | UV idx = decode_uint (dec); |
|
|
836 | |
|
|
837 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
838 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
839 | |
|
|
840 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
841 | } |
|
|
842 | break; |
|
|
843 | |
759 | case CBOR_TAG_VALUE_SHARABLE: |
844 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
845 | { |
760 | if (ecb_expect_false (!dec->sharable)) |
846 | if (ecb_expect_false (!dec->shareable)) |
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
847 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
762 | |
848 | |
|
|
849 | sv = newSV (0); |
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
850 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
764 | |
851 | |
765 | return sv; |
852 | SV *osv = decode_sv (dec); |
|
|
853 | sv_setsv (sv, osv); |
|
|
854 | SvREFCNT_dec_NN (osv); |
|
|
855 | } |
|
|
856 | break; |
766 | |
857 | |
767 | case CBOR_TAG_VALUE_SHAREDREF: |
858 | case CBOR_TAG_VALUE_SHAREDREF: |
768 | { |
859 | { |
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
860 | if ((*dec->cur >> 5) != 0) |
770 | UV idx = SvUV (sv); |
861 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
771 | |
862 | |
|
|
863 | UV idx = decode_uint (dec); |
|
|
864 | |
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
865 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
866 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
774 | |
867 | |
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
868 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
778 | } |
869 | } |
|
|
870 | break; |
779 | |
871 | |
780 | case CBOR_TAG_PERL_OBJECT: |
872 | case CBOR_TAG_PERL_OBJECT: |
781 | { |
873 | { |
|
|
874 | sv = decode_sv (dec); |
|
|
875 | |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
876 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
877 | ERR ("corrupted CBOR data (non-array perl object)"); |
784 | |
878 | |
785 | AV *av = (AV *)SvRV (sv); |
879 | AV *av = (AV *)SvRV (sv); |
786 | int len = av_len (av) + 1; |
880 | int len = av_len (av) + 1; |
… | |
… | |
821 | sv = SvREFCNT_inc (POPs); |
915 | sv = SvREFCNT_inc (POPs); |
822 | |
916 | |
823 | PUTBACK; |
917 | PUTBACK; |
824 | |
918 | |
825 | FREETMPS; LEAVE; |
919 | FREETMPS; LEAVE; |
826 | |
|
|
827 | return sv; |
|
|
828 | } |
920 | } |
|
|
921 | break; |
829 | |
922 | |
830 | default: |
923 | default: |
831 | { |
924 | { |
|
|
925 | sv = decode_sv (dec); |
|
|
926 | |
|
|
927 | dSP; |
|
|
928 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
929 | EXTEND (SP, 2); |
|
|
930 | PUSHs (newSVuv (tag)); |
|
|
931 | PUSHs (sv); |
|
|
932 | |
|
|
933 | PUTBACK; |
|
|
934 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
935 | SPAGAIN; |
|
|
936 | |
|
|
937 | if (SvTRUE (ERRSV)) |
|
|
938 | { |
|
|
939 | FREETMPS; LEAVE; |
|
|
940 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
941 | } |
|
|
942 | |
|
|
943 | if (count) |
|
|
944 | { |
|
|
945 | SvREFCNT_dec (sv); |
|
|
946 | sv = SvREFCNT_inc (POPs); |
|
|
947 | } |
|
|
948 | else |
|
|
949 | { |
832 | AV *av = newAV (); |
950 | AV *av = newAV (); |
833 | av_push (av, newSVuv (tag)); |
951 | av_push (av, newSVuv (tag)); |
834 | av_push (av, sv); |
952 | av_push (av, sv); |
835 | |
953 | |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
954 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
837 | ? cbor_tagged_stash |
955 | ? cbor_tagged_stash |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
956 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
839 | |
|
|
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
957 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
841 | } |
958 | } |
|
|
959 | |
|
|
960 | PUTBACK; |
|
|
961 | |
|
|
962 | FREETMPS; LEAVE; |
|
|
963 | } |
|
|
964 | break; |
842 | } |
965 | } |
|
|
966 | |
|
|
967 | return sv; |
843 | |
968 | |
844 | fail: |
969 | fail: |
845 | SvREFCNT_dec (sv); |
970 | SvREFCNT_dec (sv); |
846 | return &PL_sv_undef; |
971 | return &PL_sv_undef; |
847 | } |
972 | } |
… | |
… | |
988 | |
1113 | |
989 | types_true = get_bool ("Types::Serialiser::true" ); |
1114 | types_true = get_bool ("Types::Serialiser::true" ); |
990 | types_false = get_bool ("Types::Serialiser::false"); |
1115 | types_false = get_bool ("Types::Serialiser::false"); |
991 | types_error = get_bool ("Types::Serialiser::error"); |
1116 | types_error = get_bool ("Types::Serialiser::error"); |
992 | |
1117 | |
|
|
1118 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1119 | |
993 | sv_cbor = newSVpv ("CBOR", 0); |
1120 | sv_cbor = newSVpv ("CBOR", 0); |
994 | SvREADONLY_on (sv_cbor); |
1121 | SvREADONLY_on (sv_cbor); |
995 | } |
1122 | } |
996 | |
1123 | |
997 | PROTOTYPES: DISABLE |
1124 | PROTOTYPES: DISABLE |
… | |
… | |
1018 | void shrink (CBOR *self, int enable = 1) |
1145 | void shrink (CBOR *self, int enable = 1) |
1019 | ALIAS: |
1146 | ALIAS: |
1020 | shrink = F_SHRINK |
1147 | shrink = F_SHRINK |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
1148 | allow_unknown = F_ALLOW_UNKNOWN |
1022 | allow_sharing = F_ALLOW_SHARING |
1149 | allow_sharing = F_ALLOW_SHARING |
1023 | dedup_keys = F_DEDUP_KEYS |
1150 | allow_stringref = F_ALLOW_STRINGREF |
1024 | dedup_strings = F_DEDUP_STRINGS |
|
|
1025 | PPCODE: |
1151 | PPCODE: |
1026 | { |
1152 | { |
1027 | if (enable) |
1153 | if (enable) |
1028 | self->flags |= ix; |
1154 | self->flags |= ix; |
1029 | else |
1155 | else |
… | |
… | |
1035 | void get_shrink (CBOR *self) |
1161 | void get_shrink (CBOR *self) |
1036 | ALIAS: |
1162 | ALIAS: |
1037 | get_shrink = F_SHRINK |
1163 | get_shrink = F_SHRINK |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
1164 | get_allow_unknown = F_ALLOW_UNKNOWN |
1039 | get_allow_sharing = F_ALLOW_SHARING |
1165 | get_allow_sharing = F_ALLOW_SHARING |
1040 | get_dedup_keys = F_DEDUP_KEYS |
1166 | get_allow_stringref = F_ALLOW_STRINGREF |
1041 | get_dedup_strings = F_DEDUP_STRINGS |
|
|
1042 | PPCODE: |
1167 | PPCODE: |
1043 | XPUSHs (boolSV (self->flags & ix)); |
1168 | XPUSHs (boolSV (self->flags & ix)); |
1044 | |
1169 | |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1170 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1046 | PPCODE: |
1171 | PPCODE: |
… | |
… | |
1059 | XPUSHs (ST (0)); |
1184 | XPUSHs (ST (0)); |
1060 | |
1185 | |
1061 | int get_max_size (CBOR *self) |
1186 | int get_max_size (CBOR *self) |
1062 | CODE: |
1187 | CODE: |
1063 | RETVAL = self->max_size; |
1188 | RETVAL = self->max_size; |
|
|
1189 | OUTPUT: |
|
|
1190 | RETVAL |
|
|
1191 | |
|
|
1192 | void filter (CBOR *self, SV *filter = 0) |
|
|
1193 | PPCODE: |
|
|
1194 | SvREFCNT_dec (self->filter); |
|
|
1195 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1196 | XPUSHs (ST (0)); |
|
|
1197 | |
|
|
1198 | SV *get_filter (CBOR *self) |
|
|
1199 | CODE: |
|
|
1200 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1064 | OUTPUT: |
1201 | OUTPUT: |
1065 | RETVAL |
1202 | RETVAL |
1066 | |
1203 | |
1067 | void encode (CBOR *self, SV *scalar) |
1204 | void encode (CBOR *self, SV *scalar) |
1068 | PPCODE: |
1205 | PPCODE: |
… | |
… | |
1083 | EXTEND (SP, 2); |
1220 | EXTEND (SP, 2); |
1084 | PUSHs (sv); |
1221 | PUSHs (sv); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1222 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1086 | } |
1223 | } |
1087 | |
1224 | |
|
|
1225 | void DESTROY (CBOR *self) |
|
|
1226 | PPCODE: |
|
|
1227 | cbor_free (self); |
|
|
1228 | |
1088 | PROTOTYPES: ENABLE |
1229 | PROTOTYPES: ENABLE |
1089 | |
1230 | |
1090 | void encode_cbor (SV *scalar) |
1231 | void encode_cbor (SV *scalar) |
1091 | PPCODE: |
1232 | PPCODE: |
1092 | { |
1233 | { |