MySQL 9.1.0
Source Code Documentation
byte_order_helpers.h
Go to the documentation of this file.
1// Copyright (c) 2023, 2024, 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_SERIALIZATION_BYTE_ORDER_HELPERS_H
25#define MYSQL_SERIALIZATION_BYTE_ORDER_HELPERS_H
26
27#include <bitset>
28#include <sstream>
29#include <vector>
30#include "my_byteorder.h"
31
32#ifdef HAVE_ENDIAN_CONVERSION_MACROS
33#include <endian.h>
34#endif
35
36/// @file
37/// Experimental API header
38/// Conversions between different number representations. MySQL writes
39/// binary data in LE, therefore this header defines the following conversions:
40/// host -> LE (for writing)
41/// LE -> host (for reading)
42
43#if !defined(le64toh)
44/**
45 Converting a 64 bit integer from little-endian byte order to host byteorder
46
47 @param x 64-bit integer in little endian byte order
48 @return 64-bit integer in host byte order
49*/
50uint64_t inline le64toh(uint64_t x) {
51#ifndef WORDS_BIGENDIAN
52 return x;
53#else
54 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
55 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
56 return (x << 32) | (x >> 32);
57#endif
58}
59#endif
60
61#if !defined(htole64)
62/**
63 Converting a 64 bit integer from host's byte order to little-endian byte
64 order.
65
66 @param x 64-bit integer in host's byte order
67 @return 64-bit integer in little endian byte order
68*/
69
70uint64_t inline htole64(uint64_t x) {
71#ifndef WORDS_BIGENDIAN
72 return x;
73#else
74 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
75 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
76 return (x << 32) | (x >> 32);
77#endif
78}
79#endif
80
81#endif // MYSQL_SERIALIZATION_BYTE_ORDER_HELPERS_H
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
Include only the necessary part of Sun RPC for Windows builds.
Functions for reading and storing in machine-independent format.