… | |
… | |
24 | |
24 | |
25 | // known tags |
25 | // known tags |
26 | enum cbor_tag |
26 | enum cbor_tag |
27 | { |
27 | { |
28 | // inofficial extensions (pending iana registration) |
28 | // inofficial extensions (pending iana registration) |
29 | CBOR_TAG_PERL_OBJECT = 256, |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 257, |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
|
|
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
|
|
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
|
|
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
|
|
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
31 | |
36 | |
32 | // rfc7049 |
37 | // rfc7049 |
33 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
34 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
35 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
… | |
… | |
49 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
50 | |
55 | |
51 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
52 | }; |
57 | }; |
53 | |
58 | |
54 | #define F_SHRINK 0x00000200UL |
59 | #define F_SHRINK 0x00000001UL |
55 | #define F_ALLOW_UNKNOWN 0x00002000UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
|
|
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
|
|
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
|
|
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
56 | |
64 | |
57 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
58 | |
66 | |
59 | #define SB do { |
67 | #define SB do { |
60 | #define SE } while (0) |
68 | #define SE } while (0) |
… | |
… | |
117 | #endif |
125 | #endif |
118 | } |
126 | } |
119 | } |
127 | } |
120 | |
128 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
129 | ///////////////////////////////////////////////////////////////////////////// |
122 | // fp hell |
|
|
123 | |
|
|
124 | //TODO |
|
|
125 | |
|
|
126 | ///////////////////////////////////////////////////////////////////////////// |
|
|
127 | // encoder |
130 | // encoder |
128 | |
131 | |
129 | // structure used for encoding CBOR |
132 | // structure used for encoding CBOR |
130 | typedef struct |
133 | typedef struct |
131 | { |
134 | { |
132 | char *cur; // SvPVX (sv) + current output position |
135 | char *cur; // SvPVX (sv) + current output position |
133 | char *end; // SvEND (sv) |
136 | char *end; // SvEND (sv) |
134 | SV *sv; // result scalar |
137 | SV *sv; // result scalar |
135 | CBOR cbor; |
138 | CBOR cbor; |
136 | U32 depth; // recursion level |
139 | U32 depth; // recursion level |
|
|
140 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
141 | UV stringref_idx; |
|
|
142 | HV *shareable; // ptr => index, or 0 |
|
|
143 | UV shareable_idx; |
137 | } enc_t; |
144 | } enc_t; |
138 | |
145 | |
139 | ecb_inline void |
146 | ecb_inline void |
140 | need (enc_t *enc, STRLEN len) |
147 | need (enc_t *enc, STRLEN len) |
141 | { |
148 | { |
… | |
… | |
202 | need (enc, len); |
209 | need (enc, len); |
203 | memcpy (enc->cur, str, len); |
210 | memcpy (enc->cur, str, len); |
204 | enc->cur += len; |
211 | enc->cur += len; |
205 | } |
212 | } |
206 | |
213 | |
|
|
214 | ecb_inline void |
|
|
215 | encode_tag (enc_t *enc, UV tag) |
|
|
216 | { |
|
|
217 | encode_uint (enc, 0xc0, tag); |
|
|
218 | } |
|
|
219 | |
207 | static void encode_sv (enc_t *enc, SV *sv); |
220 | static void encode_sv (enc_t *enc, SV *sv); |
208 | |
221 | |
209 | static void |
222 | static void |
210 | encode_av (enc_t *enc, AV *av) |
223 | encode_av (enc_t *enc, AV *av) |
211 | { |
224 | { |
… | |
… | |
223 | SV **svp = av_fetch (av, i, 0); |
236 | SV **svp = av_fetch (av, i, 0); |
224 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
237 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
225 | } |
238 | } |
226 | |
239 | |
227 | --enc->depth; |
240 | --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)); |
228 | } |
250 | } |
229 | |
251 | |
230 | static void |
252 | static void |
231 | encode_hv (enc_t *enc, HV *hv) |
253 | encode_hv (enc_t *enc, HV *hv) |
232 | { |
254 | { |
… | |
… | |
245 | else |
267 | else |
246 | encode_uint (enc, 0xa0, pairs); |
268 | encode_uint (enc, 0xa0, pairs); |
247 | |
269 | |
248 | while ((he = hv_iternext (hv))) |
270 | while ((he = hv_iternext (hv))) |
249 | { |
271 | { |
|
|
272 | if (ecb_expect_false (enc->cbor.flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS))) |
|
|
273 | { |
|
|
274 | SV **svp; |
|
|
275 | |
250 | if (HeKLEN (he) == HEf_SVKEY) |
276 | if (HeKLEN (he) == HEf_SVKEY) |
251 | encode_sv (enc, HeSVKEY (he)); |
277 | svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return 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 | } |
252 | else |
293 | else |
253 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
294 | encode_he (enc, he); |
254 | |
295 | |
255 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
296 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
256 | } |
297 | } |
257 | |
298 | |
258 | if (mg) |
299 | if (mg) |
… | |
… | |
263 | |
304 | |
264 | // encode objects, arrays and special \0=false and \1=true values. |
305 | // encode objects, arrays and special \0=false and \1=true values. |
265 | static void |
306 | static void |
266 | encode_rv (enc_t *enc, SV *sv) |
307 | encode_rv (enc_t *enc, SV *sv) |
267 | { |
308 | { |
268 | svtype svt; |
|
|
269 | |
|
|
270 | SvGETMAGIC (sv); |
309 | 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 | |
271 | svt = SvTYPE (sv); |
333 | svtype svt = SvTYPE (sv); |
272 | |
334 | |
273 | if (ecb_expect_false (SvOBJECT (sv))) |
335 | if (ecb_expect_false (SvOBJECT (sv))) |
274 | { |
336 | { |
275 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
337 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
276 | ? types_boolean_stash |
338 | ? types_boolean_stash |
… | |
… | |
336 | |
398 | |
337 | // catch this surprisingly common error |
399 | // catch this surprisingly common error |
338 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
400 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
339 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
401 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
340 | |
402 | |
341 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
403 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
342 | encode_uint (enc, 0x80, count + 1); |
404 | encode_uint (enc, 0x80, count + 1); |
343 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
405 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
344 | |
406 | |
345 | while (count) |
407 | while (count) |
346 | encode_sv (enc, SP[1 - count--]); |
408 | encode_sv (enc, SP[1 - count--]); |
… | |
… | |
355 | } |
417 | } |
356 | else if (svt == SVt_PVHV) |
418 | else if (svt == SVt_PVHV) |
357 | encode_hv (enc, (HV *)sv); |
419 | encode_hv (enc, (HV *)sv); |
358 | else if (svt == SVt_PVAV) |
420 | else if (svt == SVt_PVAV) |
359 | encode_av (enc, (AV *)sv); |
421 | encode_av (enc, (AV *)sv); |
360 | else if (svt < SVt_PVAV) |
|
|
361 | { |
|
|
362 | STRLEN len = 0; |
|
|
363 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
364 | |
|
|
365 | if (len == 1 && *pv == '1') |
|
|
366 | encode_ch (enc, 0xe0 | 21); |
|
|
367 | else if (len == 1 && *pv == '0') |
|
|
368 | encode_ch (enc, 0xe0 | 20); |
|
|
369 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
370 | encode_ch (enc, 0xe0 | 23); |
|
|
371 | else |
|
|
372 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
373 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
374 | } |
|
|
375 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
376 | encode_ch (enc, 0xe0 | 23); |
|
|
377 | else |
422 | else |
378 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
423 | { |
379 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
424 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
425 | encode_sv (enc, sv); |
|
|
426 | } |
380 | } |
427 | } |
381 | |
428 | |
382 | static void |
429 | static void |
383 | encode_nv (enc_t *enc, SV *sv) |
430 | encode_nv (enc_t *enc, SV *sv) |
384 | { |
431 | { |
… | |
… | |
449 | } |
496 | } |
450 | |
497 | |
451 | static SV * |
498 | static SV * |
452 | encode_cbor (SV *scalar, CBOR *cbor) |
499 | encode_cbor (SV *scalar, CBOR *cbor) |
453 | { |
500 | { |
454 | enc_t enc; |
501 | enc_t enc = { }; |
455 | |
502 | |
456 | enc.cbor = *cbor; |
503 | enc.cbor = *cbor; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
504 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
505 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
506 | enc.end = SvEND (enc.sv); |
460 | enc.depth = 0; |
|
|
461 | |
507 | |
462 | SvPOK_only (enc.sv); |
508 | SvPOK_only (enc.sv); |
|
|
509 | |
|
|
510 | if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) |
|
|
511 | { |
|
|
512 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
513 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
514 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
515 | } |
|
|
516 | |
463 | encode_sv (&enc, scalar); |
517 | encode_sv (&enc, scalar); |
464 | |
518 | |
465 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
519 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
466 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
520 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
467 | |
521 | |
… | |
… | |
481 | U8 *end; // end of input string |
535 | U8 *end; // end of input string |
482 | const char *err; // parse error, if != 0 |
536 | const char *err; // parse error, if != 0 |
483 | CBOR cbor; |
537 | CBOR cbor; |
484 | U32 depth; // recursion depth |
538 | U32 depth; // recursion depth |
485 | U32 maxdepth; // recursion depth limit |
539 | U32 maxdepth; // recursion depth limit |
|
|
540 | AV *shareable; |
|
|
541 | AV *stringref; |
486 | } dec_t; |
542 | } dec_t; |
487 | |
543 | |
488 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
544 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
489 | |
545 | |
490 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
546 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
597 | I32 len = decode_uint (dec); |
653 | I32 len = decode_uint (dec); |
598 | char *key = (char *)dec->cur; |
654 | char *key = (char *)dec->cur; |
599 | |
655 | |
600 | dec->cur += len; |
656 | dec->cur += len; |
601 | |
657 | |
|
|
658 | if (ecb_expect_false (dec->stringref)) |
|
|
659 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
660 | |
602 | hv_store (hv, key, len, decode_sv (dec), 0); |
661 | hv_store (hv, key, len, decode_sv (dec), 0); |
603 | } |
662 | } |
604 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
663 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
605 | { |
664 | { |
606 | I32 len = decode_uint (dec); |
665 | I32 len = decode_uint (dec); |
607 | char *key = (char *)dec->cur; |
666 | char *key = (char *)dec->cur; |
608 | |
667 | |
609 | dec->cur += len; |
668 | dec->cur += len; |
|
|
669 | |
|
|
670 | if (ecb_expect_false (dec->stringref)) |
|
|
671 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
610 | |
672 | |
611 | hv_store (hv, key, -len, decode_sv (dec), 0); |
673 | hv_store (hv, key, -len, decode_sv (dec), 0); |
612 | } |
674 | } |
613 | else |
675 | else |
614 | { |
676 | { |
… | |
… | |
696 | } |
758 | } |
697 | |
759 | |
698 | if (utf8) |
760 | if (utf8) |
699 | SvUTF8_on (sv); |
761 | SvUTF8_on (sv); |
700 | |
762 | |
|
|
763 | if (ecb_expect_false (dec->stringref)) |
|
|
764 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
765 | |
701 | return sv; |
766 | return sv; |
702 | |
767 | |
703 | fail: |
768 | fail: |
704 | SvREFCNT_dec (sv); |
769 | SvREFCNT_dec (sv); |
705 | return &PL_sv_undef; |
770 | return &PL_sv_undef; |
706 | } |
771 | } |
707 | |
772 | |
708 | static SV * |
773 | static SV * |
709 | decode_tagged (dec_t *dec) |
774 | decode_tagged (dec_t *dec) |
710 | { |
775 | { |
|
|
776 | SV *sv = 0; |
711 | UV tag = decode_uint (dec); |
777 | UV tag = decode_uint (dec); |
|
|
778 | |
|
|
779 | WANT (1); |
|
|
780 | |
|
|
781 | switch (tag) |
|
|
782 | { |
|
|
783 | case CBOR_TAG_MAGIC: |
|
|
784 | sv = decode_sv (dec); |
|
|
785 | break; |
|
|
786 | |
|
|
787 | case CBOR_TAG_INDIRECTION: |
|
|
788 | sv = newRV_noinc (decode_sv (dec)); |
|
|
789 | break; |
|
|
790 | |
|
|
791 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
792 | { |
|
|
793 | ENTER; SAVETMPS; |
|
|
794 | |
|
|
795 | SAVESPTR (dec->stringref); |
|
|
796 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
797 | |
|
|
798 | sv = decode_sv (dec); |
|
|
799 | |
|
|
800 | FREETMPS; LEAVE; |
|
|
801 | } |
|
|
802 | break; |
|
|
803 | |
|
|
804 | case CBOR_TAG_STRINGREF: |
|
|
805 | { |
|
|
806 | if ((*dec->cur >> 5) != 0) |
|
|
807 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
808 | |
|
|
809 | UV idx = decode_uint (dec); |
|
|
810 | |
|
|
811 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
812 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
813 | |
|
|
814 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
815 | } |
|
|
816 | break; |
|
|
817 | |
|
|
818 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
819 | { |
|
|
820 | if (ecb_expect_false (!dec->shareable)) |
|
|
821 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
822 | |
|
|
823 | sv = newSV (0); |
|
|
824 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
825 | |
712 | SV *sv = decode_sv (dec); |
826 | SV *osv = decode_sv (dec); |
|
|
827 | sv_setsv (sv, osv); |
|
|
828 | SvREFCNT_dec_NN (osv); |
|
|
829 | } |
|
|
830 | break; |
713 | |
831 | |
714 | if (tag == CBOR_TAG_MAGIC) |
832 | case CBOR_TAG_VALUE_SHAREDREF: |
715 | return sv; |
833 | { |
716 | else if (tag == CBOR_TAG_PERL_OBJECT) |
834 | if ((*dec->cur >> 5) != 0) |
717 | { |
835 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
|
|
836 | |
|
|
837 | UV idx = decode_uint (dec); |
|
|
838 | |
|
|
839 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
|
|
840 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
841 | |
|
|
842 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
843 | } |
|
|
844 | break; |
|
|
845 | |
|
|
846 | case CBOR_TAG_PERL_OBJECT: |
|
|
847 | { |
|
|
848 | sv = decode_sv (dec); |
|
|
849 | |
718 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
850 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
719 | ERR ("corrupted CBOR data (non-array perl object)"); |
851 | ERR ("corrupted CBOR data (non-array perl object)"); |
720 | |
852 | |
721 | AV *av = (AV *)SvRV (sv); |
853 | AV *av = (AV *)SvRV (sv); |
722 | int len = av_len (av) + 1; |
854 | int len = av_len (av) + 1; |
723 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
855 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
724 | |
856 | |
725 | if (!stash) |
857 | if (!stash) |
726 | ERR ("cannot decode perl-object (package does not exist)"); |
858 | ERR ("cannot decode perl-object (package does not exist)"); |
727 | |
859 | |
728 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
860 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
729 | |
861 | |
730 | if (!method) |
862 | if (!method) |
731 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
863 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
732 | |
864 | |
733 | dSP; |
865 | dSP; |
734 | |
866 | |
735 | ENTER; SAVETMPS; PUSHMARK (SP); |
867 | ENTER; SAVETMPS; PUSHMARK (SP); |
736 | EXTEND (SP, len + 1); |
868 | EXTEND (SP, len + 1); |
737 | // we re-bless the reference to get overload and other niceties right |
869 | // we re-bless the reference to get overload and other niceties right |
738 | PUSHs (*av_fetch (av, 0, 1)); |
870 | PUSHs (*av_fetch (av, 0, 1)); |
739 | PUSHs (sv_cbor); |
871 | PUSHs (sv_cbor); |
740 | |
872 | |
741 | int i; |
873 | int i; |
742 | |
874 | |
743 | for (i = 1; i < len; ++i) |
875 | for (i = 1; i < len; ++i) |
744 | PUSHs (*av_fetch (av, i, 1)); |
876 | PUSHs (*av_fetch (av, i, 1)); |
745 | |
877 | |
746 | PUTBACK; |
878 | PUTBACK; |
747 | call_sv ((SV *)GvCV (method), G_SCALAR); |
879 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
748 | SPAGAIN; |
880 | SPAGAIN; |
749 | |
881 | |
|
|
882 | if (SvTRUE (ERRSV)) |
|
|
883 | { |
|
|
884 | FREETMPS; LEAVE; |
|
|
885 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
886 | } |
|
|
887 | |
750 | SvREFCNT_dec (sv); |
888 | SvREFCNT_dec (sv); |
751 | sv = SvREFCNT_inc (POPs); |
889 | sv = SvREFCNT_inc (POPs); |
752 | |
890 | |
753 | PUTBACK; |
891 | PUTBACK; |
754 | |
892 | |
755 | FREETMPS; LEAVE; |
893 | FREETMPS; LEAVE; |
|
|
894 | } |
|
|
895 | break; |
756 | |
896 | |
757 | return sv; |
897 | default: |
758 | } |
898 | { |
759 | else |
899 | sv = decode_sv (dec); |
760 | { |
900 | |
761 | AV *av = newAV (); |
901 | AV *av = newAV (); |
762 | av_push (av, newSVuv (tag)); |
902 | av_push (av, newSVuv (tag)); |
763 | av_push (av, sv); |
903 | av_push (av, sv); |
764 | |
904 | |
765 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
905 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
766 | ? cbor_tagged_stash |
906 | ? cbor_tagged_stash |
767 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
907 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
768 | |
908 | |
769 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
909 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
910 | } |
|
|
911 | break; |
770 | } |
912 | } |
|
|
913 | |
|
|
914 | return sv; |
771 | |
915 | |
772 | fail: |
916 | fail: |
773 | SvREFCNT_dec (sv); |
917 | SvREFCNT_dec (sv); |
774 | return &PL_sv_undef; |
918 | return &PL_sv_undef; |
775 | } |
919 | } |
… | |
… | |
866 | } |
1010 | } |
867 | |
1011 | |
868 | static SV * |
1012 | static SV * |
869 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1013 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
870 | { |
1014 | { |
871 | dec_t dec; |
1015 | dec_t dec = { }; |
872 | SV *sv; |
1016 | SV *sv; |
873 | STRLEN len; |
1017 | STRLEN len; |
874 | char *data = SvPVbyte (string, len); |
1018 | char *data = SvPVbyte (string, len); |
875 | |
1019 | |
876 | if (len > cbor->max_size && cbor->max_size) |
1020 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
878 | (unsigned long)len, (unsigned long)cbor->max_size); |
1022 | (unsigned long)len, (unsigned long)cbor->max_size); |
879 | |
1023 | |
880 | dec.cbor = *cbor; |
1024 | dec.cbor = *cbor; |
881 | dec.cur = (U8 *)data; |
1025 | dec.cur = (U8 *)data; |
882 | dec.end = (U8 *)data + len; |
1026 | dec.end = (U8 *)data + len; |
883 | dec.err = 0; |
|
|
884 | dec.depth = 0; |
|
|
885 | |
1027 | |
886 | sv = decode_sv (&dec); |
1028 | sv = decode_sv (&dec); |
887 | |
1029 | |
888 | if (offset_return) |
1030 | if (offset_return) |
889 | *offset_return = dec.cur; |
1031 | *offset_return = dec.cur; |
… | |
… | |
947 | |
1089 | |
948 | void shrink (CBOR *self, int enable = 1) |
1090 | void shrink (CBOR *self, int enable = 1) |
949 | ALIAS: |
1091 | ALIAS: |
950 | shrink = F_SHRINK |
1092 | shrink = F_SHRINK |
951 | allow_unknown = F_ALLOW_UNKNOWN |
1093 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1094 | allow_sharing = F_ALLOW_SHARING |
|
|
1095 | dedup_keys = F_DEDUP_KEYS |
|
|
1096 | dedup_strings = F_DEDUP_STRINGS |
952 | PPCODE: |
1097 | PPCODE: |
953 | { |
1098 | { |
954 | if (enable) |
1099 | if (enable) |
955 | self->flags |= ix; |
1100 | self->flags |= ix; |
956 | else |
1101 | else |
… | |
… | |
961 | |
1106 | |
962 | void get_shrink (CBOR *self) |
1107 | void get_shrink (CBOR *self) |
963 | ALIAS: |
1108 | ALIAS: |
964 | get_shrink = F_SHRINK |
1109 | get_shrink = F_SHRINK |
965 | get_allow_unknown = F_ALLOW_UNKNOWN |
1110 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1111 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1112 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1113 | get_dedup_strings = F_DEDUP_STRINGS |
966 | PPCODE: |
1114 | PPCODE: |
967 | XPUSHs (boolSV (self->flags & ix)); |
1115 | XPUSHs (boolSV (self->flags & ix)); |
968 | |
1116 | |
969 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1117 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
970 | PPCODE: |
1118 | PPCODE: |