-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateCommandLine.cpp
302 lines (242 loc) · 8.88 KB
/
CreateCommandLine.cpp
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
// SPDX-FileCopyrightText: 2022 Oliver Old <[email protected]>
// SPDX-License-Identifier: MIT
/*
MIT License
Copyright (c) 2022 Oliver Old
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "CreateCommandLine.hpp"
#pragma region Target architecture definitions
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_IX86)
#define _X86_
#if !defined(_CHPE_X86_ARM64_) && defined(_M_HYBRID)
#define _CHPE_X86_ARM64_
#endif
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_AMD64)
#define _AMD64_
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_ARM)
#define _ARM_
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_ARM64)
#define _ARM64_
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_M68K)
#define _68K_
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_MPPC)
#define _MPPC_
#endif
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_M_IX86) && !defined(_AMD64_) && \
!defined(_ARM_) && !defined(_ARM64_) && defined(_M_IA64)
#if !defined(_IA64_)
#define _IA64_
#endif
#endif
#ifndef _MAC
#if defined(_68K_) || defined(_MPPC_)
#define _MAC
#endif
#endif
#pragma endregion
#include <winerror.h>
#include <intsafe.h>
#include <cstddef>
#include <cstdlib>
#include <cwchar>
#include <memory>
#include <utility>
namespace createcommandline
{
namespace details
{
static long GetArgumentLength(const wchar_t *pszArgument, std::size_t &rcchArgument) noexcept
{
long hr;
if (!*pszArgument)
{
// Empty argument. Needs double quote plus NUL character.
rcchArgument = 3;
return S_FALSE;
}
auto bNeedsProcessing = false;
const wchar_t *pCh;
for (pCh = pszArgument; *pCh; ++pCh)
{
if (*pCh == L' ' || *pCh == L'\t' || *pCh == L'\n' || *pCh == L'\v' || *pCh == L'\"')
{
bNeedsProcessing = true;
break;
}
}
if (!bNeedsProcessing)
{
while (*pCh)
++pCh;
// No modification needed.
rcchArgument = pCh - pszArgument;
return S_OK;
}
std::size_t cchArgument = 2; // Quote + quote.
pCh = pszArgument;
do
{
std::size_t cchBackslashes = 0;
while (*pCh && *pCh == L'\\')
{
++pCh;
++cchBackslashes;
}
if (!*pCh)
{
// Double backslashes.
if (FAILED(hr = SizeTMult(cchBackslashes, 2, &cchBackslashes)))
return hr;
if (FAILED(hr = SizeTAdd(cchBackslashes, cchArgument, &cchArgument)))
return hr;
break;
}
if (*pCh == L'"')
{
// Double backslashes.
if (FAILED(hr = SizeTMult(cchBackslashes, 2, &cchBackslashes)))
return hr;
// ... + backslash + quote.
if (FAILED(hr = SizeTAdd(cchBackslashes, 2, &cchBackslashes)))
return hr;
if (FAILED(hr = SizeTAdd(cchBackslashes, cchArgument, &cchArgument)))
return hr;
}
else
{
// Backslashes + next character.
if (FAILED(hr = SizeTAdd(cchBackslashes, 1, &cchBackslashes)))
return hr;
if (FAILED(hr = SizeTAdd(cchBackslashes, cchArgument, &cchArgument)))
return hr;
}
++pCh;
} while (*pCh);
rcchArgument = cchArgument;
return S_FALSE;
}
static wchar_t *EscapeArgument(wchar_t *pszDest, const wchar_t *pszArgument) noexcept
{
auto pCh = pszArgument;
*pszDest++ = L'"';
do
{
std::size_t cchBackslashes = 0;
while (*pCh && *pCh == L'\\')
{
++pCh;
++cchBackslashes;
}
if (!*pCh)
{
const auto cchTheseBackslashes = cchBackslashes * 2;
pszDest = std::wmemset(pszDest, L'\\', cchTheseBackslashes) + cchTheseBackslashes;
break;
}
if (*pCh == L'"')
{
const auto cchTheseBackslashes = cchBackslashes * 2 + 1;
pszDest = std::wmemset(pszDest, L'\\', cchTheseBackslashes) + cchTheseBackslashes;
*pszDest++ = *pCh++;
continue;
}
pszDest = std::wmemset(pszDest, L'\\', cchBackslashes) + cchBackslashes;
*pszDest++ = *pCh++;
} while (pCh);
*pszDest++ = L'"';
return pszDest;
}
void Free(void *pMem) noexcept
{
std::free(pMem);
}
} // namespace details
long CreateCommandLine(const wchar_t *pszCommand, const wchar_t *const *ppszArguments,
CommandLinePtr &rpszCommandLine) noexcept
{
using namespace details;
long hr;
long hrArgLen;
void *pMem;
if (!pszCommand)
return E_INVALIDARG;
std::size_t cpszArguments = 0;
if (ppszArguments)
while (ppszArguments[cpszArguments])
++cpszArguments;
std::size_t cbacchArguments;
if (FAILED(hr = SizeTMult(cpszArguments, sizeof(std::size_t), &cbacchArguments)))
return hr;
pMem = std::malloc(cbacchArguments);
if (!pMem)
return E_OUTOFMEMORY;
// Elements contain either argument length (argument will be copied) or 0 (argument will be constructed).
std::unique_ptr<std::size_t[], CommandLineDeleter> pcchArguments((std::size_t *)pMem);
// Ditto.
std::size_t cchCommand;
if (FAILED(hrArgLen = GetArgumentLength(pszCommand, cchCommand)))
return hrArgLen;
std::size_t cchCommandLine = cchCommand;
if (hrArgLen != S_OK)
cchCommand = 0;
for (std::size_t i = 0; i < cpszArguments; ++i)
{
if (FAILED(hrArgLen = GetArgumentLength(ppszArguments[i], pcchArguments[i])))
return hrArgLen;
// This size plus space character.
if (FAILED(hr = SizeTAdd(cchCommandLine, pcchArguments[i], &cchCommandLine)))
return hr;
if (FAILED(hr = SizeTAdd(cchCommandLine, 1, &cchCommandLine)))
return hr;
if (hrArgLen != S_OK)
pcchArguments[i] = 0;
}
// Total size plus NUL.
if (FAILED(hr = SizeTAdd(cchCommandLine, 1, &cchCommandLine)))
return hr;
std::size_t cbszCommandLine;
if (FAILED(hr = SizeTMult(cchCommandLine, sizeof(wchar_t), &cbszCommandLine)))
return hr;
pMem = std::malloc(cbszCommandLine);
if (!pMem)
return E_OUTOFMEMORY;
CommandLinePtr pszCommandLine((wchar_t *)pMem);
auto pCh = (wchar_t *)pMem;
if (cchCommand)
pCh = std::wmemcpy(pCh, pszCommand, cchCommand) + cchCommand;
else
pCh = EscapeArgument(pCh, pszCommand);
for (std::size_t i = 0; i < cpszArguments; ++i)
{
*pCh++ = L' ';
if (pcchArguments[i])
pCh = std::wmemcpy(pCh, ppszArguments[i], pcchArguments[i]) + pcchArguments[i];
else
pCh = EscapeArgument(pCh, ppszArguments[i]);
}
*pCh = L'\0';
rpszCommandLine = std::move(pszCommandLine);
return S_OK;
}
} // namespace createcommandline