EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
util.cpp File Reference
#include "eve-common.h"
Include dependency graph for util.cpp:

Go to the source code of this file.

Namespaces

 Utils
 

Functions

int32 ascent_roundf (float v)
 
int32 ascent_roundd (double v)
 
vector< string > StrSplit (const string &src, const string &sep)
 
time_t convTimePeriod (uint32 dLength, char dType)
 
void MakeIntString (char *buf, int num)
 
void MakeIntStringNoZero (char *buf, int num)
 
string ConvertTimeStampToString (uint32 timestamp)
 
string ConvertTimeStampToStringNC (uint32 timestamp)
 
string ConvertTimeStampToDataTime (uint32 timestamp)
 
bool ParseCIDRBan (unsigned int IP, unsigned int Mask, unsigned int MaskBits)
 
unsigned int MakeIP (const char *str)
 
int get_tokens (const char *szInput, char **pOutput, int iMaxCount, char cSeperator)
 
bool CheckIPs (const char *szIPList)
 

Variables

const char * szDayNames [7]
 
const char * szMonthNames [12]
 

Function Documentation

int32 ascent_roundd ( double  v)

Definition at line 34 of file util.cpp.

35 {
36  double int_part = floor(v);
37  double float_part = v - int_part;
38  if( float_part > 0.5f )
39  return int32(int_part + 1.0f);
40  else
41  return int32(int_part);
42 }
signed __int32 int32
Definition: eve-compat.h:49
int32 ascent_roundf ( float  v)

Definition at line 24 of file util.cpp.

References float2int32().

25 {
26  float int_part = floorf(v);
27  float float_part = v - int_part;
28  if( float_part > 0.5f )
29  return float2int32(int_part + 1.0f);
30  else
31  return float2int32(int_part);
32 }
int32 float2int32(const float value)
Fastest Method of float2int32.
Definition: FastInt.h:42

Here is the call graph for this function:

bool CheckIPs ( const char *  szIPList)

Definition at line 545 of file util.cpp.

References get_tokens(), ParseCIDRBan(), and strdup.

546 {
547 #ifdef HAVE_WINDOWS_H
548  char* ip_string = strdup(szIPList);
549 
550  char* tokens[30];
551  int count = get_tokens(ip_string, (char**)tokens, 30, ':');
552 
553  unsigned int ips[30];
554  int masks[30];
555  for(int i = 0; i < count; ++i)
556  {
557  char* t = strchr(tokens[i], '/');
558  if( t != NULL )
559  {
560  *t = 0;
561  masks[i] = atoi(t+1);
562  }
563  else
564  masks[i] = 32;
565 
566  ips[i] = inet_addr(tokens[i]);
567  }
568 
569  char computer_name[100] = {0};
570  DWORD computer_name_len = 100;
571  GetComputerNameA(computer_name, &computer_name_len);
572 
573  hostent* ent = gethostbyname(computer_name);
574  if( ent == NULL )
575  {
576  free(ip_string);
577  return false;
578  }
579 
580  for( int i = 0; ent->h_addr_list[i] != NULL; ++i )
581  {
582  bool pass = false;
583 
584  for( int j = 0; j < count; ++j )
585  {
586  if( ParseCIDRBan(*(unsigned int*)ent->h_addr_list[i], ips[j], masks[j]) )
587  {
588  pass = true;
589  break;
590  }
591  }
592 
593  if( !pass )
594  {
595  free(ip_string);
596  return false;
597  }
598  }
599 
600  free(ip_string);
601 #endif /* HAVE_WINDOWS_H */
602 
603  return true;
604 }
int get_tokens(const char *szInput, char **pOutput, int iMaxCount, char cSeperator)
Definition: util.cpp:491
#define strdup
Definition: eve-compat.h:258
bool ParseCIDRBan(unsigned int IP, unsigned int Mask, unsigned int MaskBits)
Definition: util.cpp:424

Here is the call graph for this function:

