From eeab1d5c5ea8d458e2b0594d306988235fffc9a2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 10 Aug 2020 02:35:27 +0100 Subject: [PATCH] Add base64 decoding function --- .gitmodules | 3 +++ meson.build | 2 +- src/gnulib | 1 + src/meson.build | 6 ++++++ src/oro/base64.cpp | 31 +++++++++++++++++++++++++++++++ src/oro/base64.hpp | 10 ++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 160000 src/gnulib create mode 100644 src/oro/base64.cpp create mode 100644 src/oro/base64.hpp diff --git a/.gitmodules b/.gitmodules index 9add61b..614e606 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "subprojects/SQLiteCpp/SQLiteCpp"] path = subprojects/SQLiteCpp/SQLiteCpp url = https://github.com/SRombauts/SQLiteCpp.git +[submodule "src/gnulib"] + path = src/gnulib + url = git://git.savannah.gnu.org/gnulib.git diff --git a/meson.build b/meson.build index 27410b9..66c80b5 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('orotool', 'cpp', +project('orotool', 'cpp', 'c', version: '0.1.0', meson_version: '>=0.49.2', default_options: ['buildtype=debug', 'cpp_std=gnu++17'], diff --git a/src/gnulib b/src/gnulib new file mode 160000 index 0000000..444acd3 --- /dev/null +++ b/src/gnulib @@ -0,0 +1 @@ +Subproject commit 444acd314f6c4a1fb7ea7412a54b8ea5fc0ad7c2 diff --git a/src/meson.build b/src/meson.build index 76e6866..eafcb60 100644 --- a/src/meson.build +++ b/src/meson.build @@ -27,6 +27,10 @@ project_config_file = configure_file( configuration: conf, ) +gnulib_conf = configuration_data() +gnulib_conf.set('_GL_ATTRIBUTE_CONST', '__attribute__ ((__const__))') +configure_file(output : 'config.h', configuration : gnulib_conf) + lib_deps = [ restc_cpp_dep, sqlitecpp_dep, @@ -47,6 +51,8 @@ executable(meson.project_name(), 'timer_items.cpp', 'timer_icons.cpp', 'oro/originsdb.cpp', + 'gnulib/lib/base64.c', + 'oro/base64.cpp', project_config_file, install: true, dependencies: lib_deps, diff --git a/src/oro/base64.cpp b/src/oro/base64.cpp new file mode 100644 index 0000000..71e0043 --- /dev/null +++ b/src/oro/base64.cpp @@ -0,0 +1,31 @@ +#include "base64.hpp" +#include +#define restrict __restrict__ +#include "gnulib/lib/base64.h" +#include +#include + +#if defined(base64_decode) +# undef base64_decode +#endif + +namespace oro { + +std::vector base64_decode (std::string_view text) { + base64_decode_context ctx; + base64_decode_ctx_init(&ctx); + std::vector retval(3 * (text.size() / 4) + 3); + + size_t out_size = retval.size(); + const bool success = base64_decode_ctx(&ctx, text.data(), text.size(), retval.data(), &out_size); + if (not success) + throw std::runtime_error("Base64 decode failed"); + + assert(out_size <= retval.size()); + retval.resize(out_size); + return retval; +} + +} //namespace oro + +#undef restrict diff --git a/src/oro/base64.hpp b/src/oro/base64.hpp new file mode 100644 index 0000000..7b78626 --- /dev/null +++ b/src/oro/base64.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +namespace oro { + +std::vector base64_decode (std::string_view text); + +} //namespace oro