apt: apt-pkg/contrib/hashes.h Source File

apt 2.3.15 commandline package manager

hashes.h
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Hashes - Simple wrapper around the hash functions
6
7 This is just used to make building the methods simpler, this is the
8 only interface required..
9
10 ##################################################################### */
11 /*}}}*/
12#ifndef APTPKG_HASHES_H
13#define APTPKG_HASHES_H
14
15#include <apt-pkg/macros.h>
16
17#include <cstring>
18#include <string>
19#include <vector>
20
21
22
23class FileFd;
24
25// helper class that contains hash function name
26// and hash
27 class APT_PUBLIC HashString
28{
29 protected:
30 std::string Type;
31 std::string Hash;
32 static const char * _SupportedHashes[10];
33
34 // internal helper
35 std::string GetHashForFile(std::string filename) const;
36
37 public:
38 HashString(std::string Type, std::string Hash);
39 explicit HashString(std::string StringedHashString); // init from str as "type:hash"
40 HashString();
41
42 // get hash type used
43 std::string HashType() const { return Type; };
44 std::string HashValue() const { return Hash; };
45
46 // verify the given filename against the currently loaded hash
47 bool VerifyFile(std::string filename) const;
48
49 // generate a hash string from the given filename
50 bool FromFile(std::string filename);
51
52
53 // helper
54 std::string toStr() const; // convert to str as "type:hash"
55 bool empty() const;
56 bool usable() const;
57 bool operator==(HashString const &other) const;
58 bool operator!=(HashString const &other) const;
59
60 // return the list of hashes we support
61 static APT_PURE const char** SupportedHashes();
62};
63
64 class APT_PUBLIC HashStringList
65{
66 public:
74 HashString const * find(char const * const type) const;
75 HashString const * find(std::string const &type) const { return find(type.c_str()); }
76
83 unsigned long long FileSize() const;
84
90 bool FileSize(unsigned long long const Size);
91
97 static APT_PURE bool supported(char const * const type);
104 bool push_back(const HashString &hashString);
106 size_t size() const { return list.size(); }
107
113 bool VerifyFile(std::string filename) const;
114
119 bool empty() const { return list.empty(); }
120
128 bool usable() const;
129
130 typedef std::vector<HashString>::const_iterator const_iterator;
131
133 const_iterator begin() const { return list.begin(); }
134
136 const_iterator end() const { return list.end(); }
137
139 void clear() { list.clear(); }
140
149 bool operator==(HashStringList const &other) const;
150 bool operator!=(HashStringList const &other) const;
151
152 HashStringList() {}
153
154 // simplifying API-compatibility constructors
155 explicit HashStringList(std::string const &hash) {
156 if (hash.empty() == false)
157 list.push_back(HashString(hash));
158 }
159 explicit HashStringList(char const * const hash) {
160 if (hash != NULL && hash[0] != '\0')
161 list.push_back(HashString(hash));
162 }
163
164 private:
165 std::vector<HashString> list;
166};
167
168class PrivateHashes;
169 class APT_PUBLIC Hashes
170{
171 PrivateHashes * const d;
172 public:
173 static const int UntilEOF = 0;
174
175 bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2);
176 inline bool Add(const char * const Data) APT_NONNULL(2)
177 {return Add(reinterpret_cast<unsigned char const *>(Data),strlen(Data));};
178 inline bool Add(const char *const Data, unsigned long long const Size) APT_NONNULL(2)
179 {
180 return Add(reinterpret_cast<unsigned char const *>(Data), Size);
181 };
182 inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3)
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);
188 bool AddFD(FileFd &Fd,unsigned long long Size = 0);
189
190 HashStringList GetHashStringList();
191
193 HashString GetHashString(SupportedHashes hash);
194
200 Hashes();
202 explicit Hashes(unsigned int const Hashes);
204 explicit Hashes(HashStringList const &Hashes);
205 virtual ~Hashes();
206};
207
208#endif
FileFd
Definition: fileutl.h:39
HashStringList
Definition: hashes.h:65
HashStringList::size
size_t size() const
Definition: hashes.h:106
HashStringList::begin
const_iterator begin() const
Definition: hashes.h:133
HashStringList::empty
bool empty() const
Definition: hashes.h:119
HashStringList::push_back
bool push_back(const HashString &hashString)
Definition: hashes.cc:232
HashStringList::clear
void clear()
Definition: hashes.h:139
HashStringList::end
const_iterator end() const
Definition: hashes.h:136
HashString
Definition: hashes.h:28
Hashes
Definition: hashes.h:170
PrivateHashes
Definition: hashes.cc:300