From d4060681482d6bac0f322131333f5586620b9fc1 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 19 May 2005 21:21:37 +0000 Subject: [PATCH] reduce crypto++ code by doing our own file writing (simpler) --- stepmania/src/CryptHelpers.cpp | 37 ------------------------------ stepmania/src/CryptHelpers.h | 25 --------------------- stepmania/src/CryptManager.cpp | 41 +++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 65 deletions(-) diff --git a/stepmania/src/CryptHelpers.cpp b/stepmania/src/CryptHelpers.cpp index c291c8c14c..c1ff7ec42c 100644 --- a/stepmania/src/CryptHelpers.cpp +++ b/stepmania/src/CryptHelpers.cpp @@ -119,43 +119,6 @@ unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigne return 0; } -void RageFileSink::IsolatedInitialize(const NameValuePairs ¶meters) -{ - const char *fileName; - if( !parameters.GetValue("OutputFileName", fileName) ) - ASSERT(0); - - if( !m_file.Open( fileName, RageFile::WRITE ) ) // trucates existing data - throw OpenErr(fileName); -} - -bool RageFileSink::IsolatedFlush(bool hardFlush, bool blocking) -{ - if (!m_file.IsOpen()) - throw Err("FileSink: output stream not opened"); - - - if( m_file.Flush() == -1 ) - throw WriteErr( m_file ); - - return false; -} - -unsigned int RageFileSink::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) -{ - if (!m_file.IsOpen()) - throw Err("FileSink: output stream not opened"); - - if( m_file.Write((const char *)inString, length) == -1 ) - throw WriteErr( m_file ); - - if (messageEnd) - if( m_file.Flush() == -1 ) - throw WriteErr( m_file ); - - return 0; -} - /* * (c) 2001-2004 Chris Danford * All rights reserved. diff --git a/stepmania/src/CryptHelpers.h b/stepmania/src/CryptHelpers.h index 8fab446489..b117be23aa 100644 --- a/stepmania/src/CryptHelpers.h +++ b/stepmania/src/CryptHelpers.h @@ -55,31 +55,6 @@ public: : SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputFileName", filename)("InputBinaryMode", binary));} }; - -//! . -class RageFileSink : public Sink -{ -public: - class Err : public Exception - { - public: - Err(const std::string &s) : Exception(IO_ERROR, s) {} - }; - class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}}; - struct WriteErr : public Err { WriteErr(const RageFile &f) : Err("RageFileSink(" + f.GetPath() + "): write error: " + f.GetError()) {}}; - - RageFileSink() {} - RageFileSink(const char *filename, bool binary=true) - {IsolatedInitialize(MakeParameters("OutputFileName", filename)("OutputBinaryMode", binary));} - - void IsolatedInitialize(const NameValuePairs ¶meters); - unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking); - bool IsolatedFlush(bool hardFlush, bool blocking); - -private: - RageFile m_file; -}; - #endif /* diff --git a/stepmania/src/CryptManager.cpp b/stepmania/src/CryptManager.cpp index 0e4d7441d8..c4db83090a 100644 --- a/stepmania/src/CryptManager.cpp +++ b/stepmania/src/CryptManager.cpp @@ -61,25 +61,56 @@ CryptManager::~CryptManager() } +static bool WriteFile( CString sFile, CString sBuf ) +{ + RageFile output; + if( !output.Open(sFile, RageFile::WRITE) ) + { + LOG->Warn( "WriteFile: opening %s failed: %s", sFile.c_str(), output.GetError().c_str() ); + return false; + } + + if( output.Write(sBuf) == -1 || output.Flush() == -1 ) + { + LOG->Warn( "WriteFile: writing %s failed: %s", sFile.c_str(), output.GetError().c_str() ); + output.Close(); + FILEMAN->Remove( sFile ); + return false; + } + + return true; +} + void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed ) { ASSERT( PREFSMAN->m_bSignProfileData ); + CString sPubKey, sPrivKey; try { NonblockingRng rng; RSASSA_PKCS1v15_SHA_Signer priv(rng, keyLength); - RageFileSink privFile(privFilename); + StringSink privFile( sPrivKey ); priv.DEREncode(privFile); privFile.MessageEnd(); RSASSA_PKCS1v15_SHA_Verifier pub(priv); - RageFileSink pubFile(pubFilename); + StringSink pubFile( sPubKey ); pub.DEREncode(pubFile); pubFile.MessageEnd(); } catch( const CryptoPP::Exception &s ) { LOG->Warn( "GenerateRSAKey failed: %s", s.what() ); + return; + } + + if( !WriteFile(pubFilename, sPubKey) ) + return; + + if( !WriteFile(privFilename, sPrivKey) ) + { + FILEMAN->Remove( privFilename ); + return; } } @@ -104,15 +135,19 @@ void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile ) return; } + CString sSignature; try { RageFileSource privFile(sPrivFilename, true); RSASSA_PKCS1v15_SHA_Signer priv(privFile); NonblockingRng rng; - RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new RageFileSink(sSignatureFile))); + RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new StringSink(sSignature))); } catch( const CryptoPP::Exception &s ) { LOG->Warn( "SignFileToFile failed: %s", s.what() ); + return; } + + WriteFile( sSignatureFile, sSignature ); } bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )