feat(base64, hex): alphabet moved to public interface as extern var

This commit is contained in:
2024-09-19 00:05:35 +03:00
parent 5822443706
commit f2119850ee
4 changed files with 68 additions and 66 deletions

View File

@ -7,6 +7,9 @@
namespace base64 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, uint64_t str_size) noexcept;
bool isValid(std::string_view str) noexcept; bool isValid(std::string_view str) noexcept;

View File

@ -7,6 +7,9 @@
namespace hex 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, uint64_t str_size) noexcept;
bool isValid(std::string_view str) noexcept; bool isValid(std::string_view str) noexcept;

View File

@ -5,31 +5,29 @@
#include <base/base64.hpp> #include <base/base64.hpp>
#include <base/baseN.hpp> #include <base/baseN.hpp>
static const char b64digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int8_t b64map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -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,
//
};
namespace base64 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,
//
};
bool isValid(const char *str, uint64_t str_size) noexcept bool isValid(const char *str, uint64_t str_size) noexcept
{ {
return base64::isValid(std::string_view(str, str_size)); return base64::isValid(std::string_view(str, str_size));
@ -43,7 +41,7 @@ namespace base64
{ {
return false; return false;
} }
return baseN::isValid(sv, b64map); return baseN::isValid(sv, map);
} }
uint64_t sizeEncoded(std::span<const uint8_t> data) uint64_t sizeEncoded(std::span<const uint8_t> data)
{ {
@ -70,10 +68,10 @@ namespace base64
} }
for (uint64_t i = 0; i < data_size / 3; i++) for (uint64_t i = 0; i < data_size / 3; i++)
{ {
str[i * 4] = b64digits[data[i * 3] >> 2]; str[i * 4] = digits[data[i * 3] >> 2];
str[i * 4 + 1] = b64digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F]; str[i * 4 + 1] = digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F];
str[i * 4 + 2] = b64digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F]; str[i * 4 + 2] = digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F];
str[i * 4 + 3] = b64digits[data[i * 3 + 2] & 0x3F]; str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F];
} }
uint64_t last_idx = data_size / 3 * 4; uint64_t last_idx = data_size / 3 * 4;
if (last_idx + 3 < str_size) if (last_idx + 3 < str_size)
@ -81,15 +79,15 @@ namespace base64
switch (data_size % 3) switch (data_size % 3)
{ {
case 1: case 1:
str[last_idx] = b64digits[data[data_size - 1] >> 2]; str[last_idx] = digits[data[data_size - 1] >> 2];
str[last_idx + 1] = b64digits[data[data_size - 1] << 4 & 0x30]; str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30];
str[last_idx + 2] = '='; str[last_idx + 2] = '=';
str[last_idx + 3] = '='; str[last_idx + 3] = '=';
break; break;
case 2: case 2:
str[last_idx] = b64digits[data[data_size - 2] >> 2]; str[last_idx] = digits[data[data_size - 2] >> 2];
str[last_idx + 1] = b64digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F]; str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
str[last_idx + 2] = b64digits[data[data_size - 1] & 0x0F]; str[last_idx + 2] = digits[data[data_size - 1] & 0x0F];
str[last_idx + 3] = '='; str[last_idx + 3] = '=';
break; break;
default: default:
@ -123,19 +121,19 @@ namespace base64
} }
for (auto i = 0; i < size / 4; i++) for (auto i = 0; i < size / 4; i++)
{ {
data[i * 3] = b64map[(int8_t)str[i * 4]] << 2 | b64map[(int8_t)str[i * 4 + 1]] >> 4; data[i * 3] = map[(int8_t)str[i * 4]] << 2 | map[(int8_t)str[i * 4 + 1]] >> 4;
data[i * 3 + 1] = b64map[(int8_t)str[i * 4 + 1]] << 4 | b64map[(int8_t)str[i * 4 + 2]] >> 2; data[i * 3 + 1] = map[(int8_t)str[i * 4 + 1]] << 4 | map[(int8_t)str[i * 4 + 2]] >> 2;
data[i * 3 + 2] = b64map[(int8_t)str[i * 4 + 2]] << 6 | b64map[(int8_t)str[i * 4 + 3]]; 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; uint64_t last_idx = size / 4 * 3;
switch (size % 4) switch (size % 4)
{ {
case 2: case 2:
data[last_idx] = b64map[(int8_t)str[size - 2]] << 2 | b64map[(int8_t)str[size - 1]] >> 4; data[last_idx] = map[(int8_t)str[size - 2]] << 2 | map[(int8_t)str[size - 1]] >> 4;
break; break;
case 3: case 3:
data[last_idx] = b64map[(int8_t)str[size - 3]] << 2 | b64map[(int8_t)str[size - 2]] >> 4; data[last_idx] = map[(int8_t)str[size - 3]] << 2 | map[(int8_t)str[size - 2]] >> 4;
data[last_idx + 1] = b64map[(int8_t)str[size - 2]] << 4 | b64map[(int8_t)str[size - 1]] >> 2; data[last_idx + 1] = map[(int8_t)str[size - 2]] << 4 | map[(int8_t)str[size - 1]] >> 2;
break; break;
default: default:
break; break;

View File

@ -4,37 +4,35 @@
#include <base/baseN.hpp> #include <base/baseN.hpp>
#include <base/hex.hpp> #include <base/hex.hpp>
static const char hexdigits[] = "0123456789abcdef";
static const int8_t hexmap[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -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,
//
};
namespace hex 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,
//
};
bool isValid(const char *str, uint64_t str_size) noexcept bool isValid(const char *str, uint64_t str_size) noexcept
{ {
return baseN::isValid(str, str_size, hexmap); return baseN::isValid(str, str_size, map);
} }
bool isValid(std::string_view str) noexcept bool isValid(std::string_view str) noexcept
{ {
return baseN::isValid(str, hexmap); return baseN::isValid(str, map);
} }
uint64_t sizeEncoded(std::span<const uint8_t> data) uint64_t sizeEncoded(std::span<const uint8_t> data)
{ {
@ -56,8 +54,8 @@ namespace hex
} }
for (uint64_t i = 0; i < data_size; i++) for (uint64_t i = 0; i < data_size; i++)
{ {
str[i * 2] = hexdigits[data[i] >> 4]; str[i * 2] = digits[data[i] >> 4];
str[i * 2 + 1] = hexdigits[data[i] & 0x0F]; str[i * 2 + 1] = digits[data[i] & 0x0F];
} }
} }
std::string encode(std::span<const uint8_t> data) noexcept std::string encode(std::span<const uint8_t> data) noexcept
@ -82,7 +80,7 @@ namespace hex
} }
for (uint64_t i = 0; i * 2 < str_size; i++) for (uint64_t i = 0; i * 2 < str_size; i++)
{ {
data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]; data[i] = map[(int8_t)str[i * 2]] << 4 | map[(int8_t)str[i * 2 + 1]];
} }
} }
std::vector<uint8_t> decode(std::string_view str) noexcept std::vector<uint8_t> decode(std::string_view str) noexcept