BearParser
Portable Executable parsing library (from PE-bear)
ExceptionDirWrapper.cpp
Go to the documentation of this file.
2#include "pe/PEFile.h"
3
4/*
5typedef struct _IMAGE_IA64_RUNTIME_FUNCTION_ENTRY {
6 DWORD BeginAddress;
7 DWORD EndAddress;
8 DWORD UnwindInfoAddress;
9} IMAGE_IA64_RUNTIME_FUNCTION_ENTRY, *PIMAGE_IA64_RUNTIME_FUNCTION_ENTRY;
10*/
11
13{
14 clear();
15 parsedSize = 0;
16 bufsize_t maxSize = getDirEntrySize(true);
17 if (maxSize == 0) return false; // nothing to parse
18
19 if (!exceptFunc64()) return false;
20
21 const size_t ENTRY_SIZE = sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
22 size_t entryId = 0;
23 while (parsedSize < maxSize) {
24 ExceptionEntryWrapper* entry = new ExceptionEntryWrapper(this->m_Exe, this, entryId++);
25
26 if (entry->getPtr() == NULL) {
27 delete entry;
28 break;
29 }
30 this->parsedSize += ENTRY_SIZE;
31 this->entries.push_back(entry);
32 }
34 "Entries num = %lu, parsedSize = %lX",
35 static_cast<unsigned long>(entries.size()),
36 static_cast<unsigned long>(parsedSize)
37 );
38 return true;
39}
40
41IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* ExceptionDirWrapper::exceptFunc64()
42{
44 BYTE *ptr = m_Exe->getContentAt(rva, Executable::RVA, sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY));
45 if (ptr == NULL) return NULL;
46
47 IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* exc = (IMAGE_IA64_RUNTIME_FUNCTION_ENTRY*) ptr;
48 return exc;
49}
50
51//----------------
52
54{
55 if (this->parentDir == NULL) return NULL;
56 IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* first = this->parentDir->exceptFunc64();
57 if (!first) return NULL;
58
59 const size_t ENTRY_SIZE = sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
60
61 uint64_t firstOffset = this->getOffset(first);
62 uint64_t myOffset = firstOffset + this->entryNum * ENTRY_SIZE;
63
64 BYTE *ptr = m_Exe->getContentAt(myOffset, Executable::RAW, ENTRY_SIZE);
65 return ptr;
66}
67
69{
70 if (this->parentDir == NULL) return 0;
71 if (this->getPtr() == NULL) return 0;
72
73 return sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
74}
75
76void* ExceptionEntryWrapper::getFieldPtr(size_t fieldId, size_t subField)
77{
78 IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* exc = (IMAGE_IA64_RUNTIME_FUNCTION_ENTRY*) this->getPtr();
79 if (!exc) return NULL;
80
81 switch (fieldId) {
82 case BEGIN_ADDR : return &exc->BeginAddress;
83 case END_ADDR : return &exc->EndAddress;
84 case UNWIND_INFO_ADDR : return &exc->UnwindInfoAddress;
85 }
86 return getPtr();
87}
88
90{
91 switch (fieldId) {
92 case BEGIN_ADDR : return "BeginAddress";
93 case END_ADDR : return "EndAddress";
94 case UNWIND_INFO_ADDR : return "UnwindInfoAddress";
95 }
96 return getName();
97}
98
100{
101 switch (fieldId) {
102 case BEGIN_ADDR :
103 case END_ADDR :
104 case UNWIND_INFO_ADDR :
105 return Executable::RVA;
106 }
108}
109
uint32_t bufsize_t
uint64_t offset_t
bufsize_t getDirEntrySize(bool trimToExeSize=false)
offset_t getDirEntryAddress()
friend class ExceptionEntryWrapper
IMAGE_IA64_RUNTIME_FUNCTION_ENTRY * exceptFunc64()
virtual void * getPtr()
virtual QString getName()
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
@ BEGIN_ADDR
@ UNWIND_INFO_ADDR
@ END_ADDR
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField)
virtual QString getFieldName(size_t fieldId)
virtual bufsize_t getSize()
std::vector< ExeNodeWrapper * > entries
virtual void clear()
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition: Executable.h:57
bool append(dbg_level lvl, const char *format,...)
Definition: Util.cpp:8
@ D_INFO
Definition: Util.h:26