3 base64.hpp is a repackaging of the base64.cpp and base64.h files into a
4 single header suitable for use as a header only library. This conversion was
5 done by Peter Thorson (webmaster@zaphoyd.com) in 2012. All modifications to
6 the code are redistributed under the same license as the original, which is
10 base64.cpp and base64.h
12 Copyright (C) 2004-2008 René Nyffenegger
14 This source code is provided 'as-is', without any express or implied
15 warranty. In no event will the author be held liable for any damages
16 arising from the use of this software.
18 Permission is granted to anyone to use this software for any purpose,
19 including commercial applications, and to alter it and redistribute it
20 freely, subject to the following restrictions:
22 1. The origin of this source code must not be misrepresented; you must not
23 claim that you wrote the original source code. If you use this source code
24 in a product, an acknowledgment in the product documentation would be
25 appreciated but is not required.
27 2. Altered source versions must be plainly marked as such, and must not be
28 misrepresented as being the original source code.
30 3. This notice may not be removed or altered from any source distribution.
32 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
41 namespace websocketpp {
43 static std::string const base64_chars =
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 "abcdefghijklmnopqrstuvwxyz"
48 /// Test whether a character is a valid base64 character
50 * @param c The character to test
51 * @return true if c is a valid base64 character
53 static inline bool is_base64(unsigned char c) {
54 return (c == 43 || // +
55 (c >= 47 && c <= 57) || // /-9
56 (c >= 65 && c <= 90) || // A-Z
57 (c >= 97 && c <= 122)); // a-z
60 /// Encode a char buffer into a base64 string
62 * @param input The input data
63 * @param len The length of input in bytes
64 * @return A base64 encoded string representing input
66 inline std::string base64_encode(unsigned char const * input, size_t len) {
70 unsigned char char_array_3[3];
71 unsigned char char_array_4[4];
74 char_array_3[i++] = *(input++);
76 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
77 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
78 ((char_array_3[1] & 0xf0) >> 4);
79 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
80 ((char_array_3[2] & 0xc0) >> 6);
81 char_array_4[3] = char_array_3[2] & 0x3f;
83 for(i = 0; (i <4) ; i++) {
84 ret += base64_chars[char_array_4[i]];
91 for(j = i; j < 3; j++) {
92 char_array_3[j] = '\0';
95 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
96 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
97 ((char_array_3[1] & 0xf0) >> 4);
98 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
99 ((char_array_3[2] & 0xc0) >> 6);
100 char_array_4[3] = char_array_3[2] & 0x3f;
102 for (j = 0; (j < i + 1); j++) {
103 ret += base64_chars[char_array_4[j]];
114 /// Encode a string into a base64 string
116 * @param input The input data
117 * @return A base64 encoded string representing input
119 inline std::string base64_encode(std::string const & input) {
120 return base64_encode(
121 reinterpret_cast<const unsigned char *>(input.data()),
126 /// Decode a base64 encoded string into a string of raw bytes
128 * @param input The base64 encoded input data
129 * @return A string representing the decoded raw bytes
131 inline std::string base64_decode(std::string const & input) {
132 size_t in_len = input.size();
136 unsigned char char_array_4[4], char_array_3[3];
139 while (in_len-- && ( input[in_] != '=') && is_base64(input[in_])) {
140 char_array_4[i++] = input[in_]; in_++;
142 for (i = 0; i <4; i++) {
143 char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
146 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
147 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
148 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
150 for (i = 0; (i < 3); i++) {
151 ret += char_array_3[i];
158 for (j = i; j <4; j++)
161 for (j = 0; j <4; j++)
162 char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
164 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
165 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
166 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
168 for (j = 0; (j < i - 1); j++) {
169 ret += static_cast<std::string::value_type>(char_array_3[j]);
176 } // namespace websocketpp
178 #endif // _BASE64_HPP_