LCOV - code coverage report
Current view: top level - src/util - strencodings.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 50 50
Test Date: 2024-08-28 04:44:32 Functions: 97.0 % 33 32
Branches: 81.6 % 76 62

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : /**
       7                 :             :  * Utilities for converting data from/to strings.
       8                 :             :  */
       9                 :             : #ifndef BITCOIN_UTIL_STRENCODINGS_H
      10                 :             : #define BITCOIN_UTIL_STRENCODINGS_H
      11                 :             : 
      12                 :             : #include <crypto/hex_base.h> // IWYU pragma: export
      13                 :             : #include <span.h>
      14                 :             : #include <util/string.h>
      15                 :             : 
      16                 :             : #include <charconv>
      17                 :             : #include <cstddef>
      18                 :             : #include <cstdint>
      19                 :             : #include <limits>
      20                 :             : #include <optional>
      21                 :             : #include <string>      // IWYU pragma: export
      22                 :             : #include <string_view> // IWYU pragma: export
      23                 :             : #include <system_error>
      24                 :             : #include <type_traits>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :             : /** Used by SanitizeString() */
      28                 :             : enum SafeChars
      29                 :             : {
      30                 :             :     SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
      31                 :             :     SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
      32                 :             :     SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
      33                 :             :     SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
      34                 :             : };
      35                 :             : 
      36                 :             : /**
      37                 :             :  * Used by ParseByteUnits()
      38                 :             :  * Lowercase base 1000
      39                 :             :  * Uppercase base 1024
      40                 :             : */
      41                 :             : enum class ByteUnit : uint64_t {
      42                 :             :     NOOP = 1ULL,
      43                 :             :     k = 1000ULL,
      44                 :             :     K = 1024ULL,
      45                 :             :     m = 1'000'000ULL,
      46                 :             :     M = 1ULL << 20,
      47                 :             :     g = 1'000'000'000ULL,
      48                 :             :     G = 1ULL << 30,
      49                 :             :     t = 1'000'000'000'000ULL,
      50                 :             :     T = 1ULL << 40,
      51                 :             : };
      52                 :             : 
      53                 :             : /**
      54                 :             : * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
      55                 :             : * addresses, but avoid anything even possibly remotely dangerous like & or >
      56                 :             : * @param[in] str    The string to sanitize
      57                 :             : * @param[in] rule   The set of safe chars to choose (default: least restrictive)
      58                 :             : * @return           A new string without unsafe chars
      59                 :             : */
      60                 :             : std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
      61                 :             : /** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. Returns nullopt on invalid input. */
      62                 :             : template <typename Byte = std::byte>
      63                 :             : std::optional<std::vector<Byte>> TryParseHex(std::string_view str);
      64                 :             : /** Like TryParseHex, but returns an empty vector on invalid input. */
      65                 :             : template <typename Byte = uint8_t>
      66                 :       10917 : std::vector<Byte> ParseHex(std::string_view hex_str)
      67                 :             : {
      68                 :       10917 :     return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
      69                 :             : }
      70                 :             : /* Returns true if each character in str is a hex character, and has an even
      71                 :             :  * number of hex digits.*/
      72                 :             : bool IsHex(std::string_view str);
      73                 :             : std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
      74                 :             : std::string EncodeBase64(Span<const unsigned char> input);
      75                 :           1 : inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
      76                 :           8 : inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
      77                 :             : std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
      78                 :             : 
      79                 :             : /**
      80                 :             :  * Base32 encode.
      81                 :             :  * If `pad` is true, then the output will be padded with '=' so that its length
      82                 :             :  * is a multiple of 8.
      83                 :             :  */
      84                 :             : std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
      85                 :             : 
      86                 :             : /**
      87                 :             :  * Base32 encode.
      88                 :             :  * If `pad` is true, then the output will be padded with '=' so that its length
      89                 :             :  * is a multiple of 8.
      90                 :             :  */
      91                 :             : std::string EncodeBase32(std::string_view str, bool pad = true);
      92                 :             : 
      93                 :             : /**
      94                 :             :  * Splits socket address string into host string and port value.
      95                 :             :  * Validates port value.
      96                 :             :  *
      97                 :             :  * @param[in] in        The socket address string to split.
      98                 :             :  * @param[out] portOut  Port-portion of the input, if found and parsable.
      99                 :             :  * @param[out] hostOut  Host-portion of the input, if found.
     100                 :             :  * @return              true if port-portion is absent or within its allowed range, otherwise false
     101                 :             :  */
     102                 :             : bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
     103                 :             : 
     104                 :             : // LocaleIndependentAtoi is provided for backwards compatibility reasons.
     105                 :             : //
     106                 :             : // New code should use ToIntegral or the ParseInt* functions
     107                 :             : // which provide parse error feedback.
     108                 :             : //
     109                 :             : // The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
     110                 :             : // std::atoi as it behaves under the "C" locale, and remove some undefined
     111                 :             : // behavior. If the parsed value is bigger than the integer type's maximum
     112                 :             : // value, or smaller than the integer type's minimum value, std::atoi has
     113                 :             : // undefined behavior, while this function returns the maximum or minimum
     114                 :             : // values, respectively.
     115                 :             : template <typename T>
     116                 :      106371 : T LocaleIndependentAtoi(std::string_view str)
     117                 :             : {
     118                 :             :     static_assert(std::is_integral<T>::value);
     119                 :             :     T result;
     120                 :             :     // Emulate atoi(...) handling of white space and leading +/-.
     121         [ +  + ]:      106371 :     std::string_view s = util::TrimStringView(str);
     122   [ +  +  +  + ]:      106371 :     if (!s.empty() && s[0] == '+') {
     123   [ +  -  +  + ]:          24 :         if (s.length() >= 2 && s[1] == '-') {
     124                 :             :             return 0;
     125                 :             :         }
     126                 :          18 :         s = s.substr(1);
     127                 :             :     }
     128                 :      106365 :     auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
     129         [ +  + ]:      106365 :     if (error_condition == std::errc::result_out_of_range) {
     130   [ +  -  +  + ]:          38 :         if (s.length() >= 1 && s[0] == '-') {
     131                 :             :             // Saturate underflow, per strtoll's behavior.
     132                 :             :             return std::numeric_limits<T>::min();
     133                 :             :         } else {
     134                 :             :             // Saturate overflow, per strtoll's behavior.
     135                 :             :             return std::numeric_limits<T>::max();
     136                 :             :         }
     137         [ +  + ]:      106327 :     } else if (error_condition != std::errc{}) {
     138                 :             :         return 0;
     139                 :             :     }
     140                 :      106241 :     return result;
     141                 :             : }
     142                 :             : 
     143                 :             : /**
     144                 :             :  * Tests if the given character is a decimal digit.
     145                 :             :  * @param[in] c     character to test
     146                 :             :  * @return          true if the argument is a decimal digit; otherwise false.
     147                 :             :  */
     148                 :      326910 : constexpr bool IsDigit(char c)
     149                 :             : {
     150   [ +  +  +  +  :      326910 :     return c >= '0' && c <= '9';
          +  +  #  #  #  
           # ][ +  +  +  
          -  +  +  +  -  
           +  - ][ #  # ]
     151                 :             : }
     152                 :             : 
     153                 :             : /**
     154                 :             :  * Tests if the given character is a whitespace character. The whitespace characters
     155                 :             :  * are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal
     156                 :             :  * tab ('\t'), and vertical tab ('\v').
     157                 :             :  *
     158                 :             :  * This function is locale independent. Under the C locale this function gives the
     159                 :             :  * same result as std::isspace.
     160                 :             :  *
     161                 :             :  * @param[in] c     character to test
     162                 :             :  * @return          true if the argument is a whitespace character; otherwise false
     163                 :             :  */
     164                 :      957805 : constexpr inline bool IsSpace(char c) noexcept {
     165   [ +  +  +  + ]:      957805 :     return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
     166                 :             : }
     167                 :             : 
     168                 :             : /**
     169                 :             :  * Convert string to integral type T. Leading whitespace, a leading +, or any
     170                 :             :  * trailing character fail the parsing. The required format expressed as regex
     171                 :             :  * is `-?[0-9]+`. The minus sign is only permitted for signed integer types.
     172                 :             :  *
     173                 :             :  * @returns std::nullopt if the entire string could not be parsed, or if the
     174                 :             :  *   parsed value is not in the range representable by the type T.
     175                 :             :  */
     176                 :             : template <typename T>
     177                 :       12901 : std::optional<T> ToIntegral(std::string_view str)
     178                 :             : {
     179                 :             :     static_assert(std::is_integral<T>::value);
     180                 :             :     T result;
     181                 :       12901 :     const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
     182   [ +  +  +  + ]:       12901 :     if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
     183                 :         697 :         return std::nullopt;
     184                 :             :     }
     185                 :       12204 :     return result;
     186                 :             : }
     187                 :             : 
     188                 :             : /**
     189                 :             :  * Convert string to signed 32-bit integer with strict parse error feedback.
     190                 :             :  * @returns true if the entire string could be parsed as valid integer,
     191                 :             :  *   false if not the entire string could be parsed or when overflow or underflow occurred.
     192                 :             :  */
     193                 :             : [[nodiscard]] bool ParseInt32(std::string_view str, int32_t *out);
     194                 :             : 
     195                 :             : /**
     196                 :             :  * Convert string to signed 64-bit integer with strict parse error feedback.
     197                 :             :  * @returns true if the entire string could be parsed as valid integer,
     198                 :             :  *   false if not the entire string could be parsed or when overflow or underflow occurred.
     199                 :             :  */
     200                 :             : [[nodiscard]] bool ParseInt64(std::string_view str, int64_t *out);
     201                 :             : 
     202                 :             : /**
     203                 :             :  * Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
     204                 :             :  * @returns true if the entire string could be parsed as valid integer,
     205                 :             :  *   false if not the entire string could be parsed or when overflow or underflow occurred.
     206                 :             :  */
     207                 :             : [[nodiscard]] bool ParseUInt8(std::string_view str, uint8_t *out);
     208                 :             : 
     209                 :             : /**
     210                 :             :  * Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
     211                 :             :  * @returns true if the entire string could be parsed as valid integer,
     212                 :             :  *   false if the entire string could not be parsed or if overflow or underflow occurred.
     213                 :             :  */
     214                 :             : [[nodiscard]] bool ParseUInt16(std::string_view str, uint16_t* out);
     215                 :             : 
     216                 :             : /**
     217                 :             :  * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
     218                 :             :  * @returns true if the entire string could be parsed as valid integer,
     219                 :             :  *   false if not the entire string could be parsed or when overflow or underflow occurred.
     220                 :             :  */
     221                 :             : [[nodiscard]] bool ParseUInt32(std::string_view str, uint32_t *out);
     222                 :             : 
     223                 :             : /**
     224                 :             :  * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
     225                 :             :  * @returns true if the entire string could be parsed as valid integer,
     226                 :             :  *   false if not the entire string could be parsed or when overflow or underflow occurred.
     227                 :             :  */
     228                 :             : [[nodiscard]] bool ParseUInt64(std::string_view str, uint64_t *out);
     229                 :             : 
     230                 :             : /**
     231                 :             :  * Format a paragraph of text to a fixed width, adding spaces for
     232                 :             :  * indentation to any added line.
     233                 :             :  */
     234                 :             : std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
     235                 :             : 
     236                 :             : /**
     237                 :             :  * Timing-attack-resistant comparison.
     238                 :             :  * Takes time proportional to length
     239                 :             :  * of first argument.
     240                 :             :  */
     241                 :             : template <typename T>
     242         [ +  + ]:          14 : bool TimingResistantEqual(const T& a, const T& b)
     243                 :             : {
     244         [ +  + ]:          14 :     if (b.size() == 0) return a.size() == 0;
     245                 :          10 :     size_t accumulator = a.size() ^ b.size();
     246         [ +  + ]:          28 :     for (size_t i = 0; i < a.size(); i++)
     247                 :          18 :         accumulator |= size_t(a[i] ^ b[i%b.size()]);
     248                 :          10 :     return accumulator == 0;
     249                 :             : }
     250                 :             : 
     251                 :             : /** Parse number as fixed point according to JSON number syntax.
     252                 :             :  * @returns true on success, false on error.
     253                 :             :  * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
     254                 :             :  */
     255                 :             : [[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
     256                 :             : 
     257                 :             : namespace {
     258                 :             : /** Helper class for the default infn argument to ConvertBits (just returns the input). */
     259                 :             : struct IntIdentity
     260                 :             : {
     261                 :             :     [[maybe_unused]] int operator()(int x) const { return x; }
     262                 :             : };
     263                 :             : 
     264                 :             : } // namespace
     265                 :             : 
     266                 :             : /** Convert from one power-of-2 number base to another. */
     267                 :             : template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity>
     268                 :        6477 : bool ConvertBits(O outfn, It it, It end, I infn = {}) {
     269                 :        6477 :     size_t acc = 0;
     270                 :        6477 :     size_t bits = 0;
     271                 :        6477 :     constexpr size_t maxv = (1 << tobits) - 1;
     272                 :        6477 :     constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
     273         [ +  + ]:      145010 :     while (it != end) {
     274         [ +  + ]:      138540 :         int v = infn(*it);
     275         [ +  + ]:        3235 :         if (v < 0) return false;
     276                 :      138533 :         acc = ((acc << frombits) | v) & max_acc;
     277                 :      138533 :         bits += frombits;
     278         [ +  + ]:      352216 :         while (bits >= tobits) {
     279                 :      213683 :             bits -= tobits;
     280                 :      213683 :             outfn((acc >> bits) & maxv);
     281                 :             :         }
     282                 :      138533 :         ++it;
     283                 :             :     }
     284                 :             :     if (pad) {
     285         [ +  + ]:        6322 :         if (bits) outfn((acc << (tobits - bits)) & maxv);
     286   [ +  +  +  + ]:         148 :     } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
     287                 :           5 :         return false;
     288                 :             :     }
     289                 :             :     return true;
     290                 :             : }
     291                 :             : 
     292                 :             : /**
     293                 :             :  * Converts the given character to its lowercase equivalent.
     294                 :             :  * This function is locale independent. It only converts uppercase
     295                 :             :  * characters in the standard 7-bit ASCII range.
     296                 :             :  * This is a feature, not a limitation.
     297                 :             :  *
     298                 :             :  * @param[in] c     the character to convert to lowercase.
     299                 :             :  * @return          the lowercase equivalent of c; or the argument
     300                 :             :  *                  if no conversion is possible.
     301                 :             :  */
     302                 :        1919 : constexpr char ToLower(char c)
     303                 :             : {
     304         [ +  + ]:        1919 :     return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
     305                 :             : }
     306                 :             : 
     307                 :             : /**
     308                 :             :  * Returns the lowercase equivalent of the given string.
     309                 :             :  * This function is locale independent. It only converts uppercase
     310                 :             :  * characters in the standard 7-bit ASCII range.
     311                 :             :  * This is a feature, not a limitation.
     312                 :             :  *
     313                 :             :  * @param[in] str   the string to convert to lowercase.
     314                 :             :  * @returns         lowercased equivalent of str
     315                 :             :  */
     316                 :             : std::string ToLower(std::string_view str);
     317                 :             : 
     318                 :             : /**
     319                 :             :  * Converts the given character to its uppercase equivalent.
     320                 :             :  * This function is locale independent. It only converts lowercase
     321                 :             :  * characters in the standard 7-bit ASCII range.
     322                 :             :  * This is a feature, not a limitation.
     323                 :             :  *
     324                 :             :  * @param[in] c     the character to convert to uppercase.
     325                 :             :  * @return          the uppercase equivalent of c; or the argument
     326                 :             :  *                  if no conversion is possible.
     327                 :             :  */
     328                 :          31 : constexpr char ToUpper(char c)
     329                 :             : {
     330   [ +  -  +  + ]:          31 :     return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
                 [ #  # ]
     331                 :             : }
     332                 :             : 
     333                 :             : /**
     334                 :             :  * Returns the uppercase equivalent of the given string.
     335                 :             :  * This function is locale independent. It only converts lowercase
     336                 :             :  * characters in the standard 7-bit ASCII range.
     337                 :             :  * This is a feature, not a limitation.
     338                 :             :  *
     339                 :             :  * @param[in] str   the string to convert to uppercase.
     340                 :             :  * @returns         UPPERCASED EQUIVALENT OF str
     341                 :             :  */
     342                 :             : std::string ToUpper(std::string_view str);
     343                 :             : 
     344                 :             : /**
     345                 :             :  * Capitalizes the first character of the given string.
     346                 :             :  * This function is locale independent. It only converts lowercase
     347                 :             :  * characters in the standard 7-bit ASCII range.
     348                 :             :  * This is a feature, not a limitation.
     349                 :             :  *
     350                 :             :  * @param[in] str   the string to capitalize.
     351                 :             :  * @returns         string with the first letter capitalized.
     352                 :             :  */
     353                 :             : std::string Capitalize(std::string str);
     354                 :             : 
     355                 :             : /**
     356                 :             :  * Parse a string with suffix unit [k|K|m|M|g|G|t|T].
     357                 :             :  * Must be a whole integer, fractions not allowed (0.5t), no whitespace or +-
     358                 :             :  * Lowercase units are 1000 base. Uppercase units are 1024 base.
     359                 :             :  * Examples: 2m,27M,19g,41T
     360                 :             :  *
     361                 :             :  * @param[in] str                  the string to convert into bytes
     362                 :             :  * @param[in] default_multiplier   if no unit is found in str use this unit
     363                 :             :  * @returns                        optional uint64_t bytes from str or nullopt
     364                 :             :  *                                 if ToIntegral is false, str is empty, trailing whitespace or overflow
     365                 :             :  */
     366                 :             : std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
     367                 :             : 
     368                 :             : #endif // BITCOIN_UTIL_STRENCODINGS_H
        

Generated by: LCOV version 2.0-1