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