MySQL 9.6.0
Source Code Documentation
hex_format.h
Go to the documentation of this file.
1// Copyright (c) 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24#ifndef MYSQL_STRCONV_FORMATS_HEX_FORMAT_H
25#define MYSQL_STRCONV_FORMATS_HEX_FORMAT_H
26
27/// @file
28/// Experimental API header
29
30#include <array> // array
31#include <cassert> // assert
32#include "mysql/strconv/formats/format.h" // Format_base
33
34/// @addtogroup GroupLibsMysqlStrconv
35/// @{
36
37// ==== Conversion tables in namespace detail ====
38
39namespace mysql::strconv::detail {
40
41/// Conversion table with 16 elements, where element i is lowercase hex for i.
42static inline constexpr std::array<unsigned char, 16> int_to_hex_lower{
43 '0', '1', '2', '3', '4', '5', '6', '7',
44 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
45
46/// Conversion table with 16 elements, where element i is uppercase hex i.
47static inline constexpr std::array<unsigned char, 16> int_to_hex_upper{
48 '0', '1', '2', '3', '4', '5', '6', '7',
49 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
50
51/// Policy for using uppercase/lowercase when parsing hex.
52// NOLINTNEXTLINE(performance-enum-size): silence clang-tidy's pointless hint
53enum class Hex_parse_case {
54 /// Accept only lowercase
55 lower,
56 /// Accept only uppercase
57 upper,
58 /// Accept lowercase or uppercase
60};
61
62/// Return a conversion table with 256 elements, where elements for hex chars
63/// is the corresponding integer between 0 and 15, and other elements are -1.
64template <Hex_parse_case hex_parse_case_tp>
66 public:
67 /// Return the table.
68 static auto &table() {
69 // build table on first invocation
70 static auto &ret{build_table()};
71 return ret;
72 }
73
74 private:
75 /// Fill and return the conversion table.
76 //
77 // Todo: Make this constexpr when we have C++23
78 static auto &build_table() {
79 /// The conversion table.
80 static std::array<int, 256> table;
81
82 // Initialize all elements to -1
83 for (int i = 0; i != 256; ++i) table[i] = -1;
84 // Set elements corresponding to lowercase hex to the converted value.
85 if (hex_parse_case_tp != Hex_parse_case::upper) {
86 for (int i = 0; i != 16; ++i) table[int_to_hex_lower[i]] = i;
87 }
88 // Set elements corresponding to uppercase hex to the converted value.
89 if (hex_parse_case_tp != Hex_parse_case::lower) {
90 for (int i = 0; i != 16; ++i) table[int_to_hex_upper[i]] = i;
91 }
92
93 return table;
94 }
95}; // class Hex_to_int
96
97} // namespace mysql::strconv::detail
98
99namespace mysql::strconv {
100
101// ==== Helper type to define format variants ====
102
103/// Policy for using uppercase/lowercase in hex conversion.
104// NOLINTNEXTLINE(performance-enum-size): silence clang-tidy's pointless hint
105enum class Hex_case {
106 // Formatter uses lowercase, parser accepts lowercase or uppercase
107 lower,
108 // Formatter uses uppercase, parser accepts lowercase or uppercase
109 upper,
110 // Formatter uses lowercase, parser accepts lowercase only
112 // Formatter uses uppercase, parser accepts uppercase only
114};
115
116// ==== Format class ====
117
118/// Format tag to identify hex format when encoding and decoding strings.
119///
120/// This also holds two member variables that bound the parsed string length
121/// (impacting only decode, not encode).
122template <Hex_case hex_case_tp = Hex_case::lower>
123struct Hex_format : public Format_base {
124 static constexpr Hex_case hex_case = hex_case_tp;
125
126 /// Construct a new Hex_format with no bounds on the
127 Hex_format() = default;
128
129 /// Return the hex digit for a given integer in the range 0..15.
130 static int int_to_hex(int half_byte) {
131 assert(half_byte >= 0);
132 assert(half_byte < 16);
135 : detail::int_to_hex_upper)[half_byte];
136 }
137
138 /// Return the numeric value between 0 and 15 for a given hex character, or -1
139 /// if the character is not hex. The behavior is undefined if the hex
140 /// character is not in the range 0 to 255.
141 static int hex_to_int(int hex_char) {
142 assert(hex_char >= 0);
143 assert(hex_char < 256);
148 : detail::Hex_parse_case::flexible > ::table())[hex_char];
149 }
150}; // class Hex_format
151
152} // namespace mysql::strconv
153
154// addtogroup GroupLibsMysqlStrconv
155/// @}
156
157#endif // ifndef MYSQL_STRCONV_FORMATS_HEX_FORMAT_H
Return a conversion table with 256 elements, where elements for hex chars is the corresponding intege...
Definition: hex_format.h:65
static auto & build_table()
Fill and return the conversion table.
Definition: hex_format.h:78
static auto & table()
Return the table.
Definition: hex_format.h:68
Experimental API header.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Definition: gtid_binary_format_conv.h:252
static constexpr std::array< unsigned char, 16 > int_to_hex_lower
Conversion table with 16 elements, where element i is lowercase hex for i.
Definition: hex_format.h:42
static constexpr std::array< unsigned char, 16 > int_to_hex_upper
Conversion table with 16 elements, where element i is uppercase hex i.
Definition: hex_format.h:47
Hex_parse_case
Policy for using uppercase/lowercase when parsing hex.
Definition: hex_format.h:53
@ flexible
Accept lowercase or uppercase.
Definition: gtid_binary_format.h:41
Hex_case
Policy for using uppercase/lowercase in hex conversion.
Definition: hex_format.h:105
Top of the hierarchy.
Definition: format.h:38
Format tag to identify hex format when encoding and decoding strings.
Definition: hex_format.h:123
static int hex_to_int(int hex_char)
Return the numeric value between 0 and 15 for a given hex character, or -1 if the character is not he...
Definition: hex_format.h:141
Hex_format()=default
Construct a new Hex_format with no bounds on the.
static constexpr Hex_case hex_case
Definition: hex_format.h:124
static int int_to_hex(int half_byte)
Return the hex digit for a given integer in the range 0..15.
Definition: hex_format.h:130