Crypto++
queue.h
1 // specification file for an unlimited queue for storing bytes
2 
3 #ifndef CRYPTOPP_QUEUE_H
4 #define CRYPTOPP_QUEUE_H
5 
6 #include "simple.h"
7 //#include <algorithm>
8 
9 NAMESPACE_BEGIN(CryptoPP)
10 
11 /** The queue is implemented as a linked list of byte arrays, but you don't need to
12  know about that. So just ignore this next line. :) */
13 class ByteQueueNode;
14 
15 //! Byte Queue
16 class CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
17 {
18 public:
19  ByteQueue(size_t nodeSize=0);
20  ByteQueue(const ByteQueue &copy);
21  ~ByteQueue();
22 
23  lword MaxRetrievable() const
24  {return CurrentSize();}
25  bool AnyRetrievable() const
26  {return !IsEmpty();}
27 
28  void IsolatedInitialize(const NameValuePairs &parameters);
29  byte * CreatePutSpace(size_t &size);
30  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
31 
32  size_t Get(byte &outByte);
33  size_t Get(byte *outString, size_t getMax);
34 
35  size_t Peek(byte &outByte) const;
36  size_t Peek(byte *outString, size_t peekMax) const;
37 
38  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
39  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
40 
41  // these member functions are not inherited
42  void SetNodeSize(size_t nodeSize);
43 
44  lword CurrentSize() const;
45  bool IsEmpty() const;
46 
47  void Clear();
48 
49  void Unget(byte inByte);
50  void Unget(const byte *inString, size_t length);
51 
52  const byte * Spy(size_t &contiguousSize) const;
53 
54  void LazyPut(const byte *inString, size_t size);
55  void LazyPutModifiable(byte *inString, size_t size);
56  void UndoLazyPut(size_t size);
57  void FinalizeLazyPut();
58 
59  ByteQueue & operator=(const ByteQueue &rhs);
60  bool operator==(const ByteQueue &rhs) const;
61  bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
62  byte operator[](lword i) const;
63  void swap(ByteQueue &rhs);
64 
65  class Walker : public InputRejecting<BufferedTransformation>
66  {
67  public:
68  Walker(const ByteQueue &queue)
69  : m_queue(queue) {Initialize();}
70 
71  lword GetCurrentPosition() {return m_position;}
72 
73  lword MaxRetrievable() const
74  {return m_queue.CurrentSize() - m_position;}
75 
76  void IsolatedInitialize(const NameValuePairs &parameters);
77 
78  size_t Get(byte &outByte);
79  size_t Get(byte *outString, size_t getMax);
80 
81  size_t Peek(byte &outByte) const;
82  size_t Peek(byte *outString, size_t peekMax) const;
83 
84  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
85  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
86 
87  private:
88  const ByteQueue &m_queue;
89  const ByteQueueNode *m_node;
90  lword m_position;
91  size_t m_offset;
92  const byte *m_lazyString;
93  size_t m_lazyLength;
94  };
95 
96  friend class Walker;
97 
98 private:
99  void CleanupUsedNodes();
100  void CopyFrom(const ByteQueue &copy);
101  void Destroy();
102 
103  bool m_autoNodeSize;
104  size_t m_nodeSize;
105  ByteQueueNode *m_head, *m_tail;
106  byte *m_lazyString;
107  size_t m_lazyLength;
108  bool m_lazyStringModifiable;
109 };
110 
111 //! use this to make sure LazyPut is finalized in event of exception
112 class CRYPTOPP_DLL LazyPutter
113 {
114 public:
115  LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
116  : m_bq(bq) {bq.LazyPut(inString, size);}
117  ~LazyPutter()
118  {try {m_bq.FinalizeLazyPut();} catch(...) {}}
119 protected:
120  LazyPutter(ByteQueue &bq) : m_bq(bq) {}
121 private:
122  ByteQueue &m_bq;
123 };
124 
125 //! like LazyPutter, but does a LazyPutModifiable instead
127 {
128 public:
129  LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
130  : LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
131 };
132 
133 NAMESPACE_END
134 
135 #ifndef __BORLANDC__
136 NAMESPACE_BEGIN(std)
137 template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
138 {
139  a.swap(b);
140 }
141 NAMESPACE_END
142 #endif
143 
144 #endif