Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* UFO Extender - Mod for X-COM
- * See <http://www.ufopaedia.org/index.php?title=User:Seb76> for more details.
- * Copyright (C) 2008 Seb76
- *
- * This file is part of UFO Extender.
- * UFO Extender is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- * UFO Extender is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with UFO Extender. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "stdafx.h"
- #include "patcher.h"
- typedef struct patch_s
- {
- unsigned int addr;
- int len;
- struct patch_s *next;
- } patch_t;
- static patch_t *head=NULL;
- int CheckOverlap(unsigned int addr, unsigned int len)
- {
- for(patch_t *patch=head;patch!=NULL;patch=patch->next)
- {
- if(addr+len>patch->addr&&addr<patch->addr+patch->len)
- {
- char buf[128];
- sprintf(buf,"Trying to patch the same place twice (0x%08x, 0x%08x).\nSend this error to the author to have it fixed.",addr, patch->addr);
- MessageBoxA(NULL,buf,"Patch overlap error",MB_ICONERROR);
- exit(0);
- }
- }
- return 1;
- }
- void AddPatch(unsigned int addr, unsigned int len)
- {
- // char buf[128];
- // sprintf(buf,"Patching 0x%08x",addr);
- // MessageBoxA(NULL,buf,"Patch overlap error",MB_ICONERROR);
- CheckOverlap(addr,len);
- patch_t *patch=(patch_t *)malloc(sizeof patch_t);
- patch->next = head;
- head=patch;
- patch->addr=addr;
- patch->len=len;
- }
- void PatchInPlace(unsigned int dst, void *src, unsigned int len)
- {
- AddPatch(dst, len);
- DWORD oldProtect;
- VirtualProtect((void *)dst,len,PAGE_EXECUTE_READWRITE,&oldProtect);
- memcpy((void *)dst,src,len);
- VirtualProtect((void *)dst,len,oldProtect,&oldProtect);
- }
- void InsertCall(unsigned int dst, void *func)
- {
- AddPatch(dst, 5);
- DWORD oldProtect;
- DWORD *pOffset=(DWORD *)(((char *)dst)+1);
- VirtualProtect((void *)dst,5,PAGE_EXECUTE_READWRITE,&oldProtect);
- *(char *)dst=(char)0xE8;
- *pOffset=(int)func-(int)dst-5;
- VirtualProtect((void *)dst,5,oldProtect,&oldProtect);
- }
- void InsertJmp(unsigned int dst, void *func)
- {
- AddPatch(dst, 5);
- DWORD oldProtect;
- DWORD *pOffset=(DWORD *)(((char *)dst)+1);
- VirtualProtect((void *)dst,5,PAGE_EXECUTE_READWRITE,&oldProtect);
- *(char *)dst=(char)0xE9;
- *pOffset=(int)func-(int)dst-5;
- VirtualProtect((void *)dst,5,oldProtect,&oldProtect);
- }
- void Nop(unsigned int dst, unsigned int len)
- {
- AddPatch(dst, len);
- DWORD oldProtect;
- VirtualProtect((void *)dst,len,PAGE_EXECUTE_READWRITE,&oldProtect);
- memset((void *)dst,0x90,len);
- VirtualProtect((void *)dst,len,oldProtect,&oldProtect);
- }
- void InsertJnz(unsigned int dst, void *func)
- {
- AddPatch(dst, 6);
- DWORD oldProtect;
- DWORD *pOffset=(DWORD *)(((char *)dst)+2);
- VirtualProtect((void *)dst,6,PAGE_EXECUTE_READWRITE,&oldProtect);
- *(char *)dst=(char)0x0F;
- *(char *)(dst+1)=(char)0x84;
- *pOffset=(int)func-(int)dst-6;
- VirtualProtect((void *)dst,6,oldProtect,&oldProtect);
- }
- void CheckForET()
- {
- if(*(int *)0x45DEE0 == 0xFE28EBE9)
- {
- MessageBoxA(NULL,"ET is not supported, don't bother trying.","ET detected",MB_ICONEXCLAMATION);
- exit(666);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement