VirtualBox

source: vbox/trunk/src/libs/curl-7.64.0/lib/system_win32.c@ 86643

Last change on this file since 86643 was 85671, checked in by vboxsync, 4 years ago

Export out internal curl copy to make it a lot simpler to build VBox (OSE) on Windows. bugref:9814

  • Property svn:eol-style set to native
File size: 10.4 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2016 - 2017, 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.haxx.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 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#if defined(WIN32)
26
27#include <curl/curl.h>
28#include "system_win32.h"
29#include "warnless.h"
30
31/* The last #include files should be: */
32#include "curl_memory.h"
33#include "memdebug.h"
34
35#if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
36 defined(USE_WINSOCK))
37
38
39#if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
40#define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
41#endif
42
43#if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
44#define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
45#endif
46
47/* We use our own typedef here since some headers might lack these */
48typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
49
50/* See function definitions in winbase.h */
51#ifdef UNICODE
52# ifdef _WIN32_WCE
53# define LOADLIBARYEX L"LoadLibraryExW"
54# else
55# define LOADLIBARYEX "LoadLibraryExW"
56# endif
57#else
58# define LOADLIBARYEX "LoadLibraryExA"
59#endif
60
61#endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
62
63/*
64 * Curl_verify_windows_version()
65 *
66 * This is used to verify if we are running on a specific windows version.
67 *
68 * Parameters:
69 *
70 * majorVersion [in] - The major version number.
71 * minorVersion [in] - The minor version number.
72 * platform [in] - The optional platform identifier.
73 * condition [in] - The test condition used to specifier whether we are
74 * checking a version less then, equal to or greater than
75 * what is specified in the major and minor version
76 * numbers.
77 *
78 * Returns TRUE if matched; otherwise FALSE.
79 */
80bool Curl_verify_windows_version(const unsigned int majorVersion,
81 const unsigned int minorVersion,
82 const PlatformIdentifier platform,
83 const VersionCondition condition)
84{
85 bool matched = FALSE;
86
87#if defined(CURL_WINDOWS_APP)
88 /* We have no way to determine the Windows version from Windows apps,
89 so let's assume we're running on the target Windows version. */
90 const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
91 const WORD targetVersion = (WORD)_WIN32_WINNT;
92
93 switch(condition) {
94 case VERSION_LESS_THAN:
95 matched = targetVersion < fullVersion;
96 break;
97
98 case VERSION_LESS_THAN_EQUAL:
99 matched = targetVersion <= fullVersion;
100 break;
101
102 case VERSION_EQUAL:
103 matched = targetVersion == fullVersion;
104 break;
105
106 case VERSION_GREATER_THAN_EQUAL:
107 matched = targetVersion >= fullVersion;
108 break;
109
110 case VERSION_GREATER_THAN:
111 matched = targetVersion > fullVersion;
112 break;
113 }
114
115 if(matched && (platform == PLATFORM_WINDOWS)) {
116 /* we're always running on PLATFORM_WINNT */
117 matched = FALSE;
118 }
119#elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
120 (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
121 OSVERSIONINFO osver;
122
123 memset(&osver, 0, sizeof(osver));
124 osver.dwOSVersionInfoSize = sizeof(osver);
125
126 /* Find out Windows version */
127 if(GetVersionEx(&osver)) {
128 /* Verify the Operating System version number */
129 switch(condition) {
130 case VERSION_LESS_THAN:
131 if(osver.dwMajorVersion < majorVersion ||
132 (osver.dwMajorVersion == majorVersion &&
133 osver.dwMinorVersion < minorVersion))
134 matched = TRUE;
135 break;
136
137 case VERSION_LESS_THAN_EQUAL:
138 if(osver.dwMajorVersion < majorVersion ||
139 (osver.dwMajorVersion == majorVersion &&
140 osver.dwMinorVersion <= minorVersion))
141 matched = TRUE;
142 break;
143
144 case VERSION_EQUAL:
145 if(osver.dwMajorVersion == majorVersion &&
146 osver.dwMinorVersion == minorVersion)
147 matched = TRUE;
148 break;
149
150 case VERSION_GREATER_THAN_EQUAL:
151 if(osver.dwMajorVersion > majorVersion ||
152 (osver.dwMajorVersion == majorVersion &&
153 osver.dwMinorVersion >= minorVersion))
154 matched = TRUE;
155 break;
156
157 case VERSION_GREATER_THAN:
158 if(osver.dwMajorVersion > majorVersion ||
159 (osver.dwMajorVersion == majorVersion &&
160 osver.dwMinorVersion > minorVersion))
161 matched = TRUE;
162 break;
163 }
164
165 /* Verify the platform identifier (if necessary) */
166 if(matched) {
167 switch(platform) {
168 case PLATFORM_WINDOWS:
169 if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
170 matched = FALSE;
171 break;
172
173 case PLATFORM_WINNT:
174 if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
175 matched = FALSE;
176
177 default: /* like platform == PLATFORM_DONT_CARE */
178 break;
179 }
180 }
181 }
182#else
183 ULONGLONG cm = 0;
184 OSVERSIONINFOEX osver;
185 BYTE majorCondition;
186 BYTE minorCondition;
187 BYTE spMajorCondition;
188 BYTE spMinorCondition;
189
190 switch(condition) {
191 case VERSION_LESS_THAN:
192 majorCondition = VER_LESS;
193 minorCondition = VER_LESS;
194 spMajorCondition = VER_LESS_EQUAL;
195 spMinorCondition = VER_LESS_EQUAL;
196 break;
197
198 case VERSION_LESS_THAN_EQUAL:
199 majorCondition = VER_LESS_EQUAL;
200 minorCondition = VER_LESS_EQUAL;
201 spMajorCondition = VER_LESS_EQUAL;
202 spMinorCondition = VER_LESS_EQUAL;
203 break;
204
205 case VERSION_EQUAL:
206 majorCondition = VER_EQUAL;
207 minorCondition = VER_EQUAL;
208 spMajorCondition = VER_GREATER_EQUAL;
209 spMinorCondition = VER_GREATER_EQUAL;
210 break;
211
212 case VERSION_GREATER_THAN_EQUAL:
213 majorCondition = VER_GREATER_EQUAL;
214 minorCondition = VER_GREATER_EQUAL;
215 spMajorCondition = VER_GREATER_EQUAL;
216 spMinorCondition = VER_GREATER_EQUAL;
217 break;
218
219 case VERSION_GREATER_THAN:
220 majorCondition = VER_GREATER;
221 minorCondition = VER_GREATER;
222 spMajorCondition = VER_GREATER_EQUAL;
223 spMinorCondition = VER_GREATER_EQUAL;
224 break;
225
226 default:
227 return FALSE;
228 }
229
230 memset(&osver, 0, sizeof(osver));
231 osver.dwOSVersionInfoSize = sizeof(osver);
232 osver.dwMajorVersion = majorVersion;
233 osver.dwMinorVersion = minorVersion;
234 if(platform == PLATFORM_WINDOWS)
235 osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
236 else if(platform == PLATFORM_WINNT)
237 osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
238
239 cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
240 cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
241 cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
242 cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
243 if(platform != PLATFORM_DONT_CARE)
244 cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
245
246 if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
247 VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR),
248 cm))
249 matched = TRUE;
250#endif
251
252 return matched;
253}
254
255#if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
256 defined(USE_WINSOCK))
257
258/*
259 * Curl_load_library()
260 *
261 * This is used to dynamically load DLLs using the most secure method available
262 * for the version of Windows that we are running on.
263 *
264 * Parameters:
265 *
266 * filename [in] - The filename or full path of the DLL to load. If only the
267 * filename is passed then the DLL will be loaded from the
268 * Windows system directory.
269 *
270 * Returns the handle of the module on success; otherwise NULL.
271 */
272HMODULE Curl_load_library(LPCTSTR filename)
273{
274 HMODULE hModule = NULL;
275 LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
276
277 /* Get a handle to kernel32 so we can access it's functions at runtime */
278 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
279 if(!hKernel32)
280 return NULL;
281
282 /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
283 and above */
284 pLoadLibraryEx =
285 CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
286 (GetProcAddress(hKernel32, LOADLIBARYEX)));
287
288 /* Detect if there's already a path in the filename and load the library if
289 there is. Note: Both back slashes and forward slashes have been supported
290 since the earlier days of DOS at an API level although they are not
291 supported by command prompt */
292 if(_tcspbrk(filename, TEXT("\\/"))) {
293 /** !checksrc! disable BANNEDFUNC 1 **/
294 hModule = pLoadLibraryEx ?
295 pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
296 LoadLibrary(filename);
297 }
298 /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
299 supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
300 Server 2008 R2 with this patch or natively on Windows 8 and above */
301 else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
302 /* Load the DLL from the Windows system directory */
303 hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
304 }
305 else {
306 /* Attempt to get the Windows system path */
307 UINT systemdirlen = GetSystemDirectory(NULL, 0);
308 if(systemdirlen) {
309 /* Allocate space for the full DLL path (Room for the null terminator
310 is included in systemdirlen) */
311 size_t filenamelen = _tcslen(filename);
312 TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
313 if(path && GetSystemDirectory(path, systemdirlen)) {
314 /* Calculate the full DLL path */
315 _tcscpy(path + _tcslen(path), TEXT("\\"));
316 _tcscpy(path + _tcslen(path), filename);
317
318 /* Load the DLL from the Windows system directory */
319 /** !checksrc! disable BANNEDFUNC 1 **/
320 hModule = pLoadLibraryEx ?
321 pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
322 LoadLibrary(path);
323
324 }
325 free(path);
326 }
327 }
328
329 return hModule;
330}
331
332#endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
333
334#endif /* WIN32 */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette