removed OpenSSL in Download.cc

pull/40/head
Nestal Wan 2012-06-10 15:36:37 +08:00
parent b34e41c3a5
commit 89e07bb257
6 changed files with 82 additions and 68 deletions

View File

@ -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() ;
}

View File

@ -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 ) ;

View File

@ -21,6 +21,7 @@
// #include "util/SignalHandler.hh"
#include "Error.hh"
#include "util/Crypt.hh"
// boost headers
#include <boost/throw_exception.hpp>
@ -32,8 +33,6 @@
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
#include <openssl/evp.h>
#include <cassert>
#include <new>
@ -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<unsigned char*>(&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 ) ;
}

View File

@ -24,9 +24,14 @@
#include <string>
#include <openssl/evp.h>
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<crypt::MD5> m_crypt ;
} ;
} } // end of namespace

View File

@ -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<int>(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<int>(md5[i]) ;
gcry_md_close( hd ) ;
return ss.str() ;
return crypt.Get() ;
}
} } // end of namespaces

View File

@ -20,6 +20,7 @@
#pragma once
#include <string>
#include <memory>
#include <boost/filesystem.hpp>
@ -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<Impl> m_impl ;
} ;
} } // end of namespace gr