2021-10-15 15:41:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2021 Maschell
|
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "WUXFileWriter.h"
|
|
|
|
#include "WUDDumperState.h"
|
2022-07-26 09:24:06 +02:00
|
|
|
#include "utils/StringTools.h"
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <utils/logger.h>
|
2021-10-15 15:41:16 +02:00
|
|
|
|
2022-07-26 08:16:27 +02:00
|
|
|
WUXFileWriter::WUXFileWriter(const char *path, int32_t cacheSize, int32_t sectorSize, bool split) : WUDFileWriter(path, cacheSize, sectorSize, split) {
|
|
|
|
wuxHeader_t wuxHeader = {0};
|
|
|
|
wuxHeader.magic0 = WUX_MAGIC_0;
|
|
|
|
wuxHeader.magic1 = WUX_MAGIC_1;
|
|
|
|
wuxHeader.sectorSize = swap_uint32(this->sectorSize);
|
2021-10-15 15:41:16 +02:00
|
|
|
wuxHeader.uncompressedSize = swap_uint64(WUD_FILE_SIZE);
|
2022-07-26 08:16:27 +02:00
|
|
|
wuxHeader.flags = 0;
|
2021-10-15 15:41:16 +02:00
|
|
|
|
2022-07-28 13:27:44 +02:00
|
|
|
if (this->write((uint8_t *) &wuxHeader, sizeof(wuxHeader_t)) != sizeof(wuxHeader_t)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to write header");
|
|
|
|
WUDFileWriter::close();
|
|
|
|
return;
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
this->sectorTableStart = this->tell();
|
|
|
|
this->totalSectorCount = WUD_FILE_SIZE / this->sectorSize;
|
|
|
|
|
2022-07-26 09:24:06 +02:00
|
|
|
this->sectorIndexTable = (void *) memalign(0x40, ROUNDUP(totalSectorCount * 4, 0x40));
|
2021-10-15 15:41:16 +02:00
|
|
|
if (sectorIndexTable == nullptr) {
|
2022-07-28 12:49:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to alloc");
|
2021-10-15 15:41:16 +02:00
|
|
|
WUDFileWriter::close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(this->sectorIndexTable, 0, totalSectorCount * 4);
|
|
|
|
|
2022-07-28 13:27:44 +02:00
|
|
|
if (this->write((uint8_t *) this->sectorIndexTable, totalSectorCount * 4) != (int32_t) ((uint32_t) totalSectorCount * 4)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to write initial sector index table");
|
2021-10-15 15:41:16 +02:00
|
|
|
WUDFileWriter::close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->sectorTableEnd = this->tell();
|
2022-07-26 08:16:27 +02:00
|
|
|
uint64_t tableEnd = this->sectorTableEnd;
|
2021-10-15 15:41:16 +02:00
|
|
|
|
|
|
|
this->sectorTableEnd += this->sectorSize - 1;
|
|
|
|
this->sectorTableEnd -= (this->sectorTableEnd % this->sectorSize);
|
|
|
|
|
2022-07-26 08:16:27 +02:00
|
|
|
uint64_t padding = this->sectorTableEnd - tableEnd;
|
2022-07-26 09:24:06 +02:00
|
|
|
auto *paddingData = (uint8_t *) memalign(0x40, ROUNDUP(padding, 0x40));
|
2021-10-15 15:41:16 +02:00
|
|
|
memset(paddingData, 0, padding);
|
2022-07-28 13:27:44 +02:00
|
|
|
if (this->write(reinterpret_cast<const uint8_t *>(paddingData), padding) != (int32_t) padding) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to write padding.");
|
|
|
|
WUDFileWriter::close();
|
|
|
|
return;
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
free(paddingData);
|
|
|
|
this->hashMap.clear();
|
2022-07-28 13:27:44 +02:00
|
|
|
if (!flush()) {
|
|
|
|
WUXFileWriter::close();
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 09:24:06 +02:00
|
|
|
|
2021-10-15 15:41:16 +02:00
|
|
|
int32_t WUXFileWriter::writeSector(const uint8_t *buffer, uint32_t numberOfSectors) {
|
2022-07-28 13:27:44 +02:00
|
|
|
if (!isOpen()) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to write sector, file is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
int32_t curWrittenSectors = 0;
|
|
|
|
for (uint32_t i = 0; i < numberOfSectors; i++) {
|
|
|
|
uint32_t addr = ((uint32_t) buffer) + (i * this->sectorSize);
|
2022-07-26 09:24:06 +02:00
|
|
|
std::array<uint8_t, 32> hashOut{};
|
|
|
|
calculateHash256(reinterpret_cast<unsigned char *>(addr), this->sectorSize, reinterpret_cast<unsigned char *>(hashOut.data()));
|
2021-10-15 15:41:16 +02:00
|
|
|
|
|
|
|
auto *indexTable = (uint32_t *) this->sectorIndexTable;
|
|
|
|
|
2022-07-26 09:24:06 +02:00
|
|
|
auto it = hashMap.find(hashOut);
|
2021-10-15 15:41:16 +02:00
|
|
|
if (it != hashMap.end()) {
|
2022-07-26 09:24:06 +02:00
|
|
|
indexTable[this->currentSector] = swap_uint32(this->hashMap[hashOut]);
|
2021-10-15 15:41:16 +02:00
|
|
|
} else {
|
|
|
|
indexTable[this->currentSector] = swap_uint32(this->writtenSector);
|
2022-07-26 09:24:06 +02:00
|
|
|
hashMap[hashOut] = writtenSector;
|
2021-10-15 15:41:16 +02:00
|
|
|
if (isOpen()) {
|
2022-07-28 12:54:31 +02:00
|
|
|
if (write((uint8_t *) addr, this->sectorSize) != this->sectorSize) {
|
2022-07-28 12:49:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Write failed");
|
2021-10-15 15:41:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->writtenSector++;
|
|
|
|
curWrittenSectors++;
|
|
|
|
}
|
|
|
|
this->currentSector++;
|
|
|
|
}
|
|
|
|
return curWrittenSectors;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:27:44 +02:00
|
|
|
bool WUXFileWriter::writeSectorIndexTable() {
|
2021-10-15 15:41:16 +02:00
|
|
|
if (this->isOpen()) {
|
2022-07-28 13:27:44 +02:00
|
|
|
if (!flush()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
// We need to make sure to call CFile::seek!
|
2022-07-28 13:27:44 +02:00
|
|
|
if (seek((int64_t) sectorTableStart, SEEK_SET_BASE_CLASS) < 0) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Seek failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (write((uint8_t *) sectorIndexTable, totalSectorCount * 4) != (int32_t) ((uint32_t) totalSectorCount * 4)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to write sector index table");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!flush()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
}
|
2022-07-28 13:27:44 +02:00
|
|
|
return true;
|
2021-10-15 15:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WUXFileWriter::~WUXFileWriter() {
|
2022-07-28 13:27:44 +02:00
|
|
|
WUXFileWriter::flush();
|
2021-10-15 15:41:16 +02:00
|
|
|
WUXFileWriter::close();
|
|
|
|
free(sectorIndexTable);
|
|
|
|
}
|
2021-10-16 13:03:32 +02:00
|
|
|
|
2022-07-28 13:27:44 +02:00
|
|
|
bool WUXFileWriter::finalize() {
|
2021-10-16 13:03:32 +02:00
|
|
|
WUDFileWriter::finalize();
|
2022-07-28 13:27:44 +02:00
|
|
|
bool res = writeSectorIndexTable();
|
2021-10-16 13:03:32 +02:00
|
|
|
WUXFileWriter::close();
|
2022-07-28 13:27:44 +02:00
|
|
|
return res;
|
2021-10-16 13:03:32 +02:00
|
|
|
}
|