diff options
author | Matt Oliver <protogonoi@gmail.com> | 2016-10-29 16:08:41 +0300 |
---|---|---|
committer | Matt Oliver <protogonoi@gmail.com> | 2016-11-05 10:08:32 +0300 |
commit | 85553b42f92457a581e13edbd3e2c2e6136931eb (patch) | |
tree | 432c491bc604c42c3341e145d8990aeb5ead804f /compat/w32dlfcn.h | |
parent | 55061bbc558e22db04a40e4efed46d9c15b124b6 (diff) |
compat/w32dlfcn.h: Add safe win32 dlopen/dlclose/dlsym functions.
Signed-off-by: Matt Oliver <protogonoi@gmail.com>
Diffstat (limited to 'compat/w32dlfcn.h')
-rw-r--r-- | compat/w32dlfcn.h | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h new file mode 100644 index 0000000000..bc9bb8c9f5 --- /dev/null +++ b/compat/w32dlfcn.h @@ -0,0 +1,83 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef COMPAT_W32DLFCN_H +#define COMPAT_W32DLFCN_H + +#ifdef _WIN32 +#include <windows.h> +#if _WIN32_WINNT < 0x0602 +#include "libavutil/wchar_filename.h" +#endif +/** + * Safe function used to open dynamic libs. This attempts to improve program security + * by removing the current directory from the dll search path. Only dll's found in the + * executable or system directory are allowed to be loaded. + * @param name The dynamic lib name. + * @return A handle to the opened lib. + */ +static inline HMODULE win32_dlopen(const char *name) +{ +#if _WIN32_WINNT < 0x0602 + // Need to check if KB2533623 is available + if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) { + HMODULE module = NULL; + wchar_t *path = NULL, *name_w = NULL; + DWORD pathlen; + if (utf8towchar(name, &name_w)) + goto exit; + path = (wchar_t *)av_mallocz_array(MAX_PATH, sizeof(wchar_t)); + // Try local directory first + pathlen = GetModuleFileNameW(NULL, path, MAX_PATH); + pathlen = wcsrchr(path, '\\') - path; + if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH) + goto exit; + path[pathlen] = '\\'; + wcscpy(path + pathlen + 1, name_w); + module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + if (module == NULL) { + // Next try System32 directory + pathlen = GetSystemDirectoryW(path, MAX_PATH); + if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH) + goto exit; + path[pathlen] = '\\'; + wcscpy(path + pathlen + 1, name_w); + module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + } +exit: + av_free(path); + av_free(name_w); + return module; + } +#endif +#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR +# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200 +#endif +#ifndef LOAD_LIBRARY_SEARCH_SYSTEM32 +# define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800 +#endif + return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); +} +#define dlopen(name, flags) win32_dlopen(name) +#define dlclose FreeLibrary +#define dlsym GetProcAddress +#else +#include <dlfcn.h> +#endif + +#endif /* COMPAT_W32DLFCN_H */ |