-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.cpp
182 lines (141 loc) · 4.02 KB
/
integration.cpp
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "integration.h"
#ifdef _WIN32
#include <regex>
#else
#include <re2/re2.h>
#endif
#include <sstream>
#include <unordered_map>
#include "logger.h"
#include "util.h"
namespace trace
{
std::mutex m_assemblyReferenceCacheMutex;
std::unordered_map<WSTRING, std::unique_ptr<AssemblyReference>> m_assemblyReferenceCache;
AssemblyReference::AssemblyReference(const WSTRING& str)
: name(GetNameFromAssemblyReferenceString(str))
, version(GetVersionFromAssemblyReferenceString(str))
, locale(GetLocaleFromAssemblyReferenceString(str))
, public_key(GetPublicKeyFromAssemblyReferenceString(str))
{
}
AssemblyReference* AssemblyReference::GetFromCache(const WSTRING& str)
{
std::lock_guard<std::mutex> guard(m_assemblyReferenceCacheMutex);
auto findRes = m_assemblyReferenceCache.find(str);
if (findRes != m_assemblyReferenceCache.end())
{
return findRes->second.get();
}
AssemblyReference* aref = new AssemblyReference(str);
m_assemblyReferenceCache[str] = std::unique_ptr<AssemblyReference>(aref);
return aref;
}
namespace
{
WSTRING GetNameFromAssemblyReferenceString(const WSTRING& wstr)
{
WSTRING name = wstr;
auto pos = name.find(WStr(','));
if (pos != WSTRING::npos)
{
name = name.substr(0, pos);
}
// strip spaces
pos = name.rfind(WStr(' '));
if (pos != WSTRING::npos)
{
name = name.substr(0, pos);
}
return name;
}
Version GetVersionFromAssemblyReferenceString(const WSTRING& str)
{
unsigned short major = 0;
unsigned short minor = 0;
unsigned short build = 0;
unsigned short revision = 0;
if (str.empty())
{
return {major, minor, build, revision};
}
#ifdef _WIN32
static auto re = std::wregex(WStr("Version=([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
std::wsmatch match;
if (std::regex_search(str, match, re) && match.size() == 5)
{
WSTRINGSTREAM(match.str(1)) >> major;
WSTRINGSTREAM(match.str(2)) >> minor;
WSTRINGSTREAM(match.str(3)) >> build;
WSTRINGSTREAM(match.str(4)) >> revision;
}
#else
static re2::RE2 re("Version=([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)", RE2::Quiet);
re2::RE2::PartialMatch(ToString(str), re, &major, &minor, &build, &revision);
#endif
return {major, minor, build, revision};
}
WSTRING GetLocaleFromAssemblyReferenceString(const WSTRING& str)
{
WSTRING locale = WStr("neutral");
if (str.empty())
{
return locale;
}
#ifdef _WIN32
static auto re = std::wregex(WStr("Culture=([a-zA-Z0-9]+)"));
std::wsmatch match;
if (std::regex_search(str, match, re) && match.size() == 2)
{
locale = match.str(1);
}
#else
static re2::RE2 re("Culture=([a-zA-Z0-9]+)", RE2::Quiet);
std::string match;
if (re2::RE2::PartialMatch(ToString(str), re, &match))
{
locale = ToWSTRING(match);
}
#endif
return locale;
}
PublicKey GetPublicKeyFromAssemblyReferenceString(const WSTRING& str)
{
BYTE data[8] = {0};
if (str.empty())
{
return PublicKey(data);
}
#ifdef _WIN32
static auto re = std::wregex(WStr("PublicKeyToken=([a-fA-F0-9]{16})"));
std::wsmatch match;
if (std::regex_search(str, match, re) && match.size() == 2)
{
for (int i = 0; i < 8; i++)
{
auto s = match.str(1).substr(i * 2, 2);
unsigned long x;
WSTRINGSTREAM(s) >> std::hex >> x;
data[i] = BYTE(x);
}
}
#else
static re2::RE2 re("PublicKeyToken=([a-fA-F0-9]{16})");
std::string match;
if (re2::RE2::PartialMatch(ToString(str), re, &match))
{
for (int i = 0; i < 8; i++)
{
auto s = match.substr(i * 2, 2);
unsigned long x;
std::stringstream(s) >> std::hex >> x;
data[i] = BYTE(x);
}
}
#endif
return PublicKey(data);
}
} // namespace
} // namespace trace