9#include <botan/data_src.h>
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/mem_utils.h>
18#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
36 if(this->
read(&b, 1) == 1) {
47 return peek(&out, 1, 0);
54 uint8_t buf[64] = {0};
58 const size_t got = this->
read(buf, std::min(n,
sizeof(buf)));
74 const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
75 copy_mem(out, m_source.data() + m_offset, got);
81 return (n <= (m_source.size() - m_offset));
88 const size_t bytes_left = m_source.size() - m_offset;
89 if(peek_offset >= bytes_left) {
93 const size_t got = std::min(bytes_left - peek_offset, length);
94 copy_mem(out, &m_source[m_offset + peek_offset], got);
102 return (m_offset == m_source.size());
119 const size_t got =
static_cast<size_t>(m_source.gcount());
125 const std::streampos orig_pos = m_source.tellg();
126 m_source.seekg(0, std::ios::end);
127 const size_t avail =
static_cast<size_t>(m_source.tellg() - orig_pos);
128 m_source.seekg(orig_pos);
137 throw Invalid_State(
"DataSource_Stream: Cannot peek when out of data");
148 got =
static_cast<size_t>(m_source.gcount());
156 got =
static_cast<size_t>(m_source.gcount());
162 m_source.seekg(m_total_read, std::ios::beg);
171 return (!m_source.good());
181#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
188 m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
189 m_source(*m_source_memory),
191 if(!m_source.good()) {
192 throw Stream_IO_Error(fmt(
"DataSource: Failure opening file '{}'", path));
202 m_identifier(name), m_source(in), m_total_read(0) {}
DataSource_Memory(std::string_view in)
size_t read(uint8_t buf[], size_t length) override
bool check_available(size_t n) override
size_t peek(uint8_t buf[], size_t length, size_t offset) const override
bool end_of_data() const override
~DataSource_Stream() override
bool end_of_data() const override
BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::istream &in, std::string_view id="<std::istream>")
size_t read(uint8_t buf[], size_t length) override
size_t peek(uint8_t buf[], size_t length, size_t offset) const override
bool check_available(size_t n) override
std::string id() const override
size_t peek_byte(uint8_t &out) const
std::optional< uint8_t > read_byte()
size_t discard_next(size_t N)
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0
virtual size_t read(uint8_t out[], size_t length)=0
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
constexpr void copy_mem(T *out, const T *in, size_t n)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
std::vector< T, secure_allocator< T > > secure_vector