]>
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 DK |
13 | #include <apt-pkg/fileutl.h> |
14 | ||
7ac56f8f MV |
15 | #include <string> |
16 | #include <cstring> | |
17 | #include <algorithm> | |
18 | #include <stdint.h> | |
19 | ||
e75aa333 MV |
20 | #include <apt-pkg/strutl.h> |
21 | ||
a4f6bdc8 DK |
22 | #ifndef APT_8_CLEANER_HEADERS |
23 | using std::string; | |
24 | using std::min; | |
25 | #endif | |
26 | ||
7ac56f8f MV |
27 | template<int N> |
28 | class HashSumValue | |
29 | { | |
30 | unsigned char Sum[N/8]; | |
31 | ||
32 | public: | |
33 | ||
34 | // Accessors | |
35 | bool operator ==(const HashSumValue &rhs) const | |
36 | { | |
37 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; | |
99a2ea5a DK |
38 | }; |
39 | bool operator !=(const HashSumValue &rhs) const | |
40 | { | |
41 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0; | |
42 | }; | |
7ac56f8f | 43 | |
8f3ba4e8 | 44 | std::string Value() const |
7ac56f8f MV |
45 | { |
46 | char Conv[16] = | |
47 | { '0','1','2','3','4','5','6','7','8','9','a','b', | |
48 | 'c','d','e','f' | |
49 | }; | |
50 | char Result[((N/8)*2)+1]; | |
51 | Result[(N/8)*2] = 0; | |
52 | ||
53 | // Convert each char into two letters | |
54 | int J = 0; | |
55 | int I = 0; | |
56 | for (; I != (N/8)*2; J++,I += 2) | |
57 | { | |
58 | Result[I] = Conv[Sum[J] >> 4]; | |
59 | Result[I + 1] = Conv[Sum[J] & 0xF]; | |
60 | } | |
8f3ba4e8 | 61 | return std::string(Result); |
7ac56f8f MV |
62 | }; |
63 | ||
64 | inline void Value(unsigned char S[N/8]) | |
65 | { | |
66 | for (int I = 0; I != sizeof(Sum); I++) | |
67 | S[I] = Sum[I]; | |
68 | }; | |
69 | ||
8f3ba4e8 | 70 | inline operator std::string() const |
7ac56f8f MV |
71 | { |
72 | return Value(); | |
73 | }; | |
74 | ||
8f3ba4e8 | 75 | bool Set(std::string Str) |
7ac56f8f MV |
76 | { |
77 | return Hex2Num(Str,Sum,sizeof(Sum)); | |
78 | }; | |
79 | ||
80 | inline void Set(unsigned char S[N/8]) | |
81 | { | |
82 | for (int I = 0; I != sizeof(Sum); I++) | |
83 | Sum[I] = S[I]; | |
84 | }; | |
85 | ||
8f3ba4e8 | 86 | HashSumValue(std::string Str) |
7ac56f8f MV |
87 | { |
88 | memset(Sum,0,sizeof(Sum)); | |
89 | Set(Str); | |
90 | } | |
91 | HashSumValue() | |
92 | { | |
93 | memset(Sum,0,sizeof(Sum)); | |
94 | } | |
95 | }; | |
96 | ||
c31c1dde DK |
97 | class SummationImplementation |
98 | { | |
99 | public: | |
650faab0 DK |
100 | virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0; |
101 | inline bool Add(const char *inbuf, unsigned long long const inlen) | |
c31c1dde DK |
102 | { return Add((unsigned char *)inbuf, inlen); }; |
103 | ||
104 | inline bool Add(const unsigned char *Data) | |
105 | { return Add(Data, strlen((const char *)Data)); }; | |
106 | inline bool Add(const char *Data) | |
107 | { return Add((const unsigned char *)Data, strlen((const char *)Data)); }; | |
108 | ||
109 | inline bool Add(const unsigned char *Beg, const unsigned char *End) | |
110 | { return Add(Beg, End - Beg); }; | |
111 | inline bool Add(const char *Beg, const char *End) | |
112 | { return Add((const unsigned char *)Beg, End - Beg); }; | |
113 | ||
650faab0 | 114 | bool AddFD(int Fd, unsigned long long Size = 0); |
109eb151 | 115 | bool AddFD(FileFd &Fd, unsigned long long Size = 0); |
c31c1dde DK |
116 | }; |
117 | ||
7ac56f8f | 118 | #endif |