Skip to content

Commit b97f3e4

Browse files
committed
Initial import.
0 parents  commit b97f3e4

File tree

223 files changed

+28799
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+28799
-0
lines changed

.hgignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Build directories
2+
/Debug.*
3+
/Release.*
4+
5+
# Files generated by Mac OS X Finder
6+
(^|/)\.DS_Store$
7+
8+
# Files generated by Windows Explorer
9+
(^|/)[dD]esktop\.ini$
10+
(^|/)[tT]humbs\.db$

CDetour/detourhelpers.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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$
30+
*/
31+
32+
#ifndef _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
33+
#define _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
34+
35+
struct patch_t
36+
{
37+
patch_t()
38+
{
39+
patch[0] = 0;
40+
bytes = 0;
41+
}
42+
unsigned char patch[20];
43+
size_t bytes;
44+
};
45+
46+
inline void SetMemPatchable(void *address, size_t size)
47+
{
48+
SetMemAccess(address, size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
49+
}
50+
51+
inline void SetMemExec(void *address, size_t size)
52+
{
53+
SetMemAccess(address, size, SH_MEM_READ|SH_MEM_EXEC);
54+
}
55+
56+
inline void DoGatePatch(unsigned char *target, void *callback)
57+
{
58+
SetMemPatchable(target, 5);
59+
60+
target[0] = IA32_JMP_IMM32;
61+
*(int32_t *)(&target[1]) = int32_t((unsigned char *)callback - (target + 5));
62+
63+
SetMemExec(target, 5);
64+
}
65+
66+
inline void ApplyPatch(void *address, int offset, const patch_t *patch, patch_t *restore)
67+
{
68+
SetMemPatchable(address, sizeof(patch->patch));
69+
70+
unsigned char *addr = (unsigned char *)address + offset;
71+
if (restore)
72+
{
73+
for (size_t i=0; i<patch->bytes; i++)
74+
{
75+
restore->patch[i] = addr[i];
76+
}
77+
restore->bytes = patch->bytes;
78+
}
79+
80+
memcpy(addr, patch->patch, patch->bytes);
81+
82+
SetMemExec(address, sizeof(patch->patch));
83+
}
84+
85+
#endif //_INCLUDE_SOURCEMOD_DETOURHELPERS_H_

CDetour/detours.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* vim: set ts=4 :
3+
* =============================================================================
4+
* SourceMod
5+
* Copyright (C) 2004-2008 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$
30+
*/
31+
32+
#include "detours.h"
33+
#include <asm/asm.h>
34+
35+
CPageAlloc GenBuffer::ms_Allocator(16);
36+
37+
CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, void *addr)
38+
{
39+
CDetour *detour = new CDetour(callbackfunction, trampoline);
40+
if (detour)
41+
{
42+
if (!detour->Init(addr))
43+
{
44+
delete detour;
45+
return NULL;
46+
}
47+
48+
return detour;
49+
}
50+
51+
return NULL;
52+
}
53+
54+
CDetour::CDetour(void *callbackfunction, void **trampoline)
55+
{
56+
enabled = false;
57+
detoured = false;
58+
detour_address = NULL;
59+
detour_trampoline = NULL;
60+
this->detour_callback = callbackfunction;
61+
this->trampoline = trampoline;
62+
}
63+
64+
bool CDetour::Init(void *addr)
65+
{
66+
detour_address = addr;
67+
68+
if (!CreateDetour())
69+
{
70+
enabled = false;
71+
return enabled;
72+
}
73+
74+
enabled = true;
75+
76+
return enabled;
77+
}
78+
79+
void CDetour::Destroy()
80+
{
81+
DeleteDetour();
82+
delete this;
83+
}
84+
85+
bool CDetour::IsEnabled()
86+
{
87+
return enabled;
88+
}
89+
90+
void *CDetour::GetTargetAddr()
91+
{
92+
return detour_address;
93+
}
94+
95+
bool CDetour::CreateDetour()
96+
{
97+
if (!detour_address)
98+
{
99+
return false;
100+
}
101+
102+
/*
103+
* Determine how many bytes to save from target function.
104+
* We want 5 for our detour jmp, but it could require more.
105+
*/
106+
detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE);
107+
108+
/* First, save restore bits */
109+
memcpy(detour_restore.patch, (unsigned char *)detour_address, detour_restore.bytes);
110+
111+
/* Patch old bytes in */
112+
codegen.alloc(detour_restore.bytes);
113+
copy_bytes((unsigned char *)detour_address, codegen.GetData(), detour_restore.bytes);
114+
115+
/* Return to the original function */
116+
jitoffs_t call = IA32_Jump_Imm32(&codegen, 0);
117+
IA32_Write_Jump32_Abs(&codegen, call, (unsigned char *)detour_address + detour_restore.bytes);
118+
119+
codegen.SetRE();
120+
121+
*trampoline = codegen.GetData();
122+
123+
return true;
124+
}
125+
126+
void CDetour::DeleteDetour()
127+
{
128+
if (detoured)
129+
{
130+
DisableDetour();
131+
}
132+
133+
if (detour_trampoline)
134+
{
135+
/* Free the allocated trampoline memory */
136+
codegen.clear();
137+
detour_trampoline = NULL;
138+
}
139+
}
140+
141+
void CDetour::EnableDetour()
142+
{
143+
if (!detoured)
144+
{
145+
DoGatePatch((unsigned char *)detour_address, detour_callback);
146+
detoured = true;
147+
}
148+
}
149+
150+
void CDetour::DisableDetour()
151+
{
152+
if (detoured)
153+
{
154+
/* Remove the patch */
155+
ApplyPatch(detour_address, 0, &detour_restore, NULL);
156+
detoured = false;
157+
}
158+
}

0 commit comments

Comments
 (0)