summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/pe_base.cpp
blob: 97baa17cb38d35cee4903367298de8c10948c5dd (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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
/*************************************************************************/
/* 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>
#include <vector>
#include <istream>
#include <ostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string.h>
#include "pe_exception.h"
#include "pe_base.h"

namespace pe_bliss
{
using namespace pe_win;

//Constructor
pe_base::pe_base(std::istream& file, const pe_properties& props, bool read_debug_raw_data)
{
	props_ = props.duplicate().release();

	//Save istream state
	std::ios_base::iostate state = file.exceptions();
	std::streamoff old_offset = file.tellg();

	try
	{
		file.exceptions(std::ios::goodbit);
		//Read DOS header, PE headers and section data
		read_dos_header(file);
		read_pe(file, read_debug_raw_data);
	}
	catch(const std::exception&)
	{
		//If something went wrong, restore istream state
		file.seekg(old_offset);
		file.exceptions(state);
		file.clear();
		//Rethrow
		throw;
	}

	//Restore istream state
	file.seekg(old_offset);
	file.exceptions(state);
	file.clear();
}

pe_base::pe_base(const pe_properties& props, uint32_t section_alignment, bool dll, uint16_t subsystem)
{
	props_ = props.duplicate().release();
	props_->create_pe(section_alignment, subsystem);

	has_overlay_ = false;
	memset(&dos_header_, 0, sizeof(dos_header_));

	dos_header_.e_magic = 0x5A4D; //"MZ"
	//Magic numbers from MSVC++ build
	dos_header_.e_maxalloc = 0xFFFF;
	dos_header_.e_cblp = 0x90;
	dos_header_.e_cp = 3;
	dos_header_.e_cparhdr = 4;
	dos_header_.e_sp = 0xB8;
	dos_header_.e_lfarlc = 64;

	set_characteristics(image_file_executable_image | image_file_relocs_stripped);

	if(get_pe_type() == pe_type_32)
		set_characteristics_flags(image_file_32bit_machine);

	if(dll)
		set_characteristics_flags(image_file_dll);

	set_subsystem_version(5, 1); //WinXP
	set_os_version(5, 1); //WinXP
}

pe_base::pe_base(const pe_base& pe)
	:dos_header_(pe.dos_header_),
	rich_overlay_(pe.rich_overlay_),
	sections_(pe.sections_),
	has_overlay_(pe.has_overlay_),
	full_headers_data_(pe.full_headers_data_),
	debug_data_(pe.debug_data_),
	props_(0)
{
	props_ = pe.props_->duplicate().release();
}

pe_base& pe_base::operator=(const pe_base& pe)
{
	dos_header_ = pe.dos_header_;
	rich_overlay_ = pe.rich_overlay_;
	sections_ = pe.sections_;
	has_overlay_ = pe.has_overlay_;
	full_headers_data_ = pe.full_headers_data_;
	debug_data_ = pe.debug_data_;
	delete props_;
	props_ = 0;
	props_ = pe.props_->duplicate().release();

	return *this;
}

pe_base::~pe_base()
{
	delete props_;
}

//Returns dos header
const image_dos_header& pe_base::get_dos_header() const
{
	return dos_header_;
}

//Returns dos header
image_dos_header& pe_base::get_dos_header()
{
	return dos_header_;
}

//Returns PE headers start position (e_lfanew)
int32_t pe_base::get_pe_header_start() const
{
	return dos_header_.e_lfanew;
}

//Strips MSVC stub overlay
void pe_base::strip_stub_overlay()
{
	rich_overlay_.clear();
}

//Fills MSVC stub overlay with character c
void pe_base::fill_stub_overlay(char c)
{
	if(rich_overlay_.length())
		rich_overlay_.assign(rich_overlay_.length(), c);
}

//Sets stub MSVS overlay
void pe_base::set_stub_overlay(const std::string& data)
{
	rich_overlay_ = data;
}

//Returns stub overlay
const std::string& pe_base::get_stub_overlay() const
{
	return rich_overlay_;
}

//Realigns all sections
void pe_base::realign_all_sections()
{
	for(unsigned int i = 0; i < sections_.size(); i++)
		realign_section(i);
}

//Returns number of sections from PE header
uint16_t pe_base::get_number_of_sections() const
{
	return props_->get_number_of_sections();
}

//Updates number of sections in PE header
uint16_t pe_base::update_number_of_sections()
{
	uint16_t new_number = static_cast<uint16_t>(sections_.size());
	props_->set_number_of_sections(new_number);
	return new_number;
}

//Returns section alignment
uint32_t pe_base::get_section_alignment() const
{
	return props_->get_section_alignment();
}

//Returns image sections list
section_list& pe_base::get_image_sections()
{
	return sections_;
}

//Returns image sections list
const section_list& pe_base::get_image_sections() const
{
	return sections_;
}

//Realigns section by index
void pe_base::realign_section(uint32_t index)
{
	//Check index
	if(sections_.size() <= index)
		throw pe_exception("Section not found", pe_exception::section_not_found);

	//Get section iterator
	section_list::iterator it = sections_.begin() + index;
	section& s = *it;

	//Calculate, how many null bytes we have in the end of raw section data
	std::size_t strip = 0;
	for(std::size_t i = (*it).get_raw_data().length(); i >= 1; --i)
	{
		if(s.get_raw_data()[i - 1] == 0)
			strip++;
		else
			break;
	}

	if(it == sections_.end() - 1) //If we're realigning the last section
	{
		//We can strip ending null bytes
		s.set_size_of_raw_data(static_cast<uint32_t>(s.get_raw_data().length() - strip));
		s.get_raw_data().resize(s.get_raw_data().length() - strip, 0);
	}
	else
	{
		//Else just set size of raw data
		uint32_t raw_size_aligned = s.get_aligned_raw_size(get_file_alignment());
		s.set_size_of_raw_data(raw_size_aligned);
		s.get_raw_data().resize(raw_size_aligned, 0);
	}
}

//Returns file alignment
uint32_t pe_base::get_file_alignment() const
{
	return props_->get_file_alignment();
}

//Sets file alignment
void pe_base::set_file_alignment(uint32_t alignment)
{
	//Check alignment
	if(alignment < minimum_file_alignment)
		throw pe_exception("File alignment can't be less than 512", pe_exception::incorrect_file_alignment);

	if(!pe_utils::is_power_of_2(alignment))
		throw pe_exception("File alignment must be a power of 2", pe_exception::incorrect_file_alignment);

	if(alignment > get_section_alignment())
		throw pe_exception("File alignment must be <= section alignment", pe_exception::incorrect_file_alignment);

	//Set file alignment without any additional checks
	set_file_alignment_unchecked(alignment);
}

//Returns size of image
uint32_t pe_base::get_size_of_image() const
{
	return props_->get_size_of_image();
}

//Returns image entry point
uint32_t pe_base::get_ep() const
{
	return props_->get_ep();
}

//Sets image entry point (just a value of PE header)
void pe_base::set_ep(uint32_t new_ep)
{
	props_->set_ep(new_ep);
}

//Returns number of RVA and sizes (number of DATA_DIRECTORY entries)
uint32_t pe_base::get_number_of_rvas_and_sizes() const
{
	return props_->get_number_of_rvas_and_sizes();
}

//Sets number of RVA and sizes (number of DATA_DIRECTORY entries)
void pe_base::set_number_of_rvas_and_sizes(uint32_t number)
{
	props_->set_number_of_rvas_and_sizes(number);
}

//Returns PE characteristics
uint16_t pe_base::get_characteristics() const
{
	return props_->get_characteristics();
}

//Sets PE characteristics (a value inside header)
void pe_base::set_characteristics(uint16_t ch)
{
	props_->set_characteristics(ch);
}

//Returns section from RVA
section& pe_base::section_from_rva(uint32_t rva)
{
	//Search for section
	for(section_list::iterator i = sections_.begin(); i != sections_.end(); ++i)
	{
		section& s = *i;
		//Return section if found
		if(rva >= s.get_virtual_address() && rva < s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()))
			return s;
	}

	throw pe_exception("No section found by presented address", pe_exception::no_section_found);
}

//Returns section from RVA
const section& pe_base::section_from_rva(uint32_t rva) const
{
	//Search for section
	for(section_list::const_iterator i = sections_.begin(); i != sections_.end(); ++i)
	{
		const section& s = *i;
		//Return section if found
		if(rva >= s.get_virtual_address() && rva < s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()))
			return s;
	}

	throw pe_exception("No section found by presented address", pe_exception::no_section_found);
}

//Returns section from directory ID
section& pe_base::section_from_directory(uint32_t directory_id)
{
	return section_from_rva(get_directory_rva(directory_id));		
}

//Returns section from directory ID
const section& pe_base::section_from_directory(uint32_t directory_id) const
{
	return section_from_rva(get_directory_rva(directory_id));	
}

//Sets section virtual size (actual for the last one of this PE or for unbound section)
void pe_base::set_section_virtual_size(section& s, uint32_t vsize)
{
	//Check if we're changing virtual size of the last section
	//Of course, we can change virtual size of section that's not bound to this PE file
	if(sections_.empty() || std::find_if(sections_.begin(), sections_.end() - 1, section_ptr_finder(s)) != sections_.end() - 1)
		throw pe_exception("Can't change virtual size of any section, except last one", pe_exception::error_changing_section_virtual_size);

	//If we're setting virtual size to zero
	if(vsize == 0)
	{
		//Check if section is empty
		if(s.empty())
			throw pe_exception("Cannot set virtual size of empty section to zero", pe_exception::error_changing_section_virtual_size);

		//Set virtual size equal to aligned size of raw data
		s.set_virtual_size(s.get_size_of_raw_data());
	}
	else
	{
		s.set_virtual_size(vsize);
	}

	//Update image size if we're changing virtual size for the last section of this PE
	if(!sections_.empty() || &s == &(*(sections_.end() - 1)))
		update_image_size();
}

//Expands section raw or virtual size to hold data from specified RVA with specified size
//Section must be free (not bound to any image)
//or the last section of this image
bool pe_base::expand_section(section& s, uint32_t needed_rva, uint32_t needed_size, section_expand_type expand)
{
	//Check if we're changing the last section
	//Of course, we can change the section that's not bound to this PE file
	if(sections_.empty() || std::find_if(sections_.begin(), sections_.end() - 1, section_ptr_finder(s)) != sections_.end() - 1)
		throw pe_exception("Can't expand any section, except last one", pe_exception::error_expanding_section);

	//Check if we should expand our section
	if(expand == expand_section_raw && section_data_length_from_rva(s, needed_rva, section_data_raw) < needed_size)
	{
		//Expand section raw data
		s.get_raw_data().resize(needed_rva - s.get_virtual_address() + needed_size);
		recalculate_section_sizes(s, false);
		return true;
	}
	else if(expand == expand_section_virtual && section_data_length_from_rva(s, needed_rva, section_data_virtual) < needed_size)
	{
		//Expand section virtual data
		set_section_virtual_size(s, needed_rva - s.get_virtual_address() + needed_size);
		return true;
	}
	
	return false;
}

//Updates image virtual size
void pe_base::update_image_size()
{
	//Write virtual size of image to headers
	if(!sections_.empty())
		set_size_of_image(sections_.back().get_virtual_address() + sections_.back().get_aligned_virtual_size(get_section_alignment()));
	else
		set_size_of_image(get_size_of_headers());
}

//Returns checksum of PE file from header
uint32_t pe_base::get_checksum() const
{
	return props_->get_checksum();
}

//Sets checksum of PE file
void pe_base::set_checksum(uint32_t checksum)
{
	props_->set_checksum(checksum);
}

//Returns timestamp of PE file from header
uint32_t pe_base::get_time_date_stamp() const
{
	return props_->get_time_date_stamp();
}

//Sets timestamp of PE file
void pe_base::set_time_date_stamp(uint32_t timestamp)
{
	props_->set_time_date_stamp(timestamp);
}

//Returns Machine field value of PE file from header
uint16_t pe_base::get_machine() const
{
	return props_->get_machine();
}

//Sets Machine field value of PE file
void pe_base::set_machine(uint16_t machine)
{
	props_->set_machine(machine);
}

//Prepares section before attaching it
void pe_base::prepare_section(section& s)
{
	//Calculate its size of raw data
	s.set_size_of_raw_data(static_cast<uint32_t>(pe_utils::align_up(s.get_raw_data().length(), get_file_alignment())));

	//Check section virtual and raw size
	if(!s.get_size_of_raw_data() && !s.get_virtual_size())
		throw pe_exception("Virtual and Physical sizes of section can't be 0 at the same time", pe_exception::zero_section_sizes);

	//If section virtual size is zero
	if(!s.get_virtual_size())
	{
		s.set_virtual_size(s.get_size_of_raw_data());
	}
	else
	{
		//Else calculate its virtual size
		s.set_virtual_size(
			std::max<uint32_t>(pe_utils::align_up(s.get_size_of_raw_data(), get_file_alignment()),
			pe_utils::align_up(s.get_virtual_size(), get_section_alignment())));
	}
}

//Adds section to image
section& pe_base::add_section(section s)
{
	if(sections_.size() >= maximum_number_of_sections)
		throw pe_exception("Maximum number of sections has been reached", pe_exception::no_more_sections_can_be_added);

	//Prepare section before adding it
	prepare_section(s);

	//Calculate section virtual address
	if(!sections_.empty())
	{
		s.set_virtual_address(pe_utils::align_up(sections_.back().get_virtual_address() + sections_.back().get_aligned_virtual_size(get_section_alignment()), get_section_alignment()));

		//We should align last section raw size, if it wasn't aligned
		section& last = sections_.back();
		last.set_size_of_raw_data(static_cast<uint32_t>(pe_utils::align_up(last.get_raw_data().length(), get_file_alignment())));
	}
	else
	{
		s.set_virtual_address(
			s.get_virtual_address() == 0
			? pe_utils::align_up(get_size_of_headers(), get_section_alignment())
			: pe_utils::align_up(s.get_virtual_address(), get_section_alignment()));
	}

	//Add section to the end of section list
	sections_.push_back(s);
	//Set number of sections in PE header
	set_number_of_sections(static_cast<uint16_t>(sections_.size()));
	//Recalculate virtual size of image
	set_size_of_image(get_size_of_image() + s.get_aligned_virtual_size(get_section_alignment()));
	//Return last section
	return sections_.back();
}

//Returns true if sectios "s" is already attached to this PE file
bool pe_base::section_attached(const section& s) const
{
	return sections_.end() != std::find_if(sections_.begin(), sections_.end(), section_ptr_finder(s));
}

//Returns true if directory exists
bool pe_base::directory_exists(uint32_t id) const
{
	return props_->directory_exists(id);
}

//Removes directory
void pe_base::remove_directory(uint32_t id)
{
	props_->remove_directory(id);
}

//Returns directory RVA
uint32_t pe_base::get_directory_rva(uint32_t id) const
{
	return props_->get_directory_rva(id);
}

//Returns directory size
uint32_t pe_base::get_directory_size(uint32_t id) const
{
	return props_->get_directory_size(id);
}

//Sets directory RVA (just a value of PE header, no moving occurs)
void pe_base::set_directory_rva(uint32_t id, uint32_t rva)
{
	return props_->set_directory_rva(id, rva);
}

//Sets directory size (just a value of PE header, no moving occurs)
void pe_base::set_directory_size(uint32_t id, uint32_t size)
{
	return props_->set_directory_size(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
uint32_t pe_base::strip_data_directories(uint32_t min_count, bool strip_iat_directory)
{
	return props_->strip_data_directories(min_count, strip_iat_directory);
}

//Returns true if image has import directory
bool pe_base::has_imports() const
{
	return directory_exists(image_directory_entry_import);
}

//Returns true if image has export directory
bool pe_base::has_exports() const
{
	return directory_exists(image_directory_entry_export);
}

//Returns true if image has resource directory
bool pe_base::has_resources() const
{
	return directory_exists(image_directory_entry_resource);
}

//Returns true if image has security directory
bool pe_base::has_security() const
{
	return directory_exists(image_directory_entry_security);
}

//Returns true if image has relocations
bool pe_base::has_reloc() const
{
	return directory_exists(image_directory_entry_basereloc) && !(get_characteristics() & image_file_relocs_stripped);
}

//Returns true if image has TLS directory
bool pe_base::has_tls() const
{
	return directory_exists(image_directory_entry_tls);
}

//Returns true if image has config directory
bool pe_base::has_config() const
{
	return directory_exists(image_directory_entry_load_config);
}

//Returns true if image has bound import directory
bool pe_base::has_bound_import() const
{
	return directory_exists(image_directory_entry_bound_import);
}

//Returns true if image has delay import directory
bool pe_base::has_delay_import() const
{
	return directory_exists(image_directory_entry_delay_import);
}

//Returns true if image has COM directory
bool pe_base::is_dotnet() const
{
	return directory_exists(image_directory_entry_com_descriptor);
}

//Returns true if image has exception directory
bool pe_base::has_exception_directory() const
{
	return directory_exists(image_directory_entry_exception);
}

//Returns true if image has debug directory
bool pe_base::has_debug() const
{
	return directory_exists(image_directory_entry_debug);
}

//Returns corresponding section data pointer from RVA inside section "s" (checks bounds)
char* pe_base::section_data_from_rva(section& s, uint32_t rva)
{
	//Check if RVA is inside section "s"
	if(rva >= s.get_virtual_address() && rva < s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()))
	{
		if(s.get_raw_data().empty())
			throw pe_exception("Section raw data is empty and cannot be changed", pe_exception::section_is_empty);

		return &s.get_raw_data()[rva - s.get_virtual_address()];
	}

	throw pe_exception("RVA not found inside section", pe_exception::rva_not_exists);
}

//Returns corresponding section data pointer from RVA inside section "s" (checks bounds)
const char* pe_base::section_data_from_rva(const section& s, uint32_t rva, section_data_type datatype) const
{
	//Check if RVA is inside section "s"
	if(rva >= s.get_virtual_address() && rva < s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()))
		return (datatype == section_data_raw ? s.get_raw_data().data() : s.get_virtual_data(get_section_alignment()).c_str()) + rva - s.get_virtual_address();

	throw pe_exception("RVA not found inside section", pe_exception::rva_not_exists);
}

//Returns section TOTAL RAW/VIRTUAL data length from RVA inside section
uint32_t pe_base::section_data_length_from_rva(uint32_t rva, section_data_type datatype, bool include_headers) const
{
	//if RVA is inside of headers and we're searching them too...
	if(include_headers && rva < full_headers_data_.length())
		return static_cast<unsigned long>(full_headers_data_.length());

	const section& s = section_from_rva(rva);
	return static_cast<unsigned long>(datatype == section_data_raw ? s.get_raw_data().length() /* instead of SizeOfRawData */ : s.get_aligned_virtual_size(get_section_alignment()));
}

