summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/pe_properties_generic.cpp
blob: bcf6f2047da9c83f6610d86aac0198658224719c (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*************************************************************************/
/* Copyright (c) 2015 dx, http://kaimi.ru                                */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person           */
/* obtaining a copy of this software and associated documentation        */
/* files (the "Software"), to deal in the Software without               */
/* restriction, including without limitation the rights to use,          */
/* copy, modify, merge, publish, distribute, sublicense, and/or          */
/* sell copies of the Software, and to permit persons to whom the        */
/* Software is furnished to do so, subject to the following conditions:  */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include <string.h>
#include "pe_properties_generic.h"
#include "pe_exception.h"
#include "utils.h"

namespace pe_bliss
{
using namespace pe_win;
	
//Constructor
template<typename PEClassType>
std::auto_ptr<pe_properties> pe_properties_generic<PEClassType>::duplicate() const
{
	return std::auto_ptr<pe_properties>(new pe_properties_generic<PEClassType>(*this));
}

//Fills properly PE structures
template<typename PEClassType>
void pe_properties_generic<PEClassType>::create_pe(uint32_t section_alignment, uint16_t subsystem)
{
	memset(&nt_headers_, 0, sizeof(nt_headers_));
	nt_headers_.Signature = 0x4550; //"PE"
	nt_headers_.FileHeader.Machine = 0x14C; //i386
	nt_headers_.FileHeader.SizeOfOptionalHeader = sizeof(nt_headers_.OptionalHeader);
	nt_headers_.OptionalHeader.Magic = PEClassType::Id;
	nt_headers_.OptionalHeader.ImageBase = 0x400000;
	nt_headers_.OptionalHeader.SectionAlignment = section_alignment;
	nt_headers_.OptionalHeader.FileAlignment = 0x200;
	nt_headers_.OptionalHeader.SizeOfHeaders = 1024;
	nt_headers_.OptionalHeader.Subsystem = subsystem;
	nt_headers_.OptionalHeader.SizeOfHeapReserve = 0x100000;
	nt_headers_.OptionalHeader.SizeOfHeapCommit = 0x1000;
	nt_headers_.OptionalHeader.SizeOfStackReserve = 0x100000;
	nt_headers_.OptionalHeader.SizeOfStackCommit = 0x1000;
	nt_headers_.OptionalHeader.NumberOfRvaAndSizes = 0x10;
}

//Duplicate
template<typename PEClassType>
pe_properties_generic<PEClassType>::~pe_properties_generic()
{}

//Returns true if directory exists
template<typename PEClassType>
bool pe_properties_generic<PEClassType>::directory_exists(uint32_t id) const
{
	return (nt_headers_.OptionalHeader.NumberOfRvaAndSizes - 1) >= id &&
		nt_headers_.OptionalHeader.DataDirectory[id].VirtualAddress;
}

//Removes directory
template<typename PEClassType>
void pe_properties_generic<PEClassType>::remove_directory(uint32_t id)
{
	if(directory_exists(id))
	{
		nt_headers_.OptionalHeader.DataDirectory[id].VirtualAddress = 0;
		nt_headers_.OptionalHeader.DataDirectory[id].Size = 0;

		if(id == image_directory_entry_basereloc)
		{
			set_characteristics_flags(image_file_relocs_stripped);
			set_dll_characteristics(get_dll_characteristics() & ~image_dllcharacteristics_dynamic_base);
		}
		else if(id == image_directory_entry_export)
		{
			clear_characteristics_flags(image_file_dll);
		}
	}
}

//Returns directory RVA
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_directory_rva(uint32_t id) const
{
	//Check if directory exists
	if(nt_headers_.OptionalHeader.NumberOfRvaAndSizes <= id)
		throw pe_exception("Specified directory does not exist", pe_exception::directory_does_not_exist);

	return nt_headers_.OptionalHeader.DataDirectory[id].VirtualAddress;
}

//Returns directory size
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_directory_rva(uint32_t id, uint32_t va)
{
	//Check if directory exists
	if(nt_headers_.OptionalHeader.NumberOfRvaAndSizes <= id)
		throw pe_exception("Specified directory does not exist", pe_exception::directory_does_not_exist);

	nt_headers_.OptionalHeader.DataDirectory[id].VirtualAddress = va;
}

template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_directory_size(uint32_t id, uint32_t size)
{
	//Check if directory exists
	if(nt_headers_.OptionalHeader.NumberOfRvaAndSizes <= id)
		throw pe_exception("Specified directory does not exist", pe_exception::directory_does_not_exist);

	nt_headers_.OptionalHeader.DataDirectory[id].Size = size;
}

//Returns directory size
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_directory_size(uint32_t id) const
{
	//Check if directory exists
	if(nt_headers_.OptionalHeader.NumberOfRvaAndSizes <= id)
		throw pe_exception("Specified directory does not exist", pe_exception::directory_does_not_exist);

	return nt_headers_.OptionalHeader.DataDirectory[id].Size;
}

//Strips only zero DATA_DIRECTORY entries to count = min_count
//Returns resulting number of data directories
//strip_iat_directory - if true, even not empty IAT directory will be stripped
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::strip_data_directories(uint32_t min_count, bool strip_iat_directory)
{
	int i = nt_headers_.OptionalHeader.NumberOfRvaAndSizes - 1;

	//Enumerate all data directories from the end
	for(; i >= 0; i--)
	{
		//If directory exists, break
		if(nt_headers_.OptionalHeader.DataDirectory[i].VirtualAddress && (static_cast<uint32_t>(i) != image_directory_entry_iat || !strip_iat_directory))
			break;

		if(i <= static_cast<int>(min_count) - 2)
			break;
	}

	if(i == image_numberof_directory_entries - 1)
		return image_numberof_directory_entries;

	//Return new number of data directories
	return nt_headers_.OptionalHeader.NumberOfRvaAndSizes = i + 1;
}

//Returns image base for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_image_base_32() const
{
	return static_cast<uint32_t>(nt_headers_.OptionalHeader.ImageBase);
}

//Returns image base for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::get_image_base_64() const
{
	return static_cast<uint64_t>(nt_headers_.OptionalHeader.ImageBase);
}

//Sets new image base
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_image_base(uint32_t base)
{
	nt_headers_.OptionalHeader.ImageBase = base;
}

//Sets new image base
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_image_base_64(uint64_t base)
{
	nt_headers_.OptionalHeader.ImageBase = static_cast<typename PEClassType::BaseSize>(base);
}

//Returns image entry point
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_ep() const
{
	return nt_headers_.OptionalHeader.AddressOfEntryPoint;
}

//Sets image entry point
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_ep(uint32_t new_ep)
{
	nt_headers_.OptionalHeader.AddressOfEntryPoint = new_ep;
}

//Returns file alignment
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_file_alignment() const
{
	return nt_headers_.OptionalHeader.FileAlignment;
}

//Returns section alignment
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_section_alignment() const
{
	return nt_headers_.OptionalHeader.SectionAlignment;
}

//Sets heap size commit for PE32
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_heap_size_commit(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfHeapCommit = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets heap size commit for PE32/PE64
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_heap_size_commit(uint64_t size)
{
	nt_headers_.OptionalHeader.SizeOfHeapCommit = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets heap size reserve for PE32
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_heap_size_reserve(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfHeapReserve = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets heap size reserve for PE32/PE64
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_heap_size_reserve(uint64_t size)
{
	nt_headers_.OptionalHeader.SizeOfHeapReserve = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets stack size commit for PE32
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_stack_size_commit(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfStackCommit = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets stack size commit for PE32/PE64
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_stack_size_commit(uint64_t size)
{
	nt_headers_.OptionalHeader.SizeOfStackCommit = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets stack size reserve for PE32
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_stack_size_reserve(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfStackReserve = static_cast<typename PEClassType::BaseSize>(size);
}

//Sets stack size reserve for PE32/PE64
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_stack_size_reserve(uint64_t size)
{
	nt_headers_.OptionalHeader.SizeOfStackReserve = static_cast<typename PEClassType::BaseSize>(size);
}

//Returns heap size commit for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_heap_size_commit_32() const
{
	return static_cast<uint32_t>(nt_headers_.OptionalHeader.SizeOfHeapCommit);
}

//Returns heap size commit for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::get_heap_size_commit_64() const
{
	return static_cast<uint64_t>(nt_headers_.OptionalHeader.SizeOfHeapCommit);
}

//Returns heap size reserve for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_heap_size_reserve_32() const
{
	return static_cast<uint32_t>(nt_headers_.OptionalHeader.SizeOfHeapReserve);
}

//Returns heap size reserve for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::get_heap_size_reserve_64() const
{
	return static_cast<uint64_t>(nt_headers_.OptionalHeader.SizeOfHeapReserve);
}

//Returns stack size commit for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_stack_size_commit_32() const
{
	return static_cast<uint32_t>(nt_headers_.OptionalHeader.SizeOfStackCommit);
}

//Returns stack size commit for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::get_stack_size_commit_64() const
{
	return static_cast<uint64_t>(nt_headers_.OptionalHeader.SizeOfStackCommit);
}

//Returns stack size reserve for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_stack_size_reserve_32() const
{
	return static_cast<uint32_t>(nt_headers_.OptionalHeader.SizeOfStackReserve);
}

//Returns stack size reserve for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::get_stack_size_reserve_64() const
{
	return static_cast<uint64_t>(nt_headers_.OptionalHeader.SizeOfStackReserve);
}

//Returns virtual size of image
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_size_of_image() const
{
	return nt_headers_.OptionalHeader.SizeOfImage;
}

//Returns number of RVA and sizes (number of DATA_DIRECTORY entries)
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_number_of_rvas_and_sizes() const
{
	return nt_headers_.OptionalHeader.NumberOfRvaAndSizes;
}

//Sets number of RVA and sizes (number of DATA_DIRECTORY entries)
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_number_of_rvas_and_sizes(uint32_t number)
{
	nt_headers_.OptionalHeader.NumberOfRvaAndSizes = number;
}

//Returns PE characteristics
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_characteristics() const
{
	return nt_headers_.FileHeader.Characteristics;
}

//Returns checksum of PE file from header
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_checksum() const
{
	return nt_headers_.OptionalHeader.CheckSum;
}

//Sets checksum of PE file
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_checksum(uint32_t checksum)
{
	nt_headers_.OptionalHeader.CheckSum = checksum;
}

//Returns DLL Characteristics
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_dll_characteristics() const
{
	return nt_headers_.OptionalHeader.DllCharacteristics;
}

//Returns timestamp of PE file from header
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_time_date_stamp() const
{
	return nt_headers_.FileHeader.TimeDateStamp;
}

//Sets timestamp of PE file
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_time_date_stamp(uint32_t timestamp)
{
	nt_headers_.FileHeader.TimeDateStamp = timestamp;
}

//Sets DLL Characteristics
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_dll_characteristics(uint16_t characteristics)
{
	nt_headers_.OptionalHeader.DllCharacteristics = characteristics;
}

//Returns Machine field value of PE file from header
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_machine() const
{
	return nt_headers_.FileHeader.Machine;
}

//Sets Machine field value of PE file
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_machine(uint16_t machine)
{
	nt_headers_.FileHeader.Machine = machine;
}

//Sets PE characteristics
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_characteristics(uint16_t ch)
{
	nt_headers_.FileHeader.Characteristics = ch;
}

//Returns size of headers
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_size_of_headers() const
{
	return nt_headers_.OptionalHeader.SizeOfHeaders;
}

//Returns subsystem
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_subsystem() const
{
	return nt_headers_.OptionalHeader.Subsystem;
}

//Sets subsystem
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_subsystem(uint16_t subsystem)
{
	nt_headers_.OptionalHeader.Subsystem = subsystem;
}

//Returns size of optional header
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_size_of_optional_header() const
{
	return nt_headers_.FileHeader.SizeOfOptionalHeader;
}

//Returns PE signature
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_pe_signature() const
{
	return nt_headers_.Signature;
}

//Returns PE magic value
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_magic() const
{
	return nt_headers_.OptionalHeader.Magic;
}

//Sets required operation system version
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_os_version(uint16_t major, uint16_t minor)
{
	nt_headers_.OptionalHeader.MinorOperatingSystemVersion = minor;
	nt_headers_.OptionalHeader.MajorOperatingSystemVersion = major;
}

//Returns required operation system version (minor word)
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_minor_os_version() const
{
	return nt_headers_.OptionalHeader.MinorOperatingSystemVersion;
}

//Returns required operation system version (major word)
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_major_os_version() const
{
	return nt_headers_.OptionalHeader.MajorOperatingSystemVersion;
}

//Sets required subsystem version
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_subsystem_version(uint16_t major, uint16_t minor)
{
	nt_headers_.OptionalHeader.MinorSubsystemVersion = minor;
	nt_headers_.OptionalHeader.MajorSubsystemVersion = major;
}

//Returns required subsystem version (minor word)
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_minor_subsystem_version() const
{
	return nt_headers_.OptionalHeader.MinorSubsystemVersion;
}

//Returns required subsystem version (major word)
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_major_subsystem_version() const
{
	return nt_headers_.OptionalHeader.MajorSubsystemVersion;
}

//Virtual Address (VA) to Relative Virtual Address (RVA) convertions for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::va_to_rva(uint32_t va, bool bound_check) const
{
	if(bound_check && static_cast<uint64_t>(va) - nt_headers_.OptionalHeader.ImageBase > pe_utils::max_dword)
		throw pe_exception("Incorrect address conversion", pe_exception::incorrect_address_conversion);

	return static_cast<uint32_t>(va - nt_headers_.OptionalHeader.ImageBase);
}

//Virtual Address (VA) to Relative Virtual Address (RVA) convertions for PE32/PE64
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::va_to_rva(uint64_t va, bool bound_check) const
{
	if(bound_check && va - nt_headers_.OptionalHeader.ImageBase > pe_utils::max_dword)
		throw pe_exception("Incorrect address conversion", pe_exception::incorrect_address_conversion);

	return static_cast<uint32_t>(va - nt_headers_.OptionalHeader.ImageBase);
}

//Relative Virtual Address (RVA) to Virtual Address (VA) convertions for PE32
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::rva_to_va_32(uint32_t rva) const
{
	if(!pe_utils::is_sum_safe(rva, static_cast<uint32_t>(nt_headers_.OptionalHeader.ImageBase)))
		throw pe_exception("Incorrect address conversion", pe_exception::incorrect_address_conversion);

	return static_cast<uint32_t>(rva + nt_headers_.OptionalHeader.ImageBase);
}

//Relative Virtual Address (RVA) to Virtual Address (VA) convertions for PE32/PE64
template<typename PEClassType>
uint64_t pe_properties_generic<PEClassType>::rva_to_va_64(uint32_t rva) const
{
	return static_cast<uint64_t>(rva) + nt_headers_.OptionalHeader.ImageBase;
}

//Returns number of sections
template<typename PEClassType>
uint16_t pe_properties_generic<PEClassType>::get_number_of_sections() const
{
	return nt_headers_.FileHeader.NumberOfSections;
}

//Sets number of sections
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_number_of_sections(uint16_t number)
{
	nt_headers_.FileHeader.NumberOfSections = number;
}

//Sets virtual size of image
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_size_of_image(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfImage = size;
}

//Sets size of headers
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_size_of_headers(uint32_t size)
{
	nt_headers_.OptionalHeader.SizeOfHeaders = size;
}

//Sets size of optional headers
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_size_of_optional_header(uint16_t size)
{
	nt_headers_.FileHeader.SizeOfOptionalHeader = size;
}

//Returns nt headers data pointer
template<typename PEClassType>
char* pe_properties_generic<PEClassType>::get_nt_headers_ptr()
{
	return reinterpret_cast<char*>(&nt_headers_);
}

//Returns nt headers data pointer
template<typename PEClassType>
const char* pe_properties_generic<PEClassType>::get_nt_headers_ptr() const
{
	return reinterpret_cast<const char*>(&nt_headers_);
}

//Returns size of NT header
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_sizeof_nt_header() const
{
	return sizeof(typename PEClassType::NtHeaders);
}

//Returns size of optional headers
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_sizeof_opt_headers() const
{
	return sizeof(typename PEClassType::OptHeaders);
}

//Sets file alignment (no checks)
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_file_alignment_unchecked(uint32_t alignment) 
{
	nt_headers_.OptionalHeader.FileAlignment = alignment;
}

//Sets base of code
template<typename PEClassType>
void pe_properties_generic<PEClassType>::set_base_of_code(uint32_t base)
{
	nt_headers_.OptionalHeader.BaseOfCode = base;
}

//Returns base of code
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_base_of_code() const
{
	return nt_headers_.OptionalHeader.BaseOfCode;
}

//Returns needed PE magic for PE or PE+ (from template parameters)
template<typename PEClassType>
uint32_t pe_properties_generic<PEClassType>::get_needed_magic() const
{
	return PEClassType::Id;
}

//Returns PE type of this image
template<typename PEClassType>
pe_type pe_properties_generic<PEClassType>::get_pe_type() const
{
	return PEClassType::Id == image_nt_optional_hdr32_magic ? pe_type_32 : pe_type_64;
}

//Two used instantiations for PE32 (PE) and PE64 (PE+)
template class pe_properties_generic<pe_types_class_32>;
template class pe_properties_generic<pe_types_class_64>;
}