Dodaj pliki projektów.
This commit is contained in:
parent
4912ed6774
commit
f6150b0d17
18 changed files with 9210 additions and 0 deletions
217
ExtractCargo.cpp
Normal file
217
ExtractCargo.cpp
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
/*
|
||||
* This file is part of VoidArchiveTool.
|
||||
*
|
||||
* Copyright (C) 2025 Yanczi
|
||||
*
|
||||
* Void Archive Toolis free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ExtractCargo.h"
|
||||
|
||||
ExtractCargo::ExtractCargo()
|
||||
:filesLen(0)
|
||||
, tablePosition(0)
|
||||
, filesHeadsOffset(0)
|
||||
, version(VERSION)
|
||||
, signature(SIGNATURE)
|
||||
{
|
||||
// TODO Auto-generated constructor stub
|
||||
|
||||
}
|
||||
|
||||
ExtractCargo::~ExtractCargo()
|
||||
{
|
||||
if (cargoFile.is_open())
|
||||
{
|
||||
cargoFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Punkt wejœcia
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ExtractCargo::Extract(const std::string& cFile)
|
||||
{
|
||||
cargoFileName = cFile;
|
||||
|
||||
//SprawdŸ czy plik istnieje
|
||||
if (!std::filesystem::exists(cargoFileName))
|
||||
{
|
||||
std::cerr << "Error: The given file is not exist!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
//SprawdŸ czy plik jets plikiem
|
||||
if (!std::filesystem::is_regular_file(cargoFileName))
|
||||
{
|
||||
std::cerr << "Error: The given file is a directory!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
//Otwieranie kontenera
|
||||
cargoFile.open(cargoFileName, std::ios::binary);
|
||||
|
||||
if (!CheckCargoFile())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
LoadFilesTable();
|
||||
ExtractingFilesFromCargo();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sprawdzenie poprawnoœci archiwum
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ExtractCargo::CheckCargoFile()
|
||||
{
|
||||
std::vector<char> magic(6);
|
||||
uint8_t cargoVer = 0;
|
||||
|
||||
if (!cargoFile.is_open())
|
||||
{
|
||||
std::cerr << "Error: Failed to open container" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
cargoFile.read(magic.data(), magic.size());
|
||||
cargoFile.read(reinterpret_cast<char*>(&cargoVer), sizeof(cargoVer));
|
||||
cargoFile.read(reinterpret_cast<char*>(&filesLen), sizeof(filesLen));
|
||||
cargoFile.read(reinterpret_cast<char*>(&tablePosition), sizeof(tablePosition));
|
||||
|
||||
if (std::string(magic.begin(), magic.end()) != signature)
|
||||
{
|
||||
std::cerr << "Error: Corrupted Cargo" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cargoVer != version)
|
||||
{
|
||||
std::cerr << "Error: Wrong cargo version" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
filesHeadsOffset = signature.length() + sizeof(cargoVer) + sizeof(filesLen);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Dekomprezja danych
|
||||
//-----------------------------------------------------------------------------
|
||||
std::vector<char> ExtractCargo::DecompressingData(const std::vector<char>& zip, const uint32_t& estimatedSize)
|
||||
{
|
||||
std::vector<char> unzip(estimatedSize);
|
||||
|
||||
int sizeData = LZ4_decompress_safe(
|
||||
zip.data(),
|
||||
unzip.data(),
|
||||
static_cast<int>(zip.size()),
|
||||
static_cast<int>(estimatedSize)
|
||||
);
|
||||
|
||||
if (sizeData < 0)
|
||||
{
|
||||
throw std::runtime_error("LZ4 Decompressing Error");
|
||||
}
|
||||
unzip.resize(sizeData);
|
||||
|
||||
return unzip;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sprawdzanie sumy kontrolnej
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ExtractCargo::HashValid(const std::vector<char>& data, const uint64_t& crc)
|
||||
{
|
||||
uint64_t actualCrc = XXH64(data.data(), data.size(), VERSION);
|
||||
|
||||
if (actualCrc != crc)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Pobieranie nag³ówków plików
|
||||
//-----------------------------------------------------------------------------
|
||||
void ExtractCargo::LoadFilesTable()
|
||||
{
|
||||
cargoFile.seekg(tablePosition);
|
||||
|
||||
for (uint32_t i = 0; i < filesLen; ++i)
|
||||
{
|
||||
FilesTable fhTmp;
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.nameLen), sizeof(fhTmp.nameLen));
|
||||
|
||||
std::vector<char> nameBuffor(fhTmp.nameLen);
|
||||
cargoFile.read(nameBuffor.data(), fhTmp.nameLen);
|
||||
fhTmp.nameFile = std::string(nameBuffor.begin(), nameBuffor.end());
|
||||
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.offset), sizeof(fhTmp.offset));
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.size), sizeof(fhTmp.size));
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.rawSize), sizeof(fhTmp.rawSize));
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.crc), sizeof(fhTmp.crc));
|
||||
cargoFile.read(reinterpret_cast<char*>(&fhTmp.isZip), sizeof(fhTmp.isZip));
|
||||
|
||||
filesHeads.push_back(fhTmp);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Wypakowywanie plików
|
||||
//-----------------------------------------------------------------------------
|
||||
void ExtractCargo::ExtractingFilesFromCargo()
|
||||
{
|
||||
for (const auto& fh : filesHeads)
|
||||
{
|
||||
std::filesystem::path dir = cargoFileName.stem() / fh.nameFile;
|
||||
CreateDirections(dir);
|
||||
std::ofstream file(dir, std::ios::binary);
|
||||
|
||||
cargoFile.seekg(fh.offset);
|
||||
std::vector<char> buffor(fh.size);
|
||||
|
||||
cargoFile.read(buffor.data(), fh.size);
|
||||
|
||||
std::vector<char> rawBuffor = fh.isZip ? DecompressingData(buffor, fh.rawSize) : buffor;
|
||||
|
||||
if (!HashValid(rawBuffor, fh.crc))
|
||||
{
|
||||
std::cerr << fh.nameFile << " Error: Corrupted data integration CRC" << std::endl;
|
||||
}
|
||||
|
||||
file.write(reinterpret_cast<const char*>(rawBuffor.data()), rawBuffor.size());
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::cout << "Unpacking complete!" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Utwórz katalog
|
||||
//-----------------------------------------------------------------------------
|
||||
void ExtractCargo::CreateDirections(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path.parent_path()))
|
||||
{
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue