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
|
#include "envmodule.h"
#include "env.h"
#include <utility.h>
#include <log.h>
namespace env
{
using namespace MOBase;
Module::Module(QString path, std::size_t fileSize)
: m_path(std::move(path)), m_fileSize(fileSize)
{
const auto fi = getFileInfo();
m_version = getVersion(fi.ffi);
m_timestamp = getTimestamp(fi.ffi);
m_versionString = fi.fileDescription;
m_md5 = getMD5();
}
const QString& Module::path() const
{
return m_path;
}
QString Module::displayPath() const
{
return QDir::fromNativeSeparators(m_path.toLower());
}
std::size_t Module::fileSize() const
{
return m_fileSize;
}
const QString& Module::version() const
{
return m_version;
}
const QString& Module::versionString() const
{
return m_versionString;
}
const QDateTime& Module::timestamp() const
{
return m_timestamp;
}
const QString& Module::md5() const
{
return m_md5;
}
QString Module::timestampString() const
{
if (!m_timestamp.isValid()) {
return "(no timestamp)";
}
return m_timestamp.toString(Qt::DateFormat::ISODate);
}
QString Module::toString() const
{
QStringList sl;
// file size
sl.push_back(displayPath());
sl.push_back(QString("%1 B").arg(m_fileSize));
// version
if (m_version.isEmpty() && m_versionString.isEmpty()) {
sl.push_back("(no version)");
} else {
if (!m_version.isEmpty()) {
sl.push_back(m_version);
}
if (!m_versionString.isEmpty() && m_versionString != m_version) {
sl.push_back(versionString());
}
}
// timestamp
if (m_timestamp.isValid()) {
sl.push_back(m_timestamp.toString(Qt::DateFormat::ISODate));
} else {
sl.push_back("(no timestamp)");
}
// md5
if (!m_md5.isEmpty()) {
sl.push_back(m_md5);
}
return sl.join(", ");
}
Module::FileInfo Module::getFileInfo() const
{
const auto wspath = m_path.toStdWString();
// getting version info size
DWORD dummy = 0;
const DWORD size = GetFileVersionInfoSizeW(wspath.c_str(), &dummy);
if (size == 0) {
const auto e = GetLastError();
if (e == ERROR_RESOURCE_TYPE_NOT_FOUND) {
// not an error, no version information built into that module
return {};
}
log::error(
"GetFileVersionInfoSizeW() failed on '{}', {}",
m_path, formatSystemMessage(e));
return {};
}
// getting version info
auto buffer = std::make_unique<std::byte[]>(size);
if (!GetFileVersionInfoW(wspath.c_str(), 0, size, buffer.get())) {
const auto e = GetLastError();
log::error(
"GetFileVersionInfoW() failed on '{}', {}",
m_path, formatSystemMessage(e));
return {};
}
// the version info has two major parts: a fixed version and a localizable
// set of strings
FileInfo fi;
fi.ffi = getFixedFileInfo(buffer.get());
fi.fileDescription = getFileDescription(buffer.get());
return fi;
}
VS_FIXEDFILEINFO Module::getFixedFileInfo(std::byte* buffer) const
{
void* valuePointer = nullptr;
unsigned int valueSize = 0;
// the fixed version info is in the root
const auto ret = VerQueryValueW(buffer, L"\\", &valuePointer, &valueSize);
if (!ret || !valuePointer || valueSize == 0) {
// not an error, no fixed file info
return {};
}
const auto* fi = static_cast<VS_FIXEDFILEINFO*>(valuePointer);
// signature is always 0xfeef04bd
if (fi->dwSignature != 0xfeef04bd) {
log::error(
"bad file info signature {:#x} for '{}'",
fi->dwSignature, m_path);
return {};
}
return *fi;
}
QString Module::getFileDescription(std::byte* buffer) const
{
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
};
void* valuePointer = nullptr;
unsigned int valueSize = 0;
// getting list of available languages
auto ret = VerQueryValueW(
buffer, L"\\VarFileInfo\\Translation", &valuePointer, &valueSize);
if (!ret || !valuePointer || valueSize == 0) {
log::error("VerQueryValueW() for translations failed on '{}'", m_path);
return {};
}
// number of languages
const auto count = valueSize / sizeof(LANGANDCODEPAGE);
if (count == 0) {
return {};
}
// using the first language in the list to get FileVersion
const auto* lcp = static_cast<LANGANDCODEPAGE*>(valuePointer);
const auto subBlock = QString("\\StringFileInfo\\%1%2\\FileVersion")
.arg(lcp->wLanguage, 4, 16, QChar('0'))
.arg(lcp->wCodePage, 4, 16, QChar('0'));
ret = VerQueryValueW(
buffer, subBlock.toStdWString().c_str(), &valuePointer, &valueSize);
if (!ret || !valuePointer || valueSize == 0) {
// not an error, no file version
return {};
}
// valueSize includes the null terminator
return QString::fromWCharArray(
static_cast<wchar_t*>(valuePointer), valueSize - 1);
}
QString Module::getVersion(const VS_FIXEDFILEINFO& fi) const
{
if (fi.dwSignature == 0) {
return {};
}
const DWORD major = (fi.dwFileVersionMS >> 16 ) & 0xffff;
const DWORD minor = (fi.dwFileVersionMS >> 0 ) & 0xffff;
const DWORD maintenance = (fi.dwFileVersionLS >> 16 ) & 0xffff;
const DWORD build = (fi.dwFileVersionLS >> 0 ) & 0xffff;
if (major == 0 && minor == 0 && maintenance == 0 && build == 0) {
return {};
}
return QString("%1.%2.%3.%4")
.arg(major).arg(minor).arg(maintenance).arg(build);
}
QDateTime Module::getTimestamp(const VS_FIXEDFILEINFO& fi) const
{
FILETIME ft = {};
if (fi.dwSignature == 0 || (fi.dwFileDateMS == 0 && fi.dwFileDateLS == 0)) {
// if the file info is invalid or doesn't have a date, use the creation
// time on the file
// opening the file
HandlePtr h(CreateFileW(
m_path.toStdWString().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
if (h.get() == INVALID_HANDLE_VALUE) {
const auto e = GetLastError();
log::error(
"can't open file '{}' for timestamp, {}",
m_path, formatSystemMessage(e));
return {};
}
// getting the file time
if (!GetFileTime(h.get(), &ft, nullptr, nullptr)) {
const auto e = GetLastError();
log::error(
"can't get file time for '{}', {}",
m_path, formatSystemMessage(e));
return {};
}
} else {
// use the time from the file info
ft.dwHighDateTime = fi.dwFileDateMS;
ft.dwLowDateTime = fi.dwFileDateLS;
}
// converting to SYSTEMTIME
SYSTEMTIME utc = {};
if (!FileTimeToSystemTime(&ft, &utc)) {
log::error(
"FileTimeToSystemTime() failed on timestamp high={:#x} low={:#x} for '{}'",
ft.dwHighDateTime, ft.dwLowDateTime, m_path);
return {};
}
return QDateTime(
QDate(utc.wYear, utc.wMonth, utc.wDay),
QTime(utc.wHour, utc.wMinute, utc.wSecond, utc.wMilliseconds));
}
QString Module::getMD5() const
{
if (m_path.contains("\\windows\\", Qt::CaseInsensitive)) {
// don't calculate md5 for system files, it's not really relevant and
// it takes a while
return {};
}
// opening the file
QFile f(m_path);
if (!f.open(QFile::ReadOnly)) {
log::error("failed to open file '{}' for md5", m_path);
return {};
}
// hashing
QCryptographicHash hash(QCryptographicHash::Md5);
if (!hash.addData(&f)) {
log::error("failed to calculate md5 for '{}'", m_path);
return {};
}
return hash.result().toHex();
}
std::vector<Module> getLoadedModules()
{
HandlePtr snapshot(CreateToolhelp32Snapshot(
TH32CS_SNAPMODULE32 | TH32CS_SNAPMODULE, GetCurrentProcessId()));
if (snapshot.get() == INVALID_HANDLE_VALUE)
{
const auto e = GetLastError();
log::error("CreateToolhelp32Snapshot() failed, {}", formatSystemMessage(e));
return {};
}
MODULEENTRY32 me = {};
me.dwSize = sizeof(me);
// first module, this shouldn't fail because there's at least the executable
if (!Module32First(snapshot.get(), &me))
{
const auto e = GetLastError();
log::error("Module32First() failed, {}", formatSystemMessage(e));
return {};
}
std::vector<Module> v;
for (;;)
{
const auto path = QString::fromWCharArray(me.szExePath);
if (!path.isEmpty()) {
v.push_back(Module(path, me.modBaseSize));
}
// next module
if (!Module32Next(snapshot.get(), &me)) {
const auto e = GetLastError();
// no more modules is not an error
if (e != ERROR_NO_MORE_FILES) {
log::error("Module32Next() failed, {}", formatSystemMessage(e));
}
break;
}
}
// sorting by display name
std::sort(v.begin(), v.end(), [](auto&& a, auto&& b) {
return (a.displayPath().compare(b.displayPath(), Qt::CaseInsensitive) < 0);
});
return v;
}
} // namespace
|