5 #ifndef CRYPTOPP_IMPORTS 12 static const unsigned int s_maxAutoNodeSize = 16*1024;
25 inline size_t MaxSize()
const {
return buf.
size();}
27 inline size_t CurrentSize()
const 32 inline bool UsedUp()
const 34 return (m_head==MaxSize());
42 inline size_t Put(
const byte *begin,
size_t length)
45 if (!begin || !length)
return length;
46 size_t l =
STDMIN(length, MaxSize()-m_tail);
47 if (buf+m_tail != begin)
48 memcpy(buf+m_tail, begin, l);
53 inline size_t Peek(byte &outByte)
const 62 inline size_t Peek(byte *target,
size_t copyMax)
const 64 size_t len =
STDMIN(copyMax, m_tail-m_head);
65 memcpy(target, buf+m_head, len);
71 size_t len = m_tail-m_head;
78 size_t len =
STDMIN(copyMax, m_tail-m_head);
83 inline size_t Get(byte &outByte)
85 size_t len = Peek(outByte);
90 inline size_t Get(byte *outString,
size_t getMax)
92 size_t len = Peek(outString, getMax);
99 size_t len = m_tail-m_head;
107 size_t len =
UnsignedMin(m_tail-m_head, transferMax);
113 inline size_t Skip(
size_t skipMax)
115 size_t len =
STDMIN(skipMax, m_tail-m_head);
120 inline byte operator[](
size_t i)
const 122 return buf[m_head+i];
128 size_t m_head, m_tail;
133 ByteQueue::ByteQueue(
size_t nodeSize)
135 , m_head(NULL), m_tail(NULL), m_lazyString(NULL), m_lazyLength(0), m_lazyStringModifiable(
false)
137 SetNodeSize(nodeSize);
141 void ByteQueue::SetNodeSize(
size_t nodeSize)
143 m_autoNodeSize = !nodeSize;
144 m_nodeSize = m_autoNodeSize ? 256 : nodeSize;
147 ByteQueue::ByteQueue(
const ByteQueue ©)
153 void ByteQueue::CopyFrom(
const ByteQueue ©)
156 m_autoNodeSize = copy.m_autoNodeSize;
157 m_nodeSize = copy.m_nodeSize;
160 for (
ByteQueueNode *current=copy.m_head->next; current; current=current->next)
163 m_tail = m_tail->next;
168 Put(copy.m_lazyString, copy.m_lazyLength);
171 ByteQueue::~ByteQueue()
176 void ByteQueue::Destroy()
178 for (
ByteQueueNode *next, *current=m_head; current; current=next)
191 lword ByteQueue::CurrentSize()
const 195 for (
ByteQueueNode *current=m_head; current; current=current->next)
196 size += current->CurrentSize();
198 return size + m_lazyLength;
201 bool ByteQueue::IsEmpty()
const 203 return m_head==m_tail && m_head->CurrentSize()==0 && m_lazyLength==0;
206 void ByteQueue::Clear()
208 for (
ByteQueueNode *next, *current=m_head->next; current; current=next)
220 size_t ByteQueue::Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking)
222 CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
224 if (m_lazyLength > 0)
228 while ((len=m_tail->Put(inString, length)) < length)
232 if (m_autoNodeSize && m_nodeSize < s_maxAutoNodeSize)
237 while (m_nodeSize < length && m_nodeSize < s_maxAutoNodeSize);
239 m_tail = m_tail->next;
245 void ByteQueue::CleanupUsedNodes()
248 while (m_head && m_head != m_tail && m_head->UsedUp())
256 if (m_head && m_head->CurrentSize() == 0)
260 void ByteQueue::LazyPut(
const byte *inString,
size_t size)
262 if (m_lazyLength > 0)
265 if (inString == m_tail->buf+m_tail->m_tail)
269 m_lazyString =
const_cast<byte *
>(inString);
271 m_lazyStringModifiable =
false;
275 void ByteQueue::LazyPutModifiable(byte *inString,
size_t size)
277 if (m_lazyLength > 0)
279 m_lazyString = inString;
281 m_lazyStringModifiable =
true;
284 void ByteQueue::UndoLazyPut(
size_t size)
286 if (m_lazyLength < size)
287 throw InvalidArgument(
"ByteQueue: size specified for UndoLazyPut is too large");
289 m_lazyLength -= size;
292 void ByteQueue::FinalizeLazyPut()
294 size_t len = m_lazyLength;
297 Put(m_lazyString, len);
302 if (m_head->Get(outByte))
304 if (m_head->UsedUp())
308 else if (m_lazyLength > 0)
310 outByte = *m_lazyString++;
321 return (
size_t)TransferTo(sink, getMax);
326 if (m_head->Peek(outByte))
328 else if (m_lazyLength > 0)
330 outByte = *m_lazyString;
340 return (
size_t)CopyTo(sink, peekMax);
347 lword bytesLeft = transferBytes;
348 for (
ByteQueueNode *current=m_head; bytesLeft && current; current=current->next)
349 bytesLeft -= current->TransferTo(target, bytesLeft, channel);
352 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_lazyLength);
355 if (m_lazyStringModifiable)
358 target.
ChannelPut(channel, m_lazyString, len);
363 transferBytes -= bytesLeft;
369 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
379 lword transferBytes = end-begin;
380 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
381 begin += transferBytes;
385 void ByteQueue::Unget(byte inByte)
390 void ByteQueue::Unget(
const byte *inString,
size_t length)
392 size_t len =
STDMIN(length, m_head->m_head);
394 m_head->m_head -= len;
395 memcpy(m_head->buf + m_head->m_head, inString + length, len);
400 newHead->next = m_head;
402 m_head->Put(inString, length);
406 const byte * ByteQueue::Spy(
size_t &contiguousSize)
const 408 contiguousSize = m_head->m_tail - m_head->m_head;
409 if (contiguousSize == 0 && m_lazyLength > 0)
411 contiguousSize = m_lazyLength;
415 return m_head->buf + m_head->m_head;
420 if (m_lazyLength > 0)
423 if (m_tail->m_tail == m_tail->MaxSize())
426 m_tail = m_tail->next;
429 size = m_tail->MaxSize() - m_tail->m_tail;
430 return m_tail->buf + m_tail->m_tail;
440 bool ByteQueue::operator==(
const ByteQueue &rhs)
const 442 const lword currentSize = CurrentSize();
444 if (currentSize != rhs.CurrentSize())
447 Walker walker1(*
this), walker2(rhs);
450 while (walker1.Get(b1) && walker2.
Get(b2))
457 byte ByteQueue::operator[](lword i)
const 459 for (
ByteQueueNode *current=m_head; current; current=current->next)
461 if (i < current->CurrentSize())
462 return (*current)[(size_t)i];
464 i -= current->CurrentSize();
467 assert(i < m_lazyLength);
468 return m_lazyString[i];
473 std::swap(m_autoNodeSize, rhs.m_autoNodeSize);
474 std::swap(m_nodeSize, rhs.m_nodeSize);
475 std::swap(m_head, rhs.m_head);
476 std::swap(m_tail, rhs.m_tail);
477 std::swap(m_lazyString, rhs.m_lazyString);
478 std::swap(m_lazyLength, rhs.m_lazyLength);
479 std::swap(m_lazyStringModifiable, rhs.m_lazyStringModifiable);
486 CRYPTOPP_UNUSED(parameters);
488 m_node = m_queue.m_head;
491 m_lazyString = m_queue.m_lazyString;
492 m_lazyLength = m_queue.m_lazyLength;
498 return (
size_t)TransferTo(sink, 1);
504 return (
size_t)TransferTo(sink, getMax);
510 return (
size_t)CopyTo(sink, 1);
516 return (
size_t)CopyTo(sink, peekMax);
521 lword bytesLeft = transferBytes;
522 size_t blockedBytes = 0;
526 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_node->CurrentSize()-m_offset);
527 blockedBytes = target.
ChannelPut2(channel, m_node->buf+m_node->m_head+m_offset, len, 0, blocking);
541 m_node = m_node->next;
545 if (bytesLeft && m_lazyLength)
547 size_t len = (size_t)
STDMIN(bytesLeft, (lword)m_lazyLength);
548 blockedBytes = target.
ChannelPut2(channel, m_lazyString, len, 0, blocking);
558 transferBytes -= bytesLeft;
566 lword transferBytes = end-begin;
567 size_t blockedBytes = walker.
TransferTo2(target, transferBytes, channel, blocking);
568 begin += transferBytes;
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
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.
void IsolatedInitialize(const NameValuePairs ¶meters)
Initialize or reinitialize this object, without signal propagation.
void IsolatedInitialize(const NameValuePairs ¶meters)
Initialize or reinitialize this object, without signal propagation.
SecByteBlock is a SecBlock<byte> typedef.
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.
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.
size_t Peek(byte &outByte) const
Peek a 8-bit byte.
Implementation of BufferedTransformation's attachment interface in cryptlib.h.
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.
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.
Interface for retrieving values given their names.