forked from iOS-Auth/iOSAuthRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.cpp
238 lines (212 loc) · 7.19 KB
/
Library.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
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
#include "Library.h"
// 模块句柄
static HMODULE static_module_iTunesCore = nullptr;
static HMODULE static_module_AirTrafficHost = nullptr;
static HMODULE static_module_CoreFoundation = nullptr;
// 模块版本
static std::wstring static_version_iTunesCore;
static std::wstring static_version_AirTrafficHost;
static std::wstring static_version_CoreFoundation;
// 查找文件版本
static std::wstring iOSAuthLibraryQueryFileVersion(const std::wstring& _Path) noexcept
{
// 申请版本信息内存
auto vVersionSize = GetFileVersionInfoSizeW(_Path.data(), nullptr);
if(vVersionSize == 0)
{
return {};
}
auto vVersionData = new(std::nothrow) wchar_t[vVersionSize + 1];
if(vVersionData == nullptr)
{
return {};
}
std::wmemset(vVersionData, 0, vVersionSize + 1);
// 获取具体版本信息
if(!GetFileVersionInfoW(_Path.data(), 0, vVersionSize, vVersionData))
{
delete[] vVersionData;
return {};
}
// 转换版本信息
VS_FIXEDFILEINFO* pVsInfo;
auto iFileInfoSize = sizeof(VS_FIXEDFILEINFO);
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
}*lpTranslate;
wchar_t vVersionFormat[64] = { 0 };
if(VerQueryValueW(vVersionData, L"\\VarFileInfo\\Translation", (void**)&lpTranslate, &iFileInfoSize))
{
unsigned int version_len = 0;
if(VerQueryValueW(vVersionData, L"\\", (void**)&pVsInfo, &version_len))
{
std::swprintf(vVersionFormat, L"%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
}
}
delete[] vVersionData;
return vVersionFormat;
}
// 查找模块安装目录
static std::wstring iOSAuthLibraryModuleInstallDir(HKEY _Key, const wchar_t* _Path) noexcept
{
std::wstring vValue;
HKEY vKey;
if(ERROR_SUCCESS == RegOpenKeyExW(_Key, _Path, 0, KEY_READ, &vKey))
{
wchar_t vValueByte[MAX_PATH] = { 0 };
DWORD vValueSize = MAX_PATH;
RegQueryValueExW(vKey, L"InstallDir", nullptr, nullptr, (LPBYTE)vValueByte, &vValueSize);
vValue = vValueByte;
if (!vValue.empty() && vValue[vValue.size() - 1] == L'\\')
{
vValue.pop_back();
}
RegCloseKey(vKey);
}
return vValue;
}
// 将目录添加进PATH
static bool iOSAuthLibraryEnvAppend(const std::wstring& _Directory) noexcept
{
auto vEnvSize = 32767;
auto vEnvByte = new(std::nothrow) wchar_t[vEnvSize];
if (vEnvByte == nullptr)
{
return false;
}
std::memset(vEnvByte, 0, vEnvSize);
if (0 == GetEnvironmentVariableW(L"PATH", vEnvByte, vEnvSize))
{
delete[] vEnvByte;
return false;
}
auto vEnvNew = _Directory + L";" + vEnvByte;
if (nullptr == std::wcsstr(vEnvByte, _Directory.data()))
{
if (FALSE == SetEnvironmentVariableW(L"PATH", vEnvNew.data()))
{
delete[] vEnvByte;
return false;
}
}
delete[] vEnvByte;
return true;
}
// 加载苹果库
IOS_AUTH_RUNTIME_API bool iOSAuthLibraryInitialize() noexcept
{
auto vDirCore = iOSAuthLibraryModuleInstallDir(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Apple Computer, Inc.\iTunes)");
auto vDirAAS = iOSAuthLibraryModuleInstallDir(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Apple inc.\Apple Application Support)");
auto vDirMDS = iOSAuthLibraryModuleInstallDir(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Apple inc.\Apple Mobile Device Support)");
if(vDirCore.empty() || vDirAAS.empty() || vDirMDS.empty())
{
return false;
}
// 设置环境变量
iOSAuthLibraryEnvAppend(vDirCore);
iOSAuthLibraryEnvAppend(vDirAAS);
iOSAuthLibraryEnvAppend(vDirMDS);
// 检查模块是否存在
auto vPathCore1 = vDirCore + LR"(\iTunes.dll)";
auto vPathCore2 = vDirCore + LR"(\iTunesCore.dll)";
auto vPathAAS = vDirAAS + LR"(\CoreFoundation.dll)";
auto vPathMDS = vDirMDS + LR"(\AirTrafficHost.dll)";
auto vAttribCore1 = GetFileAttributesW(vPathCore1.data());
auto vAttribCore2 = GetFileAttributesW(vPathCore2.data());
auto vAttribAAS = GetFileAttributesW(vPathAAS.data());
auto vAttribMDS = GetFileAttributesW(vPathMDS.data());
if((INVALID_FILE_ATTRIBUTES == vAttribCore1 && INVALID_FILE_ATTRIBUTES == vAttribCore2) || INVALID_FILE_ATTRIBUTES == vAttribAAS || INVALID_FILE_ATTRIBUTES == vAttribMDS)
{
return false;
}
// 加载模块
static_module_iTunesCore = LoadLibraryW(vPathCore1.data());
if(static_module_iTunesCore == nullptr)
{
static_module_iTunesCore = LoadLibraryW(vPathCore2.data());
if(static_module_iTunesCore == nullptr)
{
iOSAuthLibraryRelease();
return false;
}
}
static_module_AirTrafficHost = LoadLibraryW(vPathMDS.data());
if(static_module_AirTrafficHost == nullptr)
{
iOSAuthLibraryRelease();
return false;
}
static_module_CoreFoundation = LoadLibraryW(vPathAAS.data());
if(static_module_CoreFoundation == nullptr)
{
iOSAuthLibraryRelease();
return false;
}
auto version_core_1 = iOSAuthLibraryQueryFileVersion(vPathCore1);
auto version_core_2 = iOSAuthLibraryQueryFileVersion(vPathCore2);
static_version_iTunesCore = version_core_1.empty() ? version_core_2 : version_core_1;
static_version_AirTrafficHost = iOSAuthLibraryQueryFileVersion(vPathMDS);
static_version_CoreFoundation = iOSAuthLibraryQueryFileVersion(vPathAAS);
if(static_version_iTunesCore.empty() || static_version_AirTrafficHost.empty() || static_version_CoreFoundation.empty())
{
iOSAuthLibraryRelease();
return false;
}
return true;
}
// 释放苹果库
IOS_AUTH_RUNTIME_API void iOSAuthLibraryRelease() noexcept
{
if(static_module_iTunesCore)
{
FreeLibrary(static_module_iTunesCore);
static_module_iTunesCore = nullptr;
}
if(static_module_AirTrafficHost)
{
FreeLibrary(static_module_AirTrafficHost);
static_module_AirTrafficHost = nullptr;
}
if(static_module_CoreFoundation)
{
FreeLibrary(static_module_CoreFoundation);
static_module_CoreFoundation = nullptr;
}
}
// 获取模块版本
IOS_AUTH_RUNTIME_API std::wstring iOSAuthLibraryModuleVersion(iOS_AUTH_MODULE_TYPE _Module) noexcept
{
switch(_Module)
{
case iOS_AUTH_MODULE_iTunesCore: return static_version_iTunesCore;
case iOS_AUTH_MODULE_AirTrafficHost: return static_version_AirTrafficHost;
case iOS_AUTH_MODULE_CoreFoundation: return static_version_CoreFoundation;
default: return {};
}
}
// 获取模块基址
IOS_AUTH_RUNTIME_API void* iOSAuthLibraryModuleAddress(iOS_AUTH_MODULE_TYPE _Module) noexcept
{
switch(_Module)
{
case iOS_AUTH_MODULE_iTunesCore: return static_module_iTunesCore;
case iOS_AUTH_MODULE_AirTrafficHost: return static_module_AirTrafficHost;
case iOS_AUTH_MODULE_CoreFoundation: return static_module_CoreFoundation;
default: return nullptr;
}
}
// 获取模块偏移
IOS_AUTH_RUNTIME_API void* iOSAuthLibraryModuleOffset(iOS_AUTH_MODULE_TYPE _Module, iOS_AUTH_MODULE_OFFSET _Offset) noexcept
{
auto vAddress = (iOS_AUTH_MODULE_OFFSET)iOSAuthLibraryModuleAddress(_Module);
return (void*)(vAddress + _Offset);
}
// 获取模块函数
IOS_AUTH_RUNTIME_API void* iOSAuthLibraryModuleFunction(iOS_AUTH_MODULE_TYPE _Module, const char* _Function) noexcept
{
auto vModule = (HMODULE)iOSAuthLibraryModuleAddress(_Module);
auto vAddress = (void*)GetProcAddress(vModule, _Function);
return vAddress;
}