summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/hashsum_template.h
blob: fe0984f4e95376f326a23564ebbd2b855c5451ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// -*- mode: cpp; mode: fold -*-
// Description                                                          /*{{{*/
// $Id: hashsum_template.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
/* ######################################################################

   HashSumValueTemplate - Generic Storage for a hash value
   
   ##################################################################### */
                                                                        /*}}}*/
#ifndef APTPKG_HASHSUM_TEMPLATE_H
#define APTPKG_HASHSUM_TEMPLATE_H

#include <string>
#include <cstring>
#include <algorithm>
#include <stdint.h>

using std::string;
using std::min;

template<int N>
class HashSumValue
{
   unsigned char Sum[N/8];
   
   public:

   // Accessors
   bool operator ==(const HashSumValue &rhs) const
   {
      return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
   };
   bool operator !=(const HashSumValue &rhs) const
   {
      return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
   };

   string Value() const
   {
      char Conv[16] =
      { '0','1','2','3','4','5','6','7','8','9','a','b',
        'c','d','e','f'
      };
      char Result[((N/8)*2)+1];
      Result[(N/8)*2] = 0;
      
      // Convert each char into two letters
      int J = 0;
      int I = 0;
      for (; I != (N/8)*2; J++,I += 2)
      {
         Result[I] = Conv[Sum[J] >> 4];
         Result[I + 1] = Conv[Sum[J] & 0xF];
      }
      return string(Result);
   };
   
   inline void Value(unsigned char S[N/8])
   {
      for (int I = 0; I != sizeof(Sum); I++) 
         S[I] = Sum[I];
   };

   inline operator string() const 
   {
      return Value();
   };

   bool Set(string Str) 
   {
      return Hex2Num(Str,Sum,sizeof(Sum));
   };

   inline void Set(unsigned char S[N/8]) 
   {
      for (int I = 0; I != sizeof(Sum); I++) 
         Sum[I] = S[I];
   };

   HashSumValue(string Str) 
   {
         memset(Sum,0,sizeof(Sum));
         Set(Str);
   }
   HashSumValue()
   {
      memset(Sum,0,sizeof(Sum));
   }
};

class SummationImplementation
{
   public:
   virtual bool Add(const unsigned char *inbuf, unsigned long inlen) = 0;
   inline bool Add(const char *inbuf, unsigned long const inlen)
   { return Add((unsigned char *)inbuf, inlen); };

   inline bool Add(const unsigned char *Data)
   { return Add(Data, strlen((const char *)Data)); };
   inline bool Add(const char *Data)
   { return Add((const unsigned char *)Data, strlen((const char *)Data)); };

   inline bool Add(const unsigned char *Beg, const unsigned char *End)
   { return Add(Beg, End - Beg); };
   inline bool Add(const char *Beg, const char *End)
   { return Add((const unsigned char *)Beg, End - Beg); };

   bool AddFD(int Fd, unsigned long Size = 0);
};

#endif