2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
2009-01-15 19:58:34 -07:00
|
|
|
#include <windows.h>
|
2008-12-07 22:30:24 -07:00
|
|
|
#endif
|
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <algorithm>
|
2013-10-26 03:55:41 -06:00
|
|
|
#include <cinttypes>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
2014-02-18 18:02:01 -07:00
|
|
|
#include <zlib.h>
|
2013-10-26 03:55:41 -06:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Hash.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/CompressedBlob.h"
|
|
|
|
#include "DiscIO/DiscScrubber.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
CompressedBlobReader::CompressedBlobReader(const std::string& filename) : file_name(filename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.Open(filename, "rb");
|
2010-12-03 05:42:01 -07:00
|
|
|
file_size = File::GetSize(filename);
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.ReadArray(&header, 1);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
SetSectorSize(header.block_size);
|
|
|
|
|
|
|
|
// cache block pointers and hashes
|
|
|
|
block_pointers = new u64[header.num_blocks];
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.ReadArray(block_pointers, header.num_blocks);
|
2008-12-07 22:30:24 -07:00
|
|
|
hashes = new u32[header.num_blocks];
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.ReadArray(hashes, header.num_blocks);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
data_offset = (sizeof(CompressedBlobHeader))
|
|
|
|
+ (sizeof(u64)) * header.num_blocks // skip block pointers
|
|
|
|
+ (sizeof(u32)) * header.num_blocks; // skip hashes
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
zlib_buffer_size = header.block_size + 64;
|
|
|
|
zlib_buffer = new u8[zlib_buffer_size];
|
|
|
|
memset(zlib_buffer, 0, zlib_buffer_size);
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
CompressedBlobReader* CompressedBlobReader::Create(const std::string& filename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (IsCompressedBlob(filename))
|
|
|
|
return new CompressedBlobReader(filename);
|
|
|
|
else
|
2014-03-09 14:14:26 -06:00
|
|
|
return nullptr;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CompressedBlobReader::~CompressedBlobReader()
|
|
|
|
{
|
|
|
|
delete [] zlib_buffer;
|
|
|
|
delete [] block_pointers;
|
|
|
|
delete [] hashes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IMPORTANT: Calling this function invalidates all earlier pointers gotten from this function.
|
|
|
|
u64 CompressedBlobReader::GetBlockCompressedSize(u64 block_num) const
|
|
|
|
{
|
|
|
|
u64 start = block_pointers[block_num];
|
|
|
|
if (block_num < header.num_blocks - 1)
|
|
|
|
return block_pointers[block_num + 1] - start;
|
|
|
|
else if (block_num == header.num_blocks - 1)
|
|
|
|
return header.compressed_data_size - start;
|
|
|
|
else
|
|
|
|
PanicAlert("GetBlockCompressedSize - illegal block number %i", (int)block_num);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompressedBlobReader::GetBlock(u64 block_num, u8 *out_ptr)
|
|
|
|
{
|
|
|
|
bool uncompressed = false;
|
|
|
|
u32 comp_block_size = (u32)GetBlockCompressedSize(block_num);
|
|
|
|
u64 offset = block_pointers[block_num] + data_offset;
|
|
|
|
|
|
|
|
if (offset & (1ULL << 63))
|
|
|
|
{
|
|
|
|
if (comp_block_size != header.block_size)
|
|
|
|
PanicAlert("Uncompressed block with wrong size");
|
|
|
|
uncompressed = true;
|
|
|
|
offset &= ~(1ULL << 63);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear unused part of zlib buffer. maybe this can be deleted when it works fully.
|
|
|
|
memset(zlib_buffer + comp_block_size, 0, zlib_buffer_size - comp_block_size);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.Seek(offset, SEEK_SET);
|
|
|
|
m_file.ReadBytes(zlib_buffer, comp_block_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
u8* source = zlib_buffer;
|
|
|
|
u8* dest = out_ptr;
|
|
|
|
|
|
|
|
// First, check hash.
|
|
|
|
u32 block_hash = HashAdler32(source, comp_block_size);
|
|
|
|
if (block_hash != hashes[block_num])
|
2013-10-26 03:55:41 -06:00
|
|
|
PanicAlert("Hash of block %" PRIu64 " is %08x instead of %08x.\n"
|
2010-01-11 16:27:02 -07:00
|
|
|
"Your ISO, %s, is corrupt.",
|
|
|
|
block_num, block_hash, hashes[block_num],
|
2014-03-12 13:33:41 -06:00
|
|
|
file_name.c_str());
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
if (uncompressed)
|
|
|
|
{
|
|
|
|
memcpy(dest, source, comp_block_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z_stream z;
|
|
|
|
memset(&z, 0, sizeof(z));
|
|
|
|
z.next_in = source;
|
|
|
|
z.avail_in = comp_block_size;
|
|
|
|
if (z.avail_in > header.block_size)
|
|
|
|
{
|
|
|
|
PanicAlert("We have a problem");
|
|
|
|
}
|
|
|
|
z.next_out = dest;
|
|
|
|
z.avail_out = header.block_size;
|
|
|
|
inflateInit(&z);
|
|
|
|
int status = inflate(&z, Z_FULL_FLUSH);
|
|
|
|
u32 uncomp_size = header.block_size - z.avail_out;
|
|
|
|
if (status != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
// this seem to fire wrongly from time to time
|
|
|
|
// to be sure, don't use compressed isos :P
|
2013-10-26 03:55:41 -06:00
|
|
|
PanicAlert("Failure reading block %" PRIu64 " - out of data and not at end.", block_num);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
inflateEnd(&z);
|
|
|
|
if (uncomp_size != header.block_size)
|
|
|
|
PanicAlert("Wrong block size");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u32 sub_type,
|
2008-12-07 22:30:24 -07:00
|
|
|
int block_size, CompressCB callback, void* arg)
|
|
|
|
{
|
2010-02-08 15:46:04 -07:00
|
|
|
bool scrubbing = false;
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
if (IsCompressedBlob(infile))
|
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
PanicAlertT("%s is already compressed! Cannot compress it further.", infile.c_str());
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
if (sub_type == 1)
|
|
|
|
{
|
2010-02-08 15:46:04 -07:00
|
|
|
if (!DiscScrubber::SetupScrub(infile, block_size))
|
2009-05-22 01:14:34 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
PanicAlertT("%s failed to be scrubbed. Probably the image is corrupt.", infile.c_str());
|
2009-05-21 13:19:15 -06:00
|
|
|
return false;
|
2009-05-22 01:14:34 -06:00
|
|
|
}
|
2010-02-08 15:46:04 -07:00
|
|
|
|
|
|
|
scrubbing = true;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile inf(infile, "rb");
|
|
|
|
File::IOFile f(outfile, "wb");
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
if (!f || !inf)
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
callback("Files opened, ready to compress.", 0, arg);
|
|
|
|
|
|
|
|
CompressedBlobHeader header;
|
|
|
|
header.magic_cookie = kBlobCookie;
|
|
|
|
header.sub_type = sub_type;
|
|
|
|
header.block_size = block_size;
|
2010-12-03 05:42:01 -07:00
|
|
|
header.data_size = File::GetSize(infile);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// round upwards!
|
|
|
|
header.num_blocks = (u32)((header.data_size + (block_size - 1)) / block_size);
|
|
|
|
|
|
|
|
u64* offsets = new u64[header.num_blocks];
|
|
|
|
u32* hashes = new u32[header.num_blocks];
|
|
|
|
u8* out_buf = new u8[block_size];
|
|
|
|
u8* in_buf = new u8[block_size];
|
|
|
|
|
|
|
|
// seek past the header (we will write it at the end)
|
2011-03-11 03:21:46 -07:00
|
|
|
f.Seek(sizeof(CompressedBlobHeader), SEEK_CUR);
|
2008-12-07 22:30:24 -07:00
|
|
|
// seek past the offset and hash tables (we will write them at the end)
|
2011-03-11 03:21:46 -07:00
|
|
|
f.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// Now we are ready to write compressed data!
|
|
|
|
u64 position = 0;
|
|
|
|
int num_compressed = 0;
|
|
|
|
int num_stored = 0;
|
2010-05-04 12:15:22 -06:00
|
|
|
int progress_monitor = max<int>(1, header.num_blocks / 1000);
|
2010-02-08 15:22:20 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
for (u32 i = 0; i < header.num_blocks; i++)
|
|
|
|
{
|
2010-05-04 12:15:22 -06:00
|
|
|
if (i % progress_monitor == 0)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
const u64 inpos = inf.Tell();
|
2008-12-07 22:30:24 -07:00
|
|
|
int ratio = 0;
|
|
|
|
if (inpos != 0)
|
|
|
|
ratio = (int)(100 * position / inpos);
|
|
|
|
char temp[512];
|
2010-07-31 22:09:59 -06:00
|
|
|
sprintf(temp, "%i of %i blocks. Compression ratio %i%%", i, header.num_blocks, ratio);
|
2008-12-07 22:30:24 -07:00
|
|
|
callback(temp, (float)i / (float)header.num_blocks, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets[i] = position;
|
|
|
|
// u64 start = i * header.block_size;
|
|
|
|
// u64 size = header.block_size;
|
2009-08-03 16:51:13 -06:00
|
|
|
std::fill(in_buf, in_buf + header.block_size, 0);
|
2010-02-08 15:46:04 -07:00
|
|
|
if (scrubbing)
|
2013-01-31 14:29:29 -07:00
|
|
|
DiscScrubber::GetNextBlock(inf, in_buf);
|
2010-02-08 15:46:04 -07:00
|
|
|
else
|
2011-03-11 03:21:46 -07:00
|
|
|
inf.ReadBytes(in_buf, header.block_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
z_stream z;
|
|
|
|
memset(&z, 0, sizeof(z));
|
|
|
|
z.zalloc = Z_NULL;
|
|
|
|
z.zfree = Z_NULL;
|
|
|
|
z.opaque = Z_NULL;
|
|
|
|
z.next_in = in_buf;
|
|
|
|
z.avail_in = header.block_size;
|
|
|
|
z.next_out = out_buf;
|
|
|
|
z.avail_out = block_size;
|
|
|
|
int retval = deflateInit(&z, 9);
|
|
|
|
|
|
|
|
if (retval != Z_OK)
|
|
|
|
{
|
2009-05-21 15:15:35 -06:00
|
|
|
ERROR_LOG(DISCIO, "Deflate failed");
|
2008-12-07 22:30:24 -07:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = deflate(&z, Z_FINISH);
|
|
|
|
int comp_size = block_size - z.avail_out;
|
|
|
|
if ((status != Z_STREAM_END) || (z.avail_out < 10))
|
|
|
|
{
|
|
|
|
//PanicAlert("%i %i Store %i", i*block_size, position, comp_size);
|
|
|
|
// let's store uncompressed
|
|
|
|
offsets[i] |= 0x8000000000000000ULL;
|
2011-03-11 03:21:46 -07:00
|
|
|
f.WriteBytes(in_buf, block_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
hashes[i] = HashAdler32(in_buf, block_size);
|
|
|
|
position += block_size;
|
|
|
|
num_stored++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// let's store compressed
|
|
|
|
//PanicAlert("Comp %i to %i", block_size, comp_size);
|
2011-03-11 03:21:46 -07:00
|
|
|
f.WriteBytes(out_buf, comp_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
hashes[i] = HashAdler32(out_buf, comp_size);
|
|
|
|
position += comp_size;
|
|
|
|
num_compressed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
deflateEnd(&z);
|
|
|
|
}
|
|
|
|
|
|
|
|
header.compressed_data_size = position;
|
|
|
|
|
|
|
|
// Okay, go back and fill in headers
|
2011-03-11 03:21:46 -07:00
|
|
|
f.Seek(0, SEEK_SET);
|
|
|
|
f.WriteArray(&header, 1);
|
|
|
|
f.WriteArray(offsets, header.num_blocks);
|
|
|
|
f.WriteArray(hashes, header.num_blocks);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
// Cleanup
|
|
|
|
delete[] in_buf;
|
|
|
|
delete[] out_buf;
|
|
|
|
delete[] offsets;
|
2009-10-08 16:22:13 -06:00
|
|
|
delete[] hashes;
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2010-02-08 15:46:04 -07:00
|
|
|
DiscScrubber::Cleanup();
|
2008-12-07 22:30:24 -07:00
|
|
|
callback("Done compressing disc image.", 1.0f, arg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool DecompressBlobToFile(const std::string& infile, const std::string& outfile, CompressCB callback, void* arg)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!IsCompressedBlob(infile))
|
|
|
|
{
|
2011-01-12 19:05:58 -07:00
|
|
|
PanicAlertT("File not compressed");
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressedBlobReader* reader = CompressedBlobReader::Create(infile);
|
2013-01-09 18:59:31 -07:00
|
|
|
if (!reader)
|
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile f(outfile, "wb");
|
2013-01-09 18:59:31 -07:00
|
|
|
if (!f)
|
2013-04-19 07:21:45 -06:00
|
|
|
{
|
|
|
|
delete reader;
|
2013-01-09 18:59:31 -07:00
|
|
|
return false;
|
2013-04-19 07:21:45 -06:00
|
|
|
}
|
2013-01-09 18:59:31 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
const CompressedBlobHeader &header = reader->GetHeader();
|
|
|
|
u8* buffer = new u8[header.block_size];
|
2010-05-04 12:15:22 -06:00
|
|
|
int progress_monitor = max<int>(1, header.num_blocks / 100);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
for (u64 i = 0; i < header.num_blocks; i++)
|
|
|
|
{
|
2010-05-04 12:15:22 -06:00
|
|
|
if (i % progress_monitor == 0)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
callback("Unpacking", (float)i / (float)header.num_blocks, arg);
|
|
|
|
}
|
|
|
|
reader->Read(i * header.block_size, header.block_size, buffer);
|
2011-03-11 03:21:46 -07:00
|
|
|
f.WriteBytes(buffer, header.block_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
f.Resize(header.data_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
delete reader;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool IsCompressedBlob(const std::string& filename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile f(filename, "rb");
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
CompressedBlobHeader header;
|
2011-03-11 03:21:46 -07:00
|
|
|
return f.ReadArray(&header, 1) && (header.magic_cookie == kBlobCookie);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|