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