fix(hex): basen::Exception

This commit is contained in:
2024-09-30 17:41:43 +03:00
parent 64c2e51104
commit edcaebcf53
3 changed files with 21 additions and 19 deletions

View File

@ -14,24 +14,24 @@ namespace hex
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 str_size %2 != 0 (isn't hex)
* @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 str_size %2 != 0 (isn't hex)
* @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);
}

View File

@ -2,6 +2,7 @@
#include <stdexcept>
#include <basen/baseN.hpp>
#include <basen/Exception.hpp>
#include <basen/hex.hpp>
namespace hex
@ -38,7 +39,7 @@ namespace hex
{
if (data.size() > std::numeric_limits<size_t>::max() / 2)
{
throw std::overflow_error("hex::sizeEncoded: overflow");
throw basen::Exception(basen::Exception::Code::OVERFLOW);
}
return data.size() * 2;
}
@ -50,7 +51,7 @@ namespace hex
{
if (str_size < hex::sizeEncoded(std::span<const uint8_t>(data, data_size)))
{
throw std::length_error("hex::encode: not enough allocated length");
throw basen::Exception(basen::Exception::Code::LENGTH);
}
for (size_t i = 0; i < data_size; i++)
{
@ -58,7 +59,7 @@ namespace hex
str[i * 2 + 1] = digits[data[i] & 0x0F];
}
}
std::string encode(std::span<const uint8_t> data) noexcept
std::string encode(std::span<const uint8_t> data)
{
std::string str(hex::sizeEncoded(data), ' ');
hex::encode(data.data(), data.size(), str.data(), str.size());
@ -68,22 +69,22 @@ namespace hex
{
if (str_size % 2 != 0)
{
throw std::logic_error("hex::decode: isn't hex");
throw basen::Exception(basen::Exception::Code::PADDING);
}
if (data_size < hex::sizeDecoded(std::string_view(str, str_size)))
{
throw std::length_error("hex::decode: not enough allocated length");
throw basen::Exception(basen::Exception::Code::LENGTH);
}
if (!hex::isValid(str, str_size))
{
throw std::logic_error("hex::decode: out of digits map");
throw basen::Exception(basen::Exception::Code::OUT_OF_ALPH);
}
for (size_t i = 0; i * 2 < str_size; i++)
{
data[i] = map[(uint8_t)str[i * 2]] << 4 | map[(uint8_t)str[i * 2 + 1]];
}
}
std::vector<uint8_t> decode(std::string_view str) noexcept
std::vector<uint8_t> decode(std::string_view str)
{
std::vector<uint8_t> data(hex::sizeDecoded(str));
hex::decode(str.data(), str.size(), data.data(), data.size());

View File

@ -1,5 +1,6 @@
#include <utility>
#include <basen/Exception.hpp>
#include <basen/hex.hpp>
#include <gtest/gtest.h>
@ -20,8 +21,8 @@ TEST(hex, encode)
EXPECT_EQ("74657374", encode(data));
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(hex, encode_1e7)
@ -34,9 +35,9 @@ TEST(hex, decode)
std::vector<uint8_t> data = {0x61, 0x6e, 0x6f};
EXPECT_EQ(decode("616e6f"), data);
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(hex, decode_1e7)