-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionalHeader.cpp
64 lines (48 loc) · 2.07 KB
/
OptionalHeader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "OptionalHeader.hpp"
#include "Defines.hpp"
#include "ParseException.hpp"
OptionalHeader::OptionalHeader() {
}
OptionalHeader::~OptionalHeader() {
}
void OptionalHeader::parse(Buffer buffer, size_t header_offset) {
// Get magic number
uint16_t magic = buffer.get<uint16_t>(header_offset);
switch(magic) {
case IMAGE_PE32:
{
this->bPe32Plus = false;
nPE32 optional_struct = buffer.get<nPE32>(header_offset);
memcpy(&magic, &optional_struct.magic, 24);
this->image_base = optional_struct.image_base;
memcpy(§ion_alignment, &optional_struct.section_alignment, 40);
this->stack_reserve_size = optional_struct.stack_reserve_size;
this->stack_commit_size = optional_struct.stack_commit_size;
this->heap_reserve_size = optional_struct.heap_reserve_size;
this->heap_commit_size = optional_struct.heap_commit_size;
memcpy(&loader_flags, &optional_struct.loader_flags, 8);
break;
}
case IMAGE_PE32PLUS:
{
this->bPe32Plus = true;
nPE32PLUS optional_struct = buffer.get<nPE32PLUS>(header_offset);
memcpy(&magic, &optional_struct.magic, 20);
this->image_base = optional_struct.image_base;
memcpy(§ion_alignment, &optional_struct.section_alignment, 40);
this->stack_reserve_size = optional_struct.stack_reserve_size;
this->stack_commit_size = optional_struct.stack_commit_size;
this->heap_reserve_size = optional_struct.heap_reserve_size;
this->heap_commit_size = optional_struct.heap_commit_size;
memcpy(&loader_flags, &optional_struct.loader_flags, 8);
break;
}
default:
{
throw new ParseException(NO_MAGIC);
}
}
}
size_t OptionalHeader::sizeOf() {
return bPe32Plus ? sizeof(nPE32PLUS) : sizeof(nPE32);
}