BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
SectHdrsWrapper.h
Go to the documentation of this file.
1#pragma once
2
3#include "PENodeWrapper.h"
4#include <vector>
5#include <map>
6
7#include "../WatchedLocker.h"
8
9#define SEC_SHOW_LOCK false
10
12{
13public:
14 static const size_t SECNAME_LEN;
15
16 static std::vector<DWORD> splitCharacteristics(DWORD characteristics);
17 static QString getSecHdrAccessRightsDesc(DWORD characteristics);
18
19 static void initSecCharacter(std::map<DWORD, QString> &secHdrCharact);
20 static QString translateCharacteristics(DWORD charact);
21
22 /* fields :*/
40
41 SectionHdrWrapper(PEFile *pe, size_t sectionNumber)
42 : PENodeWrapper(pe), sectNum(sectionNumber), name(NULL), header(NULL) { wrap(); }
43
45 {
46 if (name) {
47 ::free(name);
48 }
49 }
50
51 bool wrap();
52
53 /* full structure boundaries */
54 virtual void* getPtr();
55 virtual bufsize_t getSize();
56 virtual QString getName();
57 virtual size_t getFieldsCount() { return FIELD_COUNTER; }
58 virtual size_t getSubFieldsCount() { return 1; }
59
60 /* specific field boundaries */
61 virtual void* getFieldPtr(size_t fieldId, size_t subField = FIELD_NONE);
62 virtual QString getFieldName(size_t fieldId);
63
64 virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField = FIELD_NONE);
65 virtual WrappedValue::data_type containsDataType(size_t fieldId, size_t subField = FIELD_NONE);
66
67 offset_t getContentOffset(Executable::addr_type aType, bool useMapped = true);
68 offset_t getContentEndOffset(Executable::addr_type aType, bool roundup); //always useMapped startOFfset
69
70 bufsize_t getContentSize(Executable::addr_type aType, bool recalculate);
71
72 /* wrappers */
73 DWORD getCharacteristics() { return header ? header->Characteristics : 0; }
74
75 DWORD getRawPtr() { return header ? header->PointerToRawData : 0; }
76
77 DWORD getVirtualPtr() { return header ? header->VirtualAddress : 0; }
78
79//modifications:
80
81 bool setCharacteristics(DWORD newCharacteristics)
82 {
83 if (!header) return false;
84 //TODO: validate newCharacteristics
85
86 header->Characteristics = newCharacteristics;
87 return true;
88 }
89
90 bool reloadName();
91
92 QString mappedName;
93
94protected:
95 SectionHdrWrapper(PEFile *pe, IMAGE_SECTION_HEADER *v_header) //standalone entry
96 : PENodeWrapper(pe), sectNum(INVALID_ENTRYNUM), name(NULL), header(v_header)
97 {
98 reloadName();
99 }
100
102
106
107 char *name;
108 size_t sectNum;
109
110private:
111 static std::map<DWORD, QString> s_secHdrCharact;
112 IMAGE_SECTION_HEADER *header;
113
114friend class PEFile;
115};
116
117//----
118
120{
121public:
122 static size_t SECT_COUNT_MAX;
123 static size_t SECT_INVALID_INDEX;
124
125 // fields :
127 bool wrap();
128 virtual void reloadMapping();
129
130 // full structure boundaries
131 virtual void* getPtr();
132 virtual bufsize_t getSize();
133 virtual QString getName() { return "Section Hdrs"; }
134 virtual size_t getFieldsCount();
135
136 // specific field boundaries
137 virtual void* getFieldPtr(size_t fieldId, size_t subField) { return getSubfieldPtr(fieldId, subField ); }
138 virtual bufsize_t getFieldSize(size_t fieldId, size_t subField) { return getSubfieldSize(fieldId, subField ); }
139 virtual QString getFieldName(size_t fieldId);
140
141 SectionHdrWrapper* getSecHdrAtOffset(offset_t offset, Executable::addr_type addrType, bool roundup, bool verbose = false);
142
144
146 {
147 if (!sec || !entries.size()) return SECT_INVALID_INDEX;
148 size_t indx = 0;
149 for (auto itr = entries.begin(); itr != entries.end(); ++itr, ++indx) {
150 if (sec == *itr) {
151 return indx;
152 }
153 }
154 return indx;
155 }
156
157 //---
158 bool canAddEntry();
160
161/* protected by mutex*/
163 {
164 WatchedLocker lock(&m_secMutex, SEC_SHOW_LOCK, __FUNCTION__);
165 return _getSecHdr(index);
166 }
167
168protected:
170 {
171 if (index == SECT_INVALID_INDEX || index >= entries.size()) return NULL;
172 return dynamic_cast<SectionHdrWrapper*>(entries.at(index));
173 }
174
175private:
176 void clear();
177 void addMapping(SectionHdrWrapper *sec);
178 virtual bool loadNextEntry(size_t entryNum);
179 bool isMyEntryType(ExeNodeWrapper *entry); // is it an entry of appropriate type
180
181 std::map<offset_t, SectionHdrWrapper*> vSec;
182 std::map<offset_t, SectionHdrWrapper*> rSec;
183 QMutex m_secMutex;
184
185friend class PEFile;
186};
uint32_t bufsize_t
uint64_t offset_t
#define FIELD_NONE
const size_t INVALID_ENTRYNUM
#define SEC_SHOW_LOCK
virtual bufsize_t getContentSize()
virtual void * getSubfieldPtr(size_t fieldId, size_t subField)
std::vector< ExeNodeWrapper * > entries
virtual bufsize_t getSubfieldSize(size_t fieldId, size_t subField)
SectHdrsWrapper(PEFile *pe)
virtual bufsize_t getFieldSize(size_t fieldId, size_t subField)
SectionHdrWrapper * _getSecHdr(size_t index)
SectionHdrWrapper * getSecHdr(size_t index)
void printSectionsMapping(Executable::addr_type aType)
ExeNodeWrapper * addEntry(ExeNodeWrapper *entry)
virtual bufsize_t getSize()
virtual QString getFieldName(size_t fieldId)
SectionHdrWrapper * getSecHdrAtOffset(offset_t offset, Executable::addr_type addrType, bool roundup, bool verbose=false)
virtual void reloadMapping()
virtual void * getPtr()
static size_t SECT_COUNT_MAX
virtual void * getFieldPtr(size_t fieldId, size_t subField)
size_t getSecIndex(SectionHdrWrapper *sec) const
virtual QString getName()
static size_t SECT_INVALID_INDEX
virtual size_t getFieldsCount()
virtual size_t getFieldsCount()
static std::vector< DWORD > splitCharacteristics(DWORD characteristics)
bufsize_t getMappedVirtualSize()
virtual QString getName()
bool setCharacteristics(DWORD newCharacteristics)
SectionHdrWrapper(PEFile *pe, size_t sectionNumber)
virtual QString getFieldName(size_t fieldId)
offset_t getContentDeclaredOffset(Executable::addr_type aType)
static void initSecCharacter(std::map< DWORD, QString > &secHdrCharact)
SectionHdrWrapper(PEFile *pe, IMAGE_SECTION_HEADER *v_header)
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
virtual size_t getSubFieldsCount()
bufsize_t getContentDeclaredSize(Executable::addr_type aType)
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField=FIELD_NONE)
static QString translateCharacteristics(DWORD charact)
offset_t getContentEndOffset(Executable::addr_type aType, bool roundup)
offset_t getContentOffset(Executable::addr_type aType, bool useMapped=true)
static QString getSecHdrAccessRightsDesc(DWORD characteristics)
virtual void * getPtr()
bufsize_t getMappedRawSize()
static const size_t SECNAME_LEN
virtual WrappedValue::data_type containsDataType(size_t fieldId, size_t subField=FIELD_NONE)
virtual bufsize_t getSize()