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