ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.62 by root, Sat Nov 26 02:11:52 2016 UTC vs.
Revision 1.63 by root, Sat Nov 26 04:50:58 2016 UTC

20#ifndef HvNAMELEN 20#ifndef HvNAMELEN
21# define HvNAMELEN(hv) HvNAMELEN_get (hv) 21# define HvNAMELEN(hv) HvNAMELEN_get (hv)
22#endif 22#endif
23#ifndef HvNAMEUTF8 23#ifndef HvNAMEUTF8
24# define HvNAMEUTF8(hv) 0 24# define HvNAMEUTF8(hv) 0
25#endif
26#ifndef SvREFCNT_inc_NN
27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
25#endif 28#endif
26#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
27# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
28#endif 31#endif
29 32
354 357
355 ++enc->depth; 358 ++enc->depth;
356 359
357 encode_uint (enc, MAJOR_ARRAY, len + 1); 360 encode_uint (enc, MAJOR_ARRAY, len + 1);
358 361
359 if (SvMAGICAL (av)) 362 if (ecb_expect_false (SvMAGICAL (av)))
360 for (i = 0; i <= len; ++i) 363 for (i = 0; i <= len; ++i)
361 { 364 {
362 SV **svp = av_fetch (av, i, 0); 365 SV **svp = av_fetch (av, i, 0);
363 encode_sv (enc, svp ? *svp : &PL_sv_undef); 366 encode_sv (enc, svp ? *svp : &PL_sv_undef);
364 } 367 }
383 ++enc->depth; 386 ++enc->depth;
384 387
385 int pairs = hv_iterinit (hv); 388 int pairs = hv_iterinit (hv);
386 int mg = SvMAGICAL (hv); 389 int mg = SvMAGICAL (hv);
387 390
388 if (mg) 391 if (ecb_expect_false (mg))
389 encode_ch (enc, MAJOR_MAP | MINOR_INDEF); 392 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
390 else 393 else
391 encode_uint (enc, MAJOR_MAP, pairs); 394 encode_uint (enc, MAJOR_MAP, pairs);
392 395
393 while ((he = hv_iternext (hv))) 396 while ((he = hv_iternext (hv)))
398 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 401 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
399 402
400 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 403 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
401 } 404 }
402 405
403 if (mg) 406 if (ecb_expect_false (mg))
404 encode_ch (enc, MAJOR_MISC | MINOR_INDEF); 407 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
405 408
406 --enc->depth; 409 --enc->depth;
407} 410}
408 411
451 } 454 }
452 455
453 if (ecb_expect_false (SvREFCNT (sv) > 1) 456 if (ecb_expect_false (SvREFCNT (sv) > 1)
454 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) 457 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
455 { 458 {
456 if (!enc->shareable) 459 if (ecb_expect_false (!enc->shareable))
457 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); 460 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
458 461
459 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); 462 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
460 463
461 if (SvOK (*svp)) 464 if (SvOK (*svp))
560 if (ecb_expect_false (nv == (NV)(U32)nv)) 563 if (ecb_expect_false (nv == (NV)(U32)nv))
561 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 564 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
562 //TODO: maybe I32? 565 //TODO: maybe I32?
563 else if (ecb_expect_false (nv == (float)nv)) 566 else if (ecb_expect_false (nv == (float)nv))
564 { 567 {
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
565 uint32_t fp = ecb_float_to_binary32 (nv); 570 uint32_t fp = ecb_float_to_binary32 (nv);
566
567 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
568 571
569 if (!ecb_big_endian ()) 572 if (!ecb_big_endian ())
570 fp = ecb_bswap32 (fp); 573 fp = ecb_bswap32 (fp);
571 574
572 memcpy (enc->cur, &fp, 4); 575 memcpy (enc->cur, &fp, 4);
573 enc->cur += 4; 576 enc->cur += 4;
574 } 577 }
575 else 578 else
576 { 579 {
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
577 uint64_t fp = ecb_double_to_binary64 (nv); 582 uint64_t fp = ecb_double_to_binary64 (nv);
578
579 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
580 583
581 if (!ecb_big_endian ()) 584 if (!ecb_big_endian ())
582 fp = ecb_bswap64 (fp); 585 fp = ecb_bswap64 (fp);
583 586
584 memcpy (enc->cur, &fp, 8); 587 memcpy (enc->cur, &fp, 8);
622static SV * 625static SV *
623encode_cbor (SV *scalar, CBOR *cbor) 626encode_cbor (SV *scalar, CBOR *cbor)
624{ 627{
625 enc_t enc = { 0 }; 628 enc_t enc = { 0 };
626 629
627 enc.cbor = *cbor; 630 enc.cbor = *cbor;
628 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
629 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
630 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
631 634
632 SvPOK_only (enc.sv); 635 SvPOK_only (enc.sv);
633 636
634 if (cbor->flags & F_PACK_STRINGS) 637 if (cbor->flags & F_PACK_STRINGS)
635 { 638 {
693 696
694ecb_cold static void 697ecb_cold static void
695err_unexpected_end (dec_t *dec) 698err_unexpected_end (dec_t *dec)
696{ 699{
697 err_set (dec, "unexpected end of CBOR data"); 700 err_set (dec, "unexpected end of CBOR data");
698}
699
700ecb_cold static void
701err_nesting_exceeded (dec_t *dec)
702{
703 err_set (dec, ERR_NESTING_EXCEEDED);
704} 701}
705 702
706#define ERR_DO(do) SB do; goto fail; SE 703#define ERR_DO(do) SB do; goto fail; SE
707#define ERR(reason) ERR_DO (err_set (dec, reason)) 704#define ERR(reason) ERR_DO (err_set (dec, reason))
708#define ERR_ERRSV ERR_DO (err_errsv (dec)) 705#define ERR_ERRSV ERR_DO (err_errsv (dec))
807 804
808 DEC_DEC_DEPTH; 805 DEC_DEC_DEPTH;
809 return newRV_noinc ((SV *)av); 806 return newRV_noinc ((SV *)av);
810 807
811fail: 808fail:
812 SvREFCNT_dec (av); 809 SvREFCNT_dec_NN (av);
813 DEC_DEC_DEPTH; 810 DEC_DEC_DEPTH;
814 return &PL_sv_undef; 811 return &PL_sv_undef;
815} 812}
816 813
817static void 814static void
880 877
881 return; 878 return;
882 } 879 }
883 880
884 hv_store_ent (hv, k, v, 0); 881 hv_store_ent (hv, k, v, 0);
885 SvREFCNT_dec (k); 882 SvREFCNT_dec_NN (k);
886 883
887fail: 884fail:
888 ; 885 ;
889} 886}
890 887
924 921
925 DEC_DEC_DEPTH; 922 DEC_DEC_DEPTH;
926 return newRV_noinc ((SV *)hv); 923 return newRV_noinc ((SV *)hv);
927 924
928fail: 925fail:
929 SvREFCNT_dec (hv); 926 SvREFCNT_dec_NN (hv);
930 DEC_DEC_DEPTH; 927 DEC_DEC_DEPTH;
931 return &PL_sv_undef; 928 return &PL_sv_undef;
932} 929}
933 930
934static SV * 931static SV *
935decode_str (dec_t *dec, int utf8) 932decode_str (dec_t *dec, int utf8)
936{ 933{
937 SV *sv = 0; 934 SV *sv = 0;
938 935
939 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 936 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
940 { 937 {
941 // indefinite length strings 938 // indefinite length strings
942 ++dec->cur; 939 ++dec->cur;
943 940
944 U8 major = *dec->cur & MAJOR_MISC; 941 U8 major = *dec->cur & MAJOR_MISC;
1012 sv = newRV_noinc (decode_sv (dec)); 1009 sv = newRV_noinc (decode_sv (dec));
1013 break; 1010 break;
1014 1011
1015 case CBOR_TAG_STRINGREF_NAMESPACE: 1012 case CBOR_TAG_STRINGREF_NAMESPACE:
1016 { 1013 {
1017 // do nmot use SAVETMPS/FREETMPS, as these will 1014 // do not use SAVETMPS/FREETMPS, as these will
1018 // erase mortalised caches, e.g. "shareable" 1015 // erase mortalised caches, e.g. "shareable"
1019 ENTER; 1016 ENTER;
1020 1017
1021 SAVESPTR (dec->stringref); 1018 SAVESPTR (dec->stringref);
1022 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1019 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
1126 { 1123 {
1127 FREETMPS; LEAVE; 1124 FREETMPS; LEAVE;
1128 ERR_ERRSV; 1125 ERR_ERRSV;
1129 } 1126 }
1130 1127
1131 SvREFCNT_dec (sv); 1128 SvREFCNT_dec_NN (sv);
1132 sv = SvREFCNT_inc (POPs); 1129 sv = SvREFCNT_inc (POPs);
1133 1130
1134 PUTBACK; 1131 PUTBACK;
1135 1132
1136 FREETMPS; LEAVE; 1133 FREETMPS; LEAVE;
1156 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1153 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1157 SPAGAIN; 1154 SPAGAIN;
1158 1155
1159 if (SvTRUE (ERRSV)) 1156 if (SvTRUE (ERRSV))
1160 { 1157 {
1161 SvREFCNT_dec (tag_sv); 1158 SvREFCNT_dec_NN (tag_sv);
1162 FREETMPS; LEAVE; 1159 FREETMPS; LEAVE;
1163 ERR_ERRSV; 1160 ERR_ERRSV;
1164 } 1161 }
1165 1162
1166 if (count) 1163 if (count)
1167 { 1164 {
1168 SvREFCNT_dec (tag_sv); 1165 SvREFCNT_dec_NN (tag_sv);
1169 SvREFCNT_dec (sv); 1166 SvREFCNT_dec_NN (sv);
1170 sv = SvREFCNT_inc (POPs); 1167 sv = SvREFCNT_inc_NN (POPs);
1171 } 1168 }
1172 else 1169 else
1173 { 1170 {
1174 AV *av = newAV (); 1171 AV *av = newAV ();
1175 av_push (av, tag_sv); 1172 av_push (av, tag_sv);
1318 for (i = av_len (dec.shareable) + 1; i--; ) 1315 for (i = av_len (dec.shareable) + 1; i--; )
1319 if ((svp = av_fetch (dec.shareable, i, 0))) 1316 if ((svp = av_fetch (dec.shareable, i, 0)))
1320 sv_setsv (*svp, &PL_sv_undef); 1317 sv_setsv (*svp, &PL_sv_undef);
1321 } 1318 }
1322 1319
1323 SvREFCNT_dec (sv); 1320 SvREFCNT_dec_NN (sv);
1324 1321
1325 if (dec.err_sv) 1322 if (dec.err_sv)
1326 sv_2mortal (dec.err_sv); 1323 sv_2mortal (dec.err_sv);
1327 1324
1328 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1325 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines