]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.h
fix "Mismatched free() / delete / delete []" in simple_buffer
[apt.git] / apt-pkg / contrib / hashes.h
CommitLineData
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
26using std::min;
27using 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
36class FileFd;
a4f6bdc8 37
495e5cb2
MV
38// helper class that contains hash function name
39// and hash
40class 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; };
5dd00edb
DK
58 APT_DEPRECATED_MSG("method was const-ified") std::string HashType() { return Type; };
59 APT_DEPRECATED_MSG("method was const-ified") 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;
55ae7a51 71 bool usable() const;
f4c3850e
DK
72 bool operator==(HashString const &other) const;
73 bool operator!=(HashString const &other) const;
495e5cb2
MV
74
75 // return the list of hashes we support
a02db58f 76 static APT_CONST const char** SupportedHashes();
495e5cb2 77};
42ab8223 78
f4c3850e
DK
79class HashStringList
80{
81 public:
82 /** find best hash if no specific one is requested
83 *
84 * @param type of the checksum to return, can be \b NULL
85 * @return If type is \b NULL (or the empty string) it will
86 * return the 'best' hash; otherwise the hash which was
87 * specifically requested. If no hash is found \b NULL will be returned.
88 */
89 HashString const * find(char const * const type) const;
90 HashString const * find(std::string const &type) const { return find(type.c_str()); }
448c38bd
DK
91
92 /** finds the filesize hash and returns it as number
93 *
94 * @return beware: if the size isn't known we return \b 0 here,
95 * just like we would do for an empty file. If that is a problem
96 * for you have to get the size manually out of the list.
97 */
98 unsigned long long FileSize() const;
99
4f51fd86
DK
100 /** sets the filesize hash
101 *
102 * @param Size of the file
103 * @return @see #push_back
104 */
105 bool FileSize(unsigned long long const Size);
106
f4c3850e
DK
107 /** check if the given hash type is supported
108 *
109 * @param type to check
110 * @return true if supported, otherwise false
111 */
112 static APT_PURE bool supported(char const * const type);
113 /** add the given #HashString to the list
114 *
115 * @param hashString to add
116 * @return true if the hash is added because it is supported and
117 * not already a different hash of the same type included, otherwise false
118 */
119 bool push_back(const HashString &hashString);
120 /** @return size of the list of HashStrings */
121 size_t size() const { return list.size(); }
122
875a9e54 123 /** verify file against all hashes in the list
f4c3850e
DK
124 *
125 * @param filename to verify
126 * @return true if the file matches the hashsum, otherwise false
127 */
128 bool VerifyFile(std::string filename) const;
129
130 /** is the list empty ?
131 *
132 * @return \b true if the list is empty, otherwise \b false
133 */
134 bool empty() const { return list.empty(); }
135
b3501edb
DK
136 /** has the list at least one good entry
137 *
138 * similar to #empty, but handles forced hashes.
139 *
140 * @return if no hash is forced, same result as #empty,
141 * if one is forced \b true if this has is available, \b false otherwise
142 */
143 bool usable() const;
144
f4c3850e
DK
145 typedef std::vector<HashString>::const_iterator const_iterator;
146
147 /** iterator to the first element */
148 const_iterator begin() const { return list.begin(); }
149
150 /** iterator to the end element */
151 const_iterator end() const { return list.end(); }
152
153 /** start fresh with a clear list */
154 void clear() { list.clear(); }
155
156 /** compare two HashStringList for similarity.
157 *
158 * Two lists are similar if at least one hashtype is in both lists
b3501edb
DK
159 * and the hashsum matches. All hashes are checked by default,
160 * if one doesn't match false is returned regardless of how many
161 * matched before. If a hash is forced, only this hash is compared,
162 * all others are ignored.
f4c3850e
DK
163 */
164 bool operator==(HashStringList const &other) const;
165 bool operator!=(HashStringList const &other) const;
166
167 HashStringList() {}
168
169 // simplifying API-compatibility constructors
170 HashStringList(std::string const &hash) {
171 if (hash.empty() == false)
172 list.push_back(HashString(hash));
173 }
174 HashStringList(char const * const hash) {
175 if (hash != NULL && hash[0] != '\0')
176 list.push_back(HashString(hash));
177 }
178
179 private:
180 std::vector<HashString> list;
181};
182
23397c9d 183class PrivateHashes;
63b1700f
AL
184class Hashes
185{
6c55f07a 186 PrivateHashes * const d;
b3501edb 187
63b1700f 188 public:
b3501edb
DK
189 /* those will disappear in the future as it is hard to add new ones this way.
190 * Use Add* to build the results and get them via GetHashStringList() instead */
5dd00edb
DK
191 APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") MD5Summation MD5;
192 APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA1Summation SHA1;
193 APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA256Summation SHA256;
194 APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA512Summation SHA512;
63b1700f 195
ce928105
MV
196 static const int UntilEOF = 0;
197
9224ce3d 198 bool Add(const unsigned char * const Data, unsigned long long const Size);
5dd00edb 199 APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes);
b3501edb
DK
200 inline bool Add(const char * const Data)
201 {return Add((unsigned char const * const)Data,strlen(Data));};
202 inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
203 {return Add(Beg,End-Beg);};
204
205 enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
206 SHA512SUM = (1 << 3) };
9224ce3d 207 bool AddFD(int const Fd,unsigned long long Size = 0);
5dd00edb 208 APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes);
9224ce3d 209 bool AddFD(FileFd &Fd,unsigned long long Size = 0);
5dd00edb 210 APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes);
b3501edb
DK
211
212 HashStringList GetHashStringList();
213
586d8704 214APT_IGNORE_DEPRECATED_PUSH
9224ce3d
DK
215 /** create a Hashes object to calculate all supported hashes
216 *
217 * If ALL is too much, you can limit which Hashes are calculated
218 * with the following other constructors which mention explicitly
219 * which hashes to generate. */
b3501edb 220 Hashes();
9224ce3d
DK
221 /** @param Hashes bitflag composed of #SupportedHashes */
222 Hashes(unsigned int const Hashes);
223 /** @param Hashes is a list of hashes */
224 Hashes(HashStringList const &Hashes);
b3501edb 225 virtual ~Hashes();
586d8704 226APT_IGNORE_DEPRECATED_POP
b3501edb
DK
227
228 private:
229 APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
63b1700f 230 {
b3501edb
DK
231 unsigned int Hashes = ~0;
232 if (addMD5 == false) Hashes &= ~MD5SUM;
233 if (addSHA1 == false) Hashes &= ~SHA1SUM;
234 if (addSHA256 == false) Hashes &= ~SHA256SUM;
235 if (addSHA512 == false) Hashes &= ~SHA512SUM;
236 return Hashes;
237 }
238
239 public:
9224ce3d 240APT_IGNORE_DEPRECATED_PUSH
5dd00edb 241 APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
b3501edb
DK
242 bool const addSHA1, bool const addSHA256, bool const addSHA512) {
243 return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
244 };
5dd00edb 245 APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
b3501edb
DK
246 bool const addSHA1, bool const addSHA256, bool const addSHA512) {
247 return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
63b1700f 248 };
9224ce3d 249APT_IGNORE_DEPRECATED_POP
63b1700f
AL
250};
251
252#endif