From 89e07bb257da0d80ec993d4979b536a51c792ec7 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Sun, 10 Jun 2012 15:36:37 +0800 Subject: [PATCH] removed OpenSSL in Download.cc --- libgrive/src/drive/Entry.cc | 2 +- libgrive/src/drive/Resource.cc | 1 - libgrive/src/http/Download.cc | 40 ++++----------------- libgrive/src/http/Download.hh | 15 +++++--- libgrive/src/util/Crypt.cc | 66 ++++++++++++++++++++++------------ libgrive/src/util/Crypt.hh | 26 ++++++++++---- 6 files changed, 82 insertions(+), 68 deletions(-) diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index aa500e9..843e58f 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -65,7 +65,7 @@ void Entry::FromLocal( const fs::path& path ) m_title = path.filename().string() ; m_filename = path.filename().string() ; m_kind = fs::is_directory(path) ? "folder" : "file" ; - m_md5 = fs::is_directory(path) ? "" : crypt::MD5( path ) ; + m_md5 = fs::is_directory(path) ? "" : crypt::MD5::Get( path ) ; m_mtime = fs::exists(path) ? os::FileCTime( path ) : DateTime() ; } diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index 2a36d2c..b3ad663 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -216,7 +216,6 @@ void Resource::FromLocal( const DateTime& last_sync ) m_state = ( mtime > last_sync ? local_new : remote_deleted ) ; m_entry.FromLocal( path ) ; - Trace( "file %1% read from local %2%", path, m_state ) ; } assert( m_state != unknown ) ; diff --git a/libgrive/src/http/Download.cc b/libgrive/src/http/Download.cc index 6895a98..15171cd 100644 --- a/libgrive/src/http/Download.cc +++ b/libgrive/src/http/Download.cc @@ -21,6 +21,7 @@ // #include "util/SignalHandler.hh" #include "Error.hh" +#include "util/Crypt.hh" // boost headers #include @@ -32,8 +33,6 @@ #include #include -#include - #include #include @@ -43,25 +42,17 @@ namespace gr { namespace http { Download::Download( const std::string& filename ) : m_file( filename, 0600 ), - m_mdctx( ::EVP_MD_CTX_create() ) + m_crypt( new crypt::MD5 ) { - if ( m_mdctx == 0 ) - throw std::bad_alloc() ; - - if ( ::EVP_DigestInit_ex( m_mdctx, ::EVP_md5(), 0 ) != 1 ) - BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( "cannot create MD5 digest context" ) ) ; } Download::Download( const std::string& filename, NoChecksum ) : - m_file( filename, 0600 ), - m_mdctx( 0 ) + m_file( filename, 0600 ) { } -Download::~Download( ) +Download::~Download() { - if ( m_mdctx != 0 ) - ::EVP_MD_CTX_destroy( m_mdctx ) ; } void Download::Clear() @@ -71,32 +62,15 @@ void Download::Clear() std::string Download::Finish() const { - // Unregister the signal -// SignalHandler::GetInstance().UnregisterSignal( SIGINT ) ; - - std::string result ; - - // get the checksum and return it ; - if ( m_mdctx != 0 ) - { - unsigned int size = EVP_MAX_MD_SIZE ; - result.resize( size ) ; - - if ( ::EVP_DigestFinal_ex( m_mdctx, reinterpret_cast(&result[0]), &size ) != 1 ) - throw Error() << expt::ErrMsg( "cannot calculate checksum" ) ; - - result.resize( size ) ; - } - - return result ; + return m_crypt.get() != 0 ? m_crypt->Get() : "" ; } std::size_t Download::OnData( void *data, std::size_t count ) { assert( data != 0 ) ; - if ( m_mdctx != 0 ) - ::EVP_DigestUpdate( m_mdctx, data, count ) ; + if ( m_crypt.get() != 0 ) + m_crypt->Write( data, count ) ; return m_file.Write( data, count ) ; } diff --git a/libgrive/src/http/Download.hh b/libgrive/src/http/Download.hh index 1c25b89..d3bdc59 100644 --- a/libgrive/src/http/Download.hh +++ b/libgrive/src/http/Download.hh @@ -24,9 +24,14 @@ #include -#include +namespace gr { -namespace gr { namespace http { +namespace crypt +{ + class MD5 ; +} + +namespace http { class Download : public http::Receivable { @@ -34,7 +39,7 @@ public : struct NoChecksum {} ; Download( const std::string& filename ) ; Download( const std::string& filename, NoChecksum ) ; - ~Download( ) ; + ~Download() ; std::string Finish() const ; @@ -42,8 +47,8 @@ public : std::size_t OnData( void *data, std::size_t count ) ; private : - StdioFile m_file ; - EVP_MD_CTX *m_mdctx ; + StdioFile m_file ; + std::auto_ptr m_crypt ; } ; } } // end of namespace diff --git a/libgrive/src/util/Crypt.cc b/libgrive/src/util/Crypt.cc index 715751d..6b9ca32 100644 --- a/libgrive/src/util/Crypt.cc +++ b/libgrive/src/util/Crypt.cc @@ -33,12 +33,49 @@ namespace gr { namespace crypt { const std::size_t read_size = 8 * 1024 ; -std::string MD5( const fs::path& file ) +struct MD5::Impl +{ + gcry_md_hd_t hd ; +} ; + +MD5::MD5() : m_impl( new Impl ) +{ + ::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ; + if ( err != GPG_ERR_NO_ERROR ) + { + BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( ::gcry_strerror(err) ) ) ; + } +} + +MD5::~MD5() +{ + ::gcry_md_close( m_impl->hd ) ; +} + +void MD5::Write( const void *data, std::size_t size ) +{ + ::gcry_md_write( m_impl->hd, data, size ) ; +} + +std::string MD5::Get() const +{ + unsigned char *md5 = ::gcry_md_read( m_impl->hd, GCRY_MD_MD5 ) ; + unsigned int len = ::gcry_md_get_algo_dlen(GCRY_MD_MD5) ; + + // format the MD5 string + std::ostringstream ss ; + for ( unsigned int i = 0 ; i < len ; i++ ) + ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(md5[i]) ; + + return ss.str() ; +} + +std::string MD5::Get( const fs::path& file ) { try { StdioFile sfile( file ) ; - return MD5( sfile ) ; + return Get( sfile ) ; } catch ( StdioFile::Error& ) { @@ -46,32 +83,17 @@ std::string MD5( const fs::path& file ) } } -std::string MD5( StdioFile& file ) +std::string MD5::Get( StdioFile& file ) { char buf[read_size] ; - - gcry_md_hd_t hd ; - gcry_error_t err = gcry_md_open( &hd, GCRY_MD_MD5, 0 ) ; - if ( err != GPG_ERR_NO_ERROR ) - { - BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( gcry_strerror(err) ) ) ; - } + + MD5 crypt ; std::size_t count = 0 ; while ( (count = file.Read( buf, sizeof(buf) )) > 0 ) - gcry_md_write( hd, buf, count ) ; + crypt.Write( buf, count ) ; - unsigned char *md5 = gcry_md_read( hd, GCRY_MD_MD5 ) ; - unsigned int len = gcry_md_get_algo_dlen(GCRY_MD_MD5) ; - - // format the MD5 string - std::ostringstream ss ; - for ( unsigned int i = 0 ; i < len ; i++ ) - ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(md5[i]) ; - - gcry_md_close( hd ) ; - - return ss.str() ; + return crypt.Get() ; } } } // end of namespaces diff --git a/libgrive/src/util/Crypt.hh b/libgrive/src/util/Crypt.hh index 38862d7..6126b53 100644 --- a/libgrive/src/util/Crypt.hh +++ b/libgrive/src/util/Crypt.hh @@ -20,6 +20,7 @@ #pragma once #include +#include #include @@ -27,10 +28,23 @@ namespace gr { class StdioFile ; -namespace crypt -{ - std::string MD5( StdioFile& file ) ; - std::string MD5( const boost::filesystem::path& file ) ; -} +namespace crypt { -} // end of namespace gr +class MD5 +{ +public : + MD5() ; + ~MD5() ; + + static std::string Get( StdioFile& file ) ; + static std::string Get( const boost::filesystem::path& file ) ; + + void Write( const void *data, std::size_t size ) ; + std::string Get() const ; + +private : + struct Impl ; + std::auto_ptr m_impl ; +} ; + +} } // end of namespace gr