… | |
… | |
985 | } |
985 | } |
986 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
986 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
987 | { |
987 | { |
988 | STRLEN len = decode_uint (dec); |
988 | STRLEN len = decode_uint (dec); |
989 | char *key = (char *)dec->cur; |
989 | char *key = (char *)dec->cur; |
990 | printf ("len %d\n", len);//D |
|
|
991 | |
990 | |
992 | WANT (len); |
991 | WANT (len); |
993 | dec->cur += len; |
992 | dec->cur += len; |
994 | |
993 | |
995 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
994 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
… | |
… | |
1197 | if (ecb_expect_false (!dec->shareable)) |
1196 | if (ecb_expect_false (!dec->shareable)) |
1198 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1197 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1199 | |
1198 | |
1200 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
1199 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
1201 | { |
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; |
1202 | sv = newSV (0); |
1205 | AV *av = newAV (); |
1203 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
1206 | av_push (dec->shareable, SvREFCNT_inc_NN ((SV *)av)); |
|
|
1207 | idx = AvFILLp (dec->shareable); |
1204 | |
1208 | |
1205 | SV *osv = decode_sv (dec); |
1209 | sv = decode_sv (dec); |
1206 | 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 |
1207 | SvREFCNT_dec_NN (osv); |
1217 | SvREFCNT_dec_NN (av); |
|
|
1218 | AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv); |
1208 | } |
1219 | } |
1209 | else |
1220 | else |
1210 | { |
1221 | { |
1211 | av_push (dec->shareable, &PL_sv_undef); |
1222 | av_push (dec->shareable, &PL_sv_undef); |
1212 | int idx = AvFILLp (dec->shareable); |
1223 | int idx = AvFILLp (dec->shareable); |
… | |
… | |
1224 | UV idx = decode_uint (dec); |
1235 | UV idx = decode_uint (dec); |
1225 | |
1236 | |
1226 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1237 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1227 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1238 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1228 | |
1239 | |
1229 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1240 | sv = AvARRAY (dec->shareable)[idx]; |
1230 | |
1241 | |
1231 | 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, sv); |
|
|
1249 | } |
|
|
1250 | else if (sv == &PL_sv_undef) // not yet decoded, but cycles not allowed |
1232 | 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); |
1233 | } |
1254 | } |
1234 | break; |
1255 | break; |
1235 | |
1256 | |
1236 | case CBOR_TAG_PERL_OBJECT: |
1257 | case CBOR_TAG_PERL_OBJECT: |
1237 | { |
1258 | { |
… | |
… | |
1474 | SvREFCNT_dec_NN (sv); |
1495 | SvREFCNT_dec_NN (sv); |
1475 | |
1496 | |
1476 | if (dec.err_sv) |
1497 | if (dec.err_sv) |
1477 | sv_2mortal (dec.err_sv); |
1498 | sv_2mortal (dec.err_sv); |
1478 | |
1499 | |
1479 | 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); |
1480 | } |
1501 | } |
1481 | |
1502 | |
1482 | sv = sv_2mortal (sv); |
1503 | sv = sv_2mortal (sv); |
1483 | |
1504 | |
1484 | return sv; |
1505 | return sv; |