X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/4f51fd8636592a96aecf17c8bf4cfdb3ea2207cc..f378b41f9ab2493bcbc5892d482b18826b0b84c0:/apt-pkg/contrib/hashes.cc diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 46cf0ba08..662c2bf8b 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -129,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; @@ -151,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::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; @@ -276,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) @@ -372,19 +404,8 @@ APT_IGNORE_DEPRECATED_POP 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