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