]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.cc
fix occurrence typo in tagfile comment
[apt.git] / apt-pkg / contrib / hashes.cc
CommitLineData
63b1700f
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
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// Include Files /*{{{*/
ea542140
DK
14#include <config.h>
15
63b1700f 16#include <apt-pkg/hashes.h>
495e5cb2
MV
17#include <apt-pkg/fileutl.h>
18#include <apt-pkg/configuration.h>
453b82a3
DK
19#include <apt-pkg/md5.h>
20#include <apt-pkg/sha1.h>
21#include <apt-pkg/sha2.h>
aea7f4c8 22
453b82a3
DK
23#include <stddef.h>
24#include <algorithm>
ea542140 25#include <unistd.h>
495e5cb2
MV
26#include <string>
27#include <iostream>
63b1700f
AL
28 /*}}}*/
29
f4c3850e 30const char * HashString::_SupportedHashes[] =
495e5cb2 31{
d9b9e9e2 32 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
495e5cb2
MV
33};
34
35HashString::HashString()
36{
37}
38
8f3ba4e8 39HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
495e5cb2
MV
40{
41}
42
8f3ba4e8 43HashString::HashString(std::string StringedHash) /*{{{*/
495e5cb2 44{
f4c3850e 45 if (StringedHash.find(":") == std::string::npos)
495e5cb2 46 {
f4c3850e
DK
47 // legacy: md5sum without "MD5Sum:" prefix
48 if (StringedHash.size() == 32)
49 {
50 Type = "MD5Sum";
51 Hash = StringedHash;
52 }
53 if(_config->FindB("Debug::Hashes",false) == true)
54 std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
495e5cb2
MV
55 return;
56 }
8f3ba4e8 57 std::string::size_type pos = StringedHash.find(":");
8a8feb29 58 Type = StringedHash.substr(0,pos);
495e5cb2
MV
59 Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
60
61 if(_config->FindB("Debug::Hashes",false) == true)
62 std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
63}
92fcbfc1 64 /*}}}*/
8f3ba4e8 65bool HashString::VerifyFile(std::string filename) const /*{{{*/
e6645b9f
MV
66{
67 std::string fileHash = GetHashForFile(filename);
68
69 if(_config->FindB("Debug::Hashes",false) == true)
70 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
71
72 return (fileHash == Hash);
73}
74 /*}}}*/
75bool HashString::FromFile(std::string filename) /*{{{*/
76{
77 // pick the strongest hash
78 if (Type == "")
79 Type = _SupportedHashes[0];
80
81 Hash = GetHashForFile(filename);
82 return true;
83}
84 /*}}}*/
85std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
495e5cb2 86{
8f3ba4e8 87 std::string fileHash;
495e5cb2
MV
88
89 FileFd Fd(filename, FileFd::ReadOnly);
f4c3850e 90 if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
495e5cb2 91 {
2dcf7b8f 92 MD5Summation MD5;
109eb151 93 MD5.AddFD(Fd);
8f3ba4e8 94 fileHash = (std::string)MD5.Result();
2dcf7b8f 95 }
f4c3850e 96 else if (strcasecmp(Type.c_str(), "SHA1") == 0)
495e5cb2 97 {
2dcf7b8f 98 SHA1Summation SHA1;
109eb151 99 SHA1.AddFD(Fd);
8f3ba4e8 100 fileHash = (std::string)SHA1.Result();
2dcf7b8f 101 }
f4c3850e 102 else if (strcasecmp(Type.c_str(), "SHA256") == 0)
495e5cb2 103 {
2dcf7b8f 104 SHA256Summation SHA256;
109eb151 105 SHA256.AddFD(Fd);
8f3ba4e8 106 fileHash = (std::string)SHA256.Result();
495e5cb2 107 }
f4c3850e 108 else if (strcasecmp(Type.c_str(), "SHA512") == 0)
d9b9e9e2 109 {
2dcf7b8f 110 SHA512Summation SHA512;
109eb151 111 SHA512.AddFD(Fd);
8f3ba4e8 112 fileHash = (std::string)SHA512.Result();
d9b9e9e2 113 }
495e5cb2
MV
114 Fd.Close();
115
e6645b9f 116 return fileHash;
495e5cb2 117}
92fcbfc1 118 /*}}}*/
f4c3850e 119const char** HashString::SupportedHashes() /*{{{*/
495e5cb2
MV
120{
121 return _SupportedHashes;
122}
f4c3850e
DK
123 /*}}}*/
124APT_PURE bool HashString::empty() const /*{{{*/
495e5cb2
MV
125{
126 return (Type.empty() || Hash.empty());
127}
f4c3850e
DK
128 /*}}}*/
129std::string HashString::toStr() const /*{{{*/
130{
131 return Type + ":" + Hash;
132}
133 /*}}}*/
134APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
135{
136 return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
137}
138APT_PURE bool HashString::operator!=(HashString const &other) const
139{
140 return !(*this == other);
141}
142 /*}}}*/
143
b3501edb
DK
144bool HashStringList::usable() const /*{{{*/
145{
146 if (empty() == true)
147 return false;
148 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
149 if (forcedType.empty() == true)
150 return true;
151 return find(forcedType) != NULL;
152}
153 /*}}}*/
f4c3850e
DK
154HashString const * HashStringList::find(char const * const type) const /*{{{*/
155{
156 if (type == NULL || type[0] == '\0')
157 {
b3501edb 158 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
f4c3850e
DK
159 if (forcedType.empty() == false)
160 return find(forcedType.c_str());
161 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
162 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
163 if (strcasecmp(hs->HashType().c_str(), *t) == 0)
164 return &*hs;
165 return NULL;
166 }
167 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
168 if (strcasecmp(hs->HashType().c_str(), type) == 0)
169 return &*hs;
170 return NULL;
171}
172 /*}}}*/
173bool HashStringList::supported(char const * const type) /*{{{*/
174{
175 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
176 if (strcasecmp(*t, type) == 0)
177 return true;
178 return false;
179}
180 /*}}}*/
181bool HashStringList::push_back(const HashString &hashString) /*{{{*/
182{
183 if (hashString.HashType().empty() == true ||
184 hashString.HashValue().empty() == true ||
185 supported(hashString.HashType().c_str()) == false)
186 return false;
495e5cb2 187
f4c3850e
DK
188 // ensure that each type is added only once
189 HashString const * const hs = find(hashString.HashType().c_str());
190 if (hs != NULL)
191 return *hs == hashString;
192
193 list.push_back(hashString);
194 return true;
195}
196 /*}}}*/
197bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
198{
199 if (list.empty() == true)
200 return false;
201 HashString const * const hs = find(NULL);
202 if (hs == NULL || hs->VerifyFile(filename) == false)
203 return false;
204 return true;
205}
206 /*}}}*/
207bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
495e5cb2 208{
b3501edb
DK
209 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
210 if (forcedType.empty() == false)
211 {
212 HashString const * const hs = other.find(forcedType);
213 HashString const * const ohs = other.find(forcedType);
214 if (hs == NULL || ohs == NULL)
215 return false;
216 return hs == ohs;
217 }
f4c3850e
DK
218 short matches = 0;
219 for (const_iterator hs = begin(); hs != end(); ++hs)
220 {
221 HashString const * const ohs = other.find(hs->HashType());
222 if (ohs == NULL)
223 continue;
224 if (*hs != *ohs)
225 return false;
226 ++matches;
227 }
228 if (matches == 0)
229 return false;
230 return true;
231}
232bool HashStringList::operator!=(HashStringList const &other) const
233{
234 return !(*this == other);
495e5cb2 235}
f4c3850e 236 /*}}}*/
495e5cb2 237
b3501edb
DK
238// Hashes::Add* - Add the contents of data or FD /*{{{*/
239bool Hashes::Add(const unsigned char * const Data,unsigned long long const Size, unsigned int const Hashes)
240{
241 bool Res = true;
242#if __GNUC__ >= 4
243 #pragma GCC diagnostic push
244 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
245#endif
246 if ((Hashes & MD5SUM) == MD5SUM)
247 Res &= MD5.Add(Data, Size);
248 if ((Hashes & SHA1SUM) == SHA1SUM)
249 Res &= SHA1.Add(Data, Size);
250 if ((Hashes & SHA256SUM) == SHA256SUM)
251 Res &= SHA256.Add(Data, Size);
252 if ((Hashes & SHA512SUM) == SHA512SUM)
253 Res &= SHA512.Add(Data, Size);
254#if __GNUC__ >= 4
255 #pragma GCC diagnostic pop
256#endif
257 return Res;
258}
259bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
63b1700f
AL
260{
261 unsigned char Buf[64*64];
ce928105 262 bool const ToEOF = (Size == UntilEOF);
04f4e1a3 263 while (Size != 0 || ToEOF)
63b1700f 264 {
650faab0 265 unsigned long long n = sizeof(Buf);
8f3ba4e8 266 if (!ToEOF) n = std::min(Size, n);
9ce3cfc9 267 ssize_t const Res = read(Fd,Buf,n);
650faab0 268 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
1dab797c 269 return false;
04f4e1a3 270 if (ToEOF && Res == 0) // EOF
1dab797c 271 break;
63b1700f 272 Size -= Res;
b3501edb
DK
273 if (Add(Buf, Res, Hashes) == false)
274 return false;
63b1700f
AL
275 }
276 return true;
109eb151 277}
b3501edb 278bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
109eb151
DK
279{
280 unsigned char Buf[64*64];
281 bool const ToEOF = (Size == 0);
282 while (Size != 0 || ToEOF)
283 {
284 unsigned long long n = sizeof(Buf);
285 if (!ToEOF) n = std::min(Size, n);
286 unsigned long long a = 0;
287 if (Fd.Read(Buf, n, &a) == false) // error
288 return false;
289 if (ToEOF == false)
290 {
291 if (a != n) // short read
292 return false;
293 }
294 else if (a == 0) // EOF
295 break;
296 Size -= a;
b3501edb
DK
297 if (Add(Buf, a, Hashes) == false)
298 return false;
109eb151
DK
299 }
300 return true;
63b1700f
AL
301}
302 /*}}}*/
b3501edb
DK
303HashStringList Hashes::GetHashStringList()
304{
305 HashStringList hashes;
306#if __GNUC__ >= 4
307 #pragma GCC diagnostic push
308 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
309#endif
310 hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
311 hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
312 hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
313 hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
314#if __GNUC__ >= 4
315 #pragma GCC diagnostic pop
316#endif
317 return hashes;
318}
319#if __GNUC__ >= 4
320 #pragma GCC diagnostic push
321 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
322 #pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
323#endif
324Hashes::Hashes() {}
325Hashes::~Hashes() {}
326#if __GNUC__ >= 4
327 #pragma GCC diagnostic pop
328#endif