Skip to content

Commit d8b02e0

Browse files
committed
[REFACT] Cleaned up types to avoid MinGW warnings
1 parent 1222db3 commit d8b02e0

11 files changed

+65
-65
lines changed

libpeconv/include/peconv/buffer_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace peconv {
6666
/**
6767
Allocates a buffer of a virtual memory (using VirtualAlloc). Can be used in the cases when the buffer have to be aligned to the beginning of a page.
6868
*/
69-
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL);
69+
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, void* desired_base=nullptr);
7070

7171
/**
7272
Frees buffer allocated by alloc_aligned.
@@ -78,7 +78,7 @@ namespace peconv {
7878
/**
7979
Allocates an aligned buffer for a PE file.
8080
*/
81-
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL);
81+
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, void* desired_base=nullptr);
8282

8383
/**
8484
Free the memory allocated with alloc_pe_buffer.

libpeconv/include/peconv/pe_loader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace peconv {
1616
If the relocate flag is true, applies relocations. Does not load imports.
1717
Automatically allocates buffer of the needed size (the size is returned in outputSize). The buffer can be freed by the function free_pe_buffer.
1818
*/
19-
BYTE* load_pe_module(BYTE* payload_raw, size_t r_size, OUT size_t &v_size, bool executable, bool relocate, ULONGLONG desired_base = 0);
19+
BYTE* load_pe_module(BYTE* payload_raw, size_t r_size, OUT size_t &v_size, bool executable, bool relocate, ULONG_PTR desired_base = 0);
2020

2121
/**
2222
Reads PE from the given file into memory and maps it into vitual format.
@@ -25,13 +25,13 @@ namespace peconv {
2525
If the relocate flag is true, applies relocations. Does not load imports.
2626
Automatically allocates buffer of the needed size (the size is returned in outputSize). The buffer can be freed by the function free_pe_buffer.
2727
*/
28-
BYTE* load_pe_module(LPCTSTR filename, OUT size_t &v_size, bool executable, bool relocate, ULONGLONG desired_base = 0);
28+
BYTE* load_pe_module(LPCTSTR filename, OUT size_t &v_size, bool executable, bool relocate, ULONG_PTR desired_base = 0);
2929

3030
/**
3131
Loads full PE from the raw buffer in a way in which it can be directly executed: remaps to virual format, applies relocations, loads imports.
3232
Allows for supplying custom function resolver.
3333
*/
34-
BYTE* load_pe_executable(BYTE* payload_raw, size_t r_size, OUT size_t &v_size, t_function_resolver* import_resolver = nullptr, ULONGLONG desired_base = 0);
34+
BYTE* load_pe_executable(BYTE* payload_raw, size_t r_size, OUT size_t &v_size, t_function_resolver* import_resolver = nullptr, ULONG_PTR desired_base = 0);
3535

