MySQL  8.0.18
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, 2017, 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 <string.h>
36 #include <sys/types.h>
37 
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41 
42 #if defined(_MSC_VER)
43 #include <stdlib.h>
44 #endif
45 
46 #if defined(_WIN32) && defined(WIN32_LEAN_AND_MEAN)
47 #include <winsock2.h>
48 #endif
49 
50 #ifdef WORDS_BIGENDIAN
51 #include "big_endian.h" // IWYU pragma: export
52 #else
53 #include "little_endian.h" // IWYU pragma: export
54 #endif
55 
56 #include "my_inttypes.h"
57 
58 #ifdef __cplusplus
59 #include "template_utils.h"
60 #endif
61 
62 static inline int32 sint3korr(const uchar *A) {
63  return ((int32)(((A[2]) & 128)
64  ? (((uint32)255L << 24) | (((uint32)A[2]) << 16) |
65  (((uint32)A[1]) << 8) | ((uint32)A[0]))
66  : (((uint32)A[2]) << 16) | (((uint32)A[1]) << 8) |
67  ((uint32)A[0])));
68 }
69 
70 static inline uint32 uint3korr(const uchar *A) {
71  return (uint32)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
72  (((uint32)(A[2])) << 16));
73 }
74 
75 static inline ulonglong uint5korr(const uchar *A) {
76  return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
77  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
78  (((ulonglong)(A[4])) << 32));
79 }
80 
81 static inline ulonglong uint6korr(const uchar *A) {
82  return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
83  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
84  (((ulonglong)(A[4])) << 32) + (((ulonglong)(A[5])) << 40));
85 }
86 
87 /**
88  int3store
89 
90  Stores an unsinged integer in a platform independent way
91 
92  @param T The destination buffer. Must be at least 3 bytes long
93  @param A The integer to store.
94 
95  _Example:_
96  A @ref a_protocol_type_int3 "int <3>" with the value 1 is stored as:
97  ~~~~~~~~~~~~~~~~~~~~~
98  01 00 00
99  ~~~~~~~~~~~~~~~~~~~~~
100 */
101 static inline void int3store(uchar *T, uint A) {
102  *(T) = (uchar)(A);
103  *(T + 1) = (uchar)(A >> 8);
104  *(T + 2) = (uchar)(A >> 16);
105 }
106 
107 static inline void int5store(uchar *T, ulonglong A) {
108  *(T) = (uchar)(A);
109  *(T + 1) = (uchar)(A >> 8);
110  *(T + 2) = (uchar)(A >> 16);
111  *(T + 3) = (uchar)(A >> 24);
112  *(T + 4) = (uchar)(A >> 32);
113 }
114 
115 static inline void int6store(uchar *T, ulonglong A) {
116  *(T) = (uchar)(A);
117  *(T + 1) = (uchar)(A >> 8);
118  *(T + 2) = (uchar)(A >> 16);
119  *(T + 3) = (uchar)(A >> 24);
120  *(T + 4) = (uchar)(A >> 32);
121  *(T + 5) = (uchar)(A >> 40);
122 }
123 
124 #ifdef __cplusplus
125 
126 static inline int16 sint2korr(const char *pT) {
127  return sint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
128 }
129 
130 static inline uint16 uint2korr(const char *pT) {
131  return uint2korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
132 }
133 
134 static inline uint32 uint3korr(const char *pT) {
135  return uint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
136 }
137 
138 static inline int32 sint3korr(const char *pT) {
139  return sint3korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
140 }
141 
142 static inline uint32 uint4korr(const char *pT) {
143  return uint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
144 }
145 
146 static inline int32 sint4korr(const char *pT) {
147  return sint4korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
148 }
149 
150 static inline ulonglong uint6korr(const char *pT) {
151  return uint6korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
152 }
153 
154 static inline ulonglong uint8korr(const char *pT) {
155  return uint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
156 }
157 
158 static inline longlong sint8korr(const char *pT) {
159  return sint8korr(static_cast<const uchar *>(static_cast<const void *>(pT)));
160 }
161 
162 static inline void int2store(char *pT, uint16 A) {
163  int2store(static_cast<uchar *>(static_cast<void *>(pT)), A);
164 }
165 
166 static inline void int3store(char *pT, uint A) {
167  int3store(static_cast<uchar *>(static_cast<void *>(pT)), A);
168 }
169 
170 static inline void int4store(char *pT, uint32 A) {
171  int4store(static_cast<uchar *>(static_cast<void *>(pT)), A);
172 }
173 
174 static inline void int5store(char *pT, ulonglong A) {
175  int5store(static_cast<uchar *>(static_cast<void *>(pT)), A);
176 }
177 
178 static inline void int6store(char *pT, ulonglong A) {
179  int6store(static_cast<uchar *>(static_cast<void *>(pT)), A);
180 }
181 
182 static inline void int8store(char *pT, ulonglong A) {
183  int8store(static_cast<uchar *>(static_cast<void *>(pT)), A);
184 }
185 
186 /*
187  Functions for reading and storing in machine format from/to
188  short/long to/from some place in memory V should be a variable
189  and M a pointer to byte.
190 */
191 
192 static inline void float4store(char *V, float M) {
193  float4store(static_cast<uchar *>(static_cast<void *>(V)), M);
194 }
195 
196 static inline void float8get(double *V, const char *M) {
197  float8get(V, static_cast<const uchar *>(static_cast<const void *>(M)));
198 }
199 
200 static inline void float8store(char *V, double M) {
201  float8store(static_cast<uchar *>(static_cast<void *>(V)), M);
202 }
203 
204 /*
205  Functions for big-endian loads and stores. These are safe to use
206  no matter what the compiler, CPU or alignment, and also with -fstrict-aliasing.
207 
208  The stores return a pointer just past the value that was written.
209 */
210 
211 static inline uint16 load16be(const char *ptr) {
212  uint16 val;
213  memcpy(&val, ptr, sizeof(val));
214  return ntohs(val);
215 }
216 
217 static inline uint32 load32be(const char *ptr) {
218  uint32 val;
219  memcpy(&val, ptr, sizeof(val));
220  return ntohl(val);
221 }
222 
223 static ALWAYS_INLINE char *store16be(char *ptr, uint16 val) {
224 #if defined(_MSC_VER)
225  // _byteswap_ushort is an intrinsic on MSVC, but htons is not.
226  val = _byteswap_ushort(val);
227 #else
228  val = htons(val);
229 #endif
230  memcpy(ptr, &val, sizeof(val));
231  return ptr + sizeof(val);
232 }
233 
234 static inline char *store32be(char *ptr, uint32 val) {
235  val = htonl(val);
236  memcpy(ptr, &val, sizeof(val));
237  return ptr + sizeof(val);
238 }
239 
240 // Adapters for using uchar * instead of char *.
241 
242 static inline uint16 load16be(const uchar *ptr) {
243  return load16be(pointer_cast<const char *>(ptr));
244 }
245 
246 static inline uint32 load32be(const uchar *ptr) {
247  return load32be(pointer_cast<const char *>(ptr));
248 }
249 
251  return pointer_cast<uchar *>(store16be(pointer_cast<char *>(ptr), val));
252 }
253 
254 static inline uchar *store32be(uchar *ptr, uint32 val) {
255  return pointer_cast<uchar *>(store32be(pointer_cast<char *>(ptr), val));
256 }
257 
258 #endif /* __cplusplus */
259 
260 #endif /* MY_BYTEORDER_INCLUDED */
static int32 sint4korr(const char *pT)
Definition: my_byteorder.h:146
static ulonglong uint6korr(const uchar *A)
Definition: my_byteorder.h:81
unsigned long long int ulonglong
Definition: my_inttypes.h:55
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:70
static void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:182
Some integer typedefs for easier portability.
static uint32 load32be(const char *ptr)
Definition: my_byteorder.h:217
static ulonglong uint5korr(const uchar *A)
Definition: my_byteorder.h:75
Endianness-independent definitions (little_endian.h contains optimized versions if you know you are o...
static void float8get(double *V, const char *M)
Definition: my_byteorder.h:196
static uint16 load16be(const char *ptr)
Definition: my_byteorder.h:211
static longlong sint8korr(const char *pT)
Definition: my_byteorder.h:158
static void float4store(char *V, float M)
Definition: my_byteorder.h:192
static void float8store(char *V, double M)
Definition: my_byteorder.h:200
static uint16 uint2korr(const char *pT)
Definition: my_byteorder.h:130
static void int4store(char *pT, uint32 A)
Definition: my_byteorder.h:170
static int16 sint2korr(const char *pT)
Definition: my_byteorder.h:126
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:62
uint32_t uint32
Definition: my_inttypes.h:66
static void int6store(uchar *T, ulonglong A)
Definition: my_byteorder.h:115
#define M
Definition: ctype-tis620.cc:71
Data in little-endian format.
static void int3store(uchar *T, uint A)
int3store
Definition: my_byteorder.h:101
int32_t int32
Definition: my_inttypes.h:65
static ulonglong uint8korr(const char *pT)
Definition: my_byteorder.h:154
#define L
Definition: ctype-tis620.cc:73
static ALWAYS_INLINE char * store16be(char *ptr, uint16 val)
Definition: my_byteorder.h:223
static void int5store(uchar *T, ulonglong A)
Definition: my_byteorder.h:107
static uint32 uint3korr(const uchar *A)
Definition: my_byteorder.h:70
static uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:142
static char * store32be(char *ptr, uint32 val)
Definition: my_byteorder.h:234
int16_t int16
Definition: my_inttypes.h:63
#define ALWAYS_INLINE
Definition: my_compiler.h:109
static void int2store(char *pT, uint16 A)
Definition: my_byteorder.h:162