]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashsum_template.h
debian/gbp.conf: point debian-branch to master
[apt.git] / apt-pkg / contrib / hashsum_template.h
CommitLineData
7ac56f8f
MV
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
109eb151 13
7ac56f8f
MV
14#include <string>
15#include <cstring>
7ac56f8f 16
e75aa333
MV
17#include <apt-pkg/strutl.h>
18
453b82a3
DK
19#ifndef APT_10_CLEANER_HEADERS
20#include <apt-pkg/fileutl.h>
21#include <algorithm>
22#include <stdint.h>
23#endif
a4f6bdc8
DK
24#ifndef APT_8_CLEANER_HEADERS
25using std::string;
26using std::min;
27#endif
28
453b82a3
DK
29class FileFd;
30
7ac56f8f
MV
31template<int N>
32class HashSumValue
33{
34 unsigned char Sum[N/8];
cf4ff3b7 35
7ac56f8f
MV
36 public:
37
38 // Accessors
39 bool operator ==(const HashSumValue &rhs) const
40 {
41 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
cf4ff3b7 42 }
99a2ea5a
DK
43 bool operator !=(const HashSumValue &rhs) const
44 {
45 return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
cf4ff3b7 46 }
7ac56f8f 47
8f3ba4e8 48 std::string Value() const
7ac56f8f
MV
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;
cf4ff3b7 56
7ac56f8f
MV
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 }
8f3ba4e8 65 return std::string(Result);
cf4ff3b7
DK
66 }
67
7ac56f8f
MV
68 inline void Value(unsigned char S[N/8])
69 {
cf4ff3b7 70 for (int I = 0; I != sizeof(Sum); ++I)
7ac56f8f 71 S[I] = Sum[I];
cf4ff3b7 72 }
7ac56f8f 73
cf4ff3b7 74 inline operator std::string() const
7ac56f8f
MV
75 {
76 return Value();
cf4ff3b7 77 }
7ac56f8f 78
cf4ff3b7 79 bool Set(std::string Str)
7ac56f8f
MV
80 {
81 return Hex2Num(Str,Sum,sizeof(Sum));
cf4ff3b7 82 }
7ac56f8f 83
cf4ff3b7 84 inline void Set(unsigned char S[N/8])
7ac56f8f 85 {
cf4ff3b7 86 for (int I = 0; I != sizeof(Sum); ++I)
7ac56f8f 87 Sum[I] = S[I];
cf4ff3b7 88 }
7ac56f8f 89
e8afd168 90 explicit HashSumValue(std::string const &Str)
7ac56f8f
MV
91 {
92 memset(Sum,0,sizeof(Sum));
93 Set(Str);
94 }
95 HashSumValue()
96 {
97 memset(Sum,0,sizeof(Sum));
98 }
99};
100
c31c1dde
DK
101class SummationImplementation
102{
103 public:
650faab0
DK
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)
cf4ff3b7 106 { return Add((const unsigned char *)inbuf, inlen); }
c31c1dde
DK
107
108 inline bool Add(const unsigned char *Data)
cf4ff3b7 109 { return Add(Data, strlen((const char *)Data)); }
c31c1dde 110 inline bool Add(const char *Data)
e788a834 111 { return Add((const unsigned char *)Data, strlen(Data)); }
c31c1dde
DK
112
113 inline bool Add(const unsigned char *Beg, const unsigned char *End)
cf4ff3b7 114 { return Add(Beg, End - Beg); }
c31c1dde 115 inline bool Add(const char *Beg, const char *End)
cf4ff3b7 116 { return Add((const unsigned char *)Beg, End - Beg); }
c31c1dde 117
650faab0 118 bool AddFD(int Fd, unsigned long long Size = 0);
109eb151 119 bool AddFD(FileFd &Fd, unsigned long long Size = 0);
c31c1dde
DK
120};
121
7ac56f8f 122#endif