LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
DosHeader.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_DOS_HEADER_H
17#define LIEF_PE_DOS_HEADER_H
18#include <cstdint>
19#include <array>
20#include <ostream>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/visibility.h"
24
25namespace LIEF {
26namespace PE {
27
28enum class PE_TYPE : uint16_t;
29
30namespace details {
31struct pe_dos_header;
32}
33
38class LIEF_API DosHeader : public Object {
39 public:
40 using reserved_t = std::array<uint16_t, 4>;
41 using reserved2_t = std::array<uint16_t, 10>;
42
43 static constexpr uint16_t MAGIC = 0x5a4d; // MZ
44
45 DosHeader(const details::pe_dos_header& header);
46 DosHeader(const DosHeader&);
47 DosHeader& operator=(const DosHeader&);
48
50 DosHeader& operator=(DosHeader&&);
51
52 ~DosHeader() override;
53
55 uint16_t magic() const {
56 return magic_;
57 }
58
59 uint16_t used_bytes_in_last_page() const {
60 return used_bytes_in_last_page_;
61 }
62
63 uint16_t file_size_in_pages() const {
64 return file_sz_in_pages_;
65 }
66
67 uint16_t numberof_relocation() const {
68 return nb_relocations_;
69 }
70
71 uint16_t header_size_in_paragraphs() const {
72 return header_sz_in_paragraphs_;
73 }
74
75 uint16_t minimum_extra_paragraphs() const {
76 return min_extra_paragraphs_;
77 }
78
79 uint16_t maximum_extra_paragraphs() const {
80 return max_extra_paragraphs_;
81 }
82
83 uint16_t initial_relative_ss() const {
84 return init_relative_ss_;
85 }
86
87 uint16_t initial_sp() const {
88 return init_sp_;
89 }
90
91 uint16_t checksum() const {
92 return checksum_;
93 }
94
95 uint16_t initial_ip() const {
96 return init_ip_;
97 }
98
99 uint16_t initial_relative_cs() const {
100 return init_rel_cs_;
101 }
102
103 uint16_t addressof_relocation_table() const {
104 return addr_reloc_table_;
105 }
106
107 uint16_t overlay_number() const {
108 return overlay_number_;
109 }
110
111 const reserved_t& reserved() const {
112 return reserved_;
113 }
114
115 uint16_t oem_id() const {
116 return oem_id_;
117 }
118
119 uint16_t oem_info() const {
120 return oem_info_;
121 }
122
123 const reserved2_t& reserved2() const {
124 return reserved2_;
125 }
126
128 uint32_t addressof_new_exeheader() const {
129 return addr_new_exe_header_;
130 }
131
132 void magic(uint16_t magic) {
133 magic_ = magic;
134 }
135
136 void used_bytes_in_last_page(uint16_t value) {
137 used_bytes_in_last_page_ = value;
138 }
139
140 void file_size_in_pages(uint16_t value) {
141 file_sz_in_pages_ = value;
142 }
143
144 void numberof_relocation(uint16_t value) {
145 nb_relocations_ = value;
146 }
147
148 void header_size_in_paragraphs(uint16_t value) {
149 header_sz_in_paragraphs_ = value;
150 }
151
152 void minimum_extra_paragraphs(uint16_t value) {
153 min_extra_paragraphs_ = value;
154 }
155
156 void maximum_extra_paragraphs(uint16_t value) {
157 max_extra_paragraphs_ = value;
158 }
159
160 void initial_relative_ss(uint16_t value) {
161 init_relative_ss_ = value;
162 }
163
164 void initial_sp(uint16_t value) {
165 init_sp_ = value;
166 }
167
168 void checksum(uint16_t value) {
169 checksum_ = value;
170 }
171
172 void initial_ip(uint16_t value) {
173 init_ip_ = value;
174 }
175
176 void initial_relative_cs(uint16_t value) {
177 init_rel_cs_ = value;
178 }
179
180 void addressof_relocation_table(uint16_t value) {
181 addr_reloc_table_ = value;
182 }
183
184 void overlay_number(uint16_t value) {
185 overlay_number_ = value;
186 }
187
188 void reserved(const reserved_t& reserved) {
189 reserved_ = reserved;
190 }
191
192 void oem_id(uint16_t value) {
193 oem_id_ = value;
194 }
195
196 void oem_info(uint16_t value) {
197 oem_info_ = value;
198 }
199
200 void reserved2(const reserved2_t& reserved2) {
201 reserved2_ = reserved2;
202 }
203
204 void addressof_new_exeheader(uint32_t value) {
205 addr_new_exe_header_ = value;
206 }
207
208 void accept(Visitor& visitor) const override;
209
210 LIEF_API friend std::ostream& operator<<(std::ostream& os, const DosHeader& entry);
211
212 static DosHeader create(PE_TYPE type);
213
214 private:
215 DosHeader();
216
217 uint16_t magic_ = 0;
218 uint16_t used_bytes_in_last_page_ = 0;
219 uint16_t file_sz_in_pages_ = 0;
220 uint16_t nb_relocations_ = 0;
221 uint16_t header_sz_in_paragraphs_ = 0;
222 uint16_t min_extra_paragraphs_ = 0;
223 uint16_t max_extra_paragraphs_ = 0;
224 uint16_t init_relative_ss_ = 0;
225 uint16_t init_sp_ = 0;
226 uint16_t checksum_ = 0;
227 uint16_t init_ip_ = 0;
228 uint16_t init_rel_cs_ = 0;
229 uint16_t addr_reloc_table_ = 0;
230 uint16_t overlay_number_ = 0;
231 reserved_t reserved_;
232 uint16_t oem_id_ = 0;
233 uint16_t oem_info_ = 0;
234 reserved2_t reserved2_;
235 uint32_t addr_new_exe_header_ = 0;
236};
237}
238}
239
240#endif
241
Definition Object.hpp:25
Class which represents the DosHeader, the first structure presents at the beginning of a PE file.
Definition DosHeader.hpp:38
uint16_t magic() const
Magic bytes identifying a DOS/PE binary.
Definition DosHeader.hpp:55
uint32_t addressof_new_exeheader() const
Return the offset to the PE::Header structure.
Definition DosHeader.hpp:128
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:32