|
1 | 1 | #include "fastfetch.h" |
2 | 2 | #include "common/library.h" |
3 | 3 |
|
| 4 | +#if _WIN32 |
| 5 | +#include "common/debug.h" |
| 6 | +#include "common/windows/nt.h" |
| 7 | +#include <errno.h> |
| 8 | +#include <ntstatus.h> |
| 9 | +#endif |
| 10 | + |
4 | 11 | #ifndef FF_DISABLE_DLOPEN |
5 | 12 |
|
6 | 13 | #include <stdarg.h> |
@@ -36,7 +43,7 @@ static void* libraryLoad(const char* path, int maxVersion) |
36 | 43 | if (pathLen == instance.state.platform.exePath.length) |
37 | 44 | return result; |
38 | 45 |
|
39 | | - char absPath[MAX_PATH + 1]; |
| 46 | + char absPath[MAX_PATH * 2]; |
40 | 47 | strcpy(mempcpy(absPath, instance.state.platform.exePath.chars, pathLen + 1), path); |
41 | 48 | return dlopen(absPath, FF_DLOPEN_FLAGS); |
42 | 49 |
|
@@ -92,3 +99,81 @@ void* ffLibraryLoad(const char* path, int maxVersion, ...) |
92 | 99 | } |
93 | 100 |
|
94 | 101 | #endif |
| 102 | + |
| 103 | +#if _WIN32 |
| 104 | + |
| 105 | +void* dlopen(const char* path, FF_MAYBE_UNUSED int mode) |
| 106 | +{ |
| 107 | + wchar_t pathW[MAX_PATH + 1]; |
| 108 | + ULONG pathWBytes = 0; |
| 109 | + |
| 110 | + NTSTATUS status = RtlUTF8ToUnicodeN(pathW, sizeof(pathW), &pathWBytes, path, (uint32_t)strlen(path) + 1); |
| 111 | + if (!NT_SUCCESS(status)) |
| 112 | + { |
| 113 | + FF_DEBUG("RtlUTF8ToUnicodeN failed for path %s with status 0x%08lX: %s", path, status, ffDebugNtStatus(status)); |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + |
| 117 | + PVOID module = NULL; |
| 118 | + status = LdrLoadDll(NULL, NULL, &(UNICODE_STRING) { |
| 119 | + .Length = (USHORT) pathWBytes - sizeof(wchar_t), // Exclude null terminator |
| 120 | + .MaximumLength = (USHORT) pathWBytes, |
| 121 | + .Buffer = pathW, |
| 122 | + }, &module); |
| 123 | + |
| 124 | + if (!NT_SUCCESS(status)) |
| 125 | + { |
| 126 | + FF_DEBUG("LdrLoadDll failed for path %s with status 0x%08lX: %s", path, status, ffDebugNtStatus(status)); |
| 127 | + return NULL; |
| 128 | + } |
| 129 | + |
| 130 | + return module; |
| 131 | +} |
| 132 | + |
| 133 | +int dlclose(void* handle) |
| 134 | +{ |
| 135 | + NTSTATUS status = LdrUnloadDll(handle); |
| 136 | + if (!NT_SUCCESS(status)) |
| 137 | + { |
| 138 | + FF_DEBUG("LdrUnloadDll failed for handle %p with status 0x%08lX: %s", handle, status, ffDebugNtStatus(status)); |
| 139 | + return -1; |
| 140 | + } |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +void* dlsym(void* handle, const char* symbol) |
| 145 | +{ |
| 146 | + void* address; |
| 147 | + USHORT symbolBytes = (USHORT) strlen(symbol) + 1; |
| 148 | + NTSTATUS status = LdrGetProcedureAddress(handle, &(ANSI_STRING) { |
| 149 | + .Length = symbolBytes - sizeof(char), |
| 150 | + .MaximumLength = symbolBytes, |
| 151 | + .Buffer = (char*) symbol, |
| 152 | + }, 0, &address); |
| 153 | + if (!NT_SUCCESS(status)) |
| 154 | + { |
| 155 | + FF_DEBUG("LdrGetProcedureAddress failed for symbol %s with status 0x%08lX: %s", symbol, status, ffDebugNtStatus(status)); |
| 156 | + return NULL; |
| 157 | + } |
| 158 | + return address; |
| 159 | +} |
| 160 | + |
| 161 | +void* ffLibraryGetModule(const wchar_t* libraryFileName) |
| 162 | +{ |
| 163 | + assert(libraryFileName != NULL && "Use \"ffGetPeb()->ImageBaseAddress\" instead"); |
| 164 | + |
| 165 | + void* module = NULL; |
| 166 | + USHORT libraryFileNameBytes = (USHORT) (wcslen(libraryFileName) * sizeof(wchar_t)) + sizeof(wchar_t); |
| 167 | + NTSTATUS status = LdrGetDllHandle(NULL, NULL, &(UNICODE_STRING) { |
| 168 | + .Length = libraryFileNameBytes - sizeof(wchar_t), |
| 169 | + .MaximumLength = libraryFileNameBytes, |
| 170 | + .Buffer = (wchar_t*) libraryFileName, |
| 171 | + }, &module); |
| 172 | + if (!NT_SUCCESS(status)) |
| 173 | + { |
| 174 | + FF_DEBUG("LdrGetDllHandle failed for library %ls with status 0x%08lX: %s", libraryFileName, status, ffDebugNtStatus(status)); |
| 175 | + return NULL; |
| 176 | + } |
| 177 | + return module; |
| 178 | +} |
| 179 | +#endif |
0 commit comments