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