Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions FastcodePatch.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

interface

function FastcodeGetAddress(AStub: Pointer): Pointer;
function FastcodeGetAddress(AProc: Pointer): Pointer;
procedure FastcodeAddressPatch(const ASource, ADestination: Pointer);

implementation
Expand All @@ -39,18 +39,24 @@ implementation
PJump = ^TJump;
TJump = packed record
OpCode: Byte;
Distance: Pointer;
Distance: integer;
end;

function FastcodeGetAddress(AStub: Pointer): Pointer;
function FastcodeGetAddress(AProc: Pointer): Pointer;
type
PAbsoluteIndirectJmp = ^TAbsoluteIndirectJmp;
TAbsoluteIndirectJmp = packed record
OpCode: Word; //$FF25(Jmp, FF /4)
Addr: Cardinal;
end;
var J: PAbsoluteIndirectJmp;
begin
if PBYTE(AStub)^ = $E8 then
begin
Inc(Integer(AStub));
Result := Pointer(Integer(AStub) + SizeOf(Pointer) + PInteger(AStub)^);
end
J := PAbsoluteIndirectJmp(AProc);
if (J.OpCode = $25FF) then
{$ifdef Win32}Result := PPointer(J.Addr)^{$endif}
{$ifdef Win64}Result := PPointer(NativeUInt(AProc) + J.Addr + 6{Instruction Size})^{$endif}
else
Result := nil;
Result := AProc;
end;

procedure FastcodeAddressPatch(const ASource, ADestination: Pointer);
Expand All @@ -64,11 +70,11 @@ procedure FastcodeAddressPatch(const ASource, ADestination: Pointer);
begin
NewJump := PJump(ASource);
NewJump.OpCode := $E9;
NewJump.Distance := Pointer(Integer(ADestination) - Integer(ASource) - 5);
NewJump.Distance := NativeUInt(ADestination) - NativeUInt(ASource) - Size;

FlushInstructionCache(GetCurrentProcess, ASource, SizeOf(TJump));
VirtualProtect(ASource, Size, OldProtect, @OldProtect);
end;
end;

end.
end.