mirror of
https://github.com/alliedmodders/metamod-source.git
synced 2024-11-29 11:24:19 +01:00
69bc1cf15b
--HG-- extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%402
74 lines
1.7 KiB
C++
74 lines
1.7 KiB
C++
/* ======== SourceMod ========
|
|
* Copyright (C) 2004-2005 SourceMod Development Team
|
|
* No warranties of any kind
|
|
*
|
|
* File: shint_memory.h
|
|
* Memory unprotection
|
|
*
|
|
* License: See LICENSE.txt
|
|
*
|
|
* Author(s): PM, Damaged Soul
|
|
* Contributors: lancevorgin, XAD, theqizmo, Damaged-Soul
|
|
* ============================
|
|
*/
|
|
|
|
#ifndef __SHINT_MEMORY_H__
|
|
#define __SHINT_MEMORY_H__
|
|
|
|
// Feb 17 / 2005:
|
|
// Unprotect now sets to readwrite
|
|
// The vtable doesn't need to be executable anyway
|
|
|
|
# if /********/ defined _WIN32
|
|
# include <windows.h>
|
|
# define SH_MEM_READ 1
|
|
# define SH_MEM_WRITE 2
|
|
# define SH_MEM_EXEC 4
|
|
# elif /******/ defined __linux__
|
|
# include <sys/mman.h>
|
|
// http://www.die.net/doc/linux/man/man2/mprotect.2.html
|
|
# include <limits.h>
|
|
# ifndef PAGESIZE
|
|
# define PAGESIZE 4096
|
|
# endif
|
|
# define SH_MEM_READ PROT_READ
|
|
# define SH_MEM_WRITE PROT_WRITE
|
|
# define SH_MEM_EXEC PROT_EXEC
|
|
|
|
// We need to align addr down to pagesize on linux
|
|
// We assume PAGESIZE is a power of two
|
|
# define SH_LALIGN(x) (void*)((int)(x) & ~(PAGESIZE-1))
|
|
|
|
# else
|
|
# error Unsupported OS/Compiler
|
|
# endif
|
|
|
|
|
|
namespace SourceHook
|
|
{
|
|
inline bool SetMemAccess(void *addr, size_t len, int access)
|
|
{
|
|
# ifdef __linux__
|
|
return mprotect(SH_LALIGN(addr), len, access)==0 ? true : false;
|
|
# else
|
|
DWORD tmp;
|
|
DWORD prot;
|
|
switch (access)
|
|
{
|
|
case SH_MEM_READ:
|
|
prot = PAGE_READONLY; break;
|
|
case SH_MEM_READ | SH_MEM_WRITE:
|
|
prot = PAGE_READWRITE; break;
|
|
case SH_MEM_READ | SH_MEM_EXEC:
|
|
prot = PAGE_EXECUTE_READ; break;
|
|
default:
|
|
case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
|
|
prot = PAGE_EXECUTE_READWRITE; break;
|
|
}
|
|
return VirtualProtect(addr, len, prot, &tmp) ? true : false;
|
|
# endif
|
|
}
|
|
|
|
}
|
|
|
|
#endif |