libPeConv
A library to load, manipulate, dump PE files.
buffer_util.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
5//
6// validate pointer:
7//
8
9bool peconv::validate_ptr(IN const void* buffer_bgn, IN SIZE_T buffer_size, IN const void* field_bgn, IN SIZE_T field_size)
10{
11 if (buffer_bgn == nullptr || field_bgn == nullptr) {
12 return false;
13 }
14 BYTE* _start = (BYTE*)buffer_bgn;
15 BYTE* _end = _start + buffer_size;
16
17 BYTE* _field_start = (BYTE*)field_bgn;
18 BYTE* _field_end = (BYTE*)field_bgn + field_size;
19
20 if (_field_start < _start) {
21 return false;
22 }
23 if (_field_end > _end) {
24 return false;
25 }
26 return true;
27}
28
29//-----------------------------------------------------------------------------------
30//
31// alloc/free unaligned buffers:
32//
33
34//allocates a buffer that does not have to start from the beginning of the section
36{
37 if (!buf_size) return NULL;
38
39 UNALIGNED_BUF buf = (UNALIGNED_BUF) calloc(buf_size, sizeof(BYTE));
40 return buf;
41}
42
44{
45 free(section_buffer);
46}
47
48//
49// alloc/free aligned buffers:
50//
51
52peconv::ALIGNED_BUF peconv::alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base)
53{
54 if (!buffer_size) return NULL;
55
56 ALIGNED_BUF buf = (ALIGNED_BUF) VirtualAlloc((LPVOID) desired_base, buffer_size, MEM_COMMIT | MEM_RESERVE, protect);
57 return buf;
58}
59
60bool peconv::free_aligned(peconv::ALIGNED_BUF buffer, size_t buffer_size)
61{
62 if (buffer == nullptr) return true;
63 if (!VirtualFree(buffer, 0, MEM_RELEASE)) {
64#ifdef _DEBUG
65 std::cerr << "Releasing failed" << std::endl;
66#endif
67 return false;
68 }
69 return true;
70}
71
72//-----------------------------------------------------------------------------------
73//
74// wrappers using appropriate buffer type according to the purpose:
75//
76
77// allocate a buffer for PE module:
78peconv::ALIGNED_BUF peconv::alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base)
79{
80 return alloc_aligned(buffer_size, protect, desired_base);
81}
82
83// Free loaded PE module
84bool peconv::free_pe_buffer(peconv::ALIGNED_BUF buffer, size_t buffer_size)
85{
86 return peconv::free_aligned(buffer, buffer_size);
87}
88
Definitions of the used buffer types. Functions for their allocation and deallocation.
bool validate_ptr(IN const void *buffer_bgn, IN SIZE_T buffer_size, IN const void *field_bgn, IN SIZE_T field_size)
Definition: buffer_util.cpp:9
bool free_aligned(ALIGNED_BUF buffer, size_t buffer_size=0)
Definition: buffer_util.cpp:60
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
Definition: buffer_util.cpp:52
PBYTE ALIGNED_BUF
Definition: buffer_util.h:41
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
Definition: buffer_util.cpp:35
bool free_pe_buffer(ALIGNED_BUF buffer, size_t buffer_size=0)
Definition: buffer_util.cpp:84
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
Definition: buffer_util.cpp:78
PBYTE UNALIGNED_BUF
Definition: buffer_util.h:36
void free_unaligned(UNALIGNED_BUF section_buffer)
Definition: buffer_util.cpp:43