| 1 |
/* |
| 2 |
* util.c |
| 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 -- 08/11/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "../Platform.h" |
| 20 |
#include <shellapi.h> |
| 21 |
#include "util.h" |
| 22 |
#include "state.h" |
| 23 |
#include "config.h" |
| 24 |
#include "lang.h" |
| 25 |
#include "fileform.h" |
| 26 |
#include "exec.h" |
| 27 |
#include "ui.h" |
| 28 |
#include "resource.h" |
| 29 |
#include "../tchar.h" |
| 30 |
|
| 31 |
#ifdef NSIS_CONFIG_LOG |
| 32 |
#if !defined(NSIS_CONFIG_LOG_ODS) && !defined(NSIS_CONFIG_LOG_STDOUT) |
| 33 |
TCHAR g_log_file[1024]; |
| 34 |
#endif |
| 35 |
#endif |
| 36 |
|
| 37 |
// *** DO NOT DECLARE MORE VARIABLES INSIDE THESE PRAGMAS *** |
| 38 |
// This will produce a special section called ".ndata" (stands for nsis data) |
| 39 |
// this way makensis during build time, can search for this section by name |
| 40 |
// and change the virtual size of this section |
| 41 |
// which results in extra memory for extra variables without code to do allocation :) |
| 42 |
// nsis then removes the "DISCARDABLE" style from section (for safe) |
| 43 |
#ifdef _MSC_VER |
| 44 |
# pragma bss_seg(NSIS_VARS_SECTION) |
| 45 |
NSIS_STRING g_usrvarssection[1]; |
| 46 |
# pragma bss_seg() |
| 47 |
# pragma comment(linker, "/section:" NSIS_VARS_SECTION ",rwd") |
| 48 |
#else |
| 49 |
# ifdef __GNUC__ |
| 50 |
// GCC does not treat g_usrvarssection as a bss section so we keep the size as small as possible. |
| 51 |
// NSIS_STRING g_usrvarssection[31] is required to remove this hack but that really bloats the exehead. |
| 52 |
TCHAR g_usrvarssection[1] __attribute__((section (NSIS_VARS_SECTION))); |
| 53 |
const NSIS_STRING*const g_usrvarsstart = (const NSIS_STRING*const) g_usrvarssection; |
| 54 |
# else |
| 55 |
# error Unknown compiler. You must implement the separate PE section yourself. |
| 56 |
# endif |
| 57 |
#endif |
| 58 |
|
| 59 |
const UINT32 g_restrictedacl[] = { |
| 60 |
0x00340002, 0x00000002, // ACL (ACL_REVISION2, 2 ACEs) |
| 61 |
0x00180300, // ACCESS_ALLOWED_ACE:ACE_HEADER (ACCESS_ALLOWED_ACE_TYPE, CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE) |
| 62 |
0x10000000, // ACCESS_ALLOWED_ACE:ACCESS_MASK: GENERIC_ALL |
| 63 |
0x00000201, 0x05000000, 0x00000020, 0x00000220, // ACCESS_ALLOWED_ACE:SID (BUILTIN\Administrators) NOTE: GetAdminGrpSid() relies on this being the first SID in the ACL |
| 64 |
0x00140300, // ACCESS_ALLOWED_ACE:ACE_HEADER (ACCESS_ALLOWED_ACE_TYPE, CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE) |
| 65 |
0x001200c1, // ACCESS_ALLOWED_ACE:ACCESS_MASK: SYNCHRONIZE|READ_CONTROL|FILE_LIST_DIRECTORY|FILE_DELETE_CHILD|FILE_READ_ATTRIBUTES |
| 66 |
0x00000101, 0x01000000, 0x00000000 // ACCESS_ALLOWED_ACE:SID (WORLD\Everyone) |
| 67 |
}; |
| 68 |
|
| 69 |
DWORD NSISCALL CreateRestrictedDirectory(LPCTSTR path) |
| 70 |
{ |
| 71 |
PSID admingrpsid = GetAdminGrpSid(); |
| 72 |
SECURITY_DESCRIPTOR sd = { 1, 0, SE_DACL_PRESENT, admingrpsid, admingrpsid, NULL, GetAdminGrpAcl() }; |
| 73 |
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), &sd, FALSE }; |
| 74 |
return CreateDirectory(path, &sa) ? ERROR_SUCCESS : GetLastError(); |
| 75 |
} |
| 76 |
DWORD NSISCALL CreateNormalDirectory(LPCTSTR path) |
| 77 |
{ |
| 78 |
return CreateDirectory(path, NULL) ? ERROR_SUCCESS : GetLastError(); |
| 79 |
} |
| 80 |
|
| 81 |
BOOL NSISCALL UserIsAdminGrpMember() |
| 82 |
{ |
| 83 |
FARPROC iuaa = myGetProcAddress(MGA_IsUserAnAdmin); |
| 84 |
return iuaa && ((BOOL(WINAPI*)())iuaa)(); |
| 85 |
} |
| 86 |
|
| 87 |
HANDLE NSISCALL myCreateProcess(TCHAR *cmd) |
| 88 |
{ |
| 89 |
PROCESS_INFORMATION ProcInfo; |
| 90 |
static STARTUPINFO StartUp; |
| 91 |
StartUp.cb=sizeof(StartUp); |
| 92 |
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &StartUp, &ProcInfo)) |
| 93 |
return NULL; |
| 94 |
CloseHandle(ProcInfo.hThread); |
| 95 |
return ProcInfo.hProcess; |
| 96 |
} |
| 97 |
|
| 98 |
BOOL NSISCALL myShellExecuteEx(SHELLEXECUTEINFO*pSEI) |
| 99 |
{ |
| 100 |
pSEI->cbSize = sizeof(SHELLEXECUTEINFO); |
| 101 |
pSEI->lpIDList = NULL; // Must set this because SEE_MASK_INVOKEIDLIST might be set by ExecShell[Wait] |
| 102 |
return ShellExecuteEx(pSEI); |
| 103 |
} |
| 104 |
|
| 105 |
/*BOOL NSISCALL my_SetWindowText(HWND hWnd, const TCHAR *val) |
| 106 |
{ |
| 107 |
return SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)val); |
| 108 |
}*/ |
| 109 |
|
| 110 |
BOOL NSISCALL my_SetDialogItemText(HWND dlg, UINT idx, const TCHAR *val) |
| 111 |
{ |
| 112 |
return SetDlgItemText(dlg,idx,val); |
| 113 |
// return my_SetWindowText(GetDlgItem(dlg, idx), val); |
| 114 |
} |
| 115 |
|
| 116 |
int NSISCALL my_GetDialogItemText(UINT idx, TCHAR *val) |
| 117 |
{ |
| 118 |
extern HWND m_curwnd; |
| 119 |
return GetDlgItemText(m_curwnd, idx, val, NSIS_MAX_STRLEN); |
| 120 |
// return my_GetWindowText(GetDlgItem(m_curwnd, idx), val, NSIS_MAX_STRLEN); |
| 121 |
} |
| 122 |
|
| 123 |
int NSISCALL my_MessageBox(const TCHAR *text, UINT type) { |
| 124 |
int _type = type & 0x001FFFFF; |
| 125 |
static MSGBOXPARAMS mbp = { |
| 126 |
sizeof(MSGBOXPARAMS), |
| 127 |
0, |
| 128 |
0, |
| 129 |
0, |
| 130 |
0, |
| 131 |
0, |
| 132 |
MAKEINTRESOURCE(IDI_ICON2), |
| 133 |
0, |
| 134 |
0, |
| 135 |
0 |
| 136 |
}; |
| 137 |
|
| 138 |
#ifdef NSIS_CONFIG_SILENT_SUPPORT |
| 139 |
// default for silent installers |
| 140 |
if (g_exec_flags.silent && type >> 21) |
| 141 |
return type >> 21; |
| 142 |
#endif |
| 143 |
// no silent or no default, just show |
| 144 |
if (g_exec_flags.rtl) |
| 145 |
_type ^= MB_RIGHT | MB_RTLREADING; |
| 146 |
|
| 147 |
mbp.hwndOwner = g_hwnd; |
| 148 |
mbp.hInstance = g_hInstance; |
| 149 |
mbp.lpszText = text; |
| 150 |
mbp.lpszCaption = g_caption; // Should call update_caption() here? |
| 151 |
mbp.dwStyle = _type; |
| 152 |
|
| 153 |
return MessageBoxIndirect(&mbp); |
| 154 |
} |
| 155 |
|
| 156 |
BOOL NSISCALL delete_with_ro_attr_handling(LPCTSTR fileordir,int flags) |
| 157 |
{ |
| 158 |
const DWORD attr=remove_ro_attr(fileordir); |
| 159 |
if (attr != INVALID_FILE_ATTRIBUTES) |
| 160 |
{ |
| 161 |
if (flags & DEL_DIR) |
| 162 |
{ |
| 163 |
if (RemoveDirectory(fileordir)) return TRUE; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
if (DeleteFile(fileordir)) return TRUE; |
| 168 |
} |
| 169 |
|
| 170 |
// Not sure if wininit.ini and MoveFileEx handle RO attr in the same |
| 171 |
// way so we just play it safe |
| 172 |
if (!(flags & DEL_REBOOT)) SetFileAttributes(fileordir,attr); |
| 173 |
} |
| 174 |
return FALSE; |
| 175 |
} |
| 176 |
|
| 177 |
void NSISCALL myDelete(TCHAR *buf, int flags) |
| 178 |
{ |
| 179 |
static TCHAR lbuf[NSIS_MAX_STRLEN]; |
| 180 |
const int rebootflag=(flags & DEL_REBOOT); |
| 181 |
|
| 182 |
HANDLE h; |
| 183 |
WIN32_FIND_DATA fd; |
| 184 |
TCHAR *fn; |
| 185 |
int valid_dir=is_valid_instpath(buf); |
| 186 |
|
| 187 |
if ((flags & DEL_SIMPLE)) |
| 188 |
{ |
| 189 |
g_exec_flags.exec_error += !DeleteFile(buf); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
#ifdef NSIS_SUPPORT_RMDIR |
| 194 |
if (!(flags & DEL_DIR) || (valid_dir && (flags & DEL_RECURSE))) |
| 195 |
#endif//NSIS_SUPPORT_RMDIR |
| 196 |
{ |
| 197 |
mystrcpy(lbuf,buf); |
| 198 |
#ifdef NSIS_SUPPORT_RMDIR |
| 199 |
if (flags & DEL_DIR) |
| 200 |
mystrcat(lbuf,_T("\\*.*")); |
| 201 |
else |
| 202 |
#endif//NSIS_SUPPORT_RMDIR |
| 203 |
trimslashtoend(buf); |
| 204 |
|
| 205 |
// only append backslash if the path isn't relative to the working directory [bug #1851273] |
| 206 |
if (*buf || *lbuf == _T('\\')) |
| 207 |
mystrcat(buf,_T("\\")); |
| 208 |
|
| 209 |
fn=buf+mystrlen(buf); |
| 210 |
|
| 211 |
h = FindFirstFile(lbuf,&fd); |
| 212 |
if (h != INVALID_HANDLE_VALUE) |
| 213 |
{ |
| 214 |
do |
| 215 |
{ |
| 216 |
TCHAR *fdfn = fd.cFileName; |
| 217 |
#ifndef _UNICODE |
| 218 |
if (*findchar(fdfn, _T('?')) && *fd.cAlternateFileName) |
| 219 |
// name contains unicode, use short name |
| 220 |
fdfn = fd.cAlternateFileName; |
| 221 |
#endif |
| 222 |
|
| 223 |
#ifdef NSIS_SUPPORT_RMDIR |
| 224 |
if (fdfn[0] == _T('.') && !fdfn[1]) continue; |
| 225 |
if (fdfn[0] == _T('.') && fdfn[1] == _T('.') && !fdfn[2]) continue; |
| 226 |
#endif//NSIS_SUPPORT_RMDIR |
| 227 |
{ |
| 228 |
mystrcpy(fn,fdfn); |
| 229 |
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 230 |
{ |
| 231 |
#ifdef NSIS_SUPPORT_RMDIR |
| 232 |
if ((flags & (DEL_DIR | DEL_RECURSE)) == (DEL_DIR | DEL_RECURSE)) |
| 233 |
{ |
| 234 |
myDelete(buf,flags); |
| 235 |
} |
| 236 |
#endif//NSIS_SUPPORT_RMDIR |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
log_printf2(_T("Delete: DeleteFile(\"%s\")"),buf); |
| 241 |
|
| 242 |
if (!delete_with_ro_attr_handling(buf,rebootflag)) |
| 243 |
{ |
| 244 |
#ifdef NSIS_SUPPORT_MOVEONREBOOT |
| 245 |
if (rebootflag) |
| 246 |
{ |
| 247 |
log_printf2(_T("Delete: DeleteFile on Reboot(\"%s\")"),buf); |
| 248 |
update_status_text(LANG_DELETEONREBOOT,buf); |
| 249 |
MoveFileOnReboot(buf,NULL); |
| 250 |
} |
| 251 |
else |
| 252 |
#endif//NSIS_SUPPORT_MOVEONREBOOT |
| 253 |
{ |
| 254 |
log_printf2(_T("Delete: DeleteFile failed(\"%s\")"),buf); |
| 255 |
g_exec_flags.exec_error++; |
| 256 |
} |
| 257 |
} |
| 258 |
else |
| 259 |
update_status_text(LANG_DELETEFILE,buf); |
| 260 |
} |
| 261 |
} |
| 262 |
} while (FindNextFile(h,&fd)); |
| 263 |
FindClose(h); |
| 264 |
} |
| 265 |
|
| 266 |
#ifdef NSIS_SUPPORT_RMDIR |
| 267 |
if (flags & DEL_DIR) |
| 268 |
fn[-1]=0; |
| 269 |
#endif//NSIS_SUPPORT_RMDIR |
| 270 |
} |
| 271 |
|
| 272 |
#ifdef NSIS_SUPPORT_RMDIR |
| 273 |
if ((flags & DEL_DIR)) |
| 274 |
{ |
| 275 |
if (!valid_dir) |
| 276 |
{ |
| 277 |
log_printf2(_T("RMDir: RemoveDirectory invalid input(\"%s\")"),buf); |
| 278 |
g_exec_flags.exec_error++; |
| 279 |
} |
| 280 |
else if (file_exists(buf)) |
| 281 |
{ |
| 282 |
addtrailingslash(buf); |
| 283 |
log_printf2(_T("RMDir: RemoveDirectory(\"%s\")"),buf); |
| 284 |
if (!delete_with_ro_attr_handling(buf,DEL_DIR|rebootflag)) |
| 285 |
{ |
| 286 |
#ifdef NSIS_SUPPORT_MOVEONREBOOT |
| 287 |
if (rebootflag) |
| 288 |
{ |
| 289 |
log_printf2(_T("RMDir: RemoveDirectory on Reboot(\"%s\")"),buf); |
| 290 |
update_status_text(LANG_DELETEONREBOOT,buf); |
| 291 |
MoveFileOnReboot(buf,NULL); |
| 292 |
} |
| 293 |
else |
| 294 |
#endif//NSIS_SUPPORT_MOVEONREBOOT |
| 295 |
{ |
| 296 |
log_printf2(_T("RMDir: RemoveDirectory failed(\"%s\")"),buf); |
| 297 |
g_exec_flags.exec_error++; |
| 298 |
} |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
update_status_text(LANG_REMOVEDIR,buf); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
#endif//NSIS_SUPPORT_RMDIR |
| 307 |
} |
| 308 |
|
| 309 |
TCHAR *NSISCALL addtrailingslash(TCHAR *str) |
| 310 |
{ |
| 311 |
if (lastchar(str)!=_T('\\')) mystrcat(str,_T("\\")); |
| 312 |
return str; |
| 313 |
} |
| 314 |
|
| 315 |
/*char NSISCALL lastchar(const char *str) |
| 316 |
{ |
| 317 |
return *CharPrev(str,str+mystrlen(str)); |
| 318 |
}*/ |
| 319 |
|
| 320 |
TCHAR * NSISCALL findchar(TCHAR *str, TCHAR c) |
| 321 |
{ |
| 322 |
while (*str && *str != c) |
| 323 |
{ |
| 324 |
str = CharNext(str); |
| 325 |
} |
| 326 |
return str; |
| 327 |
} |
| 328 |
|
| 329 |
// Separates a full path to the directory portion and file name portion |
| 330 |
// and returns the pointer to the filename portion. |
| 331 |
TCHAR * NSISCALL trimslashtoend(TCHAR *buf) |
| 332 |
{ |
| 333 |
TCHAR *p = buf + mystrlen(buf); |
| 334 |
do |
| 335 |
{ |
| 336 |
if (*p == _T('\\')) |
| 337 |
break; |
| 338 |
p = CharPrev(buf, p); |
| 339 |
} while (p > buf); |
| 340 |
|
| 341 |
*p = 0; |
| 342 |
|
| 343 |
return p + 1; |
| 344 |
} |
| 345 |
|
| 346 |
int NSISCALL validpathspec(TCHAR *ubuf) |
| 347 |
{ |
| 348 |
TCHAR dl = ubuf[0] | 0x20; // convert alleged drive letter to lower case |
| 349 |
return ((ubuf[0] == _T('\\') && ubuf[1] == _T('\\')) || |
| 350 |
(dl >= _T('a') && dl <= _T('z') && ubuf[1] == _T(':'))); |
| 351 |
} |
| 352 |
|
| 353 |
TCHAR * NSISCALL skip_root(TCHAR *path) |
| 354 |
{ |
| 355 |
TCHAR *p = CharNext(path); |
| 356 |
TCHAR *p2 = CharNext(p); |
| 357 |
|
| 358 |
if (*path && p[0] == _T(':') && p[1] == _T('\\')) |
| 359 |
{ |
| 360 |
return CharNext(p2); |
| 361 |
} |
| 362 |
else if (path[0] == _T('\\') && path[1] == _T('\\')) |
| 363 |
{ |
| 364 |
// skip host and share name |
| 365 |
int x = 2; |
| 366 |
while (x--) |
| 367 |
{ |
| 368 |
p2 = findchar(p2, _T('\\')); |
| 369 |
if (!*p2) |
| 370 |
return NULL; |
| 371 |
p2++; // skip backslash |
| 372 |
} |
| 373 |
|
| 374 |
return p2; |
| 375 |
} |
| 376 |
else |
| 377 |
return NULL; |
| 378 |
} |
| 379 |
|
| 380 |
int NSISCALL is_valid_instpath(TCHAR *s) |
| 381 |
{ |
| 382 |
static TCHAR tmp[NSIS_MAX_STRLEN]; |
| 383 |
TCHAR *root; |
| 384 |
|
| 385 |
mystrcpy(tmp, s); |
| 386 |
|
| 387 |
root = skip_root(tmp); |
| 388 |
|
| 389 |
if (!root) |
| 390 |
return 0; |
| 391 |
|
| 392 |
// must be called after skip_root or AllowRootDirInstall won't work. |
| 393 |
// validate_filename removes trailing backslashes and so converts |
| 394 |
// "C:\" to "C:" which is not a valid directory. skip_root returns |
| 395 |
// NULL for "C:" so the above test returns 0. |
| 396 |
// validate_filename is called so directories such as "C:\ " will |
| 397 |
// not pass as a valid non-root directory. |
| 398 |
validate_filename(root); |
| 399 |
|
| 400 |
if ((g_flags & CH_FLAGS_NO_ROOT_DIR) && (!*root || *root == _T('\\'))) |
| 401 |
return 0; |
| 402 |
|
| 403 |
while (mystrlen(tmp) > root - tmp) |
| 404 |
{ |
| 405 |
WIN32_FIND_DATA *fd = file_exists(tmp); |
| 406 |
// if the directory bit not set then it's a file, which is not a valid inst dir... |
| 407 |
// GetFileAttributes is not used because it doesn't work with certain files (error 32) |
| 408 |
// as for concerns of the user using * or ?, that's invalid anyway... |
| 409 |
if (fd && !(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) |
| 410 |
return 0; |
| 411 |
trimslashtoend(tmp); |
| 412 |
} |
| 413 |
|
| 414 |
// if the root drive exists |
| 415 |
addtrailingslash(tmp); // don't check the current directory, check the root directory |
| 416 |
if (GetFileAttributes(tmp) == INVALID_FILE_ATTRIBUTES) |
| 417 |
return 0; |
| 418 |
|
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
// Used strictly for the wininit.ini file which is an ASCII file. |
| 423 |
char * NSISCALL mystrstriA(char *a, const char *b) |
| 424 |
{ |
| 425 |
int l = lstrlenA(b); |
| 426 |
while (lstrlenA(a) >= l) |
| 427 |
{ |
| 428 |
char c = a[l]; |
| 429 |
a[l] = 0; |
| 430 |
if (!lstrcmpiA(a, b)) |
| 431 |
{ |
| 432 |
a[l] = c; |
| 433 |
return a; |
| 434 |
} |
| 435 |
a[l] = c; |
| 436 |
a = CharNextA(a); |
| 437 |
} |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
#ifndef _NSIS_NODEFLIB_CRTMEMCPY |
| 443 |
// mini_memcpy takes the number of bytes to copy. |
| 444 |
void NSISCALL mini_memcpy(void *out, const void *in, UINT_PTR cb) |
| 445 |
{ |
| 446 |
char *dst = (char*) out, *src = (char*) in; |
| 447 |
while (cb-- > 0) *dst++ = *src++; |
| 448 |
} |
| 449 |
#endif |
| 450 |
|
| 451 |
DWORD NSISCALL remove_ro_attr(LPCTSTR file) |
| 452 |
{ |
| 453 |
const DWORD attr = GetFileAttributes(file); |
| 454 |
if (attr != INVALID_FILE_ATTRIBUTES) |
| 455 |
SetFileAttributes(file,attr&(~FILE_ATTRIBUTE_READONLY)); |
| 456 |
return attr; |
| 457 |
} |
| 458 |
|
| 459 |
HANDLE NSISCALL myOpenFile(const TCHAR *fn, DWORD da, DWORD cd) |
| 460 |
{ |
| 461 |
int attr = GetFileAttributes(fn); |
| 462 |
return CreateFile( |
| 463 |
fn, |
| 464 |
da, |
| 465 |
FILE_SHARE_READ, |
| 466 |
NULL, |
| 467 |
cd, |
| 468 |
attr == INVALID_FILE_ATTRIBUTES ? 0 : attr, |
| 469 |
NULL |
| 470 |
); |
| 471 |
} |
| 472 |
|
| 473 |
TCHAR * NSISCALL my_GetTempFileName(TCHAR *buf, const TCHAR *dir) |
| 474 |
{ |
| 475 |
int n = 100; |
| 476 |
while (n--) |
| 477 |
{ |
| 478 |
TCHAR prefix[4] = _T("nsa"); |
| 479 |
prefix[2] += (TCHAR)(GetTickCount() % 26); |
| 480 |
if (GetTempFileName(dir, prefix, 0, buf)) |
| 481 |
return buf; |
| 482 |
} |
| 483 |
*buf = 0; |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
BOOL NSISCALL myReadFile(HANDLE h, LPVOID buf, DWORD cb) |
| 488 |
{ |
| 489 |
DWORD cbio; |
| 490 |
return ReadFile(h, buf, cb, &cbio, NULL) && cb == cbio; |
| 491 |
} |
| 492 |
|
| 493 |
BOOL NSISCALL myWriteFile(HANDLE h, const void*buf, DWORD cb) |
| 494 |
{ |
| 495 |
DWORD cbio; |
| 496 |
return WriteFile(h, buf, cb, &cbio, NULL) && cb == cbio; |
| 497 |
} |
| 498 |
|
| 499 |
// Reading skips the BOM if present, writing writes it to a empty file |
| 500 |
HRESULT NSISCALL UTF16LEBOM(HANDLE h, INT_PTR ForWrite) |
| 501 |
{ |
| 502 |
DWORD orgpos = SetFilePointer(h, 0, NULL, FILE_CURRENT); |
| 503 |
if (0 == orgpos) |
| 504 |
{ |
| 505 |
BYTE bom[2]; |
| 506 |
if (myReadFile(h, bom, 2) && (0xfeff == *(USHORT*) &bom[0])) |
| 507 |
{ |
| 508 |
return S_OK; |
| 509 |
} |
| 510 |
else if (ForWrite) |
| 511 |
{ |
| 512 |
if (0 == SetFilePointer(h, 0, NULL, FILE_CURRENT)) // Is the file empty? |
| 513 |
{ |
| 514 |
static const BYTE bom16le[] = { 0xff, 0xfe }; |
| 515 |
return myWriteFile(h, bom16le, 2) ? S_OK : E_FAIL; |
| 516 |
} |
| 517 |
} |
| 518 |
SetFilePointer(h, 0, NULL, FILE_BEGIN); // The file may have started with something that was not a BOM, undo the read |
| 519 |
} |
| 520 |
return S_FALSE; |
| 521 |
} |
| 522 |
|
| 523 |
#ifdef NSIS_SUPPORT_MOVEONREBOOT |
| 524 |
#ifndef _WIN64 |
| 525 |
/** Modifies the wininit.ini file to rename / delete a file. |
| 526 |
* |
| 527 |
* @param prevName The previous / current name of the file. |
| 528 |
* @param newName The new name to move the file to. If NULL, the current file |
| 529 |
* will be deleted. |
| 530 |
*/ |
| 531 |
void RenameViaWininit(const TCHAR* prevName, const TCHAR* newName) |
| 532 |
{ |
| 533 |
static char szRenameLine[1024]; |
| 534 |
static TCHAR wininit[1024]; |
| 535 |
static TCHAR tmpbuf[1024]; |
| 536 |
|
| 537 |
int cchRenameLine; |
| 538 |
LPCSTR szRenameSec = "[Rename]\r\n"; // rename section marker |
| 539 |
HANDLE hfile; |
| 540 |
DWORD dwFileSize, dwRenameLinePos; |
| 541 |
char *pszWinInit; // Contains the file contents of wininit.ini |
| 542 |
|
| 543 |
int spn; // length of the short path name in TCHARs. |
| 544 |
|
| 545 |
// Optimized mystrcpy(tmpbuf, _T("NUL")): |
| 546 |
if (sizeof(TCHAR) == 1) |
| 547 |
*(UINT32*)tmpbuf = ((UINT32)'N' << 0) | ((UINT32)'U' << 8) | ((UINT32)'L' << 16) | ((UINT32)'\0' << 24); |
| 548 |
else |
| 549 |
*(UINT64*)tmpbuf = ((UINT64)'N' << 0) | ((UINT64)'U' << 16) | ((UINT64)'L' << 32) | ((UINT64)'\0' << 48); |
| 550 |
|
| 551 |
if (newName) { |
| 552 |
// create the file if it's not already there to prevent GetShortPathName from failing |
| 553 |
CloseHandle(myOpenFile(newName,0,CREATE_NEW)); |
| 554 |
spn = GetShortPathName(newName,tmpbuf,1024); |
| 555 |
if (!spn || spn > 1024) |
| 556 |
return; |
| 557 |
} |
| 558 |
// wininit is used as a temporary here |
| 559 |
spn = GetShortPathName(prevName,wininit,1024); |
| 560 |
if (!spn || spn > 1024) |
| 561 |
return; |
| 562 |
#ifdef _UNICODE |
| 563 |
cchRenameLine = wsprintfA(szRenameLine, "%ls=%ls\r\n", tmpbuf, wininit); |
| 564 |
#else |
| 565 |
cchRenameLine = wsprintfA(szRenameLine, "%s=%s\r\n", tmpbuf, wininit); |
| 566 |
#endif |
| 567 |
// Get the path to the wininit.ini file. |
| 568 |
GetNSISString(wininit, g_header->str_wininit); |
| 569 |
|
| 570 |
hfile = myOpenFile(wininit, GENERIC_READ | GENERIC_WRITE, OPEN_ALWAYS); |
| 571 |
|
| 572 |
if (hfile != INVALID_HANDLE_VALUE) |
| 573 |
{ |
| 574 |
// We are now working on the Windows wininit file |
| 575 |
dwFileSize = GetFileSize(hfile, NULL); |
| 576 |
pszWinInit = (char*) GlobalAlloc(GPTR, dwFileSize + cchRenameLine + 10); |
| 577 |
|
| 578 |
if (pszWinInit != NULL) |
| 579 |
{ |
| 580 |
if (myReadFile(hfile, pszWinInit, dwFileSize)) |
| 581 |
{ |
| 582 |
// Look for the rename section in the current file. |
| 583 |
LPSTR pszRenameSecInFile = mystrstriA(pszWinInit, szRenameSec); |
| 584 |
if (pszRenameSecInFile == NULL) |
| 585 |
{ |
| 586 |
// No rename section. So we add it to the end of file. |
| 587 |
lstrcpyA(pszWinInit+dwFileSize, szRenameSec); |
| 588 |
dwFileSize += 10; |
| 589 |
dwRenameLinePos = dwFileSize; |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
// There is a rename section, but is there another section after it? |
| 594 |
char *pszFirstRenameLine = pszRenameSecInFile+10; |
| 595 |
char *pszNextSec = mystrstriA(pszFirstRenameLine,"\n["); |
| 596 |
if (pszNextSec) |
| 597 |
{ |
| 598 |
char *p = pszWinInit + dwFileSize; |
| 599 |
char *pEnd = pszWinInit + dwFileSize + cchRenameLine; |
| 600 |
|
| 601 |
while (p > pszNextSec) |
| 602 |
{ |
| 603 |
*pEnd-- = *p--; |
| 604 |
} |
| 605 |
|
| 606 |
dwRenameLinePos = BUGBUG64TRUNCATE(DWORD, pszNextSec - pszWinInit) + 1; // +1 for the \n |
| 607 |
} |
| 608 |
// rename section is last, stick item at end of file |
| 609 |
else dwRenameLinePos = dwFileSize; |
| 610 |
} |
| 611 |
|
| 612 |
mini_memcpy(&pszWinInit[dwRenameLinePos], szRenameLine, cchRenameLine); |
| 613 |
dwFileSize += cchRenameLine; |
| 614 |
|
| 615 |
SetFilePointer(hfile, 0, NULL, FILE_BEGIN); |
| 616 |
myWriteFile(hfile, pszWinInit, dwFileSize); |
| 617 |
|
| 618 |
GlobalFree(pszWinInit); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
CloseHandle(hfile); |
| 623 |
} |
| 624 |
} |
| 625 |
#endif |
| 626 |
|
| 627 |
/** |
| 628 |
* MoveFileOnReboot tries to move a file by the name of pszExisting to the |
| 629 |
* name pszNew. |
| 630 |
* |
| 631 |
* @param pszExisting The old name of the file. |
| 632 |
* @param pszNew The new name of the file. |
| 633 |
*/ |
| 634 |
void NSISCALL MoveFileOnReboot(LPCTSTR pszExisting, LPCTSTR pszNew) |
| 635 |
{ |
| 636 |
#ifndef _WIN64 // Shut up GCC unused warning |
| 637 |
BOOL fOk = |
| 638 |
#endif |
| 639 |
MoveFileEx(pszExisting, pszNew, MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); |
| 640 |
#ifndef _WIN64 |
| 641 |
if (!fOk) |
| 642 |
{ |
| 643 |
RenameViaWininit(pszExisting, pszNew); |
| 644 |
} |
| 645 |
#endif |
| 646 |
|
| 647 |
#ifdef NSIS_SUPPORT_REBOOT |
| 648 |
g_exec_flags.exec_reboot++; |
| 649 |
#endif |
| 650 |
} |
| 651 |
#endif |
| 652 |
|
| 653 |
#define GetAltViewREGSAM() ( sizeof(void*) > 4 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY ) |
| 654 |
HKEY NSISCALL GetRegKeyAndSAM(HKEY hKey, REGSAM*pRS) |
| 655 |
{ |
| 656 |
const REGSAM samviewmask = (KEY_WOW64_32KEY|KEY_WOW64_64KEY); |
| 657 |
const REGSAM incompatsamview = SystemSupportsAltRegView() ? 0 : GetAltViewREGSAM(); |
| 658 |
REGSAM sam = *pRS, incompatsam = incompatsamview; |
| 659 |
#ifdef C_ASSERT |
| 660 |
{C_ASSERT(REGROOTVIEWTOSAMVIEW(REGROOTVIEW32|REGROOTVIEW64) == (KEY_WOW64_32KEY|KEY_WOW64_64KEY));} |
| 661 |
#endif |
| 662 |
if ((sam & KEY_FORCEVIEW) && IsRegRootkeyForcedView(hKey)) |
| 663 |
{ |
| 664 |
REGSAM keysamview = REGROOTVIEWTOSAMVIEW(hKey); |
| 665 |
if (keysamview == samviewmask) keysamview = (g_exec_flags.alter_reg_view & ~incompatsamview); // HKxxANY tries to honor SetRegView |
| 666 |
sam &= ~samviewmask, sam |= (keysamview & ~(sizeof(void*) > 4 ? 0 : KEY_WOW64_32KEY)); // HKxx32 has the *_32KEY bit set but WinNT4&2000 cannot handle any KEY_WOW64_xxKEY flags. |
| 667 |
hKey = (HKEY) ( (UINT_PTR) hKey & ~(REGROOTVIEW32|REGROOTVIEW64) ); |
| 668 |
} |
| 669 |
else if (sam & KEY_ALTERVIEW) |
| 670 |
{ |
| 671 |
sam |= g_exec_flags.alter_reg_view; // We don't mask away the incompatsamview bits because the operation is supposed to fail if the view is not supported. |
| 672 |
} |
| 673 |
*pRS = sam & ~(NSIS_REGSAM_PRIVATEMASK); // Filter away internal flags |
| 674 |
return (incompatsam & sam) ? NULL : hKey; // Fail if the requested view is not supported |
| 675 |
} |
| 676 |
LONG NSISCALL RegKeyOpen(HKEY hBase, LPCTSTR SubKey, REGSAM RS, HKEY*phKey) |
| 677 |
{ |
| 678 |
if (!(hBase = GetRegKeyAndSAM(hBase, &RS))) return ERROR_INVALID_HANDLE; // ERROR_CANTOPEN? |
| 679 |
return RegOpenKeyEx(hBase, SubKey, 0, RS, phKey); |
| 680 |
} |
| 681 |
LONG NSISCALL RegKeyCreate(HKEY hBase, LPCTSTR SubKey, REGSAM RS, HKEY*phKey) |
| 682 |
{ |
| 683 |
if (!(hBase = GetRegKeyAndSAM(hBase, &RS))) return ERROR_INVALID_HANDLE; // ERROR_CANTOPEN? |
| 684 |
return RegCreateKeyEx(hBase, SubKey, 0, 0, 0, RS, 0, phKey, 0); |
| 685 |
} |
| 686 |
|
| 687 |
void NSISCALL myRegGetStr(HKEY root, const TCHAR *sub, const TCHAR *name, TCHAR *out, UINT altview) |
| 688 |
{ |
| 689 |
HKEY hKey; |
| 690 |
DWORD cb = NSIS_MAX_STRLEN*sizeof(TCHAR), rt, ec; |
| 691 |
REGSAM samview = altview ? GetAltViewREGSAM() : 0; |
| 692 |
if ((ec = RegKeyOpen(root, sub, KEY_READ|samview, &hKey)) == ERROR_SUCCESS) |
| 693 |
{ |
| 694 |
ec = RegQueryValueEx(hKey, name, NULL, &rt, (LPBYTE)out, &cb); |
| 695 |
RegCloseKey(hKey); |
| 696 |
out[NSIS_MAX_STRLEN-1] = 0; // Make sure the string is terminated. This could potentially truncate a long string by 1 character! |
| 697 |
} |
| 698 |
if (ec != ERROR_SUCCESS || (rt != REG_SZ && rt != REG_EXPAND_SZ)) *out = 0; // Empty string on failure |
| 699 |
} |
| 700 |
|
| 701 |
void NSISCALL iptrtostr(TCHAR *s, INT_PTR d) |
| 702 |
{ |
| 703 |
#ifdef _WIN64 |
| 704 |
static const TCHAR c[] = _T("%Id"); |
| 705 |
#else |
| 706 |
static const TCHAR c[] = _T("%d"); |
| 707 |
#endif |
| 708 |
wsprintf(s,c,d); |
| 709 |
} |
| 710 |
|
| 711 |
INT_PTR NSISCALL strtoiptr(const TCHAR *s) |
| 712 |
{ |
| 713 |
UINT_PTR v=0; |
| 714 |
INT_PTR sign=1; // sign of positive |
| 715 |
TCHAR m=10; // base of 10 |
| 716 |
TCHAR t=_T('9'); // cap top of numbers at 9 |
| 717 |
|
| 718 |
if (*s == _T('-')) |
| 719 |
{ |
| 720 |
s++; //skip over - |
| 721 |
sign=-1; // sign flip |
| 722 |
} |
| 723 |
|
| 724 |
if (*s == _T('0')) |
| 725 |
{ |
| 726 |
s++; // skip over 0 |
| 727 |
if (s[0] >= _T('0') && s[0] <= _T('7')) |
| 728 |
{ |
| 729 |
m=8; // base of 8 |
| 730 |
t=_T('7'); // cap top at 7 |
| 731 |
} |
| 732 |
if ((s[0] & ~0x20) == _T('X')) |
| 733 |
{ |
| 734 |
m=16; // base of 16 |
| 735 |
s++; // advance over 'x' |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
for (;;) |
| 740 |
{ |
| 741 |
int c=*s++; |
| 742 |
if (c >= _T('0') && c <= t) c-=_T('0'); |
| 743 |
// clever little trick to do both upper and lowercase A-F. |
| 744 |
else if (m==16 && (c & ~0x20) >= _T('A') && (c & ~0x20) <= _T('F')) c = (c & 7) + 9; |
| 745 |
else break; |
| 746 |
v*=m; |
| 747 |
v+=c; |
| 748 |
} |
| 749 |
return ((INT_PTR)v)*sign; |
| 750 |
} |
| 751 |
|
| 752 |
// Straight copies of selected shell functions. Calling local functions |
| 753 |
// requires less code than DLL functions. For the savings to outweigh the cost |
| 754 |
// of a new function there should be about a couple of dozen or so calls. |
| 755 |
TCHAR * NSISCALL mystrcpy(TCHAR *out, const TCHAR *in) |
| 756 |
{ |
| 757 |
return lstrcpyn(out, in, NSIS_MAX_STRLEN); |
| 758 |
} |
| 759 |
|
| 760 |
int NSISCALL mystrlen(const TCHAR *in) |
| 761 |
{ |
| 762 |
return lstrlen(in); |
| 763 |
} |
| 764 |
|
| 765 |
TCHAR * NSISCALL mystrcat(TCHAR *out, const TCHAR *concat) |
| 766 |
{ |
| 767 |
return lstrcat(out, concat); |
| 768 |
} |
| 769 |
|
| 770 |
int StrWideToACP(LPCWSTR Src, char* Dst, int DstCap) |
| 771 |
{ |
| 772 |
return WideCharToMultiByte(CP_ACP, 0, Src, -1, Dst, DstCap, NULL, NULL); |
| 773 |
} |
| 774 |
|
| 775 |
#ifndef UNICODE |
| 776 |
void strcpyWideToT(TCHAR *out, LPCWSTR in) |
| 777 |
{ |
| 778 |
StrWideToACP(in, out, NSIS_MAX_STRLEN); |
| 779 |
} |
| 780 |
#endif |
| 781 |
|
| 782 |
#if !defined(_WIN64) && !defined(UNICODE) |
| 783 |
HRESULT ComIIDFromString(LPCTSTR str, IID*out) |
| 784 |
{ |
| 785 |
WCHAR buf[130]; |
| 786 |
signed char i; |
| 787 |
for (i = 0; i >= 0; ++i) |
| 788 |
if (!(buf[i] = str[i])) |
| 789 |
return IIDFromString(buf, out); |
| 790 |
return E_FAIL; |
| 791 |
} |
| 792 |
#endif |
| 793 |
|
| 794 |
TCHAR ps_tmpbuf[NSIS_MAX_STRLEN*2]; |
| 795 |
|
| 796 |
const TCHAR SYSREGKEY[] = _T("Software\\Microsoft\\Windows\\CurrentVersion"); |
| 797 |
const TCHAR QUICKLAUNCH[] = _T("\\Microsoft\\Internet Explorer\\Quick Launch"); |
| 798 |
|
| 799 |
typedef HRESULT (WINAPI* PFNSHGETFOLDERPATH)(HWND, int, HANDLE, DWORD, LPTSTR); |
| 800 |
extern void *g_SHGetFolderPath; |
| 801 |
typedef LPITEMIDLIST (WINAPI* PFNSHCLONESPECIALIDLIST)(HWND, int, BOOL); |
| 802 |
|
| 803 |
// Based on Dave Laundon's simplified process_string |
| 804 |
// The string actually has a lot of different data encoded into it. This |
| 805 |
// function extracts the special data out and puts it into outbuf. |
| 806 |
TCHAR * NSISCALL GetNSISString(TCHAR *outbuf, int strtab) |
| 807 |
{ |
| 808 |
// This looks at the g_block (copied from header->blocks) and |
| 809 |
// indexes into the language |
| 810 |
TCHAR *in = (TCHAR*)GetNSISStringNP(GetNSISTab(strtab)); |
| 811 |
TCHAR *out = ps_tmpbuf; |
| 812 |
|
| 813 |
// Still working within ps_tmpbuf, so set out to the |
| 814 |
// current position that is passed in. |
| 815 |
if (outbuf >= ps_tmpbuf && |
| 816 |
(size_t) (outbuf - ps_tmpbuf) < COUNTOF(ps_tmpbuf)) |
| 817 |
{ |
| 818 |
out = outbuf; |
| 819 |
outbuf = 0; |
| 820 |
} |
| 821 |
|
| 822 |
while (*in && out - ps_tmpbuf < NSIS_MAX_STRLEN) |
| 823 |
{ |
| 824 |
_TUCHAR nVarIdx = (_TUCHAR)*in++; |
| 825 |
int nData; |
| 826 |
int fldrs[4]; |
| 827 |
if (nVarIdx < NS_SKIP_CODE) |
| 828 |
{ |
| 829 |
// The next 2 BYTEs in the string might be coding either a value 0..MAX_CODED (nData), or 2 CSIDL of Special folders (for NS_SHELL_CODE) |
| 830 |
nData = DECODE_SHORT(in); |
| 831 |
// There are 2 CSIDL parameters for each context and query must be used before create |
| 832 |
// because of bug #820 (CSIDL_FLAG_CREATE failures on root paths are cached in Vista). |
| 833 |
#ifdef _UNICODE |
| 834 |
fldrs[1] = LOBYTE(*in); // current user |
| 835 |
fldrs[0] = fldrs[1] | CSIDL_FLAG_CREATE; |
| 836 |
fldrs[3] = HIBYTE(*in); // all users |
| 837 |
fldrs[2] = fldrs[3] | CSIDL_FLAG_CREATE; |
| 838 |
#else |
| 839 |
fldrs[0] = in[0] | CSIDL_FLAG_CREATE; // current user |
| 840 |
fldrs[1] = in[0]; |
| 841 |
fldrs[2] = in[1] | CSIDL_FLAG_CREATE; // all users |
| 842 |
fldrs[3] = in[1]; |
| 843 |
#endif |
| 844 |
in += sizeof(SHORT)/sizeof(TCHAR); |
| 845 |
|
| 846 |
if (nVarIdx == NS_SHELL_CODE) |
| 847 |
{ |
| 848 |
LPITEMIDLIST idl; |
| 849 |
|
| 850 |
int x = 2; |
| 851 |
BOOL isWin9598 = IsWin9598(); |
| 852 |
/* |
| 853 |
SHGetFolderPath as provided by shfolder.dll is used to get special folders |
| 854 |
unless the installer is running on Windows 95/98. For 95/98 shfolder.dll is |
| 855 |
only used for the Application Data and Documents folder (if the DLL exists). |
| 856 |
Otherwise, the old SHGetSpecialFolderLocation API is called. |
| 857 |
|
| 858 |
The reason for not using shfolder.dll for all folders on 95/98 is that some |
| 859 |
unsupported folders (such as the Start Menu folder for all users) are |
| 860 |
simulated instead of returning an error so we can fall back on the current |
| 861 |
user folder. |
| 862 |
|
| 863 |
SHGetFolderPath in shell32.dll could be called directly for Windows versions |
| 864 |
later than 95/98 but there is no need to do so, because shfolder.dll is still |
| 865 |
provided and calls shell32.dll. |
| 866 |
*/ |
| 867 |
BOOL use_shfolder = |
| 868 |
// Use shfolder if not on 95/98 |
| 869 |
!isWin9598 || |
| 870 |
// Unless the Application Data or Documents folder is requested |
| 871 |
( |
| 872 |
(fldrs[3] == CSIDL_COMMON_APPDATA) || |
| 873 |
(fldrs[3] == CSIDL_COMMON_DOCUMENTS) |
| 874 |
); |
| 875 |
|
| 876 |
/* Carry on... shfolder stuff is over. */ |
| 877 |
|
| 878 |
if (g_exec_flags.all_user_var) |
| 879 |
{ |
| 880 |
x = 4; // Get common folder > Create common folder > Get user folder > Create user folder |
| 881 |
} |
| 882 |
|
| 883 |
if (fldrs[1] & 0x80) |
| 884 |
{ |
| 885 |
myRegGetStr(HKEY_LOCAL_MACHINE, SYSREGKEY, GetNSISStringNP(fldrs[1] & 0x3F), out, fldrs[1] & 0x40); |
| 886 |
if (!*out) |
| 887 |
GetNSISString(out, fldrs[3]); |
| 888 |
x = 0; |
| 889 |
} |
| 890 |
else if (fldrs[1] == CSIDL_SYSTEM) // Does not work on 95, 98 nor NT4. Works on ME and 2000+. |
| 891 |
{ |
| 892 |
GetSystemDirectory(out, NSIS_MAX_STRLEN); |
| 893 |
x = 0; |
| 894 |
} |
| 895 |
else if (fldrs[1] == CSIDL_WINDOWS) // Does not work on 95, 98 nor NT4. Works on ME and 2000+. |
| 896 |
{ |
| 897 |
GetWindowsDirectory(out, NSIS_MAX_STRLEN); |
| 898 |
x = 0; |
| 899 |
} |
| 900 |
|
| 901 |
while (x--) |
| 902 |
{ |
| 903 |
PFNSHCLONESPECIALIDLIST SHCSILFunc; |
| 904 |
int idandflags = fldrs[x] & ~0x40, create = idandflags & CSIDL_FLAG_CREATE; |
| 905 |
|
| 906 |
if (g_SHGetFolderPath && use_shfolder) |
| 907 |
{ |
| 908 |
PFNSHGETFOLDERPATH SHGetFolderPathFunc = (PFNSHGETFOLDERPATH) g_SHGetFolderPath; |
| 909 |
if (!SHGetFolderPathFunc(g_hwnd, idandflags, NULL, SHGFP_TYPE_CURRENT, out)) |
| 910 |
{ |
| 911 |
break; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
// This function exists in 95 & NT4 but was undocumented back then so we have to import it by ordinal. |
| 916 |
SHCSILFunc = (PFNSHCLONESPECIALIDLIST) myGetProcAddress(MGA_SHCloneSpecialIDList); |
| 917 |
idl = SHCSILFunc(g_hwnd, LOBYTE(idandflags), create); |
| 918 |
if (idl) |
| 919 |
{ |
| 920 |
BOOL res = SHGetPathFromIDList(idl, out); |
| 921 |
CoTaskMemFree(idl); |
| 922 |
if (res) break; |
| 923 |
} |
| 924 |
|
| 925 |
*out=0; |
| 926 |
} |
| 927 |
|
| 928 |
if (*out) |
| 929 |
{ |
| 930 |
// all users' version is CSIDL_APPDATA only for $QUICKLAUNCH |
| 931 |
// for normal $APPDATA, it'd be CSIDL_APPDATA_COMMON |
| 932 |
if (fldrs[3] == CSIDL_APPDATA) |
| 933 |
{ |
| 934 |
mystrcat(out, QUICKLAUNCH); // append suffix path for $QUICKLAUNCH |
| 935 |
} |
| 936 |
} |
| 937 |
validate_filename(out); |
| 938 |
} |
| 939 |
else if (nVarIdx == NS_VAR_CODE) |
| 940 |
{ |
| 941 |
if (nData == 29) // $HWNDPARENT |
| 942 |
iptrtostr(out, (INT_PTR) g_hwnd); |
| 943 |
else |
| 944 |
mystrcpy(out, g_usrvars[nData]); |
| 945 |
// validate the directory name |
| 946 |
if ((unsigned int)(nData - 21) < 7) { |
| 947 |
// validate paths for $INSTDIR, $OUTDIR, $EXEDIR, $LANGUAGE, $TEMP, $PLUGINSDIR and $EXEPATH |
| 948 |
// $LANGUAGE is just a number anyway... |
| 949 |
validate_filename(out); |
| 950 |
} |
| 951 |
} // == VAR_CODES_START |
| 952 |
else if (nVarIdx == NS_LANG_CODE) |
| 953 |
{ |
| 954 |
GetNSISString(out, -nData-1); |
| 955 |
} |
| 956 |
out += mystrlen(out); |
| 957 |
} |
| 958 |
else if (nVarIdx == NS_SKIP_CODE) |
| 959 |
{ |
| 960 |
*out++ = *in++; |
| 961 |
} |
| 962 |
else // Normal char |
| 963 |
{ |
| 964 |
*out++ = nVarIdx; |
| 965 |
} |
| 966 |
} // while |
| 967 |
*out = 0; |
| 968 |
if (outbuf) |
| 969 |
return mystrcpy(outbuf, ps_tmpbuf); |
| 970 |
return ps_tmpbuf; |
| 971 |
} |
| 972 |
|
| 973 |
void NSISCALL validate_filename(TCHAR *in) { |
| 974 |
TCHAR *nono = _T("*?|<>/\":"); |
| 975 |
TCHAR *out; |
| 976 |
TCHAR *out_save; |
| 977 |
|
| 978 |
// ignoring spaces is wrong, _T(" C:\blah") is invalid |
| 979 |
//while (*in == _T(' ')) in = CharNext(in); |
| 980 |
|
| 981 |
if (in[0] == _T('\\') && in[1] == _T('\\') && in[2] == _T('?') && in[3] == _T('\\')) |
| 982 |
{ |
| 983 |
// at least four bytes |
| 984 |
in += 4; |
| 985 |
} |
| 986 |
if (*in) |
| 987 |
{ |
| 988 |
// at least two bytes |
| 989 |
if (validpathspec(in)) in += 2; |
| 990 |
} |
| 991 |
out = out_save = in; |
| 992 |
while (*in) |
| 993 |
{ |
| 994 |
if ((_TUCHAR)*in > 31 && !*findchar(nono, *in)) |
| 995 |
{ |
| 996 |
mini_memcpy(out, in, CharNext(in) - in); |
| 997 |
out = CharNext(out); |
| 998 |
} |
| 999 |
in = CharNext(in); |
| 1000 |
} |
| 1001 |
*out = 0; |
| 1002 |
// now trim rightmost backslashes & spaces |
| 1003 |
do |
| 1004 |
{ |
| 1005 |
out = CharPrev(out_save, out); |
| 1006 |
if (*out == _T(' ') || *out == _T('\\')) |
| 1007 |
*out = 0; |
| 1008 |
else |
| 1009 |
break; |
| 1010 |
} while (out_save < out); |
| 1011 |
} |
| 1012 |
|
| 1013 |
#ifdef NSIS_CONFIG_LOG |
| 1014 |
int log_dolog; |
| 1015 |
TCHAR log_text[2048]; // 1024 for each wsprintf |
| 1016 |
|
| 1017 |
#if !defined(NSIS_CONFIG_LOG_ODS) && !defined(NSIS_CONFIG_LOG_STDOUT) |
| 1018 |
void NSISCALL log_write(int close) |
| 1019 |
{ |
| 1020 |
static HANDLE fp=INVALID_HANDLE_VALUE; |
| 1021 |
if (close) |
| 1022 |
{ |
| 1023 |
if (fp!=INVALID_HANDLE_VALUE) |
| 1024 |
{ |
| 1025 |
CloseHandle(fp); |
| 1026 |
} |
| 1027 |
fp=INVALID_HANDLE_VALUE; |
| 1028 |
return; |
| 1029 |
} |
| 1030 |
if (log_dolog) |
| 1031 |
{ |
| 1032 |
if (g_log_file[0] && fp==INVALID_HANDLE_VALUE) |
| 1033 |
{ |
| 1034 |
fp = myOpenFile(g_log_file,GENERIC_WRITE,OPEN_ALWAYS); |
| 1035 |
if (fp!=INVALID_HANDLE_VALUE) |
| 1036 |
SetFilePointer(fp,0,NULL,FILE_END); |
| 1037 |
} |
| 1038 |
if (fp!=INVALID_HANDLE_VALUE) |
| 1039 |
{ |
| 1040 |
mystrcat(log_text,_T("\r\n")); |
| 1041 |
myWriteFile(fp,log_text,mystrlen(log_text)*sizeof(TCHAR)); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
#endif//!NSIS_CONFIG_LOG_ODS && !NSIS_CONFIG_LOG_STDOUT |
| 1046 |
|
| 1047 |
const TCHAR * _RegKeyHandleToName(HKEY hKey) |
| 1048 |
{ |
| 1049 |
if (hKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT"); |
| 1050 |
if (hKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER"); |
| 1051 |
if (hKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE"); |
| 1052 |
if (hKey == HKEY_USERS) return _T("HKEY_USERS"); |
| 1053 |
if (hKey == HKEY_PERFORMANCE_DATA) return _T("HKEY_PERFORMANCE_DATA"); |
| 1054 |
if (hKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG"); |
| 1055 |
if (hKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA"); |
| 1056 |
if (hKey == HKSHCTX) return _T("HKSHCTX"); |
| 1057 |
if (hKey == HKSHCTX32) return _T("HKSHCTX32"); |
| 1058 |
if (hKey == HKSHCTX64) return _T("HKSHCTX64"); |
| 1059 |
if (hKey == HKCR32) return _T("HKCR32"); |
| 1060 |
if (hKey == HKCR64) return _T("HKCR64"); |
| 1061 |
if (hKey == HKCU32) return _T("HKCU32"); |
| 1062 |
if (hKey == HKCU64) return _T("HKCU64"); |
| 1063 |
if (hKey == HKLM32) return _T("HKLM32"); |
| 1064 |
if (hKey == HKLM64) return _T("HKLM64"); |
| 1065 |
if (hKey == HKSHCTXANY) return _T("HKSHCTXANY"); |
| 1066 |
if (hKey == HKCRANY) return _T("HKCRANY"); |
| 1067 |
if (hKey == HKCUANY) return _T("HKCUANY"); |
| 1068 |
if (hKey == HKLMANY) return _T("HKLMANY"); |
| 1069 |
return _T("HK??"); |
| 1070 |
} |
| 1071 |
|
| 1072 |
void _LogData2Hex(TCHAR *buf, size_t cchbuf, BYTE *data, size_t cbdata) |
| 1073 |
{ |
| 1074 |
TCHAR *p = buf; |
| 1075 |
int dots = 0; |
| 1076 |
size_t i, bufbytes = cchbuf / 3; // 2 hex digits, one space/null |
| 1077 |
|
| 1078 |
if (cbdata > bufbytes) |
| 1079 |
bufbytes--, dots++; |
| 1080 |
else |
| 1081 |
bufbytes = cbdata; |
| 1082 |
|
| 1083 |
for (i = 0; i < bufbytes; i++) |
| 1084 |
{ |
| 1085 |
wsprintf(p, _T("%02x%c"), data[i], (i == bufbytes - 1) ? _T('\0') : _T(' ')); |
| 1086 |
p += 3; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (dots) mystrcat(buf, _T("...")); |
| 1090 |
} |
| 1091 |
|
| 1092 |
#ifdef NSIS_CONFIG_LOG_TIMESTAMP |
| 1093 |
void log_timestamp(TCHAR *buf) |
| 1094 |
{ |
| 1095 |
SYSTEMTIME st; |
| 1096 |
GetLocalTime(&st); |
| 1097 |
wsprintf(buf,_T("[%04hu/%02hu/%02hu %02hu:%02hu:%02hu] "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); |
| 1098 |
} |
| 1099 |
#else |
| 1100 |
# define log_timestamp(x) |
| 1101 |
#endif//NSIS_CONFIG_LOG_TIMESTAMP |
| 1102 |
|
| 1103 |
void log_printf(TCHAR *format, ...) |
| 1104 |
{ |
| 1105 |
#if defined(NSIS_CONFIG_LOG_STDOUT) |
| 1106 |
HANDLE hStdOut; |
| 1107 |
#endif |
| 1108 |
va_list val; |
| 1109 |
va_start(val,format); |
| 1110 |
|
| 1111 |
log_text[0] = _T('\0'); |
| 1112 |
log_timestamp(log_text); |
| 1113 |
wvsprintf(log_text+mystrlen(log_text),format,val); |
| 1114 |
|
| 1115 |
va_end(val); |
| 1116 |
#if defined(NSIS_CONFIG_LOG_ODS) |
| 1117 |
if (log_dolog) |
| 1118 |
OutputDebugString(log_text); |
| 1119 |
#elif defined(NSIS_CONFIG_LOG_STDOUT) |
| 1120 |
if (log_dolog && (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) != INVALID_HANDLE_VALUE) |
| 1121 |
{ |
| 1122 |
const DWORD cch = lstrlen(log_text), cb = cch * sizeof(TCHAR); |
| 1123 |
#ifdef UNICODE |
| 1124 |
DWORD conmode, cchio; |
| 1125 |
if (GetConsoleMode(hStdOut, &conmode)) |
| 1126 |
{ |
| 1127 |
WriteConsoleW(hStdOut, log_text, cch, &cchio, 0); |
| 1128 |
myWriteFile(hStdOut, "\n", 1); |
| 1129 |
} |
| 1130 |
else |
| 1131 |
#endif //~ UNICODE |
| 1132 |
{ |
| 1133 |
myWriteFile(hStdOut, log_text, cb); |
| 1134 |
myWriteFile(hStdOut, _T("\n"), 1 * sizeof(TCHAR)); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
#else |
| 1138 |
log_write(0); |
| 1139 |
#endif |
| 1140 |
} |
| 1141 |
#endif//NSIS_CONFIG_LOG |
| 1142 |
|
| 1143 |
// Jim Park: This function is non-reentrant because of the static. |
| 1144 |
WIN32_FIND_DATA * NSISCALL file_exists(TCHAR *buf) |
| 1145 |
{ |
| 1146 |
HANDLE h; |
| 1147 |
static WIN32_FIND_DATA fd; |
| 1148 |
h = FindFirstFile(buf,&fd); |
| 1149 |
if (h != INVALID_HANDLE_VALUE) |
| 1150 |
{ |
| 1151 |
FindClose(h); |
| 1152 |
return &fd; |
| 1153 |
} |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
|
| 1157 |
// Jim Park: Keep these as chars since there's only ANSI version of |
| 1158 |
// GetProcAddress. |
| 1159 |
struct MGA_FUNC |
| 1160 |
{ |
| 1161 |
const char *dll; |
| 1162 |
const char *func; |
| 1163 |
}; |
| 1164 |
|
| 1165 |
struct MGA_FUNC MGA_FUNCS[] = { |
| 1166 |
#ifdef _UNICODE |
| 1167 |
{"KERNEL32", "SetDefaultDllDirectories"}, |
| 1168 |
#ifndef _WIN64 |
| 1169 |
{"KERNEL32", "GetDiskFreeSpaceExW"}, |
| 1170 |
{"KERNEL32", "GetUserDefaultUILanguage"}, |
| 1171 |
#endif |
| 1172 |
#if !defined(_WIN64) || defined(_M_IA64) |
| 1173 |
{"ADVAPI32", "RegDeleteKeyExW"}, |
| 1174 |
#endif |
| 1175 |
{"ADVAPI32", "InitiateShutdownW"}, |
| 1176 |
{"SHELL32", "SHGetKnownFolderPath"}, |
| 1177 |
{"SHELL32", (CHAR*) 680}, // IsUserAnAdmin |
| 1178 |
{"SHELL32", (CHAR*) 89}, // SHCloneSpecialIDList |
| 1179 |
#ifndef _WIN64 |
| 1180 |
{"SHLWAPI", (CHAR*) 437}, // IsOS |
| 1181 |
#endif |
| 1182 |
{"SHLWAPI", "SHAutoComplete"}, |
| 1183 |
{"SHFOLDER", "SHGetFolderPathW"}, |
| 1184 |
#ifdef NSIS_SUPPORT_GETDLLVERSION |
| 1185 |
{"VERSION", "GetFileVersionInfoSizeW"}, |
| 1186 |
{"VERSION", "GetFileVersionInfoW"}, |
| 1187 |
{"VERSION", "VerQueryValueW"} |
| 1188 |
#endif |
| 1189 |
}; |
| 1190 |
#else |
| 1191 |
{"KERNEL32", "SetDefaultDllDirectories"}, |
| 1192 |
{"KERNEL32", "GetDiskFreeSpaceExA"}, |
| 1193 |
{"KERNEL32", "GetUserDefaultUILanguage"}, |
| 1194 |
{"ADVAPI32", "RegDeleteKeyExA"}, |
| 1195 |
{"ADVAPI32", "InitiateShutdownA"}, |
| 1196 |
{"SHELL32", "SHGetKnownFolderPath"}, |
| 1197 |
{"SHELL32", (CHAR*) 680}, // IsUserAnAdmin |
| 1198 |
{"SHELL32", (CHAR*) 89}, // SHCloneSpecialIDList |
| 1199 |
#ifndef _WIN64 |
| 1200 |
{"SHLWAPI", (CHAR*) 437}, // IsOS |
| 1201 |
#endif |
| 1202 |
{"SHLWAPI", "SHAutoComplete"}, |
| 1203 |
{"SHFOLDER", "SHGetFolderPathA"}, |
| 1204 |
#ifdef NSIS_SUPPORT_GETDLLVERSION |
| 1205 |
{"VERSION", "GetFileVersionInfoSizeA"}, |
| 1206 |
{"VERSION", "GetFileVersionInfoA"}, |
| 1207 |
{"VERSION", "VerQueryValueA"} |
| 1208 |
#endif |
| 1209 |
}; |
| 1210 |
#endif |
| 1211 |
|
| 1212 |
HMODULE NSISCALL LoadSystemLibrary(LPCSTR name) |
| 1213 |
{ |
| 1214 |
LPCTSTR fmt = sizeof(*fmt) > 1 ? TEXT("%s%S.dll") : TEXT("%s%s.dll"); // The module name is always ANSI |
| 1215 |
BYTE bytebuf[(MAX_PATH+1+20+1+3+!0) * sizeof(*fmt)]; // 20+4 is more than enough for |
| 1216 |
LPTSTR path = (LPTSTR) bytebuf; // the dllnames we are using. |
| 1217 |
|
| 1218 |
UINT cch = GetSystemDirectory(path, MAX_PATH); |
| 1219 |
if (cch > MAX_PATH) // MAX_PATH was somehow not large enough and we don't support |
| 1220 |
cch = 0; // \\?\ paths so we have to settle for just the name. |
| 1221 |
wsprintf(path + cch, fmt, TEXT("\\") + (!cch || path[cch-1] == '\\'), name); |
| 1222 |
|
| 1223 |
return LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 1224 |
} |
| 1225 |
|
| 1226 |
/** |
| 1227 |
* Given a function enum, it will load the appropriate DLL and get the |
| 1228 |
* process address of the function and return the pointer. It's up to |
| 1229 |
* the caller to know how to call that function, however. |
| 1230 |
* |
| 1231 |
* @param func Enum value that indexes the MGA_FUNCS array. |
| 1232 |
* @return Pointer to the function identified by the enum value. |
| 1233 |
*/ |
| 1234 |
void* NSISCALL myGetProcAddress(const enum myGetProcAddressFunctions func) |
| 1235 |
{ |
| 1236 |
const char *dllname = MGA_FUNCS[func].dll; |
| 1237 |
HMODULE hModule; |
| 1238 |
|
| 1239 |
hModule = GetModuleHandleA(dllname); // Avoid LoadLibrary if possible because |
| 1240 |
if (!hModule) // it can crash on 64-bit dlls if |
| 1241 |
hModule = LoadSystemLibrary(dllname); // WoW64 FS redirection is off. |
| 1242 |
|
| 1243 |
return hModule |
| 1244 |
? GetProcAddress(hModule, MGA_FUNCS[func].func) |
| 1245 |
: (FARPROC) hModule; // Optimized "return NULL;" |
| 1246 |
} |
| 1247 |
|
| 1248 |
void NSISCALL MessageLoop(UINT uCheckedMsg) |
| 1249 |
{ |
| 1250 |
MSG msg; |
| 1251 |
while (PeekMessage(&msg, NULL, uCheckedMsg, uCheckedMsg, PM_REMOVE)) |
| 1252 |
DispatchMessage(&msg); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/** |
| 1256 |
* This function is useful for Unicode support. Since the Windows |
| 1257 |
* GetProcAddress function always takes a char*, this function wraps |
| 1258 |
* the windows call and does the appropriate translation when |
| 1259 |
* appropriate. |
| 1260 |
* |
| 1261 |
* @param dllHandle Handle to the DLL loaded by LoadLibrary[Ex]. |
| 1262 |
* @param funcName The name of the function to get the address of. |
| 1263 |
* @return The pointer to the function. Null if failure. |
| 1264 |
*/ |
| 1265 |
void * NSISCALL NSISGetProcAddress(HANDLE dllHandle, TCHAR* funcName) |
| 1266 |
{ |
| 1267 |
#ifdef _UNICODE |
| 1268 |
char ansiName[256]; |
| 1269 |
if (StrWideToACP(funcName, ansiName, 256) != 0) |
| 1270 |
return GetProcAddress(dllHandle, ansiName); |
| 1271 |
return NULL; |
| 1272 |
#else |
| 1273 |
return GetProcAddress(dllHandle, funcName); |
| 1274 |
#endif |
| 1275 |
} |
| 1276 |
|
| 1277 |
DWORD NSISCALL WaitForProcess(HANDLE hProcess) |
| 1278 |
{ |
| 1279 |
DWORD excod; |
| 1280 |
while (WaitForSingleObject(hProcess, 100) == WAIT_TIMEOUT) |
| 1281 |
MessageLoop(WM_PAINT); |
| 1282 |
|
| 1283 |
GetExitCodeProcess(hProcess, &excod); |
| 1284 |
return excod; |
| 1285 |
} |