MySQL 8.3.0
Source Code Documentation
byteorder.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @file
25 plugin/group_replication/libmysqlgcs/include/mysql/gcs/xplatform/byteorder.h
26 @author Neha Kumari
27
28 @brief The file contains functions to convert the byte encoding of integer
29 values to and from little-endian and big-endian byte order.
30*/
31
32#ifndef BYTEORDER_INCLUDED
33#define BYTEORDER_INCLUDED
34
35#include <stdint.h>
36
38
39/*
40 Methods for reading and storing in machine independent
41 format (low byte first).
42*/
43
44/*
45 Checking le16toh is required because the machine may have the header
46 but the functions might not be defined if the version of glibc < 2.9
47*/
48#ifdef HAVE_ENDIAN_CONVERSION_MACROS
49#include <endian.h>
50#endif
51
52#if !defined(le16toh)
53/**
54 Converting a 16 bit integer from little-endian byte order to host byteorder.
55
56 @param x 16-bit integer in little endian byte order
57 @return 16-bit integer in host byte order
58*/
59
60uint16_t inline le16toh(uint16_t x) {
61#ifndef WORDS_BIGENDIAN
62 return x;
63#else
64 return static_cast<uint16_t>((x >> 8) | (x << 8));
65#endif
66}
67#endif
68
69#if !defined(le32toh)
70/**
71 Converting a 32 bit integer from little-endian byte order to host byteorder.
72
73 @param x 32-bit integer in little endian byte order
74 @return 32-bit integer in host byte order
75*/
76
77uint32_t inline le32toh(uint32_t x) {
78#ifndef WORDS_BIGENDIAN
79 return x;
80#else
81 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
82 ((x << 24) & 0xff000000));
83#endif
84}
85#endif
86
87#if !defined(le64toh)
88/**
89 Converting a 64 bit integer from little-endian byte order to host byteorder.
90
91 @param x 64-bit integer in little endian byte order
92 @return 64-bit integer in host byte order
93*/
94
95uint64_t inline le64toh(uint64_t x) {
96#ifndef WORDS_BIGENDIAN
97 return x;
98#else
99 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
100 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
101 return (x << 32) | (x >> 32);
102#endif
103}
104#endif
105
106#if !defined(htole16)
107/**
108 Converting a 16 bit integer from host's byte order to little-endian byte
109 order.
110
111 @param x 16-bit integer in host byte order
112 @return 16-bit integer in little endian byte order
113*/
114
115uint16_t inline htole16(uint16_t x) {
116#ifndef WORDS_BIGENDIAN
117 return x;
118#else
119 return static_cast<uint16_t>((x >> 8) | (x << 8));
120#endif
121}
122#endif
123
124#if !defined(htole32)
125/**
126 Converting a 32 bit integer from host's byte order to little-endian byte
127 order.
128
129 @param x 32-bit integer in host byte order
130 @return 32-bit integer in little endian byte order
131*/
132
133uint32_t inline htole32(uint32_t x) {
134#ifndef WORDS_BIGENDIAN
135 return x;
136#else
137 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
138 ((x << 24) & 0xff000000));
139#endif
140}
141#endif
142
143#if !defined(htole64)
144/**
145 Converting a 64 bit integer from host's byte order to little-endian byte
146 order.
147
148 @param x 64-bit integer in host's byte order
149 @return 64-bit integer in little endian byte order
150*/
151
152uint64_t inline htole64(uint64_t x) {
153#ifndef WORDS_BIGENDIAN
154 return x;
155#else
156 x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
157 x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL);
158 return (x << 32) | (x >> 32);
159#endif
160}
161#endif
162
163#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:61
uint32_t le32toh(uint32_t x)
Converting a 32 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:77
uint64_t htole64(uint64_t x)
Converting a 64 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:152
uint64_t le64toh(uint64_t x)
Converting a 64 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:95
uint16_t htole16(uint16_t x)
Converting a 16 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:115
uint32_t htole32(uint32_t x)
Converting a 32 bit integer from host's byte order to little-endian byte order.
Definition: byteorder.h:133