digest.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 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, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef CCXX_DIGEST_H_
00044 #define CCXX_DIGEST_H_
00045 
00046 #ifndef CCXX_MISSING_H_
00047 #include <cc++/missing.h>
00048 #endif
00049 
00050 #ifndef CCXX_THREAD_H_
00051 #include <cc++/thread.h>
00052 #endif
00053 
00054 #ifndef CCXX_EXCEPTION_H_
00055 #include <cc++/exception.h>
00056 #endif
00057 
00058 #ifdef  CCXX_NAMESPACES
00059 namespace ost {
00060 #endif
00061 
00069 class __EXPORT Digest : protected std::streambuf, public std::ostream
00070 {
00071 protected:
00072         Digest();
00073 
00079         virtual unsigned getSize(void) = 0;
00080 
00087         virtual unsigned getDigest(unsigned char *buffer) = 0;
00088 
00095         virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
00096 
00102         virtual std::ostream &strDigest(std::ostream &os) = 0;
00103 
00104         friend std::ostream &operator<<(std::ostream &os, Digest &ia)
00105                 {return ia.strDigest(os);};
00106 
00107 public:
00111         virtual void initDigest(void) = 0;
00112 
00113         virtual ~Digest();
00114 };
00115 
00122 class __EXPORT ChecksumDigest : public Digest
00123 {
00124 private:
00125         unsigned char csum;
00126 
00127 protected:
00128         int overflow(int c);
00129         std::ostream &strDigest(std::ostream &os);
00130 
00131 public:
00132         ChecksumDigest();
00133 
00134         void initDigest(void)
00135                 {csum = 0;};
00136 
00137         unsigned getSize(void)
00138                 {return 1;};
00139 
00140         unsigned getDigest(unsigned char *buffer);
00141 
00142         void putDigest(const unsigned char *buffer, unsigned length);
00143 };
00144 
00151 class __EXPORT CRC16Digest : public Digest
00152 {
00153 private:
00154         unsigned short crc16;
00155 
00156 protected:
00157         int overflow(int c);
00158 
00159         std::ostream &strDigest(std::ostream &os);
00160 
00161 public:
00162         CRC16Digest();
00163 
00164         inline void initDigest(void)
00165                 {crc16 = 0;};
00166 
00167         inline unsigned getSize(void)
00168                 {return 2;};
00169 
00170         unsigned getDigest(unsigned char *buffer);
00171 
00172         void putDigest(const unsigned char *buffer, unsigned length);
00173 };
00174 
00182 class __EXPORT CRC32Digest : public Digest
00183 {
00184 private:
00185    unsigned long crc_table[256];
00186    unsigned long crc_reg;
00187    unsigned long crc32;
00188 
00189 protected:
00190    unsigned char overflow(unsigned char octet);
00191 
00192    std::ostream &strDigest(std::ostream &os);
00193 
00194 public:
00195    CRC32Digest();
00196 
00197    void initDigest(void);
00198 
00199    inline unsigned getSize(void) {return 4;}
00200 
00201    unsigned getDigest(unsigned char *buffer);
00202 
00203    void putDigest(const unsigned char *buffer, unsigned length);
00204 };
00205 
00212 class __EXPORT MD5Digest : public Digest
00213 {
00214 private:
00215         unsigned long state[4];
00216         unsigned long count[2];
00217         unsigned char buf[64];
00218         unsigned bpos;
00219         unsigned char md5[16];
00220         bool updated;
00221 
00222 protected:
00223         int overflow(int c);
00224 
00225         void update(void);
00226 
00227         void commit(void);
00228 
00229         std::ostream &strDigest(std::ostream &os);
00230 
00231 public:
00232         MD5Digest();
00233 
00234         void initDigest(void);
00235 
00236         inline unsigned getSize(void)
00237                 {return 16;};
00238 
00239         unsigned getDigest(unsigned char *buffer);
00240 
00241         void putDigest(const unsigned char *buffer, unsigned len);
00242 };
00243 
00244 #ifdef  COMMON_STD_EXCEPTION
00245 
00254 class __EXPORT DigestException : public Exception {
00255 public:
00256         DigestException(const String &str) : Exception(str) {};
00257 };
00258 #endif
00259 
00260 #ifdef  CCXX_NAMESPACES
00261 }
00262 #endif
00263 
00264 #endif
00265 

Generated on Fri May 30 14:35:00 2008 for GNU CommonC++ by  doxygen 1.4.7