… | |
… | |
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_SHAREABLE = 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 |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
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 | |
… | |
… | |
122 | SvPV_shrink_to_cur (sv); |
131 | SvPV_shrink_to_cur (sv); |
123 | #elif defined (SvPV_renew) |
132 | #elif defined (SvPV_renew) |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
133 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
134 | #endif |
126 | } |
135 | } |
|
|
136 | } |
|
|
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; |
127 | } |
151 | } |
128 | |
152 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
153 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
154 | // encoder |
131 | |
155 | |
… | |
… | |
165 | static void |
189 | static void |
166 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
167 | { |
191 | { |
168 | need (enc, 9); |
192 | need (enc, 9); |
169 | |
193 | |
170 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
171 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
172 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
173 | { |
197 | { |
174 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
175 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
176 | } |
200 | } |
177 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
200 | *enc->cur++ = len >> 8; |
224 | *enc->cur++ = len >> 8; |
201 | *enc->cur++ = len; |
225 | *enc->cur++ = len; |
202 | } |
226 | } |
203 | } |
227 | } |
204 | |
228 | |
205 | 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 |
206 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
207 | { |
237 | { |
208 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
209 | need (enc, len); |
239 | need (enc, len); |
210 | memcpy (enc->cur, str, len); |
240 | memcpy (enc->cur, str, len); |
211 | enc->cur += len; |
241 | enc->cur += len; |
212 | } |
242 | } |
213 | |
243 | |
214 | ecb_inline void |
244 | static void |
215 | encode_tag (enc_t *enc, UV tag) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
216 | { |
246 | { |
217 | encode_uint (enc, 0xc0, tag); |
247 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
|
|
248 | { |
|
|
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
250 | |
|
|
251 | if (SvOK (*svp)) |
|
|
252 | { |
|
|
253 | // already registered, use stringref |
|
|
254 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
255 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
256 | return; |
|
|
257 | } |
|
|
258 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
259 | { |
|
|
260 | // register only |
|
|
261 | sv_setuv (*svp, enc->stringref_idx); |
|
|
262 | ++enc->stringref_idx; |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | encode_str (enc, utf8, str, len); |
218 | } |
267 | } |
219 | |
268 | |
220 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
221 | |
270 | |
222 | static void |
271 | static void |
… | |
… | |
236 | SV **svp = av_fetch (av, i, 0); |
285 | SV **svp = av_fetch (av, i, 0); |
237 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
286 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
238 | } |
287 | } |
239 | |
288 | |
240 | --enc->depth; |
289 | --enc->depth; |
241 | } |
|
|
242 | |
|
|
243 | ecb_inline void |
|
|
244 | encode_he (enc_t *enc, HE *he) |
|
|
245 | { |
|
|
246 | if (HeKLEN (he) == HEf_SVKEY) |
|
|
247 | encode_sv (enc, HeSVKEY (he)); |
|
|
248 | else |
|
|
249 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
|
|
250 | } |
290 | } |
251 | |
291 | |
252 | static void |
292 | static void |
253 | encode_hv (enc_t *enc, HV *hv) |
293 | encode_hv (enc_t *enc, HV *hv) |
254 | { |
294 | { |
… | |
… | |
267 | else |
307 | else |
268 | encode_uint (enc, 0xa0, pairs); |
308 | encode_uint (enc, 0xa0, pairs); |
269 | |
309 | |
270 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
271 | { |
311 | { |
272 | if (ecb_expect_false (enc->cbor.flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS))) |
|
|
273 | { |
|
|
274 | SV **svp; |
|
|
275 | |
|
|
276 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
277 | svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return HE :/ |
313 | encode_sv (enc, HeSVKEY (he)); |
278 | else |
|
|
279 | svp = hv_fetch (enc->stringref[!! HeKUTF8 (he) ], HeKEY (he), HeKLEN (he), 1); |
|
|
280 | |
|
|
281 | if (SvOK (*svp)) |
|
|
282 | { |
|
|
283 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
284 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
285 | } |
|
|
286 | else |
|
|
287 | { |
|
|
288 | sv_setuv (*svp, enc->stringref_idx); |
|
|
289 | ++enc->stringref_idx; |
|
|
290 | encode_he (enc, he); |
|
|
291 | } |
|
|
292 | } |
|
|
293 | else |
314 | else |
294 | encode_he (enc, he); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
295 | |
316 | |
296 | 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)); |
297 | } |
318 | } |
298 | |
319 | |
299 | if (mg) |
320 | if (mg) |
… | |
… | |
305 | // encode objects, arrays and special \0=false and \1=true values. |
326 | // encode objects, arrays and special \0=false and \1=true values. |
306 | static void |
327 | static void |
307 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
308 | { |
329 | { |
309 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
310 | |
|
|
311 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
312 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
313 | { |
|
|
314 | if (!enc->shareable) |
|
|
315 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
316 | |
|
|
317 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
318 | |
|
|
319 | if (SvOK (*svp)) |
|
|
320 | { |
|
|
321 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
322 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
323 | return; |
|
|
324 | } |
|
|
325 | else |
|
|
326 | { |
|
|
327 | sv_setuv (*svp, enc->shareable_idx); |
|
|
328 | ++enc->shareable_idx; |
|
|
329 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
330 | } |
|
|
331 | } |
|
|
332 | |
331 | |
333 | svtype svt = SvTYPE (sv); |
332 | svtype svt = SvTYPE (sv); |
334 | |
333 | |
335 | if (ecb_expect_false (SvOBJECT (sv))) |
334 | if (ecb_expect_false (SvOBJECT (sv))) |
336 | { |
335 | { |
… | |
… | |
400 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
399 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
401 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
400 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
402 | |
401 | |
403 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
402 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
404 | encode_uint (enc, 0x80, count + 1); |
403 | encode_uint (enc, 0x80, count + 1); |
405 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
404 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
406 | |
405 | |
407 | while (count) |
406 | while (count) |
408 | encode_sv (enc, SP[1 - count--]); |
407 | encode_sv (enc, SP[1 - count--]); |
409 | |
408 | |
410 | PUTBACK; |
409 | PUTBACK; |
… | |
… | |
419 | encode_hv (enc, (HV *)sv); |
418 | encode_hv (enc, (HV *)sv); |
420 | else if (svt == SVt_PVAV) |
419 | else if (svt == SVt_PVAV) |
421 | encode_av (enc, (AV *)sv); |
420 | encode_av (enc, (AV *)sv); |
422 | else |
421 | else |
423 | { |
422 | { |
|
|
423 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
424 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
425 | { |
|
|
426 | if (!enc->shareable) |
|
|
427 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
428 | |
|
|
429 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
430 | |
|
|
431 | if (SvOK (*svp)) |
|
|
432 | { |
|
|
433 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
434 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
435 | return; |
|
|
436 | } |
|
|
437 | else |
|
|
438 | { |
|
|
439 | sv_setuv (*svp, enc->shareable_idx); |
|
|
440 | ++enc->shareable_idx; |
|
|
441 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
442 | } |
|
|
443 | } |
|
|
444 | |
424 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
445 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
425 | encode_sv (enc, sv); |
446 | encode_sv (enc, sv); |
426 | } |
447 | } |
427 | } |
448 | } |
428 | |
449 | |
… | |
… | |
469 | |
490 | |
470 | if (SvPOKp (sv)) |
491 | if (SvPOKp (sv)) |
471 | { |
492 | { |
472 | STRLEN len; |
493 | STRLEN len; |
473 | char *str = SvPV (sv, len); |
494 | char *str = SvPV (sv, len); |
474 | encode_str (enc, SvUTF8 (sv), str, len); |
495 | encode_strref (enc, SvUTF8 (sv), str, len); |
475 | } |
496 | } |
476 | else if (SvNOKp (sv)) |
497 | else if (SvNOKp (sv)) |
477 | encode_nv (enc, sv); |
498 | encode_nv (enc, sv); |
478 | else if (SvIOKp (sv)) |
499 | else if (SvIOKp (sv)) |
479 | { |
500 | { |
… | |
… | |
505 | enc.cur = SvPVX (enc.sv); |
526 | enc.cur = SvPVX (enc.sv); |
506 | enc.end = SvEND (enc.sv); |
527 | enc.end = SvEND (enc.sv); |
507 | |
528 | |
508 | SvPOK_only (enc.sv); |
529 | SvPOK_only (enc.sv); |
509 | |
530 | |
510 | if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) |
531 | if (cbor->flags & F_PACK_STRINGS) |
511 | { |
532 | { |
512 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
533 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
513 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
534 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
514 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
535 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
515 | } |
536 | } |
… | |
… | |
537 | CBOR cbor; |
558 | CBOR cbor; |
538 | U32 depth; // recursion depth |
559 | U32 depth; // recursion depth |
539 | U32 maxdepth; // recursion depth limit |
560 | U32 maxdepth; // recursion depth limit |
540 | AV *shareable; |
561 | AV *shareable; |
541 | AV *stringref; |
562 | AV *stringref; |
|
|
563 | SV *decode_tagged; |
542 | } dec_t; |
564 | } dec_t; |
543 | |
565 | |
544 | #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 |
545 | |
567 | |
546 | #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") |
… | |
… | |
644 | |
666 | |
645 | static void |
667 | static void |
646 | decode_he (dec_t *dec, HV *hv) |
668 | decode_he (dec_t *dec, HV *hv) |
647 | { |
669 | { |
648 | // for speed reasons, we specialcase single-string |
670 | // for speed reasons, we specialcase single-string |
649 | // byte or utf-8 strings as keys. |
671 | // byte or utf-8 strings as keys, but only when !stringref |
650 | |
672 | |
|
|
673 | if (ecb_expect_true (!dec->stringref)) |
651 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
674 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
652 | { |
675 | { |
653 | I32 len = decode_uint (dec); |
676 | I32 len = decode_uint (dec); |
654 | char *key = (char *)dec->cur; |
677 | char *key = (char *)dec->cur; |
655 | |
678 | |
656 | dec->cur += len; |
679 | dec->cur += len; |
657 | |
680 | |
658 | if (ecb_expect_false (dec->stringref)) |
681 | if (ecb_expect_false (dec->stringref)) |
659 | av_push (dec->stringref, newSVpvn (key, len)); |
682 | av_push (dec->stringref, newSVpvn (key, len)); |
660 | |
683 | |
661 | hv_store (hv, key, len, decode_sv (dec), 0); |
684 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
685 | |
|
|
686 | return; |
662 | } |
687 | } |
663 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
688 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
664 | { |
689 | { |
665 | I32 len = decode_uint (dec); |
690 | I32 len = decode_uint (dec); |
666 | char *key = (char *)dec->cur; |
691 | char *key = (char *)dec->cur; |
667 | |
692 | |
668 | dec->cur += len; |
693 | dec->cur += len; |
669 | |
694 | |
670 | if (ecb_expect_false (dec->stringref)) |
695 | if (ecb_expect_false (dec->stringref)) |
671 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
696 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
672 | |
697 | |
673 | hv_store (hv, key, -len, decode_sv (dec), 0); |
698 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
699 | |
|
|
700 | return; |
674 | } |
701 | } |
675 | else |
702 | |
676 | { |
|
|
677 | SV *k = decode_sv (dec); |
703 | SV *k = decode_sv (dec); |
678 | SV *v = decode_sv (dec); |
704 | SV *v = decode_sv (dec); |
679 | |
705 | |
680 | hv_store_ent (hv, k, v, 0); |
706 | hv_store_ent (hv, k, v, 0); |
681 | SvREFCNT_dec (k); |
707 | SvREFCNT_dec (k); |
682 | } |
|
|
683 | } |
708 | } |
684 | |
709 | |
685 | static SV * |
710 | static SV * |
686 | decode_hv (dec_t *dec) |
711 | decode_hv (dec_t *dec) |
687 | { |
712 | { |
… | |
… | |
728 | { |
753 | { |
729 | SV *sv = 0; |
754 | SV *sv = 0; |
730 | |
755 | |
731 | if ((*dec->cur & 31) == 31) |
756 | if ((*dec->cur & 31) == 31) |
732 | { |
757 | { |
|
|
758 | // indefinite length strings |
733 | ++dec->cur; |
759 | ++dec->cur; |
734 | |
760 | |
|
|
761 | unsigned char major = *dec->cur & 0xe0; |
|
|
762 | |
735 | sv = newSVpvn ("", 0); |
763 | sv = newSVpvn ("", 0); |
736 | |
764 | |
737 | // not very fast, and certainly not robust against illegal input |
|
|
738 | for (;;) |
765 | for (;;) |
739 | { |
766 | { |
740 | WANT (1); |
767 | WANT (1); |
741 | |
768 | |
|
|
769 | if ((*dec->cur ^ major) >= 31) |
742 | if (*dec->cur == (0xe0 | 31)) |
770 | if (*dec->cur == (0xe0 | 31)) |
743 | { |
771 | { |
744 | ++dec->cur; |
772 | ++dec->cur; |
745 | break; |
773 | break; |
746 | } |
774 | } |
|
|
775 | else |
|
|
776 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
747 | |
777 | |
748 | sv_catsv (sv, decode_sv (dec)); |
778 | STRLEN len = decode_uint (dec); |
|
|
779 | |
|
|
780 | WANT (len); |
|
|
781 | sv_catpvn (sv, dec->cur, len); |
|
|
782 | dec->cur += len; |
749 | } |
783 | } |
750 | } |
784 | } |
751 | else |
785 | else |
752 | { |
786 | { |
753 | STRLEN len = decode_uint (dec); |
787 | STRLEN len = decode_uint (dec); |
754 | |
788 | |
755 | WANT (len); |
789 | WANT (len); |
756 | sv = newSVpvn (dec->cur, len); |
790 | sv = newSVpvn (dec->cur, len); |
757 | dec->cur += len; |
791 | dec->cur += len; |
|
|
792 | |
|
|
793 | if (ecb_expect_false (dec->stringref) |
|
|
794 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
795 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
758 | } |
796 | } |
759 | |
797 | |
760 | if (utf8) |
798 | if (utf8) |
761 | SvUTF8_on (sv); |
799 | SvUTF8_on (sv); |
762 | |
|
|
763 | if (ecb_expect_false (dec->stringref)) |
|
|
764 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
765 | |
800 | |
766 | return sv; |
801 | return sv; |
767 | |
802 | |
768 | fail: |
803 | fail: |
769 | SvREFCNT_dec (sv); |
804 | SvREFCNT_dec (sv); |
… | |
… | |
896 | |
931 | |
897 | default: |
932 | default: |
898 | { |
933 | { |
899 | sv = decode_sv (dec); |
934 | sv = decode_sv (dec); |
900 | |
935 | |
|
|
936 | dSP; |
|
|
937 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
938 | EXTEND (SP, 2); |
|
|
939 | PUSHs (newSVuv (tag)); |
|
|
940 | PUSHs (sv); |
|
|
941 | |
|
|
942 | PUTBACK; |
|
|
943 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
944 | SPAGAIN; |
|
|
945 | |
|
|
946 | if (SvTRUE (ERRSV)) |
|
|
947 | { |
|
|
948 | FREETMPS; LEAVE; |
|
|
949 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
950 | } |
|
|
951 | |
|
|
952 | if (count) |
|
|
953 | { |
|
|
954 | SvREFCNT_dec (sv); |
|
|
955 | sv = SvREFCNT_inc (POPs); |
|
|
956 | } |
|
|
957 | else |
|
|
958 | { |
901 | AV *av = newAV (); |
959 | AV *av = newAV (); |
902 | av_push (av, newSVuv (tag)); |
960 | av_push (av, newSVuv (tag)); |
903 | av_push (av, sv); |
961 | av_push (av, sv); |
904 | |
962 | |
905 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
963 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
906 | ? cbor_tagged_stash |
964 | ? cbor_tagged_stash |
907 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
965 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
908 | |
|
|
909 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
966 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
967 | } |
|
|
968 | |
|
|
969 | PUTBACK; |
|
|
970 | |
|
|
971 | FREETMPS; LEAVE; |
910 | } |
972 | } |
911 | break; |
973 | break; |
912 | } |
974 | } |
913 | |
975 | |
914 | return sv; |
976 | return sv; |
… | |
… | |
1060 | |
1122 | |
1061 | types_true = get_bool ("Types::Serialiser::true" ); |
1123 | types_true = get_bool ("Types::Serialiser::true" ); |
1062 | types_false = get_bool ("Types::Serialiser::false"); |
1124 | types_false = get_bool ("Types::Serialiser::false"); |
1063 | types_error = get_bool ("Types::Serialiser::error"); |
1125 | types_error = get_bool ("Types::Serialiser::error"); |
1064 | |
1126 | |
|
|
1127 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1128 | |
1065 | sv_cbor = newSVpv ("CBOR", 0); |
1129 | sv_cbor = newSVpv ("CBOR", 0); |
1066 | SvREADONLY_on (sv_cbor); |
1130 | SvREADONLY_on (sv_cbor); |
1067 | } |
1131 | } |
1068 | |
1132 | |
1069 | PROTOTYPES: DISABLE |
1133 | PROTOTYPES: DISABLE |
… | |
… | |
1090 | void shrink (CBOR *self, int enable = 1) |
1154 | void shrink (CBOR *self, int enable = 1) |
1091 | ALIAS: |
1155 | ALIAS: |
1092 | shrink = F_SHRINK |
1156 | shrink = F_SHRINK |
1093 | allow_unknown = F_ALLOW_UNKNOWN |
1157 | allow_unknown = F_ALLOW_UNKNOWN |
1094 | allow_sharing = F_ALLOW_SHARING |
1158 | allow_sharing = F_ALLOW_SHARING |
1095 | dedup_keys = F_DEDUP_KEYS |
|
|
1096 | dedup_strings = F_DEDUP_STRINGS |
1159 | pack_strings = F_PACK_STRINGS |
1097 | PPCODE: |
1160 | PPCODE: |
1098 | { |
1161 | { |
1099 | if (enable) |
1162 | if (enable) |
1100 | self->flags |= ix; |
1163 | self->flags |= ix; |
1101 | else |
1164 | else |
… | |
… | |
1107 | void get_shrink (CBOR *self) |
1170 | void get_shrink (CBOR *self) |
1108 | ALIAS: |
1171 | ALIAS: |
1109 | get_shrink = F_SHRINK |
1172 | get_shrink = F_SHRINK |
1110 | get_allow_unknown = F_ALLOW_UNKNOWN |
1173 | get_allow_unknown = F_ALLOW_UNKNOWN |
1111 | get_allow_sharing = F_ALLOW_SHARING |
1174 | get_allow_sharing = F_ALLOW_SHARING |
1112 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1113 | get_dedup_strings = F_DEDUP_STRINGS |
1175 | get_pack_strings = F_PACK_STRINGS |
1114 | PPCODE: |
1176 | PPCODE: |
1115 | XPUSHs (boolSV (self->flags & ix)); |
1177 | XPUSHs (boolSV (self->flags & ix)); |
1116 | |
1178 | |
1117 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1179 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1118 | PPCODE: |
1180 | PPCODE: |
… | |
… | |
1131 | XPUSHs (ST (0)); |
1193 | XPUSHs (ST (0)); |
1132 | |
1194 | |
1133 | int get_max_size (CBOR *self) |
1195 | int get_max_size (CBOR *self) |
1134 | CODE: |
1196 | CODE: |
1135 | RETVAL = self->max_size; |
1197 | RETVAL = self->max_size; |
|
|
1198 | OUTPUT: |
|
|
1199 | RETVAL |
|
|
1200 | |
|
|
1201 | void filter (CBOR *self, SV *filter = 0) |
|
|
1202 | PPCODE: |
|
|
1203 | SvREFCNT_dec (self->filter); |
|
|
1204 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1205 | XPUSHs (ST (0)); |
|
|
1206 | |
|
|
1207 | SV *get_filter (CBOR *self) |
|
|
1208 | CODE: |
|
|
1209 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1136 | OUTPUT: |
1210 | OUTPUT: |
1137 | RETVAL |
1211 | RETVAL |
1138 | |
1212 | |
1139 | void encode (CBOR *self, SV *scalar) |
1213 | void encode (CBOR *self, SV *scalar) |
1140 | PPCODE: |
1214 | PPCODE: |
… | |
… | |
1155 | EXTEND (SP, 2); |
1229 | EXTEND (SP, 2); |
1156 | PUSHs (sv); |
1230 | PUSHs (sv); |
1157 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1231 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1158 | } |
1232 | } |
1159 | |
1233 | |
|
|
1234 | void DESTROY (CBOR *self) |
|
|
1235 | PPCODE: |
|
|
1236 | cbor_free (self); |
|
|
1237 | |
1160 | PROTOTYPES: ENABLE |
1238 | PROTOTYPES: ENABLE |
1161 | |
1239 | |
1162 | void encode_cbor (SV *scalar) |
1240 | void encode_cbor (SV *scalar) |
1163 | PPCODE: |
1241 | PPCODE: |
1164 | { |
1242 | { |