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