X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/027010953973bc868262a110e79610880610728d..5f1b8fadbba7108ba20bd07c7479eb5e5704308e:/apt-pkg/pkgcache.cc?ds=sidebyside diff --git a/apt-pkg/pkgcache.cc b/apt-pkg/pkgcache.cc index e65e37bca..347a0954e 100644 --- a/apt-pkg/pkgcache.cc +++ b/apt-pkg/pkgcache.cc @@ -39,6 +39,7 @@ #include #include #include +#include #include /*}}}*/ @@ -57,7 +58,7 @@ pkgCache::Header::Header() /* Whenever the structures change the major version should be bumped, whenever the generator changes the minor version should be bumped. */ APT_HEADER_SET(MajorVersion, 10); - APT_HEADER_SET(MinorVersion, 2); + APT_HEADER_SET(MinorVersion, 3); APT_HEADER_SET(Dirty, false); APT_HEADER_SET(HeaderSz, sizeof(pkgCache::Header)); @@ -93,7 +94,7 @@ pkgCache::Header::Header() VerSysName = 0; Architecture = 0; SetArchitectures(0); - SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 10 * 1048)); + SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 15013)); memset(Pools,0,sizeof(Pools)); CacheFileSize = 0; @@ -173,9 +174,6 @@ bool pkgCache::ReMap(bool const &Errorchecks) HeaderP->CheckSizes(DefHeader) == false) return _error->Error(_("The package cache file is an incompatible version")); - if (Map.Size() < HeaderP->CacheFileSize) - return _error->Error(_("The package cache file is corrupted, it is too small")); - if (HeaderP->VerSysName == 0 || HeaderP->Architecture == 0 || HeaderP->GetArchitectures() == 0) return _error->Error(_("The package cache file is corrupted")); @@ -193,6 +191,13 @@ bool pkgCache::ReMap(bool const &Errorchecks) list != StrP + HeaderP->GetArchitectures()) return _error->Error(_("The package cache was built for different architectures: %s vs %s"), StrP + HeaderP->GetArchitectures(), list.c_str()); + + auto hash = CacheHash(); + if (_config->FindB("Debug::pkgCacheGen", false)) + std::clog << "Opened cache with hash " << hash << ", expecting " << HeaderP->CacheFileSize << "\n"; + if (hash != HeaderP->CacheFileSize) + return _error->Error(_("The package cache file is corrupted, it has the wrong hash")); + return true; } /*}}}*/ @@ -203,19 +208,44 @@ bool pkgCache::ReMap(bool const &Errorchecks) table (480 used items) */ map_id_t pkgCache::sHash(const string &Str) const { - unsigned long Hash = 0; + uint32_t Hash = 5381; for (string::const_iterator I = Str.begin(); I != Str.end(); ++I) - Hash = 41 * Hash + tolower_ascii(*I); + Hash = 33 * Hash + tolower_ascii(*I); return Hash % HeaderP->GetHashTableSize(); } map_id_t pkgCache::sHash(const char *Str) const { - unsigned long Hash = tolower_ascii(*Str); - for (const char *I = Str + 1; *I != 0; ++I) - Hash = 41 * Hash + tolower_ascii(*I); + uint32_t Hash = 5381; + for (const char *I = Str; *I != 0; ++I) + Hash = 33 * Hash + tolower_ascii(*I); return Hash % HeaderP->GetHashTableSize(); } + +uint32_t pkgCache::CacheHash() +{ + pkgCache::Header header = {}; + uLong adler = adler32(0L, Z_NULL, 0); + + if (Map.Size() < sizeof(header)) + return adler; + memcpy(&header, GetMap().Data(), sizeof(header)); + + header.Dirty = false; + header.CacheFileSize = 0; + + adler = adler32(adler, + reinterpret_cast(&header), + sizeof(header)); + + if (Map.Size() > sizeof(header)) { + adler = adler32(adler, + static_cast(GetMap().Data()) + sizeof(header), + GetMap().Size() - sizeof(header)); + } + + return adler; +} /*}}}*/ // Cache::FindPkg - Locate a package by name /*{{{*/ // ---------------------------------------------------------------------