1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

Perform check for magic header only after copying to internal dir

This commit is contained in:
Ivan Savenko
2025-02-18 14:33:23 +00:00
parent aef04236b9
commit 5eaa12d902

View File

@@ -318,9 +318,8 @@ QString FirstLaunchView::getHeroesInstallDir()
void FirstLaunchView::extractGogData()
{
#ifdef ENABLE_INNOEXTRACT
auto fileSelection = [this](QByteArray magic, QString filter, QString startPath = {}) {
auto fileSelection = [this](QString filter, QString startPath = {}) {
QString titleSel = tr("Select %1 file...", "param is file extension").arg(filter);
QString titleErr = tr("You have to select %1 file!", "param is file extension").arg(filter);
#if defined(VCMI_MOBILE)
filter = tr("GOG file (*.*)");
QMessageBox::information(this, tr("File selection"), titleSel);
@@ -328,27 +327,35 @@ void FirstLaunchView::extractGogData()
QString file = QFileDialog::getOpenFileName(this, titleSel, startPath.isEmpty() ? QDir::homePath() : startPath, filter);
if(file.isEmpty())
return QString{};
return file;
};
QFile tmpFile(file);
auto checkMagic = [this](QString filename, QString filter, QByteArray magic)
{
QString titleErr = tr("You have to select %1 file!", "param is file extension").arg(filter);
QFile tmpFile(filename);
if(!tmpFile.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, tr("File cannot be opened"), tmpFile.errorString());
return QString{};
return false;
}
QByteArray magicFile = tmpFile.read(magic.length());
if(!magicFile.startsWith(magic))
{
QMessageBox::critical(this, tr("Invalid file selected"), titleErr);
return QString{};
return false;
}
return file;
return true;
};
QString fileBin = fileSelection(QByteArray{"idska32"}, tr("GOG data") + " (*.bin)");
QString filterBin = tr("GOG data") + " (*.bin)";
QString filterExe = tr("GOG installer") + " (*.exe)";
QString fileBin = fileSelection(filterBin);
if(fileBin.isEmpty())
return;
QString fileExe = fileSelection(QByteArray{"MZ"}, tr("GOG installer") + " (*.exe)", QFileInfo(fileBin).absolutePath());
QString fileExe = fileSelection(filterExe, QFileInfo(fileBin).absolutePath());
if(fileExe.isEmpty())
return;
@@ -356,7 +363,7 @@ void FirstLaunchView::extractGogData()
ui->pushButtonGogInstall->setVisible(false);
setEnabled(false);
QTimer::singleShot(100, this, [this, fileExe, fileBin](){ // background to make sure FileDialog is closed...
QTimer::singleShot(100, this, [this, fileExe, fileBin, checkMagic, filterBin, filterExe](){ // background to make sure FileDialog is closed...
QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
if(tempDir.cd("tmp"))
{
@@ -373,6 +380,10 @@ void FirstLaunchView::extractGogData()
Helper::performNativeCopy(fileExe, tmpFileExe);
Helper::performNativeCopy(fileBin, tmpFileBin);
if (!checkMagic(tmpFileBin, filterBin, QByteArray{"idska32"}) ||
!checkMagic(tmpFileExe, filterExe, QByteArray{"MZ"}))
return;
logGlobal->info("Installing exe '%s' ('%s')", tmpFileExe.toStdString(), fileExe.toStdString());
logGlobal->info("Installing bin '%s' ('%s')", tmpFileBin.toStdString(), fileBin.toStdString());