]>
Commit | Line | Data |
---|---|---|
7ac56f8f MV |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | // $Id: hashsum_template.h,v 1.3 2001/05/07 05:05:47 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | HashSumValueTemplate - Generic Storage for a hash value | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | #ifndef APTPKG_HASHSUM_TEMPLATE_H | |
11 | #define APTPKG_HASHSUM_TEMPLATE_H | |
12 | ||
109eb151 | 13 | |
7ac56f8f MV |
14 | #include <string> |
15 | #include <cstring> | |
eff0c22e JAK |
16 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
17 | #include <apt-pkg/string_view.h> | |
18 | #endif | |
7ac56f8f | 19 | |
e75aa333 MV |
20 | #include <apt-pkg/strutl.h> |
21 | ||
453b82a3 DK |
22 | #ifndef APT_10_CLEANER_HEADERS |
23 | #include <apt-pkg/fileutl.h> | |
24 | #include <algorithm> | |
25 | #include <stdint.h> | |
26 | #endif | |
a4f6bdc8 DK |
27 | #ifndef APT_8_CLEANER_HEADERS |
28 | using std::string; | |
29 | using std::min; | |
30 | #endif | |
31 | ||
453b82a3 DK |
32 | class FileFd; |
33 | ||
7ac56f8f MV |
34 | template<int N> |
35 | class HashSumValue | |
36 | { | |
37 | unsigned char Sum[N/8]; | |
cf4ff3b7 | 38 | |
7ac56f8f MV |
39 | public: |
40 | ||
41 | // Accessors | |
42 | bool operator ==(const HashSumValue &rhs) const | |
43 | { | |
44 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; | |
cf4ff3b7 | 45 | } |
99a2ea5a DK |
46 | bool operator !=(const HashSumValue &rhs) const |
47 | { | |
48 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0; | |
cf4ff3b7 | 49 | } |
7ac56f8f | 50 | |
8f3ba4e8 | 51 | std::string Value() const |
7ac56f8f MV |
52 | { |
53 | char Conv[16] = | |
54 | { '0','1','2','3','4','5','6','7','8','9','a','b', | |
55 | 'c','d','e','f' | |
56 | }; | |
57 | char Result[((N/8)*2)+1]; | |
58 | Result[(N/8)*2] = 0; | |
cf4ff3b7 | 59 | |
7ac56f8f MV |
60 | // Convert each char into two letters |
61 | int J = 0; | |
62 | int I = 0; | |
63 | for (; I != (N/8)*2; J++,I += 2) | |
64 | { | |
65 | Result[I] = Conv[Sum[J] >> 4]; | |
66 | Result[I + 1] = Conv[Sum[J] & 0xF]; | |
67 | } | |
8f3ba4e8 | 68 | return std::string(Result); |
cf4ff3b7 DK |
69 | } |
70 | ||
7ac56f8f MV |
71 | inline void Value(unsigned char S[N/8]) |
72 | { | |
cf4ff3b7 | 73 | for (int I = 0; I != sizeof(Sum); ++I) |
7ac56f8f | 74 | S[I] = Sum[I]; |
cf4ff3b7 | 75 | } |
7ac56f8f | 76 | |
cf4ff3b7 | 77 | inline operator std::string() const |
7ac56f8f MV |
78 | { |
79 | return Value(); | |
cf4ff3b7 | 80 | } |
7ac56f8f | 81 | |
eff0c22e JAK |
82 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
83 | APT_HIDDEN bool Set(APT::StringView Str) | |
84 | { | |
85 | return Hex2Num(Str,Sum,sizeof(Sum)); | |
86 | } | |
af4899c9 JAK |
87 | #else |
88 | bool Set(std::string Str) | |
eff0c22e | 89 | { |
af4899c9 | 90 | return Hex2Num(Str,Sum,sizeof(Sum)); |
eff0c22e JAK |
91 | } |
92 | #endif | |
cf4ff3b7 | 93 | inline void Set(unsigned char S[N/8]) |
7ac56f8f | 94 | { |
cf4ff3b7 | 95 | for (int I = 0; I != sizeof(Sum); ++I) |
7ac56f8f | 96 | Sum[I] = S[I]; |
cf4ff3b7 | 97 | } |
7ac56f8f | 98 | |
e8afd168 | 99 | explicit HashSumValue(std::string const &Str) |
7ac56f8f MV |
100 | { |
101 | memset(Sum,0,sizeof(Sum)); | |
102 | Set(Str); | |
103 | } | |
eff0c22e JAK |
104 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
105 | APT_HIDDEN explicit HashSumValue(APT::StringView const &Str) | |
106 | { | |
107 | memset(Sum,0,sizeof(Sum)); | |
108 | Set(Str); | |
109 | } | |
110 | APT_HIDDEN explicit HashSumValue(const char *Str) | |
111 | { | |
112 | memset(Sum,0,sizeof(Sum)); | |
113 | Set(Str); | |
114 | } | |
115 | #endif | |
7ac56f8f MV |
116 | HashSumValue() |
117 | { | |
118 | memset(Sum,0,sizeof(Sum)); | |
119 | } | |
120 | }; | |
121 | ||
c31c1dde DK |
122 | class SummationImplementation |
123 | { | |
124 | public: | |
650faab0 DK |
125 | virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0; |
126 | inline bool Add(const char *inbuf, unsigned long long const inlen) | |
cf4ff3b7 | 127 | { return Add((const unsigned char *)inbuf, inlen); } |
c31c1dde DK |
128 | |
129 | inline bool Add(const unsigned char *Data) | |
cf4ff3b7 | 130 | { return Add(Data, strlen((const char *)Data)); } |
c31c1dde | 131 | inline bool Add(const char *Data) |
e788a834 | 132 | { return Add((const unsigned char *)Data, strlen(Data)); } |
c31c1dde DK |
133 | |
134 | inline bool Add(const unsigned char *Beg, const unsigned char *End) | |
cf4ff3b7 | 135 | { return Add(Beg, End - Beg); } |
c31c1dde | 136 | inline bool Add(const char *Beg, const char *End) |
cf4ff3b7 | 137 | { return Add((const unsigned char *)Beg, End - Beg); } |
c31c1dde | 138 | |
650faab0 | 139 | bool AddFD(int Fd, unsigned long long Size = 0); |
109eb151 | 140 | bool AddFD(FileFd &Fd, unsigned long long Size = 0); |
c31c1dde DK |
141 | }; |
142 | ||
7ac56f8f | 143 | #endif |