Coverage Report

Created: 2023-09-25 06:56

/src/FreeRDP/winpr/libwinpr/library/library.c
Line
Count
Source (jump to first uncovered line)
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
20
#include <winpr/config.h>
21
22
#include <winpr/crt.h>
23
#include <winpr/platform.h>
24
25
#include <winpr/library.h>
26
27
#include "../log.h"
28
#define TAG WINPR_TAG("library")
29
30
/**
31
 * api-ms-win-core-libraryloader-l1-1-1.dll:
32
 *
33
 * AddDllDirectory
34
 * RemoveDllDirectory
35
 * SetDefaultDllDirectories
36
 * DisableThreadLibraryCalls
37
 * EnumResourceLanguagesExA
38
 * EnumResourceLanguagesExW
39
 * EnumResourceNamesExA
40
 * EnumResourceNamesExW
41
 * EnumResourceTypesExA
42
 * EnumResourceTypesExW
43
 * FindResourceExW
44
 * FindStringOrdinal
45
 * FreeLibrary
46
 * FreeLibraryAndExitThread
47
 * FreeResource
48
 * GetModuleFileNameA
49
 * GetModuleFileNameW
50
 * GetModuleHandleA
51
 * GetModuleHandleExA
52
 * GetModuleHandleExW
53
 * GetModuleHandleW
54
 * GetProcAddress
55
 * LoadLibraryExA
56
 * LoadLibraryExW
57
 * LoadResource
58
 * LoadStringA
59
 * LoadStringW
60
 * LockResource
61
 * QueryOptionalDelayLoadedAPI
62
 * SizeofResource
63
 */
