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.71 by root, Fri Mar 19 17:30:27 2021 UTC vs.
Revision 1.79 by root, Fri Sep 8 06:05:01 2023 UTC

27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) 27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
28#endif 28#endif
29#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
31#endif 31#endif
32
33// perl's is_utf8_string interprets len=0 as "calculate len", but we want it to mean 0
34#define cbor_is_utf8_string(str,len) (!(len) || is_utf8_string ((str), (len)))
32 35
33// known major and minor types 36// known major and minor types
34enum cbor_type 37enum cbor_type
35{ 38{
36 MAJOR_SHIFT = 5, 39 MAJOR_SHIFT = 5,
987 990
988 WANT (len); 991 WANT (len);
989 dec->cur += len; 992 dec->cur += len;
990 993
991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 994 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
992 if (!is_utf8_string ((U8 *)key, len)) 995 if (!cbor_is_utf8_string ((U8 *)key, len))
993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 996 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
994 997
995 hv_store (hv, key, -len, decode_sv (dec), 0); 998 hv_store (hv, key, -len, decode_sv (dec), 0);
996 999
997 return; 1000 return;
1126 } 1129 }
1127 1130
1128 if (utf8) 1131 if (utf8)
1129 { 1132 {
1130 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1133 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1131 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1134 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
1132 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1135 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
1133 1136
1134 SvUTF8_on (sv); 1137 SvUTF8_on (sv);
1135 } 1138 }
1136 1139
1193 if (ecb_expect_false (!dec->shareable)) 1196 if (ecb_expect_false (!dec->shareable))
1194 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1197 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
1195 1198
1196 if (dec->cbor.flags & F_ALLOW_CYCLES) 1199 if (dec->cbor.flags & F_ALLOW_CYCLES)
1197 { 1200 {
1201 // if cycles are allowed, then we store an AV as value
1202 // while it is being decoded, and gather unresolved
1203 // references in it, to be re4solved after decoding.
1204 int idx, i;
1198 sv = newSV (0); 1205 AV *av = newAV ();
1199 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1206 av_push (dec->shareable, (SV *)av);
1207 idx = AvFILLp (dec->shareable);
1200 1208
1201 SV *osv = decode_sv (dec); 1209 sv = decode_sv (dec);
1202 sv_setsv (sv, osv); 1210
1211 // the AV now contains \undef for all unresolved references,
1212 // so we fix them up here.
1213 for (i = 0; i <= AvFILLp (av); ++i)
1214 SvRV_set (AvARRAY (av)[i], SvREFCNT_inc_NN (SvRV (sv)));
1215
1216 // now replace the AV by a reference to the completed value
1203 SvREFCNT_dec_NN (osv); 1217 SvREFCNT_dec_NN ((SV *)av);
1218 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1204 } 1219 }
1205 else 1220 else
1206 { 1221 {
1207 av_push (dec->shareable, &PL_sv_undef); 1222 av_push (dec->shareable, &PL_sv_undef);
1208 int idx = AvFILLp (dec->shareable); 1223 int idx = AvFILLp (dec->shareable);
1209 sv = decode_sv (dec); 1224 sv = decode_sv (dec);
1210 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); 1225 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1211 } 1226 }
1212 } 1227 }
1213 break; 1228 break;
1214 1229
1215 case CBOR_TAG_VALUE_SHAREDREF: 1230 case CBOR_TAG_VALUE_SHAREDREF:
1220 UV idx = decode_uint (dec); 1235 UV idx = decode_uint (dec);
1221 1236
1222 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1237 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1223 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1238 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1224 1239
1225 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1240 sv = AvARRAY (dec->shareable)[idx];
1226 1241
1227 if (sv == &PL_sv_undef) 1242 // reference to cycle, we create a new \undef and use that, and also
1243 // registerr it in the AV for later fixing
1244 if (SvTYPE (sv) == SVt_PVAV)
1245 {
1246 AV *av = (AV *)sv;
1247 sv = newRV_noinc (&PL_sv_undef);
1248 av_push (av, SvREFCNT_inc_NN (sv));
1249 }
1250 else if (sv == &PL_sv_undef) // not yet decoded, but cycles not allowed
1228 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); 1251 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
1252 else // we decoded the object earlier, no cycle
1253 sv = newSVsv (sv);
1229 } 1254 }
1230 break; 1255 break;
1231 1256
1232 case CBOR_TAG_PERL_OBJECT: 1257 case CBOR_TAG_PERL_OBJECT:
1233 { 1258 {
1470 SvREFCNT_dec_NN (sv); 1495 SvREFCNT_dec_NN (sv);
1471 1496
1472 if (dec.err_sv) 1497 if (dec.err_sv)
1473 sv_2mortal (dec.err_sv); 1498 sv_2mortal (dec.err_sv);
1474 1499
1475 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1500 croak ("%s, at offset %ld (octet 0x%02x)", dec.err, (long)(dec.cur - (U8 *)data), (int)(uint8_t)*dec.cur);
1476 } 1501 }
1477 1502
1478 sv = sv_2mortal (sv); 1503 sv = sv_2mortal (sv);
1479 1504
1480 return sv; 1505 return sv;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines