add a simple container for HashStrings
authorDavid Kalnischkies <kalnischkies@gmail.com>
Sun, 18 Aug 2013 21:17:05 +0000 (23:17 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Mon, 10 Nov 2014 16:23:29 +0000 (17:23 +0100)
APT supports more than just one HashString and even allows to enforce
the usage of a specific hash. This class is intended to help with
storage and passing around of the HashStrings.

The cherry-pick here the un-const-ification of HashType() compared to
f4c3850ea335545e297504941dc8c7a8f1c83358. The point of this commit is
adding infrastructure for the next one. All by itself, it just adds new
symbols.

Git-Dch: Ignore

apt-pkg/contrib/hashes.cc
apt-pkg/contrib/hashes.h
debian/libapt-pkg4.12.symbols
test/libapt/hashsums_test.cc

index 15f8361..bb11a3f 100644 (file)
@@ -27,7 +27,7 @@
 #include <iostream>
                                                                        /*}}}*/
 
-const char* HashString::_SupportedHashes[] = 
+const char * HashString::_SupportedHashes[] =
 {
    "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
 };
@@ -42,11 +42,16 @@ HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Ha
 
 HashString::HashString(std::string StringedHash)                       /*{{{*/
 {
-   // legacy: md5sum without "MD5Sum:" prefix
-   if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32)
+   if (StringedHash.find(":") == std::string::npos)
    {
-      Type = "MD5Sum";
-      Hash = StringedHash;
+      // legacy: md5sum without "MD5Sum:" prefix
+      if (StringedHash.size() == 32)
+      {
+        Type = "MD5Sum";
+        Hash = StringedHash;
+      }
+      if(_config->FindB("Debug::Hashes",false) == true)
+        std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
       return;
    }
    std::string::size_type pos = StringedHash.find(":");
@@ -82,25 +87,25 @@ std::string HashString::GetHashForFile(std::string filename) const      /*{{{*/
    std::string fileHash;
 
    FileFd Fd(filename, FileFd::ReadOnly);
-   if(Type == "MD5Sum")
+   if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
    {
       MD5Summation MD5;
       MD5.AddFD(Fd);
       fileHash = (std::string)MD5.Result();
    }
-   else if (Type == "SHA1")
+   else if (strcasecmp(Type.c_str(), "SHA1") == 0)
    {
       SHA1Summation SHA1;
       SHA1.AddFD(Fd);
       fileHash = (std::string)SHA1.Result();
    }
-   else if (Type == "SHA256")
+   else if (strcasecmp(Type.c_str(), "SHA256") == 0)
    {
       SHA256Summation SHA256;
       SHA256.AddFD(Fd);
       fileHash = (std::string)SHA256.Result();
    }
-   else if (Type == "SHA512")
+   else if (strcasecmp(Type.c_str(), "SHA512") == 0)
    {
       SHA512Summation SHA512;
       SHA512.AddFD(Fd);
@@ -111,20 +116,105 @@ std::string HashString::GetHashForFile(std::string filename) const      /*{{{*/
    return fileHash;
 }
                                                                        /*}}}*/
-const char** HashString::SupportedHashes()
+const char** HashString::SupportedHashes()                             /*{{{*/
 {
    return _SupportedHashes;
 }
-
-APT_PURE bool HashString::empty() const
+                                                                       /*}}}*/
+APT_PURE bool HashString::empty() const                                        /*{{{*/
 {
    return (Type.empty() || Hash.empty());
 }
+                                                                       /*}}}*/
+std::string HashString::toStr() const                                  /*{{{*/
+{
+   return Type + ":" + Hash;
+}
+                                                                       /*}}}*/
+APT_PURE bool HashString::operator==(HashString const &other) const    /*{{{*/
+{
+   return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
+}
+APT_PURE bool HashString::operator!=(HashString const &other) const
+{
+   return !(*this == other);
+}
+                                                                       /*}}}*/
+
+HashString const * HashStringList::find(char const * const type) const /*{{{*/
+{
+   if (type == NULL || type[0] == '\0')
+   {
+      std::string forcedType = _config->Find("Acquire::ForceHash", "");
+      if (forcedType.empty() == false)
+        return find(forcedType.c_str());
+      for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
+        for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
+           if (strcasecmp(hs->HashType().c_str(), *t) == 0)
+              return &*hs;
+      return NULL;
+   }
+   for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
+      if (strcasecmp(hs->HashType().c_str(), type) == 0)
+        return &*hs;
+   return NULL;
+}
+                                                                       /*}}}*/
+bool HashStringList::supported(char const * const type)                        /*{{{*/
+{
+   for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
+      if (strcasecmp(*t, type) == 0)
+        return true;
+   return false;
+}
+                                                                       /*}}}*/
+bool HashStringList::push_back(const HashString &hashString)           /*{{{*/
+{
+   if (hashString.HashType().empty() == true ||
+        hashString.HashValue().empty() == true ||
+        supported(hashString.HashType().c_str()) == false)
+      return false;
+
+   // ensure that each type is added only once
+   HashString const * const hs = find(hashString.HashType().c_str());
+   if (hs != NULL)
+      return *hs == hashString;
 
-std::string HashString::toStr() const
+   list.push_back(hashString);
+   return true;
+}
+                                                                       /*}}}*/
+bool HashStringList::VerifyFile(std::string filename) const            /*{{{*/
 {
-   return Type + std::string(":") + Hash;
+   if (list.empty() == true)
+      return false;
+   HashString const * const hs = find(NULL);
+   if (hs == NULL || hs->VerifyFile(filename) == false)
+      return false;
+   return true;
 }
+                                                                       /*}}}*/
+bool HashStringList::operator==(HashStringList const &other) const     /*{{{*/
+{
+   short matches = 0;
+   for (const_iterator hs = begin(); hs != end(); ++hs)
+   {
+      HashString const * const ohs = other.find(hs->HashType());
+      if (ohs == NULL)
+        continue;
+      if (*hs != *ohs)
+        return false;
+      ++matches;
+   }
+   if (matches == 0)
+      return false;
+   return true;
+}
+bool HashStringList::operator!=(HashStringList const &other) const
+{
+   return !(*this == other);
+}
+                                                                       /*}}}*/
 
 // Hashes::AddFD - Add the contents of the FD                          /*{{{*/
 // ---------------------------------------------------------------------
index 7a62f8a..5a42138 100644 (file)
@@ -17,6 +17,7 @@
 #include <apt-pkg/md5.h>
 #include <apt-pkg/sha1.h>
 #include <apt-pkg/sha2.h>
+#include <apt-pkg/macros.h>
 
 #include <cstring>
 #include <string>
@@ -41,7 +42,7 @@ class HashString
  protected:
    std::string Type;
    std::string Hash;
-   static const char* _SupportedHashes[10];
+   static const char * _SupportedHashes[10];
 
    // internal helper
    std::string GetHashForFile(std::string filename) const;
@@ -53,6 +54,8 @@ class HashString
 
    // get hash type used
    std::string HashType() { return Type; };
+   std::string HashType() const { return Type; };
+   std::string HashValue() const { return Hash; };
 
    // verify the given filename against the currently loaded hash
    bool VerifyFile(std::string filename) const;
@@ -64,11 +67,90 @@ class HashString
    // helper
    std::string toStr() const;                    // convert to str as "type:hash"
    bool empty() const;
+   bool operator==(HashString const &other) const;
+   bool operator!=(HashString const &other) const;
 
    // return the list of hashes we support
    static APT_CONST const char** SupportedHashes();
 };
 
