187 lines
5.1 KiB
C++
187 lines
5.1 KiB
C++
#include "fastfile_cod9_pc.h"
|
|
#include "zonefile_cod9_pc.h"
|
|
#include "encryption.h"
|
|
|
|
#include <QFile>
|
|
#include <QDebug>
|
|
|
|
FastFile_COD9_PC::FastFile_COD9_PC()
|
|
: FastFile() {
|
|
SetCompany(COMPANY_INFINITY_WARD);
|
|
SetType(FILETYPE_FAST_FILE);
|
|
SetSignage(SIGNAGE_UNSIGNED);
|
|
SetMagic(0);
|
|
SetVersion(0);
|
|
SetGame("COD9");
|
|
SetPlatform("PC");
|
|
}
|
|
|
|
FastFile_COD9_PC::FastFile_COD9_PC(const QByteArray& aData)
|
|
: FastFile_COD9_PC() {
|
|
|
|
if (!aData.isEmpty()) {
|
|
Load(aData);
|
|
}
|
|
}
|
|
|
|
FastFile_COD9_PC::FastFile_COD9_PC(const QString aFilePath)
|
|
: FastFile_COD9_PC() {
|
|
|
|
if (!aFilePath.isEmpty()) {
|
|
Load(aFilePath);
|
|
}
|
|
}
|
|
|
|
FastFile_COD9_PC::~FastFile_COD9_PC() {
|
|
|
|
}
|
|
|
|
QByteArray FastFile_COD9_PC::GetBinaryData() const {
|
|
return QByteArray();
|
|
}
|
|
|
|
bool FastFile_COD9_PC::Load(const QString aFilePath) {
|
|
if (aFilePath.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
// Check fastfile can be read
|
|
QFile *file = new QFile(aFilePath);
|
|
if (!file->open(QIODevice::ReadOnly)) {
|
|
qDebug() << QString("Error: Failed to open FastFile: %1!").arg(aFilePath);
|
|
return false;
|
|
}
|
|
|
|
// Decompress fastfile and close
|
|
const QString fastFileStem = aFilePath.section("/", -1, -1).section(".", 0, 0);
|
|
SetStem(fastFileStem);
|
|
if (!Load(file->readAll())) {
|
|
qDebug() << "Error: Failed to load fastfile: " << fastFileStem + ".ff";
|
|
return false;
|
|
}
|
|
|
|
file->close();
|
|
|
|
// Open zone file after decompressing ff and writing
|
|
return true;
|
|
}
|
|
|
|
bool FastFile_COD9_PC::Load(const QByteArray aData) {
|
|
// Create a XDataStream on the input data.
|
|
XDataStream fastFileStream(aData);
|
|
fastFileStream.setByteOrder(XDataStream::LittleEndian);
|
|
|
|
// Parse header values.
|
|
SetCompany(pParseFFCompany(&fastFileStream));
|
|
SetType(pParseFFFileType(&fastFileStream));
|
|
SetSignage(pParseFFSignage(&fastFileStream));
|
|
SetMagic(pParseFFMagic(&fastFileStream));
|
|
SetVersion(pParseFFVersion(&fastFileStream));
|
|
|
|
// Validate the fastfile magic.
|
|
QByteArray fileMagic(8, Qt::Uninitialized);
|
|
fastFileStream.readRawData(fileMagic.data(), 8);
|
|
if (fileMagic != "PHEEBs71") {
|
|
qWarning() << "Invalid fast file magic!";
|
|
return false;
|
|
}
|
|
fastFileStream.skipRawData(4);
|
|
|
|
// Read IV seed name (32 bytes).
|
|
QByteArray nameKey(32, Qt::Uninitialized);
|
|
fastFileStream.readRawData(nameKey.data(), 32);
|
|
|
|
// --- Salsa20 + IV setup ---
|
|
static QVector<quint32> ivCounter(4, 1);
|
|
QByteArray ivTable = Encryption::InitIVTable(nameKey);
|
|
ivCounter.fill(1); // reset global counters
|
|
|
|
// Skip RSA signature (0x100)
|
|
fastFileStream.skipRawData(0x100);
|
|
|
|
// Decrypt + decompress loop
|
|
QByteArray finalZone;
|
|
int chunkIndex = 0;
|
|
|
|
while (!fastFileStream.atEnd()) {
|
|
quint32 dataLength = 0;
|
|
fastFileStream >> dataLength;
|
|
if (dataLength == 0)
|
|
break;
|
|
|
|
QByteArray encryptedBlock(dataLength, Qt::Uninitialized);
|
|
if (fastFileStream.readRawData(encryptedBlock.data(), dataLength) != dataLength) {
|
|
qWarning() << "Unexpected EOF while reading block";
|
|
break;
|
|
}
|
|
|
|
// Derive IV for this chunk
|
|
QByteArray iv = Encryption::GetIV(ivTable, chunkIndex % 4);
|
|
|
|
// Salsa20 decryption
|
|
QByteArray decryptedBlock = Encryption::salsa20DecryptSection(
|
|
encryptedBlock,
|
|
QByteArray::fromHex("641D8A2FE31D3AA63622BBC9CE8587229D42B0F8ED9B924130BF88B65EDC50BE"),
|
|
iv,
|
|
64
|
|
);
|
|
|
|
// SHA1 hash of decrypted block
|
|
QCryptographicHash sha1(QCryptographicHash::Sha1);
|
|
sha1.addData(decryptedBlock);
|
|
QByteArray sha1Hash = sha1.result();
|
|
|
|
// Inflate into buffer
|
|
z_stream strm{};
|
|
strm.avail_in = static_cast<uInt>(decryptedBlock.size());
|
|
strm.next_in = reinterpret_cast<Bytef*>(decryptedBlock.data());
|
|
|
|
QByteArray decompressedData;
|
|
QByteArray buffer(0x10000, Qt::Uninitialized);
|
|
inflateInit2(&strm, -15);
|
|
|
|
int ret;
|
|
do {
|
|
strm.avail_out = buffer.size();
|
|
strm.next_out = reinterpret_cast<Bytef*>(buffer.data());
|
|
|
|
ret = inflate(&strm, Z_NO_FLUSH);
|
|
|
|
if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
|
|
qWarning() << "inflate failed with code" << ret;
|
|
break;
|
|
}
|
|
|
|
int have = buffer.size() - strm.avail_out;
|
|
if (have > 0)
|
|
decompressedData.append(buffer.constData(), have);
|
|
|
|
} while (ret != Z_STREAM_END);
|
|
|
|
inflateEnd(&strm);
|
|
|
|
finalZone.append(decompressedData);
|
|
|
|
// Update IV table for next block
|
|
Encryption::UpdateIVTable(ivTable, chunkIndex % 4, sha1Hash);
|
|
|
|
chunkIndex++;
|
|
}
|
|
|
|
// Export decompressed zone
|
|
Utils::ExportData(GetBaseStem() + ".zone", finalZone);
|
|
|
|
// Load zone file
|
|
ZoneFile_COD9_PC* zoneFile = new ZoneFile_COD9_PC();
|
|
zoneFile->SetStem(GetBaseStem() + ".zone");
|
|
if (!zoneFile->Load(finalZone)) {
|
|
qWarning() << "Failed to load ZoneFile!";
|
|
return false;
|
|
}
|
|
SetZoneFile(zoneFile);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
}
|