MySQL 9.6.0
Source Code Documentation
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_BINARY_BASIC_H
25#define MYSQL_STRCONV_CONV_BINARY_BASIC_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/serialization/variable_length_integers.h" // read_varlen_bytes_unsigned
31#include "mysql/strconv/decode/parser.h" // Parser
32#include "mysql/strconv/encode/string_target.h" // Is_string_target
33#include "mysql/strconv/formats/binary_format.h" // Binary_format
34#include "mysql/strconv/formats/fixstr_binary_format.h" // Fixstr_binary_format
35#include "mysql/strconv/formats/format.h" // Format_base
36#include "mysql/utils/return_status.h" // Return_status
37
38/// @addtogroup GroupLibsMysqlStrconv
39/// @{
40
41namespace mysql::strconv {
42
43// ==== Format integers and strings into binary format ====
44
45/// Format an integer in variable-length, binary format.
46///
47/// The format is the varint format defined in the `serialization` library.
48///
49/// @tparam Target_t Type of `target`.
50///
51/// @param format Type tag to identify that this relates to
52/// binary format with variable-length integers.
53///
54/// @param[out] target Target object to which the string will be written.
55///
56/// @param value The value to write.
57template <Is_string_target Target_t>
58void encode_impl(const Binary_format &format [[maybe_unused]], Target_t &target,
59 const std::integral auto &value) {
60 if constexpr (Target_t::target_type == Target_type::counter) {
61 target.advance(
63 } else {
64 target.advance(
66 }
67}
68
69/// Format a string using variable-length, binary format for the length.
70///
71/// The format is the length followed by the string data. The length is in the
72/// varint format defined in the `serialization` library.
73///
74/// @param format Type tag to identify that this relates to variable-length,
75/// binary format.
76///
77/// @param[out] target Target object to which the string will be written.
78///
79/// @param sv string_view to write.
81 const std::string_view &sv) {
82 target.write(format, sv.size());
83 target.write_raw(sv);
84}
85
86// ==== Parse integers from binary format ====
87
88/// Parse an integral in variable-width integer format type into @c out,
89/// advance the position, and return the status.
90///
91/// The format is as given by the `serialization` library.
92///
93/// @param format Type tag to identify that this relates to binary format with
94/// variable-width integers.
95///
96/// @param[in,out] parser Parser position and parser.
97///
98/// @param[out] out Variable of integral type to read into.
99///
100/// The possible error states are:
101///
102/// - not found: The position was at end-of-string, or the first character was a
103/// non-number.
104///
105/// - parse_error: The number was out of range.
106void decode_impl(const Binary_format &format [[maybe_unused]], Parser &parser,
107 std::integral auto &out) {
108 if (parser.remaining_size() < 1) {
109 parser.set_parse_error("Expected integer");
110 return;
111 }
113 parser.upos(), parser.remaining_size(), out);
114 if (length == 0) {
115 parser.set_parse_error("Expected integer");
116 return;
117 }
119}
120
121// ==== Parse strings from binary format ====
122
123/// Parse a string in binary format, using variable-length integer format for
124/// the length; store the result by making the given `std::string_view &` point
125/// directly into the input string.
126///
127/// @param format Type tag to identify that this relates to binary format.
128///
129/// @param[in,out] parser Parser position and parser.
130///
131/// @param[out] out Reference to `std::string_view` which will be set to point
132/// directly into the input string.
133///
134/// The possible error states are:
135///
136/// - not found: The position was at end-of-string, or the first character was a
137/// non-number, or the remaining input is shorter than the string length.
138///
139/// - parse_error: The number was out of range.
140inline void decode_impl(const Binary_format &format [[maybe_unused]],
141 Parser &parser, std::string_view &out) {
142 uint64_t size{0};
144 if (parser.read(Fixstr_binary_format{size}, out) !=
146 return;
147}
148
149/// Parse a string in binary format, using variable-length integer format for
150/// the length; make the given `std::string_ref &` refer to the corresponding
151/// segment of the input buffer; advance the position; and return the status.
152///
153/// @param format Type tag to identify that this relates to binary format.
154///
155/// @param[in,out] parser Parser position and parser.
156///
157/// @param[out] out Destination string_view which will be set to point directly
158/// into the input string.
159///
160/// The possible error states are:
161///
162/// - not found: The position was at end-of-string, or the first character was a
163/// non-number.
164///
165/// - parse_error: The number was out of range.
167 Is_string_target auto &out) {
168 std::string_view sv;
170 out.write_raw(sv);
171}
172
173} // namespace mysql::strconv
174
175// addtogroup GroupLibsMysqlStrconv
176/// @}
177
178#endif // ifndef MYSQL_STRCONV_CONV_BINARY_BASIC_H
Experimental API header.
Object used to parse strings.
Definition: parser.h:69
Return_status_t read(const Is_parse_options auto &opt, Object_t &obj)
Parse into the given object.
Definition: parser.h:225
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 unsigned char * upos() const
Return the current position as an unsigned char pointer.
Definition: parse_position.h:113
Concept that holds for String_counter and String_writer.
Definition: string_target.h:111
Experimental API header.
Experimental API header.
struct Parser parser
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
size_t write_varlen_bytes(unsigned char *stream, const std::unsigned_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:124
size_t get_size_integer_varlen(const std::unsigned_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:76
size_t read_varlen_bytes(const unsigned char *stream, std::size_t stream_bytes, std::unsigned_integral auto &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:196
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
@ ok
operation succeeded
size_t size(const char *const c)
Definition: base64.h:46
Experimental API header.
Experimental API header.
Experimental API header.
Format tag to identify binary format.
Definition: binary_format.h:38
Format tag to identify fixed-length-string binary format.
Definition: fixstr_binary_format.h:38
Experimental API header.