w32dlfcn.h 3.0 KB

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