]>
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> |
3f439e2b | 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; | |
3f439e2b | 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 |
8f3ba4e8 | 56 | std::string HashType() { return Type; }; |
3f439e2b DK |
57 | std::string HashType() const { return Type; }; |
58 | std::string HashValue() const { return Hash; }; | |
8a8feb29 | 59 | |
495e5cb2 | 60 | // verify the given filename against the currently loaded hash |
8f3ba4e8 | 61 | bool VerifyFile(std::string filename) const; |
495e5cb2 | 62 | |
e6645b9f MV |
63 | // generate a hash string from the given filename |
64 | bool FromFile(std::string filename); | |
65 | ||
66 | ||
495e5cb2 | 67 | // helper |
8f3ba4e8 | 68 | std::string toStr() const; // convert to str as "type:hash" |
495e5cb2 | 69 | bool empty() const; |
3f439e2b DK |
70 | bool operator==(HashString const &other) const; |
71 | bool operator!=(HashString const &other) const; | |
495e5cb2 MV |
72 | |
73 | // return the list of hashes we support | |
a02db58f | 74 | static APT_CONST const char** SupportedHashes(); |
495e5cb2 | 75 | }; |
42ab8223 | 76 | |
3f439e2b DK |
77 | class HashStringList |
78 | { | |
79 | public: | |
80 | /** find best hash if no specific one is requested | |
81 | * | |
82 | * @param type of the checksum to return, can be \b NULL | |
83 | * @return If type is \b NULL (or the empty string) it will | |
84 | * return the 'best' hash; otherwise the hash which was | |
85 | * specifically requested. If no hash is found \b NULL will be returned. | |
86 | */ | |
87 | HashString const * find(char const * const type) const; | |
88 | HashString const * find(std::string const &type) const { return find(type.c_str()); } | |
89 | /** check if the given hash type is supported | |
90 | * | |
91 | * @param type to check | |
92 | * @return true if supported, otherwise false | |
93 | */ | |
94 | static APT_PURE bool supported(char const * const type); | |
95 | /** add the given #HashString to the list | |
96 | * | |
97 | * @param hashString to add | |
98 | * @return true if the hash is added because it is supported and | |
99 | * not already a different hash of the same type included, otherwise false | |
100 | */ | |
101 | bool push_back(const HashString &hashString); | |
102 | /** @return size of the list of HashStrings */ | |
103 | size_t size() const { return list.size(); } | |
104 | ||
105 | /** take the 'best' hash and verify file with it | |
106 | * | |
107 | * @param filename to verify | |
108 | * @return true if the file matches the hashsum, otherwise false | |
109 | */ | |
110 | bool VerifyFile(std::string filename) const; | |
111 | ||
112 | /** is the list empty ? | |
113 | * | |
114 | * @return \b true if the list is empty, otherwise \b false | |
115 | */ | |
116 | bool empty() const { return list.empty(); } | |
117 | ||
118 | typedef std::vector<HashString>::const_iterator const_iterator; | |
119 | ||
120 | /** iterator to the first element */ | |
121 | const_iterator begin() const { return list.begin(); } | |
122 | ||
123 | /** iterator to the end element */ | |
124 | const_iterator end() const { return list.end(); } | |
125 | ||
126 | /** start fresh with a clear list */ | |
127 | void clear() { list.clear(); } | |
128 | ||
129 | /** compare two HashStringList for similarity. | |
130 | * | |
131 | * Two lists are similar if at least one hashtype is in both lists | |
132 | * and the hashsum matches. All hashes are checked, if one doesn't | |
133 | * match false is returned regardless of how many matched before. | |
134 | */ | |
135 | bool operator==(HashStringList const &other) const; | |
136 | bool operator!=(HashStringList const &other) const; | |
137 | ||
138 | HashStringList() {} | |
139 | ||
140 | // simplifying API-compatibility constructors | |
141 | HashStringList(std::string const &hash) { | |
142 | if (hash.empty() == false) | |
143 | list.push_back(HashString(hash)); | |
144 | } | |
145 | HashStringList(char const * const hash) { | |
146 | if (hash != NULL && hash[0] != '\0') | |
147 | list.push_back(HashString(hash)); | |
148 | } | |
149 | ||
150 | private: | |
151 | std::vector<HashString> list; | |
152 | }; | |
153 | ||
63b1700f AL |
154 | class Hashes |
155 | { | |
156 | public: | |
157 | ||
158 | MD5Summation MD5; | |
159 | SHA1Summation SHA1; | |
cde41ae8 | 160 | SHA256Summation SHA256; |
d9b9e9e2 | 161 | SHA512Summation SHA512; |
63b1700f | 162 | |
ce928105 MV |
163 | static const int UntilEOF = 0; |
164 | ||
650faab0 | 165 | inline bool Add(const unsigned char *Data,unsigned long long Size) |
63b1700f | 166 | { |
d9b9e9e2 | 167 | return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size); |
63b1700f | 168 | }; |
cf4ff3b7 | 169 | inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));}; |
650faab0 | 170 | inline bool AddFD(int const Fd,unsigned long long Size = 0) |
1dab797c | 171 | { return AddFD(Fd, Size, true, true, true, true); }; |
650faab0 | 172 | bool AddFD(int const Fd, unsigned long long Size, bool const addMD5, |
1dab797c | 173 | bool const addSHA1, bool const addSHA256, bool const addSHA512); |
109eb151 DK |
174 | inline bool AddFD(FileFd &Fd,unsigned long long Size = 0) |
175 | { return AddFD(Fd, Size, true, true, true, true); }; | |
176 | bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5, | |
177 | bool const addSHA1, bool const addSHA256, bool const addSHA512); | |
63b1700f AL |
178 | inline bool Add(const unsigned char *Beg,const unsigned char *End) |
179 | {return Add(Beg,End-Beg);}; | |
180 | }; | |
181 | ||
182 | #endif |