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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
// Exit macros
#define VerExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_VERUTIL, x, s, __VA_ARGS__)
#define VerExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_VERUTIL, p, x, e, s, __VA_ARGS__)
#define VerExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_VERUTIL, p, x, s, __VA_ARGS__)
#define VerExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_VERUTIL, p, x, e, s, __VA_ARGS__)
#define VerExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_VERUTIL, p, x, s, __VA_ARGS__)
#define VerExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_VERUTIL, e, x, s, __VA_ARGS__)
// constants
const DWORD GROW_RELEASE_LABELS = 3;
// Forward declarations.
static int CompareDword(
__in const DWORD& dw1,
__in const DWORD& dw2
);
static HRESULT CompareReleaseLabel(
__in const VERUTIL_VERSION_RELEASE_LABEL* p1,
__in LPCWSTR wzVersion1,
__in const VERUTIL_VERSION_RELEASE_LABEL* p2,
__in LPCWSTR wzVersion2,
__out int* pnResult
);
static HRESULT CompareVersionSubstring(
__in LPCWSTR wzString1,
__in int cchCount1,
__in LPCWSTR wzString2,
__in int cchCount2,
__out int* pnResult
);
DAPI_(HRESULT) VerCompareParsedVersions(
__in_opt VERUTIL_VERSION* pVersion1,
__in_opt VERUTIL_VERSION* pVersion2,
__out int* pnResult
)
{
HRESULT hr = S_OK;
int nResult = 0;
DWORD cMaxReleaseLabels = 0;
BOOL fCompareMetadata = FALSE;
if (pVersion1 && !pVersion1->sczVersion ||
pVersion2 && !pVersion2->sczVersion)
{
ExitFunction1(hr = E_INVALIDARG);
}
if (pVersion1 == pVersion2)
{
ExitFunction1(nResult = 0);
}
else if (pVersion1 && !pVersion2)
{
ExitFunction1(nResult = 1);
}
else if (!pVersion1 && pVersion2)
{
ExitFunction1(nResult = -1);
}
nResult = CompareDword(pVersion1->dwMajor, pVersion2->dwMajor);
if (0 != nResult)
{
ExitFunction();
}
nResult = CompareDword(pVersion1->dwMinor, pVersion2->dwMinor);
if (0 != nResult)
{
ExitFunction();
}
nResult = CompareDword(pVersion1->dwPatch, pVersion2->dwPatch);
if (0 != nResult)
{
ExitFunction();
}
nResult = CompareDword(pVersion1->dwRevision, pVersion2->dwRevision);
if (0 != nResult)
{
ExitFunction();
}
if (pVersion1->cReleaseLabels)
{
if (pVersion2->cReleaseLabels)
{
cMaxReleaseLabels = max(pVersion1->cReleaseLabels, pVersion2->cReleaseLabels);
}
else
{
ExitFunction1(nResult = -1);
}
}
else if (pVersion2->cReleaseLabels)
{
ExitFunction1(nResult = 1);
}
if (cMaxReleaseLabels)
{
for (DWORD i = 0; i < cMaxReleaseLabels; ++i)
{
VERUTIL_VERSION_RELEASE_LABEL* pReleaseLabel1 = pVersion1->cReleaseLabels > i ? pVersion1->rgReleaseLabels + i : NULL;
VERUTIL_VERSION_RELEASE_LABEL* pReleaseLabel2 = pVersion2->cReleaseLabels > i ? pVersion2->rgReleaseLabels + i : NULL;
hr = CompareReleaseLabel(pReleaseLabel1, pVersion1->sczVersion, pReleaseLabel2, pVersion2->sczVersion, &nResult);
if (FAILED(hr) || 0 != nResult)
{
ExitFunction();
}
}
}
if (pVersion1->fInvalid)
{
if (!pVersion2->fInvalid)
{
ExitFunction1(nResult = -1);
}
else
{
fCompareMetadata = TRUE;
}
}
else if (pVersion2->fInvalid)
{
ExitFunction1(nResult = 1);
}
if (fCompareMetadata)
{
hr = CompareVersionSubstring(pVersion1->sczVersion + pVersion1->cchMetadataOffset, -1, pVersion2->sczVersion + pVersion2->cchMetadataOffset, -1, &nResult);
}
LExit:
*pnResult = nResult;
return hr;
}
DAPI_(HRESULT) VerCompareStringVersions(
__in_z LPCWSTR wzVersion1,
__in_z LPCWSTR wzVersion2,
__in BOOL fStrict,
__out int* pnResult
)
{
HRESULT hr = S_OK;
VERUTIL_VERSION* pVersion1 = NULL;
VERUTIL_VERSION* pVersion2 = NULL;
int nResult = 0;
hr = VerParseVersion(wzVersion1, 0, fStrict, &pVersion1);
VerExitOnFailure(hr, "Failed to parse Verutil version '%ls'", wzVersion1);
hr = VerParseVersion(wzVersion2, 0, fStrict, &pVersion2);
VerExitOnFailure(hr, "Failed to parse Verutil version '%ls'", wzVersion2);
hr = VerCompareParsedVersions(pVersion1, pVersion2, &nResult);
VerExitOnFailure(hr, "Failed to compare parsed Verutil versions '%ls' and '%ls'.", wzVersion1, wzVersion2);
LExit:
*pnResult = nResult;
ReleaseVerutilVersion(pVersion1);
ReleaseVerutilVersion(pVersion2);
return hr;
}
DAPI_(HRESULT) VerCopyVersion(
__in VERUTIL_VERSION* pSource,
__out VERUTIL_VERSION** ppVersion
)
{
HRESULT hr = S_OK;
VERUTIL_VERSION* pCopy = NULL;
pCopy = reinterpret_cast<VERUTIL_VERSION*>(MemAlloc(sizeof(VERUTIL_VERSION), TRUE));
VerExitOnNull(pCopy, hr, E_OUTOFMEMORY, "Failed to allocate memory for Verutil version copy.");
hr = StrAllocString(&pCopy->sczVersion, pSource->sczVersion, 0);
VerExitOnFailure(hr, "Failed to copy Verutil version string '%ls'.", pSource->sczVersion);
pCopy->dwMajor = pSource->dwMajor;
pCopy->dwMinor = pSource->dwMinor;
pCopy->dwPatch = pSource->dwPatch;
pCopy->dwRevision = pSource->dwRevision;
if (pSource->cReleaseLabels)
{
hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&pCopy->rgReleaseLabels), 0, sizeof(VERUTIL_VERSION_RELEASE_LABEL), pSource->cReleaseLabels);
VerExitOnFailure(hr, "Failed to allocate memory for Verutil version release labels copies.");
pCopy->cReleaseLabels = pSource->cReleaseLabels;
for (DWORD i = 0; i < pCopy->cReleaseLabels; ++i)
{
VERUTIL_VERSION_RELEASE_LABEL* pSourceLabel = pSource->rgReleaseLabels + i;
VERUTIL_VERSION_RELEASE_LABEL* pCopyLabel = pCopy->rgReleaseLabels + i;
pCopyLabel->cchLabelOffset = pSourceLabel->cchLabelOffset;
pCopyLabel->cchLabel = pSourceLabel->cchLabel;
pCopyLabel->fNumeric = pSourceLabel->fNumeric;
pCopyLabel->dwValue = pSourceLabel->dwValue;
}
}
pCopy->cchMetadataOffset = pSource->cchMetadataOffset;
pCopy->fInvalid = pSource->fInvalid;
*ppVersion = pCopy;
pCopy = NULL;
LExit:
ReleaseVerutilVersion(pCopy);
return hr;
}
DAPI_(void) VerFreeVersion(
__in VERUTIL_VERSION* pVersion
)
{
if (pVersion)
{
ReleaseStr(pVersion->sczVersion);
ReleaseMem(pVersion->rgReleaseLabels);
ReleaseMem(pVersion);
}
}
DAPI_(HRESULT) VerParseVersion(
__in_z LPCWSTR wzVersion,
__in SIZE_T cchVersion,
__in BOOL fStrict,
__out VERUTIL_VERSION** ppVersion
)
{
HRESULT hr = S_OK;
VERUTIL_VERSION* pVersion = NULL;
LPCWSTR wzEnd = NULL;
LPCWSTR wzPartBegin = NULL;
LPCWSTR wzPartEnd = NULL;
BOOL fInvalid = FALSE;
BOOL fLastPart = FALSE;
BOOL fTrailingDot = FALSE;
BOOL fParsedVersionNumber = FALSE;
BOOL fExpectedReleaseLabels = FALSE;
DWORD iPart = 0;
if (!wzVersion || !ppVersion)
{
ExitFunction1(hr = E_INVALIDARG);
}
// Get string length if not provided.
if (!cchVersion)
{
hr = ::StringCchLengthW(wzVersion, STRSAFE_MAX_CCH, reinterpret_cast<size_t*>(&cchVersion));
VerExitOnRootFailure(hr, "Failed to get length of version string: %ls", wzVersion);
}
else if (INT_MAX < cchVersion)
{
VerExitOnRootFailure(hr = E_INVALIDARG, "Version string is too long: %Iu", cchVersion);
}
if (L'v' == *wzVersion || L'V' == *wzVersion)
{
++wzVersion;
--cchVersion;
}
pVersion = reinterpret_cast<VERUTIL_VERSION*>(MemAlloc(sizeof(VERUTIL_VERSION), TRUE));
VerExitOnNull(pVersion, hr, E_OUTOFMEMORY, "Failed to allocate memory for Verutil version '%ls'.", wzVersion);
hr = StrAllocString(&pVersion->sczVersion, wzVersion, cchVersion);
VerExitOnFailure(hr, "Failed to copy Verutil version string '%ls'.", wzVersion);
wzVersion = wzPartBegin = wzPartEnd = pVersion->sczVersion;
// Save end pointer.
wzEnd = wzVersion + cchVersion;
// Parse version number
while (wzPartBegin < wzEnd)
{
fTrailingDot = FALSE;
// Find end of part.
for (;;)
{
if (wzPartEnd >= wzEnd)
{
fLastPart = TRUE;
break;
}
switch (*wzPartEnd)
{
case L'0':
case L'1':
case L'2':
case L'3':
case L'4':
case L'5':
case L'6':
case L'7':
case L'8':
case L'9':
++wzPartEnd;
continue;
case L'.':
fTrailingDot = TRUE;
break;
case L'-':
case L'+':
fLastPart = TRUE;
break;
default:
fInvalid = TRUE;
break;
}
break;
}
if (wzPartBegin == wzPartEnd)
{
fInvalid = TRUE;
}
if (fInvalid)
{
break;
}
DWORD cchPart = 0;
hr = ::PtrdiffTToDWord(wzPartEnd - wzPartBegin, &cchPart);
if (FAILED(hr))
{
fInvalid = TRUE;
break;
}
// Parse version part.
UINT uPart = 0;
hr = StrStringToUInt32(wzPartBegin, cchPart, &uPart);
if (FAILED(hr))
{
fInvalid = TRUE;
break;
}
switch (iPart)
{
case 0:
pVersion->dwMajor = uPart;
break;
case 1:
pVersion->dwMinor = uPart;
break;
case 2:
pVersion->dwPatch = uPart;
break;
case 3:
pVersion->dwRevision = uPart;
break;
}
if (fTrailingDot)
{
++wzPartEnd;
}
wzPartBegin = wzPartEnd;
++iPart;
if (4 <= iPart || fLastPart)
{
fParsedVersionNumber = TRUE;
break;
}
}
fInvalid |= !fParsedVersionNumber || fTrailingDot;
if (!fInvalid && wzPartBegin < wzEnd && *wzPartBegin == L'-')
{
wzPartBegin = wzPartEnd = wzPartBegin + 1;
fExpectedReleaseLabels = TRUE;
fLastPart = FALSE;
}
while (fExpectedReleaseLabels && wzPartBegin < wzEnd)
{
fTrailingDot = FALSE;
// Find end of part.
for (;;)
{
if (wzPartEnd >= wzEnd)
{
fLastPart = TRUE;
break;
}
if (*wzPartEnd >= L'0' && *wzPartEnd <= L'9' ||
*wzPartEnd >= L'A' && *wzPartEnd <= L'Z' ||
*wzPartEnd >= L'a' && *wzPartEnd <= L'z' ||
*wzPartEnd == L'-')
{
++wzPartEnd;
continue;
}
else if (*wzPartEnd == L'+')
{
fLastPart = TRUE;
}
else if (*wzPartEnd == L'.')
{
fTrailingDot = TRUE;
}
else
{
fInvalid = TRUE;
}
break;
}
if (wzPartBegin == wzPartEnd)
{
fInvalid = TRUE;
}
if (fInvalid)
{
break;
}
int cchLabel = 0;
hr = ::PtrdiffTToInt32(wzPartEnd - wzPartBegin, &cchLabel);
if (FAILED(hr) || 0 > cchLabel)
{
fInvalid = TRUE;
break;
}
hr = MemEnsureArraySizeForNewItems(reinterpret_cast<LPVOID*>(&pVersion->rgReleaseLabels), pVersion->cReleaseLabels, 1, sizeof(VERUTIL_VERSION_RELEASE_LABEL), GROW_RELEASE_LABELS);
VerExitOnFailure(hr, "Failed to allocate memory for Verutil version release labels '%ls'", wzVersion);
VERUTIL_VERSION_RELEASE_LABEL* pReleaseLabel = pVersion->rgReleaseLabels + pVersion->cReleaseLabels;
++pVersion->cReleaseLabels;
// Try to parse as number.
UINT uLabel = 0;
hr = StrStringToUInt32(wzPartBegin, cchLabel, &uLabel);
if (SUCCEEDED(hr))
{
pReleaseLabel->fNumeric = TRUE;
pReleaseLabel->dwValue = uLabel;
}
pReleaseLabel->cchLabelOffset = wzPartBegin - pVersion->sczVersion;
pReleaseLabel->cchLabel = cchLabel;
if (fTrailingDot)
{
++wzPartEnd;
}
wzPartBegin = wzPartEnd;
if (fLastPart)
{
break;
}
}
fInvalid |= fExpectedReleaseLabels && (!pVersion->cReleaseLabels || fTrailingDot);
if (!fInvalid && wzPartBegin < wzEnd)
{
if (*wzPartBegin == L'+')
{
wzPartBegin = wzPartEnd = wzPartBegin + 1;
}
else
{
fInvalid = TRUE;
}
}
if (fInvalid && fStrict)
{
ExitFunction1(hr = E_INVALIDARG);
}
pVersion->cchMetadataOffset = min(wzPartBegin, wzEnd) - pVersion->sczVersion;
pVersion->fInvalid = fInvalid;
*ppVersion = pVersion;
pVersion = NULL;
hr = S_OK;
LExit:
ReleaseVerutilVersion(pVersion);
return hr;
}
DAPI_(HRESULT) VerVersionFromQword(
__in DWORD64 qwVersion,
__out VERUTIL_VERSION** ppVersion
)
{
HRESULT hr = S_OK;
VERUTIL_VERSION* pVersion = NULL;
pVersion = reinterpret_cast<VERUTIL_VERSION*>(MemAlloc(sizeof(VERUTIL_VERSION), TRUE));
VerExitOnNull(pVersion, hr, E_OUTOFMEMORY, "Failed to allocate memory for Verutil version from QWORD.");
pVersion->dwMajor = (WORD)(qwVersion >> 48 & 0xffff);
pVersion->dwMinor = (WORD)(qwVersion >> 32 & 0xffff);
pVersion->dwPatch = (WORD)(qwVersion >> 16 & 0xffff);
pVersion->dwRevision = (WORD)(qwVersion & 0xffff);
hr = StrAllocFormatted(&pVersion->sczVersion, L"%lu.%lu.%lu.%lu", pVersion->dwMajor, pVersion->dwMinor, pVersion->dwPatch, pVersion->dwRevision);
ExitOnFailure(hr, "Failed to allocate and format the version string.");
pVersion->cchMetadataOffset = lstrlenW(pVersion->sczVersion);
*ppVersion = pVersion;
pVersion = NULL;
LExit:
ReleaseVerutilVersion(pVersion);
return hr;
}
static int CompareDword(
__in const DWORD& dw1,
__in const DWORD& dw2
)
{
int nResult = 0;
if (dw1 > dw2)
{
nResult = 1;
}
else if (dw1 < dw2)
{
nResult = -1;
}
return nResult;
}
static HRESULT CompareReleaseLabel(
__in const VERUTIL_VERSION_RELEASE_LABEL* p1,
__in LPCWSTR wzVersion1,
__in const VERUTIL_VERSION_RELEASE_LABEL* p2,
__in LPCWSTR wzVersion2,
__out int* pnResult
)
{
HRESULT hr = S_OK;
int nResult = 0;
if (p1 == p2)
{
ExitFunction();
}
else if (p1 && !p2)
{
ExitFunction1(nResult = 1);
}
else if (!p1 && p2)
{
ExitFunction1(nResult = -1);
}
if (p1->fNumeric)
{
if (p2->fNumeric)
{
nResult = CompareDword(p1->dwValue, p2->dwValue);
}
else
{
nResult = -1;
}
}
else
{
if (p2->fNumeric)
{
nResult = 1;
}
else
{
hr = CompareVersionSubstring(wzVersion1 + p1->cchLabelOffset, p1->cchLabel, wzVersion2 + p2->cchLabelOffset, p2->cchLabel, &nResult);
}
}
LExit:
*pnResult = nResult;
return hr;
}
static HRESULT CompareVersionSubstring(
__in LPCWSTR wzString1,
__in int cchCount1,
__in LPCWSTR wzString2,
__in int cchCount2,
__out int* pnResult
)
{
HRESULT hr = S_OK;
int nResult = 0;
nResult = ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, wzString1, cchCount1, wzString2, cchCount2);
if (!nResult)
{
VerExitOnLastError(hr, "Failed to compare version substrings");
}
LExit:
*pnResult = nResult - 2;
return hr;
}
|