apt @VERSION@
hashsum_template.h
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: hashsum_template.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
00004 /* ######################################################################
00005 
00006    HashSumValueTemplate - Generic Storage for a hash value
00007    
00008    ##################################################################### */
00009                                                                         /*}}}*/
00010 #ifndef APTPKG_HASHSUM_TEMPLATE_H
00011 #define APTPKG_HASHSUM_TEMPLATE_H
00012 
00013 #include <string>
00014 #include <cstring>
00015 #include <algorithm>
00016 #include <stdint.h>
00017 
00018 using std::string;
00019 using std::min;
00020 
00021 template<int N>
00022 class HashSumValue
00023 {
00024    unsigned char Sum[N/8];
00025    
00026    public:
00027 
00028    // Accessors
00029    bool operator ==(const HashSumValue &rhs) const
00030    {
00031       return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
00032    }; 
00033 
00034    string Value() const
00035    {
00036       char Conv[16] =
00037       { '0','1','2','3','4','5','6','7','8','9','a','b',
00038         'c','d','e','f'
00039       };
00040       char Result[((N/8)*2)+1];
00041       Result[(N/8)*2] = 0;
00042       
00043       // Convert each char into two letters
00044       int J = 0;
00045       int I = 0;
00046       for (; I != (N/8)*2; J++,I += 2)
00047       {
00048          Result[I] = Conv[Sum[J] >> 4];
00049          Result[I + 1] = Conv[Sum[J] & 0xF];
00050       }
00051       return string(Result);
00052    };
00053    
00054    inline void Value(unsigned char S[N/8])
00055    {
00056       for (int I = 0; I != sizeof(Sum); I++) 
00057          S[I] = Sum[I];
00058    };
00059 
00060    inline operator string() const 
00061    {
00062       return Value();
00063    };
00064 
00065    bool Set(string Str) 
00066    {
00067       return Hex2Num(Str,Sum,sizeof(Sum));
00068    };
00069 
00070    inline void Set(unsigned char S[N/8]) 
00071    {
00072       for (int I = 0; I != sizeof(Sum); I++) 
00073          Sum[I] = S[I];
00074    };
00075 
00076    HashSumValue(string Str) 
00077    {
00078          memset(Sum,0,sizeof(Sum));
00079          Set(Str);
00080    }
00081    HashSumValue()
00082    {
00083       memset(Sum,0,sizeof(Sum));
00084    }
00085 };
00086 
00087 class SummationImplementation
00088 {
00089    public:
00090    virtual bool Add(const unsigned char *inbuf, unsigned long inlen) = 0;
00091    inline bool Add(const char *inbuf, unsigned long const inlen)
00092    { return Add((unsigned char *)inbuf, inlen); };
00093 
00094    inline bool Add(const unsigned char *Data)
00095    { return Add(Data, strlen((const char *)Data)); };
00096    inline bool Add(const char *Data)
00097    { return Add((const unsigned char *)Data, strlen((const char *)Data)); };
00098 
00099    inline bool Add(const unsigned char *Beg, const unsigned char *End)
00100    { return Add(Beg, End - Beg); };
00101    inline bool Add(const char *Beg, const char *End)
00102    { return Add((const unsigned char *)Beg, End - Beg); };
00103 
00104    bool AddFD(int Fd, unsigned long Size = 0);
00105 };
00106 
00107 #endif