- Make checksum throw if invalid at construction
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 30 Jun 2006 11:20:16 +0000 (11:20 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 30 Jun 2006 11:20:16 +0000 (11:20 +0000)
- move checksum code to a cc file. It was growing too much to be
kept in a header
- svn:ignore

zypp/CheckSum.cc [new file with mode: 0644]
zypp/CheckSum.h
zypp/Makefile.am
zypp/cache/Utils.h [new file with mode: 0644]

diff --git a/zypp/CheckSum.cc b/zypp/CheckSum.cc
new file mode 100644 (file)
index 0000000..3cb4551
--- /dev/null
@@ -0,0 +1,75 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ 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
+///////////////////////////////////////////////////////////////////
index a22eb77..b6d3994 100644 (file)
@@ -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
 ///////////////////////////////////////////////////////////////////
index bf5a4ba..5e0fe30 100644 (file)
@@ -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 (file)
index 0000000..333e3df
--- /dev/null
@@ -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 <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