feat(base64): isValid

This commit is contained in:
2024-09-16 17:56:04 +03:00
parent 66cd56fe95
commit 891e0e77d8
6 changed files with 119 additions and 0 deletions

59
src/base64.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <algorithm>
#include <stdexcept>
#include <base/base64.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
{
bool isValid(const char *str) noexcept
{
return base64::isValid(std::string_view(str));
}
bool isValid(std::string_view str) noexcept
{
std::string_view sv(str.begin(), std::find_if(str.rbegin(), str.rend(), [](char ch)
{ return ch != '='; })
.base());
if (2 + sv.size() < str.size())
{
return false;
}
return baseN::isValid(sv, b64map);
}
// void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept
// {
// }
// std::string encode(std::span<const uint8_t> data) noexcept
// {
// }
// void decode(const char *str, uint8_t *data, uint64_t data_size)
// {
// }
// std::vector<uint8_t> decode(std::string_view str)
// {
// }
}