summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/pe_section.h
blob: 617ecebe26fb99b340338fed53e1b44786e204c5 (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
/*************************************************************************/
/* 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.                */
/*************************************************************************/
#pragma once
#include <string>
#include <vector>
#include "pe_structures.h"

namespace pe_bliss
{
//Enumeration of section data types, used in functions below
enum section_data_type
{
	section_data_raw,
	section_data_virtual
};

//Class representing image section
class section
{
public:
	//Default constructor
	section();

	//Sets the name of section (stripped to 8 characters)
	void set_name(const std::string& name);

	//Returns the name of section
	const std::string get_name() const;

	//Changes attributes of section
	section& readable(bool readable);
	section& writeable(bool writeable);
	section& executable(bool executable);
	section& shared(bool shared);
	section& discardable(bool discardable);

	//Returns attributes of section
	bool readable() const;
	bool writeable() const;
	bool executable() const;
	bool shared() const;
	bool discardable() const;

	//Returns true if section has no RAW data
	bool empty() const;

	//Returns raw section data from file image
	std::string& get_raw_data();
	//Returns raw section data from file image
	const std::string& get_raw_data() const;
	//Returns mapped virtual section data
	const std::string& get_virtual_data(uint32_t section_alignment) const;
	//Returns mapped virtual section data
	std::string& get_virtual_data(uint32_t section_alignment);

public: //Header getters
	//Returns section virtual size
	uint32_t get_virtual_size() const;
	//Returns section virtual address (RVA)
	uint32_t get_virtual_address() const;
	//Returns size of section raw data
	uint32_t get_size_of_raw_data() const;
	//Returns pointer to raw section data in PE file
	uint32_t get_pointer_to_raw_data() const;
	//Returns section characteristics
	uint32_t get_characteristics() const;

	//Returns raw image section header
	const pe_win::image_section_header& get_raw_header() const;

public: //Aligned sizes calculation
	//Calculates aligned virtual section size
	uint32_t get_aligned_virtual_size(uint32_t section_alignment) const;
	//Calculates aligned raw section size
	uint32_t get_aligned_raw_size(uint32_t file_alignment) const;

public: //Setters
	//Sets size of raw section data
	void set_size_of_raw_data(uint32_t size_of_raw_data);
	//Sets pointer to section raw data
	void set_pointer_to_raw_data(uint32_t pointer_to_raw_data);
	//Sets section characteristics
	void set_characteristics(uint32_t characteristics);
	//Sets raw section data from file image
	void set_raw_data(const std::string& data);

public: //Setters, be careful
	//Sets section virtual size (doesn't set internal aligned virtual size, changes only header value)
	//Better use pe_base::set_section_virtual_size
	void set_virtual_size(uint32_t virtual_size);
	//Sets section virtual address
	void set_virtual_address(uint32_t virtual_address);
	//Returns raw image section header
	pe_win::image_section_header& get_raw_header();

private:
	//Section header
	pe_win::image_section_header header_;

	//Maps virtual section data
	void map_virtual(uint32_t section_alignment) const;

	//Unmaps virtual section data
	void unmap_virtual() const;

	//Set flag (attribute) of section
	section& set_flag(uint32_t flag, bool setflag);

	//Old size of section (stored after mapping of virtual section memory)
	mutable std::size_t old_size_;

	//Section raw/virtual data
	mutable std::string raw_data_;
};

//Section by file offset finder helper (4gb max)
struct section_by_raw_offset
{
public:
	explicit section_by_raw_offset(uint32_t offset);
	bool operator()(const section& s) const;

private:
	uint32_t offset_;
};

//Helper: finder of section* in sections list
struct section_ptr_finder
{
public:
	explicit section_ptr_finder(const section& s);
	bool operator()(const section& s) const;

private:
	const section& s_;
};

typedef std::vector<section> section_list;
}