CheckSum.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00013 #include "zypp/base/Logger.h"
00014 #include "zypp/base/String.h"
00015 
00016 #include "zypp/CheckSum.h"
00017 #include "zypp/Digest.h"
00018 
00019 using std::endl;
00020 
00022 namespace zypp
00023 { 
00024 
00025   const std::string & CheckSum::md5Type()
00026   { static std::string _type( "md5" ); return _type; }
00027 
00028   const std::string & CheckSum::shaType()
00029   { static std::string _type( "sha" ); return _type; }
00030 
00031   const std::string & CheckSum::sha1Type()
00032   { static std::string _type( "sha1" ); return _type; }
00033 
00034   const std::string & CheckSum::sha256Type()
00035   { static std::string _type( "sha256" ); return _type; }
00036 
00037 
00038   CheckSum::CheckSum()
00039   {}
00040 
00041   CheckSum::CheckSum( const std::string & type, const std::string & checksum )
00042   : _type( str::toLower( type ) )
00043   , _checksum( checksum )
00044   {
00045     switch ( checksum.size() )
00046       {
00047       case 64:
00048         if ( _type == sha256Type() )
00049           return;
00050         if ( _type.empty() || _type == shaType() )
00051           {
00052             _type = sha256Type();
00053             return;
00054           }
00055         // else: dubious
00056         break;
00057 
00058       case 40:
00059         if ( _type == sha1Type() )
00060           return;
00061         if ( _type.empty() || _type == shaType() )
00062           {
00063             _type = sha1Type();
00064             return;
00065           }
00066         // else: dubious
00067         break;
00068 
00069       case 32:
00070         if (  _type == md5Type() )
00071           return;
00072         if ( _type.empty() )
00073           {
00074             _type = md5Type();
00075             return;
00076           }
00077         // else: dubious
00078         break;
00079 
00080       case 0:
00081         return; // empty checksum is ok
00082         break;
00083 
00084       default:
00085         if ( _type.empty() )
00086           {
00087             WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
00088             return;
00089           }
00090         // else: dubious
00091         break;
00092       }
00093 
00094     // dubious
00095     WAR << "Dubious type '" << _type << "' for " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
00096   }
00097 
00098   CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
00099   {
00100     if ( input_r.good() && ! type_r.empty() )
00101       {
00102         _type = str::toLower( type_r );
00103         _checksum = Digest::digest( _type, input_r );
00104         if ( ! input_r.eof() || _checksum.empty() )
00105           {
00106             _type = _checksum = std::string();
00107           }
00108       }
00109   }
00110 
00111   std::string CheckSum::type() const
00112   { return _type; }
00113 
00114   std::string CheckSum::checksum() const
00115   { return _checksum; }
00116 
00117   bool CheckSum::empty() const
00118   { return (checksum().empty() || type().empty()); }
00119   
00120   std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
00121   {
00122     if ( obj.checksum().empty() )
00123       {
00124         return str << std::string("NoCheckSum");
00125       }
00126 
00127     return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
00128   }
00129 
00131   bool operator==( const CheckSum & lhs, const CheckSum & rhs )
00132   { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
00133 
00135   bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
00136   { return ! ( lhs == rhs ); }
00137 
00139 } // namespace zypp

Generated on Tue Sep 25 19:23:00 2007 for libzypp by  doxygen 1.5.3