24#ifndef MYSQL_SERIALIZATION_VARIABLE_LENGTH_INTEGERS_H
25#define MYSQL_SERIALIZATION_VARIABLE_LENGTH_INTEGERS_H
50 const std::unsigned_integral
auto &data) {
61 int bits_in_number = std::bit_width(data);
62 return ((bits_in_number * 575) >> 12) + 1;
69 auto sign_mask = data >> (
sizeof(data) * 8 - 1);
90 const std::unsigned_integral
auto &data) {
91 uint64_t data_cpy = data;
93 stream[0] = ((1 << (byte_count - 1)) - 1) |
94 static_cast<uint8_t
>(data_cpy << byte_count);
96 if (byte_count == 1) {
101 data_cpy >>= (8 - byte_count + ((byte_count + 7) >> 4));
104 memcpy(&stream[1], &data_cpy, byte_count - 1);
111 const std::signed_integral
auto &data) {
114 auto sign_mask = (data >> (
sizeof(data) * 8 - 1));
115 uint64_t data_cpy = (data ^ sign_mask);
117 data_cpy = (data_cpy << 1) | (sign_mask & 1);
124 const std::unsigned_integral
auto &data) {
131 const std::signed_integral
auto &data) {
141template <
typename Type>
143 const unsigned char *stream, std::size_t stream_bytes,
144 Type &data)
requires std::unsigned_integral<Type> {
145 if (stream_bytes == 0) {
148 uint8_t first_byte = stream[0];
149 std::size_t num_bytes = std::countr_one(first_byte) + 1;
150 if (num_bytes > stream_bytes) {
153 Type data_cpy = first_byte >> num_bytes;
154 if (num_bytes == 1) {
158 uint64_t data_tmp = 0;
159 memcpy(&data_tmp, &stream[1], num_bytes - 1);
163 data_tmp <<= (8 - num_bytes + ((num_bytes + 7) >> 4));
164 if (data_tmp > std::numeric_limits<Type>::max()) {
167 data_cpy |= data_tmp;
173template <
typename Type>
175 const unsigned char *stream, std::size_t stream_bytes,
176 Type &data)
requires std::signed_integral<Type> {
177 using Type_unsigned = std::make_unsigned_t<Type>;
178 Type_unsigned data_tmp = 0;
179 std::size_t num_bytes =
183 Type_unsigned sign_mask = -
static_cast<Type>(data_tmp & 1);
185 data_tmp = data_tmp >> 1;
187 data_tmp = data_tmp ^ sign_mask;
188 data =
Type(data_tmp);
194 std::unsigned_integral
auto &data) {
200 std::signed_integral
auto &data) {
uint64_t htole64(uint64_t x)
Converting a 64 bit integer from host's byte order to little-endian byte order.
Definition: byte_order_helpers.h:70
uint64_t le64toh(uint64_t x)
Converting a 64 bit integer from little-endian byte order to host byteorder.
Definition: byte_order_helpers.h:50
Functions for reading and storing in machine-independent format.
Definition: variable_length_integers.h:43
size_t get_size_integer_varlen_unsigned(const std::unsigned_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:49
size_t write_varlen_bytes_signed(unsigned char *stream, const std::signed_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:110
size_t read_varlen_bytes_unsigned(const unsigned char *stream, std::size_t stream_bytes, Type &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:142
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:123
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:75
size_t read_varlen_bytes_signed(const unsigned char *stream, std::size_t stream_bytes, Type &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:174
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:193
size_t get_size_integer_varlen_signed(const std::signed_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:67
size_t write_varlen_bytes_unsigned(unsigned char *stream, const std::unsigned_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:89
Type
Definition: resource_group_basic_types.h:33