00001
00002
00003
00004
00005
00006
00007 #ifndef CRYPTOPP_QUEUE_H
00008 #define CRYPTOPP_QUEUE_H
00009
00010 #include "cryptlib.h"
00011 #include "simple.h"
00012
00013 NAMESPACE_BEGIN(CryptoPP)
00014
00015
00016
00017 class ByteQueueNode;
00018
00019
00020 class CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
00021 {
00022 public:
00023 ByteQueue(size_t nodeSize=0);
00024 ByteQueue(const ByteQueue ©);
00025 ~ByteQueue();
00026
00027 lword MaxRetrievable() const
00028 {return CurrentSize();}
00029 bool AnyRetrievable() const
00030 {return !IsEmpty();}
00031
00032 void IsolatedInitialize(const NameValuePairs ¶meters);
00033 byte * CreatePutSpace(size_t &size);
00034 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
00035
00036 size_t Get(byte &outByte);
00037 size_t Get(byte *outString, size_t getMax);
00038
00039 size_t Peek(byte &outByte) const;
00040 size_t Peek(byte *outString, size_t peekMax) const;
00041
00042 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
00043 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
00044
00045
00046 void SetNodeSize(size_t nodeSize);
00047
00048 lword CurrentSize() const;
00049 bool IsEmpty() const;
00050
00051 void Clear();
00052
00053 void Unget(byte inByte);
00054 void Unget(const byte *inString, size_t length);
00055
00056 const byte * Spy(size_t &contiguousSize) const;
00057
00058 void LazyPut(const byte *inString, size_t size);
00059 void LazyPutModifiable(byte *inString, size_t size);
00060 void UndoLazyPut(size_t size);
00061 void FinalizeLazyPut();
00062
00063 ByteQueue & operator=(const ByteQueue &rhs);
00064 bool operator==(const ByteQueue &rhs) const;
00065 bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
00066 byte operator[](lword i) const;
00067 void swap(ByteQueue &rhs);
00068
00069 class Walker : public InputRejecting<BufferedTransformation>
00070 {
00071 public:
00072 Walker(const ByteQueue &queue)
00073 : m_queue(queue), m_node(NULL), m_position(0), m_offset(0), m_lazyString(NULL), m_lazyLength(0)
00074 {Initialize();}
00075
00076 lword GetCurrentPosition() {return m_position;}
00077
00078 lword MaxRetrievable() const
00079 {return m_queue.CurrentSize() - m_position;}
00080
00081 void IsolatedInitialize(const NameValuePairs ¶meters);
00082
00083 size_t Get(byte &outByte);
00084 size_t Get(byte *outString, size_t getMax);
00085
00086 size_t Peek(byte &outByte) const;
00087 size_t Peek(byte *outString, size_t peekMax) const;
00088
00089 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
00090 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
00091
00092 private:
00093 const ByteQueue &m_queue;
00094 const ByteQueueNode *m_node;
00095 lword m_position;
00096 size_t m_offset;
00097 const byte *m_lazyString;
00098 size_t m_lazyLength;
00099 };
00100
00101 friend class Walker;
00102
00103 private:
00104 void CleanupUsedNodes();
00105 void CopyFrom(const ByteQueue ©);
00106 void Destroy();
00107
00108 bool m_autoNodeSize;
00109 size_t m_nodeSize;
00110 ByteQueueNode *m_head, *m_tail;
00111 byte *m_lazyString;
00112 size_t m_lazyLength;
00113 bool m_lazyStringModifiable;
00114 };
00115
00116
00117 class CRYPTOPP_DLL LazyPutter
00118 {
00119 public:
00120 LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
00121 : m_bq(bq) {bq.LazyPut(inString, size);}
00122 ~LazyPutter()
00123 {try {m_bq.FinalizeLazyPut();} catch(const Exception&) {assert(0);}}
00124 protected:
00125 LazyPutter(ByteQueue &bq) : m_bq(bq) {}
00126 private:
00127 ByteQueue &m_bq;
00128 };
00129
00130
00131 class LazyPutterModifiable : public LazyPutter
00132 {
00133 public:
00134 LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
00135 : LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
00136 };
00137
00138 NAMESPACE_END
00139
00140 #ifndef __BORLANDC__
00141 NAMESPACE_BEGIN(std)
00142 template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
00143 {
00144 a.swap(b);
00145 }
00146 NAMESPACE_END
00147 #endif
00148
00149 #endif