diff --git a/src/public/tier0/utility.h b/src/public/tier0/utility.h index 64a981fb..08d90c02 100644 --- a/src/public/tier0/utility.h +++ b/src/public/tier0/utility.h @@ -69,9 +69,9 @@ void FourCCToString(FourCCString_t& buf, const int n); ///////////////////////////////////////////////////////////////////////////// // Bytes -vector StringToBytes(const char* const szInput, const bool bNullTerminator); +vector StringToBytes(const char* const szInput, const bool bNullTerminator); pair, string> StringToMaskedBytes(const char* const szInput, const bool bNullTerminator); -vector PatternToBytes(const char* const szInput); +vector PatternToBytes(const char* const szInput); pair, string> PatternToMaskedBytes(const char* const szInput); vector IntToDigits(int iValue); diff --git a/src/tier0/memaddr.cpp b/src/tier0/memaddr.cpp index 88042128..076ad45e 100644 --- a/src/tier0/memaddr.cpp +++ b/src/tier0/memaddr.cpp @@ -78,22 +78,24 @@ CMemory CMemory::FindPattern(const char* szPattern, const Direction searchDirect { uint8_t* pScanBytes = reinterpret_cast(ptr); // Get the base of the module. - const vector PatternBytes = PatternToBytes(szPattern); // Convert our pattern to a byte array. - const pair bytesInfo = std::make_pair(PatternBytes.size(), PatternBytes.data()); // Get the size and data of our bytes. + const vector PatternBytes = PatternToBytes(szPattern); // Convert our pattern to a byte array. + const pair bytesInfo = std::make_pair(PatternBytes.size(), PatternBytes.data()); // Get the size and data of our bytes. + ptrdiff_t occurrences = 0; for (long i = 01; i < opCodesToScan + bytesInfo.first; i++) { bool bFound = true; - int nMemOffset = searchDirect == Direction::DOWN ? i : -i; + const int nMemOffset = searchDirect == Direction::DOWN ? i : -i; - for (DWORD j = 0ul; j < bytesInfo.first; j++) + for (size_t j = 0ull; j < bytesInfo.first; j++) { // If either the current byte equals to the byte in our pattern or our current byte in the pattern is a wildcard // our if clause will be false. uint8_t* const pCurrentAddr = (pScanBytes + nMemOffset + j); _mm_prefetch(reinterpret_cast(pCurrentAddr + 64), _MM_HINT_T0); // precache some data in L1. - if (*pCurrentAddr != bytesInfo.second[j] && bytesInfo.second[j] != -1) + + if (*pCurrentAddr != bytesInfo.second[j] && bytesInfo.second[j] != 0xffff) { bFound = false; break; @@ -125,8 +127,9 @@ CMemory CMemory::FindPatternSelf(const char* szPattern, const Direction searchDi { uint8_t* pScanBytes = reinterpret_cast(ptr); // Get the base of the module. - const vector PatternBytes = PatternToBytes(szPattern); // Convert our pattern to a byte array. - const pair bytesInfo = std::make_pair(PatternBytes.size(), PatternBytes.data()); // Get the size and data of our bytes. + const vector PatternBytes = PatternToBytes(szPattern); // Convert our pattern to a byte array. + const pair bytesInfo = std::make_pair(PatternBytes.size(), PatternBytes.data()); // Get the size and data of our bytes. + ptrdiff_t occurrences = 0; for (long i = 01; i < opCodesToScan + bytesInfo.first; i++) @@ -134,13 +137,13 @@ CMemory CMemory::FindPatternSelf(const char* szPattern, const Direction searchDi bool bFound = true; int nMemOffset = searchDirect == Direction::DOWN ? i : -i; - for (DWORD j = 0ul; j < bytesInfo.first; j++) + for (size_t j = 0ull; j < bytesInfo.first; j++) { // If either the current byte equals to the byte in our pattern or our current byte in the pattern is a wildcard // our if clause will be false. uint8_t* const pCurrentAddr = (pScanBytes + nMemOffset + j); _mm_prefetch(reinterpret_cast(pCurrentAddr + 64), _MM_HINT_T0); // precache some data in L1. - if (*pCurrentAddr != bytesInfo.second[j] && bytesInfo.second[j] != -1) + if (*pCurrentAddr != bytesInfo.second[j] && bytesInfo.second[j] != 0xffff) { bFound = false; break; diff --git a/src/tier0/module.cpp b/src/tier0/module.cpp index ee299bc6..2d1240a0 100644 --- a/src/tier0/module.cpp +++ b/src/tier0/module.cpp @@ -296,9 +296,8 @@ CMemory CModule::FindStringReadOnly(const char* szString, bool bNullTerminator) } // Convert our string to a byte array. - const vector vBytes = StringToBytes(szString, bNullTerminator); - const pair bytesInfo = std::make_pair< - size_t, const int*>(vBytes.size(), vBytes.data()); // Get the size and data of our bytes. + const vector vBytes = StringToBytes(szString, bNullTerminator); + const pair bytesInfo = std::make_pair(vBytes.size(), vBytes.data()); // Get the size and data of our bytes. // Get start of .rdata section. const uint8_t* pBase = reinterpret_cast(readOnlyData.m_pSectionBase); diff --git a/src/tier0/utility.cpp b/src/tier0/utility.cpp index f9970a50..4d353314 100644 --- a/src/tier0/utility.cpp +++ b/src/tier0/utility.cpp @@ -912,10 +912,10 @@ string& StringTrim(string& svInput, const char* const pszToTrim, const bool bTri /////////////////////////////////////////////////////////////////////////////// // For converting a string to an array of bytes. -vector StringToBytes(const char* const szInput, const bool bNullTerminator) +vector StringToBytes(const char* const szInput, const bool bNullTerminator) { const char* const pszStringEnd = szInput + strlen(szInput); - vector vBytes; + vector vBytes; for (const char* pszCurrentByte = szInput; pszCurrentByte < pszStringEnd; ++pszCurrentByte) { @@ -933,14 +933,14 @@ vector StringToBytes(const char* const szInput, const bool bNullTerminator) /////////////////////////////////////////////////////////////////////////////// // For converting a string to an array of bytes. -pair, string> StringToMaskedBytes(const char* szInput, bool bNullTerminator) +pair, string> StringToMaskedBytes(const char* const szInput, const bool bNullTerminator) { - const char* pszStringStart = const_cast(szInput); - const char* pszStringEnd = pszStringStart + strlen(szInput); vector vBytes; string svMask; - for (const char* pszCurrentByte = pszStringStart; pszCurrentByte < pszStringEnd; ++pszCurrentByte) + const char* pszStringEnd = szInput + strlen(szInput); + + for (const char* pszCurrentByte = szInput; pszCurrentByte < pszStringEnd; ++pszCurrentByte) { // Dereference character and push back the byte. vBytes.push_back(*pszCurrentByte); @@ -952,6 +952,7 @@ pair, string> StringToMaskedBytes(const char* szInput, bool bNul vBytes.push_back(0x0); svMask += 'x'; } + return make_pair(vBytes, svMask); }; @@ -968,50 +969,53 @@ void FourCCToString(FourCCString_t& buf, const int n) /////////////////////////////////////////////////////////////////////////////// // For converting a string pattern with wildcards to an array of bytes. -vector PatternToBytes(const char* szInput) +vector PatternToBytes(const char* const szInput) { - const char* pszPatternStart = const_cast(szInput); - const char* pszPatternEnd = pszPatternStart + strlen(szInput); - vector vBytes; + vector vBytes; + const char* const pszPatternEnd = szInput + strlen(szInput); - for (const char* pszCurrentByte = pszPatternStart; pszCurrentByte < pszPatternEnd; ++pszCurrentByte) + for (const char* pszCurrentByte = szInput; pszCurrentByte < pszPatternEnd; ++pszCurrentByte) { if (*pszCurrentByte == '?') { ++pszCurrentByte; + if (*pszCurrentByte == '?') { ++pszCurrentByte; // Skip double wildcard. } - vBytes.push_back(-1); // Push the byte back as invalid. + + vBytes.push_back(0xffff); // Push the byte back as invalid. } else { - vBytes.push_back(strtoul(pszCurrentByte, const_cast(&pszCurrentByte), 16)); + vBytes.push_back(static_cast(strtoul(pszCurrentByte, const_cast(&pszCurrentByte), 16))); } } + return vBytes; }; /////////////////////////////////////////////////////////////////////////////// // For converting a string pattern with wildcards to an array of bytes and mask. -pair, string> PatternToMaskedBytes(const char* szInput) +pair, string> PatternToMaskedBytes(const char* const szInput) { - const char* pszPatternStart = const_cast(szInput); - const char* pszPatternEnd = pszPatternStart + strlen(szInput); - vector vBytes; string svMask; - for (const char* pszCurrentByte = pszPatternStart; pszCurrentByte < pszPatternEnd; ++pszCurrentByte) + const char* const pszPatternEnd = szInput + strlen(szInput); + + for (const char* pszCurrentByte = szInput; pszCurrentByte < pszPatternEnd; ++pszCurrentByte) { if (*pszCurrentByte == '?') { ++pszCurrentByte; + if (*pszCurrentByte == '?') { ++pszCurrentByte; // Skip double wildcard. } + vBytes.push_back(0); // Push the byte back as invalid. svMask += '?'; } @@ -1021,6 +1025,7 @@ pair, string> PatternToMaskedBytes(const char* szInput) svMask += 'x'; } } + return make_pair(vBytes, svMask); };