hashsum_template.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. bool Set(std::string Str)
  68. {
  69. return Hex2Num(Str,Sum,sizeof(Sum));
  70. }
  71. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  72. APT_HIDDEN bool Set(APT::StringView Str)
  73. {
  74. return Hex2Num(Str,Sum,sizeof(Sum));
  75. }
  76. APT_HIDDEN bool Set(const char *Str)
  77. {
  78. return Hex2Num(APT::StringView(Str),Sum,sizeof(Sum));
  79. }
  80. #endif
  81. inline void Set(unsigned char S[N/8])
  82. {
  83. for (int I = 0; I != sizeof(Sum); ++I)
  84. Sum[I] = S[I];
  85. }
  86. explicit HashSumValue(std::string const &Str)
  87. {
  88. memset(Sum,0,sizeof(Sum));
  89. Set(Str);
  90. }
  91. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  92. APT_HIDDEN explicit HashSumValue(APT::StringView const &Str)
  93. {
  94. memset(Sum,0,sizeof(Sum));
  95. Set(Str);
  96. }
  97. APT_HIDDEN explicit HashSumValue(const char *Str)
  98. {
  99. memset(Sum,0,sizeof(Sum));
  100. Set(Str);
  101. }
  102. #endif
  103. HashSumValue()
  104. {
  105. memset(Sum,0,sizeof(Sum));
  106. }
  107. };
  108. class SummationImplementation
  109. {
  110. public:
  111. virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0;
  112. inline bool Add(const char *inbuf, unsigned long long const inlen)
  113. { return Add((const unsigned char *)inbuf, inlen); }
  114. inline bool Add(const unsigned char *Data)
  115. { return Add(Data, strlen((const char *)Data)); }
  116. inline bool Add(const char *Data)
  117. { return Add((const unsigned char *)Data, strlen(Data)); }
  118. inline bool Add(const unsigned char *Beg, const unsigned char *End)
  119. { return Add(Beg, End - Beg); }
  120. inline bool Add(const char *Beg, const char *End)
  121. { return Add((const unsigned char *)Beg, End - Beg); }
  122. bool AddFD(int Fd, unsigned long long Size = 0);
  123. bool AddFD(FileFd &Fd, unsigned long long Size = 0);
  124. };
  125. #endif