/* * (C) 2023-2024 René Meusel - Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ #ifndef BOTAN_BUFFER_SLICER_H_ #define BOTAN_BUFFER_SLICER_H_ #include #include #include #include #include #include #include namespace Botan { /** * Helper class to ease unmarshalling of concatenated fixed-length values */ class BufferSlicer final { public: explicit BufferSlicer(std::span buffer) : m_remaining(buffer) {} template auto copy(const size_t count) { const auto result = take(count); return ContainerT(result.begin(), result.end()); } auto copy_as_vector(const size_t count) { return copy>(count); } auto copy_as_secure_vector(const size_t count) { return copy>(count); } std::span take(const size_t count) { BOTAN_STATE_CHECK(remaining() >= count); auto result = m_remaining.first(count); m_remaining = m_remaining.subspan(count); return result; } template std::span take() { BOTAN_STATE_CHECK(remaining() >= count); auto result = m_remaining.first(); m_remaining = m_remaining.subspan(count); return result; } template StrongSpan take(const size_t count) { return StrongSpan(take(count)); } uint8_t take_byte() { return take(1)[0]; } void copy_into(std::span sink) { const auto data = take(sink.size()); std::copy(data.begin(), data.end(), sink.begin()); } void skip(const size_t count) { take(count); } size_t remaining() const { return m_remaining.size(); } bool empty() const { return m_remaining.empty(); } private: std::span m_remaining; }; } // namespace Botan #endif