apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

hashes.h
Go to the documentation of this file.
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 
23 class FileFd;
24 
25 // helper class that contains hash function name
26 // and hash
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 
65 {
66  public:
67  /** find best hash if no specific one is requested
68  *
69  * @param type of the checksum to return, can be \b NULL
70  * @return If type is \b NULL (or the empty string) it will
71  * return the 'best' hash; otherwise the hash which was
72  * specifically requested. If no hash is found \b NULL will be returned.
73  */
74  HashString const * find(char const * const type) const;
75  HashString const * find(std::string const &type) const { return find(type.c_str()); }
76 
77  /** finds the filesize hash and returns it as number
78  *
79  * @return beware: if the size isn't known we return \b 0 here,
80  * just like we would do for an empty file. If that is a problem
81  * for you have to get the size manually out of the list.
82  */
83  unsigned long long FileSize() const;
84 
85  /** sets the filesize hash
86  *
87  * @param Size of the file
88  * @return @see #push_back
89  */
90  bool FileSize(unsigned long long const Size);
91 
92  /** check if the given hash type is supported
93  *
94  * @param type to check
95  * @return true if supported, otherwise false
96  */
97  static APT_PURE bool supported(char const * const type);
98  /** add the given #HashString to the list
99  *
100  * @param hashString to add
101  * @return true if the hash is added because it is supported and
102  * not already a different hash of the same type included, otherwise false
103  */
104  bool push_back(const HashString &hashString);
105  /** @return size of the list of HashStrings */
106  size_t size() const { return list.size(); }
107 
108  /** verify file against all hashes in the list
109  *
110  * @param filename to verify
111  * @return true if the file matches the hashsum, otherwise false
112  */
113  bool VerifyFile(std::string filename) const;
114 
115  /** is the list empty ?
116  *
117  * @return \b true if the list is empty, otherwise \b false
118  */
119  bool empty() const { return list.empty(); }
120 
121  /** has the list at least one good entry
122  *
123  * similar to #empty, but handles forced hashes.
124  *
125  * @return if no hash is forced, same result as #empty,
126  * if one is forced \b true if this has is available, \b false otherwise
127  */
128  bool usable() const;
129 
130  typedef std::vector<HashString>::const_iterator const_iterator;
131 
132  /** iterator to the first element */
133  const_iterator begin() const { return list.begin(); }
134 
135  /** iterator to the end element */
136  const_iterator end() const { return list.end(); }
137 
138  /** start fresh with a clear list */
139  void clear() { list.clear(); }
140 
141  /** compare two HashStringList for similarity.
142  *
143  * Two lists are similar if at least one hashtype is in both lists
144  * and the hashsum matches. All hashes are checked by default,
145  * if one doesn't match false is returned regardless of how many
146  * matched before. If a hash is forced, only this hash is compared,
147  * all others are ignored.
148  */
149  bool operator==(HashStringList const &other) const;
150  bool operator!=(HashStringList const &other) const;
151 
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 
168 class PrivateHashes;
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 
192  /** Get a specific hash. It is an error to use a hash that was not hashes */
193  HashString GetHashString(SupportedHashes hash);
194 
195  /** create a Hashes object to calculate all supported hashes
196  *
197  * If ALL is too much, you can limit which Hashes are calculated
198  * with the following other constructors which mention explicitly
199  * which hashes to generate. */
200  Hashes();
201  /** @param Hashes bitflag composed of #SupportedHashes */
202  explicit Hashes(unsigned int const Hashes);
203  /** @param Hashes is a list of hashes */
204  explicit Hashes(HashStringList const &Hashes);
205  virtual ~~Hashes();
206 };
207 
208 #endif
static bool operator!=(LineBuffer const &buf, APT::StringView const exp) noexcept
Definition: gpgv.cc:108
static bool operator==(LineBuffer const &buf, APT::StringView const exp) noexcept
Definition: gpgv.cc:104
Definition: fileutl.h:39
HashStringList(char const *const hash)
Definition: hashes.h:159
HashStringList(std::string const &hash)
Definition: hashes.h:155
size_t size() const
Definition: hashes.h:106
const_iterator begin() const
Definition: hashes.h:133
HashStringList()
Definition: hashes.h:152
std::vector< HashString >::const_iterator const_iterator
Definition: hashes.h:130
std::vector< HashString > list
Definition: hashes.h:165
bool empty() const
Definition: hashes.h:119
HashString const * find(std::string const &type) const
Definition: hashes.h:75
void clear()
Definition: hashes.h:139
const_iterator end() const
Definition: hashes.h:136
std::string Hash
Definition: hashes.h:31
std::string Type
Definition: hashes.h:30
std::string HashValue() const
Definition: hashes.h:44
std::string HashType() const
Definition: hashes.h:43
Definition: hashes.h:170
SupportedHashes
Definition: hashes.h:185
bool Add(const unsigned char *const Beg, const unsigned char *const End) APT_NONNULL(2
bool Add(const char *const Data) APT_NONNULL(2)
Definition: hashes.h:176
bool Add(const char *const Data, unsigned long long const Size) APT_NONNULL(2)
Definition: hashes.h:178
PrivateHashes *const d
Definition: hashes.h:171
#define APT_PURE
Definition: macros.h:56
#define APT_PUBLIC
Definition: macros.h:77
#define APT_NONNULL(...)
Definition: macros.h:67