From 6075c310175003238a127337d84661b658f123a3 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 21 Nov 2014 17:54:46 +0100 Subject: [PATCH] Implemented randomness functions. --- MUtilities_VS2013.vcxproj | 5 ++ MUtilities_VS2013.vcxproj.filters | 6 ++ include/MUtils/Global.h | 35 ++++++++++++ include/MUtils/UpdateChecker.h | 4 +- src/Global.cpp | 93 +++++++++++++++++++++++++++++++ src/UpdateChecker.cpp | 31 +++++------ 6 files changed, 156 insertions(+), 18 deletions(-) create mode 100644 include/MUtils/Global.h create mode 100644 src/Global.cpp diff --git a/MUtilities_VS2013.vcxproj b/MUtilities_VS2013.vcxproj index bac520c..f4d0451 100644 --- a/MUtilities_VS2013.vcxproj +++ b/MUtilities_VS2013.vcxproj @@ -15,9 +15,11 @@ + + @@ -62,14 +64,17 @@ $(SolutionDir)\bin\$(Platform)\$(Configuration)\ $(SolutionDir)\obj\$(Platform)\$(Configuration)\ + libMUtils-1 $(SolutionDir)\bin\$(Platform)\$(Configuration)\ $(SolutionDir)\obj\$(Platform)\$(Configuration)\ + libMUtils-1 $(SolutionDir)\bin\$(Platform)\$(Configuration)\ $(SolutionDir)\obj\$(Platform)\$(Configuration)\ + libMUtils-1 diff --git a/MUtilities_VS2013.vcxproj.filters b/MUtilities_VS2013.vcxproj.filters index cec3182..09e3505 100644 --- a/MUtilities_VS2013.vcxproj.filters +++ b/MUtilities_VS2013.vcxproj.filters @@ -18,10 +18,16 @@ Source Files + + Source Files + Header Files + + Header Files + \ No newline at end of file diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h new file mode 100644 index 0000000..a7b0dc8 --- /dev/null +++ b/include/MUtils/Global.h @@ -0,0 +1,35 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include + +/////////////////////////////////////////////////////////////////////////////// + +namespace MUtils +{ + //Random + void seed_rand(void); + QString rand_str(const bool &bLong = false); + quint32 next_rand32(void); + quint64 next_rand64(void); +} diff --git a/include/MUtils/UpdateChecker.h b/include/MUtils/UpdateChecker.h index 376e61b..3916846 100644 --- a/include/MUtils/UpdateChecker.h +++ b/include/MUtils/UpdateChecker.h @@ -26,8 +26,8 @@ /////////////////////////////////////////////////////////////////////////////// -namespace MUtils { - +namespace MUtils +{ class UpdateCheckerInfo { friend class UpdateChecker; diff --git a/src/Global.cpp b/src/Global.cpp new file mode 100644 index 0000000..eba0258 --- /dev/null +++ b/src/Global.cpp @@ -0,0 +1,93 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#if _MSC_VER +#define _CRT_RAND_S 1 +#endif + +#include + +//CRT +#include +#include +#include + +/////////////////////////////////////////////////////////////////////////////// +// Random Support +/////////////////////////////////////////////////////////////////////////////// + +//Robert Jenkins' 96 bit Mix Function +static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z) +{ + unsigned int a = x; + unsigned int b = y; + unsigned int c = z; + + a=a-b; a=a-c; a=a^(c >> 13); + b=b-c; b=b-a; b=b^(a << 8 ); + c=c-a; c=c-b; c=c^(b >> 13); + a=a-b; a=a-c; a=a^(c >> 12); + b=b-c; b=b-a; b=b^(a << 16); + c=c-a; c=c-b; c=c^(b >> 5 ); + a=a-b; a=a-c; a=a^(c >> 3 ); + b=b-c; b=b-a; b=b^(a << 10); + c=c-a; c=c-b; c=c^(b >> 15); + + return a ^ b ^ c; +} + +void MUtils::seed_rand(void) +{ + qsrand(mix_function(clock(), time(NULL), _getpid())); +} + +quint32 MUtils::next_rand32(void) +{ + quint32 rnd = 0xDEADBEEF; + +#ifdef _CRT_RAND_S + if(rand_s(&rnd) == 0) + { + return rnd; + } +#endif //_CRT_RAND_S + + for(size_t i = 0; i < sizeof(quint32); i++) + { + rnd = (rnd << 8) ^ qrand(); + } + + return rnd; +} + +quint64 MUtils::next_rand64(void) +{ + return (quint64(next_rand32()) << 32) | quint64(next_rand32()); +} + +QString MUtils::rand_str(const bool &bLong) +{ + if(!bLong) + { + return QString::number(next_rand64(), 16).rightJustified(16, QLatin1Char('0')); + } + return QString("%1%2").arg(rand_str(false), rand_str(false)); +} diff --git a/src/UpdateChecker.cpp b/src/UpdateChecker.cpp index 9b9de18..5725207 100644 --- a/src/UpdateChecker.cpp +++ b/src/UpdateChecker.cpp @@ -20,6 +20,7 @@ ////////////////////////////////////////////////////////////////////////////////// #include +#include #include #include @@ -253,6 +254,7 @@ void UpdateChecker::checkForUpdates(void) m_success = false; m_updateInfo->resetInfo(); + seed_rand(); setProgress(0); // ----- Test Internet Connection ----- // @@ -282,18 +284,16 @@ void UpdateChecker::checkForUpdates(void) hostList << QString::fromLatin1(known_hosts[i]); } - lamexp_seed_rand(); - while(!(hostList.isEmpty() || (connectionScore >= MIN_CONNSCORE) || (maxConnectTries < 1))) { - switch(tryContactHost(hostList.takeAt(lamexp_rand() % hostList.count()))) + switch(tryContactHost(hostList.takeAt(next_rand32() % hostList.count()))) { case 01: connectionScore += 1; break; case 02: connectionScore += 2; break; default: maxConnectTries -= 1; break; } setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1)); - lamexp_sleep(64); + msleep(64); } if(connectionScore < MIN_CONNSCORE) @@ -315,13 +315,12 @@ void UpdateChecker::checkForUpdates(void) mirrorList << QString::fromLatin1(update_mirrors_prim[index]); } - lamexp_seed_rand(); if(const int len = mirrorList.count()) { const int rounds = len * 1097; for(int i = 0; i < rounds; i++) { - mirrorList.swap(i % len, lamexp_rand() % len); + mirrorList.swap(i % len, next_rand32() % len); } } @@ -345,7 +344,7 @@ void UpdateChecker::checkForUpdates(void) } else { - lamexp_sleep(64); + msleep(64); } } @@ -389,7 +388,7 @@ void UpdateChecker::testKnownHosts(void) QString currentHost = hostList.takeFirst(); qDebug("Testing: %s", currentHost.toLatin1().constData()); log("", "Testing:", currentHost, ""); - QString outFile = QString("%1/%2.htm").arg(lamexp_temp_folder2(), lamexp_rand_str()); + QString outFile = QString("%1/%2.htm").arg(temp_folder(), rand_str()); bool httpOk = false; if(!getFile(currentHost, outFile, 0, &httpOk)) { @@ -440,7 +439,7 @@ void UpdateChecker::log(const QString &str1, const QString &str2, const QString int UpdateChecker::tryContactHost(const QString &url) { int result = -1; bool httpOkay = false; - const QString outFile = QString("%1/%2.htm").arg(lamexp_temp_folder2(), lamexp_rand_str()); + const QString outFile = QString("%1/%2.htm").arg(temp_folder(), rand_str()); log("", "Testing host:", url); if(getFile(url, outFile, 0, &httpOkay)) @@ -471,9 +470,9 @@ bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString bool success = false; log("", "Trying mirror:", url); - const QString randPart = lamexp_rand_str(); - const QString outFileVers = QString("%1/%2.ver").arg(lamexp_temp_folder2(), randPart); - const QString outFileSign = QString("%1/%2.sig").arg(lamexp_temp_folder2(), randPart); + const QString randPart = rand_str(); + const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart); + const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart); if(getUpdateInfo(url, outFileVers, outFileSign)) { @@ -532,7 +531,7 @@ bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned } QProcess process; - lamexp_init_process(process, output.absolutePath()); + init_process(process, output.absolutePath()); QStringList args; args << "-T" << "15" << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir); @@ -605,7 +604,7 @@ bool UpdateChecker::checkSignature(const QString &file, const QString &signature } QProcess process; - lamexp_init_process(process, QFileInfo(file).absolutePath()); + init_process(process, QFileInfo(file).absolutePath()); QEventLoop loop; connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); @@ -708,13 +707,13 @@ bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *upd log("WARNING: Version info timestamp is missing!"); return false; } - else if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < lamexp_current_date_safe()) + else if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < current_date_safe()) { updateInfo->resetInfo(); log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS).toString(Qt::ISODate))); return false; } - else if(lamexp_current_date_safe() < updateInfoDate) + else if(current_date_safe() < updateInfoDate) { log("Version info is from the future, take care!"); qWarning("Version info is from the future, take care!");