├── .gitignore
├── PEDataSource.cpp
├── PEDataSource.h
├── PEDataTypes.h
├── PEFile.cpp
├── PEFile.h
├── PEFileResources.cpp
├── PEFileResources.h
├── PEVersion.cpp
├── PEVersion.h
├── README.md
├── build-mingw.bat
├── build-msvc-debug.bat
├── build-msvc.bat
└── gpl.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files
2 | *.slo
3 | *.lo
4 | *.o
5 |
6 | # Compiled Dynamic libraries
7 | *.so
8 |
9 | # Compiled Static libraries
10 | *.lib
11 | *.lai
12 | *.la
13 | *.a
14 |
15 | # Compiled Symbol files
16 | *.pdb
--------------------------------------------------------------------------------
/PEDataSource.cpp:
--------------------------------------------------------------------------------
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 .
16 |
17 |
18 | #ifdef __cplusplus_cli
19 | #pragma unmanaged
20 | #endif
21 | #include "PEDataSource.h"
22 |
23 | #include
24 | #include
25 |
26 | #ifdef USE_WINDOWS_API
27 | #ifdef ARRAYSIZE
28 | #undef ARRAYSIZE
29 | #endif
30 | #define WIN32_LEAN_AND_MEAN
31 | #include
32 | #else
33 | #include
34 | #include
35 | #endif
36 |
37 | using namespace PE;
38 |
39 | RawDataSource::RawDataSource(void* 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 | void* 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