9 #define _POSIX_C_SOURCE 200112L
13 #include <sys/resource.h>
20 #include <boost/algorithm/string/join.hpp>
21 #include <boost/algorithm/string/case_conv.hpp>
22 #include <boost/algorithm/string/predicate.hpp>
29 namespace program_options {
34 #include <boost/program_options/detail/config_file.hpp>
35 #include <boost/program_options/parsers.hpp>
36 #include <boost/filesystem.hpp>
37 #include <boost/filesystem/fstream.hpp>
38 #include <boost/foreach.hpp>
39 #include <boost/thread.hpp>
40 #include <openssl/crypto.h>
41 #include <openssl/rand.h>
46 #pragma warning(disable:4786)
47 #pragma warning(disable:4804)
48 #pragma warning(disable:4805)
49 #pragma warning(disable:4717)
54 #define _WIN32_WINNT 0x0501
58 #define _WIN32_IE 0x0501
59 #define WIN32_LEAN_AND_MEAN 1
65 #elif defined(__linux__)
66 # include <sys/prctl.h>
93 if (mode & CRYPTO_LOCK) {
110 for (
int i = 0; i < CRYPTO_num_locks(); i++)
125 CRYPTO_set_locking_callback(NULL);
126 for (
int i = 0; i < CRYPTO_num_locks(); i++)
127 delete ppmutexOpenSSL[i];
128 OPENSSL_free(ppmutexOpenSSL);
144 RAND_add(&nCounter,
sizeof(nCounter), 1.5);
145 memset(&nCounter, 0,
sizeof(nCounter));
153 static int64 nLastPerfmon;
154 if (
GetTime() < nLastPerfmon + 10 * 60)
161 unsigned char pdata[250000];
162 memset(pdata, 0,
sizeof(pdata));
163 unsigned long nSize =
sizeof(pdata);
164 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global", NULL, NULL, pdata, &nSize);
165 RegCloseKey(HKEY_PERFORMANCE_DATA);
166 if (ret == ERROR_SUCCESS)
168 RAND_add(pdata, nSize, nSize/100.0);
169 OPENSSL_cleanse(pdata, nSize);
170 printf(
"RandAddSeed() %lu bytes\n", nSize);
182 uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
185 RAND_bytes((
unsigned char*)&nRand,
sizeof(nRand));
186 while (nRand >= nRange);
187 return (nRand % nMax);
198 RAND_bytes((
unsigned char*)&hash,
sizeof(hash));
219 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
222 static FILE* fileout = NULL;
223 static boost::mutex* mutexDebugLog = NULL;
225 static void DebugPrintInit()
227 assert(fileout == NULL);
228 assert(mutexDebugLog == NULL);
230 boost::filesystem::path pathDebug =
GetDataDir() /
"debug.log";
231 fileout = fopen(pathDebug.string().c_str(),
"a");
232 if (fileout) setbuf(fileout, NULL);
234 mutexDebugLog =
new boost::mutex();
244 va_start(arg_ptr, pszFormat);
245 ret += vprintf(pszFormat, arg_ptr);
250 static bool fStartedNewLine =
true;
251 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
256 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
261 boost::filesystem::path pathDebug =
GetDataDir() /
"debug.log";
262 if (freopen(pathDebug.string().c_str(),
"a",fileout) != NULL)
263 setbuf(fileout, NULL);
269 if (pszFormat[strlen(pszFormat) - 1] ==
'\n')
270 fStartedNewLine =
true;
272 fStartedNewLine =
false;
275 va_start(arg_ptr, pszFormat);
276 ret += vfprintf(fileout, pszFormat, arg_ptr);
287 LOCK(cs_OutputDebugStringF);
288 static std::string buffer;
291 va_start(arg_ptr, pszFormat);
295 int line_start = 0, line_end;
296 while((line_end = buffer.find(
'\n', line_start)) != -1)
298 OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
299 line_start = line_end + 1;
300 ret += line_end-line_start;
302 buffer.erase(0, line_start);
313 int limit =
sizeof(buffer);
318 va_copy(arg_ptr, ap);
320 ret = _vsnprintf(p, limit, format, arg_ptr);
322 ret = vsnprintf(p, limit, format, arg_ptr);
325 if (ret >= 0 && ret < limit)
332 throw std::bad_alloc();
334 string str(p, p+ret);
343 va_start(arg_ptr, dummy);
352 va_start(arg_ptr, dummy);
353 string str =
vstrprintf(format.c_str(), arg_ptr);
361 va_start(arg_ptr, format);
362 std::string str =
vstrprintf(format, arg_ptr);
364 printf(
"ERROR: %s\n", str.c_str());
373 string::size_type i1 = 0;
374 string::size_type i2;
377 i2 = str.find(c, i1);
380 v.push_back(str.substr(i1));
383 v.push_back(str.substr(i1, i2-i1));
393 int64 n_abs = (n > 0 ? n : -n);
394 int64 quotient = n_abs/COIN;
395 int64 remainder = n_abs%COIN;
400 for (
int i = str.size()-1; (str[i] ==
'0' && isdigit(str[i-2])); --i)
403 str.erase(str.size()-nTrim, nTrim);
406 str.insert((
unsigned int)0, 1,
'-');
407 else if (fPlus && n > 0)
408 str.insert((
unsigned int)0, 1,
'+');
422 const char* p = pszIn;
430 int64 nMult = CENT*10;
431 while (isdigit(*p) && (nMult > 0))
433 nUnits += nMult * (*p++ -
'0');
442 strWhole.insert(strWhole.end(), *p);
447 if (strWhole.size() > 10)
449 if (nUnits < 0 || nUnits > COIN)
452 int64 nValue = nWhole*COIN + nUnits;
460 static string safeChars(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@");
464 for (std::string::size_type i = 0; i < str.size(); i++)
466 if (safeChars.find(str[i]) != std::string::npos)
467 strResult.push_back(str[i]);
472 static const signed char phexdigit[256] =
473 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
474 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
475 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
476 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
477 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
478 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
479 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
480 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
481 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
482 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
483 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
484 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
485 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
486 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
487 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
488 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
492 BOOST_FOREACH(
unsigned char c, str)
494 if (phexdigit[c] < 0)
497 return (str.size() > 0) && (str.size()%2 == 0);
503 vector<unsigned char> vch;
506 while (isspace(*psz))
508 signed char c = phexdigit[(
unsigned char)*psz++];
509 if (c == (
signed char)-1)
511 unsigned char n = (c << 4);
512 c = phexdigit[(
unsigned char)*psz++];
513 if (c == (
signed char)-1)
526 static void InterpretNegativeSetting(
string name, map<string, string>& mapSettingsRet)
529 if (name.find(
"-no") == 0)
531 std::string positive(
"-");
532 positive.append(name.begin()+3, name.end());
533 if (mapSettingsRet.count(positive) == 0)
536 mapSettingsRet[positive] = (value ?
"1" :
"0");
545 for (
int i = 1; i < argc; i++)
547 std::string str(argv[i]);
548 std::string strValue;
549 size_t is_index = str.find(
'=');
550 if (is_index != std::string::npos)
552 strValue = str.substr(is_index+1);
553 str = str.substr(0, is_index);
556 boost::to_lower(str);
557 if (boost::algorithm::starts_with(str,
"/"))
558 str =
"-" + str.substr(1);
570 string name = entry.first;
573 if (name.find(
"--") == 0)
575 std::string singleDash(name.begin()+1, name.end());
576 if (
mapArgs.count(singleDash) == 0)
577 mapArgs[singleDash] = entry.second;
582 InterpretNegativeSetting(name,
mapArgs);
586 std::string
GetArg(
const std::string& strArg,
const std::string& strDefault)
611 bool SoftSetArg(
const std::string& strArg,
const std::string& strValue)
630 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
633 strRet.reserve((len+2)/3*4);
636 const unsigned char *pchEnd = pch+len;
644 strRet += pbase64[enc >> 2];
645 left = (enc & 3) << 4;
650 strRet += pbase64[left | (enc >> 4)];
651 left = (enc & 15) << 2;
656 strRet += pbase64[left | (enc >> 6)];
657 strRet += pbase64[enc & 63];
665 strRet += pbase64[left];
676 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
681 static const int decode64_table[256] =
683 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
684 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
685 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
686 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
687 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
688 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
689 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
690 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
691 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
692 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
693 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
694 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
695 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
701 vector<unsigned char> vchRet;
702 vchRet.reserve(strlen(p)*3/4);
709 int dec = decode64_table[(
unsigned char)*p];
710 if (dec == -1)
break;
720 vchRet.push_back((left<<2) | (dec>>4));
726 vchRet.push_back((left<<4) | (dec>>2));
732 vchRet.push_back((left<<6) | dec);
749 if (left || p[0] !=
'=' || p[1] !=
'=' || decode64_table[(
unsigned char)p[2]] != -1)
754 if (left || p[0] !=
'=' || decode64_table[(
unsigned char)p[1]] != -1)
764 vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
765 return string((
const char*)&vchRet[0], vchRet.size());
770 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
773 strRet.reserve((len+4)/5*8);
776 const unsigned char *pchEnd = pch+len;
784 strRet += pbase32[enc >> 3];
785 left = (enc & 7) << 2;
790 strRet += pbase32[left | (enc >> 6)];
791 strRet += pbase32[(enc >> 1) & 31];
792 left = (enc & 1) << 4;
797 strRet += pbase32[left | (enc >> 4)];
798 left = (enc & 15) << 1;
803 strRet += pbase32[left | (enc >> 7)];
804 strRet += pbase32[(enc >> 2) & 31];
805 left = (enc & 3) << 3;
810 strRet += pbase32[left | (enc >> 5)];
811 strRet += pbase32[enc & 31];
816 static const int nPadding[5] = {0, 6, 4, 3, 1};
819 strRet += pbase32[left];
820 for (
int n=0; n<nPadding[mode]; n++)
829 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
834 static const int decode32_table[256] =
836 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
837 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
838 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
839 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
840 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
841 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
842 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
843 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
844 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
845 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
846 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
847 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
848 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
854 vector<unsigned char> vchRet;
855 vchRet.reserve((strlen(p))*5/8);
862 int dec = decode32_table[(
unsigned char)*p];
863 if (dec == -1)
break;
873 vchRet.push_back((left<<3) | (dec>>2));
879 left = left << 5 | dec;
884 vchRet.push_back((left<<1) | (dec>>4));
890 vchRet.push_back((left<<4) | (dec>>1));
896 left = left << 5 | dec;
901 vchRet.push_back((left<<2) | (dec>>3));
907 vchRet.push_back((left<<5) | dec);
926 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || p[4] !=
'=' || p[5] !=
'=' || decode32_table[(
unsigned char)p[6]] != -1)
931 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || decode32_table[(
unsigned char)p[4]] != -1)
936 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || decode32_table[(
unsigned char)p[3]] != -1)
941 if (left || p[0] !=
'=' || decode32_table[(
unsigned char)p[1]] != -1)
951 vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
952 return string((
const char*)&vchRet[0], vchRet.size());
963 return (*psz ==
'\0');
992 static std::string FormatException(std::exception* pex,
const char* pszThread)
996 GetModuleFileNameA(NULL, pszModule,
sizeof(pszModule));
998 const char* pszModule =
"feathercoin";
1002 "EXCEPTION: %s \n%s \n%s in %s \n",
typeid(*pex).name(), pex->what(), pszModule, pszThread);
1005 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
1010 std::string message = FormatException(pex, pszThread);
1011 printf(
"\n%s", message.c_str());
1016 std::string message = FormatException(pex, pszThread);
1017 printf(
"\n\n************************\n%s\n", message.c_str());
1018 fprintf(stderr,
"\n\n************************\n%s\n", message.c_str());
1025 std::string message = FormatException(pex, pszThread);
1026 printf(
"\n\n************************\n%s\n", message.c_str());
1027 fprintf(stderr,
"\n\n************************\n%s\n", message.c_str());
1033 namespace fs = boost::filesystem;
1040 return GetSpecialFolderPath(CSIDL_APPDATA) /
"Feathercoin";
1043 char* pszHome = getenv(
"HOME");
1044 if (pszHome == NULL || strlen(pszHome) == 0)
1045 pathRet = fs::path(
"/");
1047 pathRet = fs::path(pszHome);
1050 pathRet /=
"Library/Application Support";
1051 fs::create_directory(pathRet);
1052 return pathRet /
"Feathercoin";
1055 return pathRet /
".feathercoin";
1062 namespace fs = boost::filesystem;
1064 static fs::path pathCached[2];
1067 fs::path &path = pathCached[fNetSpecific];
1076 if (
mapArgs.count(
"-datadir")) {
1077 path = fs::system_complete(
mapArgs[
"-datadir"]);
1078 if (!fs::is_directory(path)) {
1085 if (fNetSpecific &&
GetBoolArg(
"-testnet",
false))
1088 fs::create_directories(path);
1096 boost::filesystem::path pathConfigFile(
GetArg(
"-conf",
"feathercoin.conf"));
1097 if (!pathConfigFile.is_complete()) pathConfigFile =
GetDataDir(
false) / pathConfigFile;
1098 return pathConfigFile;
1102 map<
string, vector<string> >& mapMultiSettingsRet)
1105 if (!streamConfig.good())
1111 set<string> setOptions;
1112 setOptions.insert(
"*");
1114 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
1117 string strKey = string(
"-") + it->string_key;
1118 if (mapSettingsRet.count(strKey) == 0)
1120 mapSettingsRet[strKey] = it->value[0];
1122 InterpretNegativeSetting(strKey, mapSettingsRet);
1124 mapMultiSettingsRet[strKey].push_back(it->value[0]);
1130 boost::filesystem::path pathPidFile(
GetArg(
"-pid",
"feathercoind.pid"));
1131 if (!pathPidFile.is_complete()) pathPidFile =
GetDataDir() / pathPidFile;
1138 FILE* file = fopen(path.string().c_str(),
"w");
1141 fprintf(file,
"%d\n", pid);
1147 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
1150 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
1151 MOVEFILE_REPLACE_EXISTING);
1153 int rc = std::rename(src.string().c_str(), dest.string().c_str());
1162 _commit(_fileno(fileout));
1164 #if defined(__linux__) || defined(__NetBSD__)
1165 fdatasync(fileno(fileout));
1166 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
1167 fcntl(fileno(fileout), F_FULLFSYNC, 0);
1169 fsync(fileno(fileout));
1176 int nSavePos = ftell(file);
1178 if (fseek(file, 0, SEEK_END) == 0)
1179 nFilesize = ftell(file);
1180 fseek(file, nSavePos, SEEK_SET);
1186 return _chsize(_fileno(file), length) == 0;
1188 return ftruncate(fileno(file), length) == 0;
1199 struct rlimit limitFD;
1200 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
1201 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
1202 limitFD.rlim_cur = nMinFD;
1203 if (limitFD.rlim_cur > limitFD.rlim_max)
1204 limitFD.rlim_cur = limitFD.rlim_max;
1205 setrlimit(RLIMIT_NOFILE, &limitFD);
1206 getrlimit(RLIMIT_NOFILE, &limitFD);
1208 return limitFD.rlim_cur;
1219 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
1220 LARGE_INTEGER nFileSize;
1222 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
1223 nFileSize.u.HighPart = nEndPos >> 32;
1224 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
1225 SetEndOfFile(hFile);
1226 #elif defined(MAC_OSX)
1229 fst.fst_flags = F_ALLOCATECONTIG;
1230 fst.fst_posmode = F_PEOFPOSMODE;
1232 fst.fst_length = (off_t)offset + length;
1233 fst.fst_bytesalloc = 0;
1234 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
1235 fst.fst_flags = F_ALLOCATEALL;
1236 fcntl(fileno(file), F_PREALLOCATE, &fst);
1238 ftruncate(fileno(file), fst.fst_length);
1239 #elif defined(__linux__)
1241 off_t nEndPos = (off_t)offset + length;
1242 posix_fallocate(fileno(file), 0, nEndPos);
1246 static const char buf[65536] = {};
1247 fseek(file, offset, SEEK_SET);
1248 while (length > 0) {
1249 unsigned int now = 65536;
1252 fwrite(buf, 1, now, file);
1261 boost::filesystem::path pathLog =
GetDataDir() /
"debug.log";
1262 FILE* file = fopen(pathLog.string().c_str(),
"r");
1267 fseek(file, -
sizeof(pch), SEEK_END);
1268 int nBytes = fread(pch, 1,
sizeof(pch), file);
1271 file = fopen(pathLog.string().c_str(),
"w");
1274 fwrite(pch, 1, nBytes, file);
1278 else if(file != NULL)
1296 static int64 nMockTime = 0;
1300 if (nMockTime)
return nMockTime;
1307 nMockTime = nMockTimeIn;
1310 static int64 nTimeOffset = 0;
1327 static set<CNetAddr> setKnown;
1328 if (!setKnown.insert(ip).second)
1339 if (
abs64(nMedian) < 10 * 60)
1341 nTimeOffset = nMedian;
1351 bool fMatch =
false;
1352 BOOST_FOREACH(
int64 nOffset, vSorted)
1353 if (nOffset != 0 &&
abs64(nOffset) < 5 * 60)
1359 string strMessage =
_(
"Warning: Please check that your computer's date and time are correct! If your clock is wrong Feathercoin will not work properly.");
1361 printf(
"*** %s\n", strMessage.c_str());
1367 BOOST_FOREACH(
int64 n, vSorted)
1368 printf(
"%+"PRI64d
" ", n);
1371 printf(
"nTimeOffset = %+"PRI64d
" (%+"PRI64d
" minutes)\n", nTimeOffset, nTimeOffset/60);
1382 insecure_rand_Rz = insecure_rand_Rw = 11;
1386 RAND_bytes((
unsigned char*)&tmp, 4);
1387 }
while(tmp == 0 || tmp == 0x9068ffffU);
1388 insecure_rand_Rz = tmp;
1390 RAND_bytes((
unsigned char*)&tmp, 4);
1391 }
while(tmp == 0 || tmp == 0x464fffffU);
1392 insecure_rand_Rw = tmp;
1398 if (nVersion%100 == 0)
1399 return strprintf(
"%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1401 return strprintf(
"%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1410 std::string
FormatSubVersion(
const std::string& name,
int nClientVersion,
const std::vector<std::string>& comments)
1412 std::ostringstream ss;
1415 if (!comments.empty())
1416 ss <<
"(" << boost::algorithm::join(comments,
"; ") <<
")";
1422 boost::filesystem::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
1424 namespace fs = boost::filesystem;
1428 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1430 return fs::path(pszPath);
1433 printf(
"SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1434 return fs::path(
"");
1439 #if BOOST_FILESYSTEM_VERSION == 3
1440 return boost::filesystem::temp_directory_path();
1443 boost::filesystem::path path;
1447 if (GetTempPathA(
MAX_PATH, pszPath))
1448 path = boost::filesystem::path(pszPath);
1450 path = boost::filesystem::path(
"/tmp");
1452 if (path.empty() || !boost::filesystem::is_directory(path)) {
1453 printf(
"GetTempPath(): failed to find temp path\n");
1454 return boost::filesystem::path(
"");
1462 int nErr = ::system(strCommand.c_str());
1464 printf(
"runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
1469 #if defined(PR_SET_NAME)
1471 ::prctl(PR_SET_NAME, name, 0, 0, 0);
1472 #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
1476 pthread_set_name_np(pthread_self(), name);
1478 #elif defined(MAC_OSX) && defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
1481 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
1482 pthread_setname_np(name);
1495 boost::thread(pfn, parg);
1496 }
catch(boost::thread_resource_error &e) {
1497 printf(
"Error creating thread: %s\n", e.what());
CMedianFilter< int64 > vTimeOffsets(200, 0)
bool error(const char *format,...)
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
void locking_callback(int mode, int i, const char *file, int line)
int64 GetPerformanceCounter()
#define strprintf(format,...)
static LockedPageManager instance
void FileCommit(FILE *fileout)
string real_strprintf(const char *format, int dummy,...)
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
void LogException(std::exception *pex, const char *pszThread)
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
string FormatVersion(int nVersion)
boost::filesystem::path GetTempPath()
boost::filesystem::path GetPidFile()
void seed_insecure_rand(bool fDeterministic)
Seed insecure_rand using the random pool.
string vstrprintf(const char *format, va_list ap)
bool IsHex(const string &str)
unsigned long long uint64
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
void RandAddSeedPerfmon()
void RenameThread(const char *name)
volatile bool fReopenDebugLog
int64 atoi64(const char *psz)
int RaiseFileDescriptorLimit(int nMinFD)
bool NewThread(void(*pfn)(void *), void *parg)
class CInit instance_of_cinit
int std::string int dummy
void SetMockTime(int64 nMockTimeIn)
string EncodeBase64(const unsigned char *pch, size_t len)
bool TruncateFile(FILE *file, unsigned int length)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
#define LEAVE_CRITICAL_SECTION(cs)
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
string SanitizeString(const string &str)
uint64 GetRand(uint64 nMax)
CClientUIInterface uiInterface
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
string EncodeBase32(const unsigned char *pch, size_t len)
Singleton class to keep track of locked (ie, non-swappable) memory pages, for use in std::allocator t...
std::string to_internal(const std::string &)
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
std::string DateTimeStrFormat(const char *pszFormat, int64 nTime)
void ParseParameters(int argc, const char *const argv[])
int OutputDebugStringF(const char *pszFormat,...)
void ParseString(const string &str, char c, vector< string > &v)
#define ENTER_CRITICAL_SECTION(cs)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
int GetFilesize(FILE *file)
void ReadConfigFile(map< string, string > &mapSettingsRet, map< string, vector< string > > &mapMultiSettingsRet)
void AddTimeData(const CNetAddr &ip, int64 nTime)
const std::string CLIENT_BUILD
string FormatMoney(int64 n, bool fPlus)
string FormatFullVersion()
bool WildcardMatch(const char *psz, const char *mask)
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
uint32_t insecure_rand_Rz
MWC RNG of George Marsaglia This is intended to be fast.
boost::signals2::signal< bool(const std::string &message, const std::string &caption, unsigned int style), boost::signals2::last_value< bool > > ThreadSafeMessageBox
Show message box.
AnnotatedMixin< boost::recursive_mutex > CCriticalSection
Wrapped boost mutex: supports recursive locking, but no waiting.
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
uint32_t insecure_rand_Rw
bool ParseMoney(const string &str, int64 &nRet)
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
map< string, vector< string > > mapMultiArgs
vector< unsigned char > ParseHex(const char *psz)
map< string, string > mapArgs
int atoi(const std::string &str)
boost::filesystem::path GetDefaultDataDir()
void runCommand(std::string strCommand)
void PrintException(std::exception *pex, const char *pszThread)
boost::filesystem::path GetConfigFile()