base64.ipp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2016-2019 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_IPP
  30. #define BOOST_BEAST_DETAIL_BASE64_IPP
  31. #include <boost/beast/core/detail/base64.hpp>
  32. #include <boost/beast/core/string.hpp>
  33. #include <cctype>
  34. #include <string>
  35. #include <utility>
  36. namespace boost {
  37. namespace beast {
  38. namespace detail {
  39. namespace base64 {
  40. char const*
  41. get_alphabet()
  42. {
  43. static char constexpr tab[] = {
  44. "ABCDEFGHIJKLMNOP"
  45. "QRSTUVWXYZabcdef"
  46. "ghijklmnopqrstuv"
  47. "wxyz0123456789+/"
  48. };
  49. return &tab[0];
  50. }
  51. signed char const*
  52. get_inverse()
  53. {
  54. static signed char constexpr tab[] = {
  55. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
  56. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
  57. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
  58. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63
  59. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
  60. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
  61. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
  62. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
  65. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
  67. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
  68. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
  69. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
  71. };
  72. return &tab[0];
  73. }
  74. /** Encode a series of octets as a padded, base64 string.
  75. The resulting string will not be null terminated.
  76. @par Requires
  77. The memory pointed to by `out` points to valid memory
  78. of at least `encoded_size(len)` bytes.
  79. @return The number of characters written to `out`. This
  80. will exclude any null termination.
  81. */
  82. std::size_t
  83. encode(void* dest, void const* src, std::size_t len)
  84. {
  85. char* out = static_cast<char*>(dest);
  86. char const* in = static_cast<char const*>(src);
  87. auto const tab = base64::get_alphabet();
  88. for(auto n = len / 3; n--;)
  89. {
  90. *out++ = tab[ (in[0] & 0xfc) >> 2];
  91. *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
  92. *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)];
  93. *out++ = tab[ in[2] & 0x3f];
  94. in += 3;
  95. }
  96. switch(len % 3)
  97. {
  98. case 2:
  99. *out++ = tab[ (in[0] & 0xfc) >> 2];
  100. *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
  101. *out++ = tab[ (in[1] & 0x0f) << 2];
  102. *out++ = '=';
  103. break;
  104. case 1:
  105. *out++ = tab[ (in[0] & 0xfc) >> 2];
  106. *out++ = tab[((in[0] & 0x03) << 4)];
  107. *out++ = '=';
  108. *out++ = '=';
  109. break;
  110. case 0:
  111. break;
  112. }
  113. return out - static_cast<char*>(dest);
  114. }
  115. /** Decode a padded base64 string into a series of octets.
  116. @par Requires
  117. The memory pointed to by `out` points to valid memory
  118. of at least `decoded_size(len)` bytes.
  119. @return The number of octets written to `out`, and
  120. the number of characters read from the input string,
  121. expressed as a pair.
  122. */
  123. std::pair<std::size_t, std::size_t>
  124. decode(void* dest, char const* src, std::size_t len)
  125. {
  126. char* out = static_cast<char*>(dest);
  127. auto in = reinterpret_cast<unsigned char const*>(src);
  128. unsigned char c3[3], c4[4];
  129. int i = 0;
  130. int j = 0;
  131. auto const inverse = base64::get_inverse();
  132. while(len-- && *in != '=')
  133. {
  134. auto const v = inverse[*in];
  135. if(v == -1)
  136. break;
  137. ++in;
  138. c4[i] = v;
  139. if(++i == 4)
  140. {
  141. c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
  142. c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
  143. c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
  144. for(i = 0; i < 3; i++)
  145. *out++ = c3[i];
  146. i = 0;
  147. }
  148. }
  149. if(i)
  150. {
  151. c3[0] = ( c4[0] << 2) + ((c4[1] & 0x30) >> 4);
  152. c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
  153. c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
  154. for(j = 0; j < i - 1; j++)
  155. *out++ = c3[j];
  156. }
  157. return {out - static_cast<char*>(dest),
  158. in - reinterpret_cast<unsigned char const*>(src)};
  159. }
  160. } // base64
  161. } // detail
  162. } // beast
  163. } // boost
  164. #endif