hashes.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // verify the given filename against the currently loaded hash
  46. bool VerifyFile(std::string filename) const;
  47. // generate a hash string from the given filename
  48. bool FromFile(std::string filename);
  49. // helper
  50. std::string toStr() const; // convert to str as "type:hash"
  51. bool empty() const;
  52. bool operator==(HashString const &other) const;
  53. bool operator!=(HashString const &other) const;
  54. // return the list of hashes we support
  55. static APT_CONST const char** SupportedHashes();
  56. };
  57. class HashStringList
  58. {
  59. public:
  60. /** find best hash if no specific one is requested
  61. *
  62. * @param type of the checksum to return, can be \b NULL
  63. * @return If type is \b NULL (or the empty string) it will
  64. * return the 'best' hash; otherwise the hash which was
  65. * specifically requested. If no hash is found \b NULL will be returned.
  66. */
  67. HashString const * find(char const * const type) const;
  68. HashString const * find(std::string const &type) const { return find(type.c_str()); }
  69. /** check if the given hash type is supported
  70. *
  71. * @param type to check
  72. * @return true if supported, otherwise false
  73. */
  74. static APT_PURE bool supported(char const * const type);
  75. /** add the given #HashString to the list
  76. *
  77. * @param hashString to add
  78. * @return true if the hash is added because it is supported and
  79. * not already a different hash of the same type included, otherwise false
  80. */
  81. bool push_back(const HashString &hashString);
  82. /** @return size of the list of HashStrings */
  83. size_t size() const { return list.size(); }
  84. /** take the 'best' hash and verify file with it
  85. *
  86. * @param filename to verify
  87. * @return true if the file matches the hashsum, otherwise false
  88. */
  89. bool VerifyFile(std::string filename) const;
  90. /** is the list empty ?
  91. *
  92. * @return \b true if the list is empty, otherwise \b false
  93. */
  94. bool empty() const { return list.empty(); }
  95. /** has the list at least one good entry
  96. *
  97. * similar to #empty, but handles forced hashes.
  98. *
  99. * @return if no hash is forced, same result as #empty,
  100. * if one is forced \b true if this has is available, \b false otherwise
  101. */
  102. bool usable() const;
  103. typedef std::vector<HashString>::const_iterator const_iterator;
  104. /** iterator to the first element */
  105. const_iterator begin() const { return list.begin(); }
  106. /** iterator to the end element */
  107. const_iterator end() const { return list.end(); }
  108. /** start fresh with a clear list */
  109. void clear() { list.clear(); }
  110. /** compare two HashStringList for similarity.
  111. *
  112. * Two lists are similar if at least one hashtype is in both lists
  113. * and the hashsum matches. All hashes are checked by default,
  114. * if one doesn't match false is returned regardless of how many
  115. * matched before. If a hash is forced, only this hash is compared,
  116. * all others are ignored.
  117. */
  118. bool operator==(HashStringList const &other) const;
  119. bool operator!=(HashStringList const &other) const;
  120. HashStringList() {}
  121. // simplifying API-compatibility constructors
  122. HashStringList(std::string const &hash) {
  123. if (hash.empty() == false)
  124. list.push_back(HashString(hash));
  125. }
  126. HashStringList(char const * const hash) {
  127. if (hash != NULL && hash[0] != '\0')
  128. list.push_back(HashString(hash));
  129. }
  130. private:
  131. std::vector<HashString> list;
  132. };
  133. class Hashes
  134. {
  135. /** \brief dpointer placeholder */
  136. void *d;
  137. public:
  138. /* those will disappear in the future as it is hard to add new ones this way.
  139. * Use Add* to build the results and get them via GetHashStringList() instead */
  140. APT_DEPRECATED MD5Summation MD5;
  141. APT_DEPRECATED SHA1Summation SHA1;
  142. APT_DEPRECATED SHA256Summation SHA256;
  143. APT_DEPRECATED SHA512Summation SHA512;
  144. static const int UntilEOF = 0;
  145. bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes = ~0);
  146. inline bool Add(const char * const Data)
  147. {return Add((unsigned char const * const)Data,strlen(Data));};
  148. inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
  149. {return Add(Beg,End-Beg);};
  150. enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
  151. SHA512SUM = (1 << 3) };
  152. bool AddFD(int const Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0);
  153. bool AddFD(FileFd &Fd,unsigned long long Size = 0, unsigned int const Hashes = ~0);
  154. HashStringList GetHashStringList();
  155. #if __GNUC__ >= 4
  156. #pragma GCC diagnostic push
  157. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  158. #endif
  159. Hashes();
  160. virtual ~Hashes();
  161. #if __GNUC__ >= 4
  162. #pragma GCC diagnostic pop
  163. #endif
  164. private:
  165. APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
  166. {
  167. unsigned int Hashes = ~0;
  168. if (addMD5 == false) Hashes &= ~MD5SUM;
  169. if (addSHA1 == false) Hashes &= ~SHA1SUM;
  170. if (addSHA256 == false) Hashes &= ~SHA256SUM;
  171. if (addSHA512 == false) Hashes &= ~SHA512SUM;
  172. return Hashes;
  173. }
  174. public:
  175. APT_DEPRECATED bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  176. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  177. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  178. };
  179. APT_DEPRECATED bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  180. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  181. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  182. };
  183. };
  184. #endif