version_win32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 #include files should be: */
  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 then, 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. #if defined(CURL_WINDOWS_APP)
  74. (void)buildVersion;
  75. /* We have no way to determine the Windows version from Windows apps,
  76. so let's assume we're running on the target Windows version. */
  77. const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
  78. const WORD targetVersion = (WORD)_WIN32_WINNT;
  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're always running on PLATFORM_WINNT */
  98. matched = FALSE;
  99. }
  100. #elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  101. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  102. OSVERSIONINFO osver;
  103. memset(&osver, 0, sizeof(osver));
  104. osver.dwOSVersionInfoSize = sizeof(osver);
  105. /* Find out Windows version */
  106. if(GetVersionEx(&osver)) {
  107. /* Verify the Operating System version number */
  108. switch(condition) {
  109. case VERSION_LESS_THAN:
  110. if(osver.dwMajorVersion < majorVersion ||
  111. (osver.dwMajorVersion == majorVersion &&
  112. osver.dwMinorVersion < minorVersion) ||
  113. (buildVersion != 0 &&
  114. (osver.dwMajorVersion == majorVersion &&
  115. osver.dwMinorVersion == minorVersion &&
  116. osver.dwBuildNumber < buildVersion)))
  117. matched = TRUE;
  118. break;
  119. case VERSION_LESS_THAN_EQUAL:
  120. if(osver.dwMajorVersion < majorVersion ||
  121. (osver.dwMajorVersion == majorVersion &&
  122. osver.dwMinorVersion < minorVersion) ||
  123. (osver.dwMajorVersion == majorVersion &&
  124. osver.dwMinorVersion == minorVersion &&
  125. (buildVersion == 0 ||
  126. osver.dwBuildNumber <= buildVersion)))
  127. matched = TRUE;
  128. break;
  129. case VERSION_EQUAL:
  130. if(osver.dwMajorVersion == majorVersion &&
  131. osver.dwMinorVersion == minorVersion &&
  132. (buildVersion == 0 ||
  133. osver.dwBuildNumber == buildVersion))
  134. matched = TRUE;
  135. break;
  136. case VERSION_GREATER_THAN_EQUAL:
  137. if(osver.dwMajorVersion > majorVersion ||
  138. (osver.dwMajorVersion == majorVersion &&
  139. osver.dwMinorVersion > minorVersion) ||
  140. (osver.dwMajorVersion == majorVersion &&
  141. osver.dwMinorVersion == minorVersion &&
  142. (buildVersion == 0 ||
  143. osver.dwBuildNumber >= buildVersion)))
  144. matched = TRUE;
  145. break;
  146. case VERSION_GREATER_THAN:
  147. if(osver.dwMajorVersion > majorVersion ||
  148. (osver.dwMajorVersion == majorVersion &&
  149. osver.dwMinorVersion > minorVersion) ||
  150. (buildVersion != 0 &&
  151. (osver.dwMajorVersion == majorVersion &&
  152. osver.dwMinorVersion == minorVersion &&
  153. osver.dwBuildNumber > buildVersion)))
  154. matched = TRUE;
  155. break;
  156. }
  157. /* Verify the platform identifier (if necessary) */
  158. if(matched) {
  159. switch(platform) {
  160. case PLATFORM_WINDOWS:
  161. if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  162. matched = FALSE;
  163. break;
  164. case PLATFORM_WINNT:
  165. if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
  166. matched = FALSE;
  167. break;
  168. default: /* like platform == PLATFORM_DONT_CARE */
  169. break;
  170. }
  171. }
  172. }
  173. #else
  174. ULONGLONG cm = 0;
  175. struct OUR_OSVERSIONINFOEXW osver;
  176. BYTE majorCondition;
  177. BYTE minorCondition;
  178. BYTE buildCondition;
  179. BYTE spMajorCondition;
  180. BYTE spMinorCondition;
  181. DWORD dwTypeMask = VER_MAJORVERSION | VER_MINORVERSION |
  182. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR;
  183. typedef LONG (APIENTRY *RTLVERIFYVERSIONINFO_FN)
  184. (struct OUR_OSVERSIONINFOEXW *, ULONG, ULONGLONG);
  185. static RTLVERIFYVERSIONINFO_FN pRtlVerifyVersionInfo;
  186. static bool onetime = true; /* safe because first call is during init */
  187. if(onetime) {
  188. pRtlVerifyVersionInfo = CURLX_FUNCTION_CAST(RTLVERIFYVERSIONINFO_FN,
  189. (GetProcAddress(GetModuleHandleA("ntdll"), "RtlVerifyVersionInfo")));
  190. onetime = false;
  191. }
  192. switch(condition) {
  193. case VERSION_LESS_THAN:
  194. majorCondition = VER_LESS;
  195. minorCondition = VER_LESS;
  196. buildCondition = VER_LESS;
  197. spMajorCondition = VER_LESS_EQUAL;
  198. spMinorCondition = VER_LESS_EQUAL;
  199. break;
  200. case VERSION_LESS_THAN_EQUAL:
  201. majorCondition = VER_LESS_EQUAL;
  202. minorCondition = VER_LESS_EQUAL;
  203. buildCondition = VER_LESS_EQUAL;
  204. spMajorCondition = VER_LESS_EQUAL;
  205. spMinorCondition = VER_LESS_EQUAL;
  206. break;
  207. case VERSION_EQUAL:
  208. majorCondition = VER_EQUAL;
  209. minorCondition = VER_EQUAL;
  210. buildCondition = VER_EQUAL;
  211. spMajorCondition = VER_GREATER_EQUAL;
  212. spMinorCondition = VER_GREATER_EQUAL;
  213. break;
  214. case VERSION_GREATER_THAN_EQUAL:
  215. majorCondition = VER_GREATER_EQUAL;
  216. minorCondition = VER_GREATER_EQUAL;
  217. buildCondition = VER_GREATER_EQUAL;
  218. spMajorCondition = VER_GREATER_EQUAL;
  219. spMinorCondition = VER_GREATER_EQUAL;
  220. break;
  221. case VERSION_GREATER_THAN:
  222. majorCondition = VER_GREATER;
  223. minorCondition = VER_GREATER;
  224. buildCondition = VER_GREATER;
  225. spMajorCondition = VER_GREATER_EQUAL;
  226. spMinorCondition = VER_GREATER_EQUAL;
  227. break;
  228. default:
  229. return FALSE;
  230. }
  231. memset(&osver, 0, sizeof(osver));
  232. osver.dwOSVersionInfoSize = sizeof(osver);
  233. osver.dwMajorVersion = majorVersion;
  234. osver.dwMinorVersion = minorVersion;
  235. osver.dwBuildNumber = buildVersion;
  236. if(platform == PLATFORM_WINDOWS)
  237. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  238. else if(platform == PLATFORM_WINNT)
  239. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  240. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  241. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  242. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  243. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  244. if(platform != PLATFORM_DONT_CARE) {
  245. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  246. dwTypeMask |= VER_PLATFORMID;
  247. }
  248. /* Later versions of Windows have version functions that may not return the
  249. real version of Windows unless the application is so manifested. We prefer
  250. the real version always, so we use the Rtl variant of the function when
  251. possible. Note though the function signatures have underlying fundamental
  252. types that are the same, the return values are different. */
  253. if(pRtlVerifyVersionInfo)
  254. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  255. else
  256. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver, dwTypeMask, cm);
  257. /* Compare the build number separately. VerifyVersionInfo normally compares
  258. major.minor in hierarchical order (eg 1.9 is less than 2.0) but does not
  259. do the same for build (eg 1.9 build 222 is not less than 2.0 build 111).
  260. Build comparison is only needed when build numbers are equal (eg 1.9 is
  261. always less than 2.0 so build comparison is not needed). */
  262. if(matched && buildVersion &&
  263. (condition == VERSION_EQUAL ||
  264. ((condition == VERSION_GREATER_THAN_EQUAL ||
  265. condition == VERSION_LESS_THAN_EQUAL) &&
  266. curlx_verify_windows_version(majorVersion, minorVersion, 0,
  267. platform, VERSION_EQUAL)))) {
  268. cm = VerSetConditionMask(0, VER_BUILDNUMBER, buildCondition);
  269. dwTypeMask = VER_BUILDNUMBER;
  270. if(pRtlVerifyVersionInfo)
  271. matched = !pRtlVerifyVersionInfo(&osver, dwTypeMask, cm);
  272. else
  273. matched = !!VerifyVersionInfoW((OSVERSIONINFOEXW *)&osver,
  274. dwTypeMask, cm);
  275. }
  276. #endif
  277. return matched;
  278. }
  279. #endif /* _WIN32 */