Made libPtr param const. Doesn't get modified so why not.
Also removed some unneccessary code (for now)
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "MemoryUtils.h"
16 #include "sm_platform.h"
22 MemoryUtils g_MemUtils;
25 MemoryUtils::MemoryUtils()
27 #ifdef PLATFORM_WINDOWS
32 m_PageSize = info.dwPageSize;
34 #elif defined PLATFORM_POSIX
36 m_PageSize = sysconf(_SC_PAGE_SIZE);
42 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
48 memset(&lib, 0, sizeof(DynLibInfo));
50 if (!GetLibraryInfo(libPtr, lib))
55 ptr = reinterpret_cast<char *>(lib.baseAddress);
56 end = ptr + lib.memorySize;
61 for (register size_t i = 0; i < len; i++)
63 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
79 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
81 unsigned long baseAddr;
88 #ifdef PLATFORM_WINDOWS
90 MEMORY_BASIC_INFORMATION info;
91 IMAGE_DOS_HEADER *dos;
93 IMAGE_FILE_HEADER *file;
94 IMAGE_OPTIONAL_HEADER *opt;
96 if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
101 baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
103 /* All this is for our insane sanity checks :o */
104 dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
105 pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
106 file = &pe->FileHeader;
107 opt = &pe->OptionalHeader;
109 /* Check PE magic and signature */
110 if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
115 /* Check architecture, which is 32-bit/x86 right now
116 * Should change this for 64-bit if Valve gets their act together
118 if (file->Machine != IMAGE_FILE_MACHINE_I386)
123 /* For our purposes, this must be a dynamic library */
124 if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
129 /* Finally, we can do this */
130 lib.memorySize = opt->SizeOfImage;
132 #elif defined PLATFORM_LINUX
139 if (!dladdr(libPtr, &info))
144 if (!info.dli_fbase || !info.dli_fname)
149 /* This is for our insane sanity checks :o */
150 baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
151 file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
153 /* Check ELF magic */
154 if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
159 /* Check ELF version */
160 if (file->e_ident[EI_VERSION] != EV_CURRENT)
165 /* Check ELF architecture, which is 32-bit/x86 right now
166 * Should change this for 64-bit if Valve gets their act together
168 if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
173 /* For our purposes, this must be a dynamic library/shared object */
174 if (file->e_type != ET_DYN)
179 phdrCount = file->e_phnum;
180 phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
182 /* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
183 for (uint16_t i = 0; i < phdrCount; i++)
185 Elf32_Phdr &hdr = phdr[i];
187 if (hdr.p_type == PT_LOAD)
189 lib.memorySize += hdr.p_memsz;
195 lib.baseAddress = reinterpret_cast<void *>(baseAddr);