2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#include "Blob.h"
|
2013-10-18 07:32:56 +00:00
|
|
|
#include "CDUtils.h"
|
2010-12-29 14:42:20 +00:00
|
|
|
#include "CISOBlob.h"
|
2013-10-18 07:32:56 +00:00
|
|
|
#include "CompressedBlob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "DriveBlob.h"
|
2013-10-18 07:32:56 +00:00
|
|
|
#include "FileBlob.h"
|
|
|
|
#include "FileUtil.h"
|
2012-05-03 14:09:01 +00:00
|
|
|
#include "WbfsBlob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
|
|
|
// Provides caching and split-operation-to-block-operations facilities.
|
|
|
|
// Used for compressed blob reading and direct drive reading.
|
|
|
|
|
|
|
|
void SectorReader::SetSectorSize(int blocksize)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CACHE_SIZE; i++)
|
|
|
|
{
|
|
|
|
cache[i] = new u8[blocksize];
|
|
|
|
cache_tags[i] = (u64)(s64) - 1;
|
|
|
|
}
|
|
|
|
m_blocksize = blocksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SectorReader::~SectorReader() {
|
|
|
|
for (int i = 0; i < CACHE_SIZE; i++)
|
2009-02-22 16:48:54 +00:00
|
|
|
delete [] cache[i];
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const u8 *SectorReader::GetBlockData(u64 block_num)
|
|
|
|
{
|
2009-02-22 16:48:54 +00:00
|
|
|
// TODO : Expand usage of the cache to more than one block :P
|
2008-12-08 05:30:24 +00:00
|
|
|
if (cache_tags[0] == block_num)
|
|
|
|
{
|
|
|
|
return cache[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetBlock(block_num, cache[0]);
|
|
|
|
cache_tags[0] = block_num;
|
|
|
|
return cache[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
|
|
|
|
{
|
|
|
|
u64 startingBlock = offset / m_blocksize;
|
|
|
|
u64 remain = size;
|
|
|
|
|
|
|
|
int positionInBlock = (int)(offset % m_blocksize);
|
|
|
|
u64 block = startingBlock;
|
|
|
|
|
|
|
|
while (remain > 0)
|
|
|
|
{
|
2009-02-22 16:48:54 +00:00
|
|
|
// Check if we are ready to do a large block read. > instead of >= so we don't bother if remain is only one block.
|
2009-03-07 08:07:11 +00:00
|
|
|
if (positionInBlock == 0 && remain > (u64)m_blocksize)
|
2009-02-22 16:48:54 +00:00
|
|
|
{
|
|
|
|
u64 num_blocks = remain / m_blocksize;
|
|
|
|
ReadMultipleAlignedBlocks(block, num_blocks, out_ptr);
|
|
|
|
block += num_blocks;
|
|
|
|
out_ptr += num_blocks * m_blocksize;
|
|
|
|
remain -= num_blocks * m_blocksize;
|
2009-02-22 16:59:22 +00:00
|
|
|
continue;
|
2009-02-22 16:48:54 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
u32 toCopy = m_blocksize - positionInBlock;
|
|
|
|
if (toCopy >= remain)
|
|
|
|
{
|
2009-02-22 16:48:54 +00:00
|
|
|
const u8* data = GetBlockData(block);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Yay, we are done!
|
2008-12-08 05:30:24 +00:00
|
|
|
memcpy(out_ptr, data + positionInBlock, (size_t)remain);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-22 16:48:54 +00:00
|
|
|
const u8* data = GetBlockData(block);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
memcpy(out_ptr, data + positionInBlock, toCopy);
|
|
|
|
out_ptr += toCopy;
|
|
|
|
remain -= toCopy;
|
|
|
|
positionInBlock = 0;
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-22 16:48:54 +00:00
|
|
|
bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8 *out_ptr)
|
|
|
|
{
|
2009-03-07 08:07:11 +00:00
|
|
|
for (u64 i = 0; i < num_blocks; i++)
|
2009-02-22 16:48:54 +00:00
|
|
|
{
|
|
|
|
const u8 *data = GetBlockData(block_num + i);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
2009-02-22 16:59:22 +00:00
|
|
|
memcpy(out_ptr + i * m_blocksize, data, m_blocksize);
|
2009-02-22 16:48:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
IBlobReader* CreateBlobReader(const char* filename)
|
|
|
|
{
|
2010-02-16 08:46:21 +00:00
|
|
|
if (cdio_is_cdrom(std::string(filename)))
|
2009-02-21 23:44:40 +00:00
|
|
|
return DriveReader::Create(filename);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
if (!File::Exists(filename))
|
|
|
|
return 0;
|
|
|
|
|
2012-05-03 14:09:01 +00:00
|
|
|
if (IsWbfsBlob(filename))
|
|
|
|
return WbfsFileReader::Create(filename);
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
if (IsCompressedBlob(filename))
|
|
|
|
return CompressedBlobReader::Create(filename);
|
|
|
|
|
2010-12-29 14:42:20 +00:00
|
|
|
if (IsCISOBlob(filename))
|
|
|
|
return CISOFileReader::Create(filename);
|
|
|
|
|
2009-02-24 19:57:29 +00:00
|
|
|
// Still here? Assume plain file - since we know it exists due to the File::Exists check above.
|
2008-12-08 05:30:24 +00:00
|
|
|
return PlainFileReader::Create(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|