- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# simple.h

Go to the documentation of this file.

    00001 // simple.h - written and placed in the public domain by Wei Dai
    00002 /*! \file
    00003         Simple non-interface classes derived from classes in cryptlib.h.
    00004 */
    00005
    00006 #ifndef CRYPTOPP_SIMPLE_H
    00007 #define CRYPTOPP_SIMPLE_H
    00008
    00009 #include "cryptlib.h"
    00010 #include "misc.h"
    00011
    00012 NAMESPACE_BEGIN(CryptoPP)
    00013
    00014 //! _
    00015 template <class DERIVED, class BASE>
    00016 class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
    00017 {
    00018 public:
    00019         Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
    00020 };
    00021
    00022 //! _
    00023 template <class BASE, class ALGORITHM_INFO=BASE>
    00024 class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
    00025 {
    00026 public:
    00027         static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
    00028         std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
    00029 };
    00030
    00031 //! _
    00032 class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
    00033 {
    00034 public:
    00035         explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
    00036 };
    00037
    00038 //! _
    00039 class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
    00040 {
    00041 public:
    00042         explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
    00043 };
    00044
    00045 //! _
    00046 // TODO: look into this virtual inheritance
    00047 class CRYPTOPP_DLL ASN1CryptoMaterial : virtual public ASN1Object, virtual public CryptoMaterial
    00048 {
    00049 public:
    00050         void Save(BufferedTransformation &bt) const
    00051                 {BEREncode(bt);}
    00052         void Load(BufferedTransformation &bt)
    00053                 {BERDecode(bt);}
    00054 };
    00055
    00056 // *****************************
    00057
    00058 //! _
    00059 template <class T>
    00060 class CRYPTOPP_NO_VTABLE Bufferless : public T
    00061 {
    00062 public:
    00063         bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
    00064 };
    00065
    00066 //! _
    00067 template <class T>
    00068 class CRYPTOPP_NO_VTABLE Unflushable : public T
    00069 {
    00070 public:
    00071         bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
    00072                 {return ChannelFlush(this->NULL_CHANNEL, completeFlush, propagation, blocking);}
    00073         bool IsolatedFlush(bool hardFlush, bool blocking)
    00074                 {assert(false); return false;}
    00075         bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
    00076         {
    00077                 if (hardFlush && !InputBufferIsEmpty())
    00078                         throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
    00079                 else
    00080                 {
    00081                         BufferedTransformation *attached = this->AttachedTransformation();
    00082                         return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
    00083                 }
    00084         }
    00085
    00086 protected:
    00087         virtual bool InputBufferIsEmpty() const {return false;}
    00088 };
    00089
    00090 //! _
    00091 template <class T>
    00092 class CRYPTOPP_NO_VTABLE InputRejecting : public T
    00093 {
    00094 public:
    00095         struct InputRejected : public NotImplemented
    00096                 {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
    00097
    00098         // shouldn't be calling these functions on this class
    00099         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00100                 {throw InputRejected();}
    00101         bool IsolatedFlush(bool, bool) {return false;}
    00102         bool IsolatedMessageSeriesEnd(bool) {throw InputRejected();}
    00103
    00104         size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
    00105                 {throw InputRejected();}
    00106         bool ChannelMessageSeriesEnd(const std::string &, int, bool) {throw InputRejected();}
    00107 };
    00108
    00109 //! _
    00110 template <class T>
    00111 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
    00112 {
    00113 public:
    00114         virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
    00115
    00116 private:
    00117         bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;}
    00118 };
    00119
    00120 //! _
    00121 template <class T>
    00122 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
    00123 {
    00124 public:
    00125         virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
    00126
    00127 private:
    00128         void IsolatedInitialize(const NameValuePairs &parameters) {assert(false);}
    00129 };
    00130
    00131 //! _
    00132 template <class T>
    00133 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
    00134 {
    00135 public:
    00136         bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
    00137                 {return ChannelFlush(this->NULL_CHANNEL, hardFlush, propagation, blocking);}
    00138         bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
    00139                 {return ChannelMessageSeriesEnd(this->NULL_CHANNEL, propagation, blocking);}
    00140         byte * CreatePutSpace(size_t &size)
    00141                 {return ChannelCreatePutSpace(this->NULL_CHANNEL, size);}
    00142         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00143                 {return ChannelPut2(this->NULL_CHANNEL, begin, length, messageEnd, blocking);}
    00144         size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
    00145                 {return ChannelPutModifiable2(this->NULL_CHANNEL, inString, length, messageEnd, blocking);}
    00146
    00147 //      void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
    00148 //              {PropagateMessageSeriesEnd(propagation, channel);}
    00149         byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
    00150                 {size = 0; return NULL;}
    00151         bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
    00152                 {this->ChannelPut(channel, inString, length); return false;}
    00153
    00154         virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
    00155         size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
    00156                 {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
    00157
    00158         virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
    00159 };
    00160
    00161 //! _
    00162 template <class T>
    00163 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
    00164 {
    00165 public:
    00166         AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
    00167
    00168         void SetAutoSignalPropagation(int propagation)
    00169                 {m_autoSignalPropagation = propagation;}
    00170         int GetAutoSignalPropagation() const
    00171                 {return m_autoSignalPropagation;}
    00172
    00173 private:
    00174         int m_autoSignalPropagation;
    00175 };
    00176
    00177 //! A BufferedTransformation that only contains pre-existing data as "output"
    00178 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
    00179 {
    00180 public:
    00181         Store() : m_messageEnd(false) {}
    00182
    00183         void IsolatedInitialize(const NameValuePairs &parameters)
    00184         {
    00185                 m_messageEnd = false;
    00186                 StoreInitialize(parameters);
    00187         }
    00188
    00189         unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
    00190         bool GetNextMessage();
    00191         unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
    00192
    00193 protected:
    00194         virtual void StoreInitialize(const NameValuePairs &parameters) =0;
    00195
    00196         bool m_messageEnd;
    00197 };
    00198
    00199 //! A BufferedTransformation that doesn't produce any retrievable output
    00200 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
    00201 {
    00202 protected:
    00203         // make these functions protected to help prevent unintentional calls to them
    00204         BufferedTransformation::Get;
    00205         BufferedTransformation::Peek;
    00206         BufferedTransformation::TransferTo;
    00207         BufferedTransformation::CopyTo;
    00208         BufferedTransformation::CopyRangeTo;
    00209         BufferedTransformation::TransferMessagesTo;
    00210         BufferedTransformation::CopyMessagesTo;
    00211         BufferedTransformation::TransferAllTo;
    00212         BufferedTransformation::CopyAllTo;
    00213         size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true)
    00214                 {transferBytes = 0; return 0;}
    00215         size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
    00216                 {return 0;}
    00217 };
    00218
    00219 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
    00220 {
    00221 public:
    00222         std::string AlgorithmName() const {return "BitBucket";}
    00223         void IsolatedInitialize(const NameValuePairs &parameters) {}
    00224         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00225                 {return 0;}
    00226 };
    00227
    00228 NAMESPACE_END
    00229
    00230 #endif