version_win32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
  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. #if defined(_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. #ifdef BUILDING_LIBCURL
  31. #include "curl_memory.h"
  32. #endif
  33. #include "memdebug.h"
  34. /* This Unicode version struct works for VerifyVersionInfoW (OSVERSIONINFOEXW)
  35. and RtlVerifyVersionInfo (RTLOSVERSIONINFOEXW) */
  36. struct OUR_OSVERSIONINFOEXW {
  37. ULONG dwOSVersionInfoSize;
  38. ULONG dwMajorVersion;
  39. ULONG dwMinorVersion;
  40. ULONG dwBuildNumber;
  41. ULONG dwPlatformId;
  42. WCHAR szCSDVersion[128];
  43. USHORT wServicePackMajor;
  44. USHORT wServicePackMinor;
  45. USHORT wSuiteMask;
  46. UCHAR wProductType;
  47. UCHAR wReserved;
  48. };
  49. /*
  50. * curlx_verify_windows_version()
  51. *
  52. * This is used to verify if we are running on a specific Windows version.
  53. *
  54. * Parameters:
  55. *
  56. * majorVersion [in] - The major version number.
  57. * minorVersion [in] - The minor version number.
  58. * buildVersion [in] - The build version number. If 0, this parameter is
  59. * ignored.
  60. * platform [in] - The optional platform identifier.
  61. * condition [in] - The test condition used to specifier whether we are
  62. * checking a version less than, equal to or greater than
  63. * what is specified in the major and minor version
  64. * numbers.
  65. *
  66. * Returns TRUE if matched; otherwise FALSE.
  67. */
  68. bool curlx_verify_windows_version(const unsigned int majorVersion,
  69. const unsigned int minorVersion,
  70. const unsigned int buildVersion,
  71. const PlatformIdentifier platform,
  72. const VersionCondition condition)
  73. {
  74. bool matched = FALSE;
  75. #if defined(CURL_WINDOWS_UWP)
  76. /* We have no way to determine the Windows version from Windows apps,
  77. so let's assume we are running on the target Windows version. */
  78. const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
  79. const WORD targetVersion = (WORD)_WIN32_WINNT;
  80. (void)buildVersion;
  81. switch(condition) {
  82. case VERSION_LESS_THAN:
  83. matched = targetVersion < fullVersion;
  84. break;
  85. case VERSION_LESS_THAN_EQUAL:
  86. matched = targetVersion <= fullVersion;
  87. break;
  88. case VERSION_EQUAL:
  89. matched = targetVersion == fullVersion;
  90. break;
  91. case VERSION_GREATER_THAN_EQUAL:
  92. matched = targetVersion >= fullVersion;
  93. break;
  94. case VERSION_GREATER_THAN:
  95. matched = targetVersion > fullVersion;
  96. break;
  97. }
  98. if(matched && (platform == PLATFORM_WINDOWS)) {
  99. /* we are always running on PLATFORM_WINNT */
  100. matched = FALSE;
  101. }
  102. #elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  103. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  104. OSVERSIONINFO osver;
  105. memset(&osver, 0, sizeof(osver));
  106. osver.dwOSVersionInfoSize = sizeof(osver);
  107. /* Find out Windows version */
  108. if(GetVersionEx(&osver)) {
  109. /* Verify the Operating System version number */
  110. switch(condition) {
  111. case VERSION_LESS_THAN:
  112. if(osver.dwMajorVersion < majorVersion ||
  113. (osver.dwMajorVersion == majorVersion &&
  114. osver.dwMinorVersion < minorVersion) ||
  115. (buildVersion != 0 &&
  116. (osver.dwMajorVersion == majorVersion &&
  117. osver.dwMinorVersion == minorVersion &&
  118. osver.dwBuildNumber < buildVersion)))
  119. matched = TRUE;
  120. break;
  121. case VERSION_LESS_THAN_EQUAL:
  122. if(osver.dwMajorVersion < majorVersion ||
  123. (osver.dwMajorVersion == majorVersion &&
  124. osver.dwMinorVersion < minorVersion) ||
  125. (osver.dwMajorVersion == majorVersion &&
  126. osver.dwMinorVersion == minorVersion &&
  127. (buildVersion == 0 ||
  128. osver.dwBuildNumber <= buildVersion)))
  129. matched = TRUE;
  130. break;
  131. case VERSION_EQUAL:
  132. if(osver.dwMajorVersion == majorVersion &&
  133. osver.dwMinorVersion == minorVersion &&
  134. (buildVersion == 0 ||
  135. osver.dwBuildNumber == buildVersion))
  136. matched = TRUE;
  137. break;
  138. case VERSION_GREATER_THAN_EQUAL:
  139. if(osver.dwMajorVersion > majorVersion ||
  140. (osver.dwMajorVersion == majorVersion &&
  141. osver.dwMinorVersion > minorVersion) ||
  142. (osver.dwMajorVersion == majorVersion &&
  143. osver.dwMinorVersion == minorVersion &&
  144. (buildVersion == 0 ||
  145. osver.dwBuildNumber >= buildVersion)))
  146. matched = TRUE;
  147. break;
  148. case VERSION_GREATER_THAN:
  149. if(osver.dwMajorVersion > majorVersion ||
  150. (osver.dwMajorVersion == majorVersion &&
  151. osver.dwMinorVersion > minorVersion) ||
  152. (buildVersion != 0 &&
  153. (osver.dwMajorVersion == majorVersion &&
  154. osver.dwMinorVersion == minorVersion &&
  155. osver.dwBuildNumber > buildVersion)))
  156. matched = TRUE;
  157. break;
  158. }
  159. /* Verify the platform identifier (if necessary) */
  160. if(matched) {
  161. switch(platform) {
  162. case PLATFORM_WINDOWS:
  163. if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  164. matched = FALSE;
  165. break;
  166. case PLATFORM_WINNT:
  167. if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
  168. matched = FALSE;
  169. break;
  170. default: /* like platform == PLATFORM_DONT_CARE */
  171. break;
  172. }
  173. }
  174. }
  175. #else
  176. ULONGLONG cm = 0;
  177. struct OUR_OSVERSIONINFOEXW osver;
  178. BYTE majorCondition;
  179. BYTE minorCondition;
  180. BYTE buildCondition;
  181. BYTE spMajorCondition;
  182. BYTE spMinorCondition;
  183. DWORD dwTypeMask = VER_MAJORVERSION | VER_MINORVERSION |
  184. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR;
  185. typedef LONG (APIENTRY *RTLVERIFYVERSIONINFO_FN)
  186. (struct OUR_OSVERSIONINFOEXW *, ULONG, ULONGLONG);
  187. static RTLVERIFYVERSIONINFO_FN pRtlVerifyVersionInfo;
  188. static bool onetime = TRUE; /* safe because first call is during init */
  189. if(onetime) {
  190. pRtlVerifyVersionInfo = CURLX_FUNCTION_CAST(RTLVERIFYVERSIONINFO_FN,
  191. (GetProcAddress(GetModuleHandleA("ntdll"), "RtlVerifyVersionInfo")));
  192. onetime = FALSE;
  193. }
  194. switch(condition) {
  195. case VERSION_LESS_THAN:
  196. majorCondition = VER_LESS;
  197. minorCondition = VER_LESS;
  198. buildCondition = VER_LESS;
  199. spMajorCondition = VER_LESS_EQUAL;
  200. spMinorCondition = VER_LESS_EQUAL;
  201. break;
  202. case VERSION_LESS_THAN_EQUAL:
  203. majorCondition = VER_LESS_EQUAL;
  204. minorCondition = VER_LESS_EQUAL;
  205. buildCondition = VER_LESS_EQUAL;
  206. spMajorCondition = VER_LESS_EQUAL;
  207. spMinorCondition = VER_LESS_EQUAL;
  208. break;
  209. case VERSION_EQUAL:
  210. majorCondition = VER_EQUAL;
  211. minorCondition = VER_EQUAL;
  212. buildCondition = VER_EQUAL;
  213. spMajorCondition = VER_GREATER_EQUAL;
  214. spMinorCondition = VER_GREATER_EQUAL;
  215. break;
  216. case VERSION_GREATER_THAN_EQUAL:
  217. majorCondition = VER_GREATER_EQUAL;
  218. minorCondition = VER_GREATER_EQUAL;
  219. buildCondition = VER_GREATER_EQUAL;
  220. spMajorCondition = VER_GREATER_EQUAL;
  221. spMinorCondition = VER_GREATER_EQUAL;
  222. break;
  223. case VERSION_GREATER_THAN:
  224. majorCondition = VER_GREATER;
  225. minorCondition = VER_GREATER;
  226. buildCondition = VER_GREATER;
  227. spMajorCondition = VER_GREATER_EQUAL;
  228. spMinorCondition = VER_GREATER_EQUAL;
  229. break;
  230. default:
  231. return FALSE;
  232. }
  233. memset(&osver, 0, sizeof(osver));
  234. osver.dwOSVersionInfoSize = sizeof(osver);
  235. osver.dwMajorVersion = majorVersion;
  236. osver.dwMinorVersion = minorVersion;
  237. osver.dwBuildNumber = buildVersion;
  238. if(platform == PLATFORM_WINDOWS)
  239. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  240. else if(platform == PLATFORM_WINNT)
  241. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  242. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  243. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  244. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  245. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  246. if(platform != PLATFORM_DONT_CARE) {
  247. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  248. dwTypeMask |= VER_PLATFORMID;
  249. }
  250. /* Later versions of Windows have version functions that may not return the
  251. real version of Windows unless the application is so manifested. We prefer
  252. the real version always, so we use the Rtl variant of the function when
  253. possible. Note though the function signatures have underlying fundamental
  254. types that are the same, the return values are different. */
  255. if(pRtlVerifyVersionInfo)
  256. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  257. else
  258. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver, dwTypeMask, cm);
  259. /* Compare the build number separately. VerifyVersionInfo normally compares
  260. major.minor in hierarchical order (eg 1.9 is less than 2.0) but does not
  261. do the same for build (eg 1.9 build 222 is not less than 2.0 build 111).
  262. Build comparison is only needed when build numbers are equal (eg 1.9 is
  263. always less than 2.0 so build comparison is not needed). */
  264. if(matched && buildVersion &&
  265. (condition == VERSION_EQUAL ||
  266. ((condition == VERSION_GREATER_THAN_EQUAL ||
  267. condition == VERSION_LESS_THAN_EQUAL) &&
  268. curlx_verify_windows_version(majorVersion, minorVersion, 0,
  269. platform, VERSION_EQUAL)))) {
  270. cm = VerSetConditionMask(0, VER_BUILDNUMBER, buildCondition);
  271. dwTypeMask = VER_BUILDNUMBER;
  272. if(pRtlVerifyVersionInfo)
  273. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  274. else
  275. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver,
  276. dwTypeMask, cm);
  277. }
  278. #endif
  279. return matched;
  280. }
  281. #endif /* _WIN32 */