Skip to content

Commit cc5844d

Browse files
nihil-admirarimstorsjo
authored andcommitted
libavutil: Add wchartoutf8(), wchartoansi(), utf8toansi(), getenv_utf8(), freeenv_utf8() and getenv_dup()
wchartoutf8() converts strings returned by WinAPI into UTF-8, which is FFmpeg's preffered encoding. Some external dependencies, such as AviSynth, are still not Unicode-enabled. utf8toansi() converts UTF-8 strings into ANSI in two steps: UTF-8 -> wchar_t -> ANSI. wchartoansi() is responsible for the second step of the conversion. Conversion in just one step is not supported by WinAPI. Since these character converting functions allocate the buffer of necessary size, they also facilitate the removal of MAX_PATH limit in places where fixed-size ANSI/WCHAR strings were used as filename buffers. On Windows, getenv_utf8() wraps _wgetenv() converting its input from and its output to UTF-8. Strings returned by getenv_utf8() must be freed by freeenv_utf8(). On all other platforms getenv_utf8() is a wrapper around getenv(), and freeenv_utf8() is a no-op. The value returned by plain getenv() cannot be modified; av_strdup() is usually used when modifications are required. However, on Windows, av_strdup() after getenv_utf8() leads to unnecessary allocation. getenv_dup() is introduced to avoid such an allocation. Value returned by getenv_dup() must be freed by av_free(). Because of cleanup complexities, in places that only test the existence of an environment variable or compare its value with a string consisting entirely of ASCII characters, the use of plain getenv() is still preferred. (libavutil/log.c check_color_terminal() is an example of such a place.) Plain getenv() is also preffered in UNIX-only code, such as bktr.c, fbdev_common.c, oss.c in libavdevice or af_ladspa.c in libavfilter. Signed-off-by: Martin Storsjö <martin@martin.st>
1 parent c11fb46 commit cc5844d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,7 @@ SYSTEM_FUNCS="
22722272
fcntl
22732273
getaddrinfo
22742274
getauxval
2275+
getenv
22752276
gethrtime
22762277
getopt
22772278
GetModuleHandle

libavutil/getenv_utf8.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* This file is part of FFmpeg.
3+
*
4+
* FFmpeg is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* FFmpeg is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with FFmpeg; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef AVUTIL_GETENV_UTF8_H
20+
#define AVUTIL_GETENV_UTF8_H
21+
22+
#include <stdlib.h>
23+
24+
#include "config.h"
25+
#include "mem.h"
26+
27+
#if HAVE_GETENV && defined(_WIN32)
28+
29+
#include "libavutil/wchar_filename.h"
30+
31+
static inline char *getenv_utf8(const char *varname)
32+
{
33+
wchar_t *varname_w, *var_w;
34+
char *var;
35+
36+
if (utf8towchar(varname, &varname_w))
37+
return NULL;
38+
if (!varname_w)
39+
return NULL;
40+
41+
var_w = _wgetenv(varname_w);
42+
av_free(varname_w);
43+
44+
if (!var_w)
45+
return NULL;
46+
if (wchartoutf8(var_w, &var))
47+
return NULL;
48+
49+
return var;
50+
51+
// No CP_ACP fallback compared to other *_utf8() functions:
52+
// non UTF-8 strings must not be returned.
53+
}
54+
55+
static inline void freeenv_utf8(char *var)
56+
{
57+
av_free(var);
58+
}
59+
60+
static inline char *getenv_dup(const char *varname)
61+
{
62+
return getenv_utf8(varname);
63+
}
64+
65+
#else
66+
67+
static inline char *getenv_utf8(const char *varname)
68+
{
69+
return getenv(varname);
70+
}
71+
72+
static inline void freeenv_utf8(char *var)
73+
{
74+
}
75+
76+
static inline char *getenv_dup(const char *varname)
77+
{
78+
char *var = getenv(varname);
79+
if (!var)
80+
return NULL;
81+
return av_strdup(var);
82+
}
83+
84+
#endif // HAVE_GETENV && defined(_WIN32)
85+
86+
#endif // AVUTIL_GETENV_UTF8_H

libavutil/wchar_filename.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#define AVUTIL_WCHAR_FILENAME_H
2121

2222
#ifdef _WIN32
23+
24+
#define WIN32_LEAN_AND_MEAN
2325
#include <windows.h>
2426
#include "mem.h"
2527

@@ -41,6 +43,57 @@ static inline int utf8towchar(const char *filename_utf8, wchar_t **filename_w)
4143
return 0;
4244
}
4345

46+
av_warn_unused_result
47+
static inline int wchartocp(unsigned int code_page, const wchar_t *filename_w,
48+
char **filename)
49+
{
50+
DWORD flags = code_page == CP_UTF8 ? WC_ERR_INVALID_CHARS : 0;
51+
int num_chars = WideCharToMultiByte(code_page, flags, filename_w, -1,
52+
NULL, 0, NULL, NULL);
53+
if (num_chars <= 0) {
54+
*filename = NULL;
55+
return 0;
56+
}
57+
*filename = av_malloc_array(num_chars, sizeof *filename);
58+
if (!*filename) {
59+
errno = ENOMEM;
60+
return -1;
61+
}
62+
WideCharToMultiByte(code_page, flags, filename_w, -1,
63+
*filename, num_chars, NULL, NULL);
64+
return 0;
65+
}
66+
67+
av_warn_unused_result
68+
static inline int wchartoutf8(const wchar_t *filename_w, char **filename)
69+
{
70+
return wchartocp(CP_UTF8, filename_w, filename);
71+
}
72+
73+
av_warn_unused_result
74+
static inline int wchartoansi(const wchar_t *filename_w, char **filename)
75+
{
76+
return wchartocp(CP_ACP, filename_w, filename);
77+
}
78+
79+
av_warn_unused_result
80+
static inline int utf8toansi(const char *filename_utf8, char **filename)
81+
{
82+
wchar_t *filename_w = NULL;
83+
int ret = -1;
84+
if (utf8towchar(filename_utf8, &filename_w))
85+
return -1;
86+
87+
if (!filename_w) {
88+
*filename = NULL;
89+
return 0;
90+
}
91+
92+
ret = wchartoansi(filename_w, filename);
93+
av_free(filename_w);
94+
return ret;
95+
}
96+
4497
/**
4598
* Checks for extended path prefixes for which normalization needs to be skipped.
4699
* see .NET6: PathInternal.IsExtended()

0 commit comments

Comments
 (0)