From 7dd69075213c1d7769e0bbf2e5c2fe8f0a17d1d6 Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sun, 14 Jul 2019 18:20:15 -0700 Subject: [PATCH] * Minor code cleanup. --- CommonAPI/Common/source/DXAPI/DXAPI.cpp | 42 ++++++++++--------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/CommonAPI/Common/source/DXAPI/DXAPI.cpp b/CommonAPI/Common/source/DXAPI/DXAPI.cpp index 6e9520f..0254371 100644 --- a/CommonAPI/Common/source/DXAPI/DXAPI.cpp +++ b/CommonAPI/Common/source/DXAPI/DXAPI.cpp @@ -34,15 +34,6 @@ namespace DX return retval; } - bool memPatch(unsigned int addr, unsigned char * data, unsigned int size){ - DWORD oldprotect=0; - DWORD oldnewprotect=0; - VirtualProtect((void *)addr,size,PAGE_EXECUTE_READWRITE,&oldprotect); - memcpy((void *)addr,(void*) data,size); - VirtualProtect((void *)addr,size,oldprotect,&oldnewprotect); - return true; - } - float memToFloat(unsigned int addr){ DWORD oldprotect=0; DWORD oldnewprotect=0; @@ -73,15 +64,16 @@ namespace DX VirtualProtect((void *)addr,size,PAGE_EXECUTE_READWRITE,&oldprotect); for (int i=0; i(hexdigits,"%02X ",*((unsigned char*)addr+i)); } else { - sprintf(hexdigits,"%02X",*((unsigned char*)addr+i)); + sprintf_s<20>(hexdigits,"%02X",*((unsigned char*)addr+i)); } - strncat(outstr,hexdigits,254-strlen(hexdigits)); + strncat_s(outstr, 256, hexdigits, 254 - strlen(hexdigits)); } VirtualProtect((void *)addr,size,oldprotect,&oldnewprotect); - strncpy(dst,outstr,255); + + strncpy_s(dst, 256, outstr, 255); return true; } @@ -141,8 +133,9 @@ namespace DX sprintf_s(ret, buffer_length, "%s/%s", modname, filename); // Check if it exists - FILE *handle = fopen(ret, "r"); - if (handle) + FILE* handle; + errno_t error = fopen_s(&handle, ret, "r"); + if (handle && !error) { fclose(handle); delete[] modpaths_temp; @@ -197,20 +190,19 @@ namespace DX if (!success) return false; - // Come on Microsoft... why can't anything you make actually be good. - // NOTE: This memcpy invocation was crashing the linker... - //memcpy(address, payload, payloadSize); - - unsigned char* destination = reinterpret_cast(address); - unsigned char* sourceData = reinterpret_cast(payload); - for (unsigned int iteration = 0; iteration < payloadSize; ++iteration) - destination[iteration] = sourceData[iteration]; - - return true; + DWORD newProtect; + memcpy(address, payload, payloadSize); + success = VirtualProtect(address, payloadSize, oldProtect, &newProtect); + return success; } bool memPatch(unsigned int address, void* payload, unsigned int payloadSize) { return memPatch(reinterpret_cast(address), payload, payloadSize); } + + bool memPatch(unsigned int address, unsigned char* data, unsigned int size) + { + return memPatch(reinterpret_cast(address), reinterpret_cast(data), size); + } } \ No newline at end of file