Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:03

0001 // filters.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file filters.h

0004 /// \brief Implementation of BufferedTransformation's attachment interface.

0005 
0006 #ifndef CRYPTOPP_FILTERS_H
0007 #define CRYPTOPP_FILTERS_H
0008 
0009 #include "config.h"
0010 
0011 #if CRYPTOPP_MSC_VERSION
0012 # pragma warning(push)
0013 # pragma warning(disable: 4127 4189 4231 4275 4514)
0014 #endif
0015 
0016 #include "cryptlib.h"
0017 #include "simple.h"
0018 #include "secblock.h"
0019 #include "misc.h"
0020 #include "smartptr.h"
0021 #include "queue.h"
0022 #include "algparam.h"
0023 #include "stdcpp.h"
0024 
0025 NAMESPACE_BEGIN(CryptoPP)
0026 
0027 /// \brief Implementation of BufferedTransformation's attachment interface

0028 /// \details Filter is a cornerstone of the Pipeline trinity. Data flows from

0029 ///  Sources, through Filters, and then terminates in Sinks. The difference

0030 ///  between a Source and Filter is a Source \a pumps data, while a Filter does

0031 ///  not. The difference between a Filter and a Sink is a Filter allows an

0032 ///  attached transformation, while a Sink does not.

0033 /// \details See the discussion of BufferedTransformation in cryptlib.h for

0034 ///  more details.

0035 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Filter : public BufferedTransformation, public NotCopyable
0036 {
0037 public:
0038     virtual ~Filter() {}
0039 
0040     /// \name ATTACHMENT

0041     //@{

0042 
0043     /// \brief Construct a Filter

0044     /// \param attachment an optional attached transformation

0045     /// \details attachment can be NULL.

0046     Filter(BufferedTransformation *attachment = NULLPTR);
0047 
0048     /// \brief Determine if attachable

0049     /// \return true if the object allows attached transformations, false otherwise.

0050     /// \note Source and Filter offer attached transformations; while Sink does not.

0051     bool Attachable() {return true;}
0052 
0053     /// \brief Retrieve attached transformation

0054     /// \return pointer to a BufferedTransformation if there is an attached transformation, NULL otherwise.

0055     BufferedTransformation *AttachedTransformation();
0056 
0057     /// \brief Retrieve attached transformation

0058     /// \return pointer to a BufferedTransformation if there is an attached transformation, NULL otherwise.

0059     const BufferedTransformation *AttachedTransformation() const;
0060 
0061     /// \brief Replace an attached transformation

0062     /// \param newAttachment an optional attached transformation

0063     /// \details newAttachment can be a single filter, a chain of filters or NULL.

0064     ///  Pass NULL to remove an existing BufferedTransformation or chain of filters

0065     void Detach(BufferedTransformation *newAttachment = NULLPTR);
0066 
0067     //@}

0068 
0069     /// \name RETRIEVAL OF ONE MESSAGE

0070     //@{

0071 
0072     // BufferedTransformation in cryptlib.h

0073     size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
0074     size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
0075 
0076     //@}

0077 
0078     /// \name SIGNALS

0079     //@{

0080 
0081     // BufferedTransformation in cryptlib.h

0082     void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1);
0083     bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
0084     bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
0085 
0086     //@}

0087 
0088 protected:
0089     virtual BufferedTransformation * NewDefaultAttachment() const;
0090     void Insert(Filter *nextFilter);    // insert filter after this one

0091 
0092     virtual bool ShouldPropagateMessageEnd() const {return true;}
0093     virtual bool ShouldPropagateMessageSeriesEnd() const {return true;}
0094 
0095     void PropagateInitialize(const NameValuePairs &parameters, int propagation);
0096 
0097     /// \brief Forward processed data on to attached transformation

0098     /// \param outputSite unknown, system crash between keyboard and chair...

0099     /// \param inString the byte buffer to process

0100     /// \param length the size of the string, in bytes

0101     /// \param messageEnd means how many filters to signal MessageEnd() to, including this one

0102     /// \param blocking specifies whether the object should block when processing input

0103     /// \param channel the channel to process the data

0104     /// \return the number of bytes that remain to be processed (i.e., bytes not processed).

0105     ///  0 indicates all bytes were processed.

0106     size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0107 
0108     /// \brief Output multiple bytes that may be modified by callee.

0109     /// \param outputSite unknown, system crash between keyboard and chair...

0110     /// \param inString the byte buffer to process

0111     /// \param length the size of the string, in bytes

0112     /// \param messageEnd means how many filters to signal MessageEnd() to, including this one

0113     /// \param blocking specifies whether the object should block when processing input

0114     /// \param channel the channel to process the data

0115     /// \return the number of bytes that remain to be processed (i.e., bytes not processed).

0116     ///  0 indicates all bytes were processed.

0117     size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0118 
0119     /// \brief Signals the end of messages to the object

0120     /// \param outputSite unknown, system crash between keyboard and chair...

0121     /// \param propagation the number of attached transformations the  MessageEnd() signal should be passed

0122     /// \param blocking specifies whether the object should block when processing input

0123     /// \param channel the channel to process the data

0124     /// \return true is the MessageEnd signal was successful, false otherwise.

0125     /// \details propagation count includes this object. Setting  propagation to <tt>1</tt> means this

0126     ///  object only. Setting propagation to <tt>-1</tt> means unlimited propagation.

0127     bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0128 
0129     /// \brief Flush buffered input and/or output, with signal propagation

0130     /// \param outputSite unknown, system crash between keyboard and chair...

0131     /// \param hardFlush is used to indicate whether all data should be flushed

0132     /// \param propagation the number of attached transformations the  Flush() signal should be passed

0133     /// \param blocking specifies whether the object should block when processing input

0134     /// \param channel the channel to process the data

0135     /// \return true is the Flush signal was successful, false otherwise.

0136     /// \details propagation count includes this object. Setting  propagation to <tt>1</tt> means this

0137     ///  object only. Setting  propagation to <tt>-1</tt> means unlimited propagation.

0138     /// \note Hard flushes must be used with care. It means try to process and output everything, even if

0139     ///  there may not be enough data to complete the action. For example, hard flushing a  HexDecoder

0140     ///  would cause an error if you do it after inputing an odd number of hex encoded characters.

0141     /// \note For some types of filters, like  ZlibDecompressor, hard flushes can only

0142     ///  be done at "synchronization points". These synchronization points are positions in the data

0143     ///  stream that are created by hard flushes on the corresponding reverse filters, in this

0144     ///  example ZlibCompressor. This is useful when zlib compressed data is moved across a

0145     ///  network in packets and compression state is preserved across packets, as in the SSH2 protocol.

0146     bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0147 
0148     /// \brief Marks the end of a series of messages, with signal propagation

0149     /// \param outputSite unknown, system crash between keyboard and chair...

0150     /// \param propagation the number of attached transformations the  MessageSeriesEnd() signal should be passed

0151     /// \param blocking specifies whether the object should block when processing input

0152     /// \param channel the channel to process the data

0153     /// \return true is the MessageEnd signal was successful, false otherwise.

0154     /// \details Each object that receives the signal will perform its processing, decrement

0155     ///  propagation, and then pass the signal on to attached transformations if the value is not 0.

0156     /// \details propagation count includes this object. Setting  propagation to <tt>1</tt> means this

0157     ///  object only. Setting  propagation to <tt>-1</tt> means unlimited propagation.

0158     /// \note There should be a MessageEnd() immediately before MessageSeriesEnd().

0159     bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0160 
0161 private:
0162     member_ptr<BufferedTransformation> m_attachment;
0163 
0164 protected:
0165     size_t m_inputPosition;
0166     int m_continueAt;
0167 };
0168 
0169 /// \brief Create a working space in a BufferedTransformation

