size_t ByteQueue::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking) { if (blocking) { lword bytesLeft = transferBytes; for (ByteQueueNode *current=m_head; bytesLeft && current; current=current->next) bytesLeft -= current->TransferTo(target, bytesLeft, channel); CleanupUsedNodes(); size_t len = (size_t)STDMIN(bytesLeft, (lword)m_lazyLength); if (len) { if (m_lazyStringModifiable) target.ChannelPutModifiable(channel, m_lazyString, len); else target.ChannelPut(channel, m_lazyString, len); m_lazyString += len; m_lazyLength -= len; bytesLeft -= len; } transferBytes -= bytesLeft; return 0; } else { Walker walker(*this); size_t blockedBytes = walker.TransferTo2(target, transferBytes, channel, blocking); Skip(transferBytes); return blockedBytes; } }
unsigned int ByteQueue::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking) { if (blocking) { unsigned long bytesLeft = transferBytes; for (ByteQueueNode *current=m_head; bytesLeft && current; current=current->next) bytesLeft -= current->TransferTo(target, bytesLeft, channel); CleanupUsedNodes(); unsigned int len = (unsigned int)STDMIN(bytesLeft, (unsigned long)m_lazyLength); if (len) { target.ChannelPut(channel, m_lazyString, len); m_lazyString += len; m_lazyLength -= len; bytesLeft -= len; } transferBytes -= bytesLeft; return 0; } else { Walker walker(*this); unsigned int blockedBytes = walker.TransferTo2(target, transferBytes, channel, blocking); Skip(transferBytes); return blockedBytes; } }
unsigned int RandomNumberStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking) { if (!blocking) throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object"); unsigned long transferMax = transferBytes; for (transferBytes = 0; transferBytes<transferMax && m_count < (unsigned long)m_length; ++transferBytes, ++m_count) target.ChannelPut(channel, m_rng->GenerateByte()); return 0; }
void RandomizedTransfer(BufferedTransformation &source, BufferedTransformation &target, bool finish, const std::string &channel=DEFAULT_CHANNEL) { while (source.MaxRetrievable() > (finish ? 0 : 4096)) { byte buf[4096+64]; size_t start = GlobalRNG().GenerateWord32(0, 63); size_t len = GlobalRNG().GenerateWord32(1, UnsignedMin(4096U, 3*source.MaxRetrievable()/2)); len = source.Get(buf+start, len); target.ChannelPut(channel, buf+start, len); } }
void OldRandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size) { while (size > 0) { if (getPos == pool.size()) Stir(); size_t t = UnsignedMin(pool.size() - getPos, size); target.ChannelPut(channel, pool+getPos, t); size -= t; getPos += t; }}
void RandomNumberGenerator::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length) { FixedSizeSecBlock<byte, 256> buffer; while (length) { size_t len = UnsignedMin(buffer.size(), length); GenerateBlock(buffer, len); target.ChannelPut(channel, buffer, len); length -= len; } }
unsigned int FileStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const { if (!m_stream) return 0; if (begin == 0 && end == 1) { int result = m_stream->peek(); if (result == EOF) // GCC workaround: 2.95.2 doesn't have char_traits<char>::eof() return 0; else { unsigned int blockedBytes = target.ChannelPut(channel, byte(result), blocking); begin += 1-blockedBytes; return blockedBytes; } } // TODO: figure out what happens on cin streampos current = m_stream->tellg(); streampos endPosition = m_stream->seekg(0, ios::end).tellg(); streampos newPosition = current + (streamoff)begin; if (newPosition >= endPosition) { m_stream->seekg(current); return 0; // don't try to seek beyond the end of file } m_stream->seekg(newPosition); unsigned long total = 0; try { assert(!m_waiting); unsigned long copyMax = end-begin; unsigned int blockedBytes = const_cast<FileStore *>(this)->TransferTo2(target, copyMax, channel, blocking); begin += copyMax; if (blockedBytes) { const_cast<FileStore *>(this)->m_waiting = false; return blockedBytes; } } catch(...) { m_stream->clear(); m_stream->seekg(current); throw; } m_stream->clear(); m_stream->seekg(current); return 0; }
size_t FileStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const { if (!m_stream) return 0; if (begin == 0 && end == 1) { int result = m_stream->peek(); if (result == std::char_traits<char>::eof()) return 0; else { size_t blockedBytes = target.ChannelPut(channel, byte(result), blocking); begin += 1-blockedBytes; return blockedBytes; } } // TODO: figure out what happens on cin std::streampos current = m_stream->tellg(); std::streampos endPosition = m_stream->seekg(0, std::ios::end).tellg(); std::streampos newPosition = current + static_cast<std::streamoff>(begin); if (newPosition >= endPosition) { m_stream->seekg(current); return 0; // don't try to seek beyond the end of file } m_stream->seekg(newPosition); try { assert(!m_waiting); lword copyMax = end-begin; size_t blockedBytes = const_cast<FileStore *>(this)->TransferTo2(target, copyMax, channel, blocking); begin += copyMax; if (blockedBytes) { const_cast<FileStore *>(this)->m_waiting = false; return blockedBytes; } } catch(...) { m_stream->clear(); m_stream->seekg(current); throw; } m_stream->clear(); m_stream->seekg(current); return 0; }
unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const { if( !m_file.IsGood() ) return 0; if (begin == 0 && end == 1) { int current = m_file.Tell(); byte result; m_file.Read( &result, 1 ); m_file.Seek( current ); if( m_file.AtEOF() ) return 0; unsigned int blockedBytes = target.ChannelPut(channel, byte(result), blocking); begin += 1-blockedBytes; return blockedBytes; } // TODO: figure out what happens on cin // (What does that mean?) int current = m_file.Tell(); int newPosition = current + (streamoff)begin; if( newPosition >= m_file.GetFileSize() ) return 0; // don't try to seek beyond the end of file m_file.Seek(newPosition); try { assert(!m_waiting); unsigned long copyMax = end-begin; unsigned int blockedBytes = const_cast<RageFileStore *>(this)->TransferTo2(target, copyMax, channel, blocking); begin += copyMax; if (blockedBytes) { const_cast<RageFileStore *>(this)->m_waiting = false; return blockedBytes; } } catch(...) { m_file.ClearError(); m_file.Seek(current); throw; } m_file.ClearError(); m_file.Seek(current); return 0; }
unsigned int RandomPool::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking) { if (!blocking) throw NotImplemented("RandomPool: nonblocking transfer is not implemented by this object"); unsigned int t; unsigned long size = transferBytes; while (size > (t = pool.size() - getPos)) { target.ChannelPut(channel, pool+getPos, t); size -= t; Stir(); } if (size) { target.ChannelPut(channel, pool+getPos, size); getPos += size; } return 0; }
void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size) { if (size > 0) { if (!m_keySet) m_pCipher->SetKey(m_key, 32); CRYPTOPP_COMPILE_ASSERT(sizeof(TimerWord) <= 16); CRYPTOPP_COMPILE_ASSERT(sizeof(time_t) <= 8); Timer timer; TimerWord tw = timer.GetCurrentTimerValue(); *(TimerWord *)(void*)m_seed.data() += tw; time_t t = time(NULLPTR); // UBsan finding: signed integer overflow: 1876017710 + 1446085457 cannot be represented in type 'long int' // *(time_t *)(m_seed.data()+8) += t; word64 tt1 = 0, tt2 = (word64)t; ::memcpy(&tt1, m_seed.data()+8, 8); ::memcpy(m_seed.data()+8, &(tt2 += tt1), 8); // Wipe the intermediates *((volatile TimerWord*)&tw) = 0; *((volatile word64*)&tt1) = 0; *((volatile word64*)&tt2) = 0; do { m_pCipher->ProcessBlock(m_seed); size_t len = UnsignedMin(16, size); target.ChannelPut(channel, m_seed, len); size -= len; } while (size > 0); } }
inline size_t CopyTo(BufferedTransformation &target, size_t copyMax, const std::string &channel=DEFAULT_CHANNEL) const { size_t len = STDMIN(copyMax, m_tail-m_head); target.ChannelPut(channel, buf+m_head, len); return len; }
inline unsigned int CopyTo(BufferedTransformation &target, unsigned int copyMax, const std::string &channel=BufferedTransformation::NULL_CHANNEL) const { unsigned int len = STDMIN(copyMax, m_tail-m_head); target.ChannelPut(channel, buf+m_head, len); return len; }
inline size_t CopyTo(BufferedTransformation &target, const std::string &channel=BufferedTransformation::NULL_CHANNEL) const { size_t len = m_tail-m_head; target.ChannelPut(channel, buf+m_head, len); return len; }