13 #include <sys/types.h>
15 #include <sys/resource.h>
23 #include <boost/version.hpp>
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <boost/filesystem/path.hpp>
27 #include <boost/date_time/gregorian/gregorian_types.hpp>
28 #include <boost/date_time/posix_time/posix_time_types.hpp>
35 static const int64 COIN = 100000000;
36 static const int64 CENT = 1000000;
39 #define BEGIN(a) ((char*)&(a))
40 #define END(a) ((char*)&((&(a))[1]))
41 #define UBEGIN(a) ((unsigned char*)&(a))
42 #define UEND(a) ((unsigned char*)&((&(a))[1]))
43 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
46 #if defined(_MSC_VER) || defined(__MSVCRT__)
58 #if defined(_MSC_VER) || defined(__MSVCRT__)
78 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
81 template <
size_t nBytes,
typename T>
90 u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
95 #define MSG_NOSIGNAL 0
96 #define MSG_DONTWAIT 0
103 #define MAX_PATH 1024
112 #if BOOST_VERSION >= 105000 && (!defined(BOOST_HAS_NANOSLEEP) || BOOST_VERSION >= 105200)
113 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
115 boost::this_thread::sleep(boost::posix_time::milliseconds(n));
124 #define ATTR_WARN_PRINTF(X,Y) __attribute__((format(printf,X,Y)))
126 #define ATTR_WARN_PRINTF(X,Y)
136 extern std::map<std::string, std::string>
mapArgs;
137 extern std::map<std::string, std::vector<std::string> >
mapMultiArgs;
169 #define strprintf(format, ...) real_strprintf(format, 0, __VA_ARGS__)
170 std::string
vstrprintf(
const char *format, va_list ap);
180 #define printf OutputDebugStringF
182 void LogException(std::exception* pex,
const char* pszThread);
185 void ParseString(
const std::string& str,
char c, std::vector<std::string>& v);
190 std::vector<unsigned char>
ParseHex(
const char* psz);
191 std::vector<unsigned char>
ParseHex(
const std::string& str);
192 bool IsHex(
const std::string& str);
193 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid = NULL);
195 std::string
EncodeBase64(
const unsigned char* pch,
size_t len);
197 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid = NULL);
199 std::string
EncodeBase32(
const unsigned char* pch,
size_t len);
203 bool WildcardMatch(
const std::string& str,
const std::string& mask);
209 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
211 const boost::filesystem::path &
GetDataDir(
bool fNetSpecific =
true);
215 void CreatePidFile(
const boost::filesystem::path &path, pid_t pid);
217 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
219 boost::filesystem::path GetSpecialFolderPath(
int nFolder,
bool fCreate =
true);
231 std::string
FormatSubVersion(
const std::string&
name,
int nClientVersion,
const std::vector<std::string>& comments);
258 return strtoll(psz, NULL, 10);
265 return _atoi64(str.c_str());
267 return strtoll(str.c_str(), NULL, 10);
271 inline int atoi(
const std::string& str)
273 return atoi(str.c_str());
278 return (
int)(d > 0 ? d + 0.5 : d - 0.5);
283 return (
int64)(d > 0 ? d + 0.5 : d - 0.5);
288 return (n >= 0 ? n : -n);
292 std::string
HexStr(
const T itbegin,
const T itend,
bool fSpaces=
false)
295 static const char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
296 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
297 rv.reserve((itend-itbegin)*3);
298 for(T it = itbegin; it < itend; ++it)
300 unsigned char val = (
unsigned char)(*it);
301 if(fSpaces && it != itbegin)
303 rv.push_back(hexmap[val>>4]);
304 rv.push_back(hexmap[val&15]);
311 inline std::string
HexStr(
const T& vch,
bool fSpaces=
false)
313 return HexStr(vch.begin(), vch.end(), fSpaces);
317 void PrintHex(
const T pbegin,
const T pend,
const char* pszFormat=
"%s",
bool fSpaces=
true)
319 printf(pszFormat,
HexStr(pbegin, pend, fSpaces).c_str());
322 inline void PrintHex(
const std::vector<unsigned char>& vch,
const char* pszFormat=
"%s",
bool fSpaces=
true)
331 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
334 gettimeofday(&t, NULL);
335 nCounter = (
int64) t.tv_sec * 1000000 + t.tv_usec;
342 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
343 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
348 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
349 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
355 struct tm* ptmTime = gmtime(&n);
357 strftime(pszTime,
sizeof(pszTime), pszFormat, ptmTime);
371 return c ==
'-' || c ==
'/';
384 std::string
GetArg(
const std::string& strArg,
const std::string& strDefault);
402 bool GetBoolArg(
const std::string& strArg,
bool fDefault=
false);
411 bool SoftSetArg(
const std::string& strArg,
const std::string& strValue);
431 static inline uint32_t insecure_rand(
void)
433 insecure_rand_Rz = 36969 * (insecure_rand_Rz & 65535) + (insecure_rand_Rz >> 16);
434 insecure_rand_Rw = 18000 * (insecure_rand_Rw & 65535) + (insecure_rand_Rw >> 16);
449 template <
typename T>
452 if (b.size() == 0)
return a.size() == 0;
453 size_t accumulator = a.size() ^ b.size();
454 for (
size_t i = 0; i < a.size(); i++)
455 accumulator |= a[i] ^ b[i%b.size()];
456 return accumulator == 0;
472 vValues.reserve(size);
473 vValues.push_back(initial_value);
479 if(vValues.size() ==
nSize)
481 vValues.erase(vValues.begin());
483 vValues.push_back(value);
485 vSorted.resize(vValues.size());
486 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
487 std::sort(vSorted.begin(), vSorted.end());
492 int size = vSorted.size();
496 return vSorted[size/2];
500 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
506 return vValues.size();
515 bool NewThread(
void(*pfn)(
void*),
void* parg);
524 #define THREAD_PRIORITY_LOWEST PRIO_MAX
525 #define THREAD_PRIORITY_BELOW_NORMAL 2
526 #define THREAD_PRIORITY_NORMAL 0
527 #define THREAD_PRIORITY_ABOVE_NORMAL 0
534 setpriority(PRIO_THREAD, 0, nPriority);
536 setpriority(PRIO_PROCESS, 0, nPriority);
542 pthread_exit((
void*)nExitCode);
550 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
551 return (value<<16) | (value>>16);
563 std::string s =
strprintf(
"bitcoin-%s", name);
565 printf(
"%s thread start\n", name);
574 catch (boost::thread_interrupted)
576 printf(
"%s thread stop\n", name);
579 catch (std::exception& e) {
589 std::string s =
strprintf(
"bitcoin-%s", name);
593 printf(
"%s thread start\n", name);
595 printf(
"%s thread exit\n", name);
597 catch (boost::thread_interrupted)
599 printf(
"%s thread interrupt\n", name);
602 catch (std::exception& e) {
std::string FormatMoney(int64 n, bool fPlus=false)
bool error(const char *format,...)
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
unsigned long long uint64
#define ATTR_WARN_PRINTF(X, Y)
int64 GetPerformanceCounter()
#define strprintf(format,...)
void PrintException(std::exception *pex, const char *pszThread)
bool ParseMoney(const std::string &str, int64 &nRet)
bool GetBoolArg(const std::string &strArg, bool fDefault=false)
Return boolean argument or default value.
void PrintHex(const T pbegin, const T pend, const char *pszFormat="%s", bool fSpaces=true)
void LogException(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=NULL)
void RandAddSeedPerfmon()
void RenameThread(const char *name)
void ReadConfigFile(std::map< std::string, std::string > &mapSettingsRet, std::map< std::string, std::vector< std::string > > &mapMultiSettingsRet)
std::string SanitizeString(const std::string &str)
unsigned long long uint64
std::map< std::string, std::vector< std::string > > mapMultiArgs
void LoopForever(const char *name, Callable func, int64 msecs)
std::string FormatFullVersion()
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
int64 atoi64(const char *psz)
void ParseString(const std::string &str, char c, std::vector< std::string > &v)
bool NewThread(void(*pfn)(void *), void *parg)
std::map< std::string, std::string > mapArgs
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
std::vector< unsigned char > ParseHex(const char *psz)
int std::string int dummy
int GetFilesize(FILE *file)
const boost::filesystem::path & GetDataDir(bool fNetSpecific=true)
std::string itostr(int n)
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
void TraceThread(const char *name, Callable func)
void ParseParameters(int argc, const char *const argv[])
std::string DateTimeStrFormat(const char *pszFormat, int64 nTime)
void ExitThread(size_t nExitCode)
int OutputDebugStringF(const char *pszFormat,...)
bool IsHex(const std::string &str)
std::string EncodeBase64(const unsigned char *pch, size_t len)
void SetThreadPriority(int nPriority)
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=NULL)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
std::string EncodeBase32(const unsigned char *pch, size_t len)
std::string strMiscWarning
bool IsSwitchChar(char c)
uint32_t insecure_rand_Rz
MWC RNG of George Marsaglia This is intended to be fast.
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
uint32_t ByteReverse(uint32_t value)
void FileCommit(FILE *fileout)
boost::filesystem::path GetConfigFile()
void seed_insecure_rand(bool fDeterministic=false)
Seed insecure_rand using the random pool.
boost::filesystem::path GetTempPath()
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
void SetMockTime(int64 nMockTimeIn)
bool WildcardMatch(const char *psz, const char *mask)
int RaiseFileDescriptorLimit(int nMinFD)
uint64 GetRand(uint64 nMax)
boost::filesystem::path GetPidFile()
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
bool TruncateFile(FILE *file, unsigned int length)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::string i64tostr(int64 n)
void AddTimeData(const CNetAddr &ip, int64 nTime)
volatile bool fReopenDebugLog
int64 roundint64(double d)
boost::filesystem::path GetDefaultDataDir()
std::string vstrprintf(const char *format, va_list ap)
int std::string int std::string real_strprintf(const std::string &format, int dummy,...)
Overload strprintf for std::string, to be able to use it with _ (translation).
uint32_t insecure_rand_Rw
int atoi(const std::string &str)
void runCommand(std::string strCommand)