1 #ifndef LITTLE_ENDIAN_INCLUDED
2 #define LITTLE_ENDIAN_INCLUDED
3 /* Copyright (c) 2012, 2021, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15
16 Without limiting anything contained in the foregoing, this file,
17 which is part of C Driver for MySQL (Connector/C), is also subject to the
18 Universal FOSS Exception, version 1.0, a copy of which can be found at
19 http://oss.oracle.com/licenses/universal-foss-exception.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License, version 2.0, for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
29
30 /*
31 Data in little-endian format.
32 */
33
34 #include <string.h>
35
float4get(float * V,const uchar * M)36 static inline void float4get (float *V, const uchar *M)
37 {
38 memcpy(V, (M), sizeof(float));
39 }
40
float4store(uchar * V,float M)41 static inline void float4store(uchar *V, float M)
42 {
43 memcpy(V, (&M), sizeof(float));
44 }
45
float8get(double * V,const uchar * M)46 static inline void float8get (double *V, const uchar *M)
47 {
48 memcpy(V, M, sizeof(double));
49 }
50
float8store(uchar * V,double M)51 static inline void float8store(uchar *V, double M)
52 {
53 memcpy(V, &M, sizeof(double));
54 }
55
floatget(float * V,const uchar * M)56 static inline void floatget (float *V, const uchar *M) { float4get(V, M); }
floatstore(uchar * V,float M)57 static inline void floatstore (uchar *V, float M) { float4store(V, M); }
58
59 /* Bi-endian hardware.... */
60 #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
doublestore(uchar * T,double V)61 static inline void doublestore(uchar *T, double V)
62 { *(((char*)T)+0)=(char) ((uchar *) &V)[4];
63 *(((char*)T)+1)=(char) ((uchar *) &V)[5];
64 *(((char*)T)+2)=(char) ((uchar *) &V)[6];
65 *(((char*)T)+3)=(char) ((uchar *) &V)[7];
66 *(((char*)T)+4)=(char) ((uchar *) &V)[0];
67 *(((char*)T)+5)=(char) ((uchar *) &V)[1];
68 *(((char*)T)+6)=(char) ((uchar *) &V)[2];
69 *(((char*)T)+7)=(char) ((uchar *) &V)[3]; }
doubleget(double * V,const uchar * M)70 static inline void doubleget(double *V, const uchar *M)
71 { double def_temp;
72 ((uchar*) &def_temp)[0]=(M)[4];
73 ((uchar*) &def_temp)[1]=(M)[5];
74 ((uchar*) &def_temp)[2]=(M)[6];
75 ((uchar*) &def_temp)[3]=(M)[7];
76 ((uchar*) &def_temp)[4]=(M)[0];
77 ((uchar*) &def_temp)[5]=(M)[1];
78 ((uchar*) &def_temp)[6]=(M)[2];
79 ((uchar*) &def_temp)[7]=(M)[3];
80 (*V) = def_temp; }
81
82 #else /* Bi-endian hardware.... */
83
doublestore(uchar * T,double V)84 static inline void doublestore(uchar *T, double V) { memcpy(T, &V, sizeof(double)); }
doubleget(double * V,const uchar * M)85 static inline void doubleget (double *V, const uchar *M) { memcpy(V, M, sizeof(double)); }
86
87 #endif /* Bi-endian hardware.... */
88
ushortget(uint16 * V,const uchar * pM)89 static inline void ushortget(uint16 *V, const uchar *pM) { *V= uint2korr(pM); }
shortget(int16 * V,const uchar * pM)90 static inline void shortget (int16 *V, const uchar *pM) { *V= sint2korr(pM); }
longget(int32 * V,const uchar * pM)91 static inline void longget (int32 *V, const uchar *pM) { *V= sint4korr(pM); }
ulongget(uint32 * V,const uchar * pM)92 static inline void ulongget (uint32 *V, const uchar *pM) { *V= uint4korr(pM); }
shortstore(uchar * T,int16 V)93 static inline void shortstore(uchar *T, int16 V) { int2store(T, V); }
longstore(uchar * T,int32 V)94 static inline void longstore (uchar *T, int32 V) { int4store(T, V); }
95
longlongget(longlong * V,const uchar * M)96 static inline void longlongget(longlong *V, const uchar *M)
97 {
98 memcpy(V, (M), sizeof(ulonglong));
99 }
longlongstore(uchar * T,longlong V)100 static inline void longlongstore(uchar *T, longlong V)
101 {
102 memcpy((T), &V, sizeof(ulonglong));
103 }
104
105 #endif /* LITTLE_ENDIAN_INCLUDED */
106