Skip to content

Commit 6162d2e

Browse files
Decrease the size of compiled binary further.
1 parent 1b59fdc commit 6162d2e

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

Dependencies/Helpers.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,37 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr);
5656

5757
#define WRITE_MEMORY(location, type, ...) \
5858
do { \
59-
const type data[] = { __VA_ARGS__ }; \
60-
DWORD oldProtect; \
61-
VirtualProtect((void*)(location), sizeof(data), PAGE_EXECUTE_READWRITE, &oldProtect); \
62-
memcpy((void*)(location), data, sizeof(data)); \
63-
VirtualProtect((void*)(location), sizeof(data), oldProtect, &oldProtect); \
59+
void* writeMemLoc = (void*)(location); \
60+
const type writeMemData[] = { __VA_ARGS__ }; \
61+
DWORD writeMemOldProtect; \
62+
VirtualProtect(writeMemLoc, sizeof(writeMemData), PAGE_EXECUTE_READWRITE, &writeMemOldProtect); \
63+
memcpy(writeMemLoc, writeMemData, sizeof(writeMemData)); \
64+
VirtualProtect(writeMemLoc, sizeof(writeMemData), writeMemOldProtect, &writeMemOldProtect); \
6465
} while(0)
6566

6667
#define WRITE_JUMP(location, function) \
6768
do { \
68-
WRITE_MEMORY((size_t)(location), uint8_t, 0x48, 0xB8); \
69-
WRITE_MEMORY((size_t)(location) + 2, uint64_t, (uint64_t)(function)); \
70-
WRITE_MEMORY((size_t)(location) + 10, uint8_t, 0xFF, 0xE0); \
69+
size_t writeJmpLoc = (size_t)(location); \
70+
WRITE_MEMORY(writeJmpLoc, uint8_t, 0x48, 0xB8); \
71+
WRITE_MEMORY(writeJmpLoc + 2, uint64_t, (uint64_t)(function)); \
72+
WRITE_MEMORY(writeJmpLoc + 10, uint8_t, 0xFF, 0xE0); \
7173
} while(0)
7274

7375
#define WRITE_CALL(location, function) \
7476
do { \
75-
WRITE_MEMORY((size_t)(location), uint8_t, 0x48, 0xB8); \
76-
WRITE_MEMORY((size_t)(location) + 2, uint64_t, (uint64_t)(function)); \
77-
WRITE_MEMORY((size_t)(location) + 10, uint8_t, 0xFF, 0xD0); \
77+
size_t writeCallLoc = (size_t)(location); \
78+
WRITE_MEMORY(writeCallLoc, uint8_t, 0x48, 0xB8); \
79+
WRITE_MEMORY(writeCallLoc + 2, uint64_t, (uint64_t)(function)); \
80+
WRITE_MEMORY(writeCallLoc + 10, uint8_t, 0xFF, 0xD0); \
7881
} while(0)
7982

8083
#define WRITE_NOP(location, count) \
8184
do { \
82-
DWORD oldProtect; \
83-
VirtualProtect((void*)(location), (size_t)(count), PAGE_EXECUTE_READWRITE, &oldProtect); \
84-
for (size_t i = 0; i < (size_t)(count); i++) \
85-
*((uint8_t*)(location) + i) = 0x90; \
86-
VirtualProtect((void*)(location), (size_t)(count), oldProtect, &oldProtect); \
85+
void* writeNopLoc = (void*)(location); \
86+
size_t writeNopCount = (size_t)(count); \
87+
DWORD writeNopOldProtect; \
88+
VirtualProtect(writeNopLoc, writeNopCount, PAGE_EXECUTE_READWRITE, &writeNopOldProtect); \
89+
for (size_t i = 0; i < writeNopCount; i++) \
90+
*((uint8_t*)writeNopLoc + i) = 0x90; \
91+
VirtualProtect(writeNopLoc, writeNopCount, writeNopOldProtect, &writeNopOldProtect); \
8792
} while(0)

0 commit comments

Comments
 (0)