0170 struct CRYPTOPP_DLL FilterPutSpaceHelper
0171 {
0172     virtual ~FilterPutSpaceHelper() {}
0173 
0174     /// \brief Create a working space in a BufferedTransformation

0175     /// \param target BufferedTransformation for the working space

0176     /// \param channel channel for the working space

0177     /// \param minSize minimum size of the allocation, in bytes

0178     /// \param desiredSize preferred size of the allocation, in bytes

0179     /// \param bufferSize actual size of the allocation, in bytes

0180     /// \pre <tt>desiredSize >= minSize</tt> and <tt>bufferSize >= minSize</tt>.

0181     /// \details bufferSize is an IN and OUT parameter. If HelpCreatePutSpace() returns a non-NULL value, then

0182     ///  bufferSize is valid and provides the size of the working space created for the caller.

0183     /// \details Internally, HelpCreatePutSpace() calls \ref BufferedTransformation::ChannelCreatePutSpace

0184     ///  "ChannelCreatePutSpace()" using desiredSize. If the target returns desiredSize with a size less

0185     ///  than minSize (i.e., the request could not be fulfilled), then an internal SecByteBlock

0186     ///  called m_tempSpace is resized and used for the caller.

0187     byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
0188     {
0189         CRYPTOPP_ASSERT(desiredSize >= minSize && bufferSize >= minSize);
0190         if (m_tempSpace.size() < minSize)
0191         {
0192             byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
0193             if (desiredSize >= minSize)
0194             {
0195                 bufferSize = desiredSize;
0196                 return result;
0197             }
0198             m_tempSpace.New(bufferSize);
0199         }
0200 
0201         bufferSize = m_tempSpace.size();
0202         return m_tempSpace.begin();
0203     }
0204 
0205     /// \brief Create a working space in a BufferedTransformation

0206     /// \param target the BufferedTransformation for the working space

0207     /// \param channel channel for the working space

0208     /// \param minSize minimum size of the allocation, in bytes

0209     /// \return pointer to the created space

0210     /// \details Internally, the overload calls HelpCreatePutSpace() using minSize for missing arguments.

0211     /// \details The filter will delete the space. The caller does not need to delete the space.

0212     byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize)
0213         {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
0214 
0215     /// \brief Create a working space in a BufferedTransformation

0216     /// \param target the BufferedTransformation for the working space

0217     /// \param channel channel for the working space

0218     /// \param minSize minimum size of the allocation, in bytes

0219     /// \param bufferSize the actual size of the allocation, in bytes

0220     /// \details Internally, the overload calls HelpCreatePutSpace() using minSize for missing arguments.

0221     /// \details The filter will delete the space. The caller does not need to delete the space.

0222     byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t bufferSize)
0223         {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
0224 
0225     /// \brief Temporary working space

0226     SecByteBlock m_tempSpace;
0227 };
0228 
0229 /// \brief Measure how many bytes and messages pass through the filter

0230 /// \details measure how many bytes and messages pass through the filter. The filter also serves as valve by

0231 ///  maintaining a list of ranges to skip during processing.

0232 class CRYPTOPP_DLL MeterFilter : public Bufferless<Filter>
0233 {
0234 public:
0235     virtual ~MeterFilter() {}
0236 
0237     /// \brief Construct a MeterFilter

0238     /// \param attachment an optional attached transformation

0239     /// \param transparent flag indicating if the filter should function transparently

0240     /// \details attachment can be NULL. The filter is transparent by default. If the filter is

0241     ///  transparent, then PutMaybeModifiable() does not process a request and always returns 0.

0242     MeterFilter(BufferedTransformation *attachment=NULLPTR, bool transparent=true)
0243         : m_transparent(transparent), m_currentMessageBytes(0), m_totalBytes(0)
0244         , m_currentSeriesMessages(0), m_totalMessages(0), m_totalMessageSeries(0)
0245         , m_begin(NULLPTR), m_length(0) {Detach(attachment); ResetMeter();}
0246 
0247     /// \brief Set or change the transparent mode of this object

0248     /// \param transparent the new transparent mode

0249     void SetTransparent(bool transparent) {m_transparent = transparent;}
0250 
0251     /// \brief Adds a range to skip during processing

0252     /// \param message the message to apply the range

0253     /// \param position the 0-based index in the current stream

0254     /// \param size the length of the range

0255     /// \param sortNow flag indicating whether the range should be sorted

0256     /// \details Internally, MeterFilter maitains a deque of ranges to skip. As messages are processed,

0257     ///  ranges of bytes are skipped according to the list of ranges.

0258     void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow = true);
0259 
0260     /// \brief Resets the meter

0261     /// \details ResetMeter() reinitializes the meter by setting counters to 0 and removing previous

0262     ///  skip ranges.

0263     void ResetMeter();
0264 
0265     // BufferedTransformation in cryptlib.h

0266     void IsolatedInitialize(const NameValuePairs &parameters)
0267         {CRYPTOPP_UNUSED(parameters); ResetMeter();}
0268 
0269     /// \brief Number of bytes in the current message

0270     /// \return the number of bytes in the current message

0271     lword GetCurrentMessageBytes() const {return m_currentMessageBytes;}
0272 
0273     /// \brief Number of bytes processed by the filter

0274     /// \return the number of bytes processed by the filter

0275     lword GetTotalBytes() const {return m_totalBytes;}
0276 
0277     /// \brief Message number in the series

0278     /// \return the message number in the series

0279     unsigned int GetCurrentSeriesMessages() const {return m_currentSeriesMessages;}
0280 
0281     /// \brief Number of messages in the message series

0282     /// \return the number of messages in the message series

0283     unsigned int GetTotalMessages() const {return m_totalMessages;}
0284 
0285     /// \brief Number of messages processed by the filter

0286     /// \return the number of messages processed by the filter

0287     unsigned int GetTotalMessageSeries() const {return m_totalMessageSeries;}
0288 
0289     // BufferedTransformation in cryptlib.h

0290     byte * CreatePutSpace(size_t &size) {return AttachedTransformation()->CreatePutSpace(size);}
0291     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0292     size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking);
0293     bool IsolatedMessageSeriesEnd(bool blocking);
0294 
0295 private:
0296     size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable);
0297     bool ShouldPropagateMessageEnd() const {return m_transparent;}
0298     bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;}
0299 
0300     struct MessageRange
0301     {
0302         inline bool operator<(const MessageRange &b) const  // BCB2006 workaround: this has to be a member function

0303             {return message < b.message || (message == b.message && position < b.position);}
0304         unsigned int message; lword position; lword size;
0305     };
0306 
0307     bool m_transparent;
0308     lword m_currentMessageBytes, m_totalBytes;
0309     unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
0310     std::deque<MessageRange> m_rangesToSkip;
0311     byte *m_begin;
0312     size_t m_length;
0313 };
0314 
0315 /// \brief A transparent MeterFilter

0316 /// \sa MeterFilter, OpaqueFilter

0317 class CRYPTOPP_DLL TransparentFilter : public MeterFilter
0318 {
0319 public:
0320     /// \brief Construct a TransparentFilter

0321     /// \param attachment an optional attached transformation

0322     TransparentFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, true) {}
0323 };
0324 
0325 /// \brief A non-transparent MeterFilter

0326 /// \sa MeterFilter, TransparentFilter

0327 class CRYPTOPP_DLL OpaqueFilter : public MeterFilter
0328 {
0329 public:
0330     /// \brief Construct an OpaqueFilter

0331     /// \param attachment an optional attached transformation

0332     OpaqueFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, false) {}
0333 };
0334 
0335 /// \brief Divides an input stream into discrete blocks

0336 /// \details FilterWithBufferedInput divides the input stream into a first block, a number of

0337 ///  middle blocks, and a last block. First and last blocks are optional, and middle blocks may

0338 ///  be a stream instead (i.e. <tt>blockSize == 1</tt>).

0339 /// \sa AuthenticatedEncryptionFilter, AuthenticatedDecryptionFilter, HashVerificationFilter,

0340 ///  SignatureVerificationFilter, StreamTransformationFilter

