From dfb9e4e7afb3db0421549e3f0c1057a780244428 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Wed, 11 Sep 2024 20:37:14 +0300 Subject: [PATCH] feat(hex): overloading uint8_t * --- include/base/hex.hpp | 3 +++ src/baseN.cpp | 2 +- src/hex.cpp | 46 ++++++++++++++++++++++++++++++-------------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/include/base/hex.hpp b/include/base/hex.hpp index 495f204..7b8956a 100644 --- a/include/base/hex.hpp +++ b/include/base/hex.hpp @@ -6,7 +6,10 @@ namespace hex { + bool isValid(const char *str) noexcept; bool isValid(const std::string &str) noexcept; + void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept; std::string encode(const std::vector &data) noexcept; + void decode(const char *str, uint8_t *data, uint64_t data_size); std::vector decode(const std::string &str); } \ No newline at end of file diff --git a/src/baseN.cpp b/src/baseN.cpp index a6df33a..be596d9 100644 --- a/src/baseN.cpp +++ b/src/baseN.cpp @@ -6,7 +6,7 @@ namespace baseN { bool isValid(const char *str, const int8_t *map) noexcept { - uint8_t i = 0; + uint64_t i = 0; while (str[i] != '\0') { if (map[(int8_t)str[i]] == -1) diff --git a/src/hex.cpp b/src/hex.cpp index 525dac2..cbe0cf3 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -1,5 +1,6 @@ #include +#include #include static const char hexdigits[] = "0123456789abcdef"; @@ -25,38 +26,55 @@ static const int8_t hexmap[] = { namespace hex { + bool isValid(const char *str) noexcept + { + return baseN::isValid(str, hexmap); + } bool isValid(const std::string &str) noexcept { - for (int64_t i = str.size() - 1; i >= 0; i--) + return baseN::isValid(str, hexmap); + } + void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept + { + uint64_t i = 0; + while (str[i] != '\0' && i / 2 < data_size) { - if (hexmap[(int8_t)str[i]] == -1) + if (i % 2 == 0) { - return false; + str[i] = hexdigits[data[i / 2] >> 4]; + } else { + str[i] = hexdigits[data[i / 2] & 0x0F]; } + i++; } - return true; } std::string encode(const std::vector &data) noexcept { - std::string str; - for (uint64_t i = 0; i < data.size(); i++) - { - str.push_back(hexdigits[data[i] >> 4]); - str.push_back(hexdigits[data[i] & 0x0F]); - } + std::string str(data.size() * 2, ' '); + hex::encode(data.data(), data.size(), str.data()); return str; } - std::vector decode(const std::string &str) + void decode(const char *str, uint8_t *data, uint64_t data_size) { if (!hex::isValid(str)) { throw std::logic_error("hex::decode: out of digits map"); } - std::vector data; - for (uint64_t i = 0; i < str.size() / 2; i++) + uint64_t i = 0; + while (i < data_size && str[i * 2] != '\0') { - data.push_back(hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]); + if(str[i + 1] == '\0') + { + throw std::logic_error("hex::decode: isn't hex"); + } + data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]; + i++; } + } + std::vector decode(const std::string &str) + { + std::vector data(str.size() / 2); + hex::decode(str.data(), data.data(), data.size()); return data; } } \ No newline at end of file