fix(hex): basen::Exception
This commit is contained in:
15
src/hex.cpp
15
src/hex.cpp
@ -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());
|
||||
|
||||
Reference in New Issue
Block a user