Implemented randomness functions.
This commit is contained in:
parent
0039f4d4b7
commit
6075c31017
@ -15,9 +15,11 @@
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\Global.cpp" />
|
||||
<ClCompile Include="src\UpdateChecker.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\MUtils\Global.h" />
|
||||
<ClInclude Include="include\Mutils\UpdateChecker.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
@ -62,14 +64,17 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
<TargetName>libMUtils-1</TargetName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
<TargetName>libMUtils-1</TargetName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Static|Win32'">
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
<TargetName>libMUtils-1</TargetName>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
|
@ -18,10 +18,16 @@
|
||||
<ClCompile Include="src\UpdateChecker.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Global.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\Mutils\UpdateChecker.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\MUtils\Global.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
35
include/MUtils/Global.h
Normal file
35
include/MUtils/Global.h
Normal file
@ -0,0 +1,35 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// MuldeR's Utilities for Qt
|
||||
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
||||
//
|
||||
// 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 <QString>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace MUtils
|
||||
{
|
||||
//Random
|
||||
void seed_rand(void);
|
||||
QString rand_str(const bool &bLong = false);
|
||||
quint32 next_rand32(void);
|
||||
quint64 next_rand64(void);
|
||||
}
|
@ -26,8 +26,8 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace MUtils {
|
||||
|
||||
namespace MUtils
|
||||
{
|
||||
class UpdateCheckerInfo
|
||||
{
|
||||
friend class UpdateChecker;
|
||||
|
93
src/Global.cpp
Normal file
93
src/Global.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// MuldeR's Utilities for Qt
|
||||
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
||||
//
|
||||
// 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 <MUtils/Global.h>
|
||||
|
||||
//CRT
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <process.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// 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));
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <MUtils/UpdateChecker.h>
|
||||
#include <MUtils/Global.h>
|
||||
|
||||
#include <QStringList>
|
||||
#include <QFile>
|
||||
@ -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!");
|
||||
|
Loading…
Reference in New Issue
Block a user