]> git.sesse.net Git - casparcg/blob - common/utility/base64.cpp
Merge branch 'master' of https://github.com/ronag/Server
[casparcg] / common / utility / base64.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../stdafx.h"
23
24 #include "base64.h"
25
26 #include <vector>
27 #include <algorithm>
28
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>
36
37 #include "../exception/exceptions.h"
38
39 namespace caspar {
40
41 std::string to_base64(const char* data, size_t length)
42 {
43         using namespace boost::archive::iterators;
44
45         // From http://www.webbiscuit.co.uk/2012/04/02/base64-encoder-and-boost/
46                 
47         typedef
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 *,
52                                         6,
53                                         8
54                                 >
55                         >,
56                         76
57                 >
58         base64_iterator; // compose all the above operations in to a new iterator
59         std::vector<char> bytes;
60         bytes.resize(length);
61         std::memcpy(bytes.data(), data, length);
62
63         int padding = 0;
64
65         while (bytes.size() % 3 != 0)
66         {
67                 ++padding;
68                 bytes.push_back(0x00);
69         }
70
71         std::string result(base64_iterator(bytes.data()), base64_iterator(bytes.data() + length - padding));
72         result.insert(result.end(), padding, '=');
73
74         return std::move(result);
75 }
76
77 std::vector<unsigned char> from_base64(const std::string& data)
78 {
79         int padding = 0;
80         std::string zero_padding;
81
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)
84         {
85                 if (data[data.length() - 1] == '=')
86                 {
87                         ++padding;
88                         zero_padding += 'A';
89                 }
90
91                 if (data[data.length() - 2] == '=')
92                 {
93                         ++padding;
94                         zero_padding += 'A';
95                 }
96         }
97
98         if (padding > 0)
99         {
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()));
103
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())>
108                                 >,
109                                 8,
110                                 6
111                         > base64_iterator;
112
113                 std::vector<unsigned char> result(base64_iterator(concatenated.begin()), base64_iterator(concatenated.end()));
114
115                 result.resize(result.size() - padding);
116
117                 return std::move(result);
118         }
119         else
120         {
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>
125                                 >,
126                                 8,
127                                 6
128                         > base64_iterator;
129                 std::vector<unsigned char> result(base64_iterator(data.begin()), base64_iterator(data.end()));
130
131                 return std::move(result);
132         }
133 }
134
135 }