Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RenardDev committed Feb 9, 2024
1 parent 4ccf933 commit 87ffc28
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
50 changes: 47 additions & 3 deletions Detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ----------------------------------------------------------------
Expand Down Expand Up @@ -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<void*>(reinterpret_cast<char*>(hModule) + pBlock->m_unRVA);
}
Expand Down
12 changes: 12 additions & 0 deletions Detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ----------------------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 87ffc28

Please sign in to comment.