- Main Page
- Namespaces
- Classes
- Files

# queue.h

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