From 87ffc28a69c2566917b207ed21af9b96a018e6ed Mon Sep 17 00:00:00 2001 From: Ren Date: Fri, 9 Feb 2024 19:13:15 +0700 Subject: [PATCH] Minor changes --- Detours.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- Detours.h | 12 ++++++++++++ main.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/Detours.cpp b/Detours.cpp index 2998eef..2c4528b 100644 --- a/Detours.cpp +++ b/Detours.cpp @@ -473,6 +473,46 @@ namespace Detours { namespace Codec { + // ---------------------------------------------------------------- + // UpperCase + // ---------------------------------------------------------------- + + bool UpperCase(char szBuffer[], const size_t unSize) { + if (!szBuffer || !unSize) { + return false; + } + + for (size_t i = 0; i < unSize; ++i) { + const unsigned char unX = szBuffer[i]; + + if ((unX >= 'a') && (unX <= 'z')) { + szBuffer[i] = unX & ~0x20; + } + } + + return true; + } + + // ---------------------------------------------------------------- + // LowerCase + // ---------------------------------------------------------------- + + bool LowerCase(char szBuffer[], const size_t unSize) { + if (!szBuffer || !unSize) { + return false; + } + + for (size_t i = 0; i < unSize; ++i) { + const unsigned char unX = szBuffer[i]; + + if ((unX >= 'A') && (unX <= 'Z')) { + szBuffer[i] = unX | 0x20; + } + } + + return true; + } + // ---------------------------------------------------------------- // Encode // ---------------------------------------------------------------- @@ -921,13 +961,17 @@ namespace Detours { PIMAGE_POGO_BLOCK pBlock = pPI->m_pBlocks; while (pBlock->m_unRVA != 0) { - const size_t unNameLength = strnlen_s(pBlock->m_pName, 0x1000) + 1; // FIXME: Unsafe. - size_t unBlockSize = sizeof(DWORD) * 2 + unNameLength; + const size_t unNameLength = strnlen_s(pBlock->m_pName, 0x7FF); + if (unNameLength == 0x7FF) { + break; + } + + size_t unBlockSize = sizeof(DWORD) * 2 + unNameLength + 1; if (unBlockSize & 3) { unBlockSize += (4 - (unBlockSize & 3)); } - if (strncmp(szSectionName, pBlock->m_pName, 0x1000) == 0) { // FIXME: Unsafe. + if (strncmp(szSectionName, pBlock->m_pName, 0x7FF) == 0) { if (pAddress) { *pAddress = reinterpret_cast(reinterpret_cast(hModule) + pBlock->m_unRVA); } diff --git a/Detours.h b/Detours.h index f6e2d80..6b2fe1b 100644 --- a/Detours.h +++ b/Detours.h @@ -1460,6 +1460,18 @@ namespace Detours { namespace Codec { + // ---------------------------------------------------------------- + // UpperCase + // ---------------------------------------------------------------- + + bool UpperCase(char szBuffer[], const size_t unSize); + + // ---------------------------------------------------------------- + // LowerCase + // ---------------------------------------------------------------- + + bool LowerCase(char szBuffer[], const size_t unSize); + // ---------------------------------------------------------------- // Encode // ---------------------------------------------------------------- diff --git a/main.cpp b/main.cpp index 5d6f795..e91bc04 100644 --- a/main.cpp +++ b/main.cpp @@ -1707,6 +1707,32 @@ int _tmain(int nArguments, PTCHAR* pArguments) { _tprintf_s(_T("Codec Example\n\n")); + char* szHelloWorld = _strdup("Hello, World!"); + if (szHelloWorld) { + const size_t unSize = strnlen(szHelloWorld, 0x7FF); + if (unSize != 0x7FF) { +#ifdef _UNICODE + if (Detours::Codec::UpperCase(szHelloWorld, unSize)) { + _tprintf_s(_T("UpperCase: `%hs`\n"), szHelloWorld); + } + + if (Detours::Codec::LowerCase(szHelloWorld, unSize)) { + _tprintf_s(_T("LowerCase: `%hs`\n"), szHelloWorld); + } +#else + if (Detours::Codec::UpperCase(szHelloWorld, unSize)) { + _tprintf_s(_T("UpperCase: `%s`\n"), szHelloWorld); + } + + if (Detours::Codec::LowerCase(szHelloWorld, unSize)) { + _tprintf_s(_T("LowerCase: `%s`\n"), szHelloWorld); + } +#endif + } + + free(szHelloWorld); + } + int nEncodeSize = Detours::Codec::Encode(CP_UTF8, "Hello, World!"); if (nEncodeSize > 0) { HANDLE hHeap = GetProcessHeap();