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 "peconv/logger.h"
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 LOG_WARNING("Integer Overflow, limit exceeded! start_delta: %zu field_size: %zu area_size: %zu", start_delta, field_size, area_size);
26 return false;
27 }
28 return true;
29}
30
31//-----------------------------------------------------------------------------------
32//
33// alloc/free unaligned buffers:
34//
35
36//allocates a buffer that does not have to start from the beginning of the section
38{
39 if (!buf_size) return NULL;
40
41 UNALIGNED_BUF buf = (UNALIGNED_BUF) calloc(buf_size, sizeof(BYTE));
42 return buf;
43}
44
46{
47 free(section_buffer);
48}
49
50//
51// alloc/free aligned buffers:
52//
53
54peconv::ALIGNED_BUF peconv::alloc_aligned(size_t buffer_size, DWORD protect, void* desired_base)
55{
56 if (!buffer_size) return NULL;
57
58 ALIGNED_BUF buf = (ALIGNED_BUF) VirtualAlloc(desired_base, buffer_size, MEM_COMMIT | MEM_RESERVE, protect);
59 return buf;
60}
61
62bool peconv::free_aligned(peconv::ALIGNED_BUF buffer, size_t buffer_size)
63{
64 if (buffer == nullptr) return true;
65 if (!VirtualFree(buffer, 0, MEM_RELEASE)) {
66 LOG_ERROR("VirtualFree failed.");
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, void* 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.
#define LOG_ERROR(fmt,...)
Definition logger.h:36
#define LOG_WARNING(fmt,...)
Definition logger.h:44
bool free_aligned(ALIGNED_BUF buffer, size_t buffer_size=0)
PBYTE ALIGNED_BUF
Definition buffer_util.h:46
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, void *desired_base=nullptr)
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_aligned(size_t buffer_size, DWORD protect, void *desired_base=nullptr)
PBYTE UNALIGNED_BUF
Definition buffer_util.h:41
void free_unaligned(UNALIGNED_BUF section_buffer)