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

apt 2.7.6 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/string_view.h>
19#include <apt-pkg/tagfile-keys.h>
20#endif
21
22#include <cstring>
23#include <string>
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 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 APT::StringView name;
70 pkgTagSection::Key namekey;
71 APT::StringView 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
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:27
FileFd
Definition fileutl.h:41
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::push_back
bool push_back(const HashString &hashString)
Definition hashes.cc:243
HashStringList::clear
void clear()
Definition hashes.h:153
HashStringList::end
const_iterator end() const
Definition hashes.h:150
HashString
Definition hashes.h:33
Hashes
Definition hashes.h:184
PrivateHashes
Definition hashes.cc:311