fix(base64): basen::Exception
This commit is contained in:
@ -14,24 +14,24 @@ namespace base64
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
* @throw basen::Exception(OVERFLOW) if if there is an overflow
|
||||
*/
|
||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||
size_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
||||
std::string encode(std::span<const uint8_t> data) noexcept;
|
||||
std::string encode(std::span<const uint8_t> data);
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @throw std::logic_error if out of digits map
|
||||
* @throw std::logic_error if incorrect padding
|
||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
||||
* @throw basen::Exception(PADDING) if incorrect padding
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
std::vector<uint8_t> decode(std::string_view str);
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include <basen/base64.hpp>
|
||||
#include <basen/baseN.hpp>
|
||||
#include <basen/Exception.hpp>
|
||||
|
||||
namespace base64
|
||||
{
|
||||
@ -48,7 +49,7 @@ namespace base64
|
||||
size_t str_size = data.size() / 3;
|
||||
if (str_size > std::numeric_limits<size_t>::max() / 4)
|
||||
{
|
||||
throw std::overflow_error("base64::sizeEncoded: overflow");
|
||||
throw basen::Exception(basen::Exception::Code::OVERFLOW);
|
||||
}
|
||||
str_size = str_size * 4 + (data.size() % 3 ? 4 : 0);
|
||||
return str_size;
|
||||
@ -64,7 +65,7 @@ namespace base64
|
||||
{
|
||||
if (str_size < base64::sizeEncoded(std::span<const uint8_t>(data, data_size)))
|
||||
{
|
||||
throw std::length_error("base64::encode: not enough allocated length");
|
||||
throw basen::Exception(basen::Exception::Code::LENGTH);
|
||||
}
|
||||
for (size_t i = 0; i < data_size / 3; i++)
|
||||
{
|
||||
@ -92,7 +93,7 @@ namespace base64
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::string encode(std::span<const uint8_t> data) noexcept
|
||||
std::string encode(std::span<const uint8_t> data)
|
||||
{
|
||||
std::string str(base64::sizeEncoded(data), ' ');
|
||||
base64::encode(data.data(), data.size(), str.data(), str.size());
|
||||
@ -103,18 +104,18 @@ namespace base64
|
||||
std::string_view sv(str, str_size);
|
||||
if (data_size < base64::sizeDecoded(sv))
|
||||
{
|
||||
throw std::length_error("base64::decode: not enough allocated length");
|
||||
throw basen::Exception(basen::Exception::Code::LENGTH);
|
||||
}
|
||||
if (!base64::isValid(sv))
|
||||
{
|
||||
throw std::logic_error("base64::decode: out of digits map");
|
||||
throw basen::Exception(basen::Exception::Code::OUT_OF_ALPH);
|
||||
}
|
||||
auto size = std::distance(sv.begin(), std::find_if(sv.rbegin(), sv.rend(), [](char ch)
|
||||
{ return ch != '='; })
|
||||
.base());
|
||||
if (sv.size() % 4 != 0 || sv.size() - size > 2)
|
||||
{
|
||||
throw std::logic_error("base64::decode: incorrect padding");
|
||||
throw basen::Exception(basen::Exception::Code::PADDING);
|
||||
}
|
||||
for (auto i = 0; i < size / 4; i++)
|
||||
{
|
||||
@ -136,7 +137,7 @@ namespace base64
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept
|
||||
std::vector<uint8_t> decode(std::string_view str)
|
||||
{
|
||||
std::vector<uint8_t> data(base64::sizeDecoded(str));
|
||||
base64::decode(str.data(), str.size(), data.data(), data.size());
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include <basen/base64.hpp>
|
||||
#include <basen/Exception.hpp>
|
||||
#include <basen/hex.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
@ -31,8 +32,8 @@ TEST(base64, encode)
|
||||
|
||||
std::vector<uint8_t> data = {0x74, 0x65, 0x73, 0x74};
|
||||
std::string str = "";
|
||||
EXPECT_THROW(encode(data.data(), std::numeric_limits<size_t>::max(), str.data(), str.size()), std::overflow_error);
|
||||
EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error);
|
||||
EXPECT_THROW(encode(data.data(), std::numeric_limits<size_t>::max(), str.data(), str.size()), basen::Exception);
|
||||
EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), basen::Exception);
|
||||
EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size()));
|
||||
}
|
||||
TEST(base64, encode_1e7)
|
||||
@ -46,9 +47,9 @@ TEST(base64, decode)
|
||||
EXPECT_EQ(hex::encode(decode(it.first)), it.second);
|
||||
|
||||
std::vector<uint8_t> data = {0x61, 0x6e, 0x6f};
|
||||
EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("FF==", 2, data.data(), 0), std::length_error);
|
||||
EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), basen::Exception);
|
||||
EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), basen::Exception);
|
||||
EXPECT_THROW(decode("FF==", 2, data.data(), 0), basen::Exception);
|
||||
EXPECT_NO_THROW(decode("", 0, data.data(), 0));
|
||||
}
|
||||
TEST(base64, decode_1e7)
|
||||
|
||||
Reference in New Issue
Block a user