MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
hex.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_HEX_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_HEX_H_
28
29#include <cassert>
30#include <cstdint>
31#include <iomanip>
32#include <sstream>
33#include <stdexcept>
34#include <type_traits>
35
36namespace helper {
37namespace string {
38
40 public:
42 os << std::setfill('0') << std::setw(2) << std::hex;
43 }
44
45 template <typename ValueType>
46 int operator()(const ValueType &v) {
47 return v;
48 }
49};
50
52 public:
54 os << std::setfill('0') << std::setw(2) << std::hex;
55 }
56
57 int operator()(const char &v) { return (int)static_cast<uint8_t>(v); }
58};
59
60template <typename Container, typename Transform = DefaultHex>
61std::string hex(const Container &c) {
63 Transform trans;
64 auto end = std::end(c);
65 auto it = std::begin(c);
66 static_assert(sizeof(decltype(*it)) == 1);
67
68 for (; it != end; ++it) {
69 trans(os);
70 os << trans(*it);
71 }
72 return os.str();
73}
74
75inline bool get_unhex_character(const char c, uint8_t *out) {
76 static_assert('0' < 'A');
77 static_assert('A' < 'a');
78 static_assert('0' < '9');
79
80 if (c > 'f') return false;
81
82 if (c >= 'a') {
83 *out = c - 'a' + 10;
84 return true;
85 }
86
87 if (c > 'F') return false;
88
89 if (c >= 'A') {
90 *out = c - 'A' + 10;
91 return true;
92 }
93
94 if (c > '9') return false;
95
96 if (c >= '0') {
97 *out = c - '0';
98 return true;
99 }
100
101 return false;
102}
103
104inline bool get_unhex_character_or_throw(const char c, uint8_t *out) {
105 if (get_unhex_character(c, out)) return true;
106
107 throw std::runtime_error("Invalid character in hexadecimal value.");
108
109 return false;
110}
111
112using HexConverter = bool (*)(const char c, uint8_t *out);
113
114inline uint8_t unhex_character(const char c) {
115 uint8_t result;
116 if (!get_unhex_character(c, &result))
117 throw std::runtime_error("Invalid character in hexadecimal value.");
118 return result;
119}
120
121template <HexConverter converter, typename IT1, typename IT2>
122bool get_hex_skip(IT1 &it, const IT2 &end, uint8_t *out) {
123 bool result;
124 do {
125 if (it == end) return false;
126 result = converter(*it, out);
127 ++it;
128 } while (!result);
129 return result;
130}
131
132template <typename Container,
134Container unhex(const std::string &h) {
135 Container result;
136 uint8_t v1, v2;
137
138 for (auto i = h.begin(); i != h.end();) {
139 if (!get_hex_skip<converter>(i, h.end(), &v2)) break;
140 if (!get_hex_skip<converter>(i, h.end(), &v1)) break;
141 uint8_t v = v2 * 16 + v1;
142 result.push_back(v);
143 }
144 return result;
145}
146
147} // namespace string
148} // namespace helper
149
150#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_HEX_H_
Definition: hex.h:51
void operator()(std::ostringstream &os)
Definition: hex.h:53
int operator()(const char &v)
Definition: hex.h:57
Definition: hex.h:39
void operator()(std::ostringstream &os)
Definition: hex.h:41
int operator()(const ValueType &v)
Definition: hex.h:46
uint8_t unhex_character(const char c)
Definition: hex.h:114
std::string hex(const Container &c)
Definition: hex.h:61
Container unhex(const std::string &h)
Definition: hex.h:134
bool(*)(const char c, uint8_t *out) HexConverter
Definition: hex.h:112
bool get_unhex_character_or_throw(const char c, uint8_t *out)
Definition: hex.h:104
bool get_unhex_character(const char c, uint8_t *out)
Definition: hex.h:75
bool get_hex_skip(IT1 &it, const IT2 &end, uint8_t *out)
Definition: hex.h:122
Definition: cache.h:33
const char * begin(const char *const c)
Definition: base64.h:44
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872
struct result result
Definition: result.h:34
Definition: result.h:30