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