Files
itgmania212121/stepmania/src/CryptHelpers.cpp
T

170 lines
4.3 KiB
C++
Raw Normal View History

2004-05-13 21:32:48 +00:00
#include "global.h"
#include "CryptHelpers.h"
// crypt headers
#include "crypto51/files.h"
2005-05-19 22:50:57 +00:00
RageFileStore::RageFileStore()
{
m_pFile = NULL;
}
RageFileStore::~RageFileStore()
{
delete m_pFile;
}
RageFileStore::RageFileStore( const RageFileStore &cpy ):
Store(cpy),
FilterPutSpaceHelper(cpy)
{
if( cpy.m_pFile != NULL )
m_pFile = new RageFile( *cpy.m_pFile );
else
m_pFile = NULL;
ASSERT( !cpy.m_waiting );
m_waiting = false;
}
2004-05-13 21:32:48 +00:00
void RageFileStore::StoreInitialize(const NameValuePairs &parameters)
{
const char *fileName;
2005-05-19 21:34:47 +00:00
if( parameters.GetValue("InputFileName", fileName) )
2004-05-13 21:32:48 +00:00
{
2005-05-19 22:50:57 +00:00
m_pFile = new RageFile;
if( !m_pFile->Open(fileName, RageFile::READ) )
2005-05-19 21:34:47 +00:00
throw OpenErr( fileName );
2004-05-13 21:32:48 +00:00
}
else
{
ASSERT(0);
}
m_waiting = false;
}
unsigned long RageFileStore::MaxRetrievable() const
{
2005-05-19 22:50:57 +00:00
if( m_pFile == NULL || !m_pFile->IsGood() )
2004-05-13 21:32:48 +00:00
return 0;
2004-06-06 08:34:58 +00:00
2005-05-19 22:50:57 +00:00
return m_pFile->GetFileSize() - m_pFile->Tell();
2004-05-13 21:32:48 +00:00
}
unsigned int RageFileStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking)
{
2005-05-19 22:50:57 +00:00
if( m_pFile == NULL || !m_pFile->IsGood() )
2004-05-13 21:32:48 +00:00
{
transferBytes = 0;
return 0;
}
2004-06-06 08:34:58 +00:00
2004-05-13 21:32:48 +00:00
unsigned long size=transferBytes;
transferBytes = 0;
2004-06-06 08:34:58 +00:00
2005-05-19 21:34:47 +00:00
if( m_waiting )
2004-05-13 21:32:48 +00:00
goto output;
2004-06-06 08:34:58 +00:00
2005-05-19 22:50:57 +00:00
while( size && !m_pFile->AtEOF() )
2004-05-13 21:32:48 +00:00
{
{
2004-06-06 08:34:58 +00:00
unsigned int spaceSize = 1024;
m_space = HelpCreatePutSpace(target, channel, 1, (unsigned int)STDMIN(size, (unsigned long)UINT_MAX), spaceSize);
2005-05-19 22:50:57 +00:00
m_len = m_pFile->Read( (char *)m_space, STDMIN(size, (unsigned long)spaceSize));
2004-06-06 22:31:49 +00:00
if( m_len == -1 )
2005-05-19 22:50:57 +00:00
throw ReadErr( *m_pFile );
2004-05-13 21:32:48 +00:00
}
unsigned int blockedBytes;
output:
blockedBytes = target.ChannelPutModifiable2(channel, m_space, m_len, 0, blocking);
m_waiting = blockedBytes > 0;
2005-05-19 21:34:47 +00:00
if( m_waiting )
2004-05-13 21:32:48 +00:00
return blockedBytes;
size -= m_len;
transferBytes += m_len;
}
2004-06-06 08:34:58 +00:00
2004-05-13 21:32:48 +00:00
return 0;
}
unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const
{
2005-05-19 22:50:57 +00:00
if( m_pFile == NULL || !m_pFile->IsGood() )
2004-05-13 21:32:48 +00:00
return 0;
2004-06-06 08:34:58 +00:00
2005-05-19 21:34:47 +00:00
if( begin == 0 && end == 1 )
2004-05-13 21:32:48 +00:00
{
2005-05-19 22:50:57 +00:00
int current = m_pFile->Tell();
2004-05-13 21:32:48 +00:00
byte result;
2005-05-19 22:50:57 +00:00
m_pFile->Read( &result, 1 );
m_pFile->Seek( current );
if( m_pFile->AtEOF() )
2004-05-13 21:32:48 +00:00
return 0;
2004-06-06 23:20:44 +00:00
2005-05-19 21:34:47 +00:00
unsigned int blockedBytes = target.ChannelPut( channel, byte(result), blocking );
2004-06-06 23:20:44 +00:00
begin += 1-blockedBytes;
return blockedBytes;
2004-05-13 21:32:48 +00:00
}
2004-06-06 08:34:58 +00:00
2004-05-13 21:32:48 +00:00
// TODO: figure out what happens on cin
2004-06-06 23:17:44 +00:00
// (What does that mean?)
2005-05-19 22:50:57 +00:00
int current = m_pFile->Tell();
2004-05-13 21:32:48 +00:00
int newPosition = current + (streamoff)begin;
2004-06-06 08:34:58 +00:00
2005-05-19 22:50:57 +00:00
if( newPosition >= m_pFile->GetFileSize() )
2004-05-13 21:32:48 +00:00
return 0; // don't try to seek beyond the end of file
2004-06-06 23:18:38 +00:00
2005-05-19 22:50:57 +00:00
m_pFile->Seek( newPosition );
2004-05-13 21:32:48 +00:00
try
{
2005-05-19 21:34:47 +00:00
ASSERT( !m_waiting );
2004-05-13 21:32:48 +00:00
unsigned long copyMax = end-begin;
2005-05-19 21:34:47 +00:00
unsigned int blockedBytes = const_cast<RageFileStore *>(this)->TransferTo2( target, copyMax, channel, blocking );
2004-05-13 21:32:48 +00:00
begin += copyMax;
2005-05-19 21:34:47 +00:00
if( blockedBytes )
2004-05-13 21:32:48 +00:00
{
const_cast<RageFileStore *>(this)->m_waiting = false;
return blockedBytes;
}
}
catch(...)
{
2005-05-19 22:50:57 +00:00
m_pFile->ClearError();
m_pFile->Seek( current );
2004-05-13 21:32:48 +00:00
throw;
}
2005-05-19 22:50:57 +00:00
m_pFile->ClearError();
m_pFile->Seek( current );
2004-06-06 08:34:58 +00:00
2004-05-13 21:32:48 +00:00
return 0;
}
2004-06-08 05:30:06 +00:00
/*
* (c) 2001-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/