From e036dec9bdd621db809bb23822b7d22f66ab43fc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Sun, 15 Sep 2024 22:46:55 +0200
Subject: [PATCH 5/5] lavf/mxfdec: Remove a call to avio_tell() in
klv_read_packet()
---
libavformat/mxfdec.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
@@ -431,7 +431,7 @@ static void mxf_free_metadataset(MXFMetadataSet **ctx, enum MXFMetadataSetType t
av_freep(ctx);
}
-static int64_t klv_decode_ber_length(AVIOContext *pb)
+static int64_t klv_decode_ber_length(AVIOContext *pb, int *llen)
{
uint64_t size = avio_r8(pb);
if (size & 0x80) { /* long form */
@@ -439,9 +439,13 @@ static int64_t klv_decode_ber_length(AVIOContext *pb)
/* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
if (bytes_num > 8)
return AVERROR_INVALIDDATA;
+ if (llen)
+ *llen = bytes_num + 1;
size = 0;
while (bytes_num--)
size = size << 8 | avio_r8(pb);
+ } else if (llen) {
+ *llen = 1;
}
if (size > INT64_MAX)
return AVERROR_INVALIDDATA;
@@ -480,6 +484,8 @@ static int mxf_read_sync_klv(AVIOContext *pb)
static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb)
{
int64_t length, pos;
+ int llen;
+
if (!mxf_read_sync_klv(pb))
return AVERROR_INVALIDDATA;
klv->offset = avio_tell(pb) - 4;
@@ -488,11 +494,11 @@ static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb)
memcpy(klv->key, mxf_klv_key, 4);
avio_read(pb, klv->key + 4, 12);
- length = klv_decode_ber_length(pb);
+ length = klv_decode_ber_length(pb, &llen);
if (length < 0)
return length;
klv->length = length;
- pos = avio_tell(pb);
+ pos = klv->offset + 16 + llen;
if (pos > INT64_MAX - length)
return AVERROR_INVALIDDATA;
klv->next_klv = pos + length;
@@ -663,15 +669,15 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv
av_aes_init(mxf->aesc, s->key, 128, 1);
}
// crypto context
- size = klv_decode_ber_length(pb);
+ size = klv_decode_ber_length(pb, NULL);
if (size < 0)
return size;
avio_skip(pb, size);
// plaintext offset
- klv_decode_ber_length(pb);
+ klv_decode_ber_length(pb ,NULL);
plaintext_size = avio_rb64(pb);
// source klv key
- klv_decode_ber_length(pb);
+ klv_decode_ber_length(pb, NULL);
avio_read(pb, klv->key, 16);
if (!IS_KLV_KEY(klv, mxf_essence_element_key))
return AVERROR_INVALIDDATA;
@@ -681,12 +687,12 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv
if (index < 0)
return AVERROR_INVALIDDATA;
// source size
- klv_decode_ber_length(pb);
+ klv_decode_ber_length(pb, NULL);
orig_size = avio_rb64(pb);
if (orig_size < plaintext_size)
return AVERROR_INVALIDDATA;
// enc. code
- size = klv_decode_ber_length(pb);
+ size = klv_decode_ber_length(pb, NULL);
if (size < 32 || size - 32 < orig_size || (int)orig_size != orig_size)
return AVERROR_INVALIDDATA;
avio_read(pb, ivec, 16);
--
2.39.2