From 891e0e77d896e10717530db74b104e1680ef8c65 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Mon, 16 Sep 2024 17:56:04 +0300 Subject: [PATCH] feat(base64): isValid --- Makefile | 2 ++ include/base/base64.hpp | 18 +++++++++++++ include/base/hex.hpp | 2 ++ include/libbase | 1 + src/base64.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ test/test-base64.cpp | 37 ++++++++++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 include/base/base64.hpp create mode 100644 src/base64.cpp create mode 100644 test/test-base64.cpp diff --git a/Makefile b/Makefile index 5771ac0..0cdc671 100644 --- a/Makefile +++ b/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) diff --git a/include/base/base64.hpp b/include/base/base64.hpp new file mode 100644 index 0000000..c745128 --- /dev/null +++ b/include/base/base64.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include +#include + +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 data) noexcept; + + void decode(const char *str, uint8_t *data, uint64_t data_size); + std::vector decode(std::string_view str); +} \ No newline at end of file diff --git a/include/base/hex.hpp b/include/base/hex.hpp index ae90df2..f1518c1 100644 --- a/include/base/hex.hpp +++ b/include/base/hex.hpp @@ -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 data) noexcept; + void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size); std::vector decode(std::string_view str); } \ No newline at end of file diff --git a/include/libbase b/include/libbase index 90c42e7..279ac37 100644 --- a/include/libbase +++ b/include/libbase @@ -1,3 +1,4 @@ #include #include +#include #include \ No newline at end of file diff --git a/src/base64.cpp b/src/base64.cpp new file mode 100644 index 0000000..aa98dbd --- /dev/null +++ b/src/base64.cpp @@ -0,0 +1,59 @@ +#include +#include + +#include +#include + +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 data) noexcept + // { + // } + // void decode(const char *str, uint8_t *data, uint64_t data_size) + // { + // } + // std::vector decode(std::string_view str) + // { + // } +} \ No newline at end of file diff --git a/test/test-base64.cpp b/test/test-base64.cpp new file mode 100644 index 0000000..0e053f5 --- /dev/null +++ b/test/test-base64.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +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 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(); +} \ No newline at end of file