]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/hashes.cc
The entire concept of PendingError() is flawed :/.
[apt.git] / apt-pkg / contrib / hashes.cc
index 953465091e4f9a593ff9c2362a3e41707b7e03d0..27e6177511d824331fb3ba9b50645eaa202afba8 100644 (file)
@@ -23,6 +23,7 @@
 #include <stddef.h>
 #include <algorithm>
 #include <unistd.h>
+#include <stdlib.h>
 #include <string>
 #include <iostream>
                                                                        /*}}}*/
@@ -128,6 +129,24 @@ APT_PURE bool HashString::empty() const                                    /*{{{*/
    return (Type.empty() || Hash.empty());
 }
                                                                        /*}}}*/
+
+APT_PURE static bool IsConfigured(const char *name, const char *what)
+{
+   std::string option;
+   strprintf(option, "APT::Hashes::%s::%s", name, what);
+   return _config->FindB(option, false);
+}
+
+APT_PURE bool HashString::usable() const                               /*{{{*/
+{
+   return (
+      (Type != "Checksum-FileSize") &&
+      //(Type != "MD5Sum") &&
+      //(Type != "SHA1") &&
+      !IsConfigured(Type.c_str(), "Untrusted")
+   );
+}
+                                                                       /*}}}*/
 std::string HashString::toStr() const                                  /*{{{*/
 {
    return Type + ":" + Hash;
@@ -150,10 +169,10 @@ bool HashStringList::usable() const                                       /*{{{*/
    std::string const forcedType = _config->Find("Acquire::ForceHash", "");
    if (forcedType.empty() == true)
    {
-      // FileSize alone isn't usable
-      for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
-        if (hs->HashType() != "Checksum-FileSize")
-           return true;
+      // See if there is at least one usable hash
+      for (auto const &hs: list)
+         if (hs.usable())
+            return true;
       return false;
    }
    return find(forcedType) != NULL;
@@ -178,6 +197,22 @@ HashString const * HashStringList::find(char const * const type) const /*{{{*/
    return NULL;
 }
                                                                        /*}}}*/
+unsigned long long HashStringList::FileSize() const                    /*{{{*/
+{
+   HashString const * const hsf = find("Checksum-FileSize");
+   if (hsf == NULL)
+      return 0;
+   std::string const hv = hsf->HashValue();
+   return strtoull(hv.c_str(), NULL, 10);
+}
+                                                                       /*}}}*/
+bool HashStringList::FileSize(unsigned long long const Size)           /*{{{*/
+{
+   std::string size;
+   strprintf(size, "%llu", Size);
+   return push_back(HashString("Checksum-FileSize", size));
+}
+                                                                       /*}}}*/
 bool HashStringList::supported(char const * const type)                        /*{{{*/
 {
    for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
@@ -204,15 +239,22 @@ bool HashStringList::push_back(const HashString &hashString)              /*{{{*/
                                                                        /*}}}*/
 bool HashStringList::VerifyFile(std::string filename) const            /*{{{*/
 {
-   if (list.empty() == true)
-      return false;
-   HashString const * const hs = find(NULL);
-   if (hs == NULL || hs->VerifyFile(filename) == false)
+   if (usable() == false)
       return false;
+
+   Hashes hashes(*this);
+   FileFd file(filename, FileFd::ReadOnly);
    HashString const * const hsf = find("Checksum-FileSize");
-   if (hsf != NULL && hsf->VerifyFile(filename) == false)
-      return false;
-   return true;
+   if (hsf != NULL)
+   {
+      std::string fileSize;
+      strprintf(fileSize, "%llu", file.FileSize());
+      if (hsf->HashValue() != fileSize)
+        return false;
+   }
+   hashes.AddFD(file);
+   HashStringList const hsl = hashes.GetHashStringList();
+   return hsl == *this;
 }
                                                                        /*}}}*/
 bool HashStringList::operator==(HashStringList const &other) const     /*{{{*/
@@ -252,12 +294,26 @@ public:
    unsigned long long FileSize;
    unsigned int CalcHashes;
 
-   PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
+   explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
+   explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) {
+      unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
+      if (Hashes.find("MD5Sum") != NULL)
+        calcHashes |= Hashes::MD5SUM;
+      if (Hashes.find("SHA1") != NULL)
+        calcHashes |= Hashes::SHA1SUM;
+      if (Hashes.find("SHA256") != NULL)
+        calcHashes |= Hashes::SHA256SUM;
+      if (Hashes.find("SHA512") != NULL)
+        calcHashes |= Hashes::SHA512SUM;
+      CalcHashes = calcHashes;
+   }
 };
                                                                        /*}}}*/
 // Hashes::Add* - Add the contents of data or FD                       /*{{{*/
 bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
 {
+   if (Size == 0)
+      return true;
    bool Res = true;
 APT_IGNORE_DEPRECATED_PUSH
    if ((d->CalcHashes & MD5SUM) == MD5SUM)
@@ -344,25 +400,12 @@ APT_IGNORE_DEPRECATED_PUSH
    if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
       hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
 APT_IGNORE_DEPRECATED_POP
-   std::string SizeStr;
-   strprintf(SizeStr, "%llu", d->FileSize);
-   hashes.push_back(HashString("Checksum-FileSize", SizeStr));
+   hashes.FileSize(d->FileSize);
    return hashes;
 }
 APT_IGNORE_DEPRECATED_PUSH
-Hashes::Hashes() { d = new PrivateHashes(~0); }
-Hashes::Hashes(unsigned int const Hashes) { d = new PrivateHashes(Hashes); }
-Hashes::Hashes(HashStringList const &Hashes) {
-   unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
-   if (Hashes.find("MD5Sum") != NULL)
-      calcHashes |= MD5SUM;
-   if (Hashes.find("SHA1") != NULL)
-      calcHashes |= SHA1SUM;
-   if (Hashes.find("SHA256") != NULL)
-      calcHashes |= SHA256SUM;
-   if (Hashes.find("SHA512") != NULL)
-      calcHashes |= SHA512SUM;
-   d = new PrivateHashes(calcHashes);
-}
+Hashes::Hashes() : d(new PrivateHashes(~0)) { }
+Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {}
+Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {}
 Hashes::~Hashes() { delete d; }
 APT_IGNORE_DEPRECATED_POP