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