apt 2.9.16 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 <vector>
24
25
26
27 class FileFd;
28
29// helper class that contains hash function name
30// and hash
31 class APT_PUBLIC HashString
32{
33 protected:
34 std::string Type;
35 std::string Hash;
36 static const char * _SupportedHashes[10];
37
38 // internal helper
39 std::string GetHashForFile(std::string filename) const;
40
41 public:
42 HashString(std::string Type, std::string Hash);
43#if APT_PKG_ABI > 600
44 explicit HashString(std::string_view StringedHashString); // init from str as "type:hash"
45#else
46 explicit HashString(std::string StringedHashString); // init from str as "type:hash"
47#endif
48 HashString();
49
50 // get hash type used
51 std::string HashType() const { return Type; };
52 std::string HashValue() const { return Hash; };
53
54 // verify the given filename against the currently loaded hash
55 bool VerifyFile(std::string filename) const;
56
57 // generate a hash string from the given filename
58 bool FromFile(std::string filename);
59
60
61 // helper
62 std::string toStr() const; // convert to str as "type:hash"
63 bool empty() const;
64 bool usable() const;
65 bool operator==(HashString const &other) const;
66 bool operator!=(HashString const &other) const;
67
68 // return the list of hashes we support
69 static APT_PURE const char** SupportedHashes();
70#ifdef APT_COMPILING_APT
71 struct APT_HIDDEN HashSupportInfo {
72 std::string_view name;
73 pkgTagSection::Key namekey;
74 std::string_view chksumsname;
75 pkgTagSection::Key chksumskey;
76 };
77 APT_HIDDEN static std::vector<HashSupportInfo> SupportedHashesInfo();
78#endif
79};
80
81 class APT_PUBLIC HashStringList
82{
83 public:
91 HashString const * find(char const * const type) const;
92 HashString const * find(std::string const &type) const { return find(type.c_str()); }
93
100 unsigned long long FileSize() const;
101
107 bool FileSize(unsigned long long const Size);
108
114 static APT_PURE bool supported(char const * const type);
121 bool push_back(const HashString &hashString);
123 size_t size() const { return list.size(); }
124
130 bool VerifyFile(std::string filename) const;
131
136 bool empty() const { return list.empty(); }
137
145 bool usable() const;
146
147 typedef std::vector<HashString>::const_iterator const_iterator;
148
150 const_iterator begin() const { return list.begin(); }
151
153 const_iterator end() const { return list.end(); }
154
156 void clear() { list.clear(); }
157
166 bool operator==(HashStringList const &other) const;
167 bool operator!=(HashStringList const &other) const;
168
169 HashStringList() {}
170
171 // simplifying API-compatibility constructors
172 explicit HashStringList(std::string const &hash) {
173 if (hash.empty() == false)
174 list.push_back(HashString(hash));
175 }
176 explicit HashStringList(char const * const hash) {
177 if (hash != NULL && hash[0] != '\0')
178 list.push_back(HashString(hash));
179 }
180
181 private:
182 std::vector<HashString> list;
183};
184
185 class PrivateHashes;
186 class APT_PUBLIC Hashes
187{
188 PrivateHashes * const d;
189 public:
190 static const int UntilEOF = 0;
191
192 bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2);
193 inline bool Add(const char * const Data) APT_NONNULL(2)
194 {return Add(reinterpret_cast<unsigned char const *>(Data),strlen(Data));};
195 inline bool Add(const char *const Data, unsigned long long const Size) APT_NONNULL(2)
196 {
197 return Add(reinterpret_cast<unsigned char const *>(Data), Size);
198 };
199 inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3)
200 {return Add(Beg,End-Beg);};
201
202 enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
203 SHA512SUM = (1 << 3) };
204 bool AddFD(int const Fd,unsigned long long Size = 0);
205 bool AddFD(FileFd &Fd,unsigned long long Size = 0);
206
207 HashStringList GetHashStringList();
208
210 HashString GetHashString(SupportedHashes hash);
211
217 Hashes();
219 explicit Hashes(unsigned int const Hashes);
221 explicit Hashes(HashStringList const &Hashes);
222 virtual ~Hashes();
223};
224
225#endif
FileFd
Definition fileutl.h:42
HashStringList
Definition hashes.h:82
HashStringList::size
size_t size() const
Definition hashes.h:123
HashStringList::begin
const_iterator begin() const
Definition hashes.h:150
HashStringList::empty
bool empty() const
Definition hashes.h:136
HashStringList::push_back
bool push_back(const HashString &hashString)
Definition hashes.cc:248
HashStringList::clear
void clear()
Definition hashes.h:156
HashStringList::end
const_iterator end() const
Definition hashes.h:153
HashString
Definition hashes.h:32
Hashes
Definition hashes.h:187
PrivateHashes
Definition hashes.cc:316