1515
1616#include < array>
1717#include < cassert>
18- #include < vector>
1918#include < string>
2019#include < string_view>
20+ #include < type_traits>
2121#include < utility>
22+ #include < vector>
2223
2324#ifdef __cpp_concepts
2425# include < concepts>
2526#endif
2627
28+ #ifdef __cpp_lib_debugging
29+ # include < debugging>
30+ #endif
31+
2732#ifdef __cpp_consteval
2833# define DYNLIB_COMPILE_TIME_EXPR consteval
2934#else
@@ -68,8 +73,8 @@ struct Pattern_t
6873
6974// Concept for pattern callback.
7075// Signature: bool callback(std::size_t index, CMemory match)
71- // Returns: false -> continue scanning.
72- // true -> stop scanning.
76+ // Returns: false -> stop scanning.
77+ // true -> continue scanning.
7378#if defined(__cpp_concepts) && __cpp_concepts >= 201907L
7479template <typename T>
7580concept PatternCallback_t = requires (T func, std::size_t index, CMemory match)
@@ -260,16 +265,13 @@ class CModule
260265 return Find (pStart, pSection);
261266 }
262267
263- [[nodiscard]] CMemory Find (const CMemory pStart, const Section_t* pSection) const
268+ [[nodiscard]] CMemory Find (const CMemory pStart, const Section_t* pSection = nullptr ) const
264269 {
265270 return m_pModule->FindPattern <SIZE >(CMemory (Base_t::m_aBytes.data ()), std::string_view (Base_t::m_aMask.data (), Base_t::m_nSize), pStart, pSection);
266271 }
267- [[nodiscard]] CMemory FindAndOffset (const std::ptrdiff_t offset, const CMemory pStart = nullptr , const Section_t* pSection = nullptr ) const { return Find (pStart, pSection).Offset (offset); }
268- [[nodiscard]] CMemory FindAndOffsetFromSelf (const CMemory pStart = nullptr , const Section_t* pSection = nullptr ) const { return FindAndOffset (Base_t::m_nSize, pStart, pSection); }
269-
270- [[nodiscard]] CMemory FindAndDeref (const std::uintptr_t deref = 1 , const CMemory pStart = nullptr , const Section_t* pSection = nullptr ) const { return Find (pStart, pSection).Deref (deref); }
271- [[nodiscard]]
272- CMemory FollowCall (const std::ptrdiff_t opcodeOffset = 0x1 , const std::ptrdiff_t nextInstructionOffset = 0x5 , const CMemory pStart = nullptr , const Section_t* pSection = nullptr ) const { return Find (pStart, pSection).FollowNearCall (opcodeOffset, nextInstructionOffset); }
272+ [[nodiscard]] CMemory OffsetAndFind (const std::ptrdiff_t offset, CMemory pStart, const Section_t* pSection = nullptr ) const { return Find (pStart + offset, pSection); }
273+ [[nodiscard]] CMemory OffsetFromSelfAndFind (const CMemory pStart, const Section_t* pSection = nullptr ) const { return OffsetAndFind (Base_t::m_nSize, pStart, pSection); }
274+ [[nodiscard]] CMemory DerefAndFind (const std::uintptr_t deref, CMemory pStart, const Section_t* pSection = nullptr ) const { return Find (pStart.Deref (deref), pSection); }
273275 }; // struct CSignatureView
274276
275277 CModule () : m_pExecutableSection(nullptr ), m_pHandle(nullptr ) {}
@@ -278,10 +280,10 @@ class CModule
278280 CModule (const CModule&) = delete ;
279281 CModule& operator =(const CModule&) = delete ;
280282 CModule (CModule&& other) noexcept : m_sPath(std::move(other.m_sPath)), m_vecSections(std::move(other.m_vecSections)), m_pExecutableSection(std::move(other.m_pExecutableSection)), m_pHandle(std::move(other.m_pHandle)) {}
283+ CModule (const CMemory pModuleMemory);
281284 explicit CModule (const std::string_view svModuleName);
282285 explicit CModule (const char * pszModuleName) : CModule(std::string_view(pszModuleName)) {}
283286 explicit CModule (const std::string& sModuleName ) : CModule(std::string_view(sModuleName )) {}
284- CModule (const CMemory pModuleMemory);
285287
286288 bool LoadFromPath (const std::string_view svModelePath, int flags);
287289
@@ -428,20 +430,35 @@ class CModule
428430 CMemory pIter = pStartAddress ? pStartAddress : pBase;
429431 const CMemory pEnd = pBase + sectionSize;
430432
431- std::size_t foundLength = 0 ;
433+ std::size_t foundCount = 0 ;
432434
433- for (CMemory pMatch = sig (pIter, pSection);
434- pMatch.IsValid () &&
435- pMatch < pEnd;
436- pIter = sig.FindAndOffsetFromSelf (pMatch, pSection))
435+ pIter = sig (pIter, pSection);
436+
437+ do
437438 {
438- if (callback (foundLength, pMatch)) // foundLength = the index of found pattern now.
439+ if (!callback (foundCount, pIter)) // foundCount = the index of found pattern now.
440+ break ;
441+
442+ // Break the loop, stop this madness.
443+ if (foundCount > 999 )
444+ {
445+ std::fprintf (stderr, " %s%s:%d\n " ,
446+ " >> Detected an INFINITE LOOP!\n "
447+ " >> Breaking from\n " ,
448+ __FILE__, __LINE__);
449+
450+ #ifdef __cpp_lib_debugging
451+ std::breakpoint ();
452+ #endif
453+
439454 break ;
455+ }
440456
441- ++foundLength ;
457+ ++foundCount ;
442458 }
459+ while ((pIter = sig.OffsetFromSelfAndFind (pIter, pSection)).IsValid ());
443460
444- return foundLength ; // Count of the found patterns.
461+ return foundCount ; // Count of the found patterns.
445462 }
446463
447464 [[nodiscard]] CMemory GetVirtualTableByName (const std::string_view svTableName, bool bDecorated = false ) const ;
0 commit comments