#ifndef _UTILS_H #define _UTILS_H #include #include #include #include #include using std::string; using std::vector; using std::map; #ifdef _DEBUG #define DEBUG_ONLY(f) f #else #define DEBUG_ONLY(f) ((void)0) #endif static const char g_HexDigits[] = "0123456789abcdef"; class CUtils { public: CUtils(); virtual ~CUtils(); static string GetIP(unsigned long addr); static unsigned long GetLongIP(const string& sIP); static string ChangeDir(const string& sPath, const string& sAdd, const string& sHomeDir); static string ToString(short i); static string ToString(unsigned short i); static string ToString(int i); static string ToString(unsigned int i); static string ToString(long i); static string ToString(unsigned long i); static string ToString(unsigned long long i); static string ToString(double i); static string ToString(float i); static string ToPercent(double d); static string ToKBytes(double d); static string Left(const string& s, unsigned int u); static string Right(const string& s, unsigned int u); static string& Trim(string& s); static string& LeftChomp(string& s, unsigned int uLen = 1); static string& RightChomp(string& s, unsigned int uLen = 1); static string Token(const string& s, unsigned int uPos, bool bRest = false, char cSep = ' '); static string Ellipsize(const string& s, unsigned int uLen); static bool WildCmp(const string& sWild, const string& sString); private: protected: }; class CTable : public vector* > { public: CTable(); virtual ~CTable(); bool AddColumn(const string& sName); unsigned int AddRow(); bool SetCell(const string& sColumn, const string& sValue, unsigned int uRowIdx = ~0); bool GetLine(unsigned int uIdx, string& sLine); unsigned int GetColumnWidth(unsigned int uIdx); private: protected: vector m_vsHeaders; map m_msuWidths; // Used to cache the width of a column }; class CFile { public: CFile(const string& sLongName); virtual ~CFile(); enum EFileTypes { FT_REGULAR, FT_DIRECTORY, FT_CHARACTER, FT_BLOCK, FT_FIFO, FT_LINK, FT_SOCK }; static bool IsReg( const string& sLongName, bool bUseLstat = false ); static bool IsDir( const string& sLongName, bool bUseLstat = false ); static bool IsChr( const string& sLongName, bool bUseLstat = false ); static bool IsBlk( const string& sLongName, bool bUseLstat = false ); static bool IsFifo( const string& sLongName, bool bUseLstat = false ); static bool IsLnk( const string& sLongName, bool bUseLstat = true ); static bool IsSock( const string& sLongName, bool bUseLstat = false ); bool IsReg( bool bUseLstat = false ); bool IsDir( bool bUseLstat = false ); bool IsChr( bool bUseLstat = false ); bool IsBlk( bool bUseLstat = false ); bool IsFifo( bool bUseLstat = false ); bool IsLnk( bool bUseLstat = true ); bool IsSock( bool bUseLstat = false ); bool access( int mode ); // for gettin file types, using fstat instead static bool FType( const string sFileName, EFileTypes eType, bool bUseLstat = false ); enum EFileAttr { FA_Name, FA_Size, FA_ATime, FA_MTime, FA_CTime, FA_UID }; // // Functions to retrieve file information // bool Exists() const; unsigned long long GetSize() const; unsigned int GetATime() const; unsigned int GetMTime() const; unsigned int GetCTime() const; int GetUID() const; int GetGID() const; static bool Exists(const string& sFile); static unsigned long long GetSize(const string& sFile); static unsigned int GetATime(const string& sFile); static unsigned int GetMTime(const string& sFile); static unsigned int GetCTime(const string& sFile); static int GetUID(const string& sFile); static int GetGID(const string& sFile); static int GetInfo(const string& sFile, struct stat& st); // // Functions to manipulate the file on the filesystem // int Delete(); int Move(const string& sNewFileName, bool bOverwrite = false); static bool Delete(const string& sFileName); static bool Move(const string& sOldFileName, const string& sNewFileName, bool bOverwrite = false); bool Chmod(mode_t mode); static bool Chmod(const string& sFile, mode_t mode); bool Seek(unsigned long uPos); bool Open( int iFlags, mode_t iMode = 0644 ); int Read( char *pszBuffer, int iBytes ); bool ReadLine( string & sData ); int Write( const char *pszBuffer, u_int iBytes ); int Write( const string & sData ); void Close(); string GetLongName() const; string GetShortName() const; void SetFD( int iFD ); private: string m_sBuffer; int m_iFD; protected: string m_sLongName; //!< Absolute filename (m_sPath + "/" + m_sShortName) string m_sShortName; //!< Filename alone, without path }; #ifdef HAVE_LIBSSL #include #include //! does Blowfish w/64 bit feedback, no padding class CBlowfish { public: /** * @sPassword key to encrypt with * @iEncrypt encrypt method (BF_DECRYPT or BF_ENCRYPT) * @sIvec what to set the ivector to start with, default sets it all 0's */ CBlowfish(const string & sPassword, int iEncrypt, const string & sIvec = ""); ~CBlowfish(); //! output must be freed static unsigned char *MD5(const unsigned char *input, u_int ilen); //! returns an md5 of the string ( not hex encoded ) static string MD5(const string & sInput, bool bHexEncode = false); //! output must be the same size as input void Crypt(unsigned char *input, unsigned char *output, u_int ibytes); //! must free result unsigned char * Crypt(unsigned char *input, u_int ibytes); string Crypt(const string & sData); private: unsigned char *m_ivec; BF_KEY m_bkey; int m_iEncrypt, m_num; }; #endif /* HAVE_LIBSSL */ #define RF_BUFF 4096 inline bool ReadFile(const string & sFilename, string & sLine) { char inbuff[RF_BUFF]; int bytes; // clear ourselves out sLine.clear(); FILE *f = fopen(sFilename.c_str(), "r"); if (!f) { return false; } while((bytes = fread(inbuff, sizeof(char), RF_BUFF, f)) > 0) { sLine.append(inbuff, bytes); } fclose(f); if (bytes < 0) { return false; } return true; } inline bool WriteFile(const string & sFilename, const string & sData) { FILE *f = fopen(sFilename.c_str(), "w"); if (!f) { return false; } int iRet = fwrite(sData.data(), sizeof(char), sData.length(), f); fclose(f); if (iRet <= 0) { return false; } return true; } inline bool ReadLine(const string & sData, string & sLine, u_int & iPos) { sLine.clear(); if (iPos >= sData.length()) { return false; } u_int iFind = sData.find("\n", iPos); if (iFind == string::npos) { sLine = sData.substr(iPos, (sData.length() - iPos)); iPos = string::npos; return true; } sLine = sData.substr(iPos, (iFind - iPos)) + "\n"; iPos = iFind + 1; return true; } inline string Lower(const string & sLine) { string sRet; for(u_int a = 0; a < sLine.length(); a++) { sRet += tolower(sLine[a]); } return sRet; } inline string Upper(const string & sLine) { string sRet; for(u_int a = 0; a < sLine.length(); a++) { sRet += toupper(sLine[a]); } return sRet; } #endif // !_UTILS_H