2 * Copyright 2013 Sveriges Television AB http://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
22 #include "../stdafx.h"
29 #include <boost/archive/iterators/insert_linebreaks.hpp>
30 #include <boost/archive/iterators/base64_from_binary.hpp>
31 #include <boost/archive/iterators/binary_from_base64.hpp>
32 #include <boost/archive/iterators/transform_width.hpp>
33 #include <boost/archive/iterators/remove_whitespace.hpp>
34 #include <boost/range/join.hpp>
35 #include <boost/range/adaptor/sliced.hpp>
37 #include "../exception/exceptions.h"
41 std::string to_base64(const char* data, size_t length)
43 using namespace boost::archive::iterators;
45 // From http://www.webbiscuit.co.uk/2012/04/02/base64-encoder-and-boost/
48 insert_linebreaks< // insert line breaks every 76 characters
49 base64_from_binary< // convert binary values to base64 characters
50 transform_width< // retrieve 6 bit integers from a sequence of 8 bit bytes
51 const unsigned char *,
58 base64_iterator; // compose all the above operations in to a new iterator
59 std::vector<char> bytes;
61 std::memcpy(bytes.data(), data, length);
65 while (bytes.size() % 3 != 0)
68 bytes.push_back(0x00);
71 std::string result(base64_iterator(bytes.data()), base64_iterator(bytes.data() + length - padding));
72 result.insert(result.end(), padding, '=');
74 return std::move(result);
77 std::vector<unsigned char> from_base64(const std::string& data)
80 std::string zero_padding;
82 // binary_from_base64 does not support padding characters so we have to append base64 0 -> 'A' and then remove it after decoding
83 if (data.length() >= 2)
85 if (data[data.length() - 1] == '=')
91 if (data[data.length() - 2] == '=')
100 auto concatenated = boost::join(
101 data | boost::adaptors::sliced(0, data.length() - padding),
102 boost::make_iterator_range(zero_padding.cbegin(), zero_padding.cend()));
104 // From https://svn.boost.org/trac/boost/ticket/5624
105 typedef boost::archive::iterators::transform_width<
106 boost::archive::iterators::binary_from_base64<
107 boost::archive::iterators::remove_whitespace<decltype(concatenated.begin())>
113 std::vector<unsigned char> result(base64_iterator(concatenated.begin()), base64_iterator(concatenated.end()));
115 result.resize(result.size() - padding);
117 return std::move(result);
121 // From https://svn.boost.org/trac/boost/ticket/5624
122 typedef boost::archive::iterators::transform_width<
123 boost::archive::iterators::binary_from_base64<
124 boost::archive::iterators::remove_whitespace<std::string::const_iterator>
129 std::vector<unsigned char> result(base64_iterator(data.begin()), base64_iterator(data.end()));
131 return std::move(result);