]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | ||
14 | #include <string> | |
15 | #include <cstring> | |
16 | #ifdef APT_PKG_EXPOSE_STRING_VIEW | |
17 | #include <apt-pkg/string_view.h> | |
18 | #endif | |
19 | ||
20 | #include <apt-pkg/strutl.h> | |
21 | ||
22 | #ifndef APT_10_CLEANER_HEADERS | |
23 | #include <apt-pkg/fileutl.h> | |
24 | #include <algorithm> | |
25 | #include <stdint.h> | |
26 | #endif | |
27 | #ifndef APT_8_CLEANER_HEADERS | |
28 | using std::string; | |
29 | using std::min; | |
30 | #endif | |
31 | ||
32 | class FileFd; | |
33 | ||
34 | template<int N> | |
35 | class HashSumValue | |
36 | { | |
37 | unsigned char Sum[N/8]; | |
38 | ||
39 | public: | |
40 | ||
41 | // Accessors | |
42 | bool operator ==(const HashSumValue &rhs) const | |
43 | { | |
44 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; | |
45 | } | |
46 | bool operator !=(const HashSumValue &rhs) const | |
47 | { | |
48 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0; | |
49 | } | |
50 | ||
51 | std::string Value() const | |
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; | |
59 | ||
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 | } | |
68 | return std::string(Result); | |
69 | } | |
70 | ||
71 | inline void Value(unsigned char S[N/8]) | |
72 | { | |
73 | for (int I = 0; I != sizeof(Sum); ++I) | |
74 | S[I] = Sum[I]; | |
75 | } | |
76 | ||
77 | inline operator std::string() const | |
78 | { | |
79 | return Value(); | |
80 | } | |
81 | ||
82 | #ifdef APT_PKG_EXPOSE_STRING_VIEW | |
83 | APT_HIDDEN bool Set(APT::StringView Str) | |
84 | { | |
85 | return Hex2Num(Str,Sum,sizeof(Sum)); | |
86 | } | |
87 | #else | |
88 | bool Set(std::string Str) | |
89 | { | |
90 | return Hex2Num(Str,Sum,sizeof(Sum)); | |
91 | } | |
92 | #endif | |
93 | inline void Set(unsigned char S[N/8]) | |
94 | { | |
95 | for (int I = 0; I != sizeof(Sum); ++I) | |
96 | Sum[I] = S[I]; | |
97 | } | |
98 | ||
99 | explicit HashSumValue(std::string const &Str) | |
100 | { | |
101 | memset(Sum,0,sizeof(Sum)); | |
102 | Set(Str); | |
103 | } | |
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 | |
116 | HashSumValue() | |
117 | { | |
118 | memset(Sum,0,sizeof(Sum)); | |
119 | } | |
120 | }; | |
121 | ||
122 | class SummationImplementation | |
123 | { | |
124 | public: | |
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) | |
127 | { return Add((const unsigned char *)inbuf, inlen); } | |
128 | ||
129 | inline bool Add(const unsigned char *Data) | |
130 | { return Add(Data, strlen((const char *)Data)); } | |
131 | inline bool Add(const char *Data) | |
132 | { return Add((const unsigned char *)Data, strlen(Data)); } | |
133 | ||
134 | inline bool Add(const unsigned char *Beg, const unsigned char *End) | |
135 | { return Add(Beg, End - Beg); } | |
136 | inline bool Add(const char *Beg, const char *End) | |
137 | { return Add((const unsigned char *)Beg, End - Beg); } | |
138 | ||
139 | bool AddFD(int Fd, unsigned long long Size = 0); | |
140 | bool AddFD(FileFd &Fd, unsigned long long Size = 0); | |
141 | }; | |
142 | ||
143 | #endif |