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