hashsum_template.h 2.6 KB

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