[vlc-devel] [PATCH 1/2] [RFC] core: add a function to load a system shared library

Steve Lhomme robux4 at videolabs.io
Thu Mar 9 11:50:14 CET 2017


it cannot be loaded from anywhere but the system
---
 include/vlc_modules.h | 13 ++++++++++++
 src/libvlccore.sym    |  1 +
 src/missing.c         |  7 +++++++
 src/network/udp.c     |  3 ++-
 src/text/url.c        |  2 +-
 src/win32/plugin.c    | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/include/vlc_modules.h b/include/vlc_modules.h
index 7f45217969..ec5c69c36a 100644
--- a/include/vlc_modules.h
+++ b/include/vlc_modules.h
@@ -65,6 +65,19 @@ VLC_API const char * module_get_capability( const module_t *m ) VLC_USED;
 VLC_API int module_get_score( const module_t *m ) VLC_USED;
 VLC_API const char * module_gettext( const module_t *, const char * ) VLC_USED;
 
+#ifdef _WIN32
+typedef HMODULE vlc_lib_handle_t;
+#else
+typedef void *vlc_lib_handle_t;
+#endif
+
+/**
+ * Load a system library (DLL) in memory.
+ *
+ * @param libname the name of system library to load.
+ */
+VLC_API vlc_lib_handle_t vlc_load_syslib(const char *libname);
+
 VLC_USED static inline module_t *module_get_main (void)
 {
     return module_find ("core");
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 57c83495a3..1c0e6ee0a3 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -462,6 +462,7 @@ us_vasprintf
 vlc_close
 vlc_fopen
 utf8_fprintf
+vlc_load_syslib
 vlc_loaddir
 vlc_lstat
 vlc_mkdir
diff --git a/src/missing.c b/src/missing.c
index c76e99a4bc..99670c8e95 100644
--- a/src/missing.c
+++ b/src/missing.c
@@ -442,3 +442,10 @@ update_release_t *update_GetRelease(update_t *u)
     vlc_assert_unreachable();
 }
 #endif /* !UPDATE_CHECK */
+
+#ifndef _WIN32
+void *vlc_load_syslib(const char *libname)
+{
+    return NULL;
+}
+#endif
diff --git a/src/network/udp.c b/src/network/udp.c
index d098fb1425..bf3dc2266a 100644
--- a/src/network/udp.c
+++ b/src/network/udp.c
@@ -36,6 +36,7 @@
 #include <errno.h>
 #include <assert.h>
 
+#include <vlc_modules.h>
 #include <vlc_network.h>
 
 #ifdef _WIN32
@@ -105,7 +106,7 @@ static int net_SetupDgramSocket (vlc_object_t *p_obj, int fd,
      * receive buffer if that isn't present
      */
 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
-    HINSTANCE h_Network = LoadLibrary(TEXT("Windows.Networking.dll"));
+    HMODULE h_Network = vlc_load_syslib("Windows.Networking.dll");
     if( (h_Network == NULL) ||
         (GetProcAddress( h_Network, "SetSocketMediaStreamingMode" ) == NULL ) )
     {
diff --git a/src/text/url.c b/src/text/url.c
index 1b602b599e..682ea9665d 100644
--- a/src/text/url.c
+++ b/src/text/url.c
@@ -837,7 +837,7 @@ char *vlc_uri_fixup(const char *str)
 #  define IDN_ALLOW_UNASSIGNED 0x01
 static int IdnToAscii(DWORD flags, LPCWSTR str, int len, LPWSTR buf, int size)
 {
-    HMODULE h = LoadLibrary(_T("Normaliz.dll"));
+    HMODULE h = vlc_load_syslib("Normaliz.dll");
     if (h == NULL)
     {
         errno = ENOSYS;
diff --git a/src/win32/plugin.c b/src/win32/plugin.c
index 1a65521fca..6bade68fbc 100644
--- a/src/win32/plugin.c
+++ b/src/win32/plugin.c
@@ -29,6 +29,7 @@
 
 #include <vlc_common.h>
 #include <vlc_charset.h>
+#include <vlc_modules.h>
 #include "modules/modules.h"
 #include <windows.h>
 #include <wchar.h>
@@ -133,3 +134,59 @@ void *module_Lookup( module_handle_t handle, const char *psz_function )
 {
     return (void *)GetProcAddress( handle, (char *)psz_function );
 }
+
+HMODULE vlc_load_syslib(const char *libname)
+{
+    HMODULE module;
+    bool has_KB2533623 = true;
+    TCHAR *tlibname;
+#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
+    /* check for KB2533623 */
+    if (GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
+                                       "SetDefaultDllDirectories") == NULL)
+        has_KB2533623 = false;
+#endif
+
+#ifdef UNICODE
+    tlibname = ToWide(libname);
+    if (unlikely(tlibname==NULL))
+        return NULL;
+#else
+    tlibname = libname;
+#endif
+
+    if (false && has_KB2533623)
+        module = LoadLibraryEx(tlibname, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
+    else
+    {
+        /* force the path of the DLL inside system32 */
+        DWORD systemLen = GetSystemDirectory(NULL, 0);
+        if (systemLen) {
+            systemLen += 1 + _tcslen(tlibname);
+            TCHAR *sysdir = malloc(systemLen * sizeof(*sysdir));
+            if (likely(sysdir!=NULL)) {
+                if (GetSystemDirectory(sysdir, systemLen)) {
+                    _tcsncat(sysdir, _T("\\"), systemLen);
+                    _tcsncat(sysdir, tlibname, systemLen);
+
+#ifdef UNICODE
+                    free(tlibname);
+#endif
+                    tlibname = sysdir;
+                }
+            }
+        }
+
+        module = LoadLibrary(tlibname);
+
+        if ((const char*)tlibname != libname) {
+            free(tlibname);
+            tlibname = NULL; /* no double free on unicode */
+        }
+    }
+
+#ifdef UNICODE
+    free(tlibname);
+#endif
+    return module;
+}
-- 
2.11.1



More information about the vlc-devel mailing list