PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source/boost/beast/core/detail/base64.hpp

https://bitbucket.org/liflg/library-boost
C++ Header | 251 lines | 159 code | 29 blank | 63 comment | 10 complexity | c7b178a0dca0f00addda760306faf25a MD5 | raw file
  1. //
  2. // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  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)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. /*
  10. Portions from http://www.adp-gmbh.ch/cpp/common/base64.html
  11. Copyright notice:
  12. base64.cpp and base64.h
  13. Copyright (C) 2004-2008 Rene 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.
  17. Permission is granted to anyone to use this software for any purpose,
  18. including commercial applications, and to alter it and redistribute it
  19. freely, subject to the following restrictions:
  20. 1. The origin of this source code must not be misrepresented; you must not
  21. claim that you wrote the original source code. If you use this source code
  22. in a product, an acknowledgment in the product documentation would be
  23. appreciated but is not required.
  24. 2. Altered source versions must be plainly marked as such, and must not be
  25. misrepresented as being the original source code.
  26. 3. This notice may not be removed or altered from any source distribution.
  27. Rene Nyffenegger rene.nyffenegger@adp-gmbh.ch
  28. */
  29. #ifndef BOOST_BEAST_DETAIL_BASE64_HPP
  30. #define BOOST_BEAST_DETAIL_BASE64_HPP
  31. #include <cctype>
  32. #include <string>
  33. #include <utility>
  34. namespace boost {
  35. namespace beast {
  36. namespace detail {
  37. namespace base64 {
  38. inline
  39. char const*
  40. get_alphabet()
  41. {
  42. static char constexpr tab[] = {
  43. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  44. };
  45. return &tab[0];
  46. }
  47. inline
  48. signed char const*
  49. get_inverse()
  50. {
  51. static signed char constexpr tab[] = {
  52. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
  53. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
  54. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
  55. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63
  56. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
  57. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
  58. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
  59. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
  60. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
  61. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
  62. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
  65. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
  67. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
  68. };
  69. return &tab[0];
  70. }
  71. /// Returns max chars needed to encode a base64 string
  72. inline
  73. std::size_t constexpr
  74. encoded_size(std::size_t n)
  75. {
  76. return 4 * ((n + 2) / 3);
  77. }
  78. /// Returns max bytes needed to decode a base64 string
  79. inline
  80. std::size_t constexpr
  81. decoded_size(std::size_t n)
  82. {
  83. return n / 4 * 3; // requires n&3==0, smaller
  84. //return 3 * n / 4;
  85. }
  86. /** Encode a series of octets as a padded, base64 string.
  87. The resulting string will not be null terminated.
  88. @par Requires
  89. The memory pointed to by `out` points to valid memory
  90. of at least `encoded_size(len)` bytes.
  91. @return The number of characters written to `out`. This
  92. will exclude any null termination.
  93. */
  94. template<class = void>
  95. std::size_t
  96. encode(void* dest, void const* src, std::size_t len)
  97. {
  98. char* out = static_cast<char*>(dest);
  99. char const* in = static_cast<char const*>(src);
  100. auto const tab = base64::get_alphabet();
  101. for(auto n = len / 3; n--;)
  102. {
  103. *out++ = tab[ (in[0] & 0xfc) >> 2];
  104. *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
  105. *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)];
  106. *out++ = tab[ in[2] & 0x3f];
  107. in += 3;
  108. }
  109. switch(len % 3)
  110. {
  111. case 2:
  112. *out++ = tab[ (in[0] & 0xfc) >> 2];
  113. *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
  114. *out++ = tab[ (in[1] & 0x0f) << 2];
  115. *out++ = '=';
  116. break;
  117. case 1:
  118. *out++ = tab[ (in[0] & 0xfc) >> 2];
  119. *out++ = tab[((in[0] & 0x03) << 4)];
  120. *out++ = '=';
  121. *out++ = '=';
  122. break;
  123. case 0:
  124. break;
  125. }
  126. return out - static_cast<char*>(dest);
  127. }
  128. /** Decode a padded base64 string into a series of octets.
  129. @par Requires
  130. The memory pointed to by `out` points to valid memory
  131. of at least `decoded_size(len)` bytes.
  132. @return The number of octets written to `out`, and
  133. the number of characters read from the input string,
  134. expressed as a pair.
  135. */
  136. template<class = void>
  137. std::pair<std::size_t, std::size_t>
  138. decode(void* dest, char const* src, std::size_t len)
  139. {
  140. char* out = static_cast<char*>(dest);
  141. auto in = reinterpret_cast<unsigned char const*>(src);
  142. unsigned char c3[3], c4[4];
  143. int i = 0;
  144. int j = 0;
  145. auto const inverse = base64::get_inverse();
  146. while(len-- && *in != '=')
  147. {
  148. auto const v = inverse[*in];
  149. if(v == -1)
  150. break;
  151. ++in;
  152. c4[i] = v;
  153. if(++i == 4)
  154. {
  155. c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
  156. c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
  157. c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
  158. for(i = 0; i < 3; i++)
  159. *out++ = c3[i];
  160. i = 0;
  161. }
  162. }
  163. if(i)
  164. {
  165. c3[0] = ( c4[0] << 2) + ((c4[1] & 0x30) >> 4);
  166. c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
  167. c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
  168. for(j = 0; j < i - 1; j++)
  169. *out++ = c3[j];
  170. }
  171. return {out - static_cast<char*>(dest),
  172. in - reinterpret_cast<unsigned char const*>(src)};
  173. }
  174. } // base64
  175. template<class = void>
  176. std::string
  177. base64_encode (std::uint8_t const* data,
  178. std::size_t len)
  179. {
  180. std::string dest;
  181. dest.resize(base64::encoded_size(len));
  182. dest.resize(base64::encode(&dest[0], data, len));
  183. return dest;
  184. }
  185. inline
  186. std::string
  187. base64_encode(std::string const& s)
  188. {
  189. return base64_encode (reinterpret_cast <
  190. std::uint8_t const*> (s.data()), s.size());
  191. }
  192. template<class = void>
  193. std::string
  194. base64_decode(std::string const& data)
  195. {
  196. std::string dest;
  197. dest.resize(base64::decoded_size(data.size()));
  198. auto const result = base64::decode(
  199. &dest[0], data.data(), data.size());
  200. dest.resize(result.first);
  201. return dest;
  202. }
  203. } // detail
  204. } // beast
  205. } // boost
  206. #endif