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
|
/**
* WinPR: Windows Portable Runtime
* Process Argument Vector Functions
*
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/crt.h>
#include <winpr/handle.h>
#include <winpr/thread.h>
#ifdef WINPR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "../log.h"
#define TAG WINPR_TAG("thread")
/**
* CommandLineToArgvW function:
* http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391/
*
* CommandLineToArgvW has a special interpretation of backslash characters
* when they are followed by a quotation mark character ("), as follows:
*
* 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
* (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a
* quotation mark. n backslashes not followed by a quotation mark simply produce n backslashes.
*
* The address returned by CommandLineToArgvW is the address of the first element in an array of
* LPWSTR values; the number of pointers in this array is indicated by pNumArgs. Each pointer to a
* null-terminated Unicode string represents an individual argument found on the command line.
*
* CommandLineToArgvW allocates a block of contiguous memory for pointers to the argument strings,
* and for the argument strings themselves; the calling application must free the memory used by the
* argument list when it is no longer needed. To free the memory, use a single call to the LocalFree
* function.
*/
/**
* Parsing C++ Command-Line Arguments:
* http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft
*
* Microsoft C/C++ startup code uses the following rules when
* interpreting arguments given on the operating system command line:
*
* Arguments are delimited by white space, which is either a space or a tab.
*
* The caret character (^) is not recognized as an escape character or delimiter.
* The character is handled completely by the command-line parser in the operating
* system before being passed to the argv array in the program.
*
* A string surrounded by double quotation marks ("string") is interpreted as a
* single argument, regardless of white space contained within. A quoted string
* can be embedded in an argument.
*
* A double quotation mark preceded by a backslash (\") is interpreted as a
* literal double quotation mark character (").
*
* Backslashes are interpreted literally, unless they immediately
* precede a double quotation mark.
*
* If an even number of backslashes is followed by a double quotation mark,
* one backslash is placed in the argv array for every pair of backslashes,
* and the double quotation mark is interpreted as a string delimiter.
*
* If an odd number of backslashes is followed by a double quotation mark,
* one backslash is placed in the argv array for every pair of backslashes,
* and the double quotation mark is "escaped" by the remaining backslash,
* causing a literal double quotation mark (") to be placed in argv.
*
*/
LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
{
const char* p = NULL;
size_t length = 0;
const char* pBeg = NULL;
const char* pEnd = NULL;
char* buffer = NULL;
char* pOutput = NULL;
int numArgs = 0;
LPSTR* pArgs = NULL;
size_t maxNumArgs = 0;
size_t maxBufferSize = 0;
size_t cmdLineLength = 0;
BOOL* lpEscapedChars = NULL;
LPSTR lpEscapedCmdLine = NULL;
if (!lpCmdLine)
return NULL;
if (!pNumArgs)
return NULL;
pArgs = NULL;
lpEscapedCmdLine = NULL;
cmdLineLength = strlen(lpCmdLine);
lpEscapedChars = (BOOL*)calloc(cmdLineLength + 1, sizeof(BOOL));
if (!lpEscapedChars)
return NULL;
if (strstr(lpCmdLine, "\\\""))
{
size_t n = 0;
const char* pLastEnd = NULL;
lpEscapedCmdLine = (char*)calloc(cmdLineLength + 1, sizeof(char));
if (!lpEscapedCmdLine)
{
free(lpEscapedChars);
return NULL;
}
p = (const char*)lpCmdLine;
pLastEnd = (const char*)lpCmdLine;
pOutput = (char*)lpEscapedCmdLine;
while (p < &lpCmdLine[cmdLineLength])
{
pBeg = strstr(p, "\\\"");
if (!pBeg)
{
length = strlen(p);
CopyMemory(pOutput, p, length);
pOutput += length;
break;
}
pEnd = pBeg + 2;
while (pBeg >= lpCmdLine)
{
if (*pBeg != '\\')
{
pBeg++;
break;
}
pBeg--;
}
n = ((pEnd - pBeg) - 1);
length = (pBeg - pLastEnd);
CopyMemory(pOutput, p, length);
pOutput += length;
p += length;
for (size_t i = 0; i < (n / 2); i++)
*pOutput++ = '\\';
p += n + 1;
if ((n % 2) != 0)
lpEscapedChars[pOutput - lpEscapedCmdLine] = TRUE;
*pOutput++ = '"';
pLastEnd = p;
}
*pOutput++ = '\0';
lpCmdLine = (LPCSTR)lpEscapedCmdLine;
cmdLineLength = strlen(lpCmdLine);
}
maxNumArgs = 2;
p = (const char*)lpCmdLine;
while (p < lpCmdLine + cmdLineLength)
{
p += strcspn(p, " \t");
p += strspn(p, " \t");
maxNumArgs++;
}
maxBufferSize = (maxNumArgs * (sizeof(char*))) + (cmdLineLength + 1);
buffer = calloc(maxBufferSize, sizeof(char));
if (!buffer)
{
free(lpEscapedCmdLine);
free(lpEscapedChars);
return NULL;
}
pArgs = (LPSTR*)buffer;
pOutput = (char*)&buffer[maxNumArgs * (sizeof(char*))];
p = (const char*)lpCmdLine;
while (p < lpCmdLine + cmdLineLength)
{
pBeg = p;
while (1)
{
p += strcspn(p, " \t\"\0");
if ((*p != '"') || !lpEscapedChars[p - lpCmdLine])
break;
p++;
}
if (*p != '"')
{
/* no whitespace escaped with double quotes */
length = (p - pBeg);
CopyMemory(pOutput, pBeg, length);
pOutput[length] = '\0';
pArgs[numArgs++] = pOutput;
pOutput += (length + 1);
}
else
{
p++;
while (1)
{
p += strcspn(p, "\"\0");
if ((*p != '"') || !lpEscapedChars[p - lpCmdLine])
break;
p++;
}
if (*p != '"')
WLog_ERR(TAG, "parsing error: uneven number of unescaped double quotes!");
if (*p && *(++p))
p += strcspn(p, " \t\0");
pArgs[numArgs++] = pOutput;
while (pBeg < p)
{
if (*pBeg != '"')
*pOutput++ = *pBeg;
pBeg++;
}
*pOutput++ = '\0';
}
p += strspn(p, " \t");
}
free(lpEscapedCmdLine);
free(lpEscapedChars);
*pNumArgs = numArgs;
return pArgs;
}
#ifndef _WIN32
LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int* pNumArgs)
{
return NULL;
}
#endif
|