1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

fftools: Remove MAX_PATH limit and switch to UTF-8 versions of fopen() and getenv()

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Nil Admirari 2022-06-20 13:29:59 +03:00 committed by Martin Storsjö
parent dfa062ed3c
commit 13350e81fd
2 changed files with 47 additions and 15 deletions

View File

@ -39,6 +39,7 @@
#include "libavutil/avstring.h" #include "libavutil/avstring.h"
#include "libavutil/channel_layout.h" #include "libavutil/channel_layout.h"
#include "libavutil/display.h" #include "libavutil/display.h"
#include "libavutil/getenv_utf8.h"
#include "libavutil/mathematics.h" #include "libavutil/mathematics.h"
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
#include "libavutil/libm.h" #include "libavutil/libm.h"
@ -47,9 +48,11 @@
#include "libavutil/dict.h" #include "libavutil/dict.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "cmdutils.h" #include "cmdutils.h"
#include "fopen_utf8.h"
#include "opt_common.h" #include "opt_common.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include "compat/w32dlfcn.h"
#endif #endif
AVDictionary *sws_dict; AVDictionary *sws_dict;
@ -465,7 +468,7 @@ static void check_options(const OptionDef *po)
void parse_loglevel(int argc, char **argv, const OptionDef *options) void parse_loglevel(int argc, char **argv, const OptionDef *options)
{ {
int idx = locate_option(argc, argv, options, "loglevel"); int idx = locate_option(argc, argv, options, "loglevel");
const char *env; char *env;
check_options(options); check_options(options);
@ -474,7 +477,8 @@ void parse_loglevel(int argc, char **argv, const OptionDef *options)
if (idx && argv[idx + 1]) if (idx && argv[idx + 1])
opt_loglevel(NULL, "loglevel", argv[idx + 1]); opt_loglevel(NULL, "loglevel", argv[idx + 1]);
idx = locate_option(argc, argv, options, "report"); idx = locate_option(argc, argv, options, "report");
if ((env = getenv("FFREPORT")) || idx) { env = getenv_utf8("FFREPORT");
if (env || idx) {
FILE *report_file = NULL; FILE *report_file = NULL;
init_report(env, &report_file); init_report(env, &report_file);
if (report_file) { if (report_file) {
@ -487,6 +491,7 @@ void parse_loglevel(int argc, char **argv, const OptionDef *options)
fflush(report_file); fflush(report_file);
} }
} }
freeenv_utf8(env);
idx = locate_option(argc, argv, options, "hide_banner"); idx = locate_option(argc, argv, options, "hide_banner");
if (idx) if (idx)
hide_banner = 1; hide_banner = 1;
@ -812,28 +817,45 @@ FILE *get_preset_file(char *filename, size_t filename_size,
{ {
FILE *f = NULL; FILE *f = NULL;
int i; int i;
const char *base[3] = { getenv("FFMPEG_DATADIR"), #if HAVE_GETMODULEHANDLE && defined(_WIN32)
getenv("HOME"), char *datadir = NULL;
#endif
char *env_home = getenv_utf8("HOME");
char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
const char *base[3] = { env_home,
env_ffmpeg_datadir,
FFMPEG_DATADIR, }; FFMPEG_DATADIR, };
if (is_path) { if (is_path) {
av_strlcpy(filename, preset_name, filename_size); av_strlcpy(filename, preset_name, filename_size);
f = fopen(filename, "r"); f = fopen_utf8(filename, "r");
} else { } else {
#if HAVE_GETMODULEHANDLE && defined(_WIN32) #if HAVE_GETMODULEHANDLE && defined(_WIN32)
char datadir[MAX_PATH], *ls; wchar_t *datadir_w = get_module_filename(NULL);
base[2] = NULL; base[2] = NULL;
if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1)) if (wchartoutf8(datadir_w, &datadir))
datadir = NULL;
av_free(datadir_w);
if (datadir)
{ {
for (ls = datadir; ls < datadir + strlen(datadir); ls++) char *ls;
for (ls = datadir; *ls; ls++)
if (*ls == '\\') *ls = '/'; if (*ls == '\\') *ls = '/';
if (ls = strrchr(datadir, '/')) if (ls = strrchr(datadir, '/'))
{ {
*ls = 0; ptrdiff_t datadir_len = ls - datadir;
strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir)); size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
base[2] = datadir; char *new_datadir = av_realloc_array(
datadir, desired_size, sizeof *datadir);
if (new_datadir) {
datadir = new_datadir;
datadir[datadir_len] = 0;
strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
base[2] = datadir;
}
} }
} }
#endif #endif
@ -842,17 +864,22 @@ FILE *get_preset_file(char *filename, size_t filename_size,
continue; continue;
snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
i != 1 ? "" : "/.ffmpeg", preset_name); i != 1 ? "" : "/.ffmpeg", preset_name);
f = fopen(filename, "r"); f = fopen_utf8(filename, "r");
if (!f && codec_name) { if (!f && codec_name) {
snprintf(filename, filename_size, snprintf(filename, filename_size,
"%s%s/%s-%s.ffpreset", "%s%s/%s-%s.ffpreset",
base[i], i != 1 ? "" : "/.ffmpeg", codec_name, base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
preset_name); preset_name);
f = fopen(filename, "r"); f = fopen_utf8(filename, "r");
} }
} }
} }
#if HAVE_GETMODULEHANDLE && defined(_WIN32)
av_free(datadir);
#endif
freeenv_utf8(env_ffmpeg_datadir);
freeenv_utf8(env_home);
return f; return f;
} }

View File

@ -44,6 +44,7 @@
#include "libavutil/avutil.h" #include "libavutil/avutil.h"
#include "libavutil/bprint.h" #include "libavutil/bprint.h"
#include "libavutil/channel_layout.h" #include "libavutil/channel_layout.h"
#include "libavutil/getenv_utf8.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "libavutil/fifo.h" #include "libavutil/fifo.h"
#include "libavutil/mathematics.h" #include "libavutil/mathematics.h"
@ -1402,8 +1403,10 @@ static int get_preset_file_2(const char *preset_name, const char *codec_name, AV
{ {
int i, ret = -1; int i, ret = -1;
char filename[1000]; char filename[1000];
const char *base[3] = { getenv("AVCONV_DATADIR"), char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR");
getenv("HOME"), char *env_home = getenv_utf8("HOME");
const char *base[3] = { env_avconv_datadir,
env_home,
AVCONV_DATADIR, AVCONV_DATADIR,
}; };
@ -1421,6 +1424,8 @@ static int get_preset_file_2(const char *preset_name, const char *codec_name, AV
ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL); ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
} }
} }
freeenv_utf8(env_home);
freeenv_utf8(env_avconv_datadir);
return ret; return ret;
} }