hashes.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $
  4. /* ######################################################################
  5. Hashes - Simple wrapper around the hash functions
  6. This is just used to make building the methods simpler, this is the
  7. only interface required..
  8. ##################################################################### */
  9. /*}}}*/
  10. #ifndef APTPKG_HASHES_H
  11. #define APTPKG_HASHES_H
  12. #include <apt-pkg/md5.h>
  13. #include <apt-pkg/sha1.h>
  14. #include <apt-pkg/sha2.h>
  15. #include <apt-pkg/macros.h>
  16. #include <cstring>
  17. #include <string>
  18. #ifndef APT_8_CLEANER_HEADERS
  19. using std::min;
  20. using std::vector;
  21. #endif
  22. #ifndef APT_10_CLEANER_HEADERS
  23. #include <apt-pkg/fileutl.h>
  24. #include <algorithm>
  25. #include <vector>
  26. #endif
  27. class FileFd;
  28. // helper class that contains hash function name
  29. // and hash
  30. class HashString
  31. {
  32. protected:
  33. std::string Type;
  34. std::string Hash;
  35. static const char * _SupportedHashes[10];
  36. // internal helper
  37. std::string GetHashForFile(std::string filename) const;
  38. public:
  39. HashString(std::string Type, std::string Hash);
  40. HashString(std::string StringedHashString); // init from str as "type:hash"
  41. HashString();
  42. // get hash type used
  43. std::string HashType() const { return Type; };
  44. std::string HashValue() const { return Hash; };
  45. APT_DEPRECATED_MSG("method was const-ified") std::string HashType() { return Type; };
  46. APT_DEPRECATED_MSG("method was const-ified") std::string HashValue() { return Hash; };
  47. // verify the given filename against the currently loaded hash
  48. bool VerifyFile(std::string filename) const;
  49. // generate a hash string from the given filename
  50. bool FromFile(std::string filename);
  51. // helper
  52. std::string toStr() const; // convert to str as "type:hash"
  53. bool empty() const;
  54. bool usable() const;
  55. bool operator==(HashString const &other) const;
  56. bool operator!=(HashString const &other) const;
  57. // return the list of hashes we support
  58. static APT_CONST const char** SupportedHashes();
  59. };
  60. class HashStringList
  61. {
  62. public:
  63. /** find best hash if no specific one is requested
  64. *
  65. * @param type of the checksum to return, can be \b NULL
  66. * @return If type is \b NULL (or the empty string) it will
  67. * return the 'best' hash; otherwise the hash which was
  68. * specifically requested. If no hash is found \b NULL will be returned.
  69. */
  70. HashString const * find(char const * const type) const;
  71. HashString const * find(std::string const &type) const { return find(type.c_str()); }
  72. /** finds the filesize hash and returns it as number
  73. *
  74. * @return beware: if the size isn't known we return \b 0 here,
  75. * just like we would do for an empty file. If that is a problem
  76. * for you have to get the size manually out of the list.
  77. */
  78. unsigned long long FileSize() const;
  79. /** sets the filesize hash
  80. *
  81. * @param Size of the file
  82. * @return @see #push_back
  83. */
  84. bool FileSize(unsigned long long const Size);
  85. /** check if the given hash type is supported
  86. *
  87. * @param type to check
  88. * @return true if supported, otherwise false
  89. */
  90. static APT_PURE bool supported(char const * const type);
  91. /** add the given #HashString to the list
  92. *
  93. * @param hashString to add
  94. * @return true if the hash is added because it is supported and
  95. * not already a different hash of the same type included, otherwise false
  96. */
  97. bool push_back(const HashString &hashString);
  98. /** @return size of the list of HashStrings */
  99. size_t size() const { return list.size(); }
  100. /** verify file against all hashes in the list
  101. *
  102. * @param filename to verify
  103. * @return true if the file matches the hashsum, otherwise false
  104. */
  105. bool VerifyFile(std::string filename) const;
  106. /** is the list empty ?
  107. *
  108. * @return \b true if the list is empty, otherwise \b false
  109. */
  110. bool empty() const { return list.empty(); }
  111. /** has the list at least one good entry
  112. *
  113. * similar to #empty, but handles forced hashes.
  114. *
  115. * @return if no hash is forced, same result as #empty,
  116. * if one is forced \b true if this has is available, \b false otherwise
  117. */
  118. bool usable() const;
  119. typedef std::vector<HashString>::const_iterator const_iterator;
  120. /** iterator to the first element */
  121. const_iterator begin() const { return list.begin(); }
  122. /** iterator to the end element */
  123. const_iterator end() const { return list.end(); }
  124. /** start fresh with a clear list */
  125. void clear() { list.clear(); }
  126. /** compare two HashStringList for similarity.
  127. *
  128. * Two lists are similar if at least one hashtype is in both lists
  129. * and the hashsum matches. All hashes are checked by default,
  130. * if one doesn't match false is returned regardless of how many
  131. * matched before. If a hash is forced, only this hash is compared,
  132. * all others are ignored.
  133. */
  134. bool operator==(HashStringList const &other) const;
  135. bool operator!=(HashStringList const &other) const;
  136. HashStringList() {}
  137. // simplifying API-compatibility constructors
  138. HashStringList(std::string const &hash) {
  139. if (hash.empty() == false)
  140. list.push_back(HashString(hash));
  141. }
  142. HashStringList(char const * const hash) {
  143. if (hash != NULL && hash[0] != '\0')
  144. list.push_back(HashString(hash));
  145. }
  146. private:
  147. std::vector<HashString> list;
  148. };
  149. class PrivateHashes;
  150. class Hashes
  151. {
  152. PrivateHashes * const d;
  153. public:
  154. /* those will disappear in the future as it is hard to add new ones this way.
  155. * Use Add* to build the results and get them via GetHashStringList() instead */
  156. APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") MD5Summation MD5;
  157. APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA1Summation SHA1;
  158. APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA256Summation SHA256;
  159. APT_DEPRECATED_MSG("Use general .Add* and .GetHashStringList methods instead of hardcoding specific hashes") SHA512Summation SHA512;
  160. static const int UntilEOF = 0;
  161. bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2);
  162. APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes) APT_NONNULL(2);
  163. inline bool Add(const char * const Data) APT_NONNULL(2)
  164. {return Add((unsigned char const * const)Data,strlen(Data));};
  165. inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3)
  166. {return Add(Beg,End-Beg);};
  167. enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
  168. SHA512SUM = (1 << 3) };
  169. bool AddFD(int const Fd,unsigned long long Size = 0);
  170. APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes);
  171. bool AddFD(FileFd &Fd,unsigned long long Size = 0);
  172. APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes);
  173. HashStringList GetHashStringList();
  174. APT_IGNORE_DEPRECATED_PUSH
  175. /** create a Hashes object to calculate all supported hashes
  176. *
  177. * If ALL is too much, you can limit which Hashes are calculated
  178. * with the following other constructors which mention explicitly
  179. * which hashes to generate. */
  180. Hashes();
  181. /** @param Hashes bitflag composed of #SupportedHashes */
  182. Hashes(unsigned int const Hashes);
  183. /** @param Hashes is a list of hashes */
  184. Hashes(HashStringList const &Hashes);
  185. virtual ~Hashes();
  186. APT_IGNORE_DEPRECATED_POP
  187. private:
  188. APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
  189. {
  190. unsigned int Hashes = ~0;
  191. if (addMD5 == false) Hashes &= ~MD5SUM;
  192. if (addSHA1 == false) Hashes &= ~SHA1SUM;
  193. if (addSHA256 == false) Hashes &= ~SHA256SUM;
  194. if (addSHA512 == false) Hashes &= ~SHA512SUM;
  195. return Hashes;
  196. }
  197. public:
  198. APT_IGNORE_DEPRECATED_PUSH
  199. APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  200. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  201. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  202. };
  203. APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  204. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  205. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  206. };
  207. APT_IGNORE_DEPRECATED_POP
  208. };
  209. #endif