]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashsum_template.h
Switch performance critical code to use APT::StringView
[apt.git] / apt-pkg / contrib / hashsum_template.h
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
14 #include <string>
15 #include <cstring>
16 #ifdef APT_PKG_EXPOSE_STRING_VIEW
17 #include <apt-pkg/string_view.h>
18 #endif
19
20 #include <apt-pkg/strutl.h>
21
22 #ifndef APT_10_CLEANER_HEADERS
23 #include <apt-pkg/fileutl.h>
24 #include <algorithm>
25 #include <stdint.h>
26 #endif
27 #ifndef APT_8_CLEANER_HEADERS
28 using std::string;
29 using std::min;
30 #endif
31
32 class FileFd;
33
34 template<int N>
35 class HashSumValue
36 {
37 unsigned char Sum[N/8];
38
39 public:
40
41 // Accessors
42 bool operator ==(const HashSumValue &rhs) const
43 {
44 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
45 }
46 bool operator !=(const HashSumValue &rhs) const
47 {
48 return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
49 }
50
51 std::string Value() const
52 {
53 char Conv[16] =
54 { '0','1','2','3','4','5','6','7','8','9','a','b',
55 'c','d','e','f'
56 };
57 char Result[((N/8)*2)+1];
58 Result[(N/8)*2] = 0;
59
60 // Convert each char into two letters
61 int J = 0;
62 int I = 0;
63 for (; I != (N/8)*2; J++,I += 2)
64 {
65 Result[I] = Conv[Sum[J] >> 4];
66 Result[I + 1] = Conv[Sum[J] & 0xF];
67 }
68 return std::string(Result);
69 }
70
71 inline void Value(unsigned char S[N/8])
72 {
73 for (int I = 0; I != sizeof(Sum); ++I)
74 S[I] = Sum[I];
75 }
76
77 inline operator std::string() const
78 {
79 return Value();
80 }
81
82 bool Set(std::string Str)
83 {
84 return Hex2Num(Str,Sum,sizeof(Sum));
85 }
86 #ifdef APT_PKG_EXPOSE_STRING_VIEW
87 APT_HIDDEN bool Set(APT::StringView Str)
88 {
89 return Hex2Num(Str,Sum,sizeof(Sum));
90 }
91 APT_HIDDEN bool Set(const char *Str)
92 {
93 return Hex2Num(APT::StringView(Str),Sum,sizeof(Sum));
94 }
95 #endif
96 inline void Set(unsigned char S[N/8])
97 {
98 for (int I = 0; I != sizeof(Sum); ++I)
99 Sum[I] = S[I];
100 }
101
102 explicit HashSumValue(std::string const &Str)
103 {
104 memset(Sum,0,sizeof(Sum));
105 Set(Str);
106 }
107 #ifdef APT_PKG_EXPOSE_STRING_VIEW
108 APT_HIDDEN explicit HashSumValue(APT::StringView const &Str)
109 {
110 memset(Sum,0,sizeof(Sum));
111 Set(Str);
112 }
113 APT_HIDDEN explicit HashSumValue(const char *Str)
114 {
115 memset(Sum,0,sizeof(Sum));
116 Set(Str);
117 }
118 #endif
119 HashSumValue()
120 {
121 memset(Sum,0,sizeof(Sum));
122 }
123 };
124
125 class SummationImplementation
126 {
127 public:
128 virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0;
129 inline bool Add(const char *inbuf, unsigned long long const inlen)
130 { return Add((const unsigned char *)inbuf, inlen); }
131
132 inline bool Add(const unsigned char *Data)
133 { return Add(Data, strlen((const char *)Data)); }
134 inline bool Add(const char *Data)
135 { return Add((const unsigned char *)Data, strlen(Data)); }
136
137 inline bool Add(const unsigned char *Beg, const unsigned char *End)
138 { return Add(Beg, End - Beg); }
139 inline bool Add(const char *Beg, const char *End)
140 { return Add((const unsigned char *)Beg, End - Beg); }
141
142 bool AddFD(int Fd, unsigned long long Size = 0);
143 bool AddFD(FileFd &Fd, unsigned long long Size = 0);
144 };
145
146 #endif