libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1
6#pragma once
7#include <limits>
8
9#include "file_util.h"
10#include "resource_util.h"
11
12namespace peconv {
19 bool is_padding(const BYTE* cave_ptr, size_t cave_size, const BYTE padding_char);
20
24 DWORD get_process_id(HANDLE hProcess);
25
33 bool is_mem_accessible(LPCVOID areaStart, SIZE_T areaSize, DWORD accessRights);
34
40 bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize);
41
42 template <typename INT_TYPE>
43 INT_TYPE round_up_to_unit(const INT_TYPE size, const INT_TYPE unit)
44 {
45 if (unit == 0) return size;
46
47 INT_TYPE rem = size % unit;
48 if (rem == 0) return size;
49
50 const INT_TYPE addend = unit - rem;
51 if (size > (std::numeric_limits<INT_TYPE>::max)() - addend) {
52 return size;
53 }
54 return size + addend;
55 }
56
60 template <typename CHAR_T>
61 bool is_valid_string(LPVOID modulePtr, const size_t moduleSize, const CHAR_T* name_ptr, const size_t max_len = 260)
62 {
63 bool is_terminated = false;
64 size_t i = 0;
65 for (; i < max_len; i++) {
66 if (!peconv::validate_ptr(modulePtr, moduleSize, &name_ptr[i], sizeof(CHAR_T))) {
67 return false;
68 }
69 if (name_ptr[i] == 0) {
70 is_terminated = true;
71 break;
72 }
73 }
74 return is_terminated && (i != 0);
75 }
76
77 template <typename CHAR_T>
78 inline CHAR_T to_lowercase(CHAR_T c1)
79 {
80 if (c1 <= CHAR_T('Z') && c1 >= CHAR_T('A')) {
81 c1 = (c1 - CHAR_T('A')) + CHAR_T('a');
82 }
83 return c1;
84 }
85};
Functions related to operations on files. Wrappers for read/write.
DWORD get_process_id(HANDLE hProcess)
Definition: util.cpp:82
bool is_valid_string(LPVOID modulePtr, const size_t moduleSize, const CHAR_T *name_ptr, const size_t max_len=260)
Definition: util.h:61
bool is_mem_accessible(LPCVOID areaStart, SIZE_T areaSize, DWORD accessRights)
Definition: util.cpp:116
CHAR_T to_lowercase(CHAR_T c1)
Definition: util.h:78
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 is_padding(const BYTE *cave_ptr, size_t cave_size, const BYTE padding_char)
Definition: util.cpp:106
INT_TYPE round_up_to_unit(const INT_TYPE size, const INT_TYPE unit)
Definition: util.h:43
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
Definition: util.cpp:156
Functions related to manual retrieving of PE resources.