27 validate_construction(
data.data(),
data.size());
31 validate_construction(
data.data(),
data.size());
48 data_.assign(
reinterpret_cast<const uint8_t*
>(
data),
reinterpret_cast<const uint8_t*
>(
data) +
size);
53 validate_construction(span.
data(), span.
size());
54 if (span.
data() && span.
size() > 0) {
55 data_.assign(span.
begin(), span.
end());
72 validate_index(index);
77 validate_index(index);
78 return data_.at(index);
100 if (data_.size() > 1024 * 1024 * 100) {
101 throw std::runtime_error(
"Buffer size exceeds maximum allowed size");
106 void SafeDataBuffer::validate_index(
size_t index)
const {
107 if (index >= data_.size()) {
108 throw std::out_of_range(
"Index " +
std::to_string(index) +
" is out of range for buffer of size " +
113 void SafeDataBuffer::validate_construction(
const void* ptr,
size_t size)
const {
114 if (
size > 0 && !ptr) {
115 throw std::invalid_argument(
"Cannot construct buffer from null pointer with non-zero size");
117 if (
size > 1024 * 1024 * 100) {
118 throw std::invalid_argument(
"Buffer size exceeds maximum allowed size");
123 namespace safe_buffer_factory {
Safe data buffer for type-safe data transfer.
const uint8_t & at(size_t index) const
void resize(size_t new_size)
void reserve(size_t capacity)
ConstByteSpan as_span() const noexcept
bool operator!=(const SafeDataBuffer &other) const noexcept
SafeDataBuffer(const std::string &data)
std::string as_string() const
const uint8_t & operator[](size_t index) const
bool is_valid() const noexcept
const uint8_t * data() const noexcept
size_t size() const noexcept
bool empty() const noexcept
bool operator==(const SafeDataBuffer &other) const noexcept
A C++17 compatible span-like class for safe array access.
constexpr size_type size() const noexcept
constexpr pointer data() const noexcept
constexpr iterator begin() const noexcept
constexpr iterator end() const noexcept
SafeDataBuffer from_vector(const std::vector< uint8_t > &vec)
SafeDataBuffer from_span(ConstByteSpan span)
SafeDataBuffer from_string(const std::string &str)
SafeDataBuffer from_c_string(const char *str)
SafeDataBuffer from_raw_data(const uint8_t *data, size_t size)
SafeSpan< const uint8_t > ConstByteSpan
std::string to_string(ErrorCode code)
Convert ErrorCode to human-readable string.