5 #ifndef CRYPTOPP_IMPORTS 14 static const unsigned int s_maxAutoNodeSize = 16*1024;
30 inline size_t MaxSize()
const {
return m_buf.
size();}
32 inline size_t CurrentSize()
const 37 inline bool UsedUp()
const 39 return (m_head==MaxSize());
47 inline size_t Put(
const byte *begin,
size_t length)
50 if (!begin || !length)
return length;
51 size_t l =
STDMIN(length, MaxSize()-m_tail);
52 if (m_buf+m_tail != begin)
53 memcpy(m_buf+m_tail, begin, l);
58 inline size_t Peek(byte &outByte)
const 63 outByte=m_buf[m_head];
67 inline size_t Peek(byte *target,
size_t copyMax)
const 69 size_t len =
STDMIN(copyMax, m_tail-m_head);
70 memcpy(target, m_buf+m_head, len);
76 size_t len = m_tail-m_head;
83 size_t len =
STDMIN(copyMax, m_tail-m_head);
88 inline size_t Get(byte &outByte)
90 size_t len = Peek(outByte);
95 inline size_t Get(byte *outString,
size_t getMax)
97 size_t len = Peek(outString, getMax);
104 size_t len = m_tail-m_head;
112 size_t len =
UnsignedMin(m_tail-m_head, transferMax);
118 inline size_t Skip(
size_t skipMax)
120 size_t len =
STDMIN(skipMax, m_tail-m_head);
125 inline byte operator[](
size_t i)
const 127 return m_buf[m_head+i];
133 size_t m_head, m_tail;
140 , m_head(NULLPTR), m_tail(NULLPTR), m_lazyString(NULLPTR), m_lazyLength(0), m_lazyStringModifiable(false)
145 SetNodeSize(nodeSize);
149 void ByteQueue::SetNodeSize(
size_t nodeSize)
151 m_autoNodeSize = !nodeSize;
152 m_nodeSize = m_autoNodeSize ? 256 : nodeSize;
161 void ByteQueue::CopyFrom(
const ByteQueue ©)
164 m_autoNodeSize = copy.m_autoNodeSize;
165 m_nodeSize = copy.m_nodeSize;
168 for (
ByteQueueNode *current=copy.m_head->m_next; current; current=current->m_next)
171 m_tail = m_tail->m_next;
174 m_tail->m_next = NULLPTR;
176 Put(copy.m_lazyString, copy.m_lazyLength);
179 ByteQueue::~ByteQueue()
184 void ByteQueue::Destroy()
186 for (
ByteQueueNode *next, *current=m_head; current; current=next)
188 next=current->m_next;
199 lword ByteQueue::CurrentSize()
const 203 for (
ByteQueueNode *current=m_head; current; current=current->m_next)
204 size += current->CurrentSize();
206 return size + m_lazyLength;
209 bool ByteQueue::IsEmpty()
const 211 return m_head==m_tail && m_head->CurrentSize()==0 && m_lazyLength==0;
214 void ByteQueue::Clear()
216 for (
ByteQueueNode *next, *current=m_head->m_next; current; current=next)
218 next=current->m_next;
224 m_head->m_next = NULLPTR;
228 size_t ByteQueue::Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking)
230 CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
232 if (m_lazyLength > 0)
236 while ((len=m_tail->Put(inString, length)) < length)
238 inString =
PtrAdd(inString, len);
240 if (m_autoNodeSize && m_nodeSize < s_maxAutoNodeSize)
246 while (m_nodeSize < length && m_nodeSize < s_maxAutoNodeSize);
249 m_tail = m_tail->m_next;
255 void ByteQueue::CleanupUsedNodes()
258 while (m_head && m_head != m_tail && m_head->UsedUp())
261 m_head=m_head->m_next;
266 if (m_head && m_head->CurrentSize() == 0)
270 void ByteQueue::LazyPut(
const byte *inString,
size_t size)
272 if (m_lazyLength > 0)
275 if (inString == m_tail->m_buf+m_tail->m_tail)
279 m_lazyString =
const_cast<byte *
>(inString);
281 m_lazyStringModifiable =
false;
285 void ByteQueue::LazyPutModifiable(byte *inString,
size_t size)
287 if (m_lazyLength > 0)
289 m_lazyString = inString;
291 m_lazyStringModifiable =
true;
294 void ByteQueue::UndoLazyPut(
size_t size)
296 if (m_lazyLength < size)
297 throw InvalidArgument(
"ByteQueue: size specified for UndoLazyPut is too large");
299 m_lazyLength -= size;
302 void ByteQueue::FinalizeLazyPut()
304 size_t len = m_lazyLength;
307 Put(m_lazyString, len);
312 if (m_head->Get(outByte))
314 if (m_head->UsedUp())
318 else if (m_lazyLength > 0)
320 outByte = *m_lazyString++;
336 if (m_head->Peek(outByte))
338 else if (m_lazyLength > 0)
340 outByte = *m_lazyString;
350 return (
size_t)
CopyTo(sink, peekMax);
360 lword bytesLeft = transferBytes;
361 for (
ByteQueueNode *current=m_head; bytesLeft && current; current=current->m_next)
362 bytesLeft -= current->TransferTo(target, bytesLeft, channel);
365 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_lazyLength);
368 if (m_lazyStringModifiable)
371 target.
ChannelPut(channel, m_lazyString, len);
372 m_lazyString =
PtrAdd(m_lazyString, len);
376 transferBytes -= bytesLeft;
382 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
392 lword transferBytes = end-begin;
394 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
395 begin += transferBytes;
399 void ByteQueue::Unget(byte inByte)
404 void ByteQueue::Unget(
const byte *inString,
size_t length)
409 size_t len =
STDMIN(length, m_head->m_head);
411 m_head->m_head = m_head->m_head - len;
412 memcpy(m_head->m_buf + m_head->m_head, inString + length, len);
417 newHead->m_next = m_head;
419 m_head->Put(inString, length);
423 const byte * ByteQueue::Spy(
size_t &contiguousSize)
const 425 contiguousSize = m_head->m_tail - m_head->m_head;
426 if (contiguousSize == 0 && m_lazyLength > 0)
428 contiguousSize = m_lazyLength;
432 return m_head->m_buf + m_head->m_head;
442 if (m_lazyLength > 0)
445 if (m_tail->m_tail == m_tail->MaxSize())
448 m_tail = m_tail->m_next;
451 size = m_tail->MaxSize() - m_tail->m_tail;
452 return PtrAdd(m_tail->m_buf.
begin(), m_tail->m_tail);
462 bool ByteQueue::operator==(
const ByteQueue &rhs)
const 464 const lword currentSize = CurrentSize();
466 if (currentSize != rhs.CurrentSize())
469 Walker walker1(*
this), walker2(rhs);
472 while (walker1.Get(b1) && walker2.
Get(b2))
479 byte ByteQueue::operator[](lword i)
const 481 for (
ByteQueueNode *current=m_head; current; current=current->m_next)
483 if (i < current->CurrentSize())
484 return (*current)[(size_t)i];
486 i -= current->CurrentSize();
490 return m_lazyString[i];
495 std::swap(m_autoNodeSize, rhs.m_autoNodeSize);
499 std::swap(m_lazyString, rhs.m_lazyString);
500 std::swap(m_lazyLength, rhs.m_lazyLength);
501 std::swap(m_lazyStringModifiable, rhs.m_lazyStringModifiable);
508 CRYPTOPP_UNUSED(parameters);
510 m_node = m_queue.m_head;
513 m_lazyString = m_queue.m_lazyString;
514 m_lazyLength = m_queue.m_lazyLength;
532 return (
size_t)
CopyTo(sink, 1);
538 return (
size_t)
CopyTo(sink, peekMax);
546 lword bytesLeft = transferBytes;
547 size_t blockedBytes = 0;
551 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_node->CurrentSize()-m_offset);
552 blockedBytes = target.
ChannelPut2(channel, m_node->m_buf+m_node->m_head+m_offset, len, 0, blocking);
566 m_node = m_node->m_next;
570 if (bytesLeft && m_lazyLength)
572 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_lazyLength);
573 blockedBytes = target.
ChannelPut2(channel, m_lazyString, len, 0, blocking);
577 m_lazyString =
PtrAdd(m_lazyString, len);
583 transferBytes -= bytesLeft;
591 lword transferBytes = end-begin;
593 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
594 begin += transferBytes;
An invalid argument was detected.
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
void IsolatedInitialize(const NameValuePairs ¶meters)
Initialize or reinitialize this object, without signal propagation.
Utility functions for the Crypto++ library.
void IsolatedInitialize(const NameValuePairs ¶meters)
Initialize or reinitialize this object, without signal propagation.
size_t Get(byte &outByte)
Retrieve a 8-bit byte.
Copy input to a memory buffer.
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Classes for an unlimited queue to store bytes.
ByteQueue(size_t nodeSize=0)
Construct a ByteQueue.
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
size_t Peek(byte &outByte) const
Peek a 8-bit byte.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
size_t Peek(byte &outByte) const
Peek a 8-bit byte.
Data structure used to store byte strings.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Implementation of BufferedTransformation's attachment interface.
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
Debugging and diagnostic assertions.
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Crypto++ library namespace.
void swap(::SecBlock< T, A > &a, ::SecBlock< T, A > &b)
Swap two SecBlocks.
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
size_t Get(byte &outByte)
Retrieve a 8-bit byte.
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
size_type size() const
Provides the count of elements in the SecBlock.
Base class for bufferless filters.
#define SIZE_MAX
The maximum value of a machine word.
Interface for retrieving values given their names.