+class HashStringList
+{
+   public:
+   /** find best hash if no specific one is requested
+    *
+    * @param type of the checksum to return, can be \b NULL
+    * @return If type is \b NULL (or the empty string) it will
+    *  return the 'best' hash; otherwise the hash which was
+    *  specifically requested. If no hash is found \b NULL will be returned.
+    */
+   HashString const * find(char const * const type) const;
+   HashString const * find(std::string const &type) const { return find(type.c_str()); }
+   /** check if the given hash type is supported
+    *
+    * @param type to check
+    * @return true if supported, otherwise false
+    */
+   static APT_PURE bool supported(char const * const type);
+   /** add the given #HashString to the list
+    *
+    * @param hashString to add
+    * @return true if the hash is added because it is supported and
+    *  not already a different hash of the same type included, otherwise false
+    */
+   bool push_back(const HashString &hashString);
+   /** @return size of the list of HashStrings */
+   size_t size() const { return list.size(); }
+
+   /** take the 'best' hash and verify file with it
+    *
+    * @param filename to verify
+    * @return true if the file matches the hashsum, otherwise false
+    */
+   bool VerifyFile(std::string filename) const;
+
+   /** is the list empty ?
+    *
+    * @return \b true if the list is empty, otherwise \b false
+    */
+   bool empty() const { return list.empty(); }
+
+   typedef std::vector<HashString>::const_iterator const_iterator;
+
+   /** iterator to the first element */
+   const_iterator begin() const { return list.begin(); }
+
+   /** iterator to the end element */
+   const_iterator end() const { return list.end(); }
+
+   /** start fresh with a clear list */
+   void clear() { list.clear(); }
+
+   /** compare two HashStringList for similarity.
+    *
+    * Two lists are similar if at least one hashtype is in both lists
+    * and the hashsum matches. All hashes are checked, if one doesn't
+    * match false is returned regardless of how many matched before.
+    */
+   bool operator==(HashStringList const &other) const;
+   bool operator!=(HashStringList const &other) const;
+
+   HashStringList() {}
+
+   // simplifying API-compatibility constructors
+   HashStringList(std::string const &hash) {
+      if (hash.empty() == false)
+        list.push_back(HashString(hash));
+   }
+   HashStringList(char const * const hash) {
+      if (hash != NULL && hash[0] != '\0')
+        list.push_back(HashString(hash));
+   }
+
+   private:
+   std::vector<HashString> list;
+};
+
 class Hashes
 {
    public:
index 3fa128c..d89f07b 100644 (file)
@@ -1579,6 +1579,14 @@ libapt-pkg.so.4.12 libapt-pkg4.12 #MINVER#
  (c++)"typeinfo for debTranslationsParser@Base" 1.0.4
  (c++)"typeinfo name for debTranslationsParser@Base" 1.0.4
  (c++)"vtable for debTranslationsParser@Base" 1.0.4
+ (c++)"HashStringList::find(char const*) const@Base" 1.0.9.4
+ (c++)"HashStringList::operator==(HashStringList const&) const@Base" 1.0.9.4
+ (c++)"HashStringList::operator!=(HashStringList const&) const@Base" 1.0.9.4
+ (c++)"HashStringList::push_back(HashString const&)@Base" 1.0.9.4
+ (c++)"HashStringList::supported(char const*)@Base" 1.0.9.4
+ (c++)"HashStringList::VerifyFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) const@Base" 1.0.9.4
+ (c++)"HashString::operator==(HashString const&) const@Base" 1.0.9.4
+ (c++)"HashString::operator!=(HashString const&) const@Base" 1.0.9.4
 ### demangle strangeness - buildd report it as MISSING and as new…
  (c++)"pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<IndexTarget*, std::allocator<IndexTarget*> > const*, indexRecords*)@Base" 0.8.0
 ### gcc-4.6 artefacts
index c06d85e..ac7d415 100644 (file)
@@ -207,16 +207,56 @@ TEST(HashSumsTest, FileBased)
    }
    fd.Close();
 
