/ghost/util.h

http://ghostcb.googlecode.com/ · C Header · 98 lines · 56 code · 17 blank · 25 comment · 0 complexity · d1accbe05d6b83feb6d968945e03cef2 MD5 · raw file

  1. /*
  2. Copyright [2008] [Trevor Hogan]
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
  13. */
  14. #ifndef UTIL_H
  15. #define UTIL_H
  16. // byte arrays
  17. BYTEARRAY UTIL_CreateByteArray( unsigned char *a, int size );
  18. BYTEARRAY UTIL_CreateByteArray( unsigned char c );
  19. BYTEARRAY UTIL_CreateByteArray( uint16_t i, bool reverse );
  20. BYTEARRAY UTIL_CreateByteArray( uint32_t i, bool reverse );
  21. uint16_t UTIL_ByteArrayToUInt16( BYTEARRAY b, bool reverse, unsigned int start = 0 );
  22. uint32_t UTIL_ByteArrayToUInt32( BYTEARRAY b, bool reverse, unsigned int start = 0 );
  23. string UTIL_ByteArrayToDecString( BYTEARRAY b );
  24. string UTIL_ByteArrayToHexString( BYTEARRAY b );
  25. void UTIL_AppendByteArray( BYTEARRAY &b, BYTEARRAY append );
  26. void UTIL_AppendByteArrayFast( BYTEARRAY &b, BYTEARRAY &append );
  27. void UTIL_AppendByteArray( BYTEARRAY &b, unsigned char *a, int size );
  28. void UTIL_AppendByteArray( BYTEARRAY &b, string append, bool terminator = true );
  29. void UTIL_AppendByteArrayFast( BYTEARRAY &b, string &append, bool terminator = true );
  30. void UTIL_AppendByteArray( BYTEARRAY &b, uint16_t i, bool reverse );
  31. void UTIL_AppendByteArray( BYTEARRAY &b, uint32_t i, bool reverse );
  32. BYTEARRAY UTIL_ExtractCString( BYTEARRAY &b, unsigned int start );
  33. unsigned char UTIL_ExtractHex( BYTEARRAY &b, unsigned int start, bool reverse );
  34. BYTEARRAY UTIL_ExtractNumbers( string s, unsigned int count );
  35. BYTEARRAY UTIL_ExtractHexNumbers( string s );
  36. // conversions
  37. string UTIL_ToString( unsigned long i );
  38. string UTIL_ToString( unsigned short i );
  39. string UTIL_ToString( unsigned int i );
  40. string UTIL_ToString( long i );
  41. string UTIL_ToString( short i );
  42. string UTIL_ToString( int i );
  43. string UTIL_ToString( float f, int digits );
  44. string UTIL_ToString( double d, int digits );
  45. string UTIL_ToHexString( uint32_t i );
  46. uint16_t UTIL_ToUInt16( string &s );
  47. uint32_t UTIL_ToUInt32( string &s );
  48. int16_t UTIL_ToInt16( string &s );
  49. int32_t UTIL_ToInt32( string &s );
  50. double UTIL_ToDouble( string &s );
  51. string UTIL_MSToString( uint32_t ms );
  52. void UTIL_Construct_UTF8_Latin1_Map( );
  53. string UTIL_Latin1ToUTF8( string &s );
  54. string UTIL_UTF8ToLatin1( string &s );
  55. unsigned long UTIL_ToULong( int i );
  56. // files
  57. bool UTIL_FileExists( string file );
  58. string UTIL_FileRead( string file, uint32_t start, uint32_t length );
  59. string UTIL_FileRead( string file );
  60. bool UTIL_FileWrite( string file, unsigned char *data, uint32_t length );
  61. string UTIL_FileSafeName( string fileName );
  62. string UTIL_AddPathSeperator( string path );
  63. // stat strings
  64. BYTEARRAY UTIL_EncodeStatString( BYTEARRAY &data );
  65. BYTEARRAY UTIL_DecodeStatString( BYTEARRAY &data );
  66. // other
  67. bool UTIL_IsLanIP( BYTEARRAY ip );
  68. bool UTIL_IsLocalIP( BYTEARRAY ip, vector<BYTEARRAY> &localIPs );
  69. void UTIL_Replace( string &Text, string Key, string Value );
  70. vector<string> UTIL_Tokenize( string s, char delim );
  71. // math
  72. uint32_t UTIL_Factorial( uint32_t x );
  73. #define nCr(n, r) (UTIL_Factorial(n) / UTIL_Factorial((n)-(r)) / UTIL_Factorial(r))
  74. #define nPr(n, r) (UTIL_Factorial(n) / UTIL_Factorial((n)-(r)))
  75. #endif