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