1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "LockedFile.h"
29 #include <QCryptographicHash>
35 ///////////////////////////////////////////////////////////////////////////////
39 char error_msg[512]; \
40 strcpy_s(error_msg, 512, STR); \
44 // WARNING: Passing file descriptors into Qt does NOT work with dynamically linked CRT!
46 static const bool g_useFileDescr
= 1;
48 static const bool g_useFileDescr
= 0;
51 ///////////////////////////////////////////////////////////////////////////////
53 static const char *g_seed
= "b14bf0ed8d5a62311b366907a6450ea2d52ad8bbc7d520b70400f1dd8ddfa9339011a473";
54 static const char *g_salt
= "80e0cce7af513880065488568071342e5889d6c5ee96190c303b7260e5c8356c350fbe44";
56 static QByteArray
fileHash(QFile
&file
)
58 QByteArray hash
= QByteArray::fromHex(g_seed
);
59 QByteArray salt
= QByteArray::fromHex(g_salt
);
61 if(file
.isOpen() && file
.reset())
63 QByteArray data
= file
.readAll();
64 QCryptographicHash
sha(QCryptographicHash::Sha1
);
65 QCryptographicHash
md5(QCryptographicHash::Md5
);
67 for(int r
= 0; r
< 8; r
++)
69 sha
.reset(); md5
.reset();
70 sha
.addData(hash
); md5
.addData(hash
);
71 sha
.addData(data
); md5
.addData(data
);
72 sha
.addData(salt
); md5
.addData(salt
);
73 hash
= sha
.result() + md5
.result();
80 ///////////////////////////////////////////////////////////////////////////////
82 LockedFile::LockedFile(const QString
&resourcePath
, const QString
&outPath
, const QByteArray
&expectedHash
)
85 QResource
resource(resourcePath
);
87 //Make sure the resource is valid
88 if(!resource
.isValid())
90 THROW(QString("Resource '%1' is invalid!").arg(QFileInfo(resourcePath
).absoluteFilePath().replace(QRegExp("^:/"), QString())).toUtf8().constData());
93 QFile
outFile(outPath
);
94 m_filePath
= QFileInfo(outFile
).absoluteFilePath();
97 for(int i
= 0; i
< 64; i
++)
99 if(outFile
.open(QIODevice::WriteOnly
)) break;
100 if(!i
) qWarning("Failed to open file on first attemp, retrying...");
105 if(outFile
.isOpen() && outFile
.isWritable())
107 if(outFile
.write(reinterpret_cast<const char*>(resource
.data()), resource
.size()) != resource
.size())
109 QFile::remove(QFileInfo(outFile
).canonicalFilePath());
110 THROW(QString("File '%1' could not be written!").arg(QFileInfo(outFile
).fileName()).toUtf8().constData());
116 THROW(QString("File '%1' could not be created!").arg(QFileInfo(outFile
).fileName()).toUtf8().constData());
120 for(int i
= 0; i
< 64; i
++)
122 m_fileHandle
= CreateFileW(QWCHAR(QDir::toNativeSeparators(m_filePath
)), GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
123 if((m_fileHandle
!= NULL
) && (m_fileHandle
!= INVALID_HANDLE_VALUE
)) break;
124 if(!i
) qWarning("Failed to lock file on first attemp, retrying...");
128 //Locked successfully?
129 if((m_fileHandle
== NULL
) || (m_fileHandle
== INVALID_HANDLE_VALUE
))
131 QFile::remove(QFileInfo(outFile
).canonicalFilePath());
133 strcpy_s(error_msg
, 512, QString("File '%1' could not be locked!").arg(QFileInfo(outFile
).fileName()).toLatin1().constData());
137 //Open file for reading
140 int fd
= _open_osfhandle(reinterpret_cast<intptr_t>(m_fileHandle
), _O_RDONLY
| _O_BINARY
);
141 if(fd
>= 0) outFile
.open(fd
, QIODevice::ReadOnly
);
145 for(int i
= 0; i
< 64; i
++)
147 if(outFile
.open(QIODevice::ReadOnly
)) break;
148 if(!i
) qWarning("Failed to re-open file on first attemp, retrying...");
153 //Verify file contents
157 hash
= fileHash(outFile
).toHex();
162 QFile::remove(m_filePath
);
163 THROW(QString("File '%1' could not be read!").arg(QFileInfo(outFile
).fileName()).toLatin1().constData());
167 if(hash
.isNull() || _stricmp(hash
.constData(), expectedHash
.constData()))
169 qWarning("\nFile checksum error:\n A = %s\n B = %s\n", expectedHash
.constData(), hash
.constData());
170 LAMEXP_CLOSE(m_fileHandle
);
171 QFile::remove(m_filePath
);
172 THROW(QString("File '%1' is corruputed, take care!").arg(QFileInfo(resourcePath
).absoluteFilePath().replace(QRegExp("^:/"), QString())).toLatin1().constData());
176 LockedFile::LockedFile(const QString
&filePath
)
179 QFileInfo
existingFile(filePath
);
180 existingFile
.setCaching(false);
182 //Make sure the file exists, before we try to lock it
183 if(!existingFile
.exists())
186 strcpy_s(error_msg
, 256, QString("File '%1' does not exist!").arg(existingFile
.fileName()).toLatin1().constData());
191 m_filePath
= existingFile
.canonicalFilePath();
194 for(int i
= 0; i
< 64; i
++)
196 m_fileHandle
= CreateFileW(QWCHAR(QDir::toNativeSeparators(filePath
)), GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
197 if((m_fileHandle
!= NULL
) && (m_fileHandle
!= INVALID_HANDLE_VALUE
)) break;
198 if(!i
) qWarning("Failed to lock file on first attemp, retrying...");
202 //Locked successfully?
203 if((m_fileHandle
== NULL
) || (m_fileHandle
== INVALID_HANDLE_VALUE
))
205 THROW(QString("File '%1' could not be locked!").arg(existingFile
.fileName()).toLatin1().constData());
209 LockedFile::~LockedFile(void)
211 LAMEXP_CLOSE(m_fileHandle
);
214 const QString
&LockedFile::filePath()