Skip to content

Commit

Permalink
Add PatchCallsInRange API
Browse files Browse the repository at this point in the history
  • Loading branch information
llde committed Sep 15, 2023
1 parent b91e36a commit ee793c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions obse/obse_common/SafeWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ void WriteNop(UInt32 nopAddr, UInt8 numOfByte) {
for (UInt8 i = 0; i < numOfByte; i++) {
SafeWrite8(nopAddr + i, 0x90);
}
}

void PatchCallsInRange(UInt32 start, UInt32 end, UInt32 CallToPatch, UInt32 HookCall) {
UInt32 oldProtect;

VirtualProtect((void*)start , end - start, PAGE_EXECUTE_READWRITE, &oldProtect);
for (UInt32 current = start; current < end; current++) {
if (*(UInt8*)current == 0xE8) {
UInt32 callTarget = *(UInt32*)(current + 1) + current + 1 + 4;
// _MESSAGE("%08X", callTarget);
if (callTarget == CallToPatch) {
// _MESSAGE("Call found");
SafeWrite32(current + 1, HookCall - current - 1 - 4);
}
}
}
VirtualProtect((void*)start, end - start, oldProtect, &oldProtect);

/*
006020FB 054 db 61h ; a
.text:006020FC 054 db 59h ; Y
.text:006020FD 054 db 0EAh ; ê
.text:006020FE 054 db 0FFh ; ÿ
*/
}
4 changes: 3 additions & 1 deletion obse/obse_common/SafeWrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ void WriteRelJnz(UInt32 jumpSrc, UInt32 jumpTgt);
void WriteRelJle(UInt32 jumpSrc, UInt32 jumpTgt);

//Write arbitrary nop
void WriteNop(UInt32 nopAddr, UInt8 numOfByte);
void WriteNop(UInt32 nopAddr, UInt8 numOfByte);
//PAtch calls to address in a certain range
void PatchCallsInRange(UInt32 start, UInt32 end, UInt32 CallToPatch, UInt32 HookCall);

0 comments on commit ee793c6

Please sign in to comment.