]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.cc
guard const-ification API changes
[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{
23397c9d 32 "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", 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 }
23397c9d
DK
114 else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0)
115 strprintf(fileHash, "%llu", Fd.FileSize());
495e5cb2
MV
116 Fd.Close();
117
e6645b9f 118 return fileHash;
495e5cb2 119}
92fcbfc1 120 /*}}}*/
f4c3850e 121const char** HashString::SupportedHashes() /*{{{*/
495e5cb2
MV
122{
123 return _SupportedHashes;
124}
f4c3850e
DK
125 /*}}}*/
126APT_PURE bool HashString::empty() const /*{{{*/
495e5cb2
MV
127{
128 return (Type.empty() || Hash.empty());
129}
f4c3850e
DK
130 /*}}}*/
131std::string HashString::toStr() const /*{{{*/
132{
133 return Type + ":" + Hash;
134}
135 /*}}}*/
136APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
137{
138 return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
139}
140APT_PURE bool HashString::operator!=(HashString const &other) const
141{
142 return !(*this == other);
143}
144 /*}}}*/
145
b3501edb
DK
146bool HashStringList::usable() const /*{{{*/
147{
148 if (empty() == true)
149 return false;
150 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
151 if (forcedType.empty() == true)
23397c9d
DK
152 {
153 // FileSize alone isn't usable
154 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
155 if (hs->HashType() != "Checksum-FileSize")
156 return true;
157 return false;
158 }
b3501edb
DK
159 return find(forcedType) != NULL;
160}
161 /*}}}*/
f4c3850e
DK
162HashString const * HashStringList::find(char const * const type) const /*{{{*/
163{
164 if (type == NULL || type[0] == '\0')
165 {
b3501edb 166 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
f4c3850e
DK
167 if (forcedType.empty() == false)
168 return find(forcedType.c_str());
169 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
170 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
171 if (strcasecmp(hs->HashType().c_str(), *t) == 0)
172 return &*hs;
173 return NULL;
174 }
175 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
176 if (strcasecmp(hs->HashType().c_str(), type) == 0)
177 return &*hs;
178 return NULL;
179}
180 /*}}}*/
181bool HashStringList::supported(char const * const type) /*{{{*/
182{
183 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
184 if (strcasecmp(*t, type) == 0)
185 return true;
186 return false;
187}
188 /*}}}*/
189bool HashStringList::push_back(const HashString &hashString) /*{{{*/
190{
191 if (hashString.HashType().empty() == true ||
192 hashString.HashValue().empty() == true ||
193 supported(hashString.HashType().c_str()) == false)
194 return false;
495e5cb2 195
f4c3850e
DK
196 // ensure that each type is added only once
197 HashString const * const hs = find(hashString.HashType().c_str());
198 if (hs != NULL)
199 return *hs == hashString;
200
201 list.push_back(hashString);
202 return true;
203}
204 /*}}}*/
205bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
206{
207 if (list.empty() == true)
208 return false;
209 HashString const * const hs = find(NULL);
210 if (hs == NULL || hs->VerifyFile(filename) == false)
211 return false;
23397c9d
DK
212 HashString const * const hsf = find("Checksum-FileSize");
213 if (hsf != NULL && hsf->VerifyFile(filename) == false)
214 return false;
f4c3850e
DK
215 return true;
216}
217 /*}}}*/
218bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
495e5cb2 219{
b3501edb
DK
220 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
221 if (forcedType.empty() == false)
222 {
f6d4ab9a 223 HashString const * const hs = find(forcedType);
b3501edb
DK
224 HashString const * const ohs = other.find(forcedType);
225 if (hs == NULL || ohs == NULL)
226 return false;
f6d4ab9a 227 return *hs == *ohs;
b3501edb 228 }
f4c3850e
DK
229 short matches = 0;
230 for (const_iterator hs = begin(); hs != end(); ++hs)
231 {
232 HashString const * const ohs = other.find(hs->HashType());
233 if (ohs == NULL)
234 continue;
235 if (*hs != *ohs)
236 return false;
237 ++matches;
238 }
239 if (matches == 0)
240 return false;
241 return true;
242}
243bool HashStringList::operator!=(HashStringList const &other) const
244{
245 return !(*this == other);
495e5cb2 246}
f4c3850e 247 /*}}}*/
495e5cb2 248
23397c9d
DK
249// PrivateHashes /*{{{*/
250class PrivateHashes {
251public:
252 unsigned long long FileSize;
253
254 PrivateHashes() : FileSize(0) {}
255};
256 /*}}}*/
b3501edb
DK
257// Hashes::Add* - Add the contents of data or FD /*{{{*/
258bool Hashes::Add(const unsigned char * const Data,unsigned long long const Size, unsigned int const Hashes)
259{
260 bool Res = true;
586d8704 261APT_IGNORE_DEPRECATED_PUSH
b3501edb
DK
262 if ((Hashes & MD5SUM) == MD5SUM)
263 Res &= MD5.Add(Data, Size);
264 if ((Hashes & SHA1SUM) == SHA1SUM)
265 Res &= SHA1.Add(Data, Size);
266 if ((Hashes & SHA256SUM) == SHA256SUM)
267 Res &= SHA256.Add(Data, Size);
268 if ((Hashes & SHA512SUM) == SHA512SUM)
269 Res &= SHA512.Add(Data, Size);
586d8704 270APT_IGNORE_DEPRECATED_POP
23397c9d 271 d->FileSize += Size;
b3501edb
DK
272 return Res;
273}
274bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
63b1700f
AL
275{
276 unsigned char Buf[64*64];
ce928105 277 bool const ToEOF = (Size == UntilEOF);
04f4e1a3 278 while (Size != 0 || ToEOF)
63b1700f 279 {
650faab0 280 unsigned long long n = sizeof(Buf);
8f3ba4e8 281 if (!ToEOF) n = std::min(Size, n);
9ce3cfc9 282 ssize_t const Res = read(Fd,Buf,n);
650faab0 283 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
1dab797c 284 return false;
04f4e1a3 285 if (ToEOF && Res == 0) // EOF
1dab797c 286 break;
63b1700f 287 Size -= Res;
b3501edb
DK
288 if (Add(Buf, Res, Hashes) == false)
289 return false;
63b1700f
AL
290 }
291 return true;
109eb151 292}
b3501edb 293bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
109eb151
DK
294{
295 unsigned char Buf[64*64];
296 bool const ToEOF = (Size == 0);
297 while (Size != 0 || ToEOF)
298 {
299 unsigned long long n = sizeof(Buf);
300 if (!ToEOF) n = std::min(Size, n);
301 unsigned long long a = 0;
302 if (Fd.Read(Buf, n, &a) == false) // error
303 return false;
304 if (ToEOF == false)
305 {
306 if (a != n) // short read
307 return false;
308 }
309 else if (a == 0) // EOF
310 break;
311 Size -= a;
b3501edb
DK
312 if (Add(Buf, a, Hashes) == false)
313 return false;
109eb151
DK
314 }
315 return true;
63b1700f
AL
316}
317 /*}}}*/
b3501edb
DK
318HashStringList Hashes::GetHashStringList()
319{
320 HashStringList hashes;
586d8704 321APT_IGNORE_DEPRECATED_PUSH
b3501edb
DK
322 hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
323 hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
324 hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
325 hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
586d8704 326APT_IGNORE_DEPRECATED_POP
23397c9d
DK
327 std::string SizeStr;
328 strprintf(SizeStr, "%llu", d->FileSize);
329 hashes.push_back(HashString("Checksum-FileSize", SizeStr));
b3501edb
DK
330 return hashes;
331}
586d8704 332APT_IGNORE_DEPRECATED_PUSH
23397c9d
DK
333Hashes::Hashes() { d = new PrivateHashes(); }
334Hashes::~Hashes() { delete d; }
586d8704 335APT_IGNORE_DEPRECATED_POP