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.57 by root, Fri Nov 25 12:16:12 2016 UTC vs.
Revision 1.61 by root, Sat Nov 26 02:10:19 2016 UTC

6#include <string.h> 6#include <string.h>
7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11#include <inttypes.h>
11 12
12#define ECB_NO_THREADS 1 13#define ECB_NO_THREADS 1
13#include "ecb.h" 14#include "ecb.h"
14 15
15// compatibility with perl <5.18 16// compatibility with perl <5.18
99 100
100#define F_SHRINK 0x00000001UL 101#define F_SHRINK 0x00000001UL
101#define F_ALLOW_UNKNOWN 0x00000002UL 102#define F_ALLOW_UNKNOWN 0x00000002UL
102#define F_ALLOW_SHARING 0x00000004UL 103#define F_ALLOW_SHARING 0x00000004UL
103#define F_ALLOW_CYCLES 0x00000008UL 104#define F_ALLOW_CYCLES 0x00000008UL
105#define F_FORBID_OBJECTS 0x00000010UL
104#define F_PACK_STRINGS 0x00000010UL 106#define F_PACK_STRINGS 0x00000020UL
105#define F_TEXT_KEYS 0x00000020UL 107#define F_TEXT_KEYS 0x00000040UL
106#define F_TEXT_STRINGS 0x00000040UL 108#define F_TEXT_STRINGS 0x00000080UL
107#define F_VALIDATE_UTF8 0x00000080UL 109#define F_VALIDATE_UTF8 0x00000100UL
108 110
109#define INIT_SIZE 32 // initial scalar size to be allocated 111#define INIT_SIZE 32 // initial scalar size to be allocated
110 112
111#define SB do { 113#define SB do {
112#define SE } while (0) 114#define SE } while (0)
185 187
186// minimum length of a string to be registered for stringref 188// minimum length of a string to be registered for stringref
187ecb_inline int 189ecb_inline int
188minimum_string_length (UV idx) 190minimum_string_length (UV idx)
189{ 191{
190 return idx > 23 192
191 ? idx > 0xffU 193 return idx <= 23 ? 3
192 ? idx > 0xffffU 194 : idx <= 0xffU ? 4
195 : idx <= 0xffffU ? 5
193 ? idx > 0xffffffffU 196 : idx <= 0xffffffffU ? 7
194 ? 11 197 : 11;
195 : 7
196 : 5
197 : 4
198 : 3;
199} 198}
200 199
201///////////////////////////////////////////////////////////////////////////// 200/////////////////////////////////////////////////////////////////////////////
202// encoder 201// encoder
203 202
216} enc_t; 215} enc_t;
217 216
218ecb_inline void 217ecb_inline void
219need (enc_t *enc, STRLEN len) 218need (enc_t *enc, STRLEN len)
220{ 219{
221 if (ecb_expect_false (enc->cur + len >= enc->end)) 220 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
222 { 221 {
223 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 222 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
224 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 223 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
225 enc->cur = SvPVX (enc->sv) + cur; 224 enc->cur = SvPVX (enc->sv) + cur;
226 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 225 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
477 if (ecb_expect_false (SvOBJECT (sv))) 476 if (ecb_expect_false (SvOBJECT (sv)))
478 { 477 {
479 HV *stash = SvSTASH (sv); 478 HV *stash = SvSTASH (sv);
480 GV *method; 479 GV *method;
481 480
481 if (enc->cbor.flags & F_FORBID_OBJECTS)
482 croak ("encountered object '%s', but forbid_objects is enabled",
483 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 484 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
483 { 485 {
484 dSP; 486 dSP;
485 487
486 ENTER; SAVETMPS; 488 ENTER; SAVETMPS;
487 PUSHMARK (SP); 489 PUSHMARK (SP);
661 U32 depth; // recursion depth 663 U32 depth; // recursion depth
662 U32 maxdepth; // recursion depth limit 664 U32 maxdepth; // recursion depth limit
663 AV *shareable; 665 AV *shareable;
664 AV *stringref; 666 AV *stringref;
665 SV *decode_tagged; 667 SV *decode_tagged;
668 SV *err_sv; // optional sv for error, needs to be freed
666} dec_t; 669} dec_t;
667 670
668#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 671// set dec->err to ERRSV
672ecb_cold static void
673err_errsv (dec_t *dec)
674{
675 if (!dec->err)
676 {
677 dec->err_sv = newSVsv (ERRSV);
669 678
679 // chop off the trailing \n
680 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
681 *SvEND (dec->err_sv) = 0;
682
683 dec->err = SvPVutf8_nolen (dec->err_sv);
684 }
685}
686
687// the following functions are used to reduce code size and help the compiler to optimise
688ecb_cold static void
689err_set (dec_t *dec, const char *reason)
690{
691 if (!dec->err)
692 dec->err = reason;
693}
694
695ecb_cold static void
696err_unexpected_end (dec_t *dec)
697{
698 err_set (dec, "unexpected end of CBOR data");
699}
700
701ecb_cold static void
702err_nesting_exceeded (dec_t *dec)
703{
704 err_set (dec, ERR_NESTING_EXCEEDED);
705}
706
707#define ERR_DO(do) SB do; goto fail; SE
708#define ERR(reason) ERR_DO (err_set (dec, reason))
709#define ERR_ERRSV ERR_DO (err_errsv (dec))
710
670#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data") 711#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
671 712
672#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) 713#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
673#define DEC_DEC_DEPTH --dec->depth 714#define DEC_DEC_DEPTH --dec->depth
674 715
675static UV 716static UV
811 } 852 }
812 853
813 SV *k = decode_sv (dec); 854 SV *k = decode_sv (dec);
814 SV *v = decode_sv (dec); 855 SV *v = decode_sv (dec);
815 856
857 // we leak memory if uncaught exceptions are thrown by random magical
858 // methods, and this is hopefully the only place where it can happen,
859 // so if there is a chance of an exception, take the very slow path.
860 // since catching exceptions is "undocumented/internal/forbidden" by
861 // the new p5p powers, we need to call out to a perl function :/
862 if (ecb_expect_false (SvAMAGIC (k)))
863 {
864 dSP;
865
866 ENTER; SAVETMPS;
867 PUSHMARK (SP);
868 EXTEND (SP, 3);
869 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
870 PUSHs (sv_2mortal (k));
871 PUSHs (sv_2mortal (v));
872
873 PUTBACK;
874 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
875 SPAGAIN;
876
877 FREETMPS; LEAVE;
878
879 if (SvTRUE (ERRSV))
880 ERR_ERRSV;
881
882 return;
883 }
884
816 hv_store_ent (hv, k, v, 0); 885 hv_store_ent (hv, k, v, 0);
817 SvREFCNT_dec (k); 886 SvREFCNT_dec (k);
818 887
819fail: 888fail:
820 ; 889 ;
1014 } 1083 }
1015 break; 1084 break;
1016 1085
1017 case CBOR_TAG_PERL_OBJECT: 1086 case CBOR_TAG_PERL_OBJECT:
1018 { 1087 {
1088 if (dec->cbor.flags & F_FORBID_OBJECTS)
1089 goto filter;
1090
1019 sv = decode_sv (dec); 1091 sv = decode_sv (dec);
1020 1092
1021 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 1093 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
1022 ERR ("corrupted CBOR data (non-array perl object)"); 1094 ERR ("corrupted CBOR data (non-array perl object)");
1023 1095
1052 SPAGAIN; 1124 SPAGAIN;
1053 1125
1054 if (SvTRUE (ERRSV)) 1126 if (SvTRUE (ERRSV))
1055 { 1127 {
1056 FREETMPS; LEAVE; 1128 FREETMPS; LEAVE;
1057 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1129 ERR_ERRSV;
1058 } 1130 }
1059 1131
1060 SvREFCNT_dec (sv); 1132 SvREFCNT_dec (sv);
1061 sv = SvREFCNT_inc (POPs); 1133 sv = SvREFCNT_inc (POPs);
1062 1134
1065 FREETMPS; LEAVE; 1137 FREETMPS; LEAVE;
1066 } 1138 }
1067 break; 1139 break;
1068 1140
1069 default: 1141 default:
1142 filter:
1070 { 1143 {
1144 SV *tag_sv = newSVuv (tag);
1145
1071 sv = decode_sv (dec); 1146 sv = decode_sv (dec);
1072 1147
1073 dSP; 1148 dSP;
1074 ENTER; SAVETMPS; 1149 ENTER; SAVETMPS;
1075 SAVESTACK_POS (); 1150 SAVESTACK_POS ();
1076 PUSHMARK (SP); 1151 PUSHMARK (SP);
1077 EXTEND (SP, 2); 1152 EXTEND (SP, 2);
1078 PUSHs (newSVuv (tag)); 1153 PUSHs (tag_sv);
1079 PUSHs (sv); 1154 PUSHs (sv);
1080 1155
1081 PUTBACK; 1156 PUTBACK;
1082 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1157 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1083 SPAGAIN; 1158 SPAGAIN;
1084 1159
1085 if (SvTRUE (ERRSV)) 1160 if (SvTRUE (ERRSV))
1086 { 1161 {
1162 SvREFCNT_dec (tag_sv);
1087 FREETMPS; LEAVE; 1163 FREETMPS; LEAVE;
1088 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1164 ERR_ERRSV;
1089 } 1165 }
1090 1166
1091 if (count) 1167 if (count)
1092 { 1168 {
1169 SvREFCNT_dec (tag_sv);
1093 SvREFCNT_dec (sv); 1170 SvREFCNT_dec (sv);
1094 sv = SvREFCNT_inc (POPs); 1171 sv = SvREFCNT_inc (POPs);
1095 } 1172 }
1096 else 1173 else
1097 { 1174 {
1098 AV *av = newAV (); 1175 AV *av = newAV ();
1099 av_push (av, newSVuv (tag)); 1176 av_push (av, tag_sv);
1100 av_push (av, sv); 1177 av_push (av, sv);
1101 1178
1102 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1179 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1103 ? cbor_tagged_stash 1180 ? cbor_tagged_stash
1104 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1181 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1233 1310
1234 if (dec.err) 1311 if (dec.err)
1235 { 1312 {
1236 if (dec.shareable) 1313 if (dec.shareable)
1237 { 1314 {
1238 // need to break cyclic links, which whould all be in shareable 1315 // need to break cyclic links, which would all be in shareable
1239 int i; 1316 int i;
1240 SV **svp; 1317 SV **svp;
1241 1318
1242 for (i = av_len (dec.shareable) + 1; i--; ) 1319 for (i = av_len (dec.shareable) + 1; i--; )
1243 if ((svp = av_fetch (dec.shareable, i, 0))) 1320 if ((svp = av_fetch (dec.shareable, i, 0)))
1244 sv_setsv (*svp, &PL_sv_undef); 1321 sv_setsv (*svp, &PL_sv_undef);
1245 } 1322 }
1246 1323
1247 SvREFCNT_dec (sv); 1324 SvREFCNT_dec (sv);
1325
1326 if (dec.err_sv)
1327 sv_2mortal (dec.err_sv);
1328
1248 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1329 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1249 } 1330 }
1250 1331
1251 sv = sv_2mortal (sv); 1332 sv = sv_2mortal (sv);
1252 1333
1427 ALIAS: 1508 ALIAS:
1428 shrink = F_SHRINK 1509 shrink = F_SHRINK
1429 allow_unknown = F_ALLOW_UNKNOWN 1510 allow_unknown = F_ALLOW_UNKNOWN
1430 allow_sharing = F_ALLOW_SHARING 1511 allow_sharing = F_ALLOW_SHARING
1431 allow_cycles = F_ALLOW_CYCLES 1512 allow_cycles = F_ALLOW_CYCLES
1513 forbid_objects = F_FORBID_OBJECTS
1432 pack_strings = F_PACK_STRINGS 1514 pack_strings = F_PACK_STRINGS
1433 text_keys = F_TEXT_KEYS 1515 text_keys = F_TEXT_KEYS
1434 text_strings = F_TEXT_STRINGS 1516 text_strings = F_TEXT_STRINGS
1435 validate_utf8 = F_VALIDATE_UTF8 1517 validate_utf8 = F_VALIDATE_UTF8
1436 PPCODE: 1518 PPCODE:
1447 ALIAS: 1529 ALIAS:
1448 get_shrink = F_SHRINK 1530 get_shrink = F_SHRINK
1449 get_allow_unknown = F_ALLOW_UNKNOWN 1531 get_allow_unknown = F_ALLOW_UNKNOWN
1450 get_allow_sharing = F_ALLOW_SHARING 1532 get_allow_sharing = F_ALLOW_SHARING
1451 get_allow_cycles = F_ALLOW_CYCLES 1533 get_allow_cycles = F_ALLOW_CYCLES
1534 get_forbid_objects = F_FORBID_OBJECTS
1452 get_pack_strings = F_PACK_STRINGS 1535 get_pack_strings = F_PACK_STRINGS
1453 get_text_keys = F_TEXT_KEYS 1536 get_text_keys = F_TEXT_KEYS
1454 get_text_strings = F_TEXT_STRINGS 1537 get_text_strings = F_TEXT_STRINGS
1455 get_validate_utf8 = F_VALIDATE_UTF8 1538 get_validate_utf8 = F_VALIDATE_UTF8
1456 PPCODE: 1539 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines