do not pollute namespace in the headers with using (Closes: #500198)
[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
7ac56f8f
MV
18template<int N>
19class 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
8f3ba4e8 31 std::string Value() const
7ac56f8f
MV
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 }
8f3ba4e8 48 return std::string(Result);
7ac56f8f
MV
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
8f3ba4e8 57 inline operator std::string() const
7ac56f8f
MV
58 {
59 return Value();
60 };
61
8f3ba4e8 62 bool Set(std::string Str)
7ac56f8f
MV
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
8f3ba4e8 73 HashSumValue(std::string Str)
7ac56f8f
MV
74 {
75 memset(Sum,0,sizeof(Sum));
76 Set(Str);
77 }
78 HashSumValue()
79 {
80 memset(Sum,0,sizeof(Sum));
81 }
82};
83
c31c1dde
DK
84class SummationImplementation
85{
86 public:
650faab0
DK
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)
c31c1dde
DK
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
650faab0 101 bool AddFD(int Fd, unsigned long long Size = 0);
c31c1dde
DK
102};
103
7ac56f8f 104#endif