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