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