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