w32dlfcn.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef COMPAT_W32DLFCN_H
  19. #define COMPAT_W32DLFCN_H
  20. #ifdef _WIN32
  21. #include <stdint.h>
  22. #include <windows.h>
  23. #include "config.h"
  24. #include "libavutil/macros.h"
  25. #include "libavutil/wchar_filename.h"
  26. static inline wchar_t *get_module_filename(HMODULE module)
  27. {
  28. wchar_t *path = NULL, *new_path;
  29. DWORD path_size = 0, path_len;
  30. do {
  31. path_size = path_size ? FFMIN(2 * path_size, INT16_MAX + 1) : MAX_PATH;
  32. new_path = av_realloc_array(path, path_size, sizeof *path);
  33. if (!new_path) {
  34. av_free(path);
  35. return NULL;
  36. }
  37. path = new_path;
  38. // Returns path_size in case of insufficient buffer.
  39. // Whether the error is set or not and whether the output
  40. // is null-terminated or not depends on the version of Windows.
  41. path_len = GetModuleFileNameW(module, path, path_size);
  42. } while (path_len && path_size <= INT16_MAX && path_size <= path_len);
  43. if (!path_len) {
  44. av_free(path);
  45. return NULL;
  46. }
  47. return path;
  48. }
  49. /**
  50. * Safe function used to open dynamic libs. This attempts to improve program security
  51. * by removing the current directory from the dll search path. Only dll's found in the
  52. * executable or system directory are allowed to be loaded.
  53. * @param name The dynamic lib name.
  54. * @return A handle to the opened lib.
  55. */
  56. static inline HMODULE win32_dlopen(const char *name)
  57. {
  58. wchar_t *name_w;
  59. HMODULE module = NULL;
  60. if (utf8towchar(name, &name_w))
  61. name_w = NULL;
  62. #if _WIN32_WINNT < 0x0602
  63. // On Win7 and earlier we check if KB2533623 is available
  64. if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) {
  65. wchar_t *path = NULL, *new_path;
  66. DWORD pathlen, pathsize, namelen;
  67. if (!name_w)
  68. goto exit;
  69. namelen = wcslen(name_w);
  70. // Try local directory first
  71. path = get_module_filename(NULL);
  72. if (!path)
  73. goto exit;
  74. new_path = wcsrchr(path, '\\');
  75. if (!new_path)
  76. goto exit;
  77. pathlen = new_path - path;
  78. pathsize = pathlen + namelen + 2;
  79. new_path = av_realloc_array(path, pathsize, sizeof *path);
  80. if (!new_path)
  81. goto exit;
  82. path = new_path;
  83. wcscpy(path + pathlen + 1, name_w);
  84. module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  85. if (module == NULL) {
  86. // Next try System32 directory
  87. pathlen = GetSystemDirectoryW(path, pathsize);
  88. if (!pathlen)
  89. goto exit;
  90. // Buffer is not enough in two cases:
  91. // 1. system directory + \ + module name
  92. // 2. system directory even without the module name.
  93. if (pathlen + namelen + 2 > pathsize) {
  94. pathsize = pathlen + namelen + 2;
  95. new_path = av_realloc_array(path, pathsize, sizeof *path);
  96. if (!new_path)
  97. goto exit;
  98. path = new_path;
  99. // Query again to handle the case #2.
  100. pathlen = GetSystemDirectoryW(path, pathsize);
  101. if (!pathlen)
  102. goto exit;
  103. }
  104. path[pathlen] = L'\\';
  105. wcscpy(path + pathlen + 1, name_w);
  106. module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  107. }
  108. exit:
  109. av_free(path);
  110. av_free(name_w);
  111. return module;
  112. }
  113. #endif
  114. #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
  115. # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
  116. #endif
  117. #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
  118. # define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  119. #endif
  120. #if HAVE_WINRT
  121. if (!name_w)
  122. return NULL;
  123. module = LoadPackagedLibrary(name_w, 0);
  124. #else
  125. #define LOAD_FLAGS (LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32)
  126. /* filename may be be in CP_ACP */
  127. if (!name_w)
  128. return LoadLibraryExA(name, NULL, LOAD_FLAGS);
  129. module = LoadLibraryExW(name_w, NULL, LOAD_FLAGS);
  130. #undef LOAD_FLAGS
  131. #endif
  132. av_free(name_w);
  133. return module;
  134. }
  135. #define dlopen(name, flags) win32_dlopen(name)
  136. #define dlclose FreeLibrary
  137. #define dlsym GetProcAddress
  138. #else
  139. #include <dlfcn.h>
  140. #endif
  141. #endif /* COMPAT_W32DLFCN_H */