mqueue.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // mqueue.h - originally written and placed in the public domain by Wei Dai
  2. /// \file
  3. /// \brief Classes for an unlimited queue to store messages
  4. #ifndef CRYPTOPP_MQUEUE_H
  5. #define CRYPTOPP_MQUEUE_H
  6. #include "cryptlib.h"
  7. #include "queue.h"
  8. #include "filters.h"
  9. #include "misc.h"
  10. #include <deque>
  11. NAMESPACE_BEGIN(CryptoPP)
  12. /// \brief Data structure used to store messages
  13. /// \details The queue is implemented with a ByteQueue.
  14. /// \sa <A HREF="https://www.cryptopp.com/wiki/MessageQueue">MessageQueue</A>
  15. /// on the Crypto++ wiki.
  16. /// \since Crypto++ 2.0
  17. class CRYPTOPP_DLL MessageQueue : public AutoSignaling<BufferedTransformation>
  18. {
  19. public:
  20. virtual ~MessageQueue() {}
  21. /// \brief Construct a MessageQueue
  22. /// \param nodeSize the initial node size
  23. MessageQueue(unsigned int nodeSize=256);
  24. // BufferedTransformation
  25. void IsolatedInitialize(const NameValuePairs &parameters)
  26. {m_queue.IsolatedInitialize(parameters); m_lengths.assign(1, 0U); m_messageCounts.assign(1, 0U);}
  27. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  28. {
  29. CRYPTOPP_UNUSED(blocking);
  30. m_queue.Put(begin, length);
  31. m_lengths.back() += length;
  32. if (messageEnd)
  33. {
  34. m_lengths.push_back(0);
  35. m_messageCounts.back()++;
  36. }
  37. return 0;
  38. }
  39. bool IsolatedFlush(bool hardFlush, bool blocking)
  40. {CRYPTOPP_UNUSED(hardFlush), CRYPTOPP_UNUSED(blocking); return false;}
  41. bool IsolatedMessageSeriesEnd(bool blocking)
  42. {CRYPTOPP_UNUSED(blocking); m_messageCounts.push_back(0); return false;}
  43. lword MaxRetrievable() const
  44. {return m_lengths.front();}
  45. bool AnyRetrievable() const
  46. {return m_lengths.front() > 0;}
  47. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
  48. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
  49. lword TotalBytesRetrievable() const
  50. {return m_queue.MaxRetrievable();}
  51. unsigned int NumberOfMessages() const
  52. {return (unsigned int)m_lengths.size()-1;}
  53. bool GetNextMessage();
  54. unsigned int NumberOfMessagesInThisSeries() const
  55. {return m_messageCounts[0];}
  56. unsigned int NumberOfMessageSeries() const
  57. {return (unsigned int)m_messageCounts.size()-1;}
  58. /// \brief Copy messages from this object to another BufferedTransformation.
  59. /// \param target the destination BufferedTransformation
  60. /// \param count the number of messages to copy
  61. /// \param channel the channel on which the transfer should occur
  62. /// \return the number of messages that remain in the copy (i.e., messages not copied)
  63. unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
  64. /// \brief Peek data in the queue
  65. /// \param contiguousSize the size of the data
  66. /// \details Spy() peeks at data at the head of the queue. Spy() does
  67. /// not remove data from the queue.
  68. /// \details The data's size is returned in <tt>contiguousSize</tt>.
  69. /// Spy() returns the size of the first message in the list.
  70. const byte * Spy(size_t &contiguousSize) const;
  71. /// \brief Swap contents with another MessageQueue
  72. /// \param rhs the other MessageQueue
  73. void swap(MessageQueue &rhs);
  74. private:
  75. ByteQueue m_queue;
  76. std::deque<lword> m_lengths;
  77. std::deque<unsigned int> m_messageCounts;
  78. };
  79. /// \brief Filter that checks messages on two channels for equality
  80. class CRYPTOPP_DLL EqualityComparisonFilter : public Unflushable<Multichannel<Filter> >
  81. {
  82. public:
  83. /// \brief Different messages were detected
  84. struct MismatchDetected : public Exception
  85. {
  86. /// \brief Construct a MismatchDetected exception
  87. MismatchDetected() : Exception(DATA_INTEGRITY_CHECK_FAILED, "EqualityComparisonFilter: did not receive the same data on two channels") {}
  88. };
  89. /// \brief Construct an EqualityComparisonFilter
  90. /// \param attachment an attached transformation
  91. /// \param throwIfNotEqual flag indicating whether the objects throws
  92. /// \param firstChannel string naming the first channel
  93. /// \param secondChannel string naming the second channel
  94. /// \throw MismatchDetected if throwIfNotEqual is true and not equal
  95. /// \details If throwIfNotEqual is false, this filter will output a '\\0'
  96. /// byte when it detects a mismatch, '\\1' otherwise.
  97. EqualityComparisonFilter(BufferedTransformation *attachment=NULLPTR, bool throwIfNotEqual=true, const std::string &firstChannel="0", const std::string &secondChannel="1")
  98. : m_throwIfNotEqual(throwIfNotEqual), m_mismatchDetected(false)
  99. , m_firstChannel(firstChannel), m_secondChannel(secondChannel)
  100. {Detach(attachment);}
  101. // BufferedTransformation
  102. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
  103. bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
  104. protected:
  105. unsigned int MapChannel(const std::string &channel) const;
  106. bool HandleMismatchDetected(bool blocking);
  107. private:
  108. bool m_throwIfNotEqual, m_mismatchDetected;
  109. std::string m_firstChannel, m_secondChannel;
  110. MessageQueue m_q[2];
  111. };
  112. NAMESPACE_END
  113. #ifndef __BORLANDC__
  114. NAMESPACE_BEGIN(std)
  115. template<> inline void swap(CryptoPP::MessageQueue &a, CryptoPP::MessageQueue &b)
  116. {
  117. a.swap(b);
  118. }
  119. NAMESPACE_END
  120. #endif
  121. #endif