3636
/**
3737
Loads full PE from file in a way in which it can be directly executed: remaps to virtual format, applies relocations, loads imports.

libpeconv/include/peconv/pe_raw_to_virtual.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace peconv {
2424
IN size_t rawPeSize,
2525
OUT size_t &outputSize,
2626
IN OPTIONAL bool executable = true,
27-
IN OPTIONAL ULONGLONG desired_base = 0
27+
IN OPTIONAL ULONG_PTR desired_base = 0
2828
);
2929

3030
}; // namespace peconv

libpeconv/src/buffer_util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ void peconv::free_unaligned(peconv::UNALIGNED_BUF section_buffer)
5353
// alloc/free aligned buffers:
5454
//
5555

56-
peconv::ALIGNED_BUF peconv::alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base)
56+
peconv::ALIGNED_BUF peconv::alloc_aligned(size_t buffer_size, DWORD protect, void* desired_base)
5757
{
5858
if (!buffer_size) return NULL;
5959

60-
ALIGNED_BUF buf = (ALIGNED_BUF) VirtualAlloc((LPVOID) desired_base, buffer_size, MEM_COMMIT | MEM_RESERVE, protect);
60+
ALIGNED_BUF buf = (ALIGNED_BUF) VirtualAlloc(desired_base, buffer_size, MEM_COMMIT | MEM_RESERVE, protect);
6161
return buf;
6262
}
6363

@@ -79,7 +79,7 @@ bool peconv::free_aligned(peconv::ALIGNED_BUF buffer, size_t buffer_size)
7979
//
8080

8181
// allocate a buffer for PE module:
82-
peconv::ALIGNED_BUF peconv::alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base)
82+
peconv::ALIGNED_BUF peconv::alloc_pe_buffer(size_t buffer_size, DWORD protect, void* desired_base)
8383
{
8484
return alloc_aligned(buffer_size, protect, desired_base);
8585
}

libpeconv/src/delayed_imports_loader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool parse_delayed_desc(BYTE* modulePtr, const size_t moduleSize,
4141
T_FIELD* record_va = (T_FIELD*)((ULONGLONG)modulePtr + iat_addr);
4242
T_IMAGE_THUNK_DATA* thunk_va = (T_IMAGE_THUNK_DATA*)((ULONGLONG)modulePtr + thunk_addr);
4343

44-
for (; *record_va != NULL && thunk_va != NULL; record_va++, thunk_va++) {
44+
for (; (*record_va) && thunk_va; record_va++, thunk_va++) {
4545
if (!peconv::validate_ptr(modulePtr, moduleSize, record_va, sizeof(T_FIELD))) {
4646
return false;
4747
}
@@ -131,7 +131,7 @@ bool peconv::load_delayed_imports(BYTE* modulePtr, ULONGLONG moduleBase, t_funct
131131
for (size_t i = 0; i < max_count; i++) {
132132
IMAGE_DELAYLOAD_DESCRIPTOR *desc = &first_desc[i];
133133
if (!validate_ptr(modulePtr, module_size, desc, sizeof(IMAGE_DELAYLOAD_DESCRIPTOR))) break;
134-
if (desc->DllNameRVA == NULL) {
134+
if (!desc->DllNameRVA) {
135135
break;
136136
}
137137
ULONGLONG dll_name_rva = desc->DllNameRVA;

libpeconv/src/fix_imports.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ size_t find_addresses_to_fill(FIELD_T call_via, FIELD_T thunk_addr, LPVOID modul
2626
}
2727
FIELD_T *thunk_val = reinterpret_cast<FIELD_T*>(thunk_ptr);
2828
FIELD_T *call_via_val = reinterpret_cast<FIELD_T*>(call_via_ptr);
29-
if (*call_via_val == 0) {
29+
if ((*call_via_val) == 0) {
3030
//nothing to fill, probably the last record
3131
break;
3232
}
@@ -217,14 +217,14 @@ bool peconv::fix_imports(IN OUT PVOID modulePtr, IN size_t moduleSize, IN const
217217
{
218218
bool skip_bound = false; // skip boud imports?
219219
IMAGE_DATA_DIRECTORY *importsDir = peconv::get_directory_entry((const BYTE*) modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
220-
if (importsDir == NULL) {
220+
if (!importsDir) {
221221
return true; // done! no imports -> nothing to fix
222222
}
223223
bool is64 = peconv::is64bit((BYTE*)modulePtr);
224224
DWORD maxSize = importsDir->Size;
225225
DWORD impAddr = importsDir->VirtualAddress;
226226

227-
IMAGE_IMPORT_DESCRIPTOR* lib_desc = NULL;
227+
IMAGE_IMPORT_DESCRIPTOR* lib_desc = nullptr;
228228
DWORD parsedSize = 0;
229229
#ifdef _DEBUG
230230
printf("---IMP---\n");
@@ -240,7 +240,7 @@ bool peconv::fix_imports(IN OUT PVOID modulePtr, IN size_t moduleSize, IN const
240240
return false;
241241
}
242242
parsedSize += sizeof(IMAGE_IMPORT_DESCRIPTOR);
243-
if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
243+
if (!lib_desc->OriginalFirstThunk && !lib_desc->FirstThunk) {
244244
break;
245245
}
246246
const bool is_bound = (lib_desc->TimeDateStamp == (-1));

libpeconv/src/imports_loader.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class CollectThunksCallback : public peconv::ImportThunksCallback
9393

9494
virtual bool processThunks(LPSTR libName, ULONG_PTR origFirstThunkPtr, ULONG_PTR va)
9595
{
96-
if (va == NULL) {
96+
if (!va) {
9797
return false; // invalid thunk
9898
}
9999
const ULONGLONG module_base = reinterpret_cast<ULONGLONG>(this->vBuf);
@@ -143,7 +143,7 @@ class CollectImportsCallback : public ImportThunksCallback
143143
template <typename T_FIELD, typename T_IMAGE_THUNK_DATA>
144144
bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA* desc, T_FIELD* call_via, T_FIELD ordinal_flag)
145145
{
146-
if (call_via == nullptr) {
146+
if (!call_via) {
147147
return false;
148148
}
149149

@@ -199,7 +199,7 @@ bool process_imp_functions_tpl(BYTE* modulePtr, size_t module_size, LPSTR lib_na
199199
if (!validate_ptr(modulePtr, module_size, desc, sizeof(T_IMAGE_THUNK_DATA))) {
200200
break;
201201
}
202-
if (desc->u1.Function == NULL) {
202+
if (!desc->u1.Function) {
203203
break;
204204
}
205205
T_FIELD ordinal_flag = (sizeof(T_FIELD) == sizeof(ULONGLONG)) ? IMAGE_ORDINAL_FLAG64 : IMAGE_ORDINAL_FLAG32;
@@ -233,7 +233,7 @@ bool process_dlls(BYTE* modulePtr, size_t module_size, IMAGE_IMPORT_DESCRIPTOR *
233233
if (!validate_ptr(modulePtr, module_size, lib_desc, sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
234234
break;
235235
}
236-
if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
236+
if (!lib_desc->OriginalFirstThunk && !lib_desc->FirstThunk) {
237237
break;
238238
}
239239
LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
@@ -243,7 +243,7 @@ bool process_dlls(BYTE* modulePtr, size_t module_size, IMAGE_IMPORT_DESCRIPTOR *
243243
}
244244
DWORD call_via = lib_desc->FirstThunk;
245245
DWORD thunk_addr = lib_desc->OriginalFirstThunk;
246-
if (thunk_addr == NULL) {
246+
if (!thunk_addr) {
247247
thunk_addr = lib_desc->FirstThunk;
248248
}
249249
#ifdef _DEBUG_EX
@@ -330,11 +330,11 @@ bool peconv::is_valid_import_name(const PBYTE modulePtr, const size_t moduleSize
330330
bool peconv::has_valid_import_table(const PBYTE modulePtr, size_t moduleSize)
331331
{
332332
IMAGE_DATA_DIRECTORY *importsDir = get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
333-
if (importsDir == NULL) return false;
333+
if (!importsDir) return false;
334334

335335
const DWORD impAddr = importsDir->VirtualAddress;
336336

337-
IMAGE_IMPORT_DESCRIPTOR* lib_desc = NULL;
337+
IMAGE_IMPORT_DESCRIPTOR* lib_desc = nullptr;
338338
DWORD parsedSize = 0;
339339
size_t valid_records = 0;
340340

@@ -345,15 +345,15 @@ bool peconv::has_valid_import_table(const PBYTE modulePtr, size_t moduleSize)
345345
}
346346
parsedSize += sizeof(IMAGE_IMPORT_DESCRIPTOR);
347347

348-
if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
348+
if (!lib_desc->OriginalFirstThunk && !lib_desc->FirstThunk) {
349349
break;
350350
}
351351
LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
352352
if (!is_valid_import_name(modulePtr, moduleSize, lib_name)) return false;
353353

354354
DWORD call_via = lib_desc->FirstThunk;
355355
DWORD thunk_addr = lib_desc->OriginalFirstThunk;
356-
if (thunk_addr == NULL) thunk_addr = lib_desc->FirstThunk;
356+
if (!thunk_addr) thunk_addr = lib_desc->FirstThunk;
357357

358358
DWORD *thunks = (DWORD*)((ULONGLONG)modulePtr + thunk_addr);
359359
if (!peconv::validate_ptr(modulePtr, moduleSize, thunks, sizeof(DWORD))) return false;

libpeconv/src/imports_uneraser.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ LPVOID search_name(std::string name, const char* modulePtr, size_t moduleSize)
99
const char* namec = name.c_str();
1010
const size_t searched_len = name.length() + 1; // with terminating NULL
1111
const char* found_ptr = std::search(modulePtr, modulePtr + moduleSize, namec, namec + searched_len);
12-
if (found_ptr == NULL) {
13-
return NULL;
12+
if (!found_ptr) {
13+
return nullptr;
1414
}
1515
size_t o = found_ptr - modulePtr;
1616
if (o < moduleSize) {
1717
return (LPVOID)(found_ptr);
1818
}
19-
return NULL;
19+
return nullptr;
2020
}
2121

2222
bool ImportsUneraser::writeFoundDllName(IMAGE_IMPORT_DESCRIPTOR* lib_desc, const std::string &found_name)
@@ -69,7 +69,7 @@ bool ImportsUneraser::findNameInBinaryAndFill(IMAGE_IMPORT_DESCRIPTOR* lib_desc,
6969
std::map<ULONGLONG, std::set<ExportedFunc>> &addr_to_func
7070
)
7171
{
72-
if (call_via_ptr == NULL || modulePtr == NULL || lib_desc == NULL) {
72+
if (!call_via_ptr || !modulePtr || !lib_desc) {
7373
return false; //malformed input
7474
}
7575
IMAGE_DATA_DIRECTORY *importsDir = get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
@@ -170,23 +170,23 @@ bool ImportsUneraser::fillImportNames(
170170
OUT OPTIONAL ImpsNotCovered* notCovered
171171
)
172172
{
173-
if (lib_desc == NULL) return false;
173+
if (!lib_desc) return false;
174174

175175
FIELD_T call_via = lib_desc->FirstThunk;
176-
if (call_via == NULL) return false;
176+
if (!call_via) return false;
177177

178178
size_t processed_imps = 0;
179179
size_t recovered_imps = 0;
180180

181181
FIELD_T thunk_addr = lib_desc->OriginalFirstThunk;
182-
if (thunk_addr == NULL) {
182+
if (!thunk_addr) {
183183
thunk_addr = call_via;
184184
}
185185

186186
BYTE* call_via_ptr = (BYTE*)((ULONGLONG)modulePtr + call_via);
187187
BYTE* thunk_ptr = (BYTE*)((ULONGLONG)modulePtr + thunk_addr);
188188
for (;
189-
call_via_ptr != NULL && thunk_ptr != NULL;
189+
call_via_ptr && thunk_ptr;
190190
call_via_ptr += sizeof(FIELD_T), thunk_ptr += sizeof(FIELD_T)
191191
)
192192
{
@@ -197,7 +197,7 @@ bool ImportsUneraser::fillImportNames(
197197
break;
198198
}
199199
IMAGE_THUNK_DATA_T* desc = (IMAGE_THUNK_DATA_T*)thunk_ptr;
200-
if (desc->u1.Function == NULL) {
200+
if (!desc->u1.Function) {
201201
break;
202202
}
203203
ULONGLONG searchedAddr = ULONGLONG(*call_via_val);

libpeconv/src/pe_hdrs_helper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ IMAGE_DATA_DIRECTORY* peconv::get_directory_entry(IN const BYTE *pe_buffer, IN D
143143
IMAGE_NT_HEADERS32* nt_headers64 = (IMAGE_NT_HEADERS32*)nt_headers;
144144
peDir = &(nt_headers64->OptionalHeader.DataDirectory[dir_id]);
145145
}
146-
if (!allow_empty && peDir->VirtualAddress == NULL) {
146+
if (!allow_empty && !peDir->VirtualAddress) {
147147
return nullptr;
148148
}
149149
return peDir;
@@ -457,7 +457,7 @@ WORD peconv::get_subsystem(IN const BYTE* payload)
457457
if (!payload) return 0;
458458

459459
BYTE* payload_nt_hdr = get_nt_hdrs(payload);
460-
if (payload_nt_hdr == NULL) {
460+
if (!payload_nt_hdr) {
461461
return 0;
462462
}
463463
const bool is64b = is64bit(payload);
@@ -604,7 +604,7 @@ DWORD peconv::calc_pe_size(IN const PBYTE pe_buffer, IN size_t pe_size, IN bool
604604

605605
bool peconv::is_valid_sectons_alignment(IN const BYTE* payload, IN const SIZE_T payload_size, IN bool is_raw)
606606
{
607-
if (payload == NULL) return false;
607+
if (!payload) return false;
608608

609609
const DWORD my_align = peconv::get_sec_alignment(payload, is_raw);
610610
if (my_align == 0) {

libpeconv/src/pe_loader.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ using namespace peconv;
1414
namespace peconv {
1515
BYTE* load_no_sec_pe(BYTE* dllRawData, size_t r_size, OUT size_t &v_size, bool executable)
1616
{
17-
ULONGLONG desired_base = 0;
17+
ULONG_PTR desired_base = 0;
1818
size_t out_size = (r_size < PAGE_SIZE) ? PAGE_SIZE : r_size;
1919
if (executable) {
2020
desired_base = get_image_base(dllRawData);
2121
out_size = peconv::get_image_size(dllRawData);
2222
}
2323
DWORD protect = (executable) ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
24-
BYTE* mappedPE = peconv::alloc_pe_buffer(out_size, protect, desired_base);
24+
BYTE* mappedPE = peconv::alloc_pe_buffer(out_size, protect, reinterpret_cast<void*>(desired_base));
2525
if (!mappedPE) {
26-
return NULL;
26+
return nullptr;
2727
}
2828
memcpy(mappedPE, dllRawData, r_size);
2929
v_size = out_size;
3030
return mappedPE;
3131
}
3232
};
3333

34-
BYTE* peconv::load_pe_module(BYTE* dllRawData, size_t r_size, OUT size_t &v_size, bool executable, bool relocate, ULONGLONG desired_base)
34+
BYTE* peconv::load_pe_module(BYTE* dllRawData, size_t r_size, OUT size_t &v_size, bool executable, bool relocate, ULONG_PTR desired_base)
3535
{
3636
if (!peconv::get_nt_hdrs(dllRawData, r_size)) {
37-
return NULL;
37+
return nullptr;
3838
}
3939
if (peconv::get_sections_count(dllRawData, r_size) == 0) {
4040
return load_no_sec_pe(dllRawData, r_size, v_size, executable);
@@ -53,35 +53,35 @@ BYTE* peconv::load_pe_module(BYTE* dllRawData, size_t r_size, OUT size_t &v_size
5353
// relocating was required, but it failed - thus, the full PE image is useless
5454
std::cerr << "[!] Could not relocate the module!\n";
5555
free_pe_buffer(mappedDLL, v_size);
56-
mappedDLL = NULL;
56+
mappedDLL = nullptr;
5757
}
5858
} else {
5959
std::cerr << "[!] Could not allocate memory at the desired base!\n";
6060
}
6161
return mappedDLL;
6262
}
6363

64-
BYTE* peconv::load_pe_module(LPCTSTR filename, OUT size_t &v_size, bool executable, bool relocate, ULONGLONG desired_base)
64+
BYTE* peconv::load_pe_module(LPCTSTR filename, OUT size_t &v_size, bool executable, bool relocate, ULONG_PTR desired_base)
6565
{
6666
size_t r_size = 0;
6767
BYTE *dllRawData = load_file(filename, r_size);
6868
if (!dllRawData) {
6969
#ifdef _DEBUG
7070
std::cerr << "Cannot load the file: " << filename << std::endl;
7171
#endif
72-
return NULL;
72+
return nullptr;
7373
}
7474
BYTE* mappedPE = load_pe_module(dllRawData, r_size, v_size, executable, relocate, desired_base);
7575
free_file(dllRawData);
7676
return mappedPE;
7777
}
7878

79-
BYTE* peconv::load_pe_executable(BYTE* dllRawData, size_t r_size, OUT size_t &v_size, t_function_resolver* import_resolver, ULONGLONG desired_base)
79+
BYTE* peconv::load_pe_executable(BYTE* dllRawData, size_t r_size, OUT size_t &v_size, t_function_resolver* import_resolver, ULONG_PTR desired_base)
8080
{
8181
BYTE* loaded_pe = load_pe_module(dllRawData, r_size, v_size, true, true, desired_base);
8282
if (!loaded_pe) {
8383
std::cerr << "[-] Loading failed!\n";
84-
return NULL;
84+
return nullptr;
8585
}
8686
#if _DEBUG
8787
printf("Loaded at: %p\n", loaded_pe);
@@ -116,7 +116,7 @@ BYTE* peconv::load_pe_executable(LPCTSTR my_path, OUT size_t &v_size, t_function
116116
if (!load_imports(loaded_pe, import_resolver)) {
117117
printf("[-] Loading imports failed!");
118118
free_pe_buffer(loaded_pe, v_size);
119-
return NULL;
119+
return nullptr;
120120
}
121121
return loaded_pe;
122122
}

0 commit comments

Comments
 (0)