ext-cryptopp/queue.h

273 lines
9.4 KiB
C
Raw Permalink Normal View History

// queue.h - originally written and placed in the public domain by Wei Dai
/// \file
/// \brief Classes for an unlimited queue to store bytes
2015-11-05 06:59:46 +00:00
#ifndef CRYPTOPP_QUEUE_H
#define CRYPTOPP_QUEUE_H
#include "cryptlib.h"
#include "simple.h"
NAMESPACE_BEGIN(CryptoPP)
class ByteQueueNode;
/// \brief Data structure used to store byte strings
/// \details The queue is implemented as a linked list of byte arrays.
/// Each byte array is stored in a ByteQueueNode.
/// \sa <A HREF="https://www.cryptopp.com/wiki/ByteQueue">ByteQueue</A>
/// on the Crypto++ wiki.
/// \since Crypto++ 2.0
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
{
public:
2021-04-09 23:07:56 +00:00
virtual ~ByteQueue();
/// \brief Construct a ByteQueue
/// \param nodeSize the initial node size
2021-04-09 23:07:56 +00:00
/// \details Internally, ByteQueue uses a ByteQueueNode to store bytes,
/// and <tt>nodeSize</tt> determines the size of the ByteQueueNode. A value
/// of 0 indicates the ByteQueueNode should be automatically sized,
/// which means a value of 256 is used.
2015-11-05 06:59:46 +00:00
ByteQueue(size_t nodeSize=0);
2015-12-16 08:22:22 +00:00
/// \brief Copy construct a ByteQueue
/// \param copy the other ByteQueue
2015-11-05 06:59:46 +00:00
ByteQueue(const ByteQueue &copy);
2021-04-09 23:07:56 +00:00
// BufferedTransformation
2015-11-05 06:59:46 +00:00
lword MaxRetrievable() const
{return CurrentSize();}
bool AnyRetrievable() const
{return !IsEmpty();}
void IsolatedInitialize(const NameValuePairs &parameters);
byte * CreatePutSpace(size_t &size);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
size_t Get(byte &outByte);
size_t Get(byte *outString, size_t getMax);
size_t Peek(byte &outByte) const;
size_t Peek(byte *outString, size_t peekMax) const;
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;
2021-04-09 23:07:56 +00:00
/// \brief Set node size
/// \param nodeSize the new node size, in bytes
/// \details The default node size is 256.
2015-11-05 06:59:46 +00:00
void SetNodeSize(size_t nodeSize);
2021-04-09 23:07:56 +00:00
/// \brief Determine data size
/// \return the data size, in bytes
2015-11-05 06:59:46 +00:00
lword CurrentSize() const;
2021-04-09 23:07:56 +00:00
/// \brief Determine data availability
2021-04-10 01:29:50 +00:00
/// \return true if the ByteQueue has data, false otherwise
2015-11-05 06:59:46 +00:00
bool IsEmpty() const;
2021-04-09 23:07:56 +00:00
/// \brief Empty the queue
2015-11-05 06:59:46 +00:00
void Clear();
2021-04-09 23:07:56 +00:00
/// \brief Insert data in the queue
/// \param inByte a byte to insert
/// \details Unget() inserts a byte at the head of the queue
2015-11-05 06:59:46 +00:00
void Unget(byte inByte);
2021-04-09 23:07:56 +00:00
/// \brief Insert data in the queue
/// \param inString a byte array to insert
/// \param length the size of the byte array
/// \details Unget() inserts a byte array at the head of the queue
2015-11-05 06:59:46 +00:00
void Unget(const byte *inString, size_t length);
2021-04-10 09:32:39 +00:00
/// \brief Peek data in the queue
2021-04-09 23:07:56 +00:00
/// \param contiguousSize the size of the data
/// \details Spy() peeks at data at the head of the queue. Spy() does
/// not remove data from the queue.
/// \details The data's size is returned in <tt>contiguousSize</tt>.
/// Spy() returns the size of the first byte array in the list. The
/// entire data may be larger since the queue is a linked list of
/// byte arrays.
2015-11-05 06:59:46 +00:00
const byte * Spy(size_t &contiguousSize) const;
2021-04-09 23:07:56 +00:00
/// \brief Insert data in the queue
/// \param inString a byte array to insert
2021-04-09 23:12:00 +00:00
/// \param size the length of the byte array
2021-04-09 23:07:56 +00:00
/// \details LazyPut() inserts a byte array at the tail of the queue.
/// The data may not be copied at this point. Rather, the pointer
/// and size to external data are recorded.
/// \details Another call to Put() or LazyPut() will force the data to
/// be copied. When lazy puts are used, the data is copied when
/// FinalizeLazyPut() is called.
/// \sa LazyPutter
2015-11-05 06:59:46 +00:00
void LazyPut(const byte *inString, size_t size);
2021-04-09 23:07:56 +00:00
/// \brief Insert data in the queue
/// \param inString a byte array to insert
2021-04-09 23:12:00 +00:00
/// \param size the length of the byte array
2021-04-09 23:07:56 +00:00
/// \details LazyPut() inserts a byte array at the tail of the queue.
/// The data may not be copied at this point. Rather, the pointer
/// and size to external data are recorded.
/// \details Another call to Put() or LazyPut() will force the data to
/// be copied. When lazy puts are used, the data is copied when
/// FinalizeLazyPut() is called.
/// \sa LazyPutter
2015-11-05 06:59:46 +00:00
void LazyPutModifiable(byte *inString, size_t size);
2021-04-09 23:07:56 +00:00
/// \brief Remove data from the queue
/// \param size the length of the data
/// \throw InvalidArgument if there is no lazy data in the queue or if
/// size is larger than the lazy string
/// \details UndoLazyPut() truncates data inserted using LazyPut() by
/// modifying size.
/// \sa LazyPutter
2015-11-05 06:59:46 +00:00
void UndoLazyPut(size_t size);
2021-04-09 23:07:56 +00:00
/// \brief Insert data in the queue
/// \details FinalizeLazyPut() copies external data inserted using
/// LazyPut() or LazyPutModifiable() into the tail of the queue.
/// \sa LazyPutter
2015-11-05 06:59:46 +00:00
void FinalizeLazyPut();
2021-04-09 23:07:56 +00:00
/// \brief Assign contents from another ByteQueue
2021-04-09 23:16:12 +00:00
/// \param rhs the other ByteQueue
2021-04-09 23:07:56 +00:00
/// \return reference to this ByteQueue
2015-11-05 06:59:46 +00:00
ByteQueue & operator=(const ByteQueue &rhs);
2021-04-09 23:07:56 +00:00
/// \brief Bitwise compare two ByteQueue
/// \param rhs the other ByteQueue
/// \return true if the size and bits are equal, false otherwise
/// \details operator==() walks each ByteQueue comparing bytes in
/// each queue. operator==() is not constant time.
2015-11-05 06:59:46 +00:00
bool operator==(const ByteQueue &rhs) const;
2021-04-09 23:07:56 +00:00
/// \brief Bitwise compare two ByteQueue
/// \param rhs the other ByteQueue
/// \return true if the size and bits are not equal, false otherwise
/// \details operator!=() is implemented in terms of operator==().
/// operator==() is not constant time.
2015-11-05 06:59:46 +00:00
bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
2021-04-09 23:07:56 +00:00
/// \brief Retrieve data from the queue
/// \param index of byte to retrieve
/// \return byte at the specified index
/// \details operator[]() does not perform bounds checking.
2021-04-09 23:16:12 +00:00
byte operator[](lword index) const;
2021-04-09 23:07:56 +00:00
/// \brief Swap contents with another ByteQueue
2021-04-10 09:32:39 +00:00
/// \param rhs the other ByteQueue
2015-11-05 06:59:46 +00:00
void swap(ByteQueue &rhs);
/// \brief A ByteQueue iterator
2015-11-05 06:59:46 +00:00
class Walker : public InputRejecting<BufferedTransformation>
{
public:
/// \brief Construct a ByteQueue Walker
/// \param queue a ByteQueue
2015-11-05 06:59:46 +00:00
Walker(const ByteQueue &queue)
2017-03-01 11:10:06 +00:00
: m_queue(queue), m_node(NULLPTR), m_position(0), m_offset(0), m_lazyString(NULLPTR), m_lazyLength(0)
{Initialize();}
2015-11-05 06:59:46 +00:00
lword GetCurrentPosition() {return m_position;}
lword MaxRetrievable() const
{return m_queue.CurrentSize() - m_position;}
void IsolatedInitialize(const NameValuePairs &parameters);
size_t Get(byte &outByte);
size_t Get(byte *outString, size_t getMax);
size_t Peek(byte &outByte) const;
size_t Peek(byte *outString, size_t peekMax) const;
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;
private:
const ByteQueue &m_queue;
const ByteQueueNode *m_node;
lword m_position;
size_t m_offset;
const byte *m_lazyString;
size_t m_lazyLength;
};
friend class Walker;
2021-04-09 23:07:56 +00:00
protected:
2015-11-05 06:59:46 +00:00
void CleanupUsedNodes();
void CopyFrom(const ByteQueue &copy);
void Destroy();
2021-04-09 23:07:56 +00:00
private:
2015-11-05 06:59:46 +00:00
ByteQueueNode *m_head, *m_tail;
byte *m_lazyString;
size_t m_lazyLength;
2021-04-09 23:07:56 +00:00
size_t m_nodeSize;
2015-11-05 06:59:46 +00:00
bool m_lazyStringModifiable;
2021-04-09 23:07:56 +00:00
bool m_autoNodeSize;
2015-11-05 06:59:46 +00:00
};
/// \brief Helper class to finalize Puts on ByteQueue
/// \details LazyPutter ensures LazyPut is committed to the ByteQueue
/// in event of exception. During destruction, the LazyPutter class
/// calls FinalizeLazyPut.
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL LazyPutter
{
public:
virtual ~LazyPutter() {
try {m_bq.FinalizeLazyPut();}
catch(const Exception&) {CRYPTOPP_ASSERT(0);}
}
/// \brief Construct a LazyPutter
/// \param bq the ByteQueue
/// \param inString a byte array to insert
/// \param size the length of the byte array
/// \details LazyPutter ensures LazyPut is committed to the ByteQueue
/// in event of exception. During destruction, the LazyPutter class
/// calls FinalizeLazyPut.
2015-11-05 06:59:46 +00:00
LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
: m_bq(bq) {bq.LazyPut(inString, size);}
2015-11-05 06:59:46 +00:00
protected:
LazyPutter(ByteQueue &bq) : m_bq(bq) {}
2015-11-05 06:59:46 +00:00
private:
ByteQueue &m_bq;
};
/// \brief Helper class to finalize Puts on ByteQueue
/// \details LazyPutterModifiable ensures LazyPut is committed to the
/// ByteQueue in event of exception. During destruction, the
/// LazyPutterModifiable class calls FinalizeLazyPut.
2015-11-05 06:59:46 +00:00
class LazyPutterModifiable : public LazyPutter
{
public:
/// \brief Construct a LazyPutterModifiable
/// \param bq the ByteQueue
/// \param inString a byte array to insert
/// \param size the length of the byte array
/// \details LazyPutterModifiable ensures LazyPut is committed to the
/// ByteQueue in event of exception. During destruction, the
/// LazyPutterModifiable class calls FinalizeLazyPut.
2015-11-05 06:59:46 +00:00
LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
: LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
};
NAMESPACE_END
#ifndef __BORLANDC__
NAMESPACE_BEGIN(std)
template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
2015-11-05 06:59:46 +00:00
{
a.swap(b);
}
NAMESPACE_END
#endif
#endif