Back to index

nordugrid-arc-nox  1.1.0~rc6
CheckSum.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_CHECKSUM_H__
00004 #define __ARC_CHECKSUM_H__
00005 
00006 #include <cstring>
00007 #include <cstdio>
00008 
00009 #include <inttypes.h>
00010 #include <sys/types.h>
00011 #include <zlib.h>
00012 
00013 namespace Arc {
00014 
00016 
00017   class CheckSum {
00018   public:
00019     CheckSum(void) {}
00020     virtual ~CheckSum(void) {}
00021     virtual void start(void) = 0;
00022     virtual void add(void *buf, unsigned long long int len) = 0;
00023     virtual void end(void) = 0;
00024     virtual void result(unsigned char*& res, unsigned int& len) const = 0;
00025     virtual int print(char *buf, int len) const {
00026       if (len > 0)
00027         buf[0] = 0;
00028       return 0;
00029     }
00030     virtual void scan(const char *buf) = 0;
00031     virtual operator bool(void) const {
00032       return false;
00033     }
00034     virtual bool operator!(void) const {
00035       return true;
00036     }
00037   };
00038 
00040   class CRC32Sum
00041     : public CheckSum {
00042   private:
00043     uint32_t r;
00044     unsigned long long count;
00045     bool computed;
00046   public:
00047     CRC32Sum(void);
00048     virtual ~CRC32Sum(void) {}
00049     virtual void start(void);
00050     virtual void add(void *buf, unsigned long long int len);
00051     virtual void end(void);
00052     virtual void result(unsigned char*& res, unsigned int& len) const {
00053       res = (unsigned char*)&r;
00054       len = 4;
00055     }
00056     virtual int print(char *buf, int len) const;
00057     virtual void scan(const char *buf);
00058     virtual operator bool(void) const {
00059       return computed;
00060     }
00061     virtual bool operator!(void) const {
00062       return !computed;
00063     }
00064     uint32_t crc(void) const {
00065       return r;
00066     }
00067   };
00068 
00070   class MD5Sum
00071     : public CheckSum {
00072   private:
00073     bool computed;
00074     uint32_t A;
00075     uint32_t B;
00076     uint32_t C;
00077     uint32_t D;
00078     uint64_t count;
00079     uint32_t X[16];
00080     unsigned int Xlen;
00081     // uint32_t T[64];
00082   public:
00083     MD5Sum(void);
00084     virtual void start(void);
00085     virtual void add(void *buf, unsigned long long int len);
00086     virtual void end(void);
00087     virtual void result(unsigned char*& res, unsigned int& len) const {
00088       res = (unsigned char*)&A;
00089       len = 16;
00090     }
00091     virtual int print(char *buf, int len) const;
00092     virtual void scan(const char *buf);
00093     virtual operator bool(void) const {
00094       return computed;
00095     }
00096     virtual bool operator!(void) const {
00097       return !computed;
00098     }
00099   };
00100 
00102   class Adler32Sum
00103     : public CheckSum {
00104    private:
00105     uLong adler;
00106     bool computed;
00107    public:
00108     Adler32Sum(void) : computed(false) { 
00109       start(); 
00110     }
00111     virtual void start(void) {
00112       adler = adler32(0L, Z_NULL, 0); 
00113     }
00114     virtual void add(void* buf,unsigned long long int len) {
00115       adler = adler32(adler, (const Bytef *)buf, len); 
00116     }
00117     virtual void end(void) {
00118       computed = true;
00119     }
00120     virtual void result(unsigned char*& res,unsigned int& len) const { 
00121       res=(unsigned char*)&adler;
00122       len=4;
00123     }  
00124     virtual int print(char* buf,int len) const { 
00125       if(!computed) { 
00126         if(len>0) {
00127           buf[0]=0;
00128           return 0;
00129         }
00130       }
00131       return snprintf(buf,len,"adler32:%08lx",adler);
00132     };
00133     virtual void scan(const char* buf) { };
00134     virtual operator bool(void) const {
00135       return computed;
00136     }
00137     virtual bool operator!(void) const {
00138       return !computed;
00139     }
00140   };
00141 
00143 
00145   class CheckSumAny
00146     : public CheckSum {
00147   public:
00148     typedef enum {
00149       none,
00150       unknown,
00151       undefined,
00152       cksum,
00153       md5,
00154       adler32
00155     } type;
00156   private:
00157     CheckSum *cs;
00158     type tp;
00159   public:
00160     CheckSumAny(CheckSum *c = NULL)
00161       : cs(c),
00162         tp(none) {}
00163     CheckSumAny(type type);
00164     CheckSumAny(const char *type);
00165     virtual ~CheckSumAny(void) {
00166       if (cs)
00167         delete cs;
00168     }
00169     virtual void start(void) {
00170       if (cs)
00171         cs->start();
00172     }
00173     virtual void add(void *buf, unsigned long long int len) {
00174       if (cs)
00175         cs->add(buf, len);
00176     }
00177     virtual void end(void) {
00178       if (cs)
00179         cs->end();
00180     }
00181     virtual void result(unsigned char*& res, unsigned int& len) const {
00182       if (cs) {
00183         cs->result(res, len);
00184         return;
00185       }
00186       len = 0;
00187     }
00188     virtual int print(char *buf, int len) const {
00189       if (cs)
00190         return cs->print(buf, len);
00191       if (len > 0)
00192         buf[0] = 0;
00193       return 0;
00194     }
00195     virtual void scan(const char *buf) {
00196       if (cs)
00197         cs->scan(buf);
00198     }
00199     virtual operator bool(void) const {
00200       if (!cs)
00201         return false;
00202       return *cs;
00203     }
00204     virtual bool operator!(void) const {
00205       if (!cs)
00206         return true;
00207       return !(*cs);
00208     }
00209     bool active(void) {
00210       return (cs != NULL);
00211     }
00212     static type Type(const char *crc);
00213     type Type(void) const {
00214       return tp;
00215     }
00216     void operator=(const char *type);
00217     bool operator==(const char *s);
00218     bool operator==(const CheckSumAny& ck);
00219   };
00220 
00221 } // namespace Arc
00222 
00223 #endif // __ARC_CHECKSUM_H__