version_win32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <[email protected]>.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "../curl_setup.h"
  25. #ifdef _WIN32
  26. #include <curl/curl.h>
  27. #include "version_win32.h"
  28. #include "warnless.h"
  29. /* The last 2 #include files should be in this order */
  30. #include "../curl_memory.h"
  31. #include "../memdebug.h"
  32. /* This Unicode version struct works for VerifyVersionInfoW (OSVERSIONINFOEXW)
  33. and RtlVerifyVersionInfo (RTLOSVERSIONINFOEXW) */
  34. struct OUR_OSVERSIONINFOEXW {
  35. ULONG dwOSVersionInfoSize;
  36. ULONG dwMajorVersion;
  37. ULONG dwMinorVersion;
  38. ULONG dwBuildNumber;
  39. ULONG dwPlatformId;
  40. WCHAR szCSDVersion[128];
  41. USHORT wServicePackMajor;
  42. USHORT wServicePackMinor;
  43. USHORT wSuiteMask;
  44. UCHAR wProductType;
  45. UCHAR wReserved;
  46. };
  47. /*
  48. * curlx_verify_windows_version()
  49. *
  50. * This is used to verify if we are running on a specific Windows version.
  51. *
  52. * Parameters:
  53. *
  54. * majorVersion [in] - The major version number.
  55. * minorVersion [in] - The minor version number.
  56. * buildVersion [in] - The build version number. If 0, this parameter is
  57. * ignored.
  58. * platform [in] - The optional platform identifier.
  59. * condition [in] - The test condition used to specifier whether we are
  60. * checking a version less than, equal to or greater than
  61. * what is specified in the major and minor version
  62. * numbers.
  63. *
  64. * Returns TRUE if matched; otherwise FALSE.
  65. */
  66. bool curlx_verify_windows_version(const unsigned int majorVersion,
  67. const unsigned int minorVersion,
  68. const unsigned int buildVersion,
  69. const PlatformIdentifier platform,
  70. const VersionCondition condition)
  71. {
  72. bool matched = FALSE;
  73. #ifdef CURL_WINDOWS_UWP
  74. /* We have no way to determine the Windows version from Windows apps,
  75. so let's assume we are running on the target Windows version. */
  76. const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
  77. const WORD targetVersion = (WORD)_WIN32_WINNT;
  78. (void)buildVersion;
  79. switch(condition) {
  80. case VERSION_LESS_THAN:
  81. matched = targetVersion < fullVersion;
  82. break;
  83. case VERSION_LESS_THAN_EQUAL:
  84. matched = targetVersion <= fullVersion;
  85. break;
  86. case VERSION_EQUAL:
  87. matched = targetVersion == fullVersion;
  88. break;
  89. case VERSION_GREATER_THAN_EQUAL:
  90. matched = targetVersion >= fullVersion;
  91. break;
  92. case VERSION_GREATER_THAN:
  93. matched = targetVersion > fullVersion;
  94. break;
  95. }
  96. if(matched && (platform == PLATFORM_WINDOWS)) {
  97. /* we are always running on PLATFORM_WINNT */
  98. matched = FALSE;
  99. }
  100. #elif defined(UNDER_CE)
  101. (void)majorVersion;
  102. (void)minorVersion;
  103. (void)buildVersion;
  104. (void)platform;
  105. (void)condition;
  106. #else
  107. ULONGLONG cm = 0;
  108. struct OUR_OSVERSIONINFOEXW osver;
  109. BYTE majorCondition;
  110. BYTE minorCondition;
  111. BYTE buildCondition;
  112. BYTE spMajorCondition;
  113. BYTE spMinorCondition;
  114. DWORD dwTypeMask = VER_MAJORVERSION | VER_MINORVERSION |
  115. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR;
  116. typedef LONG (APIENTRY *RTLVERIFYVERSIONINFO_FN)
  117. (struct OUR_OSVERSIONINFOEXW *, ULONG, ULONGLONG);
  118. static RTLVERIFYVERSIONINFO_FN pRtlVerifyVersionInfo;
  119. static bool onetime = TRUE; /* safe because first call is during init */
  120. if(onetime) {
  121. #if defined(__clang__) && __clang_major__ >= 16
  122. #pragma clang diagnostic push
  123. #pragma clang diagnostic ignored "-Wcast-function-type-strict"
  124. #endif
  125. pRtlVerifyVersionInfo = CURLX_FUNCTION_CAST(RTLVERIFYVERSIONINFO_FN,
  126. GetProcAddress(GetModuleHandleA("ntdll"), "RtlVerifyVersionInfo"));
  127. #if defined(__clang__) && __clang_major__ >= 16
  128. #pragma clang diagnostic pop
  129. #endif
  130. onetime = FALSE;
  131. }
  132. switch(condition) {
  133. case VERSION_LESS_THAN:
  134. majorCondition = VER_LESS;
  135. minorCondition = VER_LESS;
  136. buildCondition = VER_LESS;
  137. spMajorCondition = VER_LESS_EQUAL;
  138. spMinorCondition = VER_LESS_EQUAL;
  139. break;
  140. case VERSION_LESS_THAN_EQUAL:
  141. majorCondition = VER_LESS_EQUAL;
  142. minorCondition = VER_LESS_EQUAL;
  143. buildCondition = VER_LESS_EQUAL;
  144. spMajorCondition = VER_LESS_EQUAL;
  145. spMinorCondition = VER_LESS_EQUAL;
  146. break;
  147. case VERSION_EQUAL:
  148. majorCondition = VER_EQUAL;
  149. minorCondition = VER_EQUAL;
  150. buildCondition = VER_EQUAL;
  151. spMajorCondition = VER_GREATER_EQUAL;
  152. spMinorCondition = VER_GREATER_EQUAL;
  153. break;
  154. case VERSION_GREATER_THAN_EQUAL:
  155. majorCondition = VER_GREATER_EQUAL;
  156. minorCondition = VER_GREATER_EQUAL;
  157. buildCondition = VER_GREATER_EQUAL;
  158. spMajorCondition = VER_GREATER_EQUAL;
  159. spMinorCondition = VER_GREATER_EQUAL;
  160. break;
  161. case VERSION_GREATER_THAN:
  162. majorCondition = VER_GREATER;
  163. minorCondition = VER_GREATER;
  164. buildCondition = VER_GREATER;
  165. spMajorCondition = VER_GREATER_EQUAL;
  166. spMinorCondition = VER_GREATER_EQUAL;
  167. break;
  168. default:
  169. return FALSE;
  170. }
  171. memset(&osver, 0, sizeof(osver));
  172. osver.dwOSVersionInfoSize = sizeof(osver);
  173. osver.dwMajorVersion = majorVersion;
  174. osver.dwMinorVersion = minorVersion;
  175. osver.dwBuildNumber = buildVersion;
  176. if(platform == PLATFORM_WINDOWS)
  177. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  178. else if(platform == PLATFORM_WINNT)
  179. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  180. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  181. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  182. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  183. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  184. if(platform != PLATFORM_DONT_CARE) {
  185. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  186. dwTypeMask |= VER_PLATFORMID;
  187. }
  188. /* Later versions of Windows have version functions that may not return the
  189. real version of Windows unless the application is so manifested. We prefer
  190. the real version always, so we use the Rtl variant of the function when
  191. possible. Note though the function signatures have underlying fundamental
  192. types that are the same, the return values are different. */
  193. if(pRtlVerifyVersionInfo)
  194. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  195. else
  196. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver, dwTypeMask, cm);
  197. /* Compare the build number separately. VerifyVersionInfo normally compares
  198. major.minor in hierarchical order (eg 1.9 is less than 2.0) but does not
  199. do the same for build (eg 1.9 build 222 is not less than 2.0 build 111).
  200. Build comparison is only needed when build numbers are equal (eg 1.9 is
  201. always less than 2.0 so build comparison is not needed). */
  202. if(matched && buildVersion &&
  203. (condition == VERSION_EQUAL ||
  204. ((condition == VERSION_GREATER_THAN_EQUAL ||
  205. condition == VERSION_LESS_THAN_EQUAL) &&
  206. curlx_verify_windows_version(majorVersion, minorVersion, 0,
  207. platform, VERSION_EQUAL)))) {
  208. cm = VerSetConditionMask(0, VER_BUILDNUMBER, buildCondition);
  209. dwTypeMask = VER_BUILDNUMBER;
  210. if(pRtlVerifyVersionInfo)
  211. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  212. else
  213. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver,
  214. dwTypeMask, cm);
  215. }
  216. #endif
  217. return matched;
  218. }
  219. #endif /* _WIN32 */