]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_b64.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
6 #include <boost/archive/iterators/base64_from_binary.hpp>
7 #include <boost/archive/iterators/binary_from_base64.hpp>
8 #include <boost/archive/iterators/insert_linebreaks.hpp>
9 #include <boost/archive/iterators/transform_width.hpp>
10 #include <boost/archive/iterators/remove_whitespace.hpp>
13 #include <string_view>
18 * A header-only Base64 encoder built on boost::archive. The
19 * formula is based on a class poposed for inclusion in boost in
20 * 2011 by Denis Shevchenko (abandoned), updated slightly
21 * (e.g., uses std::string_view).
23 * Also, wrap_width added as template argument, based on
24 * feedback from Marcus.
27 template<int wrap_width
= std::numeric_limits
<int>::max()>
28 inline std::string
to_base64(std::string_view sview
)
30 using namespace boost::archive::iterators
;
32 // output must be =padded modulo 3
33 auto psize
= sview
.size();
34 while ((psize
% 3) != 0) {
38 /* RFC 2045 requires linebreaks to be present in the output
39 * sequence every at-most 76 characters (MIME-compliance),
40 * but we could likely omit it. */
45 std::string_view::const_iterator
51 std::string
outstr(b64_iter(sview
.data()),
52 b64_iter(sview
.data() + sview
.size()));
54 // pad outstr with '=' to a length that is a multiple of 3
55 for (size_t ix
= 0; ix
< (psize
-sview
.size()); ++ix
)
56 outstr
.push_back('=');
61 inline std::string
from_base64(std::string_view sview
)
63 using namespace boost::archive::iterators
;
66 /* MIME-compliant input will have line-breaks, so we have to
72 std::string_view::const_iterator
>>
76 while (sview
.back() == '=')
77 sview
.remove_suffix(1);
79 std::string
outstr(b64_iter(sview
.data()),
80 b64_iter(sview
.data() + sview
.size()));