libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
file_util.cpp
Go to the documentation of this file.
1#include "peconv/file_util.h"
3#include "peconv/util.h"
4
5#include <fstream>
6#ifdef _DEBUG
7 #include <iostream>
8#endif
9
10//load file content using MapViewOfFile
12{
15#ifdef _DEBUG
16 std::cerr << "Could not open file!" << std::endl;
17#endif
18 return nullptr;
19 }
21 if (!mapping) {
22#ifdef _DEBUG
23 std::cerr << "Could not create mapping!" << std::endl;
24#endif
26 return nullptr;
27 }
29 if (!dllRawData) {
30#ifdef _DEBUG
31 std::cerr << "Could not map view of file" << std::endl;
32#endif
35 return nullptr;
36 }
37 size_t r_size = GetFileSize(file, 0);
38 if (read_size != 0 && read_size <= r_size) {
40 }
42 std::cerr << "[-] Mapping of " << filename << " is invalid!" << std::endl;
46 return nullptr;
47 }
49 if (localCopyAddress != nullptr) {
52 } else {
53 read_size = 0;
54#ifdef _DEBUG
55 std::cerr << "Could not allocate memory in the current process" << std::endl;
56#endif
57 }
61 return localCopyAddress;
62}
63
64//load file content using ReadFile
66{
69#ifdef _DEBUG
70 std::cerr << "Cannot open the file for reading!" << std::endl;
71#endif
72 return nullptr;
73 }
75 if (read_size != 0 && read_size <= r_size) {
77 }
79 if (buffer == nullptr) {
80#ifdef _DEBUG
81 std::cerr << "Allocation has failed!" << std::endl;
82#endif
83 return nullptr;
84 }
85 DWORD out_size = 0;
86 if (!ReadFile(file, buffer, r_size, &out_size, nullptr)) {
87#ifdef _DEBUG
88 std::cerr << "Reading failed!" << std::endl;
89#endif
90 peconv::free_file(buffer);
91 buffer = nullptr;
92 read_size = 0;
93 } else {
95 }
97 return buffer;
98}
99
100//save the given buffer into a file
102{
103 if (!out_path || !dump_data || !dump_size) return false;
104
106 if (file == INVALID_HANDLE_VALUE) {
107#ifdef _DEBUG
108 std::cerr << "Cannot open the file for writing!" << std::endl;
109#endif
110 return false;
111 }
113 bool is_dumped = false;
114 if (WriteFile(file, dump_data, (DWORD) dump_size, &written_size, nullptr)) {
115 is_dumped = true;
116 }
117#ifdef _DEBUG
118 else {
119 std::cerr << "Failed to write to the file : " << out_path << std::endl;
120 }
121#endif
123 return is_dumped;
124}
125
126//free the buffer allocated by load_file/read_from_file
131
132std::string peconv::get_file_name(IN const std::string str)
133{
134 size_t found = str.find_last_of("/\\");
135 if (found == std::string::npos) {
136 return str;
137 }
138 return str.substr(found + 1);
139}
140
141std::string peconv::get_directory_name(IN const std::string str)
142{
143 size_t found = str.find_last_of("/\\");
144 if (found == std::string::npos) {
145 return "";
146 }
147 return str.substr(0, found);
148}
149
150size_t peconv::find_extension_pos(IN const std::string str)
151{
152 size_t len = str.length();
153 size_t ext_pos = len;
154 for (size_t k = len; k != 0; k--) {
155 size_t i = k - 1;
156 char c = str[i];
157 if (c == '.') {
158 ext_pos = i;
159 break;
160 }
161 }
162 return ext_pos;
163}
Definitions of the used buffer types. Functions for their allocation and deallocation.
#define MASK_TO_DWORD(val)
Definition buffer_util.h:12
bool parse_delayed_desc(BYTE *modulePtr, const size_t moduleSize, const ULONGLONG img_base, LPSTR lib_name, const T_FIELD ordinal_flag, IMAGE_DELAYLOAD_DESCRIPTOR *desc, peconv::t_function_resolver *func_resolver)
Functions related to operations on files. Wrappers for read/write.
bool dump_to_file(IN LPCTSTR path, IN PBYTE dump_data, IN size_t dump_size)
peconv::UNALIGNED_BUF load_file(IN LPCTSTR filename, OUT size_t &r_size)
Definition file_util.cpp:11
PBYTE ALIGNED_BUF
Definition buffer_util.h:46
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
std::string get_directory_name(IN const std::string full_path)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
Definition util.cpp:150
void free_file(IN peconv::UNALIGNED_BUF buffer)
peconv::UNALIGNED_BUF read_from_file(IN LPCTSTR path, IN OUT size_t &read_size)
Definition file_util.cpp:65
PBYTE UNALIGNED_BUF
Definition buffer_util.h:41
size_t find_extension_pos(IN const std::string str)
std::string get_file_name(IN const std::string full_path)
void free_unaligned(UNALIGNED_BUF section_buffer)
Miscellaneous utility functions.