//Returns section TOTAL RAW/VIRTUAL data length from VA inside section for PE32
uint32_t pe_base::section_data_length_from_va(uint32_t va, section_data_type datatype, bool include_headers) const
{
	return section_data_length_from_rva(va_to_rva(va), datatype, include_headers);
}

//Returns section TOTAL RAW/VIRTUAL data length from VA inside section for PE32/PE64
uint32_t pe_base::section_data_length_from_va(uint64_t va, section_data_type datatype, bool include_headers) const
{
	return section_data_length_from_rva(va_to_rva(va), datatype, include_headers);
}

//Returns section remaining RAW/VIRTUAL data length from RVA "rva_inside" to the end of section containing RVA "rva"
uint32_t pe_base::section_data_length_from_rva(uint32_t rva, uint32_t rva_inside, section_data_type datatype, bool include_headers) const
{
	//if RVAs are inside of headers and we're searching them too...
	if(include_headers && rva < full_headers_data_.length() && rva_inside < full_headers_data_.length())
		return static_cast<unsigned long>(full_headers_data_.length() - rva_inside);

	const section& s = section_from_rva(rva);
	if(rva_inside < s.get_virtual_address())
		throw pe_exception("RVA not found inside section", pe_exception::rva_not_exists);

	//Calculate remaining length of section data from "rva" address
	long length = static_cast<long>(datatype == section_data_raw ? s.get_raw_data().length() /* instead of SizeOfRawData */ : s.get_aligned_virtual_size(get_section_alignment()))
		+ s.get_virtual_address() - rva_inside;

	if(length < 0)
		return 0;

	return static_cast<unsigned long>(length);
}

