2006-10-28 19:45:02 +00:00
/*
* util . h
*
* This file is a part of NSIS .
*
2021-01-01 20:27:52 +00:00
* Copyright ( C ) 1999 - 2021 Nullsoft and Contributors
2006-10-28 19:45:02 +00:00
*
* Licensed under the zlib / libpng license ( the " License " ) ;
* you may not use this file except in compliance with the License .
*
* Licence details can be found in the file COPYING .
*
* This software is provided ' as - is ' , without any express or implied
* warranty .
2010-03-24 17:22:56 +00:00
*
* Unicode support by Jim Park - - 07 / 23 / 2007
2006-10-28 19:45:02 +00:00
*/
2007-04-20 20:32:04 +00:00
# ifndef ___NSIS_UTIL_H___
# define ___NSIS_UTIL_H___
2004-03-12 20:43:54 +00:00
# include "../Platform.h"
2002-08-02 10:01:35 +00:00
# include "config.h"
2003-12-22 00:28:30 +00:00
# include <shlobj.h>
2002-08-02 10:01:35 +00:00
2010-03-24 17:22:56 +00:00
extern TCHAR ps_tmpbuf [ NSIS_MAX_STRLEN * 2 ] ;
TCHAR * NSISCALL GetNSISString ( TCHAR * outbuf , int strtab ) ;
2003-09-04 18:25:57 +00:00
# define GetNSISStringTT(strtab) GetNSISString(0, (strtab))
2010-03-24 17:22:56 +00:00
# define GetNSISStringNP(strtab) ((const TCHAR *)g_blocks[NB_STRINGS].offset+(strtab))
// A negative string table index means it's a language string table, so we
// use the LANG_STR_TAB() macro to decode it.
2003-09-04 18:25:57 +00:00
# define GetNSISTab(strtab) (strtab < 0 ? LANG_STR_TAB(strtab) : strtab)
2010-03-24 17:22:56 +00:00
2013-08-07 23:04:23 +00:00
# define myatoi(s) ( (int)strtoiptr(s) )
INT_PTR NSISCALL strtoiptr ( const TCHAR * s ) ;
# define myitoa iptrtostr
void NSISCALL iptrtostr ( TCHAR * s , INT_PTR d ) ;
2010-03-24 17:22:56 +00:00
TCHAR * NSISCALL mystrcpy ( TCHAR * out , const TCHAR * in ) ;
int NSISCALL mystrlen ( const TCHAR * in ) ;
TCHAR * NSISCALL mystrcat ( TCHAR * out , const TCHAR * concat ) ;
TCHAR * NSISCALL mystrstr ( TCHAR * a , TCHAR * b ) ;
2020-03-12 23:43:51 +00:00
int StrWideToACP ( LPCWSTR Src , char * Dst , int DstCap ) ;
# ifdef UNICODE
# define strcpyWideToT mystrcpy
# else
void strcpyWideToT ( TCHAR * out , LPCWSTR in ) ;
# endif
2017-06-04 22:48:13 +00:00
2020-03-12 23:43:51 +00:00
# ifdef _WIN64
# define ComIIDFromString(s,out) SHCLSIDFromString((s),(CLSID*)(out))
# elif defined(UNICODE)
# define ComIIDFromString(s,out) IIDFromString((s), (IID*)(out))
# else
HRESULT ComIIDFromString ( LPCTSTR str , IID * out ) ;
# endif
2017-06-04 22:48:13 +00:00
2017-06-06 19:51:43 +00:00
# ifndef KEY_CREATE_LINK
# define KEY_CREATE_LINK 0x0020
# endif
# define KEY_FORCEVIEW KEY_CREATE_LINK // Our private flag used by RegKey* to indicate that we want it to handle HKLM[32|64] style root keys. Cannot be set if the HKEY is a real handle!
2017-06-04 22:48:13 +00:00
# define KEY_ALTERVIEW SYNCHRONIZE // Our private flag used by RegKey* to indicate that we want it to apply g_exec_flags.alter_reg_view. (MSDN:"Registry keys do not support the SYNCHRONIZE standard access right")
2017-06-06 19:51:43 +00:00
# define KEY_FROMSCRIPT (KEY_FORCEVIEW|KEY_ALTERVIEW) // Use this flag for registry operations from a .nsi script
# define NSIS_REGSAM_PRIVATEMASK (KEY_FROMSCRIPT|KEY_FORCEVIEW|KEY_ALTERVIEW)
HKEY NSISCALL GetRegKeyAndSAM ( HKEY hKey , REGSAM * pRS ) ;
2017-06-04 22:48:13 +00:00
LONG NSISCALL RegKeyOpen ( HKEY hBase , LPCTSTR SubKey , REGSAM RS , HKEY * phKey ) ;
LONG NSISCALL RegKeyCreate ( HKEY hBase , LPCTSTR SubKey , REGSAM RS , HKEY * phKey ) ;
void NSISCALL myRegGetStr ( HKEY root , const TCHAR * sub , const TCHAR * name , TCHAR * out , UINT altview ) ;
2021-08-29 18:39:07 +00:00
# define GetWinVerNTDDIMajMin() ( *(UINT16*)(&g_osinfo.WVMin) )
# define IsWin95NT4() ( sizeof(void*) == 4 && GetWinVerNTDDIMajMin() == 0x0400 )
# define IsWin9598ME() ( sizeof(TCHAR) == 1 && g_osinfo.WVProd == 0 )
# define IsWin9598() ( IsWin9598ME() && GetWinVerNTDDIMajMin() < MAKEWORD(90, 4) )
# define IsWinVista() ( GetWinVerNTDDIMajMin() == 0x0600 )
# define NSIS_OSINFO_PROD_WOW64FLAG ( sizeof(void*) > 4 ? ( 0 ) : ( 0x80 ) )
# define IsWow64() ( sizeof(void*) > 4 ? ( FALSE ) : ( (signed char) g_osinfo.WVProd < 0 ) ) // NSIS_OSINFO_PROD_WOW64FLAG
2017-06-04 22:48:13 +00:00
# define SystemSupportsAltRegView() ( sizeof(void*) > 4 ? ( TRUE ) : ( IsWow64() ) )
2010-03-24 17:22:56 +00:00
WIN32_FIND_DATA * NSISCALL file_exists ( TCHAR * buf ) ;
TCHAR * NSISCALL my_GetTempFileName ( TCHAR * buf , const TCHAR * dir ) ;
2013-07-25 19:18:34 +00:00
BOOL NSISCALL myReadFile ( HANDLE h , LPVOID buf , DWORD cb ) ;
2015-10-31 16:27:34 +00:00
BOOL NSISCALL myWriteFile ( HANDLE h , const void * buf , DWORD cb ) ;
2015-10-26 23:29:39 +00:00
HRESULT NSISCALL UTF16LEBOM ( HANDLE h , INT_PTR ForWrite ) ;
2010-03-24 17:22:56 +00:00
//BOOL NSISCALL my_SetWindowText(HWND hWnd, const TCHAR *val);
2003-12-29 14:27:33 +00:00
# define my_SetWindowText SetWindowText
2010-03-24 17:22:56 +00:00
BOOL NSISCALL my_SetDialogItemText ( HWND dlg , UINT idx , const TCHAR * val ) ;
2004-01-04 17:05:03 +00:00
//#define my_SetDialogItemText SetDlgItemText
2010-03-24 17:22:56 +00:00
//int NSISCALL my_GetWindowText(HWND hWnd, TCHAR *val, int size);
2003-02-07 23:04:25 +00:00
# define my_GetWindowText GetWindowText
2010-03-24 17:22:56 +00:00
int NSISCALL my_GetDialogItemText ( UINT idx , TCHAR * val ) ;
2004-01-04 17:05:03 +00:00
//#define my_GetDialogItemText GetDlgItemText
2002-08-02 10:01:35 +00:00
# ifdef NSIS_CONFIG_LOG
2014-05-08 20:12:26 +00:00
extern TCHAR log_text [ 2048 ] ; // BUGBUG: Should this be 2*NSIS_MAX_STRLEN? ...and a little larger when NSIS_CONFIG_LOG_TIMESTAMP is defined!
2002-08-19 23:18:19 +00:00
void NSISCALL log_write ( int close ) ;
2010-03-24 17:22:56 +00:00
const TCHAR * _RegKeyHandleToName ( HKEY hKey ) ;
2014-05-08 20:12:26 +00:00
void _LogData2Hex ( TCHAR * buf , size_t cchbuf , BYTE * data , size_t cbdata ) ;
2010-03-24 17:22:56 +00:00
void log_printf ( TCHAR * format , . . . ) ;
2004-01-27 18:44:31 +00:00
# define log_printf2(x1,x2) log_printf(x1,x2);
# define log_printf3(x1,x2,x3) log_printf(x1,x2,x3);
# define log_printf4(x1,x2,x3,x4) log_printf(x1,x2,x3,x4);
# define log_printf5(x1,x2,x3,x4,x5) log_printf(x1,x2,x3,x4,x5);
# define log_printf6(x1,x2,x3,x4,x5,x6) log_printf(x1,x2,x3,x4,x5,x6);
# define log_printf7(x1,x2,x3,x4,x5,x6,x7) log_printf(x1,x2,x3,x4,x5,x6,x7);
# define log_printf8(x1,x2,x3,x4,x5,x6,x7,x8) log_printf(x1,x2,x3,x4,x5,x6,x7,x8);
2005-11-24 16:45:04 +00:00
# define RegKeyHandleToName(x1) _RegKeyHandleToName(x1);
# define LogData2Hex(x1,x2,x3,x4) _LogData2Hex(x1,x2,x3,x4);
2002-08-02 10:01:35 +00:00
extern int log_dolog ;
2010-03-24 17:22:56 +00:00
extern TCHAR g_log_file [ 1024 ] ;
2002-08-02 10:01:35 +00:00
# else
# define log_printf(x1)
# define log_printf2(x1,x2)
# define log_printf3(x1,x2,x3)
# define log_printf4(x1,x2,x3,x4)
# define log_printf5(x1,x2,x3,x4,x5)
# define log_printf6(x1,x2,x3,x4,x5,x6)
2004-01-27 18:44:31 +00:00
# define log_printf7(x1,x2,x3,x4,x5,x6,x7)
2002-08-02 10:01:35 +00:00
# define log_printf8(x1,x2,x3,x4,x5,x6,x7,x8)
2005-11-24 16:45:04 +00:00
# define RegKeyHandleToName(x1) NULL
# define LogData2Hex(x1,x2,x3,x4)
2002-08-02 10:01:35 +00:00
# endif
2015-12-07 20:49:46 +00:00
extern const UINT32 g_restrictedacl [ ] ;
# define GetAdminGrpAcl() ( (PACL) g_restrictedacl )
# define GetAdminGrpSid() ( (PSID) &g_restrictedacl[4] )
BOOL NSISCALL UserIsAdminGrpMember ( ) ; // Does not check integrity level, returns true if the process has a non-deny administrators group ACE in the token
DWORD NSISCALL CreateRestrictedDirectory ( LPCTSTR path ) ;
DWORD NSISCALL CreateNormalDirectory ( LPCTSTR path ) ;
2010-03-24 17:22:56 +00:00
HANDLE NSISCALL myCreateProcess ( TCHAR * cmd ) ;
2017-06-05 01:01:28 +00:00
BOOL NSISCALL myShellExecuteEx ( SHELLEXECUTEINFO * pSEI ) ;
2010-03-24 17:22:56 +00:00
int NSISCALL my_MessageBox ( const TCHAR * text , UINT type ) ;
2002-08-08 15:04:45 +00:00
2010-03-24 17:22:56 +00:00
void NSISCALL myDelete ( TCHAR * buf , int flags ) ;
2002-08-11 18:56:30 +00:00
2010-03-24 17:22:56 +00:00
HANDLE NSISCALL myOpenFile ( const TCHAR * fn , DWORD da , DWORD cd ) ;
int NSISCALL validpathspec ( TCHAR * ubuf ) ;
TCHAR * NSISCALL addtrailingslash ( TCHAR * str ) ;
//TCHAR NSISCALL lastchar(const TCHAR *str);
2003-02-20 18:47:58 +00:00
# define lastchar(str) *CharPrev(str,str+mystrlen(str))
2010-03-24 17:22:56 +00:00
TCHAR * NSISCALL findchar ( TCHAR * str , TCHAR c ) ;
TCHAR * NSISCALL trimslashtoend ( TCHAR * buf ) ;
TCHAR * NSISCALL skip_root ( TCHAR * path ) ;
int NSISCALL is_valid_instpath ( TCHAR * s ) ;
2014-10-26 23:22:27 +00:00
void NSISCALL validate_filename ( TCHAR * fn ) ;
2010-03-24 17:22:56 +00:00
/**
* MoveFileOnReboot tries to move a file by the name of pszExisting to the
* name pszNew .
*
* @ param pszExisting The old name of the file .
* @ param pszNew The new name of the file .
*/
2003-09-09 14:25:16 +00:00
void NSISCALL MoveFileOnReboot ( LPCTSTR pszExisting , LPCTSTR pszNew ) ;
2010-09-24 18:43:04 +00:00
DWORD NSISCALL remove_ro_attr ( LPCTSTR file ) ;
2002-09-22 19:05:43 +00:00
2014-02-08 00:13:52 +00:00
# ifdef _NSIS_NODEFLIB_CRTMEMCPY
# define mini_memcpy memcpy
# else
void NSISCALL mini_memcpy ( void * out , const void * in , UINT_PTR cb ) ;
# endif
2007-04-20 20:32:04 +00:00
enum myGetProcAddressFunctions {
2015-12-08 01:54:30 +00:00
MGA_SetDefaultDllDirectories , // Win8+ but also exists on Vista/2008/7/2008R2 if KB2533623 is installed
2014-02-08 00:13:52 +00:00
# ifndef _WIN64
2010-03-24 17:22:56 +00:00
MGA_GetDiskFreeSpaceEx ,
2014-06-27 16:30:16 +00:00
MGA_GetUserDefaultUILanguage ,
2020-04-12 18:25:21 +00:00
# endif
# if !defined(_WIN64) || defined(_M_IA64)
2010-03-24 17:22:56 +00:00
MGA_RegDeleteKeyEx ,
2014-02-08 00:13:52 +00:00
# endif
2014-06-27 16:30:16 +00:00
MGA_InitiateShutdown ,
2020-03-12 23:43:51 +00:00
MGA_SHGetKnownFolderPath ,
2015-12-07 20:49:46 +00:00
MGA_IsUserAnAdmin ,
2017-06-04 22:48:13 +00:00
# ifndef _WIN64
MGA_IsOS ,
# endif
2014-02-08 00:13:52 +00:00
MGA_SHAutoComplete , // x64 can link to shlwapi directly but as long as MGA_SHGetFolderPath is used we can stick with myGetProcAddress
MGA_SHGetFolderPath , // TODO: This can probably call something else directly on x64
2015-11-29 13:54:35 +00:00
# ifdef NSIS_SUPPORT_GETDLLVERSION
2015-11-28 13:32:54 +00:00
MGA_GetFileVersionInfoSize , // Version.dll exists in all Windows versions, it is delay loaded to avoid DLL hijacking [bug #1125]
2015-11-25 13:13:42 +00:00
MGA_GetFileVersionInfo ,
MGA_VerQueryValue
2015-11-29 13:54:35 +00:00
# endif
2007-04-20 20:32:04 +00:00
} ;
2015-11-29 13:54:35 +00:00
HMODULE NSISCALL LoadSystemLibrary ( LPCSTR name ) ;
2015-11-28 13:32:54 +00:00
void * NSISCALL myGetProcAddress ( const enum myGetProcAddressFunctions func ) ;
2005-09-09 15:21:45 +00:00
void NSISCALL MessageLoop ( UINT uCheckedMsg ) ;
2005-09-02 11:35:45 +00:00
2010-04-13 15:01:14 +00:00
/**
* This function is useful for Unicode support . Since the Windows
* GetProcAddress function always takes a char * , this function wraps
* the windows call and does the appropriate translation when
* appropriate .
*
2015-11-29 13:54:35 +00:00
* @ param dllHandle Handle to the DLL loaded by LoadLibrary [ Ex ] .
2010-04-13 15:01:14 +00:00
* @ param funcName The name of the function to get the address of .
* @ return The pointer to the function . Null if failure .
*/
2017-03-21 22:04:40 +00:00
void * NSISCALL NSISGetProcAddress ( HANDLE dllHandle , TCHAR * funcName ) ;
DWORD NSISCALL WaitForProcess ( HANDLE hProcess ) ;
2010-04-13 15:01:14 +00:00
2002-09-22 19:05:43 +00:00
// Turn a pair of chars into a word
// Turn four chars into a dword
# ifdef __BIG_ENDIAN__ // Not very likely, but, still...
2010-04-14 15:51:48 +00:00
# ifdef _UNICODE
# ifdef _NSIS_NO_INT64_SHR
# define CMP4CHAR(mem, const4) ((((LPDWORD)(mem))[0] == (const4[3]|const4[2]<<16)) && (((LPDWORD)(mem))[1] == (const4[1]|const4[0]<<16)))
2002-09-22 19:05:43 +00:00
# else
2010-04-14 15:51:48 +00:00
# define CMP4CHAR(mem, const4) (*(PDWORD64)(mem) == (const4[3]|const4[2]<<16|(DWORD64)const4[1]<<32|(DWORD64)const4[0]<<48))
# endif
# define SET2CHAR(mem, const2) (*(LPDWORD)(mem) = (const2[1]|const2[0]<<16))
# else
# define CMP4CHAR(mem, const4) (*(LPDWORD)(mem) == (const4[3]|const4[2]<<8|const4[1]<<16|const4[0]<<24))
# define SET2CHAR(mem, const2) (*(LPWORD)(mem) = (const2[1]|const2[0]<<8))
# endif
# else
# ifdef _UNICODE
# ifdef _NSIS_NO_INT64_SHR
2010-07-04 20:15:48 +00:00
# define CMP4CHAR(mem, const4) ((((LPDWORD)(mem))[0] == (DWORD)(const4[0]|const4[1]<<16)) && (((LPDWORD)(mem))[1] == (DWORD)(const4[2]|const4[3]<<16)))
2010-04-14 15:51:48 +00:00
# else
2010-07-04 20:15:48 +00:00
# define CMP4CHAR(mem, const4) (*(PDWORD64)(mem) == (DWORD64)(const4[0]|const4[1]<<16|(DWORD64)const4[2]<<32|(DWORD64)const4[3]<<48))
2010-04-14 15:51:48 +00:00
# endif
# define SET2CHAR(mem, const2) (*(LPDWORD)(mem) = (const2[0]|const2[1]<<16))
# else
2010-07-04 20:15:48 +00:00
# define CMP4CHAR(mem, const4) (*(LPDWORD)(mem) == (DWORD)(const4[0]|const4[1]<<8|const4[2]<<16|const4[3]<<24))
2010-04-14 15:51:48 +00:00
# define SET2CHAR(mem, const2) (*(LPWORD)(mem) = (const2[0]|const2[1]<<8))
# endif
2002-09-22 19:05:43 +00:00
# endif
2007-04-20 20:32:04 +00:00
# endif //!___NSIS_UTIL_H___