hashsum_template.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <apt-pkg/strutl.h>
  13. #ifndef APT_10_CLEANER_HEADERS
  14. #include <apt-pkg/fileutl.h>
  15. #include <algorithm>
  16. #include <stdint.h>
  17. #endif
  18. #ifndef APT_8_CLEANER_HEADERS
  19. using std::string;
  20. using std::min;
  21. #endif
  22. class FileFd;
  23. template<int N>
  24. class HashSumValue
  25. {
  26. unsigned char Sum[N/8];
  27. public:
  28. // Accessors
  29. bool operator ==(const HashSumValue &rhs) const
  30. {
  31. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  32. }
  33. bool operator !=(const HashSumValue &rhs) const
  34. {
  35. return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
  36. }
  37. std::string Value() const
  38. {
  39. char Conv[16] =
  40. { '0','1','2','3','4','5','6','7','8','9','a','b',
  41. 'c','d','e','f'
  42. };
  43. char Result[((N/8)*2)+1];
  44. Result[(N/8)*2] = 0;
  45. // Convert each char into two letters
  46. int J = 0;
  47. int I = 0;
  48. for (; I != (N/8)*2; J++,I += 2)
  49. {
  50. Result[I] = Conv[Sum[J] >> 4];
  51. Result[I + 1] = Conv[Sum[J] & 0xF];
  52. }
  53. return std::string(Result);
  54. }
  55. inline void Value(unsigned char S[N/8])
  56. {
  57. for (int I = 0; I != sizeof(Sum); ++I)
  58. S[I] = Sum[I];
  59. }
  60. inline operator std::string() const
  61. {
  62. return Value();
  63. }
  64. bool Set(std::string Str)
  65. {
  66. return Hex2Num(Str,Sum,sizeof(Sum));
  67. }
  68. inline void Set(unsigned char S[N/8])
  69. {
  70. for (int I = 0; I != sizeof(Sum); ++I)
  71. Sum[I] = S[I];
  72. }
  73. HashSumValue(std::string Str)
  74. {
  75. memset(Sum,0,sizeof(Sum));
  76. Set(Str);
  77. }
  78. HashSumValue()
  79. {
  80. memset(Sum,0,sizeof(Sum));
  81. }
  82. };
  83. class SummationImplementation
  84. {
  85. public:
  86. virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0;
  87. inline bool Add(const char *inbuf, unsigned long long const inlen)
  88. { return Add((const unsigned char *)inbuf, inlen); }
  89. inline bool Add(const unsigned char *Data)
  90. { return Add(Data, strlen((const char *)Data)); }
  91. inline bool Add(const char *Data)
  92. { return Add((const unsigned char *)Data, strlen(Data)); }
  93. inline bool Add(const unsigned char *Beg, const unsigned char *End)
  94. { return Add(Beg, End - Beg); }
  95. inline bool Add(const char *Beg, const char *End)
  96. { return Add((const unsigned char *)Beg, End - Beg); }
  97. bool AddFD(int Fd, unsigned long long Size = 0);
  98. bool AddFD(FileFd &Fd, unsigned long long Size = 0);
  99. };
  100. #endif