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 &e) {
26 //
27 }
28 return exe;
29}
30
31//-------------------------------------------------------------
32
34 : MappedExe(v_buf, Executable::BITS_16), dosHdrWrapper(NULL)
35{
36 wrap(buf);
37}
38
40{
41 this->dosHdrWrapper = new DosHdrWrapper(this);
42
43 m_dosHdr = (IMAGE_DOS_HEADER*) getContentAt(0, sizeof(IMAGE_DOS_HEADER));
44 if (m_dosHdr == NULL) throw ExeException("Could not Wrap!");
45
46 WORD* magic = (WORD*) this->dosHdrWrapper->getFieldPtr(DosHdrWrapper::MAGIC);
47
48 if (this->dosHdrWrapper->getPtr() == NULL || magic == NULL) {
49 throw ExeException("Could not Wrap!");
50 }
51
52 if ((*magic) != pe::S_DOS && (*magic) != pe::S_DOS2) {
53 Logger::append(Logger::D_WARNING, "It is not a DOS file!\n");
54 throw ExeException("It is not a DOS file!");
55 }
56 this->wrappers[WR_DOS_HDR] = this->dosHdrWrapper;
57}
58
60{
61 LONG* lfnew = (LONG*) this->dosHdrWrapper->getFieldPtr(DosHdrWrapper::LFNEW);
62 if (lfnew == NULL) return 0;
63
64 return static_cast<offset_t>(*lfnew);
65}
66
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
Definition: DOSExe.h:19
DOSExe(AbstractByteBuffer *v_buf)
Definition: DOSExe.cpp:33
IMAGE_DOS_HEADER * m_dosHdr
Definition: DOSExe.h:68
offset_t peSignatureOffset()
Definition: DOSExe.cpp:59
DosHdrWrapper * dosHdrWrapper
Definition: DOSExe.h:67
@ WR_DOS_HDR
Definition: DOSExe.h:23
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
virtual void * getPtr()
Definition: DosHdrWrapper.h:39
std::map< size_t, ExeElementWrapper * > wrappers
Definition: MappedExe.h:27
AbstractByteBuffer * buf
Definition: Executable.h:125
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition: Executable.h:57
virtual void wrap()
Definition: MappedExe.h:32
bool append(dbg_level lvl, const char *format,...)
Definition: Util.cpp:8
@ D_WARNING
Definition: Util.h:26