summaryrefslogtreecommitdiff
path: root/tools/pe_bliss/version_info_viewer.cpp
blob: 6e2d0d5c5b5e35a6e5ad8e3fb069349f18fc5c54 (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
/*************************************************************************/
/* 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 <iomanip>
#include <sstream>
#include "pe_exception.h"
#include "version_info_viewer.h"

namespace pe_bliss
{
//Default process language, UNICODE
const std::wstring version_info_viewer::default_language_translation(L"040904b0");

//Default constructor
//strings - version info strings with charsets
//translations - version info translations map
version_info_viewer::version_info_viewer(const lang_string_values_map& strings, const translation_values_map& translations)
	:strings_(strings), translations_(translations)
{}

//Below functions have parameter translation
//If it's empty, the default language translation will be taken
//If there's no default language translation, the first one will be taken

//Returns company name
const std::wstring version_info_viewer::get_company_name(const std::wstring& translation) const
{
	return get_property(L"CompanyName", translation);
}

//Returns file description
const std::wstring version_info_viewer::get_file_description(const std::wstring& translation) const
{
	return get_property(L"FileDescription", translation);
}

//Returns file version
const std::wstring version_info_viewer::get_file_version(const std::wstring& translation) const
{
	return get_property(L"FileVersion", translation);
}

//Returns internal file name
const std::wstring version_info_viewer::get_internal_name(const std::wstring& translation) const
{
	return get_property(L"InternalName", translation);
}

//Returns legal copyright
const std::wstring version_info_viewer::get_legal_copyright(const std::wstring& translation) const
{
	return get_property(L"LegalCopyright", translation);
}

//Returns original file name
const std::wstring version_info_viewer::get_original_filename(const std::wstring& translation) const
{
	return get_property(L"OriginalFilename", translation);
}

//Returns product name
const std::wstring version_info_viewer::get_product_name(const std::wstring& translation) const
{
	return get_property(L"ProductName", translation);
}

//Returns product version
const std::wstring version_info_viewer::get_product_version(const std::wstring& translation) const
{
	return get_property(L"ProductVersion", translation);
}

//Returns list of translations in string representation
const version_info_viewer::translation_list version_info_viewer::get_translation_list() const
{
	translation_list ret;

	//Enumerate all translations
	for(translation_values_map::const_iterator it = translations_.begin(); it != translations_.end(); ++it)
	{
		//Create string representation of translation value
		std::wstringstream ss;
		ss << std::hex
			<< std::setw(4) << std::setfill(L'0') << (*it).first
			<< std::setw(4) << std::setfill(L'0') <<  (*it).second;

		//Save it
		ret.push_back(ss.str());
	}

	return ret;
}

//Returns version info property value
//property_name - required property name
//If throw_if_absent = true, will throw exception if property does not exist
//If throw_if_absent = false, will return empty string if property does not exist
const std::wstring version_info_viewer::get_property(const std::wstring& property_name, const std::wstring& translation, bool throw_if_absent) const
{
	std::wstring ret;

	//If there're no strings
	if(strings_.empty())
	{
		if(throw_if_absent)
			throw pe_exception("Version info string does not exist", pe_exception::version_info_string_does_not_exist);

		return ret;
	}
	
	lang_string_values_map::const_iterator it = strings_.begin();

	if(translation.empty())
	{
		//If no translation was specified
		it = strings_.find(default_language_translation); //Find default translation table
		if(it == strings_.end()) //If there's no default translation table, take the first one
			it = strings_.begin();
	}
	else
	{
		it = strings_.find(translation); //Find specified translation table
		if(it == strings_.end())
		{
			if(throw_if_absent)
				throw pe_exception("Version info string does not exist", pe_exception::version_info_string_does_not_exist);

			return ret;
		}
	}
	
	//Find value of the required property
	string_values_map::const_iterator str_it = (*it).second.find(property_name);

	if(str_it == (*it).second.end())
	{
		if(throw_if_absent)
			throw pe_exception("Version info string does not exist", pe_exception::version_info_string_does_not_exist);

		return ret;
	}

	ret = (*str_it).second;

	return ret;
}

//Converts translation HEX-string to pair of language ID and codepage ID
const version_info_viewer::translation_pair version_info_viewer::translation_from_string(const std::wstring& translation)
{
	uint32_t translation_id = 0;

	{
		//Convert string to DWORD
		std::wstringstream ss;
		ss << std::hex << translation;
		ss >> translation_id;
	}

	return std::make_pair(static_cast<uint16_t>(translation_id >> 16), static_cast<uint16_t>(translation_id & 0xFFFF));
}
}