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