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