BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
PECore.cpp
Go to the documentation of this file.
1#include "pe/PECore.h"
2
3#define DEFAULT_IMGBASE 0x10000
4
6{
7 dos = NULL;
8 fHdr = NULL;
9 opt32 = NULL;
10 opt64 = NULL;
11}
12
14{
15 buf = v_buf;
16 const bool allowExceptionsFromBuffer = false;
17
18 // reset all:
19 reset();
20
21 offset_t offset = 0;
23 if (!dos) throw ExeException("Could not wrap PECore: invalid DOS Header!");
24
25 offset = dos->e_lfanew + sizeof(DWORD); //skip 'PE' signature
27 if (!fHdr) throw ExeException("Could not wrap PECore!");
28
29 offset = offset + sizeof(IMAGE_FILE_HEADER);
31 if (!magic) throw ExeException("Could not wrap PECore: invalid FileHeader");
32
33 const Executable::exe_bits mode = ((*magic) == pe::OH_NT64) ? Executable::BITS_64 : Executable::BITS_32;
36
37 if (ntHdrPtr) {
39 this->opt32 = (IMAGE_OPTIONAL_HEADER32*)ntHdrPtr;
40 }
41 else if (mode == Executable::BITS_64) {
42 this->opt64 = (IMAGE_OPTIONAL_HEADER64*)ntHdrPtr;
43 }
44 }
45 if (!this->opt32 && !this->opt64) {
46 throw ExeException("Could not wrap PECore: invalid OptionalHeader");
47 }
48 return true;
49}
50
52{
53 if (opt32) return Executable::BITS_32;
54 if (opt64) return Executable::BITS_64;
55
56 return Executable::BITS_32; // DEFAULT
57}
58
60{
61 return static_cast<offset_t> (dos->e_lfanew);
62}
63
65{
66 const offset_t offset = peSignatureOffset();
67 if (offset == INVALID_ADDR) {
68 return INVALID_ADDR;
69 }
70 const offset_t signSize = sizeof(DWORD);
71 return offset + signSize;
72}
73
75{
76 const offset_t offset = peFileHdrOffset();
77 if (offset == INVALID_ADDR) {
78 return INVALID_ADDR;
79 }
80 return offset + sizeof(IMAGE_FILE_HEADER);
81}
82
84{
86 return sizeof(IMAGE_NT_HEADERS64);
87
88 return sizeof(IMAGE_NT_HEADERS32);
89}
90
92{
93 const offset_t offset = peOptHdrOffset();
94 if (offset == INVALID_ADDR) {
95 return INVALID_ADDR;
96 }
97 if (!fHdr) {
98 return INVALID_ADDR;
99 }
100 const offset_t size = static_cast<offset_t>(this->fHdr->SizeOfOptionalHeader);
101 return offset + size;
102}
103
105{
106 if (this->opt32) {
107 if (aType == Executable::RAW) return opt32->FileAlignment;
108 return opt32->SectionAlignment;
109 }
110 if (this->opt64) {
111 if (aType == Executable::RAW) return opt64->FileAlignment;
112 return opt64->SectionAlignment;
113 }
114 return 0;
115}
116
118{
119 bufsize_t imgSize = 0;
120 if (this->opt32) {
121 imgSize = opt32->SizeOfImage;
122 }
123 if (this->opt64) {
124 imgSize = opt64->SizeOfImage;
125 }
126 return imgSize;
127}
128
130{
132 if (this->opt32) {
133 hdrsSize = opt32->SizeOfHeaders;
134 }
135 if (this->opt64) {
136 hdrsSize = opt64->SizeOfHeaders;
137 }
138 return hdrsSize;
139}
140
142{
143 offset_t imgBase = 0;
144 if (this->opt32) {
145 imgBase = opt32->ImageBase;
146 }
147 if (this->opt64) {
148 imgBase = opt64->ImageBase;
149 }
150 //can be null, under XP. In this case, the binary will be relocated to 10000h
151 //(quote: http://code.google.com/p/corkami/wiki/PE)
152 if (imgBase == 0 && recalculate) {
154 }
155 //in 32 bit PEs: it can be any value as long as ImageBase + 'SizeOfImage' < 80000000h
156 //if the ImageBase is bigger than that, the binary will be relocated to 10000h
157 if (this->opt32) {
159 if (maxOffset >= 0x80000000 && recalculate) {
161 }
162 }
163 return imgBase;
164}
165
INT_TYPE _getNumValue(void *ptr)
uint32_t bufsize_t
const offset_t INVALID_ADDR
uint64_t offset_t
#define DEFAULT_IMGBASE
Definition PECore.cpp:3
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual bufsize_t getAlignment(Executable::addr_type aType) const
Definition PECore.cpp:104
bufsize_t hdrsSize() const
Definition PECore.cpp:129
offset_t peOptHdrOffset() const
Definition PECore.cpp:74
IMAGE_OPTIONAL_HEADER32 * opt32
Definition PECore.h:56
bool wrap(AbstractByteBuffer *v_buf)
Definition PECore.cpp:13
virtual offset_t getImageBase(bool recalculate=false)
Definition PECore.cpp:141
offset_t peFileHdrOffset() const
Definition PECore.cpp:64
AbstractByteBuffer * buf
Definition PECore.h:52
void reset()
Definition PECore.cpp:5
virtual bufsize_t getImageSize()
Definition PECore.cpp:117
offset_t peSignatureOffset() const
Definition PECore.cpp:59
IMAGE_FILE_HEADER * fHdr
Definition PECore.h:55
bufsize_t peNtHeadersSize() const
Definition PECore.cpp:83
offset_t secHdrsOffset() const
Definition PECore.cpp:91
IMAGE_OPTIONAL_HEADER64 * opt64
Definition PECore.h:57
Executable::exe_bits getHdrBitMode() const
Definition PECore.cpp:51
IMAGE_DOS_HEADER * dos
Definition PECore.h:54