Skip to content

Commit

Permalink
Added etl::string interface to etl::base64
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Oct 5, 2023
1 parent 6b7093d commit 5437b4b
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 52 deletions.
105 changes: 75 additions & 30 deletions include/etl/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ SOFTWARE.

#include "etl/platform.h"
#include "etl/span.h"
#include "etl/static_assert.h"
#include "etl/error_handler.h"
#include "etl/exception.h"
#include "etl/type_traits.h"
#include "etl/binary.h"
#include "etl/algorithm.h"
#include "etl/integral_limits.h"
#include "etl/iterator.h"
#include "etl/string.h"

#include <stdint.h>

#if ETL_USING_STL
#include <string>
#endif

namespace etl
{
//***************************************************************************
Expand Down Expand Up @@ -90,17 +96,9 @@ namespace etl
}

// Figure out if the output buffer is large enough.
size_t required_output_length = (input_length * 8U) / 6U;
size_t required_output_length = encode_size(input_length);

if ((input_length % 3U) != 0U)
{
while ((required_output_length % 4U) != 0)
{
++required_output_length;
}
}

ETL_ASSERT_OR_RETURN_VALUE(output_length >= required_output_length, ETL_ERROR(base64_overflow), etl::span<const T>());
ETL_ASSERT_OR_RETURN_VALUE(output_length >= required_output_length, ETL_ERROR(base64_overflow), 0U);

const T* p_in = input;
const T* p_in_end = input + input_length;
Expand Down Expand Up @@ -187,6 +185,19 @@ namespace etl
return static_cast<size_t>(etl::distance(output, p_out));
}

//*************************************************************************
/// Encode to Base64 from and to pointer/pointer
//*************************************************************************
template <typename T>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
encode(const T* input_begin, const T* input_end, char* output_begin, char* output_end)
{
return encode(input_begin, static_cast<size_t>(etl::distance(input_begin, input_end)),
output_begin, static_cast<size_t>(etl::distance(output_begin, output_end)));
}

//*************************************************************************
/// Encode to Base64 from and to span/span
//*************************************************************************
Expand All @@ -202,16 +213,51 @@ namespace etl
}

//*************************************************************************
/// Encode to Base64 from and to pointer/pointer
/// Encode to Base64 from pointer/length to etl::istring
//*************************************************************************
template <typename T>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
encode(const T* input_begin, const T* input_end, char* output_begin, char* output_end)
encode(const T* input_begin, size_t input_length,
etl::istring& output)
{
return encode(input_begin, static_cast<size_t>(etl::distance(input_begin, input_end)),
output_begin, static_cast<size_t>(etl::distance(output_begin, output_end)));
output.resize(etl::base64::encode_size(input_length));

return encode(input_begin, input_length,
output.data(), output.size());
}

//*************************************************************************
/// Encode to Base64 from pointer/pointer to etl::istring
//*************************************************************************
template <typename T>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
encode(const T* input_begin, const T* input_end,
etl::istring& output)
{
output.resize(etl::base64::encode_size(etl::distance(input_begin, input_end)));

return encode(input_begin, static_cast<size_t>(etl::distance(input_begin, input_end)),
output.data(), output.size());
}

//*************************************************************************
/// Encode to Base64 from span to etl::istring
//*************************************************************************
template <typename T, size_t Length1>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
encode(const etl::span<const T, Length1>& input_span,
etl::istring& output)
{
output.resize(etl::base64::encode_size(Length1));

return encode(input_span.begin(), input_span.size(),
output.data(), output.size());
}

//*************************************************************************
Expand Down Expand Up @@ -250,10 +296,9 @@ namespace etl
}

// Figure out if the output buffer is large enough.
size_t length = static_cast<size_t>(etl::distance(input, etl::find(input, input + input_length, padding())) - 1);
size_t required_output_length = length - (length / 4U);
size_t required_output_length = etl::base64::decode_size(input, input_length);

ETL_ASSERT_OR_RETURN_VALUE(output_length >= required_output_length, ETL_ERROR(base64_overflow), etl::span<const T>());
ETL_ASSERT_OR_RETURN_VALUE(output_length >= required_output_length, ETL_ERROR(base64_overflow), 0U);

const char* p_in = input;
const char* p_in_end = input + input_length;
Expand Down Expand Up @@ -328,30 +373,30 @@ namespace etl
}

//*************************************************************************
/// Decode from Base64 from and to span/span
/// Decode from Base64 from and to pointer/pointer
//*************************************************************************
template <typename T, size_t Length1, size_t Length2>
template <typename T>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
decode(const etl::span<const char, Length1>& input_span,
const etl::span<T, Length2>& output_span)
decode(const char* input_begin, const char* input_end, T* output_begin, T* output_end)
{
return decode(input_span.begin(), input_span.size(),
output_span.begin(), output_span.size());
return decode(input_begin, static_cast<size_t>(etl::distance(input_begin, input_end)),
output_begin, static_cast<size_t>(etl::distance(output_begin, output_end)));
}

//*************************************************************************
/// Decode from Base64 from and to pointer/pointer
/// Decode from Base64 from and to span/span
//*************************************************************************
template <typename T>
template <typename T, size_t Length1, size_t Length2>
ETL_CONSTEXPR14
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
decode(const char* input_begin, const char* input_end, T* output_begin, T* output_end)
static
typename etl::enable_if<etl::is_integral<T>::value && (etl::integral_limits<T>::bits == 8U), size_t>::type
decode(const etl::span<const char, Length1>& input_span,
const etl::span<T, Length2>& output_span)
{
return decode(input_begin, static_cast<size_t>(etl::distance(input_begin, input_end)),
output_begin, static_cast<size_t>(etl::distance(output_begin, output_end)));
return decode(input_span.begin(), input_span.size(),
output_span.begin(), output_span.size());
}

//*************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions include/etl/basic_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace etl
/// Gets the current size of the string.
///\return The current size of the string.
//*************************************************************************
size_type size() const
ETL_CONSTEXPR size_type size() const
{
return current_size;
}
Expand Down Expand Up @@ -605,7 +605,7 @@ namespace etl
/// Returns a const pointer to the beginning of the string data.
///\return A const pointer to the beginning of the string data.
//*********************************************************************
const_pointer data() const
ETL_CONSTEXPR const_pointer data() const
{
return p_buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions include/etl/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace etl
/// Returns a const pointer to the beginning of the vector data.
///\return A const pointer to the beginning of the vector data.
//*********************************************************************
const_pointer data() const
ETL_CONSTEXPR const_pointer data() const
{
return p_buffer;
}
Expand Down Expand Up @@ -974,7 +974,7 @@ namespace etl
/// Gets the current size of the vector.
///\return The current size of the vector.
//*************************************************************************
size_type size() const
ETL_CONSTEXPR size_type size() const
{
return size_t(p_end - p_buffer);
}
Expand Down
Loading

0 comments on commit 5437b4b

Please sign in to comment.