2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
30 #include <boost/archive/iterators/insert_linebreaks.hpp>
31 #include <boost/archive/iterators/base64_from_binary.hpp>
32 #include <boost/archive/iterators/binary_from_base64.hpp>
33 #include <boost/archive/iterators/transform_width.hpp>
34 #include <boost/archive/iterators/remove_whitespace.hpp>
35 #include <boost/range/join.hpp>
36 #include <boost/range/adaptor/sliced.hpp>
42 std::string to_base64(const char* data, size_t length)
44 using namespace boost::archive::iterators;
46 // From http://www.webbiscuit.co.uk/2012/04/02/base64-encoder-and-boost/
49 insert_linebreaks< // insert line breaks every 76 characters
50 base64_from_binary< // convert binary values to base64 characters
51 transform_width< // retrieve 6 bit integers from a sequence of 8 bit bytes
52 const unsigned char *,
59 base64_iterator; // compose all the above operations in to a new iterator
60 std::vector<char> bytes;
62 std::memcpy(bytes.data(), data, length);
66 while (bytes.size() % 3 != 0)
69 bytes.push_back(0x00);
72 std::string result(base64_iterator(bytes.data()), base64_iterator(bytes.data() + length));
73 result.insert(result.end(), padding, '=');
75 return std::move(result);
78 std::vector<unsigned char> from_base64(const std::string& data)
80 // The boost base64 iterator will over-iterate the string if not a multiple
81 // of 4, so we have to short circuit before.
82 auto length = std::count_if(
85 [] (char c) { return !std::isspace(static_cast<unsigned char>(c)); });
88 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(
89 "The length of a base64 sequence must be a multiple of 4"));
92 std::string zero_padding;
94 // binary_from_base64 does not support padding characters so we have to append base64 0 -> 'A' and then remove it after decoding
95 if (data.length() >= 2)
97 if (data[data.length() - 1] == '=')
103 if (data[data.length() - 2] == '=')
112 auto concatenated = boost::join(
113 data | boost::adaptors::sliced(0, data.length() - padding),
114 boost::make_iterator_range(zero_padding.cbegin(), zero_padding.cend()));
116 // From https://svn.boost.org/trac/boost/ticket/5624
117 typedef boost::archive::iterators::transform_width<
118 boost::archive::iterators::binary_from_base64<
119 boost::archive::iterators::remove_whitespace<decltype(concatenated.begin())>
125 std::vector<unsigned char> result(base64_iterator(concatenated.begin()), base64_iterator(concatenated.end()));
127 result.resize(result.size() - padding);
129 return std::move(result);
133 // From https://svn.boost.org/trac/boost/ticket/5624
134 typedef boost::archive::iterators::transform_width<
135 boost::archive::iterators::binary_from_base64<
136 boost::archive::iterators::remove_whitespace<std::string::const_iterator>
141 std::vector<unsigned char> result(base64_iterator(data.begin()), base64_iterator(data.end()));
143 return std::move(result);