GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 // As a special exception, you may use this file as part of a free software 00019 // library without restriction. Specifically, if other files instantiate 00020 // templates or use macros or inline functions from this file, or you compile 00021 // this file and link it with other files to produce an executable, this 00022 // file does not by itself cause the resulting executable to be covered by 00023 // the GNU General Public License. This exception does not however 00024 // invalidate any other reasons why the executable file might be covered by 00025 // the GNU General Public License. 00026 // 00027 // This exception applies only to the code released under the name GNU 00028 // Common C++. If you copy code from other releases into a copy of GNU 00029 // Common C++, as the General Public License permits, the exception does 00030 // not apply to the code that you add in this way. To avoid misleading 00031 // anyone as to the status of such modified files, you must delete 00032 // this exception notice from them. 00033 // 00034 // If you write modifications of your own for GNU Common C++, it is your choice 00035 // whether to permit this exception to apply to your modifications. 00036 // If you do not wish that, delete this exception notice. 00037 // 00038 00044 #ifndef CCXX_DIGEST_H_ 00045 #define CCXX_DIGEST_H_ 00046 00047 #ifndef CCXX_MISSING_H_ 00048 #include <cc++/missing.h> 00049 #endif 00050 00051 #ifndef CCXX_THREAD_H_ 00052 #include <cc++/thread.h> 00053 #endif 00054 00055 #ifndef CCXX_EXCEPTION_H_ 00056 #include <cc++/exception.h> 00057 #endif 00058 00059 #ifdef CCXX_NAMESPACES 00060 namespace ost { 00061 #endif 00062 00070 class __EXPORT Digest : protected std::streambuf, public std::ostream 00071 { 00072 protected: 00073 Digest(); 00074 00080 virtual unsigned getSize(void) = 0; 00081 00088 virtual unsigned getDigest(unsigned char *buffer) = 0; 00089 00096 virtual void putDigest(const unsigned char *buffer, unsigned length) = 0; 00097 00103 virtual std::ostream &strDigest(std::ostream &os) = 0; 00104 00105 friend std::ostream &operator<<(std::ostream &os, Digest &ia) 00106 {return ia.strDigest(os);}; 00107 00108 public: 00112 virtual void initDigest(void) = 0; 00113 00114 virtual ~Digest(); 00115 }; 00116 00123 class __EXPORT ChecksumDigest : public Digest 00124 { 00125 private: 00126 unsigned char csum; 00127 00128 protected: 00129 int overflow(int c); 00130 std::ostream &strDigest(std::ostream &os); 00131 00132 public: 00133 ChecksumDigest(); 00134 00135 void initDigest(void) 00136 {csum = 0;}; 00137 00138 unsigned getSize(void) 00139 {return 1;}; 00140 00141 unsigned getDigest(unsigned char *buffer); 00142 00143 void putDigest(const unsigned char *buffer, unsigned length); 00144 }; 00145 00152 class __EXPORT CRC16Digest : public Digest 00153 { 00154 private: 00155 uint16 crc16; 00156 00157 protected: 00158 int overflow(int c); 00159 00160 std::ostream &strDigest(std::ostream &os); 00161 00162 public: 00163 CRC16Digest(); 00164 00165 CRC16Digest ( const CRC16Digest &crc ); 00166 00167 virtual ~CRC16Digest() {}; 00168 00169 inline void initDigest(uint16 crc) {crc16 = crc;}; 00170 00171 void initDigest(void) {initDigest(0);}; 00172 00173 inline unsigned getSize ( void ) 00174 {return sizeof(crc16);}; 00175 00176 CRC16Digest& operator= ( const CRC16Digest &right ); 00177 00178 operator const uint16() const 00179 {return crc16;}; 00180 00181 inline uint16 getDigest(void) 00182 {return crc16;}; 00183 00184 unsigned getDigest ( unsigned char *buffer ); 00185 00186 void putDigest ( const unsigned char *buffer, unsigned length ); 00187 00188 }; 00189 00197 class __EXPORT CRC32Digest : public Digest 00198 { 00199 private: 00200 uint32 crc_table[256]; 00201 uint32 crc_reg; 00202 uint32 crc32; 00203 00204 protected: 00205 unsigned char overflow(unsigned char octet); 00206 00207 std::ostream &strDigest(std::ostream &os); 00208 00209 public: 00210 CRC32Digest(); 00211 CRC32Digest(const CRC32Digest &crc); 00212 00213 void initDigest(void); 00214 00215 inline unsigned getSize(void) {return sizeof(crc32);} 00216 00217 operator const uint32() const 00218 {return crc32;}; 00219 00220 inline uint32 getDigest(void) 00221 {return crc32;}; 00222 00223 unsigned getDigest(unsigned char *buffer); 00224 00225 void putDigest(const unsigned char *buffer, unsigned length); 00226 00227 CRC32Digest& operator= (const CRC32Digest &right); 00228 }; 00229 00236 class __EXPORT MD5Digest : public Digest 00237 { 00238 private: 00239 unsigned long state[4]; 00240 unsigned long count[2]; 00241 unsigned char buf[64]; 00242 unsigned bpos; 00243 unsigned char md5[16]; 00244 bool updated; 00245 00246 protected: 00247 int overflow(int c); 00248 00249 void update(void); 00250 00251 void commit(void); 00252 00253 std::ostream &strDigest(std::ostream &os); 00254 00255 public: 00256 MD5Digest(); 00257 00258 void initDigest(void); 00259 00260 inline unsigned getSize(void) 00261 {return 16;}; 00262 00263 unsigned getDigest(unsigned char *buffer); 00264 00265 void putDigest(const unsigned char *buffer, unsigned len); 00266 }; 00267 00268 #ifdef COMMON_STD_EXCEPTION 00269 00278 class __EXPORT DigestException : public Exception { 00279 public: 00280 DigestException(const String &str) : Exception(str) {}; 00281 }; 00282 #endif 00283 00284 #ifdef CCXX_NAMESPACES 00285 } 00286 #endif 00287 00288 #endif 00289