-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.cpp
397 lines (362 loc) · 13.6 KB
/
Common.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
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
#include "Hout.h"
#include "Common.h"
#include "Version.h"
#include <algorithm>
#include <iostream>
//Original WildcardCmp:
// Written by Jack Handy <[email protected]>
// http://www.codeproject.com/Articles/1088/Wildcard-string-compare-globbing
//Changes made:
// Uses wchar_t instead of char
// Match is case-insensitive
// Optimized cp variable increment
// "Path" version additionally matches '\' (Windows path delimiter) in string only with '\' in wildcard ignoring '?' and '*'
bool WildcardCmp(const wchar_t* wild, const wchar_t* string)
{
const wchar_t *cp=NULL, *mp=NULL;
while ((*string)&&(*wild!=L'*')) {
if ((towlower(*wild)!=towlower(*string))&&(*wild!=L'?'))
return false;
wild++;
string++;
}
while (*string) {
if (*wild==L'*') {
if (!*++wild)
return true;
mp=wild;
cp=string;
} else if ((towlower(*wild)==towlower(*string))||(*wild==L'?')) {
wild++;
string++;
} else {
wild=mp;
string=++cp;
}
}
while (*wild==L'*')
wild++;
return !*wild;
}
bool PathWildcardCmp(const wchar_t* wild, const wchar_t* string)
{
const wchar_t *cp=NULL, *mp=NULL;
while (*string&&*wild!=L'*') {
if (towlower(*wild)!=towlower(*string)&&(*wild!=L'?'||*string==L'\\'))
return false;
wild++;
string++;
}
while (*string) {
if (*wild==L'*') {
mp=++wild;
cp=string;
} else if (towlower(*wild)==towlower(*string)||(*wild==L'?'&&*string!=L'\\')) {
wild++;
string++;
} else if (*cp==L'\\') {
return false;
} else {
wild=mp;
string=++cp;
}
}
while (*wild==L'*')
wild++;
return !*wild;
}
bool MultiWildcardCmp(const wchar_t* wild, const wchar_t* string, const wchar_t* delim, bool is_path)
{
wchar_t buffer[wcslen(wild)+1];
wcscpy(buffer, wild);
for (wchar_t* token=wcstok(buffer, delim); token; token=wcstok(NULL, delim))
if (is_path?PathWildcardCmp(token, string):WildcardCmp(token, string)) return true;
return false;
}
void MakeRulesFromArgv(int argc, wchar_t** argv, std::stack<std::wstring> &rules, int skip_argc)
{
wchar_t *head, *token;
while (argc-->skip_argc) switch (*argv[argc]) {
case L'/':
if ((token=wcschr(argv[argc], L'=')))
*token++=L'\0';
rules.push((head=wcstok(argv[argc], L":")));
if (token)
rules.push(std::wstring(L"=")+token);
while ((token=wcstok(NULL, L":")))
rules.push(head+std::wstring(L":")+token);
continue;
case L'+':
case L'-':
head=argv[argc];
while (*++argv[argc]!=L'\0')
rules.push({*head, *argv[argc]});
continue;
case L'#':
case L'\0':
continue;
default:
std::wcerr<<L"Warning: unknown input: "<<argv[argc]<<std::endl;
}
#if DEBUG>=3
std::stack<std::wstring> _rules=rules;
std::wcerr<<L"" __FILE__ ":MakeRulesFromArgv:"<<__LINE__<<L": Rules (unfolding stack)..."<<std::endl;
while (!_rules.empty()) {
std::wcerr<<L"\t\t"<<_rules.top()<<std::endl;
_rules.pop();
}
#endif
}
bool PidListCmp(const wchar_t* pid_list, std::vector<ULONG_PTR> *uptr_array, const ULONG_PTR *pid)
{
std::vector<ULONG_PTR> local_uptr_array;
if (!uptr_array)
uptr_array=&local_uptr_array;
if (pid_list) {
wchar_t buffer[wcslen(pid_list)+1];
wcscpy(buffer, pid_list);
ULONG_PTR dw_pri, dw_sec, *pdw_cur=&dw_pri;
wchar_t* rtok;
bool cnv_err=false;
for (wchar_t* token=wcstok(buffer, L",;"); token; token=wcstok(NULL, L",;")) {
for(;;) {
if (!*token||*token==L'-'||*token==L'+'||*token==L' ') {
cnv_err=true;
break;
}
*pdw_cur=wcstoul(token, &rtok, 0);
if ((*pdw_cur==0&&rtok==token)||(*pdw_cur==ULONG_MAX&&errno==ERANGE)||(*rtok&&(*rtok!=L'-'||pdw_cur!=&dw_pri))) {
cnv_err=true;
break;
}
if (*rtok) {
token=rtok+1;
pdw_cur=&dw_sec;
} else {
if (pdw_cur==&dw_sec) {
for (DWORD dw_i=dw_pri; uptr_array->push_back(dw_i), dw_i!=dw_sec; dw_pri<=dw_sec?dw_i++:dw_i--);
pdw_cur=&dw_pri;
} else
uptr_array->push_back(dw_pri);
break;
}
}
if (cnv_err) {
std::wcerr<<L"Warning: PID list \""<<pid_list<<L"\" is malformed, error in token \""<<token<<L"\"!"<<std::endl;
uptr_array->clear();
return false;
}
}
//All this hassle with sorting, erasing and following binary_search is to speed up performance with big PID arrays
//Because intended use for this function is mass PID killing or killing single PIDs from vaguely known range
std::sort(uptr_array->begin(), uptr_array->end());
uptr_array->erase(std::unique(uptr_array->begin(), uptr_array->end()), uptr_array->end());
}
if (pid)
return std::binary_search(uptr_array->begin(), uptr_array->end(), *pid);
else
return true;
}
HANDLE OpenProcessWrapper(DWORD dwProcessId, DWORD &dwDesiredAccess, DWORD dwMandatory)
{
//If OpenProcess with provided dwDesiredAccess succeeded - return resulting hProcess
if (HANDLE hProcess=OpenProcess(dwDesiredAccess, FALSE, dwProcessId))
return hProcess;
//If PROCESS_ALL_ACCESS is required - fail
if (dwDesiredAccess==PROCESS_ALL_ACCESS) {
dwDesiredAccess=0;
return NULL;
//If dwDesiredAccess contains PROCESS_QUERY_INFORMATION - change it to PROCESS_QUERY_LIMITED_INFORMATION and try again
} else if (dwDesiredAccess&PROCESS_QUERY_INFORMATION&~dwMandatory) {
dwDesiredAccess&=~PROCESS_QUERY_INFORMATION;
dwDesiredAccess|=PROCESS_QUERY_LIMITED_INFORMATION;
//If dwDesiredAccess contains PROCESS_VM_READ - remove it and try again
//Additionally change PROCESS_QUERY_LIMITED_INFORMATION to PROCESS_QUERY_INFORMATION if present
} else if (dwDesiredAccess&PROCESS_VM_READ&~dwMandatory) {
if (dwDesiredAccess&PROCESS_QUERY_LIMITED_INFORMATION&~dwMandatory) {
dwDesiredAccess&=~PROCESS_QUERY_LIMITED_INFORMATION;
dwDesiredAccess|=PROCESS_QUERY_INFORMATION;
}
dwDesiredAccess&=~PROCESS_VM_READ;
//If no PROCESS_QUERY_INFORMATION or PROCESS_VM_READ flags was set - fail
} else {
dwDesiredAccess=0;
return NULL;
}
return OpenProcessWrapper(dwProcessId, dwDesiredAccess, dwMandatory);
}
std::wstring GetNamePartFromFullPath(const std::wstring& fpath)
{
//Instead of using clumsy _wsplitpath use std::wstring magick nowing that supplied path is full one
size_t last_backslash;
if ((last_backslash=fpath.find_last_of(L'\\'))!=std::wstring::npos&&last_backslash<fpath.length())
return fpath.substr(last_backslash+1);
else
return std::wstring();
}
bool CheckIfFileExists(const wchar_t* fpath)
{
if (!fpath||fpath[0]==L'\0'||( //We don't need NULL or empty paths
(fpath[0]!=L'\\'||fpath[1]!=L'\\')&& //We interested in UNC and...
(fpath[1]!=L':'||fpath[2]!=L'\\') //...absolute paths
))
return false;
//Ballad about full vs relative paths
//CheckIfFileExists needs full path: i.e. path which can't be misinterpreted - it should stay the same regardless of CWD, CD or PATH variable
//It needs it because it is hevaily used in scenarios where real path should be reconstructed from some nonsense
//And some of this nonsense may look like relative path and be falsely reported as something that might be real
//In the end, there is no relative-path based algorithms in SnK - only name and full-path based
//Here MS have an official paper about which paths are considred relative/full on Windows: https://msdn.microsoft.com/library/windows/desktop/aa365247.aspx#paths
//They also have PathIsRelative funcion in shlwapi.dll (4.71+)
//In ReactOS/Wine PathIsRelative is reversed to the following algorithm (original Win NT algorithm is actually the same):
//If it starts from slash ('\') or second character is colon (':') then return false, otherwise return true
//Main thing to consider is Microsoft's definition of "relative path" - here it means "path relative to current directory of the current drive" (historically each drive letter has it's own current directory)
//So if PathIsRelative returns false it doesn't really mean that path is absolute - it simply means that path doesn't satisfy the above-mentioned definition
//E.g. "C:tmp.txt" (relative to current directory but not drive) and "\blah\blah.txt" (relative to current drive but not directory) causes PathIsRelative to return false
//So here is refined algorithm for CheckIfFileExists to check if file path is absolute in strict NT kernel terms: RtlPathTypeUncAbsolute or RtlPathTypeDriveAbsolute (see RtlDetermineDosPathNameType_U):
//It starts from double slash ("\\") or it's second-to-third chracters are colon with slash (":\") - it's assumed that supplied path has nothing to do with device paths
DWORD dwAttrib=GetFileAttributes(fpath); //Works with UNC paths (ok), relative paths (fixed by code above), affected by Wow64FsRedirection (need some external code to turn this off), can fail because of security restrictions (whatever)
if (dwAttrib!=INVALID_FILE_ATTRIBUTES&&!(dwAttrib&FILE_ATTRIBUTE_DIRECTORY)) //Don't need directories
return true;
else
return false;
}
void PrintUsage()
{
#ifndef HIDDEN
Hout::Separator(L"Usage: SnK [settings_block|swith[:parametres][=argument]] ...");
Hout::EmptyLine();
Hout::Separator(L"This is a usage quick reference. Please check README.TXT for more information.");
Hout::EmptyLine();
Hout::Separator(L"Switches:");
Hout::Separator(L"/hlp", 2);
Hout::Paragraph(L"Print this help and exit.",
4);
Hout::Separator(L"/ver", 2);
Hout::Paragraph(L"Print version information and exit.",
4);
Hout::Separator(L"/bpp", 2);
Hout::Paragraph(L"Make standart Windows 'informational beep' and continue execution.",
4);
Hout::Separator(L"/prn=TEXT", 2);
Hout::Paragraph(L"Print passed text to stdout and continue execution.",
4);
Hout::Separator(L"/sec", 2);
Hout::Paragraph(L"Secured execution. Will exit program if there is another instance already running that has executed this switch.",
4);
Hout::Separator(L"/cpu", 2);
Hout::Paragraph(L"Kill process with highest cpu load.",
4);
Hout::Separator(L"/pth[:full]=WCARDS", 2);
Hout::Paragraph(L"Kill process with highest cpu load which name matches one of wildcars (case-insensitive, with globbing).",
4);
Hout::Separator(L"/mod[:full]=WCARDS", 2);
Hout::Paragraph(L"Kill process with highest cpu load that has module which name matches one of wildcars (case-insensitive, with globbing).",
4);
Hout::Separator(L"/pid[:parent|=PIDS]", 2);
Hout::Paragraph(L"Kill process with highest cpu load which PID belongs to PID array.",
4);
Hout::Separator(L"/d3d[:simple]", 2);
Hout::Paragraph(L"Kill process with highest cpu load that uses DirectX (Direct3D).",
4);
Hout::Separator(L"/ogl[:simple]", 2);
Hout::Paragraph(L"Kill process with highest cpu load that uses OpenGL.",
4);
Hout::Separator(L"/gld[:simple]", 2);
Hout::Paragraph(L"Kill process with highest cpu load that uses Glide (3Dfx).",
4);
Hout::Separator(L"/inr[:plus]", 2);
Hout::Paragraph(L"Kill process with highest cpu load that doesn't respond (Is Not Responding).",
4);
Hout::Separator(L"/fsc[:anywnd][:primary]", 2);
Hout::Paragraph(L"Kill process with highest cpu load that is running in fullscreen.",
4);
Hout::Separator(L"/fgd[:anywnd]", 2);
Hout::Paragraph(L"Kill process which window is in foreground.",
4);
Hout::Separator(L"/cmd[:sub][:utf8|:utf16]=FILE", 2);
Hout::Paragraph(L"Load additional commands from file and continue execution.",
4);
Hout::Separator(L"/lst[:clrmask|:invmask]", 2);
Hout::Paragraph(L"List currently available processes and continue execution.",
4);
Hout::Separator(L"/psh=ARGUMENT", 2);
Hout::Paragraph(L"Push argument to argument stack and continue execution.",
4);
Hout::Separator(L"/pop[=ENV_VAR]", 2);
Hout::Paragraph(L"Pop argument from argument stack and continue execution.",
4);
Hout::Separator(L"/end", 2);
Hout::Paragraph(L"Unconditionally exit program.",
4);
Hout::EmptyLine();
Hout::Separator(L"Settings:");
Hout::Separator(L"+t|-t", 2);
Hout::Paragraph(L"Turn 'test' mode on/off.",
4);
Hout::Separator(L"+v|-v", 2);
Hout::Paragraph(L"Turn 'verbose' mode on/off.",
4);
Hout::Separator(L"+a|-a", 2);
Hout::Paragraph(L"Turn 'query all processes' mode on/off.",
4);
Hout::Separator(L"+l|-l", 2);
Hout::Paragraph(L"Turn 'loop' mode on/off.",
4);
Hout::Separator(L"+i|-i", 2);
Hout::Paragraph(L"Turn 'ignore' mode on/off.",
4);
Hout::Separator(L"+n|-n", 2);
Hout::Paragraph(L"Turn 'negate' mode on/off.",
4);
Hout::Separator(L"+b|-b", 2);
Hout::Paragraph(L"Turn 'blacklist' mode on/off.",
4);
Hout::Separator(L"+w|-w", 2);
Hout::Paragraph(L"Turn 'whitelist' mode on/off.",
4);
Hout::Separator(L"+r|-r", 2);
Hout::Paragraph(L"Turn 'recently created sort' mode on/off.",
4);
Hout::Separator(L"+m|-m", 2);
Hout::Paragraph(L"Turn 'mute' mode on/off.",
4);
Hout::Separator(L"+c|-c", 2);
Hout::Paragraph(L"Turn 'close' mode on/off.",
4);
Hout::Separator(L"+e|-e", 2);
Hout::Paragraph(L"Turn 'expand environment variables' mode on/off.",
4);
#else
Hout::Separator(L"Usage: SnKh [settings_block|swith[:parametres][=argument]] ...");
Hout::EmptyLine();
Hout::Separator(L"Please check README.TXT for more information.");
Hout::EmptyLine();
#endif
}
void PrintVersion()
{
#ifndef HIDDEN
#ifdef _WIN64
Hout::Separator(L"Search and Kill (x64) v" SNK_STR_VERSION);
#else
Hout::Separator(L"Search and Kill v" SNK_STR_VERSION);
#endif
#else
#ifdef _WIN64
Hout::Separator(L"Search and Kill (x64 windowless) v" SNK_STR_VERSION);
#else
Hout::Separator(L"Search and Kill (windowless) v" SNK_STR_VERSION);
#endif
#endif
Hout::Separator(L"Built on " __DATE__ L" at " __TIME__);
Hout::Separator(L"Copyright (c) " SNK_CRIGHT_YEARS " Lcferrum");
Hout::Separator(L"Licensed under BSD license - see LICENSE.TXT file for details");
Hout::EmptyLine();
#ifndef HIDDEN
Hout::Separator(L"Run with /hlp switch for usage information");
#else
Hout::Separator(L"Please check README.TXT for more information");
Hout::EmptyLine();
#endif
}