From cb042bbf3cec16e9f514a6b9de52c45d7c532695 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 12 Jan 2013 01:53:43 +0100 Subject: [PATCH] Added new function to detect the current date. Slightly more robust against manipulations. --- LameXP_VS2012.vcxproj | 4 +-- src/Config.h | 2 +- src/Dialog_About.cpp | 12 ++++--- src/Dialog_MainWindow.cpp | 6 ++-- src/Dialog_Update.cpp | 4 +-- src/Global.cpp | 70 +++++++++++++++++++++++++++++++++++++-- src/Global.h | 1 + src/Main.cpp | 7 ++-- 8 files changed, 87 insertions(+), 19 deletions(-) diff --git a/LameXP_VS2012.vcxproj b/LameXP_VS2012.vcxproj index 6b6c7b24..36ac49d0 100644 --- a/LameXP_VS2012.vcxproj +++ b/LameXP_VS2012.vcxproj @@ -149,7 +149,7 @@ copy /Y "$(SolutionDir)\etc\Prerequisites\VisualLeakDetector\bin\Win32\*.manifes "/MANIFESTDEPENDENCY:type=%27win32%27 name=%27Microsoft.Windows.Common-Controls%27 version=%276.0.0.0%27 publicKeyToken=%276595b64144ccf1df%27 language=%27*%27 processorArchitecture=%27*%27" %(AdditionalOptions) - QtCore4.lib;QtGui4.lib;Winmm.lib;Shlwapi.lib;Wininet.lib;PowrProf.lib;QKeccakHash_VS2012.lib;%(AdditionalDependencies) + QtCore4.lib;QtGui4.lib;Winmm.lib;Shlwapi.lib;Wininet.lib;PowrProf.lib;psapi.lib;QKeccakHash_VS2012.lib;%(AdditionalDependencies) LinkVerboseLib @@ -222,7 +222,7 @@ del "$(TargetDir)imageformats\q???d4.dll" "/MANIFESTDEPENDENCY:type=%27win32%27 name=%27Microsoft.Windows.Common-Controls%27 version=%276.0.0.0%27 publicKeyToken=%276595b64144ccf1df%27 language=%27*%27 processorArchitecture=%27*%27" %(AdditionalOptions) - QtCore.lib;QtGui.lib;QtSvg.lib;qtmain.lib;qsvg.lib;qico.lib;qtga.lib;Winmm.lib;imm32.lib;ws2_32.lib;Shlwapi.lib;Wininet.lib;PowrProf.lib;QKeccakHash_VS2012.lib;EncodePointer.lib;%(AdditionalDependencies) + QtCore.lib;QtGui.lib;QtSvg.lib;qtmain.lib;qsvg.lib;qico.lib;qtga.lib;Winmm.lib;imm32.lib;ws2_32.lib;Shlwapi.lib;Wininet.lib;PowrProf.lib;psapi.lib;QKeccakHash_VS2012.lib;EncodePointer.lib;%(AdditionalDependencies) LinkVerboseLib $(SolutionDir)\etc\Prerequisites\qt4_static\lib;$(SolutionDir)\etc\Prerequisites\qt4_static\plugins\imageformats;$(SolutionDir)\etc\Prerequisites\EncodePointer\lib;$(SolutionDir)\etc\Prerequisites\VisualLeakDetector\lib\Win32;$(SolutionDir)\etc\Prerequisites\keccak\lib\$(Configuration);%(AdditionalLibraryDirectories) %(IgnoreSpecificDefaultLibraries) diff --git a/src/Config.h b/src/Config.h index 67c87771..02453a03 100644 --- a/src/Config.h +++ b/src/Config.h @@ -30,7 +30,7 @@ #define VER_LAMEXP_MINOR_LO 7 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 7 -#define VER_LAMEXP_BUILD 1221 +#define VER_LAMEXP_BUILD 1223 /////////////////////////////////////////////////////////////////////////////// // Tool versions (minimum expected versions!) diff --git a/src/Dialog_About.cpp b/src/Dialog_About.cpp index ed26a412..05c16e3c 100644 --- a/src/Dialog_About.cpp +++ b/src/Dialog_About.cpp @@ -473,7 +473,7 @@ void AboutDialog::initInformationTab(void) const QString copyrightStr = QString().sprintf ( "Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>. Some rights reserved.", - qMax(lamexp_version_date().year(), QDate::currentDate().year()) + qMax(lamexp_version_date().year(), lamexp_current_date_safe().year()) ); QString aboutText; @@ -484,25 +484,27 @@ void AboutDialog::initInformationTab(void) aboutText += QString("%1
").arg(NOBR(tr("Please visit %1 for news and updates!").arg(LINK(lamexp_website_url())))); #if QT_VERSION < QT_VERSION_CHECK(5,0,0) + const QDate currentDate = lamexp_current_date_safe(); if(LAMEXP_DEBUG) { - int daysLeft = qMax(QDate::currentDate().daysTo(lamexp_version_expires()), 0); + int daysLeft = qMax(currentDate.daysTo(lamexp_version_expires()), 0); aboutText += QString("
%1").arg(NOBR(QString("!!! --- DEBUG BUILD --- Expires at: %1 · Days left: %2 --- DEBUG BUILD --- !!!").arg(lamexp_version_expires().toString(Qt::ISODate), QString::number(daysLeft)))); } else if(lamexp_version_demo()) { - int daysLeft = qMax(QDate::currentDate().daysTo(lamexp_version_expires()), 0); + int daysLeft = qMax(currentDate.daysTo(lamexp_version_expires()), 0); aboutText += QString("
%1").arg(NOBR(tr("Note: This demo (pre-release) version of LameXP will expire at %1. Still %2 days left.").arg(lamexp_version_expires().toString(Qt::ISODate), QString::number(daysLeft)))); } #else + const QDate currentDate = lamexp_current_date_safe(); if(LAMEXP_DEBUG) { - int daysLeft = qMax(QDate::currentDate().daysTo(lamexp_version_expires()), 0i64); + int daysLeft = qMax(currentDate.daysTo(lamexp_version_expires()), 0i64); aboutText += QString("
%1").arg(NOBR(QString("!!! --- DEBUG BUILD --- Expires at: %1 · Days left: %2 --- DEBUG BUILD --- !!!").arg(lamexp_version_expires().toString(Qt::ISODate), QString::number(daysLeft)))); } else if(lamexp_version_demo()) { - int daysLeft = qMax(QDate::currentDate().daysTo(lamexp_version_expires()), 0i64); + int daysLeft = qMax(currentDate.daysTo(lamexp_version_expires()), 0i64); aboutText += QString("
%1").arg(NOBR(tr("Note: This demo (pre-release) version of LameXP will expire at %1. Still %2 days left.").arg(lamexp_version_expires().toString(Qt::ISODate), QString::number(daysLeft)))); } #endif diff --git a/src/Dialog_MainWindow.cpp b/src/Dialog_MainWindow.cpp index 9351706c..bd01c5e8 100644 --- a/src/Dialog_MainWindow.cpp +++ b/src/Dialog_MainWindow.cpp @@ -1273,7 +1273,7 @@ void MainWindow::windowShown(void) //Check for expiration if(lamexp_version_demo()) { - if(QDate::currentDate() >= lamexp_version_expires()) + if(lamexp_current_date_safe() >= lamexp_version_expires()) { qWarning("Binary has expired !!!"); PlaySound(MAKEINTRESOURCE(IDR_WAVE_WHAMMY), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC); @@ -1300,7 +1300,7 @@ void MainWindow::windowShown(void) } //Update reminder - if(QDate::currentDate() >= lamexp_version_date().addYears(1)) + if(lamexp_current_date_safe() >= lamexp_version_date().addYears(1)) { qWarning("Binary is more than a year old, time to update!"); int ret = QMessageBox::warning(this, tr("Urgent Update"), NOBR(tr("Your version of LameXP is more than a year old. Time for an update!")), tr("Check for Updates"), tr("Exit Program"), tr("Ignore")); @@ -1326,7 +1326,7 @@ void MainWindow::windowShown(void) else if(m_settings->autoUpdateEnabled()) { QDate lastUpdateCheck = QDate::fromString(m_settings->autoUpdateLastCheck(), Qt::ISODate); - if(!firstRun && (!lastUpdateCheck.isValid() || QDate::currentDate() >= lastUpdateCheck.addDays(14))) + if(!firstRun && (!lastUpdateCheck.isValid() || lamexp_current_date_safe() >= lastUpdateCheck.addDays(14))) { if(QMessageBox::information(this, tr("Update Reminder"), NOBR(lastUpdateCheck.isValid() ? tr("Your last update check was more than 14 days ago. Check for updates now?") : tr("Your did not check for LameXP updates yet. Check for updates now?")), tr("Check for Updates"), tr("Postpone")) == 0) { diff --git a/src/Dialog_Update.cpp b/src/Dialog_Update.cpp index 3c940001..8465a524 100644 --- a/src/Dialog_Update.cpp +++ b/src/Dialog_Update.cpp @@ -840,13 +840,13 @@ bool UpdateDialog::parseVersionInfo(const QString &file, UpdateInfo *updateInfo) m_logFile->append("WARNING: Version info timestamp is missing!"); return false; } - else if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < QDate::currentDate()) + else if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < lamexp_current_date_safe()) { updateInfo->resetInfo(); m_logFile->append(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS).toString(Qt::ISODate))); return false; } - else if(QDate::currentDate() < updateInfoDate) + else if(lamexp_current_date_safe() < updateInfoDate) { m_logFile->append("Version info is from the future, take care!"); qWarning("Version info is from the future, take care!"); diff --git a/src/Global.cpp b/src/Global.cpp index d3a897f0..ac06a52a 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -71,10 +71,8 @@ #include #include -//Debug only includes -#if LAMEXP_DEBUG +//Process API #include -#endif //Initialize static Qt plugins #ifdef QT_NODLL @@ -2251,6 +2249,69 @@ unsigned int lamexp_rand(void) return rnd; } +/* + * Determines the current date, resistant against certain manipulations + */ +QDate lamexp_current_date_safe(void) +{ + const DWORD MAX_PROC = 1024; + DWORD *processes = new DWORD[MAX_PROC]; + DWORD bytesReturned = 0; + + if(!EnumProcesses(processes, sizeof(DWORD) * MAX_PROC, &bytesReturned)) + { + LAMEXP_DELETE_ARRAY(processes); + return QDate::currentDate(); + } + + const DWORD procCount = bytesReturned / sizeof(DWORD); + ULARGE_INTEGER lastStartTime; + memset(&lastStartTime, 0, sizeof(ULARGE_INTEGER)); + + for(DWORD i = 0; i < procCount; i++) + { + HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processes[i]); + if(hProc) + { + FILETIME processTime[4]; + if(GetProcessTimes(hProc, &processTime[0], &processTime[1], &processTime[2], &processTime[3])) + { + ULARGE_INTEGER timeCreation; + timeCreation.LowPart = processTime[0].dwLowDateTime; + timeCreation.HighPart = processTime[0].dwHighDateTime; + if(timeCreation.QuadPart > lastStartTime.QuadPart) + { + lastStartTime.QuadPart = timeCreation.QuadPart; + } + } + CloseHandle(hProc); + } + } + + LAMEXP_DELETE_ARRAY(processes); + + FILETIME lastStartTime_fileTime; + lastStartTime_fileTime.dwHighDateTime = lastStartTime.HighPart; + lastStartTime_fileTime.dwLowDateTime = lastStartTime.LowPart; + + FILETIME lastStartTime_localTime; + if(!FileTimeToLocalFileTime(&lastStartTime_fileTime, &lastStartTime_localTime)) + { + memcpy(&lastStartTime_localTime, &lastStartTime_fileTime, sizeof(FILETIME)); + } + + SYSTEMTIME lastStartTime_system; + if(!FileTimeToSystemTime(&lastStartTime_localTime, &lastStartTime_system)) + { + memset(&lastStartTime_system, 0, sizeof(SYSTEMTIME)); + lastStartTime_system.wYear = 1970; lastStartTime_system.wMonth = lastStartTime_system.wDay = 1; + } + + const QDate currentDate = QDate::currentDate(); + const QDate processDate = QDate(lastStartTime_system.wYear, lastStartTime_system.wMonth, lastStartTime_system.wDay); + return (currentDate >= processDate) ? currentDate : processDate; +} + /* * Entry point checks */ @@ -2329,6 +2390,9 @@ void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessag TerminateProcess(GetCurrentProcess(), -1); } } + + Sleep(30000); + TerminateProcess(GetCurrentProcess(), -1); } /* diff --git a/src/Global.h b/src/Global.h index 7c4d626d..dda620ab 100644 --- a/src/Global.h +++ b/src/Global.h @@ -158,6 +158,7 @@ const QString lamexp_clean_filename(const QString &str); const QString lamexp_clean_filepath(const QString &str); void lamexp_seed_rand(void); unsigned int lamexp_rand(void); +QDate lamexp_current_date_safe(void); void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL); //Debug-only functions diff --git a/src/Main.cpp b/src/Main.cpp index dc9e65ea..8200a9a5 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -57,7 +57,7 @@ static int lamexp_main(int argc, char* argv[]) //Print version info qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build()); - qDebug("Copyright (c) 2004-%04d LoRd_MuldeR . Some rights reserved.", qMax(lamexp_version_date().year(),QDate::currentDate().year())); + qDebug("Copyright (c) 2004-%04d LoRd_MuldeR . Some rights reserved.", qMax(lamexp_version_date().year(), lamexp_current_date_safe().year())); qDebug("Built on %s at %s with %s for Win-%s.\n", lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_time(), lamexp_version_compiler(), lamexp_version_arch()); //print license info @@ -98,9 +98,10 @@ static int lamexp_main(int argc, char* argv[]) //Check for expiration if(lamexp_version_demo()) { - if(QDate::currentDate().addDays(1) < lamexp_version_date()) + const QDate currentDate = lamexp_current_date_safe(); + if(currentDate.addDays(1) < lamexp_version_date()) { - qFatal("System's date (%s) is before LameXP build date (%s). Huh?", QDate::currentDate().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData()); + qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData()); } qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData()); }