2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:09:55 +02:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 06:30:24 +01:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2009-01-16 03:58:34 +01:00
|
|
|
#include <windows.h>
|
2017-06-05 02:29:56 +02:00
|
|
|
#include <io.h>
|
2008-12-08 06:30:24 +01:00
|
|
|
#endif
|
|
|
|
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <algorithm>
|
2013-10-26 11:55:41 +02:00
|
|
|
#include <cinttypes>
|
2014-02-21 01:47:53 +01:00
|
|
|
#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>
|
2014-02-19 02:02:01 +01:00
|
|
|
#include <zlib.h>
|
2013-10-26 11:55:41 +02:00
|
|
|
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 21:46:32 +01:00
|
|
|
#include "Common/File.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Hash.h"
|
2015-09-26 23:13:07 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-03 07:08:54 +02:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-21 01:47:53 +01:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "DiscIO/CompressedBlob.h"
|
|
|
|
#include "DiscIO/DiscScrubber.h"
|
2019-03-30 16:20:45 +01:00
|
|
|
#include "DiscIO/Volume.h"
|
2008-12-08 06:30:24 +01: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-12-08 06:30:24 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
m_file_size = m_file.GetSize();
|
|
|
|
m_file.Seek(0, SEEK_SET);
|
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-12-08 06:30:24 +01: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-12-08 06:30:24 +01: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-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-12-08 06:30:24 +01: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-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CompressedBlobReader::~CompressedBlobReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-08 06:30:24 +01:00
|
|
|
else
|
|
|
|
PanicAlert("GetBlockCompressedSize - illegal block number %i", (int)block_num);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-26 13:24:08 +02:00
|
|
|
bool CompressedBlobReader::GetBlock(u64 block_num, u8* out_ptr)
|
2008-12-08 06:30:24 +01: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-12-08 06:30:24 +01:00
|
|
|
if (offset & (1ULL << 63))
|
|
|
|
{
|
2014-09-01 21:48:02 +02:00
|
|
|
if (comp_block_size != m_header.block_size)
|
2008-12-08 06:30:24 +01:00
|
|
|
PanicAlert("Uncompressed block with wrong size");
|
|
|
|
uncompressed = true;
|
|
|
|
offset &= ~(1ULL << 63);
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01: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
|
|
|
|
2011-03-11 11:21:46 +01:00
|
|
|
m_file.Seek(offset, SEEK_SET);
|
2016-04-26 13:24:08 +02:00
|
|
|
if (!m_file.ReadBytes(m_zlib_buffer.data(), comp_block_size))
|
|
|
|
{
|
|
|
|
PanicAlertT("The disc image \"%s\" is truncated, some of the data is missing.",
|
|
|
|
m_file_name.c_str());
|
|
|
|
m_file.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
// First, check hash.
|
2018-05-16 21:22:13 +02:00
|
|
|
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])
|
2015-03-15 16:42:22 +01:00
|
|
|
PanicAlertT("The disc image \"%s\" is corrupt.\n"
|
2015-09-08 07:44:37 +02:00
|
|
|
"Hash of block %" PRIu64 " is %08x instead of %08x.",
|
2015-03-15 16:42:22 +01:00
|
|
|
m_file_name.c_str(), block_num, block_hash, m_hashes[block_num]);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
if (uncompressed)
|
|
|
|
{
|
2015-12-08 01:53:42 +01:00
|
|
|
std::copy(m_zlib_buffer.begin(), m_zlib_buffer.begin() + comp_block_size, out_ptr);
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-08 01:53:42 +01:00
|
|
|
z_stream z = {};
|
|
|
|
z.next_in = m_zlib_buffer.data();
|
2008-12-08 06:30:24 +01:00
|
|
|
z.avail_in = comp_block_size;
|
2014-09-01 21:48:02 +02:00
|
|
|
if (z.avail_in > m_header.block_size)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
|
|
|
PanicAlert("We have a problem");
|
|
|
|
}
|
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-12-08 06:30:24 +01: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-12-08 06:30:24 +01:00
|
|
|
if (status != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
// this seem to fire wrongly from time to time
|
|
|
|
// to be sure, don't use compressed isos :P
|
2015-09-08 07:44:37 +02:00
|
|
|
PanicAlert("Failure reading block %" PRIu64 " - out of data and not at end.", block_num);
|
2008-12-08 06:30:24 +01: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
|
|
|
{
|
2008-12-08 06:30:24 +01:00
|
|
|
PanicAlert("Wrong block size");
|
2016-04-26 13:24:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-04-26 13:24:08 +02:00
|
|
|
return true;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
bool CompressFileToBlob(const std::string& infile_path, const std::string& outfile_path,
|
|
|
|
u32 sub_type, int block_size, CompressCB callback, void* arg)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2010-02-08 23:46:04 +01:00
|
|
|
bool scrubbing = false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
File::IOFile infile(infile_path, "rb");
|
2015-09-26 14:46:53 +02:00
|
|
|
if (IsGCZBlob(infile))
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
PanicAlertT("\"%s\" is already compressed! Cannot compress it further.", infile_path.c_str());
|
2015-03-01 00:49:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
if (!infile)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
PanicAlertT("Failed to open the input file \"%s\".", infile_path.c_str());
|
2015-03-01 00:49:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +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
|
|
|
{
|
|
|
|
PanicAlertT("Failed to open the output file \"%s\".\n"
|
|
|
|
"Check that you have permissions to write the target folder and that the media can "
|
|
|
|
"be written.",
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile_path.c_str());
|
2008-12-08 06:30:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-04 20:31:38 +01:00
|
|
|
DiscScrubber disc_scrubber;
|
2019-07-14 15:49:42 +02:00
|
|
|
std::unique_ptr<VolumeDisc> volume;
|
2009-05-21 21:19:15 +02:00
|
|
|
if (sub_type == 1)
|
|
|
|
{
|
2019-07-14 15:49:42 +02:00
|
|
|
volume = CreateDisc(infile_path);
|
2019-03-30 16:20:45 +01:00
|
|
|
if (!volume || !disc_scrubber.SetupScrub(volume.get(), block_size))
|
2009-05-22 09:14:34 +02:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
PanicAlertT("\"%s\" failed to be scrubbed. Probably the image is corrupt.",
|
|
|
|
infile_path.c_str());
|
2009-05-21 21:19:15 +02:00
|
|
|
return false;
|
2009-05-22 09:14:34 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-02-08 23:46:04 +01:00
|
|
|
scrubbing = true;
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-11-27 20:12:17 +01:00
|
|
|
z_stream z = {};
|
2014-11-27 17:57:49 +01:00
|
|
|
if (deflateInit(&z, 9) != Z_OK)
|
2008-12-08 06:30:24 +01:00
|
|
|
return false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-06-17 05:45:37 +02:00
|
|
|
callback(Common::GetStringT("Files opened, ready to compress."), 0, arg);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
CompressedBlobHeader header;
|
2016-12-21 11:30:12 +01:00
|
|
|
header.magic_cookie = GCZ_MAGIC;
|
2008-12-08 06:30:24 +01:00
|
|
|
header.sub_type = sub_type;
|
|
|
|
header.block_size = block_size;
|
2016-12-21 12:50:15 +01:00
|
|
|
header.data_size = infile.GetSize();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01: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);
|
|
|
|
std::vector<u8> out_buf(block_size);
|
|
|
|
std::vector<u8> in_buf(block_size);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
// seek past the header (we will write it at the end)
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.Seek(sizeof(CompressedBlobHeader), SEEK_CUR);
|
2008-12-08 06:30:24 +01:00
|
|
|
// seek past the offset and hash tables (we will write them at the end)
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
|
2017-01-21 21:27:25 +01:00
|
|
|
// seek to the start of the input file to make sure we get everything
|
|
|
|
infile.Seek(0, SEEK_SET);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
// Now we are ready to write compressed data!
|
|
|
|
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);
|
2015-03-01 00:49:58 +01:00
|
|
|
bool success = true;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
for (u32 i = 0; i < header.num_blocks; i++)
|
|
|
|
{
|
2010-05-04 20:15:22 +02:00
|
|
|
if (i % progress_monitor == 0)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
const u64 inpos = infile.Tell();
|
2008-12-08 06:30:24 +01:00
|
|
|
int ratio = 0;
|
|
|
|
if (inpos != 0)
|
|
|
|
ratio = (int)(100 * position / inpos);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-06-17 05:45:37 +02:00
|
|
|
const std::string temp =
|
|
|
|
StringFromFormat(Common::GetStringT("%i of %i blocks. Compression ratio %i%%").c_str(), i,
|
2015-11-20 11:33:47 +01:00
|
|
|
header.num_blocks, ratio);
|
2015-03-01 00:49:58 +01:00
|
|
|
bool was_cancelled = !callback(temp, (float)i / (float)header.num_blocks, arg);
|
2014-11-27 16:53:28 +01:00
|
|
|
if (was_cancelled)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
|
|
|
success = false;
|
2014-11-27 16:53:28 +01:00
|
|
|
break;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
offsets[i] = position;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-11-27 17:57:49 +01:00
|
|
|
size_t read_bytes;
|
2010-02-08 23:46:04 +01:00
|
|
|
if (scrubbing)
|
2016-12-21 12:50:15 +01:00
|
|
|
read_bytes = disc_scrubber.GetNextBlock(infile, in_buf.data());
|
2010-02-08 23:46:04 +01:00
|
|
|
else
|
2016-12-21 12:50:15 +01:00
|
|
|
infile.ReadArray(in_buf.data(), header.block_size, &read_bytes);
|
2014-11-27 17:57:49 +01:00
|
|
|
if (read_bytes < header.block_size)
|
2015-12-08 01:53:42 +01:00
|
|
|
std::fill(in_buf.begin() + read_bytes, in_buf.begin() + header.block_size, 0);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-11-27 17:57:49 +01:00
|
|
|
int retval = deflateReset(&z);
|
2015-12-08 01:53:42 +01:00
|
|
|
z.next_in = in_buf.data();
|
2008-12-08 06:30:24 +01:00
|
|
|
z.avail_in = header.block_size;
|
2015-12-08 01:53:42 +01:00
|
|
|
z.next_out = out_buf.data();
|
2008-12-08 06:30:24 +01:00
|
|
|
z.avail_out = block_size;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
if (retval != Z_OK)
|
|
|
|
{
|
2009-05-21 23:15:35 +02:00
|
|
|
ERROR_LOG(DISCIO, "Deflate failed");
|
2015-03-01 00:49:58 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
int status = deflate(&z, Z_FINISH);
|
|
|
|
int comp_size = block_size - z.avail_out;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-03-01 00:49:58 +01:00
|
|
|
u8* write_buf;
|
|
|
|
int write_size;
|
2008-12-08 06:30:24 +01:00
|
|
|
if ((status != Z_STREAM_END) || (z.avail_out < 10))
|
|
|
|
{
|
|
|
|
// PanicAlert("%i %i Store %i", i*block_size, position, comp_size);
|
|
|
|
// let's store uncompressed
|
2015-12-08 01:53:42 +01:00
|
|
|
write_buf = in_buf.data();
|
2008-12-08 06:30:24 +01:00
|
|
|
offsets[i] |= 0x8000000000000000ULL;
|
2015-03-01 00:49:58 +01:00
|
|
|
write_size = block_size;
|
2008-12-08 06:30:24 +01:00
|
|
|
num_stored++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// let's store compressed
|
|
|
|
// PanicAlert("Comp %i to %i", block_size, comp_size);
|
2015-12-08 01:53:42 +01:00
|
|
|
write_buf = out_buf.data();
|
2015-03-01 00:49:58 +01:00
|
|
|
write_size = comp_size;
|
2008-12-08 06:30:24 +01:00
|
|
|
num_compressed++;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
if (!outfile.WriteBytes(write_buf, write_size))
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
|
|
|
PanicAlertT("Failed to write the output file \"%s\".\n"
|
|
|
|
"Check that you have enough space available on the target drive.",
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile_path.c_str());
|
2015-03-01 00:49:58 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-03-01 00:49:58 +01:00
|
|
|
position += write_size;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-05-16 21:22:13 +02:00
|
|
|
hashes[i] = Common::HashAdler32(write_buf, write_size);
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
header.compressed_data_size = position;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-03-01 00:49:58 +01:00
|
|
|
if (!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
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.Seek(0, SEEK_SET);
|
|
|
|
outfile.WriteArray(&header, 1);
|
|
|
|
outfile.WriteArray(offsets.data(), header.num_blocks);
|
|
|
|
outfile.WriteArray(hashes.data(), header.num_blocks);
|
2014-11-27 16:53:28 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
// Cleanup
|
2014-11-27 17:57:49 +01:00
|
|
|
deflateEnd(&z);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-03-01 00:49:58 +01:00
|
|
|
if (success)
|
|
|
|
{
|
2019-06-17 05:45:37 +02:00
|
|
|
callback(Common::GetStringT("Done compressing disc image."), 1.0f, arg);
|
2015-03-01 00:49:58 +01:00
|
|
|
}
|
|
|
|
return success;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
bool DecompressBlobToFile(const std::string& infile_path, const std::string& outfile_path,
|
2014-03-12 20:33:41 +01:00
|
|
|
CompressCB callback, void* arg)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
std::unique_ptr<CompressedBlobReader> reader;
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
File::IOFile infile(infile_path, "rb");
|
|
|
|
if (!IsGCZBlob(infile))
|
|
|
|
{
|
|
|
|
PanicAlertT("File not compressed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader = CompressedBlobReader::Create(std::move(infile), infile_path);
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-10 02:59:31 +01:00
|
|
|
if (!reader)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
PanicAlertT("Failed to open the input file \"%s\".", infile_path.c_str());
|
2013-01-10 02:59:31 +01:00
|
|
|
return false;
|
2015-03-01 00:49:58 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +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
|
|
|
{
|
|
|
|
PanicAlertT("Failed to open the output file \"%s\".\n"
|
|
|
|
"Check that you have permissions to write the target folder and that the media can "
|
|
|
|
"be written.",
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile_path.c_str());
|
2013-01-10 02:59:31 +01:00
|
|
|
return false;
|
2015-03-01 00:49:58 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
const CompressedBlobHeader& header = reader->GetHeader();
|
2014-11-27 17:34:44 +01:00
|
|
|
static const size_t BUFFER_BLOCKS = 32;
|
|
|
|
size_t buffer_size = header.block_size * BUFFER_BLOCKS;
|
2014-12-04 17:39:20 +01:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
2015-02-13 05:30:11 +01:00
|
|
|
u32 num_buffers = (header.num_blocks + BUFFER_BLOCKS - 1) / BUFFER_BLOCKS;
|
2014-11-27 17:34:44 +01:00
|
|
|
int progress_monitor = std::max<int>(1, num_buffers / 100);
|
2015-03-01 00:49:58 +01:00
|
|
|
bool success = true;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-11-27 17:34:44 +01:00
|
|
|
for (u64 i = 0; i < num_buffers; i++)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2010-05-04 20:15:22 +02:00
|
|
|
if (i % progress_monitor == 0)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2019-06-17 05:45:37 +02:00
|
|
|
const bool was_cancelled =
|
|
|
|
!callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers, arg);
|
2014-11-27 16:53:28 +01:00
|
|
|
if (was_cancelled)
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
|
|
|
success = false;
|
2014-11-27 16:53:28 +01:00
|
|
|
break;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
2020-04-15 22:11:31 +02:00
|
|
|
const u64 inpos = i * buffer_size;
|
|
|
|
const u64 sz = std::min<u64>(buffer_size, header.data_size - inpos);
|
|
|
|
reader->Read(inpos, sz, buffer.data());
|
2016-12-21 12:50:15 +01:00
|
|
|
if (!outfile.WriteBytes(buffer.data(), sz))
|
2015-03-01 00:49:58 +01:00
|
|
|
{
|
|
|
|
PanicAlertT("Failed to write the output file \"%s\".\n"
|
|
|
|
"Check that you have enough space available on the target drive.",
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile_path.c_str());
|
2015-03-01 00:49:58 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-03-01 00:49:58 +01:00
|
|
|
if (!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
|
|
|
|
{
|
2016-12-21 12:50:15 +01:00
|
|
|
outfile.Resize(header.data_size);
|
2014-11-27 16:53:28 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-21 21:27:25 +01:00
|
|
|
return success;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 12:50:15 +01:00
|
|
|
bool IsGCZBlob(File::IOFile& file)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2017-01-21 21:26:19 +01:00
|
|
|
const u64 position = file.Tell();
|
|
|
|
if (!file.Seek(0, SEEK_SET))
|
|
|
|
return false;
|
2008-12-08 06:30:24 +01:00
|
|
|
CompressedBlobHeader header;
|
2017-01-21 21:26:19 +01:00
|
|
|
bool is_gcz = file.ReadArray(&header, 1) && header.magic_cookie == GCZ_MAGIC;
|
|
|
|
file.Seek(position, SEEK_SET);
|
|
|
|
return is_gcz;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
2019-05-06 01:48:12 +02:00
|
|
|
} // namespace DiscIO
|