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