string ConvertTimeStampToDataTime ( uint32  timestamp)

Definition at line 391 of file util.cpp.

References MakeIntString().

392 {
393  char szTempBuf[100];
394  time_t t = (time_t)timestamp;
395  tm * pTM = localtime(&t);
396 
397  string szResult;
398  szResult += szDayNames[pTM->tm_wday];
399  szResult += ", ";
400 
401  MakeIntString(szTempBuf, pTM->tm_mday);
402  szResult += szTempBuf;
403  szResult += " ";
404 
405  szResult += szMonthNames[pTM->tm_mon];
406  szResult += " ";
407 
408  MakeIntString(szTempBuf, pTM->tm_year+1900);
409  szResult += szTempBuf;
410  szResult += ", ";
411  MakeIntString(szTempBuf, pTM->tm_hour);
412  szResult += szTempBuf;
413  szResult += ":";
414  MakeIntString(szTempBuf, pTM->tm_min);
415  szResult += szTempBuf;
416  szResult += ":";
417  MakeIntString(szTempBuf, pTM->tm_sec);
418  szResult += szTempBuf;
419 
420  return szResult;
421 }
const char * szMonthNames[12]
Definition: util.cpp:190
void MakeIntString(char *buf, int num)
Definition: util.cpp:194
const char * szDayNames[7]
Definition: util.cpp:186

Here is the call graph for this function:

string ConvertTimeStampToString ( uint32  timestamp)

Definition at line 209 of file util.cpp.

References MakeIntStringNoZero().

210 {
211  int seconds = (int)timestamp;
212  int mins=0;
213  int hours=0;
214  int days=0;
215  int months=0;
216  int years=0;
217  if(seconds >= 60)
218  {
219  mins = seconds / 60;
220  if(mins)
221  {
222  seconds -= mins*60;
223  if(mins >= 60)
224  {
225  hours = mins / 60;
226  if(hours)
227  {
228  mins -= hours*60;
229  if(hours >= 24)
230  {
231  days = hours/24;
232  if(days)
233  {
234  hours -= days*24;
235  if(days >= 30)
236  {
237  months = days / 30;
238  if(months)
239  {
240  days -= months*30;
241  if(months >= 12)
242  {
243  years = months / 12;
244  if(years)
245  {
246  months -= years*12;
247  }
248  }
249  }
250  }
251  }
252  }
253  }
254  }
255  }
256  }
257 
258  char szTempBuf[100];
259  string szResult;
260 
261  if(years) {
262  MakeIntStringNoZero(szTempBuf, years);
263  szResult += szTempBuf;
264  szResult += " years, ";
265  }
266 
267  if(months) {
268  MakeIntStringNoZero(szTempBuf, months);
269  szResult += szTempBuf;
270  szResult += " months, ";
271  }
272 
273  if(days) {
274  MakeIntStringNoZero(szTempBuf, days);
275  szResult += szTempBuf;
276  szResult += " days, ";
277  }
278 
279  if(hours) {
280  MakeIntStringNoZero(szTempBuf, hours);
281  szResult += szTempBuf;
282  szResult += " hours, ";
283  }
284 
285  if(mins) {
286  MakeIntStringNoZero(szTempBuf, mins);
287  szResult += szTempBuf;
288  szResult += " minutes, ";
289  }
290 
291  if(seconds) {
292  MakeIntStringNoZero(szTempBuf, seconds);
293  szResult += szTempBuf;
294  szResult += " seconds";
295  }
296 
297  return szResult;
298 }
void MakeIntStringNoZero(char *buf, int num)
Definition: util.cpp:204

Here is the call graph for this function:

string ConvertTimeStampToStringNC ( uint32  timestamp)

Definition at line 300 of file util.cpp.

References MakeIntStringNoZero().

