websocketpp  0.3.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 static inline bool is_base64(unsigned char c) {
49  return (c == 43 || // +
50  (c >= 47 && c <= 57) || // /-9
51  (c >= 65 && c <= 90) || // A-Z
52  (c >= 97 && c <= 122)); // a-z
53 }
54 
55 inline std::string base64_encode(unsigned char const * bytes_to_encode, unsigned
56  int in_len)
57 {
58  std::string ret;
59  int i = 0;
60  int j = 0;
61  unsigned char char_array_3[3];
62  unsigned char char_array_4[4];
63 
64  while (in_len--) {
65  char_array_3[i++] = *(bytes_to_encode++);
66  if (i == 3) {
67  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
68  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
69  ((char_array_3[1] & 0xf0) >> 4);
70  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
71  ((char_array_3[2] & 0xc0) >> 6);
72  char_array_4[3] = char_array_3[2] & 0x3f;
73 
74  for(i = 0; (i <4) ; i++) {
75  ret += base64_chars[char_array_4[i]];
76  }
77  i = 0;
78  }
79  }
80 
81  if (i) {
82  for(j = i; j < 3; j++) {
83  char_array_3[j] = '\0';
84  }
85 
86  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
87  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
88  ((char_array_3[1] & 0xf0) >> 4);
89  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
90  ((char_array_3[2] & 0xc0) >> 6);
91  char_array_4[3] = char_array_3[2] & 0x3f;
92 
93  for (j = 0; (j < i + 1); j++) {
94  ret += base64_chars[char_array_4[j]];
95  }
96 
97  while((i++ < 3)) {
98  ret += '=';
99  }
100  }
101 
102  return ret;
103 }
104 
105 inline std::string base64_encode(std::string const & data) {
106  return base64_encode(reinterpret_cast<const unsigned char *>(data.data()),data.size());
107 }
108 
109 inline std::string base64_decode(std::string const & encoded_string) {
110  size_t in_len = encoded_string.size();
111  int i = 0;
112  int j = 0;
113  int in_ = 0;
114  unsigned char char_array_4[4], char_array_3[3];
115  std::string ret;
116 
117  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
118  char_array_4[i++] = encoded_string[in_]; in_++;
119  if (i ==4) {
120  for (i = 0; i <4; i++) {
121  char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
122  }
123 
124  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
125  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
126  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
127 
128  for (i = 0; (i < 3); i++) {
129  ret += char_array_3[i];
130  }
131  i = 0;
132  }
133  }
134 
135  if (i) {
136  for (j = i; j <4; j++)
137  char_array_4[j] = 0;
138 
139  for (j = 0; j <4; j++)
140  char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
141 
142  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
143  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
144  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
145 
146  for (j = 0; (j < i - 1); j++) {
147  ret += static_cast<std::string::value_type>(char_array_3[j]);
148  }
149  }
150 
151  return ret;
152 }
153 
154 } // namespace websocketpp
155 
156 #endif // _BASE64_HPP_
Namespace for the WebSocket++ project.
Definition: base64.hpp:41