//Returns section remaining RAW/VIRTUAL data length from VA "va_inside" to the end of section containing VA "va" for PE32
uint32_t pe_base::section_data_length_from_va(uint32_t va, uint32_t va_inside, section_data_type datatype, bool include_headers) const
{
	return section_data_length_from_rva(va_to_rva(va), va_to_rva(va_inside), datatype, include_headers);
}

//Returns section remaining RAW/VIRTUAL data length from VA "va_inside" to the end of section containing VA "va" for PE32/PE64
uint32_t pe_base::section_data_length_from_va(uint64_t va, uint64_t va_inside, section_data_type datatype, bool include_headers) const
{
	return section_data_length_from_rva(va_to_rva(va), va_to_rva(va_inside), datatype, include_headers);
}

//Returns section remaining RAW/VIRTUAL data length from RVA to the end of section "s" (checks bounds)
uint32_t pe_base::section_data_length_from_rva(const section& s, uint32_t rva_inside, section_data_type datatype) const
{
	//Check rva_inside
	if(rva_inside >= s.get_virtual_address() && rva_inside < s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()))
	{
		//Calculate remaining length of section data from "rva" address
		int32_t length = static_cast<int32_t>(datatype == section_data_raw ? s.get_raw_data().length() /* instead of SizeOfRawData */ : s.get_aligned_virtual_size(get_section_alignment()))
			+ s.get_virtual_address() - rva_inside;

		if(length < 0)
			return 0;

		return static_cast<uint32_t>(length);
	}

	throw pe_exception("RVA not found inside section", pe_exception::rva_not_exists);
}