301 {
302  int seconds = (int)timestamp;
303  int mins=0;
304  int hours=0;
305  int days=0;
306  int months=0;
307  int years=0;
308  if(seconds >= 60)
309  {
310  mins = seconds / 60;
311  if(mins)
312  {
313  seconds -= mins*60;
314  if(mins >= 60)
315  {
316  hours = mins / 60;
317  if(hours)
318  {
319  mins -= hours*60;
320  if(hours >= 24)
321  {
322  days = hours/24;
323  if(days)
324  {
325  hours -= days*24;
326  if(days >= 30)
327  {
328  months = days / 30;
329  if(months)
330  {
331  days -= months*30;
332  if(months >= 12)
333  {
334  years = months / 12;
335  if(years)
336  {
337  months -= years*12;
338  }
339  }
340  }
341  }
342  }
343  }
344  }
345  }
346  }
347  }
348 
349  char szTempBuf[100];
350  string szResult;
351 
352  if(years) {
353  MakeIntStringNoZero(szTempBuf, years);
354  szResult += szTempBuf;
355  szResult += " yrs ";
356  }
357 
358  if(months) {
359  MakeIntStringNoZero(szTempBuf, months);
360  szResult += szTempBuf;
361  szResult += " months ";
362  }
363 
364  if(days) {
365  MakeIntStringNoZero(szTempBuf, days);
366  szResult += szTempBuf;
367  szResult += " days ";
368  }
369 
370  if(hours) {
371  MakeIntStringNoZero(szTempBuf, hours);
372  szResult += szTempBuf;
373  szResult += " hrs ";
374  }
375 
376  if(mins) {
377  MakeIntStringNoZero(szTempBuf, mins);
378  szResult += szTempBuf;
379  szResult += " mins ";
380  }
381 
382  if(seconds) {
383  MakeIntStringNoZero(szTempBuf, seconds);
384  szResult += szTempBuf;
385  szResult += " secs";
386  }
387 
388  return szResult;
389 }
void MakeIntStringNoZero(char *buf, int num)
Definition: util.cpp:204

Here is the call graph for this function:

time_t convTimePeriod ( uint32  dLength,
char  dType 
)

Definition at line 96 of file util.cpp.

97 {
98  time_t rawtime = 0;
99  if (dLength == 0)
100  return rawtime;
101  struct tm * ti = localtime( &rawtime );
102  switch(dType)
103  {
104  case 'h': // hours
105  ti->tm_hour += dLength;
106  break;
107  case 'd': // days
108  ti->tm_mday += dLength;
109  break;
110  case 'w': // weeks
111  ti->tm_mday += 7 * dLength;
112  break;
113  case 'm': // months
114  ti->tm_mon += dLength;
115  break;
116  case 'y': // years
117  // are leap years considered ? do we care ?
118  ti->tm_year += dLength;
119  break;
120  default: // minutes
121  ti->tm_min += dLength;
122  break;
123  }
124  return mktime(ti);
125 }
int get_tokens ( const char *  szInput,
char **  pOutput,
int  iMaxCount,
char  cSeperator 
)

Definition at line 491 of file util.cpp.

Referenced by CheckIPs().

492 {
493  char* p;
494  char* q;
495  int count = 0;
496 
497  p = (char*)szInput;
498  q = p;
499 
500  for(;;)
501  {
502  // get some tokenz!
503  while(*p != '\0' && *p != cSeperator)
504  {
505  ++p;
506  }
507 
508  // add token
509  if( *p == 0 )
510  {
511  // end of string.
512  // do we have data?
513  if( p != q )
514  pOutput[count++] = q;
515 
516  // bai.
517  break;
518  }
519 
520  *p = 0;
521  ++p;
522 
523  // set output pointer
524  pOutput[count++] = q;
525 
526  // skip while bytes are the same as the seperator
527  while(*p == cSeperator)
528  ++p;
529 
530  // reset q pointer
531  q = p;
532 
533  // make sure we're not at EOL
534  if( *p == '\0' )
535  break;
536 
537  // make sure we don't overflow
538  if( count == iMaxCount )
539  break;
540  }
541 
542  return count;
543 }

