2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BEAST_DETAIL_BASE64_HPP
9 #define BEAST_DETAIL_BASE64_HPP
18 Portions from http://www.adp-gmbh.ch/cpp/common/base64.html
21 base64.cpp and base64.h
23 Copyright (C) 2004-2008 René Nyffenegger
25 This source code is provided 'as-is', without any express or implied
26 warranty. In no event will the author be held liable for any damages
27 arising from the use of this software.
29 Permission is granted to anyone to use this software for any purpose,
30 including commercial applications, and to alter it and redistribute it
31 freely, subject to the following restrictions:
33 1. The origin of this source code must not be misrepresented; you must not
34 claim that you wrote the original source code. If you use this source code
35 in a product, an acknowledgment in the product documentation would be
36 appreciated but is not required.
38 2. Altered source versions must be plainly marked as such, and must not be
39 misrepresented as being the original source code.
41 3. This notice may not be removed or altered from any source distribution.
43 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
47 template<class = void>
51 static std::string const alphabet =
52 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53 "abcdefghijklmnopqrstuvwxyz"
60 is_base64(unsigned char c)
62 return (std::isalnum(c) || (c == '+') || (c == '/'));
65 template<class = void>
67 base64_encode (std::uint8_t const* data,
70 unsigned char c3[3], c4[4];
75 ret.reserve (3 + in_len * 8 / 6);
77 char const* alphabet (base64_alphabet().data());
84 c4[0] = (c3[0] & 0xfc) >> 2;
85 c4[1] = ((c3[0] & 0x03) << 4) + ((c3[1] & 0xf0) >> 4);
86 c4[2] = ((c3[1] & 0x0f) << 2) + ((c3[2] & 0xc0) >> 6);
88 for(i = 0; (i < 4); i++)
89 ret += alphabet[c4[i]];
96 for(j = i; j < 3; j++)
99 c4[0] = (c3[0] & 0xfc) >> 2;
100 c4[1] = ((c3[0] & 0x03) << 4) + ((c3[1] & 0xf0) >> 4);
101 c4[2] = ((c3[1] & 0x0f) << 2) + ((c3[2] & 0xc0) >> 6);
102 c4[3] = c3[2] & 0x3f;
104 for(j = 0; (j < i + 1); j++)
105 ret += alphabet[c4[j]];
115 template<class = void>
117 base64_encode (std::string const& s)
119 return base64_encode (reinterpret_cast <
120 std::uint8_t const*> (s.data()), s.size());
123 template<class = void>
125 base64_decode(std::string const& data)
127 auto in_len = data.size();
128 unsigned char c3[3], c4[4];
134 ret.reserve (in_len * 6 / 8); // ???
136 while(in_len-- && (data[in_] != '=') &&
137 is_base64(data[in_]))
139 c4[i++] = data[in_]; in_++;
141 for(i = 0; i < 4; i++)
142 c4[i] = static_cast<unsigned char>(
143 base64_alphabet().find(c4[i]));
145 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
146 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
147 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
149 for(i = 0; (i < 3); i++)
157 for(j = i; j < 4; j++)
160 for(j = 0; j < 4; j++)
161 c4[j] = static_cast<unsigned char>(
162 base64_alphabet().find(c4[j]));
164 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
165 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
166 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
168 for(j = 0; (j < i - 1); j++)