summaryrefslogtreecommitdiff
path: root/compat/w32dlfcn.h (plain)
blob: bc9bb8c9f51e8a78627bbeabe61348cb42999729
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 COMPAT_W32DLFCN_H
20#define COMPAT_W32DLFCN_H
21
22#ifdef _WIN32
23#include <windows.h>
24#if _WIN32_WINNT < 0x0602
25#include "libavutil/wchar_filename.h"
26#endif
27/**
28 * Safe function used to open dynamic libs. This attempts to improve program security
29 * by removing the current directory from the dll search path. Only dll's found in the
30 * executable or system directory are allowed to be loaded.
31 * @param name The dynamic lib name.
32 * @return A handle to the opened lib.
33 */
34static inline HMODULE win32_dlopen(const char *name)
35{
36#if _WIN32_WINNT < 0x0602
37 // Need to check if KB2533623 is available
38 if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) {
39 HMODULE module = NULL;
40 wchar_t *path = NULL, *name_w = NULL;
41 DWORD pathlen;
42 if (utf8towchar(name, &name_w))
43 goto exit;
44 path = (wchar_t *)av_mallocz_array(MAX_PATH, sizeof(wchar_t));
45 // Try local directory first
46 pathlen = GetModuleFileNameW(NULL, path, MAX_PATH);
47 pathlen = wcsrchr(path, '\\') - path;
48 if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
49 goto exit;
50 path[pathlen] = '\\';
51 wcscpy(path + pathlen + 1, name_w);
52 module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
53 if (module == NULL) {
54 // Next try System32 directory
55 pathlen = GetSystemDirectoryW(path, MAX_PATH);
56 if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
57 goto exit;
58 path[pathlen] = '\\';
59 wcscpy(path + pathlen + 1, name_w);
60 module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
61 }
62exit:
63 av_free(path);
64 av_free(name_w);
65 return module;
66 }
67#endif
68#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
69# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
70#endif
71#ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
72# define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
73#endif
74 return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
75}
76#define dlopen(name, flags) win32_dlopen(name)
77#define dlclose FreeLibrary
78#define dlsym GetProcAddress
79#else
80#include <dlfcn.h>
81#endif
82
83#endif /* COMPAT_W32DLFCN_H */
84