library.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Library Loader
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/crt.h>
  23. #include <winpr/platform.h>
  24. #include <winpr/library.h>
  25. #include "../log.h"
  26. #define TAG WINPR_TAG("library")
  27. /**
  28. * api-ms-win-core-libraryloader-l1-1-1.dll:
  29. *
  30. * AddDllDirectory
  31. * RemoveDllDirectory
  32. * SetDefaultDllDirectories
  33. * DisableThreadLibraryCalls
  34. * EnumResourceLanguagesExA
  35. * EnumResourceLanguagesExW
  36. * EnumResourceNamesExA
  37. * EnumResourceNamesExW
  38. * EnumResourceTypesExA
  39. * EnumResourceTypesExW
  40. * FindResourceExW
  41. * FindStringOrdinal
  42. * FreeLibrary
  43. * FreeLibraryAndExitThread
  44. * FreeResource
  45. * GetModuleFileNameA
  46. * GetModuleFileNameW
  47. * GetModuleHandleA
  48. * GetModuleHandleExA
  49. * GetModuleHandleExW
  50. * GetModuleHandleW
  51. * GetProcAddress
  52. * LoadLibraryExA
  53. * LoadLibraryExW
  54. * LoadResource
  55. * LoadStringA
  56. * LoadStringW
  57. * LockResource
  58. * QueryOptionalDelayLoadedAPI
  59. * SizeofResource
  60. */
  61. #if !defined(_WIN32) || defined(_UWP)
  62. #ifndef _WIN32
  63. #include <dlfcn.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <unistd.h>
  67. #include <sys/types.h>
  68. #include <sys/stat.h>
  69. #ifdef __MACOSX__
  70. #include <mach-o/dyld.h>
  71. #endif
  72. #endif
  73. DLL_DIRECTORY_COOKIE AddDllDirectory(PCWSTR NewDirectory)
  74. {
  75. /* TODO: Implement */
  76. WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
  77. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  78. return NULL;
  79. }
  80. BOOL RemoveDllDirectory(DLL_DIRECTORY_COOKIE Cookie)
  81. {
  82. /* TODO: Implement */
  83. WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
  84. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  85. return FALSE;
  86. }
  87. BOOL SetDefaultDllDirectories(DWORD DirectoryFlags)
  88. {
  89. /* TODO: Implement */
  90. WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
  91. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  92. return FALSE;
  93. }
  94. char* DLoadErrorString()
  95. {
  96. return dlerror();
  97. }
  98. HMODULE LoadLibraryA(LPCSTR lpLibFileName)
  99. {
  100. HMODULE library;
  101. library = dlopen(lpLibFileName, RTLD_LOCAL | RTLD_LAZY);
  102. if (!library) {
  103. //WLog_ERR(TAG, "%s failed with %s", __FUNCTION__, dlerror());
  104. return NULL;
  105. }
  106. return library;
  107. }
  108. HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
  109. {
  110. #if defined(_UWP)
  111. return LoadPackagedLibrary(lpLibFileName, 0);
  112. #else
  113. char* name = NULL;
  114. HMODULE module;
  115. int rc = ConvertFromUnicode(CP_UTF8, 0, lpLibFileName, -1, &name, 0, NULL, NULL);
  116. if (rc < 0)
  117. return NULL;
  118. module = LoadLibraryA(name);
  119. free(name);
  120. return module;
  121. #endif
  122. }
  123. HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
  124. {
  125. if (dwFlags != 0)
  126. WLog_WARN(TAG, "%s does not support dwFlags 0x%08" PRIx32, __FUNCTION__, dwFlags);
  127. if (hFile)
  128. WLog_WARN(TAG, "%s does not support hFile != NULL", __FUNCTION__);
  129. return LoadLibraryA(lpLibFileName);
  130. }
  131. HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
  132. {
  133. if (dwFlags != 0)
  134. WLog_WARN(TAG, "%s does not support dwFlags 0x%08" PRIx32, __FUNCTION__, dwFlags);
  135. if (hFile)
  136. WLog_WARN(TAG, "%s does not support hFile != NULL", __FUNCTION__);
  137. return LoadLibraryW(lpLibFileName);
  138. }
  139. BOOL DisableThreadLibraryCalls(HMODULE hLibModule)
  140. {
  141. return TRUE;
  142. }
  143. #endif
  144. #if !defined(_WIN32) && !defined(__CYGWIN__)
  145. FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  146. {
  147. FARPROC proc;
  148. proc = dlsym(hModule, lpProcName);
  149. if (proc == NULL)
  150. {
  151. WLog_ERR(TAG, "GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
  152. return (FARPROC)NULL;
  153. }
  154. return proc;
  155. }
  156. BOOL FreeLibrary(HMODULE hLibModule)
  157. {
  158. int status;
  159. status = dlclose(hLibModule);
  160. if (status != 0)
  161. return FALSE;
  162. return TRUE;
  163. }
  164. HMODULE GetModuleHandleA(LPCSTR lpModuleName)
  165. {
  166. /* TODO: Implement */
  167. WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
  168. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  169. return NULL;
  170. }
  171. HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
  172. {
  173. /* TODO: Implement */
  174. WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
  175. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  176. return NULL;
  177. }
  178. /**
  179. * GetModuleFileName:
  180. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
  181. *
  182. * Finding current executable's path without /proc/self/exe:
  183. * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
  184. */
  185. DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
  186. {
  187. DWORD status;
  188. char* name = calloc(nSize, sizeof(char));
  189. if (!name)
  190. {
  191. SetLastError(ERROR_INTERNAL_ERROR);
  192. return 0;
  193. }
  194. status = GetModuleFileNameA(hModule, name, nSize);
  195. if ((status > INT_MAX) || (nSize > INT_MAX))
  196. {
  197. SetLastError(ERROR_INTERNAL_ERROR);
  198. status = 0;
  199. }
  200. if (status > 0)
  201. {
  202. int rc = ConvertToUnicode(CP_UTF8, 0, name, (int)status, &lpFilename, (int)nSize);
  203. if (rc < 0)
  204. {
  205. free(name);
  206. SetLastError(ERROR_INTERNAL_ERROR);
  207. return 0;
  208. }
  209. }
  210. free(name);
  211. return status;
  212. }
  213. #define READLINK_LEN 9216
  214. DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
  215. {
  216. #if defined(__linux__)
  217. int status;
  218. size_t length;
  219. char path[64];
  220. if (!hModule) {
  221. char buffer[READLINK_LEN];
  222. sprintf_s(path, ARRAYSIZE(path), "/proc/%d/exe", getpid());
  223. status = readlink(path, buffer, sizeof(buffer));
  224. if (status < 0) {
  225. SetLastError(ERROR_INTERNAL_ERROR);
  226. return 0;
  227. }
  228. buffer[status] = '\0';
  229. length = strnlen(buffer, sizeof(buffer));
  230. if (length < nSize) {
  231. CopyMemory(lpFilename, buffer, length);
  232. lpFilename[length] = '\0';
  233. return length;
  234. }
  235. CopyMemory(lpFilename, buffer, nSize - 1);
  236. lpFilename[nSize - 1] = '\0';
  237. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  238. return nSize;
  239. }
  240. #elif defined(__MACOSX__)
  241. int status;
  242. size_t length;
  243. if (!hModule)
  244. {
  245. char path[4096];
  246. char buffer[4096];
  247. uint32_t size = sizeof(path);
  248. status = _NSGetExecutablePath(path, &size);
  249. if (status != 0)
  250. {
  251. /* path too small */
  252. SetLastError(ERROR_INTERNAL_ERROR);
  253. return 0;
  254. }
  255. /*
  256. * _NSGetExecutablePath may not return the canonical path,
  257. * so use realpath to find the absolute, canonical path.
  258. */
  259. realpath(path, buffer);
  260. length = strnlen(buffer, sizeof(buffer));
  261. if (length < nSize)
  262. {
  263. CopyMemory(lpFilename, buffer, length);
  264. lpFilename[length] = '\0';
  265. return (DWORD)length;
  266. }
  267. CopyMemory(lpFilename, buffer, nSize - 1);
  268. lpFilename[nSize - 1] = '\0';
  269. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  270. return nSize;
  271. }
  272. #endif
  273. WLog_ERR(TAG, "%s is not implemented", __FUNCTION__);
  274. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  275. return 0;
  276. }
  277. #endif