0341 class CRYPTOPP_DLL FilterWithBufferedInput : public Filter
0342 {
0343 public:
0344     virtual ~FilterWithBufferedInput() {}
0345 
0346     /// \brief Construct a FilterWithBufferedInput with an attached transformation

0347     /// \param attachment an attached transformation

0348     FilterWithBufferedInput(BufferedTransformation *attachment);
0349 
0350     /// \brief Construct a FilterWithBufferedInput with an attached transformation

0351     /// \param firstSize the size of the first block

0352     /// \param blockSize the size of middle blocks

0353     /// \param lastSize the size of the last block

0354     /// \param attachment an attached transformation

0355     /// \details firstSize and lastSize may be 0. blockSize must be at least 1.

0356     FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment);
0357 
0358     void IsolatedInitialize(const NameValuePairs &parameters);
0359     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0360     {
0361         return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false);
0362     }
0363 
0364     size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
0365     {
0366         return PutMaybeModifiable(inString, length, messageEnd, blocking, true);
0367     }
0368 
0369     /// \brief Flushes data buffered by this object, without signal propagation

0370     /// \param hardFlush indicates whether all data should be flushed

0371     /// \param blocking specifies whether the object should block when processing input

0372     /// \return true if the Flush was successful, false otherwise

0373     /// \details IsolatedFlush() calls ForceNextPut() if hardFlush is true

0374     /// \note  hardFlush must be used with care

0375     bool IsolatedFlush(bool hardFlush, bool blocking);
0376 
0377     /// \brief Flushes data buffered by this object

0378     /// \details The input buffer may contain more than blockSize bytes if <tt>lastSize != 0</tt>.

0379     ///  ForceNextPut() forces a call to NextPut() if this is the case.

0380     void ForceNextPut();
0381 
0382 protected:
0383     virtual bool DidFirstPut() const {return m_firstInputDone;}
0384     virtual size_t GetFirstPutSize() const {return m_firstSize;}
0385     virtual size_t GetBlockPutSize() const {return m_blockSize;}
0386     virtual size_t GetLastPutSize() const {return m_lastSize;}
0387 
0388     virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
0389         {CRYPTOPP_UNUSED(parameters); CRYPTOPP_UNUSED(firstSize); CRYPTOPP_UNUSED(blockSize); CRYPTOPP_UNUSED(lastSize); InitializeDerived(parameters);}
0390     virtual void InitializeDerived(const NameValuePairs &parameters)
0391         {CRYPTOPP_UNUSED(parameters);}
0392     // FirstPut() is called if (firstSize != 0 and totalLength >= firstSize)

0393     // or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received)).

0394     // inString is m_firstSize in length.

0395     virtual void FirstPut(const byte *inString) =0;
0396     // NextPut() is called if totalLength >= firstSize+blockSize+lastSize

0397     virtual void NextPutSingle(const byte *inString)
0398         {CRYPTOPP_UNUSED(inString); CRYPTOPP_ASSERT(false);}
0399     // Same as NextPut() except length can be a multiple of blockSize

0400     // Either NextPut() or NextPutMultiple() must be overridden

0401     virtual void NextPutMultiple(const byte *inString, size_t length);
0402     // Same as NextPutMultiple(), but inString can be modified

0403     virtual void NextPutModifiable(byte *inString, size_t length)
0404         {NextPutMultiple(inString, length);}
0405     /// \brief Input the last block of data

0406     /// \param inString the input byte buffer

0407     /// \param length the size of the input buffer, in bytes

0408     /// \details LastPut() processes the last block of data and signals attached filters to do the same.

0409     ///  LastPut() is always called. The pseudo algorithm for the logic is:

0410     /// <pre>

0411     ///     if totalLength < firstSize then length == totalLength

0412     ///     else if totalLength <= firstSize+lastSize then length == totalLength-firstSize

0413     ///     else lastSize <= length < lastSize+blockSize

0414     /// </pre>

0415     virtual void LastPut(const byte *inString, size_t length) =0;
0416     virtual void FlushDerived() {}
0417 
0418 protected:
0419     size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable);
0420     void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable)
0421     {
0422         if (modifiable) NextPutModifiable(inString, length);
0423         else NextPutMultiple(inString, length);
0424     }
0425 
0426     // This function should no longer be used, put this here to cause a compiler error

0427     // if someone tries to override NextPut().

0428     virtual int NextPut(const byte *inString, size_t length)
0429         {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_ASSERT(false); return 0;}
0430 
0431     class BlockQueue
0432     {
0433     public:
0434         void ResetQueue(size_t blockSize, size_t maxBlocks);
0435         byte *GetBlock();
0436         byte *GetContigousBlocks(size_t &numberOfBytes);
0437         size_t GetAll(byte *outString);
0438         void Put(const byte *inString, size_t length);
0439         size_t CurrentSize() const {return m_size;}
0440         size_t MaxSize() const {return m_buffer.size();}
0441 
0442     private:
0443         SecByteBlock m_buffer;
0444         size_t m_blockSize, m_maxBlocks, m_size;
0445         byte *m_begin;
0446     };
0447 
0448     size_t m_firstSize, m_blockSize, m_lastSize;
0449     bool m_firstInputDone;
0450     BlockQueue m_queue;
0451 };
0452 
0453 /// \brief A filter that buffers input using a ByteQueue

0454 /// \details FilterWithInputQueue will buffer input using a ByteQueue. When the filter receives

0455 ///  a \ref BufferedTransformation::MessageEnd() "MessageEnd()" signal it will pass the data

0456 ///  on to its attached transformation.

0457 class CRYPTOPP_DLL FilterWithInputQueue : public Filter
0458 {
0459 public:
0460     virtual ~FilterWithInputQueue() {}
0461 
0462     /// \brief Construct a FilterWithInputQueue

0463     /// \param attachment an optional attached transformation

0464     FilterWithInputQueue(BufferedTransformation *attachment=NULLPTR) : Filter(attachment) {}
0465 
0466     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0467     {
0468         if (!blocking)
0469             throw BlockingInputOnly("FilterWithInputQueue");
0470 
0471         m_inQueue.Put(inString, length);
0472         if (messageEnd)
0473         {
0474             IsolatedMessageEnd(blocking);
0475             Output(0, NULLPTR, 0, messageEnd, blocking);
0476         }
0477         return 0;
0478     }
0479 
0480 protected:
0481     virtual bool IsolatedMessageEnd(bool blocking) =0;
0482     void IsolatedInitialize(const NameValuePairs &parameters)
0483         {CRYPTOPP_UNUSED(parameters); m_inQueue.Clear();}
0484 
0485     ByteQueue m_inQueue;
0486 };
0487 
0488 /// \struct BlockPaddingSchemeDef

0489 /// \brief Padding schemes used for block ciphers

0490 /// \since Crypto++ 5.0

0491 struct BlockPaddingSchemeDef
0492 {
0493     /// \enum BlockPaddingScheme

0494     /// \brief Padding schemes used for block ciphers.

0495     /// \details DEFAULT_PADDING means PKCS_PADDING if <tt>cipher.MandatoryBlockSize() > 1 &&

0496     ///  cipher.MinLastBlockSize() == 0</tt>, which holds for ECB or CBC mode. Otherwise,

0497     ///  NO_PADDING for modes like OFB, CFB, CTR, CBC-CTS.

0498     /// \sa <A HREF="http://www.weidai.com/scan-mirror/csp.html">Block Cipher Padding</A> for

0499     ///  additional details.

0500     /// \since Crypto++ 5.0

0501     enum BlockPaddingScheme {
0502         /// \brief No padding added to a block

0503         /// \since Crypto++ 5.0

0504         NO_PADDING,
0505         /// \brief 0's padding added to a block

0506         /// \since Crypto++ 5.0

0507         ZEROS_PADDING,
0508         /// \brief PKCS padding added to a block

0509         /// \since Crypto++ 5.0

0510         PKCS_PADDING,
0511         /// \brief 1 and 0's padding added to a block

0512         /// \since Crypto++ 5.0

0513         ONE_AND_ZEROS_PADDING,
0514         /// \brief W3C padding added to a block

0515         /// \sa <A HREF="http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html">XML

0516         ///  Encryption Syntax and Processing</A>

0517         /// \since Crypto++ 6.0

0518         W3C_PADDING,
0519         /// \brief Default padding scheme

0520         /// \since Crypto++ 5.0

0521         DEFAULT_PADDING
0522     };
0523 };
0524 
0525 /// \brief Filter wrapper for StreamTransformation

0526 /// \details StreamTransformationFilter() is a filter wrapper for StreamTransformation(). It is used when

