DBG: added patches.cpp with simple std::map for patch storage
This commit is contained in:
		
							parent
							
								
									cd8ca17568
								
							
						
					
					
						commit
						ad4eda86d2
					
				|  | @ -0,0 +1,118 @@ | |||
| #include "patches.h" | ||||
| #include "addrinfo.h" | ||||
| #include "memory.h" | ||||
| #include "debugger.h" | ||||
| 
 | ||||
| PatchesInfo patches; | ||||
| 
 | ||||
| bool patchset(uint addr, unsigned char oldbyte, unsigned char newbyte) | ||||
| { | ||||
|     if(!DbgIsDebugging() || !memisvalidreadptr(fdProcessInfo->hProcess, addr)) | ||||
|         return false; | ||||
|     if(oldbyte==newbyte) | ||||
|         return true; //no need to make a patch for a byte that is equal to itself
 | ||||
|     PATCHINFO newPatch; | ||||
|     newPatch.addr=addr-modbasefromaddr(addr); | ||||
|     modnamefromaddr(addr, newPatch.mod, true); | ||||
|     newPatch.oldbyte=oldbyte; | ||||
|     newPatch.newbyte=newbyte; | ||||
|     uint key=modhashfromva(addr); | ||||
|     PatchesInfo::iterator found=patches.find(key); | ||||
|     if(found!=patches.end()) //we found a patch on the specified address
 | ||||
|     { | ||||
|         if(found->second.oldbyte == newbyte) //patch is undone
 | ||||
|         { | ||||
|             patches.erase(found); | ||||
|             return true; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             newPatch.oldbyte=found->second.oldbyte; //keep the original byte from the previous patch
 | ||||
|             found->second=newPatch; | ||||
|         } | ||||
|     } | ||||
|     else | ||||
|         patches.insert(std::make_pair(key, newPatch)); | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| bool patchget(uint addr, PATCHINFO* patch) | ||||
| { | ||||
|     if(!DbgIsDebugging()) | ||||
|         return false; | ||||
|     PatchesInfo::iterator found=patches.find(modhashfromva(addr)); | ||||
|     if(found==patches.end()) //not found
 | ||||
|         return false; | ||||
|     if(patch) | ||||
|     { | ||||
|         *patch=found->second; | ||||
|         patch->addr+=modbasefromaddr(addr); | ||||
|         return true; | ||||
|     } | ||||
|     return (found->second.oldbyte != found->second.newbyte); | ||||
| } | ||||
| 
 | ||||
| bool patchdel(uint addr) | ||||
| { | ||||
|     if(!DbgIsDebugging()) | ||||
|         return false; | ||||
|     return (patches.erase(modhashfromva(addr))==1); | ||||
| } | ||||
| 
 | ||||
| void patchdelrange(uint start, uint end) | ||||
| { | ||||
|     if(!DbgIsDebugging()) | ||||
|         return; | ||||
|     bool bDelAll=(start==0 && end==~0); //0x00000000-0xFFFFFFFF
 | ||||
|     uint modbase=modbasefromaddr(start); | ||||
|     if(modbase!=modbasefromaddr(end)) | ||||
|         return; | ||||
|     start-=modbase; | ||||
|     end-=modbase; | ||||
|     PatchesInfo::iterator i=patches.begin(); | ||||
|     while(i!=patches.end()) | ||||
|     { | ||||
|         if(bDelAll || (i->second.addr>=start && i->second.addr<end)) | ||||
|             patches.erase(i++); | ||||
|         else | ||||
|             i++; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void patchclear(const char* mod) | ||||
| { | ||||
|     if(!mod or !*mod) | ||||
|         patches.clear(); | ||||
|     else | ||||
|     { | ||||
|         PatchesInfo::iterator i=patches.begin(); | ||||
|         while(i!=patches.end()) | ||||
|         { | ||||
|             if(!_stricmp(i->second.mod, mod)) | ||||
|                 patches.erase(i++); | ||||
|             else | ||||
|                 i++; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| bool patchenum(PATCHINFO* patcheslist, size_t* cbsize) | ||||
| { | ||||
|     if(!DbgIsDebugging()) | ||||
|         return false; | ||||
|     if(!patcheslist && !cbsize) | ||||
|         return false; | ||||
|     if(!patcheslist && cbsize) | ||||
|     { | ||||
|         *cbsize=patches.size()*sizeof(LOOPSINFO); | ||||
|         return true; | ||||
|     } | ||||
|     int j=0; | ||||
|     for(PatchesInfo::iterator i=patches.begin(); i!=patches.end(); ++i,j++) | ||||
|     { | ||||
|         patcheslist[j]=i->second; | ||||
|         uint modbase=modbasefromname(patcheslist[j].mod); | ||||
|         patcheslist[j].addr+=modbase; | ||||
|     } | ||||
|     return true; | ||||
| } | ||||
|  | @ -0,0 +1,22 @@ | |||
| #ifndef _PATCHES_H | ||||
| #define _PATCHES_H | ||||
| 
 | ||||
| #include "_global.h" | ||||
| 
 | ||||
| struct PATCHINFO | ||||
| { | ||||
|     char mod[MAX_MODULE_SIZE]; | ||||
|     uint addr; | ||||
|     unsigned char oldbyte; | ||||
|     unsigned char newbyte; | ||||
| }; | ||||
| typedef std::map<uint, PATCHINFO> PatchesInfo; | ||||
| 
 | ||||
| bool patchset(uint addr, unsigned char oldbyte, unsigned char newbyte); | ||||
| bool patchget(uint addr, PATCHINFO* patch); | ||||
| bool patchdel(uint addr); | ||||
| void patchdelrange(uint start, uint end); | ||||
| void patchclear(const char* mod); | ||||
| bool patchenum(PATCHINFO* patchlist, size_t* cbsize); | ||||
| 
 | ||||
| #endif //_PATCHES_H
 | ||||
|  | @ -26,6 +26,7 @@ | |||
|     <ClCompile Include="memory.cpp" /> | ||||
|     <ClCompile Include="msgqueue.cpp" /> | ||||
|     <ClCompile Include="murmurhash.cpp" /> | ||||
|     <ClCompile Include="patches.cpp" /> | ||||
|     <ClCompile Include="plugin_loader.cpp" /> | ||||
|     <ClCompile Include="reference.cpp" /> | ||||
|     <ClCompile Include="simplescript.cpp" /> | ||||
|  | @ -68,6 +69,7 @@ | |||
|     <ClInclude Include="memory.h" /> | ||||
|     <ClInclude Include="msgqueue.h" /> | ||||
|     <ClInclude Include="murmurhash.h" /> | ||||
|     <ClInclude Include="patches.h" /> | ||||
|     <ClInclude Include="plugin_loader.h" /> | ||||
|     <ClInclude Include="reference.h" /> | ||||
|     <ClInclude Include="simplescript.h" /> | ||||
|  |  | |||
|  | @ -123,6 +123,9 @@ | |||
|     <ClCompile Include="_dbgfunctions.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="patches.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="_exports.h"> | ||||
|  | @ -257,5 +260,8 @@ | |||
|     <ClInclude Include="_dbgfunctions.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="patches.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
		Loading…
	
		Reference in New Issue