summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/pe_resource_manager.cpp
blob: 0ee7840ff074e6ef42d53cf11244ca24f3e5d766 (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
/*************************************************************************/
/* 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 <algorithm>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <string.h>
#include "pe_resource_manager.h"
#include "resource_internal.h"

namespace pe_bliss
{
using namespace pe_win;

//Constructor from root resource directory
pe_resource_manager::pe_resource_manager(resource_directory& root_directory)
	:pe_resource_viewer(root_directory), root_dir_edit_(root_directory)
{}

resource_directory& pe_resource_manager::get_root_directory()
{
	return root_dir_edit_;
}

//Removes all resources of given type or root name
//If there's more than one directory entry of a given type, only the
//first one will be deleted (that's an unusual situation)
//Returns true if resource was deleted
bool pe_resource_manager::remove_resource_type(resource_type type)
{
	//Search for resource type
	resource_directory::entry_list& entries = root_dir_edit_.get_entry_list();
	resource_directory::entry_list::iterator it = std::find_if(entries.begin(), entries.end(), resource_directory::id_entry_finder(type));
	if(it != entries.end())
	{
		//Remove it, if found
		entries.erase(it);
		return true;
	}

	return false;
}

bool pe_resource_manager::remove_resource(const std::wstring& root_name)
{
	//Search for resource type
	resource_directory::entry_list& entries = root_dir_edit_.get_entry_list();
	resource_directory::entry_list::iterator it = std::find_if(entries.begin(), entries.end(), resource_directory::name_entry_finder(root_name));
	if(it != entries.end())
	{
		//Remove it, if found
		entries.erase(it);
		return true;
	}

	return false;
}

//Helper to remove resource
bool pe_resource_manager::remove_resource(const resource_directory::entry_finder& root_finder, const resource_directory::entry_finder& finder)
{
	//Search for resource type
	resource_directory::entry_list& entries_type = root_dir_edit_.get_entry_list();
	resource_directory::entry_list::iterator it_type = std::find_if(entries_type.begin(), entries_type.end(), root_finder);
	if(it_type != entries_type.end())
	{
		//Search for resource name/ID with "finder"
		resource_directory::entry_list& entries_name = (*it_type).get_resource_directory().get_entry_list();
		resource_directory::entry_list::iterator it_name = std::find_if(entries_name.begin(), entries_name.end(), finder);
		if(it_name != entries_name.end())
		{
			//Erase resource, if found
			entries_name.erase(it_name);
			if(entries_name.empty())
				entries_type.erase(it_type);

			return true;
		}
	}

	return false;
}

//Removes all resource languages by resource type/root name and name
//Deletes only one entry of given type and name
//Returns true if resource was deleted
bool pe_resource_manager::remove_resource(resource_type type, const std::wstring& name)
{
	return remove_resource(resource_directory::entry_finder(type), resource_directory::entry_finder(name));
}

bool pe_resource_manager::remove_resource(const std::wstring& root_name, const std::wstring& name)
{
	return remove_resource(resource_directory::entry_finder(root_name), resource_directory::entry_finder(name));
}

//Removes all resource languages by resource type/root name and ID
//Deletes only one entry of given type and ID
//Returns true if resource was deleted
bool pe_resource_manager::remove_resource(resource_type type, uint32_t id)
{
	return remove_resource(resource_directory::entry_finder(type), resource_directory::entry_finder(id));
}

bool pe_resource_manager::remove_resource(const std::wstring& root_name, uint32_t id)
{
	return remove_resource(resource_directory::entry_finder(root_name), resource_directory::entry_finder(id));
}

//Helper to remove resource
bool pe_resource_manager::remove_resource(const resource_directory::entry_finder& root_finder, const resource_directory::entry_finder& finder, uint32_t language)
{
	//Search for resource type
	resource_directory::entry_list& entries_type = root_dir_edit_.get_entry_list();
	resource_directory::entry_list::iterator it_type = std::find_if(entries_type.begin(), entries_type.end(), root_finder);
	if(it_type != entries_type.end())
	{
		//Search for resource name/ID with "finder"
		resource_directory::entry_list& entries_name = (*it_type).get_resource_directory().get_entry_list();
		resource_directory::entry_list::iterator it_name = std::find_if(entries_name.begin(), entries_name.end(), finder);
		if(it_name != entries_name.end())
		{
			//Search for resource language
			resource_directory::entry_list& entries_lang = (*it_name).get_resource_directory().get_entry_list();
			resource_directory::entry_list::iterator it_lang = std::find_if(entries_lang.begin(), entries_lang.end(), resource_directory::id_entry_finder(language));
			if(it_lang != entries_lang.end())
			{
				//Erase resource, if found
				entries_lang.erase(it_lang);
				if(entries_lang.empty())
				{
					entries_name.erase(it_name);
					if(entries_name.empty())
						entries_type.erase(it_type);
				}

				return true;
			}
		}
	}

	return false;
}

//Removes resource language by resource type/root name and name
//Deletes only one entry of given type, name and language
//Returns true if resource was deleted
bool pe_resource_manager::remove_resource(resource_type type, const std::wstring& name, uint32_t language)
{
	return remove_resource(resource_directory::entry_finder(type), resource_directory::entry_finder(name), language);
}

bool pe_resource_manager::remove_resource(const std::wstring& root_name, const std::wstring& name, uint32_t language)
{
	return remove_resource(resource_directory::entry_finder(root_name), resource_directory::entry_finder(name), language);
}

//Removes recource language by resource type/root name and ID
//Deletes only one entry of given type, ID and language
//Returns true if resource was deleted
bool pe_resource_manager::remove_resource(resource_type type, uint32_t id, uint32_t language)
{
	return remove_resource(resource_directory::entry_finder(type), resource_directory::entry_finder(id), language);
}

bool pe_resource_manager::remove_resource(const std::wstring& root_name, uint32_t id, uint32_t language)
{
	return remove_resource(resource_directory::entry_finder(root_name), resource_directory::entry_finder(id), language);
}

//Helper to add/replace resource
void pe_resource_manager::add_resource(const std::string& data, resource_type type, resource_directory_entry& new_entry, const resource_directory::entry_finder& finder, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_type_entry;
	new_type_entry.set_id(type);

	add_resource(data, new_type_entry, resource_directory::entry_finder(type), new_entry, finder, language, codepage, timestamp);
}

//Helper to add/replace resource
void pe_resource_manager::add_resource(const std::string& data, const std::wstring& root_name, resource_directory_entry& new_entry, const resource_directory::entry_finder& finder, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_type_entry;
	new_type_entry.set_name(root_name);
	
	add_resource(data, new_type_entry, resource_directory::entry_finder(root_name), new_entry, finder, language, codepage, timestamp);
}

//Helper to add/replace resource
void pe_resource_manager::add_resource(const std::string& data, resource_directory_entry& new_root_entry, const resource_directory::entry_finder& root_finder, resource_directory_entry& new_entry, const resource_directory::entry_finder& finder, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	//Search for resource type
	resource_directory::entry_list* entries = &root_dir_edit_.get_entry_list();
	resource_directory::entry_list::iterator it = std::find_if(entries->begin(), entries->end(), root_finder);
	if(it == entries->end())
	{
		//Add resource type directory, if it was not found
		resource_directory dir;
		dir.set_timestamp(timestamp);
		new_root_entry.add_resource_directory(dir);
		entries->push_back(new_root_entry);
		it = entries->end() - 1;
	}

	//Search for resource name/ID directory with "finder"
	entries = &(*it).get_resource_directory().get_entry_list();
	it = std::find_if(entries->begin(), entries->end(), finder);
	if(it == entries->end())
	{
		//Add resource name/ID directory, if it was not found
		resource_directory dir;
		dir.set_timestamp(timestamp);
		new_entry.add_resource_directory(dir);
		entries->push_back(new_entry);
		it = entries->end() - 1;
	}

	//Search for data resource entry by language
	entries = &(*it).get_resource_directory().get_entry_list();
	it = std::find_if(entries->begin(), entries->end(), resource_directory::id_entry_finder(language));
	if(it != entries->end())
		entries->erase(it); //Erase it, if found

	//Add new data entry
	resource_directory_entry new_dir_data_entry;
	resource_data_entry data_dir(data, codepage);
	new_dir_data_entry.add_data_entry(data_dir);
	new_dir_data_entry.set_id(language);
	entries->push_back(new_dir_data_entry);
}

//Adds resource. If resource already exists, replaces it
void pe_resource_manager::add_resource(const std::string& data, resource_type type, const std::wstring& name, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_entry;
	new_entry.set_name(name);

	add_resource(data, type, new_entry, resource_directory::entry_finder(name), language, codepage, timestamp);
}

//Adds resource. If resource already exists, replaces it
void pe_resource_manager::add_resource(const std::string& data, const std::wstring& root_name, const std::wstring& name, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_entry;
	new_entry.set_name(name);

	add_resource(data, root_name, new_entry, resource_directory::entry_finder(name), language, codepage, timestamp);
}

//Adds resource. If resource already exists, replaces it
void pe_resource_manager::add_resource(const std::string& data, resource_type type, uint32_t id, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_entry;
	new_entry.set_id(id);

	add_resource(data, type, new_entry, resource_directory::entry_finder(id), language, codepage, timestamp);
}

//Adds resource. If resource already exists, replaces it
void pe_resource_manager::add_resource(const std::string& data, const std::wstring& root_name, uint32_t id, uint32_t language, uint32_t codepage, uint32_t timestamp)
{
	resource_directory_entry new_entry;
	new_entry.set_id(id);

	add_resource(data, root_name, new_entry, resource_directory::entry_finder(id), language, codepage, timestamp);
}
}