BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
DOSExe.cpp
Go to the documentation of this file.
1#include "pe/DOSExe.h"
2
3
5{
6 if (buf == NULL) return false;
7
8 WORD *magic = (WORD*) buf->getContentAt(0, sizeof(WORD));
9 if (magic == NULL) return false;
10
11 if ((*magic) == pe::S_DOS || (*magic) == pe::S_DOS2) {
12 return true;
13 }
14
15 return false;
16}
17
19{
20 Executable *exe = NULL;
21 if (signatureMatches(buf) == false) return NULL;
22
23 try {
24 exe = new DOSExe(buf);
25 } catch (ExeException) {
26 //
27 }
28
29 return exe;
30}
31
32//-------------------------------------------------------------
33
35 : MappedExe(v_buf, Executable::BITS_16), dosHdrWrapper(NULL)
36{
37 wrap();
38}
39
41{
42 this->dosHdrWrapper = new DosHdrWrapper(this);
43
44 m_dosHdr = (IMAGE_DOS_HEADER*) getContentAt(0, sizeof(IMAGE_DOS_HEADER));
45 if (m_dosHdr == NULL) throw ExeException("Could not Wrap!");
46
47 WORD* magic = (WORD*) this->dosHdrWrapper->getFieldPtr(DosHdrWrapper::MAGIC);
48
49 if (this->dosHdrWrapper->getPtr() == NULL || magic == NULL) {
50 throw ExeException("Could not Wrap!");
51 }
52
53 if ((*magic) != pe::S_DOS && (*magic) != pe::S_DOS2) {
54 Logger::append(Logger::D_WARNING, "It is not a DOS file!\n");
55 throw ExeException("It is not a DOS file!");
56 }
57 this->wrappers[WR_DOS_HDR] = this->dosHdrWrapper;
58}
59
61{
62 LONG* lfnew = (LONG*) this->dosHdrWrapper->getFieldPtr(DosHdrWrapper::LFNEW);
63 if (lfnew == NULL) return 0;
64
65 return static_cast<offset_t>(*lfnew);
66}
67
uint64_t offset_t
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual Executable * build(AbstractByteBuffer *buf)
Definition DOSExe.cpp:18
virtual bool signatureMatches(AbstractByteBuffer *buf)
Definition DOSExe.cpp:4
virtual void wrap()
Definition DOSExe.cpp:40
DOSExe(AbstractByteBuffer *v_buf)
Definition DOSExe.cpp:34
IMAGE_DOS_HEADER * m_dosHdr
Definition DOSExe.h:70
offset_t peSignatureOffset()
Definition DOSExe.cpp:60
DosHdrWrapper * dosHdrWrapper
Definition DOSExe.h:69
@ WR_DOS_HDR
Definition DOSExe.h:23
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
virtual void * getPtr()
std::map< size_t, ExeElementWrapper * > wrappers
Definition MappedExe.h:27
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition Executable.h:65
bool append(dbg_level lvl, const char *format,...)
Definition Util.cpp:8
@ D_WARNING
Definition Util.h:26