]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashsum_template.h
template based hashsum implementation
[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
18using std::string;
19using std::min;
20
21template<int N>
22class HashSumValue
23{
24 unsigned char Sum[N/8];
25
26 public:
27
28 // Accessors
29 bool operator ==(const HashSumValue &rhs) const
30 {
31 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
32 };
33
34 string Value() const
35 {
36 char Conv[16] =
37 { '0','1','2','3','4','5','6','7','8','9','a','b',
38 'c','d','e','f'
39 };
40 char Result[((N/8)*2)+1];
41 Result[(N/8)*2] = 0;
42
43 // Convert each char into two letters
44 int J = 0;
45 int I = 0;
46 for (; I != (N/8)*2; J++,I += 2)
47 {
48 Result[I] = Conv[Sum[J] >> 4];
49 Result[I + 1] = Conv[Sum[J] & 0xF];
50 }
51 return string(Result);
52 };
53
54 inline void Value(unsigned char S[N/8])
55 {
56 for (int I = 0; I != sizeof(Sum); I++)
57 S[I] = Sum[I];
58 };
59
60 inline operator string() const
61 {
62 return Value();
63 };
64
65 bool Set(string Str)
66 {
67 return Hex2Num(Str,Sum,sizeof(Sum));
68 };
69
70 inline void Set(unsigned char S[N/8])
71 {
72 for (int I = 0; I != sizeof(Sum); I++)
73 Sum[I] = S[I];
74 };
75
76 HashSumValue(string Str)
77 {
78 memset(Sum,0,sizeof(Sum));
79 Set(Str);
80 }
81 HashSumValue()
82 {
83 memset(Sum,0,sizeof(Sum));
84 }
85};
86
87#endif