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