diff mbox

[FFmpeg-devel,v2,2/5] avformat/s337m: Split read_packet/get_packet

Message ID 20190806115044.4116-3-nicolas.gaullier@arkena.com
State New
Headers show

Commit Message

Gaullier Nicolas Aug. 6, 2019, 11:50 a.m. UTC
Prepare use of s337m_get_packet from outside
---
 libavformat/s337m.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

Comments

Tomas Härdin Aug. 6, 2019, 12:07 p.m. UTC | #1
tis 2019-08-06 klockan 13:50 +0200 skrev Nicolas Gaullier:
> Prepare use of s337m_get_packet from outside
> ---
>  libavformat/s337m.c | 29 +++++++++++++++++++++++------
>  1 file changed, 23 insertions(+), 6 deletions(-)

Looks OK

/Tomas
diff mbox

Patch

diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 8956afb23f..22140297e6 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -31,6 +31,8 @@ 
 #define IS_24LE_MARKER(state)   ((state & 0xFFFFFFFFFFFF) == MARKER_24LE)
 #define IS_LE_MARKER(state)     (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state))
 
+int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec);
+
 static int s337m_get_offset_and_codec(void *avc,
                                       uint64_t state,
                                       int data_type, int data_size,
@@ -141,18 +143,20 @@  static void bswap_buf24(uint8_t *data, int size)
         FFSWAP(uint8_t, data[0], data[2]);
 }
 
-static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
+int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec)
 {
-    AVIOContext *pb = s->pb;
     uint64_t state = 0;
     int ret, data_type, data_size, offset;
-    enum AVCodecID codec;
-    int64_t pos;
+    int64_t pos, orig_pos = avio_tell(pb);
 
     while (!IS_LE_MARKER(state)) {
         state = (state << 8) | avio_r8(pb);
         if (avio_feof(pb))
             return AVERROR_EOF;
+        if (avio_tell(pb) - orig_pos + 6 >= size) {
+            av_log(avc, AV_LOG_ERROR, "s337m : sync bytes not found at packet pos=0x%"PRIx64" size_max=%d\n", orig_pos, size);
+            return AVERROR_INVALIDDATA;
+        }
     }
 
     if (IS_16LE_MARKER(state)) {
@@ -165,10 +169,12 @@  static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     pos = avio_tell(pb);
 
-    if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
+    if ((ret = s337m_get_offset_and_codec(avc, state, data_type, data_size, &offset, codec)) < 0)
         return ret;
 
-    if ((ret = av_new_packet(pkt, offset)) < 0)
+    if (pos - orig_pos > size)
+        return AVERROR_INVALIDDATA;
+    if (ret = av_new_packet(pkt, FFMIN(offset, size - (pos - orig_pos))) < 0)
         return ret;
 
     pkt->pos = pos;
@@ -183,6 +189,17 @@  static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
     else
         bswap_buf24(pkt->data, pkt->size);
 
+    return 0;
+}
+
+static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    enum AVCodecID codec;
+    int ret;
+
+    if ((ret = avpriv_s337m_get_packet((void *)s, s->pb, pkt, avio_size(s->pb), &codec)) < 0)
+        return ret;
+
     if (!s->nb_streams) {
         AVStream *st = avformat_new_stream(s, NULL);
         if (!st) {