diff options
author | David Kalnischkies <kalnischkies@gmail.com> | 2011-07-13 16:37:15 +0200 |
---|---|---|
committer | David Kalnischkies <kalnischkies@gmail.com> | 2011-07-13 16:37:15 +0200 |
commit | c31c1dded85ee1e88231a041aac7e507f2ed426c (patch) | |
tree | bcd28c9d5631d392af9c636814fd80f99a1455dc /apt-pkg | |
parent | dd5e47ff5069e3859bd76d5fc8f6887121710556 (diff) |
move implementation of checksums around by abstracting even more
Diffstat (limited to 'apt-pkg')
-rw-r--r-- | apt-pkg/contrib/hashsum.cc | 28 | ||||
-rw-r--r-- | apt-pkg/contrib/hashsum_template.h | 20 | ||||
-rw-r--r-- | apt-pkg/contrib/md5.cc | 23 | ||||
-rw-r--r-- | apt-pkg/contrib/md5.h | 16 | ||||
-rw-r--r-- | apt-pkg/contrib/sha1.cc | 23 | ||||
-rw-r--r-- | apt-pkg/contrib/sha1.h | 11 | ||||
-rw-r--r-- | apt-pkg/contrib/sha2.cc | 43 | ||||
-rw-r--r-- | apt-pkg/contrib/sha2.h | 27 | ||||
-rw-r--r-- | apt-pkg/makefile | 2 |
9 files changed, 66 insertions, 127 deletions
diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc new file mode 100644 index 000000000..b97eaf831 --- /dev/null +++ b/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; +} + /*}}}*/ diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 7667baf92..2847f3308 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/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 diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 6820d3951..65e20e9bb 100644 --- a/apt-pkg/contrib/md5.cc +++ b/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 diff --git a/apt-pkg/contrib/md5.h b/apt-pkg/contrib/md5.h index 9cc88cfbe..e76428325 100644 --- a/apt-pkg/contrib/md5.h +++ b/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(); }; diff --git a/apt-pkg/contrib/sha1.cc b/apt-pkg/contrib/sha1.cc index 9a6725ef3..4b0552102 100644 --- a/apt-pkg/contrib/sha1.cc +++ b/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; -} - /*}}}*/ diff --git a/apt-pkg/contrib/sha1.h b/apt-pkg/contrib/sha1.h index e7683fa7b..2701fc67e 100644 --- a/apt-pkg/contrib/sha1.h +++ b/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(); diff --git a/apt-pkg/contrib/sha2.cc b/apt-pkg/contrib/sha2.cc deleted file mode 100644 index 4604d3167..000000000 --- a/apt-pkg/contrib/sha2.cc +++ /dev/null @@ -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; -} - /*}}}*/ - diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h index bd5472527..386225889 100644 --- a/apt-pkg/contrib/sha2.h +++ b/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) { diff --git a/apt-pkg/makefile b/apt-pkg/makefile index b11e35250..69d6cbffd 100644 --- a/apt-pkg/makefile +++ b/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 \ |