CDetour/detourhelpers.h
changeset 0 4c53fd0b014e
child 1 abd35d097176
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/CDetour/detourhelpers.h	Fri Feb 20 15:24:04 2009 +1300
     1.3 @@ -0,0 +1,98 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id: detourhelpers.h 248 2008-08-27 00:56:22Z pred $
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    1.36 +#define _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    1.37 +
    1.38 +#if defined PLATFORM_LINUX
    1.39 +#include <sys/mman.h>
    1.40 +#define	PAGE_SIZE	4096
    1.41 +#define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
    1.42 +#define	PAGE_EXECUTE_READWRITE	PROT_READ|PROT_WRITE|PROT_EXEC
    1.43 +#endif
    1.44 +
    1.45 +struct patch_t
    1.46 +{
    1.47 +	patch_t()
    1.48 +	{
    1.49 +		patch[0] = 0;
    1.50 +		bytes = 0;
    1.51 +	}
    1.52 +	unsigned char patch[20];
    1.53 +	size_t bytes;
    1.54 +};
    1.55 +
    1.56 +inline void ProtectMemory(void *addr, int length, int prot)
    1.57 +{
    1.58 +#if defined PLATFORM_LINUX
    1.59 +	void *addr2 = (void *)ALIGN(addr);
    1.60 +	mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
    1.61 +#elif defined PLATFORM_WINDOWS
    1.62 +	DWORD old_prot;
    1.63 +	VirtualProtect(addr, length, prot, &old_prot);
    1.64 +#endif
    1.65 +}
    1.66 +
    1.67 +inline void SetMemPatchable(void *address, size_t size)
    1.68 +{
    1.69 +	ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
    1.70 +}
    1.71 +
    1.72 +inline void DoGatePatch(unsigned char *target, void *callback)
    1.73 +{
    1.74 +	SetMemPatchable(target, 20);
    1.75 +
    1.76 +	target[0] = 0xFF;	/* JMP */
    1.77 +	target[1] = 0x25;	/* MEM32 */
    1.78 +	*(void **)(&target[2]) = callback;
    1.79 +}
    1.80 +
    1.81 +inline void ApplyPatch(void *address, int offset, const patch_t *patch, patch_t *restore)
    1.82 +{
    1.83 +	ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
    1.84 +
    1.85 +	unsigned char *addr = (unsigned char *)address + offset;
    1.86 +	if (restore)
    1.87 +	{
    1.88 +		for (size_t i=0; i<patch->bytes; i++)
    1.89 +		{
    1.90 +			restore->patch[i] = addr[i];
    1.91 +		}
    1.92 +		restore->bytes = patch->bytes;
    1.93 +	}
    1.94 +
    1.95 +	for (size_t i=0; i<patch->bytes; i++)
    1.96 +	{
    1.97 +		addr[i] = patch->patch[i];
    1.98 +	}
    1.99 +}
   1.100 +
   1.101 +#endif //_INCLUDE_SOURCEMOD_DETOURHELPERS_H_