fix(hex): now throws exc if not enough allocated mem
This commit is contained in:
@ -10,9 +10,9 @@ namespace hex
|
||||
bool isValid(const char *str) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept;
|
||||
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;
|
||||
|
||||
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);
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
}
|
||||
16
src/hex.cpp
16
src/hex.cpp
@ -35,9 +35,13 @@ namespace hex
|
||||
{
|
||||
return baseN::isValid(str, hexmap);
|
||||
}
|
||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept
|
||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size)
|
||||
{
|
||||
for (size_t i = 0; i < data_size && i * 2 + 1 < str_size; i++)
|
||||
if (str_size < data_size * 2)
|
||||
{
|
||||
throw std::logic_error("hex::encode: not enough allocated length");
|
||||
}
|
||||
for (size_t i = 0; i < data_size; i++)
|
||||
{
|
||||
str[i * 2] = hexdigits[data[i] >> 4];
|
||||
str[i * 2 + 1] = hexdigits[data[i] & 0x0F];
|
||||
@ -59,12 +63,16 @@ namespace hex
|
||||
{
|
||||
throw std::logic_error("hex::decode: isn't hex");
|
||||
}
|
||||
for (size_t i = 0; i < data_size && i * 2 < str_size; i++)
|
||||
if (data_size < str_size / 2)
|
||||
{
|
||||
throw std::logic_error("hex::decode: not enough allocated length");
|
||||
}
|
||||
for (size_t i = 0; i * 2 < str_size; i++)
|
||||
{
|
||||
data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]];
|
||||
}
|
||||
}
|
||||
std::vector<uint8_t> decode(std::string_view str)
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept
|
||||
{
|
||||
std::vector<uint8_t> data(str.size() / 2);
|
||||
hex::decode(str.data(), str.size(), data.data(), data.size());
|
||||
|
||||
@ -7,6 +7,15 @@ TEST(hex, encode)
|
||||
{
|
||||
std::vector<uint8_t> data = {0x74, 0x65, 0x73, 0x74};
|
||||
EXPECT_EQ("74657374", encode(data));
|
||||
try
|
||||
{
|
||||
std::string str = "";
|
||||
encode(data.data(), data.size(), str.data(), str.size());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.what(), "hex::encode: not enough allocated length");
|
||||
}
|
||||
}
|
||||
TEST(hex, encode_1e6)
|
||||
{
|
||||
@ -17,6 +26,15 @@ TEST(hex, decode)
|
||||
{
|
||||
std::vector<uint8_t> data = {0x61, 0x6e, 0x6f};
|
||||
EXPECT_EQ(decode("616e6f"), data);
|
||||
try
|
||||
{
|
||||
std::string str = "";
|
||||
decode("616", 3, data.data(), data.size());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.what(), "hex::decode: isn't hex");
|
||||
}
|
||||
}
|
||||
TEST(hex, decode_1e6)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user