-   {
-      HashString sha2("SHA256", sha256.Value());
-      EXPECT_TRUE(sha2.VerifyFile(__FILE__));
-   }
-   {
-      HashString sha2("SHA512", sha512.Value());
-      EXPECT_TRUE(sha2.VerifyFile(__FILE__));
-   }
-   {
-      HashString sha2("SHA256:" + sha256.Value());
-      EXPECT_TRUE(sha2.VerifyFile(__FILE__));
-   }
+   HashString sha2file("SHA512", sha512.Value());
+   EXPECT_TRUE(sha2file.VerifyFile(__FILE__));
+   HashString sha2wrong("SHA512", "00000000000");
+   EXPECT_FALSE(sha2wrong.VerifyFile(__FILE__));
+   EXPECT_EQ(sha2file, sha2file);
+   EXPECT_TRUE(sha2file == sha2file);
+   EXPECT_NE(sha2file, sha2wrong);
+   EXPECT_TRUE(sha2file != sha2wrong);
+
+   HashString sha2big("SHA256", sha256.Value());
+   EXPECT_TRUE(sha2big.VerifyFile(__FILE__));
+   HashString sha2small("sha256:" + sha256.Value());
+   EXPECT_TRUE(sha2small.VerifyFile(__FILE__));
+   EXPECT_EQ(sha2big, sha2small);
+   EXPECT_TRUE(sha2big == sha2small);
+   EXPECT_FALSE(sha2big != sha2small);
+
+   HashStringList hashes;
+   EXPECT_TRUE(hashes.empty());
+   EXPECT_TRUE(hashes.push_back(sha2file));
+   EXPECT_FALSE(hashes.empty());
+   EXPECT_EQ(1, hashes.size());
+
+   HashStringList wrong;
+   EXPECT_TRUE(wrong.push_back(sha2wrong));
+   EXPECT_NE(wrong, hashes);
+   EXPECT_FALSE(wrong == hashes);
+   EXPECT_TRUE(wrong != hashes);
+
+   HashStringList similar;
+   EXPECT_TRUE(similar.push_back(sha2big));
+   EXPECT_NE(similar, hashes);
+   EXPECT_FALSE(similar == hashes);
+   EXPECT_TRUE(similar != hashes);
+
+   EXPECT_TRUE(hashes.push_back(sha2big));
+   EXPECT_EQ(2, hashes.size());
+   EXPECT_TRUE(hashes.push_back(sha2small));
+   EXPECT_EQ(2, hashes.size());
+   EXPECT_FALSE(hashes.push_back(sha2wrong));
+   EXPECT_EQ(2, hashes.size());
+   EXPECT_TRUE(hashes.VerifyFile(__FILE__));
+
+   EXPECT_EQ(similar, hashes);
+   EXPECT_TRUE(similar == hashes);
+   EXPECT_FALSE(similar != hashes);
+   similar.clear();
+   EXPECT_TRUE(similar.empty());
+   EXPECT_EQ(0, similar.size());
+   EXPECT_NE(similar, hashes);
+   EXPECT_FALSE(similar == hashes);
+   EXPECT_TRUE(similar != hashes);
 }