simple.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // simple.h - originally written and placed in the public domain by Wei Dai
  2. /// \file simple.h
  3. /// \brief Classes providing basic library services.
  4. #ifndef CRYPTOPP_SIMPLE_H
  5. #define CRYPTOPP_SIMPLE_H
  6. #include "config.h"
  7. #if CRYPTOPP_MSC_VERSION
  8. # pragma warning(push)
  9. # pragma warning(disable: 4127 4189)
  10. #endif
  11. #include "cryptlib.h"
  12. #include "misc.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief Base class for identifying algorithm
  15. /// \tparam BASE base class from which to derive
  16. /// \tparam DERIVED class which to clone
  17. template <class DERIVED, class BASE>
  18. class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
  19. {
  20. public:
  21. /// \brief Create a copy of this object
  22. /// \return a copy of this object
  23. /// \details The caller is responsible for freeing the object.
  24. Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
  25. };
  26. /// \brief Base class information
  27. /// \tparam BASE an Algorithm derived class
  28. /// \tparam ALGORITHM_INFO an Algorithm derived class
  29. /// \details AlgorithmImpl provides StaticAlgorithmName from the template parameter BASE
  30. template <class BASE, class ALGORITHM_INFO=BASE>
  31. class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
  32. {
  33. public:
  34. /// \brief The algorithm name
  35. /// \return the algorithm name
  36. /// \details StaticAlgorithmName returns the algorithm's name as a static member function.
  37. /// The name is taken from information provided by BASE.
  38. static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
  39. /// \brief The algorithm name
  40. /// \return the algorithm name
  41. /// \details AlgorithmName returns the algorithm's name as a member function.
  42. /// The name is acquired by calling StaticAlgorithmName.
  43. std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
  44. };
  45. /// \brief Exception thrown when an invalid key length is encountered
  46. class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
  47. {
  48. public:
  49. /// \brief Construct an InvalidKeyLength
  50. /// \param algorithm the Algorithm associated with the exception
  51. /// \param length the key size associated with the exception
  52. explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
  53. };
  54. /// \brief Exception thrown when an invalid number of rounds is encountered
  55. class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
  56. {
  57. public:
  58. /// \brief Construct an InvalidRounds
  59. /// \param algorithm the Algorithm associated with the exception
  60. /// \param rounds the number of rounds associated with the exception
  61. explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
  62. };
  63. /// \brief Exception thrown when an invalid block size is encountered
  64. class CRYPTOPP_DLL InvalidBlockSize : public InvalidArgument
  65. {
  66. public:
  67. /// \brief Construct an InvalidBlockSize
  68. /// \param algorithm the Algorithm associated with the exception
  69. /// \param length the block size associated with the exception
  70. explicit InvalidBlockSize(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid block size") {}
  71. };
  72. /// \brief Exception thrown when an invalid derived key length is encountered
  73. class CRYPTOPP_DLL InvalidDerivedKeyLength : public InvalidArgument
  74. {
  75. public:
  76. /// \brief Construct an InvalidDerivedKeyLength
  77. /// \param algorithm the Algorithm associated with the exception
  78. /// \param length the size associated with the exception
  79. explicit InvalidDerivedKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid derived key length") {}
  80. };
  81. /// \brief Exception thrown when an invalid personalization string length is encountered
  82. class CRYPTOPP_DLL InvalidPersonalizationLength : public InvalidArgument
  83. {
  84. public:
  85. /// \brief Construct an InvalidPersonalizationLength
  86. /// \param algorithm the Algorithm associated with the exception
  87. /// \param length the personalization size associated with the exception
  88. explicit InvalidPersonalizationLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid salt length") {}
  89. };
  90. /// \brief Exception thrown when an invalid salt length is encountered
  91. class CRYPTOPP_DLL InvalidSaltLength : public InvalidArgument
  92. {
  93. public:
  94. /// \brief Construct an InvalidSaltLength
  95. /// \param algorithm the Algorithm associated with the exception
  96. /// \param length the salt size associated with the exception
  97. explicit InvalidSaltLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid salt length") {}
  98. };
  99. // *****************************
  100. /// \brief Base class for bufferless filters
  101. /// \tparam T the class or type
  102. template <class T>
  103. class CRYPTOPP_NO_VTABLE Bufferless : public T
  104. {
  105. public:
  106. /// \brief Flushes data buffered by this object, without signal propagation
  107. /// \param hardFlush indicates whether all data should be flushed
  108. /// \param blocking specifies whether the object should block when processing input
  109. /// \note hardFlush must be used with care
  110. bool IsolatedFlush(bool hardFlush, bool blocking)
  111. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
  112. };
  113. /// \brief Base class for unflushable filters
  114. /// \tparam T the class or type
  115. template <class T>
  116. class CRYPTOPP_NO_VTABLE Unflushable : public T
  117. {
  118. public:
  119. /// \brief Flush buffered input and/or output, with signal propagation
  120. /// \param completeFlush is used to indicate whether all data should be flushed
  121. /// \param propagation the number of attached transformations the Flush()
  122. /// signal should be passed
  123. /// \param blocking specifies whether the object should block when processing
  124. /// input
  125. /// \details propagation count includes this object. Setting propagation to
  126. /// <tt>1</tt> means this object only. Setting propagation to <tt>-1</tt>
  127. /// means unlimited propagation.
  128. /// \note Hard flushes must be used with care. It means try to process and
  129. /// output everything, even if there may not be enough data to complete the
  130. /// action. For example, hard flushing a HexDecoder would cause an error if
  131. /// you do it after inputing an odd number of hex encoded characters.
  132. /// \note For some types of filters, like ZlibDecompressor, hard flushes can
  133. /// only be done at "synchronization points". These synchronization points
  134. /// are positions in the data stream that are created by hard flushes on the
  135. /// corresponding reverse filters, in this example ZlibCompressor. This is
  136. /// useful when zlib compressed data is moved across a network in packets
  137. /// and compression state is preserved across packets, as in the SSH2 protocol.
  138. bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
  139. {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
  140. /// \brief Flushes data buffered by this object, without signal propagation
  141. /// \param hardFlush indicates whether all data should be flushed
  142. /// \param blocking specifies whether the object should block when processing input
  143. /// \note hardFlush must be used with care
  144. bool IsolatedFlush(bool hardFlush, bool blocking)
  145. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); CRYPTOPP_ASSERT(false); return false;}
  146. /// \brief Flush buffered input and/or output on a channel
  147. /// \param channel the channel to flush the data
  148. /// \param hardFlush is used to indicate whether all data should be flushed
  149. /// \param propagation the number of attached transformations the ChannelFlush()
  150. /// signal should be passed
  151. /// \param blocking specifies whether the object should block when processing input
  152. /// \return true of the Flush was successful
  153. /// \details propagation count includes this object. Setting propagation to
  154. /// <tt>1</tt> means this object only. Setting propagation to <tt>-1</tt> means
  155. /// unlimited propagation.
  156. bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
  157. {
  158. if (hardFlush && !InputBufferIsEmpty())
  159. throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
  160. else
  161. {
  162. BufferedTransformation *attached = this->AttachedTransformation();
  163. return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
  164. }
  165. }
  166. protected:
  167. virtual bool InputBufferIsEmpty() const {return false;}
  168. };
  169. /// \brief Base class for input rejecting filters
  170. /// \tparam T the class or type
  171. /// \details T should be a BufferedTransformation derived class
  172. template <class T>
  173. class CRYPTOPP_NO_VTABLE InputRejecting : public T
  174. {
  175. public:
  176. struct InputRejected : public NotImplemented
  177. {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
  178. /// \name INPUT
  179. //@{
  180. /// \brief Input a byte array for processing
  181. /// \param inString the byte array to process
  182. /// \param length the size of the string, in bytes
  183. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one
  184. /// \param blocking specifies whether the object should block when processing input
  185. /// \throw InputRejected
  186. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  187. /// \details Internally, the default implementation throws InputRejected.
  188. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  189. {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  190. //@}
  191. /// \name SIGNALS
  192. //@{
  193. /// \brief Flushes data buffered by this object, without signal propagation
  194. /// \param hardFlush indicates whether all data should be flushed
  195. /// \param blocking specifies whether the object should block when processing input
  196. /// \note hardFlush must be used with care
  197. bool IsolatedFlush(bool hardFlush, bool blocking)
  198. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
  199. /// \brief Marks the end of a series of messages, without signal propagation
  200. /// \param blocking specifies whether the object should block when completing the processing on
  201. /// the current series of messages
  202. /// \return true if the message was successful, false otherwise
  203. bool IsolatedMessageSeriesEnd(bool blocking)
  204. {CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  205. /// \brief Input multiple bytes for processing on a channel.
  206. /// \param channel the channel to process the data.
  207. /// \param inString the byte buffer to process.
  208. /// \param length the size of the string, in bytes.
  209. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
  210. /// \param blocking specifies whether the object should block when processing input.
  211. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  212. size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
  213. {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
  214. CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  215. /// \brief Marks the end of a series of messages on a channel
  216. /// \param channel the channel to signal the end of a series of messages
  217. /// \param messageEnd the number of attached transformations the ChannelMessageSeriesEnd() signal should be passed
  218. /// \param blocking specifies whether the object should block when processing input
  219. /// \return true if the message was successful, false otherwise
  220. /// \details Each object that receives the signal will perform its processing, decrement
  221. /// propagation, and then pass the signal on to attached transformations if the value is not 0.
  222. /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
  223. /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
  224. /// \note There should be a MessageEnd() immediately before MessageSeriesEnd().
  225. bool ChannelMessageSeriesEnd(const std::string& channel, int messageEnd, bool blocking)
  226. {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  227. //@}
  228. };
  229. /// \brief Interface for custom flush signals propagation
  230. /// \tparam T BufferedTransformation derived class
  231. template <class T>
  232. class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
  233. {
  234. public:
  235. /// \name SIGNALS
  236. //@{
  237. /// \brief Flush buffered input and/or output, with signal propagation
  238. /// \param hardFlush is used to indicate whether all data should be flushed
  239. /// \param propagation the number of attached transformations the Flush() signal should be passed
  240. /// \param blocking specifies whether the object should block when processing input
  241. /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
  242. /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
  243. /// \note Hard flushes must be used with care. It means try to process and output everything, even if
  244. /// there may not be enough data to complete the action. For example, hard flushing a HexDecoder
  245. /// would cause an error if you do it after inputing an odd number of hex encoded characters.
  246. /// \note For some types of filters, like ZlibDecompressor, hard flushes can only
  247. /// be done at "synchronization points". These synchronization points are positions in the data
  248. /// stream that are created by hard flushes on the corresponding reverse filters, in this
  249. /// example ZlibCompressor. This is useful when zlib compressed data is moved across a
  250. /// network in packets and compression state is preserved across packets, as in the SSH2 protocol.
  251. virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
  252. //@}
  253. private:
  254. bool IsolatedFlush(bool hardFlush, bool blocking)
  255. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); CRYPTOPP_ASSERT(false); return false;}
  256. };
  257. /// \brief Interface for custom flush signals
  258. /// \tparam T BufferedTransformation derived class
  259. template <class T>
  260. class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
  261. {
  262. public:
  263. /// \brief Initialize or reinitialize this object, with signal propagation
  264. /// \param parameters a set of NameValuePairs to initialize or reinitialize this object
  265. /// \param propagation the number of attached transformations the Initialize() signal should be passed
  266. /// \details Initialize() is used to initialize or reinitialize an object using a variable number of
  267. /// arbitrarily typed arguments. The function avoids the need for multiple constructors providing
  268. /// all possible combintations of configurable parameters.
  269. /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
  270. /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
  271. virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
  272. private:
  273. void IsolatedInitialize(const NameValuePairs &parameters)
  274. {CRYPTOPP_UNUSED(parameters); CRYPTOPP_ASSERT(false);}
  275. };
  276. /// \brief Multiple channels support for custom signal processing
  277. /// \tparam T the class or type
  278. /// \details T should be a BufferedTransformation derived class
  279. template <class T>
  280. class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
  281. {
  282. public:
  283. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
  284. {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
  285. /// \brief Marks the end of a series of messages, with signal propagation
  286. /// \param propagation the number of attached transformations the MessageSeriesEnd() signal should be passed
  287. /// \param blocking specifies whether the object should block when processing input
  288. /// \details Each object that receives the signal will perform its processing, decrement
  289. /// propagation, and then pass the signal on to attached transformations if the value is not 0.
  290. /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
  291. /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
  292. /// \note There should be a MessageEnd() immediately before MessageSeriesEnd().
  293. bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
  294. {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
  295. /// \brief Request space which can be written into by the caller
  296. /// \param size the requested size of the buffer
  297. /// \details The purpose of this method is to help avoid extra memory allocations.
  298. /// \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made,
  299. /// size is the requested size of the buffer. When the call returns, size is the size of
  300. /// the array returned to the caller.
  301. /// \details The base class implementation sets size to 0 and returns NULL.
  302. /// \note Some objects, like ArraySink, cannot create a space because its fixed. In the case of
  303. /// an ArraySink, the pointer to the array is returned and the size is remaining size.
  304. byte * CreatePutSpace(size_t &size)
  305. {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
  306. /// \brief Input multiple bytes for processing
  307. /// \param inString the byte buffer to process
  308. /// \param length the size of the string, in bytes
  309. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one
  310. /// \param blocking specifies whether the object should block when processing input
  311. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  312. /// \details Derived classes must implement Put2().
  313. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  314. {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
  315. /// \brief Input multiple bytes that may be modified by callee.
  316. /// \param inString the byte buffer to process.
  317. /// \param length the size of the string, in bytes.
  318. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
  319. /// \param blocking specifies whether the object should block when processing input.
  320. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  321. /// \details Internally, PutModifiable2() calls Put2().
  322. size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
  323. {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
  324. // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
  325. // {PropagateMessageSeriesEnd(propagation, channel);}
  326. /// \brief Request space which can be written into by the caller
  327. /// \param channel the channel to process the data
  328. /// \param size the requested size of the buffer
  329. /// \return a pointer to a memory block with length size
  330. /// \details The purpose of this method is to help avoid extra memory allocations.
  331. /// \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made,
  332. /// size is the requested size of the buffer. When the call returns, size is the size of
  333. /// the array returned to the caller.
  334. /// \details The base class implementation sets size to 0 and returns NULL.
  335. /// \note Some objects, like ArraySink(), cannot create a space because its fixed. In the case of
  336. /// an ArraySink(), the pointer to the array is returned and the size is remaining size.
  337. byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
  338. {CRYPTOPP_UNUSED(channel); size = 0; return NULLPTR;}
  339. /// \brief Input multiple bytes that may be modified by callee on a channel
  340. /// \param channel the channel to process the data.
  341. /// \param inString the byte buffer to process
  342. /// \param length the size of the string, in bytes
  343. /// \return true if all bytes were processed, false otherwise.
  344. bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
  345. {this->ChannelPut(channel, inString, length); return false;}
  346. /// \brief Input multiple bytes for processing on a channel.
  347. /// \param channel the channel to process the data.
  348. /// \param begin the byte buffer to process.
  349. /// \param length the size of the string, in bytes.
  350. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
  351. /// \param blocking specifies whether the object should block when processing input.
  352. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  353. virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
  354. /// \brief Input multiple bytes that may be modified by callee on a channel
  355. /// \param channel the channel to process the data
  356. /// \param begin the byte buffer to process
  357. /// \param length the size of the string, in bytes
  358. /// \param messageEnd means how many filters to signal MessageEnd() to, including this one
  359. /// \param blocking specifies whether the object should block when processing input
  360. /// \return the number of bytes that remain to be processed (i.e., bytes not processed)
  361. size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
  362. {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
  363. /// \brief Flush buffered input and/or output on a channel
  364. /// \param channel the channel to flush the data
  365. /// \param hardFlush is used to indicate whether all data should be flushed
  366. /// \param propagation the number of attached transformations the ChannelFlush() signal should be passed
  367. /// \param blocking specifies whether the object should block when processing input
  368. /// \return true of the Flush was successful
  369. /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
  370. /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
  371. virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
  372. };
  373. /// \brief Provides auto signaling support
  374. /// \tparam T BufferedTransformation derived class
  375. template <class T>
  376. class CRYPTOPP_NO_VTABLE AutoSignaling : public T
  377. {
  378. public:
  379. /// \brief Construct an AutoSignaling
  380. /// \param propagation the propagation count
  381. AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
  382. /// \brief Set propagation of automatically generated and transferred signals
  383. /// \param propagation then new value
  384. /// \details Setting propagation to <tt>0</tt> means do not automatically generate signals. Setting
  385. /// propagation to <tt>-1</tt> means unlimited propagation.
  386. void SetAutoSignalPropagation(int propagation)
  387. {m_autoSignalPropagation = propagation;}
  388. /// \brief Retrieve automatic signal propagation value
  389. /// \return the number of attached transformations the signal is propagated to. 0 indicates
  390. /// the signal is only witnessed by this object
  391. int GetAutoSignalPropagation() const
  392. {return m_autoSignalPropagation;}
  393. private:
  394. int m_autoSignalPropagation;
  395. };
  396. /// \brief Acts as a Source for pre-existing, static data
  397. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
  398. {
  399. public:
  400. /// \brief Construct a Store
  401. Store() : m_messageEnd(false) {}
  402. void IsolatedInitialize(const NameValuePairs &parameters)
  403. {
  404. m_messageEnd = false;
  405. StoreInitialize(parameters);
  406. }
  407. unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
  408. bool GetNextMessage();
  409. unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
  410. protected:
  411. virtual void StoreInitialize(const NameValuePairs &parameters) =0;
  412. bool m_messageEnd;
  413. };
  414. /// \brief Implementation of BufferedTransformation's attachment interface
  415. /// \details Sink is a cornerstone of the Pipeline trinity. Data flows from
  416. /// Sources, through Filters, and then terminates in Sinks. The difference
  417. /// between a Source and Filter is a Source \a pumps data, while a Filter does
  418. /// not. The difference between a Filter and a Sink is a Filter allows an
  419. /// attached transformation, while a Sink does not.
  420. /// \details A Sink does not produce any retrievable output.
  421. /// \details See the discussion of BufferedTransformation in cryptlib.h for
  422. /// more details.
  423. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
  424. {
  425. public:
  426. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
  427. {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
  428. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
  429. {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
  430. };
  431. /// \brief Acts as an input discarding Filter or Sink
  432. /// \details The BitBucket discards all input and returns 0 to the caller
  433. /// to indicate all data was processed.
  434. class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
  435. {
  436. public:
  437. std::string AlgorithmName() const {return "BitBucket";}
  438. void IsolatedInitialize(const NameValuePairs &params)
  439. {CRYPTOPP_UNUSED(params);}
  440. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  441. {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
  442. };
  443. NAMESPACE_END
  444. #if CRYPTOPP_MSC_VERSION
  445. # pragma warning(pop)
  446. #endif
  447. #endif