hashsum_template.h 2.8 KB

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