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