BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
ImportBaseDirWrapper.cpp
Go to the documentation of this file.
2
3//---------------------------------
4
6
7bool imports_util::isNameValid(Executable *pe, char* myName)
8{
9 if (!myName) return false; // do not parse, invalid entry
10 bufsize_t upperLimit = pe->getMaxSizeFromPtr((BYTE*) myName);
11 if (upperLimit == 0) return false;
12
13 bool isInvalid = pe_util::hasNonPrintable(myName, upperLimit);
14 if (isInvalid) return false;
15 if (pe_util::noWhiteCount(myName) == 0) return false;
16
17 return true;
18}
19//---------------------------------
20
21using namespace imports_util;
22
24 if (bits == Executable::BITS_32) return sizeof (uint32_t);
25 else if (bits == Executable::BITS_64) return sizeof (uint64_t);
26 return 0;
27}
28
29
30
32{
33 ImportBaseFuncWrapper* func = dynamic_cast<ImportBaseFuncWrapper*> (funcNode);
34 if (func == NULL) return;
35
36 offset_t via = func->callVia();
37 if (via == INVALID_ADDR) return;
38 /*
39 if (m_Exe->isValidVA(via)) {
40 via = m_Exe->VaToRva(via);
41 }*/
42 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*>(func->getParentNode());
43 if (!lib) return;
44
45 this->thunksList.push_back(via);
46 size_t num = lib->getEntryId();
47 thunkToLibMap[via] = num;
48
49 num = func->getEntryId();
50 lib->thunkToFuncMap[via] = num;
51}
52
54{
55 thunksList.clear();
56 thunkToLibMap.clear();
57}
58
59
61{
63 size_t entriesCount = this->entries.size();
64
65 for (size_t i = 0; i < entriesCount; i++) {
66 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*> (this->getEntryAt(i));
67 if (lib == NULL) continue;
68 size_t libId = lib->entryNum;
69
70 size_t funcCount = lib->getEntriesCount();
71 for (size_t fI = 0; fI < funcCount; fI++) {
72 addMapping(lib->getEntryAt(fI));
73 }
74 }
75}
76
78{
79 std::map<offset_t, size_t>::iterator libItr = thunkToLibMap.find(thunk);
80 if (libItr == thunkToLibMap.end()) return NULL;
81
82 size_t libId = libItr->second;
83 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*>(this->getEntryAt(libId));
84 return lib;
85}
86
88{
90 if (!lib) return NULL;
91
92 std::map<offset_t, size_t>::iterator funcItr = lib->thunkToFuncMap.find(thunk);
93 if (funcItr == lib->thunkToFuncMap.end()) return NULL;
94
95 ImportBaseFuncWrapper* func = dynamic_cast<ImportBaseFuncWrapper*>(lib->getEntryAt(funcItr->second));
96 return func;
97}
98
99QString ImportBaseDirWrapper::thunkToFuncName(offset_t thunk, bool shortName)
100{
102 if (func == NULL) return "";
103 if (shortName) {
104 return func->getShortName();
105 }
106 return func->getName();
107}
108
110{
112 if (!lib) return "";
113 return lib->getName();
114}
115
117{
118 clearMapping();
119 clear();
120
121 size_t oldCount = this->importsCount;
122 this->importsCount = 0;
123
124 if (!getDataDirectory()) {
125 return (oldCount != this->importsCount); //has count changed
126 }
127
128 const size_t LIMIT = (-1);
129
130 size_t cntr = 0;
131 for (cntr = 0; cntr < LIMIT; cntr++) {
132 if (loadNextEntry(cntr) == false) break;
133 }
134
135 this->importsCount = cntr;
136 return (oldCount != this->importsCount); //has count changed
137}
138
139//--------------------------------------------------------------------------------------------------------------
140
142{
143 char *libName = this->getLibraryName();
144 bool isValid = imports_util::isNameValid(m_Exe, libName);
145 return isValid;
146}
147
149{
150 clear();
151 thunkToFuncMap.clear();
152
153 const size_t LIMIT = (-1);
154 if (!isValid()) {
155 return false;
156 }
157 size_t cntr = 0;
158 if (this->getPtr() == NULL) {
159 return false;
160 }
161 for (cntr = 0; cntr < LIMIT; cntr++) {
162 if (loadNextEntry(cntr) == false) break;
163 }
164 //printf("Entries: %d\n", entries.size());
165 return true;
166}
167
168//--------------------------------------------------------------------------------------------------------------
170{
171 QString functionName;
172 if (isByOrdinal()) {
173 uint64_t val = getOrdinal();
174 static char buf[0xFF];
175 snprintf(buf, 0xFF, "<ord: %llX>", static_cast<unsigned long long>(val));
176 functionName = buf;
177 } else {
178 char *fName = this->getFunctionName();
179 if (!fName) return "";
180 functionName = fName;
181 }
182 return functionName;
183}
184
186{
187 QString functionName = getShortName();
189 if (!p) return functionName;
190
191 char *libName = p->getLibraryName();
192 if (!libName) return functionName;
193
194 functionName = "[" + QString(libName) + "]." + functionName;
195 return functionName;
196}
197
uint32_t bufsize_t
const offset_t INVALID_ADDR
uint64_t offset_t
bufsize_t getMaxSizeFromPtr(BYTE *ptr)
IMAGE_DATA_DIRECTORY * getDataDirectory()
virtual void * getPtr()=0
virtual QString getName()=0
virtual ExeNodeWrapper * getEntryAt(size_t fieldId)
std::vector< ExeNodeWrapper * > entries
size_t getEntryId()
virtual bool loadNextEntry(size_t entryNum)
virtual size_t getEntriesCount()
virtual void clear()
static bufsize_t thunkSize(Executable::exe_bits bits)
ImportBaseEntryWrapper * thunkToLib(offset_t thunk)
QList< offset_t > thunksList
std::map< offset_t, size_t > thunkToLibMap
QString thunkToLibName(offset_t thunk)
ImportBaseFuncWrapper * thunkToFunction(offset_t thunk)
void addMapping(ExeNodeWrapper *func)
QString thunkToFuncName(offset_t thunk, bool shortName=true)
static bufsize_t NameLenLimit
bool wrap()
bool isValid()
virtual char * getLibraryName()=0
std::map< offset_t, size_t > thunkToFuncMap
virtual uint64_t getOrdinal()=0
virtual char * getFunctionName()=0
virtual offset_t callVia()=0
virtual bool isByOrdinal()=0
virtual PENodeWrapper * getParentNode()
Definition: PENodeWrapper.h:33
bool isNameValid(Executable *pe, char *myName)
bool hasNonPrintable(const char *ptr, size_t maxInp)
Definition: Util.cpp:70
size_t noWhiteCount(char *buf, size_t bufSize)
Definition: Util.cpp:112