Crypto++  8.0
Free C++ class library of cryptographic schemes
queue.h
Go to the documentation of this file.
1 // queue.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file
4 /// \brief Classes for an unlimited queue to store bytes
5 
6 #ifndef CRYPTOPP_QUEUE_H
7 #define CRYPTOPP_QUEUE_H
8 
9 #include "cryptlib.h"
10 #include "simple.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 class ByteQueueNode;
15 
16 /// \brief Data structure used to store byte strings
17 /// \details The queue is implemented as a linked list of byte arrays
18 class CRYPTOPP_DLL ByteQueue : public Bufferless<BufferedTransformation>
19 {
20 public:
21  /// \brief Construct a ByteQueue
22  /// \param nodeSize the initial node size
23  /// \details Internally, ByteQueue uses a ByteQueueNode to store bytes, and \p nodeSize determines the
24  /// size of the ByteQueueNode. A value of 0 indicates the ByteQueueNode should be automatically sized,
25  /// which means a value of 256 is used.
26  ByteQueue(size_t nodeSize=0);
27 
28  /// \brief Copy construct a ByteQueue
29  /// \param copy the other ByteQueue
30  ByteQueue(const ByteQueue &copy);
31  ~ByteQueue();
32 
33  lword MaxRetrievable() const
34  {return CurrentSize();}
35  bool AnyRetrievable() const
36  {return !IsEmpty();}
37 
38  void IsolatedInitialize(const NameValuePairs &parameters);
39  byte * CreatePutSpace(size_t &size);
40  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
41 
42  size_t Get(byte &outByte);
43  size_t Get(byte *outString, size_t getMax);
44 
45  size_t Peek(byte &outByte) const;
46  size_t Peek(byte *outString, size_t peekMax) const;
47 
48  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
49  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
50 
51  // these member functions are not inherited
52  void SetNodeSize(size_t nodeSize);
53 
54  lword CurrentSize() const;
55  bool IsEmpty() const;
56 
57  void Clear();
58 
59  void Unget(byte inByte);
60  void Unget(const byte *inString, size_t length);
61 
62  const byte * Spy(size_t &contiguousSize) const;
63 
64  void LazyPut(const byte *inString, size_t size);
65  void LazyPutModifiable(byte *inString, size_t size);
66  void UndoLazyPut(size_t size);
67  void FinalizeLazyPut();
68 
69  ByteQueue & operator=(const ByteQueue &rhs);
70  bool operator==(const ByteQueue &rhs) const;
71  bool operator!=(const ByteQueue &rhs) const {return !operator==(rhs);}
72  byte operator[](lword i) const;
73  void swap(ByteQueue &rhs);
74 
75  /// \brief A ByteQueue iterator
76  class Walker : public InputRejecting<BufferedTransformation>
77  {
78  public:
79  /// \brief Construct a ByteQueue Walker
80  /// \param queue a ByteQueue
81  Walker(const ByteQueue &queue)
82  : m_queue(queue), m_node(NULLPTR), m_position(0), m_offset(0), m_lazyString(NULLPTR), m_lazyLength(0)
83  {Initialize();}
84 
85  lword GetCurrentPosition() {return m_position;}
86 
87  lword MaxRetrievable() const
88  {return m_queue.CurrentSize() - m_position;}
89 
90  void IsolatedInitialize(const NameValuePairs &parameters);
91 
92  size_t Get(byte &outByte);
93  size_t Get(byte *outString, size_t getMax);
94 
95  size_t Peek(byte &outByte) const;
96  size_t Peek(byte *outString, size_t peekMax) const;
97 
98  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
99  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
100 
101  private:
102  const ByteQueue &m_queue;
103  const ByteQueueNode *m_node;
104  lword m_position;
105  size_t m_offset;
106  const byte *m_lazyString;
107  size_t m_lazyLength;
108  };
109 
110  friend class Walker;
111 
112 private:
113  void CleanupUsedNodes();
114  void CopyFrom(const ByteQueue &copy);
115  void Destroy();
116 
117  bool m_autoNodeSize;
118  size_t m_nodeSize;
119  ByteQueueNode *m_head, *m_tail;
120  byte *m_lazyString;
121  size_t m_lazyLength;
122  bool m_lazyStringModifiable;
123 };
124 
125 /// use this to make sure LazyPut is finalized in event of exception
126 class CRYPTOPP_DLL LazyPutter
127 {
128 public:
129  LazyPutter(ByteQueue &bq, const byte *inString, size_t size)
130  : m_bq(bq) {bq.LazyPut(inString, size);}
131  ~LazyPutter()
132  {try {m_bq.FinalizeLazyPut();} catch(const Exception&) {CRYPTOPP_ASSERT(0);}}
133 protected:
134  LazyPutter(ByteQueue &bq) : m_bq(bq) {}
135 private:
136  ByteQueue &m_bq;
137 };
138 
139 /// like LazyPutter, but does a LazyPutModifiable instead
141 {
142 public:
143  LazyPutterModifiable(ByteQueue &bq, byte *inString, size_t size)
144  : LazyPutter(bq) {bq.LazyPutModifiable(inString, size);}
145 };
146 
147 NAMESPACE_END
148 
149 #ifndef __BORLANDC__
150 NAMESPACE_BEGIN(std)
151 template<> inline void swap(CryptoPP::ByteQueue &a, CryptoPP::ByteQueue &b)
152 {
153  a.swap(b);
154 }
155 NAMESPACE_END
156 #endif
157 
158 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:158
use this to make sure LazyPut is finalized in event of exception
Definition: queue.h:126
Classes providing basic library services.
Abstract base classes that provide a uniform interface to this library.
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: queue.h:33
STL namespace.
Base class for input rejecting filters.
Definition: simple.h:134
like LazyPutter, but does a LazyPutModifiable instead
Definition: queue.h:140
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: queue.h:87
Interface for buffered transformations.
Definition: cryptlib.h:1598
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.h:482
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:69
Data structure used to store byte strings.
Definition: queue.h:18
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
Definition: queue.h:35
Crypto++ library namespace.
A ByteQueue iterator.
Definition: queue.h:76
Walker(const ByteQueue &queue)
Construct a ByteQueue Walker.
Definition: queue.h:81
Base class for bufferless filters.
Definition: simple.h:98
Interface for retrieving values given their names.
Definition: cryptlib.h:293