]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.h
implement MarkAndSweep in cc instead of header
[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; };
8a8feb29 58
495e5cb2 59 // verify the given filename against the currently loaded hash
8f3ba4e8 60 bool VerifyFile(std::string filename) const;
495e5cb2 61
e6645b9f
MV
62 // generate a hash string from the given filename
63 bool FromFile(std::string filename);
64
65
495e5cb2 66 // helper
8f3ba4e8 67 std::string toStr() const; // convert to str as "type:hash"
495e5cb2 68 bool empty() const;
f4c3850e
DK
69 bool operator==(HashString const &other) const;
70 bool operator!=(HashString const &other) const;
495e5cb2
MV
71
72 // return the list of hashes we support
a02db58f 73 static APT_CONST const char** SupportedHashes();
495e5cb2 74};
42ab8223 75
f4c3850e
DK
76class HashStringList
77{
78 public:
79 /** find best hash if no specific one is requested
80 *
81 * @param type of the checksum to return, can be \b NULL
82 * @return If type is \b NULL (or the empty string) it will
83 * return the 'best' hash; otherwise the hash which was
84 * specifically requested. If no hash is found \b NULL will be returned.
85 */
86 HashString const * find(char const * const type) const;
87 HashString const * find(std::string const &type) const { return find(type.c_str()); }
88 /** check if the given hash type is supported
89 *
90 * @param type to check
91 * @return true if supported, otherwise false
92 */
93 static APT_PURE bool supported(char const * const type);
94 /** add the given #HashString to the list
95 *
96 * @param hashString to add
97 * @return true if the hash is added because it is supported and
98 * not already a different hash of the same type included, otherwise false
99 */
100 bool push_back(const HashString &hashString);
101 /** @return size of the list of HashStrings */
102 size_t size() const { return list.size(); }
103
104 /** take the 'best' hash and verify file with it
105 *
106 * @param filename to verify
107 * @return true if the file matches the hashsum, otherwise false
108 */
109 bool VerifyFile(std::string filename) const;
110
111 /** is the list empty ?
112 *
113 * @return \b true if the list is empty, otherwise \b false
114 */
115 bool empty() const { return list.empty(); }
116
b3501edb
DK
117 /** has the list at least one good entry
118 *
119 * similar to #empty, but handles forced hashes.
120 *
121 * @return if no hash is forced, same result as #empty,
122 * if one is forced \b true if this has is available, \b false otherwise
123 */
124 bool usable() const;
125
f4c3850e
DK
126 typedef std::vector<HashString>::const_iterator const_iterator;
127
128 /** iterator to the first element */
129 const_iterator begin() const { return list.begin(); }
130
131 /** iterator to the end element */
132 const_iterator end() const { return list.end(); }
133
134 /** start fresh with a clear list */
135 void clear() { list.clear(); }
136
137 /** compare two HashStringList for similarity.
138 *
139 * Two lists are similar if at least one hashtype is in both lists
b3501edb
DK
140 * and the hashsum matches. All hashes are checked by default,
141 * if one doesn't match false is returned regardless of how many
142 * matched before. If a hash is forced, only this hash is compared,
143 * all others are ignored.
f4c3850e
DK
144 */
145 bool operator==(HashStringList const &other) const;
146 bool operator!=(HashStringList const &other) const;
147
148 HashStringList() {}
149
150 // simplifying API-compatibility constructors
151 HashStringList(std::string const &hash) {
152 if (hash.empty() == false)
153 list.push_back(HashString(hash));
154 }
155 HashStringList(char const * const hash) {
156 if (hash != NULL && hash[0] != '\0')
157 list.push_back(HashString(hash));
158 }
159
160 private:
161 std::vector<HashString> list;
162};
163
63b1700f
AL
164class Hashes
165{
b3501edb
DK
166 /** \brief dpointer placeholder */
167 void *d;
168
63b1700f 169 public:
b3501edb
DK
170 /* those will disappear in the future as it is hard to add new ones this way.
171 * Use Add* to build the results and get them via GetHashStringList() instead */
172 APT_DEPRECATED MD5Summation MD5;
173 APT_DEPRECATED SHA1Summation SHA1;
174 APT_DEPRECATED SHA256Summation SHA256;
175 APT_DEPRECATED SHA512Summation SHA512;
63b1700f 176
ce928105
MV
177 static const int UntilEOF = 0;
178
b3501edb
DK
179 bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes = ~0);
180 inline bool Add(const char * const Data)
181 {return Add((unsigned char const * const)Data,strlen(Data));};
182 inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
183 {return Add(Beg,End-Beg);};
184
185 enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
186 SHA512SUM = (1 << 3) };
187 bool AddFD(int const Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0);
188 bool AddFD(FileFd &Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0);
189
190 HashStringList GetHashStringList();
191
192#if __GNUC__ >= 4
193 #pragma GCC diagnostic push
194 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
195#endif
196 Hashes();
197 virtual ~Hashes();
198#if __GNUC__ >= 4
199 #pragma GCC diagnostic pop
200#endif
201
202 private:
203 APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
63b1700f 204 {
b3501edb
DK
205 unsigned int Hashes = ~0;
206 if (addMD5 == false) Hashes &= ~MD5SUM;
207 if (addSHA1 == false) Hashes &= ~SHA1SUM;
208 if (addSHA256 == false) Hashes &= ~SHA256SUM;
209 if (addSHA512 == false) Hashes &= ~SHA512SUM;
210 return Hashes;
211 }
212
213 public:
214 APT_DEPRECATED bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
215 bool const addSHA1, bool const addSHA256, bool const addSHA512) {
216 return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
217 };
218
219 APT_DEPRECATED bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
220 bool const addSHA1, bool const addSHA256, bool const addSHA512) {
221 return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
63b1700f 222 };
63b1700f
AL
223};
224
225#endif