fix(types): uint64_t -> size_t

This commit is contained in:
2024-09-30 12:35:59 +03:00
parent be4f2ae036
commit 1d6b086bfa
14 changed files with 60 additions and 60 deletions

View File

@ -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<const uint8_t> data) noexcept;
uint64_t sizeDecoded(std::string_view str) noexcept;
size_t sizeEncoded(std::span<const uint8_t> 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<const uint8_t> 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<uint8_t> decode(std::string_view str) noexcept;
/**

View File

@ -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<const uint8_t> data);
uint64_t sizeDecoded(std::string_view str) noexcept;
size_t sizeEncoded(std::span<const uint8_t> 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<const uint8_t> 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<uint8_t> decode(std::string_view str) noexcept;
}

View File

@ -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<const uint8_t> data, uint8_t base);
size_t sizeEncoded(std::span<const uint8_t> 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

View File

@ -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<uint8_t> sha256(std::span<const uint8_t> data) noexcept;
}

View File

@ -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<const uint8_t> data);
uint64_t sizeDecoded(std::string_view str) noexcept;
size_t sizeEncoded(std::span<const uint8_t> 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<const uint8_t> 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<uint8_t> decode(std::string_view str) noexcept;
}

View File

@ -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<const uint8_t> data) noexcept
size_t sizeEncoded(std::span<const uint8_t> 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);
}

View File

@ -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<const uint8_t> data)
size_t sizeEncoded(std::span<const uint8_t> data)
{
uint64_t str_size = data.size() / 3;
if (str_size > std::numeric_limits<uint64_t>::max() / 4)
size_t str_size = data.size() / 3;
if (str_size > std::numeric_limits<size_t>::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<const uint8_t>(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:

View File

@ -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<const uint8_t> data, uint8_t base)
size_t sizeEncoded(std::span<const uint8_t> data, uint8_t base)
{
std::span<const uint8_t> dv(std::find_if(data.begin(), data.end(), [](uint8_t item)
{ return item != 0; }),
data.end());
if (dv.size() > std::numeric_limits<uint64_t>::max() / log256)
if (dv.size() > std::numeric_limits<size_t>::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<uint8_t> 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<const uint8_t> 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<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept
{
std::vector<uint8_t> 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;
}

View File

@ -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);

View File

@ -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<const uint8_t> data)
size_t sizeEncoded(std::span<const uint8_t> data)
{
if (data.size() > std::numeric_limits<uint64_t>::max() / 2)
if (data.size() > std::numeric_limits<size_t>::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<const uint8_t>(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]];
}

View File

@ -11,7 +11,7 @@ TEST(hash, sha256)
TEST(hash, sha256_1e4)
{
std::vector<uint8_t> data(32);
for (uint64_t i = 0; i < 1e4; i++)
for (size_t i = 0; i < 1e4; i++)
{
sha256(data);
}

View File

@ -31,7 +31,7 @@ TEST(base64, encode)
std::vector<uint8_t> data = {0x74, 0x65, 0x73, 0x74};
std::string str = "";
EXPECT_THROW(encode(data.data(), std::numeric_limits<uint64_t>::max(), str.data(), str.size()), std::overflow_error);
EXPECT_THROW(encode(data.data(), std::numeric_limits<size_t>::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()));
}

View File

@ -26,7 +26,7 @@ TEST(baseN, isValid)
}
TEST(baseN, sizeEncoded)
{
std::vector<std::pair<uint64_t, std::string>> tests = {
std::vector<std::pair<size_t, std::string>> tests = {
{6, "12341234"},
{5, "00000000"},
};
@ -35,7 +35,7 @@ TEST(baseN, sizeEncoded)
}
TEST(baseN, sizeDecoded)
{
std::vector<std::pair<uint64_t, std::string>> tests = {
std::vector<std::pair<size_t, std::string>> tests = {
{3, "qwer"},
{5, "1111"},
};

View File

@ -20,7 +20,7 @@ TEST(hex, encode)
EXPECT_EQ("74657374", encode(data));
std::string str = "";
EXPECT_THROW(encode(data.data(), std::numeric_limits<uint64_t>::max(), str.data(), str.size()), std::overflow_error);
EXPECT_THROW(encode(data.data(), std::numeric_limits<size_t>::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()));
}