hashsum_template.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. using std::string;
  15. using std::min;
  16. template<int N>
  17. class HashSumValue
  18. {
  19. unsigned char Sum[N/8];
  20. public:
  21. // Accessors
  22. bool operator ==(const HashSumValue &rhs) const
  23. {
  24. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  25. };
  26. string Value() const
  27. {
  28. char Conv[16] =
  29. { '0','1','2','3','4','5','6','7','8','9','a','b',
  30. 'c','d','e','f'
  31. };
  32. char Result[((N/8)*2)+1];
  33. Result[(N/8)*2] = 0;
  34. // Convert each char into two letters
  35. int J = 0;
  36. int I = 0;
  37. for (; I != (N/8)*2; J++,I += 2)
  38. {
  39. Result[I] = Conv[Sum[J] >> 4];
  40. Result[I + 1] = Conv[Sum[J] & 0xF];
  41. }
  42. return string(Result);
  43. };
  44. inline void Value(unsigned char S[N/8])
  45. {
  46. for (int I = 0; I != sizeof(Sum); I++)
  47. S[I] = Sum[I];
  48. };
  49. inline operator string() const
  50. {
  51. return Value();
  52. };
  53. bool Set(string Str)
  54. {
  55. return Hex2Num(Str,Sum,sizeof(Sum));
  56. };
  57. inline void Set(unsigned char S[N/8])
  58. {
  59. for (int I = 0; I != sizeof(Sum); I++)
  60. Sum[I] = S[I];
  61. };
  62. HashSumValue(string Str)
  63. {
  64. memset(Sum,0,sizeof(Sum));
  65. Set(Str);
  66. }
  67. HashSumValue()
  68. {
  69. memset(Sum,0,sizeof(Sum));
  70. }
  71. };
  72. #endif