]>
Commit | Line | Data |
---|---|---|
63b1700f AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
678bc33e | 3 | // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $ |
63b1700f AL |
4 | /* ###################################################################### |
5 | ||
6 | Hashes - Simple wrapper around the hash functions | |
7 | ||
8 | This is just used to make building the methods simpler, this is the | |
9 | only interface required.. | |
10 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | #ifndef APTPKG_HASHES_H | |
14 | #define APTPKG_HASHES_H | |
15 | ||
63b1700f AL |
16 | |
17 | #include <apt-pkg/md5.h> | |
18 | #include <apt-pkg/sha1.h> | |
84a0890e | 19 | #include <apt-pkg/sha2.h> |
f4c3850e | 20 | #include <apt-pkg/macros.h> |
63b1700f | 21 | |
b16c2617 | 22 | #include <cstring> |
453b82a3 | 23 | #include <string> |
a4f6bdc8 DK |
24 | |
25 | #ifndef APT_8_CLEANER_HEADERS | |
26 | using std::min; | |
27 | using std::vector; | |
28 | #endif | |
453b82a3 DK |
29 | #ifndef APT_10_CLEANER_HEADERS |
30 | #include <apt-pkg/fileutl.h> | |
31 | #include <algorithm> | |
32 | #include <vector> | |
33 | #endif | |
34 | ||
35 | ||
36 | class FileFd; | |
a4f6bdc8 | 37 | |
495e5cb2 MV |
38 | // helper class that contains hash function name |
39 | // and hash | |
40 | class HashString | |
41 | { | |
42 | protected: | |
8f3ba4e8 DK |
43 | std::string Type; |
44 | std::string Hash; | |
f4c3850e | 45 | static const char * _SupportedHashes[10]; |
e6645b9f MV |
46 | |
47 | // internal helper | |
48 | std::string GetHashForFile(std::string filename) const; | |
495e5cb2 MV |
49 | |
50 | public: | |
8f3ba4e8 DK |
51 | HashString(std::string Type, std::string Hash); |
52 | HashString(std::string StringedHashString); // init from str as "type:hash" | |
495e5cb2 MV |
53 | HashString(); |
54 | ||
8a8feb29 | 55 | // get hash type used |
f4c3850e DK |
56 | std::string HashType() const { return Type; }; |
57 | std::string HashValue() const { return Hash; }; | |
02e20767 DK |
58 | APT_DEPRECATED std::string HashType() { return Type; }; |
59 | APT_DEPRECATED std::string HashValue() { return Hash; }; | |
8a8feb29 | 60 | |
495e5cb2 | 61 | // verify the given filename against the currently loaded hash |
8f3ba4e8 | 62 | bool VerifyFile(std::string filename) const; |
495e5cb2 | 63 | |
e6645b9f MV |
64 | // generate a hash string from the given filename |
65 | bool FromFile(std::string filename); | |
66 | ||
67 | ||
495e5cb2 | 68 | // helper |
8f3ba4e8 | 69 | std::string toStr() const; // convert to str as "type:hash" |
495e5cb2 | 70 | bool empty() const; |
f4c3850e DK |
71 | bool operator==(HashString const &other) const; |
72 | bool operator!=(HashString const &other) const; | |
495e5cb2 MV |
73 | |
74 | // return the list of hashes we support | |
a02db58f | 75 | static APT_CONST const char** SupportedHashes(); |
495e5cb2 | 76 | }; |
42ab8223 | 77 | |
f4c3850e DK |
78 | class HashStringList |
79 | { | |
80 | public: | |
81 | /** find best hash if no specific one is requested | |
82 | * | |
83 | * @param type of the checksum to return, can be \b NULL | |
84 | * @return If type is \b NULL (or the empty string) it will | |
85 | * return the 'best' hash; otherwise the hash which was | |
86 | * specifically requested. If no hash is found \b NULL will be returned. | |
87 | */ | |
88 | HashString const * find(char const * const type) const; | |
89 | HashString const * find(std::string const &type) const { return find(type.c_str()); } | |
90 | /** check if the given hash type is supported | |
91 | * | |
92 | * @param type to check | |
93 | * @return true if supported, otherwise false | |
94 | */ | |
95 | static APT_PURE bool supported(char const * const type); | |
96 | /** add the given #HashString to the list | |
97 | * | |
98 | * @param hashString to add | |
99 | * @return true if the hash is added because it is supported and | |
100 | * not already a different hash of the same type included, otherwise false | |
101 | */ | |
102 | bool push_back(const HashString &hashString); | |
103 | /** @return size of the list of HashStrings */ | |
104 | size_t size() const { return list.size(); } | |
105 | ||
106 | /** take the 'best' hash and verify file with it | |
107 | * | |
108 | * @param filename to verify | |
109 | * @return true if the file matches the hashsum, otherwise false | |
110 | */ | |
111 | bool VerifyFile(std::string filename) const; | |
112 | ||
113 | /** is the list empty ? | |
114 | * | |
115 | * @return \b true if the list is empty, otherwise \b false | |
116 | */ | |
117 | bool empty() const { return list.empty(); } | |
118 | ||
b3501edb DK |
119 | /** has the list at least one good entry |
120 | * | |
121 | * similar to #empty, but handles forced hashes. | |
122 | * | |
123 | * @return if no hash is forced, same result as #empty, | |
124 | * if one is forced \b true if this has is available, \b false otherwise | |
125 | */ | |
126 | bool usable() const; | |
127 | ||
f4c3850e DK |
128 | typedef std::vector<HashString>::const_iterator const_iterator; |
129 | ||
130 | /** iterator to the first element */ | |
131 | const_iterator begin() const { return list.begin(); } | |
132 | ||
133 | /** iterator to the end element */ | |
134 | const_iterator end() const { return list.end(); } | |
135 | ||
136 | /** start fresh with a clear list */ | |
137 | void clear() { list.clear(); } | |
138 | ||
139 | /** compare two HashStringList for similarity. | |
140 | * | |
141 | * Two lists are similar if at least one hashtype is in both lists | |
b3501edb DK |
142 | * and the hashsum matches. All hashes are checked by default, |
143 | * if one doesn't match false is returned regardless of how many | |
144 | * matched before. If a hash is forced, only this hash is compared, | |
145 | * all others are ignored. | |
f4c3850e DK |
146 | */ |
147 | bool operator==(HashStringList const &other) const; | |
148 | bool operator!=(HashStringList const &other) const; | |
149 | ||
150 | HashStringList() {} | |
151 | ||
152 | // simplifying API-compatibility constructors | |
153 | HashStringList(std::string const &hash) { | |
154 | if (hash.empty() == false) | |
155 | list.push_back(HashString(hash)); | |
156 | } | |
157 | HashStringList(char const * const hash) { | |
158 | if (hash != NULL && hash[0] != '\0') | |
159 | list.push_back(HashString(hash)); | |
160 | } | |
161 | ||
162 | private: | |
163 | std::vector<HashString> list; | |
164 | }; | |
165 | ||
23397c9d | 166 | class PrivateHashes; |
63b1700f AL |
167 | class Hashes |
168 | { | |
23397c9d | 169 | PrivateHashes *d; |
b3501edb | 170 | |
63b1700f | 171 | public: |
b3501edb DK |
172 | /* those will disappear in the future as it is hard to add new ones this way. |
173 | * Use Add* to build the results and get them via GetHashStringList() instead */ | |
174 | APT_DEPRECATED MD5Summation MD5; | |
175 | APT_DEPRECATED SHA1Summation SHA1; | |
176 | APT_DEPRECATED SHA256Summation SHA256; | |
177 | APT_DEPRECATED SHA512Summation SHA512; | |
63b1700f | 178 | |
ce928105 MV |
179 | static const int UntilEOF = 0; |
180 | ||
b3501edb DK |
181 | bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes = ~0); |
182 | inline bool Add(const char * const Data) | |
183 | {return Add((unsigned char const * const)Data,strlen(Data));}; | |
184 | inline bool Add(const unsigned char * const Beg,const unsigned char * const End) | |
185 | {return Add(Beg,End-Beg);}; | |
186 | ||
187 | enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2), | |
188 | SHA512SUM = (1 << 3) }; | |
189 | bool AddFD(int const Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0); | |
190 | bool AddFD(FileFd &Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0); | |
191 | ||
192 | HashStringList GetHashStringList(); | |
193 | ||
586d8704 | 194 | APT_IGNORE_DEPRECATED_PUSH |
b3501edb DK |
195 | Hashes(); |
196 | virtual ~Hashes(); | |
586d8704 | 197 | APT_IGNORE_DEPRECATED_POP |
b3501edb DK |
198 | |
199 | private: | |
200 | APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512) | |
63b1700f | 201 | { |
b3501edb DK |
202 | unsigned int Hashes = ~0; |
203 | if (addMD5 == false) Hashes &= ~MD5SUM; | |
204 | if (addSHA1 == false) Hashes &= ~SHA1SUM; | |
205 | if (addSHA256 == false) Hashes &= ~SHA256SUM; | |
206 | if (addSHA512 == false) Hashes &= ~SHA512SUM; | |
207 | return Hashes; | |
208 | } | |
209 | ||
210 | public: | |
211 | APT_DEPRECATED bool AddFD(int const Fd, unsigned long long Size, bool const addMD5, | |
212 | bool const addSHA1, bool const addSHA256, bool const addSHA512) { | |
213 | return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512)); | |
214 | }; | |
215 | ||
216 | APT_DEPRECATED bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5, | |
217 | bool const addSHA1, bool const addSHA256, bool const addSHA512) { | |
218 | return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512)); | |
63b1700f | 219 | }; |
63b1700f AL |
220 | }; |
221 | ||
222 | #endif |