From b21162cf8e06f40baa1f58be6a8c17435cebc34d Mon Sep 17 00:00:00 2001 From: weidai Date: Fri, 4 Oct 2002 17:31:41 +0000 Subject: Initial revision git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@2 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- queue.h | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 queue.h (limited to 'queue.h') diff --git a/queue.h b/queue.h new file mode 100644 index 0000000..e247d9f --- /dev/null +++ b/queue.h @@ -0,0 +1,128 @@ +// specification file for an unlimited queue for storing bytes + +#ifndef CRYPTOPP_QUEUE_H +#define CRYPTOPP_QUEUE_H + +#include "simple.h" +//#include + +NAMESPACE_BEGIN(CryptoPP) + +/** The queue is implemented as a linked list of byte arrays, but you don't need to + know about that. So just ignore this next line. :) */ +class ByteQueueNode; + +//! Byte Queue +class ByteQueue : public Bufferless +{ +public: + ByteQueue(unsigned int m_nodeSize=256); + ByteQueue(const ByteQueue ©); + ~ByteQueue(); + + unsigned long MaxRetrievable() const + {return CurrentSize();} + bool AnyRetrievable() const + {return !IsEmpty();} + + void IsolatedInitialize(const NameValuePairs ¶meters); + byte * CreatePutSpace(unsigned int &size); + unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking); + + unsigned int Get(byte &outByte); + unsigned int Get(byte *outString, unsigned int getMax); + + unsigned int Peek(byte &outByte) const; + unsigned int Peek(byte *outString, unsigned int peekMax) const; + + unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true); + unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const; + + // these member functions are not inherited + void SetNodeSize(unsigned int nodeSize) {m_nodeSize = nodeSize;} + + unsigned long CurrentSize() const; + bool IsEmpty() const; + + void Clear(); + + void Unget(byte inByte); + void Unget(const byte *inString, unsigned int length); + + const byte * Spy(unsigned int &contiguousSize) const; + + void LazyPut(const byte *inString, unsigned int size); + void UndoLazyPut(unsigned int size); + void FinalizeLazyPut(); + + ByteQueue & operator=(const ByteQueue &rhs); + bool operator==(const ByteQueue &rhs) const; + byte operator[](unsigned long i) const; + void swap(ByteQueue &rhs); + + class Walker : public InputRejecting + { + public: + Walker(const ByteQueue &queue) + : m_queue(queue) {Initialize();} + + unsigned long GetCurrentPosition() {return m_position;} + + unsigned long MaxRetrievable() const + {return m_queue.CurrentSize() - m_position;} + + void IsolatedInitialize(const NameValuePairs ¶meters); + + unsigned int Get(byte &outByte); + unsigned int Get(byte *outString, unsigned int getMax); + + unsigned int Peek(byte &outByte) const; + unsigned int Peek(byte *outString, unsigned int peekMax) const; + + unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true); + unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const; + + private: + const ByteQueue &m_queue; + const ByteQueueNode *m_node; + unsigned long m_position; + unsigned int m_offset; + const byte *m_lazyString; + unsigned int m_lazyLength; + }; + + friend class Walker; + +private: + void CleanupUsedNodes(); + void CopyFrom(const ByteQueue ©); + void Destroy(); + + unsigned int m_nodeSize; + ByteQueueNode *m_head, *m_tail; + const byte *m_lazyString; + unsigned int m_lazyLength; +}; + +//! use this to make sure LazyPut is finalized in event of exception +class LazyPutter +{ +public: + LazyPutter(ByteQueue &bq, const byte *inString, unsigned int size) + : m_bq(bq) {bq.LazyPut(inString, size);} + ~LazyPutter() + {try {m_bq.FinalizeLazyPut();} catch(...) {}} +private: + ByteQueue &m_bq; +}; + +NAMESPACE_END + +NAMESPACE_BEGIN(std) +template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b) +{ + a.swap(b); +} +NAMESPACE_END + +#endif -- cgit v1.2.1