From 9a961efc5cedeb2b7439114df18e8d0201c83fcf Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 22 Feb 2011 22:06:31 +0100 Subject: add sha512 support to apt-ftparchive --- ftparchive/cachedb.cc | 39 +++++++++++++++++++++++++++++++++++++-- ftparchive/cachedb.h | 20 ++++++++++++++++---- ftparchive/writer.cc | 30 +++++++++++++++++++++++++++++- ftparchive/writer.h | 2 ++ 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/ftparchive/cachedb.cc b/ftparchive/cachedb.cc index b04244347..699718e57 100644 --- a/ftparchive/cachedb.cc +++ b/ftparchive/cachedb.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -162,7 +163,8 @@ bool CacheDB::GetCurStat() // --------------------------------------------------------------------- bool CacheDB::GetFileInfo(string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly, bool const &DoMD5, bool const &DoSHA1, - bool const &DoSHA256, bool const &checkMtime) + bool const &DoSHA256, bool const &DoSHA512, + bool const &checkMtime) { this->FileName = FileName; @@ -190,7 +192,9 @@ bool CacheDB::GetFileInfo(string const &FileName, bool const &DoControl, bool co || (DoContents && LoadContents(GenContentsOnly) == false) || (DoMD5 && GetMD5(false) == false) || (DoSHA1 && GetSHA1(false) == false) - || (DoSHA256 && GetSHA256(false) == false)) + || (DoSHA256 && GetSHA256(false) == false) + || (DoSHA512 && GetSHA512(false) == false) + ) { delete Fd; Fd = NULL; @@ -412,6 +416,37 @@ bool CacheDB::GetSHA256(bool const &GenOnly) return true; } /*}}}*/ +// CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool CacheDB::GetSHA512(bool const &GenOnly) +{ + // Try to read the control information out of the DB. + if ((CurStat.Flags & FlSHA512) == FlSHA512) + { + if (GenOnly == true) + return true; + + SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512)); + return true; + } + + Stats.SHA512Bytes += CurStat.FileSize; + + if (Fd == NULL && OpenFile() == false) + { + return false; + } + SHA512Summation SHA512; + if (Fd->Seek(0) == false || SHA512.AddFD(Fd->Fd(),CurStat.FileSize) == false) + return false; + + SHA512Res = SHA512.Result(); + hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512)); + CurStat.Flags |= FlSHA512; + return true; +} + /*}}}*/ // CacheDB::Finish - Write back the cache structure /*{{{*/ // --------------------------------------------------------------------- /* */ diff --git a/ftparchive/cachedb.h b/ftparchive/cachedb.h index 0ba80909a..15e796325 100644 --- a/ftparchive/cachedb.h +++ b/ftparchive/cachedb.h @@ -70,10 +70,13 @@ class CacheDB bool GetMD5(bool const &GenOnly); bool GetSHA1(bool const &GenOnly); bool GetSHA256(bool const &GenOnly); + bool GetSHA512(bool const &GenOnly); // Stat info stored in the DB, Fixed types since it is written to disk. enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), - FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5)}; + FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), + FlSHA512=(1<<6)}; + struct StatStore { uint32_t Flags; @@ -82,6 +85,7 @@ class CacheDB uint8_t MD5[16]; uint8_t SHA1[20]; uint8_t SHA256[32]; + uint8_t SHA512[64]; } CurStat; struct StatStore OldStat; @@ -98,6 +102,7 @@ class CacheDB string MD5Res; string SHA1Res; string SHA256Res; + string SHA512Res; // Runtime statistics struct Stats @@ -106,14 +111,21 @@ class CacheDB double MD5Bytes; double SHA1Bytes; double SHA256Bytes; + double SHA512Bytes; unsigned long Packages; unsigned long Misses; unsigned long DeLinkBytes; inline void Add(const Stats &S) { - Bytes += S.Bytes; MD5Bytes += S.MD5Bytes; SHA1Bytes += S.SHA1Bytes; + Bytes += S.Bytes; + MD5Bytes += S.MD5Bytes; + SHA1Bytes += S.SHA1Bytes; SHA256Bytes += S.SHA256Bytes; - Packages += S.Packages; Misses += S.Misses; DeLinkBytes += S.DeLinkBytes;}; + SHA512Bytes += S.SHA512Bytes; + Packages += S.Packages; + Misses += S.Misses; + DeLinkBytes += S.DeLinkBytes; + }; Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {}; } Stats; @@ -125,7 +137,7 @@ class CacheDB bool SetFile(string const &FileName,struct stat St,FileFd *Fd); bool GetFileInfo(string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly, - bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &checkMtime = false); + bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false); bool Finish(); bool Clean(); diff --git a/ftparchive/writer.cc b/ftparchive/writer.cc index 9cdca8d3e..e7eff2045 100644 --- a/ftparchive/writer.cc +++ b/ftparchive/writer.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -311,6 +312,7 @@ PackagesWriter::PackagesWriter(string const &DB,string const &Overrides,string c DoMD5 = _config->FindB("APT::FTPArchive::MD5",true); DoSHA1 = _config->FindB("APT::FTPArchive::SHA1",true); DoSHA256 = _config->FindB("APT::FTPArchive::SHA256",true); + DoSHA256 = _config->FindB("APT::FTPArchive::SHA512",true); DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false); DoContents = _config->FindB("APT::FTPArchive::Contents",true); NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false); @@ -365,7 +367,7 @@ bool FTWScanner::SetExts(string const &Vals) bool PackagesWriter::DoPackage(string FileName) { // Pull all the data we need form the DB - if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256, DoAlwaysStat) + if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256, DoSHA512, DoAlwaysStat) == false) { return false; @@ -438,6 +440,7 @@ bool PackagesWriter::DoPackage(string FileName) SetTFRewriteData(Changes[End++], "MD5sum", Db.MD5Res.c_str()); SetTFRewriteData(Changes[End++], "SHA1", Db.SHA1Res.c_str()); SetTFRewriteData(Changes[End++], "SHA256", Db.SHA256Res.c_str()); + SetTFRewriteData(Changes[End++], "SHA512", Db.SHA512Res.c_str()); SetTFRewriteData(Changes[End++], "Filename", NewFileName.c_str()); SetTFRewriteData(Changes[End++], "Priority", OverItem->Priority.c_str()); SetTFRewriteData(Changes[End++], "Status", 0); @@ -613,8 +616,10 @@ bool SourcesWriter::DoPackage(string FileName) SHA1Summation SHA1; SHA256Summation SHA256; + SHA512Summation SHA512; SHA1.Add((unsigned char *)Start,BlkEnd - Start); SHA256.Add((unsigned char *)Start,BlkEnd - Start); + SHA512.Add((unsigned char *)Start,BlkEnd - Start); // Add an extra \n to the end, just in case *BlkEnd++ = '\n'; @@ -725,6 +730,12 @@ bool SourcesWriter::DoPackage(string FileName) << strippedName << "\n " << Tags.FindS("Checksums-Sha256"); string const ChecksumsSha256 = ostreamSha256.str(); + std::ostringstream ostreamSha512; + if (Tags.Exists("Checksums-Sha512")) + ostreamSha512 << "\n " << string(SHA512.Result()) << " " << St.st_size << " " + << strippedName << "\n " << Tags.FindS("Checksums-Sha512"); + string const ChecksumsSha512 = ostreamSha512.str(); + // Strip the DirStrip prefix from the FileName and add the PathPrefix string NewFileName; if (DirStrip.empty() == false && @@ -777,6 +788,7 @@ bool SourcesWriter::DoPackage(string FileName) SetTFRewriteData(Changes[End++],"Files",Files.c_str()); SetTFRewriteData(Changes[End++],"Checksums-Sha1",ChecksumsSha1.c_str()); SetTFRewriteData(Changes[End++],"Checksums-Sha256",ChecksumsSha256.c_str()); + SetTFRewriteData(Changes[End++],"Checksums-Sha512",ChecksumsSha512.c_str()); if (Directory != "./") SetTFRewriteData(Changes[End++],"Directory",Directory.c_str()); SetTFRewriteData(Changes[End++],"Priority",BestPrio.c_str()); @@ -1010,6 +1022,10 @@ bool ReleaseWriter::DoPackage(string FileName) SHA256.AddFD(fd.Fd(), fd.Size()); CheckSums[NewFileName].SHA256 = SHA256.Result(); + SHA256Summation SHA512; + SHA256.AddFD(fd.Fd(), fd.Size()); + CheckSums[NewFileName].SHA512 = SHA512.Result(); + fd.Close(); return true; @@ -1052,5 +1068,17 @@ void ReleaseWriter::Finish() (*I).second.size, (*I).first.c_str()); } + + fprintf(Output, "SHA512:\n"); + for(map::const_iterator I = CheckSums.begin(); + I != CheckSums.end(); + ++I) + { + fprintf(Output, " %s %32ld %s\n", + (*I).second.SHA512.c_str(), + (*I).second.size, + (*I).first.c_str()); + } + } diff --git a/ftparchive/writer.h b/ftparchive/writer.h index 3796f79f6..e1810821a 100644 --- a/ftparchive/writer.h +++ b/ftparchive/writer.h @@ -106,6 +106,7 @@ class PackagesWriter : public FTWScanner bool DoMD5; bool DoSHA1; bool DoSHA256; + bool DoSHA512; bool DoAlwaysStat; bool NoOverride; bool DoContents; @@ -195,6 +196,7 @@ protected: string MD5; string SHA1; string SHA256; + string SHA512; // Limited by FileFd::Size() unsigned long size; ~CheckSum() {}; -- cgit v1.2.3