Parcourir la source

move implementation of checksums around by abstracting even more

David Kalnischkies il y a 15 ans
Parent
commit
c31c1dded8

+ 28 - 0
apt-pkg/contrib/hashsum.cc

@@ -0,0 +1,28 @@
+// Cryptographic API Base
+
+#include <unistd.h>
+#include "hashsum_template.h"
+
+// Summation::AddFD - Add content of file into the checksum		/*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool SummationImplementation::AddFD(int const Fd, unsigned long Size) {
+   unsigned char Buf[64 * 64];
+   int Res = 0;
+   int ToEOF = (Size == 0);
+   unsigned long n = sizeof(Buf);
+   if (!ToEOF)
+      n = std::min(Size, n);
+   while (Size != 0 || ToEOF)
+   {
+      Res = read(Fd, Buf, n);
+      if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
+	 return false;
+      if (ToEOF && Res == 0) // EOF
+	 break;
+      Size -= Res;
+      Add(Buf,Res);
+   }
+   return true;
+}
+									/*}}}*/

+ 20 - 0
apt-pkg/contrib/hashsum_template.h

@@ -84,4 +84,24 @@ class HashSumValue
    }
 };
 
+class SummationImplementation
+{
+   public:
+   virtual bool Add(const unsigned char *inbuf, unsigned long inlen) = 0;
+   inline bool Add(const char *inbuf, unsigned long const inlen)
+   { return Add((unsigned char *)inbuf, inlen); };
+
+   inline bool Add(const unsigned char *Data)
+   { return Add(Data, strlen((const char *)Data)); };
+   inline bool Add(const char *Data)
+   { return Add((const unsigned char *)Data, strlen((const char *)Data)); };
+
+   inline bool Add(const unsigned char *Beg, const unsigned char *End)
+   { return Add(Beg, End - Beg); };
+   inline bool Add(const char *Beg, const char *End)
+   { return Add((const unsigned char *)Beg, End - Beg); };
+
+   bool AddFD(int Fd, unsigned long Size);
+};
+
 #endif

+ 0 - 23
apt-pkg/contrib/md5.cc

@@ -231,29 +231,6 @@ bool MD5Summation::Add(const unsigned char *data,unsigned long len)
    return true;   
 }
 									/*}}}*/
-// MD5Summation::AddFD - Add the contents of a FD to the hash		/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool MD5Summation::AddFD(int Fd,unsigned long Size)
-{
-   unsigned char Buf[64*64];
-   int Res = 0;
-   int ToEOF = (Size == 0);
-   while (Size != 0 || ToEOF)
-   {
-      unsigned n = sizeof(Buf);
-      if (!ToEOF) n = min(Size,(unsigned long)n);
-      Res = read(Fd,Buf,n);
-      if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
-         return false;
-      if (ToEOF && Res == 0) // EOF
-         break;
-      Size -= Res;
-      Add(Buf,Res);
-   }
-   return true;
-}
-									/*}}}*/
 // MD5Summation::Result - Returns the value of the sum			/*{{{*/
 // ---------------------------------------------------------------------
 /* Because this must add in the last bytes of the series it prevents anyone

+ 6 - 10
apt-pkg/contrib/md5.h

@@ -34,26 +34,22 @@ using std::min;
 
 #include "hashsum_template.h"
 
-class MD5Summation;
-
 typedef HashSumValue<128> MD5SumValue;
 
-class MD5Summation
+class MD5Summation : public SummationImplementation
 {
    uint32_t Buf[4];
    unsigned char Bytes[2*4];
    unsigned char In[16*4];
    bool Done;
-   
+
    public:
 
-   bool Add(const unsigned char *Data,unsigned long Size);
-   inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
-   bool AddFD(int Fd,unsigned long Size);
-   inline bool Add(const unsigned char *Beg,const unsigned char *End) 
-                  {return Add(Beg,End-Beg);};
+   bool Add(const unsigned char *inbuf, unsigned long inlen);
+   using SummationImplementation::Add;
+
    MD5SumValue Result();
-   
+
    MD5Summation();
 };
 

+ 0 - 23
apt-pkg/contrib/sha1.cc

@@ -273,26 +273,3 @@ bool SHA1Summation::Add(const unsigned char *data,unsigned long len)
    return true;
 }
 									/*}}}*/
-// SHA1Summation::AddFD - Add content of file into the checksum         /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool SHA1Summation::AddFD(int Fd,unsigned long Size)
-{
-   unsigned char Buf[64 * 64];
-   int Res = 0;
-   int ToEOF = (Size == 0);
-   while (Size != 0 || ToEOF)
-   {
-      unsigned n = sizeof(Buf);
-      if (!ToEOF) n = min(Size,(unsigned long)n);
-      Res = read(Fd,Buf,n);
-      if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
-	 return false;
-      if (ToEOF && Res == 0) // EOF
-         break;
-      Size -= Res;
-      Add(Buf,Res);
-   }
-   return true;
-}
-									/*}}}*/