64
65
#if !defined(_WIN32) || defined(_UWP)
66
67
#ifndef _WIN32
68
69
#include <dlfcn.h>
70
#include <stdio.h>
71
#include <stdlib.h>
72
#include <unistd.h>
73
#include <sys/types.h>
74
#include <sys/stat.h>
75
76
#ifdef __MACOSX__
77
#include <mach-o/dyld.h>
78
#endif
79
80
#endif
81
82
DLL_DIRECTORY_COOKIE AddDllDirectory(PCWSTR NewDirectory)
83
0
{
84
  /* TODO: Implement */
85
0
  WLog_ERR(TAG, "not implemented");
86
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
87
0
  return NULL;
88
0
}
89
90
BOOL RemoveDllDirectory(DLL_DIRECTORY_COOKIE Cookie)
91
0
{
92
  /* TODO: Implement */
93
0
  WLog_ERR(TAG, "not implemented");
94
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
95
0
  return FALSE;
96
0
}
97
98
BOOL SetDefaultDllDirectories(DWORD DirectoryFlags)
99
0
{
100
  /* TODO: Implement */
101
0
  WLog_ERR(TAG, "not implemented");
102
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
103
0
  return FALSE;
104
0
}
105
106
HMODULE LoadLibraryA(LPCSTR lpLibFileName)
107
0
{
108
#if defined(_UWP)
109
  int status;
110
  HMODULE hModule = NULL;
111
  WCHAR* filenameW = NULL;
112
113
  if (!lpLibFileName)
114
    return NULL;
115
116
  filenameW = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
117
  if (filenameW)
118
    return NULL;
119
120
  hModule = LoadLibraryW(filenameW);
121
  free(filenameW);
122
  return hModule;
123
#else
124
0
  HMODULE library;
125
0
  library = dlopen(lpLibFileName, RTLD_LOCAL | RTLD_LAZY);
126
127
0
  if (!library)
128
0
  {
129
0
    const char* err = dlerror();
130
0
    WLog_ERR(TAG, "failed with %s", err);
131
0
    return NULL;
132
0
  }
133
134
0
  return library;
135
0
#endif
136
0
}
137
138
HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
139
0
{
140
0
  if (!lpLibFileName)
141
0
    return NULL;
142
#if defined(_UWP)
143
  return LoadPackagedLibrary(lpLibFileName, 0);
144
#else
145
0
  HMODULE module;
146
0
  char* name = ConvertWCharToUtf8Alloc(lpLibFileName, NULL);
147
0
  if (!name)
148
0
    return NULL;
149
150
0
  module = LoadLibraryA(name);
151
0
  free(name);
152
0
  return module;
153
0
#endif
154
0
}
155
156
HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
157
0
{
158
0
  if (dwFlags != 0)
159
0
    WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
160
161
0
  if (hFile)
162
0
    WLog_WARN(TAG, "does not support hFile != NULL");
163
164
0
  return LoadLibraryA(lpLibFileName);
165
0
}
166
167
HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
168
0
{
169
0
  if (dwFlags != 0)
170
0
    WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
171
172
0
  if (hFile)
173
0
    WLog_WARN(TAG, "does not support hFile != NULL");
174
175
0
  return LoadLibraryW(lpLibFileName);
176
0
}
177
178
#endif
179
180
#if !defined(_WIN32) && !defined(__CYGWIN__)
181
182
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
183
0
{
184
0
  FARPROC proc;
185
0
  proc = dlsym(hModule, lpProcName);
186
187
0
  if (proc == NULL)
188
0
  {
189
0
    WLog_ERR(TAG, "GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
190
0
    return (FARPROC)NULL;
191
0
  }
192
193
0
  return proc;
194
0
}
195
196
BOOL FreeLibrary(HMODULE hLibModule)
197
0
{
198
0
  int status;
199
0
  status = dlclose(hLibModule);
200
201
0
  if (status != 0)
202
0
    return FALSE;
203
204
0
  return TRUE;
205
0
}
206
207
HMODULE GetModuleHandleA(LPCSTR lpModuleName)
208
0
{
209
  /* TODO: Implement */
210
0
  WLog_ERR(TAG, "not implemented");
211
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
212
0
  return NULL;
213
0
}
214
215
HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
216
0
{
217
  /* TODO: Implement */
218
0
  WLog_ERR(TAG, "not implemented");
219
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
220
0
  return NULL;
221
0
}
222
223
/**
224
 * GetModuleFileName:
225
 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
226
 *
227
 * Finding current executable's path without /proc/self/exe:
228
 * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
229
 */
230
231
DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
232
0
{
233
0
  DWORD status;
234
0
  if (!lpFilename)
235
0
  {
236
0
    SetLastError(ERROR_INTERNAL_ERROR);
237
0
    return 0;
238
0
  }
239
240
0
  char* name = calloc(nSize, sizeof(char));
241
0
  if (!name)
242
0
  {
243
0
    SetLastError(ERROR_INTERNAL_ERROR);
244
0
    return 0;
245
0
  }
246
0
  status = GetModuleFileNameA(hModule, name, nSize);
247
248
0
  if ((status > INT_MAX) || (nSize > INT_MAX))
249
0
  {
250
0
    SetLastError(ERROR_INTERNAL_ERROR);
251
0
    status = 0;
252
0
  }
253
254
0
  if (status > 0)
255
0
  {
256
0
    if (ConvertUtf8NToWChar(name, status, lpFilename, nSize) < 0)
257
0
    {
258
0
      free(name);
259
0
      SetLastError(ERROR_INTERNAL_ERROR);
260
0
      return 0;
261
0
    }
262
0
  }
263
264
0
  free(name);
265
0
  return status;
266
0
}
267
268
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
269
0
{
270
0
#if defined(__linux__)
271
0
  SSIZE_T status;
272
0
  size_t length;
273
0
  char path[64];
274
275
0
  if (!hModule)
276
0
  {
277
0
    char buffer[4096];
278
0
    sprintf_s(path, ARRAYSIZE(path), "/proc/%d/exe", getpid());
279
0
    status = readlink(path, buffer, sizeof(buffer));
280
281
0
    if (status < 0)
282
0
    {
283
0
      SetLastError(ERROR_INTERNAL_ERROR);
284
0
      return 0;
285
0
    }
286
287
0
    buffer[status] = '\0';
288
0
    length = strnlen(buffer, sizeof(buffer));
289
290
0
    if (length < nSize)
291
0
    {
292
0
      CopyMemory(lpFilename, buffer, length);
293
0
      lpFilename[length] = '\0';
294
0
      return (DWORD)length;
295
0
    }
296
297
0
    CopyMemory(lpFilename, buffer, nSize - 1);
298
0
    lpFilename[nSize - 1] = '\0';
299
0
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
300
0
    return nSize;
301
0
  }
302
303
#elif defined(__MACOSX__)
304
  int status;
305
  size_t length;
306
307
  if (!hModule)
308
  {
309
    char path[4096];
310
    char buffer[4096];
311
    uint32_t size = sizeof(path);
312
    status = _NSGetExecutablePath(path, &size);
313
314
    if (status != 0)
315
    {
316
      /* path too small */
317
      SetLastError(ERROR_INTERNAL_ERROR);
318
      return 0;
319
    }
320
321
    /*
322
     * _NSGetExecutablePath may not return the canonical path,
323
     * so use realpath to find the absolute, canonical path.
324
     */
325
    realpath(path, buffer);
326
    length = strnlen(buffer, sizeof(buffer));
327
328
    if (length < nSize)
329
    {
330
      CopyMemory(lpFilename, buffer, length);
331
      lpFilename[length] = '\0';
332
      return (DWORD)length;
333
    }
334
335
    CopyMemory(lpFilename, buffer, nSize - 1);
336
    lpFilename[nSize - 1] = '\0';
337
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
338
    return nSize;
339
  }
340
341
#endif
342
0
  WLog_ERR(TAG, "is not implemented");
343
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
344
0
  return 0;
345
0
}
346
347
#endif
348
349
HMODULE LoadLibraryX(LPCSTR lpLibFileName)
350
0
{
351
0
  if (!lpLibFileName)
352
0
    return NULL;
353
354
#if defined(_WIN32)
355
  HMODULE hm = NULL;
356
  WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
357
358
  if (wstr)
359
    hm = LoadLibraryW(wstr);
360
  free(wstr);
361
  return hm;
362
#else
363
0
  return LoadLibraryA(lpLibFileName);
364
0
#endif
365
0
}
366
367
HMODULE LoadLibraryExX(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
368
0
{
369
0
  if (!lpLibFileName)
370
0
    return NULL;
371
#if defined(_WIN32)
372
  HMODULE hm = NULL;
373
  WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
374
  if (wstr)
375
    hm = LoadLibraryExW(wstr, hFile, dwFlags);
376
  free(wstr);
377
  return hm;
378
#else
379
0
  return LoadLibraryExA(lpLibFileName, hFile, dwFlags);
380
0
#endif
381
0
}