2010-11-06 23:04:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2014-01-01 17:05:52 +01:00
|
|
|
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-11-06 23:04:47 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2010-11-06 23:04:47 +01:00
|
|
|
//
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "Global.h"
|
|
|
|
|
2014-11-24 19:33:12 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/KeccakHash.h>
|
|
|
|
|
|
|
|
//Qt
|
2010-11-06 23:04:47 +01:00
|
|
|
#include <QResource>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2011-06-26 19:21:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2013-10-18 20:49:22 +02:00
|
|
|
#include <stdexcept>
|
2011-06-26 19:21:00 +02:00
|
|
|
|
2013-10-06 19:28:12 +02:00
|
|
|
//Windows includes
|
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2011-06-26 19:21:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// WARNING: Passing file descriptors into Qt does NOT work with dynamically linked CRT!
|
|
|
|
#ifdef QT_NODLL
|
2013-11-02 01:06:15 +01:00
|
|
|
static const bool g_useFileDescrForQFile = true;
|
2011-06-26 19:21:00 +02:00
|
|
|
#else
|
2013-11-02 01:06:15 +01:00
|
|
|
static const bool g_useFileDescrForQFile = false;
|
2011-06-26 19:21:00 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-12 00:32:39 +01:00
|
|
|
static const char *g_blnk = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
|
|
|
|
static const char *g_seed = "c375d83b4388329408dfcbb4d9a065b6e06d28272f25ef299c70b506e26600af79fd2f866ae24602daf38f25c9d4b7e1";
|
|
|
|
static const char *g_salt = "ee9f7bdabc170763d2200a7e3030045aafe380011aefc1730e547e9244c62308aac42a976feeca224ba553de0c4bb883";
|
2011-07-02 16:38:46 +02:00
|
|
|
|
2013-10-20 19:12:55 +02:00
|
|
|
QByteArray LockedFile::fileHash(QFile &file)
|
2011-07-02 16:38:46 +02:00
|
|
|
{
|
2012-11-12 00:32:39 +01:00
|
|
|
QByteArray hash = QByteArray::fromHex(g_blnk);
|
|
|
|
|
2011-07-02 16:38:46 +02:00
|
|
|
if(file.isOpen() && file.reset())
|
|
|
|
{
|
2014-11-24 19:33:12 +01:00
|
|
|
MUtils::KeccakHash keccak;
|
2011-07-02 16:38:46 +02:00
|
|
|
|
2012-11-27 01:02:55 +01:00
|
|
|
const QByteArray data = file.readAll();
|
|
|
|
const QByteArray seed = QByteArray::fromHex(g_seed);
|
|
|
|
const QByteArray salt = QByteArray::fromHex(g_salt);
|
2012-11-12 00:32:39 +01:00
|
|
|
|
2014-11-24 19:33:12 +01:00
|
|
|
if(keccak.init(MUtils::KeccakHash::hb384))
|
2011-07-02 16:38:46 +02:00
|
|
|
{
|
2012-11-27 01:02:55 +01:00
|
|
|
bool ok = true;
|
|
|
|
ok = ok && keccak.addData(seed);
|
|
|
|
ok = ok && keccak.addData(data);
|
|
|
|
ok = ok && keccak.addData(salt);
|
|
|
|
if(ok)
|
2012-11-12 00:32:39 +01:00
|
|
|
{
|
2012-11-27 01:02:55 +01:00
|
|
|
const QByteArray digest = keccak.finalize();
|
|
|
|
if(!digest.isEmpty()) hash = digest.toHex();
|
2012-11-12 00:32:39 +01:00
|
|
|
}
|
2011-07-02 16:38:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
LockedFile::LockedFile(QResource *const resource, const QString &outPath, const QByteArray &expectedHash, const bool bOwnsFile)
|
|
|
|
:
|
|
|
|
m_bOwnsFile(bOwnsFile),
|
|
|
|
m_filePath(QFileInfo(outPath).absoluteFilePath())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2013-11-01 19:32:47 +01:00
|
|
|
m_fileDescriptor = -1;
|
|
|
|
HANDLE fileHandle = NULL;
|
|
|
|
|
2011-06-23 16:50:02 +02:00
|
|
|
//Make sure the resource is valid
|
2013-10-18 20:49:22 +02:00
|
|
|
if(!(resource->isValid() && resource->data()))
|
2011-06-23 16:50:02 +02:00
|
|
|
{
|
2013-10-18 20:49:22 +02:00
|
|
|
THROW_FMT("The resource at %p is invalid!", resource);
|
2011-06-23 16:50:02 +02:00
|
|
|
}
|
|
|
|
|
2013-11-02 01:06:15 +01:00
|
|
|
QFile outFile(m_filePath);
|
2011-06-23 16:50:02 +02:00
|
|
|
|
|
|
|
//Open output file
|
|
|
|
for(int i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
if(outFile.open(QIODevice::WriteOnly)) break;
|
|
|
|
if(!i) qWarning("Failed to open file on first attemp, retrying...");
|
|
|
|
Sleep(100);
|
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
|
|
|
|
//Write data to file
|
2011-06-23 16:50:02 +02:00
|
|
|
if(outFile.isOpen() && outFile.isWritable())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2013-10-18 20:49:22 +02:00
|
|
|
if(outFile.write(reinterpret_cast<const char*>(resource->data()), resource->size()) != resource->size())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2010-11-15 22:07:46 +01:00
|
|
|
QFile::remove(QFileInfo(outFile).canonicalFilePath());
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' could not be written!", MUTILS_UTF8(QFileInfo(outFile).fileName()));
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' could not be created!", MUTILS_UTF8(QFileInfo(outFile).fileName()));
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
//Close file after it has been written
|
|
|
|
outFile.close();
|
|
|
|
|
2011-06-21 14:35:46 +02:00
|
|
|
//Now lock the file!
|
2011-06-21 16:23:42 +02:00
|
|
|
for(int i = 0; i < 64; i++)
|
2011-06-21 14:35:46 +02:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
fileHandle = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(m_filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
|
2013-11-01 19:32:47 +01:00
|
|
|
if((fileHandle != NULL) && (fileHandle != INVALID_HANDLE_VALUE)) break;
|
2011-06-23 16:50:02 +02:00
|
|
|
if(!i) qWarning("Failed to lock file on first attemp, retrying...");
|
2011-06-21 14:35:46 +02:00
|
|
|
Sleep(100);
|
|
|
|
}
|
2011-06-21 16:23:42 +02:00
|
|
|
|
|
|
|
//Locked successfully?
|
2013-11-01 19:32:47 +01:00
|
|
|
if((fileHandle == NULL) || (fileHandle == INVALID_HANDLE_VALUE))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2010-11-15 22:07:46 +01:00
|
|
|
QFile::remove(QFileInfo(outFile).canonicalFilePath());
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' could not be locked!", MUTILS_UTF8(QFileInfo(m_filePath).fileName()));
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
//Get file descriptor
|
|
|
|
m_fileDescriptor = _open_osfhandle(reinterpret_cast<intptr_t>(fileHandle), _O_RDONLY | _O_BINARY);
|
|
|
|
if(m_fileDescriptor < 0)
|
2011-06-26 19:21:00 +02:00
|
|
|
{
|
2013-11-01 19:32:47 +01:00
|
|
|
THROW_FMT("Failed to obtain C Runtime file descriptor!");
|
2011-06-26 19:21:00 +02:00
|
|
|
}
|
2013-11-01 19:32:47 +01:00
|
|
|
|
|
|
|
QFile checkFile;
|
|
|
|
|
|
|
|
//Now re-open the file for reading
|
2013-11-02 01:06:15 +01:00
|
|
|
if(g_useFileDescrForQFile)
|
2011-06-26 19:21:00 +02:00
|
|
|
{
|
2013-11-02 01:06:15 +01:00
|
|
|
checkFile.open(m_fileDescriptor, QIODevice::ReadOnly);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
checkFile.setFileName(m_filePath);
|
|
|
|
for(int i = 0; i < 64; i++)
|
2011-06-26 19:21:00 +02:00
|
|
|
{
|
2013-11-01 19:32:47 +01:00
|
|
|
if(checkFile.open(QIODevice::ReadOnly)) break;
|
2013-11-02 01:06:15 +01:00
|
|
|
if(!i) qWarning("Failed to re-open file on first attemp, retrying...");
|
|
|
|
Sleep(100);
|
2011-06-26 19:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
//Opened successfully
|
|
|
|
if(!checkFile.isOpen())
|
2011-06-21 14:35:46 +02:00
|
|
|
{
|
2011-06-26 19:21:00 +02:00
|
|
|
QFile::remove(m_filePath);
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' could not be read!", MUTILS_UTF8(QFileInfo(m_filePath).fileName()));
|
2011-06-21 14:35:46 +02:00
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
//Verify file contents
|
|
|
|
const QByteArray hash = fileHash(checkFile);
|
|
|
|
checkFile.close();
|
|
|
|
|
2011-02-23 02:19:50 +01:00
|
|
|
//Compare hashes
|
2011-07-02 16:38:46 +02:00
|
|
|
if(hash.isNull() || _stricmp(hash.constData(), expectedHash.constData()))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2011-07-06 23:30:43 +02:00
|
|
|
qWarning("\nFile checksum error:\n A = %s\n B = %s\n", expectedHash.constData(), hash.constData());
|
2013-11-01 19:32:47 +01:00
|
|
|
LAMEXP_CLOSE(fileHandle);
|
2011-06-26 19:21:00 +02:00
|
|
|
QFile::remove(m_filePath);
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' is corruputed, take care!", MUTILS_UTF8(QFileInfo(m_filePath).fileName()));
|
2010-11-11 22:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:32:47 +01:00
|
|
|
LockedFile::LockedFile(const QString &filePath, const bool bOwnsFile)
|
|
|
|
:
|
|
|
|
m_bOwnsFile(bOwnsFile),
|
2013-11-02 01:06:15 +01:00
|
|
|
m_filePath(QFileInfo(filePath).canonicalFilePath())
|
2010-11-11 22:58:02 +01:00
|
|
|
{
|
2013-11-01 19:32:47 +01:00
|
|
|
m_fileDescriptor = -1;
|
|
|
|
HANDLE fileHandle = NULL;
|
|
|
|
|
2013-11-02 01:06:15 +01:00
|
|
|
QFileInfo existingFileInfo(filePath);
|
|
|
|
existingFileInfo.setCaching(false);
|
2010-11-11 22:58:02 +01:00
|
|
|
|
2011-02-23 02:19:50 +01:00
|
|
|
//Make sure the file exists, before we try to lock it
|
2014-08-16 14:45:18 +02:00
|
|
|
if((!existingFileInfo.exists()) || (!existingFileInfo.isFile()) || m_filePath.isEmpty())
|
2010-11-11 22:58:02 +01:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' does not exist!", MUTILS_UTF8(filePath));
|
2010-11-11 22:58:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Now lock the file
|
2011-06-21 16:23:42 +02:00
|
|
|
for(int i = 0; i < 64; i++)
|
2011-02-23 02:19:50 +01:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
fileHandle = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(m_filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
|
2013-11-01 19:32:47 +01:00
|
|
|
if((fileHandle != NULL) && (fileHandle != INVALID_HANDLE_VALUE)) break;
|
2011-06-23 16:50:02 +02:00
|
|
|
if(!i) qWarning("Failed to lock file on first attemp, retrying...");
|
2011-02-23 02:19:50 +01:00
|
|
|
Sleep(100);
|
|
|
|
}
|
2010-11-11 22:58:02 +01:00
|
|
|
|
2011-02-23 02:19:50 +01:00
|
|
|
//Locked successfully?
|
2013-11-01 19:32:47 +01:00
|
|
|
if((fileHandle == NULL) || (fileHandle == INVALID_HANDLE_VALUE))
|
2010-11-11 22:58:02 +01:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
THROW_FMT("File '%s' could not be locked!", MUTILS_UTF8(QFileInfo(m_filePath).fileName()));
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
2013-11-01 19:32:47 +01:00
|
|
|
|
|
|
|
//Get file descriptor
|
|
|
|
m_fileDescriptor = _open_osfhandle(reinterpret_cast<intptr_t>(fileHandle), _O_RDONLY | _O_BINARY);
|
|
|
|
if(m_fileDescriptor < 0)
|
|
|
|
{
|
|
|
|
THROW_FMT("Failed to obtain C Runtime file descriptor!");
|
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LockedFile::~LockedFile(void)
|
|
|
|
{
|
2013-11-01 19:32:47 +01:00
|
|
|
if(m_fileDescriptor >= 0)
|
|
|
|
{
|
|
|
|
_close(m_fileDescriptor);
|
|
|
|
m_fileDescriptor = -1;
|
|
|
|
}
|
|
|
|
if(m_bOwnsFile)
|
|
|
|
{
|
|
|
|
if(QFileInfo(m_filePath).exists())
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
if(QFile::remove(m_filePath)) break;
|
|
|
|
lamexp_sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &LockedFile::filePath()
|
|
|
|
{
|
|
|
|
return m_filePath;
|
2012-11-27 01:02:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LockedFile::selfTest()
|
|
|
|
{
|
2014-11-24 19:33:12 +01:00
|
|
|
if(!MUtils::KeccakHash::selfTest())
|
2012-11-27 01:02:55 +01:00
|
|
|
{
|
2013-10-18 20:49:22 +02:00
|
|
|
THROW("QKeccakHash self-test has failed!");
|
2012-11-27 01:02:55 +01:00
|
|
|
}
|
|
|
|
}
|