BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
PEFile.cpp
Go to the documentation of this file.
1#include "pe/PEFile.h"
2#include "FileBuffer.h"
3
5{
6 if (buf == NULL) return false;
7
8 offset_t dosOffset = 0;
9 WORD *magic = (WORD*) buf->getContentAt(dosOffset, sizeof(WORD));
10 if (magic == NULL) return false;
11
12 if ((*magic) != pe::S_DOS) {
13 return false;
14 }
15 offset_t newOffset = dosOffset + (sizeof(IMAGE_DOS_HEADER) - sizeof(LONG));
16 LONG* lfnew = (LONG*) buf->getContentAt(newOffset, sizeof(LONG));
17 if (lfnew == NULL) {
18 return false;
19 }
20 offset_t peOffset = static_cast<offset_t>(*lfnew);
21 DWORD *peMagic = (DWORD*) buf->getContentAt(peOffset, sizeof(DWORD));
22 if (peMagic == NULL) {
23 return false;
24 }
25 if (*peMagic == pe::S_NT) {
26 return true;
27 }
28 return false;
29}
30
32{
33 Executable *exe = NULL;
34 if (signatureMatches(buf) == false) return NULL;
35
36 try {
37 exe = new PEFile(buf);
38 } catch (ExeException &e) {
39 exe = NULL;
40 }
41 return exe;
42}
43
44//-------------------------------------------------------------
45long PEFile::computeChecksum(BYTE *buffer, size_t bufferSize, size_t checksumOffset)
46{
47 WORD *wordBuff = (WORD*)buffer;
48 size_t wordSize = bufferSize / sizeof(WORD);
49
50 size_t checksumBgn = checksumOffset;
51 size_t checksumEnd = checksumOffset + sizeof(DWORD);
52
53 const long long maxVal = ((long long)1) << 32;
54 long long checksum = 0;
55
56 for (int i = 0; i < wordSize; i++) {
57 WORD chunk = wordBuff[i];
58
59 size_t bI = i * sizeof(WORD);
60 if (bI >= checksumBgn && bI < checksumEnd) {
61 size_t mask = (checksumEnd - bI) % sizeof(WORD);
62 size_t shift = (sizeof(WORD) - mask) * 8;
63 chunk = (chunk >> shift) << shift;
64 }
65
66 checksum = (checksum & 0xffffffff) + chunk + (checksum >> 32);
67 if (checksum > maxVal) {
68 checksum = (checksum & 0xffffffff) + (checksum >> 32);
69 }
70 }
71 checksum = (checksum & 0xffff) + (checksum >> 16);
72 checksum = (checksum)+(checksum >> 16);
73 checksum = checksum & 0xffff;
74 checksum += bufferSize;
75 return checksum;
76}
77
79
81 : MappedExe(v_buf, Executable::BITS_32), dosHdrWrapper(NULL), fHdr(NULL), optHdr(NULL), sects(NULL),
82 album(NULL)
83{
84 album = new ResourcesAlbum(this);
85 wrap(v_buf);
87}
88
90{
93}
94
96{
97 for (size_t i = 0 ; i < pe::DIR_ENTRIES_COUNT; i++) {
98 dataDirEntries[i] = NULL;
99 }
100}
101
103{
104 PEFile::wrap(this->buf);
105}
106
108{
109 //erase all existing wrappers:
111
112 // rewrao the core:
113 core.wrap(v_buf);
114
115 //regenerate the wrappers:
116 this->dosHdrWrapper = new DosHdrWrapper(this);
117 this->wrappers[WR_DOS_HDR] = this->dosHdrWrapper;
118
119 this->fHdr = new FileHdrWrapper(this);
120 if (fHdr->getPtr() == NULL) throw ExeException("Cannot parse FileHdr: It is not PE File!");
121 this->wrappers[WR_FILE_HDR] = fHdr;
122 this->wrappers[WR_RICH_HDR] = new RichHdrWrapper(this);
123
124 this->optHdr = new OptHdrWrapper(this);
125 if (optHdr->getPtr() == NULL) throw ExeException("Cannot parse OptionalHeader: It is not PE File!");
127
128 this->wrappers[WR_DATADIR] = new DataDirWrapper(this);
129
130 bool isOk = false;
131 size_t secNum = fHdr->getNumValue(FileHdrWrapper::SEC_NUM, &isOk);
132 if (isOk){
133 this->sects = new SectHdrsWrapper(this);
134 this->wrappers[WR_SECTIONS] = sects;
135 }
136 // map Data Dirs
138 dataDirEntries[pe::DIR_IMPORT] = new ImportDirWrapper(this);
139 dataDirEntries[pe::DIR_DELAY_IMPORT] = new DelayImpDirWrapper(this);
140 dataDirEntries[pe::DIR_BOUND_IMPORT] = new BoundImpDirWrapper(this);
141 dataDirEntries[pe::DIR_DEBUG] = new DebugDirWrapper(this);
142 dataDirEntries[pe::DIR_EXPORT] = new ExportDirWrapper(this);
143 dataDirEntries[pe::DIR_SECURITY] = new SecurityDirWrapper(this);
144 dataDirEntries[pe::DIR_TLS] = new TlsDirWrapper(this);
145 dataDirEntries[pe::DIR_LOAD_CONFIG] = new LdConfigDirWrapper(this);
146 dataDirEntries[pe::DIR_BASERELOC] = new RelocDirWrapper(this);
147 dataDirEntries[pe::DIR_EXCEPTION] = new ExceptionDirWrapper(this);
148 dataDirEntries[pe::DIR_RESOURCE] = new ResourceDirWrapper(this, album);
149 dataDirEntries[pe::DIR_COM_DESCRIPTOR] = new ClrDirWrapper(this);
150
151 for (int i = 0; i < pe::DIR_ENTRIES_COUNT; i++) {
152 this->wrappers[WR_DIR_ENTRY + i] = dataDirEntries[i];
153 }
154
155 if (this->album) {
156 this->album->wrapLeafsContent();
157 }
158}
159
160pe::RICH_DANS_HEADER* PEFile::getRichHeaderBgn(pe::RICH_SIGNATURE* richSign)
161{
162 if (!richSign) return NULL;
163
164 DWORD xorkey = richSign->checksum;
165 const offset_t richOffset = this->getOffset(richSign);
166
167 pe::RICH_DANS_HEADER* dansHdr = NULL;
168
169 offset_t offset = richOffset - sizeof(pe::RICH_DANS_HEADER);
170 while (offset > 0) {
171 dansHdr = (pe::RICH_DANS_HEADER*) this->getContentAt(offset, sizeof(pe::RICH_DANS_HEADER));
172 if (!dansHdr) {
173 break;
174 }
175 if (dansHdr->dansId == (pe::DANS_HDR_MAGIC ^ xorkey)) {
176 break; //got it!
177 }
178 //walking back
179 offset -= sizeof(DWORD);
180 }
181 if (!dansHdr || dansHdr->dansId != (pe::DANS_HDR_MAGIC ^ xorkey)) {
182 return NULL; //not found
183 }
184 return dansHdr;
185}
186
187pe::RICH_SIGNATURE* PEFile::getRichHeaderSign()
188{
189 size_t dosStubOffset = this->core.dos->e_lfarlc;
190 size_t dosStubEnd = this->core.dos->e_lfanew; // PE header start
191 const size_t maxSize = dosStubEnd - dosStubOffset; // Rich Header is somewhere in the space between DOS and PE headers
192 BYTE *dosPtr = this->getContentAt(dosStubOffset, maxSize);
193 if (!dosPtr) {
194 return NULL;
195 }
196
197 pe::RICH_SIGNATURE* richSign = NULL;
198 size_t toSearchSize = maxSize;
199 const offset_t startOffset = dosStubOffset; //we are starting from the beginning of DOS stub
200 const size_t step = sizeof(DWORD); //RichHeader is padded by DWORDS
201
202 while (toSearchSize > 0) {
203 richSign = (pe::RICH_SIGNATURE*) this->getContentAt(startOffset + toSearchSize, sizeof(pe::RICH_SIGNATURE));
204 if (!richSign) break;
205 if (richSign->richId == pe::RICH_HDR_MAGIC) break; //got it!
206 // the search goes backward.
207 toSearchSize -= step;
208 }
209 if (!richSign) return NULL;
210 if (richSign->richId != pe::RICH_HDR_MAGIC) {
211 return NULL; //invalid
212 }
213 return richSign;
214}
215
216
218{
219 if (!this->getSectionsCount()) {
220 return INVALID_ADDR;
221 }
222 SectionHdrWrapper* sec = this->getSecHdr(0);
223 if (!sec) {
224 return INVALID_ADDR;
225 }
227}
228
230{
231 if (this->optHdr == NULL) return INVALID_ADDR;
233}
234
235IMAGE_DATA_DIRECTORY* PEFile::getDataDirectory()
236{
237 if (this->wrappers[WR_DATADIR] == NULL) return NULL;
238 return static_cast<IMAGE_DATA_DIRECTORY*>(this->wrappers[WR_DATADIR]->getPtr());
239}
240
242{
243 if (aType == Executable::NOT_ADDR) return 0;
244
245 if (aType == Executable::RAW) {
246 return this->getContentSize();
247 }
248 const size_t PAGE_SIZE = 0x1000;
249 bufsize_t vSize = 0;
250 if (aType == Executable::VA || aType == Executable::RVA) {
251 vSize = core.getImageSize();
252 }
253 if (vSize < PAGE_SIZE) {
254 return PAGE_SIZE;
255 }
256 return vSize;
257}
258
260{
261 if (optHdr == NULL) return INVALID_ADDR;
262
263 bool isOk = false;
264 offset_t entryPoint = static_cast<offset_t> (optHdr->getNumValue(OptHdrWrapper::EP, &isOk));
265 if (isOk == false) return INVALID_ADDR;
266
268 if (addrType != epType) {
269 entryPoint = this->convertAddr(entryPoint, epType, addrType);
270 }
271 return entryPoint;
272}
273
275{
276 if (optHdr == NULL) return false;
277
278 offset_t epRva = this->convertAddr(entry, aType, Executable::RVA);
279 bool isOk = optHdr->setNumValue(OptHdrWrapper::EP, epRva);
280 return isOk;
281}
282
284{
285 bool isOk = false;
286 uint64_t secNum = this->fHdr->getNumValue(FileHdrWrapper::SEC_NUM , &isOk);
287 if (isOk == false) return 0;
288
289 return static_cast<size_t> (secNum);
290}
291
292bool PEFile::setHdrSectionsNum(size_t newNum)
293{
294 uint64_t count = newNum;
295 bool canSet = fHdr->setNumValue(FileHdrWrapper::SEC_NUM , count);
296 if (canSet == false) {
297 Logger::append(Logger::D_ERROR,"Can not change FileHdr!");
298 return false;
299 }
300 return true;
301}
302
304{
305 uint64_t size = newSize;
306 bool canSet = optHdr->setNumValue(OptHdrWrapper::IMAGE_SIZE, size);
307 if (canSet == false) {
308 Logger::append(Logger::D_ERROR, "Can not change OptHdr!");
309 return false;
310 }
311 return true;
312}
313
314size_t PEFile::getSectionsCount(bool useMapped) const
315{
316 if (useMapped == false) {
317 return hdrSectionsNum();
318 }
319 return this->sects->getEntriesCount();
320}
321
323{
324 if (raw >= this->getMappedSize(Executable::RAW)) return INVALID_ADDR;
325
326 SectionHdrWrapper* sec = this->getSecHdrAtOffset(raw, Executable::RAW, true);
327 if (sec) {
330 if (bgnVA == INVALID_ADDR || bgnRaw == INVALID_ADDR) return INVALID_ADDR;
331
332 bufsize_t curr = (raw - bgnRaw);
333
334 bufsize_t vSize = sec->getContentSize(Executable::VA, true);
335 if (curr >= vSize) {
336 //address out of section
337 return INVALID_ADDR;
338 }
339 return bgnVA + curr;
340 }
341 //TODO: make more tests
342 if (this->getSectionsCount() == 0) return raw;
343 if (raw < this->hdrsSize()) {
344 return raw;
345 } //else: content that is between the end of sections headers and the first virtual section is not mapped
346 return INVALID_ADDR;
347}
348
350{
351 if (rva >= this->getMappedSize(Executable::RVA)) return INVALID_ADDR;
352
353 SectionHdrWrapper* sec = this->getSecHdrAtOffset(rva, Executable::RVA, true);
354 if (sec) {
357 if (bgnRVA == INVALID_ADDR || bgnRaw == INVALID_ADDR) return INVALID_ADDR;
358
359 bufsize_t curr = (rva - bgnRVA);
360 bufsize_t rawSize = sec->getContentSize(Executable::RAW, true);
361 if (curr >= rawSize) {
362 // the address might be in a virtual cave that is not related to any raw address
363 return INVALID_ADDR;
364 }
365 return bgnRaw + curr;
366 }
367 if (rva >= this->getMappedSize(Executable::RAW)) {
368 return INVALID_ADDR;
369 }
370 if (this->getSectionsCount()) { // do this check only if sections count is non-zero
371 if (rva >= this->hdrsSize()) {
372 // the address is in the cave between the headers and the first section: cannot be mapped
373 return INVALID_ADDR;
374 }
375 }
376 // at this point we are sure that the address is within the raw size:
377 return rva;
378}
379
381{
382 if (eType >= pe::DIR_ENTRIES_COUNT) return NULL;
383 return dataDirEntries[eType];
384}
385
387{
388 SectionHdrWrapper *sec = this->getSecHdr(secId);
389 if (sec == NULL) {
390 Logger::append(Logger::D_WARNING, "No such section");
391 return NULL;
392 }
393 return _createSectionView(sec);
394}
395
396bool PEFile::moveDataDirEntry(pe::dir_entry id, offset_t newOffset, Executable::addr_type addrType)
397{
398 bool allowExceptions = true; //TODO: configure exception mode outside...
399
401 if (entry == NULL) {
402 if (allowExceptions) throw ExeException("No such Data Directory");
403 return false;
404 }
405 DataDirWrapper* ddirWrapper = dynamic_cast<DataDirWrapper*> (this->wrappers[WR_DATADIR]);
406 IMAGE_DATA_DIRECTORY *ddir = this->getDataDirectory();
407 if (ddirWrapper == NULL || ddir == NULL) {
408 if (allowExceptions) throw ExeException("Cannot fetch DataDirTable");
409 return false;
410 }
411 Executable::addr_type dataDirAddrType = ddirWrapper->containsAddrType(id, DataDirWrapper::ADDRESS);
412 offset_t dataDirAddr = this->convertAddr(newOffset, addrType, dataDirAddrType);
413 if (dataDirAddr == INVALID_ADDR) {
414 if (allowExceptions) throw ExeException("Invalid new offset");
415 return false;
416 }
417 offset_t targetRaw = this->toRaw(newOffset, addrType);
418 if (entry->canCopyToOffset(targetRaw) == false) {
419 if (allowExceptions) throw ExeException("Cannot copy: no space at such offset");
420 return false;
421 }
422 if (entry->copyToOffset(targetRaw) == false) {
423 if (allowExceptions) throw ExeException("Cannot copy: error occured");
424 return false;
425 }
426 entry->fillContent(0);
427 ddir[id].VirtualAddress = static_cast<DWORD> (dataDirAddr);
428 return true;
429}
430
432{
434 if (sec == NULL || sec->canAddEntry() == false) {
435 return false;
436 }
437 const size_t secCount = hdrSectionsNum();
438 if (secCount == SectHdrsWrapper::SECT_COUNT_MAX) return false; //limit exceeded
439
440 //TODO: some more checks? overlay?
441 return true;
442}
443
444
446{
447 if (canAddNewSection() == false) return NULL;
448
450 if (!v_size) v_size = size;
451
454 bufsize_t newSize = roundedRawEnd + size;
455 bufsize_t newVirtualSize = roundedVirtualEnd + v_size;
456
457 if (setVirtualSize(newVirtualSize) == false) {
458 Logger::append(Logger::D_ERROR, "Failed to change virtual size");
459 return NULL;
460 }
461
462 if (resize(newSize) == false) {
463 Logger::append(Logger::D_ERROR, "Failed to resize");
464 return NULL;
465 }
466 // fetch again after resize:
467 sec = dynamic_cast<ExeNodeWrapper*>(getWrapper(PEFile::WR_SECTIONS));
468 if (sec == NULL) {
469 return NULL;
470 }
471
472 IMAGE_SECTION_HEADER secHdr;
473 memset(&secHdr, 0, sizeof(IMAGE_SECTION_HEADER));
474
475 //name copy:
476 std::string nameStr = name.toStdString();
477 const char *nameChar = nameStr.c_str();
478 size_t copySize = sizeof(secHdr.Name);
479 size_t nameLen = strlen(nameChar);
480 if (nameLen < copySize) copySize = nameLen;
481 memcpy(secHdr.Name, nameChar, copySize);
482
483 secHdr.PointerToRawData = static_cast<DWORD>(roundedRawEnd);
484 secHdr.VirtualAddress = static_cast<DWORD>(roundedVirtualEnd);
485 secHdr.SizeOfRawData = size;
486 secHdr.Misc.VirtualSize = v_size;
487
488 SectionHdrWrapper wr(this, &secHdr);
489 SectionHdrWrapper* secHdrWr = dynamic_cast<SectionHdrWrapper*>(sec->addEntry(&wr));
490 return secHdrWr;
491}
492
494{
495 size_t secCount = this->getSectionsCount(true);
496 if (secCount == 0) return NULL;
497 return this->getSecHdr(secCount - 1);
498}
499
501{
502 offset_t lastMapped = 0;
503
504 /* check sections bounds */
505 const size_t secCounter = this->getSectionsCount(true);
506 if (!secCounter) {
507 // if PE file has no sections, full file will be mapped
508 return getMappedSize(aType);
509 }
510 for (size_t i = 0; i < secCounter; i++) {
511 SectionHdrWrapper *sec = this->getSecHdr(i);
512 if (!sec) continue;
513
514 offset_t secLastMapped= sec->getContentOffset(aType, true);
515 if (secLastMapped == INVALID_ADDR) continue;
516
517 const size_t size = (aType == Executable::RAW) ? sec->getMappedRawSize() : sec->getMappedVirtualSize();
518 secLastMapped += size;
519 if (secLastMapped > lastMapped) {
520 lastMapped = secLastMapped;
521 }
522 }
523
524 /* check header bounds */
525 /* section headers: */
526 if (lastMapped < this->secHdrsEndOffset()) {
527 lastMapped = this->secHdrsEndOffset();
528 }
529 /* NT headers: */
530 int ntHeadersEndOffset = this->core.peSignatureOffset() + this->core.hdrsSize();
531 if (lastMapped < ntHeadersEndOffset) {
532 lastMapped = ntHeadersEndOffset;
533 }
534 return lastMapped;
535}
536
538{
540 if (secHdr == NULL) return NULL;
541
542 //TODO: check overlay...
543 bufsize_t fullSize = getContentSize();
544 bufsize_t newSize = fullSize + addedSize;
545
546 offset_t secROffset = secHdr->getContentOffset(Executable::RAW, false);
547 bufsize_t secRSize = secHdr->getContentSize(Executable::RAW, false);
548 bufsize_t secNewRSize = newSize - secROffset; //include overlay in section
549
550 secHdr->setNumValue(SectionHdrWrapper::RSIZE, uint64_t(secNewRSize));
551
552 offset_t secVOffset = secHdr->getContentOffset(Executable::RVA, false);
553 bufsize_t secVSize = secHdr->getContentSize(Executable::RVA, false);
554 bufsize_t secNewVSize = secVSize;
555 // if the previous virtual size is smaller than the new raw size, then update it:
556 if (secVSize < secNewRSize) {
557 secNewVSize = secNewRSize;
558 secHdr->setNumValue(SectionHdrWrapper::VSIZE, uint64_t(secNewRSize));
559
560 // if the virtual size of section has changed,
561 // update the Size of Image (saved in the header):
562 bufsize_t newVSize = secVOffset + secNewVSize;
563 this->setVirtualSize(newVSize);
564 }
565
566 //update raw size:
567 this->resize(newSize);
568 //finally, retrieve the resized section:
569 return getLastSection();
570}
571
573{
574 IMAGE_DATA_DIRECTORY* ddir = this->getDataDirectory();
575 if (ddir[pe::DIR_BOUND_IMPORT].VirtualAddress == 0 && ddir[pe::DIR_BOUND_IMPORT].Size == 0) {
576 // No bound imports already, nothing to do here!
577 return true;
578 }
579 ddir[pe::DIR_BOUND_IMPORT].VirtualAddress = 0;
580 ddir[pe::DIR_BOUND_IMPORT].Size = 0;
581 DataDirEntryWrapper *bImp = this->getDataDirEntry(pe::DIR_BOUND_IMPORT);
582 if (bImp == NULL) {
583 //printf("No Bound imports wrapper!\n");
584 return false; // todo: throw error?
585 }
586 bool isOk = bImp->wrap();
587 //TODO: change timestamp for all library entries from (-1 : BOUND) to 0 : NOT BOUND
588 return isOk;
589}
590
591bool PEFile::dumpSection(SectionHdrWrapper *sec, QString fileName)
592{
594 return false; //not my section
595 }
596 BufferView *secView = this->_createSectionView(sec);
597 if (!secView) return false;
598
599 bufsize_t dumpedSize = FileBuffer::dump(fileName, *secView, false);
600 delete secView;
601
602 return dumpedSize ? true : false;
603}
604
605//protected:
606
608{
610 offset_t start = sec->getContentOffset(aType, true);
611 bufsize_t size = sec->getContentSize(aType, true);
612 if (start == INVALID_ADDR || size == 0) {
613 return NULL;
614 }
615 return new BufferView(this, start, size);
616}
617
618size_t PEFile::getExportsMap(QMap<offset_t,QString> &entrypoints, Executable::addr_type aType)
619{
620 size_t initialSize = entrypoints.size();
621
622 ExportDirWrapper* exports = dynamic_cast<ExportDirWrapper*>(this->getWrapper(PEFile::WR_DIR_ENTRY + pe::DIR_EXPORT));
623 if (!exports) return 0;
624
625 const size_t entriesCnt = exports->getEntriesCount();
626 if (entriesCnt == 0) return 0;
627
628 for (int i = 0; i < entriesCnt; i++) {
629 ExportEntryWrapper* entry = dynamic_cast<ExportEntryWrapper*>(exports->getEntryAt(i));
630 if (!entry) continue;
631
632 QString forwarder = entry->getForwarderStr();
633 if (forwarder.length()) {
634 continue;
635 }
636 offset_t rva = entry->getFuncRva();
637 offset_t offset = this->convertAddr(rva, Executable::RVA, aType);
638 if (offset == INVALID_ADDR) {
639 continue;
640 }
641 entrypoints.insert(offset, entry->getName());
642 }
643 return entrypoints.size() - initialSize;
644}
uint32_t bufsize_t
const offset_t INVALID_ADDR
uint64_t offset_t
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual offset_t getOffset(void *ptr, bool allowExceptions=false)
bool fillContent(BYTE filling)
static bufsize_t dump(const QString &fileName, AbstractByteBuffer &buf, bool allowExceptions=false)
Definition: FileBuffer.cpp:114
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField=FIELD_NONE)
virtual bool setNumValue(size_t fieldId, size_t subField, uint64_t val)
virtual bool canCopyToOffset(offset_t rawOffset)
virtual offset_t getFieldOffset(size_t fieldId, size_t subField=FIELD_NONE)
bool copyToOffset(offset_t rawOffset)
virtual uint64_t getNumValue(size_t fieldId, size_t subField, bool *isOk)
virtual bool wrap()
virtual ExeNodeWrapper * getEntryAt(size_t fieldId)
virtual bool canAddEntry()
virtual size_t getEntriesCount()
virtual ExeNodeWrapper * addEntry(ExeNodeWrapper *entry)
std::map< size_t, ExeElementWrapper * > wrappers
Definition: MappedExe.h:27
virtual ExeElementWrapper * getWrapper(size_t wrapperId)
Definition: MappedExe.cpp:13
AbstractByteBuffer * buf
Definition: Executable.h:125
virtual offset_t toRaw(offset_t offset, addr_type addrType, bool allowExceptions=false)
Definition: Executable.cpp:85
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition: Executable.h:57
virtual offset_t convertAddr(offset_t inAddr, Executable::addr_type inType, Executable::addr_type outType)
Definition: Executable.cpp:46
virtual bufsize_t getContentSize()
Definition: Executable.h:51
QString getForwarderStr()
offset_t getFuncRva()
virtual QString getName()
virtual void * getPtr()
virtual bool resize(bufsize_t newSize)
Definition: MappedExe.h:40
virtual void * getPtr()
bufsize_t hdrsSize() const
Definition: PECore.cpp:129
bool wrap(AbstractByteBuffer *v_buf)
Definition: PECore.cpp:13
virtual bufsize_t getImageSize()
Definition: PECore.cpp:117
offset_t peSignatureOffset() const
Definition: PECore.cpp:59
IMAGE_DOS_HEADER * dos
Definition: PECore.h:54
virtual Executable * build(AbstractByteBuffer *buf)
Definition: PEFile.cpp:31
virtual bool signatureMatches(AbstractByteBuffer *buf)
Definition: PEFile.cpp:4
Definition: PEFile.h:41
DataDirEntryWrapper * dataDirEntries[pe::DIR_ENTRIES_COUNT]
Definition: PEFile.h:269
size_t getSecIndex(SectionHdrWrapper *sec) const
Definition: PEFile.h:119
virtual bufsize_t getMappedSize(Executable::addr_type aType)
Definition: PEFile.cpp:241
BufferView * _createSectionView(SectionHdrWrapper *sec)
Definition: PEFile.cpp:607
size_t getExportsMap(QMap< offset_t, QString > &entrypoints, Executable::addr_type aType=Executable::RVA)
Definition: PEFile.cpp:618
SectHdrsWrapper * sects
Definition: PEFile.h:266
SectionHdrWrapper * getSecHdr(size_t index) const
Definition: PEFile.h:109
bool unbindImports()
Definition: PEFile.cpp:572
offset_t peDataDirOffset()
Definition: PEFile.cpp:229
virtual offset_t getEntryPoint(Executable::addr_type addrType=Executable::RVA)
Definition: PEFile.cpp:259
offset_t getMinSecRVA()
Definition: PEFile.cpp:217
friend class SectHdrsWrapper
Definition: PEFile.h:271
offset_t getLastMapped(Executable::addr_type aType)
Definition: PEFile.cpp:500
FileHdrWrapper * fHdr
Definition: PEFile.h:264
ResourcesAlbum * album
Definition: PEFile.h:268
bool setEntryPoint(offset_t entry, Executable::addr_type aType)
Definition: PEFile.cpp:274
size_t getSectionsCount(bool useMapped=true) const
Definition: PEFile.cpp:314
pe::RICH_DANS_HEADER * getRichHeaderBgn(pe::RICH_SIGNATURE *sign)
Definition: PEFile.cpp:160
SectionHdrWrapper * extendLastSection(bufsize_t addedSize)
Definition: PEFile.cpp:537
@ WR_DIR_ENTRY
Definition: PEFile.h:51
@ WR_DATADIR
Definition: PEFile.h:49
@ WR_OPTIONAL_HDR
Definition: PEFile.h:48
@ WR_FILE_HDR
Definition: PEFile.h:47
@ WR_DOS_HDR
Definition: PEFile.h:45
@ WR_SECTIONS
Definition: PEFile.h:50
@ WR_RICH_HDR
Definition: PEFile.h:46
PECore core
Definition: PEFile.h:256
void initDirEntries()
Definition: PEFile.cpp:95
virtual void wrap()
Definition: PEFile.cpp:102
SectionHdrWrapper * getLastSection()
Definition: PEFile.cpp:493
OptHdrWrapper * optHdr
Definition: PEFile.h:265
bool dumpSection(SectionHdrWrapper *sec, QString fileName)
Definition: PEFile.cpp:591
virtual offset_t rvaToRaw(offset_t rva)
Definition: PEFile.cpp:349
BufferView * createSectionView(size_t secNum)
Definition: PEFile.cpp:386
bool setHdrSectionsNum(size_t newNum)
Definition: PEFile.cpp:292
bool setVirtualSize(bufsize_t newSize)
Definition: PEFile.cpp:303
DataDirEntryWrapper * getDataDirEntry(pe::dir_entry eType)
Definition: PEFile.cpp:380
bool moveDataDirEntry(pe::dir_entry id, offset_t newOffset, Executable::addr_type addType=Executable::RAW)
Definition: PEFile.cpp:396
virtual void clearWrappers()
Definition: PEFile.cpp:89
IMAGE_DATA_DIRECTORY * getDataDirectory()
Definition: PEFile.cpp:235
virtual offset_t rawToRva(offset_t raw)
Definition: PEFile.cpp:322
bufsize_t hdrsSize()
Definition: PEFile.h:93
offset_t secHdrsEndOffset() const
Definition: PEFile.h:83
size_t hdrSectionsNum() const
Definition: PEFile.cpp:283
PEFile(AbstractByteBuffer *v_buf)
Definition: PEFile.cpp:80
virtual bufsize_t getAlignment(Executable::addr_type aType) const
Definition: PEFile.h:70
static long computeChecksum(BYTE *buffer, size_t bufferSize, size_t checksumOffset)
Definition: PEFile.cpp:45
pe::RICH_SIGNATURE * getRichHeaderSign()
Definition: PEFile.cpp:187
bool canAddNewSection()
Definition: PEFile.cpp:431
DosHdrWrapper * dosHdrWrapper
Definition: PEFile.h:262
SectionHdrWrapper * getSecHdrAtOffset(offset_t offset, Executable::addr_type aType, bool recalculate=false, bool verbose=false)
Definition: PEFile.h:114
SectionHdrWrapper * addNewSection(QString name, bufsize_t size, bufsize_t v_size=0)
Definition: PEFile.cpp:445
static size_t SECT_COUNT_MAX
static size_t SECT_INVALID_INDEX
bufsize_t getMappedVirtualSize()
bufsize_t getContentSize(Executable::addr_type aType, bool recalculate)
offset_t getContentOffset(Executable::addr_type aType, bool useMapped=true)
bufsize_t getMappedRawSize()
bool append(dbg_level lvl, const char *format,...)
Definition: Util.cpp:8
@ D_ERROR
Definition: Util.h:26
@ D_WARNING
Definition: Util.h:26
@ D_INFO
Definition: Util.h:26
bufsize_t roundupToUnit(bufsize_t size, bufsize_t unit)