|
| 1 | +// pe-file: library for reading and manipulating pe-files |
| 2 | +// Copyright (C) 2012 Jeffrey Bush jeff@coderforlife.com |
| 3 | +// |
| 4 | +// This library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | + |
| 18 | +#ifdef __cplusplus_cli |
| 19 | +#pragma unmanaged |
| 20 | +#endif |
| 21 | +#include "PEDataSource.h" |
| 22 | + |
| 23 | +#include <stdlib.h> |
| 24 | +#include <memory.h> |
| 25 | + |
| 26 | +#ifdef USE_WINDOWS_API |
| 27 | +#ifdef ARRAYSIZE |
| 28 | +#undef ARRAYSIZE |
| 29 | +#endif |
| 30 | +#define WIN32_LEAN_AND_MEAN |
| 31 | +#include <Windows.h> |
| 32 | +#else |
| 33 | +#include <sys/mman.h> |
| 34 | +#include <sys/stat.h> |
| 35 | +#endif |
| 36 | + |
| 37 | +using namespace PE; |
| 38 | + |
| 39 | +RawDataSource::RawDataSource(pntr data, size_t size, bool readonly) : readonly(readonly), orig_data(data), sz(size) { |
| 40 | + if (readonly) { |
| 41 | +#ifdef USE_WINDOWS_API |
| 42 | + DWORD old_protect = 0; |
| 43 | + if ((this->d = (bytes)VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) == NULL || |
| 44 | + memcpy(this->d, this->orig_data, size) == NULL || !VirtualProtect(this->d, size, PAGE_READONLY, &old_protect)) { this->close(); } |
| 45 | +#else |
| 46 | + if ((this->d = (bytes)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED || |
| 47 | + memcpy(this->d, this->orig_data, size) == NULL || mprotect(this->d, size, PROT_READ) == -1) { if (this->d == MAP_FAILED) { this->d = NULL; } this->close(); } |
| 48 | +#endif |
| 49 | + } else { |
| 50 | + this->d = this->orig_data; |
| 51 | + } |
| 52 | +} |
| 53 | +RawDataSource::~RawDataSource() { this->close(); } |
| 54 | +bool RawDataSource::isreadonly() const { return this->readonly; }; |
| 55 | +bool RawDataSource::flush() { return true; } |
| 56 | +pntr RawDataSource::data() { return this->d; } |
| 57 | +size_t RawDataSource::size() const { return this->sz; } |
| 58 | +void RawDataSource::close() { |
| 59 | + if (this->d) { |
| 60 | + this->flush(); |
| 61 | + if (this->readonly) { |
| 62 | +#ifdef USE_WINDOWS_API |
| 63 | + VirtualFree(this->d, 0, MEM_RELEASE); |
| 64 | +#else |
| 65 | + munmap(this->d, this->sz); |
| 66 | +#endif |
| 67 | + } |
| 68 | + this->d = NULL; |
| 69 | + } |
| 70 | + if (this->orig_data) { free(this->orig_data); this->orig_data = NULL; } |
| 71 | + this->sz = 0; |
| 72 | +} |
| 73 | +bool RawDataSource::resize(size_t new_size) { |
| 74 | + if (this->readonly) { return false; } |
| 75 | + if (new_size == this->sz) { return true; } |
| 76 | + this->flush(); |
| 77 | + this->d = (bytes)realloc(this->orig_data, new_size); |
| 78 | + if (!this->d) { this->close(); return false; } |
| 79 | + this->orig_data = this->d; |
| 80 | + if (new_size < this->sz) |
| 81 | + memset((bytes)this->d+this->sz, 0, new_size-this->sz); // set new memory to 0 |
| 82 | + this->sz = new_size; |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +#pragma region Memory Map Management Functions |
| 87 | +/////////////////////////////////////////////////////////////////////////////// |
| 88 | +///// Memory Map Management Functions |
| 89 | +/////////////////////////////////////////////////////////////////////////////// |
| 90 | +#include <map> |
| 91 | +#include <vector> |
| 92 | +using namespace std; |
| 93 | +typedef map<const_str, vector<pntr> > MMFs; |
| 94 | +static MMFs mmfs; |
| 95 | +static void _RemoveMMF(MMFs &mmfs, const_str file, pntr x) { |
| 96 | + MMFs::iterator v = mmfs.find(file); |
| 97 | + if (v != mmfs.end()) { |
| 98 | + size_t size = v->second.size(); |
| 99 | + for (size_t i = 0; i < size; ++i) { |
| 100 | + if (v->second[i] == x) { |
| 101 | + if (size == 1) { |
| 102 | + mmfs.erase(v); |
| 103 | + } else { |
| 104 | + if (i != size-1) // move the last element up |
| 105 | + v->second[i] = v->second[size-1]; |
| 106 | + v->second.pop_back(); // remove the last element |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +static pntr AddMMF (const_str file, pntr mm) { if (mm != NULL && mm != (pntr)-1) { mmfs[file].push_back(mm); } return mm; } |
| 114 | +static void RemoveMMF(const_str file, pntr mm) { _RemoveMMF(mmfs, file, mm); } |
| 115 | + |
| 116 | +#ifdef USE_WINDOWS_API |
| 117 | +static MMFs mmfViews; |
| 118 | +typedef BOOL (WINAPI *UNMAP_OR_CLOSE)(void*); |
| 119 | +static pntr AddMMFView(const_str file, pntr view) { if (view != NULL) mmfViews[file].push_back(view); return view; } |
| 120 | +static void RemoveMMFView(const_str file, pntr view) { _RemoveMMF(mmfViews, file, view); } |
| 121 | +static void _UnmapAll(MMFs &mmfs, const_str file, UNMAP_OR_CLOSE func) { |
| 122 | + MMFs::iterator v = mmfs.find(file); |
| 123 | + if (v != mmfs.end()) { |
| 124 | + size_t size = v->second.size(); |
| 125 | + for (size_t i = 0; i < size; ++i) |
| 126 | + func(v->second[i]); |
| 127 | + mmfs.erase(v); |
| 128 | + } |
| 129 | +} |
| 130 | +void MemoryMappedDataSource::UnmapAllViewsOfFile(const_str file) { |
| 131 | + _UnmapAll(mmfs, file, &CloseHandle); |
| 132 | + _UnmapAll(mmfViews, file, (UNMAP_OR_CLOSE)&UnmapViewOfFile); |
| 133 | +} |
| 134 | +#else |
| 135 | +void MemoryMappedDataSource::UnmapAllViewsOfFile(const_str file) { |
| 136 | + MMFs::iterator v = mmfs.find(file); |
| 137 | + if (v != mmfs.end()) { |
| 138 | + size_t size = v->second.size(); |
| 139 | + for (size_t i = 0; i < size; ++i) |
| 140 | + munmap(v->second[i]); |
| 141 | + mmfs.erase(v); |
| 142 | + } |
| 143 | +} |
| 144 | +#endif |
| 145 | +#pragma endregion |
| 146 | + |
| 147 | +bool MemoryMappedDataSource::map() { |
| 148 | +#ifdef USE_WINDOWS_API |
| 149 | + return |
| 150 | + (this->hMap = AddMMF(this->original, CreateFileMapping(this->hFile, NULL, (readonly ? PAGE_READONLY : PAGE_READWRITE), 0, 0, NULL))) != NULL && |
| 151 | + (this->d = AddMMFView(this->original, MapViewOfFile(this->hMap, (readonly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), 0, 0, 0))) != NULL; |
| 152 | +#else |
| 153 | + return (this->d = AddMMF(this->original, mmap(NULL, this->sz, (readonly ? PROT_READ : PROT_READ | PROT_WRITE), (readonly ? MAP_PRIVATE : MAP_SHARED), this->fd, 0))) != MAP_FAILED; |
| 154 | +#endif |
| 155 | +} |
| 156 | +void MemoryMappedDataSource::unmap() { |
| 157 | +#ifdef USE_WINDOWS_API |
| 158 | + if (this->hMap) { |
| 159 | + if (this->d) |
| 160 | + { |
| 161 | + this->flush(); |
| 162 | + UnmapViewOfFile(this->d); |
| 163 | + RemoveMMFView(this->original, this->d); |
| 164 | + this->d = NULL; |
| 165 | + } |
| 166 | + RemoveMMF(this->original, this->hMap); |
| 167 | + CloseHandle(this->hMap); |
| 168 | + this->hMap = NULL; |
| 169 | + } |
| 170 | +#else |
| 171 | + if (this->d == MAP_FAILED) { this->d = NULL; } |
| 172 | + else if (this->d) |
| 173 | + { |
| 174 | + this->flush(); |
| 175 | + munmap(this->d, this->sz); |
| 176 | + RemoveMMF(this->original, this->d); |
| 177 | + this->d = NULL; |
| 178 | + } |
| 179 | +#endif |
| 180 | +} |
| 181 | +#ifdef USE_WINDOWS_API |
| 182 | +MemoryMappedDataSource::MemoryMappedDataSource(const_str file, bool readonly) : readonly(readonly), hFile(INVALID_HANDLE_VALUE), hMap(NULL), d(NULL), sz(0) { |
| 183 | +#else |
| 184 | +MemoryMappedDataSource::MemoryMappedDataSource(const_str file, bool readonly) : readonly(readonly), fd(-1), d(NULL), sz(0) { |
| 185 | +#endif |
| 186 | + this->original[0] = 0; |
| 187 | +#ifdef USE_WINDOWS_API |
| 188 | + if (!GetFullPathName(file, ARRAYSIZE(this->original), this->original, NULL) || |
| 189 | + (this->hFile = CreateFile(this->original, (readonly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE)), FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE || |
| 190 | + (this->sz = GetFileSize(this->hFile, 0)) == INVALID_FILE_SIZE) |
| 191 | + { |
| 192 | + this->close(); |
| 193 | + } |
| 194 | +#else |
| 195 | + struct stat sb; |
| 196 | + if ((_wrealpath(file, this->original) == NULL) || |
| 197 | + (this->fd = _wopen(this->original, (readonly ? O_RDONLY : O_RDWR))) == -1 || |
| 198 | + fstat(this->fd, &sb) == -1) |
| 199 | + { |
| 200 | + this->close(); |
| 201 | + } |
| 202 | + this->sz = sb.st_size; |
| 203 | +#endif |
| 204 | + if (!map()) |
| 205 | + this->close(); |
| 206 | +} |
| 207 | +MemoryMappedDataSource::~MemoryMappedDataSource() { this->close(); } |
| 208 | + |
| 209 | +bool MemoryMappedDataSource::isreadonly() const { return this->readonly; }; |
| 210 | +bool MemoryMappedDataSource::flush() { |
| 211 | +#ifdef USE_WINDOWS_API |
| 212 | + return !this->readonly && FlushViewOfFile(this->d, 0) && FlushFileBuffers(this->hFile); |
| 213 | +#else |
| 214 | + return !this->readonly && msync(this->d, this->sz, MS_SYNC | MS_INVALIDATE) != -1; |
| 215 | +#endif |
| 216 | +} |
| 217 | + |
| 218 | +pntr MemoryMappedDataSource::data() { return this->d; } |
| 219 | +size_t MemoryMappedDataSource::size() const { return this->sz; } |
| 220 | +void MemoryMappedDataSource::close() { |
| 221 | + this->unmap(); |
| 222 | +#ifdef USE_WINDOWS_API |
| 223 | + if (this->hFile != INVALID_HANDLE_VALUE) { CloseHandle(this->hFile); this->hFile = INVALID_HANDLE_VALUE; } |
| 224 | +#else |
| 225 | + if (this->fd != -1) { close(this->fd); this->fd = -1; } |
| 226 | +#endif |
| 227 | + this->sz = 0; |
| 228 | +} |
| 229 | +bool MemoryMappedDataSource::resize(size_t new_size) { |
| 230 | + if (this->readonly) { return false; } |
| 231 | + if (new_size == this->sz) { return true; } |
| 232 | + this->unmap(); |
| 233 | +#ifdef USE_WINDOWS_API |
| 234 | + if (SetFilePointer(this->hFile, (uint32_t)new_size, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER || !SetEndOfFile(this->hFile) || !this->map()) { this->close(); return false; } |
| 235 | + if (new_size > this->sz) |
| 236 | + memset((bytes)this->d+this->sz, 0, new_size-this->sz); // set new memory to 0 (I am unsure if Windows does this automatically like Linux does) |
| 237 | +#else |
| 238 | + if (ftruncate(this->fd, new_size) == -1 || !this->map()) { this->close(); return false; } |
| 239 | +#endif |
| 240 | + this->sz = new_size; |
| 241 | + return true; |
| 242 | +} |
0 commit comments