0527 ///  pipelining data for stream ciphers and confidentiality-only block ciphers. The filter will optionally

0528 ///  handle padding and unpadding when needed. If you are using an authenticated encryption mode of operation,

0529 ///  then use AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter()

0530 /// \since Crypto++ 5.0

0531 class CRYPTOPP_DLL StreamTransformationFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef, private FilterPutSpaceHelper
0532 {
0533 public:
0534     virtual ~StreamTransformationFilter() {}
0535 
0536     /// \brief Construct a StreamTransformationFilter

0537     /// \param c reference to a StreamTransformation

0538     /// \param attachment an optional attached transformation

0539     /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"

0540     /// \details This constructor creates a StreamTransformationFilter() for stream ciphers and

0541     ///  confidentiality-only block cipher modes of operation. If you are using an authenticated

0542     ///  encryption mode of operation, then use either AuthenticatedEncryptionFilter() or

0543     ///  AuthenticatedDecryptionFilter().

0544     /// \sa AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter()

0545     StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULLPTR, BlockPaddingScheme padding = DEFAULT_PADDING);
0546 
0547     std::string AlgorithmName() const {return m_cipher.AlgorithmName();}
0548 
0549 protected:
0550 
0551     friend class AuthenticatedEncryptionFilter;
0552     friend class AuthenticatedDecryptionFilter;
0553 
0554     /// \brief Construct a StreamTransformationFilter

0555     /// \param c reference to a StreamTransformation

0556     /// \param attachment an optional attached transformation

0557     /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"

0558     /// \param authenticated flag indicating whether the filter should allow authenticated encryption schemes

0559     /// \details This constructor is used for authenticated encryption mode of operation and by

0560     ///  AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter().

0561     StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding, bool authenticated);
0562 
0563     void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0564     void FirstPut(const byte *inString);
0565     void NextPutMultiple(const byte *inString, size_t length);
0566     void NextPutModifiable(byte *inString, size_t length);
0567     void LastPut(const byte *inString, size_t length);
0568 
0569     static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding);
0570 
0571     StreamTransformation &m_cipher;
0572     BlockPaddingScheme m_padding;
0573     unsigned int m_mandatoryBlockSize;
0574     unsigned int m_optimalBufferSize;
0575     unsigned int m_reservedBufferSize;
0576     bool m_isSpecial;
0577 };
0578 
0579 /// \brief Filter wrapper for HashTransformation

0580 /// \since Crypto++ 1.0

0581 class CRYPTOPP_DLL HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper
0582 {
0583 public:
0584     virtual ~HashFilter() {}
0585 
0586     /// \brief Construct a HashFilter

0587     /// \param hm reference to a HashTransformation

0588     /// \param attachment an optional attached transformation

0589     /// \param putMessage flag indicating whether the original message should be passed to an attached transformation

0590     /// \param truncatedDigestSize the size of the digest

0591     /// \param messagePutChannel the channel on which the message should be output

0592     /// \param hashPutChannel the channel on which the digest should be output

0593     HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL);
0594 
0595     std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
0596     void IsolatedInitialize(const NameValuePairs &parameters);
0597     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0598     byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);}
0599 
0600 private:
0601     HashTransformation &m_hashModule;
0602     bool m_putMessage;
0603     unsigned int m_digestSize;
0604     byte *m_space;
0605     std::string m_messagePutChannel, m_hashPutChannel;
0606 };
0607 
0608 /// \brief Filter wrapper for HashTransformation

0609 /// \since Crypto++ 4.0

0610 class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput
0611 {
0612 public:
0613     virtual ~HashVerificationFilter() {}
0614 
0615     /// \brief Exception thrown when a data integrity check failure is encountered

0616     class HashVerificationFailed : public Exception
0617     {
0618     public:
0619         HashVerificationFailed()
0620             : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerificationFilter: message hash or MAC not valid") {}
0621     };
0622 
0623     /// \enum Flags

0624     /// \brief Flags controlling filter behavior.

0625     /// \details The flags are a bitmask and can be OR'd together.

0626     enum Flags {
0627         /// \brief The hash is at the end of the message (i.e., concatenation of message+hash)

0628         HASH_AT_END=0,
0629         /// \brief The hash is at the beginning of the message (i.e., concatenation of hash+message)

0630         HASH_AT_BEGIN=1,
0631         /// \brief The message should be passed to an attached transformation

0632         PUT_MESSAGE=2,
0633         /// \brief The hash should be passed to an attached transformation

0634         PUT_HASH=4,
0635         /// \brief The result of the verification should be passed to an attached transformation

0636         PUT_RESULT=8,
0637         /// \brief The filter should throw a HashVerificationFailed if a failure is encountered

0638         THROW_EXCEPTION=16,
0639         /// \brief Default flags using HASH_AT_BEGIN and PUT_RESULT

0640         DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT
0641     };
0642 
0643     /// \brief Construct a HashVerificationFilter

0644     /// \param hm reference to a HashTransformation

0645     /// \param attachment an optional attached transformation

0646     /// \param flags flags indicating behaviors for the filter

0647     /// \param truncatedDigestSize the size of the digest

0648     /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.

0649     HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1);
0650 
0651     std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
0652     bool GetLastResult() const {return m_verified;}
0653 
0654 protected:
0655     void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0656     void FirstPut(const byte *inString);
0657     void NextPutMultiple(const byte *inString, size_t length);
0658     void LastPut(const byte *inString, size_t length);
0659 
0660 private:
0661     friend class AuthenticatedDecryptionFilter;
0662 
0663     HashTransformation &m_hashModule;
0664     word32 m_flags;
0665     unsigned int m_digestSize;
0666     bool m_verified;
0667     SecByteBlock m_expectedHash;
0668 };
0669 
0670 /// \brief Filter wrapper for encrypting with AuthenticatedSymmetricCipher

0671 /// \details AuthenticatedEncryptionFilter() is a wrapper for encrypting with

0672 ///  AuthenticatedSymmetricCipher(), optionally handling padding/unpadding when needed.

0673 /// \details AuthenticatedDecryptionFilter() for Crypto++ 8.2 and earlier

0674 ///  had a bug where a FileSource() would cause an exception, but a StringSource()

0675 ///  was OK. Also see <A HREF=

0676 ///  "https://github.com/weidai11/cryptopp/issues/817">Issue 817</A> and <A HREF=

0677 ///  "https://github.com/weidai11/cryptopp/commit/ff110c6e183e">Commit ff110c6e183e</A>.

0678 /// \sa AuthenticatedSymmetricCipher, AuthenticatedDecryptionFilter, EAX, CCM, GCM,

0679 ///  and <A HREF="https://www.cryptopp.com/wiki/AadSource">AadSource</A> on the

0680 ///  Crypto++ wiki.

0681 /// \since Crypto++ 5.6.0

0682 class CRYPTOPP_DLL AuthenticatedEncryptionFilter : public StreamTransformationFilter
0683 {
0684 public:
0685     virtual ~AuthenticatedEncryptionFilter() {}
0686 
0687     /// \brief Construct a AuthenticatedEncryptionFilter

0688     /// \param c reference to a AuthenticatedSymmetricCipher

0689     /// \param attachment an optional attached transformation

0690     /// \param putAAD flag indicating whether the AAD should be passed to an attached transformation

0691     /// \param truncatedDigestSize the size of the digest

0692     /// \param macChannel the channel on which the MAC should be output

0693     /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"

0694     /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.

0695     /// \since Crypto++ 5.6.0

0696     AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding = DEFAULT_PADDING);
0697 
0698     void IsolatedInitialize(const NameValuePairs &parameters);
0699     byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
0700     size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
0701 
0702     /// \brief Input the last block of data

0703     /// \param inString the input byte buffer

0704     /// \param length the size of the input buffer, in bytes

0705     /// \details LastPut() processes the last block of data and signals attached filters to do the same.

0706     ///  LastPut() is always called. The pseudo algorithm for the logic is:

0707     /// <pre>

0708     ///     if totalLength < firstSize then length == totalLength

0709     ///     else if totalLength <= firstSize+lastSize then length == totalLength-firstSize

