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