+ 3 - 8
apt-pkg/contrib/sha1.h

@@ -23,11 +23,9 @@ using std::min;
 
 #include "hashsum_template.h"
 
-class SHA1Summation;
-
 typedef  HashSumValue<160> SHA1SumValue;
 
-class SHA1Summation
+class SHA1Summation : public SummationImplementation
 {
    /* assumes 64-bit alignment just in case */
    unsigned char Buffer[64] __attribute__((aligned(8)));
@@ -36,12 +34,9 @@ class SHA1Summation
    bool Done;
    
    public:
+   bool Add(const unsigned char *inbuf, unsigned long inlen);
+   using SummationImplementation::Add;
 
-   bool Add(const unsigned char *inbuf,unsigned long inlen);
-   inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
-   bool AddFD(int Fd,unsigned long Size);
-   inline bool Add(const unsigned char *Beg,const unsigned char *End) 
-                  {return Add(Beg,End-Beg);};
    SHA1SumValue Result();
    
    SHA1Summation();

+ 0 - 43
apt-pkg/contrib/sha2.cc

@@ -1,43 +0,0 @@
-/*
- * Cryptographic API.							{{{
- *
- * SHA-512, as specified in
- * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option) 
- * any later version.
- *
- */									/*}}}*/
-
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/sha2.h"
-#endif
-
-#include <apt-pkg/sha2.h>
-#include <apt-pkg/strutl.h>
-
-// SHA2Summation::AddFD - Add content of file into the checksum      /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool SHA2SummationBase::AddFD(int Fd,unsigned long Size){
-   unsigned char Buf[64 * 64];
-   int Res = 0;
-   int ToEOF = (Size == 0);
-   while (Size != 0 || ToEOF)
-   {
-      unsigned n = sizeof(Buf);
-      if (!ToEOF) n = min(Size,(unsigned long)n);
-      Res = read(Fd,Buf,n);
-      if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
-         return false;
-      if (ToEOF && Res == 0) // EOF
-         break;
-      Size -= Res;
-      Add(Buf,Res);
-   }
-   return true;
-}
-                                                                       /*}}}*/
-

+ 8 - 19
apt-pkg/contrib/sha2.h

@@ -22,31 +22,16 @@
 #include "sha2_internal.h"
 #include "hashsum_template.h"
 
-using std::string;
-using std::min;
-
-class SHA512Summation;
-class SHA256Summation;
-
 typedef HashSumValue<512> SHA512SumValue;
 typedef HashSumValue<256> SHA256SumValue;
 
-class SHA2SummationBase
+class SHA2SummationBase : public SummationImplementation
 {
  protected:
    bool Done;
  public:
-   virtual bool Add(const unsigned char *inbuf,unsigned long inlen) = 0;
-   virtual bool AddFD(int Fd,unsigned long Size);
+   bool Add(const unsigned char *inbuf, unsigned long len) = 0;
 
-   inline bool Add(const char *Data) 
-   {
-      return Add((unsigned char *)Data,strlen(Data));
-   };
-   inline bool Add(const unsigned char *Beg,const unsigned char *End) 
-   {
-      return Add(Beg,End-Beg);
-   };
    void Result();
 };
 
@@ -56,13 +41,15 @@ class SHA256Summation : public SHA2SummationBase
    unsigned char Sum[32];
 
    public:
-   virtual bool Add(const unsigned char *inbuf, unsigned long len)
+   bool Add(const unsigned char *inbuf, unsigned long len)
    {
       if (Done) 
          return false;
       SHA256_Update(&ctx, inbuf, len);
       return true;
    };
+   using SummationImplementation::Add;
+
    SHA256SumValue Result()
    {
       if (!Done) {
@@ -86,13 +73,15 @@ class SHA512Summation : public SHA2SummationBase
    unsigned char Sum[64];
 
    public:
-   virtual bool Add(const unsigned char *inbuf, unsigned long len)
+   bool Add(const unsigned char *inbuf, unsigned long len)
    {
       if (Done) 
          return false;
       SHA512_Update(&ctx, inbuf, len);
       return true;
    };
+   using SummationImplementation::Add;
+
    SHA512SumValue Result()
    {
       if (!Done) {

+ 1 - 1
apt-pkg/makefile

@@ -20,7 +20,7 @@ APT_DOMAIN:=libapt-pkg$(LIBAPTPKG_MAJOR)
 # Source code for the contributed non-core things
 SOURCE = contrib/mmap.cc contrib/error.cc contrib/strutl.cc \
          contrib/configuration.cc contrib/progress.cc contrib/cmndline.cc \
-	 contrib/md5.cc contrib/sha1.cc contrib/sha2.cc  \
+	 contrib/hashsum.cc contrib/md5.cc contrib/sha1.cc \
 	 contrib/sha2_internal.cc\
          contrib/hashes.cc \
 	 contrib/cdromutl.cc contrib/crc-16.cc contrib/netrc.cc \