diff --git a/Detours.cpp b/Detours.cpp index 39c0f82..3dd882d 100644 --- a/Detours.cpp +++ b/Detours.cpp @@ -127,9 +127,14 @@ namespace Detours { // Namespaces // ---------------------------------------------------------------- + using namespace LDR; using namespace Codec; using namespace Hexadecimal; using namespace Scan; + using namespace RTTI; + using namespace Sync; + using namespace Pipe; + using namespace Parallel; using namespace Memory; using namespace Exception; using namespace rddisasm; @@ -174,6 +179,447 @@ namespace Detours { #endif } + // ---------------------------------------------------------------- + // LDR + // ---------------------------------------------------------------- + + namespace LDR { + + // ---------------------------------------------------------------- + // List Entry APIs + // ---------------------------------------------------------------- + + void InitializeListHead(PLIST_ENTRY pListHead) { + if (pListHead) { + pListHead->Flink = pListHead->Blink = pListHead; + } + } + + void InsertEntry(PLIST_ENTRY pPrev, PLIST_ENTRY pNext, PLIST_ENTRY pEntry) { + if (pPrev && pNext && pEntry) { + pEntry->Flink = pNext; + pEntry->Blink = pPrev; + + if (pPrev->Flink) { + pPrev->Flink->Blink = pEntry; + } + + if (pNext->Blink) { + pNext->Blink->Flink = pEntry; + } + + pPrev->Flink = pEntry; + pNext->Blink = pEntry; + } + } + + void InsertHeadList(PLIST_ENTRY pListHead, PLIST_ENTRY pEntry) { + if (pListHead && pEntry) { + InsertEntry(pListHead, pListHead->Flink, pEntry); + } + } + + void InsertTailList(PLIST_ENTRY pListHead, PLIST_ENTRY pEntry) { + if (pListHead && pEntry) { + InsertEntry(pListHead->Blink, pListHead, pEntry); + } + } + + void RemoveEntryList(PLIST_ENTRY pEntry) { + if (pEntry) { + PLIST_ENTRY pPrev = pEntry->Blink; + PLIST_ENTRY pNext = pEntry->Flink; + + if (pPrev->Flink) { + pPrev->Flink = pNext; + } + + if (pNext->Blink) { + pNext->Blink = pPrev; + } + } + } + + void RemoveHeadList(PLIST_ENTRY pListHead) { + if (pListHead && pListHead->Flink) { + RemoveEntryList(pListHead->Flink); + } + } + + void RemoveTailList(PLIST_ENTRY pListHead) { + if (pListHead && pListHead->Blink) { + RemoveEntryList(pListHead->Blink); + } + } + + PLIST_ENTRY GetListHeadFromEntry(PLIST_ENTRY pEntry) { + if (!pEntry) { + return nullptr; + } + + PLIST_ENTRY pHead = pEntry; + + while ((pHead->Blink != nullptr) && (pHead->Blink != pEntry)) { + pHead = pHead->Blink; + } + + return pEntry; + } + + // ---------------------------------------------------------------- + // GetListHeads + // ---------------------------------------------------------------- + + bool GetHeadsOfLists(PLIST_ENTRY* pInLoadOrderModuleList, PLIST_ENTRY* pInMemoryOrderModuleList, PLIST_ENTRY* pInInitializationOrderModuleList) { + auto pPEB = GetPEB(); + if (!pPEB) { + return false; + } + + auto pLDR = pPEB->Ldr; + if (!pLDR) { + return false; + } + + if (pInLoadOrderModuleList) { + *pInLoadOrderModuleList = &pLDR->InLoadOrderModuleList; + } + + if (pInMemoryOrderModuleList) { + *pInMemoryOrderModuleList = &pLDR->InMemoryOrderModuleList; + } + + if (pInInitializationOrderModuleList) { + *pInInitializationOrderModuleList = &pLDR->InInitializationOrderModuleList; + } + + return true; + } + + // ---------------------------------------------------------------- + // FindModuleListEntry + // ---------------------------------------------------------------- + + PLIST_ENTRY FindModuleListEntry(void* pBaseAddress) { + if (!pBaseAddress) { + return nullptr; + } + + PLIST_ENTRY pInLoadOrderModuleList = nullptr; + PLIST_ENTRY pInMemoryOrderModuleList = nullptr; + PLIST_ENTRY pInInitializationOrderModuleList = nullptr; + + if (!GetHeadsOfLists(&pInLoadOrderModuleList, &pInMemoryOrderModuleList, &pInInitializationOrderModuleList)) { + return nullptr; + } + + if (pInLoadOrderModuleList) { + PLIST_ENTRY pHead = pInLoadOrderModuleList; + PLIST_ENTRY pEntry = pInLoadOrderModuleList->Flink; + while (pEntry != pHead) { + auto pDTE = CONTAINING_RECORD(pEntry, Detours::LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); + + if (pDTE->DllBase == pBaseAddress) { + return pEntry; + } + + pEntry = pEntry->Flink; + } + } + + if (pInMemoryOrderModuleList) { + PLIST_ENTRY pHead = pInMemoryOrderModuleList; + PLIST_ENTRY pEntry = pInMemoryOrderModuleList->Flink; + while (pEntry != pHead) { + auto pDTE = CONTAINING_RECORD(pEntry, Detours::LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); + + if (pDTE->DllBase == pBaseAddress) { + return pEntry; + } + + pEntry = pEntry->Flink; + } + } + + if (pInInitializationOrderModuleList) { + PLIST_ENTRY pHead = pInInitializationOrderModuleList; + PLIST_ENTRY pEntry = pInInitializationOrderModuleList->Flink; + while (pEntry != pHead) { + auto pDTE = CONTAINING_RECORD(pEntry, Detours::LDR_DATA_TABLE_ENTRY, InInitializationOrderLinks); + + if (pDTE->DllBase == pBaseAddress) { + return pEntry; + } + + pEntry = pEntry->Flink; + } + } + + return nullptr; + } + + PLIST_ENTRY FindModuleListEntry(HMODULE hModule) { + if (!hModule) { + return nullptr; + } + + return FindModuleListEntry(static_cast(hModule)); + } + + PLIST_ENTRY FindModuleListEntryA(const char* szModuleName) { + if (!szModuleName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindModuleListEntry(hModule); + } + + PLIST_ENTRY FindModuleListEntryW(const wchar_t* szModuleName) { + if (!szModuleName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindModuleListEntry(hModule); + } + +#ifdef _UNICODE + PLIST_ENTRY FindModuleListEntry(const wchar_t* szModuleName) { + return FindModuleListEntryW(szModuleName); + } +#else + PLIST_ENTRY FindModuleListEntry(const char* szModuleName) { + return FindModuleListEntryA(szModuleName); + } +#endif + + // ---------------------------------------------------------------- + // FindModuleDataTableRecords + // ---------------------------------------------------------------- + + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(void* pBaseAddress) { + if (!pBaseAddress) { + return nullptr; + } + + auto pEntry = FindModuleListEntry(pBaseAddress); + if (!pEntry) { + return nullptr; + } + + return CONTAINING_RECORD(pEntry, Detours::LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); + } + + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(HMODULE hModule) { + if (!hModule) { + return nullptr; + } + + return FindModuleDataTableEntry(static_cast(hModule)); + } + + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntryA(const char* szModuleName) { + if (!szModuleName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindModuleDataTableEntry(hModule); + } + + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntryW(const wchar_t* szModuleName) { + if (!szModuleName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindModuleDataTableEntry(hModule); + } + +#ifdef _UNICODE + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(const wchar_t* szModuleName) { + return FindModuleDataTableEntryW(szModuleName); + } +#else + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(const char* szModuleName) { + return FindModuleDataTableEntryA(szModuleName); + } +#endif + + // ---------------------------------------------------------------- + // UnLinkModule + // ---------------------------------------------------------------- + + bool UnLinkModule(void* pBaseAddress, PLINK_DATA pLinkData) { + if (!pBaseAddress || !pLinkData) { + return false; + } + + memset(pLinkData, 0, sizeof(LINK_DATA)); + + auto pDTE = Detours::LDR::FindModuleDataTableEntry(pBaseAddress); + if (!pDTE) { + return false; + } + + PLIST_ENTRY pInLoadOrderModuleList = nullptr; + PLIST_ENTRY pInMemoryOrderModuleList = nullptr; + PLIST_ENTRY pInInitializationOrderModuleList = nullptr; + + if (!GetHeadsOfLists(&pInLoadOrderModuleList, &pInMemoryOrderModuleList, &pInInitializationOrderModuleList)) { + return false; + } + + pLinkData->m_pHeadInLoadOrderLinks = pInLoadOrderModuleList; + pLinkData->m_pHeadInMemoryOrderLinks = pInMemoryOrderModuleList; + pLinkData->m_pHeadInInitializationOrderLinks = pInInitializationOrderModuleList; + + pLinkData->m_pHeadHashLinks = GetListHeadFromEntry(&pDTE->HashLinks); + pLinkData->m_pHeadNodeModuleLink = GetListHeadFromEntry(&pDTE->NodeModuleLink); + + if (pLinkData->m_pHeadInLoadOrderLinks) { + Detours::LDR::RemoveEntryList(&pDTE->InLoadOrderLinks); + pLinkData->m_pSavedInLoadOrderLinks = &pDTE->InLoadOrderLinks; + } + + if (pLinkData->m_pHeadInMemoryOrderLinks) { + Detours::LDR::RemoveEntryList(&pDTE->InMemoryOrderLinks); + pLinkData->m_pSavedInMemoryOrderLinks = &pDTE->InMemoryOrderLinks; + } + + if (pLinkData->m_pHeadInInitializationOrderLinks) { + Detours::LDR::RemoveEntryList(&pDTE->InInitializationOrderLinks); + pLinkData->m_pSavedInInitializationOrderLinks = &pDTE->InInitializationOrderLinks; + } + + if (pLinkData->m_pHeadHashLinks) { + Detours::LDR::RemoveEntryList(&pDTE->HashLinks); + pLinkData->m_pSavedHashLinks = &pDTE->HashLinks; + } + + if (pLinkData->m_pHeadNodeModuleLink) { + Detours::LDR::RemoveEntryList(&pDTE->NodeModuleLink); + pLinkData->m_pSavedNodeModuleLink = &pDTE->NodeModuleLink; + } + + return true; + } + + bool UnLinkModule(HMODULE hModule, PLINK_DATA pLinkData) { + return UnLinkModule(static_cast(hModule), pLinkData); + } + + bool UnLinkModuleA(const char* szModuleName, PLINK_DATA pLinkData) { + if (!szModuleName || !pLinkData) { + return false; + } + + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { + return false; + } + + return UnLinkModule(hModule, pLinkData); + } + + bool UnLinkModuleW(const wchar_t* szModuleName, PLINK_DATA pLinkData) { + if (!szModuleName || !pLinkData) { + return false; + } + + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { + return false; + } + + return UnLinkModule(hModule, pLinkData); + } +#ifdef _UNICODE + bool UnLinkModule(const wchar_t* szModuleName, PLINK_DATA pLinkData) { + return UnLinkModuleW(szModuleName, pLinkData); + } +#else + bool UnLinkModule(const char* szModuleName, PLINK_DATA pLinkData) { + return UnLinkModuleA(szModuleName, pLinkData); + } +#endif + + // ---------------------------------------------------------------- + // ReLinkModule + // ---------------------------------------------------------------- + + bool ReLinkModule(LINK_DATA LinkData) { + if (LinkData.m_pSavedInLoadOrderLinks) { + if (!LinkData.m_pHeadInLoadOrderLinks) { + return false; + } + } + + if (LinkData.m_pSavedInMemoryOrderLinks) { + if (!LinkData.m_pHeadInMemoryOrderLinks) { + return false; + } + } + + if (LinkData.m_pSavedInInitializationOrderLinks) { + if (!LinkData.m_pHeadInInitializationOrderLinks) { + return false; + } + } + + if (LinkData.m_pSavedHashLinks) { + if (!LinkData.m_pHeadHashLinks) { + return false; + } + } + + if (LinkData.m_pSavedNodeModuleLink) { + if (!LinkData.m_pHeadNodeModuleLink) { + return false; + } + } + + if (LinkData.m_pSavedInLoadOrderLinks) { + Detours::LDR::InsertTailList(LinkData.m_pHeadInLoadOrderLinks, LinkData.m_pSavedInLoadOrderLinks); + } + + if (LinkData.m_pSavedInMemoryOrderLinks) { + Detours::LDR::InsertTailList(LinkData.m_pHeadInMemoryOrderLinks, LinkData.m_pSavedInMemoryOrderLinks); + } + + if (LinkData.m_pSavedInInitializationOrderLinks) { + Detours::LDR::InsertTailList(LinkData.m_pHeadInInitializationOrderLinks, LinkData.m_pSavedInInitializationOrderLinks); + } + + if (LinkData.m_pSavedHashLinks) { + Detours::LDR::InsertTailList(LinkData.m_pHeadHashLinks, LinkData.m_pSavedHashLinks); + } + + if (LinkData.m_pSavedNodeModuleLink) { + Detours::LDR::InsertTailList(LinkData.m_pHeadNodeModuleLink, LinkData.m_pSavedNodeModuleLink); + } + + return true; + } + } + // ---------------------------------------------------------------- // Codec // ---------------------------------------------------------------- @@ -256,7 +702,7 @@ namespace Detours { } unsigned char const* pByteData = reinterpret_cast(pData); - for (size_t i = 0; i < unSize; i++) { + for (size_t i = 0; i < unSize; ++i) { const unsigned char unByte = pByteData[i] ? pByteData[i] : unIgnoredByte; *szHex++ = g_pEncodeTable[unByte >> 4]; @@ -272,7 +718,7 @@ namespace Detours { } unsigned char const* pByteData = reinterpret_cast(pData); - for (size_t i = 0; i < unSize; i++) { + for (size_t i = 0; i < unSize; ++i) { const unsigned char unByte = pByteData[i] ? pByteData[i] : unIgnoredByte; *szHex++ = static_cast(g_pEncodeTable[unByte >> 4]); @@ -296,7 +742,8 @@ namespace Detours { // Decode // ---------------------------------------------------------------- - static const unsigned char g_pDecodeTableLow[256] = { + static const unsigned char g_pDecodeTable[512] = { + // LOW 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -328,10 +775,8 @@ namespace Detours { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF - }; - - static const unsigned char g_pDecodeTableHigh[256] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + // HIGH 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -379,7 +824,7 @@ namespace Detours { for (size_t i = 0; i < unHexLength; i += 2) { const unsigned char unHigh = szHex[i]; const unsigned char unLow = szHex[i + 1]; - const unsigned char unByte = g_pDecodeTableHigh[unHigh] | g_pDecodeTableLow[unLow]; + const unsigned char unByte = g_pDecodeTable[unHigh + 256] | g_pDecodeTable[unLow]; if (unByte == unIgnoredByte) { i += 2; @@ -405,7 +850,7 @@ namespace Detours { for (size_t i = 0; i < unHexLength; i += 2) { const unsigned char unHigh = static_cast(szHex[i] & 0xFF); const unsigned char unLow = static_cast(szHex[i + 1] & 0xFF); - const unsigned char unByte = g_pDecodeTableHigh[unHigh] | g_pDecodeTableLow[unLow]; + const unsigned char unByte = g_pDecodeTable[unHigh + 256] | g_pDecodeTable[unLow]; if (unByte == unIgnoredByte) { i += 2; @@ -441,6 +886,7 @@ namespace Detours { template static const T inline __align_up(const T unValue, const T unAlignment) { + static_assert(std::is_integral::value, "Template argument must be an integral type"); return (unValue + unAlignment - 1) & ~(unAlignment - 1); }; @@ -450,6 +896,7 @@ namespace Detours { template static const T inline __align_down(const T unValue, const T unAlignment) { + static_assert(std::is_integral::value, "Template argument must be an integral type"); return unValue & ~(unAlignment - 1); }; @@ -457,14 +904,62 @@ namespace Detours { // __bit_scan_forward // ---------------------------------------------------------------- + template + static const T inline __bit_scan_forward(const T unValue); + +#ifdef _M_X64 + template <> + const long long inline __bit_scan_forward(const long long unValue) { + unsigned long unIndex = 0; + if (_BitScanForward64(&unIndex, static_cast(unValue))) { + return static_cast(unIndex); + } + + return sizeof(long long) * CHAR_BIT; + } + + template <> + const unsigned long long inline __bit_scan_forward(const unsigned long long unValue) { + unsigned long unIndex = 0; + if (_BitScanForward64(&unIndex, static_cast(unValue))) { + return static_cast(unIndex); + } + + return sizeof(unsigned long long) * CHAR_BIT; + } +#endif + + template <> + const unsigned long inline __bit_scan_forward(const unsigned long unValue) { + unsigned long unIndex = 0; + if (_BitScanForward(&unIndex, static_cast(unValue))) { + return static_cast(unIndex); + } + + return sizeof(unsigned long) * CHAR_BIT; + } + + template <> + const unsigned int inline __bit_scan_forward(const unsigned int unValue) { + unsigned long unIndex = 0; + if (_BitScanForward(&unIndex, static_cast(unValue))) { + return static_cast(unIndex); + } + + return sizeof(unsigned int) * CHAR_BIT; + } + template static const T inline __bit_scan_forward(const T unValue) { + static_assert(std::is_integral::value, "Template argument must be an integral type"); + for (unsigned char i = 0; i < (sizeof(T) * CHAR_BIT); ++i) { if (((unValue >> i) & 1) != 0) { - return i; + return static_cast(i); } } - return sizeof(T) * CHAR_BIT; + + return static_cast(sizeof(T) * CHAR_BIT); } // ---------------------------------------------------------------- @@ -506,12 +1001,12 @@ namespace Detours { return false; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return false; } - return FindSection(hMod, SectionName, pAddress, pSize); + return FindSection(hModule, SectionName, pAddress, pSize); } bool FindSectionW(wchar_t const* const szModuleName, const std::array& SectionName, void** pAddress, size_t* pSize) noexcept { @@ -519,12 +1014,12 @@ namespace Detours { return false; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return false; } - return FindSection(hMod, SectionName, pAddress, pSize); + return FindSection(hModule, SectionName, pAddress, pSize); } #ifdef _UNICODE @@ -609,12 +1104,12 @@ namespace Detours { return false; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return false; } - return FindSectionPOGO(hMod, szSectionName, pAddress, pSize); + return FindSectionPOGO(hModule, szSectionName, pAddress, pSize); } bool FindSectionPOGOW(wchar_t const* const szModuleName, char const* const szSectionName, void** pAddress, size_t* pSize) noexcept { @@ -622,12 +1117,12 @@ namespace Detours { return false; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return false; } - return FindSectionPOGO(hMod, szSectionName, pAddress, pSize); + return FindSectionPOGO(hModule, szSectionName, pAddress, pSize); } #ifdef _UNICODE @@ -704,7 +1199,7 @@ namespace Detours { // FindSignature (Native) // ---------------------------------------------------------------- - void const* FindSignatureNative(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNative(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!pAddress || !unSize || !szSignature) { return nullptr; } @@ -744,7 +1239,7 @@ namespace Detours { return nullptr; } - void const* FindSignatureNative(const HMODULE hModule, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNative(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -753,10 +1248,10 @@ namespace Detours { const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); const auto& pOH = &(pNTHs->OptionalHeader); - return FindSignatureNative(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNative(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -767,10 +1262,10 @@ namespace Detours { return nullptr; } - return FindSignatureNative(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNative(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSectionName || !szSignature) { return nullptr; } @@ -781,110 +1276,110 @@ namespace Detours { return nullptr; } - return FindSignatureNative(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureNative(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #ifdef _UNICODE - void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeW(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeW(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeW(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeW(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeW(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeW(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #else - void const* FindSignatureNative(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeA(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeA(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeA(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeA(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureNative(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureNativeA(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureNative(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureNativeA(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #endif @@ -892,7 +1387,7 @@ namespace Detours { // FindSignature (SSE2) // ---------------------------------------------------------------- - void const* FindSignatureSSE2(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!pAddress || !unSize || !szSignature) { return nullptr; } @@ -946,16 +1441,16 @@ namespace Detours { const size_t unDataBytesLeft = unSize & 0xF; if (unDataBytesLeft) { if (unDataBytesLeft < unSignatureLength) { - return FindSignatureNative(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unIgnoredByte, unOffset, unHash); } - return FindSignatureNative(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unIgnoredByte, unOffset, unHash); } return nullptr; } - void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -964,10 +1459,10 @@ namespace Detours { const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); const auto& pOH = &(pNTHs->OptionalHeader); - return FindSignatureSSE2(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -978,10 +1473,10 @@ namespace Detours { return nullptr; } - return FindSignatureSSE2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSectionName || !szSignature) { return nullptr; } @@ -992,110 +1487,110 @@ namespace Detours { return nullptr; } - return FindSignatureSSE2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureSSE2(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #ifdef _UNICODE - void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2W(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2W(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2W(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2W(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2W(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2W(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #else - void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2A(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2A(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2A(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2A(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureSSE2A(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureSSE2A(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #endif @@ -1103,7 +1598,7 @@ namespace Detours { // FindSignature (AVX2) // ---------------------------------------------------------------- - void const* FindSignatureAVX2(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!pAddress || !unSize || !szSignature) { return nullptr; } @@ -1157,16 +1652,16 @@ namespace Detours { const size_t unDataBytesLeft = unSize & 0x1F; if (unDataBytesLeft) { if (unDataBytesLeft < unSignatureLength) { - return FindSignatureSSE2(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unIgnoredByte, unOffset, unHash); } - return FindSignatureSSE2(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unIgnoredByte, unOffset, unHash); } return nullptr; } - void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1175,10 +1670,10 @@ namespace Detours { const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); const auto& pOH = &(pNTHs->OptionalHeader); - return FindSignatureAVX2(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1189,10 +1684,10 @@ namespace Detours { return nullptr; } - return FindSignatureAVX2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSectionName || !szSignature) { return nullptr; } @@ -1203,110 +1698,110 @@ namespace Detours { return nullptr; } - return FindSignatureAVX2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX2(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #ifdef _UNICODE - void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2W(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2W(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2W(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2W(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2W(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2W(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #else - void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2A(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2A(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2A(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2A(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX2A(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX2A(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #endif @@ -1314,7 +1809,7 @@ namespace Detours { // FindSignature (AVX-512) [AVX512BW] // ---------------------------------------------------------------- - void const* FindSignatureAVX512(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!pAddress || !unSize || !szSignature) { return nullptr; } @@ -1366,16 +1861,16 @@ namespace Detours { const size_t unDataBytesLeft = unSize & 0x3F; if (unDataBytesLeft) { if (unDataBytesLeft < unSignatureLength) { - return FindSignatureAVX2(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(pData + unSize - unDataBytesLeft - unSignatureLength, unDataBytesLeft + unSignatureLength, szSignature, unIgnoredByte, unOffset, unHash); } - return FindSignatureAVX2(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(pData + unSize - unDataBytesLeft, unDataBytesLeft, szSignature, unIgnoredByte, unOffset, unHash); } return nullptr; } - void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1384,10 +1879,10 @@ namespace Detours { const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); const auto& pOH = &(pNTHs->OptionalHeader); - return FindSignatureAVX512(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1398,10 +1893,10 @@ namespace Detours { return nullptr; } - return FindSignatureAVX512(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSectionName || !szSignature) { return nullptr; } @@ -1412,110 +1907,110 @@ namespace Detours { return nullptr; } - return FindSignatureAVX512(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignatureAVX512(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #ifdef _UNICODE - void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512W(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512W(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512W(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512W(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512W(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512W(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #else - void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512A(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512A(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512A(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512A(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureAVX512A(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureAVX512A(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #endif @@ -1528,7 +2023,7 @@ namespace Detours { static bool bFeatureAVX2 = false; static bool bFeatureAVX512BW = false; - void const* FindSignature(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignature(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!bOnceInitialization) { bOnceInitialization = true; @@ -1547,17 +2042,17 @@ namespace Detours { } if (bFeatureAVX512BW) { - return FindSignatureAVX512(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX512(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } else if (bFeatureAVX2) { - return FindSignatureAVX2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureAVX2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } else if (bFeatureSSE2) { - return FindSignatureSSE2(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureSSE2(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } else { - return FindSignatureNative(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignatureNative(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } } - void const* FindSignature(const HMODULE hModule, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignature(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1566,10 +2061,10 @@ namespace Detours { const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); const auto& pOH = &(pNTHs->OptionalHeader); - return FindSignature(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignature(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSignature) { return nullptr; } @@ -1580,10 +2075,10 @@ namespace Detours { return nullptr; } - return FindSignature(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignature(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!hModule || !szSectionName || !szSignature) { return nullptr; } @@ -1594,110 +2089,110 @@ namespace Detours { return nullptr; } - return FindSignature(pAddress, unSize, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(pAddress, unSize, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureA(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureA(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { + void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { if (!szModuleName || !szSectionName || !szSignature) { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindSignature(hMod, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + return FindSignature(hModule, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #ifdef _UNICODE - void const* FindSignature(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureW(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureW(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureW(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureW(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureW(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureW(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #else - void const* FindSignature(char const* const szModuleName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureA(szModuleName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureA(szModuleName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureA(szModuleName, SectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureA(szModuleName, SectionName, szSignature, unIgnoredByte, unOffset, unHash); } - void const* FindSignature(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset, const unsigned int unHash, const unsigned char unIgnoredByte) noexcept { - return FindSignatureA(szModuleName, szSectionName, szSignature, unOffset, unHash, unIgnoredByte); + void const* FindSignature(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte, const size_t unOffset, const unsigned int unHash) noexcept { + return FindSignatureA(szModuleName, szSectionName, szSignature, unIgnoredByte, unOffset, unHash); } #endif @@ -1773,12 +2268,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, pData, unDataSize); + return FindDataNative(hModule, pData, unDataSize); } void const* FindDataNativeA(char const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1786,12 +2281,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, SectionName, pData, unDataSize); + return FindDataNative(hModule, SectionName, pData, unDataSize); } void const* FindDataNativeA(char const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1799,12 +2294,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, szSectionName, pData, unDataSize); + return FindDataNative(hModule, szSectionName, pData, unDataSize); } void const* FindDataNativeW(wchar_t const* const szModuleName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1812,12 +2307,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, pData, unDataSize); + return FindDataNative(hModule, pData, unDataSize); } void const* FindDataNativeW(wchar_t const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1825,12 +2320,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, SectionName, pData, unDataSize); + return FindDataNative(hModule, SectionName, pData, unDataSize); } void const* FindDataNativeW(wchar_t const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1838,12 +2333,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataNative(hMod, szSectionName, pData, unDataSize); + return FindDataNative(hModule, szSectionName, pData, unDataSize); } #ifdef _UNICODE @@ -1965,12 +2460,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, pData, unDataSize); + return FindDataSSE2(hModule, pData, unDataSize); } void const* FindDataSSE2A(char const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1978,12 +2473,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, SectionName, pData, unDataSize); + return FindDataSSE2(hModule, SectionName, pData, unDataSize); } void const* FindDataSSE2A(char const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -1991,12 +2486,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, szSectionName, pData, unDataSize); + return FindDataSSE2(hModule, szSectionName, pData, unDataSize); } void const* FindDataSSE2W(wchar_t const* const szModuleName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2004,12 +2499,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, pData, unDataSize); + return FindDataSSE2(hModule, pData, unDataSize); } void const* FindDataSSE2W(wchar_t const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2017,12 +2512,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, SectionName, pData, unDataSize); + return FindDataSSE2(hModule, SectionName, pData, unDataSize); } void const* FindDataSSE2W(wchar_t const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2030,12 +2525,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataSSE2(hMod, szSectionName, pData, unDataSize); + return FindDataSSE2(hModule, szSectionName, pData, unDataSize); } #ifdef _UNICODE @@ -2157,12 +2652,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, pData, unDataSize); + return FindDataAVX2(hModule, pData, unDataSize); } void const* FindDataAVX2A(char const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2170,12 +2665,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, SectionName, pData, unDataSize); + return FindDataAVX2(hModule, SectionName, pData, unDataSize); } void const* FindDataAVX2A(char const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2183,12 +2678,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, szSectionName, pData, unDataSize); + return FindDataAVX2(hModule, szSectionName, pData, unDataSize); } void const* FindDataAVX2W(wchar_t const* const szModuleName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2196,12 +2691,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, pData, unDataSize); + return FindDataAVX2(hModule, pData, unDataSize); } void const* FindDataAVX2W(wchar_t const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2209,12 +2704,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, SectionName, pData, unDataSize); + return FindDataAVX2(hModule, SectionName, pData, unDataSize); } void const* FindDataAVX2W(wchar_t const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2222,12 +2717,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX2(hMod, szSectionName, pData, unDataSize); + return FindDataAVX2(hModule, szSectionName, pData, unDataSize); } #ifdef _UNICODE @@ -2347,12 +2842,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, pData, unDataSize); + return FindDataAVX512(hModule, pData, unDataSize); } void const* FindDataAVX512A(char const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2360,12 +2855,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, SectionName, pData, unDataSize); + return FindDataAVX512(hModule, SectionName, pData, unDataSize); } void const* FindDataAVX512A(char const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2373,12 +2868,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, pData, unDataSize); + return FindDataAVX512(hModule, pData, unDataSize); } void const* FindDataAVX512W(wchar_t const* const szModuleName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2386,12 +2881,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, pData, unDataSize); + return FindDataAVX512(hModule, pData, unDataSize); } void const* FindDataAVX512W(wchar_t const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2399,12 +2894,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, SectionName, pData, unDataSize); + return FindDataAVX512(hModule, SectionName, pData, unDataSize); } void const* FindDataAVX512W(wchar_t const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2412,12 +2907,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindDataAVX512(hMod, szSectionName, pData, unDataSize); + return FindDataAVX512(hModule, szSectionName, pData, unDataSize); } #ifdef _UNICODE @@ -2524,12 +3019,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, pData, unDataSize); + return FindData(hModule, pData, unDataSize); } void const* FindDataA(char const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2537,12 +3032,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, SectionName, pData, unDataSize); + return FindData(hModule, SectionName, pData, unDataSize); } void const* FindDataA(char const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2550,12 +3045,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, szSectionName, pData, unDataSize); + return FindData(hModule, szSectionName, pData, unDataSize); } void const* FindDataW(wchar_t const* const szModuleName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2563,12 +3058,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, pData, unDataSize); + return FindData(hModule, pData, unDataSize); } void const* FindDataW(wchar_t const* const szModuleName, const std::array& SectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2576,12 +3071,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, SectionName, pData, unDataSize); + return FindData(hModule, SectionName, pData, unDataSize); } void const* FindDataW(wchar_t const* const szModuleName, char const* const szSectionName, unsigned char const* const pData, const size_t unDataSize) noexcept { @@ -2589,12 +3084,12 @@ namespace Detours { return nullptr; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { return nullptr; } - return FindData(hMod, szSectionName, pData, unDataSize); + return FindData(hModule, szSectionName, pData, unDataSize); } #ifdef _UNICODE @@ -3153,250 +3648,1602 @@ namespace Detours { } } - if (!pBCD) { - return nullptr; + if (!pBCD) { + return nullptr; + } + + return reinterpret_cast(reinterpret_cast(pCO) + PMDtoOffset(pCO, pBCD->m_Where)); + } + + void const* const Object::DynamicCast(void const* const pAddress, const Object* pObject) { + if (!pAddress || !pObject) { + return nullptr; + } + +#ifdef _M_X64 + return __RTDynamicCast(m_pBaseAddress, pAddress, 0, m_pTypeDescriptor, pObject->GetTypeDescriptor()); +#elif _M_IX86 + return __RTDynamicCast(pAddress, 0, m_pTypeDescriptor, pObject->GetTypeDescriptor()); +#endif + } + + const PRTTI_TYPE_DESCRIPTOR Object::GetTypeDescriptor() const { + return m_pTypeDescriptor; + } + + const PRTTI_CLASS_HIERARCHY_DESCRIPTOR Object::GetClassHierarchyDescriptor() const { + return m_pClassHierarchyDescriptor; + } + + const PRTTI_COMPLETE_OBJECT_LOCATOR Object::GetCompleteObject() const { + return m_pCompleteObject; + } + + void** Object::GetVTable() const { + return m_pVTable; + } + + std::vector>& Object::GetBaseObjects() { + return m_vecBaseClasses; + } + + // ---------------------------------------------------------------- + // FindObject + // ---------------------------------------------------------------- + + std::unique_ptr FindObject(void const* const pBaseAddress, void const* const pAddress, const size_t unSize, char const* const szName, bool bCompleteObject) { + if (!pBaseAddress || !pAddress || !unSize || !szName) { + return nullptr; + } + + const size_t unNameLength = strnlen_s(szName, 0x1000); + if (!unNameLength || (unSize <= unNameLength)) { + return nullptr; + } + + void* pReference = const_cast(pAddress); + void* pEndAddress = reinterpret_cast(const_cast(pAddress)) + unSize - unNameLength; + while (pReference && (pReference < pEndAddress)) { + pReference = const_cast(FindData(pReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pReference), reinterpret_cast(szName), unNameLength)); + if (!pReference) { + break; + } + + const auto& pTypeDescriptor = reinterpret_cast(reinterpret_cast(pReference) - sizeof(void*) * 2); + if ((pTypeDescriptor->m_pVFTable < pBaseAddress) || (pTypeDescriptor->m_pVFTable >= pEndAddress)) { + pReference = reinterpret_cast(reinterpret_cast(pReference) + 1); + continue; + } + + void* pTypeDescriptorReference = const_cast(pAddress); + while (pTypeDescriptorReference && (pTypeDescriptorReference < pEndAddress)) { +#ifdef _M_X64 + const size_t unTypeDescriptorOffsetTemp = reinterpret_cast(pTypeDescriptor) - reinterpret_cast(pBaseAddress); + const unsigned int unTypeDescriptorOffset = (*(reinterpret_cast(&unTypeDescriptorOffsetTemp))); + pTypeDescriptorReference = const_cast(FindData(pTypeDescriptorReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pTypeDescriptorReference), reinterpret_cast(&unTypeDescriptorOffset), sizeof(int))); +#elif _M_IX86 + pTypeDescriptorReference = const_cast(FindData(pTypeDescriptorReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pTypeDescriptorReference), reinterpret_cast(&pTypeDescriptor), sizeof(int))); +#endif + if (!pTypeDescriptorReference) { + break; + } + + if (bCompleteObject) { + const auto& pCompleteObjectLocator = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) - sizeof(int) * 3); + if ((pCompleteObjectLocator->m_unSignature != COL_SIG_REV0) && (pCompleteObjectLocator->m_unSignature != COL_SIG_REV1)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + +#ifdef _M_X64 + if (pCompleteObjectLocator->m_unSignature != COL_SIG_REV0) { + if ((reinterpret_cast(pCompleteObjectLocator) - reinterpret_cast(pBaseAddress)) != pCompleteObjectLocator->m_unSelf) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + } +#endif + +#ifdef _M_X64 + const auto& pClassHierarchyDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pCompleteObjectLocator->m_unClassHierarchyDescriptor); +#elif _M_IX86 + const auto& pClassHierarchyDescriptor = pCompleteObjectLocator->m_pClassHierarchyDescriptor; +#endif + if ((pClassHierarchyDescriptor < pBaseAddress) || (pClassHierarchyDescriptor >= pEndAddress)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + + if ((pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV0) && (pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV1)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + +#ifdef _M_X64 + const auto& pBaseClassArray = reinterpret_cast(reinterpret_cast(pBaseAddress) + pClassHierarchyDescriptor->m_unBaseClassArray); +#elif _M_IX86 + const auto& pBaseClassArray = pClassHierarchyDescriptor->m_pBaseClassArray; +#endif + if ((pBaseClassArray < pBaseAddress) || (pBaseClassArray >= pEndAddress)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + + for (unsigned int i = 0; i < pClassHierarchyDescriptor->m_unNumberOfBaseClasses; ++i) { +#ifdef _M_X64 + const auto& pBaseClassDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassArray->m_unBaseClassDescriptors[i]); +#elif _M_IX86 + const auto& pBaseClassDescriptor = pBaseClassArray->m_pBaseClassDescriptors[i]; +#endif + if (!pBaseClassDescriptor) { + break; + } + +#ifdef _M_X64 + if (reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unTypeDescriptor) == pTypeDescriptor) { +#elif _M_IX86 + if (pBaseClassDescriptor->m_pTypeDescriptor == pTypeDescriptor) { +#endif + void* pCompleteObjectLocatorReference = const_cast(FindData(pAddress, unSize, reinterpret_cast(&pCompleteObjectLocator), sizeof(void*))); + if (!pCompleteObjectLocatorReference) { + return nullptr; + } + + return std::make_unique(pBaseAddress, pAddress, unSize, pTypeDescriptor, pClassHierarchyDescriptor, pBaseClassArray, pCompleteObjectLocator, reinterpret_cast(reinterpret_cast(pCompleteObjectLocatorReference) + sizeof(void*))); + } + } + } else { + const auto& pBaseClassDescriptor = reinterpret_cast(pTypeDescriptorReference); +#ifdef _M_X64 + const auto& pClassHierarchyDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unClassHierarchyDescriptor); +#elif _M_IX86 + const auto& pClassHierarchyDescriptor = pBaseClassDescriptor->m_pClassHierarchyDescriptor; +#endif + if ((pClassHierarchyDescriptor < pBaseAddress) || (pClassHierarchyDescriptor >= pEndAddress)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + + if ((pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV0) && (pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV1)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + +#ifdef _M_X64 + const auto& pBaseClassArray = reinterpret_cast(reinterpret_cast(pBaseAddress) + pClassHierarchyDescriptor->m_unBaseClassArray); +#elif _M_IX86 + const auto& pBaseClassArray = pClassHierarchyDescriptor->m_pBaseClassArray; +#endif + if ((pBaseClassArray < pBaseAddress) || (pBaseClassArray >= pEndAddress)) { + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + continue; + } + +#ifdef _M_X64 + if (reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unTypeDescriptor) == pTypeDescriptor) { +#elif _M_IX86 + if (pBaseClassDescriptor->m_pTypeDescriptor == pTypeDescriptor) { +#endif + return std::make_unique(pBaseAddress, pAddress, unSize, pTypeDescriptor, pClassHierarchyDescriptor, pBaseClassArray, nullptr, nullptr); + } + } + + pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); + } + + pReference = reinterpret_cast(reinterpret_cast(pReference) + 1); + } + + return nullptr; + } + + std::unique_ptr FindObject(void const* const pAddress, const size_t unSize, char const* const szName, bool bCompleteObject) { + return FindObject(pAddress, pAddress, unSize, szName, bCompleteObject); + } + + std::unique_ptr FindObject(const HMODULE hModule, char const* const szName, bool bCompleteObject) { + if (!hModule || !szName) { + return nullptr; + } + + const auto& pDH = reinterpret_cast(hModule); + const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); + const auto& pOH = &(pNTHs->OptionalHeader); + + return FindObject(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szName, bCompleteObject); + } + + std::unique_ptr FindObjectA(char const* const szModuleName, char const* const szName, bool bCompleteObject) { + if (!szModuleName || !szName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleA(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindObject(hModule, szName, bCompleteObject); + } + + + std::unique_ptr FindObjectW(wchar_t const* const szModuleName, char const* const szName, bool bCompleteObject) { + if (!szModuleName || !szName) { + return nullptr; + } + + HMODULE hModule = GetModuleHandleW(szModuleName); + if (!hModule) { + return nullptr; + } + + return FindObject(hModule, szName, bCompleteObject); + } + +#ifdef _UNICODE + std::unique_ptr FindObject(wchar_t const* const szModuleName, char const* const szName, bool bCompleteObject) { + return FindObjectW(szModuleName, szName, bCompleteObject); + } +#else + std::unique_ptr FindObject(char const* const szModuleName, char const* const szName, bool bCompleteObject) { + return FindObjectA(szModuleName, szName, bCompleteObject); + } +#endif + } + + // ---------------------------------------------------------------- + // Sync + // ---------------------------------------------------------------- + + namespace Sync { + + // ---------------------------------------------------------------- + // Event + // ---------------------------------------------------------------- + + Event::Event(bool bManualReset, bool bInitialState) { + m_hEvent = CreateEvent(nullptr, bManualReset, bInitialState, nullptr); + } + + Event::~Event() { + if (m_hEvent && (m_hEvent != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hEvent); + } + } + + HANDLE Event::GetEvent() const { + return m_hEvent; + } + + bool Event::Signal() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!SetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool Event::Reset() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ResetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool Event::Pulse() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!PulseEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool Event::Wait(DWORD unMilliseconds) { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hEvent, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // EventServer + // ---------------------------------------------------------------- + + EventServer::EventServer(bool bIsGlobal, bool bManualReset, bool bInitialState) { + memset(m_szEventName, 0, sizeof(m_szEventName)); + m_hEvent = nullptr; + + auto pTEB = GetTEB(); + if (!pTEB) { + return; + } + + const DWORD unPID = pTEB->ClientId.UniqueProcess & 0xFFFFFFFFi32; + const DWORD unTID = pTEB->ClientId.UniqueThread & 0xFFFFFFFFi32; + const DWORD64 unCycle = __rdtsc(); + if (_stprintf_s(m_szEventName, _T("EVENT:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szEventName, 0, sizeof(m_szEventName)); + return; + } + + TCHAR szEvent[64]; + memset(szEvent, 0, sizeof(szEvent)); + if (bIsGlobal) { + if (_stprintf_s(szEvent, _T("Global\\%s"), m_szEventName) == -1) { + memset(m_szEventName, 0, sizeof(m_szEventName)); + return; + } + } else { + if (_stprintf_s(szEvent, _T("Local\\%s"), m_szEventName) == -1) { + memset(m_szEventName, 0, sizeof(m_szEventName)); + return; + } + } + + m_hEvent = CreateEvent(nullptr, bManualReset, bInitialState, szEvent); + } + + EventServer::~EventServer() { + if (m_hEvent && (m_hEvent != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hEvent); + } + } + + bool EventServer::GetEventName(TCHAR szEventName[64]) { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE) || !szEventName) { + return false; + } + + memcpy(szEventName, m_szEventName, sizeof(m_szEventName)); + + return true; + } + + HANDLE EventServer::GetEvent() const { + return m_hEvent; + } + + bool EventServer::Signal() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!SetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventServer::Reset() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ResetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventServer::Pulse() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!PulseEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventServer::Wait(DWORD unMilliseconds) { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hEvent, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // EventClient + // ---------------------------------------------------------------- + + EventClient::EventClient(TCHAR szEventName[64], bool bIsGlobal) { + m_hEvent = nullptr; + + if (!szEventName) { + return; + } + + TCHAR szEvent[64]; + memset(szEvent, 0, sizeof(szEvent)); + if (bIsGlobal) { + if (_stprintf_s(szEvent, _T("Global\\%s"), szEventName) == -1) { + return; + } + } else { + if (_stprintf_s(szEvent, _T("Local\\%s"), szEventName) == -1) { + return; + } + } + + m_hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, szEvent); + } + + EventClient::~EventClient() { + if (m_hEvent && (m_hEvent != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hEvent); + } + } + + HANDLE EventClient::GetEvent() const { + return m_hEvent; + } + + bool EventClient::Signal() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!SetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventClient::Reset() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ResetEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventClient::Pulse() { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!PulseEvent(m_hEvent)) { + return false; + } + + return true; + } + + bool EventClient::Wait(DWORD unMilliseconds) { + if (!m_hEvent || (m_hEvent == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hEvent, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // Mutex + // ---------------------------------------------------------------- + + Mutex::Mutex(bool bInitialState) { + m_hMutex = CreateMutex(nullptr, bInitialState, nullptr); + } + + Mutex::~Mutex() { + if (m_hMutex && (m_hMutex != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hMutex); + } + } + + HANDLE Mutex::GetMutex() const { + return m_hMutex; + } + + bool Mutex::Lock(DWORD unMilliseconds) { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hMutex, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool Mutex::UnLock() { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseMutex(m_hMutex)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // MutexServer + // ---------------------------------------------------------------- + + MutexServer::MutexServer(bool bIsGlobal, bool bInitialState) { + memset(m_szMutexName, 0, sizeof(m_szMutexName)); + m_hMutex = nullptr; + + auto pTEB = GetTEB(); + if (!pTEB) { + return; + } + + const DWORD unPID = pTEB->ClientId.UniqueProcess & 0xFFFFFFFFi32; + const DWORD unTID = pTEB->ClientId.UniqueThread & 0xFFFFFFFFi32; + const DWORD64 unCycle = __rdtsc(); + if (_stprintf_s(m_szMutexName, _T("MUTEX:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szMutexName, 0, sizeof(m_szMutexName)); + return; + } + + TCHAR szMutex[64]; + memset(szMutex, 0, sizeof(szMutex)); + if (bIsGlobal) { + if (_stprintf_s(szMutex, _T("Global\\%s"), m_szMutexName) == -1) { + memset(m_szMutexName, 0, sizeof(m_szMutexName)); + return; + } + } else { + if (_stprintf_s(szMutex, _T("Local\\%s"), m_szMutexName) == -1) { + memset(m_szMutexName, 0, sizeof(m_szMutexName)); + return; + } + } + + m_hMutex = CreateMutex(nullptr, bInitialState, szMutex); + } + + MutexServer::~MutexServer() { + if (m_hMutex && (m_hMutex != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hMutex); + } + } + + bool MutexServer::GetMutexName(TCHAR szMutexName[64]) { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE) || !szMutexName) { + return false; + } + + memcpy(szMutexName, m_szMutexName, sizeof(m_szMutexName)); + + return true; + } + + HANDLE MutexServer::GetMutex() const { + return m_hMutex; + } + + bool MutexServer::Lock(DWORD unMilliseconds) { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hMutex, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool MutexServer::UnLock() { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseMutex(m_hMutex)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // MutexClient + // ---------------------------------------------------------------- + + MutexClient::MutexClient(TCHAR szMutexName[64], bool bIsGlobal) { + m_hMutex = nullptr; + + if (!szMutexName) { + return; + } + + TCHAR szMutex[64]; + memset(szMutex, 0, sizeof(szMutex)); + if (bIsGlobal) { + if (_stprintf_s(szMutex, _T("Global\\%s"), szMutexName) == -1) { + return; + } + } else { + if (_stprintf_s(szMutex, _T("Local\\%s"), szMutexName) == -1) { + return; + } + } + + m_hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, szMutex); + } + + MutexClient::~MutexClient() { + if (m_hMutex && (m_hMutex != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hMutex); + } + } + + HANDLE MutexClient::GetMutex() const { + return m_hMutex; + } + + bool MutexClient::Lock(DWORD unMilliseconds) { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hMutex, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool MutexClient::UnLock() { + if (!m_hMutex || (m_hMutex == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseMutex(m_hMutex)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // Semaphore + // ---------------------------------------------------------------- + + Semaphore::Semaphore(LONG nInitialCount, LONG nMaximumCount) { + m_hSemaphore = CreateSemaphore(nullptr, nInitialCount, nMaximumCount, nullptr); + } + + Semaphore::~Semaphore() { + if (m_hSemaphore && (m_hSemaphore != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hSemaphore); + } + } + + HANDLE Semaphore::GetSemaphore() const { + return m_hSemaphore; + } + + bool Semaphore::Enter(DWORD unMilliseconds) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hSemaphore, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool Semaphore::Leave(LONG nReleaseCount) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseSemaphore(m_hSemaphore, nReleaseCount, nullptr)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // SemaphoreServer + // ---------------------------------------------------------------- + + SemaphoreServer::SemaphoreServer(bool bIsGlobal, LONG nInitialCount, LONG nMaximumCount) { + memset(m_szSemaphoreName, 0, sizeof(m_szSemaphoreName)); + m_hSemaphore = nullptr; + + auto pTEB = GetTEB(); + if (!pTEB) { + return; + } + + const DWORD unPID = pTEB->ClientId.UniqueProcess & 0xFFFFFFFFi32; + const DWORD unTID = pTEB->ClientId.UniqueThread & 0xFFFFFFFFi32; + const DWORD64 unCycle = __rdtsc(); + if (_stprintf_s(m_szSemaphoreName, _T("SEMAPHORE:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szSemaphoreName, 0, sizeof(m_szSemaphoreName)); + return; + } + + TCHAR szSemaphore[64]; + memset(szSemaphore, 0, sizeof(szSemaphore)); + if (bIsGlobal) { + if (_stprintf_s(szSemaphore, _T("Global\\%s"), m_szSemaphoreName) == -1) { + memset(m_szSemaphoreName, 0, sizeof(m_szSemaphoreName)); + return; + } + } else { + if (_stprintf_s(szSemaphore, _T("Local\\%s"), m_szSemaphoreName) == -1) { + memset(m_szSemaphoreName, 0, sizeof(m_szSemaphoreName)); + return; + } + } + + m_hSemaphore = CreateSemaphore(nullptr, nInitialCount, nMaximumCount, szSemaphore); + } + + SemaphoreServer::~SemaphoreServer() { + if (m_hSemaphore && (m_hSemaphore != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hSemaphore); + } + } + + bool SemaphoreServer::GetSemaphoreName(TCHAR szSemaphoreName[64]) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE) || !szSemaphoreName) { + return false; + } + + memcpy(szSemaphoreName, m_szSemaphoreName, sizeof(m_szSemaphoreName)); + + return true; + } + + HANDLE SemaphoreServer::GetSemaphore() const { + return m_hSemaphore; + } + + bool SemaphoreServer::Enter(DWORD unMilliseconds) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hSemaphore, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool SemaphoreServer::Leave(LONG nReleaseCount) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseSemaphore(m_hSemaphore, nReleaseCount, nullptr)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // SemaphoreClient + // ---------------------------------------------------------------- + + SemaphoreClient::SemaphoreClient(TCHAR szSemaphoreName[64], bool bIsGlobal) { + m_hSemaphore = nullptr; + + if (!szSemaphoreName) { + return; + } + + TCHAR szSemaphore[64]; + memset(szSemaphore, 0, sizeof(szSemaphore)); + if (bIsGlobal) { + if (_stprintf_s(szSemaphore, _T("Global\\%s"), szSemaphoreName) == -1) { + return; + } + } else { + if (_stprintf_s(szSemaphore, _T("Local\\%s"), szSemaphoreName) == -1) { + return; + } + } + + m_hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, szSemaphore); + } + + SemaphoreClient::~SemaphoreClient() { + if (m_hSemaphore && (m_hSemaphore != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hSemaphore); + } + } + + HANDLE SemaphoreClient::GetSemaphore() const { + return m_hSemaphore; + } + + bool SemaphoreClient::Enter(DWORD unMilliseconds) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hSemaphore, unMilliseconds) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool SemaphoreClient::Leave(LONG nReleaseCount) { + if (!m_hSemaphore || (m_hSemaphore == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!ReleaseSemaphore(m_hSemaphore, nReleaseCount, nullptr)) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // CriticalSection + // ---------------------------------------------------------------- + + CriticalSection::CriticalSection() { + InitializeCriticalSection(&m_CriticalSection); + } + + CriticalSection::CriticalSection(DWORD unSpinCount) { + InitializeCriticalSectionAndSpinCount(&m_CriticalSection, unSpinCount); + } + + CriticalSection::~CriticalSection() { + DeleteCriticalSection(&m_CriticalSection); + } + + PCRITICAL_SECTION CriticalSection::GetCriticalSection() { + return &m_CriticalSection; + } + + void CriticalSection::Enter() { + EnterCriticalSection(&m_CriticalSection); + } + + void CriticalSection::Leave() { + LeaveCriticalSection(&m_CriticalSection); + } + + // ---------------------------------------------------------------- + // SRWLock + // ---------------------------------------------------------------- + + SRWLock::SRWLock(bool bIsShared) { + m_bIsShared = bIsShared; + InitializeSRWLock(&m_SRWLock); + } + + SRWLock::~SRWLock() { + Release(); + } + + bool SRWLock::IsShared() const { + return m_bIsShared; + } + + PSRWLOCK SRWLock::GetSRWLock() { + return &m_SRWLock; + } + + void SRWLock::Acquire() { + if (m_bIsShared) { + AcquireSRWLockShared(&m_SRWLock); + } else { + AcquireSRWLockExclusive(&m_SRWLock); + } + } + + void SRWLock::Release() { + if (m_bIsShared) { + ReleaseSRWLockShared(&m_SRWLock); + } else { + AcquireSRWLockExclusive(&m_SRWLock); + } + } + + // ---------------------------------------------------------------- + // ConditionVariable + // ---------------------------------------------------------------- + + ConditionVariable::ConditionVariable() { + InitializeConditionVariable(&m_ConditionVariable); + } + + ConditionVariable::~ConditionVariable() { + WakeAll(); + } + + CONDITION_VARIABLE ConditionVariable::GetConditionVariable() const { + return m_ConditionVariable; + } + + bool ConditionVariable::Sleep(CriticalSection* pLock, DWORD unMilliseconds) { + if (!pLock) { + return false; + } + + if (!SleepConditionVariableCS(&m_ConditionVariable, pLock->GetCriticalSection(), unMilliseconds)) { + return false; + } + + return true; + } + + bool ConditionVariable::Sleep(SRWLock* pLock, DWORD unMilliseconds) { + if (!pLock) { + return false; + } + + if (!SleepConditionVariableSRW(&m_ConditionVariable, pLock->GetSRWLock(), unMilliseconds, pLock->IsShared() ? CONDITION_VARIABLE_LOCKMODE_SHARED : 0)) { + return false; + } + + return true; + } + + void ConditionVariable::Wake() { + WakeConditionVariable(&m_ConditionVariable); + } + + void ConditionVariable::WakeAll() { + WakeAllConditionVariable(&m_ConditionVariable); + } + + // ---------------------------------------------------------------- + // Suspender + // ---------------------------------------------------------------- + + Suspender::~Suspender() { + Resume(); + } + + bool Suspender::Suspend() { + if (!m_Mutex.Lock()) { + return false; + } + + const auto& hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL); + if (!hSnap || (hSnap == INVALID_HANDLE_VALUE)) { + m_Mutex.UnLock(); + return false; + } + + THREADENTRY32 te; + memset(&te, 0, sizeof(te)); + + te.dwSize = sizeof(THREADENTRY32); + + if (!Thread32First(hSnap, &te)) { + CloseHandle(hSnap); + m_Mutex.UnLock(); + return false; + } + + const auto& pTEB = GetTEB(); + if (!pTEB) { + CloseHandle(hSnap); + m_Mutex.UnLock(); + return false; + } + + do { + if ((pTEB->ClientId.UniqueProcess == te.th32OwnerProcessID) && (pTEB->ClientId.UniqueThread != te.th32ThreadID)) { + HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID); + if (hThread && (hThread != INVALID_HANDLE_VALUE)) { + SuspendThread(hThread); + + CONTEXT ctx; + memset(&ctx, 0, sizeof(CONTEXT)); + + ctx.ContextFlags = CONTEXT_FULL; + + if (!GetThreadContext(hThread, &ctx)) { + ResumeThread(hThread); + CloseHandle(hThread); + continue; + } + + m_Threads.emplace_back(te.th32ThreadID, hThread, ctx); + } + } + } while (Thread32Next(hSnap, &te)); + + CloseHandle(hSnap); + + if (!m_Mutex.UnLock()) { + return false; + } + + return true; + } + + void Suspender::Resume() { + + for (auto& thread : m_Threads) { + SetThreadContext(thread.m_hHandle, &thread.m_CTX); + ResumeThread(thread.m_hHandle); + CloseHandle(thread.m_hHandle); + } + + m_Threads.clear(); + } + + void Suspender::FixExecutionAddress(void* pAddress, void* pNewAddress) { + for (auto& thread : m_Threads) { +#ifdef _M_X64 + DWORD64 unIP = thread.m_CTX.Rip; +#elif _M_IX86 + DWORD unIP = thread.m_CTX.Eip; +#endif + + if (reinterpret_cast(unIP) == pAddress) { +#ifdef _M_X64 + unIP = reinterpret_cast(pNewAddress); +#elif _M_IX86 + unIP = reinterpret_cast(pNewAddress); +#endif + } + +#ifdef _M_X64 + thread.m_CTX.Rip = unIP; +#elif _M_IX86 + thread.m_CTX.Eip = unIP; +#endif + } + } + + Suspender g_Suspender; + } + + // ---------------------------------------------------------------- + // Pipe + // ---------------------------------------------------------------- + + namespace Pipe { + + // ---------------------------------------------------------------- + // PipeServer + // ---------------------------------------------------------------- + + PipeServer::PipeServer(const size_t unBufferSize) { + m_unBufferSize = unBufferSize; + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + m_hPipe = nullptr; + + if (!unBufferSize) { + return; + } + + auto pTEB = GetTEB(); + if (!pTEB) { + return; + } + + const DWORD unPID = pTEB->ClientId.UniqueProcess & 0xFFFFFFFFi32; + const DWORD unTID = pTEB->ClientId.UniqueThread & 0xFFFFFFFFi32; + const DWORD64 unCycle = __rdtsc(); + if (_stprintf_s(m_szPipeName, _T("PIPE:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + return; + } + + TCHAR szPipe[64]; + memset(szPipe, 0, sizeof(szPipe)); + if (_stprintf_s(szPipe, _T("\\\\.\\pipe\\%s"), m_szPipeName) == -1) { + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + return; + } + + m_hPipe = CreateNamedPipe(szPipe, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, static_cast(unBufferSize & 0xFFFFFFFF), static_cast(unBufferSize & 0xFFFFFFFF), NMPWAIT_USE_DEFAULT_WAIT, nullptr); + } + + PipeServer::~PipeServer() { + Close(); + } + + bool PipeServer::GetPipeName(TCHAR szPipeName[64]) { + memcpy(szPipeName, m_szPipeName, sizeof(m_szPipeName)); + return true; + } + + HANDLE PipeServer::GetPipe() const { + return m_hPipe; + } + + bool PipeServer::Open() { + if (!m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + TCHAR szPipe[64]; + memset(szPipe, 0, sizeof(szPipe)); + if (_stprintf_s(szPipe, _T("\\\\.\\pipe\\%s"), m_szPipeName) == -1) { + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + return false; + } + + m_hPipe = CreateNamedPipe(szPipe, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, static_cast(m_unBufferSize & 0xFFFFFFFF), static_cast(m_unBufferSize & 0xFFFFFFFF), NMPWAIT_USE_DEFAULT_WAIT, nullptr); + if (!m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + } + + if (!ConnectNamedPipe(m_hPipe, nullptr)) { + return false; + } + + return true; + } + + bool PipeServer::Close() { + if (!m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!CloseHandle(m_hPipe)) { + return false; + } + + m_hPipe = nullptr; + + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + + const DWORD unPID = GetCurrentProcessId(); + const DWORD unTID = GetCurrentThreadId(); + const DWORD64 unCycle = __rdtsc(); + if (_stprintf_s(m_szPipeName, _T("PIPE:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szPipeName, 0, sizeof(m_szPipeName)); + return false; + } + + return true; + } + + bool PipeServer::Send(unsigned char pData[]) { + if (!pData || !m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + DWORD unNumberOfBytesWritten = 0; + if (!WriteFile(m_hPipe, pData, static_cast(m_unBufferSize & 0xFFFFFFFF), &unNumberOfBytesWritten, nullptr)) { + if (GetLastError() == ERROR_NO_DATA) { + return true; + } + + return false; + } + + if (unNumberOfBytesWritten != m_unBufferSize) { + return false; + } + + return true; + } + + bool PipeServer::Receive(unsigned char pData[]) { + if (!pData || !m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + DWORD unNumberOfBytesRead = 0; + if (!ReadFile(m_hPipe, pData, static_cast(m_unBufferSize & 0xFFFFFFFF), &unNumberOfBytesRead, nullptr)) { + if (GetLastError() == ERROR_BROKEN_PIPE) { + return true; + } + + return false; + } + + if (unNumberOfBytesRead != m_unBufferSize) { + return false; + } + + return true; + } + + // ---------------------------------------------------------------- + // PipeClient + // ---------------------------------------------------------------- + + PipeClient::PipeClient(const size_t unBufferSize) { + m_unBufferSize = unBufferSize; + m_hPipe = nullptr; + } + + PipeClient::~PipeClient() { + Close(); + } + + HANDLE PipeClient::GetPipe() const { + return m_hPipe; + } + + bool PipeClient::Open(TCHAR szPipeName[64]) { + if (!szPipeName) { + return false; + } + + TCHAR szPipe[64]; + memset(szPipe, 0, sizeof(szPipe)); + _stprintf_s(szPipe, _T("\\\\.\\pipe\\%s"), szPipeName); + + m_hPipe = CreateFile(szPipe, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, OPEN_EXISTING, NULL, nullptr); + if (!m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + return true; + } + + bool PipeClient::Close() { + if (!m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + if (!CloseHandle(m_hPipe)) { + return false; + } + + m_hPipe = nullptr; + + return true; + } + + bool PipeClient::Send(unsigned char pData[]) { + if (!pData || !m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + DWORD unNumberOfBytesWritten = 0; + if (!WriteFile(m_hPipe, pData, static_cast(m_unBufferSize & 0xFFFFFFFF), &unNumberOfBytesWritten, nullptr)) { + if (GetLastError() == ERROR_NO_DATA) { + return true; + } + + return false; + } + + if (unNumberOfBytesWritten != m_unBufferSize) { + return false; + } + + return true; + } + + bool PipeClient::Receive(unsigned char pData[]) { + if (!pData || !m_hPipe || (m_hPipe == INVALID_HANDLE_VALUE)) { + return false; + } + + DWORD unNumberOfBytesRead = 0; + if (!ReadFile(m_hPipe, pData, static_cast(m_unBufferSize & 0xFFFFFFFF), &unNumberOfBytesRead, nullptr)) { + if (GetLastError() == ERROR_BROKEN_PIPE) { + return true; + } + + return false; + } + + if (unNumberOfBytesRead != m_unBufferSize) { + return false; + } + + return true; + } + } + + // ---------------------------------------------------------------- + // Parallel + // ---------------------------------------------------------------- + + namespace Parallel { + + /* + // ---------------------------------------------------------------- + // Thread Data + // ---------------------------------------------------------------- + + typedef struct _THREAD_DATA { + void* m_pParameter; + } THREAD_DATA, *PTHREAD_DATA; + + // ---------------------------------------------------------------- + // Thread Routine + // ---------------------------------------------------------------- + + DWORD WINAPI ThreadRoutine(PVOID lpThreadParameter) { + auto pTD = reinterpret_cast(lpThreadParameter); + if (!pTD) { + return EXIT_SUCCESS; + } + + auto pThread = static_cast(pTD->m_pParameter); + if (!pThread) { + return EXIT_SUCCESS; + } + + auto pCallback = pThread->GetCallBack(); + if (pCallback) { + pCallback(pThread->GetData()); + } + + return EXIT_SUCCESS; + } + + // ---------------------------------------------------------------- + // Thread + // ---------------------------------------------------------------- + + Thread::Thread() { + m_pCallBack = nullptr; + m_pData = nullptr; + m_hThread = nullptr; + } + + Thread::Thread(const fnThreadCallBack pCallBack) { + m_pCallBack = pCallBack; + m_pData = nullptr; + m_hThread = nullptr; + } + + Thread::Thread(const fnThreadCallBack pCallBack, void* pData) { + m_pCallBack = pCallBack; + m_pData = pData; + m_hThread = nullptr; + } + + Thread::~Thread() { + Join(); + } + + bool Thread::SetCallBack(const fnThreadCallBack pCallBack) { + if (!pCallBack) { + return false; + } + + m_pCallBack = pCallBack; + + return true; + } + + bool Thread::SetData(void* pData) { + if (!pData) { + return false; + } + + m_pData = pData; + + return true; + } + + bool Thread::Start() { + if (!m_pCallBack || (m_hThread && (m_hThread != INVALID_HANDLE_VALUE))) { + return false; + } + + auto pTD = std::make_unique(); + if (!pTD) { + return false; + } + + memset(pTD.get(), 0, sizeof(THREAD_DATA)); + + pTD->m_pParameter = this; + + m_hThread = CreateThread(nullptr, NULL, ThreadRoutine, pTD.get(), NULL, nullptr); + if (!m_hThread || (m_hThread == INVALID_HANDLE_VALUE)) { + return false; + } + + return true; + } + + bool Thread::Join() { + if (!m_pCallBack || !m_hThread || (m_hThread == INVALID_HANDLE_VALUE)) { + return false; + } + + if (WaitForSingleObject(m_hThread, INFINITE) != WAIT_OBJECT_0) { + return false; + } + + return true; + } + + bool Thread::Suspend() { + if (!m_pCallBack || !m_hThread || (m_hThread == INVALID_HANDLE_VALUE)) { + return false; } - return reinterpret_cast(reinterpret_cast(pCO) + PMDtoOffset(pCO, pBCD->m_Where)); + if (SuspendThread(m_hThread) == static_cast(-1)) { + return false; + } + + return true; } - void const* const Object::DynamicCast(void const* const pAddress, const Object* pObject) { - if (!pAddress || !pObject) { - return nullptr; + bool Thread::Resume() { + if (!m_pCallBack || !m_hThread || (m_hThread == INVALID_HANDLE_VALUE)) { + return false; } -#ifdef _M_X64 - return __RTDynamicCast(m_pBaseAddress, pAddress, 0, m_pTypeDescriptor, pObject->GetTypeDescriptor()); -#elif _M_IX86 - return __RTDynamicCast(pAddress, 0, m_pTypeDescriptor, pObject->GetTypeDescriptor()); -#endif - } + if (ResumeThread(m_hThread) == static_cast(-1)) { + return false; + } - const PRTTI_TYPE_DESCRIPTOR Object::GetTypeDescriptor() const { - return m_pTypeDescriptor; + return true; } - const PRTTI_CLASS_HIERARCHY_DESCRIPTOR Object::GetClassHierarchyDescriptor() const { - return m_pClassHierarchyDescriptor; + fnThreadCallBack Thread::GetCallBack() const { + return m_pCallBack; } - const PRTTI_COMPLETE_OBJECT_LOCATOR Object::GetCompleteObject() const { - return m_pCompleteObject; + void* Thread::GetData() const { + return m_pData; } + */ - void** Object::GetVTable() const { - return m_pVTable; - } + // ---------------------------------------------------------------- + // Fiber Data + // ---------------------------------------------------------------- - std::vector>& Object::GetBaseObjects() { - return m_vecBaseClasses; - } + typedef struct _FIBER_DATA { + void* m_pFiberMain; + void* m_pFiber; + void* m_pParameter; + } FIBER_DATA, *PFIBER_DATA; // ---------------------------------------------------------------- - // FindObject + // FiberRoutine // ---------------------------------------------------------------- - std::unique_ptr FindObject(void const* const pBaseAddress, void const* const pAddress, const size_t unSize, char const* const szName, bool bCompleteObject) { - if (!pBaseAddress || !pAddress || !unSize || !szName) { - return nullptr; + void WINAPI FiberRoutine(PVOID lpFiberParameter) { + auto pFD = reinterpret_cast(lpFiberParameter); + if (!pFD) { + return; } - const size_t unNameLength = strnlen_s(szName, 0x1000); - if (!unNameLength || (unSize <= unNameLength)) { - return nullptr; + if (!pFD->m_pFiberMain) { + return; } - void* pReference = const_cast(pAddress); - void* pEndAddress = reinterpret_cast(const_cast(pAddress)) + unSize - unNameLength; - while (pReference && (pReference < pEndAddress)) { - pReference = const_cast(FindData(pReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pReference), reinterpret_cast(szName), unNameLength)); - if (!pReference) { - break; - } - - const auto& pTypeDescriptor = reinterpret_cast(reinterpret_cast(pReference) - sizeof(void*) * 2); - if ((pTypeDescriptor->m_pVFTable < pBaseAddress) || (pTypeDescriptor->m_pVFTable >= pEndAddress)) { - pReference = reinterpret_cast(reinterpret_cast(pReference) + 1); - continue; - } - - void* pTypeDescriptorReference = const_cast(pAddress); - while (pTypeDescriptorReference && (pTypeDescriptorReference < pEndAddress)) { -#ifdef _M_X64 - const size_t unTypeDescriptorOffsetTemp = reinterpret_cast(pTypeDescriptor) - reinterpret_cast(pBaseAddress); - const unsigned int unTypeDescriptorOffset = (*(reinterpret_cast(&unTypeDescriptorOffsetTemp))); - pTypeDescriptorReference = const_cast(FindData(pTypeDescriptorReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pTypeDescriptorReference), reinterpret_cast(&unTypeDescriptorOffset), sizeof(int))); -#elif _M_IX86 - pTypeDescriptorReference = const_cast(FindData(pTypeDescriptorReference, reinterpret_cast(pEndAddress) - reinterpret_cast(pTypeDescriptorReference), reinterpret_cast(&pTypeDescriptor), sizeof(int))); -#endif - if (!pTypeDescriptorReference) { - break; - } - - if (bCompleteObject) { - const auto& pCompleteObjectLocator = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) - sizeof(int) * 3); - if ((pCompleteObjectLocator->m_unSignature != COL_SIG_REV0) && (pCompleteObjectLocator->m_unSignature != COL_SIG_REV1)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } - -#ifdef _M_X64 - if (pCompleteObjectLocator->m_unSignature != COL_SIG_REV0) { - if ((reinterpret_cast(pCompleteObjectLocator) - reinterpret_cast(pBaseAddress)) != pCompleteObjectLocator->m_unSelf) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } - } -#endif - -#ifdef _M_X64 - const auto& pClassHierarchyDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pCompleteObjectLocator->m_unClassHierarchyDescriptor); -#elif _M_IX86 - const auto& pClassHierarchyDescriptor = pCompleteObjectLocator->m_pClassHierarchyDescriptor; -#endif - if ((pClassHierarchyDescriptor < pBaseAddress) || (pClassHierarchyDescriptor >= pEndAddress)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } - - if ((pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV0) && (pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV1)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } - -#ifdef _M_X64 - const auto& pBaseClassArray = reinterpret_cast(reinterpret_cast(pBaseAddress) + pClassHierarchyDescriptor->m_unBaseClassArray); -#elif _M_IX86 - const auto& pBaseClassArray = pClassHierarchyDescriptor->m_pBaseClassArray; -#endif - if ((pBaseClassArray < pBaseAddress) || (pBaseClassArray >= pEndAddress)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } + if (!pFD->m_pFiber) { + SwitchToFiber(pFD->m_pFiberMain); + return; + } - for (unsigned int i = 0; i < pClassHierarchyDescriptor->m_unNumberOfBaseClasses; ++i) { -#ifdef _M_X64 - const auto& pBaseClassDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassArray->m_unBaseClassDescriptors[i]); -#elif _M_IX86 - const auto& pBaseClassDescriptor = pBaseClassArray->m_pBaseClassDescriptors[i]; -#endif - if (!pBaseClassDescriptor) { - break; - } + auto pFiber = static_cast(pFD->m_pParameter); + if (!pFiber) { + SwitchToFiber(pFD->m_pFiberMain); + return; + } -#ifdef _M_X64 - if (reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unTypeDescriptor) == pTypeDescriptor) { -#elif _M_IX86 - if (pBaseClassDescriptor->m_pTypeDescriptor == pTypeDescriptor) { -#endif - void* pCompleteObjectLocatorReference = const_cast(FindData(pAddress, unSize, reinterpret_cast(&pCompleteObjectLocator), sizeof(void*))); - if (!pCompleteObjectLocatorReference) { - return nullptr; - } + auto pCallback = pFiber->GetCallBack(); + if (pCallback) { + pCallback(pFiber->GetData()); + } - return std::make_unique(pBaseAddress, pAddress, unSize, pTypeDescriptor, pClassHierarchyDescriptor, pBaseClassArray, pCompleteObjectLocator, reinterpret_cast(reinterpret_cast(pCompleteObjectLocatorReference) + sizeof(void*))); - } - } - } else { - const auto& pBaseClassDescriptor = reinterpret_cast(pTypeDescriptorReference); -#ifdef _M_X64 - const auto& pClassHierarchyDescriptor = reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unClassHierarchyDescriptor); -#elif _M_IX86 - const auto& pClassHierarchyDescriptor = pBaseClassDescriptor->m_pClassHierarchyDescriptor; -#endif - if ((pClassHierarchyDescriptor < pBaseAddress) || (pClassHierarchyDescriptor >= pEndAddress)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } + SwitchToFiber(pFD->m_pFiberMain); + } - if ((pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV0) && (pClassHierarchyDescriptor->m_unSignature != COL_SIG_REV1)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } + // ---------------------------------------------------------------- + // Fiber + // ---------------------------------------------------------------- -#ifdef _M_X64 - const auto& pBaseClassArray = reinterpret_cast(reinterpret_cast(pBaseAddress) + pClassHierarchyDescriptor->m_unBaseClassArray); -#elif _M_IX86 - const auto& pBaseClassArray = pClassHierarchyDescriptor->m_pBaseClassArray; -#endif - if ((pBaseClassArray < pBaseAddress) || (pBaseClassArray >= pEndAddress)) { - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - continue; - } + Fiber::Fiber() { + m_pCallBack = nullptr; + m_pData = nullptr; + } - for (unsigned int i = 0; i < pClassHierarchyDescriptor->m_unNumberOfBaseClasses; ++i) { -#ifdef _M_X64 - if (reinterpret_cast(reinterpret_cast(pBaseAddress) + pBaseClassDescriptor->m_unTypeDescriptor) == pTypeDescriptor) { -#elif _M_IX86 - if (pBaseClassDescriptor->m_pTypeDescriptor == pTypeDescriptor) { -#endif - return std::make_unique(pBaseAddress, pAddress, unSize, pTypeDescriptor, pClassHierarchyDescriptor, pBaseClassArray, nullptr, nullptr); - } - } - } + Fiber::Fiber(const fnFiberCallBack pCallBack) { + m_pCallBack = pCallBack; + m_pData = nullptr; + } - pTypeDescriptorReference = reinterpret_cast(reinterpret_cast(pTypeDescriptorReference) + 1); - } + Fiber::Fiber(const fnFiberCallBack pCallBack, void* pData) { + m_pCallBack = pCallBack; + m_pData = pData; + } - pReference = reinterpret_cast(reinterpret_cast(pReference) + 1); + bool Fiber::SetCallBack(const fnFiberCallBack pCallBack) { + if (!pCallBack) { + return false; } - return nullptr; - } + m_pCallBack = pCallBack; - std::unique_ptr FindObject(void const* const pAddress, const size_t unSize, char const* const szName, bool bCompleteObject) { - return FindObject(pAddress, pAddress, unSize, szName, bCompleteObject); + return true; } - std::unique_ptr FindObject(const HMODULE hModule, char const* const szName, bool bCompleteObject) { - if (!hModule || !szName) { - return nullptr; + bool Fiber::SetData(void* pData) { + if (!pData) { + return false; } - const auto& pDH = reinterpret_cast(hModule); - const auto& pNTHs = reinterpret_cast(reinterpret_cast(hModule) + pDH->e_lfanew); - const auto& pOH = &(pNTHs->OptionalHeader); + m_pData = pData; - return FindObject(reinterpret_cast(hModule), static_cast(pOH->SizeOfImage) - 1, szName, bCompleteObject); + return true; } - std::unique_ptr FindObjectA(char const* const szModuleName, char const* const szName, bool bCompleteObject) { - if (!szModuleName || !szName) { - return nullptr; + bool Fiber::Switch() { + if (!m_pCallBack) { + return false; } - const auto& hMod = GetModuleHandleA(szModuleName); - if (!hMod) { - return nullptr; + auto pFD = std::make_unique(); + if (!pFD) { + return false; } - return FindObject(hMod, szName, bCompleteObject); - } - + memset(pFD.get(), 0, sizeof(FIBER_DATA)); - std::unique_ptr FindObjectW(wchar_t const* const szModuleName, char const* const szName, bool bCompleteObject) { - if (!szModuleName || !szName) { - return nullptr; + pFD->m_pFiberMain = ConvertThreadToFiber(nullptr); + if (!pFD->m_pFiberMain) { + return false; } - const auto& hMod = GetModuleHandleW(szModuleName); - if (!hMod) { - return nullptr; + pFD->m_pFiber = CreateFiber(NULL, FiberRoutine, pFD.get()); + if (!pFD->m_pFiber) { + return false; } - return FindObject(hMod, szName, bCompleteObject); + pFD->m_pParameter = this; + + SwitchToFiber(pFD->m_pFiber); + DeleteFiber(pFD->m_pFiber); + return true; } -#ifdef _UNICODE - std::unique_ptr FindObject(wchar_t const* const szModuleName, char const* const szName, bool bCompleteObject) { - return FindObjectW(szModuleName, szName, bCompleteObject); + fnFiberCallBack Fiber::GetCallBack() const { + return m_pCallBack; } -#else - std::unique_ptr FindObject(char const* const szModuleName, char const* const szName, bool bCompleteObject) { - return FindObjectA(szModuleName, szName, bCompleteObject); + + void* Fiber::GetData() const { + return m_pData; } -#endif - } + }; // ---------------------------------------------------------------- // Memory @@ -3419,19 +5266,63 @@ namespace Detours { }; // ---------------------------------------------------------------- - // Server + // Shared // ---------------------------------------------------------------- - Server::Server(const size_t unMemorySize, bool bIsGlobal) { - memset(m_szSessionName, 0, sizeof(m_szSessionName)); + Shared::Shared(const size_t unSize) { m_hMap = nullptr; m_pAddress = nullptr; - if (!unMemorySize) { + if (!unSize) { return; } - const auto& pTEB = GetTEB(); +#ifdef _M_X64 + m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast((unSize >> 32) & 0xFFFFFFFFi32), static_cast(unSize & 0xFFFFFFFFi32), nullptr); +#elif _M_IX86 + m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, NULL, static_cast(unSize & 0xFFFFFFFFi32), nullptr); +#endif + if (!m_hMap || (m_hMap == INVALID_HANDLE_VALUE)) { + m_hMap = nullptr; + m_pAddress = nullptr; + return; + } + + m_pAddress = MapViewOfFile(m_hMap, FILE_MAP_WRITE | FILE_MAP_READ | FILE_MAP_EXECUTE, NULL, NULL, NULL); + } + + Shared::~Shared() { + if (m_pAddress) { + UnmapViewOfFile(m_pAddress); + } + + if (m_hMap && (m_hMap != INVALID_HANDLE_VALUE)) { + CloseHandle(m_hMap); + } + } + + HANDLE Shared::GetShared() const { + return m_hMap; + } + + void* Shared::GetAddress() const { + return m_pAddress; + } + + // ---------------------------------------------------------------- + // SharedServer + // ---------------------------------------------------------------- + + SharedServer::SharedServer(const size_t unSize, bool bIsGlobal) { + memset(m_szSharedName, 0, sizeof(m_szSharedName)); + m_hMap = nullptr; + m_pAddress = nullptr; + + if (!unSize) { + return; + } + + auto pTEB = GetTEB(); if (!pTEB) { return; } @@ -3439,32 +5330,32 @@ namespace Detours { const DWORD unPID = pTEB->ClientId.UniqueProcess & 0xFFFFFFFFi32; const DWORD unTID = pTEB->ClientId.UniqueThread & 0xFFFFFFFFi32; const DWORD64 unCycle = __rdtsc(); - if (_stprintf_s(m_szSessionName, _T("GLOBAL:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { - memset(m_szSessionName, 0, sizeof(m_szSessionName)); + if (_stprintf_s(m_szSharedName, _T("SHARED:%08X:%08X:%08X%08X"), 0xFFFFFFFFi32 - unPID, 0xFFFFFFFFi32 - unTID, static_cast(unCycle & 0xFFFFFFFFi32), static_cast((unCycle >> 32) & 0xFFFFFFFFi32)) == -1) { + memset(m_szSharedName, 0, sizeof(m_szSharedName)); return; } - TCHAR szMap[64]; - memset(szMap, 0, sizeof(szMap)); + TCHAR szShared[64]; + memset(szShared, 0, sizeof(szShared)); if (bIsGlobal) { - if (_stprintf_s(szMap, _T("Global\\%s"), m_szSessionName) == -1) { - memset(m_szSessionName, 0, sizeof(m_szSessionName)); + if (_stprintf_s(szShared, _T("Global\\%s"), m_szSharedName) == -1) { + memset(m_szSharedName, 0, sizeof(m_szSharedName)); return; } } else { - if (_stprintf_s(szMap, _T("Local\\%s"), m_szSessionName) == -1) { - memset(m_szSessionName, 0, sizeof(m_szSessionName)); + if (_stprintf_s(szShared, _T("Local\\%s"), m_szSharedName) == -1) { + memset(m_szSharedName, 0, sizeof(m_szSharedName)); return; } } #ifdef _M_X64 - m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast((unMemorySize >> 32) & 0xFFFFFFFFi32), static_cast(unMemorySize & 0xFFFFFFFFi32), szMap); + m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast((unSize >> 32) & 0xFFFFFFFFi32), static_cast(unSize & 0xFFFFFFFFi32), szShared); #elif _M_IX86 - m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, NULL, static_cast(unMemorySize & 0xFFFFFFFFi32), szMap); + m_hMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, NULL, static_cast(unSize & 0xFFFFFFFFi32), szShared); #endif if (!m_hMap || (m_hMap == INVALID_HANDLE_VALUE)) { - memset(m_szSessionName, 0, sizeof(m_szSessionName)); + memset(m_szSharedName, 0, sizeof(m_szSharedName)); m_hMap = nullptr; m_pAddress = nullptr; return; @@ -3473,7 +5364,7 @@ namespace Detours { m_pAddress = MapViewOfFile(m_hMap, FILE_MAP_WRITE | FILE_MAP_READ | FILE_MAP_EXECUTE, NULL, NULL, NULL); } - Server::~Server() { + SharedServer::~SharedServer() { if (m_pAddress) { UnmapViewOfFile(m_pAddress); } @@ -3483,45 +5374,49 @@ namespace Detours { } } - bool Server::GetSessionName(TCHAR szSessionName[64]) { + bool SharedServer::GetSharedName(TCHAR szSharedName[64]) { if (!m_hMap || (m_hMap == INVALID_HANDLE_VALUE)) { return false; } - memcpy(szSessionName, m_szSessionName, sizeof(m_szSessionName)); + memcpy(szSharedName, m_szSharedName, sizeof(m_szSharedName)); return true; } - void* Server::GetAddress() const { + HANDLE SharedServer::GetShared() const { + return m_hMap; + } + + void* SharedServer::GetAddress() const { return m_pAddress; } // ---------------------------------------------------------------- - // Client + // SharedClient // ---------------------------------------------------------------- - Client::Client(TCHAR szSessionName[64], bool bIsGlobal) { + SharedClient::SharedClient(TCHAR szSharedName[64], bool bIsGlobal) { m_hMap = nullptr; m_pAddress = nullptr; - if (!szSessionName) { + if (!szSharedName) { return; } - TCHAR szMap[64]; - memset(szMap, 0, sizeof(szMap)); + TCHAR szShared[64]; + memset(szShared, 0, sizeof(szShared)); if (bIsGlobal) { - if (_stprintf_s(szMap, _T("Global\\%s"), szSessionName) == -1) { + if (_stprintf_s(szShared, _T("Global\\%s"), szSharedName) == -1) { return; } } else { - if (_stprintf_s(szMap, _T("Local\\%s"), szSessionName) == -1) { + if (_stprintf_s(szShared, _T("Local\\%s"), szSharedName) == -1) { return; } } - m_hMap = OpenFileMapping(FILE_MAP_WRITE | FILE_MAP_READ | FILE_MAP_EXECUTE, FALSE, szMap); + m_hMap = OpenFileMapping(FILE_MAP_WRITE | FILE_MAP_READ | FILE_MAP_EXECUTE, FALSE, szShared); if (!m_hMap || (m_hMap == INVALID_HANDLE_VALUE)) { m_hMap = nullptr; return; @@ -3530,7 +5425,7 @@ namespace Detours { m_pAddress = MapViewOfFile(m_hMap, FILE_MAP_WRITE | FILE_MAP_READ | FILE_MAP_EXECUTE, NULL, NULL, NULL); } - Client::~Client() { + SharedClient::~SharedClient() { if (m_pAddress) { UnmapViewOfFile(m_pAddress); } @@ -3540,7 +5435,11 @@ namespace Detours { } } - void* Client::GetAddress() const { + HANDLE SharedClient::GetShared() const { + return m_hMap; + } + + void* SharedClient::GetAddress() const { return m_pAddress; } @@ -81380,104 +83279,6 @@ namespace Detours { namespace Hook { - // ---------------------------------------------------------------- - // Thread Suspender - // ---------------------------------------------------------------- - - ThreadSuspender::~ThreadSuspender() { - ResumeThreads(); - } - - bool ThreadSuspender::SuspendThreads() { - std::lock_guard ThreadsLock(m_ThreadSuspenderMutex); - - const auto& hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); - if (!hSnap || (hSnap == INVALID_HANDLE_VALUE)) { - return false; - } - - THREADENTRY32 te; - memset(&te, 0, sizeof(te)); - - te.dwSize = sizeof(THREADENTRY32); - - if (!Thread32First(hSnap, &te)) { - CloseHandle(hSnap); - return false; - } - - const auto& pTEB = GetTEB(); - if (!pTEB) { - CloseHandle(hSnap); - return false; - } - - do { - if ((pTEB->ClientId.UniqueProcess == te.th32OwnerProcessID) && (pTEB->ClientId.UniqueThread != te.th32ThreadID)) { - HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID); - if (hThread && (hThread != INVALID_HANDLE_VALUE)) { - SuspendThread(hThread); - - CONTEXT ctx; - memset(&ctx, 0, sizeof(CONTEXT)); - - ctx.ContextFlags = CONTEXT_FULL; - - if (!GetThreadContext(hThread, &ctx)) { - ResumeThread(hThread); - CloseHandle(hThread); - continue; - } - - m_Threads.emplace_back(te.th32ThreadID, hThread, ctx); - } - } - } while (Thread32Next(hSnap, &te)); - - CloseHandle(hSnap); - return true; - } - - void ThreadSuspender::ResumeThreads() { - std::lock_guard ThreadsLock(m_ThreadSuspenderMutex); - - for (auto& thread : m_Threads) { - SetThreadContext(thread.m_hHandle, &thread.m_CTX); - ResumeThread(thread.m_hHandle); - CloseHandle(thread.m_hHandle); - } - - m_Threads.clear(); - } - - void ThreadSuspender::FixExecutionAddress(void* pAddress, void* pNewAddress) { - std::lock_guard ThreadsLock(m_ThreadSuspenderMutex); - - for (auto& thread : m_Threads) { -#ifdef _M_X64 - DWORD64 unIP = thread.m_CTX.Rip; -#elif _M_IX86 - DWORD unIP = thread.m_CTX.Eip; -#endif - - if (reinterpret_cast(unIP) == pAddress) { -#ifdef _M_X64 - unIP = reinterpret_cast(pNewAddress); -#elif _M_IX86 - unIP = reinterpret_cast(pNewAddress); -#endif - } - -#ifdef _M_X64 - thread.m_CTX.Rip = unIP; -#elif _M_IX86 - thread.m_CTX.Eip = unIP; -#endif - } - } - - ThreadSuspender g_ThreadSuspender; - // ---------------------------------------------------------------- // Memory Hook // ---------------------------------------------------------------- @@ -82057,12 +83858,12 @@ namespace Detours { } bool InlineHook::Hook(void* pHookAddress, bool bSingleInstructionOnly) { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || m_pTrampoline || !pHookAddress) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82072,7 +83873,7 @@ namespace Detours { unJumpToHookSize = 5; } else { if (bSingleInstructionOnly) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } #ifdef _M_X64 @@ -82095,7 +83896,7 @@ namespace Detours { #elif _M_IX86 if (!RD_SUCCESS(RdDecode(&ins, reinterpret_cast(m_pAddress) + unCopyingSize, RD_CODE_32, RD_DATA_32))) { #endif - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82103,20 +83904,20 @@ namespace Detours { } if (unCopyingSize >= HOOK_INLINE_TRAMPOLINE_SIZE) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } m_pTrampoline = g_HookStorage.Alloc(HOOK_INLINE_TRAMPOLINE_SIZE, m_pAddress); if (!m_pTrampoline) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } if (!Protection(m_pTrampoline, HOOK_INLINE_TRAMPOLINE_SIZE, false).ChangeProtection(PAGE_READWRITE)) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82140,7 +83941,7 @@ namespace Detours { if (unCopyingSize + unJumpFromTrampolineSize > HOOK_INLINE_TRAMPOLINE_SIZE) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82155,7 +83956,7 @@ namespace Detours { #endif g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82179,7 +83980,7 @@ namespace Detours { default: g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } else if (ins.HasRelOffs) { @@ -82199,7 +84000,7 @@ namespace Detours { default: g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } @@ -82259,7 +84060,7 @@ namespace Detours { if (!Protection(m_pTrampoline, HOOK_INLINE_TRAMPOLINE_SIZE, false).ChangeProtection(PAGE_EXECUTE_READ)) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82270,7 +84071,7 @@ namespace Detours { m_unOriginalBytes = 0; g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82282,7 +84083,7 @@ namespace Detours { m_unOriginalBytes = 0; g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82338,27 +84139,27 @@ namespace Detours { JumpToHookProtection.RestoreProtection(); - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } bool InlineHook::UnHook() { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || !m_pTrampoline) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } for (size_t unIndex = 0; unIndex < HOOK_INLINE_TRAMPOLINE_SIZE; ++unIndex) { - g_ThreadSuspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); + g_Suspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); } Protection HookProtection(m_pAddress, m_unOriginalBytes, false); if (!HookProtection.ChangeProtection(PAGE_READWRITE)) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82371,7 +84172,7 @@ namespace Detours { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } @@ -82438,25 +84239,25 @@ namespace Detours { } bool InlineWrapperHook::Hook(void* pHookAddress, bool bSingleInstructionOnly) { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || m_pWrapper || m_pTrampoline || !pHookAddress) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } m_pWrapper = g_HookStorage.Alloc(HOOK_INLINE_WRAPPER_SIZE, m_pAddress); if (!m_pWrapper) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } if (!Protection(m_pWrapper, HOOK_INLINE_WRAPPER_SIZE, false).ChangeProtection(PAGE_READWRITE)) { g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82511,7 +84312,7 @@ namespace Detours { unJumpToWrapperSize = 5; } else { if (bSingleInstructionOnly) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } #ifdef _M_X64 @@ -82534,7 +84335,7 @@ namespace Detours { #elif _M_IX86 if (!RD_SUCCESS(RdDecode(&ins, reinterpret_cast(m_pAddress) + unCopyingSize, RD_CODE_32, RD_DATA_32))) { #endif - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82542,20 +84343,20 @@ namespace Detours { } if (unCopyingSize >= HOOK_INLINE_TRAMPOLINE_SIZE) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } m_pTrampoline = g_HookStorage.Alloc(HOOK_INLINE_TRAMPOLINE_SIZE, m_pAddress); if (!m_pTrampoline) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } if (!Protection(m_pTrampoline, HOOK_INLINE_TRAMPOLINE_SIZE, false).ChangeProtection(PAGE_READWRITE)) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82579,7 +84380,7 @@ namespace Detours { if (unCopyingSize + unJumpFromTrampolineSize > HOOK_INLINE_TRAMPOLINE_SIZE) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82594,7 +84395,7 @@ namespace Detours { #endif g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82618,7 +84419,7 @@ namespace Detours { default: g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } else if (ins.HasRelOffs) { @@ -82638,7 +84439,7 @@ namespace Detours { default: g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } @@ -82698,7 +84499,7 @@ namespace Detours { if (!Protection(m_pTrampoline, HOOK_INLINE_TRAMPOLINE_SIZE, false).ChangeProtection(PAGE_EXECUTE_READ)) { g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82709,7 +84510,7 @@ namespace Detours { m_unOriginalBytes = 0; g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82721,7 +84522,7 @@ namespace Detours { m_unOriginalBytes = 0; g_HookStorage.DeAlloc(m_pTrampoline); m_pTrampoline = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82777,31 +84578,31 @@ namespace Detours { JumpToHookProtection.RestoreProtection(); - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } bool InlineWrapperHook::UnHook() { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || !m_pWrapper || !m_pTrampoline) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } for (size_t unIndex = 0; unIndex < HOOK_INLINE_WRAPPER_SIZE; ++unIndex) { - g_ThreadSuspender.FixExecutionAddress(reinterpret_cast(m_pWrapper) + unIndex, reinterpret_cast(m_pAddress)); + g_Suspender.FixExecutionAddress(reinterpret_cast(m_pWrapper) + unIndex, reinterpret_cast(m_pAddress)); } for (size_t unIndex = 0; unIndex < HOOK_INLINE_TRAMPOLINE_SIZE; ++unIndex) { - g_ThreadSuspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); + g_Suspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); } Protection HookProtection(m_pAddress, m_unOriginalBytes, false); if (!HookProtection.ChangeProtection(PAGE_READWRITE)) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -82816,7 +84617,7 @@ namespace Detours { g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } @@ -82887,25 +84688,25 @@ namespace Detours { } bool RawHook::Hook(const fnRawHookCallBack pCallBack, bool bNative, const unsigned int unReservedStackSize, bool bSingleInstructionOnly) { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || m_pWrapper || m_pTrampoline || !pCallBack) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } m_pWrapper = g_HookStorage.Alloc(HOOK_RAW_WRAPPER_SIZE, m_pAddress); if (!m_pWrapper) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } if (!Protection(m_pWrapper, HOOK_RAW_WRAPPER_SIZE, false).ChangeProtection(PAGE_READWRITE)) { g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -83907,7 +85708,7 @@ namespace Detours { if (bSingleInstructionOnly) { g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } #ifdef _M_X64 @@ -83933,7 +85734,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -83949,7 +85750,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -83959,7 +85760,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84045,7 +85846,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84055,7 +85856,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84090,7 +85891,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84117,7 +85918,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } else if (ins.HasRelOffs) { @@ -84140,7 +85941,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } } @@ -84205,7 +86006,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84219,7 +86020,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84234,7 +86035,7 @@ namespace Detours { m_unFirstInstructionSize = 0; g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84292,31 +86093,31 @@ namespace Detours { JumpToHookProtection.RestoreProtection(); - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } bool RawHook::UnHook() { - if (!g_ThreadSuspender.SuspendThreads()) { + if (!g_Suspender.Suspend()) { return false; } if (!m_bInitialized || !m_pAddress || !m_pWrapper || !m_pTrampoline) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } for (size_t unIndex = 0; unIndex < HOOK_RAW_WRAPPER_SIZE; ++unIndex) { - g_ThreadSuspender.FixExecutionAddress(reinterpret_cast(m_pWrapper) + unIndex, reinterpret_cast(m_pAddress)); + g_Suspender.FixExecutionAddress(reinterpret_cast(m_pWrapper) + unIndex, reinterpret_cast(m_pAddress)); } for (size_t unIndex = 0; unIndex < HOOK_RAW_TRAMPOLINE_SIZE; ++unIndex) { - g_ThreadSuspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); + g_Suspender.FixExecutionAddress(reinterpret_cast(m_pTrampoline) + unIndex, reinterpret_cast(m_pAddress) + unIndex); } Protection HookProtection(m_pAddress, m_unOriginalBytes, false); if (!HookProtection.ChangeProtection(PAGE_READWRITE)) { - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return false; } @@ -84332,7 +86133,7 @@ namespace Detours { g_HookStorage.DeAlloc(m_pWrapper); m_pWrapper = nullptr; - g_ThreadSuspender.ResumeThreads(); + g_Suspender.Resume(); return true; } @@ -84344,120 +86145,4 @@ namespace Detours { return m_unFirstInstructionSize; } } - - // ---------------------------------------------------------------- - // Fibers - // ---------------------------------------------------------------- - - namespace Fibers { - - // ---------------------------------------------------------------- - // FiberRoutine - // ---------------------------------------------------------------- - - void WINAPI FiberRoutine(PVOID lpFiberParameter) { - auto pFD = reinterpret_cast(lpFiberParameter); - if (!pFD) { - return; - } - - if (!pFD->m_pFiberMain) { - return; - } - - if (!pFD->m_pFiber) { - SwitchToFiber(pFD->m_pFiberMain); - return; - } - - Fiber* pFiber = reinterpret_cast(pFD->m_pParameter); - if (!pFiber) { - SwitchToFiber(pFD->m_pFiberMain); - return; - } - - auto pCallback = pFiber->GetCallBack(); - if (pCallback) { - pCallback(pFiber->GetData()); - } - - SwitchToFiber(pFD->m_pFiberMain); - } - - // ---------------------------------------------------------------- - // Fiber - // ---------------------------------------------------------------- - - Fiber::Fiber() { - m_pCallBack = nullptr; - m_pData = nullptr; - } - - Fiber::Fiber(const fnFiberCallBack pCallBack) { - m_pCallBack = pCallBack; - m_pData = nullptr; - } - - Fiber::Fiber(const fnFiberCallBack pCallBack, void* pData) { - m_pCallBack = pCallBack; - m_pData = pData; - } - - bool Fiber::SetCallBack(const fnFiberCallBack pCallBack) { - if (!pCallBack) { - return false; - } - - m_pCallBack = pCallBack; - - return true; - } - - bool Fiber::SetData(void* pData) { - if (!pData) { - return false; - } - - m_pData = pData; - - return true; - } - - bool Fiber::Switch() { - if (!m_pCallBack) { - return false; - } - - auto pFD = std::make_unique(); - if (!pFD) { - return false; - } - - memset(pFD.get(), 0, sizeof(FIBER_DATA)); - - pFD->m_pFiberMain = ConvertThreadToFiber(nullptr); - if (!pFD->m_pFiberMain) { - return false; - } - - pFD->m_pFiber = CreateFiber(NULL, FiberRoutine, pFD.get()); - if (!pFD->m_pFiber) { - return false; - } - - pFD->m_pParameter = this; - - SwitchToFiber(pFD->m_pFiber); - DeleteFiber(pFD->m_pFiber); - return true; - } - - fnFiberCallBack Fiber::GetCallBack() const { - return m_pCallBack; - } - - void* Fiber::GetData() const { - return m_pData; - } - }; } diff --git a/Detours.h b/Detours.h index f9838ae..7df71d6 100644 --- a/Detours.h +++ b/Detours.h @@ -779,6 +779,177 @@ namespace Detours { extern const volatile KUSER_SHARED_DATA& KUserSharedData; + // ---------------------------------------------------------------- + // LDR + // ---------------------------------------------------------------- + + typedef enum _LDR_DDAG_STATE { + LdrModulesMerged = -5, + LdrModulesInitError = -4, + LdrModulesSnapError = -3, + LdrModulesUnloaded = -2, + LdrModulesUnloading = -1, + LdrModulesPlaceHolder = 0, + LdrModulesMapping = 1, + LdrModulesMapped = 2, + LdrModulesWaitingForDependencies = 3, + LdrModulesSnapping = 4, + LdrModulesSnapped = 5, + LdrModulesCondensed = 6, + LdrModulesReadyToInit = 7, + LdrModulesInitializing = 8, + LdrModulesReadyToRun = 9 + } LDR_DDAG_STATE, *PLDR_DDAG_STATE; + + typedef enum _LDR_DLL_LOAD_REASON { + LoadReasonStaticDependency, + LoadReasonStaticForwarderDependency, + LoadReasonDynamicForwarderDependency, + LoadReasonDelayloadDependency, + LoadReasonDynamicLoad, + LoadReasonAsImageLoad, + LoadReasonAsDataLoad, + LoadReasonEnclavePrimary, + LoadReasonEnclaveDependency, + LoadReasonPatchImage, + LoadReasonUnknown = -1 + } LDR_DLL_LOAD_REASON, *PLDR_DLL_LOAD_REASON; + + typedef enum _LDR_HOT_PATCH_STATE { + LdrHotPatchBaseImage, + LdrHotPatchNotApplied, + LdrHotPatchAppliedReverse, + LdrHotPatchAppliedForward, + LdrHotPatchFailedToPatch, + LdrHotPatchStateMax + } LDR_HOT_PATCH_STATE, *PLDR_HOT_PATCH_STATE; + + typedef BOOLEAN(NTAPI* PLDR_INIT_ROUTINE)(PVOID DllHandle, ULONG Reason, PVOID Context); + + typedef struct _LDR_SERVICE_TAG_RECORD { + struct _LDR_SERVICE_TAG_RECORD* Next; + ULONG ServiceTag; + } LDR_SERVICE_TAG_RECORD, *PLDR_SERVICE_TAG_RECORD; + + typedef struct _LDRP_CSLIST { + PSINGLE_LIST_ENTRY Tail; + } LDRP_CSLIST, *PLDRP_CSLIST; + + typedef struct _LDR_DDAG_NODE { + LIST_ENTRY Modules; + PLDR_SERVICE_TAG_RECORD ServiceTagList; + ULONG LoadCount; + ULONG LoadWhileUnloadingCount; + ULONG LowestLink; + union { + LDRP_CSLIST Dependencies; + SINGLE_LIST_ENTRY RemovalLink; + }; + LDRP_CSLIST IncomingDependencies; + LDR_DDAG_STATE State; + SINGLE_LIST_ENTRY CondenseLink; + ULONG PreorderNumber; + } LDR_DDAG_NODE, *PLDR_DDAG_NODE; + + typedef struct _RTL_BALANCED_NODE { + union { + struct _RTL_BALANCED_NODE* Children[2]; + struct { + struct _RTL_BALANCED_NODE* Left; + struct _RTL_BALANCED_NODE* Right; + }; + }; + union { + UCHAR Red : 1; + UCHAR Balance : 2; + ULONG_PTR ParentValue; + }; + } RTL_BALANCED_NODE, *PRTL_BALANCED_NODE; + + typedef struct _RTL_RB_TREE { + PRTL_BALANCED_NODE Root; + PRTL_BALANCED_NODE Min; + } RTL_RB_TREE, *PRTL_RB_TREE; + + typedef struct _UNICODE_STRING { + USHORT Length; + USHORT MaximumLength; + PWCH Buffer; + } UNICODE_STRING, *PUNICODE_STRING; + + typedef struct _LDR_DATA_TABLE_ENTRY { + LIST_ENTRY InLoadOrderLinks; + LIST_ENTRY InMemoryOrderLinks; + union { + LIST_ENTRY InInitializationOrderLinks; + LIST_ENTRY InProgressLinks; + }; + PVOID DllBase; + PLDR_INIT_ROUTINE EntryPoint; + ULONG SizeOfImage; + UNICODE_STRING FullDllName; + UNICODE_STRING BaseDllName; + union { + UCHAR FlagGroup[4]; + ULONG Flags; + struct { + ULONG PackagedBinary : 1; + ULONG MarkedForRemoval : 1; + ULONG ImageDll : 1; + ULONG LoadNotificationsSent : 1; + ULONG TelemetryEntryProcessed : 1; + ULONG ProcessStaticImport : 1; + ULONG InLegacyLists : 1; + ULONG InIndexes : 1; + ULONG ShimDll : 1; + ULONG InExceptionTable : 1; + ULONG ReservedFlags1 : 2; + ULONG LoadInProgress : 1; + ULONG LoadConfigProcessed : 1; + ULONG EntryProcessed : 1; + ULONG ProtectDelayLoad : 1; + ULONG ReservedFlags3 : 2; + ULONG DontCallForThreads : 1; + ULONG ProcessAttachCalled : 1; + ULONG ProcessAttachFailed : 1; + ULONG CorDeferredValidate : 1; + ULONG CorImage : 1; + ULONG DontRelocate : 1; + ULONG CorILOnly : 1; + ULONG ChpeImage : 1; + ULONG ChpeEmulatorImage : 1; + ULONG ReservedFlags5 : 1; + ULONG Redirected : 1; + ULONG ReservedFlags6 : 2; + ULONG CompatDatabaseProcessed : 1; + }; + }; + USHORT ObsoleteLoadCount; + USHORT TlsIndex; + LIST_ENTRY HashLinks; + ULONG TimeDateStamp; + struct _ACTIVATION_CONTEXT* EntryPointActivationContext; + PVOID Lock; + PLDR_DDAG_NODE DdagNode; + LIST_ENTRY NodeModuleLink; + struct _LDRP_LOAD_CONTEXT* LoadContext; + PVOID ParentDllBase; + PVOID SwitchBackContext; + RTL_BALANCED_NODE BaseAddressIndexNode; + RTL_BALANCED_NODE MappingInfoIndexNode; + ULONG_PTR OriginalBase; + LARGE_INTEGER LoadTime; + ULONG BaseNameHashValue; + LDR_DLL_LOAD_REASON LoadReason; + ULONG ImplicitPathOptions; + ULONG ReferenceCount; + ULONG DependentLoadFlags; + UCHAR SigningLevel; + ULONG CheckSum; + PVOID ActivePatchImageBase; + LDR_HOT_PATCH_STATE HotPatchState; + } LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY; + // ---------------------------------------------------------------- // PEB // ---------------------------------------------------------------- @@ -795,12 +966,6 @@ namespace Detours { HANDLE ShutdownThreadId; } PEB_LDR_DATA, *PPEB_LDR_DATA; - typedef struct _UNICODE_STRING { - USHORT Length; - USHORT MaximumLength; - PWCH Buffer; - } UNICODE_STRING, *PUNICODE_STRING; - typedef struct _CURDIR { UNICODE_STRING DosPath; HANDLE Handle; @@ -1219,6 +1384,97 @@ namespace Detours { PTEB GetTEB(); + // ---------------------------------------------------------------- + // LDR + // ---------------------------------------------------------------- + + namespace LDR { + + // ---------------------------------------------------------------- + // List Entry APIs + // ---------------------------------------------------------------- + + void InitializeListHead(PLIST_ENTRY pListHead); + void InsertHeadList(PLIST_ENTRY pListHead, PLIST_ENTRY pEntry); + void InsertTailList(PLIST_ENTRY pListHead, PLIST_ENTRY pEntry); + void RemoveEntryList(PLIST_ENTRY pEntry); + void RemoveHeadList(PLIST_ENTRY pListHead); + void RemoveTailList(PLIST_ENTRY pListHead); + + PLIST_ENTRY GetListHeadFromEntry(PLIST_ENTRY pEntry); + + // ---------------------------------------------------------------- + // GetHeadsOfLists + // ---------------------------------------------------------------- + + bool GetHeadsOfLists(PLIST_ENTRY* pInLoadOrderModuleList, PLIST_ENTRY* pInMemoryOrderModuleList, PLIST_ENTRY* pInInitializationOrderModuleList); + + // ---------------------------------------------------------------- + // FindModuleListEntry + // ---------------------------------------------------------------- + + PLIST_ENTRY FindModuleListEntry(void* pBaseAddress); + PLIST_ENTRY FindModuleListEntry(HMODULE hModule); + PLIST_ENTRY FindModuleListEntryA(const char* szModuleName); + PLIST_ENTRY FindModuleListEntryW(const wchar_t* szModuleName); +#ifdef _UNICODE + PLIST_ENTRY FindModuleListEntry(const wchar_t* szModuleName); +#else + PLIST_ENTRY FindModuleListEntry(const char* szModuleName); +#endif + + // ---------------------------------------------------------------- + // FindModuleDataTableEntry + // ---------------------------------------------------------------- + + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(void* pBaseAddress); + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(HMODULE hModule); + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntryA(const char* szModuleName); + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntryW(const wchar_t* szModuleName); +#ifdef _UNICODE + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(const wchar_t* szModuleName); +#else + PLDR_DATA_TABLE_ENTRY FindModuleDataTableEntry(const char* szModuleName); +#endif + + // ---------------------------------------------------------------- + // LINK_DATA + // ---------------------------------------------------------------- + + typedef struct _LINK_DATA { + PLIST_ENTRY m_pHeadInLoadOrderLinks; + PLIST_ENTRY m_pHeadInMemoryOrderLinks; + PLIST_ENTRY m_pHeadInInitializationOrderLinks; + PLIST_ENTRY m_pHeadHashLinks; + PLIST_ENTRY m_pHeadNodeModuleLink; + PLIST_ENTRY m_pSavedInLoadOrderLinks; + PLIST_ENTRY m_pSavedInMemoryOrderLinks; + PLIST_ENTRY m_pSavedInInitializationOrderLinks; + PLIST_ENTRY m_pSavedHashLinks; + PLIST_ENTRY m_pSavedNodeModuleLink; + } LINK_DATA, *PLINK_DATA; + + // ---------------------------------------------------------------- + // UnLinkModule + // ---------------------------------------------------------------- + + bool UnLinkModule(void* pBaseAddress, PLINK_DATA pLinkData); + bool UnLinkModule(HMODULE hModule, PLINK_DATA pLinkData); + bool UnLinkModuleA(const char* szModuleName, PLINK_DATA pLinkData); + bool UnLinkModuleW(const wchar_t* szModuleName, PLINK_DATA pLinkData); +#ifdef _UNICODE + bool UnLinkModule(const wchar_t* szModuleName, PLINK_DATA pLinkData); +#else + bool UnLinkModule(const char* szModuleName, PLINK_DATA pLinkData); +#endif + + // ---------------------------------------------------------------- + // ReLinkModule + // ---------------------------------------------------------------- + + bool ReLinkModule(LINK_DATA LinkData); + } + // ---------------------------------------------------------------- // Codec // ---------------------------------------------------------------- @@ -1305,120 +1561,120 @@ namespace Detours { // FindSignature (Native) // ---------------------------------------------------------------- - void const* FindSignatureNative(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(const HMODULE hModule, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureNative(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNativeW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #ifdef _UNICODE - void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #else - void const* FindSignatureNative(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureNative(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureNative(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureNative(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #endif // ---------------------------------------------------------------- // FindSignature (SSE2) // ---------------------------------------------------------------- - void const* FindSignatureSSE2(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureSSE2(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #ifdef _UNICODE - void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #else - void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureSSE2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #endif // ---------------------------------------------------------------- // FindSignature (AVX2) // ---------------------------------------------------------------- - void const* FindSignatureAVX2(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX2(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #ifdef _UNICODE - void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #else - void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX2(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #endif // ---------------------------------------------------------------- // FindSignature (AVX512) [AVX512BW] // ---------------------------------------------------------------- - void const* FindSignatureAVX512(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX512(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512A(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512A(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512W(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #ifdef _UNICODE - void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #else - void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureAVX512(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #endif // ---------------------------------------------------------------- // FindSignature (Auto) // ---------------------------------------------------------------- - void const* FindSignature(void const* const pAddress, const size_t unSize, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(const HMODULE hModule, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureA(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignature(void const* const pAddress, const size_t unSize, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(const HMODULE hModule, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(const HMODULE hModule, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(const HMODULE hModule, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureA(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureA(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureA(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureW(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignatureW(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #ifdef _UNICODE - void const* FindSignature(wchar_t const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignature(wchar_t const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(wchar_t const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(wchar_t const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #else - void const* FindSignature(char const* const szModuleName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; - void const* FindSignature(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const size_t unOffset = 0, const unsigned int unHash = 0, const unsigned char unIgnoredByte = 0x2A) noexcept; + void const* FindSignature(char const* const szModuleName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(char const* const szModuleName, const std::array& SectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; + void const* FindSignature(char const* const szModuleName, char const* const szSectionName, char const* const szSignature, const unsigned char unIgnoredByte = '\x2A', const size_t unOffset = 0, const unsigned int unHash = 0) noexcept; #endif // ---------------------------------------------------------------- @@ -1664,6 +1920,447 @@ namespace Detours { #endif } + // ---------------------------------------------------------------- + // Sync + // ---------------------------------------------------------------- + + namespace Sync { + + // ---------------------------------------------------------------- + // Event + // ---------------------------------------------------------------- + + class Event { + public: + Event(bool bManualReset = true, bool bInitialState = false); + ~Event(); + + public: + HANDLE GetEvent() const; + + public: + bool Signal(); + bool Reset(); + bool Pulse(); + + public: + bool Wait(DWORD unMilliseconds = INFINITE); + + private: + HANDLE m_hEvent; + }; + + // ---------------------------------------------------------------- + // EventServer + // ---------------------------------------------------------------- + + class EventServer { + public: + EventServer(bool bIsGlobal = false, bool bManualReset = true, bool bInitialState = false); + ~EventServer(); + + public: + bool GetEventName(TCHAR szEventName[64]); + HANDLE GetEvent() const; + + public: + bool Signal(); + bool Reset(); + bool Pulse(); + + public: + bool Wait(DWORD unMilliseconds = INFINITE); + + private: + TCHAR m_szEventName[64]; + HANDLE m_hEvent; + }; + + // ---------------------------------------------------------------- + // EventClient + // ---------------------------------------------------------------- + + class EventClient { + public: + EventClient(TCHAR szEventName[64], bool bIsGlobal = false); + ~EventClient(); + + public: + HANDLE GetEvent() const; + + public: + bool Signal(); + bool Reset(); + bool Pulse(); + + public: + bool Wait(DWORD unMilliseconds = INFINITE); + + private: + HANDLE m_hEvent; + }; + + // ---------------------------------------------------------------- + // Mutex + // ---------------------------------------------------------------- + + class Mutex { + public: + Mutex(bool bInitialState = false); + ~Mutex(); + + public: + HANDLE GetMutex() const; + + public: + bool Lock(DWORD unMilliseconds = INFINITE); + bool UnLock(); + + private: + HANDLE m_hMutex; + }; + + // ---------------------------------------------------------------- + // MutexServer + // ---------------------------------------------------------------- + + class MutexServer { + public: + MutexServer(bool bIsGlobal = false, bool bInitialState = false); + ~MutexServer(); + + public: + bool GetMutexName(TCHAR szMutexName[64]); + HANDLE GetMutex() const; + + public: + bool Lock(DWORD unMilliseconds = INFINITE); + bool UnLock(); + + private: + TCHAR m_szMutexName[64]; + HANDLE m_hMutex; + }; + + // ---------------------------------------------------------------- + // MutexClient + // ---------------------------------------------------------------- + + class MutexClient { + public: + MutexClient(TCHAR szMutexName[64], bool bIsGlobal = false); + ~MutexClient(); + + public: + HANDLE GetMutex() const; + + public: + bool Lock(DWORD unMilliseconds = INFINITE); + bool UnLock(); + + private: + HANDLE m_hMutex; + }; + + + // ---------------------------------------------------------------- + // Semaphore + // ---------------------------------------------------------------- + + class Semaphore { + public: + Semaphore(LONG nInitialCount = 1, LONG nMaximumCount = 1); + ~Semaphore(); + + public: + HANDLE GetSemaphore() const; + + public: + bool Enter(DWORD unMilliseconds = 0); + bool Leave(LONG nReleaseCount = 1); + + private: + HANDLE m_hSemaphore; + }; + + // ---------------------------------------------------------------- + // SemaphoreServer + // ---------------------------------------------------------------- + + class SemaphoreServer { + public: + SemaphoreServer(bool bIsGlobal = false, LONG nInitialCount = 1, LONG nMaximumCount = 1); + ~SemaphoreServer(); + + public: + bool GetSemaphoreName(TCHAR szSemaphoreName[64]); + HANDLE GetSemaphore() const; + + public: + bool Enter(DWORD unMilliseconds = 0); + bool Leave(LONG nReleaseCount = 1); + + private: + TCHAR m_szSemaphoreName[64]; + HANDLE m_hSemaphore; + }; + + // ---------------------------------------------------------------- + // SemaphoreClient + // ---------------------------------------------------------------- + + class SemaphoreClient { + public: + SemaphoreClient(TCHAR szSemaphoreName[64], bool bIsGlobal = false); + ~SemaphoreClient(); + + public: + HANDLE GetSemaphore() const; + + public: + bool Enter(DWORD unMilliseconds = 0); + bool Leave(LONG nReleaseCount = 1); + + private: + HANDLE m_hSemaphore; + }; + + // ---------------------------------------------------------------- + // CriticalSection + // ---------------------------------------------------------------- + + class CriticalSection { + public: + CriticalSection(); + CriticalSection(DWORD unSpinCount = 0); + ~CriticalSection(); + + public: + PCRITICAL_SECTION GetCriticalSection(); + + public: + void Enter(); + void Leave(); + + private: + CRITICAL_SECTION m_CriticalSection; + }; + + // ---------------------------------------------------------------- + // SRWLock + // ---------------------------------------------------------------- + + class SRWLock { + public: + SRWLock(bool bIsShared = false); + ~SRWLock(); + + public: + bool IsShared() const; + PSRWLOCK GetSRWLock(); + + public: + void Acquire(); + void Release(); + + private: + bool m_bIsShared; + SRWLOCK m_SRWLock; + }; + + // ---------------------------------------------------------------- + // ConditionVariable + // ---------------------------------------------------------------- + + class ConditionVariable { + public: + ConditionVariable(); + ~ConditionVariable(); + + public: + CONDITION_VARIABLE GetConditionVariable() const; + + public: + bool Sleep(CriticalSection* pLock, DWORD unMilliseconds = INFINITE); + bool Sleep(SRWLock* pLock, DWORD unMilliseconds = INFINITE); + void Wake(); + void WakeAll(); + + private: + CONDITION_VARIABLE m_ConditionVariable; + }; + + // ---------------------------------------------------------------- + // Suspender + // ---------------------------------------------------------------- + + class Suspender { + public: + Suspender() = default; + ~Suspender(); + + public: + bool Suspend(); + void Resume(); + void FixExecutionAddress(void* pAddress, void* pNewAddress); + + private: + typedef struct _SUSPENDER_DATA { + _SUSPENDER_DATA(DWORD unThreadID, HANDLE hHandle, CONTEXT CTX) { + m_unThreadID = unThreadID; + m_hHandle = hHandle; + m_CTX = CTX; + } + + DWORD m_unThreadID; + HANDLE m_hHandle; + CONTEXT m_CTX; + } SUSPENDER_DATA, *PSUSPENDER_DATA; + + std::deque m_Threads; + Mutex m_Mutex; + }; + + extern Suspender g_Suspender; + } + + // ---------------------------------------------------------------- + // Pipe + // ---------------------------------------------------------------- + + namespace Pipe { + + // ---------------------------------------------------------------- + // PipeServer + // ---------------------------------------------------------------- + + class PipeServer { + public: + PipeServer(const size_t unBufferSize); + ~PipeServer(); + + public: + bool GetPipeName(TCHAR szPipeName[64]); + HANDLE GetPipe() const; + + public: + bool Open(); + bool Close(); + + public: + bool Send(unsigned char pData[]); + bool Receive(unsigned char pData[]); + + private: + size_t m_unBufferSize; + TCHAR m_szPipeName[64]; + HANDLE m_hPipe; + }; + + // ---------------------------------------------------------------- + // PipeClient + // ---------------------------------------------------------------- + + class PipeClient { + public: + PipeClient(const size_t unBufferSize); + ~PipeClient(); + + public: + HANDLE GetPipe() const; + + public: + bool Open(TCHAR szPipeName[64]); + bool Close(); + + public: + bool Send(unsigned char pData[]); + bool Receive(unsigned char pData[]); + + private: + size_t m_unBufferSize; + HANDLE m_hPipe; + }; + } + + // ---------------------------------------------------------------- + // Parallel + // ---------------------------------------------------------------- + + namespace Parallel { + + /* + // ---------------------------------------------------------------- + // Thread CallBack + // ---------------------------------------------------------------- + + using fnThreadCallBack = void(*)(void* pData); + + // ---------------------------------------------------------------- + // Thread + // ---------------------------------------------------------------- + + class Thread { + public: + Thread(); + Thread(const fnThreadCallBack pCallBack); + Thread(const fnThreadCallBack pCallBack, void* pData); + ~Thread(); + + public: + bool SetCallBack(const fnThreadCallBack pCallBack); + bool SetData(void* pData); + + public: + bool Start(); + bool Join(); + bool Suspend(); + bool Resume(); + + public: + fnThreadCallBack GetCallBack() const; + void* GetData() const; + + private: + fnThreadCallBack m_pCallBack; + void* m_pData; + HANDLE m_hThread; + }; + */ + + // ---------------------------------------------------------------- + // Fiber CallBack + // ---------------------------------------------------------------- + + using fnFiberCallBack = void(*)(void* pData); + + // ---------------------------------------------------------------- + // Fiber + // ---------------------------------------------------------------- + + class Fiber { + public: + Fiber(); + Fiber(const fnFiberCallBack pCallBack); + Fiber(const fnFiberCallBack pCallBack, void* pData); + + public: + bool SetCallBack(const fnFiberCallBack pCallBack); + bool SetData(void* pData); + + public: + bool Switch(); + + public: + fnFiberCallBack GetCallBack() const; + void* GetData() const; + + private: + fnFiberCallBack m_pCallBack; + void* m_pData; + }; + }; + // ---------------------------------------------------------------- // Memory // ---------------------------------------------------------------- @@ -1671,34 +2368,54 @@ namespace Detours { namespace Memory { // ---------------------------------------------------------------- - // Server + // Shared // ---------------------------------------------------------------- - class Server { + class Shared { public: - Server(const size_t unMemorySize, bool bIsGlobal = false); - ~Server(); + Shared(const size_t unSize); + ~Shared(); public: - bool GetSessionName(TCHAR szSessionName[64]); + HANDLE GetShared() const; void* GetAddress() const; private: - TCHAR m_szSessionName[64]; HANDLE m_hMap; void* m_pAddress; }; // ---------------------------------------------------------------- - // Client + // SharedServer // ---------------------------------------------------------------- - class Client { + class SharedServer { public: - Client(TCHAR szSessionName[64], bool bIsGlobal = false); - ~Client(); + SharedServer(const size_t unSize, bool bIsGlobal = false); + ~SharedServer(); public: + bool GetSharedName(TCHAR szSharedName[64]); + HANDLE GetShared() const; + void* GetAddress() const; + + private: + TCHAR m_szSharedName[64]; + HANDLE m_hMap; + void* m_pAddress; + }; + + // ---------------------------------------------------------------- + // SharedClient + // ---------------------------------------------------------------- + + class SharedClient { + public: + SharedClient(TCHAR szSharedName[64], bool bIsGlobal = false); + ~SharedClient(); + + public: + HANDLE GetShared() const; void* GetAddress() const; private: @@ -4832,43 +5549,6 @@ namespace Detours { namespace Hook { - // ---------------------------------------------------------------- - // Thread Suspender Context - // ---------------------------------------------------------------- - - typedef struct _THREAD_SUSPENDER_DATA { - _THREAD_SUSPENDER_DATA(DWORD unThreadID, HANDLE hHandle, CONTEXT CTX) { - m_unThreadID = unThreadID; - m_hHandle = hHandle; - m_CTX = CTX; - } - - DWORD m_unThreadID; - HANDLE m_hHandle; - CONTEXT m_CTX; - } THREAD_SUSPENDER_DATA, *PTHREAD_SUSPENDER_DATA; - - // ---------------------------------------------------------------- - // Thread Suspender - // ---------------------------------------------------------------- - - class ThreadSuspender { - public: - ThreadSuspender() = default; - ~ThreadSuspender(); - - public: - bool SuspendThreads(); - void ResumeThreads(); - void FixExecutionAddress(void* pAddress, void* pNewAddress); - - private: - std::deque m_Threads; - std::mutex m_ThreadSuspenderMutex; - }; - - extern ThreadSuspender g_ThreadSuspender; - // ---------------------------------------------------------------- // Memory Hook CallBack // ---------------------------------------------------------------- @@ -5084,6 +5764,10 @@ namespace Detours { return Value; } + inline void* GetAddress() const { + return m_pAddress; + } + private: void* m_pAddress; } RAW_CONTEXT_STACK, *PRAW_CONTEXT_STACK; @@ -5843,53 +6527,6 @@ namespace Detours { std::unique_ptr m_pOriginalBytes; }; } - - // ---------------------------------------------------------------- - // Fibers - // ---------------------------------------------------------------- - - namespace Fibers { - - // ---------------------------------------------------------------- - // Fiber Data - // ---------------------------------------------------------------- - - typedef struct _FIBER_DATA { - void* m_pFiberMain; - void* m_pFiber; - void* m_pParameter; - } FIBER_DATA, *PFIBER_DATA; - - // ---------------------------------------------------------------- - // Fiber CallBack - // ---------------------------------------------------------------- - - using fnFiberCallBack = void(*)(void* pData); - - // ---------------------------------------------------------------- - // Fiber - // ---------------------------------------------------------------- - - class Fiber { - public: - Fiber(); - Fiber(const fnFiberCallBack pCallBack); - Fiber(const fnFiberCallBack pCallBack, void* pData); - - public: - bool SetCallBack(const fnFiberCallBack pCallBack); - bool SetData(void* pData); - bool Switch(); - - public: - fnFiberCallBack GetCallBack() const; - void* GetData() const; - - private: - fnFiberCallBack m_pCallBack; - void* m_pData; - }; - }; } #pragma warning(pop) diff --git a/main.cpp b/main.cpp index ced24bf..2be740d 100644 --- a/main.cpp +++ b/main.cpp @@ -356,7 +356,7 @@ void TestFindData() { _tprintf_s(_T("TEST - %s\n"), Detours::Scan::FindDataAVX2(pEndArray3, sizeof(pEndArray3), reinterpret_cast("\xDE\xED"), 2) == pEndArray3 + 61 ? _T("OK") : _T("FAIL")); } -int TestMode() { +int TestScan() { _tprintf_s(_T("FindSignature Test\n")); TestFindSignature(); @@ -367,7 +367,7 @@ int TestMode() { return 0; } -int BenchmarkMode() { +int BenchmarkScan() { auto pRandomData = std::make_unique(0x800000); // 8 MiB if (!pRandomData) { return -1; @@ -511,6 +511,18 @@ void ProcessMessage(BaseMessage* pMessage) { } } +void OnThread(void* pData) { + UNREFERENCED_PARAMETER(pData); + _tprintf_s(_T("[OnThread] Started!\n")); + Sleep(5000); +} + +void OnFiber(void* pData) { + UNREFERENCED_PARAMETER(pData); + _tprintf_s(_T("[OnFiber] Started!\n")); + Sleep(5000); +} + bool OnException(const EXCEPTION_RECORD& Exception, const PCONTEXT pCTX) { if (Exception.ExceptionCode != EXCEPTION_ACCESS_VIOLATION) { return false; @@ -568,7 +580,7 @@ bool OnException(const EXCEPTION_RECORD& Exception, const PCONTEXT pCTX) { } bool Sleep_MemoryHook(const std::unique_ptr& pHook, const PCONTEXT pCTX) { - _CRT_UNUSED(pHook); + UNREFERENCED_PARAMETER(pHook); _tprintf_s(_T("[Sleep_MemoryHook] Called!\n")); #ifdef _M_X64 @@ -582,7 +594,7 @@ bool Sleep_MemoryHook(const std::unique_ptr& pHook, c } bool InterruptHook(const std::unique_ptr& pHook, const PCONTEXT pCTX) { - _CRT_UNUSED(pHook); + UNREFERENCED_PARAMETER(pHook); _tprintf_s(_T("[InterruptHook] Called `int 0x%02X`\n"), pHook->GetInterrupt()); #ifdef _M_X64 @@ -1145,7 +1157,7 @@ void Sleep_RawHookFiber(void* pData) { } -Detours::Fibers::Fiber SleepRawHookFiber(Sleep_RawHookFiber); +Detours::Parallel::Fiber SleepRawHookFiber(Sleep_RawHookFiber); #ifdef _M_X64 bool __fastcall Sleep_RawHook(Detours::Hook::PRAW_CONTEXT pCTX) { #elif _M_IX86 @@ -1286,17 +1298,241 @@ int _tmain(int nArguments, PTCHAR* pArguments) { for (int i = 0; i < nArguments; ++i) { PTCHAR pArgument = pArguments[i]; - if (_tcscmp(pArgument, _T("/test")) == 0) { - return TestMode(); + if (_tcscmp(pArgument, _T("/test-scan")) == 0) { + return TestScan(); + } + + if (_tcscmp(pArgument, _T("/benchmark-scan")) == 0) { + return BenchmarkScan(); } - if (_tcscmp(pArgument, _T("/benchmark")) == 0) { - return BenchmarkMode(); + if (_tcscmp(pArgument, _T("/event-server")) == 0) { + Detours::Sync::EventServer event; + + TCHAR szEventName[64]; + if (!event.GetEventName(szEventName)) { + return -1; + } + + _tprintf_s(_T("EventServer: %s\n"), szEventName); + + if (!event.Wait()) { + return -1; + } + + _tprintf_s(_T("Signaled\n")); + + return 0; + } + + if ((nArguments > 2) && (_tcscmp(pArgument, _T("/event-client")) == 0)) { + PTCHAR pEventName = pArguments[i + 1]; + if (_tcslen(pEventName) != 40) { + return -1; + } + + _tprintf_s(_T("Connecting to `%s`\n"), pEventName); + + Detours::Sync::EventClient event(pEventName); + + if (!event.Pulse()) { + return -1; + } + + _tprintf_s(_T("Signaled\n")); + + return 0; + } + + if (_tcscmp(pArgument, _T("/mutex-server")) == 0) { + Detours::Sync::MutexServer mutex; + + TCHAR szMutexName[64]; + if (!mutex.GetMutexName(szMutexName)) { + return -1; + } + + _tprintf_s(_T("MutexServer: %s\n"), szMutexName); + + if (!mutex.Lock()) { + return -1; + } + + _tprintf_s(_T("Locked\n")); + + Sleep(30000); + + if (!mutex.UnLock()) { + return -1; + } + + _tprintf_s(_T("UnLocked\n")); + + return 0; } - if (_tcscmp(pArgument, _T("/sv")) == 0) { - Detours::Memory::Server sv(GetLargePageMinimum()); - PSHARED_MEMORY pMemory = reinterpret_cast(sv.GetAddress()); + if ((nArguments > 2) && (_tcscmp(pArgument, _T("/mutex-client")) == 0)) { + PTCHAR pMutexName = pArguments[i + 1]; + if (_tcslen(pMutexName) != 40) { + return -1; + } + + _tprintf_s(_T("Connecting to `%s`\n"), pMutexName); + + Detours::Sync::MutexClient mutex(pMutexName); + + if (!mutex.Lock()) { + return -1; + } + + _tprintf_s(_T("Locked\n")); + + Sleep(30000); + + if (!mutex.UnLock()) { + return -1; + } + + _tprintf_s(_T("UnLocked\n")); + + return 0; + } + + if (_tcscmp(pArgument, _T("/semaphore-server")) == 0) { + Detours::Sync::SemaphoreServer semaphore(false, 2, 2); + + TCHAR szSemaphoreName[64]; + if (!semaphore.GetSemaphoreName(szSemaphoreName)) { + return -1; + } + + _tprintf_s(_T("SemaphoreServer: %s\n"), szSemaphoreName); + + if (!semaphore.Enter()) { + return -1; + } + + _tprintf_s(_T("Entered\n")); + + Sleep(30000); + + if (!semaphore.Leave()) { + return -1; + } + + _tprintf_s(_T("Leaved\n")); + + return 0; + } + + if ((nArguments > 2) && (_tcscmp(pArgument, _T("/semaphore-client")) == 0)) { + PTCHAR pSemaphoreName = pArguments[i + 1]; + if (_tcslen(pSemaphoreName) != 44) { + return -1; + } + + _tprintf_s(_T("Connecting to `%s`\n"), pSemaphoreName); + + Detours::Sync::SemaphoreClient semaphore(pSemaphoreName); + + if (!semaphore.Enter()) { + return -1; + } + + _tprintf_s(_T("Entered\n")); + + Sleep(30000); + + if (!semaphore.Leave()) { + return -1; + } + + _tprintf_s(_T("Leaved\n")); + + return 0; + } + + if (_tcscmp(pArgument, _T("/pipe-server")) == 0) { + Detours::Pipe::PipeServer pipe(0x1000); + + TCHAR szPipeName[64]; + if (!pipe.GetPipeName(szPipeName)) { + return -1; + } + + _tprintf_s(_T("PipeServer: %s\n"), szPipeName); + + if (!pipe.Open()) { + return -1; + } + + unsigned char pData[0x1000]; + memset(pData, 0, sizeof(pData)); + + pData[0] = 1; + + if (!pipe.Send(pData)) { + return -1; + } + + if (!pipe.Receive(pData)) { + return -1; + } + + if (pData[0] == 0) { + _tprintf_s(_T("Received 0\n")); + } + + if (!pipe.Close()) { + return -1; + } + + return 0; + } + + if ((nArguments > 2) && (_tcscmp(pArgument, _T("/pipe-client")) == 0)) { + PTCHAR pPipeName = pArguments[i + 1]; + if (_tcslen(pPipeName) != 39) { + return -1; + } + + _tprintf_s(_T("Connecting to `%s`\n"), pPipeName); + + Detours::Pipe::PipeClient pipe(0x1000); + + if (!pipe.Open(pPipeName)) { + return -1; + } + + unsigned char pData[0x1000]; + memset(pData, 0, sizeof(pData)); + + if (!pipe.Receive(pData)) { + return -1; + } + + if (pData[0] != 1) { + return -1; + } + + pData[0] = 0; + + if (!pipe.Send(pData)) { + return -1; + } + + _tprintf_s(_T("Send 0\n")); + + if (!pipe.Close()) { + return -1; + } + + return 0; + } + + if (_tcscmp(pArgument, _T("/shared-server")) == 0) { + Detours::Memory::SharedServer shsv(GetLargePageMinimum()); + PSHARED_MEMORY pMemory = reinterpret_cast(shsv.GetAddress()); if (!pMemory) { return -1; } @@ -1307,12 +1543,12 @@ int _tmain(int nArguments, PTCHAR* pArguments) { _tprintf_s(_T("Memory: 0x%08X\n"), reinterpret_cast(pMemory)); #endif - TCHAR szSessionName[64]; - if (!sv.GetSessionName(szSessionName)) { + TCHAR szSharedName[64]; + if (!shsv.GetSharedName(szSharedName)) { return -1; } - _tprintf_s(_T("Server Session: %s\n"), szSessionName); + _tprintf_s(_T("SharedServer: %s\n"), szSharedName); while (!pMemory->m_bStop) { pMemory->m_unTick = GetTickCount64() & 0xFFFFFFFFi32; @@ -1324,16 +1560,16 @@ int _tmain(int nArguments, PTCHAR* pArguments) { return 0; } - if ((nArguments > 2) && (_tcscmp(pArgument, _T("/cl")) == 0)) { - PTCHAR pSessionName = pArguments[i + 1]; - if (_tcslen(pSessionName) != 41) { + if ((nArguments > 2) && (_tcscmp(pArgument, _T("/shared-client")) == 0)) { + PTCHAR pSharedName = pArguments[i + 1]; + if (_tcslen(pSharedName) != 41) { return -1; } - _tprintf_s(_T("Connecting to `%s`\n"), pSessionName); + _tprintf_s(_T("Connecting to `%s`\n"), pSharedName); - Detours::Memory::Client cl(pSessionName); - PSHARED_MEMORY pMemory = reinterpret_cast(cl.GetAddress()); + Detours::Memory::SharedClient shcl(pSharedName); + PSHARED_MEMORY pMemory = reinterpret_cast(shcl.GetAddress()); if (!pMemory) { return -1; } @@ -1426,6 +1662,41 @@ int _tmain(int nArguments, PTCHAR* pArguments) { _tprintf_s(_T("\n")); + // ---------------------------------------------------------------- + // LDR Example + // ---------------------------------------------------------------- + + _tprintf_s(_T("LDR Example\n\n")); + +#ifdef _M_X64 + _tprintf_s(_T("kernel32.dll = 0x%016llX\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#elif _M_IX86 + _tprintf_s(_T("kernel32.dll = 0x%08X\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#endif + + Detours::LDR::LINK_DATA ld; + if (Detours::LDR::UnLinkModule(_T("kernel32"), &ld)) { + _tprintf_s(_T("UnLinked\n")); + } + +#ifdef _M_X64 + _tprintf_s(_T("kernel32.dll = 0x%016llX\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#elif _M_IX86 + _tprintf_s(_T("kernel32.dll = 0x%08X\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#endif + + if (Detours::LDR::ReLinkModule(ld)) { + _tprintf_s(_T("ReLinked\n")); + } + +#ifdef _M_X64 + _tprintf_s(_T("kernel32.dll = 0x%016llX\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#elif _M_IX86 + _tprintf_s(_T("kernel32.dll = 0x%08X\n"), reinterpret_cast(GetModuleHandle(_T("kernel32.dll")))); +#endif + + _tprintf_s(_T("\n")); + // ---------------------------------------------------------------- // Codec Example // ---------------------------------------------------------------- @@ -1535,9 +1806,9 @@ int _tmain(int nArguments, PTCHAR* pArguments) { } #ifdef _M_X64 - _tprintf_s(_T("FindSignature(...) = 0x%016llX\n"), reinterpret_cast(Detours::Scan::FindSignature(_T("ntdll.dll"), { '.', 't', 'e', 'x', 't', 0, 0, 0 }, "\x48\x8B\x41\x2A\x33\xD2\x4C\x8B\xC1\x48\x85\xC0\x75", 0, 0x20C2003D))); + _tprintf_s(_T("FindSignature(...) = 0x%016llX\n"), reinterpret_cast(Detours::Scan::FindSignature(_T("ntdll.dll"), { '.', 't', 'e', 'x', 't', 0, 0, 0 }, "\x48\x8B\x41\x2A\x33\xD2\x4C\x8B\xC1\x48\x85\xC0\x75", '\x2A', 0, 0x20C2003D))); #elif _M_IX86 - _tprintf_s(_T("FindSignature(...) = 0x%08X\n"), reinterpret_cast(Detours::Scan::FindSignature(_T("ntdll.dll"), { '.', 't', 'e', 'x', 't', 0, 0, 0 }, "\x8B\xD1\x8B\x42", 0, 0xF3780028))); + _tprintf_s(_T("FindSignature(...) = 0x%08X\n"), reinterpret_cast(Detours::Scan::FindSignature(_T("ntdll.dll"), { '.', 't', 'e', 'x', 't', 0, 0, 0 }, "\x8B\xD1\x8B\x42", '\x2A', 0, 0xF3780028))); #endif #ifdef _M_X64 @@ -1643,6 +1914,22 @@ int _tmain(int nArguments, PTCHAR* pArguments) { _tprintf_s(_T("\n")); + // ---------------------------------------------------------------- + // Parallel Example + // ---------------------------------------------------------------- + + _tprintf_s(_T("Parallel Example\n\n")); + + //Detours::Parallel::Thread DetoursThread(OnThread); + Detours::Parallel::Fiber DetoursFiber(OnFiber); + + //_tprintf_s(_T("DetoursThread.Start() = %d\n"), DetoursThread.Start()); + //_tprintf_s(_T("DetoursThread.Join() = %d\n"), DetoursThread.Join()); + + _tprintf_s(_T("DetoursFiber.Switch() = %d\n"), DetoursFiber.Switch()); + + _tprintf_s(_T("\n")); + // ---------------------------------------------------------------- // Memory Example // ----------------------------------------------------------------