00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00046 #ifndef CCXX_DIGEST_H_
00047 #define CCXX_DIGEST_H_
00048
00049 #ifndef CCXX_CONFIG_H_
00050 #include <cc++/config.h>
00051 #endif
00052
00053 #ifndef CCXX_THREAD_H_
00054 #include <cc++/thread.h>
00055 #endif
00056
00057 #include <iostream>
00058 #include <fstream>
00059
00060 #ifdef CCXX_NAMESPACES
00061 namespace ost {
00062 #endif
00063
00064 #ifdef WIN32
00065 class CCXX_CLASS_EXPORT Digest;
00066 class CCXX_CLASS_EXPORT ChecksumDigest;
00067 class CCXX_CLASS_EXPORT CRC16Digest;
00068 class CCXX_CLASS_EXPORT CRC32Digest;
00069 class CCXX_CLASS_EXPORT MD5Digest;
00070 #endif
00071
00079 #if defined(__KCC)
00080 #define ostream ostream_withassign
00081 #endif
00082 class Digest : protected std::streambuf, public std::ostream
00083 {
00084 protected:
00085 Digest();
00086
00090 virtual void initDigest(void) = 0;
00091
00097 virtual unsigned getSize(void) = 0;
00098
00105 virtual unsigned getDigest(unsigned char *buffer) = 0;
00106
00113 virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
00114
00120 virtual std::ostream &strDigest(std::ostream &os) = 0;
00121
00122 friend std::ostream &operator<<(std::ostream &os, Digest &ia)
00123 {return ia.strDigest(os);};
00124 };
00125
00132 class ChecksumDigest : public Digest
00133 {
00134 private:
00135 unsigned char csum;
00136
00137 protected:
00138 int overflow(int c);
00139 std::ostream &strDigest(std::ostream &os);
00140
00141 public:
00142 ChecksumDigest();
00143
00144 void initDigest(void)
00145 {csum = 0;};
00146
00147 unsigned getSize(void)
00148 {return 1;};
00149
00150 unsigned getDigest(unsigned char *buffer);
00151
00152 void putDigest(const unsigned char *buffer, unsigned length);
00153 };
00154
00161 class CRC16Digest : public Digest
00162 {
00163 private:
00164 unsigned short crc16;
00165
00166 protected:
00167 int overflow(int c);
00168
00169 std::ostream &strDigest(std::ostream &os);
00170
00171 public:
00172 CRC16Digest();
00173
00174 inline void initDigest(void)
00175 {crc16 = 0;};
00176
00177 inline unsigned getSize(void)
00178 {return 2;};
00179
00180 unsigned getDigest(unsigned char *buffer);
00181
00182 void putDigest(const unsigned char *buffer, unsigned length);
00183 };
00184
00192 class CRC32Digest : public Digest
00193 {
00194 private:
00195 unsigned long crc_table[256];
00196 unsigned long crc_reg;
00197 unsigned long crc32;
00198
00199 protected:
00200 unsigned char overflow(unsigned char octet);
00201
00202 std::ostream &strDigest(std::ostream &os);
00203
00204 public:
00205 CRC32Digest();
00206
00207 void initDigest(void);
00208
00209 inline unsigned getSize(void) {return 4;}
00210
00211 unsigned getDigest(unsigned char *buffer);
00212
00213 void putDigest(const unsigned char *buffer, unsigned length);
00214 };
00215
00222 class MD5Digest : public Digest
00223 {
00224 private:
00225 unsigned long state[4];
00226 unsigned long count[2];
00227 unsigned char buf[64];
00228 unsigned bpos;
00229 unsigned char md5[16];
00230 bool updated;
00231
00232 protected:
00233 int overflow(int c);
00234
00235 void update(void);
00236
00237 void commit(void);
00238
00239 std::ostream &strDigest(std::ostream &os);
00240
00241 public:
00242 MD5Digest();
00243
00244 void initDigest(void);
00245
00246 inline unsigned getSize(void)
00247 {return 16;};
00248
00249 unsigned getDigest(unsigned char *buffer);
00250
00251 void putDigest(const unsigned char *buffer, unsigned len);
00252 };
00253
00254 #ifdef CCXX_NAMESPACES
00255 };
00256 #endif
00257
00258 #endif
00259