#pragma once #define IN_RANGE(x,a,b) (x >= a && x <= b) #define GET_BITS( x ) (IN_RANGE(x,'0','9') ? (x - '0') : ((x&(~0x20)) - 'A' + 0xa)) #define GET_BYTE( x ) (GET_BITS(x[0]) << 4 | GET_BITS(x[1])) namespace shared::sig { inline BYTE GetBits(BYTE x) { if (IN_RANGE(x, '0', '9')) { return x - '0'; } return ((x & (~0x20)) - 'A' + 0xA); } inline BYTE GetByte(PCWSTR x) { return (GetBits((BYTE)x[0]) << 4 | GetBits((BYTE)x[1])); } extern DWORD_PTR FindPattern(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern); inline DWORD_PTR FindInImage(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR pattern, const DWORD offset = NULL) { auto dwResult = FindPattern(imageBaseAddress, imageSize, pattern); if (offset) dwResult += offset; return dwResult; } inline DWORD_PTR FindInImageEx(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR section, const PCWSTR pattern, const DWORD offset = NULL) { auto* const pDosHdr = (PIMAGE_DOS_HEADER)imageBaseAddress; auto* const pNtHdr = (PIMAGE_NT_HEADERS)(imageBaseAddress + pDosHdr->e_lfanew); if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE || pNtHdr->Signature != IMAGE_NT_SIGNATURE) return 0; auto* pSecHdr = IMAGE_FIRST_SECTION(pNtHdr); auto nSecCnt = pNtHdr->FileHeader.NumberOfSections; for (size_t i = 0; i < nSecCnt; i++) { } auto dwResult = FindPattern(imageBaseAddress, imageSize, pattern); if (offset) dwResult += offset; return dwResult; } inline DWORD_PTR FindInImagePtr(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR pattern, const DWORD offset, const DWORD offsetToInstr, const DWORD instrLength) { const auto dwInitialAddr = FindInImage(imageBaseAddress, imageSize, pattern, offset); if (!dwInitialAddr) return NULL; return RESOLVE_ADDRESS_TO_RELATIVE(dwInitialAddr, offsetToInstr, instrLength); } inline BOOLEAN FindSection(PIMAGE_DOS_HEADER pDosHdr, PIMAGE_NT_HEADERS pNtHdr, IMAGE_SECTION_HEADER* pBuffer, LPCSTR pszName) { auto* pSecHdr = IMAGE_FIRST_SECTION(pNtHdr); const auto nSecCnt = pNtHdr->FileHeader.NumberOfSections; for (size_t i = 0; i < nSecCnt; i++) { const auto& sEntry = pSecHdr[i]; if (!stl::strcmp((PCSTR)sEntry.Name, pszName)) { memcpy(pBuffer, &sEntry, sizeof(sEntry)); return TRUE; } } return FALSE; } inline DWORD_PTR FindInImageEx(const DWORD_PTR imageBaseAddress, const PCSTR section, const PCWSTR pattern, const DWORD offset = NULL) { auto* const pDosHdr = (PIMAGE_DOS_HEADER)imageBaseAddress; auto* const pNtHdr = (PIMAGE_NT_HEADERS)(imageBaseAddress + pDosHdr->e_lfanew); if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE || pNtHdr->Signature != IMAGE_NT_SIGNATURE) return 0; auto sTargetSection = IMAGE_SECTION_HEADER{}; if (!FindSection(pDosHdr, pNtHdr, &sTargetSection, section)) return 0; auto dwResult = FindPattern( imageBaseAddress + sTargetSection.VirtualAddress, sTargetSection.Misc.VirtualSize, pattern ); if (offset) dwResult += offset; return dwResult; } extern uintptr_t find_offset(uintptr_t start_addr, const uint8_t* pattern_buf, size_t pattern_len, size_t offset, size_t opcode_len) noexcept; extern DWORD_PTR FindPatternSafe(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern); }