base64.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef BOOST_BEAST_DETAIL_BASE64_HPP
  10. #define BOOST_BEAST_DETAIL_BASE64_HPP
  11. #include <boost/beast/core/string.hpp>
  12. #include <cctype>
  13. #include <utility>
  14. namespace boost {
  15. namespace beast {
  16. namespace detail {
  17. namespace base64 {
  18. BOOST_BEAST_DECL
  19. char const*
  20. get_alphabet();
  21. BOOST_BEAST_DECL
  22. signed char const*
  23. get_inverse();
  24. /// Returns max chars needed to encode a base64 string
  25. BOOST_BEAST_DECL
  26. std::size_t constexpr
  27. encoded_size(std::size_t n)
  28. {
  29. return 4 * ((n + 2) / 3);
  30. }
  31. /// Returns max bytes needed to decode a base64 string
  32. inline
  33. std::size_t constexpr
  34. decoded_size(std::size_t n)
  35. {
  36. return n / 4 * 3; // requires n&3==0, smaller
  37. }
  38. /** Encode a series of octets as a padded, base64 string.
  39. The resulting string will not be null terminated.
  40. @par Requires
  41. The memory pointed to by `out` points to valid memory
  42. of at least `encoded_size(len)` bytes.
  43. @return The number of characters written to `out`. This
  44. will exclude any null termination.
  45. */
  46. BOOST_BEAST_DECL
  47. std::size_t
  48. encode(void* dest, void const* src, std::size_t len);
  49. /** Decode a padded base64 string into a series of octets.
  50. @par Requires
  51. The memory pointed to by `out` points to valid memory
  52. of at least `decoded_size(len)` bytes.
  53. @return The number of octets written to `out`, and
  54. the number of characters read from the input string,
  55. expressed as a pair.
  56. */
  57. BOOST_BEAST_DECL
  58. std::pair<std::size_t, std::size_t>
  59. decode(void* dest, char const* src, std::size_t len);
  60. } // base64
  61. } // detail
  62. } // beast
  63. } // boost
  64. #ifdef BOOST_BEAST_HEADER_ONLY
  65. #include <boost/beast/core/detail/base64.ipp>
  66. #endif
  67. #endif