ext-cryptopp/queue.h

145 lines
3.9 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
// specification file for an unlimited queue for storing bytes
#ifndef CRYPTOPP_QUEUE_H
#define CRYPTOPP_QUEUE_H
#include "simple.h"
//#include <algorithm>
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 CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
2002-10-04 17:31:41 +00:00
{
public:
2005-07-12 04:23:32 +00:00
ByteQueue(size_t nodeSize=0);
2002-10-04 17:31:41 +00:00
ByteQueue(const ByteQueue &copy);
~ByteQueue();
2005-07-12 04:23:32 +00:00
lword MaxRetrievable() const
2002-10-04 17:31:41 +00:00
{return CurrentSize();}
bool AnyRetrievable() const
{return !IsEmpty();}
void IsolatedInitialize(const NameValuePairs &parameters);
2005-07-12 04:23:32 +00:00
byte * CreatePutSpace(size_t &size);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
size_t Get(byte &outByte);
size_t Get(byte *outString, size_t getMax);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
size_t Peek(byte &outByte) const;
size_t Peek(byte *outString, size_t peekMax) const;
2002-10-04 17:31:41 +00:00
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
2002-10-04 17:31:41 +00:00
// these member functions are not inherited
2005-07-12 04:23:32 +00:00
void SetNodeSize(size_t nodeSize);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
lword CurrentSize() const;
2002-10-04 17:31:41 +00:00
bool IsEmpty() const;
void Clear();
void Unget(byte inByte);
2005-07-12 04:23:32 +00:00
void Unget(const byte *inString, size_t length);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
const byte * Spy(size_t &contiguousSize) const;
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
void LazyPut(const byte *inString, size_t size);
void LazyPutModifiable(byte *inString, size_t size);
void UndoLazyPut(size_t size);
2002-10-04 17:31:41 +00:00
void FinalizeLazyPut();
ByteQueue & operator=(const ByteQueue &rhs);
bool operator==(const ByteQueue &rhs) const;
bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
2005-07-12 04:23:32 +00:00
byte operator[](lword i) const;
2002-10-04 17:31:41 +00:00
void swap(ByteQueue &rhs);
class Walker : public InputRejecting<BufferedTransformation>
{
public:
Walker(const ByteQueue &queue)
: m_queue(queue) {Initialize();}
2005-07-12 04:23:32 +00:00
lword GetCurrentPosition() {return m_position;}
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
lword MaxRetrievable() const
2002-10-04 17:31:41 +00:00
{return m_queue.CurrentSize() - m_position;}
void IsolatedInitialize(const NameValuePairs &parameters);
2005-07-12 04:23:32 +00:00
size_t Get(byte &outByte);
size_t Get(byte *outString, size_t getMax);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
size_t Peek(byte &outByte) const;
size_t Peek(byte *outString, size_t peekMax) const;
2002-10-04 17:31:41 +00:00
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
2002-10-04 17:31:41 +00:00
private:
const ByteQueue &m_queue;
const ByteQueueNode *m_node;
2005-07-12 04:23:32 +00:00
lword m_position;
size_t m_offset;
2002-10-04 17:31:41 +00:00
const byte *m_lazyString;
2005-07-12 04:23:32 +00:00
size_t m_lazyLength;
2002-10-04 17:31:41 +00:00
};
friend class Walker;
private:
void CleanupUsedNodes();
void CopyFrom(const ByteQueue &copy);
void Destroy();
2003-06-20 03:12:54 +00:00
bool m_autoNodeSize;
2005-07-12 04:23:32 +00:00
size_t m_nodeSize;
2002-10-04 17:31:41 +00:00
ByteQueueNode *m_head, *m_tail;
2003-06-06 02:34:03 +00:00
byte *m_lazyString;
2005-07-12 04:23:32 +00:00
size_t m_lazyLength;
2003-06-06 02:34:03 +00:00
bool m_lazyStringModifiable;
2002-10-04 17:31:41 +00:00
};
//! use this to make sure LazyPut is finalized in event of exception
class CRYPTOPP_DLL LazyPutter
2002-10-04 17:31:41 +00:00
{
public:
2005-07-12 04:23:32 +00:00
LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
2002-10-04 17:31:41 +00:00
: m_bq(bq) {bq.LazyPut(inString, size);}
~LazyPutter()
{try {m_bq.FinalizeLazyPut();} catch(...) {}}
2003-06-06 02:34:03 +00:00
protected:
LazyPutter(ByteQueue &bq) : m_bq(bq) {}
2002-10-04 17:31:41 +00:00
private:
ByteQueue &m_bq;
};
2003-06-06 02:34:03 +00:00
//! like LazyPutter, but does a LazyPutModifiable instead
class LazyPutterModifiable : public LazyPutter
{
public:
2005-07-12 04:23:32 +00:00
LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
2003-06-06 02:34:03 +00:00
: LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
};
2002-10-04 17:31:41 +00:00
NAMESPACE_END
#ifndef __BORLANDC__
2002-10-04 17:31:41 +00:00
NAMESPACE_BEGIN(std)
template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
{
a.swap(b);
}
NAMESPACE_END
#endif
2002-10-04 17:31:41 +00:00
#endif