-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathBugTrapSort.cpp
510 lines (436 loc) · 13.3 KB
/
BugTrapSort.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// BugTrapSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class ATL_NO_VTABLE CZipHandler
{
public:
CZipHandler(LPCTSTR szFilename = NULL) throw()
: hArchive( NULL )
{
if ( szFilename )
Open( szFilename );
}
bool Open(LPCTSTR szFilename) throw()
{
if ( hArchive )
unzClose( hArchive );
hArchive = unzOpen( CT2CA( szFilename ) );
if ( ! hArchive )
{
TCHAR szFileShort[ MAX_PATH ];
if ( GetShortPathName( szFilename, szFileShort, MAX_PATH ) )
hArchive = unzOpen( CT2CA( szFileShort ) );
}
return ( hArchive != NULL );
}
~CZipHandler() throw()
{
if ( hArchive )
{
unzClose( hArchive );
hArchive = NULL;
}
}
operator unzFile() const throw()
{
return hArchive;
}
bool Extract(LPCTSTR szSrc, LPCTSTR szDst) throw()
{
bool ret = false;
char* pBuf = NULL;
DWORD dwSize = 0;
if ( Extract( szSrc, &pBuf, &dwSize ) )
{
HANDLE hFile = CreateFile( szDst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile != INVALID_HANDLE_VALUE )
{
DWORD dwWritten = 0;
if ( WriteFile( hFile, pBuf, dwSize, &dwWritten, NULL ) )
{
ret = ( dwWritten == dwSize );
}
}
CloseHandle( hFile );
}
delete [] pBuf;
return ret;
}
bool Extract(LPCTSTR szSrc, char** ppBuf, DWORD* pdwSize = NULL) throw()
{
if ( pdwSize )
*pdwSize = 0;
*ppBuf = NULL;
bool ret = false;
if ( unzLocateFile( hArchive, CT2CA( szSrc ), 2 ) == UNZ_OK )
{
unz_file_info fi = {};
if ( unzGetCurrentFileInfo( hArchive, &fi, NULL, NULL, NULL, NULL, NULL, NULL ) == UNZ_OK )
{
if ( unzOpenCurrentFile( hArchive ) == UNZ_OK )
{
if ( pdwSize )
*pdwSize = fi.uncompressed_size;
*ppBuf = new char[ fi.uncompressed_size + 2 ];
if ( *ppBuf )
{
ZeroMemory( *ppBuf, fi.uncompressed_size + 2 );
ret = ( unzReadCurrentFile( hArchive, *ppBuf, fi.uncompressed_size ) == (int)fi.uncompressed_size );
}
unzCloseCurrentFile( hArchive );
}
}
}
return ret;
}
protected:
unzFile hArchive;
};
typedef struct
{
CString sIf;
CString sIs;
CString sThen;
} CRule;
typedef CAtlList< CRule > CRuleList;
typedef CAtlMap< CString, bool > CStringMap;
CString g_sConfig;
CRuleList g_oRules;
CString g_sOutput;
CString g_sInput;
CStringMap g_oModules;
#define REPLACE(x) \
x.Replace( _T("{VER_FULL}"), sVersion ); \
x.Replace( _T("{VER_NUMBER}"), sVersionNumber ); \
x.Replace( _T("{VER_CONFIG}"), sVersionConfig ); \
x.Replace( _T("{VER_PLATFORM}"), sVersionPlatform ); \
x.Replace( _T("{VER_REVISION}"), sVersionRevision ); \
x.Replace( _T("{VER_DATE}"), sVersionDate ); \
x.Replace( _T("{TIMESTAMP}"), sTimestamp ); \
x.Replace( _T("{USER}"), sUser ); \
x.Replace( _T("{COMPUTER}"), sComputer ); \
x.Replace( _T("{CPU}"), sCPU ); \
x.Replace( _T("{WHAT}"), sWhat ); \
x.Replace( _T("{MODULE}"), sModule ); \
x.Replace( _T("{ADDRESS}"), sAddress );
CString GetValue(IXMLDOMElement* pRoot, LPCTSTR szXPath)
{
CComPtr< IXMLDOMNode > pNode;
HRESULT hr = pRoot->selectSingleNode( CComBSTR( szXPath ), &pNode );
if ( hr != S_OK )
return CString();
CComBSTR bstrVersion;
hr = pNode->get_text( &bstrVersion );
if ( hr != S_OK )
return CString();
return (LPCWSTR)bstrVersion;
}
CString GetTime(IXMLDOMElement* pRoot, LPCTSTR szNode, LPCTSTR szFormat)
{
try
{
FILETIME fTimestamp;
(__int64&)fTimestamp = _tstoi64( GetValue( pRoot, szNode ) );
CTime tTimeStamp( fTimestamp );
return tTimeStamp.FormatGmt( szFormat );
}
catch( ... )
{
}
return CString();
}
bool ProcessReport(const CString& sInput)
{
_tprintf( _T("Processing %s ...\n"), PathFindFileName( sInput ) );
bool bZip = ( sInput.Find( _T(".zip") ) != -1 );
CComPtr< IXMLDOMDocument > pFile;
HRESULT hr = pFile.CoCreateInstance( CLSID_DOMDocument );
if ( FAILED( hr ) )
return false;
hr = pFile->put_async( VARIANT_FALSE );
if ( hr != S_OK )
return false;
CZipHandler pZip;
VARIANT_BOOL ret = VARIANT_FALSE;
if ( bZip )
{
if ( ! pZip.Open( sInput ) )
return false;
char* pBuf = NULL;
bool res = pZip.Extract( _T("errorlog.xml"), &pBuf );
if ( res )
hr = pFile->loadXML( CComBSTR( pBuf ), &ret );
delete [] pBuf;
if ( ! res )
return false;
}
else
{
hr = pFile->load( CComVariant( sInput + _T("errorlog.xml") ), &ret );
}
if ( hr != S_OK || ! ret )
return false;
CComPtr< IXMLDOMElement > pRoot;
hr = pFile->get_documentElement( &pRoot );
if ( hr != S_OK )
return false;
CString sTimestamp = GetTime( pRoot, _T("/report/timestamp"), _T("%y%m%d-%H%M%S") );
if ( sTimestamp.IsEmpty() ) sTimestamp = _T("000000-000000");
CString sVersion = GetValue( pRoot, _T("/report/version") );
CString sComputer = GetValue( pRoot, _T("/report/computer") );
if ( sComputer.IsEmpty() ) sComputer = _T("UNKNOWN");
CString sUser = GetValue( pRoot, _T("/report/user") );
if ( sUser.IsEmpty() ) sUser = _T("UNKNOWN");
CString sCPU = GetValue( pRoot, _T("/report/cpus/cpu/description") );
if ( sCPU.IsEmpty() )
{
sCPU = GetValue( pRoot, _T("/report/cpus/cpu/id") );
if ( sCPU.IsEmpty() )
sCPU = _T("UNKNOWN");
}
CString sWhat = GetValue( pRoot, _T("/report/error/what") );
if ( sWhat.IsEmpty() ) sWhat = _T("UNKNOWN_ERROR");
CString sAddress = GetValue( pRoot, _T("/report/error/address") );
sAddress = sAddress.Right( 4 );
sAddress.MakeLower();
CString sModule = PathFindFileName( GetValue( pRoot, _T("/report/error/module") ) );
sModule.MakeLower();
bool bGood;
if ( sModule.IsEmpty() ||
( g_oModules.Lookup( sModule, bGood ) && ! bGood ) ||
( g_oModules.Lookup( sModule + _T(":") + sAddress, bGood ) && ! bGood ) )
{
CString sOrigAddress = sAddress;
CString sOrigModule = sModule;
CComPtr< IXMLDOMNode > pStack;
hr = pRoot->selectSingleNode( CComBSTR( _T("/report/threads/thread/stack") ), &pStack );
if ( hr == S_OK )
{
CComPtr< IXMLDOMNodeList > pFrames;
hr = pStack->get_childNodes( &pFrames );
if ( hr == S_OK )
{
long n = 0;
hr = pFrames->get_length( &n );
for ( long i = 0; hr == S_OK && i < n; ++i )
{
CComPtr< IXMLDOMNode > pFrame;
hr = pFrames->get_item( i, &pFrame );
if ( hr == S_OK )
{
CComQIPtr< IXMLDOMElement > pFrameE( pFrame );
sAddress = GetValue( pFrameE, _T("address") );
sAddress = sAddress.Right( 4 );
sAddress.MakeLower();
sModule = PathFindFileName( GetValue( pFrameE, _T("module") ) );
if ( ! sModule.IsEmpty() )
{
sModule.MakeLower();
if ( ( ! g_oModules.Lookup( sModule, bGood ) || bGood ) &&
( ! g_oModules.Lookup( sModule + _T(":") + sAddress, bGood ) || bGood ) )
// Íåèçâåñòíûé ìîäóëü èëè ïîäõîäÿùèé ìîäóëü
break;
}
sAddress.Empty();
sModule.Empty();
}
}
}
}
if ( sModule.IsEmpty() )
{
sAddress = sOrigAddress;
sModule = sOrigModule;
}
}
if ( sAddress.IsEmpty() ) sAddress = _T("0000");
if ( sModule.IsEmpty() ) sModule = _T("UNKNOWN");
// Ðàçáîð âåðñèè
int i = 0;
CString sVersionNumber = sVersion.Tokenize( _T(" "), i );
if ( i == -1 ) sVersionNumber = _T("0.0.0.0");
CString sVersionConfig = ( i == -1 ) ? _T("") : sVersion.Tokenize( _T(" "), i );
if ( i == -1 ) sVersionConfig = _T("u");
if ( sVersionConfig.CompareNoCase( _T("release") ) == 0 ) sVersionConfig = _T("r");
else if ( sVersionConfig.CompareNoCase( _T("debug") ) == 0 ) sVersionConfig = _T("d");
CString sVersionPlatform = ( i == -1 ) ? _T("") : sVersion.Tokenize( _T(" "), i );
if ( i == -1 ) sVersionPlatform = _T("xx");
if ( sVersionPlatform.Find( _T("32") ) != -1 ) sVersionPlatform = _T("32");
else if ( sVersionPlatform.Find( _T("64") ) != -1 ) sVersionPlatform = _T("64");
CString sVersionRevision = ( i == -1 ) ? _T("") : sVersion.Tokenize( _T(" "), i );
if ( i == -1 ) sVersionRevision = _T("UNKNOWN");
sVersionRevision.Trim( _T(" ()") );
sVersionRevision.MakeLower();
CString sVersionDate = ( i == -1 ) ? _T("") : sVersion.Tokenize( _T(" "), i );
if ( i == -1 ) sVersionDate = _T("00000000");
sVersionDate.Trim( _T(" ()") );
if ( sVersion.IsEmpty() )
sVersion = _T("0.0.0.0");
_tprintf( _T("Version: %ls\n"), sVersion );
CString sOutput = g_sOutput;
for ( POSITION pos = g_oRules.GetHeadPosition(); pos ; )
{
CRule r = g_oRules.GetNext( pos );
REPLACE( r.sIf );
if ( StrStrI( r.sIf, r.sIs ) )
{
sOutput = r.sThen;
break;
}
}
if ( sOutput.IsEmpty() )
// Íåò äåéñòâèÿ
return true;
REPLACE( sOutput );
if ( sOutput.IsEmpty() )
// Íåò äåéñòâèÿ
return true;
sOutput.TrimRight( _T("\\") ) += _T("\\");
int res = SHCreateDirectory( GetDesktopWindow(), sOutput );
if ( res != ERROR_SUCCESS && res != ERROR_FILE_EXISTS && res != ERROR_ALREADY_EXISTS )
return false;
if ( bZip )
{
if ( ! pZip.Extract( _T("errorlog.xml"), sOutput + _T("errorlog.xml") ) )
return false;
pZip.Extract( _T("crashdump.dmp"), sOutput + _T("crashdump.dmp") );
pZip.Extract( _T("settings.reg"), sOutput + _T("settings.reg") );
}
else
{
if ( ! CopyFile( sInput + _T("errorlog.xml"), sOutput + _T("errorlog.xml"), FALSE ) )
return false;
CopyFile( sInput + _T("crashdump.dmp"), sOutput + _T("crashdump.dmp"), FALSE );
CopyFile( sInput + _T("settings.reg"), sOutput + _T("settings.reg"), FALSE );
}
return true;
}
bool Enum(LPCTSTR szInput, CAtlList< CString >& oDirs)
{
bool ret = false;
CString sDir = szInput;
sDir = sDir.Left( (DWORD)( PathFindFileName( szInput ) - szInput ) );
WIN32_FIND_DATA wfa = {};
HANDLE hFF = FindFirstFile( szInput, &wfa );
if ( hFF != INVALID_HANDLE_VALUE )
{
do
{
if ( *wfa.cFileName != _T('.') )
{
if ( ( wfa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0 )
{
ret = true;
if ( ! Enum( sDir + wfa.cFileName + _T("\\*.*"), oDirs ) )
// Add folder without subfolders
oDirs.AddTail( sDir + wfa.cFileName + _T("\\") );
}
else if ( _tcsstr( wfa.cFileName, _T(".zip") ) )
{
// Add .zip-file
oDirs.AddTail( sDir + wfa.cFileName );
}
}
}
while ( FindNextFile( hFF, &wfa ) );
FindClose( hFF );
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
if ( argc != 2 )
return 1;
CoInitializeEx( NULL, COINIT_MULTITHREADED );
g_sConfig = argv[ 1 ];
GetPrivateProfileString( _T("options"), _T("output"), NULL,
g_sOutput.GetBuffer( 4096 ), 4096, g_sConfig );
g_sOutput.ReleaseBuffer();
if ( ! g_sOutput.IsEmpty() )
g_sOutput.TrimRight( _T("\\") ) += _T("\\");
GetPrivateProfileString( _T("options"), _T("input"), NULL,
g_sInput.GetBuffer( 4096 ), 4096, g_sConfig );
g_sInput.ReleaseBuffer();
if ( g_sInput.IsEmpty() )
g_sInput = _T(".\\*.*");
// Ïðèíÿòü
CString sModules;
GetPrivateProfileString( _T("options"), _T("accept"), NULL,
sModules.GetBuffer( 4096 ), 4096, g_sConfig );
sModules.ReleaseBuffer();
for ( int i = 0; ; )
{
CString sModule = sModules.Tokenize( _T("|"), i );
if ( sModule.IsEmpty() || i == -1 )
break;
sModule.MakeLower();
bool foo;
if ( g_oModules.Lookup( sModule, foo ) )
{
_tprintf( _T("Duplicate in [options] \"accept\" parameter: %s\n"), sModule );
return 1;
}
g_oModules.SetAt( sModule, true );
}
// Íå ïðèíèìàòü
GetPrivateProfileString( _T("options"), _T("ignore"), NULL,
sModules.GetBuffer( 4096 ), 4096, g_sConfig );
sModules.ReleaseBuffer();
for ( int i = 0; ; )
{
CString sModule = sModules.Tokenize( _T("|"), i );
if ( sModule.IsEmpty() || i == -1 )
break;
sModule.MakeLower();
bool foo;
if ( g_oModules.Lookup( sModule, foo ) )
{
_tprintf( _T("Duplicate in [options] \"ignore\" parameter: %s\n"), sModule );
return 1;
}
g_oModules.SetAt( sModule, false );
}
// Çàãðóçêà ñïèñêà ñåêöèé
CAutoVectorPtr< TCHAR > pSections( new TCHAR[ 16384 ] );
GetPrivateProfileString( NULL, NULL, NULL, pSections, 16384, g_sConfig );
// Çàãðóçêà ïðàâèë èç âñåõ ñåêöèé
for ( LPCTSTR szSect = pSections ; szSect && *szSect ; szSect += lstrlen( szSect ) + 1 )
{
if ( lstrcmpi( szSect, _T("options") ) == 0 )
// Ïðîïóñê ñëóæåáíîé ñåêöèè
continue;
CRule r;
GetPrivateProfileString( szSect, _T("if"), NULL,
r.sIf.GetBuffer( 4096 ), 4096, g_sConfig );
r.sIf.ReleaseBuffer();
GetPrivateProfileString( szSect, _T("output"), NULL,
r.sThen.GetBuffer( 4096 ), 4096, g_sConfig );
r.sThen.ReleaseBuffer();
CString sIses;
GetPrivateProfileString( szSect, _T("is"), NULL,
sIses.GetBuffer( 4096 ), 4096, g_sConfig );
sIses.ReleaseBuffer();
for ( int i = 0; ; )
{
r.sIs = sIses.Tokenize( _T("|"), i );
if ( r.sIs.IsEmpty() || i == -1 )
break;
g_oRules.AddTail( r );
}
}
pSections.Free();
CAtlList< CString > oDirs;
Enum( g_sInput, oDirs );
_tprintf( _T("Processing %d reports from folder %s ...\n"),
oDirs.GetCount(), g_sInput );
for ( POSITION pos = oDirs.GetHeadPosition(); pos; )
{
if ( ! ProcessReport( oDirs.GetNext( pos ) ) )
{
_tprintf( _T("Report load error!\n") );
}
}
_tprintf( _T("Done.\n") );
CoUninitialize();
return 0;
}