Advertisement
jewalky

Untitled

Aug 13th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. /* UFO Extender - Mod for X-COM
  2.  * See <http://www.ufopaedia.org/index.php?title=User:Seb76> for more details.
  3.  * Copyright (C) 2008 Seb76
  4.  *
  5.  * This file is part of UFO Extender.
  6.  
  7.  * UFO Extender is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  
  12.  * UFO Extender is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with UFO Extender.  If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20.  
  21. #include "stdafx.h"
  22. #include "patcher.h"
  23.  
  24. typedef struct patch_s
  25. {
  26.     unsigned int addr;
  27.     int len;
  28.     struct patch_s *next;
  29. } patch_t;
  30.  
  31. static patch_t *head=NULL;
  32.  
  33. int CheckOverlap(unsigned int addr, unsigned int len)
  34. {
  35.     for(patch_t *patch=head;patch!=NULL;patch=patch->next)
  36.     {
  37.         if(addr+len>patch->addr&&addr<patch->addr+patch->len)
  38.         {
  39.             char buf[128];
  40.             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);
  41.             MessageBoxA(NULL,buf,"Patch overlap error",MB_ICONERROR);
  42.             exit(0);
  43.         }
  44.     }
  45.  
  46.     return 1;
  47. }
  48.  
  49. void AddPatch(unsigned int addr, unsigned int len)
  50. {
  51. //          char buf[128];
  52. //          sprintf(buf,"Patching 0x%08x",addr);
  53. //          MessageBoxA(NULL,buf,"Patch overlap error",MB_ICONERROR);
  54.  
  55.  
  56.  
  57.  
  58.     CheckOverlap(addr,len);
  59.  
  60.     patch_t *patch=(patch_t *)malloc(sizeof patch_t);
  61.     patch->next = head;
  62.     head=patch;
  63.     patch->addr=addr;
  64.     patch->len=len;
  65. }
  66.  
  67. void PatchInPlace(unsigned int dst, void *src, unsigned int len)
  68. {
  69.     AddPatch(dst, len);
  70.     DWORD oldProtect;
  71.     VirtualProtect((void *)dst,len,PAGE_EXECUTE_READWRITE,&oldProtect);
  72.     memcpy((void *)dst,src,len);
  73.     VirtualProtect((void *)dst,len,oldProtect,&oldProtect);
  74. }
  75.  
  76. void InsertCall(unsigned int dst, void *func)
  77. {
  78.     AddPatch(dst, 5);
  79.     DWORD oldProtect;
  80.     DWORD *pOffset=(DWORD *)(((char *)dst)+1);
  81.  
  82.     VirtualProtect((void *)dst,5,PAGE_EXECUTE_READWRITE,&oldProtect);
  83.     *(char *)dst=(char)0xE8;
  84.     *pOffset=(int)func-(int)dst-5;
  85.     VirtualProtect((void *)dst,5,oldProtect,&oldProtect);
  86. }
  87.  
  88. void InsertJmp(unsigned int dst, void *func)
  89. {
  90.     AddPatch(dst, 5);
  91.     DWORD oldProtect;
  92.     DWORD *pOffset=(DWORD *)(((char *)dst)+1);
  93.  
  94.     VirtualProtect((void *)dst,5,PAGE_EXECUTE_READWRITE,&oldProtect);
  95.     *(char *)dst=(char)0xE9;
  96.     *pOffset=(int)func-(int)dst-5;
  97.     VirtualProtect((void *)dst,5,oldProtect,&oldProtect);
  98. }
  99.  
  100. void Nop(unsigned int dst, unsigned int len)
  101. {
  102.     AddPatch(dst, len);
  103.     DWORD oldProtect;
  104.     VirtualProtect((void *)dst,len,PAGE_EXECUTE_READWRITE,&oldProtect);
  105.     memset((void *)dst,0x90,len);
  106.     VirtualProtect((void *)dst,len,oldProtect,&oldProtect);
  107. }
  108.  
  109. void InsertJnz(unsigned int dst, void *func)
  110. {
  111.     AddPatch(dst, 6);
  112.     DWORD oldProtect;
  113.     DWORD *pOffset=(DWORD *)(((char *)dst)+2);
  114.  
  115.     VirtualProtect((void *)dst,6,PAGE_EXECUTE_READWRITE,&oldProtect);
  116.     *(char *)dst=(char)0x0F;
  117.     *(char *)(dst+1)=(char)0x84;
  118.  
  119.     *pOffset=(int)func-(int)dst-6;
  120.     VirtualProtect((void *)dst,6,oldProtect,&oldProtect);
  121. }
  122.  
  123. void CheckForET()
  124. {
  125.     if(*(int *)0x45DEE0 == 0xFE28EBE9)
  126.     {
  127.         MessageBoxA(NULL,"ET is not supported, don't bother trying.","ET detected",MB_ICONEXCLAMATION);
  128.         exit(666);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement