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