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