8#ifndef BOTAN_TLS_READER_H_
9#define BOTAN_TLS_READER_H_
11#include <botan/exceptn.h>
12#include <botan/secmem.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/loadstor.h>
27 m_typename(type), m_buf(buf_in), m_offset(0) {}
31 throw_decode_error(
"Extra bytes at end of message");
41 std::vector<uint8_t>
get_remaining() {
return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end()); }
44 return std::vector<uint8_t>(m_buf.begin(), m_buf.begin() + m_offset);
48 assert_at_least(bytes);
54 uint32_t result =
make_uint32(m_buf[m_offset], m_buf[m_offset + 1], m_buf[m_offset + 2], m_buf[m_offset + 3]);
61 uint32_t result =
make_uint32(0, m_buf[m_offset], m_buf[m_offset + 1], m_buf[m_offset + 2]);
68 uint16_t result =
make_uint16(m_buf[m_offset], m_buf[m_offset + 1]);
75 return make_uint16(m_buf[m_offset], m_buf[m_offset + 1]);
80 uint8_t result = m_buf[m_offset];
85 template <
typename T,
typename Container>
87 assert_at_least(num_elems *
sizeof(
T));
89 Container result(num_elems);
91 for(
size_t i = 0; i != num_elems; ++i) {
95 m_offset += num_elems *
sizeof(
T);
104 template <
typename T>
105 std::vector<T>
get_range(
size_t len_bytes,
size_t min_elems,
size_t max_elems) {
106 const size_t num_elems = get_num_elems(len_bytes,
sizeof(
T), min_elems, max_elems);
111 template <
typename T>
113 const size_t num_elems = get_num_elems(len_bytes,
sizeof(
T), min_elems, max_elems);
118 std::string
get_string(
size_t len_bytes,
size_t min_bytes,
size_t max_bytes) {
124 template <
typename T>
130 size_t get_length_field(
size_t len_bytes) {
131 assert_at_least(len_bytes);
135 }
else if(len_bytes == 2) {
137 }
else if(len_bytes == 3) {
141 throw_decode_error(
"Bad length size");
144 size_t get_num_elems(
size_t len_bytes,
size_t T_size,
size_t min_elems,
size_t max_elems) {
145 const size_t byte_length = get_length_field(len_bytes);
147 if(byte_length % T_size != 0) {
148 throw_decode_error(
"Size isn't multiple of T");
151 const size_t num_elems = byte_length / T_size;
153 if(num_elems < min_elems || num_elems > max_elems) {
154 throw_decode_error(
"Length field outside parameters");
160 void assert_at_least(
size_t n)
const {
161 if(m_buf.size() - m_offset < n) {
162 throw_decode_error(
"Expected " + std::to_string(n) +
" bytes remaining, only " +
163 std::to_string(m_buf.size() - m_offset) +
" left");
167 [[noreturn]]
void throw_decode_error(std::string_view why)
const {
168 throw Decoding_Error(
fmt(
"Invalid {}: {}", m_typename, why));
171 const char* m_typename;
172 std::span<const uint8_t> m_buf;
179template <
typename T,
typename Alloc>
181 const size_t T_size =
sizeof(
T);
182 const size_t val_bytes = T_size * vals_size;
184 if(tag_size != 1 && tag_size != 2 && tag_size != 3) {
188 if((tag_size == 1 && val_bytes > 255) || (tag_size == 2 && val_bytes > 65535) ||
189 (tag_size == 3 && val_bytes > 16777215)) {
193 for(
size_t i = 0; i != tag_size; ++i) {
194 buf.push_back(
get_byte_var(
sizeof(val_bytes) - tag_size + i, val_bytes));
197 for(
size_t i = 0; i != vals_size; ++i) {
198 for(
size_t j = 0; j != T_size; ++j) {
204template <
typename T,
typename Alloc,
typename Alloc2>
209template <
typename Alloc>
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
bool has_remaining() const
std::vector< uint8_t > get_remaining()
size_t read_so_far() const
void discard_next(size_t bytes)
TLS_Data_Reader(const char *type, std::span< const uint8_t > buf_in)
Container get_elem(size_t num_elems)
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
size_t remaining_bytes() const
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
std::vector< T > get_fixed(size_t size)
std::vector< uint8_t > get_data_read_so_far()
uint16_t peek_uint16_t() const
std::vector< T > get_range_vector(size_t len_bytes, size_t min_elems, size_t max_elems)
int(* final)(unsigned char *, CTX *)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
std::string fmt(std::string_view format, const T &... args)
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
constexpr uint8_t get_byte_var(size_t byte_num, T input)
constexpr auto load_be(ParamTs &&... params)
const uint8_t * cast_char_ptr_to_uint8(const char *s)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)