1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
// Exit macros
#define AppExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
#define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
#define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
#define AppExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
#define AppExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
#define AppExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_APPUTIL, e, x, s, __VA_ARGS__)
#define AppExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_APPUTIL, g, x, s, __VA_ARGS__)
const DWORD PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
typedef BOOL(WINAPI *LPFN_SETDEFAULTDLLDIRECTORIES)(DWORD);
typedef BOOL(WINAPI *LPFN_SETDLLDIRECTORYW)(LPCWSTR);
/********************************************************************
EscapeCommandLineArgument - encodes wzArgument such that
::CommandLineToArgv() will parse it back unaltered. If no escaping
was required, *psczEscaped is NULL.
********************************************************************/
static HRESULT EscapeCommandLineArgument(
__in_z LPCWSTR wzArgument,
__out_z LPWSTR* psczEscaped
);
DAPI_(void) AppFreeCommandLineArgs(
__in LPWSTR* argv
)
{
// The "ignored" hack in AppParseCommandLine requires an adjustment.
LPWSTR* argvOriginal = argv - 1;
::LocalFree(argvOriginal);
}
/********************************************************************
AppInitialize - initializes the standard safety precautions for an
installation application.
********************************************************************/
DAPI_(void) AppInitialize(
__in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[],
__in DWORD cSafelyLoadSystemDlls
)
{
HRESULT hr = S_OK;
HMODULE hIgnored = NULL;
BOOL fSetDefaultDllDirectories = FALSE;
::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
// Best effort call to initialize default DLL directories to system only.
HMODULE hKernel32 = ::GetModuleHandleW(L"kernel32");
Assert(hKernel32);
LPFN_SETDEFAULTDLLDIRECTORIES pfnSetDefaultDllDirectories = (LPFN_SETDEFAULTDLLDIRECTORIES)::GetProcAddress(hKernel32, "SetDefaultDllDirectories");
if (pfnSetDefaultDllDirectories)
{
if (pfnSetDefaultDllDirectories(PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32))
{
fSetDefaultDllDirectories = TRUE;
}
else
{
hr = HRESULT_FROM_WIN32(::GetLastError());
TraceError(hr, "Failed to call SetDefaultDllDirectories.");
}
}
// Only need to safely load if the default DLL directories was not
// able to be set.
if (!fSetDefaultDllDirectories)
{
// Remove current working directory from search order.
LPFN_SETDLLDIRECTORYW pfnSetDllDirectory = (LPFN_SETDLLDIRECTORYW)::GetProcAddress(hKernel32, "SetDllDirectoryW");
if (!pfnSetDllDirectory || !pfnSetDllDirectory(L""))
{
hr = HRESULT_FROM_WIN32(::GetLastError());
TraceError(hr, "Failed to call SetDllDirectory.");
}
for (DWORD i = 0; i < cSafelyLoadSystemDlls; ++i)
{
hr = LoadSystemLibrary(rgsczSafelyLoadSystemDlls[i], &hIgnored);
if (FAILED(hr))
{
TraceError(hr, "Failed to safety load: %ls", rgsczSafelyLoadSystemDlls[i]);
}
}
}
}
DAPI_(void) AppInitializeUnsafe()
{
::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
}
DAPI_(HRESULT) AppParseCommandLine(
__in LPCWSTR wzCommandLine,
__in int* pArgc,
__in LPWSTR** pArgv
)
{
HRESULT hr = S_OK;
LPWSTR sczCommandLine = NULL;
LPWSTR* argv = NULL;
int argc = 0;
// CommandLineToArgvW tries to treat the first argument as the path to the process,
// which fails pretty miserably if your first argument is something like
// FOO="C:\Program Files\My Company". So give it something harmless to play with.
hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0);
AppExitOnFailure(hr, "Failed to initialize command line.");
hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0);
AppExitOnFailure(hr, "Failed to copy command line.");
argv = ::CommandLineToArgvW(sczCommandLine, &argc);
AppExitOnNullWithLastError(argv, hr, "Failed to parse command line.");
// Skip "ignored" argument/hack.
*pArgv = argv + 1;
*pArgc = argc - 1;
LExit:
ReleaseStr(sczCommandLine);
return hr;
}
DAPI_(HRESULT) AppAppendCommandLineArgument(
__deref_inout_z LPWSTR* psczCommandLine,
__in_z LPCWSTR wzArgument
)
{
HRESULT hr = S_OK;
LPWSTR sczQuotedArg = NULL;
hr = EscapeCommandLineArgument(wzArgument, &sczQuotedArg);
AppExitOnFailure(hr, "Failed to escape command line argument.");
// If there is already data in the command line,
// append a space before appending the argument.
if (*psczCommandLine && **psczCommandLine)
{
hr = StrAllocConcatSecure(psczCommandLine, L" ", 0);
AppExitOnFailure(hr, "Failed to append space to command line with existing data.");
}
hr = StrAllocConcatSecure(psczCommandLine, sczQuotedArg ? sczQuotedArg : wzArgument, 0);
AppExitOnFailure(hr, "Failed to copy command line argument.");
LExit:
ReleaseStr(sczQuotedArg);
return hr;
}
DAPIV_(HRESULT) AppAppendCommandLineArgumentFormatted(
__deref_inout_z LPWSTR* psczCommandLine,
__in __format_string LPCWSTR wzFormat,
...
)
{
HRESULT hr = S_OK;
va_list args;
va_start(args, wzFormat);
hr = AppAppendCommandLineArgumentFormattedArgs(psczCommandLine, wzFormat, args);
va_end(args);
return hr;
}
DAPI_(HRESULT) AppAppendCommandLineArgumentFormattedArgs(
__deref_inout_z LPWSTR* psczCommandLine,
__in __format_string LPCWSTR wzFormat,
__in va_list args
)
{
HRESULT hr = S_OK;
LPWSTR sczQuotedArg = NULL;
hr = AppEscapeCommandLineArgumentFormattedArgs(&sczQuotedArg, wzFormat, args);
AppExitOnFailure(hr, "Failed to escape command line argument.");
// If there is already data in the command line,
// append a space before appending the argument.
if (*psczCommandLine && **psczCommandLine)
{
hr = StrAllocConcatSecure(psczCommandLine, L" ", 0);
AppExitOnFailure(hr, "Failed to append space to command line with existing data.");
}
hr = StrAllocConcatSecure(psczCommandLine, sczQuotedArg, 0);
AppExitOnFailure(hr, "Failed to copy command line argument.");
LExit:
ReleaseStr(sczQuotedArg);
return hr;
}
DAPIV_(HRESULT) AppEscapeCommandLineArgumentFormatted(
__deref_inout_z LPWSTR* psczEscapedArgument,
__in __format_string LPCWSTR wzFormat,
...
)
{
HRESULT hr = S_OK;
va_list args;
va_start(args, wzFormat);
hr = AppEscapeCommandLineArgumentFormattedArgs(psczEscapedArgument, wzFormat, args);
va_end(args);
return hr;
}
DAPI_(HRESULT) AppEscapeCommandLineArgumentFormattedArgs(
__deref_inout_z LPWSTR* psczEscapedArgument,
__in __format_string LPCWSTR wzFormat,
__in va_list args
)
{
HRESULT hr = S_OK;
LPWSTR sczFormattedArg = NULL;
LPWSTR sczQuotedArg = NULL;
hr = StrAllocFormattedArgsSecure(&sczFormattedArg, wzFormat, args);
AppExitOnFailure(hr, "Failed to format command line argument.");
hr = EscapeCommandLineArgument(sczFormattedArg, &sczQuotedArg);
AppExitOnFailure(hr, "Failed to escape command line argument.");
if (sczQuotedArg)
{
*psczEscapedArgument = sczQuotedArg;
sczQuotedArg = NULL;
}
else
{
*psczEscapedArgument = sczFormattedArg;
sczFormattedArg = NULL;
}
LExit:
ReleaseStr(sczFormattedArg);
ReleaseStr(sczQuotedArg);
return hr;
}
static HRESULT EscapeCommandLineArgument(
__in_z LPCWSTR wzArgument,
__out_z LPWSTR* psczEscaped
)
{
HRESULT hr = S_OK;
BOOL fRequiresQuoting = FALSE;
SIZE_T cMaxEscapedSize = 0;
*psczEscaped = NULL;
// Loop through the argument determining if it needs to be quoted and what the maximum
// size would be if there are escape characters required.
for (LPCWSTR pwz = wzArgument; *pwz; ++pwz)
{
// Arguments with whitespace need quoting.
if (L' ' == *pwz || L'\t' == *pwz || L'\n' == *pwz || L'\v' == *pwz)
{
fRequiresQuoting = TRUE;
}
else if (L'"' == *pwz) // quotes need quoting and sometimes escaping.
{
fRequiresQuoting = TRUE;
++cMaxEscapedSize;
}
else if (L'\\' == *pwz) // some backslashes need escaping, so we'll count them all to make sure there is room.
{
++cMaxEscapedSize;
}
++cMaxEscapedSize;
}
// If we found anything in the argument that requires our argument to be quoted
if (fRequiresQuoting)
{
hr = StrAlloc(psczEscaped, cMaxEscapedSize + 3); // plus three for the start and end quote plus null terminator.
AppExitOnFailure(hr, "Failed to allocate argument to be quoted.");
LPCWSTR pwz = wzArgument;
LPWSTR pwzQuoted = *psczEscaped;
*pwzQuoted = L'"';
++pwzQuoted;
while (*pwz)
{
DWORD dwBackslashes = 0;
while (L'\\' == *pwz)
{
++dwBackslashes;
++pwz;
}
// Escape all backslashes at the end of the string.
if (!*pwz)
{
dwBackslashes *= 2;
}
else if (L'"' == *pwz) // escape all backslashes before the quote and escape the quote itself.
{
dwBackslashes = dwBackslashes * 2 + 1;
}
// the backslashes don't have to be escaped.
// Add the appropriate number of backslashes
for (DWORD i = 0; i < dwBackslashes; ++i)
{
*pwzQuoted = L'\\';
++pwzQuoted;
}
// If there is a character, add it after all the escaped backslashes
if (*pwz)
{
*pwzQuoted = *pwz;
++pwz;
++pwzQuoted;
}
}
*pwzQuoted = L'"';
++pwzQuoted;
*pwzQuoted = L'\0'; // ensure the arg is null terminated.
}
LExit:
return hr;
}
|