summaryrefslogtreecommitdiffhomepage
path: root/base64.cpp
blob: d72dc5289f7281515ffe03d5bb15c1e3f92effb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "base64.h"

#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>

std::string Reichwein::Base64::decode64(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 Reichwein::Base64::encode64(const std::string &val)
{
 using namespace boost::archive::iterators;
 using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
 auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
 return tmp.append((3 - val.size() % 3) % 3, '=');
}