2011-01-01 21:09:56 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2013-03-05 06:28:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
|
2011-01-01 21:09:56 +00:00
|
|
|
#include "Blob.h"
|
|
|
|
#include "CISOBlob.h"
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
2013-03-05 06:28:41 +00: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-03-05 06:28:41 +00:00
|
|
|
|
|
|
|
CISOHeader header;
|
2011-03-11 10:21:46 +00:00
|
|
|
m_file.ReadArray(&header, 1);
|
2013-03-05 06:28:41 +00:00
|
|
|
|
|
|
|
m_block_size = header.block_size;
|
2011-01-01 21:09:56 +00:00
|
|
|
|
2013-03-05 06:28:41 +00: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
|
|
|
}
|
|
|
|
|
|
|
|
CISOFileReader* CISOFileReader::Create(const char* filename)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-05 06:28:41 +00: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 06:28:41 +00:00
|
|
|
while (nbytes != 0)
|
2011-01-01 21:09:56 +00:00
|
|
|
{
|
2013-03-05 06:28:41 +00:00
|
|
|
auto const block = offset / m_block_size;
|
|
|
|
auto const data_offset = offset % m_block_size;
|
|
|
|
|
|
|
|
auto const bytes_to_read = std::min(m_block_size - data_offset, nbytes);
|
|
|
|
if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block])
|
2011-01-01 21:09:56 +00:00
|
|
|
{
|
2013-03-05 06:28:41 +00:00
|
|
|
// calcualte the base address
|
|
|
|
auto const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * m_block_size + data_offset;
|
|
|
|
|
|
|
|
if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadArray(out_ptr, bytes_to_read)))
|
|
|
|
return false;
|
|
|
|
|
2011-01-01 21:09:56 +00:00
|
|
|
out_ptr += bytes_to_read;
|
|
|
|
offset += bytes_to_read;
|
2013-03-05 06:28:41 +00:00
|
|
|
nbytes -= bytes_to_read;
|
2011-01-01 21:09:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-05 06:28:41 +00:00
|
|
|
std::fill_n(out_ptr, bytes_to_read, 0);
|
2011-01-01 21:09:56 +00:00
|
|
|
out_ptr += bytes_to_read;
|
|
|
|
offset += bytes_to_read;
|
2013-03-05 06:28:41 +00:00
|
|
|
nbytes -= bytes_to_read;
|
2011-01-01 21:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-05 06:28:41 +00:00
|
|
|
|
2011-01-01 21:09:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCISOBlob(const char* filename)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
File::IOFile f(filename, "rb");
|
2011-01-01 21:09:56 +00:00
|
|
|
|
2013-03-05 06:28:41 +00: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
|