2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "DiscIO/CISOBlob.h"
|
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
#include <algorithm>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstdio>
|
2015-12-06 21:15:51 -07:00
|
|
|
#include <memory>
|
2016-12-21 04:50:15 -07:00
|
|
|
#include <utility>
|
2013-03-04 23:28:41 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 04:29:41 -06:00
|
|
|
#include "Common/IOFile.h"
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2010-12-29 07:42:20 -07:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 04:50:15 -07:00
|
|
|
CISOFileReader::CISOFileReader(File::IOFile file) : m_file(std::move(file))
|
2010-12-29 07:42:20 -07:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
m_size = m_file.GetSize();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
CISOHeader header;
|
2022-01-28 21:58:31 -07:00
|
|
|
m_file.Seek(0, File::SeekOrigin::Begin);
|
2011-03-11 03:21:46 -07:00
|
|
|
m_file.ReadArray(&header, 1);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
m_block_size = header.block_size;
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
MapType count = 0;
|
2014-02-13 14:47:42 -07:00
|
|
|
for (u32 idx = 0; idx < CISO_MAP_SIZE; ++idx)
|
2013-03-04 23:28:41 -07:00
|
|
|
m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID;
|
2010-12-29 07:42:20 -07:00
|
|
|
}
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2016-12-21 04:50:15 -07:00
|
|
|
std::unique_ptr<CISOFileReader> CISOFileReader::Create(File::IOFile file)
|
2010-12-29 07:42:20 -07:00
|
|
|
{
|
2016-12-21 03:30:12 -07:00
|
|
|
CISOHeader header;
|
2022-01-28 21:58:31 -07:00
|
|
|
if (file.Seek(0, File::SeekOrigin::Begin) && file.ReadArray(&header, 1) &&
|
|
|
|
header.magic == CISO_MAGIC)
|
|
|
|
{
|
2016-12-21 04:50:15 -07:00
|
|
|
return std::unique_ptr<CISOFileReader>(new CISOFileReader(std::move(file)));
|
2022-01-28 21:58:31 -07:00
|
|
|
}
|
2015-12-06 21:15:51 -07:00
|
|
|
|
|
|
|
return nullptr;
|
2010-12-29 07:42:20 -07:00
|
|
|
}
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
u64 CISOFileReader::GetDataSize() const
|
|
|
|
{
|
2020-01-22 10:57:50 -07:00
|
|
|
return static_cast<u64>(CISO_MAP_SIZE) * m_block_size;
|
2013-03-04 23:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 CISOFileReader::GetRawSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2010-12-29 07:42:20 -07:00
|
|
|
bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2020-01-13 14:08:44 -07:00
|
|
|
if (offset + nbytes > GetDataSize())
|
|
|
|
return false;
|
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
while (nbytes != 0)
|
2010-12-29 07:42:20 -07:00
|
|
|
{
|
2014-02-13 15:09:58 -07: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);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-04 23:28:41 -07:00
|
|
|
if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block])
|
2010-12-29 07:42:20 -07:00
|
|
|
{
|
2013-04-19 07:21:45 -06:00
|
|
|
// calculate the base address
|
2014-02-13 15:09:58 -07:00
|
|
|
u64 const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * (u64)m_block_size + data_offset;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-01-28 21:58:31 -07:00
|
|
|
if (!(m_file.Seek(file_off, File::SeekOrigin::Begin) &&
|
|
|
|
m_file.ReadArray(out_ptr, bytes_to_read)))
|
2014-12-14 05:16:21 -07:00
|
|
|
{
|
|
|
|
m_file.Clear();
|
2013-03-04 23:28:41 -07:00
|
|
|
return false;
|
2014-12-14 05:16:21 -07:00
|
|
|
}
|
2010-12-29 07:42:20 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-04 23:28:41 -07:00
|
|
|
std::fill_n(out_ptr, bytes_to_read, 0);
|
2010-12-29 07:42:20 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-02-13 14:47:42 -07:00
|
|
|
out_ptr += bytes_to_read;
|
2015-02-28 15:24:02 -07:00
|
|
|
offset += bytes_to_read;
|
|
|
|
nbytes -= bytes_to_read;
|
2010-12-29 07:42:20 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-12-29 07:42:20 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-01-01 14:09:56 -07:00
|
|
|
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace DiscIO
|