[FFmpeg-devel] [PATCH] avformat/ffmetadec: do no limit size of tags to 1024

Paul B Mahol onemda at gmail.com
Thu Dec 13 15:06:18 EET 2018


Use bprint API instead.

Fixes #4833.

Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavformat/ffmetadec.c | 57 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/libavformat/ffmetadec.c b/libavformat/ffmetadec.c
index 3290b3b7bc..6f7133e389 100644
--- a/libavformat/ffmetadec.c
+++ b/libavformat/ffmetadec.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/bprint.h"
 #include "libavutil/mathematics.h"
 #include "avformat.h"
 #include "ffmeta.h"
@@ -32,6 +33,48 @@ static int probe(AVProbeData *p)
     return 0;
 }
 
+static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
+{
+    int len, end;
+    int64_t read = 0;
+    char tmp[1024];
+    char c;
+    char prev = ' ';
+
+    do {
+        len = 0;
+        do {
+            c = avio_r8(s);
+            end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
+            if (!end)
+                tmp[len++] = c;
+            prev = c;
+        } while (!end && len < sizeof(tmp));
+        av_bprint_append_data(bp, tmp, len);
+        read += len;
+    } while (!end);
+
+    if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
+        avio_skip(s, -1);
+
+    if (!c && s->error)
+        return s->error;
+
+    if (!c && !read && avio_feof(s))
+        return AVERROR_EOF;
+
+    return read;
+}
+
+static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
+{
+
+    do {
+        av_bprint_clear(bp);
+        read_line_to_bprint_escaped(s, bp);
+    } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
+}
+
 static void get_line(AVIOContext *s, uint8_t *buf, int size)
 {
     do {
@@ -128,12 +171,14 @@ static int read_tag(const uint8_t *line, AVDictionary **m)
 static int read_header(AVFormatContext *s)
 {
     AVDictionary **m = &s->metadata;
-    uint8_t line[1024];
+    AVBPrint bp;
+
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
 
     while(!avio_feof(s->pb)) {
-        get_line(s->pb, line, sizeof(line));
+        get_bprint_line(s->pb, &bp);
 
-        if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
+        if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
             AVStream *st = avformat_new_stream(s, NULL);
 
             if (!st)
@@ -143,7 +188,7 @@ static int read_header(AVFormatContext *s)
             st->codecpar->codec_id   = AV_CODEC_ID_FFMETADATA;
 
             m = &st->metadata;
-        } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
+        } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
             AVChapter *ch = read_chapter(s);
 
             if (!ch)
@@ -151,9 +196,11 @@ static int read_header(AVFormatContext *s)
 
             m = &ch->metadata;
         } else
-            read_tag(line, m);
+            read_tag(bp.str, m);
     }
 
+    av_bprint_finalize(&bp, NULL);
+
     s->start_time = 0;
     if (s->nb_chapters)
         s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
-- 
2.17.1



More information about the ffmpeg-devel mailing list