avformat: Use ffio_read_size() where appropriate

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-04-21 19:54:38 +02:00
parent 122f86d859
commit 6dd83fab44
14 changed files with 91 additions and 69 deletions

View File

@ -21,6 +21,7 @@
*/
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
#include "libavutil/intreadwrite.h"
@ -139,12 +140,10 @@ static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
av_assert0(bufsz >= size);
if ((ret = avio_read(s->pb, buf, size)) < 0)
ret = ffio_read_size(s->pb, buf, size);
if (ret < 0)
return ret;
if (ret != size)
return AVERROR(EIO);
return 0;
}
@ -155,10 +154,9 @@ static int argo_brp_read_header(AVFormatContext *s)
ArgoBRPDemuxContext *brp = s->priv_data;
uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
ret = ffio_read_size(pb, buf, BRP_FILE_HEADER_SIZE);
if (ret < 0)
return ret;
else if (ret != BRP_FILE_HEADER_SIZE)
return AVERROR(EIO);
brp->fhdr.magic = AV_RL32(buf + 0);
brp->fhdr.num_streams = AV_RL32(buf + 4);
@ -181,10 +179,9 @@ static int argo_brp_read_header(AVFormatContext *s)
if (!(st = avformat_new_stream(s, NULL)))
return AVERROR(ENOMEM);
if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
ret = ffio_read_size(pb, buf, BRP_STREAM_HEADER_SIZE);
if (ret < 0)
return ret;
else if (ret != BRP_STREAM_HEADER_SIZE)
return AVERROR(EIO);
hdr->codec_id = AV_RL32(buf + 0);
hdr->id = AV_RL32(buf + 4);
@ -286,10 +283,9 @@ static int argo_brp_read_header(AVFormatContext *s)
av_log(s, AV_LOG_TRACE, "Searching %d blocks for BASF...", BRP_BASF_LOOKAHEAD);
for (i = 0; i < BRP_BASF_LOOKAHEAD; i++) {
if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
ret = ffio_read_size(pb, buf, BRP_BLOCK_HEADER_SIZE);
if (ret < 0)
return ret;
else if (ret != BRP_BLOCK_HEADER_SIZE)
return AVERROR(EIO);
blk.stream_id = AV_RL32(buf + 0);
blk.start_ms = AV_RL32(buf + 4);
@ -313,10 +309,9 @@ static int argo_brp_read_header(AVFormatContext *s)
if (blk.size < ASF_CHUNK_HEADER_SIZE)
return AVERROR_INVALIDDATA;
if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
ret = ffio_read_size(pb, buf, BRP_BLOCK_HEADER_SIZE);
if (ret < 0)
return ret;
else if (ret != ASF_CHUNK_HEADER_SIZE)
return AVERROR(EIO);
ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
@ -358,10 +353,9 @@ static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
ArgoASFChunkHeader ckhdr;
int ret;
if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
ret = ffio_read_size(s->pb, buf, BRP_BLOCK_HEADER_SIZE);
if (ret < 0)
return ret;
else if (ret != BRP_BLOCK_HEADER_SIZE)
return AVERROR(EIO);
blk.stream_id = AV_RL32(buf + 0);
blk.start_ms = AV_RL32(buf + 4);
@ -380,8 +374,9 @@ static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
if (blk.size < ASF_CHUNK_HEADER_SIZE)
return AVERROR_INVALIDDATA;
if (avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE) != ASF_CHUNK_HEADER_SIZE)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(s->pb, buf, ASF_CHUNK_HEADER_SIZE);
if (ret < 0)
return ret;
ff_argo_asf_parse_chunk_header(&ckhdr, buf);

View File

@ -25,6 +25,7 @@
#include "libavcodec/bytestream.h"
#include "libavcodec/flac.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "flac_picture.h"
#include "internal.h"
@ -100,11 +101,9 @@ static int flac_read_header(AVFormatContext *s)
if (!buffer) {
return AVERROR(ENOMEM);
}
ret = avio_read(s->pb, buffer, metadata_size);
ret = ffio_read_size(s->pb, buffer, metadata_size);
if (ret < 0) {
RETURN_ERROR(ret);
} else if (ret != metadata_size) {
RETURN_ERROR(AVERROR_EOF);
}
break;
/* skip metadata block for unsupported types */

View File

@ -22,6 +22,7 @@
#include "config_components.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
#include "mux.h"
@ -60,9 +61,11 @@ static int ilbc_read_header(AVFormatContext *s)
AVIOContext *pb = s->pb;
AVStream *st;
uint8_t header[9];
int ret;
if (avio_read(pb, header, 9) != 9)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, header, 9);
if (ret < 0)
return ret;
st = avformat_new_stream(s, NULL);
if (!st)