0710     ///     else lastSize <= length < lastSize+blockSize

0711     /// </pre>

0712     void LastPut(const byte *inString, size_t length);
0713 
0714 protected:
0715     HashFilter m_hf;
0716 };
0717 
0718 /// \brief Filter wrapper for decrypting with AuthenticatedSymmetricCipher

0719 /// \details AuthenticatedDecryptionFilter() is a wrapper for decrypting with

0720 ///  AuthenticatedSymmetricCipher(), optionally handling padding/unpadding when

0721 ///  needed.

0722 /// \details AuthenticatedDecryptionFilter() for Crypto++ 8.2 and earlier

0723 ///  had a bug where a FileSource() would cause an exception, but a StringSource()

0724 ///  was OK. Also see <A HREF=

0725 ///  "https://github.com/weidai11/cryptopp/issues/817">Issue 817</A> and <A HREF=

0726 ///  "https://github.com/weidai11/cryptopp/commit/ff110c6e183e">Commit ff110c6e183e</A>.

0727 /// \sa AuthenticatedSymmetricCipher, AuthenticatedEncryptionFilter, EAX, CCM, GCM,

0728 ///  and <A HREF="https://www.cryptopp.com/wiki/AadSource">AadSource</A> on the

0729 ///  Crypto++ wiki.

0730 /// \since Crypto++ 5.6.0

0731 class CRYPTOPP_DLL AuthenticatedDecryptionFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef
0732 {
0733 public:
0734     /// \enum Flags

0735     /// \brief Flags controlling filter behavior.

0736     /// \details The flags are a bitmask and can be OR'd together.

0737     enum Flags {
0738         /// \brief The MAC is at the end of the message (i.e., concatenation of message+mac)

0739         MAC_AT_END=0,
0740         /// \brief The MAC is at the beginning of the message (i.e., concatenation of mac+message)

0741         MAC_AT_BEGIN=1,
0742         /// \brief The filter should throw a HashVerificationFailed if a failure is encountered

0743         THROW_EXCEPTION=16,
0744         /// \brief Default flags using THROW_EXCEPTION

0745         DEFAULT_FLAGS = THROW_EXCEPTION
0746     };
0747 
0748     virtual ~AuthenticatedDecryptionFilter() {}
0749 
0750     /// \brief Construct a AuthenticatedDecryptionFilter

0751     /// \param c reference to a AuthenticatedSymmetricCipher

0752     /// \param attachment an optional attached transformation

0753     /// \param flags flags indicating behaviors for the filter

0754     /// \param truncatedDigestSize the size of the digest

0755     /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"

0756     /// \details Additional authenticated data should be given in channel "AAD".

0757     /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.

0758     /// \since Crypto++ 5.6.0

0759     AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding = DEFAULT_PADDING);
0760 
0761     std::string AlgorithmName() const {return m_hashVerifier.AlgorithmName();}
0762     byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
0763     size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
0764     size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
0765         { return ChannelPut2(channel, begin, length, messageEnd, blocking); }
0766     /// \brief Get verifier result

0767     /// \return true if the digest on the previosus message was valid, false otherwise

0768     bool GetLastResult() const {return m_hashVerifier.GetLastResult();}
0769 
0770 protected:
0771     void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0772     void FirstPut(const byte *inString);
0773     void NextPutMultiple(const byte *inString, size_t length);
0774 
0775     /// \brief Input the last block of data

0776     /// \param inString the input byte buffer

0777     /// \param length the size of the input buffer, in bytes

0778     /// \details LastPut() processes the last block of data and signals attached filters to do the same.

0779     ///  LastPut() is always called. The pseudo algorithm for the logic is:

0780     /// <pre>

0781     ///     if totalLength < firstSize then length == totalLength

0782     ///     else if totalLength <= firstSize+lastSize then length == totalLength-firstSize

0783     ///     else lastSize <= length < lastSize+blockSize

0784     /// </pre>

0785     void LastPut(const byte *inString, size_t length);
0786 
0787     HashVerificationFilter m_hashVerifier;
0788     StreamTransformationFilter m_streamFilter;
0789 };
0790 
0791 /// \brief Filter wrapper for PK_Signer

0792 /// \since Crypto++ 4.0

0793 class CRYPTOPP_DLL SignerFilter : public Unflushable<Filter>
0794 {
0795 public:
0796     virtual ~SignerFilter() {}
0797 
0798     /// \brief Construct a SignerFilter

0799     /// \param rng a RandomNumberGenerator derived class

0800     /// \param signer a PK_Signer derived class

0801     /// \param attachment an optional attached transformation

0802     /// \param putMessage flag indicating whether the original message should be passed to an attached transformation

0803     SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULLPTR, bool putMessage=false)
0804         : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
0805 
0806     std::string AlgorithmName() const {return m_signer.AlgorithmName();}
0807 
0808     void IsolatedInitialize(const NameValuePairs &parameters);
0809     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0810 
0811 private:
0812     RandomNumberGenerator &m_rng;
0813     const PK_Signer &m_signer;
0814     member_ptr<PK_MessageAccumulator> m_messageAccumulator;
0815     bool m_putMessage;
0816     SecByteBlock m_buf;
0817 };
0818 
0819 /// \brief Filter wrapper for PK_Verifier

0820 /// \details This filter was formerly named <tt>VerifierFilter</tt>. The name changed at Crypto++ 5.0.

0821 /// \since Crypto++ 4.0

0822 class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput
0823 {
0824 public:
0825     /// \brief Exception thrown when an invalid signature is encountered

0826     class SignatureVerificationFailed : public Exception
0827     {
0828     public:
0829         SignatureVerificationFailed()
0830             : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
0831     };
0832 
0833     /// \enum Flags

0834     /// \brief Flags controlling filter behavior.

0835     /// \details The flags are a bitmask and can be OR'd together.

0836     enum Flags {
0837         /// \brief The signature is at the end of the message (i.e., concatenation of message+signature)

0838         SIGNATURE_AT_END=0,
0839         /// \brief The signature is at the beginning of the message (i.e., concatenation of signature+message)

0840         SIGNATURE_AT_BEGIN=1,
0841         /// \brief The message should be passed to an attached transformation

0842         PUT_MESSAGE=2,
0843         /// \brief The signature should be passed to an attached transformation

0844         PUT_SIGNATURE=4,
0845         /// \brief The result of the verification should be passed to an attached transformation

0846         PUT_RESULT=8,
0847         /// \brief The filter should throw a HashVerificationFailed if a failure is encountered

0848         THROW_EXCEPTION=16,
0849         /// \brief Default flags using SIGNATURE_AT_BEGIN and PUT_RESULT

0850         DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT
0851     };
0852 
0853     virtual ~SignatureVerificationFilter() {}
0854 
0855     /// \brief Construct a SignatureVerificationFilter

0856     /// \param verifier a PK_Verifier derived class

0857     /// \param attachment an optional attached transformation

0858     /// \param flags flags indicating behaviors for the filter

0859     SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS);
0860 
0861     std::string AlgorithmName() const {return m_verifier.AlgorithmName();}
0862 
0863     /// \brief Retrieves the result of the last verification

0864     /// \return true if the signature on the previosus message was valid, false otherwise

0865     bool GetLastResult() const {return m_verified;}
0866 
0867 protected:
0868     void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0869     void FirstPut(const byte *inString);
0870     void NextPutMultiple(const byte *inString, size_t length);
0871     void LastPut(const byte *inString, size_t length);
0872 
0873 private:
0874     const PK_Verifier &m_verifier;
0875     member_ptr<PK_MessageAccumulator> m_messageAccumulator;
0876     word32 m_flags;
0877     SecByteBlock m_signature;
0878     bool m_verified;
0879 };
0880 
0881 /// \brief Redirect input to another BufferedTransformation without owning it

0882 /// \since Crypto++ 4.0

