hashsum_template.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashsum_template.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
  4. /* ######################################################################
  5. HashSumValueTemplate - Generic Storage for a hash value
  6. ##################################################################### */
  7. /*}}}*/
  8. #ifndef APTPKG_HASHSUM_TEMPLATE_H
  9. #define APTPKG_HASHSUM_TEMPLATE_H
  10. #include <string>
  11. #include <cstring>
  12. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  13. #include <apt-pkg/string_view.h>
  14. #endif
  15. #include <apt-pkg/strutl.h>
  16. #ifndef APT_10_CLEANER_HEADERS
  17. #include <apt-pkg/fileutl.h>
  18. #include <algorithm>
  19. #include <stdint.h>
  20. #endif
  21. #ifndef APT_8_CLEANER_HEADERS
  22. using std::string;
  23. using std::min;
  24. #endif
  25. class FileFd;
  26. template<int N>
  27. class HashSumValue
  28. {
  29. unsigned char Sum[N/8];
  30. public:
  31. // Accessors
  32. bool operator ==(const HashSumValue &rhs) const
  33. {
  34. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  35. }
  36. bool operator !=(const HashSumValue &rhs) const
  37. {
  38. return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
  39. }
  40. std::string Value() const
  41. {
  42. char Conv[16] =
  43. { '0','1','2','3','4','5','6','7','8','9','a','b',
  44. 'c','d','e','f'
  45. };
  46. char Result[((N/8)*2)+1];
  47. Result[(N/8)*2] = 0;
  48. // Convert each char into two letters
  49. int J = 0;
  50. int I = 0;
  51. for (; I != (N/8)*2; J++,I += 2)
  52. {
  53. Result[I] = Conv[Sum[J] >> 4];
  54. Result[I + 1] = Conv[Sum[J] & 0xF];
  55. }
  56. return std::string(Result);
  57. }
  58. inline void Value(unsigned char S[N/8])
  59. {
  60. for (int I = 0; I != sizeof(Sum); ++I)
  61. S[I] = Sum[I];
  62. }
  63. inline operator std::string() const
  64. {
  65. return Value();
  66. }
  67. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  68. APT_HIDDEN bool Set(APT::StringView Str)
  69. {
  70. return Hex2Num(Str,Sum,sizeof(Sum));
  71. }
  72. #else
  73. bool Set(std::string Str)
  74. {
  75. return Hex2Num(Str,Sum,sizeof(Sum));
  76. }
  77. #endif
  78. inline void Set(unsigned char S[N/8])
  79. {
  80. for (int I = 0; I != sizeof(Sum); ++I)
  81. Sum[I] = S[I];
  82. }
  83. explicit HashSumValue(std::string const &Str)
  84. {
  85. memset(Sum,0,sizeof(Sum));
  86. Set(Str);
  87. }
  88. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  89. APT_HIDDEN explicit HashSumValue(APT::StringView const &Str)
  90. {
  91. memset(Sum,0,sizeof(Sum));
  92. Set(Str);
  93. }
  94. APT_HIDDEN explicit HashSumValue(const char *Str)
  95. {
  96. memset(Sum,0,sizeof(Sum));
  97. Set(Str);
  98. }
  99. #endif
  100. HashSumValue()
  101. {
  102. memset(Sum,0,sizeof(Sum));
  103. }
  104. };
  105. class SummationImplementation
  106. {
  107. public:
  108. virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_NONNULL(2) = 0;
  109. inline bool Add(const char *inbuf, unsigned long long const inlen) APT_NONNULL(2)
  110. { return Add((const unsigned char *)inbuf, inlen); }
  111. inline bool Add(const unsigned char *Data) APT_NONNULL(2)
  112. { return Add(Data, strlen((const char *)Data)); }
  113. inline bool Add(const char *Data) APT_NONNULL(2)
  114. { return Add((const unsigned char *)Data, strlen(Data)); }
  115. inline bool Add(const unsigned char *Beg, const unsigned char *End) APT_NONNULL(2,3)
  116. { return Add(Beg, End - Beg); }
  117. inline bool Add(const char *Beg, const char *End) APT_NONNULL(2,3)
  118. { return Add((const unsigned char *)Beg, End - Beg); }
  119. bool AddFD(int Fd, unsigned long long Size = 0);
  120. bool AddFD(FileFd &Fd, unsigned long long Size = 0);
  121. };
  122. #endif