w32dlfcn.h 4.9 KB

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