//Returns section remaining RAW/VIRTUAL data length from VA to the end of section "s" for PE32 (checks bounds)
uint32_t pe_base::section_data_length_from_va(const section& s, uint32_t va_inside, section_data_type datatype) const
{
	return section_data_length_from_rva(s, va_to_rva(va_inside), datatype);
}

//Returns section remaining RAW/VIRTUAL data length from VA to the end of section "s" for PE32/PE64 (checks bounds)
uint32_t pe_base::section_data_length_from_va(const section& s, uint64_t va_inside, section_data_type datatype) const
{
	return section_data_length_from_rva(s, va_to_rva(va_inside), datatype);
}

//Returns corresponding section data pointer from RVA inside section
char* pe_base::section_data_from_rva(uint32_t rva, bool include_headers)
{
	//if RVA is inside of headers and we're searching them too...
	if(include_headers && rva < full_headers_data_.length())
		return &full_headers_data_[rva];

	section& s = section_from_rva(rva);

	if(s.get_raw_data().empty())
		throw pe_exception("Section raw data is empty and cannot be changed", pe_exception::section_is_empty);

	return &s.get_raw_data()[rva - s.get_virtual_address()];
}

//Returns corresponding section data pointer from RVA inside section
const char* pe_base::section_data_from_rva(uint32_t rva, section_data_type datatype, bool include_headers) const
{
	//if RVA is inside of headers and we're searching them too...
	if(include_headers && rva < full_headers_data_.length())
		return &full_headers_data_[rva];

	const section& s = section_from_rva(rva);
	return (datatype == section_data_raw ? s.get_raw_data().data() : s.get_virtual_data(get_section_alignment()).c_str()) + rva - s.get_virtual_address();
}

//Reads DOS headers from istream
void pe_base::read_dos_header(std::istream& file, image_dos_header& header)
{
	//Check istream flags
	if(file.bad() || file.eof())
		throw pe_exception("PE file stream is bad or closed.", pe_exception::bad_pe_file);

	//Read DOS header and check istream
	file.read(reinterpret_cast<char*>(&header), sizeof(image_dos_header));
	if(file.bad() || file.eof())
		throw pe_exception("Unable to read IMAGE_DOS_HEADER", pe_exception::bad_dos_header);

	//Check DOS header magic
	if(header.e_magic != 0x5a4d) //"MZ"
		throw pe_exception("IMAGE_DOS_HEADER signature is incorrect", pe_exception::bad_dos_header);
}

//Reads DOS headers from istream
void pe_base::read_dos_header(std::istream& file)
{
	read_dos_header(file, dos_header_);
}

