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

100 lines
3.3 KiB

  1. #ifndef CRYPTOPP_MQUEUE_H
  2. #define CRYPTOPP_MQUEUE_H
  3. #include "queue.h"
  4. #include "filters.h"
  5. #include <deque>
  6. NAMESPACE_BEGIN(CryptoPP)
  7. //! Message Queue
  8. class CRYPTOPP_DLL MessageQueue : public AutoSignaling<BufferedTransformation>
  9. {
  10. public:
  11. MessageQueue(unsigned int nodeSize=256);
  12. void IsolatedInitialize(const NameValuePairs &parameters)
  13. {m_queue.IsolatedInitialize(parameters); m_lengths.assign(1, 0U); m_messageCounts.assign(1, 0U);}
  14. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  15. {
  16. m_queue.Put(begin, length);
  17. m_lengths.back() += length;
  18. if (messageEnd)
  19. {
  20. m_lengths.push_back(0);
  21. m_messageCounts.back()++;
  22. }
  23. return 0;
  24. }
  25. bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
  26. bool IsolatedMessageSeriesEnd(bool blocking)
  27. {m_messageCounts.push_back(0); return false;}
  28. lword MaxRetrievable() const
  29. {return m_lengths.front();}
  30. bool AnyRetrievable() const
  31. {return m_lengths.front() > 0;}
  32. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
  33. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
  34. lword TotalBytesRetrievable() const
  35. {return m_queue.MaxRetrievable();}
  36. unsigned int NumberOfMessages() const
  37. {return (unsigned int)m_lengths.size()-1;}
  38. bool GetNextMessage();
  39. unsigned int NumberOfMessagesInThisSeries() const
  40. {return m_messageCounts[0];}
  41. unsigned int NumberOfMessageSeries() const
  42. {return (unsigned int)m_messageCounts.size()-1;}
  43. unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
  44. const byte * Spy(size_t &contiguousSize) const;
  45. void swap(MessageQueue &rhs);
  46. private:
  47. ByteQueue m_queue;
  48. std::deque<lword> m_lengths;
  49. std::deque<unsigned int> m_messageCounts;
  50. };
  51. //! A filter that checks messages on two channels for equality
  52. class CRYPTOPP_DLL EqualityComparisonFilter : public Unflushable<Multichannel<Filter> >
  53. {
  54. public:
  55. struct MismatchDetected : public Exception {MismatchDetected() : Exception(DATA_INTEGRITY_CHECK_FAILED, "EqualityComparisonFilter: did not receive the same data on two channels") {}};
  56. /*! if throwIfNotEqual is false, this filter will output a '\\0' byte when it detects a mismatch, '\\1' otherwise */
  57. EqualityComparisonFilter(BufferedTransformation *attachment=NULL, bool throwIfNotEqual=true, const std::string &firstChannel="0", const std::string &secondChannel="1")
  58. : m_throwIfNotEqual(throwIfNotEqual), m_mismatchDetected(false)
  59. , m_firstChannel(firstChannel), m_secondChannel(secondChannel)
  60. {Detach(attachment);}
  61. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
  62. bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
  63. private:
  64. unsigned int MapChannel(const std::string &channel) const;
  65. bool HandleMismatchDetected(bool blocking);
  66. bool m_throwIfNotEqual, m_mismatchDetected;
  67. std::string m_firstChannel, m_secondChannel;
  68. MessageQueue m_q[2];
  69. };
  70. NAMESPACE_END
  71. #ifndef __BORLANDC__
  72. NAMESPACE_BEGIN(std)
  73. template<> inline void swap(CryptoPP::MessageQueue &a, CryptoPP::MessageQueue &b)
  74. {
  75. a.swap(b);
  76. }
  77. NAMESPACE_END
  78. #endif
  79. #endif