diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 18:07:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 18:07:13 +0000 |
commit | 636c7dc17286d93d788c741d15fd756aeda066d5 (patch) | |
tree | e7ae158cc54f591041a061b9865bcae51854f15c /apt-pkg/contrib/hashsum_template.h | |
parent | Initial commit. (diff) | |
download | apt-upstream.tar.xz apt-upstream.zip |
Adding upstream version 1.8.2.3.upstream/1.8.2.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'apt-pkg/contrib/hashsum_template.h')
-rw-r--r-- | apt-pkg/contrib/hashsum_template.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h new file mode 100644 index 0000000..f11fc2f --- /dev/null +++ b/apt-pkg/contrib/hashsum_template.h @@ -0,0 +1,141 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + HashSumValueTemplate - Generic Storage for a hash value + + ##################################################################### */ + /*}}}*/ +#ifndef APTPKG_HASHSUM_TEMPLATE_H +#define APTPKG_HASHSUM_TEMPLATE_H + +#include <cstring> +#include <string> +#ifdef APT_PKG_EXPOSE_STRING_VIEW +#include <apt-pkg/string_view.h> +#endif + +#include <apt-pkg/strutl.h> + +#ifndef APT_10_CLEANER_HEADERS +#include <apt-pkg/fileutl.h> +#include <algorithm> +#include <stdint.h> +#endif +#ifndef APT_8_CLEANER_HEADERS +using std::string; +using std::min; +#endif + +class FileFd; + +template<int N> +class HashSumValue +{ + unsigned char Sum[N/8]; + + public: + + // Accessors + bool operator ==(const HashSumValue &rhs) const + { + return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; + } + bool operator !=(const HashSumValue &rhs) const + { + return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0; + } + + std::string Value() const + { + char Conv[16] = + { '0','1','2','3','4','5','6','7','8','9','a','b', + 'c','d','e','f' + }; + char Result[((N/8)*2)+1]; + Result[(N/8)*2] = 0; + + // Convert each char into two letters + int J = 0; + int I = 0; + for (; I != (N/8)*2; J++,I += 2) + { + Result[I] = Conv[Sum[J] >> 4]; + Result[I + 1] = Conv[Sum[J] & 0xF]; + } + return std::string(Result); + } + + inline void Value(unsigned char S[N/8]) + { + for (int I = 0; I != sizeof(Sum); ++I) + S[I] = Sum[I]; + } + + inline operator std::string() const + { + return Value(); + } + +#ifdef APT_PKG_EXPOSE_STRING_VIEW + APT_HIDDEN bool Set(APT::StringView Str) + { + return Hex2Num(Str,Sum,sizeof(Sum)); + } +#else + bool Set(std::string Str) + { + return Hex2Num(Str,Sum,sizeof(Sum)); + } +#endif + inline void Set(unsigned char S[N/8]) + { + for (int I = 0; I != sizeof(Sum); ++I) + Sum[I] = S[I]; + } + + explicit HashSumValue(std::string const &Str) + { + memset(Sum,0,sizeof(Sum)); + Set(Str); + } +#ifdef APT_PKG_EXPOSE_STRING_VIEW + APT_HIDDEN explicit HashSumValue(APT::StringView const &Str) + { + memset(Sum,0,sizeof(Sum)); + Set(Str); + } + APT_HIDDEN explicit HashSumValue(const char *Str) + { + memset(Sum,0,sizeof(Sum)); + Set(Str); + } +#endif + HashSumValue() + { + memset(Sum,0,sizeof(Sum)); + } +}; + +class SummationImplementation +{ + public: + virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_NONNULL(2) = 0; + inline bool Add(const char *inbuf, unsigned long long const inlen) APT_NONNULL(2) + { return Add(reinterpret_cast<const unsigned char *>(inbuf), inlen); } + + inline bool Add(const unsigned char *Data) APT_NONNULL(2) + { return Add(Data, strlen(reinterpret_cast<const char *>(Data))); } + inline bool Add(const char *Data) APT_NONNULL(2) + { return Add(reinterpret_cast<const unsigned char *>(Data), strlen(Data)); } + + inline bool Add(const unsigned char *Beg, const unsigned char *End) APT_NONNULL(2,3) + { return Add(Beg, End - Beg); } + inline bool Add(const char *Beg, const char *End) APT_NONNULL(2,3) + { return Add(reinterpret_cast<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 |