-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosver.cpp
More file actions
262 lines (226 loc) · 7.09 KB
/
Copy pathosver.cpp
File metadata and controls
262 lines (226 loc) · 7.09 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
#include "common.h"
#include "osver.h"
#include "Support\\LWAnsiString\\LWAnsiString.h">
#include "IAT_VERSIONINFO.H"
#define WINDOWS_8_MAJOR 6
#define WINDOWS_8_MINOR 2
/// <summary>
/// FetchVerisonInfo sets this if RtlGetVersion was used to get the version info since that stores in unicode
/// </summary>
bool VERISON_INFO_IS_UNICODE;
/// <summary>
/// FetchVerisonInfo sets this if FetchVersionInfo was called at least once. The rest of the software can just OSVERSIONINFO dependeing on what union
/// </summary>
bool VERSION_INFO_WAS_GOTTON;
/// <summary>
/// Storage for versioned routined to actually get the version info.
/// </summary>
MyOSVERSIONINFO GlobalVersionInfo = { 0, 0, 0, 0 };
/*typedef int (WINAPI* GetVersionInfOExA_PTR)(
LPOSVERSIONINFOEXA lpVersionInformation
);
typedef NTSTATUS(WINAPI* RtlGetVersion_PTR)(LPOSVERSIONINFOEXW);*/
void SetVersionInfoToZeroAndSetSize(bool Unicode, MyOSVERSIONINFO* GlobalVersionInfo)
{
GlobalVersionInfo->A.dwBuildNumber = GlobalVersionInfo->A.dwMajorVersion = GlobalVersionInfo->A.dwMinorVersion = GlobalVersionInfo->A.dwPlatformId = 0;
GlobalVersionInfo->A.wSuiteMask = GlobalVersionInfo->A.wServicePackMajor = GlobalVersionInfo->A.wServicePackMinor = 0;
if (Unicode)
{
GlobalVersionInfo->W.szCSDVersion[0] = 0;
for (int i = 0; i < 128; i++)
{
GlobalVersionInfo->W.szCSDVersion[i] = 0;
}
}
else
{
GlobalVersionInfo->A.szCSDVersion[0] = 0;
}
GlobalVersionInfo->A.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
}
void SetVersionInfoFromAnother(MyOSVERSIONINFO* Source, bool Unicode)
{
if (Unicode)
{
GlobalVersionInfo.W.dwBuildNumber = Source->W.dwBuildNumber;
GlobalVersionInfo.W.dwMajorVersion = Source->W.dwMajorVersion;
GlobalVersionInfo.W.dwMinorVersion = Source->W.dwMinorVersion;
GlobalVersionInfo.W.dwPlatformId = Source->W.dwPlatformId;
for (int i = 0; i < 128; i++)
{
GlobalVersionInfo.A.szCSDVersion[i] = Source->A.szCSDVersion[i];
}
}
else
{
GlobalVersionInfo.A.dwBuildNumber = Source->A.dwBuildNumber;
GlobalVersionInfo.A.dwMajorVersion = Source->A.dwMajorVersion;
GlobalVersionInfo.A.dwMinorVersion = Source->A.dwMinorVersion;
GlobalVersionInfo.A.dwPlatformId = Source->A.dwPlatformId;
for (int i = 0; i < 128; i++)
{
GlobalVersionInfo.A.szCSDVersion[i] = Source->A.szCSDVersion[i];
}
}
}
int FetchVersionInfo(MyOSVERSIONINFO* Output, bool* UseUnicode)
{
bool ProbeNtVerison = false;
if (Output == nullptr)
return 0;
if (UseUnicode == nullptr)
return 0;
// why this:: no libc means memset. Why not MemorySet or Zeromemory? That resolves to RtlXXXX which on windows resolves to well memset
SetVersionInfoToZeroAndSetSize(false, Output);
/*
* Our plan is this:
* try loading kernel32 for GetVersionExA.
* if it fails (how???) we give it up.
*
* Next we try to GetProcAddress the GetVersionExA routine. If it works, we call it, if not we set our check ntdll flag (ProbeNtVerison) to true.
* Additionally, GetVersionExA is known to return 6.2 for Windows 8 if no manifest is set. We check for that and set the ProbeNtVerison flag to true which triggers the RtlGetVerison call if possible
*/
if (
(
((IAT_DynamicLinkVersionInfo(IAT_VERSIONINFO_GETVERSIONEXA | IAT_VERSIONINFO_NTDLL_RTLVERSIONINFO)) && (IAT_VERSIONINFO_GETVERSIONEXA))) == (IAT_VERSIONINFO_GETVERSIONEXA)
)
{
Output->A.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
if (IAT_GetVersionInfoExA(&Output->A) == 0)
{
ProbeNtVerison = true; // worth a shot
}
else
{
if (Output->A.dwMajorVersion == WINDOWS_8_MAJOR && Output->A.dwMinorVersion == WINDOWS_8_MINOR)
{
ProbeNtVerison = true;
}
else
{
VERSION_INFO_WAS_GOTTON = true;
*UseUnicode = false;
return 1;
}
}
}
else
{
ProbeNtVerison = true;
}
if (ProbeNtVerison)
{
{
if (IAT_RtlGetVersion == 0)
{
return 0;
}
else
{
IAT_RtlGetVersion(&Output->W);
VERSION_INFO_WAS_GOTTON = true;
*UseUnicode = true;
if (Output != &GlobalVersionInfo)
{
// copy our version data to the global version info on call if not the same
SetVersionInfoFromAnother(Output, true);
}
return 1;
}
}
}
return 0;
}
extern "C" {
bool ReportVersionByExitCodeCommon(int* result, const char** message_result, const char* argv[], DWORD offset)
{
bool use_unicode = false;
MyOSVERSIONINFO osvi;
int ret = FetchVersionInfo(&osvi, &use_unicode);
if (ret != 1)
{
if (message_result != nullptr)
{
*message_result = "Failed to get version info";
}
if (result != nullptr)
{
*result = ret;
}
return false;
}
*result = (int)*((unsigned char*)(&osvi.A) + offset);
*message_result = "Success";
return true;
}
bool ReportVersionPlatformIDViaExit(int* result, const char** message_result, const char* argv[], int argc)
{
return ReportVersionByExitCodeCommon(result, message_result, argv, offsetof(OSVERSIONINFOA, dwPlatformId));
}
bool ReportVersionBuildViaExit(int* result, const char** message_result, const char* argv[], int argc)
{
return ReportVersionByExitCodeCommon(result, message_result, argv, offsetof(OSVERSIONINFOA, dwBuildNumber));
}
bool ReportVersionMinorViaExit(int* result, const char** message_result, const char* argv[], int argc)
{
return ReportVersionByExitCodeCommon(result, message_result, argv, offsetof(OSVERSIONINFOA, dwMinorVersion));
}
bool ReportVersionMajorViaExit(int* result, const char** message_result, const char* argv[], int argc)
{
return ReportVersionByExitCodeCommon(result, message_result, argv, offsetof(OSVERSIONINFOA, dwMajorVersion));
}
bool ReportVersionStdout(int* result, const char** message_result, const char* argv[], int argc)
{
bool use_unicode = false;
MyOSVERSIONINFO osvi;
LWAnsiString* OutputString = LWAnsiString_CreateString(0); // it'll grow
int ret = FetchVersionInfo(&osvi, &use_unicode);
if (ret != 1)
{
if (message_result != nullptr)
{
*message_result = "Failed to get version info";
}
if (result != nullptr)
{
*result = ret;
}
return false;
}
else
{
{
LWAnsiString_AppendA(OutputString, "Version: ");
LWAnsiString_AppendNumberA(osvi.A.dwMajorVersion, OutputString, 0);
LWAnsiString_AppendA(OutputString, ".");
LWAnsiString_AppendNumberA(osvi.A.dwMinorVersion, OutputString, 0);
LWAnsiString_AppendA(OutputString, "\r\nBuild Version: ");
LWAnsiString_AppendNumberA(osvi.A.dwBuildNumber, OutputString, 0);
LWAnsiString_AppendA(OutputString, "\r\n");
if (osvi.A.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
LWAnsiString_AppendA(OutputString, "Platform: NT\r\n");
}
else if (osvi.A.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
LWAnsiString_AppendA(OutputString, "Platform: Windows 95/98/ME\r\n");
}
else
{
LWAnsiString_AppendA(OutputString, "Platform: Unknown\r\n");
}
LWAnsiString_AppendA(OutputString, "Service Pack: ");
if ((osvi.A.szCSDVersion[0] != 0) || (osvi.W.szCSDVersion[0] != 0))
{
if (use_unicode)
{
WriteStdout(LWAnsiString_ToCStr(OutputString));
LWAnsiString_ZeroString(OutputString);
}
}
}
LWAnsiString_FreeString(OutputString);
return true;
}
}
}