Security Scol plugin
queue.h
Go to the documentation of this file.
1// queue.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_QUEUE_H
7#define CRYPTOPP_QUEUE_H
8
9#include "cryptlib.h"
10#include "simple.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14class ByteQueueNode;
15
22class CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
23{
24public:
25 virtual ~ByteQueue();
26
33 ByteQueue(size_t nodeSize=0);
34
37 ByteQueue(const ByteQueue &copy);
38
39 // BufferedTransformation
41 {return CurrentSize();}
42 bool AnyRetrievable() const
43 {return !IsEmpty();}
44
45 void IsolatedInitialize(const NameValuePairs &parameters);
46 byte * CreatePutSpace(size_t &size);
47 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
48
49 size_t Get(byte &outByte);
50 size_t Get(byte *outString, size_t getMax);
51
52 size_t Peek(byte &outByte) const;
53 size_t Peek(byte *outString, size_t peekMax) const;
54
55 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
56 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
57
61 void SetNodeSize(size_t nodeSize);
62
65 lword CurrentSize() const;
66
69 bool IsEmpty() const;
70
72 void Clear();
73
77 void Unget(byte inByte);
78
83 void Unget(const byte *inString, size_t length);
84
93 const byte * Spy(size_t &contiguousSize) const;
94
105 void LazyPut(const byte *inString, size_t size);
106
117 void LazyPutModifiable(byte *inString, size_t size);
118
126 void UndoLazyPut(size_t size);
127
132 void FinalizeLazyPut();
133
137 ByteQueue & operator=(const ByteQueue &rhs);
138
144 bool operator==(const ByteQueue &rhs) const;
145
151 bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
152
157 byte operator[](lword index) const;
158
161 void swap(ByteQueue &rhs);
162
164 class Walker : public InputRejecting<BufferedTransformation>
165 {
166 public:
169 Walker(const ByteQueue &queue)
170 : m_queue(queue), m_node(NULLPTR), m_position(0), m_offset(0), m_lazyString(NULLPTR), m_lazyLength(0)
171 {Initialize();}
172
173 lword GetCurrentPosition() {return m_position;}
174
176 {return m_queue.CurrentSize() - m_position;}
177
178 void IsolatedInitialize(const NameValuePairs &parameters);
179
180 size_t Get(byte &outByte);
181 size_t Get(byte *outString, size_t getMax);
182
183 size_t Peek(byte &outByte) const;
184 size_t Peek(byte *outString, size_t peekMax) const;
185
186 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
187 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
188
189 private:
190 const ByteQueue &m_queue;
191 const ByteQueueNode *m_node;
192 lword m_position;
193 size_t m_offset;
194 const byte *m_lazyString;
195 size_t m_lazyLength;
196 };
197
198 friend class Walker;
199
200protected:
201 void CleanupUsedNodes();
202 void CopyFrom(const ByteQueue &copy);
203 void Destroy();
204
205private:
206 ByteQueueNode *m_head, *m_tail;
207 byte *m_lazyString;
208 size_t m_lazyLength;
209 size_t m_nodeSize;
210 bool m_lazyStringModifiable;
211 bool m_autoNodeSize;
212};
213
218class CRYPTOPP_DLL LazyPutter
219{
220public:
221 virtual ~LazyPutter() {
222 try {m_bq.FinalizeLazyPut();}
223 catch(const Exception&) {CRYPTOPP_ASSERT(0);}
224 }
225
233 LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
234 : m_bq(bq) {bq.LazyPut(inString, size);}
235
236protected:
237 LazyPutter(ByteQueue &bq) : m_bq(bq) {}
238
239private:
240 ByteQueue &m_bq;
241};
242
248{
249public:
257 LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
258 : LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
259};
260
261NAMESPACE_END
262
263#ifndef __BORLANDC__
264NAMESPACE_BEGIN(std)
265template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
266{
267 a.swap(b);
268}
269NAMESPACE_END
270#endif
271
272#endif
Interface for buffered transformations.
Definition cryptlib.h:1652
Base class for bufferless filters.
Definition simple.h:120
A ByteQueue iterator.
Definition queue.h:165
Walker(const ByteQueue &queue)
Construct a ByteQueue Walker.
Definition queue.h:169
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition queue.h:175
Data structure used to store byte strings.
Definition queue.h:23
void LazyPut(const byte *inString, size_t size)
Insert data in the queue.
Definition queue.cpp:271
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition queue.h:40
void LazyPutModifiable(byte *inString, size_t size)
Insert data in the queue.
Definition queue.cpp:286
bool operator!=(const ByteQueue &rhs) const
Bitwise compare two ByteQueue.
Definition queue.h:151
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
Definition queue.h:42
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
Base class for input rejecting filters.
Definition simple.h:195
Helper class to finalize Puts on ByteQueue.
Definition queue.h:219
LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
Construct a LazyPutter.
Definition queue.h:233
Helper class to finalize Puts on ByteQueue.
Definition queue.h:248
LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
Construct a LazyPutterModifiable.
Definition queue.h:257
Interface for retrieving values given their names.
Definition cryptlib.h:322
const lword LWORD_MAX
Large word type max value.
Definition config_int.h:164
word64 lword
Large word type.
Definition config_int.h:158
Abstract base classes that provide a uniform interface to this library.
Classes providing basic library services.