11 HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
12 if(file == INVALID_HANDLE_VALUE) {
16 HANDLE mapping = CreateFileMapping(file, 0, PAGE_READONLY, 0, 0, 0);
18 LOG_ERROR(
"Could not create file mapping.");
22 BYTE *dllRawData = (BYTE*) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
29 size_t r_size = GetFileSize(file, 0);
30 if (r_size == INVALID_FILE_SIZE) {
32 UnmapViewOfFile(dllRawData);
37 if (read_size != 0 && read_size <= r_size) {
42 UnmapViewOfFile(dllRawData);
48 if (localCopyAddress !=
nullptr) {
49 memcpy(localCopyAddress, dllRawData, r_size);
53 LOG_ERROR(
"Could not allocate memory in the current process.");
55 UnmapViewOfFile(dllRawData);
58 return localCopyAddress;
64 HANDLE file = CreateFile(in_path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
65 if (file == INVALID_HANDLE_VALUE) {
66 LOG_ERROR(
"Cannot open the file for reading.");
69 DWORD r_size = GetFileSize(file, 0);
70 if (r_size == INVALID_FILE_SIZE) {
75 if (read_size != 0 && read_size <= r_size) {
85 if (!ReadFile(file, buffer, r_size, &out_size,
nullptr)) {
99 if (!out_path || !dump_data || !dump_size)
return false;
101 HANDLE file = CreateFile(out_path, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
102 if (file == INVALID_HANDLE_VALUE) {
103 LOG_ERROR(
"Cannot open the file for writing.");
107 LOG_WARNING(
"Dump size exceeds DWORD range and will be truncated.");
109 DWORD written_size = 0;
110 bool is_dumped =
false;
111 if (WriteFile(file, dump_data, (DWORD) dump_size, &written_size,
nullptr)) {
112 if (written_size != dump_size) {
113 LOG_WARNING(
"The written size %llu is different than the requested size: %llu.", (
unsigned long long)written_size, (
unsigned long long)dump_size);
120 LOG_ERROR(
"Failed to write to the file.");
134 size_t found = str.find_last_of(
"/\\");
135 if (found == std::string::npos) {
138 return str.substr(found + 1);
143 size_t found = str.find_last_of(
"/\\");
144 if (found == std::string::npos) {
147 return str.substr(0, found);
152 size_t len = str.length();
153 size_t ext_pos = len;
154 for (
size_t k = len; k != 0; k--) {
Definitions of the used buffer types. Functions for their allocation and deallocation.
#define MASK_TO_DWORD(val)
Functions related to operations on files. Wrappers for read/write.
Compile-time configurable logging macros for peconv.
#define LOG_ERROR(fmt,...)
#define LOG_WARNING(fmt,...)
bool dump_to_file(IN LPCTSTR path, IN PBYTE dump_data, IN size_t dump_size)
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
peconv::UNALIGNED_BUF load_file(IN LPCTSTR filename, OUT size_t &r_size)
std::string get_directory_name(IN const std::string full_path)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
void free_file(IN peconv::UNALIGNED_BUF buffer)
size_t find_extension_pos(IN const std::string str)
peconv::UNALIGNED_BUF read_from_file(IN LPCTSTR path, IN OUT size_t &read_size)
std::string get_file_name(IN const std::string full_path)
void free_unaligned(UNALIGNED_BUF section_buffer)
Miscellaneous utility functions.