//Reads PE image from istream
void pe_base::read_pe(std::istream& file, bool read_debug_raw_data)
{
	//Get istream size
	std::streamoff filesize = pe_utils::get_file_size(file);

	//Check if PE header is DWORD-aligned
	if((dos_header_.e_lfanew % sizeof(uint32_t)) != 0)
		throw pe_exception("PE header is not DWORD-aligned", pe_exception::bad_dos_header);

	//Seek to NT headers
	file.seekg(dos_header_.e_lfanew);
	if(file.bad() || file.fail())
		throw pe_exception("Cannot reach IMAGE_NT_HEADERS", pe_exception::image_nt_headers_not_found);

	//Read NT headers
	file.read(get_nt_headers_ptr(), get_sizeof_nt_header() - sizeof(image_data_directory) * image_numberof_directory_entries);
	if(file.bad() || file.eof())
		throw pe_exception("Error reading IMAGE_NT_HEADERS", pe_exception::error_reading_image_nt_headers);

	//Check PE signature
	if(get_pe_signature() != 0x4550) //"PE"
		throw pe_exception("Incorrect PE signature", pe_exception::pe_signature_incorrect);

	//Check number of directories
	if(get_number_of_rvas_and_sizes() > image_numberof_directory_entries)
		set_number_of_rvas_and_sizes(image_numberof_directory_entries);

	if(get_number_of_rvas_and_sizes() > 0)
	{
		//Read data directory headers, if any
		file.read(get_nt_headers_ptr() + (get_sizeof_nt_header() - sizeof(image_data_directory) * image_numberof_directory_entries), sizeof(image_data_directory) * get_number_of_rvas_and_sizes());
		if(file.bad() || file.eof())
			throw pe_exception("Error reading DATA_DIRECTORY headers", pe_exception::error_reading_data_directories);
	}

	//Check section number
	//Images with zero section number accepted
	if(get_number_of_sections() > maximum_number_of_sections)
		throw pe_exception("Incorrect number of sections", pe_exception::section_number_incorrect);

	//Check PE magic
	if(get_magic() != get_needed_magic())
		throw pe_exception("Incorrect PE signature", pe_exception::pe_signature_incorrect);

	//Check section alignment
	if(!pe_utils::is_power_of_2(get_section_alignment()))
		throw pe_exception("Incorrect section alignment", pe_exception::incorrect_section_alignment);

	//Check file alignment
	if(!pe_utils::is_power_of_2(get_file_alignment()))
		throw pe_exception("Incorrect file alignment", pe_exception::incorrect_file_alignment);

	if(get_file_alignment() != get_section_alignment() && (get_file_alignment() < minimum_file_alignment || get_file_alignment() > get_section_alignment()))
		throw pe_exception("Incorrect file alignment", pe_exception::incorrect_file_alignment);

	//Check size of image
	if(pe_utils::align_up(get_size_of_image(), get_section_alignment()) == 0)
		throw pe_exception("Incorrect size of image", pe_exception::incorrect_size_of_image);
	
	//Read rich data overlay / DOS stub (if any)
	if(static_cast<uint32_t>(dos_header_.e_lfanew) > sizeof(image_dos_header))
	{
		rich_overlay_.resize(dos_header_.e_lfanew - sizeof(image_dos_header));
		file.seekg(sizeof(image_dos_header));
		file.read(&rich_overlay_[0], dos_header_.e_lfanew - sizeof(image_dos_header));
		if(file.bad() || file.eof())
			throw pe_exception("Error reading 'Rich' & 'DOS stub' overlay", pe_exception::error_reading_overlay);
	}

	//Calculate first section raw position
	//Sum is safe here
	uint32_t first_section = dos_header_.e_lfanew + get_size_of_optional_header() + sizeof(image_file_header) + sizeof(uint32_t) /* Signature */;

	if(get_number_of_sections() > 0)
	{
		//Go to first section
		file.seekg(first_section);
		if(file.bad() || file.fail())
			throw pe_exception("Cannot reach section headers", pe_exception::image_section_headers_not_found);
	}

	uint32_t last_raw_size = 0;

	//Read all sections
	for(int i = 0; i < get_number_of_sections(); i++)
	{
		section s;
		//Read section header
		file.read(reinterpret_cast<char*>(&s.get_raw_header()), sizeof(image_section_header));
		if(file.bad() || file.eof())
			throw pe_exception("Error reading section header", pe_exception::error_reading_section_header);

		//Save next section header position
		std::streamoff next_sect = file.tellg();

		//Check section virtual and raw sizes
		if(!s.get_size_of_raw_data() && !s.get_virtual_size())
			throw pe_exception("Virtual and Physical sizes of section can't be 0 at the same time", pe_exception::zero_section_sizes);

		//Check for adequate values of section fields
		if(!pe_utils::is_sum_safe(s.get_virtual_address(), s.get_virtual_size()) || s.get_virtual_size() > pe_utils::two_gb
			|| !pe_utils::is_sum_safe(s.get_pointer_to_raw_data(), s.get_size_of_raw_data()) || s.get_size_of_raw_data() > pe_utils::two_gb)
			throw pe_exception("Incorrect section address or size", pe_exception::section_incorrect_addr_or_size);

		if(s.get_size_of_raw_data() != 0)
		{
			//If section has raw data

			//If section raw data size is greater than virtual, fix it
			last_raw_size = s.get_size_of_raw_data();
			if(pe_utils::align_up(s.get_size_of_raw_data(), get_file_alignment()) > pe_utils::align_up(s.get_virtual_size(), get_section_alignment()))
				s.set_size_of_raw_data(s.get_virtual_size());

			//Check virtual and raw section sizes and addresses
			if(s.get_virtual_address() + pe_utils::align_up(s.get_virtual_size(), get_section_alignment()) > pe_utils::align_up(get_size_of_image(), get_section_alignment())
				||
				pe_utils::align_down(s.get_pointer_to_raw_data(), get_file_alignment()) + s.get_size_of_raw_data() > static_cast<uint32_t>(filesize))
				throw pe_exception("Incorrect section address or size", pe_exception::section_incorrect_addr_or_size);

			//Seek to section raw data
			file.seekg(pe_utils::align_down(s.get_pointer_to_raw_data(), get_file_alignment()));
			if(file.bad() || file.fail())
				throw pe_exception("Cannot reach section data", pe_exception::image_section_data_not_found);

			//Read section raw data
			s.get_raw_data().resize(s.get_size_of_raw_data());
			file.read(&s.get_raw_data()[0], s.get_size_of_raw_data());
			if(file.bad() || file.fail())
				throw pe_exception("Error reading section data", pe_exception::image_section_data_not_found);
		}

		//Check virtual address and size of section
		if(s.get_virtual_address() + s.get_aligned_virtual_size(get_section_alignment()) > pe_utils::align_up(get_size_of_image(), get_section_alignment()))
			throw pe_exception("Incorrect section address or size", pe_exception::section_incorrect_addr_or_size);

		//Save section
		sections_.push_back(s);

		//Seek to the next section header
		file.seekg(next_sect);
	}

	//Check size of headers: SizeOfHeaders can't be larger than first section VA
	if(!sections_.empty() && get_size_of_headers() > sections_.front().get_virtual_address())
		throw pe_exception("Incorrect size of headers", pe_exception::incorrect_size_of_headers);

	//If image has more than two sections
	if(sections_.size() >= 2)
	{
		//Check sections virtual sizes
		for(section_list::const_iterator i = sections_.begin() + 1; i != sections_.end(); ++i)
		{
			if((*i).get_virtual_address() != (*(i - 1)).get_virtual_address() + (*(i - 1)).get_aligned_virtual_size(get_section_alignment()))
				throw pe_exception("Section table is incorrect", pe_exception::image_section_table_incorrect);
		}
	}

	//Check if image has overlay in the end of file
	has_overlay_ = !sections_.empty() && filesize > static_cast<std::streamoff>(sections_.back().get_pointer_to_raw_data() + last_raw_size);

	{
		//Additionally, read data from the beginning of istream to size of headers
		file.seekg(0);
		uint32_t size_of_headers = std::min<uint32_t>(get_size_of_headers(), static_cast<uint32_t>(filesize));

		if(!sections_.empty())
		{
			for(section_list::const_iterator i = sections_.begin(); i != sections_.end(); ++i)
			{
				if(!(*i).empty())
				{
					size_of_headers = std::min<uint32_t>(get_size_of_headers(), (*i).get_pointer_to_raw_data());
					break;
				}
			}
		}

		full_headers_data_.resize(size_of_headers);
		file.read(&full_headers_data_[0], size_of_headers);
		if(file.bad() || file.eof())
			throw pe_exception("Error reading file", pe_exception::error_reading_file);
	}

	//Moreover, if there's debug directory, read its raw data for some debug info types
	while(read_debug_raw_data && has_debug())
	{
		try
		{
			//Check the length in bytes of the section containing debug directory
			if(section_data_length_from_rva(get_directory_rva(image_directory_entry_debug), get_directory_rva(image_directory_entry_debug), section_data_virtual, true) < sizeof(image_debug_directory))
				break;

			unsigned long current_pos = get_directory_rva(image_directory_entry_debug);

			//First IMAGE_DEBUG_DIRECTORY table
			image_debug_directory directory = section_data_from_rva<image_debug_directory>(current_pos, section_data_virtual, true);

			//Iterate over all IMAGE_DEBUG_DIRECTORY directories
			while(directory.PointerToRawData
				&& current_pos < get_directory_rva(image_directory_entry_debug) + get_directory_size(image_directory_entry_debug))
			{
				//If we have something to read
				if((directory.Type == image_debug_type_codeview
					|| directory.Type == image_debug_type_misc
					|| directory.Type == image_debug_type_coff)
					&& directory.SizeOfData)
				{
					std::string data;
					data.resize(directory.SizeOfData);
					file.seekg(directory.PointerToRawData);
					file.read(&data[0], directory.SizeOfData);
					if(file.bad() || file.eof())
						throw pe_exception("Error reading file", pe_exception::error_reading_file);

					debug_data_.insert(std::make_pair(directory.PointerToRawData, data));
				}

				//Go to next debug entry
				current_pos += sizeof(image_debug_directory);
				directory = section_data_from_rva<image_debug_directory>(current_pos, section_data_virtual, true);
			}

			break;
		}
		catch(const pe_exception&)
		{
			//Don't throw any exception here, if debug info is corrupted or incorrect
			break;
		}
		catch(const std::bad_alloc&)
		{
			//Don't throw any exception here, if debug info is corrupted or incorrect
			break;
		}
	}
}

