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