diff --git a/include/basen/base58.hpp b/include/basen/base58.hpp index aa9685d..551d46b 100644 --- a/include/basen/base58.hpp +++ b/include/basen/base58.hpp @@ -13,27 +13,27 @@ namespace base58 extern const char digits[59]; extern const int8_t map[256]; - bool isValid(const char *str, uint64_t str_size) noexcept; + bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; /** * @throw std::overflow_error if if there is an overflow */ - uint64_t sizeEncoded(std::span data) noexcept; - uint64_t sizeDecoded(std::string_view str) noexcept; + size_t sizeEncoded(std::span data) noexcept; + size_t sizeDecoded(std::string_view str) noexcept; /** * @return number of leading chars, which should be trimmed * @warning contain leading zeros, returns count of them */ - uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept; + size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept; std::string encode(std::span data) noexcept; /** * @return number of leading chars, which should be trimmed * @warning contain leading zeros, returns count of them */ - uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept; + size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) noexcept; std::vector decode(std::string_view str) noexcept; /** diff --git a/include/basen/base64.hpp b/include/basen/base64.hpp index cd116b0..82a119e 100644 --- a/include/basen/base64.hpp +++ b/include/basen/base64.hpp @@ -10,20 +10,20 @@ namespace base64 extern const char digits[65]; extern const int8_t map[256]; - bool isValid(const char *str, uint64_t str_size) noexcept; + bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; /** * @throw std::overflow_error if if there is an overflow */ - uint64_t sizeEncoded(std::span data); - uint64_t sizeDecoded(std::string_view str) noexcept; + size_t sizeEncoded(std::span data); + size_t sizeDecoded(std::string_view str) noexcept; /** * @throw std::length_error if not enough allocated length * @warning contain leading zeros, returns count of them */ - void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size); + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); std::string encode(std::span data) noexcept; /** @@ -32,6 +32,6 @@ namespace base64 * @throw std::logic_error if incorrect padding * @warning contain leading zeros, returns count of them */ - void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size); + void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size); std::vector decode(std::string_view str) noexcept; } \ No newline at end of file diff --git a/include/basen/baseN.hpp b/include/basen/baseN.hpp index ced5997..6b49495 100644 --- a/include/basen/baseN.hpp +++ b/include/basen/baseN.hpp @@ -20,7 +20,7 @@ namespace baseN * @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 * @return that string doesn't contain unnecessary symbols */ - bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept; + bool isValid(const char *str, size_t str_size, const int8_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 @@ -33,7 +33,7 @@ namespace baseN * @param base from 1 to 255 * @return estimated size after encoding */ - uint64_t sizeEncoded(std::span data, uint8_t base); + size_t sizeEncoded(std::span data, uint8_t base); /** * @param str string or string_view which you want to decode * @param base from 1 to 255 @@ -41,7 +41,7 @@ namespace baseN * @return estimated size after decoding * @throw std::overflow_error if if there is an overflow */ - uint64_t sizeDecoded(std::string_view str, uint8_t base, const char *digits) noexcept; + size_t sizeDecoded(std::string_view str, uint8_t base, const char *digits) noexcept; /** * @param data [in] pointer to data which you want encode @@ -61,7 +61,7 @@ namespace baseN * @return number of leading chars, which should be trimmed * @warning contain leading zeros, returns count of them */ - uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size, uint8_t base, const char *digits); + size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size, uint8_t base, const char *digits); /** * @param data vector or span of data which you want to encode * @param base from 1 to 255 @@ -93,7 +93,7 @@ namespace baseN * @return number of leading chars, which should be trimmed * @warning contain leading zeros, returns count of them */ - uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_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 int8_t *map); /** * @param str string or string_view which you want to decode * @param base from 1 to 255 diff --git a/include/basen/hash/sha256.hpp b/include/basen/hash/sha256.hpp index 279ecb5..e3a9d4c 100644 --- a/include/basen/hash/sha256.hpp +++ b/include/basen/hash/sha256.hpp @@ -6,6 +6,6 @@ namespace hash { - void sha256(const uint8_t *data, uint64_t data_size, uint8_t *hash) noexcept; + void sha256(const uint8_t *data, size_t data_size, uint8_t *hash) noexcept; std::vector sha256(std::span data) noexcept; } \ No newline at end of file diff --git a/include/basen/hex.hpp b/include/basen/hex.hpp index afd2a55..3288176 100644 --- a/include/basen/hex.hpp +++ b/include/basen/hex.hpp @@ -10,20 +10,20 @@ namespace hex extern const char digits[17]; extern const int8_t map[256]; - bool isValid(const char *str, uint64_t str_size) noexcept; + bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; /** * @throw std::overflow_error if if there is an overflow */ - uint64_t sizeEncoded(std::span data); - uint64_t sizeDecoded(std::string_view str) noexcept; + size_t sizeEncoded(std::span data); + size_t sizeDecoded(std::string_view str) noexcept; /** * @throw std::length_error if not enough allocated length * @warning contain leading zeros, returns count of them */ - void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size); + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); std::string encode(std::span data) noexcept; /** @@ -32,6 +32,6 @@ namespace hex * @throw std::logic_error if str_size %2 != 0 (isn't hex) * @warning contain leading zeros, returns count of them */ - void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size); + void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size); std::vector decode(std::string_view str) noexcept; } \ No newline at end of file diff --git a/src/base58.cpp b/src/base58.cpp index 190bac9..aa57532 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -27,7 +27,7 @@ namespace base58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // }; - bool isValid(const char *str, uint64_t str_size) noexcept + bool isValid(const char *str, size_t str_size) noexcept { return baseN::isValid(str, str_size, map); } @@ -35,15 +35,15 @@ namespace base58 { return baseN::isValid(str, map); } - uint64_t sizeEncoded(std::span data) noexcept + size_t sizeEncoded(std::span data) noexcept { return baseN::sizeEncoded(data, 58); } - uint64_t sizeDecoded(std::string_view str) noexcept + size_t sizeDecoded(std::string_view str) noexcept { return baseN::sizeDecoded(str, 58, digits); } - uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept + size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept { return baseN::encode(data, data_size, str, str_size, 58, digits); } @@ -51,7 +51,7 @@ namespace base58 { return baseN::encode(data, 58, digits); } - uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept + size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) noexcept { return baseN::decode(str, str_size, data, data_size, 58, digits, map); } diff --git a/src/base64.cpp b/src/base64.cpp index 0d20bb4..b1dc316 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -28,7 +28,7 @@ namespace base64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // }; - bool isValid(const char *str, uint64_t str_size) noexcept + bool isValid(const char *str, size_t str_size) noexcept { return base64::isValid(std::string_view(str, str_size)); } @@ -43,37 +43,37 @@ namespace base64 } return baseN::isValid(sv, map); } - uint64_t sizeEncoded(std::span data) + size_t sizeEncoded(std::span data) { - uint64_t str_size = data.size() / 3; - if (str_size > std::numeric_limits::max() / 4) + size_t str_size = data.size() / 3; + if (str_size > std::numeric_limits::max() / 4) { throw std::overflow_error("base64::sizeEncoded: overflow"); } str_size = str_size * 4 + (data.size() % 3 ? 4 : 0); return str_size; } - uint64_t sizeDecoded(std::string_view str) noexcept + size_t sizeDecoded(std::string_view str) noexcept { auto size = std::distance(str.begin(), std::find_if(str.rbegin(), str.rend(), [](char ch) { return ch != '='; }) .base()); return size / 4 * 3 + (size % 4 ? size % 4 - 1 : 0); } - void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) { if (str_size < base64::sizeEncoded(std::span(data, data_size))) { throw std::length_error("base64::encode: not enough allocated length"); } - for (uint64_t i = 0; i < data_size / 3; i++) + for (size_t i = 0; i < data_size / 3; i++) { str[i * 4] = digits[data[i * 3] >> 2]; str[i * 4 + 1] = digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F]; str[i * 4 + 2] = digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F]; str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F]; } - uint64_t last_idx = data_size / 3 * 4; + size_t last_idx = data_size / 3 * 4; switch (data_size % 3) { case 1: @@ -98,7 +98,7 @@ namespace base64 base64::encode(data.data(), data.size(), str.data(), str.size()); return str; } - void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) + void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) { std::string_view sv(str, str_size); if (data_size < base64::sizeDecoded(sv)) @@ -122,7 +122,7 @@ namespace base64 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]]; } - uint64_t last_idx = size / 4 * 3; + size_t last_idx = size / 4 * 3; switch (size % 4) { case 2: diff --git a/src/baseN.cpp b/src/baseN.cpp index 17cc15d..5512f3a 100644 --- a/src/baseN.cpp +++ b/src/baseN.cpp @@ -21,7 +21,7 @@ namespace baseN map[(int8_t)digits[i]] = i; } } - bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept + bool isValid(const char *str, size_t str_size, const int8_t *map) noexcept { return std::all_of(str, str + str_size, [map](char ch) { return map[(int8_t)ch] != -1; }); @@ -30,25 +30,25 @@ namespace baseN { return baseN::isValid(str.data(), str.size(), map); } - uint64_t sizeEncoded(std::span data, uint8_t base) + size_t sizeEncoded(std::span data, uint8_t base) { std::span dv(std::find_if(data.begin(), data.end(), [](uint8_t item) { return item != 0; }), data.end()); - if (dv.size() > std::numeric_limits::max() / log256) + if (dv.size() > std::numeric_limits::max() / log256) { throw std::overflow_error("baseN::sizeEncoded: overflow"); } return dv.size() * log256 / std::log(base) + 1 + (data.size() - dv.size()); } - uint64_t sizeDecoded(std::string_view str, uint8_t base, const char *digits) noexcept + size_t sizeDecoded(std::string_view str, uint8_t base, const char *digits) noexcept { std::string_view sv(std::find_if(str.begin(), str.end(), [digits](uint8_t ch) { return ch != digits[0]; }), str.end()); return sv.size() * std::log(base) / log256 + 1 + (str.size() - sv.size()); } - uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size, uint8_t base, const char *digits) + size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size, uint8_t base, const char *digits) { std::vector dv(std::find_if(data, data + data_size, [](uint8_t item) { return item != 0; }), @@ -86,7 +86,7 @@ namespace baseN } *sv_it++ = digits[div]; } - for (uint64_t i = 0; i < data_size - dv.size() && sv_it < sv.rend(); i++) + for (size_t i = 0; i < data_size - dv.size() && sv_it < sv.rend(); i++) { *sv_it++ = digits[0]; } @@ -95,11 +95,11 @@ namespace baseN std::string encode(std::span data, uint8_t base, const char *digits) noexcept { std::string str(baseN::sizeEncoded(data, base), ' '); - uint64_t offset = baseN::encode(data.data(), data.size(), str.data(), str.size(), base, digits); + size_t offset = baseN::encode(data.data(), data.size(), str.data(), str.size(), base, digits); str.erase(str.begin(), str.begin() + offset); return str; } - uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_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 int8_t *map) { std::string_view sv(std::find_if(str, str + str_size, [digits](char ch) { return ch != digits[0]; }), @@ -135,7 +135,7 @@ namespace baseN quo_it = dv.rbegin(); } } - for (uint64_t i = 0; i < str_size - sv.size() && quo_it_last < dv.rend(); i++) + for (size_t i = 0; i < str_size - sv.size() && quo_it_last < dv.rend(); i++) { *quo_it_last++ = 0; } @@ -144,7 +144,7 @@ namespace baseN std::vector decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept { std::vector data(baseN::sizeDecoded(str, base, digits)); - uint64_t offset = baseN::decode(str.data(), str.size(), data.data(), data.size(), base, digits, map); + size_t offset = baseN::decode(str.data(), str.size(), data.data(), data.size(), base, digits, map); data.erase(data.begin(), data.begin() + offset); return data; } diff --git a/src/hash/sha256.cpp b/src/hash/sha256.cpp index 32d2b14..c423dc4 100644 --- a/src/hash/sha256.cpp +++ b/src/hash/sha256.cpp @@ -165,7 +165,7 @@ static void sha256_final(SHA256_CTX *ctx, uint8_t *hash) namespace hash { - void sha256(const uint8_t *data, uint64_t data_size, uint8_t *hash) noexcept + void sha256(const uint8_t *data, size_t data_size, uint8_t *hash) noexcept { SHA256_CTX ctx; sha256_init(&ctx); diff --git a/src/hex.cpp b/src/hex.cpp index a5e4286..4008fd8 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -26,7 +26,7 @@ namespace hex -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // }; - bool isValid(const char *str, uint64_t str_size) noexcept + bool isValid(const char *str, size_t str_size) noexcept { return baseN::isValid(str, str_size, map); } @@ -34,25 +34,25 @@ namespace hex { return baseN::isValid(str, map); } - uint64_t sizeEncoded(std::span data) + size_t sizeEncoded(std::span data) { - if (data.size() > std::numeric_limits::max() / 2) + if (data.size() > std::numeric_limits::max() / 2) { throw std::overflow_error("hex::sizeEncoded: overflow"); } return data.size() * 2; } - uint64_t sizeDecoded(std::string_view str) noexcept + size_t sizeDecoded(std::string_view str) noexcept { return str.size() / 2; } - void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) { if (str_size < hex::sizeEncoded(std::span(data, data_size))) { throw std::length_error("hex::encode: not enough allocated length"); } - for (uint64_t i = 0; i < data_size; i++) + for (size_t i = 0; i < data_size; i++) { str[i * 2] = digits[data[i] >> 4]; str[i * 2 + 1] = digits[data[i] & 0x0F]; @@ -64,7 +64,7 @@ namespace hex hex::encode(data.data(), data.size(), str.data(), str.size()); return str; } - void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) + void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) { if (str_size % 2 != 0) { @@ -78,7 +78,7 @@ namespace hex { throw std::logic_error("hex::decode: out of digits map"); } - for (uint64_t i = 0; i * 2 < str_size; i++) + 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]]; } diff --git a/test/hash/test-sha256.cpp b/test/hash/test-sha256.cpp index 0f2dc9b..2bc8c02 100644 --- a/test/hash/test-sha256.cpp +++ b/test/hash/test-sha256.cpp @@ -11,7 +11,7 @@ TEST(hash, sha256) TEST(hash, sha256_1e4) { std::vector data(32); - for (uint64_t i = 0; i < 1e4; i++) + for (size_t i = 0; i < 1e4; i++) { sha256(data); } diff --git a/test/test-base64.cpp b/test/test-base64.cpp index 680d788..1b7aa3d 100644 --- a/test/test-base64.cpp +++ b/test/test-base64.cpp @@ -31,7 +31,7 @@ TEST(base64, encode) std::vector data = {0x74, 0x65, 0x73, 0x74}; std::string str = ""; - EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); + EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error); EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size())); } diff --git a/test/test-baseN.cpp b/test/test-baseN.cpp index 10a35d5..0ae97fd 100644 --- a/test/test-baseN.cpp +++ b/test/test-baseN.cpp @@ -26,7 +26,7 @@ TEST(baseN, isValid) } TEST(baseN, sizeEncoded) { - std::vector> tests = { + std::vector> tests = { {6, "12341234"}, {5, "00000000"}, }; @@ -35,7 +35,7 @@ TEST(baseN, sizeEncoded) } TEST(baseN, sizeDecoded) { - std::vector> tests = { + std::vector> tests = { {3, "qwer"}, {5, "1111"}, }; diff --git a/test/test-hex.cpp b/test/test-hex.cpp index fb8dc79..30a739c 100644 --- a/test/test-hex.cpp +++ b/test/test-hex.cpp @@ -20,7 +20,7 @@ TEST(hex, encode) EXPECT_EQ("74657374", encode(data)); std::string str = ""; - EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); + EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error); EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size())); }