| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2010 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // 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>
 | 
					
						
							| 
									
										
										
										
											2015-12-06 23:15:51 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2016-12-21 12:50:15 +01:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-21 12:50:15 +01:00
										 |  |  | CISOFileReader::CISOFileReader(File::IOFile file) : m_file(std::move(file)) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_size = m_file.GetSize(); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   CISOHeader header; | 
					
						
							| 
									
										
										
										
											2016-12-21 12:50:15 +01:00
										 |  |  |   m_file.Seek(0, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_file.ReadArray(&header, 1); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_block_size = header.block_size; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   MapType count = 0; | 
					
						
							|  |  |  |   for (u32 idx = 0; idx < CISO_MAP_SIZE; ++idx) | 
					
						
							|  |  |  |     m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-21 12:50:15 +01:00
										 |  |  | std::unique_ptr<CISOFileReader> CISOFileReader::Create(File::IOFile file) | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-21 11:30:12 +01:00
										 |  |  |   CISOHeader header; | 
					
						
							| 
									
										
										
										
											2016-12-21 12:50:15 +01:00
										 |  |  |   if (file.Seek(0, SEEK_SET) && file.ReadArray(&header, 1) && header.magic == CISO_MAGIC) | 
					
						
							|  |  |  |     return std::unique_ptr<CISOFileReader>(new CISOFileReader(std::move(file))); | 
					
						
							| 
									
										
										
										
											2015-12-06 23:15:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | u64 CISOFileReader::GetDataSize() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return CISO_MAP_SIZE * m_block_size; | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u64 CISOFileReader::GetRawSize() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return m_size; | 
					
						
							| 
									
										
										
										
											2013-03-05 00:28:41 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   while (nbytes != 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block]) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // calculate the base address
 | 
					
						
							|  |  |  |       u64 const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * (u64)m_block_size + data_offset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadArray(out_ptr, bytes_to_read))) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         m_file.Clear(); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       std::fill_n(out_ptr, bytes_to_read, 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     out_ptr += bytes_to_read; | 
					
						
							|  |  |  |     offset += bytes_to_read; | 
					
						
							|  |  |  |     nbytes -= bytes_to_read; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2011-01-01 21:09:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 |