Skip to content

Commit 4074f1e

Browse files
committed
Simplify conditional compiled code a bit.
1 parent caf9177 commit 4074f1e

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

MemoryModule.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ AlignValueUp(size_t value, size_t alignment) {
9696
return (value + alignment - 1) & ~(alignment - 1);
9797
}
9898

99-
#ifdef DEBUG_OUTPUT
100-
static void
99+
static inline void
101100
OutputLastError(const char *msg)
102101
{
102+
#ifndef DEBUG_OUTPUT
103+
UNREFERENCED_PARAMETER(msg);
104+
#else
103105
LPVOID tmp;
104106
char *tmpmsg;
105107
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
@@ -109,8 +111,8 @@ OutputLastError(const char *msg)
109111
OutputDebugString(tmpmsg);
110112
LocalFree(tmpmsg);
111113
LocalFree(tmp);
112-
}
113114
#endif
115+
}
114116

115117
static BOOL
116118
CheckSize(size_t size, size_t expected) {
@@ -240,9 +242,7 @@ FinalizeSection(PMEMORYMODULE module, PSECTIONFINALIZEDATA sectionData) {
240242

241243
// change memory access flags
242244
if (VirtualProtect(sectionData->address, sectionData->size, protect, &oldProtect) == 0) {
243-
#ifdef DEBUG_OUTPUT
244-
OutputLastError("Error protecting memory page")
245-
#endif
245+
OutputLastError("Error protecting memory page");
246246
return FALSE;
247247
}
248248

@@ -341,16 +341,10 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
341341
unsigned char *dest = codeBase + relocation->VirtualAddress;
342342
unsigned short *relInfo = (unsigned short *)((unsigned char *)relocation + IMAGE_SIZEOF_BASE_RELOCATION);
343343
for (i=0; i<((relocation->SizeOfBlock-IMAGE_SIZEOF_BASE_RELOCATION) / 2); i++, relInfo++) {
344-
DWORD *patchAddrHL;
345-
#ifdef _WIN64
346-
ULONGLONG *patchAddr64;
347-
#endif
348-
int type, offset;
349-
350344
// the upper 4 bits define the type of relocation
351-
type = *relInfo >> 12;
345+
int type = *relInfo >> 12;
352346
// the lower 12 bits define the offset
353-
offset = *relInfo & 0xfff;
347+
int offset = *relInfo & 0xfff;
354348

355349
switch (type)
356350
{
@@ -360,14 +354,18 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
360354

361355
case IMAGE_REL_BASED_HIGHLOW:
362356
// change complete 32 bit address
363-
patchAddrHL = (DWORD *) (dest + offset);
364-
*patchAddrHL += (DWORD) delta;
357+
{
358+
DWORD *patchAddrHL = (DWORD *) (dest + offset);
359+
*patchAddrHL += (DWORD) delta;
360+
}
365361
break;
366362

367363
#ifdef _WIN64
368364
case IMAGE_REL_BASED_DIR64:
369-
patchAddr64 = (ULONGLONG *) (dest + offset);
370-
*patchAddr64 += (ULONGLONG) delta;
365+
{
366+
ULONGLONG *patchAddr64 = (ULONGLONG *) (dest + offset);
367+
*patchAddr64 += (ULONGLONG) delta;
368+
}
371369
break;
372370
#endif
373371

@@ -528,10 +526,11 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
528526
}
529527

530528
#ifdef _WIN64
531-
if (old_header->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) {
529+
static const WORD HOST_MACHINE = IMAGE_FILE_MACHINE_AMD64;
532530
#else
533-
if (old_header->FileHeader.Machine != IMAGE_FILE_MACHINE_I386) {
531+
static const WORD HOST_MACHINE = IMAGE_FILE_MACHINE_I386;
534532
#endif
533+
if (old_header->FileHeader.Machine != HOST_MACHINE) {
535534
SetLastError(ERROR_BAD_EXE_FORMAT);
536535
return NULL;
537536
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy