Now using rand_s() to generate random numbers, which doesn't need a seed. Falling back to qsrand() + qrand() on platforms that don't support rand_s().

This commit is contained in:
LoRd_MuldeR 2012-10-13 12:36:41 +02:00
parent 760b34a0e0
commit 50e9d01cfb
6 changed files with 49 additions and 18 deletions

View File

@ -30,7 +30,7 @@
#define VER_LAMEXP_MINOR_LO 6
#define VER_LAMEXP_TYPE Beta
#define VER_LAMEXP_PATCH 2
#define VER_LAMEXP_BUILD 1141
#define VER_LAMEXP_BUILD 1145
///////////////////////////////////////////////////////////////////////////////
// Tool versions (minimum expected versions!)

View File

@ -135,23 +135,25 @@ AboutDialog::AboutDialog(SettingsModel *settings, QWidget *parent, bool firstSta
//Show about dialog for the first time?
if(!firstStart)
{
lamexp_seed_rand();
acceptButton->hide();
declineButton->hide();
aboutQtButton->show();
closeButton->show();
QPixmap disque(":/images/Disque.png");
QRect screenGeometry = QApplication::desktop()->availableGeometry();
m_disque = new QLabel(this, Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
m_disque->installEventFilter(this);
m_disque->setStyleSheet("background:transparent;");
m_disque->setAttribute(Qt::WA_TranslucentBackground);
m_disque->setGeometry(qrand() % (screenGeometry.width() - disque.width()), qrand() % (screenGeometry.height() - disque.height()), disque.width(), disque.height());
m_disque->setGeometry(static_cast<int>(lamexp_rand() % static_cast<unsigned int>(screenGeometry.width() - disque.width())), static_cast<int>(lamexp_rand() % static_cast<unsigned int>(screenGeometry.height() - disque.height())), disque.width(), disque.height());
m_disque->setPixmap(disque);
m_disque->setWindowOpacity(0.01);
m_disque->show();
m_disqueFlags[0] = (qrand() > (RAND_MAX/2));
m_disqueFlags[1] = (qrand() > (RAND_MAX/2));
m_disqueFlags[0] = (lamexp_rand() > (UINT_MAX/2));
m_disqueFlags[1] = (lamexp_rand() > (UINT_MAX/2));
m_disqueTimer = new QTimer;
connect(m_disqueTimer, SIGNAL(timeout()), this, SLOT(moveDisque()));
m_disqueTimer->setInterval(10);

View File

@ -1171,10 +1171,6 @@ void MainWindow::windowShown(void)
if(reinterpret_cast<int>(res) > 32) break;
}
}
else
{
MoveFileEx(QWCHAR(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath())), NULL, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING);
}
QApplication::quit();
return;
}

View File

@ -39,10 +39,8 @@
#include <QMovie>
#include <QtConcurrentRun>
#include <time.h>
#include <MMSystem.h>
#include <WinInet.h>
#include <process.h>
///////////////////////////////////////////////////////////////////////////////
@ -425,10 +423,10 @@ void UpdateDialog::checkForUpdates(void)
hostList << QString::fromLatin1(known_hosts[i]);
}
qsrand(lamexp_mix(clock(), time(NULL), _getpid()));
lamexp_seed_rand();
while(!hostList.isEmpty())
{
QString currentHost = hostList.takeAt(qrand() % hostList.count());
QString currentHost = hostList.takeAt(lamexp_rand() % hostList.count());
if(connectionScore < MIN_CONNSCORE)
{
m_logFile->append(QStringList() << "" << "Testing host:" << currentHost << "");
@ -486,13 +484,13 @@ void UpdateDialog::checkForUpdates(void)
mirrorList << QString::fromLatin1(update_mirrors_prim[index]);
}
qsrand(lamexp_mix(clock(), time(NULL), _getpid()));
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, qrand() % len);
mirrorList.swap(i % len, lamexp_rand() % len);
}
}

View File

@ -472,7 +472,7 @@ LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionI
/*
* Invalid parameters handler
*/
void lamexp_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t)
void lamexp_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t)
{
if(GetCurrentThreadId() != g_main_thread_id)
{
@ -2210,7 +2210,7 @@ QStringList lamexp_available_codepages(bool noAliases)
* Robert Jenkins' 96 bit Mix Function
* Source: http://www.concentric.net/~Ttwang/tech/inthash.htm
*/
unsigned int lamexp_mix(const unsigned int x, const unsigned int y, const unsigned int z)
static unsigned int lamexp_mix(const unsigned int x, const unsigned int y, const unsigned int z)
{
unsigned int a = x;
unsigned int b = y;
@ -2229,6 +2229,39 @@ unsigned int lamexp_mix(const unsigned int x, const unsigned int y, const unsign
return c;
}
/*
* Seeds the random number generator
* Note: Altough rand_s() doesn't need a seed, this must be called pripr to lamexp_rand(), just to to be sure!
*/
void lamexp_seed_rand(void)
{
qsrand(lamexp_mix(clock(), time(NULL), _getpid()));
}
/*
* Returns a randum number
* Note: This function uses rand_s() if available, but falls back to qrand() otherwise
*/
unsigned int lamexp_rand(void)
{
unsigned int rnd = 0;
if(const lamexp_os_version_t* osVer = lamexp_get_os_version())
{
if(LAMEXP_MIN_OS_VER(osVer, 5, 1))
{
if(rand_s(&rnd) == 0)
{
return rnd;
}
}
}
for(size_t i = 0; i < sizeof(unsigned int); i++)
{
rnd = (rnd << 8) ^ qrand();
}
return rnd;
}
/*
* Entry point checks
*/

View File

@ -25,6 +25,7 @@
#include "Targetver.h"
//inlcude C standard library
#define _CRT_RAND_S
#include <stdio.h>
#include <tchar.h>
@ -155,7 +156,8 @@ bool lamexp_themes_enabled(void);
void lamexp_blink_window(QWidget *poWindow, unsigned int count = 10, unsigned int delay = 150);
const QString lamexp_clean_filename(const QString &str);
const QString lamexp_clean_filepath(const QString &str);
unsigned int lamexp_mix(const unsigned int x, const unsigned int y, const unsigned int z);
void lamexp_seed_rand(void);
unsigned int lamexp_rand(void);
//Debug-only functions
SIZE_T lamexp_dbg_private_bytes(void);