feat(base64): isValid
This commit is contained in:
2
Makefile
2
Makefile
@ -10,6 +10,7 @@ LIB = base
|
||||
OBJS =\
|
||||
hex\
|
||||
baseN\
|
||||
base64\
|
||||
hash/sha256\
|
||||
|
||||
TOOLS =\
|
||||
@ -17,6 +18,7 @@ TOOLS =\
|
||||
TESTS =\
|
||||
test-hex\
|
||||
test-baseN\
|
||||
test-base64\
|
||||
hash/test-sha256\
|
||||
|
||||
ifeq (${origin CC}, default)
|
||||
|
||||
18
include/base/base64.hpp
Normal file
18
include/base/base64.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace base64
|
||||
{
|
||||
bool isValid(const char *str) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
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);
|
||||
}
|
||||
@ -9,8 +9,10 @@ namespace hex
|
||||
{
|
||||
bool isValid(const char *str) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
void 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;
|
||||
|
||||
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);
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
#include <base/hex.hpp>
|
||||
#include <base/baseN.hpp>
|
||||
#include <base/base64.hpp>
|
||||
#include <base/hash/sha256.hpp>
|
||||
59
src/base64.cpp
Normal file
59
src/base64.cpp
Normal 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)
|
||||
// {
|
||||
// }
|
||||
}
|
||||
37
test/test-base64.cpp
Normal file
37
test/test-base64.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <base/base64.hpp>
|
||||
#include <base/hex.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace base64;
|
||||
|
||||
TEST(base64, isValid)
|
||||
{
|
||||
EXPECT_TRUE(isValid("12=="));
|
||||
EXPECT_TRUE(isValid("123="));
|
||||
EXPECT_TRUE(isValid("1234"));
|
||||
|
||||
EXPECT_FALSE(isValid("1==="));
|
||||
EXPECT_FALSE(isValid("?!*"));
|
||||
}
|
||||
// TEST(base64, encode)
|
||||
// {
|
||||
// }
|
||||
// TEST(base64, encode_1e6)
|
||||
// {
|
||||
// std::vector<uint8_t> data(1e6);
|
||||
// encode(data);
|
||||
// }
|
||||
// TEST(base64, decode)
|
||||
// {
|
||||
// }
|
||||
// TEST(base64, decode_1e6)
|
||||
// {
|
||||
// std::string str(1e6, '0');
|
||||
// decode(str);
|
||||
// }
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user