Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.0 KiB

  1. // simple.h - written and placed in the public domain by Wei Dai
  2. /*! \file
  3. Simple non-interface classes derived from classes in cryptlib.h.
  4. */
  5. #ifndef CRYPTOPP_SIMPLE_H
  6. #define CRYPTOPP_SIMPLE_H
  7. #include "cryptlib.h"
  8. #include "misc.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! _
  11. template <class DERIVED, class BASE>
  12. class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
  13. {
  14. public:
  15. Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
  16. };
  17. //! _
  18. template <class BASE, class ALGORITHM_INFO=BASE>
  19. class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
  20. {
  21. public:
  22. static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
  23. std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
  24. };
  25. //! _
  26. class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
  27. {
  28. public:
  29. explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
  30. };
  31. //! _
  32. class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
  33. {
  34. public:
  35. explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
  36. };
  37. // *****************************
  38. //! _
  39. template <class T>
  40. class CRYPTOPP_NO_VTABLE Bufferless : public T
  41. {
  42. public:
  43. bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
  44. };
  45. //! _
  46. template <class T>
  47. class CRYPTOPP_NO_VTABLE Unflushable : public T
  48. {
  49. public:
  50. bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
  51. {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
  52. bool IsolatedFlush(bool hardFlush, bool blocking)
  53. {assert(false); return false;}
  54. bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
  55. {
  56. if (hardFlush && !InputBufferIsEmpty())
  57. throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
  58. else
  59. {
  60. BufferedTransformation *attached = this->AttachedTransformation();
  61. return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
  62. }
  63. }
  64. protected:
  65. virtual bool InputBufferIsEmpty() const {return false;}
  66. };
  67. //! _
  68. template <class T>
  69. class CRYPTOPP_NO_VTABLE InputRejecting : public T
  70. {
  71. public:
  72. struct InputRejected : public NotImplemented
  73. {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
  74. // shouldn't be calling these functions on this class
  75. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  76. {throw InputRejected();}
  77. bool IsolatedFlush(bool, bool) {return false;}
  78. bool IsolatedMessageSeriesEnd(bool) {throw InputRejected();}
  79. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
  80. {throw InputRejected();}
  81. bool ChannelMessageSeriesEnd(const std::string &, int, bool) {throw InputRejected();}
  82. };
  83. //! _
  84. template <class T>
  85. class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
  86. {
  87. public:
  88. virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
  89. private:
  90. bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;}
  91. };
  92. //! _
  93. template <class T>
  94. class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
  95. {
  96. public:
  97. virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
  98. private:
  99. void IsolatedInitialize(const NameValuePairs &parameters) {assert(false);}
  100. };
  101. //! _
  102. template <class T>
  103. class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
  104. {
  105. public:
  106. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
  107. {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
  108. bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
  109. {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
  110. byte * CreatePutSpace(size_t &size)
  111. {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
  112. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  113. {return this->ChannelPut2(DEFAULT_CHANNEL, begin, length, messageEnd, blocking);}
  114. size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
  115. {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
  116. // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
  117. // {PropagateMessageSeriesEnd(propagation, channel);}
  118. byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
  119. {size = 0; return NULL;}
  120. bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
  121. {this->ChannelPut(channel, inString, length); return false;}
  122. virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
  123. size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
  124. {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
  125. virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
  126. };
  127. //! _
  128. template <class T>
  129. class CRYPTOPP_NO_VTABLE AutoSignaling : public T
  130. {
  131. public:
  132. AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
  133. void SetAutoSignalPropagation(int propagation)
  134. {m_autoSignalPropagation = propagation;}
  135. int GetAutoSignalPropagation() const
  136. {return m_autoSignalPropagation;}
  137. private:
  138. int m_autoSignalPropagation;
  139. };
  140. //! A BufferedTransformation that only contains pre-existing data as "output"
  141. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
  142. {
  143. public:
  144. Store() : m_messageEnd(false) {}
  145. void IsolatedInitialize(const NameValuePairs &parameters)
  146. {
  147. m_messageEnd = false;
  148. StoreInitialize(parameters);
  149. }
  150. unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
  151. bool GetNextMessage();
  152. unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
  153. protected:
  154. virtual void StoreInitialize(const NameValuePairs &parameters) =0;
  155. bool m_messageEnd;
  156. };
  157. //! A BufferedTransformation that doesn't produce any retrievable output
  158. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
  159. {
  160. public:
  161. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
  162. {transferBytes = 0; return 0;}
  163. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
  164. {return 0;}
  165. };
  166. class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
  167. {
  168. public:
  169. std::string AlgorithmName() const {return "BitBucket";}
  170. void IsolatedInitialize(const NameValuePairs &parameters) {}
  171. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  172. {return 0;}
  173. };
  174. NAMESPACE_END
  175. #endif