hashsum_template.h 2.8 KB

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