summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/hashes.cc40
-rw-r--r--apt-pkg/contrib/hashes.h5
-rw-r--r--apt-pkg/contrib/hashsum.cc22
-rw-r--r--apt-pkg/contrib/hashsum_template.h3
4 files changed, 65 insertions, 5 deletions
diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc
index 05001f042..e1a431823 100644
--- a/apt-pkg/contrib/hashes.cc
+++ b/apt-pkg/contrib/hashes.cc
@@ -61,25 +61,25 @@ bool HashString::VerifyFile(std::string filename) const /*{{{*/
if(Type == "MD5Sum")
{
MD5Summation MD5;
- MD5.AddFD(Fd.Fd(), Fd.Size());
+ MD5.AddFD(Fd);
fileHash = (std::string)MD5.Result();
}
else if (Type == "SHA1")
{
SHA1Summation SHA1;
- SHA1.AddFD(Fd.Fd(), Fd.Size());
+ SHA1.AddFD(Fd);
fileHash = (std::string)SHA1.Result();
}
else if (Type == "SHA256")
{
SHA256Summation SHA256;
- SHA256.AddFD(Fd.Fd(), Fd.Size());
+ SHA256.AddFD(Fd);
fileHash = (std::string)SHA256.Result();
}
else if (Type == "SHA512")
{
SHA512Summation SHA512;
- SHA512.AddFD(Fd.Fd(), Fd.Size());
+ SHA512.AddFD(Fd);
fileHash = (std::string)SHA512.Result();
}
Fd.Close();
@@ -135,5 +135,35 @@ bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
}
return true;
}
+bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5,
+ bool const addSHA1, bool const addSHA256, bool const addSHA512)
+{
+ unsigned char Buf[64*64];
+ bool const ToEOF = (Size == 0);
+ while (Size != 0 || ToEOF)
+ {
+ unsigned long long n = sizeof(Buf);
+ if (!ToEOF) n = std::min(Size, n);
+ unsigned long long a = 0;
+ if (Fd.Read(Buf, n, &a) == false) // error
+ return false;
+ if (ToEOF == false)
+ {
+ if (a != n) // short read
+ return false;
+ }
+ else if (a == 0) // EOF
+ break;
+ Size -= a;
+ if (addMD5 == true)
+ MD5.Add(Buf, a);
+ if (addSHA1 == true)
+ SHA1.Add(Buf, a);
+ if (addSHA256 == true)
+ SHA256.Add(Buf, a);
+ if (addSHA512 == true)
+ SHA512.Add(Buf, a);
+ }
+ return true;
+}
/*}}}*/
-
diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h
index b206eccb8..0c0b6c6a7 100644
--- a/apt-pkg/contrib/hashes.h
+++ b/apt-pkg/contrib/hashes.h
@@ -17,6 +17,7 @@
#include <apt-pkg/md5.h>
#include <apt-pkg/sha1.h>
#include <apt-pkg/sha2.h>
+#include <apt-pkg/fileutl.h>
#include <algorithm>
#include <vector>
@@ -74,6 +75,10 @@ class Hashes
{ return AddFD(Fd, Size, true, true, true, true); };
bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
bool const addSHA1, bool const addSHA256, bool const addSHA512);
+ inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
+ { return AddFD(Fd, Size, true, true, true, true); };
+ bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
+ bool const addSHA1, bool const addSHA256, bool const addSHA512);
inline bool Add(const unsigned char *Beg,const unsigned char *End)
{return Add(Beg,End-Beg);};
};
diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc
index ff3b112bb..289e43aa4 100644
--- a/apt-pkg/contrib/hashsum.cc
+++ b/apt-pkg/contrib/hashsum.cc
@@ -25,4 +25,26 @@ bool SummationImplementation::AddFD(int const Fd, unsigned long long Size) {
}
return true;
}
+bool SummationImplementation::AddFD(FileFd &Fd, unsigned long long Size) {
+ unsigned char Buf[64 * 64];
+ bool ToEOF = (Size == 0);
+ while (Size != 0 || ToEOF)
+ {
+ unsigned long long n = sizeof(Buf);
+ if (!ToEOF) n = std::min(Size, n);
+ unsigned long long a = 0;
+ if (Fd.Read(Buf, n, &a) == false) // error
+ return false;
+ if (ToEOF == false)
+ {
+ if (a != n) // short read
+ return false;
+ }
+ else if (a == 0) // EOF
+ break;
+ Size -= a;
+ Add(Buf, a);
+ }
+ return true;
+}
/*}}}*/
diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h
index 6301ac9d0..51e3b0862 100644
--- a/apt-pkg/contrib/hashsum_template.h
+++ b/apt-pkg/contrib/hashsum_template.h
@@ -10,6 +10,8 @@
#ifndef APTPKG_HASHSUM_TEMPLATE_H
#define APTPKG_HASHSUM_TEMPLATE_H
+#include <apt-pkg/fileutl.h>
+
#include <string>
#include <cstring>
#include <algorithm>
@@ -108,6 +110,7 @@ class SummationImplementation
{ return Add((const unsigned char *)Beg, End - Beg); };
bool AddFD(int Fd, unsigned long long Size = 0);
+ bool AddFD(FileFd &Fd, unsigned long long Size = 0);
};
#endif