//Returns PE type of this image
pe_type pe_base::get_pe_type() const
{
	return props_->get_pe_type();
}

//Returns PE type (PE or PE+) from pe_type enumeration (minimal correctness checks)
pe_type pe_base::get_pe_type(std::istream& file)
{
	//Save state of the istream
	std::ios_base::iostate state = file.exceptions();
	std::streamoff old_offset = file.tellg();
	image_nt_headers32 nt_headers;
	image_dos_header header;

	try
	{
		//Read dos header
		file.exceptions(std::ios::goodbit);
		read_dos_header(file, header);

		//Seek to the NT headers start
		file.seekg(header.e_lfanew);
		if(file.bad() || file.fail())
			throw pe_exception("Cannot reach IMAGE_NT_HEADERS", pe_exception::image_nt_headers_not_found);

		//Read NT headers (we're using 32-bit version, because there's no significant differencies between 32 and 64 bit version structures)
		file.read(reinterpret_cast<char*>(&nt_headers), sizeof(image_nt_headers32) - sizeof(image_data_directory) * image_numberof_directory_entries);
		if(file.bad() || file.eof())
			throw pe_exception("Error reading IMAGE_NT_HEADERS", pe_exception::error_reading_image_nt_headers);

		//Check NT headers signature
		if(nt_headers.Signature != 0x4550) //"PE"
			throw pe_exception("Incorrect PE signature", pe_exception::pe_signature_incorrect);

		//Check NT headers magic
		if(nt_headers.OptionalHeader.Magic != image_nt_optional_hdr32_magic && nt_headers.OptionalHeader.Magic != image_nt_optional_hdr64_magic)
			throw pe_exception("Incorrect PE signature", pe_exception::pe_signature_incorrect);
	}
	catch(const std::exception&)
	{
		//If something went wrong, restore istream state
		file.exceptions(state);
		file.seekg(old_offset);
		file.clear();
		//Retrhow exception
		throw;
	}

	//Restore stream state
	file.exceptions(state);
	file.seekg(old_offset);
	file.clear();

	//Determine PE type and return it
	return nt_headers.OptionalHeader.Magic == image_nt_optional_hdr64_magic ? pe_type_64 : pe_type_32;
}

//Returns true if image has overlay data at the end of file
bool pe_base::has_overlay() const
{
	return has_overlay_;
}

//Clears PE characteristics flag
void pe_base::clear_characteristics_flags(uint16_t flags)
{
	set_characteristics(get_characteristics() & ~flags);
}

//Sets PE characteristics flag
void pe_base::set_characteristics_flags(uint16_t flags)
{
	set_characteristics(get_characteristics() | flags);
}

//Returns true if PE characteristics flag set
bool pe_base::check_characteristics_flag(uint16_t flag) const
{
	return (get_characteristics() & flag) ? true : false;
}

//Returns subsystem value
uint16_t pe_base::get_subsystem() const
{
	return props_->get_subsystem();
}

//Sets subsystem value
void pe_base::set_subsystem(uint16_t subsystem)
{
	props_->set_subsystem(subsystem);
}

//Returns true if image has console subsystem
bool pe_base::is_console() const
{
	return get_subsystem() == image_subsystem_windows_cui;
}

//Returns true if image has Windows GUI subsystem
bool pe_base::is_gui() const
{
	return get_subsystem() == image_subsystem_windows_gui;
}

//Sets required operation system version
void pe_base::set_os_version(uint16_t major, uint16_t minor)
{
	props_->set_os_version(major, minor);
}

//Returns required operation system version (minor word)
uint16_t pe_base::get_minor_os_version() const
{
	return props_->get_minor_os_version();
}

//Returns required operation system version (major word)
uint16_t pe_base::get_major_os_version() const
{
	return props_->get_major_os_version();
}

//Sets required subsystem version
void pe_base::set_subsystem_version(uint16_t major, uint16_t minor)
{
	props_->set_subsystem_version(major, minor);
}

//Returns required subsystem version (minor word)
uint16_t pe_base::get_minor_subsystem_version() const
{
	return props_->get_minor_subsystem_version();
}

//Returns required subsystem version (major word)
uint16_t pe_base::get_major_subsystem_version() const
{
	return props_->get_major_subsystem_version();
}

//Returns corresponding section data pointer from VA inside section "s" for PE32 (checks bounds)
char* pe_base::section_data_from_va(section& s, uint32_t va) //Always returns raw data
{
	return section_data_from_rva(s, va_to_rva(va));
}

//Returns corresponding section data pointer from VA inside section "s" for PE32 (checks bounds)
const char* pe_base::section_data_from_va(const section& s, uint32_t va, section_data_type datatype) const
{
	return section_data_from_rva(s, va_to_rva(va), datatype);
}

//Returns corresponding section data pointer from VA inside section for PE32
char* pe_base::section_data_from_va(uint32_t va, bool include_headers) //Always returns raw data
{
	return section_data_from_rva(va_to_rva(va), include_headers);
}

