forked from uakfdotb/ent-ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
694 lines (546 loc) · 14 KB
/
util.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
ent-ghost
Copyright [2011-2013] [Jack Lu]
This file is part of the ent-ghost source code.
ent-ghost is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ent-ghost source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ent-ghost source code. If not, see <http://www.gnu.org/licenses/>.
ent-ghost is modified from GHost++ (http://ghostplusplus.googlecode.com/)
GHost++ is Copyright [2008] [Trevor Hogan]
*/
#include "ghost.h"
#include "util.h"
#include <sys/stat.h>
BYTEARRAY UTIL_CreateByteArray( unsigned char *a, int size )
{
if( size < 1 )
return BYTEARRAY( );
return BYTEARRAY( a, a + size );
}
BYTEARRAY UTIL_CreateByteArray( unsigned char c )
{
BYTEARRAY result;
result.push_back( c );
return result;
}
BYTEARRAY UTIL_CreateByteArray( uint16_t i, bool reverse )
{
BYTEARRAY result;
result.push_back( (unsigned char)i );
result.push_back( (unsigned char)( i >> 8 ) );
if( reverse )
return BYTEARRAY( result.rbegin( ), result.rend( ) );
else
return result;
}
BYTEARRAY UTIL_CreateByteArray( uint32_t i, bool reverse )
{
BYTEARRAY result;
result.push_back( (unsigned char)i );
result.push_back( (unsigned char)( i >> 8 ) );
result.push_back( (unsigned char)( i >> 16 ) );
result.push_back( (unsigned char)( i >> 24 ) );
if( reverse )
return BYTEARRAY( result.rbegin( ), result.rend( ) );
else
return result;
}
uint16_t UTIL_ByteArrayToUInt16( BYTEARRAY b, bool reverse, unsigned int start )
{
if( b.size( ) < start + 2 )
return 0;
BYTEARRAY temp = BYTEARRAY( b.begin( ) + start, b.begin( ) + start + 2 );
if( reverse )
temp = BYTEARRAY( temp.rbegin( ), temp.rend( ) );
return (uint16_t)( temp[1] << 8 | temp[0] );
}
uint32_t UTIL_ByteArrayToUInt32( BYTEARRAY b, bool reverse, unsigned int start )
{
if( b.size( ) < start + 4 )
return 0;
BYTEARRAY temp = BYTEARRAY( b.begin( ) + start, b.begin( ) + start + 4 );
if( reverse )
temp = BYTEARRAY( temp.rbegin( ), temp.rend( ) );
return (uint32_t)( temp[3] << 24 | temp[2] << 16 | temp[1] << 8 | temp[0] );
}
string UTIL_ByteArrayToDecString( BYTEARRAY b )
{
if( b.empty( ) )
return string( );
string result = UTIL_ToString( b[0] );
for( BYTEARRAY :: iterator i = b.begin( ) + 1; i != b.end( ); ++i )
result += " " + UTIL_ToString( *i );
return result;
}
string UTIL_ByteArrayToHexString( BYTEARRAY b )
{
if( b.empty( ) )
return string( );
string result = UTIL_ToHexString( b[0] );
for( BYTEARRAY :: iterator i = b.begin( ) + 1; i != b.end( ); ++i )
{
if( *i < 16 )
result += " 0" + UTIL_ToHexString( *i );
else
result += " " + UTIL_ToHexString( *i );
}
return result;
}
void UTIL_AppendByteArray( BYTEARRAY &b, BYTEARRAY append )
{
b.insert( b.end( ), append.begin( ), append.end( ) );
}
void UTIL_AppendByteArrayFast( BYTEARRAY &b, BYTEARRAY &append )
{
b.insert( b.end( ), append.begin( ), append.end( ) );
}
void UTIL_AppendByteArray( BYTEARRAY &b, unsigned char *a, int size )
{
UTIL_AppendByteArray( b, UTIL_CreateByteArray( a, size ) );
}
void UTIL_AppendByteArray( BYTEARRAY &b, string append, bool terminator )
{
// append the string plus a null terminator
b.insert( b.end( ), append.begin( ), append.end( ) );
if( terminator )
b.push_back( 0 );
}
void UTIL_AppendByteArrayFast( BYTEARRAY &b, string &append, bool terminator )
{
// append the string plus a null terminator
b.insert( b.end( ), append.begin( ), append.end( ) );
if( terminator )
b.push_back( 0 );
}
void UTIL_AppendByteArray( BYTEARRAY &b, uint16_t i, bool reverse )
{
UTIL_AppendByteArray( b, UTIL_CreateByteArray( i, reverse ) );
}
void UTIL_AppendByteArray( BYTEARRAY &b, uint32_t i, bool reverse )
{
UTIL_AppendByteArray( b, UTIL_CreateByteArray( i, reverse ) );
}
BYTEARRAY UTIL_ExtractCString( BYTEARRAY &b, unsigned int start )
{
// start searching the byte array at position 'start' for the first null value
// if found, return the subarray from 'start' to the null value but not including the null value
if( start < b.size( ) )
{
for( unsigned int i = start; i < b.size( ); ++i )
{
if( b[i] == 0 )
return BYTEARRAY( b.begin( ) + start, b.begin( ) + i );
}
// no null value found, return the rest of the byte array
return BYTEARRAY( b.begin( ) + start, b.end( ) );
}
return BYTEARRAY( );
}
unsigned char UTIL_ExtractHex( BYTEARRAY &b, unsigned int start, bool reverse )
{
// consider the byte array to contain a 2 character ASCII encoded hex value at b[start] and b[start + 1] e.g. "FF"
// extract it as a single decoded byte
if( start + 1 < b.size( ) )
{
unsigned int c;
string temp = string( b.begin( ) + start, b.begin( ) + start + 2 );
if( reverse )
temp = string( temp.rend( ), temp.rbegin( ) );
stringstream SS;
SS << temp;
SS >> hex >> c;
return c;
}
return 0;
}
BYTEARRAY UTIL_ExtractNumbers( string s, unsigned int count )
{
// consider the string to contain a bytearray in dec-text form, e.g. "52 99 128 1"
BYTEARRAY result;
unsigned int c;
stringstream SS;
SS << s;
for( unsigned int i = 0; i < count; ++i )
{
if( SS.eof( ) )
break;
SS >> c;
// todotodo: if c > 255 handle the error instead of truncating
result.push_back( (unsigned char)c );
}
return result;
}
BYTEARRAY UTIL_ExtractHexNumbers( string s )
{
// consider the string to contain a bytearray in hex-text form, e.g. "4e 17 b7 e6"
BYTEARRAY result;
unsigned int c;
stringstream SS;
SS << s;
while( !SS.eof( ) )
{
SS >> hex >> c;
// todotodo: if c > 255 handle the error instead of truncating
result.push_back( (unsigned char)c );
}
return result;
}
string UTIL_ToString( unsigned long i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( unsigned short i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( unsigned int i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( long i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( short i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( int i )
{
string result;
stringstream SS;
SS << i;
SS >> result;
return result;
}
string UTIL_ToString( float f, int digits )
{
string result;
stringstream SS;
SS << std :: fixed << std :: setprecision( digits ) << f;
SS >> result;
return result;
}
string UTIL_ToString( double d, int digits )
{
string result;
stringstream SS;
SS << std :: fixed << std :: setprecision( digits ) << d;
SS >> result;
return result;
}
string UTIL_ToHexString( uint32_t i )
{
string result;
stringstream SS;
SS << std :: hex << i;
SS >> result;
return result;
}
// todotodo: these UTIL_ToXXX functions don't fail gracefully, they just return garbage (in the uint case usually just -1 casted to an unsigned type it looks like)
uint16_t UTIL_ToUInt16( string &s )
{
uint16_t result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
uint32_t UTIL_ToUInt32( string &s )
{
uint32_t result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
uint64_t UTIL_ToUInt64( string &s )
{
uint64_t result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
int16_t UTIL_ToInt16( string &s )
{
int16_t result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
int32_t UTIL_ToInt32( string &s )
{
int32_t result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
double UTIL_ToDouble( string &s )
{
double result;
stringstream SS;
SS << s;
SS >> result;
return result;
}
string UTIL_MSToString( uint32_t ms )
{
string MinString = UTIL_ToString( ( ms / 1000 ) / 60 );
string SecString = UTIL_ToString( ( ms / 1000 ) % 60 );
if( MinString.size( ) == 1 )
MinString.insert( 0, "0" );
if( SecString.size( ) == 1 )
SecString.insert( 0, "0" );
return MinString + "m" + SecString + "s";
}
bool UTIL_FileExists( string file )
{
struct stat fileinfo;
if( stat( file.c_str( ), &fileinfo ) == 0 )
return true;
return false;
}
string UTIL_FileRead( string file, uint32_t start, uint32_t length )
{
ifstream IS;
IS.open( file.c_str( ), ios :: binary );
if( IS.fail( ) )
{
CONSOLE_Print( "[UTIL] warning - unable to read file part [" + file + "]" );
return string( );
}
// get length of file
IS.seekg( 0, ios :: end );
uint32_t FileLength = IS.tellg( );
if( start > FileLength )
{
IS.close( );
return string( );
}
IS.seekg( start, ios :: beg );
// read data
char *Buffer = new char[length];
IS.read( Buffer, length );
string BufferString = string( Buffer, IS.gcount( ) );
IS.close( );
delete [] Buffer;
return BufferString;
}
string UTIL_FileRead( string file )
{
ifstream IS;
IS.open( file.c_str( ), ios :: binary );
if( IS.fail( ) )
{
CONSOLE_Print( "[UTIL] warning - unable to read file [" + file + "]" );
return string( );
}
// get length of file
IS.seekg( 0, ios :: end );
uint32_t FileLength = IS.tellg( );
IS.seekg( 0, ios :: beg );
// read data
char *Buffer = new char[FileLength];
IS.read( Buffer, FileLength );
string BufferString = string( Buffer, IS.gcount( ) );
IS.close( );
delete [] Buffer;
if( BufferString.size( ) == FileLength )
return BufferString;
else
return string( );
}
bool UTIL_FileWrite( string file, unsigned char *data, uint32_t length )
{
ofstream OS;
OS.open( file.c_str( ), ios :: binary );
if( OS.fail( ) )
{
CONSOLE_Print( "[UTIL] warning - unable to write file [" + file + "]" );
return false;
}
// write data
OS.write( (const char *)data, length );
OS.close( );
return true;
}
string UTIL_FileSafeName( string fileName )
{
string :: size_type BadStart = fileName.find_first_of( "\\/:*?<>|" );
while( BadStart != string :: npos )
{
fileName.replace( BadStart, 1, 1, '_' );
BadStart = fileName.find_first_of( "\\/:*?<>|" );
}
return fileName;
}
string UTIL_AddPathSeperator( string path )
{
if( path.empty( ) )
return string( );
#ifdef WIN32
char Seperator = '\\';
#else
char Seperator = '/';
#endif
if( *(path.end( ) - 1) == Seperator )
return path;
else
return path + string( 1, Seperator );
}
BYTEARRAY UTIL_EncodeStatString( BYTEARRAY &data )
{
unsigned char Mask = 1;
BYTEARRAY Result;
for( unsigned int i = 0; i < data.size( ); ++i )
{
if( ( data[i] % 2 ) == 0 )
Result.push_back( data[i] + 1 );
else
{
Result.push_back( data[i] );
Mask |= 1 << ( ( i % 7 ) + 1 );
}
if( i % 7 == 6 || i == data.size( ) - 1 )
{
Result.insert( Result.end( ) - 1 - ( i % 7 ), Mask );
Mask = 1;
}
}
return Result;
}
BYTEARRAY UTIL_DecodeStatString( BYTEARRAY &data )
{
unsigned char Mask;
BYTEARRAY Result;
for( unsigned int i = 0; i < data.size( ); ++i )
{
if( ( i % 8 ) == 0 )
Mask = data[i];
else
{
if( ( Mask & ( 1 << ( i % 8 ) ) ) == 0 )
Result.push_back( data[i] - 1 );
else
Result.push_back( data[i] );
}
}
return Result;
}
bool UTIL_IsLanIP( BYTEARRAY ip )
{
if( ip.size( ) != 4 )
return false;
// thanks to LuCasn for this function
// 127.0.0.1
if( ip[0] == 127 && ip[1] == 0 && ip[2] == 0 && ip[3] == 1 )
return true;
// 10.x.x.x
if( ip[0] == 10 )
return true;
// 172.16.0.0-172.31.255.255
if( ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31 )
return true;
// 192.168.x.x
if( ip[0] == 192 && ip[1] == 168 )
return true;
// RFC 3330 and RFC 3927 automatic address range
if( ip[0] == 169 && ip[1] == 254 )
return true;
return false;
}
bool UTIL_IsLocalIP( BYTEARRAY ip, vector<BYTEARRAY> &localIPs )
{
if( ip.size( ) != 4 )
return false;
for( vector<BYTEARRAY> :: iterator i = localIPs.begin( ); i != localIPs.end( ); ++i )
{
if( (*i).size( ) != 4 )
continue;
if( ip[0] == (*i)[0] && ip[1] == (*i)[1] && ip[2] == (*i)[2] && ip[3] == (*i)[3] )
return true;
}
return false;
}
void UTIL_Replace( string &Text, string Key, string Value )
{
// don't allow any infinite loops
if( Value.find( Key ) != string :: npos )
return;
string :: size_type KeyStart = Text.find( Key );
while( KeyStart != string :: npos )
{
Text.replace( KeyStart, Key.size( ), Value );
KeyStart = Text.find( Key );
}
}
vector<string> UTIL_Split( string &s, char delim, vector<string> &elems ) {
stringstream ss(s);
string item;
while( getline( ss, item, delim ) ) {
elems.push_back( item );
}
return elems;
}
std::vector<std::string> UTIL_Split( string &s, char delim ) {
vector<string> elems;
UTIL_Split( s, delim, elems );
return elems;
}
vector<string> UTIL_Tokenize( string s, char delim )
{
vector<string> Tokens;
string Token;
for( string :: iterator i = s.begin( ); i != s.end( ); ++i )
{
if( *i == delim )
{
if( Token.empty( ) )
continue;
Tokens.push_back( Token );
Token.clear( );
}
else
Token += *i;
}
if( !Token.empty( ) )
Tokens.push_back( Token );
return Tokens;
}
uint32_t UTIL_Factorial( uint32_t x )
{
uint32_t Factorial = 1;
for( uint32_t i = 2; i <= x; ++i )
Factorial *= i;
return Factorial;
}