2012-08-11 02:05:37 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Library Loader
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-11-07 22:57:05 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/platform.h>
|
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
#include <winpr/library.h>
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("library")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
/**
|
|
|
|
* api-ms-win-core-libraryloader-l1-1-1.dll:
|
|
|
|
*
|
|
|
|
* AddDllDirectory
|
2012-09-23 06:02:55 +04:00
|
|
|
* RemoveDllDirectory
|
|
|
|
* SetDefaultDllDirectories
|
2012-08-11 02:05:37 +04:00
|
|
|
* DisableThreadLibraryCalls
|
|
|
|
* EnumResourceLanguagesExA
|
|
|
|
* EnumResourceLanguagesExW
|
|
|
|
* EnumResourceNamesExA
|
|
|
|
* EnumResourceNamesExW
|
|
|
|
* EnumResourceTypesExA
|
|
|
|
* EnumResourceTypesExW
|
|
|
|
* FindResourceExW
|
|
|
|
* FindStringOrdinal
|
|
|
|
* FreeLibrary
|
|
|
|
* FreeLibraryAndExitThread
|
|
|
|
* FreeResource
|
|
|
|
* GetModuleFileNameA
|
|
|
|
* GetModuleFileNameW
|
|
|
|
* GetModuleHandleA
|
|
|
|
* GetModuleHandleExA
|
|
|
|
* GetModuleHandleExW
|
|
|
|
* GetModuleHandleW
|
|
|
|
* GetProcAddress
|
|
|
|
* LoadLibraryExA
|
|
|
|
* LoadLibraryExW
|
|
|
|
* LoadResource
|
|
|
|
* LoadStringA
|
|
|
|
* LoadStringW
|
|
|
|
* LockResource
|
|
|
|
* QueryOptionalDelayLoadedAPI
|
|
|
|
* SizeofResource
|
|
|
|
*/
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
#if !defined(_WIN32) || defined(_UWP)
|
|
|
|
|
2012-09-29 02:10:37 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stdio.h>
|
2013-11-07 22:57:05 +04:00
|
|
|
#include <stdlib.h>
|
2012-09-29 02:10:37 +04:00
|
|
|
#include <unistd.h>
|
2013-11-07 22:57:05 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-09-29 02:10:37 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
#endif
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
DLL_DIRECTORY_COOKIE AddDllDirectory(PCWSTR NewDirectory)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2012-09-23 06:02:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RemoveDllDirectory(DLL_DIRECTORY_COOKIE Cookie)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetDefaultDllDirectories(DWORD DirectoryFlags)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
HMODULE LoadLibraryA(LPCSTR lpLibFileName)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if defined(_UWP)
|
|
|
|
int status;
|
|
|
|
HMODULE hModule = NULL;
|
|
|
|
WCHAR* filenameW = NULL;
|
|
|
|
|
|
|
|
if (!lpLibFileName)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
status = ConvertToUnicode(CP_UTF8, 0, lpLibFileName, -1, &filenameW, 0);
|
|
|
|
|
|
|
|
if (status < 1)
|
|
|
|
return NULL;
|
|
|
|
|
2019-02-07 19:08:18 +03:00
|
|
|
hModule = LoadLibraryW(filenameW);
|
2016-02-06 00:28:45 +03:00
|
|
|
free(filenameW);
|
|
|
|
return hModule;
|
|
|
|
#else
|
2012-08-11 02:05:37 +04:00
|
|
|
HMODULE library;
|
2020-05-27 09:43:00 +03:00
|
|
|
library = dlopen(lpLibFileName, RTLD_GLOBAL | RTLD_LAZY);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2014-03-01 09:32:23 +04:00
|
|
|
if (!library)
|
2012-08-11 02:05:37 +04:00
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
WLog_ERR(TAG, "%s failed with %s", __FUNCTION__, dlerror());
|
2012-08-11 02:05:37 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return library;
|
2016-02-06 00:28:45 +03:00
|
|
|
#endif
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if defined(_UWP)
|
|
|
|
return LoadPackagedLibrary(lpLibFileName, 0);
|
|
|
|
#else
|
2019-02-07 19:08:18 +03:00
|
|
|
char* name = NULL;
|
|
|
|
HMODULE module;
|
|
|
|
int rc = ConvertFromUnicode(CP_UTF8, 0, lpLibFileName, -1, &name, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
module = LoadLibraryA(name);
|
|
|
|
free(name);
|
|
|
|
return module;
|
2016-02-06 00:28:45 +03:00
|
|
|
#endif
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
if (dwFlags != 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "%s does not support dwFlags 0x%08" PRIx32, __FUNCTION__, dwFlags);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2019-02-07 19:08:18 +03:00
|
|
|
if (hFile)
|
|
|
|
WLog_WARN(TAG, "%s does not support hFile != NULL", __FUNCTION__);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2019-02-07 19:08:18 +03:00
|
|
|
return LoadLibraryA(lpLibFileName);
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
if (dwFlags != 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "%s does not support dwFlags 0x%08" PRIx32, __FUNCTION__, dwFlags);
|
2019-02-07 19:08:18 +03:00
|
|
|
|
|
|
|
if (hFile)
|
|
|
|
WLog_WARN(TAG, "%s does not support hFile != NULL", __FUNCTION__);
|
|
|
|
|
|
|
|
return LoadLibraryW(lpLibFileName);
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
#endif
|
|
|
|
|
2017-02-10 18:06:20 +03:00
|
|
|
#if !defined(_WIN32) && !defined(__CYGWIN__)
|
2016-02-06 00:28:45 +03:00
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
|
|
|
|
{
|
|
|
|
FARPROC proc;
|
|
|
|
proc = dlsym(hModule, lpProcName);
|
|
|
|
|
|
|
|
if (proc == NULL)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
|
2019-11-06 17:24:51 +03:00
|
|
|
return (FARPROC)NULL;
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FreeLibrary(HMODULE hLibModule)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
status = dlclose(hLibModule);
|
|
|
|
|
2012-10-19 00:58:27 +04:00
|
|
|
if (status != 0)
|
2012-08-11 02:05:37 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-03-18 22:43:25 +04:00
|
|
|
HMODULE GetModuleHandleA(LPCSTR lpModuleName)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2014-03-18 22:43:25 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
|
|
|
|
{
|
2019-02-07 19:08:18 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2014-03-18 22:43:25 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-07 22:57:05 +04:00
|
|
|
/**
|
|
|
|
* GetModuleFileName:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
|
|
|
|
*
|
|
|
|
* Finding current executable's path without /proc/self/exe:
|
|
|
|
* http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
|
|
|
|
*/
|
|
|
|
|
|
|
|
DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
|
|
|
|
{
|
2019-02-07 19:41:01 +03:00
|
|
|
DWORD status;
|
2019-02-08 14:19:57 +03:00
|
|
|
char* name = calloc(nSize, sizeof(char));
|
|
|
|
if (!name)
|
2019-02-07 19:41:01 +03:00
|
|
|
{
|
2019-02-08 14:19:57 +03:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
|
|
|
return 0;
|
2019-02-07 19:41:01 +03:00
|
|
|
}
|
2019-02-08 14:19:57 +03:00
|
|
|
status = GetModuleFileNameA(hModule, name, nSize);
|
2019-02-07 19:41:01 +03:00
|
|
|
|
2019-02-08 14:19:57 +03:00
|
|
|
if ((status > INT_MAX) || (nSize > INT_MAX))
|
2019-02-07 19:41:01 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status > 0)
|
|
|
|
{
|
2019-02-08 14:19:57 +03:00
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, name, (int)status, &lpFilename, (int)nSize);
|
2019-02-07 19:41:01 +03:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
return status;
|
2013-11-07 22:57:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
|
2014-08-18 19:22:22 +04:00
|
|
|
{
|
2013-11-12 02:25:59 +04:00
|
|
|
#if defined(__linux__)
|
2013-11-07 22:57:05 +04:00
|
|
|
int status;
|
2018-10-24 14:17:14 +03:00
|
|
|
size_t length;
|
2013-11-07 22:57:05 +04:00
|
|
|
char path[64];
|
|
|
|
|
|
|
|
if (!hModule)
|
|
|
|
{
|
|
|
|
char buffer[4096];
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(path, ARRAYSIZE(path), "/proc/%d/exe", getpid());
|
2013-11-07 22:57:05 +04:00
|
|
|
status = readlink(path, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
if (status < 0)
|
2016-06-10 14:10:39 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2013-11-07 22:57:05 +04:00
|
|
|
return 0;
|
2016-06-10 14:10:39 +03:00
|
|
|
}
|
2013-11-07 22:57:05 +04:00
|
|
|
|
|
|
|
buffer[status] = '\0';
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strnlen(buffer, sizeof(buffer));
|
2013-11-07 22:57:05 +04:00
|
|
|
|
|
|
|
if (length < nSize)
|
|
|
|
{
|
|
|
|
CopyMemory(lpFilename, buffer, length);
|
|
|
|
lpFilename[length] = '\0';
|
2016-06-10 14:10:39 +03:00
|
|
|
return length;
|
2013-11-07 22:57:05 +04:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
CopyMemory(lpFilename, buffer, nSize - 1);
|
|
|
|
lpFilename[nSize - 1] = '\0';
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
return nSize;
|
2013-11-12 02:25:59 +04:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
#elif defined(__MACOSX__)
|
|
|
|
int status;
|
2019-02-21 19:28:49 +03:00
|
|
|
size_t length;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
if (!hModule)
|
|
|
|
{
|
|
|
|
char path[4096];
|
|
|
|
char buffer[4096];
|
|
|
|
uint32_t size = sizeof(path);
|
|
|
|
status = _NSGetExecutablePath(path, &size);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
/* path too small */
|
2016-06-10 14:10:39 +03:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2013-11-12 02:25:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
/*
|
|
|
|
* _NSGetExecutablePath may not return the canonical path,
|
|
|
|
* so use realpath to find the absolute, canonical path.
|
|
|
|
*/
|
|
|
|
realpath(path, buffer);
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strnlen(buffer, sizeof(buffer));
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-11-12 02:25:59 +04:00
|
|
|
if (length < nSize)
|
|
|
|
{
|
|
|
|
CopyMemory(lpFilename, buffer, length);
|
|
|
|
lpFilename[length] = '\0';
|
2019-02-21 19:28:49 +03:00
|
|
|
return (DWORD)length;
|
2013-11-12 02:25:59 +04:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2016-06-10 14:10:39 +03:00
|
|
|
CopyMemory(lpFilename, buffer, nSize - 1);
|
|
|
|
lpFilename[nSize - 1] = '\0';
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
return nSize;
|
2013-11-07 22:57:05 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#endif
|
2016-06-10 14:10:39 +03:00
|
|
|
WLog_ERR(TAG, "%s is not implemented", __FUNCTION__);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2013-11-07 22:57:05 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
#endif
|