diff --git a/include/Mods/LoadLibraryUnix.hpp b/include/Mods/LoadLibraryUnix.hpp index 5d762b2..a77690b 100644 --- a/include/Mods/LoadLibraryUnix.hpp +++ b/include/Mods/LoadLibraryUnix.hpp @@ -1,9 +1,11 @@ #pragma once #if defined(unix) #include +namespace vl::mod { + void* LoadLibrary(const std::string& path, bool isGlobal); + bool CloseLibrary(void* handle); + void* LoadSymbol(void* handle, const std::string& name); -void* LoadLibrary(const std::string& path, bool isGlobal); -bool CloseLibrary(void* handle); -void* LoadSymbol(void* handle, const std::string& name); +} #endif \ No newline at end of file diff --git a/include/Mods/LoadLibraryWin.hpp b/include/Mods/LoadLibraryWin.hpp index d74bb72..d89023f 100644 --- a/include/Mods/LoadLibraryWin.hpp +++ b/include/Mods/LoadLibraryWin.hpp @@ -2,8 +2,9 @@ #if defined(_WIN32) #include - -void* LoadLibrary(const std::string& path, bool isGlobal); -bool CloseLibrary(void* handle); -void* LoadSymbol(void* handle, const std::string& name); +namespace vl::mod { + void* LoadLibrary(const std::string& path, bool isGlobal); + bool CloseLibrary(void* handle); + void* LoadSymbol(void* handle, const std::string& name); +} #endif \ No newline at end of file diff --git a/src/Agent.cpp b/src/Agent.cpp index e95f77d..15fe30f 100644 --- a/src/Agent.cpp +++ b/src/Agent.cpp @@ -35,7 +35,7 @@ namespace vl::Agent { return; } - LoadLibrary(mods.at("VoidLoaderApi.vlmod").string(), true); + vl::mod::LoadLibrary(mods.at("VoidLoaderApi.vlmod").string(), true); mods.erase("VoidLoaderApi.vlmod"); mods.erase("VoidLoader.vlmod"); diff --git a/src/Mods/LoadLibraryUnix.cpp b/src/Mods/LoadLibraryUnix.cpp index 03db8ce..74b2732 100644 --- a/src/Mods/LoadLibraryUnix.cpp +++ b/src/Mods/LoadLibraryUnix.cpp @@ -2,19 +2,20 @@ #include #include - -void* LoadLibrary(const std::string& path, bool isGlobal) -{ - dlerror(); - return dlopen(path.c_str(), isGlobal ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW); -} -bool CloseLibrary(void* handle) -{ - return dlclose(handle) == 0; -} -void* LoadSymbol(void* handle, const std::string& name) -{ - dlerror(); - return dlsym(handle, name.c_str()); +namespace vl::mod { + void* LoadLibrary(const std::string& path, bool isGlobal) + { + dlerror(); + return dlopen(path.c_str(), isGlobal ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW); + } + bool CloseLibrary(void* handle) + { + return dlclose(handle) == 0; + } + void* LoadSymbol(void* handle, const std::string& name) + { + dlerror(); + return dlsym(handle, name.c_str()); + } } #endif \ No newline at end of file diff --git a/src/Mods/LoadLibraryWin.cpp b/src/Mods/LoadLibraryWin.cpp index 911a066..eed930b 100644 --- a/src/Mods/LoadLibraryWin.cpp +++ b/src/Mods/LoadLibraryWin.cpp @@ -4,111 +4,113 @@ #include #include #include -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) -static int32_t __wtf8_decode1(const char** input) { - uint32_t code_point; - uint8_t b1; - uint8_t b2; - uint8_t b3; - uint8_t b4; - b1 = **input; - if (b1 <= 0x7F) - return b1; /* ASCII code point */ - if (b1 < 0xC2) - return -1; /* invalid: continuation byte */ - code_point = b1; +namespace vl::mod { + #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + static int32_t __wtf8_decode1(const char** input) { + uint32_t code_point; + uint8_t b1; + uint8_t b2; + uint8_t b3; + uint8_t b4; - b2 = *++*input; - if ((b2 & 0xC0) != 0x80) - return -1; /* invalid: not a continuation byte */ - code_point = (code_point << 6) | (b2 & 0x3F); - if (b1 <= 0xDF) - return 0x7FF & code_point; /* two-byte character */ + b1 = **input; + if (b1 <= 0x7F) + return b1; /* ASCII code point */ + if (b1 < 0xC2) + return -1; /* invalid: continuation byte */ + code_point = b1; - b3 = *++*input; - if ((b3 & 0xC0) != 0x80) - return -1; /* invalid: not a continuation byte */ - code_point = (code_point << 6) | (b3 & 0x3F); - if (b1 <= 0xEF) - return 0xFFFF & code_point; /* three-byte character */ + b2 = *++*input; + if ((b2 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b2 & 0x3F); + if (b1 <= 0xDF) + return 0x7FF & code_point; /* two-byte character */ - b4 = *++*input; - if ((b4 & 0xC0) != 0x80) - return -1; /* invalid: not a continuation byte */ - code_point = (code_point << 6) | (b4 & 0x3F); - if (b1 <= 0xF4) { - code_point &= 0x1FFFFF; - if (code_point <= 0x10FFFF) - return code_point; /* four-byte character */ + b3 = *++*input; + if ((b3 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b3 & 0x3F); + if (b1 <= 0xEF) + return 0xFFFF & code_point; /* three-byte character */ + + b4 = *++*input; + if ((b4 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b4 & 0x3F); + if (b1 <= 0xF4) { + code_point &= 0x1FFFFF; + if (code_point <= 0x10FFFF) + return code_point; /* four-byte character */ + } + + /* code point too large */ + return -1; } - /* code point too large */ - return -1; -} + ssize_t wtf8_length_as_utf16(const char* source_ptr) { + size_t w_target_len = 0; + int32_t code_point; -ssize_t wtf8_length_as_utf16(const char* source_ptr) { - size_t w_target_len = 0; - int32_t code_point; - - do { - code_point = __wtf8_decode1(&source_ptr); - if (code_point < 0) - return -1; - if (code_point > 0xFFFF) + do { + code_point = __wtf8_decode1(&source_ptr); + if (code_point < 0) + return -1; + if (code_point > 0xFFFF) + w_target_len++; w_target_len++; - w_target_len++; - } while (*source_ptr++); + } while (*source_ptr++); - return w_target_len; + return w_target_len; + } + + void wtf8_to_utf16(const char* source_ptr, + WCHAR* w_target, + size_t w_target_len) { + int32_t code_point; + + do { + code_point = __wtf8_decode1(&source_ptr); + /* uv_wtf8_length_as_utf16 should have been called and checked first. */ + assert(code_point >= 0); + if (code_point > 0xFFFF) { + assert(code_point < 0x10FFFF); + *w_target++ = (((code_point - 0x10000) >> 10) + 0xD800); + *w_target++ = ((code_point - 0x10000) & 0x3FF) + 0xDC00; + w_target_len -= 2; + } else { + *w_target++ = code_point; + w_target_len -= 1; + } + } while (*source_ptr++); + + (void)w_target_len; + assert(w_target_len == 0); + } + + void* LoadLibrary(const std::string& path, bool isGlobal) + { + WCHAR filename_w[32768]; + ssize_t r; + void* handle = nullptr; + + r = wtf8_length_as_utf16(path.c_str()); + if (r < 0) + return nullptr; + if ((size_t) r > ARRAY_SIZE(filename_w)) + return nullptr; + wtf8_to_utf16(path.c_str(), filename_w, r); + + return LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + } + bool CloseLibrary(void* handle) + { + return FreeLibrary(static_cast(handle)) != 0; + } + void* LoadSymbol(void* handle, const std::string& name) + { + return (void*)(uintptr_t) GetProcAddress(static_cast(handle), name.c_str()); + } } - -void wtf8_to_utf16(const char* source_ptr, - WCHAR* w_target, - size_t w_target_len) { - int32_t code_point; - - do { - code_point = __wtf8_decode1(&source_ptr); - /* uv_wtf8_length_as_utf16 should have been called and checked first. */ - assert(code_point >= 0); - if (code_point > 0xFFFF) { - assert(code_point < 0x10FFFF); - *w_target++ = (((code_point - 0x10000) >> 10) + 0xD800); - *w_target++ = ((code_point - 0x10000) & 0x3FF) + 0xDC00; - w_target_len -= 2; - } else { - *w_target++ = code_point; - w_target_len -= 1; - } - } while (*source_ptr++); - - (void)w_target_len; - assert(w_target_len == 0); -} - -void* LoadLibrary(const std::string& path, bool isGlobal) -{ - WCHAR filename_w[32768]; - ssize_t r; - void* handle = nullptr; - - r = wtf8_length_as_utf16(path.c_str()); - if (r < 0) - return nullptr; - if ((size_t) r > ARRAY_SIZE(filename_w)) - return nullptr; - wtf8_to_utf16(path.c_str(), filename_w, r); - - return LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); -} -bool CloseLibrary(void* handle) -{ - return FreeLibrary(static_cast(handle)) != 0; -} -void* LoadSymbol(void* handle, const std::string& name) -{ - return (void*)(uintptr_t) GetProcAddress(static_cast(handle), name.c_str()); -} - #endif \ No newline at end of file