0883 class CRYPTOPP_DLL Redirector : public CustomSignalPropagation<Sink>
0884 {
0885 public:
0886     /// \enum Behavior

0887     /// \brief Controls signal propagation behavior

0888     enum Behavior
0889     {
0890         /// \brief Pass data only

0891         DATA_ONLY = 0x00,
0892         /// \brief Pass signals

0893         PASS_SIGNALS = 0x01,
0894         /// \brief Pass wait events

0895         PASS_WAIT_OBJECTS = 0x02,
0896         /// \brief Pass everything

0897         /// \details PASS_EVERYTHING is default

0898         PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
0899     };
0900 
0901     virtual ~Redirector() {}
0902 
0903     /// \brief Construct a Redirector

0904     Redirector() : m_target(NULLPTR), m_behavior(PASS_EVERYTHING) {}
0905 
0906     /// \brief Construct a Redirector

0907     /// \param target the destination BufferedTransformation

0908     /// \param behavior Behavior "flags" specifying signal propagation

0909     Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING)
0910         : m_target(&target), m_behavior(behavior) {}
0911 
0912     /// \brief Redirect input to another BufferedTransformation

0913     /// \param target the destination BufferedTransformation

0914     void Redirect(BufferedTransformation &target) {m_target = &target;}
0915     /// \brief Stop redirecting input

0916     void StopRedirection() {m_target = NULLPTR;}
0917 
0918     /// \brief Retrieve signal propagation behavior

0919     /// \return the current signal propagation behavior

0920     Behavior GetBehavior() {return static_cast<Behavior>(m_behavior);}
0921     /// \brief Set signal propagation behavior

0922     /// \param behavior the new signal propagation behavior

0923     void SetBehavior(Behavior behavior) {m_behavior=behavior;}
0924     /// \brief Retrieve signal propagation behavior

0925     /// \return true if the Redirector passes signals, false otherwise.

0926     bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;}
0927     /// \brief Set signal propagation behavior

0928     /// \param pass flag indicating if the Redirector should pass signals

0929     void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~static_cast<word32>(PASS_SIGNALS); }
0930     /// \brief Retrieve signal propagation behavior

0931     /// \return true if the Redirector passes wait objects, false otherwise.

0932     bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
0933     /// \brief Set signal propagation behavior

0934     /// \param pass flag indicating if the Redirector should pass wait objects

