2006-06-15 04:19:30 +00:00
|
|
|
#include "File.h"
|
2014-11-08 21:40:06 -05:00
|
|
|
#include <cassert>
|
2015-07-31 14:06:45 -04:00
|
|
|
#include <cstring>
|
|
|
|
#include <climits>
|
2014-11-08 21:40:06 -05:00
|
|
|
#include <algorithm>
|
2006-06-15 04:19:30 +00:00
|
|
|
|
|
|
|
using namespace ISO9660;
|
|
|
|
|
2015-05-17 21:47:31 -04:00
|
|
|
CFile::CFile(CBlockProvider* blockProvider, uint64 start)
|
2018-04-30 21:01:23 +01:00
|
|
|
: m_blockProvider(blockProvider)
|
|
|
|
, m_start(start)
|
|
|
|
, m_end(ULLONG_MAX)
|
2015-05-17 21:47:31 -04:00
|
|
|
{
|
|
|
|
InitBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
CFile::CFile(CBlockProvider* blockProvider, uint64 start, uint64 size)
|
2018-04-30 21:01:23 +01:00
|
|
|
: m_blockProvider(blockProvider)
|
|
|
|
, m_start(start)
|
|
|
|
, m_end(start + size)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2015-05-17 21:47:31 -04:00
|
|
|
InitBlock();
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFile::~CFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
void CFile::Seek(int64 amount, Framework::STREAM_SEEK_DIRECTION whence)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
switch(whence)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_SET:
|
2014-11-08 21:40:06 -05:00
|
|
|
m_isEof = false;
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position = amount;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_CUR:
|
2014-11-08 21:40:06 -05:00
|
|
|
m_isEof = false;
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position += amount;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_END:
|
2014-11-08 21:40:06 -05:00
|
|
|
m_isEof = true;
|
|
|
|
m_position = m_end - m_start;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CFile::Tell()
|
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
return m_position;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
uint64 CFile::Read(void* data, uint64 length)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
if(length == 0) return 0;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-11-08 21:40:06 -05:00
|
|
|
assert((m_start + m_position) <= m_end);
|
|
|
|
uint64 remainFileSize = m_end - (m_start + m_position);
|
|
|
|
if(remainFileSize == 0) m_isEof = true;
|
|
|
|
length = std::min<uint64>(length, remainFileSize);
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
uint64 total = length;
|
2006-06-15 04:19:30 +00:00
|
|
|
//Read what's remaining of this block
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
SyncBlock();
|
2018-04-30 21:01:23 +01:00
|
|
|
uint64 blockPosition = (m_start + m_position) % CBlockProvider::BLOCKSIZE;
|
|
|
|
uint64 blockRemain = CBlockProvider::BLOCKSIZE - blockPosition;
|
|
|
|
uint64 toRead = (length > blockRemain) ? (blockRemain) : (length);
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
memcpy(data, m_block + blockPosition, static_cast<uint32>(toRead));
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position += toRead;
|
|
|
|
length -= toRead;
|
|
|
|
data = reinterpret_cast<uint8*>(data) + toRead;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
if(length == 0) break;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
return total;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CFile::Write(const void* pData, uint64 nLength)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::IsEOF()
|
|
|
|
{
|
2014-11-08 21:40:06 -05:00
|
|
|
return m_isEof;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 21:47:31 -04:00
|
|
|
void CFile::InitBlock()
|
|
|
|
{
|
|
|
|
m_blockPosition = static_cast<uint32>(m_start / CBlockProvider::BLOCKSIZE);
|
|
|
|
m_blockProvider->ReadBlock(m_blockPosition, m_block);
|
|
|
|
}
|
|
|
|
|
2006-06-15 04:19:30 +00:00
|
|
|
void CFile::SyncBlock()
|
|
|
|
{
|
2015-05-17 21:47:31 -04:00
|
|
|
uint32 position = static_cast<uint32>((m_start + m_position) / CBlockProvider::BLOCKSIZE);
|
2014-07-12 21:59:58 -04:00
|
|
|
if(position == m_blockPosition) return;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2015-05-17 21:47:31 -04:00
|
|
|
m_blockProvider->ReadBlock(position, m_block);
|
2014-07-12 21:59:58 -04:00
|
|
|
m_blockPosition = position;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|