WebSocket++  0.8.3-dev
C++ websocket client/server library
base64.hpp
1 /*
2  ******
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
7  listed below.
8  ******
9 
10  base64.cpp and base64.h
11 
12  Copyright (C) 2004-2008 RenĂ© Nyffenegger
13 
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.
17 
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:
21 
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.
26 
27  2. Altered source versions must be plainly marked as such, and must not be
28  misrepresented as being the original source code.
29 
30  3. This notice may not be removed or altered from any source distribution.
31 
32  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
33 
34 */
35 
36 #ifndef _BASE64_HPP_
37 #define _BASE64_HPP_
38 
39 #include <string>
40 
41 namespace websocketpp {
42 
43 static std::string const base64_chars =
44  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45  "abcdefghijklmnopqrstuvwxyz"
46  "0123456789+/";
47 
48 /// Test whether a character is a valid base64 character
49 /**
50  * @param c The character to test
51  * @return true if c is a valid base64 character
52  */
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
58 }
59 
60 /// Encode a char buffer into a base64 string
61 /**
62  * @param input The input data
63  * @param len The length of input in bytes
64  * @return A base64 encoded string representing input
65  */
66 inline std::string base64_encode(unsigned char const * input, size_t len) {
67  std::string ret;
68  int i = 0;
69  int j = 0;
70  unsigned char char_array_3[3];
71  unsigned char char_array_4[4];
72 
73  while (len--) {
74  char_array_3[i++] = *(input++);
75  if (i == 3) {
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;
82 
83  for(i = 0; (i <4) ; i++) {
84  ret += base64_chars[char_array_4[i]];
85  }
86  i = 0;
87  }
88  }
89 
90  if (i) {
91  for(j = i; j < 3; j++) {
92  char_array_3[j] = '\0';
93  }
94 
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;
101 
102  for (j = 0; (j < i + 1); j++) {
103  ret += base64_chars[char_array_4[j]];
104  }
105 
106  while((i++ < 3)) {
107  ret += '=';
108  }
109  }
110 
111  return ret;
112 }
113 
114 /// Encode a string into a base64 string
115 /**
116  * @param input The input data
117  * @return A base64 encoded string representing input
118  */
119 inline std::string base64_encode(std::string const & input) {
120  return base64_encode(
121  reinterpret_cast<const unsigned char *>(input.data()),
122  input.size()
123  );
124 }
125 
126 /// Decode a base64 encoded string into a string of raw bytes
127 /**
128  * @param input The base64 encoded input data
129  * @return A string representing the decoded raw bytes
130  */
131 inline std::string base64_decode(std::string const & input) {
132  size_t in_len = input.size();
133  int i = 0;
134  int j = 0;
135  int in_ = 0;
136  unsigned char char_array_4[4], char_array_3[3];
137  std::string ret;
138 
139  while (in_len-- && ( input[in_] != '=') && is_base64(input[in_])) {
140  char_array_4[i++] = input[in_]; in_++;
141  if (i ==4) {
142  for (i = 0; i <4; i++) {
143  char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
144  }
145 
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];
149 
150  for (i = 0; (i < 3); i++) {
151  ret += char_array_3[i];
152  }
153  i = 0;
154  }
155  }
156 
157  if (i) {
158  for (j = i; j <4; j++)
159  char_array_4[j] = 0;
160 
161  for (j = 0; j <4; j++)
162  char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
163 
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];
167 
168  for (j = 0; (j < i - 1); j++) {
169  ret += static_cast<std::string::value_type>(char_array_3[j]);
170  }
171  }
172 
173  return ret;
174 }
175 
176 } // namespace websocketpp
177 
178 #endif // _BASE64_HPP_
websocketpp::base64_encode
std::string base64_encode(std::string const &input)
Encode a string into a base64 string.
Definition: base64.hpp:119
websocketpp::base64_decode
std::string base64_decode(std::string const &input)
Decode a base64 encoded string into a string of raw bytes.
Definition: base64.hpp:131
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::is_base64
static bool is_base64(unsigned char c)
Test whether a character is a valid base64 character.
Definition: base64.hpp:53
websocketpp::base64_encode
std::string base64_encode(unsigned char const *input, size_t len)
Encode a char buffer into a base64 string.
Definition: base64.hpp:66