MySQL  8.0.22
Source Code Documentation
my_byteorder.h
Go to the documentation of this file.
1 #ifndef MY_BYTEORDER_INCLUDED
2 #define MY_BYTEORDER_INCLUDED
3 
4 /* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /**
27  @file include/my_byteorder.h
28  Functions for reading and storing in machine-independent format.
29  The little-endian variants are 'korr' (assume 'corrector') variants
30  for integer types, but 'get' (assume 'getter') for floating point types.
31 */
32 
33 #include "my_config.h"
34 
35 #include "my_compiler.h"
36 
37 #include <string.h>
38 #include <sys/types.h>
39 
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
42 #endif
43 
44 #if defined(_MSC_VER)
45 #include <stdlib.h>
46 #endif
47 
48 #if defined(_WIN32) && defined(WIN32_LEAN_AND_MEAN)
49 #include <winsock2.h>
50 #endif
51 
52 #ifdef WORDS_BIGENDIAN
53 #include "big_endian.h" // IWYU pragma: export
54 #else
55 #include "little_endian.h" // IWYU pragma: export
56 #endif
57 
58 #include "my_inttypes.h"
59 
60 #ifdef __cplusplus
61 #include "template_utils.h"
62 #endif
63 
64 static inline int32 sint3korr(const uchar *A) {
65  return ((int32)(((A[2]) & 128)
66  ? (((uint32)255L << 24) | (((uint32)A[2]) << 16) |
67  (((uint32)A[1]) << 8) | ((uint32)A[0]))
68  : (((uint32)A[2]) << 16) | (((uint32)A[1]) << 8) |
69  ((uint32)A[0])));
70 }
71 
72 static inline uint32 uint3korr(const uchar *A) {
73  return (uint32)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
74  (((uint32)(A[2])) << 16));
75 }
76 
77 static inline ulonglong uint5korr(const uchar *A) {
78  return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
79  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
80  (((ulonglong)(A[4])) << 32));
81 }
82 
83 static inline ulonglong uint6korr(const uchar *A) {
84  return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
85  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
86  (((ulonglong)(A[4])) << 32) + (((ulonglong)(A[5])) << 40));
87 }
88 
89 /**
90  int3store
91 
92  Stores an unsinged integer in a platform independent way
93 
94  @param T The destination buffer. Must be at least 3 bytes long
95  @param A The integer to store.
96 
97  _Example:_
98  A @ref a_protocol_type_int3 "int <3>" with the value 1 is stored as:
99  ~~~~~~~~~~~~~~~~~~~~~
100  01 00 00
101  ~~~~~~~~~~~~~~~~~~~~~
102 */
103 static inline void int3store(uchar *T, uint A) {
104  *(T) = (uchar)(A);
105  *(T + 1) = (uchar)(A >> 8);
106  *(T + 2) = (uchar)(A >> 16);
107 }
108 
109 static inline void int5store(uchar *T, ulonglong A) {
110  *(T) = (uchar)(A);
111  *(T + 1) = (uchar)(A >> 8);
112  *(T + 2) = (uchar)(A >> 16);
113  *(T + 3) = (uchar)(A >> 24);
114  *(T + 4) = (uchar)(A >> 32);
115 }
116 
117 static inline void int6store(uchar *T, ulonglong A) {
118  *(T) = (uchar)(A);
119  *(T + 1) = (uchar)(A >> 8);
120  *(T + 2) = (uchar)(A >> 16);
121  *(T + 3) = (uchar)(A >> 24);
122  *(T + 4) = (uchar)(A >> 32);
123  *(T + 5) = (uchar)(A >> 40);
124 }
125 
126 #ifdef __cplusplus
127 
128 inline int16 sint2korr(const char *pT) {
129  return sint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
130 }
131 
132 inline uint16 uint2korr(const char *pT) {
133  return uint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
134 }
135 
136 inline uint32 uint3korr(const char *pT) {
137  return uint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
138 }
139 
140 inline int32 sint3korr(const char *pT) {
141  return sint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
142 }
143 
144 inline uint32 uint4korr(const char *pT) {
145  return uint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
146 }
147 
148 inline int32 sint4korr(const char *pT) {
149  return sint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
150 }
151 
152 inline ulonglong uint6korr(const char *pT) {
153  return uint6korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
154 }
155 
156 inline ulonglong uint8korr(const char *pT) {
157  return uint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
158 }
159 
160 inline longlong sint8korr(const char *pT) {
161  return sint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
162 }
163 
164 inline void int2store(char *pT, uint16 A) {
165  int2store(static_cast<uchar *>(static_cast<void *>(pT)), A);
166 }
167 
168 inline void int3store(char *pT, uint A) {
169  int3store(static_cast<uchar *>(static_cast<void *>(pT)), A);
170 }
171 
172 inline void int4store(char *pT, uint32 A) {
173  int4store(static_cast<uchar *>(static_cast<void *>(pT)), A);
174 }
175 
176 inline void int5store(char *pT, ulonglong A) {
177  int5store(static_cast<uchar *>(static_cast<void *>(pT)), A);
178 }
179 
180 inline void int6store(char *pT, ulonglong A) {
181  int6store(static_cast<uchar *>(static_cast<void *>(pT)), A);
182 }
183 
184 inline void int8store(char *pT, ulonglong A) {
185  int8store(static_cast<uchar *>(static_cast<void *>(pT)), A);
186 }
187 
188 /*
189  Functions for reading and storing in machine format from/to
190  short/long to/from some place in memory V should be a variable
191  and M a pointer to byte.
192 */
193 
194 inline void float4store(char *V, float M) {
195  float4store(static_cast<uchar *>(static_cast<void *>(V)), M);
196 }
197 
198 inline double float8get(const char *M) {
199  return float8get(static_cast<const uchar *>(static_cast<const void *>(M)));
200 }
201 
202 inline void float8store(char *V, double M) {
203  float8store(static_cast<uchar *>(static_cast<void *>(V)), M);
204 }
205 
206 /*
207  Functions that have the same behavior on little- and big-endian.
208 */
209 
210 inline float floatget(const uchar *ptr) {
211  float val;
212  memcpy(&val, ptr, sizeof(val));
213  return val;
214 }
215 
216 inline void floatstore(uchar *ptr, float val) {
217  memcpy(ptr, &val, sizeof(val));
218 }
219 
220 inline double doubleget(const uchar *ptr) {
221  double val;
222  memcpy(&val, ptr, sizeof(val));
223  return val;
224 }
225 
226 inline void doublestore(uchar *ptr, double val) {
227  memcpy(ptr, &val, sizeof(val));
228 }
229 
230 inline uint16 ushortget(const uchar *ptr) {
231  uint16 val;
232  memcpy(&val, ptr, sizeof(val));
233  return val;
234 }
235 
236 inline int16 shortget(const uchar *ptr) {
237  int16 val;
238  memcpy(&val, ptr, sizeof(val));
239  return val;
240 }
241 
242 inline void shortstore(uchar *ptr, int16 val) {
243  memcpy(ptr, &val, sizeof(val));
244 }
245 
246 inline int32 longget(const uchar *ptr) {
247  int32 val;
248  memcpy(&val, ptr, sizeof(val));
249  return val;
250 }
251 
252 inline void longstore(uchar *ptr, int32 val) { memcpy(ptr, &val, sizeof(val)); }
253 
254 inline uint32 ulongget(const uchar *ptr) {
255  uint32 val;
256  memcpy(&val, ptr, sizeof(val));
257  return val;
258 }
259 
260 inline longlong longlongget(const uchar *ptr) {
261  longlong val;
262  memcpy(&val, ptr, sizeof(val));
263  return val;
264 }
265 
266 inline void longlongstore(uchar *ptr, longlong val) {
267  memcpy(ptr, &val, sizeof(val));
268 }
269 
270 /*
271  Functions for big-endian loads and stores. These are safe to use
272  no matter what the compiler, CPU or alignment, and also with -fstrict-aliasing.
273 
274  The stores return a pointer just past the value that was written.
275 */
276 
277 inline uint16 load16be(const char *ptr) {
278  uint16 val;
279  memcpy(&val, ptr, sizeof(val));
280  return ntohs(val);
281 }
282 
283 inline uint32 load32be(const char *ptr) {
284  uint32 val;
285  memcpy(&val, ptr, sizeof(val));
286  return ntohl(val);
287 }
288 
289 ALWAYS_INLINE char *store16be(char *ptr, uint16 val) {
290 #if defined(_MSC_VER)
291  // _byteswap_ushort is an intrinsic on MSVC, but htons is not.
292  val = _byteswap_ushort(val);
293 #else
294  val = htons(val);
295 #endif
296  memcpy(ptr, &val, sizeof(val));
297  return ptr + sizeof(val);
298 }
299 
300 inline char *store32be(char *ptr, uint32 val) {
301  val = htonl(val);
302  memcpy(ptr, &val, sizeof(val));
303  return ptr + sizeof(val);
304 }
305 
306 // Adapters for using uchar * instead of char *.
307 
308 inline uint16 load16be(const uchar *ptr) {
309  return load16be(pointer_cast<const char *>(ptr));
310 }
311 
312 inline uint32 load32be(const uchar *ptr) {
313  return load32be(pointer_cast<const char *>(ptr));
314 }
315 
317  return pointer_cast<uchar *>(store16be(pointer_cast<char *>(ptr), val));
318 }
319 
320 inline uchar *store32be(uchar *ptr, uint32 val) {
321  return pointer_cast<uchar *>(store32be(pointer_cast<char *>(ptr), val));
322 }
323 
324 #endif /* __cplusplus */
325 
326 #endif /* MY_BYTEORDER_INCLUDED */
float floatget(const uchar *ptr)
Definition: my_byteorder.h:210
static ulonglong uint6korr(const uchar *A)
Definition: my_byteorder.h:83
int32 longget(const uchar *ptr)
Definition: my_byteorder.h:246
unsigned long long int ulonglong
Definition: my_inttypes.h:55
uint32 ulongget(const uchar *ptr)
Definition: my_byteorder.h:254
unsigned char uchar
Definition: my_inttypes.h:51
T pointer_cast(void *p)
Casts from one pointer type, to another, without using reinterpret_cast or C-style cast: foo f; bar *...
Definition: template_utils.h:73
double doubleget(const uchar *ptr)
Definition: my_byteorder.h:220
Some integer typedefs for easier portability.
void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:184
static ulonglong uint5korr(const uchar *A)
Definition: my_byteorder.h:77
longlong longlongget(const uchar *ptr)
Definition: my_byteorder.h:260
Endianness-independent definitions (little_endian.h contains optimized versions if you know you are o...
void doublestore(uchar *ptr, double val)
Definition: my_byteorder.h:226
uint16 uint2korr(const char *pT)
Definition: my_byteorder.h:132
char * store32be(char *ptr, uint32 val)
Definition: my_byteorder.h:300
uint16 load16be(const char *ptr)
Definition: my_byteorder.h:277
void shortstore(uchar *ptr, int16 val)
Definition: my_byteorder.h:242
int16 shortget(const uchar *ptr)
Definition: my_byteorder.h:236
uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:144
Header for compiler-dependent features.
void floatstore(uchar *ptr, float val)
Definition: my_byteorder.h:216
void float4store(char *V, float M)
Definition: my_byteorder.h:194
double float8get(const char *M)
Definition: my_byteorder.h:198
uint16_t uint16
Definition: my_inttypes.h:64
unsigned int uint
Definition: uca-dump.cc:29
long long int longlong
Definition: my_inttypes.h:54
static int32 sint3korr(const uchar *A)
Definition: my_byteorder.h:64
uint32_t uint32
Definition: my_inttypes.h:66
static void int6store(uchar *T, ulonglong A)
Definition: my_byteorder.h:117
#define M
Definition: ctype-tis620.cc:73
Data in little-endian format.
int16 sint2korr(const char *pT)
Definition: my_byteorder.h:128
static void int3store(uchar *T, uint A)
int3store
Definition: my_byteorder.h:103
ulonglong uint8korr(const char *pT)
Definition: my_byteorder.h:156
int32 sint4korr(const char *pT)
Definition: my_byteorder.h:148
uint16 ushortget(const uchar *ptr)
Definition: my_byteorder.h:230
int32_t int32
Definition: my_inttypes.h:65
#define L
Definition: ctype-tis620.cc:75
uint32 load32be(const char *ptr)
Definition: my_byteorder.h:283
void float8store(char *V, double M)
Definition: my_byteorder.h:202
static void int5store(uchar *T, ulonglong A)
Definition: my_byteorder.h:109
ALWAYS_INLINE char * store16be(char *ptr, uint16 val)
Definition: my_byteorder.h:289
void longlongstore(uchar *ptr, longlong val)
Definition: my_byteorder.h:266
void int4store(char *pT, uint32 A)
Definition: my_byteorder.h:172
static uint32 uint3korr(const uchar *A)
Definition: my_byteorder.h:72
void int2store(char *pT, uint16 A)
Definition: my_byteorder.h:164
void longstore(uchar *ptr, int32 val)
Definition: my_byteorder.h:252
int16_t int16
Definition: my_inttypes.h:63
longlong sint8korr(const char *pT)
Definition: my_byteorder.h:160
#define ALWAYS_INLINE
Definition: my_compiler.h:109