Add base64 decoding function

This commit is contained in:
King_DuckZ 2020-08-10 02:35:27 +01:00
parent 6b9502ea3e
commit eeab1d5c5e
6 changed files with 52 additions and 1 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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'],

1
src/gnulib Submodule

@ -0,0 +1 @@
Subproject commit 444acd314f6c4a1fb7ea7412a54b8ea5fc0ad7c2

View file

@ -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,

31
src/oro/base64.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "base64.hpp"
#include <config.h>
#define restrict __restrict__
#include "gnulib/lib/base64.h"
#include <cassert>
#include <stdexcept>
#if defined(base64_decode)
# undef base64_decode
#endif
namespace oro {
std::vector<char> base64_decode (std::string_view text) {
base64_decode_context ctx;
base64_decode_ctx_init(&ctx);
std::vector<char> 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

10
src/oro/base64.hpp Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <string_view>
#include <vector>
namespace oro {
std::vector<char> base64_decode (std::string_view text);
} //namespace oro