| 1 |
/* |
| 2 |
Copyright (C) 2002 Robert Rainwater <[email protected]> |
| 3 |
Copyright (C) 2002-2026 Nullsoft and Contributors |
| 4 |
|
| 5 |
This software is provided 'as-is', without any express or implied |
| 6 |
warranty. In no event will the authors be held liable for any damages |
| 7 |
arising from the use of this software. |
| 8 |
|
| 9 |
Permission is granted to anyone to use this software for any purpose, |
| 10 |
including commercial applications, and to alter it and redistribute it |
| 11 |
freely, subject to the following restrictions: |
| 12 |
|
| 13 |
1. The origin of this software must not be misrepresented; you must not |
| 14 |
claim that you wrote the original software. If you use this software |
| 15 |
in a product, an acknowledgment in the product documentation would be |
| 16 |
appreciated but is not required. |
| 17 |
2. Altered source versions must be plainly marked as such, and must not be |
| 18 |
misrepresented as being the original software. |
| 19 |
3. This notice may not be removed or altered from any source distribution. |
| 20 |
|
| 21 |
*/ |
| 22 |
#include <windows.h> |
| 23 |
#include <commctrl.h> |
| 24 |
#include <winnt.h> |
| 25 |
#include <nsis/pluginapi.h> // nsis plugin |
| 26 |
|
| 27 |
#if defined(_MSC_VER) && !defined(GetVersion) |
| 28 |
#if _MSC_VER >= 1500 |
| 29 |
FORCEINLINE DWORD NoDepr_GetVersion() { __pragma(warning(push))__pragma(warning(disable:4996)) DWORD r = GetVersion(); __pragma(warning(pop)) return r; } |
| 30 |
#define GetVersion NoDepr_GetVersion |
| 31 |
#endif //~ _MSC_VER >= 1500 |
| 32 |
#endif //~ _MSC_VER |
| 33 |
|
| 34 |
#define TAB_REPLACE _T(" ") |
| 35 |
#define TAB_REPLACE_SIZE (sizeof(TAB_REPLACE) - sizeof(_T(""))) |
| 36 |
#define TAB_REPLACE_CCH (TAB_REPLACE_SIZE / sizeof(_T(""))) |
| 37 |
enum { MODE_IGNOREOUTPUT = 0, MODE_LINES = 1, MODE_STACK = 2 }; |
| 38 |
|
| 39 |
#define LOOPTIMEOUT 100 |
| 40 |
HWND g_hwndParent; |
| 41 |
HWND g_hwndList; |
| 42 |
HINSTANCE g_hInst; |
| 43 |
|
| 44 |
void ExecScript(BOOL log); |
| 45 |
TCHAR *my_strstr(TCHAR *a, TCHAR *b); |
| 46 |
unsigned int my_atoi(TCHAR *s); |
| 47 |
int WINAPI AsExeWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow); |
| 48 |
|
| 49 |
void __declspec(dllexport) Exec(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) { |
| 50 |
g_hwndParent = hwndParent; |
| 51 |
EXDLL_INIT(); |
| 52 |
ExecScript(MODE_IGNOREOUTPUT); |
| 53 |
} |
| 54 |
|
| 55 |
void __declspec(dllexport) ExecToLog(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) { |
| 56 |
g_hwndParent = hwndParent; |
| 57 |
EXDLL_INIT(); |
| 58 |
ExecScript(MODE_LINES); |
| 59 |
} |
| 60 |
|
| 61 |
void __declspec(dllexport) ExecToStack(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) { |
| 62 |
g_hwndParent = hwndParent; |
| 63 |
EXDLL_INIT(); |
| 64 |
ExecScript(MODE_STACK); |
| 65 |
} |
| 66 |
|
| 67 |
BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) { |
| 68 |
g_hInst = hInst; |
| 69 |
return TRUE; |
| 70 |
} |
| 71 |
|
| 72 |
static BOOL IsLeadSurrogateUTF16(unsigned short c) { return c >= 0xd800 && c <= 0xdbff; } |
| 73 |
static BOOL IsTrailSurrogateUTF16(unsigned short c) { return c >= 0xdc00 && c <= 0xdfff; } |
| 74 |
|
| 75 |
static PWSTR MyCharNext(PCWSTR p) |
| 76 |
{ |
| 77 |
// Note: This is wrong for surrogate pair combining characters but CharNextW does |
| 78 |
// not support surrogate pairs correctly so we have to manually handle the pairs. |
| 79 |
if (!p[0]) return (PWSTR) p; |
| 80 |
if (IsLeadSurrogateUTF16(p[0]) && IsTrailSurrogateUTF16(p[1])) return (PWSTR) p + 2; // Current is a surrogate pair, we incorrectly assume that it is not followed by combining characters. |
| 81 |
if (IsLeadSurrogateUTF16(p[1]) && IsTrailSurrogateUTF16(p[2])) return (PWSTR) p + 1; // Next is a surrogate pair, we incorrectly assume that it is not a combining character for the current character. |
| 82 |
return (CharNextW)(p); |
| 83 |
} |
| 84 |
#define CharNextW MyCharNext |
| 85 |
|
| 86 |
static void TruncateStringUTF16LE(LPWSTR Buffer, SIZE_T Length, LPCWSTR Overflow, SIZE_T lenOver) { |
| 87 |
if (Length) { |
| 88 |
LPWSTR p = &Buffer[Length - 1]; |
| 89 |
UINT stripBaseCharIfCuttingCombining = TRUE; |
| 90 |
|
| 91 |
// CharNextW is buggy on XP&2003 but we don't care enough to call GetStringTypeW (http://archives.miloush.net/michkap/archive/2005/01/30/363420.html) |
| 92 |
if (stripBaseCharIfCuttingCombining && lenOver) { |
| 93 |
WCHAR buf[] = { *p, Overflow[0], lenOver > 1 ? Overflow[1] : L' ', L'\0' }; |
| 94 |
for (;;) { |
| 95 |
BOOL comb = CharNextW(buf) > buf + 1; |
| 96 |
if (!comb || p < Buffer) break; |
| 97 |
*((WORD*)((BYTE*)&buf[1])) = *((WORD*)((BYTE*)&buf[0])); |
| 98 |
buf[0] = *p; |
| 99 |
*p-- = L'\0'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (IsLeadSurrogateUTF16(*p)) { |
| 104 |
*p = L'\0'; // Avoid incomplete pair |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void TruncateStringMB(UINT Codepage, LPSTR Buffer, SIZE_T Length, unsigned short OverflowCh) { |
| 110 |
if (Length) { |
| 111 |
CHAR *p = &Buffer[Length - 1], buf[] = { *p, ' ', ' ', '\0' }; |
| 112 |
|
| 113 |
if (CharNextExA(Codepage, buf, 0) > buf + 1) { // Remove incomplete DBCS character? |
| 114 |
*p = '\0'; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static BOOL IsWOW64() { |
| 120 |
#ifdef _WIN64 |
| 121 |
return FALSE; |
| 122 |
#else |
| 123 |
typedef BOOL (WINAPI*ISWOW64PROCESS)(HANDLE, BOOL*); |
| 124 |
ISWOW64PROCESS pfIsWow64Process; |
| 125 |
typedef BOOL (WINAPI*ISWOW64PROCESS2)(HANDLE, USHORT*, USHORT*); |
| 126 |
ISWOW64PROCESS2 pfIsWow64Process2; |
| 127 |
HANDLE hProcess = GetCurrentProcess(); |
| 128 |
HMODULE hK32 = GetModuleHandleA("KERNEL32"); |
| 129 |
UINT_PTR retval; |
| 130 |
USHORT appmach, image_file_machine_unknown = 0; |
| 131 |
CHAR funcnam[16] |
| 132 |
#if defined(_MSC_VER) && (_MSC_VER-0 <= 1400) |
| 133 |
= "IsWow64Process2"; // MOVSD * 4 |
| 134 |
#else |
| 135 |
; lstrcpyA(funcnam, "IsWow64Process2"); |
| 136 |
#endif |
| 137 |
pfIsWow64Process2 = (ISWOW64PROCESS2) GetProcAddress(hK32, funcnam); |
| 138 |
if (pfIsWow64Process2 && pfIsWow64Process2(hProcess, &appmach, NULL)) { |
| 139 |
retval = image_file_machine_unknown != appmach; |
| 140 |
} |
| 141 |
else { |
| 142 |
BOOL wow64; |
| 143 |
pfIsWow64Process = (ISWOW64PROCESS) GetProcAddress(hK32, (funcnam[14] = '\0', funcnam)); |
| 144 |
retval = (UINT_PTR) pfIsWow64Process; |
| 145 |
if (pfIsWow64Process && (retval = pfIsWow64Process(hProcess, &wow64))) { |
| 146 |
retval = wow64; |
| 147 |
} |
| 148 |
} |
| 149 |
return (BOOL) (UINT) retval; |
| 150 |
#endif |
| 151 |
} |
| 152 |
|
| 153 |
// Tim Kosse's LogMessage |
| 154 |
#ifdef UNICODE |
| 155 |
static void LogMessage(const TCHAR *pStr, BOOL bOEM) { |
| 156 |
#else |
| 157 |
static void LogMessage(TCHAR *pStr, BOOL bOEM) { |
| 158 |
#endif |
| 159 |
LVITEM item; |
| 160 |
int nItemCount; |
| 161 |
if (!g_hwndList) return; |
| 162 |
//if (!*pStr) return; |
| 163 |
#ifndef UNICODE |
| 164 |
if (bOEM == TRUE) OemToCharBuff(pStr, pStr, lstrlen(pStr)); |
| 165 |
#endif |
| 166 |
nItemCount=(int) SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0); |
| 167 |
item.mask=LVIF_TEXT; |
| 168 |
item.pszText=(TCHAR *)pStr; |
| 169 |
item.cchTextMax=0; |
| 170 |
item.iItem=nItemCount, item.iSubItem=0; |
| 171 |
ListView_InsertItem(g_hwndList, &item); |
| 172 |
ListView_EnsureVisible(g_hwndList, item.iItem, 0); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
void ExecScript(int mode) { |
| 177 |
TCHAR szRet[128]; |
| 178 |
TCHAR meDLLPath[MAX_PATH]; |
| 179 |
TCHAR *g_exec, *executor; |
| 180 |
TCHAR *pExec; |
| 181 |
int ignoreData = mode == MODE_IGNOREOUTPUT; |
| 182 |
int logMode = mode == MODE_LINES, stackMode = mode == MODE_STACK; |
| 183 |
unsigned int to, tabExpandLength = logMode ? TAB_REPLACE_CCH : 0, codepage; |
| 184 |
BOOL bOEM, forceNarrowInput = FALSE; |
| 185 |
|
| 186 |
*szRet = _T('\0'); |
| 187 |
|
| 188 |
if (sizeof(void*) < 8 && !IsWOW64()) { |
| 189 |
TCHAR* p; |
| 190 |
int nComSpecSize; |
| 191 |
|
| 192 |
nComSpecSize = GetModuleFileName(g_hInst, meDLLPath, MAX_PATH) + 2; // 2 chars for quotes |
| 193 |
g_exec = (TCHAR *)GlobalAlloc(GPTR, sizeof(TCHAR) * (g_stringsize+nComSpecSize+2)); // 1 for space, 1 for null |
| 194 |
p = meDLLPath + nComSpecSize - 2; // point p at null char of meDLLPath |
| 195 |
*g_exec = _T('"'); |
| 196 |
executor = g_exec + 1; |
| 197 |
|
| 198 |
// Look for the last '\' in path. |
| 199 |
do |
| 200 |
{ |
| 201 |
if (*p == _T('\\')) |
| 202 |
break; |
| 203 |
p = CharPrev(meDLLPath, p); |
| 204 |
} |
| 205 |
while (p > meDLLPath); |
| 206 |
if (p == meDLLPath) |
| 207 |
{ |
| 208 |
// bad path |
| 209 |
pushstring(_T("error")); |
| 210 |
GlobalFree(g_exec); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
*p = 0; |
| 215 |
GetTempFileName(meDLLPath, _T("ns"), 0, executor); // executor = new temp file name in module path. |
| 216 |
*p = _T('\\'); |
| 217 |
if (CopyFile(meDLLPath, executor, FALSE)) // copy current DLL to temp file in module path. |
| 218 |
{ |
| 219 |
HANDLE hFile, hMapping; |
| 220 |
LPBYTE pMapView; |
| 221 |
PIMAGE_NT_HEADERS pNTHeaders; |
| 222 |
hFile = CreateFile(executor, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, 0); |
| 223 |
hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); |
| 224 |
pMapView = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0); |
| 225 |
if (pMapView) |
| 226 |
{ |
| 227 |
pNTHeaders = (PIMAGE_NT_HEADERS)(pMapView + ((PIMAGE_DOS_HEADER)pMapView)->e_lfanew); |
| 228 |
// Turning the copied DLL into a stripped down executable. |
| 229 |
pNTHeaders->FileHeader.Characteristics = IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_LOCAL_SYMS_STRIPPED | |
| 230 |
IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
| 231 |
#ifdef _WIN64 |
| 232 |
| IMAGE_FILE_LARGE_ADDRESS_AWARE // For ARM64 (feature request #579) |
| 233 |
#endif |
| 234 |
; |
| 235 |
// Windows character-mode user interface (CUI) subsystem. |
| 236 |
pNTHeaders->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI; |
| 237 |
// g_hInst is assumed to be the very base of the DLL in memory. |
| 238 |
// WinMain will have the address of the WinMain function in memory. |
| 239 |
// Getting the difference gets you the relative location of the |
| 240 |
// WinMain function. |
| 241 |
pNTHeaders->OptionalHeader.AddressOfEntryPoint = (DWORD) ((DWORD_PTR)AsExeWinMain - (DWORD_PTR)g_hInst); |
| 242 |
UnmapViewOfFile(pMapView); |
| 243 |
} |
| 244 |
CloseHandle(hMapping); |
| 245 |
CloseHandle(hFile); |
| 246 |
} |
| 247 |
|
| 248 |
lstrcat(g_exec, _T("\"")); |
| 249 |
|
| 250 |
// add space |
| 251 |
pExec = g_exec + lstrlen(g_exec); |
| 252 |
*pExec = _T(' '); |
| 253 |
pExec++; |
| 254 |
} else { |
| 255 |
executor = NULL; |
| 256 |
g_exec = (TCHAR *)GlobalAlloc(GPTR, sizeof(TCHAR) * (g_stringsize+1)); // 1 for NULL |
| 257 |
pExec = g_exec; |
| 258 |
} |
| 259 |
|
| 260 |
to = 0; // default is no timeout |
| 261 |
bOEM = FALSE; // default is no OEM->ANSI conversion |
| 262 |
|
| 263 |
g_hwndList = NULL; |
| 264 |
|
| 265 |
// g_hwndParent = the caller, usually NSIS installer. |
| 266 |
if (g_hwndParent) // The window class name for dialog boxes is "#32770" |
| 267 |
g_hwndList = FindWindowEx(FindWindowEx(g_hwndParent, NULL, _T("#32770"), NULL), NULL, _T("SysListView32"), NULL); |
| 268 |
|
| 269 |
// g_exec is the complete command to run: It has the copy of this DLL turned |
| 270 |
// into an executable right now. |
| 271 |
|
| 272 |
params: |
| 273 |
// Get the command I need to run from the NSIS stack. |
| 274 |
popstring(pExec); |
| 275 |
if (my_strstr(pExec, _T("/TIMEOUT=")) == pExec) { |
| 276 |
TCHAR *szTimeout = pExec + 9; |
| 277 |
to = my_atoi(szTimeout); |
| 278 |
*pExec = 0; |
| 279 |
goto params; |
| 280 |
} |
| 281 |
if (!lstrcmpi(pExec, _T("/OEM"))) { |
| 282 |
bOEM = forceNarrowInput = TRUE; |
| 283 |
*pExec = 0; |
| 284 |
goto params; |
| 285 |
} |
| 286 |
if (!lstrcmpi(pExec, _T("/MBCS"))) { |
| 287 |
forceNarrowInput = TRUE; |
| 288 |
*pExec = 0; |
| 289 |
goto params; |
| 290 |
} |
| 291 |
|
| 292 |
if (!pExec[0]) |
| 293 |
{ |
| 294 |
pushstring(_T("error")); |
| 295 |
if (pExec-2 >= g_exec) |
| 296 |
*(pExec-2) = _T('\0'); // skip space and quote |
| 297 |
if (executor) DeleteFile(executor); |
| 298 |
GlobalFree(g_exec); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
// Got all the params off the stack. |
| 303 |
|
| 304 |
{ |
| 305 |
STARTUPINFO si = { sizeof(si), }; |
| 306 |
SECURITY_ATTRIBUTES sa = { sizeof(sa), }; |
| 307 |
SECURITY_DESCRIPTOR sd = { 0, }; |
| 308 |
PROCESS_INFORMATION pi; |
| 309 |
const BOOL isNT = sizeof(void*) > 4 || (GetVersion() < 0x80000000); |
| 310 |
HANDLE newstdout = 0, read_stdout = 0; |
| 311 |
HANDLE newstdin = 0, read_stdin = 0; |
| 312 |
int utfSource = sizeof(TCHAR) > 1 && !forceNarrowInput ? -1 : FALSE, utfOutput = sizeof(TCHAR) > 1; |
| 313 |
DWORD cbRead, dwLastOutput; |
| 314 |
DWORD dwExit = 0, waitResult = WAIT_TIMEOUT; |
| 315 |
static BYTE bufSrc[1024]; |
| 316 |
BYTE *pSrc; |
| 317 |
SIZE_T cbSrcTot = sizeof(bufSrc), cbSrc = 0, cbSrcFree; |
| 318 |
TCHAR *bufOutput = 0, *pNewAlloc, *pD; |
| 319 |
SIZE_T cchAlloc, cbAlloc, cchFree; |
| 320 |
#ifndef _MSC_VER // Avoid GCC "may be used uninitialized in this function" warnings |
| 321 |
pD = NULL; |
| 322 |
cchAlloc = 0; |
| 323 |
#endif |
| 324 |
|
| 325 |
pi.hProcess = pi.hThread = NULL; |
| 326 |
codepage = bOEM ? CP_OEMCP : CP_ACP; |
| 327 |
|
| 328 |
if (!ignoreData) { |
| 329 |
cbAlloc = stackMode ? (g_stringsize * sizeof(TCHAR)) : sizeof(bufSrc) * 4, cchAlloc = cbAlloc / sizeof(TCHAR); |
| 330 |
pD = bufOutput = GlobalAlloc(GPTR, cbAlloc + sizeof(TCHAR)); // Include "hidden" space for a \0 |
| 331 |
if (!bufOutput) { |
| 332 |
lstrcpy(szRet, _T("error")); |
| 333 |
goto done; |
| 334 |
} |
| 335 |
*bufOutput = _T('\0'); |
| 336 |
} |
| 337 |
|
| 338 |
sa.bInheritHandle = TRUE; |
| 339 |
sa.lpSecurityDescriptor = NULL; |
| 340 |
if (isNT) { |
| 341 |
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); |
| 342 |
SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); |
| 343 |
sa.lpSecurityDescriptor = &sd; |
| 344 |
} |
| 345 |
|
| 346 |
if (!CreatePipe(&read_stdout, &newstdout, &sa, 0)) { |
| 347 |
lstrcpy(szRet, _T("error")); |
| 348 |
goto done; |
| 349 |
} |
| 350 |
if (!CreatePipe(&read_stdin, &newstdin, &sa, 0)) { |
| 351 |
lstrcpy(szRet, _T("error")); |
| 352 |
goto done; |
| 353 |
} |
| 354 |
|
| 355 |
GetStartupInfo(&si); // Why? |
| 356 |
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; |
| 357 |
si.wShowWindow = SW_HIDE; |
| 358 |
si.hStdInput = newstdin; |
| 359 |
si.hStdOutput = newstdout; |
| 360 |
si.hStdError = newstdout; |
| 361 |
if (!CreateProcess(NULL, g_exec, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { |
| 362 |
lstrcpy(szRet, _T("error")); |
| 363 |
goto done; |
| 364 |
} |
| 365 |
|
| 366 |
// Now I'm talking with an executable copy of myself. |
| 367 |
dwLastOutput = GetTickCount(); |
| 368 |
for (;;) { |
| 369 |
TCHAR bufCh[2]; |
| 370 |
waitForProcess: |
| 371 |
waitResult = WaitForSingleObject(pi.hProcess, 0); |
| 372 |
GetExitCodeProcess(pi.hProcess, &dwExit); |
| 373 |
readMore: |
| 374 |
PeekNamedPipe(read_stdout, 0, 0, 0, &cbRead, NULL); |
| 375 |
if (!cbRead) { |
| 376 |
if (waitResult == WAIT_OBJECT_0) { |
| 377 |
break; // No data in the pipe and process ended, we are done |
| 378 |
} |
| 379 |
if (to && GetTickCount() > dwLastOutput+to) { |
| 380 |
TerminateProcess(pi.hProcess, -1); |
| 381 |
lstrcpy(szRet, _T("timeout")); |
| 382 |
} |
| 383 |
else { |
| 384 |
Sleep(LOOPTIMEOUT); |
| 385 |
} |
| 386 |
continue; |
| 387 |
} |
| 388 |
|
| 389 |
dwLastOutput = GetTickCount(); |
| 390 |
ReadFile(read_stdout, bufSrc + cbSrc, (DWORD) (cbSrcFree = cbSrcTot - cbSrc), &cbRead, NULL); |
| 391 |
cbSrcFree -= cbRead, cbSrc = cbSrcTot - cbSrcFree; |
| 392 |
pSrc = bufSrc; |
| 393 |
|
| 394 |
if (utfSource < 0 && cbSrc) { // Simple UTF-16LE detection |
| 395 |
#ifdef UNICODE |
| 396 |
utfSource = IsTextUnicode(pSrc, (UINT) (cbSrc & ~1), NULL) != FALSE; |
| 397 |
#else |
| 398 |
utfSource = (cbSrc >= 3 && pSrc[0] && !pSrc[1]) || (cbSrc > 4 && pSrc[2] && !pSrc[3]); // Lame latin-only test |
| 399 |
utfSource |= (cbSrc > 3 && pSrc[0] == 0xFF && pSrc[1] == 0xFE && (pSrc[2] | pSrc[3])); // Lame BOM test |
| 400 |
#endif |
| 401 |
} |
| 402 |
|
| 403 |
if (ignoreData) { |
| 404 |
cbSrc = 0; // Overwrite the whole buffer every read |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
if (!cbRead) { |
| 409 |
continue; // No new data, read more before trying to parse |
| 410 |
} |
| 411 |
|
| 412 |
parseLines: |
| 413 |
cchFree = cchAlloc - (pD - bufOutput); |
| 414 |
for (;;) { |
| 415 |
DWORD cbSrcChar = 1, cchDstChar, i; |
| 416 |
*pD = _T('\0'); // Terminate output buffer because we can unexpectedly run out of data |
| 417 |
if (!cbSrc) { |
| 418 |
goto readMore; |
| 419 |
} |
| 420 |
|
| 421 |
if (utfSource) { // UTF-16LE --> ?: |
| 422 |
if (cbSrc < 2) { |
| 423 |
goto readMore; |
| 424 |
} |
| 425 |
if (utfOutput) { // UTF-16LE --> UTF-16LE: |
| 426 |
bufCh[0] = ((TCHAR*)pSrc)[0], cbSrcChar = sizeof(WCHAR), cchDstChar = 1; // We only care about certain ASCII characters so we don't bother dealing with surrogate pairs. |
| 427 |
} |
| 428 |
else { // UTF-16LE --> DBCS |
| 429 |
// TODO: This is tricky because we need the complete base character (or surrogate pair) and all the trailing combining characters for a grapheme in the buffer before we can call WideCharToMultiByte. |
| 430 |
utfOutput = FALSE; // For now we just treat it as DBCS |
| 431 |
continue; |
| 432 |
} |
| 433 |
} |
| 434 |
else { // DBCS --> ?: |
| 435 |
if (utfOutput) { // DBCS --> UTF-16LE: |
| 436 |
BOOL isMb = IsDBCSLeadByteEx(codepage, ((CHAR*)pSrc)[0]); |
| 437 |
if (isMb && cbSrc < ++cbSrcChar) { |
| 438 |
goto readMore; |
| 439 |
} |
| 440 |
cchDstChar = MultiByteToWideChar(codepage, 0, (CHAR*)pSrc, cbSrcChar, (WCHAR*) bufCh, 2); |
| 441 |
} |
| 442 |
else { // DBCS --> DBCS: |
| 443 |
bufCh[0] = ((CHAR*)pSrc)[0], cchDstChar = 1; // Note: OEM codepage will be converted by LogMessage |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (bufCh[0] == _T('\t') && tabExpandLength) { // Expand tab to spaces? |
| 448 |
if (cchFree < tabExpandLength) { |
| 449 |
goto resizeOutputBuffer; |
| 450 |
} |
| 451 |
lstrcpy(pD, TAB_REPLACE); |
| 452 |
pD += tabExpandLength, cchFree -= tabExpandLength; |
| 453 |
} |
| 454 |
else if (bufCh[0] == _T('\r') && logMode) { |
| 455 |
// Eating it |
| 456 |
} |
| 457 |
else if (bufCh[0] == _T('\n') && logMode) { |
| 458 |
LogMessage(bufOutput, bOEM); // Output has already been \0 terminated |
| 459 |
*(pD = bufOutput) = _T('\0'), cchFree = cchAlloc; |
| 460 |
} |
| 461 |
else { |
| 462 |
if (cchFree < cchDstChar) { |
| 463 |
SIZE_T cchOrgOffset; |
| 464 |
resizeOutputBuffer: |
| 465 |
if (stackMode) { |
| 466 |
ignoreData = TRUE; // Buffer was already maximum for the NSIS stack, we cannot handle more data |
| 467 |
if (utfOutput) |
| 468 |
TruncateStringUTF16LE((LPWSTR) bufOutput, pD - bufOutput, (LPCWSTR) bufCh, cchDstChar); |
| 469 |
else |
| 470 |
TruncateStringMB(codepage, (LPSTR) bufOutput, pD - bufOutput, bufCh[0]); |
| 471 |
goto waitForProcess; |
| 472 |
} |
| 473 |
cchAlloc += 1024, cbAlloc = cchAlloc / sizeof(TCHAR); |
| 474 |
pNewAlloc = GlobalReAlloc(bufOutput, cbAlloc + sizeof(TCHAR),GPTR|GMEM_MOVEABLE); // Include "hidden" space for a \0 |
| 475 |
if (!pNewAlloc) { |
| 476 |
lstrcpy(szRet, _T("error")); |
| 477 |
ignoreData = TRUE; |
| 478 |
goto waitForProcess; |
| 479 |
} |
| 480 |
cchOrgOffset = pD - bufOutput; |
| 481 |
*(pD = (bufOutput = pNewAlloc) + cchOrgOffset) = _T('\0'); |
| 482 |
goto parseLines; |
| 483 |
} |
| 484 |
for (i = 0; i < cchDstChar; ++i) { |
| 485 |
*pD++ = bufCh[i], --cchFree; |
| 486 |
} |
| 487 |
} |
| 488 |
pSrc += cbSrcChar, cbSrc -= cbSrcChar; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
done: |
| 493 |
if (stackMode) pushstring(bufOutput); |
| 494 |
if (logMode && *bufOutput) LogMessage(bufOutput,bOEM); // Write remaining output |
| 495 |
if (dwExit == STATUS_ILLEGAL_INSTRUCTION) |
| 496 |
lstrcpy(szRet, _T("error")); |
| 497 |
if (!szRet[0]) wsprintf(szRet,_T("%d"),dwExit); |
| 498 |
pushstring(szRet); |
| 499 |
CloseHandle(pi.hThread); |
| 500 |
CloseHandle(pi.hProcess); |
| 501 |
CloseHandle(newstdout); |
| 502 |
CloseHandle(read_stdout); |
| 503 |
CloseHandle(newstdin); |
| 504 |
CloseHandle(read_stdin); |
| 505 |
if (pExec-2 >= g_exec) |
| 506 |
*(pExec-2) = _T('\0'); // skip space and quote |
| 507 |
if (executor) |
| 508 |
DeleteFile(executor); |
| 509 |
GlobalFree(g_exec); |
| 510 |
if (bufOutput) |
| 511 |
GlobalFree(bufOutput); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
TCHAR *my_strstr(TCHAR *a, TCHAR *b) |
| 516 |
{ |
| 517 |
int l = lstrlen(b); |
| 518 |
while (lstrlen(a) >= l) |
| 519 |
{ |
| 520 |
TCHAR c = a[l]; |
| 521 |
a[l] = 0; |
| 522 |
if (!lstrcmpi(a, b)) |
| 523 |
{ |
| 524 |
a[l] = c; |
| 525 |
return a; |
| 526 |
} |
| 527 |
a[l] = c; |
| 528 |
a = CharNext(a); |
| 529 |
} |
| 530 |
return NULL; |
| 531 |
} |
| 532 |
|
| 533 |
unsigned int my_atoi(TCHAR *s) { |
| 534 |
unsigned int v=0; |
| 535 |
if (*s == _T('0') && (s[1] == _T('x') || s[1] == _T('X'))) { |
| 536 |
s+=2; |
| 537 |
for (;;) { |
| 538 |
int c=*s++; |
| 539 |
if (c >= _T('0') && c <= _T('9')) c-=_T('0'); |
| 540 |
else if (c >= _T('a') && c <= _T('f')) c-=_T('a')-10; |
| 541 |
else if (c >= _T('A') && c <= _T('F')) c-=_T('A')-10; |
| 542 |
else break; |
| 543 |
v<<=4; |
| 544 |
v+=c; |
| 545 |
} |
| 546 |
} |
| 547 |
else if (*s == _T('0') && s[1] <= _T('7') && s[1] >= _T('0')) { |
| 548 |
s++; |
| 549 |
for (;;) { |
| 550 |
int c=*s++; |
| 551 |
if (c >= _T('0') && c <= _T('7')) c-=_T('0'); |
| 552 |
else break; |
| 553 |
v<<=3; |
| 554 |
v+=c; |
| 555 |
} |
| 556 |
} |
| 557 |
else { |
| 558 |
for (;;) { |
| 559 |
int c=*s++ - _T('0'); |
| 560 |
if (c < 0 || c > 9) break; |
| 561 |
v*=10; |
| 562 |
v+=c; |
| 563 |
} |
| 564 |
} |
| 565 |
return (int)v; |
| 566 |
} |
| 567 |
|
| 568 |
int WINAPI AsExeWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) |
| 569 |
{ |
| 570 |
DWORD Ret; |
| 571 |
STARTUPINFO si = {0}; |
| 572 |
PROCESS_INFORMATION pi = {0}; |
| 573 |
TCHAR command_line[1024]; //BUGBUG |
| 574 |
TCHAR seekchar=_T(' '); |
| 575 |
TCHAR *cmdline; |
| 576 |
|
| 577 |
si.cb = sizeof(si); |
| 578 |
// Make child process use this app's standard files. Not needed because the handles |
| 579 |
// we created when executing this process were inheritable. |
| 580 |
//si.dwFlags = STARTF_USESTDHANDLES; |
| 581 |
//si.hStdInput = GetStdHandle (STD_INPUT_HANDLE); |
| 582 |
//si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE); |
| 583 |
//si.hStdError = GetStdHandle (STD_ERROR_HANDLE); |
| 584 |
lstrcpyn(command_line, GetCommandLine(), 1024); |
| 585 |
|
| 586 |
cmdline = command_line; |
| 587 |
if (*cmdline == _T('\"')) seekchar = *cmdline++; |
| 588 |
|
| 589 |
while (*cmdline && *cmdline != seekchar) cmdline=CharNext(cmdline); |
| 590 |
cmdline=CharNext(cmdline); |
| 591 |
// skip any spaces before the arguments |
| 592 |
while (*cmdline && *cmdline == _T(' ')) cmdline++; |
| 593 |
|
| 594 |
Ret = CreateProcess (NULL, cmdline, |
| 595 |
NULL, NULL, |
| 596 |
TRUE, 0, |
| 597 |
NULL, NULL, |
| 598 |
&si, &pi |
| 599 |
); |
| 600 |
|
| 601 |
if (Ret) |
| 602 |
{ |
| 603 |
WaitForSingleObject(pi.hProcess, INFINITE); |
| 604 |
GetExitCodeProcess(pi.hProcess, &Ret); |
| 605 |
CloseHandle (pi.hProcess); |
| 606 |
CloseHandle (pi.hThread); |
| 607 |
ExitProcess(Ret); |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
ExitProcess(STATUS_ILLEGAL_INSTRUCTION); |
| 612 |
} |
| 613 |
|
| 614 |
return 0; // dummy |
| 615 |
} |