|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.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/url |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_URL_DECODE_HPP |
| 11 | +#define BOOST_URL_DECODE_HPP |
| 12 | + |
| 13 | +#include <boost/url/detail/config.hpp> |
| 14 | +#include <boost/url/error_types.hpp> |
| 15 | +#include <boost/url/encoding_opts.hpp> |
| 16 | +#include <boost/url/grammar/string_token.hpp> |
| 17 | +#include <boost/core/detail/string_view.hpp> |
| 18 | + |
| 19 | +namespace boost { |
| 20 | +namespace urls { |
| 21 | + |
| 22 | +/** Return the buffer size needed for percent-decoding |
| 23 | +
|
| 24 | + This function returns the exact number of bytes needed |
| 25 | + to store the decoded form of the specified string using |
| 26 | + the given options. The string is validated before the |
| 27 | + size is computed; malformed escapes cause the returned |
| 28 | + result to contain an error instead. |
| 29 | +
|
| 30 | + @par Example |
| 31 | + @code |
| 32 | + auto n = decoded_size( "My%20Stuff" ); |
| 33 | + assert( n && *n == 8 ); |
| 34 | + @endcode |
| 35 | +
|
| 36 | + @par Exception Safety |
| 37 | + Throws nothing. Validation errors are reported in the |
| 38 | + returned result. |
| 39 | +
|
| 40 | + @return A result containing the decoded size, excluding |
| 41 | + any null terminator. |
| 42 | +
|
| 43 | + @param s The string to measure. |
| 44 | +
|
| 45 | + @par Specification |
| 46 | + @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1" |
| 47 | + >2.1. Percent-Encoding (rfc3986)</a> |
| 48 | +
|
| 49 | + @see |
| 50 | + @ref decode, |
| 51 | + @ref encoding_opts, |
| 52 | + @ref make_pct_string_view. |
| 53 | +*/ |
| 54 | +system::result<std::size_t> |
| 55 | +decoded_size(core::string_view s) noexcept; |
| 56 | + |
| 57 | +/** Apply percent-decoding to an arbitrary string |
| 58 | +
|
| 59 | + This function percent-decodes the specified string into |
| 60 | + the destination buffer provided by the caller. The input |
| 61 | + is validated first; malformed escapes cause the returned |
| 62 | + result to hold an error instead of a size. If the buffer |
| 63 | + is too small, the output is truncated and the number of |
| 64 | + bytes actually written is returned. |
| 65 | +
|
| 66 | + @par Example |
| 67 | + @code |
| 68 | + char buf[100]; |
| 69 | + auto n = decode( buf, sizeof(buf), "Program%20Files" ); |
| 70 | + assert( n && *n == 13 ); |
| 71 | + @endcode |
| 72 | +
|
| 73 | + @par Exception Safety |
| 74 | + Throws nothing. Validation errors are reported in the |
| 75 | + returned result. |
| 76 | +
|
| 77 | + @return The number of characters written to the |
| 78 | + destination buffer, or an error. |
| 79 | +
|
| 80 | + @param dest The destination buffer to write to. |
| 81 | +
|
| 82 | + @param size The number of writable characters pointed |
| 83 | + to by `dest`. If this is less than the decoded size, the |
| 84 | + result is truncated. |
| 85 | +
|
| 86 | + @param s The string to decode. |
| 87 | +
|
| 88 | + @param opt The decoding options. If omitted, the |
| 89 | + default options are used. |
| 90 | +
|
| 91 | + @par Specification |
| 92 | + @li <a href="https://datatracker.ietf.org/dodc/html/rfc3986#section-2.1" |
| 93 | + >2.1. Percent-Encoding (rfc3986)</a> |
| 94 | +
|
| 95 | + @see |
| 96 | + @ref decoded_size, |
| 97 | + @ref encoding_opts, |
| 98 | + @ref make_pct_string_view. |
| 99 | +*/ |
| 100 | +system::result<std::size_t> |
| 101 | +decode( |
| 102 | + char* dest, |
| 103 | + std::size_t size, |
| 104 | + core::string_view s, |
| 105 | + encoding_opts opt = {}) noexcept; |
| 106 | + |
| 107 | +//------------------------------------------------ |
| 108 | + |
| 109 | +/** Return a percent-decoded string |
| 110 | +
|
| 111 | + This function percent-decodes the specified string and |
| 112 | + returns the result using any @ref string_token. The |
| 113 | + string is validated before decoding; malformed escapes |
| 114 | + cause the returned result to hold an error. |
| 115 | +
|
| 116 | + @par Example |
| 117 | + @code |
| 118 | + auto plain = decode( "My%20Stuff" ); |
| 119 | + assert( plain && *plain == "My Stuff" ); |
| 120 | + @endcode |
| 121 | +
|
| 122 | + @par Exception Safety |
| 123 | + Calls to allocate may throw. Validation errors are |
| 124 | + reported in the returned result. |
| 125 | +
|
| 126 | + @return A result containing the decoded string in the |
| 127 | + format described by the passed string token. |
| 128 | +
|
| 129 | + @param s The string to decode. |
| 130 | +
|
| 131 | + @param opt The decoding options. If omitted, the |
| 132 | + default options are used. |
| 133 | +
|
| 134 | + @param token A string token. |
| 135 | +
|
| 136 | + @par Specification |
| 137 | + @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1" |
| 138 | + >2.1. Percent-Encoding (rfc3986)</a> |
| 139 | +
|
| 140 | + @see |
| 141 | + @ref decode, |
| 142 | + @ref decoded_size, |
| 143 | + @ref encoding_opts, |
| 144 | + @ref string_token::return_string. |
| 145 | +*/ |
| 146 | +template<BOOST_URL_STRTOK_TPARAM> |
| 147 | +system::result<typename StringToken::result_type> |
| 148 | +decode( |
| 149 | + core::string_view s, |
| 150 | + encoding_opts opt = {}, |
| 151 | + StringToken&& token = {}) noexcept; |
| 152 | + |
| 153 | +} // urls |
| 154 | +} // boost |
| 155 | + |
| 156 | +#include <boost/url/impl/decode.hpp> |
| 157 | + |
| 158 | +#endif |
0 commit comments