View File

@ -35,6 +35,7 @@
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
@ -611,11 +612,13 @@ static int ipmovie_read_header(AVFormatContext *s)
unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
int chunk_type, i;
uint8_t signature_buffer[sizeof(signature)];
int ret;
ipmovie->avf = s;
if (avio_read(pb, signature_buffer, sizeof(signature_buffer)) != sizeof(signature_buffer))
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, signature_buffer, sizeof(signature_buffer));
if (ret < 0)
return ret;
while (memcmp(signature_buffer, signature, sizeof(signature))) {
memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);

View File

@ -34,6 +34,7 @@
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
@ -144,8 +145,9 @@ static int read_packet(AVFormatContext *s,
if (avio_feof(pb))
return AVERROR_EOF;
if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE)
return AVERROR(EIO);
ret = ffio_read_size(pb, preamble, MM_PREAMBLE_SIZE);
if (ret < 0)
return ret;
type = AV_RL16(&preamble[0]);
length = AV_RL16(&preamble[2]);
@ -163,8 +165,9 @@ static int read_packet(AVFormatContext *s,
if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
return ret;
memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
return AVERROR(EIO);
ret = ffio_read_size(pb, pkt->data + MM_PREAMBLE_SIZE, length);
if (ret < 0)
return ret;
pkt->size = length + MM_PREAMBLE_SIZE;
pkt->stream_index = 0;
if (type!=MM_TYPE_PALETTE)

View File

@ -566,9 +566,9 @@ redo:
static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
unsigned char buf[8];
ret = avio_read(s->pb, buf, 8);
if (ret != 8)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(s->pb, buf, 8);
if (ret < 0)
return ret;
avio_seek(s->pb, -8, SEEK_CUR);
if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
codec_id = AV_CODEC_ID_CAVS;

View File

@ -62,6 +62,7 @@
#include "libavutil/opt.h"
#include "avformat.h"
#include "avlanguage.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
#include "mxf.h"
@ -659,6 +660,7 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv
uint64_t plaintext_size;
uint8_t ivec[16];
uint8_t tmpbuf[16];
int ret;
int index;
int body_sid;
@ -696,8 +698,9 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv
if (size < 32 || size - 32 < orig_size || (int)orig_size != orig_size)
return AVERROR_INVALIDDATA;
avio_read(pb, ivec, 16);
if (avio_read(pb, tmpbuf, 16) != 16)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, tmpbuf, 16);
if (ret < 16)
return ret;
if (mxf->aesc)
av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
if (memcmp(tmpbuf, checkv, 16))
@ -753,6 +756,7 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size
uint64_t footer_partition;
uint32_t nb_essence_containers;
uint64_t this_partition;
int ret;
if (mxf->partitions_count >= INT_MAX / 2)
return AVERROR_INVALIDDATA;
@ -818,9 +822,10 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size
if (partition->body_offset < 0)
return AVERROR_INVALIDDATA;
if (avio_read(pb, op, sizeof(UID)) != sizeof(UID)) {
ret = ffio_read_size(pb, op, sizeof(UID));
if (ret < 0) {
av_log(mxf->fc, AV_LOG_ERROR, "Failed reading UID\n");
return AVERROR_INVALIDDATA;
return ret;
}
nb_essence_containers = avio_rb32(pb);
@ -1552,12 +1557,14 @@ static int mxf_read_indirect_value(void *arg, AVIOContext *pb, int size)
{
MXFTaggedValue *tagged_value = arg;
uint8_t key[17];
int ret;
if (size <= 17)
return 0;
if (avio_read(pb, key, 17) != 17)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, key, 17);
if (ret < 0)
return ret;
/* TODO: handle other types of of indirect values */
if (memcmp(key, mxf_indirect_value_utf16le, 17) == 0) {
return mxf_read_utf16le_string(pb, size - 17, &tagged_value->value);

View File

@ -30,6 +30,7 @@
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "riff.h"
@ -94,6 +95,7 @@ static int qcp_read_header(AVFormatContext *s)
QCPContext *c = s->priv_data;
AVStream *st = avformat_new_stream(s, NULL);
uint8_t buf[16];
int ret;
int i;
unsigned nb_rates;
@ -105,8 +107,9 @@ static int qcp_read_header(AVFormatContext *s)
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
if (avio_read(pb, buf, 16) != 16)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, buf, 16);
if (ret < 0)
return ret;
if (is_qcelp_13k_guid(buf)) {
st->codecpar->codec_id = AV_CODEC_ID_QCELP;
} else if (!memcmp(buf, guid_evrc, 16)) {

View File

@ -188,8 +188,9 @@ static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
st->codecpar->ch_layout.nb_channels = avio_rb16(pb);
if (version == 5) {
ast->deint_id = avio_rl32(pb);
if (avio_read(pb, buf, 4) != 4)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, buf, 4);
if (ret < 0)
return ret;
buf[4] = 0;
} else {
AV_WL32(buf, 0);
@ -816,10 +817,11 @@ static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
pkt->data[0] = 0;
AV_WL32(pkt->data + 1, 1);
AV_WL32(pkt->data + 5, 0);
if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
ret = ffio_read_size(pb, pkt->data + 9, len);
if (ret < 0) {
av_packet_unref(pkt);
av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
return ret < 0 ? ret : AVERROR(EIO);
return ret;
}
return 0;
}
@ -856,8 +858,9 @@ static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
return 1;
}
if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
return AVERROR(EIO);
ret = ffio_read_size(pb, vst->pkt.data + vst->videobufpos, len);
if (ret < 0)
return ret;
vst->videobufpos += len;
rm->remaining_len-= len;

