8000 Added a dynamic pointer class that automatically moves when the under… · coderforlife/pe-file@4a70e28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a70e28

Browse files
committed
Added a dynamic pointer class that automatically moves when the underlying memory pointer (from a data source) moves (e.g. due to a realloc or re-memory-map). This means that far fewer returned pointers are "invalid" after certain operations.
1 parent 0c74d2c commit 4a70e28

9 files changed

+409
-234
lines changed

PEDataSource.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
using namespace PE;
3838

39-
RawDataSource::RawDataSource(pntr data, size_t size, bool readonly) : readonly(readonly), orig_data(data), sz(size) {
39+
RawDataSource::RawDataSource(void* data, size_t size, bool readonly) : readonly(readonly), orig_data(data), sz(size) {
4040
if (readonly) {
4141
#ifdef USE_WINDOWS_API
4242
DWORD old_protect = 0;
@@ -53,7 +53,7 @@ RawDataSource::RawDataSource(pntr data, size_t size, bool readonly) : readonly(r
5353
RawDataSource::~RawDataSource() { this->close(); }
5454
bool RawDataSource::isreadonly() const { return this->readonly; };
5555
bool RawDataSource::flush() { return true; }
56-
pntr RawDataSource::data() { return this->d; }
56+
void* RawDataSource::data() { return this->d; }
5757
size_t RawDataSource::size() const { return this->sz; }
5858
void RawDataSource::close() {
5959
if (this->d) {
@@ -90,9 +90,9 @@ bool RawDataSource::resize(size_t new_size) {
9090
#include <map>
9191
#include <vector>
9292
using namespace std;
93-
typedef map<const_str, vector<pntr> > MMFs;
93+
typedef map<const_str, vector<void*> > MMFs;
9494
static MMFs mmfs;
95-
static void _RemoveMMF(MMFs &mmfs, const_str file, pntr x) {
95+
static void _RemoveMMF(MMFs &mmfs, const_str file, void* x) {
9696
MMFs::iterator v = mmfs.find(file);
9797
if (v != mmfs.end()) {
9898
size_t size = v->second.size();
@@ -110,14 +110,14 @@ static void _RemoveMMF(MMFs &mmfs, const_str file, pntr x) {
110110
}
111111
}
112112
}
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); }
113+
static void* AddMMF (const_str file, void* mm) { if (mm != NULL && mm != (void*)-1) { mmfs[file].push_back(mm); } return mm; }
114+
static void RemoveMMF(const_str file, void* mm) { _RemoveMMF(mmfs, file, mm); }
115115

116116
#ifdef USE_WINDOWS_API
117117
static MMFs mmfViews;
118118
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); }
119+
static void* AddMMFView(const_str file, void* view) { if (view != NULL) mmfViews[file].push_back(view); return view; }
120+
static void RemoveMMFView(const_str file, void* view) { _RemoveMMF(mmfViews, file, view); }
121121
static void _UnmapAll(MMFs &mmfs, const_str file, UNMAP_OR_CLOSE func) {
122122
MMFs::iterator v = mmfs.find(file);
123123
if (v != mmfs.end()) {
@@ -215,7 +215,7 @@ bool MemoryMappedDataSource::flush() {
215215
#endif
216216
}
217217

218-
pntr MemoryMappedDataSource::data() { return this->d; }
218+
void* MemoryMappedDataSource::data() { return this->d; }
219219
size_t MemoryMappedDataSource::size() const { return this->sz; }
220220
void MemoryMappedDataSource::close() {
221221
this->unmap();

PEDataSource.h

Lines changed: 11 additions & 13 deletions
< 9E7A td data-grid-cell-id="diff-e6f15f8c6766b63cee1f939b3ab151370c035da2c0bcdd79f8b714cf8a8127b8-48-46-2" data-line-anchor="diff-e6f15f8c6766b63cee1f939b3ab151370c035da2c0bcdd79f8b714cf8a8127b8R46" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
virtual bool resize(size_t new_size);
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,26 @@
2121

2222
#include "PEDataTypes.h"
2323

24-
#include <stdlib.h>
25-
2624
namespace PE {
2725
class DataSourceImp {
2826
public:
2927
virtual bool isreadonly() const = 0;
3028
virtual void close() = 0;
3129
virtual bool flush() = 0;
32-
virtual pntr data() = 0;
30+
virtual void* data() = 0;
3331
virtual size_t size() const = 0;
3432
virtual bool resize(size_t new_size) = 0;
3533
};
3634

3735
class RawDataSource : public DataSourceImp {
3836
bool readonly;
39-
pntr d, orig_data;
37+
void *d, *orig_data;
4038
size_t sz;
4139
public:
42-
RawDataSource(pntr data, size_t size, bool readonly = false);
40+
RawDataSource(void* data, size_t size, bool readonly = false);
4341
~RawDataSource();
4442
virtual bool isreadonly() const;
45-
virtual pntr data();
43+
virtual void* data();
4644
virtual size_t size() const;
4745
virtual void close();
4846
@@ -53,11 +51,11 @@ namespace PE {
5351
bool readonly;
5452
wchar_t original[LARGE_PATH];
5553
#ifdef USE_WINDOWS_API
56-
pntr hFile, hMap;
54+
void *hFile, *hMap;
5755
#else
5856
int fd;
5957
#endif
60-
pntr d;
58+
void* d;
6159
size_t sz;
6260

6361
bool map();
@@ -66,7 +64,7 @@ namespace PE {
6664
MemoryMappedDataSource(const_str file, bool readonly = false);
6765
~MemoryMappedDataSource();
6866
virtual bool isreadonly() const;
69-
virtual pntr data();
67+
virtual void* data();
7068
virtual size_t size() const;
7169
virtual void close();
7270
virtual bool resize(size_t new_size);
@@ -78,7 +76,7 @@ namespace PE {
7876
class DataSource {
7977
DataSourceImp* ds;
8078
bool readonly;
81-
pntr data;
79+
void* data;
8280
size_t sz;
8381

8482
inline void update() {
@@ -107,12 +105,12 @@ namespace PE {
107105
//inline operator const_pntr() const { return this->data; }
108106
//inline operator const_bytes() const { return (const_bytes)this->data; }
109107

110-
inline bytes operator +(const size_t& off) { return ((bytes)this->data) + off; }
111-
inline const_bytes operator +(const size_t& off) const { return ((const_bytes)this->data) + off; }
108+
inline dyn_ptr<byte> operator +(const size_t& off) { return dyn_ptr<byte>(&this->data, off); }
109+
inline const dyn_ptr<byte> operator +(const size_t& off) const { return dyn_ptr<byte>(&this->data, off); }
112110

113111
inline ptrdiff_t operator -(const_bytes b) const { return (const_bytes)this->data - b; }
114112

115-
inline byte& operator[](const size_t& off) { return ((bytes)this->data)[off]; }
113+
inline byte& operator[](const size_t& off) { return ((bytes)this->data)[off]; }
116114
inline const byte& operator[](const size_t& off) const { return ((const_bytes)this->data)[off]; }
117115
};
118116

0 commit comments

Comments
 (0)
0