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 template<int N>
00019 class HashSumValue
00020 {
00021    unsigned char Sum[N/8];
00022    
00023    public:
00024 
00025    // Accessors
00026    bool operator ==(const HashSumValue &rhs) const
00027    {
00028       return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
00029    };
00030    bool operator !=(const HashSumValue &rhs) const
00031    {
00032       return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0;
00033    };
00034 
00035    std::string Value() const
00036    {
00037       char Conv[16] =
00038       { '0','1','2','3','4','5','6','7','8','9','a','b',
00039         'c','d','e','f'
00040       };
00041       char Result[((N/8)*2)+1];
00042       Result[(N/8)*2] = 0;
00043       
00044       // Convert each char into two letters
00045       int J = 0;
00046       int I = 0;
00047       for (; I != (N/8)*2; J++,I += 2)
00048       {
00049          Result[I] = Conv[Sum[J] >> 4];
00050          Result[I + 1] = Conv[Sum[J] & 0xF];
00051       }
00052       return std::string(Result);
00053    };
00054    
00055    inline void Value(unsigned char S[N/8])
00056    {
00057       for (int I = 0; I != sizeof(Sum); I++) 
00058          S[I] = Sum[I];
00059    };
00060 
00061    inline operator std::string() const 
00062    {
00063       return Value();
00064    };
00065 
00066    bool Set(std::string Str) 
00067    {
00068       return Hex2Num(Str,Sum,sizeof(Sum));
00069    };
00070 
00071    inline void Set(unsigned char S[N/8]) 
00072    {
00073       for (int I = 0; I != sizeof(Sum); I++) 
00074          Sum[I] = S[I];
00075    };
00076 
00077    HashSumValue(std::string Str) 
00078    {
00079          memset(Sum,0,sizeof(Sum));
00080          Set(Str);
00081    }
00082    HashSumValue()
00083    {
00084       memset(Sum,0,sizeof(Sum));
00085    }
00086 };
00087 
00088 class SummationImplementation
00089 {
00090    public:
00091    virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0;
00092    inline bool Add(const char *inbuf, unsigned long long const inlen)
00093    { return Add((unsigned char *)inbuf, inlen); };
00094 
00095    inline bool Add(const unsigned char *Data)
00096    { return Add(Data, strlen((const char *)Data)); };
00097    inline bool Add(const char *Data)
00098    { return Add((const unsigned char *)Data, strlen((const char *)Data)); };
00099 
00100    inline bool Add(const unsigned char *Beg, const unsigned char *End)
00101    { return Add(Beg, End - Beg); };
00102    inline bool Add(const char *Beg, const char *End)
00103    { return Add((const unsigned char *)Beg, End - Beg); };
00104 
00105    bool AddFD(int Fd, unsigned long long Size = 0);
00106 };
00107 
00108 #endif