MySQL 9.6.0
Source Code Documentation
fixint_binary_basic.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_CONV_FIXINT_BINARY_BASIC_H
25#define MYSQL_STRCONV_CONV_FIXINT_BINARY_BASIC_H
26
27/// @file
28/// Experimental API header
29
30#include <concepts> // integral
31#include "my_byteorder.h" // uint8korr
32#include "mysql/strconv/decode/parser.h" // Parser
33#include "mysql/strconv/encode/string_target.h" // Is_string_target
34#include "mysql/strconv/formats/fixint_binary_format.h" // Fixint_binary_format
35
36/// @addtogroup GroupLibsMysqlStrconv
37/// @{
38
39namespace mysql::strconv {
40
41// ==== Format integers into fixed-length binary format ====
42
43/// Format an integer in fixed-length, binary format.
44///
45/// The format is 8-byte, little endian.
46///
47/// @tparam Target_t Type of `target`.
48///
49/// @param format Type tag to identify that this relates to binary format with
50/// fixed-length integers.
51///
52/// @param[out] target Target object to which the string will be written.
53///
54/// @param value The value to write.
55template <Is_string_target Target_t>
56void encode_impl(const Fixint_binary_format &format [[maybe_unused]],
57 Target_t &target, const std::integral auto &value) {
58 if constexpr (Target_t::target_type == Target_type::counter) {
59 target.advance(8);
60 } else {
61 int8store(target.pos(), value);
62 target.advance(8);
63 }
64}
65
66// ==== Parse integers from fixed-length binary format ====
67
68/// Parse an integral in fixed-width integer format type into @c out, advance
69/// the position, and return the status.
70///
71/// The format is 8 byte, little-endian.
72///
73/// @tparam Value_t Unsigned integral type to read.
74///
75/// @param format Type tag to identify that this relates to binary format with
76/// fixed-width integers.
77///
78/// @param[in,out] parser Parser position and parser.
79///
80/// @param[out] out Destination value.
81///
82/// The possible error states are:
83///
84/// - not found: The position was at end-of-string, or the first character was a
85/// non-number.
86///
87/// - parse_error: The number was out of range.
88template <std::integral Value_t>
89void decode_impl(const Fixint_binary_format &format [[maybe_unused]],
90 Parser &parser, Value_t &out) {
91 if (parser.remaining_size() < 8) {
92 parser.set_parse_error("Expected 8-byte unsigned integer");
93 return;
94 }
95 if constexpr (std::unsigned_integral<Value_t>) {
96 uint64_t tmp = uint8korr(parser.pos());
97 if (tmp > uint64_t(std::numeric_limits<Value_t>::max())) {
98 parser.set_parse_error("Unsigned integer out of range");
99 return;
100 }
101 out = Value_t(tmp);
102 } else {
103 int64_t tmp = sint8korr(parser.pos());
104 if (tmp > int64_t(std::numeric_limits<Value_t>::max()) ||
105 tmp < int64_t(std::numeric_limits<Value_t>::min())) {
106 parser.set_parse_error("Signed integer out of range");
107 return;
108 }
109 out = Value_t(tmp);
110 }
111 parser.advance(8);
112}
113
114} // namespace mysql::strconv
115
116// addtogroup GroupLibsMysqlStrconv
117/// @}
118
119#endif // ifndef MYSQL_STRCONV_CONV_FIXINT_BINARY_BASIC_H
Object used to parse strings.
Definition: parser.h:69
void set_parse_error(const std::string_view &message)
Store a result representing that the requested object could not be parsed because the string is wrong...
Definition: parser.h:94
void advance(std::ptrdiff_t delta)
Move the iterator delta steps.
Definition: parse_position.h:79
std::size_t remaining_size() const
Return the remaining size.
Definition: parse_position.h:153
const char * pos() const
Return the current position as a char pointer.
Definition: parse_position.h:110
Experimental API header.
Functions for reading and storing in machine-independent format.
ulonglong uint8korr(const char *pT)
Definition: my_byteorder.h:164
void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:192
longlong sint8korr(const char *pT)
Definition: my_byteorder.h:168
struct Parser parser
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
ValueType max(X &&first)
Definition: gtid.h:103
Definition: gtid_binary_format.h:41
void decode_impl(const Gtid_binary_format &format, Parser &parser, mysql::gtids::Is_tag auto &tag)
Definition: gtid_binary_format_conv.h:63
void encode_impl(const Gtid_binary_format &format, Is_string_target auto &target, const mysql::gtids::Is_tag auto &tag)
Definition: gtid_binary_format_conv.h:48
Experimental API header.
Experimental API header.
Format tag to identify fixed-length-integer binary format.
Definition: fixint_binary_format.h:42