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.69 by root, Mon Nov 30 20:38:25 2020 UTC vs.
Revision 1.79 by root, Fri Sep 8 06:05:01 2023 UTC

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 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)))
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,
37 MINOR_MASK = 0x1f, 40 MINOR_MASK = 0x1f,
109 AS_BYTES = 2, 112 AS_BYTES = 2,
110 AS_TEXT = 3, 113 AS_TEXT = 3,
111 AS_FLOAT16 = 4, 114 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5, 115 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6, 116 AS_FLOAT64 = 6,
117 AS_MAP = 7,
114 // possibly future enhancements: (generic) float, (generic) string 118 // possibly future enhancements: (generic) float, (generic) string
115}; 119};
116 120
117#define F_SHRINK 0x00000001UL 121#define F_SHRINK 0x00000001UL
118#define F_ALLOW_UNKNOWN 0x00000002UL 122#define F_ALLOW_UNKNOWN 0x00000002UL
200#endif 204#endif
201 } 205 }
202} 206}
203 207
204// minimum length of a string to be registered for stringref 208// minimum length of a string to be registered for stringref
205ecb_inline int 209ecb_inline STRLEN
206minimum_string_length (UV idx) 210minimum_string_length (UV idx)
207{ 211{
208 return idx <= 23 ? 3 212 return idx <= 23 ? 3
209 : idx <= 0xffU ? 4 213 : idx <= 0xffU ? 4
210 : idx <= 0xffffU ? 5 214 : idx <= 0xffffU ? 5
238 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 242 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
239 enc->cur = SvPVX (enc->sv) + cur; 243 enc->cur = SvPVX (enc->sv) + cur;
240 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 244 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
241 } 245 }
242} 246}
247
248static void encode_sv (enc_t *enc, SV *sv);
243 249
244ecb_inline void 250ecb_inline void
245encode_ch (enc_t *enc, char ch) 251encode_ch (enc_t *enc, char ch)
246{ 252{
247 need (enc, 1); 253 need (enc, 1);
423encode_bool (enc_t *enc, int istrue) 429encode_bool (enc_t *enc, int istrue)
424{ 430{
425 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 431 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
426} 432}
427 433
434// encodes an arrayref containing key-value pairs as CBOR map
435ecb_inline void
436encode_array_as_map (enc_t *enc, SV *sv)
437{
438 if (enc->depth >= enc->cbor.max_depth)
439 croak (ERR_NESTING_EXCEEDED);
440
441 ++enc->depth;
442
443 // as_map does error checking for us, but we re-check in case
444 // things have changed.
445
446 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
447 croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)");
448
449 AV *av = (AV *)SvRV (sv);
450 int i, len = av_len (av);
451
452 if (!(len & 1))
453 croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)");
454
455 encode_uint (enc, MAJOR_MAP, (len + 1) >> 1);
456
457 for (i = 0; i <= len; ++i)
458 {
459 SV **svp = av_fetch (av, i, 0);
460 encode_sv (enc, svp ? *svp : &PL_sv_undef);
461 }
462
463 --enc->depth;
464}
465
428ecb_inline void 466ecb_inline void
429encode_forced (enc_t *enc, UV type, SV *sv) 467encode_forced (enc_t *enc, UV type, SV *sv)
430{ 468{
431 switch (type) 469 switch (type)
432 { 470 {
461 499
462 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 500 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
463 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 501 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
464 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 502 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
465 503
504 case AS_MAP: encode_array_as_map (enc, sv); break;
505
466 default: 506 default:
467 croak ("encountered malformed CBOR::XS::Tagged object"); 507 croak ("encountered malformed CBOR::XS::Tagged object");
468 } 508 }
469} 509}
470
471static void encode_sv (enc_t *enc, SV *sv);
472 510
473static void 511static void
474encode_av (enc_t *enc, AV *av) 512encode_av (enc_t *enc, AV *av)
475{ 513{
476 int i, len = av_len (av); 514 int i, len = av_len (av);
952 990
953 WANT (len); 991 WANT (len);
954 dec->cur += len; 992 dec->cur += len;
955 993
956 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 994 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
957 if (!is_utf8_string (key, len)) 995 if (!cbor_is_utf8_string ((U8 *)key, len))
958 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 996 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
959 997
960 hv_store (hv, key, -len, decode_sv (dec), 0); 998 hv_store (hv, key, -len, decode_sv (dec), 0);
961 999
962 return; 1000 return;
1091 } 1129 }
1092 1130
1093 if (utf8) 1131 if (utf8)
1094 { 1132 {
1095 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1133 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1096 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1134 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
1097 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1135 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
1098 1136
1099 SvUTF8_on (sv); 1137 SvUTF8_on (sv);
1100 } 1138 }
1101 1139
1158 if (ecb_expect_false (!dec->shareable)) 1196 if (ecb_expect_false (!dec->shareable))
1159 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1197 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
1160 1198
1161 if (dec->cbor.flags & F_ALLOW_CYCLES) 1199 if (dec->cbor.flags & F_ALLOW_CYCLES)
1162 { 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;
1163 sv = newSV (0); 1205 AV *av = newAV ();
1164 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1206 av_push (dec->shareable, (SV *)av);
1207 idx = AvFILLp (dec->shareable);
1165 1208
1166 SV *osv = decode_sv (dec); 1209 sv = decode_sv (dec);
1167 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
1168 SvREFCNT_dec_NN (osv); 1217 SvREFCNT_dec_NN ((SV *)av);
1218 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1169 } 1219 }
1170 else 1220 else
1171 { 1221 {
1172 av_push (dec->shareable, &PL_sv_undef); 1222 av_push (dec->shareable, &PL_sv_undef);
1173 int idx = AvFILLp (dec->shareable); 1223 int idx = AvFILLp (dec->shareable);
1174 sv = decode_sv (dec); 1224 sv = decode_sv (dec);
1175 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); 1225 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1176 } 1226 }
1177 } 1227 }
1178 break; 1228 break;
1179 1229
1180 case CBOR_TAG_VALUE_SHAREDREF: 1230 case CBOR_TAG_VALUE_SHAREDREF:
1185 UV idx = decode_uint (dec); 1235 UV idx = decode_uint (dec);
1186 1236
1187 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1237 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1188 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1238 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1189 1239
1190 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1240 sv = AvARRAY (dec->shareable)[idx];
1191 1241
1192 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
1193 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);
1194 } 1254 }
1195 break; 1255 break;
1196 1256
1197 case CBOR_TAG_PERL_OBJECT: 1257 case CBOR_TAG_PERL_OBJECT:
1198 { 1258 {
1435 SvREFCNT_dec_NN (sv); 1495 SvREFCNT_dec_NN (sv);
1436 1496
1437 if (dec.err_sv) 1497 if (dec.err_sv)
1438 sv_2mortal (dec.err_sv); 1498 sv_2mortal (dec.err_sv);
1439 1499
1440 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);
1441 } 1501 }
1442 1502
1443 sv = sv_2mortal (sv); 1503 sv = sv_2mortal (sv);
1444 1504
1445 return sv; 1505 return sv;
1534 1594
1535 break; 1595 break;
1536 1596
1537 case MAJOR_MAP >> MAJOR_SHIFT: 1597 case MAJOR_MAP >> MAJOR_SHIFT:
1538 len <<= 1; 1598 len <<= 1;
1599 /* FALLTHROUGH */
1539 case MAJOR_ARRAY >> MAJOR_SHIFT: 1600 case MAJOR_ARRAY >> MAJOR_SHIFT:
1540 if (len) 1601 if (len)
1541 { 1602 {
1542 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1603 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1543 count = len + 1; 1604 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines