summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/pe_resources.h
blob: 1eb643756371894b6defd09b986028f175aceebb (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
/*************************************************************************/
/* 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 <vector>
#include <string>
#include <set>
#include "pe_structures.h"
#include "pe_base.h"
#include "pe_directory.h"

namespace pe_bliss
{
//Class representing resource data entry
class resource_data_entry
{
public:
	//Default constructor
	resource_data_entry();
	//Constructor from data
	resource_data_entry(const std::string& data, uint32_t codepage);

	//Returns resource data codepage
	uint32_t get_codepage() const;
	//Returns resource data
	const std::string& get_data() const;
		
public: //These functions do not change everything inside image, they are used by PE class
	//You can also use them to rebuild resource directory
		
	//Sets resource data codepage
	void set_codepage(uint32_t codepage);
	//Sets resource data
	void set_data(const std::string& data);

private:
	uint32_t codepage_; //Resource data codepage
	std::string data_; //Resource data
};

//Forward declaration
class resource_directory;

//Class representing resource directory entry
class resource_directory_entry
{
public:
	//Default constructor
	resource_directory_entry();
	//Copy constructor
	resource_directory_entry(const resource_directory_entry& other);
	//Copy assignment operator
	resource_directory_entry& operator=(const resource_directory_entry& other);

	//Returns entry ID
	uint32_t get_id() const;
	//Returns entry name
	const std::wstring& get_name() const;
	//Returns true, if entry has name
	//Returns false, if entry has ID
	bool is_named() const;

	//Returns true, if entry includes resource_data_entry
	//Returns false, if entry includes resource_directory
	bool includes_data() const;
	//Returns resource_directory if entry includes it, otherwise throws an exception
	const resource_directory& get_resource_directory() const;
	//Returns resource_data_entry if entry includes it, otherwise throws an exception
	const resource_data_entry& get_data_entry() const;

	//Destructor
	~resource_directory_entry();

public: //These functions do not change everything inside image, they are used by PE class
	//You can also use them to rebuild resource directory

	//Sets entry name
	void set_name(const std::wstring& name);
	//Sets entry ID
	void set_id(uint32_t id);
		
	//Returns resource_directory if entry includes it, otherwise throws an exception
	resource_directory& get_resource_directory();
	//Returns resource_data_entry if entry includes it, otherwise throws an exception
	resource_data_entry& get_data_entry();

	//Adds resource_data_entry
	void add_data_entry(const resource_data_entry& entry);
	//Adds resource_directory
	void add_resource_directory(const resource_directory& dir);

private:
	//Destroys included data
	void release();

private:
	uint32_t id_;
	std::wstring name_;

	union includes
	{
		//Default constructor
		includes();

		//We use pointers, we're doing manual copying here
		class resource_data_entry* data_;
		class resource_directory* dir_; //We use pointer, because structs include each other
	};

	includes ptr_;

	bool includes_data_, named_;
};

//Class representing resource directory
class resource_directory
{
public:
	typedef std::vector<resource_directory_entry> entry_list;

public:
	//Default constructor
	resource_directory();
	//Constructor from data
	explicit resource_directory(const pe_win::image_resource_directory& dir);

	//Returns characteristics of directory
	uint32_t get_characteristics() const;
	//Returns date and time stamp of directory
	uint32_t get_timestamp() const;
	//Returns number of named entries
	uint32_t get_number_of_named_entries() const;
	//Returns number of ID entries
	uint32_t get_number_of_id_entries() const;
	//Returns major version of directory
	uint16_t get_major_version() const;
	//Returns minor version of directory
	uint16_t get_minor_version() const;
	//Returns resource_directory_entry array
	const entry_list& get_entry_list() const;
	//Returns resource_directory_entry by ID. If not found - throws an exception
	const resource_directory_entry& entry_by_id(uint32_t id) const;
	//Returns resource_directory_entry by name. If not found - throws an exception
	const resource_directory_entry& entry_by_name(const std::wstring& name) const;

public: //These functions do not change everything inside image, they are used by PE class
	//You can also use them to rebuild resource directory

	//Adds resource_directory_entry
	void add_resource_directory_entry(const resource_directory_entry& entry);
	//Clears resource_directory_entry array
	void clear_resource_directory_entry_list();

	//Sets characteristics of directory
	void set_characteristics(uint32_t characteristics);
	//Sets date and time stamp of directory
	void set_timestamp(uint32_t timestamp);
	//Sets number of named entries
	void set_number_of_named_entries(uint32_t number);
	//Sets number of ID entries
	void set_number_of_id_entries(uint32_t number);
	//Sets major version of directory
	void set_major_version(uint16_t major_version);
	//Sets minor version of directory
	void get_minor_version(uint16_t minor_version);
		
	//Returns resource_directory_entry array
	entry_list& get_entry_list();

private:
	uint32_t characteristics_;
	uint32_t timestamp_;
	uint16_t major_version_, minor_version_;
	uint32_t number_of_named_entries_, number_of_id_entries_;
	entry_list entries_;

public: //Finder helpers
	//Finds resource_directory_entry by ID
	struct id_entry_finder
	{
	public:
		explicit id_entry_finder(uint32_t id);
		bool operator()(const resource_directory_entry& entry) const;

	private:
		uint32_t id_;
	};

	//Finds resource_directory_entry by name
	struct name_entry_finder
	{
	public:
		explicit name_entry_finder(const std::wstring& name);
		bool operator()(const resource_directory_entry& entry) const;

	private:
		std::wstring name_;
	};

	//Finds resource_directory_entry by name or ID (universal)
	struct entry_finder
	{
	public:
		explicit entry_finder(const std::wstring& name);
		explicit entry_finder(uint32_t id);
		bool operator()(const resource_directory_entry& entry) const;

	private:
		std::wstring name_;
		uint32_t id_;
		bool named_;
	};
};

//Returns resources (root resource_directory) from PE file
const resource_directory get_resources(const pe_base& pe);

//Resources rebuilder
//resource_directory - root resource directory
//resources_section - section where resource directory will be placed (must be attached to PE image)
//resource_directory is non-constant, because it will be sorted
//offset_from_section_start - offset from resources_section raw data start
//save_to_pe_headers - if true, new resource directory information will be saved to PE image headers
//auto_strip_last_section - if true and resources are placed in the last section, it will be automatically stripped
//number_of_id_entries and number_of_named_entries for resource directories are recalculated and not used
const image_directory rebuild_resources(pe_base& pe, resource_directory& info, section& resources_section, uint32_t offset_from_section_start = 0, bool save_to_pe_header = true, bool auto_strip_last_section = true);
}