MySQL 9.0.0
Source Code Documentation
byteorder.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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/**
25 @file
26 plugin/group_replication/libmysqlgcs/include/mysql/gcs/xplatform/byteorder.h
27 @author Neha Kumari
28
29 @brief The file contains functions to convert the byte encoding of integer
30 values to and from little-endian and big-endian byte order.
31*/
32
33#ifndef BYTEORDER_INCLUDED
34#define BYTEORDER_INCLUDED
35
36#include <stdint.h>
37
39
40/*
41 Methods for reading and storing in machine independent
42 format (low byte first).
43*/
44
45/*
46 Checking le16toh is required because the machine may have the header
47 but the functions might not be defined if the version of glibc < 2.9
48*/
49#ifdef HAVE_ENDIAN_CONVERSION_MACROS
50#include <endian.h>
51#endif
52
53#if !defined(le16toh)
54/**
55 Converting a 16 bit integer from little-endian byte order to host byteorder.
56
57 @param x 16-bit integer in little endian byte order
58 @return 16-bit integer in host byte order
59*/
60
61uint16_t inline le16toh(uint16_t x) {
62#ifndef WORDS_BIGENDIAN
63 return x;
64#else
65 return static_cast<uint16_t>((x >> 8) | (x << 8));
66#endif
67}
68#endif
69
70#if !defined(le32toh)
71/**
72 Converting a 32 bit integer from little-endian byte order to host byteorder.
73
74 @param x 32-bit integer in little endian byte order
75 @return 32-bit integer in host byte order
76*/
77
78uint32_t inline le32toh(uint32_t x) {
79#ifndef WORDS_BIGENDIAN
80 return x;
81#else
82 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
83 ((x << 24) & 0xff000000));
84#endif
85}
86#endif
87
88#if !defined(le64toh)
89/**
90 Converting a 64 bit integer from little-endian byte order to host byteorder.
91
92 @param x 64-bit integer in little endian byte order
93 @return 64-bit integer in host byte order
94*/
95
96uint64_t inline le64toh(uint64_t x) {
97#ifndef WORDS_BIGENDIAN
98 return x;
99#else
100 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
101 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
102 return (x << 32) | (x >> 32);
103#endif
104}
105#endif
106
107#if !defined(htole16)
108/**
109 Converting a 16 bit integer from host's byte order to little-endian byte
110 order.
111
112 @param x 16-bit integer in host byte order
113 @return 16-bit integer in little endian byte order
114*/
115
116uint16_t inline htole16(uint16_t x) {
117#ifndef WORDS_BIGENDIAN
118 return x;
119#else
120 return static_cast<uint16_t>((x >> 8) | (x << 8));
121#endif
122}
123#endif
124
125#if !defined(htole32)
126/**
127 Converting a 32 bit integer from host's byte order to little-endian byte
128 order.
129
130 @param x 32-bit integer in host byte order
131 @return 32-bit integer in little endian byte order
132*/
133
134uint32_t inline htole32(uint32_t x) {
135#ifndef WORDS_BIGENDIAN
136 return x;
137#else
138 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
139 ((x << 24) & 0xff000000));
140#endif
141}
142#endif
143
144#if !defined(htole64)
145/**
146 Converting a 64 bit integer from host's byte order to little-endian byte
147 order.
148
149 @param x 64-bit integer in host's byte order
150 @return 64-bit integer in little endian byte order
151*/
152
153uint64_t inline htole64(uint64_t x) {
154#ifndef WORDS_BIGENDIAN
155 return x;
156#else
157 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
158 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
159 return (x << 32) | (x >> 32);
160#endif
161}
162#endif
163
164#endif // BYTEORDER_INCLUDED
Include only the necessary part of Sun RPC for Windows builds.
uint16_t le16toh(uint16_t x)
Converting a 16 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:62
uint32_t le32toh(uint32_t x)
Converting a 32 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:78
uint64_t htole64(uint64_t x)
Converting a 64 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:153
uint64_t le64toh(uint64_t x)
Converting a 64 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:96
uint16_t htole16(uint16_t x)
Converting a 16 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:116
uint32_t htole32(uint32_t x)
Converting a 32 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:134