Debian Codegraph @

Source/exehead/util.h

raw
1 /*
2 * util.h
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support by Jim Park -- 07/23/2007
17 */
18
19 #ifndef ___NSIS_UTIL_H___
20 #define ___NSIS_UTIL_H___
21
22 #include "../Platform.h"
23 #include "config.h"
24 #include <shlobj.h>
25
26 extern TCHAR ps_tmpbuf[NSIS_MAX_STRLEN*2];
27 TCHAR * NSISCALL GetNSISString(TCHAR *outbuf, int strtab);
28 #define GetNSISStringTT(strtab) GetNSISString(0, (strtab))
29 #define GetNSISStringNP(strtab) ((const TCHAR *)g_blocks[NB_STRINGS].offset+(strtab))
30
31 // A negative string table index means it's a language string table, so we
32 // use the LANG_STR_TAB() macro to decode it.
33 #define GetNSISTab(strtab) (strtab < 0 ? LANG_STR_TAB(strtab) : strtab)
34
35 #define myatoi(s) ( (int)strtoiptr(s) )
36 INT_PTR NSISCALL strtoiptr(const TCHAR *s);
37 #define myitoa iptrtostr
38 void NSISCALL iptrtostr(TCHAR *s, INT_PTR d);
39 TCHAR * NSISCALL mystrcpy(TCHAR *out, const TCHAR *in);
40 int NSISCALL mystrlen(const TCHAR *in);
41 TCHAR * NSISCALL mystrcat(TCHAR *out, const TCHAR *concat);
42 TCHAR * NSISCALL mystrstr(TCHAR *a, TCHAR *b);
43 int StrWideToACP(LPCWSTR Src, char* Dst, int DstCap);
44 #ifdef UNICODE
45 #define strcpyWideToT mystrcpy
46 #else
47 void strcpyWideToT(TCHAR *out, LPCWSTR in);
48 #endif
49
50 #ifdef _WIN64
51 #define ComIIDFromString(s,out) SHCLSIDFromString((s),(CLSID*)(out))
52 #elif defined(UNICODE)
53 #define ComIIDFromString(s,out) IIDFromString((s), (IID*)(out))
54 #else
55 HRESULT ComIIDFromString(LPCTSTR str, IID*out);
56 #endif
57
58 #ifndef KEY_CREATE_LINK
59 #define KEY_CREATE_LINK 0x0020
60 #endif
61 #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!
62 #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")
63 #define KEY_FROMSCRIPT (KEY_FORCEVIEW|KEY_ALTERVIEW) // Use this flag for registry operations from a .nsi script
64 #define NSIS_REGSAM_PRIVATEMASK (KEY_FROMSCRIPT|KEY_FORCEVIEW|KEY_ALTERVIEW)
65 HKEY NSISCALL GetRegKeyAndSAM(HKEY hKey, REGSAM*pRS);
66 LONG NSISCALL RegKeyOpen(HKEY hBase, LPCTSTR SubKey, REGSAM RS, HKEY*phKey);
67 LONG NSISCALL RegKeyCreate(HKEY hBase, LPCTSTR SubKey, REGSAM RS, HKEY*phKey);
68 void NSISCALL myRegGetStr(HKEY root, const TCHAR *sub, const TCHAR *name, TCHAR *out, UINT altview);
69
70
71 #define GetWinVerNTDDIMajMin() ( *(USHORT*)(&g_osinfo.WVMin) )
72 #define IsWin95NT4() ( sizeof(void*) == 4 && GetWinVerNTDDIMajMin() == 0x0400 )
73 #define IsWin9598ME() ( sizeof(TCHAR) == 1 && g_osinfo.WVProd == 0 )
74 #define IsWin9598() ( IsWin9598ME() && GetWinVerNTDDIMajMin() < MAKEWORD(90, 4) )
75 #define IsWinVista() ( GetWinVerNTDDIMajMin() == 0x0600 )
76 #define NSIS_OSINFO_PROD_WOW64FLAG ( sizeof(void*) > 4 ? ( 0 ) : ( 0x80 ) )
77 #define IsWow64() ( sizeof(void*) > 4 ? ( FALSE ) : ( (signed char) g_osinfo.WVProd < 0 ) ) // NSIS_OSINFO_PROD_WOW64FLAG
78 #define SystemSupportsAltRegView() ( sizeof(void*) > 4 ? ( TRUE ) : ( IsWow64() ) )
79
80
81 WIN32_FIND_DATA * NSISCALL file_exists(TCHAR *buf);
82 TCHAR * NSISCALL my_GetTempFileName(TCHAR *buf, const TCHAR *dir);
83 BOOL NSISCALL myReadFile(HANDLE h, LPVOID buf, DWORD cb);
84 BOOL NSISCALL myWriteFile(HANDLE h, const void*buf, DWORD cb);
85 HRESULT NSISCALL UTF16LEBOM(HANDLE h, INT_PTR ForWrite);
86
87 //BOOL NSISCALL my_SetWindowText(HWND hWnd, const TCHAR *val);
88 #define my_SetWindowText SetWindowText
89 BOOL NSISCALL my_SetDialogItemText(HWND dlg, UINT idx, const TCHAR *val);
90 //#define my_SetDialogItemText SetDlgItemText
91 //int NSISCALL my_GetWindowText(HWND hWnd, TCHAR *val, int size);
92 #define my_GetWindowText GetWindowText
93 int NSISCALL my_GetDialogItemText(UINT idx, TCHAR *val);
94 //#define my_GetDialogItemText GetDlgItemText
95
96 #ifdef NSIS_CONFIG_LOG
97 extern TCHAR log_text[2048]; // BUGBUG: Should this be 2*NSIS_MAX_STRLEN? ...and a little larger when NSIS_CONFIG_LOG_TIMESTAMP is defined!
98 void NSISCALL log_write(int close);
99 const TCHAR * _RegKeyHandleToName(HKEY hKey);
100 void _LogData2Hex(TCHAR *buf, size_t cchbuf, BYTE *data, size_t cbdata);
101 void log_printf(TCHAR *format, ...);
102 #define log_printf2(x1,x2) log_printf(x1,x2);
103 #define log_printf3(x1,x2,x3) log_printf(x1,x2,x3);
104 #define log_printf4(x1,x2,x3,x4) log_printf(x1,x2,x3,x4);
105 #define log_printf5(x1,x2,x3,x4,x5) log_printf(x1,x2,x3,x4,x5);
106 #define log_printf6(x1,x2,x3,x4,x5,x6) log_printf(x1,x2,x3,x4,x5,x6);
107 #define log_printf7(x1,x2,x3,x4,x5,x6,x7) log_printf(x1,x2,x3,x4,x5,x6,x7);
108 #define log_printf8(x1,x2,x3,x4,x5,x6,x7,x8) log_printf(x1,x2,x3,x4,x5,x6,x7,x8);
109 #define RegKeyHandleToName(x1) _RegKeyHandleToName(x1);
110 #define LogData2Hex(x1,x2,x3,x4) _LogData2Hex(x1,x2,x3,x4);
111 extern int log_dolog;
112 extern TCHAR g_log_file[1024];
113 #else
114 #define log_printf(x1)
115 #define log_printf2(x1,x2)
116 #define log_printf3(x1,x2,x3)
117 #define log_printf4(x1,x2,x3,x4)
118 #define log_printf5(x1,x2,x3,x4,x5)
119 #define log_printf6(x1,x2,x3,x4,x5,x6)
120 #define log_printf7(x1,x2,x3,x4,x5,x6,x7)
121 #define log_printf8(x1,x2,x3,x4,x5,x6,x7,x8)
122 #define RegKeyHandleToName(x1) NULL
123 #define LogData2Hex(x1,x2,x3,x4)
124 #endif
125
126 extern const UINT32 g_restrictedacl[];
127 #define GetAdminGrpAcl() ( (PACL) g_restrictedacl )
128 #define GetAdminGrpSid() ( (PSID) &g_restrictedacl[4] )
129 BOOL NSISCALL UserIsAdminGrpMember(); // Does not check integrity level, returns true if the process has a non-deny administrators group ACE in the token
130 DWORD NSISCALL CreateRestrictedDirectory(LPCTSTR path);
131 DWORD NSISCALL CreateNormalDirectory(LPCTSTR path);
132
133 HANDLE NSISCALL myCreateProcess(TCHAR *cmd);
134 BOOL NSISCALL myShellExecuteEx(SHELLEXECUTEINFO*pSEI);
135 int NSISCALL my_MessageBox(const TCHAR *text, UINT type);
136
137 void NSISCALL myDelete(TCHAR *buf, int flags);
138
139 HANDLE NSISCALL myOpenFile(const TCHAR *fn, DWORD da, DWORD cd);
140 int NSISCALL validpathspec(TCHAR *ubuf);
141 TCHAR * NSISCALL addtrailingslash(TCHAR *str);
142 //TCHAR NSISCALL lastchar(const TCHAR *str);
143 #define lastchar(str) *CharPrev(str,str+mystrlen(str))
144 TCHAR * NSISCALL findchar(TCHAR *str, TCHAR c);
145 TCHAR * NSISCALL trimslashtoend(TCHAR *buf);
146 TCHAR * NSISCALL skip_root(TCHAR *path);
147 int NSISCALL is_valid_instpath(TCHAR *s);
148 void NSISCALL validate_filename(TCHAR *fn);
149
150 /**
151 * MoveFileOnReboot tries to move a file by the name of pszExisting to the
152 * name pszNew.
153 *
154 * @param pszExisting The old name of the file.
155 * @param pszNew The new name of the file.
156 */
157 void NSISCALL MoveFileOnReboot(LPCTSTR pszExisting, LPCTSTR pszNew);
158 DWORD NSISCALL remove_ro_attr(LPCTSTR file);
159
160 #ifdef _NSIS_NODEFLIB_CRTMEMCPY
161 #define mini_memcpy memcpy
162 #else
163 void NSISCALL mini_memcpy(void *out, const void *in, UINT_PTR cb);
164 #endif
165
166 enum myGetProcAddressFunctions {
167 MGA_SetDefaultDllDirectories, // Win8+ but also exists on Vista/2008/7/2008R2 if KB2533623 is installed
168 #ifndef _WIN64
169 MGA_GetDiskFreeSpaceEx,
170 MGA_GetUserDefaultUILanguage,
171 #endif
172 #if !defined(_WIN64) || defined(_M_IA64)
173 MGA_RegDeleteKeyEx,
174 #endif
175 MGA_InitiateShutdown,
176 MGA_SHGetKnownFolderPath,
177 MGA_IsUserAnAdmin,
178 MGA_SHCloneSpecialIDList,
179 #ifndef _WIN64
180 MGA_IsOS,
181 #endif
182 MGA_SHAutoComplete, // x64 can link to shlwapi directly but as long as MGA_SHGetFolderPath is used we can stick with myGetProcAddress
183 MGA_SHGetFolderPath, // TODO: This can probably call something else directly on x64
184 #ifdef NSIS_SUPPORT_GETDLLVERSION
185 MGA_GetFileVersionInfoSize, // Version.dll exists in all Windows versions, it is delay loaded to avoid DLL hijacking [bug #1125]
186 MGA_GetFileVersionInfo,
187 MGA_VerQueryValue
188 #endif
189 };
190
191 HMODULE NSISCALL LoadSystemLibrary(LPCSTR name);
192 void*NSISCALL myGetProcAddress(const enum myGetProcAddressFunctions func);
193 void NSISCALL MessageLoop(UINT uCheckedMsg);
194
195 /**
196 * This function is useful for Unicode support. Since the Windows
197 * GetProcAddress function always takes a char*, this function wraps
198 * the windows call and does the appropriate translation when
199 * appropriate.
200 *
201 * @param dllHandle Handle to the DLL loaded by LoadLibrary[Ex].
202 * @param funcName The name of the function to get the address of.
203 * @return The pointer to the function. Null if failure.
204 */
205 void* NSISCALL NSISGetProcAddress(HANDLE dllHandle, TCHAR* funcName);
206
207 DWORD NSISCALL WaitForProcess(HANDLE hProcess);
208
209 // Turn a pair of chars into a word
210 // Turn four chars into a dword
211 #ifdef __BIG_ENDIAN__ // Not very likely, but, still...
212 #ifdef _UNICODE
213 #ifdef _NSIS_NO_INT64_SHR
214 #define CMP4CHAR(mem, const4) ((((LPDWORD)(mem))[0] == (const4[3]|const4[2]<<16)) && (((LPDWORD)(mem))[1] == (const4[1]|const4[0]<<16)))
215 #else
216 #define CMP4CHAR(mem, const4) (*(PDWORD64)(mem) == (const4[3]|const4[2]<<16|(DWORD64)const4[1]<<32|(DWORD64)const4[0]<<48))
217 #endif
218 #define SET2CHAR(mem, const2) (*(LPDWORD)(mem) = (const2[1]|const2[0]<<16))
219 #else
220 #define CMP4CHAR(mem, const4) (*(LPDWORD)(mem) == (const4[3]|const4[2]<<8|const4[1]<<16|const4[0]<<24))
221 #define SET2CHAR(mem, const2) (*(LPWORD)(mem) = (const2[1]|const2[0]<<8))
222 #endif
223 #else
224 #ifdef _UNICODE
225 #ifdef _NSIS_NO_INT64_SHR
226 #define CMP4CHAR(mem, const4) ((((LPDWORD)(mem))[0] == (DWORD)(const4[0]|const4[1]<<16)) && (((LPDWORD)(mem))[1] == (DWORD)(const4[2]|const4[3]<<16)))
227 #else
228 #define CMP4CHAR(mem, const4) (*(PDWORD64)(mem) == (DWORD64)(const4[0]|const4[1]<<16|(DWORD64)const4[2]<<32|(DWORD64)const4[3]<<48))
229 #endif
230 #define SET2CHAR(mem, const2) (*(LPDWORD)(mem) = (const2[0]|const2[1]<<16))
231 #else
232 #define CMP4CHAR(mem, const4) (*(LPDWORD)(mem) == (DWORD)(const4[0]|const4[1]<<8|const4[2]<<16|const4[3]<<24))
233 #define SET2CHAR(mem, const2) (*(LPWORD)(mem) = (const2[0]|const2[1]<<8))
234 #endif
235 #endif
236
237 #endif//!___NSIS_UTIL_H___