MySQL 8.3.0
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, 2023, Oracle and/or its affiliates.
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
64static 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
72static inline uint32 uint3korr(const uchar *A) {
73 return (uint32)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
74 (((uint32)(A[2])) << 16));
75}
76
77static 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
83static 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
89static inline ulonglong uint7korr(const uchar *A) {
90 return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
91 (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
92 (((ulonglong)(A[4])) << 32) + (((ulonglong)(A[5])) << 40) +
93 (((ulonglong)(A[6])) << 48));
94}
95
96/**
97 int3store
98
99 Stores an unsigned integer in a platform independent way
100
101 @param T The destination buffer. Must be at least 3 bytes long
102 @param A The integer to store.
103
104 _Example:_
105 A @ref a_protocol_type_int3 "int <3>" with the value 1 is stored as:
106 ~~~~~~~~~~~~~~~~~~~~~
107 01 00 00
108 ~~~~~~~~~~~~~~~~~~~~~
109*/
110static inline void int3store(uchar *T, uint A) {
111 *(T) = (uchar)(A);
112 *(T + 1) = (uchar)(A >> 8);
113 *(T + 2) = (uchar)(A >> 16);
114}
115
116static inline void int5store(uchar *T, ulonglong A) {
117 *(T) = (uchar)(A);
118 *(T + 1) = (uchar)(A >> 8);
119 *(T + 2) = (uchar)(A >> 16);
120 *(T + 3) = (uchar)(A >> 24);
121 *(T + 4) = (uchar)(A >> 32);
122}
123
124static inline void int6store(uchar *T, ulonglong A) {
125 *(T) = (uchar)(A);
126 *(T + 1) = (uchar)(A >> 8);
127 *(T + 2) = (uchar)(A >> 16);
128 *(T + 3) = (uchar)(A >> 24);
129 *(T + 4) = (uchar)(A >> 32);
130 *(T + 5) = (uchar)(A >> 40);
131}
132
133#ifdef __cplusplus
134
135inline int16 sint2korr(const char *pT) {
136 return sint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
137}
138
139inline uint16 uint2korr(const char *pT) {
140 return uint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
141}
142
143inline uint32 uint3korr(const char *pT) {
144 return uint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
145}
146
147inline int32 sint3korr(const char *pT) {
148 return sint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
149}
150
151inline uint32 uint4korr(const char *pT) {
152 return uint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
153}
154
155inline int32 sint4korr(const char *pT) {
156 return sint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
157}
158
159inline ulonglong uint6korr(const char *pT) {
160 return uint6korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
161}
162
163inline ulonglong uint8korr(const char *pT) {
164 return uint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
165}
166
167inline longlong sint8korr(const char *pT) {
168 return sint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
169}
170
171inline void int2store(char *pT, uint16 A) {
172 int2store(static_cast<uchar *>(static_cast<void *>(pT)), A);
173}
174
175inline void int3store(char *pT, uint A) {
176 int3store(static_cast<uchar *>(static_cast<void *>(pT)), A);
177}
178
179inline void int4store(char *pT, uint32 A) {
180 int4store(static_cast<uchar *>(static_cast<void *>(pT)), A);
181}
182
183inline void int5store(char *pT, ulonglong A) {
184 int5store(static_cast<uchar *>(static_cast<void *>(pT)), A);
185}
186
187inline void int6store(char *pT, ulonglong A) {
188 int6store(static_cast<uchar *>(static_cast<void *>(pT)), A);
189}
190
191inline void int8store(char *pT, ulonglong A) {
192 int8store(static_cast<uchar *>(static_cast<void *>(pT)), A);
193}
194
195/*
196 Functions for reading and storing in machine format from/to
197 short/long to/from some place in memory V should be a variable
198 and M a pointer to byte.
199*/
200
201inline void float4store(char *V, float M) {
202 float4store(static_cast<uchar *>(static_cast<void *>(V)), M);
203}
204
205inline double float8get(const char *M) {
206 return float8get(static_cast<const uchar *>(static_cast<const void *>(M)));
207}
208
209inline void float8store(char *V, double M) {
210 float8store(static_cast<uchar *>(static_cast<void *>(V)), M);
211}
212
213/*
214 Functions that have the same behavior on little- and big-endian.
215*/
216
217inline float floatget(const uchar *ptr) {
218 float val;
219 memcpy(&val, ptr, sizeof(val));
220 return val;
221}
222
223inline void floatstore(uchar *ptr, float val) {
224 memcpy(ptr, &val, sizeof(val));
225}
226
227inline double doubleget(const uchar *ptr) {
228 double val;
229 memcpy(&val, ptr, sizeof(val));
230 return val;
231}
232
233inline void doublestore(uchar *ptr, double val) {
234 memcpy(ptr, &val, sizeof(val));
235}
236
237inline uint16 ushortget(const uchar *ptr) {
238 uint16 val;
239 memcpy(&val, ptr, sizeof(val));
240 return val;
241}
242
243inline int16 shortget(const uchar *ptr) {
244 int16 val;
245 memcpy(&val, ptr, sizeof(val));
246 return val;
247}
248
249inline void shortstore(uchar *ptr, int16 val) {
250 memcpy(ptr, &val, sizeof(val));
251}
252
253inline int32 longget(const uchar *ptr) {
254 int32 val;
255 memcpy(&val, ptr, sizeof(val));
256 return val;
257}
258
259inline void longstore(uchar *ptr, int32 val) { memcpy(ptr, &val, sizeof(val)); }
260
261inline uint32 ulongget(const uchar *ptr) {
262 uint32 val;
263 memcpy(&val, ptr, sizeof(val));
264 return val;
265}
266
267inline longlong longlongget(const uchar *ptr) {
268 longlong val;
269 memcpy(&val, ptr, sizeof(val));
270 return val;
271}
272
273inline void longlongstore(uchar *ptr, longlong val) {
274 memcpy(ptr, &val, sizeof(val));
275}
276
277/*
278 Functions for big-endian loads and stores. These are safe to use
279 no matter what the compiler, CPU or alignment, and also with -fstrict-aliasing.
280
281 The stores return a pointer just past the value that was written.
282*/
283
284inline uint16 load16be(const char *ptr) {
285 uint16 val;
286 memcpy(&val, ptr, sizeof(val));
287 return ntohs(val);
288}
289
290inline uint32 load32be(const char *ptr) {
291 uint32 val;
292 memcpy(&val, ptr, sizeof(val));
293 return ntohl(val);
294}
295
296ALWAYS_INLINE char *store16be(char *ptr, uint16 val) {
297#if defined(_MSC_VER)
298 // _byteswap_ushort is an intrinsic on MSVC, but htons is not.
299 val = _byteswap_ushort(val);
300#else
301 val = htons(val);
302#endif
303 memcpy(ptr, &val, sizeof(val));
304 return ptr + sizeof(val);
305}
306
307inline char *store32be(char *ptr, uint32 val) {
308 val = htonl(val);
309 memcpy(ptr, &val, sizeof(val));
310 return ptr + sizeof(val);
311}
312
313// Adapters for using uchar * instead of char *.
314
315inline uint16 load16be(const uchar *ptr) {
316 return load16be(pointer_cast<const char *>(ptr));
317}
318
319inline uint32 load32be(const uchar *ptr) {
320 return load32be(pointer_cast<const char *>(ptr));
321}
322
324 return pointer_cast<uchar *>(store16be(pointer_cast<char *>(ptr), val));
325}
326
327inline uchar *store32be(uchar *ptr, uint32 val) {
328 return pointer_cast<uchar *>(store32be(pointer_cast<char *>(ptr), val));
329}
330
331#endif /* __cplusplus */
332
333#endif /* MY_BYTEORDER_INCLUDED */
Endianness-independent definitions (little_endian.h contains optimized versions if you know you are o...
#define M
Definition: ctype-tis620.cc:72
Data in little-endian format.
static int32 sint3korr(const uchar *A)
Definition: my_byteorder.h:64
ALWAYS_INLINE char * store16be(char *ptr, uint16 val)
Definition: my_byteorder.h:296
void float8store(char *V, double M)
Definition: my_byteorder.h:209
double float8get(const char *M)
Definition: my_byteorder.h:205
double doubleget(const uchar *ptr)
Definition: my_byteorder.h:227
void longlongstore(uchar *ptr, longlong val)
Definition: my_byteorder.h:273
void float4store(char *V, float M)
Definition: my_byteorder.h:201
float floatget(const uchar *ptr)
Definition: my_byteorder.h:217
int32 longget(const uchar *ptr)
Definition: my_byteorder.h:253
longlong longlongget(const uchar *ptr)
Definition: my_byteorder.h:267
static ulonglong uint7korr(const uchar *A)
Definition: my_byteorder.h:89
void shortstore(uchar *ptr, int16 val)
Definition: my_byteorder.h:249
static void int6store(uchar *T, ulonglong A)
Definition: my_byteorder.h:124
void int4store(char *pT, uint32 A)
Definition: my_byteorder.h:179
int16 sint2korr(const char *pT)
Definition: my_byteorder.h:135
uint16 load16be(const char *ptr)
Definition: my_byteorder.h:284
int16 shortget(const uchar *ptr)
Definition: my_byteorder.h:243
ulonglong uint8korr(const char *pT)
Definition: my_byteorder.h:163
static ulonglong uint6korr(const uchar *A)
Definition: my_byteorder.h:83
static void int5store(uchar *T, ulonglong A)
Definition: my_byteorder.h:116
static void int3store(uchar *T, uint A)
int3store
Definition: my_byteorder.h:110
void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:191
char * store32be(char *ptr, uint32 val)
Definition: my_byteorder.h:307
static uint32 uint3korr(const uchar *A)
Definition: my_byteorder.h:72
uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:151
int32 sint4korr(const char *pT)
Definition: my_byteorder.h:155
uint16 uint2korr(const char *pT)
Definition: my_byteorder.h:139
void longstore(uchar *ptr, int32 val)
Definition: my_byteorder.h:259
longlong sint8korr(const char *pT)
Definition: my_byteorder.h:167
uint16 ushortget(const uchar *ptr)
Definition: my_byteorder.h:237
static ulonglong uint5korr(const uchar *A)
Definition: my_byteorder.h:77
void floatstore(uchar *ptr, float val)
Definition: my_byteorder.h:223
uint32 ulongget(const uchar *ptr)
Definition: my_byteorder.h:261
uint32 load32be(const char *ptr)
Definition: my_byteorder.h:290
void int2store(char *pT, uint16 A)
Definition: my_byteorder.h:171
void doublestore(uchar *ptr, double val)
Definition: my_byteorder.h:233
Header for compiler-dependent features.
#define ALWAYS_INLINE
Definition: my_compiler.h:98
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
int16_t int16
Definition: my_inttypes.h:63
int32_t int32
Definition: my_inttypes.h:65
uint16_t uint16
Definition: my_inttypes.h:64
uint32_t uint32
Definition: my_inttypes.h:66