Skip to content

Commit

Permalink
Decrease the size of compiled binary further.
Browse files Browse the repository at this point in the history
  • Loading branch information
blueskythlikesclouds committed Aug 17, 2023
1 parent 1b59fdc commit 6162d2e
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions Dependencies/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,37 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr);

#define WRITE_MEMORY(location, type, ...) \
do { \
const type data[] = { __VA_ARGS__ }; \
DWORD oldProtect; \
VirtualProtect((void*)(location), sizeof(data), PAGE_EXECUTE_READWRITE, &oldProtect); \
memcpy((void*)(location), data, sizeof(data)); \
VirtualProtect((void*)(location), sizeof(data), oldProtect, &oldProtect); \
void* writeMemLoc = (void*)(location); \
const type writeMemData[] = { __VA_ARGS__ }; \
DWORD writeMemOldProtect; \
VirtualProtect(writeMemLoc, sizeof(writeMemData), PAGE_EXECUTE_READWRITE, &writeMemOldProtect); \
memcpy(writeMemLoc, writeMemData, sizeof(writeMemData)); \
VirtualProtect(writeMemLoc, sizeof(writeMemData), writeMemOldProtect, &writeMemOldProtect); \
} while(0)

#define WRITE_JUMP(location, function) \
do { \
WRITE_MEMORY((size_t)(location), uint8_t, 0x48, 0xB8); \
WRITE_MEMORY((size_t)(location) + 2, uint64_t, (uint64_t)(function)); \
WRITE_MEMORY((size_t)(location) + 10, uint8_t, 0xFF, 0xE0); \
size_t writeJmpLoc = (size_t)(location); \
WRITE_MEMORY(writeJmpLoc, uint8_t, 0x48, 0xB8); \
WRITE_MEMORY(writeJmpLoc + 2, uint64_t, (uint64_t)(function)); \
WRITE_MEMORY(writeJmpLoc + 10, uint8_t, 0xFF, 0xE0); \
} while(0)

#define WRITE_CALL(location, function) \
do { \
WRITE_MEMORY((size_t)(location), uint8_t, 0x48, 0xB8); \
WRITE_MEMORY((size_t)(location) + 2, uint64_t, (uint64_t)(function)); \
WRITE_MEMORY((size_t)(location) + 10, uint8_t, 0xFF, 0xD0); \
size_t writeCallLoc = (size_t)(location); \
WRITE_MEMORY(writeCallLoc, uint8_t, 0x48, 0xB8); \
WRITE_MEMORY(writeCallLoc + 2, uint64_t, (uint64_t)(function)); \
WRITE_MEMORY(writeCallLoc + 10, uint8_t, 0xFF, 0xD0); \
} while(0)

#define WRITE_NOP(location, count) \
do { \
DWORD oldProtect; \
VirtualProtect((void*)(location), (size_t)(count), PAGE_EXECUTE_READWRITE, &oldProtect); \
for (size_t i = 0; i < (size_t)(count); i++) \
*((uint8_t*)(location) + i) = 0x90; \
VirtualProtect((void*)(location), (size_t)(count), oldProtect, &oldProtect); \
void* writeNopLoc = (void*)(location); \
size_t writeNopCount = (size_t)(count); \
DWORD writeNopOldProtect; \
VirtualProtect(writeNopLoc, writeNopCount, PAGE_EXECUTE_READWRITE, &writeNopOldProtect); \
for (size_t i = 0; i < writeNopCount; i++) \
*((uint8_t*)writeNopLoc + i) = 0x90; \
VirtualProtect(writeNopLoc, writeNopCount, writeNopOldProtect, &writeNopOldProtect); \
} while(0)

0 comments on commit 6162d2e

Please sign in to comment.