From 9ba3baf69394c6f6bc16861eca3e5763a9f51cfc Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 30 Jun 2006 11:20:16 +0000 Subject: [PATCH] - Make checksum throw if invalid at construction - move checksum code to a cc file. It was growing too much to be kept in a header - svn:ignore --- zypp/CheckSum.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ zypp/CheckSum.h | 45 +++++++++----------------------- zypp/Makefile.am | 1 + zypp/cache/Utils.h | 33 ++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 33 deletions(-) create mode 100644 zypp/CheckSum.cc create mode 100644 zypp/cache/Utils.h diff --git a/zypp/CheckSum.cc b/zypp/CheckSum.cc new file mode 100644 index 0000000..3cb4551 --- /dev/null +++ b/zypp/CheckSum.cc @@ -0,0 +1,75 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ + +#include +#include + +#include "zypp/base/Exception.h" +#include "zypp/CheckSum.h" + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + + CheckSum::CheckSum(const std::string & type, const std::string & checksum) + { + _checksum = checksum; + + // correct ambiguous algorithm + if (str::toLower(type) == "sha") + { + if (checksum.size() == 40) + _type = "sha1"; + else if (checksum.size() == 64) + _type = "sha256"; + else + ZYPP_THROW(Exception("Bad " + type + " algorithm of size " + str::numstring(checksum.size()) + " specified. Can't determine if it is sha1 or sha256 from size.")); + } + else + { + _type = type; + } + + if ( + ( (str::toLower(type) == "md5" ) && (checksum.size() != 32) ) || + ( (str::toLower(type) == "sha1" ) && (checksum.size() != 40) ) || + ( (str::toLower(type) == "sha256") && (checksum.size() != 64) ) ) + { + ZYPP_THROW(Exception("Bad checksum, " + type + " algorithm of size " + str::numstring(checksum.size()))); + } + + } + + CheckSum::CheckSum() + {} + + std::string CheckSum::type() const + { return _type; } + + std::string CheckSum::checksum() const + { return _checksum; } + + bool CheckSum::empty() const + { return (checksum().empty() || type().empty()); } + + /** \relates CheckSum Stream output. */ + inline std::ostream & operator<<( std::ostream & str, const CheckSum & obj ) + { return str << (obj.empty() ? std::string("NoCheckSum") + : obj.type()+"-"+obj.checksum() ); } + + /** \relates CheckSum */ + inline bool operator==( const CheckSum & lhs, const CheckSum & rhs ) + { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); } + + /** \relates CheckSum */ + inline bool operator!=( const CheckSum & lhs, const CheckSum & rhs ) + { return ! ( lhs == rhs ); } + +} // namespace zypp +/////////////////////////////////////////////////////////////////// diff --git a/zypp/CheckSum.h b/zypp/CheckSum.h index a22eb77..b6d3994 100644 --- a/zypp/CheckSum.h +++ b/zypp/CheckSum.h @@ -24,51 +24,30 @@ namespace zypp class CheckSum { public: - CheckSum(const std::string & type, const std::string & checksum) - { - _checksum = checksum; - if (str::toLower(type) == "sha") - { - if (checksum.size() == 40) - _type = "sha1"; - else if (checksum.size() == 64) - _type = "sha256"; - } - else - { - _type = type; - } - } + /** + * Creates a checksum for algorithm \param type + * \throws if the checksum is invalid and can't be constructed + */ + CheckSum(const std::string & type, const std::string & checksum); + CheckSum(); - CheckSum() - {} + std::string type() const; + std::string checksum() const; - std::string type() const - { return _type; } - - std::string checksum() const - { return _checksum; } - - bool empty() const - { return (checksum().empty() || type().empty()); } - + bool empty() const; private: std::string _type; std::string _checksum; }; /** \relates CheckSum Stream output. */ - inline std::ostream & operator<<( std::ostream & str, const CheckSum & obj ) - { return str << (obj.empty() ? std::string("NoCheckSum") - : obj.type()+"-"+obj.checksum() ); } + inline std::ostream & operator<<( std::ostream & str, const CheckSum & obj ); /** \relates CheckSum */ - inline bool operator==( const CheckSum & lhs, const CheckSum & rhs ) - { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); } + inline bool operator==( const CheckSum & lhs, const CheckSum & rhs ); /** \relates CheckSum */ - inline bool operator!=( const CheckSum & lhs, const CheckSum & rhs ) - { return ! ( lhs == rhs ); } + inline bool operator!=( const CheckSum & lhs, const CheckSum & rhs ); } // namespace zypp /////////////////////////////////////////////////////////////////// diff --git a/zypp/Makefile.am b/zypp/Makefile.am index bf5a4ba..5e0fe30 100644 --- a/zypp/Makefile.am +++ b/zypp/Makefile.am @@ -105,6 +105,7 @@ lib@PACKAGE@_la_SOURCES = \ CapFactory.cc \ CapMatch.cc \ CapSet.cc \ + CheckSum.cc \ CountryCode.cc \ Date.cc \ Dep.cc \ diff --git a/zypp/cache/Utils.h b/zypp/cache/Utils.h new file mode 100644 index 0000000..333e3df --- /dev/null +++ b/zypp/cache/Utils.h @@ -0,0 +1,33 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ + +#ifndef ZYPP_DATA_UTILS_H +#define ZYPP_DATA_UTILS_H + +#include "zypp/base/Logger.h" +#include "zypp/base/String.h" +#include "zypp/CheckSum.h" +#include + +////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////// +namespace cache +{ ///////////////////////////////////////////////////////////////// + + int tribool_to_int( boost::tribool b ); + boost::tribool int_to_tribool( int i ); + std::string checksum_to_string( const CheckSum &checksum ); + CheckSum string_to_checksum( const std::string &checksum ); + +} +} + +#endif \ No newline at end of file -- 2.7.4