From 7e78bca8109b760ea8c9779407d637f05a708222 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Mon, 30 Sep 2024 13:10:47 +0300 Subject: [PATCH] fix(types): int8_t -> uint8_t; -1 -> 255 --- include/basen/base58.hpp | 2 +- include/basen/base64.hpp | 2 +- include/basen/baseN.hpp | 24 ++++++++++----------- include/basen/hex.hpp | 2 +- src/base58.cpp | 34 ++++++++++++++--------------- src/base64.cpp | 46 ++++++++++++++++++++-------------------- src/baseN.cpp | 22 +++++++++---------- src/basen.cpp | 2 +- src/hex.cpp | 36 +++++++++++++++---------------- test/test-baseN.cpp | 2 +- 10 files changed, 86 insertions(+), 86 deletions(-) diff --git a/include/basen/base58.hpp b/include/basen/base58.hpp index 551d46b..1fe6600 100644 --- a/include/basen/base58.hpp +++ b/include/basen/base58.hpp @@ -11,7 +11,7 @@ namespace base58 * @brief bitcoin alphabet */ extern const char digits[59]; - extern const int8_t map[256]; + extern const uint8_t map[256]; bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; diff --git a/include/basen/base64.hpp b/include/basen/base64.hpp index 82a119e..1ee1c76 100644 --- a/include/basen/base64.hpp +++ b/include/basen/base64.hpp @@ -8,7 +8,7 @@ namespace base64 { extern const char digits[65]; - extern const int8_t map[256]; + extern const uint8_t map[256]; bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; diff --git a/include/basen/baseN.hpp b/include/basen/baseN.hpp index 6b49495..a3a9485 100644 --- a/include/basen/baseN.hpp +++ b/include/basen/baseN.hpp @@ -10,23 +10,23 @@ namespace baseN /** * @param digits char[base] array of digits * @param digits_size size of digits array. Equals to base - * @param map [out] int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol + * @param map [out] uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol * @throw std::logic_error if alphabet contain same chars */ - void digitsMap(const char *digits, uint8_t digits_size, int8_t *map); + void digitsMap(const char *digits, uint8_t digits_size, uint8_t *map); /** * @param str [in] pointer to string * @param str_size - * @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol + * @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol * @return that string doesn't contain unnecessary symbols */ - bool isValid(const char *str, size_t str_size, const int8_t *map) noexcept; + bool isValid(const char *str, size_t str_size, const uint8_t *map) noexcept; /** * @param str string or string_view which you want to decode - * @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol + * @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol * @return that string doesn't contain unnecessary symbols */ - bool isValid(std::string_view str, const int8_t *map) noexcept; + bool isValid(std::string_view str, const uint8_t *map) noexcept; /** * @param data vector or span of data which you want to encode @@ -53,7 +53,7 @@ namespace baseN * @code{cpp} * std::vector data; * std::string str(baseN::sizeEncoded(data, 58), ' '); - * + * * auto offset = baseN::encode(data.data(), data.size(), str.data(), str.size(), 58, base58::digits); * // deleting leading zeroes * str.erase(str.begin(), str.begin() + offset); @@ -81,11 +81,11 @@ namespace baseN * @param data_size * @param base from 1 to 255 * @param digits char[base] array of digits - * @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol + * @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol * @code{cpp} * std::string str; * std::vector data(baseN::sizeDecoded(str, 58)); - * + * * auto offset = baseN::decode(str.data(), str.size(), data.data(), data.size(), 58, base58::digits, base58::map); * // deleting leading zeroes * data.erase(data.begin(), data.begin() + offset); @@ -93,17 +93,17 @@ namespace baseN * @return number of leading chars, which should be trimmed * @warning contain leading zeros, returns count of them */ - size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const int8_t *map); + size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const uint8_t *map); /** * @param str string or string_view which you want to decode * @param base from 1 to 255 * @param digits char[base] array of digits - * @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol + * @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol * @code{cpp} * std::string str; * auto data = baseN::decode(str, 58, base58::digits, base58::map); * @endcode * @return decoded data */ - std::vector decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept; + std::vector decode(std::string_view str, uint8_t base, const char *digits, const uint8_t *map) noexcept; } \ No newline at end of file diff --git a/include/basen/hex.hpp b/include/basen/hex.hpp index 3288176..de4a6d2 100644 --- a/include/basen/hex.hpp +++ b/include/basen/hex.hpp @@ -8,7 +8,7 @@ namespace hex { extern const char digits[17]; - extern const int8_t map[256]; + extern const uint8_t map[256]; bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; diff --git a/src/base58.cpp b/src/base58.cpp index aa57532..edcf0b8 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -8,23 +8,23 @@ namespace base58 { const char digits[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - const int8_t map[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, - -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1, - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, - -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, - 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + const uint8_t map[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 255, 255, 255, 255, 255, 255, + 255, 9, 10, 11, 12, 13, 14, 15, 16, 255, 17, 18, 19, 20, 21, 255, + 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 255, 255, 255, 255, 255, + 255, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 255, 44, 45, 46, + 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // }; bool isValid(const char *str, size_t str_size) noexcept diff --git a/src/base64.cpp b/src/base64.cpp index b1dc316..d433d2f 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -9,23 +9,23 @@ namespace base64 { const char digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - const int8_t map[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, - -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + const uint8_t map[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, + 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // }; bool isValid(const char *str, size_t str_size) noexcept @@ -118,19 +118,19 @@ namespace base64 } for (auto i = 0; i < size / 4; i++) { - data[i * 3] = map[(int8_t)str[i * 4]] << 2 | map[(int8_t)str[i * 4 + 1]] >> 4; - data[i * 3 + 1] = map[(int8_t)str[i * 4 + 1]] << 4 | map[(int8_t)str[i * 4 + 2]] >> 2; - data[i * 3 + 2] = map[(int8_t)str[i * 4 + 2]] << 6 | map[(int8_t)str[i * 4 + 3]]; + data[i * 3] = map[(uint8_t)str[i * 4]] << 2 | map[(uint8_t)str[i * 4 + 1]] >> 4; + data[i * 3 + 1] = map[(uint8_t)str[i * 4 + 1]] << 4 | map[(uint8_t)str[i * 4 + 2]] >> 2; + data[i * 3 + 2] = map[(uint8_t)str[i * 4 + 2]] << 6 | map[(uint8_t)str[i * 4 + 3]]; } size_t last_idx = size / 4 * 3; switch (size % 4) { case 2: - data[last_idx] = map[(int8_t)str[size - 2]] << 2 | map[(int8_t)str[size - 1]] >> 4; + data[last_idx] = map[(uint8_t)str[size - 2]] << 2 | map[(uint8_t)str[size - 1]] >> 4; break; case 3: - data[last_idx] = map[(int8_t)str[size - 3]] << 2 | map[(int8_t)str[size - 2]] >> 4; - data[last_idx + 1] = map[(int8_t)str[size - 2]] << 4 | map[(int8_t)str[size - 1]] >> 2; + data[last_idx] = map[(uint8_t)str[size - 3]] << 2 | map[(uint8_t)str[size - 2]] >> 4; + data[last_idx + 1] = map[(uint8_t)str[size - 2]] << 4 | map[(uint8_t)str[size - 1]] >> 2; break; default: break; diff --git a/src/baseN.cpp b/src/baseN.cpp index 5512f3a..9059f33 100644 --- a/src/baseN.cpp +++ b/src/baseN.cpp @@ -9,24 +9,24 @@ static constexpr auto log256 = std::log(256); namespace baseN { - void digitsMap(const char *digits, uint8_t digits_size, int8_t *map) + void digitsMap(const char *digits, uint8_t digits_size, uint8_t *map) { - std::fill(map, map + 256, -1); + std::fill(map, map + 256, 255); for (uint8_t i = 0; i < digits_size; i++) { - if (map[(int8_t)digits[i]] != -1) + if (map[(uint8_t)digits[i]] != 255) { throw std::logic_error("baseN::digitsMap: alphabet contain same chars"); } - map[(int8_t)digits[i]] = i; + map[(uint8_t)digits[i]] = i; } } - bool isValid(const char *str, size_t str_size, const int8_t *map) noexcept + bool isValid(const char *str, size_t str_size, const uint8_t *map) noexcept { return std::all_of(str, str + str_size, [map](char ch) - { return map[(int8_t)ch] != -1; }); + { return map[(uint8_t)ch] != 255; }); } - bool isValid(std::string_view str, const int8_t *map) noexcept + bool isValid(std::string_view str, const uint8_t *map) noexcept { return baseN::isValid(str.data(), str.size(), map); } @@ -99,7 +99,7 @@ namespace baseN str.erase(str.begin(), str.begin() + offset); return str; } - size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const int8_t *map) + size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const uint8_t *map) { std::string_view sv(std::find_if(str, str + str_size, [digits](char ch) { return ch != digits[0]; }), @@ -117,10 +117,10 @@ namespace baseN if (sv.size() != 0) { quo_it_last++; - *quo_it = map[(int8_t)*sv_it++]; + *quo_it = map[(uint8_t)*sv_it++]; while (sv_it < sv.end()) { - div = map[(int8_t)*sv_it++]; + div = map[(uint8_t)*sv_it++]; while (quo_it < quo_it_last && quo_it < dv.rend()) { div += *quo_it * base; @@ -141,7 +141,7 @@ namespace baseN } return std::distance(quo_it_last, dv.rend()); } - std::vector decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept + std::vector decode(std::string_view str, uint8_t base, const char *digits, const uint8_t *map) noexcept { std::vector data(baseN::sizeDecoded(str, base, digits)); size_t offset = baseN::decode(str.data(), str.size(), data.data(), data.size(), base, digits, map); diff --git a/src/basen.cpp b/src/basen.cpp index e51308e..56082a1 100644 --- a/src/basen.cpp +++ b/src/basen.cpp @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) if (program.is_used("-a")) { auto alphabet = program.get("-a"); - int8_t map[256]; + uint8_t map[256]; try { baseN::digitsMap(alphabet.data(), alphabet.size(), map); diff --git a/src/hex.cpp b/src/hex.cpp index 4008fd8..eaaf018 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -7,23 +7,23 @@ namespace hex { const char digits[] = "0123456789abcdef"; - const int8_t map[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + const uint8_t map[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // }; bool isValid(const char *str, size_t str_size) noexcept @@ -80,7 +80,7 @@ namespace hex } for (size_t i = 0; i * 2 < str_size; i++) { - data[i] = map[(int8_t)str[i * 2]] << 4 | map[(int8_t)str[i * 2 + 1]]; + data[i] = map[(uint8_t)str[i * 2]] << 4 | map[(uint8_t)str[i * 2 + 1]]; } } std::vector decode(std::string_view str) noexcept diff --git a/test/test-baseN.cpp b/test/test-baseN.cpp index 0ae97fd..dcd3f8c 100644 --- a/test/test-baseN.cpp +++ b/test/test-baseN.cpp @@ -9,7 +9,7 @@ using namespace baseN; TEST(baseN, digitsMap) { - int8_t map[256]; + uint8_t map[256]; digitsMap(base58::digits, 58, map); EXPECT_TRUE(std::equal(map, map + 256, base58::map));