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