userver: userver/utils/encoding/hex.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
hex.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/encoding/hex.hpp
4/// @brief Encoders, decoders and helpers for hexadecimal representations
5/// @ingroup userver_universal
6
7#include <cstdint>
8#include <string>
9
10#include <userver/utils/span.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::encoding {
15
16/// @brief Converts number to hex character; number must be within range [0,16)
17/// @throws out_of_bounds exception if \p num is out of range
18char ToHexChar(int num);
19
20/// @brief Calculate expected length of input after being hex encoded
21/// @{
22
23constexpr size_t LengthInHexForm(size_t size) noexcept { return size * 2; }
24
25constexpr size_t LengthInHexForm(std::string_view data) noexcept { return LengthInHexForm(data.size()); }
26/// @}
27
28/// @brief Return upper limit on number of characters required
29/// to unhex input of given size.
30///
31/// For example:
32/// - FromHexUpperBound(1) = 0, because you can't really unhex one char,
33/// it is only half byte. Where is the second half?
34/// - FromHexUpperBound(2) = 1. Two chars will be converted into one byte
35/// - FromHexUpperBound(5) = 2. First 4 chars will be unhexed into 2 bytes,
36/// and there will be one left.
37constexpr size_t FromHexUpperBound(size_t size) noexcept {
38 // Although only even-sized input is valid, we have to support an odd
39 // number is well. Luckily for us, size / 2 will get us correct value
40 // anyway
41 return size / 2;
42}
43
44/// @brief Converts input to hex and writes data to output \p out
45/// @param input bytes to convert
46/// @param out string to write data. out will be cleared
47void ToHex(std::string_view input, std::string& out) noexcept;
48
49/// @brief Converts input to hex and writes data to output @p out.
50/// @warning `out` must be pre-allocated to at least @ref LengthInHexForm bytes.
51/// @param input bytes to convert
52/// @param out buffer to write data
53void ToHexBuffer(std::string_view input, utils::span<char> out) noexcept;
54
55/// @brief Allocates std::string, converts input and writes into said string
56/// @param data range of input bytes
57inline std::string ToHex(std::string_view data) noexcept {
58 std::string result;
59 ToHex(data, result);
60 return result;
61}
62
63/// @brief Allocates std::string, converts input and writes into said string
64/// @param encoded start of continuous range in memory
65/// @param len size of that range
66inline std::string ToHex(const void* encoded, size_t len) noexcept {
67 const auto* chars = reinterpret_cast<const char*>(encoded);
68 return ToHex(std::string_view{chars, len});
69}
70
71/// @brief Converts as much of input from hex as possible and writes data
72/// into \p out.
73///
74/// To convert some range from hex, range
75/// must have even number of elements and every element must be hex character.
76/// To avoid throwing, algorithms consumes as much data as possible and returns
77/// how much it was able to process
78///
79/// @param encoded input range to convert
80/// @param out Result will be written into out. Previous value will be cleared.
81/// @returns Number of characters successfully parsed.
82size_t FromHex(std::string_view encoded, std::string& out) noexcept;
83
84/// @brief This FromHex overload allocates string and calls FromHex; if data
85/// is not fully a hex string, then it will be only partially processed.
86inline std::string FromHex(std::string_view encoded) noexcept {
87 std::string result;
88 FromHex(encoded, result);
89 return result;
90}
91
92/// Returns range that constitutes hex string - e.g. sub-view of encoded
93/// that could be fully interpreted as hex string.
94/// Basically, if you have string_view \p a and c = GetHexPart(a), then
95/// range FromHex(c) will be fully parsed.
96/// @param encoded input array of bytes.
97std::string_view GetHexPart(std::string_view encoded) noexcept;
98
99/// @brief Checks that given range is fully a hex string. That is, if passed to
100/// FromHex, it will be fully processed
101bool IsHexData(std::string_view encoded) noexcept;
102
103/// @brief Interprets uint64_t value as array of bytes and applies ToHex to it
104inline std::string ToHexString(uint64_t value) { return ToHex(&value, sizeof(value)); }
105
106} // namespace utils::encoding
107
108USERVER_NAMESPACE_END