| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <io.h>
 | 
					
						
							| 
									
										
										
										
											2009-01-16 02:58:34 +00:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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 11:39:20 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2014-12-04 11:39:20 -05:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 02:02:01 +01:00
										 |  |  | #include <zlib.h>
 | 
					
						
							| 
									
										
										
										
											2013-10-26 11:55:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							|  |  |  | #include "Common/Hash.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "DiscIO/Blob.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "DiscIO/CompressedBlob.h"
 | 
					
						
							|  |  |  | #include "DiscIO/DiscScrubber.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | CompressedBlobReader::CompressedBlobReader(const std::string& filename) : m_file_name(filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	m_file.Open(filename, "rb"); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_file_size = File::GetSize(filename); | 
					
						
							|  |  |  | 	m_file.ReadArray(&m_header, 1); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	SetSectorSize(m_header.block_size); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// cache block pointers and hashes
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_block_pointers = new u64[m_header.num_blocks]; | 
					
						
							|  |  |  | 	m_file.ReadArray(m_block_pointers, m_header.num_blocks); | 
					
						
							|  |  |  | 	m_hashes = new u32[m_header.num_blocks]; | 
					
						
							|  |  |  | 	m_file.ReadArray(m_hashes, m_header.num_blocks); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_data_offset = (sizeof(CompressedBlobHeader)) | 
					
						
							|  |  |  | 	              + (sizeof(u64)) * m_header.num_blocks  // skip block pointers
 | 
					
						
							|  |  |  | 	              + (sizeof(u32)) * m_header.num_blocks; // skip hashes
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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.
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_zlib_buffer_size = m_header.block_size + 64; | 
					
						
							|  |  |  | 	m_zlib_buffer = new u8[m_zlib_buffer_size]; | 
					
						
							|  |  |  | 	memset(m_zlib_buffer, 0, m_zlib_buffer_size); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | CompressedBlobReader* CompressedBlobReader::Create(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (IsCompressedBlob(filename)) | 
					
						
							|  |  |  | 		return new CompressedBlobReader(filename); | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		return nullptr; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CompressedBlobReader::~CompressedBlobReader() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	delete [] m_zlib_buffer; | 
					
						
							|  |  |  | 	delete [] m_block_pointers; | 
					
						
							|  |  |  | 	delete [] m_hashes; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IMPORTANT: Calling this function invalidates all earlier pointers gotten from this function.
 | 
					
						
							|  |  |  | u64 CompressedBlobReader::GetBlockCompressedSize(u64 block_num) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04: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 05:30:24 +00:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	u64 offset = m_block_pointers[block_num] + m_data_offset; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (offset & (1ULL << 63)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		if (comp_block_size != m_header.block_size) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			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.
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	memset(m_zlib_buffer + comp_block_size, 0, m_zlib_buffer_size - comp_block_size); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	m_file.Seek(offset, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_file.ReadBytes(m_zlib_buffer, comp_block_size); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	u8* source = m_zlib_buffer; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	u8* dest = out_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// First, check hash.
 | 
					
						
							|  |  |  | 	u32 block_hash = HashAdler32(source, comp_block_size); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	if (block_hash != m_hashes[block_num]) | 
					
						
							| 
									
										
										
										
											2013-10-26 11:55:41 +02:00
										 |  |  | 		PanicAlert("Hash of block %" PRIu64 " is %08x instead of %08x.\n" | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 		           "Your ISO, \"%s\", is corrupt.", | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		           block_num, block_hash, m_hashes[block_num], | 
					
						
							|  |  |  | 		           m_file_name.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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; | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		if (z.avail_in > m_header.block_size) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			PanicAlert("We have a problem"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		z.next_out  = dest; | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		z.avail_out = m_header.block_size; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		inflateInit(&z); | 
					
						
							|  |  |  | 		int status = inflate(&z, Z_FULL_FLUSH); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		u32 uncomp_size = m_header.block_size - z.avail_out; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		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 11:55:41 +02:00
										 |  |  | 			PanicAlert("Failure reading block %" PRIu64 " - out of data and not at end.", block_num); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		inflateEnd(&z); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		if (uncomp_size != m_header.block_size) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			PanicAlert("Wrong block size"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u32 sub_type, | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 						int block_size, CompressCB callback, void* arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 	bool scrubbing = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	if (IsCompressedBlob(infile)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 		PanicAlertT("\"%s\" is already compressed! Cannot compress it further.", infile.c_str()); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	File::IOFile inf(infile, "rb"); | 
					
						
							|  |  |  | 	if (!inf) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PanicAlertT("Failed to open the input file \"%s\".", infile.c_str()); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	File::IOFile f(outfile, "wb"); | 
					
						
							|  |  |  | 	if (!f) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		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.", | 
					
						
							|  |  |  | 		            outfile.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-21 19:19:15 +00:00
										 |  |  | 	if (sub_type == 1) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 		if (!DiscScrubber::SetupScrub(infile, block_size)) | 
					
						
							| 
									
										
										
										
											2009-05-22 07:14:34 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			PanicAlertT("\"%s\" failed to be scrubbed. Probably the image is corrupt.", infile.c_str()); | 
					
						
							| 
									
										
										
										
											2009-05-21 19:19:15 +00:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2009-05-22 07:14:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		scrubbing = true; | 
					
						
							| 
									
										
										
										
											2009-05-21 19:19:15 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 11:12:17 -08:00
										 |  |  | 	z_stream z = {}; | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 	if (deflateInit(&z, 9) != Z_OK) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 		DiscScrubber::Cleanup(); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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 12:42:01 +00:00
										 |  |  | 	header.data_size  = File::GetSize(infile); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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 10:21:46 +00:00
										 |  |  | 	f.Seek(sizeof(CompressedBlobHeader), SEEK_CUR); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	// seek past the offset and hash tables (we will write them at the end)
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	f.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Now we are ready to write compressed data!
 | 
					
						
							|  |  |  | 	u64 position = 0; | 
					
						
							|  |  |  | 	int num_compressed = 0; | 
					
						
							|  |  |  | 	int num_stored = 0; | 
					
						
							| 
									
										
										
										
											2014-05-02 22:47:04 -04:00
										 |  |  | 	int progress_monitor = std::max<int>(1, header.num_blocks / 1000); | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	bool success = true; | 
					
						
							| 
									
										
										
										
											2010-02-08 22:22:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	for (u32 i = 0; i < header.num_blocks; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-05-04 18:15:22 +00:00
										 |  |  | 		if (i % progress_monitor == 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 			const u64 inpos = inf.Tell(); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			int ratio = 0; | 
					
						
							|  |  |  | 			if (inpos != 0) | 
					
						
							|  |  |  | 				ratio = (int)(100 * position / inpos); | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			std::string temp = StringFromFormat("%i of %i blocks. Compression ratio %i%%", i, 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 07:53:28 -08:00
										 |  |  | 			if (was_cancelled) | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				success = false; | 
					
						
							| 
									
										
										
										
											2014-11-27 07:53:28 -08:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		offsets[i] = position; | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		size_t read_bytes; | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 		if (scrubbing) | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 			read_bytes = DiscScrubber::GetNextBlock(inf, in_buf); | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 			inf.ReadArray(in_buf, header.block_size, &read_bytes); | 
					
						
							|  |  |  | 		if (read_bytes < header.block_size) | 
					
						
							|  |  |  | 			std::fill(in_buf + read_bytes, in_buf + header.block_size, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int retval = deflateReset(&z); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		z.next_in   = in_buf; | 
					
						
							|  |  |  | 		z.avail_in  = header.block_size; | 
					
						
							|  |  |  | 		z.next_out  = out_buf; | 
					
						
							|  |  |  | 		z.avail_out = block_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (retval != Z_OK) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-21 21:15:35 +00:00
										 |  |  | 			ERROR_LOG(DISCIO, "Deflate failed"); | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			success = false; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int status = deflate(&z, Z_FINISH); | 
					
						
							|  |  |  | 		int comp_size = block_size - z.avail_out; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		u8* write_buf; | 
					
						
							|  |  |  | 		int write_size; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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-03-01 00:49:58 +01:00
										 |  |  | 			write_buf = in_buf; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			offsets[i] |= 0x8000000000000000ULL; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			write_size = block_size; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			num_stored++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// let's store compressed
 | 
					
						
							|  |  |  | 			//PanicAlert("Comp %i to %i", block_size, comp_size);
 | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			write_buf = out_buf; | 
					
						
							|  |  |  | 			write_size = comp_size; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			num_compressed++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!f.WriteBytes(write_buf, write_size)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			PanicAlertT( | 
					
						
							|  |  |  | 				"Failed to write the output file \"%s\".\n" | 
					
						
							|  |  |  | 				"Check that you have enough space available on the target drive.", | 
					
						
							|  |  |  | 				outfile.c_str()); | 
					
						
							|  |  |  | 			success = false; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		position += write_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hashes[i] = HashAdler32(write_buf, write_size); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	header.compressed_data_size = position; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	if (!success) | 
					
						
							| 
									
										
										
										
											2014-11-27 07:53:28 -08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Remove the incomplete output file.
 | 
					
						
							|  |  |  | 		f.Close(); | 
					
						
							|  |  |  | 		File::Delete(outfile); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Okay, go back and fill in headers
 | 
					
						
							|  |  |  | 		f.Seek(0, SEEK_SET); | 
					
						
							|  |  |  | 		f.WriteArray(&header, 1); | 
					
						
							|  |  |  | 		f.WriteArray(offsets, header.num_blocks); | 
					
						
							|  |  |  | 		f.WriteArray(hashes, header.num_blocks); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Cleanup
 | 
					
						
							|  |  |  | 	delete[] in_buf; | 
					
						
							|  |  |  | 	delete[] out_buf; | 
					
						
							|  |  |  | 	delete[] offsets; | 
					
						
							| 
									
										
										
										
											2009-10-08 22:22:13 +00:00
										 |  |  | 	delete[] hashes; | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 08:57:49 -08:00
										 |  |  | 	deflateEnd(&z); | 
					
						
							| 
									
										
										
										
											2010-02-08 22:46:04 +00:00
										 |  |  | 	DiscScrubber::Cleanup(); | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (success) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		callback("Done compressing disc image.", 1.0f, arg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return success; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool DecompressBlobToFile(const std::string& infile, const std::string& outfile, CompressCB callback, void* arg) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (!IsCompressedBlob(infile)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 		PanicAlertT("File not compressed"); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 11:39:20 -05:00
										 |  |  | 	std::unique_ptr<CompressedBlobReader> reader(CompressedBlobReader::Create(infile)); | 
					
						
							| 
									
										
										
										
											2013-01-09 19:59:31 -06:00
										 |  |  | 	if (!reader) | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		PanicAlertT("Failed to open the input file \"%s\".", infile.c_str()); | 
					
						
							| 
									
										
										
										
											2013-01-09 19:59:31 -06:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	File::IOFile f(outfile, "wb"); | 
					
						
							| 
									
										
										
										
											2013-01-09 19:59:31 -06:00
										 |  |  | 	if (!f) | 
					
						
							| 
									
										
										
										
											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.", | 
					
						
							|  |  |  | 			outfile.c_str()); | 
					
						
							| 
									
										
										
										
											2013-01-09 19:59:31 -06:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-09 19:59:31 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	const CompressedBlobHeader &header = reader->GetHeader(); | 
					
						
							| 
									
										
										
										
											2014-11-27 08:34:44 -08:00
										 |  |  | 	static const size_t BUFFER_BLOCKS = 32; | 
					
						
							|  |  |  | 	size_t buffer_size = header.block_size * BUFFER_BLOCKS; | 
					
						
							| 
									
										
										
										
											2015-02-12 20:30:11 -08:00
										 |  |  | 	size_t last_buffer_size = header.block_size * (header.num_blocks % BUFFER_BLOCKS); | 
					
						
							| 
									
										
										
										
											2014-12-04 11:39:20 -05:00
										 |  |  | 	std::vector<u8> buffer(buffer_size); | 
					
						
							| 
									
										
										
										
											2015-02-12 20:30:11 -08:00
										 |  |  | 	u32 num_buffers = (header.num_blocks + BUFFER_BLOCKS - 1) / BUFFER_BLOCKS; | 
					
						
							| 
									
										
										
										
											2014-11-27 08:34:44 -08:00
										 |  |  | 	int progress_monitor = std::max<int>(1, num_buffers / 100); | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	bool success = true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 08:34:44 -08:00
										 |  |  | 	for (u64 i = 0; i < num_buffers; i++) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-05-04 18:15:22 +00:00
										 |  |  | 		if (i % progress_monitor == 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			bool was_cancelled = !callback("Unpacking", (float)i / (float)num_buffers, arg); | 
					
						
							| 
									
										
										
										
											2014-11-27 07:53:28 -08:00
										 |  |  | 			if (was_cancelled) | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				success = false; | 
					
						
							| 
									
										
										
										
											2014-11-27 07:53:28 -08:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-02-12 20:30:11 -08:00
										 |  |  | 		const size_t sz = i == num_buffers - 1 ? last_buffer_size : buffer_size; | 
					
						
							|  |  |  | 		reader->Read(i * buffer_size, sz, buffer.data()); | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 		if (!f.WriteBytes(buffer.data(), sz)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			PanicAlertT( | 
					
						
							|  |  |  | 				"Failed to write the output file \"%s\".\n" | 
					
						
							|  |  |  | 				"Check that you have enough space available on the target drive.", | 
					
						
							|  |  |  | 				outfile.c_str()); | 
					
						
							|  |  |  | 			success = false; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-01 00:49:58 +01:00
										 |  |  | 	if (!success) | 
					
						
							| 
									
										
										
										
											2014-11-27 07:53:28 -08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Remove the incomplete output file.
 | 
					
						
							|  |  |  | 		f.Close(); | 
					
						
							|  |  |  | 		File::Delete(outfile); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		f.Resize(header.data_size); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool IsCompressedBlob(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	File::IOFile f(filename, "rb"); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CompressedBlobHeader header; | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	return f.ReadArray(&header, 1) && (header.magic_cookie == kBlobCookie); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 |