1 #ifndef MY_BYTEORDER_INCLUDED
2 #define MY_BYTEORDER_INCLUDED
3 
4 /* Copyright (c) 2001, 2021, 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    Without limiting anything contained in the foregoing, this file,
18    which is part of C Driver for MySQL (Connector/C), is also subject to the
19    Universal FOSS Exception, version 1.0, a copy of which can be found at
20    http://oss.oracle.com/licenses/universal-foss-exception.
21 
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25    GNU General Public License, version 2.0, for more details.
26 
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
30 
31 
32 /*
33   Functions for reading and storing in machine independent
34   format (low byte first). There are 'korr' (assume 'corrector') variants
35   for integer types, but 'get' (assume 'getter') for floating point types.
36 */
37 #if defined(__i386__) || defined(_WIN32) || defined(__x86_64__)
38 #include "byte_order_generic_x86.h"
39 #else
40 #include "byte_order_generic.h"
41 #endif
42 
sint3korr(const uchar * A)43 static inline int32 sint3korr(const uchar *A)
44 {
45   return
46     ((int32) (((A[2]) & 128) ?
47               (((uint32) 255L << 24) |
48                (((uint32) A[2]) << 16) |
49                (((uint32) A[1]) << 8) |
50                ((uint32) A[0])) :
51               (((uint32) A[2]) << 16) |
52               (((uint32) A[1]) << 8) |
53               ((uint32) A[0])))
54     ;
55 }
56 
uint3korr(const uchar * A)57 static inline uint32 uint3korr(const uchar *A)
58 {
59   return
60     (uint32) (((uint32) (A[0])) +
61               (((uint32) (A[1])) << 8) +
62               (((uint32) (A[2])) << 16))
63     ;
64 }
65 
uint5korr(const uchar * A)66 static inline ulonglong uint5korr(const uchar *A)
67 {
68   return
69     ((ulonglong)(((uint32) (A[0])) +
70                  (((uint32) (A[1])) << 8) +
71                  (((uint32) (A[2])) << 16) +
72                  (((uint32) (A[3])) << 24)) +
73      (((ulonglong) (A[4])) << 32))
74     ;
75 }
76 
uint6korr(const uchar * A)77 static inline ulonglong uint6korr(const uchar *A)
78 {
79   return
80     ((ulonglong)(((uint32) (A[0]))          +
81                  (((uint32) (A[1])) << 8)   +
82                  (((uint32) (A[2])) << 16)  +
83                  (((uint32) (A[3])) << 24)) +
84      (((ulonglong) (A[4])) << 32) +
85      (((ulonglong) (A[5])) << 40))
86     ;
87 }
88 
int3store(uchar * T,uint A)89 static inline void int3store(uchar *T, uint A)
90 {
91   *(T)=   (uchar) (A);
92   *(T+1)= (uchar) (A >> 8);
93   *(T+2)= (uchar) (A >> 16);
94 }
95 
int5store(uchar * T,ulonglong A)96 static inline void int5store(uchar *T, ulonglong A)
97 {
98   *(T)=   (uchar) (A);
99   *(T+1)= (uchar) (A >> 8);
100   *(T+2)= (uchar) (A >> 16);
101   *(T+3)= (uchar) (A >> 24);
102   *(T+4)= (uchar) (A >> 32);
103 }
104 
int6store(uchar * T,ulonglong A)105 static inline void int6store(uchar *T, ulonglong A)
106 {
107   *(T)=   (uchar) (A);
108   *(T+1)= (uchar) (A >> 8);
109   *(T+2)= (uchar) (A >> 16);
110   *(T+3)= (uchar) (A >> 24);
111   *(T+4)= (uchar) (A >> 32);
112   *(T+5)= (uchar) (A >> 40);
113 }
114 
115 #ifdef __cplusplus
116 
sint2korr(const char * pT)117 static inline int16 sint2korr(const char *pT)
118 {
119   return sint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
120 }
121 
uint2korr(const char * pT)122 static inline uint16    uint2korr(const char *pT)
123 {
124   return uint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
125 }
126 
uint3korr(const char * pT)127 static inline uint32    uint3korr(const char *pT)
128 {
129   return uint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
130 }
131 
sint3korr(const char * pT)132 static inline int32     sint3korr(const char *pT)
133 {
134   return sint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
135 }
136 
uint4korr(const char * pT)137 static inline uint32    uint4korr(const char *pT)
138 {
139   return uint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
140 }
141 
sint4korr(const char * pT)142 static inline int32     sint4korr(const char *pT)
143 {
144   return sint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
145 }
146 
uint6korr(const char * pT)147 static inline ulonglong uint6korr(const char *pT)
148 {
149   return uint6korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
150 }
151 
uint8korr(const char * pT)152 static inline ulonglong uint8korr(const char *pT)
153 {
154   return uint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
155 }
156 
sint8korr(const char * pT)157 static inline longlong  sint8korr(const char *pT)
158 {
159   return sint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
160 }
161 
162 
int2store(char * pT,uint16 A)163 static inline void int2store(char *pT, uint16 A)
164 {
165   int2store(static_cast<uchar*>(static_cast<void*>(pT)), A);
166 }
167 
int3store(char * pT,uint A)168 static inline void int3store(char *pT, uint A)
169 {
170   int3store(static_cast<uchar*>(static_cast<void*>(pT)), A);
171 }
172 
int4store(char * pT,uint32 A)173 static inline void int4store(char *pT, uint32 A)
174 {
175   int4store(static_cast<uchar*>(static_cast<void*>(pT)), A);
176 }
177 
int5store(char * pT,ulonglong A)178 static inline void int5store(char *pT, ulonglong A)
179 {
180   int5store(static_cast<uchar*>(static_cast<void*>(pT)), A);
181 }
182 
int6store(char * pT,ulonglong A)183 static inline void int6store(char *pT, ulonglong A)
184 {
185   int6store(static_cast<uchar*>(static_cast<void*>(pT)), A);
186 }
187 
int8store(char * pT,ulonglong A)188 static inline void int8store(char *pT, ulonglong A)
189 {
190   int8store(static_cast<uchar*>(static_cast<void*>(pT)), A);
191 }
192 
193 #endif  /* __cplusplus */
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 #ifdef WORDS_BIGENDIAN
201 #include "big_endian.h"
202 #else
203 #include "little_endian.h"
204 #endif
205 
206 #ifdef __cplusplus
207 
float4store(char * V,float M)208 static inline void float4store(char *V, float M)
209 {
210   float4store(static_cast<uchar*>(static_cast<void*>(V)), M);
211 }
212 
float8get(double * V,const char * M)213 static inline void float8get(double *V, const char *M)
214 {
215   float8get(V, static_cast<const uchar*>(static_cast<const void*>(M)));
216 }
217 
float8store(char * V,double M)218 static inline void float8store(char *V, double M)
219 {
220   float8store(static_cast<uchar*>(static_cast<void*>(V)), M);
221 }
222 
223 #endif /* __cplusplus */
224 
225 #endif /* MY_BYTEORDER_INCLUDED */
226