2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-12-10 03:22:16 +01:00
|
|
|
#include "DiscIO/CompressedBlob.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2014-12-04 17:39:20 +01:00
|
|
|
#include <memory>
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <string>
|
2016-12-21 12:50:15 +01:00
|
|
|
#include <utility>
|
2014-12-04 17:39:20 +01:00
|
|
|
#include <vector>
|
2020-10-14 18:34:45 +02:00
|
|
|
|
2014-02-19 02:02:01 +01:00
|
|
|
#include <zlib.h>
|
2013-10-26 11:55:41 +02:00
|
|
|
|
2021-12-10 03:22:16 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-04 20:56:20 +02:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Hash.h"
|
2020-09-15 12:29:41 +02:00
|
|
|
#include "Common/IOFile.h"
|
2015-09-26 23:13:07 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-21 01:47:53 +01:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "DiscIO/DiscScrubber.h"
|
2020-04-22 09:50:27 +02:00
|
|
|
#include "DiscIO/MultithreadedCompressor.h"
|
2019-03-30 16:20:45 +01:00
|
|
|
#include "DiscIO/Volume.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
bool IsGCZBlob(File::IOFile& file);
|
2016-12-21 11:30:12 +01:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
CompressedBlobReader::CompressedBlobReader(File::IOFile file, const std::string& filename)
|
|
|
|
: m_file(std::move(file)), m_file_name(filename)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
m_file_size = m_file.GetSize();
|
2022-01-29 05:58:31 +01:00
|
|
|
m_file.Seek(0, File::SeekOrigin::Begin);
|
2014-09-01 21:48:02 +02:00
|
|
|
m_file.ReadArray(&m_header, 1);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-09-01 21:48:02 +02:00
|
|
|
SetSectorSize(m_header.block_size);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// cache block pointers and hashes
|
2015-12-08 01:53:42 +01:00
|
|
|
m_block_pointers.resize(m_header.num_blocks);
|
|
|
|
m_file.ReadArray(m_block_pointers.data(), m_header.num_blocks);
|
|
|
|
m_hashes.resize(m_header.num_blocks);
|
|
|
|
m_file.ReadArray(m_hashes.data(), m_header.num_blocks);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-09-01 21:48:02 +02:00
|
|
|
m_data_offset = (sizeof(CompressedBlobHeader)) +
|
|
|
|
(sizeof(u64)) * m_header.num_blocks // skip block pointers
|
|
|
|
+ (sizeof(u32)) * m_header.num_blocks; // skip hashes
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// A compressed block is never ever longer than a decompressed block, so just header.block_size
|
|
|
|
// should be fine.
|
|
|
|
// I still add some safety margin.
|
2015-12-08 01:53:42 +01:00
|
|
|
const u32 zlib_buffer_size = m_header.block_size + 64;
|
|
|
|
m_zlib_buffer.resize(zlib_buffer_size);
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
std::unique_ptr<CompressedBlobReader> CompressedBlobReader::Create(File::IOFile file,
|
|
|
|
const std::string& filename)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
if (IsGCZBlob(file))
|
|
|
|
return std::unique_ptr<CompressedBlobReader>(
|
|
|
|
new CompressedBlobReader(std::move(file), filename));
|
2015-12-07 05:15:51 +01:00
|
|
|
|
|
|
|
return nullptr;
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
CompressedBlobReader::~CompressedBlobReader()
|
|
|
|
{
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2023-07-07 14:31:26 +02:00
|
|
|
std::unique_ptr<BlobReader> CompressedBlobReader::CopyReader() const
|
|
|
|
{
|
|
|
|
return Create(m_file.Duplicate("rb"), m_file_name);
|
|
|
|
}
|
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// IMPORTANT: Calling this function invalidates all earlier pointers gotten from this function.
|
|
|
|
u64 CompressedBlobReader::GetBlockCompressedSize(u64 block_num) const
|
|
|
|
{
|
2014-09-01 21:48:02 +02:00
|
|
|
u64 start = m_block_pointers[block_num];
|
|
|
|
if (block_num < m_header.num_blocks - 1)
|
|
|
|
return m_block_pointers[block_num + 1] - start;
|
|
|
|
else if (block_num == m_header.num_blocks - 1)
|
|
|
|
return m_header.compressed_data_size - start;
|
2008-09-18 01:25:35 +02:00
|
|
|
else
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "{} - illegal block number {}", __func__, block_num);
|
2008-09-18 01:25:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2016-04-26 13:24:08 +02:00
|
|
|
bool CompressedBlobReader::GetBlock(u64 block_num, u8* out_ptr)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
|
|
|
bool uncompressed = false;
|
|
|
|
u32 comp_block_size = (u32)GetBlockCompressedSize(block_num);
|
2014-09-01 21:48:02 +02:00
|
|
|
u64 offset = m_block_pointers[block_num] + m_data_offset;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
if (offset & (1ULL << 63))
|
|
|
|
{
|
2014-09-01 21:48:02 +02:00
|
|
|
if (comp_block_size != m_header.block_size)
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Uncompressed block with wrong size");
|
2008-09-18 01:25:35 +02:00
|
|
|
uncompressed = true;
|
|
|
|
offset &= ~(1ULL << 63);
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// clear unused part of zlib buffer. maybe this can be deleted when it works fully.
|
2015-12-08 01:53:42 +01:00
|
|
|
memset(&m_zlib_buffer[comp_block_size], 0, m_zlib_buffer.size() - comp_block_size);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2022-01-29 05:58:31 +01:00
|
|
|
m_file.Seek(offset, File::SeekOrigin::Begin);
|
2016-04-26 13:24:08 +02:00
|
|
|
if (!m_file.ReadBytes(m_zlib_buffer.data(), comp_block_size))
|
|
|
|
{
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "The disc image \"{}\" is truncated, some of the data is missing.",
|
|
|
|
m_file_name);
|
2022-01-29 06:01:03 +01:00
|
|
|
m_file.ClearError();
|
2016-04-26 13:24:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// First, check hash.
|
2020-11-11 07:06:33 +01:00
|
|
|
const u32 block_hash = Common::HashAdler32(m_zlib_buffer.data(), comp_block_size);
|
2014-09-01 21:48:02 +02:00
|
|
|
if (block_hash != m_hashes[block_num])
|
2020-11-11 07:06:33 +01:00
|
|
|
{
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO,
|
|
|
|
"The disc image \"{}\" is corrupt.\n"
|
|
|
|
"Hash of block {} is {:08x} instead of {:08x}.",
|
|
|
|
m_file_name, block_num, block_hash, m_hashes[block_num]);
|
2020-11-11 07:06:33 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
if (uncompressed)
|
|
|
|
{
|
2024-09-29 00:37:09 +02:00
|
|
|
std::copy_n(m_zlib_buffer.begin(), comp_block_size, out_ptr);
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-08 01:53:42 +01:00
|
|
|
z_stream z = {};
|
|
|
|
z.next_in = m_zlib_buffer.data();
|
2008-09-18 01:25:35 +02:00
|
|
|
z.avail_in = comp_block_size;
|
2014-09-01 21:48:02 +02:00
|
|
|
if (z.avail_in > m_header.block_size)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Compressed block size is larger than uncompressed block size");
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2015-12-08 01:53:42 +01:00
|
|
|
z.next_out = out_ptr;
|
2014-09-01 21:48:02 +02:00
|
|
|
z.avail_out = m_header.block_size;
|
2008-09-18 01:25:35 +02:00
|
|
|
inflateInit(&z);
|
|
|
|
int status = inflate(&z, Z_FULL_FLUSH);
|
2014-09-01 21:48:02 +02:00
|
|
|
u32 uncomp_size = m_header.block_size - z.avail_out;
|
2008-09-18 01:25:35 +02:00
|
|
|
if (status != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
// this seem to fire wrongly from time to time
|
|
|
|
// to be sure, don't use compressed isos :P
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Failure reading block {} - out of data and not at end.", block_num);
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
|
|
|
inflateEnd(&z);
|
2014-09-01 21:48:02 +02:00
|
|
|
if (uncomp_size != m_header.block_size)
|
2016-04-26 13:24:08 +02:00
|
|
|
{
|
2021-03-20 11:50:00 +01:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Wrong block size");
|
2016-04-26 13:24:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2016-04-26 13:24:08 +02:00
|
|
|
return true;
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
struct CompressThreadState
|
|
|
|
{
|
|
|
|
CompressThreadState() : z{} {}
|
|
|
|
~CompressThreadState() { deflateEnd(&z); }
|
|
|
|
|
|
|
|
// z_stream will stop working if it changes address, so this object must not be moved
|
|
|
|
CompressThreadState(const CompressThreadState&) = delete;
|
|
|
|
CompressThreadState(CompressThreadState&&) = delete;
|
|
|
|
CompressThreadState& operator=(const CompressThreadState&) = delete;
|
|
|
|
CompressThreadState& operator=(CompressThreadState&&) = delete;
|
|
|
|
|
|
|
|
std::vector<u8> compressed_buffer;
|
|
|
|
z_stream z;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompressParameters
|
|
|
|
{
|
2021-09-04 06:43:19 +02:00
|
|
|
std::vector<u8> data{};
|
|
|
|
u32 block_number = 0;
|
|
|
|
u64 inpos = 0;
|
2020-04-22 09:50:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OutputParameters
|
|
|
|
{
|
2021-09-04 06:43:19 +02:00
|
|
|
std::vector<u8> data{};
|
|
|
|
u32 block_number = 0;
|
|
|
|
bool compressed = false;
|
|
|
|
u64 inpos = 0;
|
2020-04-22 09:50:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static ConversionResultCode SetUpCompressThreadState(CompressThreadState* state)
|
|
|
|
{
|
|
|
|
return deflateInit(&state->z, 9) == Z_OK ? ConversionResultCode::Success :
|
|
|
|
ConversionResultCode::InternalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ConversionResult<OutputParameters> Compress(CompressThreadState* state,
|
|
|
|
CompressParameters parameters, int block_size,
|
|
|
|
std::vector<u32>* hashes, int* num_stored,
|
|
|
|
int* num_compressed)
|
|
|
|
{
|
|
|
|
state->compressed_buffer.resize(block_size);
|
|
|
|
|
|
|
|
int retval = deflateReset(&state->z);
|
|
|
|
state->z.next_in = parameters.data.data();
|
|
|
|
state->z.avail_in = block_size;
|
|
|
|
state->z.next_out = state->compressed_buffer.data();
|
|
|
|
state->z.avail_out = block_size;
|
|
|
|
|
|
|
|
if (retval != Z_OK)
|
|
|
|
{
|
2020-10-21 20:58:08 +02:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Deflate failed");
|
2020-04-22 09:50:27 +02:00
|
|
|
return ConversionResultCode::InternalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int status = deflate(&state->z, Z_FINISH);
|
|
|
|
|
|
|
|
state->compressed_buffer.resize(block_size - state->z.avail_out);
|
|
|
|
|
|
|
|
OutputParameters output_parameters;
|
|
|
|
if ((status != Z_STREAM_END) || (state->z.avail_out < 10))
|
|
|
|
{
|
|
|
|
// let's store uncompressed
|
|
|
|
++*num_stored;
|
|
|
|
output_parameters = OutputParameters{std::move(parameters.data), parameters.block_number, false,
|
|
|
|
parameters.inpos};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// let's store compressed
|
|
|
|
++*num_compressed;
|
|
|
|
output_parameters = OutputParameters{std::move(state->compressed_buffer),
|
|
|
|
parameters.block_number, true, parameters.inpos};
|
|
|
|
}
|
|
|
|
|
|
|
|
(*hashes)[parameters.block_number] =
|
|
|
|
Common::HashAdler32(output_parameters.data.data(), output_parameters.data.size());
|
|
|
|
|
|
|
|
return std::move(output_parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ConversionResultCode Output(OutputParameters parameters, File::IOFile* outfile,
|
|
|
|
u64* position, std::vector<u64>* offsets, int progress_monitor,
|
2020-06-26 18:35:09 +02:00
|
|
|
u32 num_blocks, CompressCB callback)
|
2020-04-22 09:50:27 +02:00
|
|
|
{
|
|
|
|
u64 offset = *position;
|
|
|
|
if (!parameters.compressed)
|
|
|
|
offset |= 0x8000000000000000ULL;
|
|
|
|
(*offsets)[parameters.block_number] = offset;
|
|
|
|
|
|
|
|
*position += parameters.data.size();
|
|
|
|
|
|
|
|
if (!outfile->WriteBytes(parameters.data.data(), parameters.data.size()))
|
|
|
|
return ConversionResultCode::WriteFailed;
|
|
|
|
|
|
|
|
if (parameters.block_number % progress_monitor == 0)
|
|
|
|
{
|
|
|
|
const int ratio =
|
|
|
|
parameters.inpos == 0 ? 0 : static_cast<int>(100 * *position / parameters.inpos);
|
|
|
|
|
2020-10-14 20:50:38 +02:00
|
|
|
const std::string text = Common::FmtFormatT("{0} of {1} blocks. Compression ratio {2}%",
|
|
|
|
parameters.block_number, num_blocks, ratio);
|
2020-04-22 09:50:27 +02:00
|
|
|
|
|
|
|
const float completion = static_cast<float>(parameters.block_number) / num_blocks;
|
|
|
|
|
2020-06-26 18:35:09 +02:00
|
|
|
if (!callback(text, completion))
|
2020-04-22 09:50:27 +02:00
|
|
|
return ConversionResultCode::Canceled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversionResultCode::Success;
|
2024-08-18 15:08:44 +02:00
|
|
|
}
|
2020-04-22 09:50:27 +02:00
|
|
|
|
2020-04-10 17:40:07 +02:00
|
|
|
bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
|
|
|
const std::string& outfile_path, u32 sub_type, int block_size,
|
2020-06-26 18:35:09 +02:00
|
|
|
CompressCB callback)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2022-08-01 11:53:30 +02:00
|
|
|
ASSERT(infile->GetDataSizeType() == DataSizeType::Accurate);
|
2020-04-04 20:56:20 +02:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
File::IOFile outfile(outfile_path, "wb");
|
|
|
|
if (!outfile)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
2020-11-11 07:06:33 +01:00
|
|
|
PanicAlertFmtT(
|
2020-11-16 13:28:11 +01:00
|
|
|
"Failed to open the output file \"{0}\".\n"
|
2020-11-11 07:06:33 +01:00
|
|
|
"Check that you have permissions to write the target folder and that the media can "
|
|
|
|
"be written.",
|
|
|
|
outfile_path);
|
2008-09-18 01:25:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-06-26 18:35:09 +02:00
|
|
|
callback(Common::GetStringT("Files opened, ready to compress."), 0);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
CompressedBlobHeader header;
|
2016-12-21 11:30:12 +01:00
|
|
|
header.magic_cookie = GCZ_MAGIC;
|
2008-09-18 01:25:35 +02:00
|
|
|
header.sub_type = sub_type;
|
|
|
|
header.block_size = block_size;
|
2020-04-10 17:40:07 +02:00
|
|
|
header.data_size = infile->GetDataSize();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// round upwards!
|
|
|
|
header.num_blocks = (u32)((header.data_size + (block_size - 1)) / block_size);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-12-08 01:53:42 +01:00
|
|
|
std::vector<u64> offsets(header.num_blocks);
|
|
|
|
std::vector<u32> hashes(header.num_blocks);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// seek past the header (we will write it at the end)
|
2022-01-29 05:58:31 +01:00
|
|
|
outfile.Seek(sizeof(CompressedBlobHeader), File::SeekOrigin::Current);
|
2008-09-18 01:25:35 +02:00
|
|
|
// seek past the offset and hash tables (we will write them at the end)
|
2022-01-29 05:58:31 +01:00
|
|
|
outfile.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, File::SeekOrigin::Current);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
// Now we are ready to write compressed data!
|
2020-04-04 20:56:20 +02:00
|
|
|
u64 inpos = 0;
|
2008-09-18 01:25:35 +02:00
|
|
|
u64 position = 0;
|
|
|
|
int num_compressed = 0;
|
|
|
|
int num_stored = 0;
|
2014-05-03 04:47:04 +02:00
|
|
|
int progress_monitor = std::max<int>(1, header.num_blocks / 1000);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
const auto compress = [&](CompressThreadState* state, CompressParameters parameters) {
|
|
|
|
return Compress(state, std::move(parameters), block_size, &hashes, &num_stored,
|
|
|
|
&num_compressed);
|
|
|
|
};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
const auto output = [&](OutputParameters parameters) {
|
|
|
|
return Output(std::move(parameters), &outfile, &position, &offsets, progress_monitor,
|
2020-06-26 18:35:09 +02:00
|
|
|
header.num_blocks, callback);
|
2020-04-22 09:50:27 +02:00
|
|
|
};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
MultithreadedCompressor<CompressThreadState, CompressParameters, OutputParameters> compressor(
|
|
|
|
SetUpCompressThreadState, compress, output);
|
2020-04-04 20:56:20 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
std::vector<u8> in_buf(block_size);
|
|
|
|
for (u32 i = 0; i < header.num_blocks; i++)
|
|
|
|
{
|
|
|
|
if (compressor.GetStatus() != ConversionResultCode::Success)
|
2020-04-04 20:56:20 +02:00
|
|
|
break;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
const u64 bytes_to_read = std::min<u64>(block_size, header.data_size - inpos);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
if (!infile->Read(inpos, bytes_to_read, in_buf.data()))
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2020-04-22 09:50:27 +02:00
|
|
|
compressor.SetError(ConversionResultCode::ReadFailed);
|
2015-03-01 00:49:58 +01:00
|
|
|
break;
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
std::fill(in_buf.begin() + bytes_to_read, in_buf.begin() + header.block_size, 0);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-04 20:56:20 +02:00
|
|
|
inpos += block_size;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
compressor.CompressAndWrite(CompressParameters{in_buf, i, inpos});
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
compressor.Shutdown();
|
|
|
|
|
2008-09-18 01:25:35 +02:00
|
|
|
header.compressed_data_size = position;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
const ConversionResultCode result = compressor.GetStatus();
|
|
|
|
|
|
|
|
if (result != ConversionResultCode::Success)
|
2014-11-27 16:53:28 +01:00
|
|
|
{
|
|
|
|
// Remove the incomplete output file.
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.Close();
|
|
|
|
File::Delete(outfile_path);
|
2014-11-27 16:53:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Okay, go back and fill in headers
|
2022-01-29 05:58:31 +01:00
|
|
|
outfile.Seek(0, File::SeekOrigin::Begin);
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.WriteArray(&header, 1);
|
|
|
|
outfile.WriteArray(offsets.data(), header.num_blocks);
|
|
|
|
outfile.WriteArray(hashes.data(), header.num_blocks);
|
2020-04-22 09:50:27 +02:00
|
|
|
|
2020-06-26 18:35:09 +02:00
|
|
|
callback(Common::GetStringT("Done compressing disc image."), 1.0f);
|
2014-11-27 16:53:28 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
if (result == ConversionResultCode::ReadFailed)
|
2020-11-16 13:28:11 +01:00
|
|
|
PanicAlertFmtT("Failed to read from the input file \"{0}\".", infile_path);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-04-22 09:50:27 +02:00
|
|
|
if (result == ConversionResultCode::WriteFailed)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
2020-11-16 13:28:11 +01:00
|
|
|
PanicAlertFmtT("Failed to write the output file \"{0}\".\n"
|
2020-11-11 07:06:33 +01:00
|
|
|
"Check that you have enough space available on the target drive.",
|
|
|
|
outfile_path);
|
2015-03-01 00:49:58 +01:00
|
|
|
}
|
2020-04-22 09:50:27 +02:00
|
|
|
|
|
|
|
return result == ConversionResultCode::Success;
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
bool IsGCZBlob(File::IOFile& file)
|
2008-09-18 01:25:35 +02:00
|
|
|
{
|
2017-01-21 21:26:19 +01:00
|
|
|
const u64 position = file.Tell();
|
2022-01-29 05:58:31 +01:00
|
|
|
if (!file.Seek(0, File::SeekOrigin::Begin))
|
2017-01-21 21:26:19 +01:00
|
|
|
return false;
|
2008-09-18 01:25:35 +02:00
|
|
|
CompressedBlobHeader header;
|
2017-01-21 21:26:19 +01:00
|
|
|
bool is_gcz = file.ReadArray(&header, 1) && header.magic_cookie == GCZ_MAGIC;
|
2022-01-29 05:58:31 +01:00
|
|
|
file.Seek(position, File::SeekOrigin::Begin);
|
2017-01-21 21:26:19 +01:00
|
|
|
return is_gcz;
|
2008-09-18 01:25:35 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2019-05-06 01:48:12 +02:00
|
|
|
} // namespace DiscIO
|