-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathversion.c
248 lines (217 loc) · 8.56 KB
/
version.c
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
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* PURPOSE: Runtime code
* FILE: lib/rtl/version.c
* PROGRAMERS: Filip Navara
* Hermes Belusca-Maito (hermes.belusca@sfr.fr)
*/
/* INCLUDES *****************************************************************/
#include <rtl.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS ******************************************************************/
NTSTATUS
NTAPI
RtlGetVersion(OUT PRTL_OSVERSIONINFOW lpVersionInformation);
/* FUNCTIONS ****************************************************************/
static BYTE
RtlpVerGetCondition(IN ULONGLONG dwlConditionMask,
IN DWORD dwTypeBitMask);
static BOOLEAN
RtlpVerCompare(ULONG left, ULONG right, UCHAR condition)
{
switch (condition)
{
case VER_EQUAL:
return (left == right);
case VER_GREATER:
return (left > right);
case VER_GREATER_EQUAL:
return (left >= right);
case VER_LESS:
return (left < right);
case VER_LESS_EQUAL:
return (left <= right);
default:
break;
}
return FALSE;
}
/*
* @implemented
*/
NTSTATUS
NTAPI
RtlVerifyVersionInfo(IN PRTL_OSVERSIONINFOEXW VersionInfo,
IN ULONG TypeMask,
IN ULONGLONG ConditionMask)
{
RTL_OSVERSIONINFOEXW ver;
NTSTATUS status;
BOOLEAN comparison;
/* FIXME:
- Check the following special case on Windows (various versions):
o lp->wSuiteMask == 0 and ver.wSuiteMask != 0 and VER_AND/VER_OR
o lp->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW)
- MSDN talks about some tests being impossible. Check what really happens.
*/
ver.dwOSVersionInfoSize = sizeof(ver);
status = RtlGetVersion((PRTL_OSVERSIONINFOW)&ver);
if (status != STATUS_SUCCESS) return status;
if (!TypeMask || !ConditionMask) return STATUS_INVALID_PARAMETER;
if (TypeMask & VER_PRODUCT_TYPE)
{
comparison = RtlpVerCompare(ver.wProductType,
VersionInfo->wProductType,
RtlpVerGetCondition(ConditionMask, VER_PRODUCT_TYPE));
if (!comparison)
return STATUS_REVISION_MISMATCH;
}
if (TypeMask & VER_SUITENAME)
{
switch (RtlpVerGetCondition(ConditionMask, VER_SUITENAME))
{
case VER_AND:
if ((VersionInfo->wSuiteMask & ver.wSuiteMask) != VersionInfo->wSuiteMask)
{
return STATUS_REVISION_MISMATCH;
}
break;
case VER_OR:
if (!(VersionInfo->wSuiteMask & ver.wSuiteMask) && VersionInfo->wSuiteMask)
{
return STATUS_REVISION_MISMATCH;
}
break;
default:
return STATUS_INVALID_PARAMETER;
}
}
if (TypeMask & VER_PLATFORMID)
{
comparison = RtlpVerCompare(ver.dwPlatformId,
VersionInfo->dwPlatformId,
RtlpVerGetCondition(ConditionMask, VER_PLATFORMID));
if (!comparison)
return STATUS_REVISION_MISMATCH;
}
if (TypeMask & VER_BUILDNUMBER)
{
comparison = RtlpVerCompare(ver.dwBuildNumber,
VersionInfo->dwBuildNumber,
RtlpVerGetCondition(ConditionMask, VER_BUILDNUMBER));
if (!comparison)
return STATUS_REVISION_MISMATCH;
}
TypeMask &= VER_MAJORVERSION|VER_MINORVERSION|VER_SERVICEPACKMAJOR|VER_SERVICEPACKMINOR;
if (TypeMask)
{
BOOLEAN do_next_check = TRUE;
/*
* Select the leading comparison operator (for example, the comparison
* operator for VER_MAJORVERSION supersedes the others for VER_MINORVERSION,
* VER_SERVICEPACKMAJOR and VER_SERVICEPACKMINOR).
*/
BYTE condition = RtlpVerGetCondition(ConditionMask, TypeMask);
comparison = TRUE;
if (TypeMask & VER_MAJORVERSION)
{
comparison = RtlpVerCompare(ver.dwMajorVersion,
VersionInfo->dwMajorVersion,
condition);
do_next_check = (ver.dwMajorVersion == VersionInfo->dwMajorVersion) &&
((condition != VER_EQUAL) || comparison);
}
if ((TypeMask & VER_MINORVERSION) && do_next_check)
{
comparison = RtlpVerCompare(ver.dwMinorVersion,
VersionInfo->dwMinorVersion,
condition);
do_next_check = (ver.dwMinorVersion == VersionInfo->dwMinorVersion) &&
((condition != VER_EQUAL) || comparison);
}
if ((TypeMask & VER_SERVICEPACKMAJOR) && do_next_check)
{
comparison = RtlpVerCompare(ver.wServicePackMajor,
VersionInfo->wServicePackMajor,
condition);
do_next_check = (ver.wServicePackMajor == VersionInfo->wServicePackMajor) &&
((condition != VER_EQUAL) || comparison);
}
if ((TypeMask & VER_SERVICEPACKMINOR) && do_next_check)
{
comparison = RtlpVerCompare(ver.wServicePackMinor,
VersionInfo->wServicePackMinor,
condition);
}
if (!comparison)
return STATUS_REVISION_MISMATCH;
}
return STATUS_SUCCESS;
}
static BYTE
RtlpVerGetCondition(IN ULONGLONG dwlConditionMask,
IN DWORD dwTypeBitMask)
{
BYTE bConditionMask = 0;
if (dwTypeBitMask & VER_PRODUCT_TYPE)
bConditionMask |= dwlConditionMask >> (7 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SUITENAME)
bConditionMask |= dwlConditionMask >> (6 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_PLATFORMID)
bConditionMask |= dwlConditionMask >> (3 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_BUILDNUMBER)
bConditionMask |= dwlConditionMask >> (2 * VER_NUM_BITS_PER_CONDITION_MASK);
/*
* We choose here the lexicographical order on the 4D space
* {(Major ; Minor ; SP Major ; SP Minor)} to select the
* appropriate comparison operator.
* Therefore the following 'else if' instructions must be in this order.
*/
else if (dwTypeBitMask & VER_MAJORVERSION)
bConditionMask |= dwlConditionMask >> (1 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_MINORVERSION)
bConditionMask |= dwlConditionMask >> (0 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
bConditionMask |= dwlConditionMask >> (5 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
bConditionMask |= dwlConditionMask >> (4 * VER_NUM_BITS_PER_CONDITION_MASK);
bConditionMask &= VER_CONDITION_MASK;
return bConditionMask;
}
/*
* @implemented
*/
ULONGLONG
NTAPI
VerSetConditionMask(IN ULONGLONG dwlConditionMask,
IN DWORD dwTypeBitMask,
IN BYTE bConditionMask)
{
ULONGLONG ullCondMask;
if (dwTypeBitMask == 0)
return dwlConditionMask;
bConditionMask &= VER_CONDITION_MASK;
if (bConditionMask == 0)
return dwlConditionMask;
ullCondMask = bConditionMask;
if (dwTypeBitMask & VER_PRODUCT_TYPE)
dwlConditionMask |= ullCondMask << (7 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SUITENAME)
dwlConditionMask |= ullCondMask << (6 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
dwlConditionMask |= ullCondMask << (5 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
dwlConditionMask |= ullCondMask << (4 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_PLATFORMID)
dwlConditionMask |= ullCondMask << (3 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_BUILDNUMBER)
dwlConditionMask |= ullCondMask << (2 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_MAJORVERSION)
dwlConditionMask |= ullCondMask << (1 * VER_NUM_BITS_PER_CONDITION_MASK);
else if (dwTypeBitMask & VER_MINORVERSION)
dwlConditionMask |= ullCondMask << (0 * VER_NUM_BITS_PER_CONDITION_MASK);
return dwlConditionMask;
}
/* EOF */