Here is the caller graph for this function:

void MakeIntString ( char *  buf,
int  num 
)

Definition at line 194 of file util.cpp.

References sprintf().

Referenced by ConvertTimeStampToDataTime().

195 {
196  if(num<10) {
197  buf[0] = '0';
198  sprintf(&buf[1], "%u", num);
199  } else {
200  sprintf(buf,"%u",num);
201  }
202 }
std::string sprintf(const char *fmt,...)
sprintf for std::string.
Definition: eve-compat.cpp:106

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeIntStringNoZero ( char *  buf,
int  num 
)

Definition at line 204 of file util.cpp.

References sprintf().

Referenced by ConvertTimeStampToString(), and ConvertTimeStampToStringNC().

205 {
206  sprintf(buf,"%u",num);
207 }
std::string sprintf(const char *fmt,...)
sprintf for std::string.
Definition: eve-compat.cpp:106

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int MakeIP ( const char *  str)

Definition at line 478 of file util.cpp.

479 {
480  unsigned int bytes[4];
481  unsigned int res;
482  if( sscanf(str, "%u.%u.%u.%u", &bytes[0], &bytes[1], &bytes[2], &bytes[3]) != 4 )
483  {
484  return 0;
485  }
486 
487  res = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
488  return res;
489 }
bool ParseCIDRBan ( unsigned int  IP,
unsigned int  Mask,
unsigned int  MaskBits 
)

Definition at line 424 of file util.cpp.

Referenced by CheckIPs().

425 {
426  // CIDR bans are a compacted form of IP / Submask
427  // So 192.168.1.0/255.255.255.0 would be 192.168.1.0/24
428  // IP's in the 192.168l.1.x range would be hit, others not.
429  unsigned char * source_ip = (unsigned char*)&IP;
430  unsigned char * mask = (unsigned char*)&Mask;
431  int full_bytes = MaskBits / 8;
432  int leftover_bits = MaskBits % 8;
433  //int byte;
434 
435  // sanity checks for the data first
436  if( MaskBits > 32 )
437  {
438  return false;
439  }
440 
441  // this is the table for comparing leftover bits
442  static const unsigned char leftover_bits_compare[9] = {
443  0x00, // 00000000
444  0x80, // 10000000
445  0xC0, // 11000000
446  0xE0, // 11100000
447  0xF0, // 11110000
448  0xF8, // 11111000
449  0xFC, // 11111100
450  0xFE, // 11111110
451  0xFF, // 11111111 - This one isn't used
452  };
453 
454  // if we have any full bytes, compare them with memcpy
455  if( full_bytes > 0 )
456  {
457  if( memcmp( source_ip, mask, full_bytes ) != 0 )
458  {
459  return false;
460  }
461  }
462 
463  // compare the left over bits
464  if( leftover_bits > 0 )
465  {
466  if( ( source_ip[full_bytes] & leftover_bits_compare[leftover_bits] ) !=
467  ( mask[full_bytes] & leftover_bits_compare[leftover_bits] ) )
468  {
469  // one of the bits does not match
470  return false;
471  }
472  }
473 
474  // all of the bits match that were testable
475  return true;
476 }

Here is the caller graph for this function:

vector<string> StrSplit ( const string &  src,
const string &  sep 
)

Definition at line 44 of file util.cpp.

45 {
46  vector<string> r;
47  string s;
48  for (string::const_iterator i = src.begin(); i != src.end(); i++) {
49  if (sep.find(*i) != string::npos) {
50  if (s.length()) r.push_back(s);
51  s = "";
52  } else {
53  s += *i;
54  }
55  }
56  if (s.length()) r.push_back(s);
57  return r;
58 }

Variable Documentation

const char* szDayNames[7]
Initial value:
= {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
}

Definition at line 186 of file util.cpp.

const char* szMonthNames[12]
Initial value:
= {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
}

Definition at line 190 of file util.cpp.