0935     void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~static_cast<word32>(PASS_WAIT_OBJECTS); }
0936 
0937     bool CanModifyInput() const
0938         {return m_target ? m_target->CanModifyInput() : false;}
0939 
0940     void Initialize(const NameValuePairs &parameters, int propagation);
0941     byte * CreatePutSpace(size_t &size)
0942     {
0943         if (m_target)
0944             return m_target->CreatePutSpace(size);
0945         else
0946         {
0947             size = 0;
0948             return NULLPTR;
0949         }
0950     }
0951     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0952         {return m_target ? m_target->Put2(inString, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0953     bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
0954         {return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;}
0955     bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
0956         {return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
0957 
0958     byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
0959     {
0960         if (m_target)
0961             return m_target->ChannelCreatePutSpace(channel, size);
0962         else
0963         {
0964             size = 0;
0965             return NULLPTR;
0966         }
0967     }
0968     size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
0969         {return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0970     size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
0971         {return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0972     bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
0973         {return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
0974     bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
0975         {return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
0976 
0977     unsigned int GetMaxWaitObjectCount() const
0978         { return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
0979     void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack)
0980         { if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
0981 
0982 private:
0983     BufferedTransformation *m_target;
0984     word32 m_behavior;
0985 };
0986 
0987 /// \brief Filter class that is a proxy for a sink

0988 /// \details Used By ProxyFilter

0989 /// \since Crypto++ 4.0

0990 class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation<Sink>
0991 {
0992 public:
0993     virtual ~OutputProxy() {}
0994 
0995     /// \brief Construct an OutputProxy

0996     /// \param owner the owning transformation

0997     /// \param passSignal flag indicating if signals should be passed

0998     OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
0999 
1000     /// \brief Retrieve passSignal flag

1001     /// \return flag indicating if signals should be passed

1002     bool GetPassSignal() const {return m_passSignal;}
1003     /// \brief Set passSignal flag

1004     /// \param passSignal flag indicating if signals should be passed

1005     void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
1006 
1007     byte * CreatePutSpace(size_t &size)
1008         {return m_owner.AttachedTransformation()->CreatePutSpace(size);}
1009     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1010         {return m_owner.AttachedTransformation()->Put2(inString, length, m_passSignal ? messageEnd : 0, blocking);}
1011     size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
1012         {return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
1013     void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
1014         {if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
1015     bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
1016         {return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
1017     bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
1018         {return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
1019 
1020     byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
1021         {return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);}
1022     size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
1023         {return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
1024     size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
1025         {return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
1026     bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
1027         {return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
1028     bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
1029         {return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
1030 
1031 private:
1032     BufferedTransformation &m_owner;
1033     bool m_passSignal;
1034 };
1035 
1036 /// \brief Base class for Filter classes that are proxies for a chain of other filters

1037 /// \since Crypto++ 4.0

1038 class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput
1039 {
1040 public:
1041     virtual ~ProxyFilter() {}
1042 
1043     /// \brief Construct a ProxyFilter

1044     /// \param filter an output filter

1045     /// \param firstSize the first Put size

1046     /// \param lastSize the last Put size

1047     /// \param attachment an attached transformation

1048     ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment);
1049 
1050     bool IsolatedFlush(bool hardFlush, bool blocking);
1051 
1052     /// \brief Sets the OutputProxy filter

1053     /// \param filter an OutputProxy filter

1054     void SetFilter(Filter *filter);
1055     void NextPutMultiple(const byte *s, size_t len);
1056     void NextPutModifiable(byte *inString, size_t length);
1057 
1058 protected:
1059     member_ptr<BufferedTransformation> m_filter;
1060 };
1061 
1062 /// \brief Proxy filter that doesn't modify the underlying filter's input or output

1063 /// \since Crypto++ 5.0

1064 class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter
1065 {
1066 public:
1067     /// \brief Construct a SimpleProxyFilter

1068     /// \param filter an output filter

1069     /// \param attachment an attached transformation

1070     SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
1071         : ProxyFilter(filter, 0, 0, attachment) {}
1072 
1073     void FirstPut(const byte * inString)
1074         {CRYPTOPP_UNUSED(inString);}
1075 
1076     /// \brief Input the last block of data

1077     /// \param inString the input byte buffer

1078     /// \param length the size of the input buffer, in bytes

1079     /// \details LastPut() processes the last block of data and signals attached filters to do the same.

1080     ///  LastPut() is always called. The pseudo algorithm for the logic is:

1081     /// <pre>

1082     ///     if totalLength < firstSize then length == totalLength

1083     ///     else if totalLength <= firstSize+lastSize then length == totalLength-firstSize

1084     ///     else lastSize <= length < lastSize+blockSize

1085     /// </pre>

1086     void LastPut(const byte *inString, size_t length)
1087         {CRYPTOPP_UNUSED(inString), CRYPTOPP_UNUSED(length); m_filter->MessageEnd();}
1088 };
1089 
1090 /// \brief Filter wrapper for PK_Encryptor

1091 /// \details PK_DecryptorFilter is a proxy for the filter created by PK_Encryptor::CreateEncryptionFilter.

1092 ///  This class provides symmetry with VerifierFilter.

1093 /// \since Crypto++ 5.0

1094 class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter
1095 {
1096 public:
1097     /// \brief Construct a PK_EncryptorFilter

1098     /// \param rng a RandomNumberGenerator derived class

1099     /// \param encryptor a PK_Encryptor derived class

1100     /// \param attachment an optional attached transformation

1101     PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULLPTR)
1102         : SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
1103 };
1104 
1105 /// \brief Filter wrapper for PK_Decryptor

1106 /// \details PK_DecryptorFilter is a proxy for the filter created by PK_Decryptor::CreateDecryptionFilter.

1107 ///  This class provides symmetry with SignerFilter.

1108 /// \since Crypto++ 5.0

1109 class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter
1110 {
1111 public:
1112     /// \brief Construct a PK_DecryptorFilter

1113     /// \param rng a RandomNumberGenerator derived class

1114     /// \param decryptor a PK_Decryptor derived class

1115     /// \param attachment an optional attached transformation

1116     PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULLPTR)
1117         : SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
1118 };
1119 
1120 /// \brief Append input to a string object

1121 /// \tparam T std::basic_string<char> type

1122 /// \details StringSinkTemplate is a StringSinkTemplate typedef

1123 /// \since Crypto++ 5.0

1124 template <class T>
1125 class StringSinkTemplate : public Bufferless<Sink>
1126 {
1127 public:
1128     typedef typename T::value_type value_type;
1129     virtual ~StringSinkTemplate() {}
1130 
1131     /// \brief Construct a StringSinkTemplate

1132     /// \param output std::basic_string<char> or std::vector<byte> type

1133     StringSinkTemplate(T &output)
1134         : m_output(&output) {CRYPTOPP_ASSERT(sizeof(value_type)==1);}
1135 
1136     void IsolatedInitialize(const NameValuePairs &parameters)
1137         {if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
1138 
1139     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1140     {
1141         CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
1142         if (length > 0)
1143         {
1144             typename T::size_type size = m_output->size();
1145             if (length < size && size + length > m_output->capacity())
1146                 m_output->reserve(2*size);
1147             m_output->insert(m_output->end(), (const value_type *)inString, (const value_type *)inString+length);
1148         }
1149         return 0;
1150     }
1151 
1152 private:
1153     T *m_output;
1154 };
1155 
1156 /// \brief Append input to a string object

1157 /// \details StringSink is a typedef for StringSinkTemplate<std::string>.

1158 /// \sa ArraySink, ArrayXorSink

1159 /// \since Crypto++ 4.0

1160 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::string>, StringSink);
1161 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>;
1162 
1163 /// \brief Append input to a std::vector<byte> object

1164 /// \details VectorSink is a typedef for StringSinkTemplate<std::vector<byte> >.

1165 /// \since Crypto++ 8.0

1166 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::vector<byte> >, VectorSink);
1167 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::vector<byte> >;
1168 
1169 /// \brief Incorporates input into RNG as additional entropy

1170 /// \since Crypto++ 4.0

1171 class RandomNumberSink : public Bufferless<Sink>
1172 {
1173 public:
1174     virtual ~RandomNumberSink() {}
1175 
1176     /// \brief Construct a RandomNumberSink

1177     RandomNumberSink()
1178         : m_rng(NULLPTR) {}
1179 
1180     /// \brief Construct a RandomNumberSink

1181     /// \param rng a RandomNumberGenerator derived class

1182     RandomNumberSink(RandomNumberGenerator &rng)
1183         : m_rng(&rng) {}
1184 
1185     void IsolatedInitialize(const NameValuePairs &parameters);
1186     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1187 
1188 private:
1189     RandomNumberGenerator *m_rng;
1190 };
1191 
1192 /// \brief Copy input to a memory buffer

1193 /// \details ArraySink wraps a fixed size buffer. The buffer is full once Put returns non-0.

1194 ///  When used in a pipeline, ArraySink silently discards input if the buffer is full.

1195 ///  AvailableSize() can be used to determine how much space remains in the buffer.

1196 ///  TotalPutLength() can be used to determine how many bytes were processed.

1197 /// \sa StringSink, ArrayXorSink

1198 /// \since Crypto++ 4.0

1199 class CRYPTOPP_DLL ArraySink : public Bufferless<Sink>
1200 {
1201 public:
1202     virtual ~ArraySink() {}
1203 
1204     /// \brief Construct an ArraySink

1205     /// \param parameters a set of NameValuePairs to initialize this object

1206     /// \details Name::OutputBuffer() is a mandatory parameter using this constructor.

1207     ArraySink(const NameValuePairs &parameters = g_nullNameValuePairs)
1208         : m_buf(NULLPTR), m_size(0), m_total(0) {IsolatedInitialize(parameters);}
1209 
1210     /// \brief Construct an ArraySink

1211     /// \param buf pointer to a memory buffer

1212     /// \param size length of the memory buffer

1213     ArraySink(byte *buf, size_t size)
1214         : m_buf(buf), m_size(size), m_total(0) {}
1215 
1216     /// \brief Provides the size remaining in the Sink

1217     /// \return size remaining in the Sink, in bytes

1218     size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);}
1219 
1220     /// \brief Provides the number of bytes written to the Sink

1221     /// \return number of bytes written to the Sink, in bytes

1222     lword TotalPutLength() {return m_total;}
1223 
1224     void IsolatedInitialize(const NameValuePairs &parameters);
1225     byte * CreatePutSpace(size_t &size);
1226     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1227 
1228 protected:
1229     byte *m_buf;
1230     size_t m_size;
1231     lword m_total;
1232 };
1233 
1234 /// \brief Xor input to a memory buffer

1235 /// \details ArrayXorSink wraps a fixed size buffer. The buffer is full once Put returns non-0.

1236 ///  When used in a pipeline, ArrayXorSink silently discards input if the buffer is full.

1237 ///  AvailableSize() can be used to determine how much space remains in the buffer.

1238 ///  TotalPutLength() can be used to determine how many bytes were processed.

1239 /// \sa StringSink, ArraySink

1240 /// \since Crypto++ 4.0

1241 class CRYPTOPP_DLL ArrayXorSink : public ArraySink
1242 {
1243 public:
1244     virtual ~ArrayXorSink() {}
1245 
1246     /// \brief Construct an ArrayXorSink

1247     /// \param buf pointer to a memory buffer

1248     /// \param size length of the memory buffer

1249     ArrayXorSink(byte *buf, size_t size)
1250         : ArraySink(buf, size) {}
1251 
1252     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1253     byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);}
1254 };
1255 
1256 /// \brief String-based implementation of Store interface

1257 /// \since Crypto++ 4.0

1258 class StringStore : public Store
1259 {
1260 public:
1261     /// \brief Construct a StringStore

1262     /// \param string pointer to a C-String

1263     StringStore(const char *string = NULLPTR)
1264         {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1265 
1266     /// \brief Construct a StringStore

1267     /// \param string pointer to a memory buffer

1268     /// \param length size of the memory buffer

1269     StringStore(const byte *string, size_t length)
1270         {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1271 
1272     /// \brief Construct a StringStore

1273     /// \tparam T std::basic_string<char> type

1274     /// \param string reference to a std::basic_string<char> type

1275     template <class T> StringStore(const T &string)
1276         {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1277 
1278     CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1279     CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1280 
1281 private:
1282     CRYPTOPP_DLL void StoreInitialize(const NameValuePairs &parameters);
1283 
1284     const byte *m_store;
1285     size_t m_length, m_count;
1286 };
1287 
1288 /// \brief RNG-based implementation of Source interface

1289 /// \since Crypto++ 4.0

1290 class CRYPTOPP_DLL RandomNumberStore : public Store
1291 {
1292 public:
1293     virtual ~RandomNumberStore() {}
1294 
1295     RandomNumberStore()
1296         : m_rng(NULLPTR), m_length(0), m_count(0) {}
1297 
1298     RandomNumberStore(RandomNumberGenerator &rng, lword length)
1299         : m_rng(&rng), m_length(length), m_count(0) {}
1300 
1301     bool AnyRetrievable() const {return MaxRetrievable() != 0;}
1302     lword MaxRetrievable() const {return m_length-m_count;}
1303 
1304     size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1305     size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
1306     {
1307         CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking);
1308         throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
1309     }
1310 
1311 private:
1312     void StoreInitialize(const NameValuePairs &parameters);
1313 
1314     RandomNumberGenerator *m_rng;
1315     lword m_length, m_count;
1316 };
1317 
1318 /// \brief Empty store

1319 /// \since Crypto++ 5.0

1320 class CRYPTOPP_DLL NullStore : public Store
1321 {
1322 public:
1323     NullStore(lword size = ULONG_MAX) : m_size(size) {}
1324     void StoreInitialize(const NameValuePairs &parameters)
1325         {CRYPTOPP_UNUSED(parameters);}
1326     lword MaxRetrievable() const {return m_size;}
1327     size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1328     size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1329 
1330 private:
1331     lword m_size;
1332 };
1333 
1334 /// \brief Implementation of BufferedTransformation's attachment interface

1335 /// \details Source is a cornerstone of the Pipeline trinitiy. Data flows from

1336 ///  Sources, through Filters, and then terminates in Sinks. The difference

1337 ///  between a Source and Filter is a Source \a pumps data, while a Filter does

1338 ///  not. The difference between a Filter and a Sink is a Filter allows an

1339 ///  attached transformation, while a Sink does not.

1340 /// \details See the discussion of BufferedTransformation in cryptlib.h for

1341 ///  more details.

1342 /// \sa Store and SourceTemplate

1343 /// \since Crypto++ 1.0

1344 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting<Filter>
1345 {
1346 public:
1347     virtual ~Source() {}
1348 
1349     /// \brief Construct a Source

1350     /// \param attachment an optional attached transformation

1351     Source(BufferedTransformation *attachment = NULLPTR)
1352         {Source::Detach(attachment);}
1353 
1354     /// \name PIPELINE

1355     //@{

1356 
1357     /// \brief Pump data to attached transformation

1358     /// \param pumpMax the maximum number of bytes to pump

1359     /// \return the number of bytes that remain to be processed (i.e., bytes not processed).

1360     ///  0 indicates all bytes were processed.

1361     /// \details Internally, Pump() calls Pump2().

1362     /// \note pumpMax is a <tt>lword</tt>, which is a 64-bit value that typically uses

1363     ///  <tt>LWORD_MAX</tt>. The default argument is <tt>SIZE_MAX</tt>, and it can be

1364     ///  32-bits or 64-bits.

1365     /// \sa Pump2, PumpAll, AnyRetrievable, MaxRetrievable

1366     lword Pump(lword pumpMax=SIZE_MAX)
1367         {Pump2(pumpMax); return pumpMax;}
1368 
1369     /// \brief Pump messages to attached transformation

1370     /// \param count the maximum number of messages to pump

1371     /// \return TODO

1372     /// \details Internally, PumpMessages() calls PumpMessages2().

1373     unsigned int PumpMessages(unsigned int count=UINT_MAX)
1374         {PumpMessages2(count); return count;}
1375 
1376     /// \brief Pump all data to attached transformation

1377     /// \details Pumps all data to the attached transformation and signal the end of the current

1378     ///  message. To avoid the MessageEnd() signal call \ref Pump "Pump(LWORD_MAX)" or \ref Pump2

1379     ///  "Pump2(LWORD_MAX, bool)".

1380     /// \details Internally, PumpAll() calls PumpAll2(), which calls PumpMessages().

1381     /// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable

1382     void PumpAll()
1383         {PumpAll2();}
1384 
1385     /// \brief Pump data to attached transformation

1386     /// \param byteCount the maximum number of bytes to pump

1387     /// \param blocking specifies whether the object should block when processing input

1388     /// \return the number of bytes that remain to be processed (i.e., bytes not processed).

1389     ///  0 indicates all bytes were processed.

1390     /// \details byteCount is an \a IN and \a OUT parameter. When the call is made, byteCount is the

1391     ///  requested size of the pump. When the call returns, byteCount is the number of bytes that

1392     ///  were pumped.

1393     /// \sa Pump, PumpAll, AnyRetrievable, MaxRetrievable

1394     virtual size_t Pump2(lword &byteCount, bool blocking=true) =0;
1395 
1396     /// \brief Pump messages to attached transformation

1397     /// \param messageCount the maximum number of messages to pump

1398     /// \param blocking specifies whether the object should block when processing input

1399     /// \details messageCount is an IN and OUT parameter.

1400     virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
1401 
1402     /// \brief Pump all data to attached transformation

1403     /// \param blocking specifies whether the object should block when processing input

1404     /// \return the number of bytes that remain to be processed (i.e., bytes not processed).

1405     ///  0 indicates all bytes were processed.

1406     /// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable

1407     virtual size_t PumpAll2(bool blocking=true);
1408 
1409     /// \brief Determines if the Source is exhausted

1410     /// \return true if the source has been exhausted

1411     virtual bool SourceExhausted() const =0;
1412 
1413     //@}

1414 
1415 protected:
1416     void SourceInitialize(bool pumpAll, const NameValuePairs &parameters)
1417     {
1418         IsolatedInitialize(parameters);
1419         if (pumpAll)
1420             PumpAll();
1421     }
1422 };
1423 
1424 /// \brief Transform a Store into a Source

1425 /// \tparam T the class or type

1426 /// \since Crypto++ 5.0

1427 template <class T>
1428 class SourceTemplate : public Source
1429 {
1430 public:
1431     virtual ~SourceTemplate() {}
1432 
1433     /// \brief Construct a SourceTemplate

1434     /// \param attachment an attached transformation

1435     SourceTemplate(BufferedTransformation *attachment)
1436         : Source(attachment) {}
1437     void IsolatedInitialize(const NameValuePairs &parameters)
1438         {m_store.IsolatedInitialize(parameters);}
1439     size_t Pump2(lword &byteCount, bool blocking=true)
1440         {return m_store.TransferTo2(*AttachedTransformation(), byteCount, DEFAULT_CHANNEL, blocking);}
1441     size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)
1442         {return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, DEFAULT_CHANNEL, blocking);}
1443     size_t PumpAll2(bool blocking=true)
1444         {return m_store.TransferAllTo2(*AttachedTransformation(), DEFAULT_CHANNEL, blocking);}
1445     bool SourceExhausted() const
1446         {return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
1447     void SetAutoSignalPropagation(int propagation)
1448         {m_store.SetAutoSignalPropagation(propagation);}
1449     int GetAutoSignalPropagation() const
1450         {return m_store.GetAutoSignalPropagation();}
1451 
1452 protected:
1453     T m_store;
1454 };
1455 
1456 /// \brief String-based implementation of the Source interface

1457 /// \since Crypto++ 4.0

1458 class CRYPTOPP_DLL StringSource : public SourceTemplate<StringStore>
1459 {
1460 public:
1461     /// \brief Construct a StringSource

1462     /// \param attachment an optional attached transformation

1463     StringSource(BufferedTransformation *attachment = NULLPTR)
1464         : SourceTemplate<StringStore>(attachment) {}
1465 
1466     /// \brief Construct a StringSource

1467     /// \param string C-String

1468     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

1469     /// \param attachment an optional attached transformation

1470     StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1471         : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1472 
1473     /// \brief Construct a StringSource

1474     /// \param string binary byte array

1475     /// \param length size of the byte array

1476     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

1477     /// \param attachment an optional attached transformation

1478     StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1479         : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1480 
1481     /// \brief Construct a StringSource

1482     /// \param string std::string

1483     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

1484     /// \param attachment an optional attached transformation

1485     StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1486         : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1487 };
1488 
1489 /// \brief Pointer-based implementation of the Source interface

1490 /// \details ArraySource is a typedef for StringSource. Use the third constructor for an array source.

1491 ///  The third constructor takes a pointer and length.

1492 /// \since Crypto++ 5.6.0

1493 DOCUMENTED_TYPEDEF(StringSource, ArraySource);
1494 
1495 /// \brief std::vector-based implementation of the Source interface

1496 /// \since Crypto++ 8.0

1497 class CRYPTOPP_DLL VectorSource : public SourceTemplate<StringStore>
1498 {
1499 public:
1500     /// \brief Construct a VectorSource

1501     /// \param attachment an optional attached transformation

1502     VectorSource(BufferedTransformation *attachment = NULLPTR)
1503         : SourceTemplate<StringStore>(attachment) {}
1504 
1505     /// \brief Construct a VectorSource

1506     /// \param vec vector of bytes

1507     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

1508     /// \param attachment an optional attached transformation

1509     VectorSource(const std::vector<byte> &vec, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1510         : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(vec)));}
1511 };
1512 
1513 /// \brief RNG-based implementation of Source interface

1514 /// \since Crypto++ 4.0

1515 class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate<RandomNumberStore>
1516 {
1517 public:
1518     RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1519         : SourceTemplate<RandomNumberStore>(attachment)
1520         {SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
1521 };
1522 
1523 NAMESPACE_END
1524 
1525 #if CRYPTOPP_MSC_VERSION
1526 # pragma warning(pop)
1527 #endif
1528 
1529 #endif