feat(base64, hex): alphabet moved to public interface as extern var
This commit is contained in:
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -5,10 +5,11 @@
|
|||||||
#include <base/base64.hpp>
|
#include <base/base64.hpp>
|
||||||
#include <base/baseN.hpp>
|
#include <base/baseN.hpp>
|
||||||
|
|
||||||
static const char b64digits[] =
|
namespace base64
|
||||||
|
{
|
||||||
|
const char digits[] =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
const int8_t map[] = {
|
||||||
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, -1, -1, -1, -1, -1,
|
-1, -1, -1, -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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
|
||||||
@ -26,10 +27,7 @@ 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, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
//
|
//
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace base64
|
|
||||||
{
|
|
||||||
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;
|
||||||
|
|||||||
22
src/hex.cpp
22
src/hex.cpp
@ -4,9 +4,10 @@
|
|||||||
#include <base/baseN.hpp>
|
#include <base/baseN.hpp>
|
||||||
#include <base/hex.hpp>
|
#include <base/hex.hpp>
|
||||||
|
|
||||||
static const char hexdigits[] = "0123456789abcdef";
|
namespace hex
|
||||||
|
{
|
||||||
static const int8_t hexmap[] = {
|
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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
@ -24,17 +25,14 @@ 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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
//
|
//
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace hex
|
|
||||||
{
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user