mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 13:03:36 +00:00
committed by
Konstantin Pastbin
parent
c9cbb64f12
commit
76ffc99abd
41
libs/coding/base64.cpp
Normal file
41
libs/coding/base64.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "coding/base64.hpp"
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wreorder"
|
||||
#elif defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-local-typedef"
|
||||
#endif
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/archive/iterators/base64_from_binary.hpp>
|
||||
#include <boost/archive/iterators/binary_from_base64.hpp>
|
||||
#include <boost/archive/iterators/transform_width.hpp>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#elif defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace base64
|
||||
{
|
||||
// From: http://stackoverflow.com/a/28471421
|
||||
|
||||
std::string Decode(const std::string & val)
|
||||
{
|
||||
using namespace boost::archive::iterators;
|
||||
using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
|
||||
return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))),
|
||||
[](char c) { return c == '\0'; });
|
||||
}
|
||||
|
||||
std::string Encode(std::string_view val)
|
||||
{
|
||||
using namespace boost::archive::iterators;
|
||||
using It = base64_from_binary<transform_width<std::string_view::const_iterator, 6, 8>>;
|
||||
auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
|
||||
return tmp.append((3 - val.size() % 3) % 3, '=');
|
||||
}
|
||||
} // namespace base64
|
||||
Reference in New Issue
Block a user