forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.cpp
More file actions
506 lines (440 loc) · 12.4 KB
/
version.cpp
File metadata and controls
506 lines (440 loc) · 12.4 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// SPDX-License-Identifier: GPL-2.0-or-later
#include "config.h"
#ifdef EXV_USE_CURL
#include <curl/curl.h>
#endif
#include "futils.hpp"
#include "makernote_int.hpp"
#include "version.hpp"
// Adobe XMP Toolkit
#ifdef EXV_HAVE_XMP_TOOLKIT
#include "xmp_exiv2.hpp"
#endif
// + standard includes
#include <array>
#include <fstream>
#include <set>
// #1147
#ifndef _WIN32
#include <sys/types.h>
#include <unistd.h>
#endif
#ifndef _MAX_PATH
#define _MAX_PATH 512
#endif
// platform specific support for getLoadedLibraries
#if defined(_WIN32) || defined(__CYGWIN__)
// clang-format off
#include <winapifamily.h>
#include <windows.h>
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_APP)
#include <psapi.h>
#endif
// clang-format on
#if __LP64__
#ifdef _WIN64
#undef _WIN64
#endif
#define _WIN64 1
#endif
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#elif defined(__FreeBSD__)
// clang-format off
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <libprocstat.h>
// clang-format on
#elif defined(__sun__)
#include <dlfcn.h>
#include <link.h>
#endif
namespace Exiv2 {
uint32_t versionNumber() {
return EXIV2_MAKE_VERSION(EXIV2_MAJOR_VERSION, EXIV2_MINOR_VERSION, EXIV2_PATCH_VERSION);
}
std::string versionString() {
std::ostringstream os;
os << EXIV2_MAJOR_VERSION << '.' << EXIV2_MINOR_VERSION << '.' << EXIV2_PATCH_VERSION;
return os.str();
}
std::string versionNumberHexString() {
std::ostringstream os;
os << std::hex << std::setw(6) << std::setfill('0') << Exiv2::versionNumber();
return os.str();
}
const char* version() {
return EXV_PACKAGE_VERSION;
}
bool testVersion(uint32_t major, uint32_t minor, uint32_t patch) {
return versionNumber() >= EXIV2_MAKE_VERSION(major, minor, patch);
}
} // namespace Exiv2
static bool shouldOutput(const std::vector<std::regex>& greps, const char* key, const std::string& value) {
bool bPrint = greps.empty();
for (auto const& g : greps) {
bPrint = std::regex_search(key, g) || std::regex_search(value, g);
if (bPrint) {
break;
}
}
return bPrint;
}
static void output(std::ostream& os, const std::vector<std::regex>& greps, const char* name, const std::string& value) {
if (shouldOutput(greps, name, value))
os << name << "=" << value << '\n';
}
static void output(std::ostream& os, const std::vector<std::regex>& greps, const char* name, int value) {
std::ostringstream stringStream;
stringStream << value;
output(os, greps, name, stringStream.str());
}
static bool pushPath(const std::string& path, std::vector<std::string>& libs, std::set<std::string>& paths) {
bool result = Exiv2::fileExists(path) && paths.find(path) == paths.end() && path != "/";
if (result) {
paths.insert(path);
libs.push_back(path);
}
return result;
}
static std::vector<std::string> getLoadedLibraries() {
std::vector<std::string> libs;
std::set<std::string> paths;
std::string path;
#if defined(_WIN32) || defined(__CYGWIN__)
// enumerate loaded libraries and determine path to executable (unsupported on UWP)
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_APP)
HMODULE handles[200];
DWORD cbNeeded;
if (EnumProcessModules(GetCurrentProcess(), handles, static_cast<DWORD>(std::size(handles)), &cbNeeded)) {
char szFilename[_MAX_PATH];
for (DWORD h = 0; h < cbNeeded / sizeof(handles[0]); h++) {
GetModuleFileNameA(handles[h], szFilename, static_cast<DWORD>(std::size(szFilename)));
pushPath(szFilename, libs, paths);
}
}
#endif
#elif defined(__APPLE__)
// man 3 dyld
uint32_t count = _dyld_image_count();
for (uint32_t image = 0; image < count; image++) {
std::string path(_dyld_get_image_name(image));
pushPath(path, libs, paths);
}
#elif defined(__FreeBSD__)
{
unsigned int n;
struct procstat* procstat = procstat_open_sysctl();
struct kinfo_proc* procs = procstat ? procstat_getprocs(procstat, KERN_PROC_PID, getpid(), &n) : nullptr;
struct filestat_list* files = procs ? procstat_getfiles(procstat, procs, true) : nullptr;
if (files) {
filestat* entry;
STAILQ_FOREACH(entry, files, next) {
if (entry && PS_FST_TYPE_VNODE == entry->fs_type && entry->fs_path) {
std::string path(entry->fs_path);
pushPath(path, libs, paths);
}
}
}
// free resources
if (files)
procstat_freefiles(procstat, files);
if (procs)
procstat_freeprocs(procstat, procs);
if (procstat)
procstat_close(procstat);
}
#elif defined(__sun__) || defined(__unix__)
// http://stackoverflow.com/questions/606041/how-do-i-get-the-path-of-a-process-in-unix-linux
char procsz[100];
char pathsz[500];
snprintf(procsz, sizeof(procsz), "/proc/%d/path/a.out", getpid());
if (auto l = readlink(procsz, pathsz, sizeof(pathsz) - 1); l > 0) {
pathsz[l] = '\0';
path.assign(pathsz);
libs.push_back(path);
}
// read file /proc/self/maps which has a list of files in memory
// (this doesn't yield anything on __sun__)
std::ifstream maps("/proc/self/maps");
std::string string;
while (std::getline(maps, string)) {
std::size_t pos = string.find_last_of(' ');
if (pos != std::string::npos) {
path = string.substr(pos + 1);
pushPath(path, libs, paths);
}
}
#endif
if (libs.empty())
libs.emplace_back("unknown");
return libs;
}
void Exiv2::dumpLibraryInfo(std::ostream& os, const std::vector<std::regex>& keys) {
int bits = 8 * sizeof(void*);
#ifdef NDEBUG
int debug = 0;
#else
int debug = 1;
#endif
#ifdef exiv2lib_EXPORTS
int dll = 1;
#else
int dll = 0;
#endif
const char* compiler =
#if defined(_MSC_VER)
"MSVC";
#ifndef __VERSION__
char version[40];
snprintf(version, sizeof(version), "%d.%02d", (_MSC_VER - 600) / 100, _MSC_VER % 100);
// add edition in brackets
// 7.10 = 2003 8.00 = 2005 etc 12.00 = 2013 13.00 = 2015 (yet the installer labels it as 14.0!)
size_t edition = (_MSC_VER - 600) / 100;
constexpr auto editions = std::array{
"0", "1", "2", "3", "4", "5", "6", "2003", "2005", "2008", "2010", "2012", "2013", "2015", "2017", "2019",
};
if (edition == 13 && _MSC_VER >= 1910)
edition++; // 2017 _MSC_VAR == 1910
if (edition == 14 && _MSC_VER >= 1920)
edition++; // 2019 _MSC_VAR == 1920
if (edition > editions.size())
edition = 0;
if (edition) {
const size_t len = ::strlen(version);
snprintf(version + len, sizeof(version) - len, " (%s/%s)", editions[edition], bits == 64 ? "x64" : "x86");
}
#define __VERSION__ version
#endif
#elif defined(__clang__)
"Clang";
#elif defined(__GNUG__)
"G++";
#elif defined(__GNUC__)
"GCC";
#elif defined(__SUNPRO_CC)
"CC (oracle)";
#elif defined(__SUNPRO_C)
"cc (oracle)";
#elif defined(__sun__)
"cc (solaris)";
#else
"unknown";
#endif
#ifndef __VERSION__
#ifdef __clang__version__
#define __VERSION__ __clang__version__
#else
#define __VERSION__ "unknown"
#endif
#endif
const char* platform =
#if defined(__MSYS__)
"msys";
#elif defined(__CYGWIN__)
"cygwin";
#elif defined(_MSC_VER)
"windows";
#elif defined(__APPLE__)
"apple";
#elif defined(__MINGW64__)
"mingw64";
#elif defined(__MINGW32__)
"mingw32";
#elif defined(__sun__)
"solaris";
#elif defined(__NetBSD__)
"netbsd";
#elif defined(__FreeBSD__)
"freebsd";
#elif defined(__linux__)
"linux";
#else
"unknown";
#endif
int have_inttypes = 0;
int have_libintl = 0;
int have_lensdata = 0;
int have_iconv = 0;
int have_memory = 0;
int have_stdbool = 0;
int have_stdint = 0;
int have_stdlib = 0;
int have_strlib = 0;
int have_strerror_r = 0;
int have_strings_h = 0;
int have_mmap = 0;
int have_munmap = 0;
int have_sys_stat = 0;
int have_unistd_h = 0;
int have_sys_mman = 0;
int have_libz = 0;
int have_brotli = 0;
int have_xmptoolkit = 0;
int adobe_xmpsdk = 0;
int have_bool = 0;
int have_strings = 0;
int have_sys_types = 0;
int have_unistd = 0;
int enable_bmff = 0;
int enable_webready = 0;
int enable_nls = 0;
int enable_video = 0;
int use_curl = 0;
#if __has_include(<inttypes.h>)
have_inttypes = 1;
#endif
#ifdef EXV_HAVE_LENSDATA
have_lensdata = 1;
#endif
#ifdef EXV_HAVE_ICONV
have_iconv = 1;
#endif
#if __has_include(<libintl.h>)
have_libintl = 1;
#endif
#if __has_include(<memory.h>)
have_memory = 1;
#endif
#if __has_include(<stdbool.h>)
have_stdbool = 1;
#endif
#if __has_include(<stdint.h>)
have_stdint = 1;
#endif
#if __has_include(<stdlib.h>)
have_stdlib = 1;
#endif
#ifdef EXV_HAVE_STRERROR_R
have_strerror_r = 1;
#endif
#if __has_include(<strings.h>)
have_strings = 1;
#endif
#if __has_include(<sys/mman.h>)
have_mmap = 1;
#endif
#if __has_include(<sys/mman.h>)
have_munmap = 1;
#endif
#if __has_include(<sys/stat.h>)
have_sys_stat = 1;
#endif
#if __has_include(<sys/types.h>)
have_sys_types = 1;
#endif
#if __has_include(<unistd.h>)
have_unistd = 1;
#endif
#if __has_include(<sys/mman.h>)
have_sys_mman = 1;
#endif
#ifdef EXV_HAVE_LIBZ
have_libz = 1;
#endif
#ifdef EXV_HAVE_BROTLI
have_brotli = 1;
#endif
#ifdef EXV_HAVE_XMP_TOOLKIT
have_xmptoolkit = 1;
#endif
#ifdef EXV_ADOBE_XMPSDK
adobe_xmpsdk = EXV_ADOBE_XMPSDK;
#endif
#ifdef EXV_ENABLE_BMFF
enable_bmff = 1;
#endif
#ifdef EXV_ENABLE_WEBREADY
enable_webready = 1;
#endif
#ifdef EXV_ENABLE_NLS
enable_nls = 1;
#endif
#ifdef EXV_ENABLE_VIDEO
enable_video = 1;
#endif
#ifdef EXV_USE_CURL
use_curl = 1;
#endif
std::vector<std::string> libs = getLoadedLibraries();
output(os, keys, "exiv2", Exiv2::versionString());
output(os, keys, "platform", platform);
output(os, keys, "compiler", compiler);
output(os, keys, "bits", bits);
output(os, keys, "dll", dll);
output(os, keys, "debug", debug);
output(os, keys, "cplusplus", __cplusplus);
output(os, keys, "version", __VERSION__);
output(os, keys, "date", __DATE__);
output(os, keys, "time", __TIME__);
output(os, keys, "processpath", Exiv2::getProcessPath());
#ifdef EXV_ENABLE_NLS
output(os, keys, "localedir", EXV_LOCALEDIR);
#endif
output(os, keys, "package_name", EXV_PACKAGE_NAME);
#ifdef EXV_USE_CURL
std::string curl_protocols;
auto vinfo = curl_version_info(CURLVERSION_NOW);
for (int i = 0; vinfo->protocols[i]; i++) {
curl_protocols += vinfo->protocols[i];
curl_protocols += " ";
}
output(os, keys, "curlprotocols", curl_protocols);
#endif
output(os, keys, "curl", use_curl);
if (libs.begin() != libs.end()) {
output(os, keys, "executable", *libs.begin());
for (auto lib = libs.begin() + 1; lib != libs.end(); ++lib)
output(os, keys, "library", *lib);
}
output(os, keys, "have_inttypes", have_inttypes);
output(os, keys, "have_libintl", have_libintl);
output(os, keys, "have_lensdata", have_lensdata);
output(os, keys, "have_iconv", have_iconv);
output(os, keys, "have_memory", have_memory);
output(os, keys, "have_stdbool", have_stdbool);
output(os, keys, "have_stdint", have_stdint);
output(os, keys, "have_stdlib", have_stdlib);
output(os, keys, "have_strlib", have_strlib);
output(os, keys, "have_strerror_r", have_strerror_r);
output(os, keys, "have_strings_h", have_strings_h);
output(os, keys, "have_mmap", have_mmap);
output(os, keys, "have_munmap", have_munmap);
output(os, keys, "have_sys_stat", have_sys_stat);
output(os, keys, "have_unistd_h", have_unistd_h);
output(os, keys, "have_sys_mman", have_sys_mman);
output(os, keys, "have_libz", have_libz);
output(os, keys, "have_brotli", have_brotli);
output(os, keys, "have_xmptoolkit", have_xmptoolkit);
output(os, keys, "adobe_xmpsdk", adobe_xmpsdk);
output(os, keys, "have_bool", have_bool);
output(os, keys, "have_strings", have_strings);
output(os, keys, "have_sys_types", have_sys_types);
output(os, keys, "have_unistd", have_unistd);
output(os, keys, "enable_bmff", enable_bmff);
output(os, keys, "enable_webready", enable_webready);
output(os, keys, "enable_nls", enable_nls);
output(os, keys, "enable_video", enable_video);
output(os, keys, "use_curl", use_curl);
output(os, keys, "config_path", Exiv2::Internal::getExiv2ConfigPath());
// #1147
#ifndef _WIN32
uid_t uid = getuid();
output(os, keys, "uid", uid);
uid_t euid = geteuid();
output(os, keys, "euid", euid);
uid_t gid = getgid();
output(os, keys, "gid", gid);
#endif
#ifdef EXV_HAVE_XMP_TOOLKIT
const char* name = "xmlns";
Exiv2::Dictionary ns;
Exiv2::XmpProperties::registeredNamespaces(ns);
for (const auto& [xmlns, uri] : ns) {
output(os, keys, name, xmlns + ":" + uri);
}
#endif
}