CDetour/detourhelpers.h
author Nicholas Hastings <skamonkey@gmail.com>
Mon Nov 12 08:36:42 2012 -0500 (2012-11-12)
changeset 1 abd35d097176
parent 0 4c53fd0b014e
permissions -rw-r--r--
Mac support.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id: detourhelpers.h 248 2008-08-27 00:56:22Z pred $
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    33 #define _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    34 
    35 #if defined PLATFORM_POSIX
    36 #include <sys/mman.h>
    37 #define	PAGE_SIZE	4096
    38 #define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
    39 #define	PAGE_EXECUTE_READWRITE	PROT_READ|PROT_WRITE|PROT_EXEC
    40 #endif
    41 
    42 struct patch_t
    43 {
    44 	patch_t()
    45 	{
    46 		patch[0] = 0;
    47 		bytes = 0;
    48 	}
    49 	unsigned char patch[20];
    50 	size_t bytes;
    51 };
    52 
    53 inline void ProtectMemory(void *addr, int length, int prot)
    54 {
    55 #if defined PLATFORM_POSIX
    56 	void *addr2 = (void *)ALIGN(addr);
    57 	mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
    58 #elif defined PLATFORM_WINDOWS
    59 	DWORD old_prot;
    60 	VirtualProtect(addr, length, prot, &old_prot);
    61 #endif
    62 }
    63 
    64 inline void SetMemPatchable(void *address, size_t size)
    65 {
    66 	ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
    67 }
    68 
    69 inline void DoGatePatch(unsigned char *target, void *callback)
    70 {
    71 	SetMemPatchable(target, 20);
    72 
    73 	target[0] = 0xFF;	/* JMP */
    74 	target[1] = 0x25;	/* MEM32 */
    75 	*(void **)(&target[2]) = callback;
    76 }
    77 
    78 inline void ApplyPatch(void *address, int offset, const patch_t *patch, patch_t *restore)
    79 {
    80 	ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
    81 
    82 	unsigned char *addr = (unsigned char *)address + offset;
    83 	if (restore)
    84 	{
    85 		for (size_t i=0; i<patch->bytes; i++)
    86 		{
    87 			restore->patch[i] = addr[i];
    88 		}
    89 		restore->bytes = patch->bytes;
    90 	}
    91 
    92 	for (size_t i=0; i<patch->bytes; i++)
    93 	{
    94 		addr[i] = patch->patch[i];
    95 	}
    96 }
    97 
    98 #endif //_INCLUDE_SOURCEMOD_DETOURHELPERS_H_