Initial commit
This commit is contained in:
+296
@@ -0,0 +1,296 @@
|
||||
#pragma once
|
||||
|
||||
namespace util
|
||||
{
|
||||
inline unsigned int mmh_32(const void* key, int len, unsigned int seed)
|
||||
{
|
||||
const unsigned int m = 0x5bd1e995;
|
||||
const int r = 24;
|
||||
|
||||
unsigned int h = seed ^ len;
|
||||
|
||||
const unsigned char* data = (const unsigned char*)key;
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
unsigned int k = *(unsigned int*)data;
|
||||
|
||||
k *= m;
|
||||
k ^= k >> r;
|
||||
k *= m;
|
||||
|
||||
h *= m;
|
||||
h ^= k;
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
switch (len)
|
||||
{
|
||||
case 3: h ^= data[2] << 16;
|
||||
case 2: h ^= data[1] << 8;
|
||||
case 1: h ^= data[0];
|
||||
h *= m;
|
||||
};
|
||||
|
||||
h ^= h >> 13;
|
||||
h *= m;
|
||||
h ^= h >> 15;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
inline int string_hash(const char* text, size_t length)
|
||||
{
|
||||
return mmh_32((void*)text, static_cast<int>(length), 6);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
inline auto find_export(const char* mod, const char* proc)
|
||||
{
|
||||
auto handle = LI_FN(GetModuleHandleA)(mod);
|
||||
return (T)LI_FN(GetProcAddress)(handle, proc);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
inline auto find_export(_hmodule mod, const char* proc)
|
||||
{
|
||||
return (T)LI_FN(GetProcAddress)(mod, proc);
|
||||
}
|
||||
|
||||
__forceinline void* get_vtable_func(void* class_base, size_t index)
|
||||
{
|
||||
if (!class_base || index < 0)
|
||||
return nullptr;
|
||||
|
||||
auto** const class_vtable = *(void***)class_base;
|
||||
|
||||
if (class_vtable == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return class_vtable[index];
|
||||
}
|
||||
|
||||
__forceinline auto protect_virtual_memory(void* address, size_t length, u_long prot, u_long* prot_old = nullptr)
|
||||
{
|
||||
u_long unused_0{};
|
||||
u_long* prot_old_act = (!(prot_old == nullptr) ? prot_old : &unused_0);
|
||||
|
||||
return win_api::virtual_protect(address, length, prot, prot_old_act);
|
||||
}
|
||||
|
||||
__forceinline auto file_header(void* pc) -> void*
|
||||
{
|
||||
void* result = nullptr;
|
||||
|
||||
if (!win_api::pc_to_file_header(pc, &result))
|
||||
return nullptr;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
__forceinline void format_string(char* buffer, size_t length, const char* fmt, T&&... args)
|
||||
{
|
||||
LI_FN(wsprintfA).cached()(buffer, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
__forceinline void format_wstring(wchar_t* buffer, size_t length, const wchar_t* fmt, T&&... args)
|
||||
{
|
||||
LI_FN(wsprintfW).cached()(buffer, fmt, args...);
|
||||
}
|
||||
|
||||
__forceinline bool is_valid_address(void* p)
|
||||
{
|
||||
auto region_info = MEMORY_BASIC_INFORMATION{};
|
||||
|
||||
if (!win_api::rebuilt::virtual_query(p, ®ion_info, sizeof(region_info)))
|
||||
return false;
|
||||
|
||||
if (region_info.Protect == PAGE_GUARD)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool read_memory(void* dst, uintptr_t src, size_t len)
|
||||
{
|
||||
size_t processed = 0;
|
||||
|
||||
const auto result = win_api::read_process_memory(
|
||||
win_api::get_current_process(),
|
||||
(LPCVOID)src,
|
||||
dst,
|
||||
len,
|
||||
&processed
|
||||
);
|
||||
|
||||
if (!result && win_api::get_last_error() != ERROR_PARTIAL_COPY)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool write_data(const char* data, const wchar_t* filename)
|
||||
{
|
||||
HANDLE hFile;
|
||||
SIZE_T dwBytesToWrite = stl::strlen(data);
|
||||
DWORD dwBytesWritten;
|
||||
BOOL bErrorFlag = FALSE;
|
||||
|
||||
hFile = LI_FN(CreateFileW).cached()(filename, // name of the write
|
||||
FILE_APPEND_DATA, // open for appending
|
||||
FILE_SHARE_READ, // share for reading only
|
||||
NULL, // default security
|
||||
OPEN_ALWAYS, // open existing file or create new file
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
NULL); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
|
||||
while (dwBytesToWrite > 0)
|
||||
{
|
||||
bErrorFlag = LI_FN(WriteFile).cached()(
|
||||
hFile, // open file handle
|
||||
data, // start of data to write
|
||||
dwBytesToWrite, // number of bytes to write
|
||||
&dwBytesWritten, // number of bytes that were written
|
||||
NULL); // no overlapped structure
|
||||
|
||||
if (!bErrorFlag)
|
||||
break;
|
||||
|
||||
data += dwBytesWritten;
|
||||
dwBytesToWrite -= dwBytesWritten;
|
||||
}
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool write_data_reg(const char* data, size_t length, const char* filename)
|
||||
{
|
||||
HANDLE hFile;
|
||||
SIZE_T dwBytesToWrite = length;
|
||||
DWORD dwBytesWritten;
|
||||
BOOL bErrorFlag = FALSE;
|
||||
|
||||
hFile = LI_FN(CreateFileA).cached()(filename, // name of the write
|
||||
FILE_APPEND_DATA, // open for appending
|
||||
FILE_SHARE_READ, // share for reading only
|
||||
NULL, // default security
|
||||
OPEN_ALWAYS, // open existing file or create new file
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
NULL); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
|
||||
while (dwBytesToWrite > 0)
|
||||
{
|
||||
bErrorFlag = LI_FN(WriteFile).cached()(
|
||||
hFile, // open file handle
|
||||
data, // start of data to write
|
||||
dwBytesToWrite, // number of bytes to write
|
||||
&dwBytesWritten, // number of bytes that were written
|
||||
NULL); // no overlapped structure
|
||||
|
||||
if (!bErrorFlag)
|
||||
break;
|
||||
|
||||
data += dwBytesWritten;
|
||||
dwBytesToWrite -= dwBytesWritten;
|
||||
}
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline BOOL file_exists(LPCSTR szPath)
|
||||
{
|
||||
DWORD dwAttrib = LI_FN(GetFileAttributesA).cached()(szPath);
|
||||
|
||||
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
|
||||
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
||||
}
|
||||
|
||||
inline BOOL file_exists_wide(LPCWSTR szPath)
|
||||
{
|
||||
DWORD dwAttrib = LI_FN(GetFileAttributesW).cached()(szPath);
|
||||
|
||||
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
|
||||
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
||||
}
|
||||
|
||||
inline BOOL read_file(LPCSTR pszPath, PVOID pBuffer, DWORD uLength)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileA).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
BOOL bResult = LI_FN(ReadFile).cached()(hFile, pBuffer, uLength, &uLength, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
if (bResult)
|
||||
{
|
||||
((PCHAR)pBuffer)[uLength] = '\0';
|
||||
}
|
||||
|
||||
return bResult;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
extern BOOL read_file_ex(LPCWSTR pszPath, PVOID pBuffer, DWORD uLength);
|
||||
extern PVOID get_file_ptr(LPCWSTR pszPath);
|
||||
|
||||
inline DWORD get_file_size(LPCSTR pszPath)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileA).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
DWORD dwResult = LI_FN(GetFileSize).cached()(hFile, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline DWORD get_file_size_wide(LPCWSTR pszPath)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileW).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
DWORD dwResult = LI_FN(GetFileSize).cached()(hFile, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline bool get_desktop_directory(char* buffer, size_t length)
|
||||
{
|
||||
if (!buffer || !length)
|
||||
return false;
|
||||
|
||||
if (!LI_FN(SHGetSpecialFolderPathA).cached()(HWND_DESKTOP, buffer, CSIDL_DESKTOP, FALSE))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user