[FFmpeg-devel] [PATCH 3/3] ffmpeg: stronger ffpresets parsing.

Clément Bœsch ubitux at gmail.com
Thu May 3 23:11:02 CEST 2012


This fixes at least issues with empty lines, and also allow CRLF lines
(in case a user makes its own preset on a MS plateform).
---
 ffmpeg.c |   33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 43a1b0a..1b2d345 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -5528,7 +5528,7 @@ static int opt_data_frames(OptionsContext *o, const char *opt, const char *arg)
 static int opt_preset(OptionsContext *o, const char *opt, const char *arg)
 {
     FILE *f=NULL;
-    char filename[1000], tmp[1000], tmp2[1000], line[1000];
+    char filename[1000], line[1000], tmp_line[1000];
     const char *codec_name = *opt == 'v' ? video_codec_name :
                              *opt == 'a' ? audio_codec_name :
                                            subtitle_codec_name;
@@ -5541,25 +5541,26 @@ static int opt_preset(OptionsContext *o, const char *opt, const char *arg)
         exit_program(1);
     }
 
-    while(!feof(f)){
-        int e= fscanf(f, "%999[^\n]\n", line) - 1;
-        if(line[0] == '#' && !e)
+    while (fgets(line, sizeof(line), f)) {
+        char *key = tmp_line, *value, *endptr;
+
+        if (strcspn(line, "#\n\r") == 0)
             continue;
-        e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
-        if(e){
+        strcpy(tmp_line, line);
+        if (!av_strtok(key,   "=",    &value) ||
+            !av_strtok(value, "\r\n", &endptr)) {
             av_log(NULL, AV_LOG_FATAL, "%s: Invalid syntax: '%s'\n", filename, line);
             exit_program(1);
         }
-        if(!strcmp(tmp, "acodec")){
-            opt_audio_codec(o, tmp, tmp2);
-        }else if(!strcmp(tmp, "vcodec")){
-            opt_video_codec(o, tmp, tmp2);
-        }else if(!strcmp(tmp, "scodec")){
-            opt_subtitle_codec(o, tmp, tmp2);
-        }else if(!strcmp(tmp, "dcodec")){
-            opt_data_codec(o, tmp, tmp2);
-        }else if(opt_default(tmp, tmp2) < 0){
-            av_log(NULL, AV_LOG_FATAL, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
+        av_log(NULL, AV_LOG_DEBUG, "ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
+
+        if      (!strcmp(key, "acodec")) opt_audio_codec   (o, key, value);
+        else if (!strcmp(key, "vcodec")) opt_video_codec   (o, key, value);
+        else if (!strcmp(key, "scodec")) opt_subtitle_codec(o, key, value);
+        else if (!strcmp(key, "dcodec")) opt_data_codec    (o, key, value);
+        else if (opt_default(key, value) < 0) {
+            av_log(NULL, AV_LOG_FATAL, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
+                   filename, line, key, value);
             exit_program(1);
         }
     }
-- 
1.7.10



More information about the ffmpeg-devel mailing list