| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <cstdio>
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							|  |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "DiscIO/CISOBlob.h"
 | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | static const char CISO_MAGIC[] = "CISO"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | CISOFileReader::CISOFileReader(std::FILE* file) | 
					
						
							|  |  |  | 	: m_file(file) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	m_size = m_file.GetSize(); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 	CISOHeader header; | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	m_file.ReadArray(&header, 1); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 	m_block_size = header.block_size; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 	MapType count = 0; | 
					
						
							| 
									
										
										
										
											2014-02-13 15:47:42 -06:00
										 |  |  | 	for (u32 idx = 0; idx < CISO_MAP_SIZE; ++idx) | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 		m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | CISOFileReader* CISOFileReader::Create(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (IsCISOBlob(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		File::IOFile f(filename, "rb"); | 
					
						
							|  |  |  | 		return new CISOFileReader(f.ReleaseHandle()); | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		return nullptr; | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | u64 CISOFileReader::GetDataSize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return GetRawSize(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u64 CISOFileReader::GetRawSize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 	while (nbytes != 0) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-02-13 16:09:58 -06:00
										 |  |  | 		u64 const block = offset / m_block_size; | 
					
						
							|  |  |  | 		u64 const data_offset = offset % m_block_size; | 
					
						
							|  |  |  | 		u64 const bytes_to_read = std::min(m_block_size - data_offset, nbytes); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 		if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block]) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-04-19 09:21:45 -04:00
										 |  |  | 			// calculate the base address
 | 
					
						
							| 
									
										
										
										
											2014-02-13 16:09:58 -06:00
										 |  |  | 			u64 const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * (u64)m_block_size + data_offset; | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadArray(out_ptr, bytes_to_read))) | 
					
						
							| 
									
										
										
										
											2014-12-14 13:16:21 +01:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				m_file.Clear(); | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2014-12-14 13:16:21 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 			std::fill_n(out_ptr, bytes_to_read, 0); | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-13 15:47:42 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		out_ptr += bytes_to_read; | 
					
						
							| 
									
										
										
										
											2015-02-28 17:24:02 -05:00
										 |  |  | 		offset  += bytes_to_read; | 
					
						
							|  |  |  | 		nbytes  -= bytes_to_read; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool IsCISOBlob(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	File::IOFile f(filename, "rb"); | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | 	CISOHeader header; | 
					
						
							|  |  |  | 	return (f.ReadArray(&header, 1) && | 
					
						
							|  |  |  | 		std::equal(header.magic, header.magic + sizeof(header.magic), CISO_MAGIC)); | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 |