12089Ssos/***************************************************************************
2228976Suqs *                                  _   _ ____  _
32089Ssos *  Project                     ___| | | |  _ \| |
42089Ssos *                             / __| | | | |_) | |
52089Ssos *                            | (__| |_| |  _ <| |___
62089Ssos *                             \___|\___/|_| \_\_____|
72089Ssos *
82089Ssos * Copyright (C) 2016 - 2017, Steve Holme, <steve_holme@hotmail.com>.
95994Ssos * Copyright (C) 2017, Expat development team
105994Ssos *
112089Ssos * All rights reserved.
122089Ssos * Licensed under the MIT license:
132089Ssos *
142089Ssos * Permission to  use, copy,  modify, and distribute  this software  for any
1597748Sschweikh * purpose with  or without fee is  hereby granted, provided that  the above
162089Ssos * copyright notice and this permission notice appear in all copies.
172089Ssos *
182089Ssos * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
192089Ssos * EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
202089Ssos * MERCHANTABILITY, FITNESS FOR A  PARTICULAR PURPOSE AND NONINFRINGEMENT OF
212089Ssos * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
222089Ssos * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
232089Ssos * CONTRACT, TORT OR  OTHERWISE, ARISING FROM, OUT OF OR  IN CONNECTION WITH
242089Ssos * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
252089Ssos *
262089Ssos * Except as contained in this notice,  the name of a copyright holder shall
272089Ssos * not be used in advertising or otherwise to promote the sale, use or other
282089Ssos * dealings  in this  Software without  prior written  authorization of  the
2930764Scharnier * copyright holder.
3030764Scharnier *
3150479Speter ***************************************************************************/
3230764Scharnier
3330764Scharnier#if defined(_WIN32)
342089Ssos
3523457Sbrian#include <windows.h>
3623457Sbrian#include <tchar.h>
372089Ssos
3875344Ssobomax
392089SsosHMODULE _Expat_LoadLibrary(LPCTSTR filename);
4075344Ssobomax
4175344Ssobomax
4275344Ssobomax#if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
432089Ssos#define LOAD_WITH_ALTERED_SEARCH_PATH  0x00000008
442089Ssos#endif
452089Ssos
462089Ssos#if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
472089Ssos#define LOAD_LIBRARY_SEARCH_SYSTEM32   0x00000800
488857Srgrimes#endif
492089Ssos
502089Ssos/* We use our own typedef here since some headers might lack these */
51140159Sdelphijtypedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
5275344Ssobomax
532089Ssos/* See function definitions in winbase.h */
542089Ssos#ifdef UNICODE
552089Ssos#  ifdef _WIN32_WCE
562089Ssos#    define LOADLIBARYEX  L"LoadLibraryExW"
572089Ssos#  else
5875344Ssobomax#    define LOADLIBARYEX  "LoadLibraryExW"
5975344Ssobomax#  endif
602089Ssos#else
6175344Ssobomax#  define LOADLIBARYEX    "LoadLibraryExA"
6275344Ssobomax#endif
6375344Ssobomax
642089Ssos
652089Ssos/*
662089Ssos * _Expat_LoadLibrary()
6775344Ssobomax *
682089Ssos * This is used to dynamically load DLLs using the most secure method available
692089Ssos * for the version of Windows that we are running on.
702089Ssos *
7175344Ssobomax * Parameters:
722089Ssos *
732089Ssos * filename  [in] - The filename or full path of the DLL to load. If only the
742089Ssos *                  filename is passed then the DLL will be loaded from the
7575344Ssobomax *                  Windows system directory.
762089Ssos *
772089Ssos * Returns the handle of the module on success; otherwise NULL.
782089Ssos */
7975344SsobomaxHMODULE _Expat_LoadLibrary(LPCTSTR filename)
8075344Ssobomax{
8175344Ssobomax  HMODULE hModule = NULL;
8275344Ssobomax  LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
8375344Ssobomax
8475344Ssobomax  /* Get a handle to kernel32 so we can access it's functions at runtime */
8575344Ssobomax  HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
8675344Ssobomax  if(!hKernel32)
8775344Ssobomax    return NULL;  /* LCOV_EXCL_LINE */
8875344Ssobomax
8975344Ssobomax  /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
9075344Ssobomax     and above */
9175344Ssobomax  pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
9275344Ssobomax
9375344Ssobomax  /* Detect if there's already a path in the filename and load the library if
9475344Ssobomax     there is. Note: Both back slashes and forward slashes have been supported
952089Ssos     since the earlier days of DOS at an API level although they are not
962089Ssos     supported by command prompt */
972089Ssos  if(_tcspbrk(filename, TEXT("\\/"))) {
982089Ssos    /** !checksrc! disable BANNEDFUNC 1 **/
992089Ssos    hModule = pLoadLibraryEx ?
100      pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
101      LoadLibrary(filename);
102  }
103  /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
104     supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
105     Server 2008 R2 with this patch or natively on Windows 8 and above */
106  else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
107    /* Load the DLL from the Windows system directory */
108    hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
109  }
110  else {
111    /* Attempt to get the Windows system path */
112    UINT systemdirlen = GetSystemDirectory(NULL, 0);
113    if(systemdirlen) {
114      /* Allocate space for the full DLL path (Room for the null terminator
115         is included in systemdirlen) */
116      size_t filenamelen = _tcslen(filename);
117      TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
118      if(path && GetSystemDirectory(path, systemdirlen)) {
119        /* Calculate the full DLL path */
120        _tcscpy(path + _tcslen(path), TEXT("\\"));
121        _tcscpy(path + _tcslen(path), filename);
122
123        /* Load the DLL from the Windows system directory */
124        /** !checksrc! disable BANNEDFUNC 1 **/
125        hModule = pLoadLibraryEx ?
126          pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
127          LoadLibrary(path);
128
129      }
130      free(path);
131    }
132  }
133
134  return hModule;
135}
136
137#else /* defined(_WIN32) */
138
139/* ISO C requires a translation unit to contain at least one declaration
140   [-Wempty-translation-unit] */
141typedef int _TRANSLATION_UNIT_LOAD_LIBRARY_C_NOT_EMTPY;
142
143#endif /* defined(_WIN32) */
144