/* * Paglo Crawler * Copyright (C) 2006-2008 Paglo Labs Inc. All rights reserved. * www.paglo.com * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #ifndef ScanAgentUtilsH #define ScanAgentUtilsH //--------------------------------------------------------------------------- #include #include #include #include #include using namespace std; //--------------------------------------------------------------------------- /* * Comparison structure/function suitable for using in a string -> string * STL map object. */ struct ltstr { bool operator()(std::string s1, std::string s2) const { return s1 < s2; } }; //--------------------------------------------------------------------------- /* * Generic wrapper for a lock. */ class TLock { public: TLock() { pthread_mutex_init(&Mutex, NULL); } pthread_mutex_t Mutex; }; //--------------------------------------------------------------------------- /* * A locker object will acquire a mutex and then hold it for the lifetime of * the object. This is useful because the Locker will ensure that a lock is * released when the Locker goes out of scope. */ class TLocker { public: TLocker(TLock &Lock) { this->Mutex = &Lock.Mutex; pthread_mutex_lock(this->Mutex); } ~TLocker() { pthread_mutex_unlock(Mutex); } private: pthread_mutex_t *Mutex; }; //--------------------------------------------------------------------------- /* * Platform independent way to close a socket. */ void CloseSocket(int Sock); /* * Platform independent sleep. Units are milliseconds. */ void MillisecondSleep(unsigned int Period); /* * Convert an integer to a string. */ string Int2String(int Value); /* * Return a day as a decimal number (0 - 6, where Sun is 0). The day must be abbreviated * according to the rules used by the "%w" format specifier for strftime(). */ int WDay(std::string Day); /* * Escape or unescape binary data into strings. */ char *EscapeString(const unsigned char *Str, int OldLen, bool EscapeQuotes = true); unsigned char *UnEscapeString(const char *Str, int *NewLen); std::string EscapeString(std::string Str, bool EscapeQuotes = true); char *XMLEscapeString(const unsigned char *Str, int OldLen); std::string XMLEscapeString(std::string Str); /* * Put a message into the log. */ extern FILE *_LogHandle; void LogInit(void); void LogClose(void); void LogMesg(char *Format, ...); void LogError(char *Format, ...); /* * Get a hex string representation of a stream of binary values. */ std::string HexDump(const unsigned char *Data, int Length); /* * Convert wchar_t strings to C++ strings. */ string WcsToStr(wchar_t *WcsPtr); /* * Join strings with the specified delimeter. */ string JoinStrings(string Delim, std::list &Strings); /* * Replace every occurance of SubS in S with Replacement. */ string ReplaceString(string S, string SubS, string Replacement); /* * Split a string on delim into a list of strings. */ std::list SplitString(string &S, string &Delim); /* * Base64 encode an STL string. */ string Base64Encode(const string &Input); /* * Base64 encode a binary sequence of bytes. */ string Base64Encode(const unsigned char *Input, int Len); /* * Convert a stream of bytes to a hex string. */ string Hex2String(const unsigned char *Hex, int Len); /* * Generate an SHA1 hash for Data as a string. */ string GenerateSHA1(string &Data); int GetWeekHour(void); string GenerateAESKey(void); string EncryptAESwithSalt(string &Key, string &Data); string DecryptAESwithSalt(string &Key, string &Data); /* * The type of an issue is used to link to the online help page for that * type of problem. */ typedef enum { itConfigurationFile, // Error parsing configuration file. itNetworkAdapter, // Adapter selection issues. itPlugin, // An issue from a plugin. itSubmission // An issue communicating with server. } TIssueType; typedef enum { isInfo, isWarning, isError } TIssueSeverity; /* * Information about an issue. */ class TIssueInfo { public: TIssueType IssueType; TIssueSeverity IssueSeverity; string Message; int Occurances; time_t FirstOccurance; time_t LastOccurance; string FullMessage(); }; //--------------------------------------------------------------------------- /* * List of issues. */ extern std::vector _IssuesList; extern TLock _IssuesListLock; /* * Log an issue. If the type, severity and Format are the same as a previous * issue then that issue will be updated. */ void LogIssue(TIssueType IssueType, TIssueSeverity IssueSeverity, char *Format, ...); #ifdef WIN32 /* * gettimeofday() replacement for WIN32. */ #ifdef __cplusplus extern "C" { #endif int gettimeofday(struct timeval *tv, struct timezone *tz); #ifdef __cplusplus } #endif #endif // WIN32 //--------------------------------------------------------------------------- #endif