… | |
… | |
327 | static void |
327 | static void |
328 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
329 | { |
329 | { |
330 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
331 | |
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 | |
|
|
354 | svtype svt = SvTYPE (sv); |
332 | svtype svt = SvTYPE (sv); |
355 | |
333 | |
356 | if (ecb_expect_false (SvOBJECT (sv))) |
334 | if (ecb_expect_false (SvOBJECT (sv))) |
357 | { |
335 | { |
358 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
336 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
… | |
… | |
364 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
342 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
365 | ? cbor_tagged_stash |
343 | ? cbor_tagged_stash |
366 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
344 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
367 | |
345 | |
368 | HV *stash = SvSTASH (sv); |
346 | HV *stash = SvSTASH (sv); |
369 | GV *method; |
|
|
370 | |
347 | |
371 | if (stash == boolean_stash) |
348 | if (stash == boolean_stash) |
|
|
349 | { |
372 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
350 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
351 | return; |
|
|
352 | } |
373 | else if (stash == error_stash) |
353 | else if (stash == error_stash) |
|
|
354 | { |
374 | encode_ch (enc, 0xe0 | 23); |
355 | encode_ch (enc, 0xe0 | 23); |
|
|
356 | return; |
|
|
357 | } |
375 | else if (stash == tagged_stash) |
358 | else if (stash == tagged_stash) |
376 | { |
359 | { |
377 | if (svt != SVt_PVAV) |
360 | if (svt != SVt_PVAV) |
378 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
361 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
379 | |
362 | |
380 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
363 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
381 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
364 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
365 | |
|
|
366 | return; |
|
|
367 | } |
|
|
368 | } |
|
|
369 | |
|
|
370 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
371 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
372 | { |
|
|
373 | if (!enc->shareable) |
|
|
374 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
375 | |
|
|
376 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
377 | |
|
|
378 | if (SvOK (*svp)) |
382 | } |
379 | { |
|
|
380 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
381 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
382 | return; |
|
|
383 | } |
|
|
384 | else |
|
|
385 | { |
|
|
386 | sv_setuv (*svp, enc->shareable_idx); |
|
|
387 | ++enc->shareable_idx; |
|
|
388 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
389 | } |
|
|
390 | } |
|
|
391 | |
|
|
392 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
393 | { |
|
|
394 | HV *stash = SvSTASH (sv); |
|
|
395 | GV *method; |
|
|
396 | |
383 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
397 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
384 | { |
398 | { |
385 | dSP; |
399 | dSP; |
386 | |
400 | |
387 | ENTER; SAVETMPS; PUSHMARK (SP); |
401 | ENTER; SAVETMPS; PUSHMARK (SP); |
388 | // we re-bless the reference to get overload and other niceties right |
402 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
600 | | ((UV)dec->cur[-1]); |
614 | | ((UV)dec->cur[-1]); |
601 | |
615 | |
602 | case 27: |
616 | case 27: |
603 | WANT (9); |
617 | WANT (9); |
604 | dec->cur += 9; |
618 | dec->cur += 9; |
|
|
619 | |
|
|
620 | return |
|
|
621 | #if UVSIZE < 8 |
|
|
622 | 0 |
|
|
623 | #else |
605 | return (((UV)dec->cur[-8]) << 56) |
624 | (((UV)dec->cur[-8]) << 56) |
606 | | (((UV)dec->cur[-7]) << 48) |
625 | | (((UV)dec->cur[-7]) << 48) |
607 | | (((UV)dec->cur[-6]) << 40) |
626 | | (((UV)dec->cur[-6]) << 40) |
608 | | (((UV)dec->cur[-5]) << 32) |
627 | | (((UV)dec->cur[-5]) << 32) |
|
|
628 | #endif |
609 | | (((UV)dec->cur[-4]) << 24) |
629 | | (((UV)dec->cur[-4]) << 24) |
610 | | (((UV)dec->cur[-3]) << 16) |
630 | | (((UV)dec->cur[-3]) << 16) |
611 | | (((UV)dec->cur[-2]) << 8) |
631 | | (((UV)dec->cur[-2]) << 8) |
612 | | ((UV)dec->cur[-1]); |
632 | | ((UV)dec->cur[-1]); |
613 | |
633 | |
… | |
… | |
753 | { |
773 | { |
754 | SV *sv = 0; |
774 | SV *sv = 0; |
755 | |
775 | |
756 | if ((*dec->cur & 31) == 31) |
776 | if ((*dec->cur & 31) == 31) |
757 | { |
777 | { |
|
|
778 | // indefinite length strings |
758 | ++dec->cur; |
779 | ++dec->cur; |
759 | |
780 | |
|
|
781 | unsigned char major = *dec->cur & 0xe0; |
|
|
782 | |
760 | sv = newSVpvn ("", 0); |
783 | sv = newSVpvn ("", 0); |
761 | |
784 | |
762 | // not very fast, and certainly not robust against illegal input |
|
|
763 | for (;;) |
785 | for (;;) |
764 | { |
786 | { |
765 | WANT (1); |
787 | WANT (1); |
766 | |
788 | |
|
|
789 | if ((*dec->cur ^ major) >= 31) |
767 | if (*dec->cur == (0xe0 | 31)) |
790 | if (*dec->cur == (0xe0 | 31)) |
768 | { |
791 | { |
769 | ++dec->cur; |
792 | ++dec->cur; |
770 | break; |
793 | break; |
771 | } |
794 | } |
|
|
795 | else |
|
|
796 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
772 | |
797 | |
773 | sv_catsv (sv, decode_sv (dec)); |
798 | STRLEN len = decode_uint (dec); |
|
|
799 | |
|
|
800 | WANT (len); |
|
|
801 | sv_catpvn (sv, dec->cur, len); |
|
|
802 | dec->cur += len; |
774 | } |
803 | } |
775 | } |
804 | } |
776 | else |
805 | else |
777 | { |
806 | { |
778 | STRLEN len = decode_uint (dec); |
807 | STRLEN len = decode_uint (dec); |