- Main Page
- Namespaces
- Classes
- Files

# 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
    00047 //! _
    00048 template <class T>
    00049 class CRYPTOPP_NO_VTABLE Bufferless : public T
    00050 {
    00051 public:
    00052         bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
    00053 };
    00054
    00055 //! _
    00056 template <class T>
    00057 class CRYPTOPP_NO_VTABLE Unflushable : public T
    00058 {
    00059 public:
    00060         bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
    00061                 {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
    00062         bool IsolatedFlush(bool hardFlush, bool blocking)
    00063                 {assert(false); return false;}
    00064         bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
    00065         {
    00066                 if (hardFlush && !InputBufferIsEmpty())
    00067                         throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
    00068                 else
    00069                 {
    00070                         BufferedTransformation *attached = this->AttachedTransformation();
    00071                         return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
    00072                 }
    00073         }
    00074
    00075 protected:
    00076         virtual bool InputBufferIsEmpty() const {return false;}
    00077 };
    00078
    00079 //! _
    00080 template <class T>
    00081 class CRYPTOPP_NO_VTABLE InputRejecting : public T
    00082 {
    00083 public:
    00084         struct InputRejected : public NotImplemented
    00085                 {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
    00086
    00087         // shouldn't be calling these functions on this class
    00088         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00089                 {throw InputRejected();}
    00090         bool IsolatedFlush(bool, bool) {return false;}
    00091         bool IsolatedMessageSeriesEnd(bool) {throw InputRejected();}
    00092
    00093         size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
    00094                 {throw InputRejected();}
    00095         bool ChannelMessageSeriesEnd(const std::string &, int, bool) {throw InputRejected();}
    00096 };
    00097
    00098 //! _
    00099 template <class T>
    00100 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
    00101 {
    00102 public:
    00103         virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
    00104
    00105 private:
    00106         bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;}
    00107 };
    00108
    00109 //! _
    00110 template <class T>
    00111 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
    00112 {
    00113 public:
    00114         virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
    00115
    00116 private:
    00117         void IsolatedInitialize(const NameValuePairs &parameters) {assert(false);}
    00118 };
    00119
    00120 //! _
    00121 template <class T>
    00122 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
    00123 {
    00124 public:
    00125         bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
    00126                 {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
    00127         bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
    00128                 {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
    00129         byte * CreatePutSpace(size_t &size)
    00130                 {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
    00131         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00132                 {return this->ChannelPut2(DEFAULT_CHANNEL, begin, length, messageEnd, blocking);}
    00133         size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
    00134                 {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
    00135
    00136 //      void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
    00137 //              {PropagateMessageSeriesEnd(propagation, channel);}
    00138         byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
    00139                 {size = 0; return NULL;}
    00140         bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
    00141                 {this->ChannelPut(channel, inString, length); return false;}
    00142
    00143         virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
    00144         size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
    00145                 {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
    00146
    00147         virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
    00148 };
    00149
    00150 //! _
    00151 template <class T>
    00152 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
    00153 {
    00154 public:
    00155         AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
    00156
    00157         void SetAutoSignalPropagation(int propagation)
    00158                 {m_autoSignalPropagation = propagation;}
    00159         int GetAutoSignalPropagation() const
    00160                 {return m_autoSignalPropagation;}
    00161
    00162 private:
    00163         int m_autoSignalPropagation;
    00164 };
    00165
    00166 //! A BufferedTransformation that only contains pre-existing data as "output"
    00167 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
    00168 {
    00169 public:
    00170         Store() : m_messageEnd(false) {}
    00171
    00172         void IsolatedInitialize(const NameValuePairs &parameters)
    00173         {
    00174                 m_messageEnd = false;
    00175                 StoreInitialize(parameters);
    00176         }
    00177
    00178         unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
    00179         bool GetNextMessage();
    00180         unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
    00181
    00182 protected:
    00183         virtual void StoreInitialize(const NameValuePairs &parameters) =0;
    00184
    00185         bool m_messageEnd;
    00186 };
    00187
    00188 //! A BufferedTransformation that doesn't produce any retrievable output
    00189 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
    00190 {
    00191 public:
    00192         size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
    00193                 {transferBytes = 0; return 0;}
    00194         size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
    00195                 {return 0;}
    00196 };
    00197
    00198 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
    00199 {
    00200 public:
    00201         std::string AlgorithmName() const {return "BitBucket";}
    00202         void IsolatedInitialize(const NameValuePairs &parameters) {}
    00203         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00204                 {return 0;}
    00205 };
    00206
    00207 NAMESPACE_END
    00208
    00209 #endif