This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
pe_section_io.cpp
393 lines (286 loc) · 11 KB
/
pe_section_io.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "stdafx.h"
#include "pe_section_io.h"
extern bool view_data(
size_t required_offset, size_t required_size, size_t& real_offset,
size_t& available_size, size_t& down_oversize, size_t& up_oversize,
size_t present_offset, size_t present_size);
pe_section_io::pe_section_io(pe_section & _section,
enma_io_mode mode,
enma_io_addressing_type type,
uint32_t raw_aligment,
uint32_t virtual_aligment) :
section(&_section), section_offset(0),
raw_aligment(raw_aligment), virtual_aligment(virtual_aligment),
last_code(enma_io_code::enma_io_success), mode(mode), addressing_type(type){}
pe_section_io::pe_section_io(
pe_section & _section,
pe_image& image,
enma_io_mode mode,
enma_io_addressing_type type
): section(&_section), section_offset(0),
raw_aligment(image.get_file_align()), virtual_aligment(image.get_section_align()),
last_code(enma_io_code::enma_io_success), mode(mode), addressing_type(type){}
pe_section_io::pe_section_io(const pe_section_io& io_section) {
this->operator=(io_section);
}
pe_section_io::~pe_section_io() {
}
pe_section_io& pe_section_io::operator=(const pe_section_io& io_section) {
this->section = io_section.section;
this->section_offset = io_section.section_offset;
this->last_code = io_section.last_code;
this->mode = io_section.mode;
this->addressing_type = io_section.addressing_type;
this->raw_aligment = io_section.raw_aligment;
this->virtual_aligment = io_section.virtual_aligment;
return *this;
}
bool pe_section_io::view_section(
size_t required_offset, size_t required_size, size_t& real_offset,
size_t& available_size, size_t& down_oversize, size_t& up_oversize) {
switch (addressing_type) {
case enma_io_addressing_type::enma_io_address_raw: {
return view_data(
required_offset, required_size,
real_offset, available_size, down_oversize, up_oversize,
this->section->get_pointer_to_raw(), ALIGN_UP(section->get_size_of_raw_data(), this->raw_aligment)
);
}
case enma_io_addressing_type::enma_io_address_rva: {
return view_data(
required_offset, required_size,
real_offset, available_size, down_oversize, up_oversize,
this->section->get_virtual_address(), ALIGN_UP(this->section->get_virtual_size(), this->virtual_aligment)
);
}
default: {return false; }
}
return false;
}
uint32_t pe_section_io::get_present_size(uint32_t required_offset) {
if (required_offset < section->get_size_of_raw_data()) {
return section->get_size_of_raw_data() - required_offset;
}
return 0;
}
enma_io_code pe_section_io::internal_read(
size_t data_offset,
void * buffer, size_t size,
size_t& readed_size, size_t& down_oversize, size_t& up_oversize) {
size_t real_offset = 0;
bool b_view = view_section(data_offset, size,
real_offset,
readed_size, down_oversize, up_oversize);
if (b_view && readed_size) {
size_t present_size = get_present_size((uint32_t)real_offset);
if (present_size) {
if (present_size >= readed_size) {
memcpy(&((uint8_t*)buffer)[down_oversize], §ion->get_section_data().data()[real_offset], readed_size);
}
else {
memcpy(&((uint8_t*)buffer)[down_oversize], §ion->get_section_data().data()[real_offset], present_size);
memset(&((uint8_t*)buffer)[down_oversize + present_size], 0, readed_size - present_size);
}
}
else {
memset(&((uint8_t*)buffer)[down_oversize], 0, readed_size);
}
if (down_oversize || up_oversize) {
last_code = enma_io_code::enma_io_incomplete;
return enma_io_code::enma_io_incomplete;
}
last_code = enma_io_code::enma_io_success;
return enma_io_code::enma_io_success;
}
last_code = enma_io_code::enma_io_data_not_present;
return enma_io_code::enma_io_data_not_present;
}
enma_io_code pe_section_io::internal_write(
size_t data_offset,
const void * buffer, size_t size,
size_t& wrote_size, size_t& down_oversize, size_t& up_oversize) {
size_t real_offset = 0;
bool b_view = view_section(data_offset, size,
real_offset,
wrote_size, down_oversize, up_oversize);
if (b_view &&
(wrote_size || (up_oversize && mode == enma_io_mode::enma_io_mode_allow_expand) )) {
if ((up_oversize && mode == enma_io_mode::enma_io_mode_allow_expand)) {
if (addressing_type == enma_io_addressing_type::enma_io_address_raw) {
add_size(
(ALIGN_UP(section->get_size_of_raw_data(), this->raw_aligment) - section->get_size_of_raw_data()) + up_oversize
, false);
wrote_size += min(up_oversize, size);
up_oversize = 0;
}
else if (addressing_type == enma_io_addressing_type::enma_io_address_rva) {
add_size(
(ALIGN_UP(section->get_virtual_size(), this->virtual_aligment) - section->get_virtual_size()) + up_oversize
, false);
wrote_size += min(up_oversize, size);
up_oversize = 0;
}
else {
return enma_io_code::enma_io_data_not_present;//unk addressing_type
}
}
if (real_offset + wrote_size > section->get_size_of_raw_data()) { //aligned by raw align
add_size((real_offset + wrote_size) - section->get_size_of_raw_data(), false);
}
memcpy(§ion->get_section_data().data()[real_offset], &((uint8_t*)buffer)[down_oversize], wrote_size);
if (down_oversize || up_oversize) {
last_code = enma_io_code::enma_io_incomplete;
return enma_io_code::enma_io_incomplete;
}
last_code = enma_io_code::enma_io_success;
return enma_io_code::enma_io_success;
}
last_code = enma_io_code::enma_io_data_not_present;
return enma_io_code::enma_io_data_not_present;
}
enma_io_code pe_section_io::read(void * data, size_t size) {
size_t readed_size;
size_t down_oversize;
size_t up_oversize;
enma_io_code code = internal_read(section_offset, data, size,
readed_size, down_oversize, up_oversize);
section_offset += (uint32_t)readed_size;
return code;
}
enma_io_code pe_section_io::write(const void * data, size_t size) {
size_t wrote_size;
size_t down_oversize;
size_t up_oversize;
enma_io_code code = internal_write(section_offset, data, size,
wrote_size, down_oversize, up_oversize);
section_offset += (uint32_t)wrote_size;
return code;
}
enma_io_code pe_section_io::read(std::vector<uint8_t>& buffer, size_t size) {
if (buffer.size() < size) { buffer.resize(size); }
return read(buffer.data(), uint32_t(buffer.size()));
}
enma_io_code pe_section_io::write(const std::vector<uint8_t>& buffer) {
return write(buffer.data(), uint32_t(buffer.size()));
}
enma_io_code pe_section_io::memory_set(size_t size, uint8_t data) {
std::vector<uint8_t> set_buffer;
set_buffer.resize(size);
memset(set_buffer.data(), data, size);
return write(set_buffer);
}
enma_io_code pe_section_io::read_string(std::string& _string) {
_string.clear();
char _char = 0;
do {
enma_io_code code = read(&_char, sizeof(_char));
if (code != enma_io_success) {
return code;
}
if (_char) {
_string += _char;
}
} while (_char);
return enma_io_code::enma_io_success;
}
enma_io_code pe_section_io::read_wstring(std::wstring& _wstring) {
_wstring.clear();
wchar_t _char = 0;
do {
enma_io_code code = read(&_char, sizeof(_char));
if (code != enma_io_success) {
return code;
}
if (_char) {
_wstring += _char;
}
} while (_char);
return enma_io_code::enma_io_success;
}
pe_section_io& pe_section_io::align_up(size_t factor, bool offset_to_end) {
add_size(ALIGN_UP(section->get_size_of_raw_data(), factor) - section->get_size_of_raw_data(), offset_to_end);
return *this;
}
pe_section_io& pe_section_io::align_up_offset(size_t factor) {
this->section_offset = (uint32_t)ALIGN_UP(this->section_offset, factor);
return *this;
}
pe_section_io& pe_section_io::add_size(size_t size, bool offset_to_end) {
if (size) {
section->get_section_data().resize(section->get_size_of_raw_data() + size);
}
if (section->get_size_of_raw_data() > section->get_virtual_size()) {
section->set_virtual_size(section->get_size_of_raw_data());
}
if (offset_to_end) { seek_to_end(); }
return *this;
}
pe_section_io& pe_section_io::set_mode(enma_io_mode mode) {
this->mode = mode;
return *this;
}
pe_section_io& pe_section_io::set_addressing_type(enma_io_addressing_type type) {
this->addressing_type = type;
return *this;
}
pe_section_io& pe_section_io::set_section_offset(uint32_t offset) {
this->section_offset = offset;
return *this;
}
pe_section_io& pe_section_io::set_raw_aligment(uint32_t aligment) {
this->raw_aligment = aligment;
return *this;
}
pe_section_io& pe_section_io::set_virtual_aligment(uint32_t aligment) {
this->virtual_aligment = aligment;
return *this;
}
pe_section_io& pe_section_io::seek_to_start() {
switch (addressing_type) {
case enma_io_addressing_type::enma_io_address_raw: {
this->section_offset = this->section->get_pointer_to_raw();
break;
}
case enma_io_addressing_type::enma_io_address_rva: {
this->section_offset = this->section->get_virtual_address();
break;
}
default: {this->section_offset = 0; break; }
}
return *this;
}
pe_section_io& pe_section_io::seek_to_end() {
switch (addressing_type) {
case enma_io_addressing_type::enma_io_address_raw: {
this->section_offset = this->section->get_pointer_to_raw() + this->section->get_size_of_raw_data();
break;
}
case enma_io_addressing_type::enma_io_address_rva: {
this->section_offset = this->section->get_virtual_address() + this->section->get_virtual_size();
break;
}
default: {this->section_offset = 0; break; }
}
return *this;
}
enma_io_mode pe_section_io::get_mode() const {
return this->mode;
}
enma_io_code pe_section_io::get_last_code() const {
return this->last_code;
}
enma_io_addressing_type pe_section_io::get_addressing_type() const {
return this->addressing_type;
}
uint32_t pe_section_io::get_section_offset() const {
return this->section_offset;
}
uint32_t pe_section_io::get_raw_aligment() const {
return this->raw_aligment;
}
uint32_t pe_section_io::get_virtual_aligment() const {
return this->virtual_aligment;
}
pe_section* pe_section_io::get_section() {
return this->section;
}