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