From 1207eb6fc8fafb389c16e96cf7ba1ad9b9b9bc43 Mon Sep 17 00:00:00 2001 From: ttttupup Date: Fri, 22 Dec 2023 23:23:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=86=85=E5=AD=98=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/base/src/include/memory.h | 11 ++++++ app/base/src/memory.cc | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/base/src/include/memory.h create mode 100644 app/base/src/memory.cc diff --git a/app/base/src/include/memory.h b/app/base/src/include/memory.h new file mode 100644 index 0000000..24a0e1e --- /dev/null +++ b/app/base/src/include/memory.h @@ -0,0 +1,11 @@ +#ifndef BASE_MEMORY_H_ +#define BASE_MEMORY_H_ +#include +#include +namespace base { +namespace memory { + std::vector ScanAndMatchValue(INT64 value, INT64 start,int align); + int sunday(const byte* total, int tlen, const byte* part, int plen); +} +} // namespace base +#endif \ No newline at end of file diff --git a/app/base/src/memory.cc b/app/base/src/memory.cc new file mode 100644 index 0000000..a5e6cc7 --- /dev/null +++ b/app/base/src/memory.cc @@ -0,0 +1,64 @@ +#include "include/memory.h" + +namespace base { +namespace memory { +std::vector ScanAndMatchValue(INT64 value, INT64 start, int align) { + SYSTEM_INFO sys_info; + GetSystemInfo(&sys_info); + std::vector result; + INT64 min_addr = + reinterpret_cast(sys_info.lpMinimumApplicationAddress); + INT64 max_addr = + reinterpret_cast(sys_info.lpMaximumApplicationAddress); + const INT64 page_size = sys_info.dwPageSize; + min_addr = min_addr > start ? min_addr : start; + + auto current_addr = min_addr; + MEMORY_BASIC_INFORMATION mem_info = {}; + HANDLE handle = GetCurrentProcess(); + while (current_addr < max_addr) { + VirtualQueryEx(handle, reinterpret_cast(current_addr), &mem_info, + sizeof(MEMORY_BASIC_INFORMATION)); + + if ((INT64)mem_info.RegionSize <= 0) { + break; + } + INT64 region_size = mem_info.RegionSize; + if ((mem_info.State & MEM_COMMIT) == MEM_COMMIT && + (mem_info.Protect & PAGE_GUARD) != PAGE_GUARD && + (mem_info.Protect & PAGE_NOACCESS) != PAGE_NOACCESS) { + for (INT64 i = 0; i < region_size; i += align) { + if (value == *(INT64 *)current_addr) { + result.push_back(current_addr); + } + current_addr += align; + } + } else { + current_addr += region_size; + } + } + return result; +} + +int sunday(const byte* total, int tlen, const byte* part, int plen) { + byte move[128] = {0}; + for (int i = 0; i < plen; i++) { + move[part[i]] = plen - i; + } + int s = 0; + int j; + while (s <= tlen - plen) { + j = 0; + while (total[s + j] == part[j]) { + j++; + if (j == plen) { + return s; + } + } + s += move[total[s + plen]]; + } + return -1; +} + +} // namespace memory +} // namespace base \ No newline at end of file