//Returns corresponding section data pointer from VA inside section for PE32
const char* pe_base::section_data_from_va(uint32_t va, section_data_type datatype, bool include_headers) const
{
	return section_data_from_rva(va_to_rva(va), datatype, include_headers);
}

//Returns corresponding section data pointer from VA inside section "s" for PE32/PE64 (checks bounds)
char* pe_base::section_data_from_va(section& s, uint64_t va)  //Always returns raw data
{
	return section_data_from_rva(s, va_to_rva(va));
}

//Returns corresponding section data pointer from VA inside section "s" for PE32/PE64 (checks bounds)
const char* pe_base::section_data_from_va(const section& s, uint64_t va, section_data_type datatype) const
{
	return section_data_from_rva(s, va_to_rva(va), datatype);
}

//Returns corresponding section data pointer from VA inside section for PE32/PE64
char* pe_base::section_data_from_va(uint64_t va, bool include_headers)  //Always returns raw data
{
	return section_data_from_rva(va_to_rva(va), include_headers);
}

//Returns corresponding section data pointer from VA inside section for PE32/PE64
const char* pe_base::section_data_from_va(uint64_t va, section_data_type datatype, bool include_headers) const
{
	return section_data_from_rva(va_to_rva(va), datatype, include_headers);
}

//Returns section from VA inside it for PE32
section& pe_base::section_from_va(uint32_t va)
{
	return section_from_rva(va_to_rva(va));
}

//Returns section from VA inside it for PE32/PE64
section& pe_base::section_from_va(uint64_t va)
{
	return section_from_rva(va_to_rva(va));
}

//Returns section from RVA inside it for PE32
const section& pe_base::section_from_va(uint32_t va) const
{
	return section_from_rva(va_to_rva(va));
}

//Returns section from RVA inside it for PE32/PE64
const section& pe_base::section_from_va(uint64_t va) const
{
	return section_from_rva(va_to_rva(va));
}

uint32_t pe_base::va_to_rva(uint32_t va, bool bound_check) const
{
	return props_->va_to_rva(va, bound_check);
}

uint32_t pe_base::va_to_rva(uint64_t va, bool bound_check) const
{
	return props_->va_to_rva(va, bound_check);
}

uint32_t pe_base::rva_to_va_32(uint32_t rva) const
{
	return props_->rva_to_va_32(rva);
}

uint64_t pe_base::rva_to_va_64(uint32_t rva) const
{
	return props_->rva_to_va_64(rva);
}

//Relative Virtual Address (RVA) to Virtual Address (VA) convertion for PE32
void pe_base::rva_to_va(uint32_t rva, uint32_t& va) const
{
	va = rva_to_va_32(rva);
}

//Relative Virtual Address (RVA) to Virtual Address (VA) convertions for PE32/PE64
void pe_base::rva_to_va(uint32_t rva, uint64_t& va) const
{
	va = rva_to_va_64(rva);
}

//Returns section from file offset (4gb max)
section& pe_base::section_from_file_offset(uint32_t offset)
{
	return *file_offset_to_section(offset);
}

//Returns section from file offset (4gb max)
const section& pe_base::section_from_file_offset(uint32_t offset) const
{
	return *file_offset_to_section(offset);
}

//Returns section and offset (raw data only) from its start from RVA
const std::pair<uint32_t, const section*> pe_base::section_and_offset_from_rva(uint32_t rva) const
{
	const section& s = section_from_rva(rva);
	return std::make_pair(rva - s.get_virtual_address(), &s);
}

//Returns DLL Characteristics
uint16_t pe_base::get_dll_characteristics() const
{
	return props_->get_dll_characteristics();
}

//Sets DLL Characteristics
void pe_base::set_dll_characteristics(uint16_t characteristics)
{
	props_->set_dll_characteristics(characteristics);
}

//Returns size of headers
uint32_t pe_base::get_size_of_headers() const
{
	return props_->get_size_of_headers();
}

//Returns size of optional header
uint16_t pe_base::get_size_of_optional_header() const
{
	return props_->get_size_of_optional_header();
}

//Returns PE signature
uint32_t pe_base::get_pe_signature() const
{
	return props_->get_pe_signature();
}

//Returns magic value
uint32_t pe_base::get_magic() const
{
	return props_->get_magic();
}

//Returns image base for PE32
void pe_base::get_image_base(uint32_t& base) const
{
	base = get_image_base_32();
}

//Returns image base for PE32 and PE64 respectively
uint32_t pe_base::get_image_base_32() const
{
	return props_->get_image_base_32();
}

//Sets image base for PE32 and PE64 respectively
uint64_t pe_base::get_image_base_64() const
{
	return props_->get_image_base_64();
}

//RVA to RAW file offset convertion (4gb max)
uint32_t pe_base::rva_to_file_offset(uint32_t rva) const
{
	//Maybe, RVA is inside PE headers
	if(rva < get_size_of_headers())
		return rva;

	const section& s = section_from_rva(rva);
	return s.get_pointer_to_raw_data() + rva - s.get_virtual_address();
}

//RAW file offset to RVA convertion (4gb max)
uint32_t pe_base::file_offset_to_rva(uint32_t offset) const
{
	//Maybe, offset is inside PE headers
	if(offset < get_size_of_headers())
		return offset;

	const section_list::const_iterator it = file_offset_to_section(offset);
	return offset - (*it).get_pointer_to_raw_data() + (*it).get_virtual_address();
}

//RAW file offset to section convertion helper (4gb max)
section_list::const_iterator pe_base::file_offset_to_section(uint32_t offset) const
{
	section_list::const_iterator it = std::find_if(sections_.begin(), sections_.end(), section_by_raw_offset(offset));
	if(it == sections_.end())
		throw pe_exception("No section found by presented file offset", pe_exception::no_section_found);

	return it;
}

//RAW file offset to section convertion helper (4gb max)
section_list::iterator pe_base::file_offset_to_section(uint32_t offset)
{
	section_list::iterator it = std::find_if(sections_.begin(), sections_.end(), section_by_raw_offset(offset));
	if(it == sections_.end())
		throw pe_exception("No section found by presented file offset", pe_exception::no_section_found);
	
	return it;
}

//RVA from section raw data offset
uint32_t pe_base::rva_from_section_offset(const section& s, uint32_t raw_offset_from_section_start)
{
	return s.get_virtual_address() + raw_offset_from_section_start;
}

//Returns image base for PE32/PE64
void pe_base::get_image_base(uint64_t& base) const
{
	base = get_image_base_64();
}

//Sets new image base
void pe_base::set_image_base(uint32_t base)
{
	props_->set_image_base(base);
}

void pe_base::set_image_base_64(uint64_t base)
{
	props_->set_image_base_64(base);
}

//Sets heap size commit for PE32 and PE64 respectively
void pe_base::set_heap_size_commit(uint32_t size)
{
	props_->set_heap_size_commit(size);
}

