--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+
+#include <iosfwd>
+#include <string>
+
+#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
+///////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////
CapFactory.cc \
CapMatch.cc \
CapSet.cc \
+ CheckSum.cc \
CountryCode.cc \
Date.cc \
Dep.cc \
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ 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 <boost/logic/tribool.hpp>
+
+//////////////////////////////////////////////////////////////////
+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