system_win32.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2016, Steve Holme, <steve_holme@hotmail.com>.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #if defined(WIN32)
00026 
00027 #include <curl/curl.h>
00028 #include "system_win32.h"
00029 
00030 /* The last #include files should be: */
00031 #include "curl_memory.h"
00032 #include "memdebug.h"
00033 
00034 #if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
00035                                   defined(USE_WINSOCK))
00036 
00037 
00038 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
00039 #define LOAD_WITH_ALTERED_SEARCH_PATH  0x00000008
00040 #endif
00041 
00042 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
00043 #define LOAD_LIBRARY_SEARCH_SYSTEM32   0x00000800
00044 #endif
00045 
00046 /* We use our own typedef here since some headers might lack these */
00047 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
00048 
00049 /* See function definitions in winbase.h */
00050 #ifdef UNICODE
00051 #  ifdef _WIN32_WCE
00052 #    define LOADLIBARYEX  L"LoadLibraryExW"
00053 #  else
00054 #    define LOADLIBARYEX  "LoadLibraryExW"
00055 #  endif
00056 #else
00057 #  define LOADLIBARYEX    "LoadLibraryExA"
00058 #endif
00059 
00060 #endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
00061 
00062 /*
00063  * Curl_verify_windows_version()
00064  *
00065  * This is used to verify if we are running on a specific windows version.
00066  *
00067  * Parameters:
00068  *
00069  * majorVersion [in] - The major version number.
00070  * minorVersion [in] - The minor version number.
00071  * platform     [in] - The optional platform identifer.
00072  * condition    [in] - The test condition used to specifier whether we are
00073  *                     checking a version less then, equal to or greater than
00074  *                     what is specified in the major and minor version
00075  *                     numbers.
00076  *
00077  * Returns TRUE if matched; otherwise FALSE.
00078  */
00079 bool Curl_verify_windows_version(const unsigned int majorVersion,
00080                                  const unsigned int minorVersion,
00081                                  const PlatformIdentifier platform,
00082                                  const VersionCondition condition)
00083 {
00084   bool matched = FALSE;
00085 
00086 #if defined(CURL_WINDOWS_APP)
00087   /* We have no way to determine the Windows version from Windows apps,
00088      so let's assume we're running on the target Windows version. */
00089   const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
00090   const WORD targetVersion = (WORD)_WIN32_WINNT;
00091 
00092   switch(condition) {
00093   case VERSION_LESS_THAN:
00094     matched = targetVersion < fullVersion;
00095     break;
00096 
00097   case VERSION_LESS_THAN_EQUAL:
00098     matched = targetVersion <= fullVersion;
00099     break;
00100 
00101   case VERSION_EQUAL:
00102     matched = targetVersion == fullVersion;
00103     break;
00104 
00105   case VERSION_GREATER_THAN_EQUAL:
00106     matched = targetVersion >= fullVersion;
00107     break;
00108 
00109   case VERSION_GREATER_THAN:
00110     matched = targetVersion > fullVersion;
00111     break;
00112   }
00113 
00114   if(matched && (platform == PLATFORM_WINDOWS)) {
00115     /* we're always running on PLATFORM_WINNT */
00116     matched = FALSE;
00117   }
00118 #elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
00119     (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
00120   OSVERSIONINFO osver;
00121 
00122   memset(&osver, 0, sizeof(osver));
00123   osver.dwOSVersionInfoSize = sizeof(osver);
00124 
00125   /* Find out Windows version */
00126   if(GetVersionEx(&osver)) {
00127     /* Verify the Operating System version number */
00128     switch(condition) {
00129     case VERSION_LESS_THAN:
00130       if(osver.dwMajorVersion < majorVersion ||
00131         (osver.dwMajorVersion == majorVersion &&
00132          osver.dwMinorVersion < minorVersion))
00133         matched = TRUE;
00134       break;
00135 
00136     case VERSION_LESS_THAN_EQUAL:
00137       if(osver.dwMajorVersion <= majorVersion &&
00138          osver.dwMinorVersion <= minorVersion)
00139         matched = TRUE;
00140       break;
00141 
00142     case VERSION_EQUAL:
00143       if(osver.dwMajorVersion == majorVersion &&
00144          osver.dwMinorVersion == minorVersion)
00145         matched = TRUE;
00146       break;
00147 
00148     case VERSION_GREATER_THAN_EQUAL:
00149       if(osver.dwMajorVersion >= majorVersion &&
00150          osver.dwMinorVersion >= minorVersion)
00151         matched = TRUE;
00152       break;
00153 
00154     case VERSION_GREATER_THAN:
00155       if(osver.dwMajorVersion > majorVersion ||
00156         (osver.dwMajorVersion == majorVersion &&
00157          osver.dwMinorVersion > minorVersion))
00158         matched = TRUE;
00159       break;
00160     }
00161 
00162     /* Verify the platform identifier (if necessary) */
00163     if(matched) {
00164       switch(platform) {
00165       case PLATFORM_WINDOWS:
00166         if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
00167           matched = FALSE;
00168         break;
00169 
00170       case PLATFORM_WINNT:
00171         if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
00172           matched = FALSE;
00173 
00174       default: /* like platform == PLATFORM_DONT_CARE */
00175         break;
00176       }
00177     }
00178   }
00179 #else
00180   ULONGLONG cm = 0;
00181   OSVERSIONINFOEX osver;
00182   BYTE majorCondition;
00183   BYTE minorCondition;
00184   BYTE spMajorCondition;
00185   BYTE spMinorCondition;
00186 
00187   switch(condition) {
00188   case VERSION_LESS_THAN:
00189     majorCondition = VER_LESS;
00190     minorCondition = VER_LESS;
00191     spMajorCondition = VER_LESS_EQUAL;
00192     spMinorCondition = VER_LESS_EQUAL;
00193     break;
00194 
00195   case VERSION_LESS_THAN_EQUAL:
00196     majorCondition = VER_LESS_EQUAL;
00197     minorCondition = VER_LESS_EQUAL;
00198     spMajorCondition = VER_LESS_EQUAL;
00199     spMinorCondition = VER_LESS_EQUAL;
00200     break;
00201 
00202   case VERSION_EQUAL:
00203     majorCondition = VER_EQUAL;
00204     minorCondition = VER_EQUAL;
00205     spMajorCondition = VER_GREATER_EQUAL;
00206     spMinorCondition = VER_GREATER_EQUAL;
00207     break;
00208 
00209   case VERSION_GREATER_THAN_EQUAL:
00210     majorCondition = VER_GREATER_EQUAL;
00211     minorCondition = VER_GREATER_EQUAL;
00212     spMajorCondition = VER_GREATER_EQUAL;
00213     spMinorCondition = VER_GREATER_EQUAL;
00214     break;
00215 
00216   case VERSION_GREATER_THAN:
00217     majorCondition = VER_GREATER;
00218     minorCondition = VER_GREATER;
00219     spMajorCondition = VER_GREATER_EQUAL;
00220     spMinorCondition = VER_GREATER_EQUAL;
00221     break;
00222 
00223   default:
00224     return FALSE;
00225   }
00226 
00227   memset(&osver, 0, sizeof(osver));
00228   osver.dwOSVersionInfoSize = sizeof(osver);
00229   osver.dwMajorVersion = majorVersion;
00230   osver.dwMinorVersion = minorVersion;
00231   if(platform == PLATFORM_WINDOWS)
00232     osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
00233   else if(platform == PLATFORM_WINNT)
00234     osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
00235 
00236   cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
00237   cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
00238   cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
00239   cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
00240   if(platform != PLATFORM_DONT_CARE)
00241     cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
00242 
00243   if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
00244                                 VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR),
00245                        cm))
00246     matched = TRUE;
00247 #endif
00248 
00249   return matched;
00250 }
00251 
00252 #if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
00253                                   defined(USE_WINSOCK))
00254 
00255 /*
00256  * Curl_load_library()
00257  *
00258  * This is used to dynamically load DLLs using the most secure method available
00259  * for the version of Windows that we are running on.
00260  *
00261  * Parameters:
00262  *
00263  * filename  [in] - The filename or full path of the DLL to load. If only the
00264  *                  filename is passed then the DLL will be loaded from the
00265  *                  Windows system directory.
00266  *
00267  * Returns the handle of the module on success; otherwise NULL.
00268  */
00269 HMODULE Curl_load_library(LPCTSTR filename)
00270 {
00271   HMODULE hModule = NULL;
00272   LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
00273 
00274   /* Get a handle to kernel32 so we can access it's functions at runtime */
00275   HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
00276   if(!hKernel32)
00277     return NULL;
00278 
00279   /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
00280      and above */
00281   pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
00282 
00283   /* Detect if there's already a path in the filename and load the library if
00284      there is. Note: Both back slashes and forward slashes have been supported
00285      since the earlier days of DOS at an API level although they are not
00286      supported by command prompt */
00287   if(_tcspbrk(filename, TEXT("\\/"))) {
00289     hModule = pLoadLibraryEx ?
00290       pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
00291       LoadLibrary(filename);
00292   }
00293   /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
00294      supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
00295      Server 2008 R2 with this patch or natively on Windows 8 and above */
00296   else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
00297     /* Load the DLL from the Windows system directory */
00298     hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
00299   }
00300   else {
00301     /* Attempt to get the Windows system path */
00302     UINT systemdirlen = GetSystemDirectory(NULL, 0);
00303     if(systemdirlen) {
00304       /* Allocate space for the full DLL path (Room for the null terminator
00305          is included in systemdirlen) */
00306       size_t filenamelen = _tcslen(filename);
00307       TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
00308       if(path && GetSystemDirectory(path, systemdirlen)) {
00309         /* Calculate the full DLL path */
00310         _tcscpy(path + _tcslen(path), TEXT("\\"));
00311         _tcscpy(path + _tcslen(path), filename);
00312 
00313         /* Load the DLL from the Windows system directory */
00315         hModule = pLoadLibraryEx ?
00316           pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
00317           LoadLibrary(path);
00318 
00319       }
00320       free(path);
00321     }
00322   }
00323 
00324   return hModule;
00325 }
00326 
00327 #endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
00328 
00329 #endif /* WIN32 */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06