]> git.sesse.net Git - casparcg/blob - common/base64.cpp
[ffmpeg_producer] Constrain SEEK values
[casparcg] / common / base64.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@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 #include <cstring>
29
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>
37
38 #include "except.h"
39
40 namespace caspar {
41
42 std::string to_base64(const char* data, size_t length)
43 {
44         using namespace boost::archive::iterators;
45
46         // From http://www.webbiscuit.co.uk/2012/04/02/base64-encoder-and-boost/
47                 
48         typedef
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 *,
53                                         6,
54                                         8
55                                 >
56                         >,
57                         76
58                 >
59                 base64_iterator; // compose all the above operations in to a new iterator
60         std::vector<char> bytes;
61         bytes.resize(length);
62         std::memcpy(bytes.data(), data, length);
63
64         int padding = 0;
65
66         while (bytes.size() % 3 != 0)
67         {
68                 ++padding;
69                 bytes.push_back(0x00);
70         }
71
72         std::string result(base64_iterator(bytes.data()), base64_iterator(bytes.data() + length));
73         result.insert(result.end(), padding, '=');
74
75         return std::move(result);
76 }
77
78 std::vector<unsigned char> from_base64(const std::string& data)
79 {
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(
83                         data.begin(),
84                         data.end(),
85                         [] (char c) { return !std::isspace(static_cast<unsigned char>(c)); });
86
87         if (length % 4 != 0)
88                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
89                                 "The length of a base64 sequence must be a multiple of 4"));
90
91         int padding = 0;
92         std::string zero_padding;
93
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)
96         {
97                 if (data[data.length() - 1] == '=')
98                 {
99                         ++padding;
100                         zero_padding += 'A';
101                 }
102
103                 if (data[data.length() - 2] == '=')
104                 {
105                         ++padding;
106                         zero_padding += 'A';
107                 }
108         }
109
110         if (padding > 0)
111         {
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()));
115
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())>
120                                 >,
121                                 8,
122                                 6
123                         > base64_iterator;
124
125                 std::vector<unsigned char> result(base64_iterator(concatenated.begin()), base64_iterator(concatenated.end()));
126
127                 result.resize(result.size() - padding);
128
129                 return std::move(result);
130         }
131         else
132         {
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>
137                                 >,
138                                 8,
139                                 6
140                         > base64_iterator;
141                 std::vector<unsigned char> result(base64_iterator(data.begin()), base64_iterator(data.end()));
142
143                 return std::move(result);
144         }
145 }
146
147 }