feat(hex, base64): sizeEncoded, sizeDecoded

This commit is contained in:
2024-09-17 14:58:21 +03:00
parent 961b5f6b0c
commit 94661af528
4 changed files with 40 additions and 6 deletions

View File

@ -1,3 +1,4 @@
#include <limits>
#include <stdexcept>
#include <base/baseN.hpp>
@ -35,9 +36,21 @@ namespace hex
{
return baseN::isValid(str, hexmap);
}
uint64_t sizeEncoded(uint64_t data_size)
{
if (data_size > std::numeric_limits<uint64_t>::max() / 2)
{
throw std::overflow_error("hex::sizeEncoded: overflow");
}
return data_size * 2;
}
uint64_t sizeDecoded(uint64_t str_size) noexcept
{
return str_size / 2;
}
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size)
{
if (str_size < data_size * 2)
if (str_size < hex::sizeEncoded(data_size))
{
throw std::logic_error("hex::encode: not enough allocated length");
}
@ -49,7 +62,7 @@ namespace hex
}
std::string encode(std::span<const uint8_t> data) noexcept
{
std::string str(data.size() * 2, ' ');
std::string str(hex::sizeEncoded(data.size()), ' ');
hex::encode(data.data(), data.size(), str.data(), str.size());
return str;
}
@ -59,7 +72,7 @@ namespace hex
{
throw std::logic_error("hex::decode: isn't hex");
}
if (data_size < str_size / 2)
if (data_size < hex::sizeDecoded(str_size))
{
throw std::logic_error("hex::decode: not enough allocated length");
}
@ -74,7 +87,7 @@ namespace hex
}
std::vector<uint8_t> decode(std::string_view str) noexcept
{
std::vector<uint8_t> data(str.size() / 2);
std::vector<uint8_t> data(hex::sizeDecoded(str.size()));
hex::decode(str.data(), str.size(), data.data(), data.size());
return data;
}