Skip to content

Commit

Permalink
Refactored base64 encoder and decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Jun 26, 2024
1 parent f22ccc5 commit ce24b0c
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 297 deletions.
148 changes: 47 additions & 101 deletions include/etl/base64_decoder.h

Large diffs are not rendered by default.

98 changes: 22 additions & 76 deletions include/etl/base64_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,11 @@ namespace etl
//*************************************************************************
/// Base64 RFC-2152 Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc2152_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_2152;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

Expand All @@ -524,7 +521,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -538,7 +535,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -559,23 +556,14 @@ namespace etl
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc2152_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc2152_encoder<Buffer_Size_>::Buffer_Size;

//*************************************************************************
/// Base64 RFC-3501 Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc3501_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_3501;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

Expand All @@ -589,7 +577,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -603,7 +591,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -624,25 +612,16 @@ namespace etl
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc3501_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc3501_encoder<Buffer_Size_>::Buffer_Size;

//*************************************************************************
/// Base64 RFC-4648 Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc4648_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_4648;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

//*************************************************************************
/// Base64 RFC-4648 constructor.
Expand All @@ -654,7 +633,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -668,7 +647,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -689,23 +668,14 @@ namespace etl
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc4648_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc4648_encoder<Buffer_Size_>::Buffer_Size;

//*************************************************************************
/// Base64 RFC-4648-Padding Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
/// Base64 RFC-4648-Padding Encoder
//*************************************************************************
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc4648_padding_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_4648_PADDING;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

Expand All @@ -719,7 +689,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -733,7 +703,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -754,23 +724,14 @@ namespace etl
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc4648_padding_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc4648_padding_encoder<Buffer_Size_>::Buffer_Size;

//*************************************************************************
/// Base64 RFC-4648-URL Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc4648_url_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_4648_URL;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

Expand All @@ -784,7 +745,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -798,7 +759,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -819,23 +780,14 @@ namespace etl
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc4648_url_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc4648_url_encoder<Buffer_Size_>::Buffer_Size;

//*************************************************************************
/// Base64 RFC-4648-URL_Padding Encoder
//*************************************************************************
template <size_t Buffer_Size_ = etl::base64::Min_Encode_Buffer_Size>
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
class base64_rfc4648_url_padding_encoder : public ibase64_encoder
{
public:

static ETL_CONSTANT etl::base64::Encoding Encoding = etl::base64::Encoding::RFC_4648_URL;
static ETL_CONSTANT size_t Buffer_Size = Buffer_Size_;

ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");

Expand All @@ -849,7 +801,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_type())
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -863,7 +815,7 @@ namespace etl
output_buffer,
Buffer_Size,
callback_)
, output_buffer{}
, output_buffer()
{
}

Expand All @@ -883,12 +835,6 @@ namespace etl
/// The internal output buffer.
char output_buffer[Buffer_Size];
};

template <size_t Buffer_Size_>
ETL_CONSTANT etl::base64::Encoding base64_rfc4648_url_padding_encoder<Buffer_Size_>::Encoding;

template <size_t Buffer_Size_>
ETL_CONSTANT size_t base64_rfc4648_url_padding_encoder<Buffer_Size_>::Buffer_Size;
}

#undef ETL_IS_TYPE_8_BIT_INTEGRAL
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC2152_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N+27j/cebMXjnZljpYhuYjRbdDd/9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva/sgO5xJ+06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj+ycBQpH40jLsHqzvl+bN8co4QrJDWnY8gLH4u6Ub/pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7/yzVh7RQtMMGLPurxp3qFI8ns3eITQ+H7VU1/u0vQ"
};

SUITE(test_base64_rfc2152_with_no_padding)
SUITE(test_base64_rfc2152_decoder_with_no_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_2152, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_2152", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC2152_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N+27j/cebMXjnZljpYhuYjRbdDd/9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva/sgO5xJ+06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj+ycBQpH40jLsHqzvl+bN8co4QrJDWnY8gLH4u6Ub/pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7/yzVh7RQtMMGLPurxp3qFI8ns3eITQ+H7VU1/u0vQ"
};

SUITE(test_base64_rfc2152_with_no_padding)
SUITE(test_base64_rfc2152_encoder_with_no_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_2152, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_2152", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC3501_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N+27j,cebMXjnZljpYhuYjRbdDd,9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva,sgO5xJ+06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj+ycBQpH40jLsHqzvl+bN8co4QrJDWnY8gLH4u6Ub,pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7,yzVh7RQtMMGLPurxp3qFI8ns3eITQ+H7VU1,u0vQ"
};

SUITE(test_base64_rfc3501_with_no_padding)
SUITE(test_base64_rfc3501_decoder_with_no_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_3501, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_3501", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC3501_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N+27j,cebMXjnZljpYhuYjRbdDd,9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva,sgO5xJ+06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj+ycBQpH40jLsHqzvl+bN8co4QrJDWnY8gLH4u6Ub,pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7,yzVh7RQtMMGLPurxp3qFI8ns3eITQ+H7VU1,u0vQ"
};

SUITE(test_base64_rfc3501_with_no_padding)
SUITE(test_base64_rfc3501_encoder_with_no_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_3501, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_3501", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC4648_URL_decoder_with_no_padding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N-27j_cebMXjnZljpYhuYjRbdDd_9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva_sgO5xJ-06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj-ycBQpH40jLsHqzvl-bN8co4QrJDWnY8gLH4u6Ub_pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7_yzVh7RQtMMGLPurxp3qFI8ns3eITQ-H7VU1_u0vQ=="
};

SUITE(test_base64_rfc4648_url_with_no_padding)
SUITE(test_base64_rfc4648_url_decoder_with_no_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_4648_URL, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_4648_URL", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
11 changes: 1 addition & 10 deletions test/test_base64_RFC4648_URL_decoder_with_padding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,8 @@ namespace
"OycDQy37KCphrrxJcTIBFWlcXvXVm96lV8nBfYDeTIHAzyrRhlbVcTfrgDLf5N-27j_cebMXjnZljpYhuYjRbdDd_9qoek31cXf9LvLkQHKMgwBvE3JT5GtwDjfKJc1oYsCrFMdZg9KCjJNtEyHACPltrIR4SYRva_sgO5xJ-06AaYIlhpXVTZHt0ncqJECK302ALc3VWiamcRVCDj-ycBQpH40jLsHqzvl-bN8co4QrJDWnY8gLH4u6Ub_pUYDSI7XRtFmufTAdABzYcGwWccdWCP6BrvvgktjbuVd8mctC7_yzVh7RQtMMGLPurxp3qFI8ns3eITQ-H7VU1_u0vQ=="
};

SUITE(test_base64_rfc4648_url_with_padding)
SUITE(test_base64_rfc4648_url_decoder_with_padding)
{
//*************************************************************************
TEST(test_basic_information)
{
codec_full_buffer b64;

CHECK_EQUAL(etl::base64::Encoding::RFC_4648_URL_PADDING, codec_full_buffer::Encoding);
CHECK_EQUAL("RFC_4648_URL_PADDING", codec_full_buffer::Encoding.c_str());
}

//*************************************************************************
TEST(test_check_encode_safe_buffer_sizes)
{
Expand Down
Loading

0 comments on commit ce24b0c

Please sign in to comment.