View File

@ -96,10 +96,10 @@ static int tak_read_header(AVFormatContext *s)
memset(buffer + size - 3, 0, AV_INPUT_BUFFER_PADDING_SIZE);
ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
ret = avio_read(pb, buffer, size - 3);
if (ret != size - 3) {
ret = ffio_read_size(pb, buffer, size - 3);
if (ret < 0) {
av_freep(&buffer);
return ret < 0 ? ret : AVERROR_INVALIDDATA;
return ret;
}
if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
@ -117,9 +117,9 @@ static int tak_read_header(AVFormatContext *s)
if (size != 19)
return AVERROR_INVALIDDATA;
ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
ret = avio_read(pb, md5, 16);
if (ret != 16)
return ret < 0 ? ret : AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, md5, 16);
if (ret < 0)
return ret;
if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
if (s->error_recognition & AV_EF_EXPLODE)

View File

@ -567,8 +567,9 @@ static int viv_read_header(AVFormatContext *s)
v = avio_r8(pb);
avio_seek(pb, v, SEEK_CUR);
if (avio_read(pb, keybuffer, 187) != 187)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, keybuffer, 187);
if (ret < 0)
return ret;
key = decode_key(keybuffer);
viv->sb_key = key;

View File

@ -20,6 +20,7 @@
*/
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
#include "libavutil/intreadwrite.h"
@ -65,10 +66,10 @@ static int add_metadata(AVFormatContext *s, uint32_t tag,
if (!buf)
return AVERROR(ENOMEM);
ret = avio_read(s->pb, buf, len);
if (ret < 0 || ret != len) {
ret = ffio_read_size(s->pb, buf, len);
if (ret < 0) {
av_free(buf);
return ret < 0 ? ret : AVERROR_INVALIDDATA;
return ret;
}
buf[len] = 0;
AV_WL32(key, tag);
@ -259,11 +260,9 @@ static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
pkt->data[1] = c->last_frame_bits;
ret = avio_read(s->pb, pkt->data+2, size);
if (ret != size) {
return AVERROR(EIO);
}
ret = ffio_read_size(s->pb, pkt->data + 2, size);
if (ret < 0)
return ret;
c->last_frame_bits = pkt->data[size+1];
c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;

View File

@ -872,9 +872,12 @@ static int w64_read_header(AVFormatContext *s)
WAVDemuxContext *wav = s->priv_data;
AVStream *st;
uint8_t guid[16];
int ret;
int ret = ffio_read_size(pb, guid, 16);
if (avio_read(pb, guid, 16) != 16 || memcmp(guid, ff_w64_guid_riff, 16))
if (ret < 0)
return ret;
if (memcmp(guid, ff_w64_guid_riff, 16))
return AVERROR_INVALIDDATA;
/* riff + wave + fmt + sizes */

View File

@ -34,6 +34,7 @@
#include "libavutil/mem.h"
#include "libavutil/time_internal.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"
#include "wtv.h"
@ -774,6 +775,7 @@ static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_p
{
WtvContext *wtv = s->priv_data;
AVIOContext *pb = wtv->pb;
int ret;
while (!avio_feof(pb)) {
ff_asf_guid g;
int len, sid, consumed;
@ -846,8 +848,9 @@ static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_p
}
buf_size = FFMIN(len - consumed, sizeof(buf));
if (avio_read(pb, buf, buf_size) != buf_size)
return AVERROR_INVALIDDATA;
ret = ffio_read_size(pb, buf, buf_size);
if (ret < 0)
return ret;
consumed += buf_size;
ff_parse_mpeg2_descriptor(s, st, 0, &pbuf, buf + buf_size, NULL, 0, 0, NULL);
}