Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

digest.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2002 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception to the GNU General Public License, permission is
00018 // granted for additional uses of the text contained in its release
00019 // of Common C++.
00020 //
00021 // The exception is that, if you link the Common C++ library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the Common C++ library code into it.
00026 //
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 //
00030 // This exception applies only to the code released under the
00031 // name Common C++.  If you copy code from other releases into a copy of
00032 // Common C++, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 //
00037 // If you write modifications of your own for Common C++, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.
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 

Generated on Thu Nov 21 12:28:30 2002 for GNU CommonC++ by doxygen1.2.18