forked from monkeybal1s/3Days
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineToArgvA.c
More file actions
449 lines (407 loc) · 11.3 KB
/
Copy pathCommandLineToArgvA.c
File metadata and controls
449 lines (407 loc) · 11.3 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
char strArgc[256]={0};
/*************************************************************************
* CommandLineToArgvA [SHELL32.@]
*
* We must interpret the quotes in the command line to rebuild the argv
* array correctly:
* - arguments are separated by spaces or tabs
* - quotes serve as optional argument delimiters
* '"a b"' -> 'a b'
* - escaped quotes must be converted back to '"'
* '\"' -> '"'
* - consecutive backslashes preceding a quote see their number halved with
* the remainder escaping the quote:
* 2n backslashes + quote -> n backslashes + quote as an argument delimiter
* 2n+1 backslashes + quote -> n backslashes + literal quote
* - backslashes that are not followed by a quote are copied literally:
* 'a\b' -> 'a\b'
* 'a\\b' -> 'a\\b'
* - in quoted strings, consecutive quotes see their number divided by three
* with the remainder modulo 3 deciding whether to close the string or not.
* Note that the opening quote must be counted in the consecutive quotes,
* that's the (1+) below:
* (1+) 3n quotes -> n quotes
* (1+) 3n+1 quotes -> n quotes plus closes the quoted string
* (1+) 3n+2 quotes -> n+1 quotes plus closes the quoted string
* - in unquoted strings, the first quote opens the quoted string and the
* remaining consecutive quotes follow the above rule.
*/
LPSTR* WINAPI CommandLineToArgvA_wine(LPSTR lpCmdline, int* numargs)
{
char bCmdLineWithMod[2048];
GetModuleFileNameA(NULL,bCmdLineWithMod,1024);
strcat(bCmdLineWithMod," ");
strcat(bCmdLineWithMod,lpCmdline);
lpCmdline=bCmdLineWithMod;
DWORD argc;
LPSTR *argv;
LPSTR s;
LPSTR d;
LPSTR cmdline;
int qcount,bcount;
if(!numargs || *lpCmdline==0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
/* --- First count the arguments */
argc=1;
s=lpCmdline;
/* The first argument, the executable path, follows special rules */
if (*s=='"')
{
/* The executable path ends at the next quote, no matter what */
s++;
while (*s)
if (*s++=='"')
break;
}
else
{
/* The executable path ends at the next space, no matter what */
while (*s && *s!=' ' && *s!='\t')
s++;
}
/* skip to the first argument, if any */
while (*s==' ' || *s=='\t')
s++;
if (*s)
argc++;
/* Analyze the remaining arguments */
qcount=bcount=0;
while (*s)
{
if ((*s==' ' || *s=='\t') && qcount==0)
{
/* skip to the next argument and count it if any */
while (*s==' ' || *s=='\t')
s++;
if (*s)
argc++;
bcount=0;
}
else if (*s=='\\')
{
/* '\', count them */
bcount++;
s++;
}
else if (*s=='"')
{
/* '"' */
if ((bcount & 1)==0)
qcount++; /* unescaped '"' */
s++;
bcount=0;
/* consecutive quotes, see comment in copying code below */
while (*s=='"')
{
qcount++;
s++;
}
qcount=qcount % 3;
if (qcount==2)
qcount=0;
}
else
{
/* a regular character */
bcount=0;
s++;
}
}
/* Allocate in a single lump, the string array, and the strings that go
* with it. This way the caller can make a single LocalFree() call to free
* both, as per MSDN.
*/
argv=LocalAlloc(LMEM_FIXED, (argc+1)*sizeof(LPSTR)+(strlen(lpCmdline)+1)*sizeof(char));
if (!argv)
return NULL;
cmdline=(LPSTR)(argv+argc+1);
strcpy(cmdline, lpCmdline);
/* --- Then split and copy the arguments */
argv[0]=d=cmdline;
argc=1;
/* The first argument, the executable path, follows special rules */
if (*d=='"')
{
/* The executable path ends at the next quote, no matter what */
s=d+1;
while (*s)
{
if (*s=='"')
{
s++;
break;
}
*d++=*s++;
}
}
else
{
/* The executable path ends at the next space, no matter what */
while (*d && *d!=' ' && *d!='\t')
d++;
s=d;
if (*s)
s++;
}
/* close the executable path */
*d++=0;
/* skip to the first argument and initialize it if any */
while (*s==' ' || *s=='\t')
s++;
if (!*s)
{
/* There are no parameters so we are all done */
argv[argc]=NULL;
*numargs=argc;
return argv;
}
/* Split and copy the remaining arguments */
argv[argc++]=d;
qcount=bcount=0;
while (*s)
{
if ((*s==' ' || *s=='\t') && qcount==0)
{
/* close the argument */
*d++=0;
bcount=0;
/* skip to the next one and initialize it if any */
do {
s++;
} while (*s==' ' || *s=='\t');
if (*s)
argv[argc++]=d;
}
else if (*s=='\\')
{
*d++=*s++;
bcount++;
}
else if (*s=='"')
{
if ((bcount & 1)==0)
{
/* Preceded by an even number of '\', this is half that
* number of '\', plus a quote which we erase.
*/
d-=bcount/2;
qcount++;
}
else
{
/* Preceded by an odd number of '\', this is half that
* number of '\' followed by a '"'
*/
d=d-bcount/2-1;
*d++='"';
}
s++;
bcount=0;
/* Now count the number of consecutive quotes. Note that qcount
* already takes into account the opening quote if any, as well as
* the quote that lead us here.
*/
while (*s=='"')
{
if (++qcount==3)
{
*d++='"';
qcount=0;
}
s++;
}
if (qcount==2)
qcount=0;
}
else
{
/* a regular character */
*d++=*s++;
bcount=0;
}
}
*d='\0';
argv[argc]=NULL;
*numargs=argc;
return argv;
}
/*************************************************************************
* CommandLineToArgvA
*
* https://github.com/ola-ct/actilog/blob/master/actiwin/CommandLineToArgvA.cpp
*/
PCHAR *CommandLineToArgvA_ola(PCHAR CmdLine, int *_argc) {
PCHAR *argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR *)LocalAlloc(LMEM_FIXED, i + (len + 2) * sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv) + i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while (a = CmdLine[i]) {
if (in_QM) {
if (a == '\"') {
in_QM = FALSE;
} else {
_argv[j] = a;
j++;
}
} else {
switch (a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if (in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
/*************************************************************************
* CommandLineToArgvA TINYCLIB version
*
* http://www.wheaty.net/
*/
#define _MAX_CMD_LINE_ARGS 128
// argv stored in below buffer
char * _ppszArgv[_MAX_CMD_LINE_ARGS+1];
int CommandLineToArgvA_wheaty(LPSTR pszSysCmdLine) {
int argc;
int cbCmdLine;
PSTR pszCmdLine;
// Set to no argv elements, in case we have to bail out
_ppszArgv[0] = 0;
// First get a pointer to the system's version of the command line, and
// figure out how long it is.
cbCmdLine = lstrlen( pszSysCmdLine );
// Allocate memory to store a copy of the command line. We'll modify
// this copy, rather than the original command line. Yes, this memory
// currently doesn't explicitly get freed, but it goes away when the
// process terminates.
pszCmdLine = (PSTR)HeapAlloc( GetProcessHeap(), 0, cbCmdLine+1 );
if ( !pszCmdLine )
return 0;
// Copy the system version of the command line into our copy
lstrcpy( pszCmdLine, pszSysCmdLine );
if ( '"' == *pszCmdLine ) // If command line starts with a quote ("),
{ // it's a quoted filename. Skip to next quote.
pszCmdLine++;
_ppszArgv[0] = pszCmdLine; // argv[0] == executable name
while ( *pszCmdLine && (*pszCmdLine != '"') )
pszCmdLine++;
if ( *pszCmdLine ) // Did we see a non-NULL ending?
*pszCmdLine++ = 0; // Null terminate and advance to next char
else
return 0; // Oops! We didn't see the end quote
}
else // A regular (non-quoted) filename
{
_ppszArgv[0] = pszCmdLine; // argv[0] == executable name
while ( *pszCmdLine && (' ' != *pszCmdLine) && ('\t' != *pszCmdLine) )
pszCmdLine++;
if ( *pszCmdLine )
*pszCmdLine++ = 0; // Null terminate and advance to next char
}
// Done processing argv[0] (i.e., the executable name). Now do th
// actual arguments
argc = 1;
while ( 1 )
{
// Skip over any whitespace
while ( *pszCmdLine && (' ' == *pszCmdLine) || ('\t' == *pszCmdLine) )
pszCmdLine++;
if ( 0 == *pszCmdLine ) // End of command line???
return argc;
if ( '"' == *pszCmdLine ) // Argument starting with a quote???
{
pszCmdLine++; // Advance past quote character
_ppszArgv[ argc++ ] = pszCmdLine;
_ppszArgv[ argc ] = 0;
// Scan to end quote, or NULL terminator
while ( *pszCmdLine && (*pszCmdLine != '"') )
pszCmdLine++;
if ( 0 == *pszCmdLine )
return argc;
if ( *pszCmdLine )
*pszCmdLine++ = 0; // Null terminate and advance to next char
}
else // Non-quoted argument
{
_ppszArgv[ argc++ ] = pszCmdLine;
_ppszArgv[ argc ] = 0;
// Skip till whitespace or NULL terminator
while ( *pszCmdLine && (' '!=*pszCmdLine) && ('\t'!=*pszCmdLine) )
pszCmdLine++;
if ( 0 == *pszCmdLine )
return argc;
if ( *pszCmdLine )
*pszCmdLine++ = 0; // Null terminate and advance to next char
}
if ( argc >= (_MAX_CMD_LINE_ARGS) )
return argc;
}
return argc;
}
// http://stackoverflow.com/a/13281447/4612829
// NO memory alloc, just modified source command line string
enum { kMaxArgs = 64 };
char *simpleArgv[kMaxArgs];
int CommandLineToArgvA_simple(LPSTR commandLine) {
int argc=0;
char *p2 = strtok(commandLine, " ");
while (p2 && argc < kMaxArgs - 1) {
simpleArgv[argc++] = p2;
p2 = strtok(0, " ");
}
simpleArgv[argc] = 0;
return argc;
}