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

apt 3.3.1 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#ifdef APT_COMPILING_APT
18#include <apt-pkg/tagfile-keys.h>
19#endif
20
21#include <cstring>
22#include <string>
23#include <string_view>
24#include <vector>
25
26
27
28class FileFd;
29
30// helper class that contains hash function name
31// and hash
32 class APT_PUBLIC HashString
33{
34 protected:
35 std::string Type;
36 std::string Hash;
37 static const char * _SupportedHashes[10];
38
39 // internal helper
40 std::string GetHashForFile(std::string filename) const;
41
42 public:
43 HashString(std::string Type, std::string Hash);
44 explicit HashString(std::string_view StringedHashString); // init from str as "type:hash"
45 HashString();
46
47 // get hash type used
48 std::string HashType() const { return Type; };
49 std::string HashValue() const { return Hash; };
50
51 // verify the given filename against the currently loaded hash
52 bool VerifyFile(std::string filename) const;
53
54 // generate a hash string from the given filename
55 bool FromFile(std::string filename);
56
57
58 // helper
59 std::string toStr() const; // convert to str as "type:hash"
60 bool empty() const;
61 bool usable() const;
62 bool operator==(HashString const &other) const;
63 bool operator!=(HashString const &other) const;
64
65 // return the list of hashes we support
66 static APT_PURE const char** SupportedHashes();
67#ifdef APT_COMPILING_APT
68 struct APT_HIDDEN HashSupportInfo {
69 std::string_view name;
70 pkgTagSection::Key namekey;
71 std::string_view chksumsname;
72 pkgTagSection::Key chksumskey;
73 };
74 APT_HIDDEN static std::vector<HashSupportInfo> SupportedHashesInfo();
75#endif
76};
77
78 class APT_PUBLIC HashStringList
79{
80 public:
88 HashString const * find(char const * const type) const;
89 HashString const * find(std::string const &type) const { return find(type.c_str()); }
90
97 unsigned long long FileSize() const;
98
104 bool FileSize(unsigned long long const Size);
105
111 static APT_PURE bool supported(char const * const type);
118 bool push_back(const HashString &hashString);
120 size_t size() const { return list.size(); }
121
127 bool VerifyFile(std::string filename) const;
128
133 bool empty() const { return list.empty(); }
134
142 bool usable() const;
143
144 typedef std::vector<HashString>::const_iterator const_iterator;
145
147 const_iterator begin() const { return list.begin(); }
148
150 const_iterator end() const { return list.end(); }
151
153 void clear() { list.clear(); }
154
163 bool operator==(HashStringList const &other) const;
164 bool operator!=(HashStringList const &other) const;
165
166 HashStringList() {}
167
168 // simplifying API-compatibility constructors
169 explicit HashStringList(std::string const &hash) {
170 if (hash.empty() == false)
171 list.push_back(HashString(hash));
172 }
173 explicit HashStringList(char const * const hash) {
174 if (hash != NULL && hash[0] != '\0')
175 list.push_back(HashString(hash));
176 }
177
178 private:
179 std::vector<HashString> list;
180};
181
182class PrivateHashes;
183 class APT_PUBLIC Hashes
184{
185 PrivateHashes * const d;
186 public:
187 static const int UntilEOF = 0;
188
189 bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2);
190 inline bool Add(const char * const Data) APT_NONNULL(2)
191 {return Add(reinterpret_cast<unsigned char const *>(Data),strlen(Data));};
192 inline bool Add(const char *const Data, unsigned long long const Size) APT_NONNULL(2)
193 {
194 return Add(reinterpret_cast<unsigned char const *>(Data), Size);
195 };
196 inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3)
197 {return Add(Beg,End-Beg);};
198
199 enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
200 SHA512SUM = (1 << 3) };
201 bool AddFD(int const Fd,unsigned long long Size = 0);
202 bool AddFD(FileFd &Fd,unsigned long long Size = 0);
203
204 HashStringList GetHashStringList();
205
207 HashString GetHashString(SupportedHashes hash);
208
214 Hashes();
216 explicit Hashes(unsigned int const Hashes);
218 explicit Hashes(HashStringList const &Hashes);
219 virtual ~Hashes();
220};
221
222#endif
FileFd
Definition fileutl.h:43
HashStringList
Definition hashes.h:79
HashStringList::size
size_t size() const
Definition hashes.h:120
HashStringList::begin
const_iterator begin() const
Definition hashes.h:147
HashStringList::empty
bool empty() const
Definition hashes.h:133
HashStringList::find
HashString const * find(char const *const type) const
Definition hashes.cc:192
HashStringList::push_back
bool push_back(const HashString &hashString)
Definition hashes.cc:233
HashStringList::supported
static APT_PURE bool supported(char const *const type)
Definition hashes.cc:225
HashStringList::clear
void clear()
Definition hashes.h:153
HashStringList::end
const_iterator end() const
Definition hashes.h:150
HashStringList::FileSize
unsigned long long FileSize() const
Definition hashes.cc:211
HashString
Definition hashes.h:33
Hashes::GetHashString
HashString GetHashString(SupportedHashes hash)
Definition hashes.cc:476
Hashes::Hashes
Hashes()
Definition hashes.cc:484
PrivateHashes
Definition hashes.cc:319