void pe_base::set_heap_size_commit(uint64_t size)
{
	props_->set_heap_size_commit(size);
}

//Sets heap size reserve for PE32 and PE64 respectively
void pe_base::set_heap_size_reserve(uint32_t size)
{
	props_->set_heap_size_reserve(size);
}

void pe_base::set_heap_size_reserve(uint64_t size)
{
	props_->set_heap_size_reserve(size);
}

//Sets stack size commit for PE32 and PE64 respectively
void pe_base::set_stack_size_commit(uint32_t size)
{
	props_->set_stack_size_commit(size);
}

void pe_base::set_stack_size_commit(uint64_t size)
{
	props_->set_stack_size_commit(size);
}

//Sets stack size reserve for PE32 and PE64 respectively
void pe_base::set_stack_size_reserve(uint32_t size)
{
	props_->set_stack_size_reserve(size);
}

void pe_base::set_stack_size_reserve(uint64_t size)
{
	props_->set_stack_size_reserve(size);
}

//Returns heap size commit for PE32 and PE64 respectively
uint32_t pe_base::get_heap_size_commit_32() const
{
	return props_->get_heap_size_commit_32();
}

uint64_t pe_base::get_heap_size_commit_64() const
{
	return props_->get_heap_size_commit_64();
}

//Returns heap size reserve for PE32 and PE64 respectively
uint32_t pe_base::get_heap_size_reserve_32() const
{
	return props_->get_heap_size_reserve_32();
}

uint64_t pe_base::get_heap_size_reserve_64() const
{
	return props_->get_heap_size_reserve_64();
}

//Returns stack size commit for PE32 and PE64 respectively
uint32_t pe_base::get_stack_size_commit_32() const
{
	return props_->get_stack_size_commit_32();
}

uint64_t pe_base::get_stack_size_commit_64() const
{
	return props_->get_stack_size_commit_64();
}

//Returns stack size reserve for PE32 and PE64 respectively
uint32_t pe_base::get_stack_size_reserve_32() const
{
	return props_->get_stack_size_reserve_32();
}

uint64_t pe_base::get_stack_size_reserve_64() const
{
	return props_->get_stack_size_reserve_64();
}

//Returns heap size commit for PE32
void pe_base::get_heap_size_commit(uint32_t& size) const
{
	size = get_heap_size_commit_32();
}

//Returns heap size commit for PE32/PE64
void pe_base::get_heap_size_commit(uint64_t& size) const
{
	size = get_heap_size_commit_64();
}

//Returns heap size reserve for PE32
void pe_base::get_heap_size_reserve(uint32_t& size) const
{
	size = get_heap_size_reserve_32();
}

//Returns heap size reserve for PE32/PE64
void pe_base::get_heap_size_reserve(uint64_t& size) const
{
	size = get_heap_size_reserve_64();
}

//Returns stack size commit for PE32
void pe_base::get_stack_size_commit(uint32_t& size) const
{
	size = get_stack_size_commit_32();
}

//Returns stack size commit for PE32/PE64
void pe_base::get_stack_size_commit(uint64_t& size) const
{
	size = get_stack_size_commit_64();
}

//Returns stack size reserve for PE32
void pe_base::get_stack_size_reserve(uint32_t& size) const
{
	size = get_stack_size_reserve_32();
}

//Returns stack size reserve for PE32/PE64
void pe_base::get_stack_size_reserve(uint64_t& size) const
{
	size = get_stack_size_reserve_64();
}

//Realigns file (changes file alignment)
void pe_base::realign_file(uint32_t new_file_alignment)
{
	//Checks alignment for correctness
	set_file_alignment(new_file_alignment);
	realign_all_sections();
}

//Helper function to recalculate RAW and virtual section sizes and strip it, if necessary
void pe_base::recalculate_section_sizes(section& s, bool auto_strip)
{
	prepare_section(s); //Recalculate section raw addresses

	//Strip RAW size of section, if it is the last one
	//For all others it must be file-aligned and calculated by prepare_section() call
	if(auto_strip && !(sections_.empty() || &s == &*(sections_.end() - 1)))
	{
		//Strip ending raw data nullbytes to optimize size
		std::string& raw_data = s.get_raw_data();
		if(!raw_data.empty())
		{
			std::string::size_type i = raw_data.length();
			for(; i != 1; --i)
			{
				if(raw_data[i - 1] != 0)
					break;
			}
			
			raw_data.resize(i);
		}

		s.set_size_of_raw_data(static_cast<uint32_t>(raw_data.length()));
	}

	//Can occur only for last section
	if(pe_utils::align_up(s.get_virtual_size(), get_section_alignment()) < pe_utils::align_up(s.get_size_of_raw_data(), get_file_alignment()))
		set_section_virtual_size(s, pe_utils::align_up(s.get_size_of_raw_data(), get_section_alignment())); //Recalculate section virtual size
}

//Returns data from the beginning of image
//Size = SizeOfHeaders
const std::string& pe_base::get_full_headers_data() const
{
	return full_headers_data_;
}

const pe_base::debug_data_list& pe_base::get_raw_debug_data_list() const
{
	return debug_data_;
}

//Sets number of sections
void pe_base::set_number_of_sections(uint16_t number)
{
	props_->set_number_of_sections(number);
}

//Sets size of image
void pe_base::set_size_of_image(uint32_t size)
{
	props_->set_size_of_image(size);
}

//Sets size of headers
void pe_base::set_size_of_headers(uint32_t size)
{
	props_->set_size_of_headers(size);
}

//Sets size of optional headers
void pe_base::set_size_of_optional_header(uint16_t size)
{
	props_->set_size_of_optional_header(size);
}

//Returns nt headers data pointer
char* pe_base::get_nt_headers_ptr()
{
	return props_->get_nt_headers_ptr();
}

//Returns nt headers data pointer
const char* pe_base::get_nt_headers_ptr() const
{
	return props_->get_nt_headers_ptr();
}

//Returns sizeof() nt headers
uint32_t pe_base::get_sizeof_nt_header() const
{
	return props_->get_sizeof_nt_header();
}

//Returns sizeof() optional headers
uint32_t pe_base::get_sizeof_opt_headers() const
{
	return props_->get_sizeof_opt_headers();
}

//Sets file alignment (no checks)
void pe_base::set_file_alignment_unchecked(uint32_t alignment)
{
	props_->set_file_alignment_unchecked(alignment);
}

//Sets base of code
void pe_base::set_base_of_code(uint32_t base)
{
	props_->set_base_of_code(base);
}

//Returns base of code
uint32_t pe_base::get_base_of_code() const
{
	return props_->get_base_of_code();
}

//Returns needed magic of image
uint32_t pe_base::get_needed_magic() const
{
	return props_->get_needed_magic();
}
}