HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / roken / versionsupport.h
1 /*
2  * Copyright (c) 2018-2019, AuriStor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the
15  *   distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31
32 #ifndef _VERSIONSUPPORT_H_
33 #define _VERSIONSUPPORT_H_ 1
34
35 /*
36  * IsWindowsVersionOrGreater() is provided by Windows SDK 8.1 or greater.
37  * As AuriStorFS supports building with SDK 7.0 and greater we must
38  * provide our own.  VerifyVersionInfoW() is present on Windows XP and
39  * later.
40  */
41
42 #ifndef _WIN32_WINNT_WIN7
43 #define _WIN32_WINNT_WIN7     0x0601
44 #endif
45
46 #ifndef _WIN32_WINNT_WIN8
47 #define _WIN32_WINNT_WIN8     0x0602
48 #endif
49
50 #ifndef _WIN32_WINNT_WINBLUE
51 #define _WIN32_WINNT_WINBLUE  0x0603
52 #endif
53
54 /* Based upon VersionHelpers.h */
55 FORCEINLINE BOOL
56 IsWindowsVersionOrGreater(WORD wMajorVersion,
57                           WORD wMinorVersion,
58                           WORD wServicePackMajor)
59 {
60     OSVERSIONINFOEXW osvi;
61     DWORDLONG        dwlConditionMask = 0;
62
63     dwlConditionMask = VerSetConditionMask(dwlConditionMask,
64                                            VER_MAJORVERSION,
65                                            VER_GREATER_EQUAL);
66     dwlConditionMask = VerSetConditionMask(dwlConditionMask,
67                                            VER_MINORVERSION,
68                                            VER_GREATER_EQUAL);
69     dwlConditionMask = VerSetConditionMask(dwlConditionMask,
70                                            VER_SERVICEPACKMAJOR,
71                                            VER_GREATER_EQUAL);
72
73     memset(&osvi, 0, sizeof(OSVERSIONINFOEXW));
74     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
75     osvi.dwMajorVersion = wMajorVersion;
76     osvi.dwMinorVersion = wMinorVersion;
77     osvi.wServicePackMajor = wServicePackMajor;
78
79     return VerifyVersionInfoW(&osvi,
80                               (VER_MAJORVERSION |
81                                VER_MINORVERSION |
82                                VER_SERVICEPACKMAJOR),
83                               dwlConditionMask);
84 }
85
86 FORCEINLINE BOOL
87 IsWindowsXPOrGreater(VOID)
88 {
89     return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP),
90                                      LOBYTE(_WIN32_WINNT_WINXP),
91                                      0);
92 }
93
94 FORCEINLINE BOOL
95 IsWindows7OrGreater(VOID)
96 {
97     return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7),
98                                      LOBYTE(_WIN32_WINNT_WIN7),
99                                      0);
100 }
101
102 FORCEINLINE BOOL
103 IsWindows8OrGreater(VOID)
104 {
105     return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8),
106                                      LOBYTE(_WIN32_WINNT_WIN8),
107                                      0);
108 }
109
110 FORCEINLINE BOOL
111 IsWindows8Point1OrGreater(VOID)
112 {
113     return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINBLUE),
114                                      LOBYTE(_WIN32_WINNT_WINBLUE),
115                                      0);
116 }
117
118 #define IS_WINDOWS_VERSION_OR_GREATER_CACHED(fn)                \
119                                                                 \
120 FORCEINLINE BOOL                                                \
121 fn##Cached(VOID)                                                \
122 {                                                               \
123     static LONG lIsVersionOrGreater = -1;                       \
124                                                                 \
125     if (lIsVersionOrGreater == -1) {                            \
126         LONG lResult = fn();                                    \
127         InterlockedCompareExchangeRelease(&lIsVersionOrGreater, \
128                                           lResult, -1);         \
129     }                                                           \
130                                                                 \
131     return lIsVersionOrGreater == 1;                            \
132 }
133
134 IS_WINDOWS_VERSION_OR_GREATER_CACHED(IsWindowsXPOrGreater)
135 IS_WINDOWS_VERSION_OR_GREATER_CACHED(IsWindows7OrGreater)
136 IS_WINDOWS_VERSION_OR_GREATER_CACHED(IsWindows8OrGreater)
137 IS_WINDOWS_VERSION_OR_GREATER_CACHED(IsWindows8Point1OrGreater)
138
139 #endif /* _VERSIONSUPPORT_H_ */