xref: /PHP-8.0/ext/mysqlnd/mysqlnd_portability.h (revision d4471c6a)
1 /* Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
2 This file is public domain and comes with NO WARRANTY of any kind */
3 
4 /*
5   Parts of the original, which are not applicable to mysqlnd have been removed.
6 
7   With small modifications, mostly casting but adding few more macros by
8   Andrey Hristov <andrey@php.net> . The additions are in the public domain and
9   were added to improve the header file, to get it more consistent.
10 */
11 
12 #ifndef MYSQLND_PORTABILITY_H
13 #define MYSQLND_PORTABILITY_H
14 
15 
16 
17 /* Comes from global.h as OFFSET, renamed to STRUCT_OFFSET */
18 #define STRUCT_OFFSET(t, f)   XtOffsetOf(t, f)
19 
20 #ifndef __attribute
21 #if !defined(__GNUC__)
22 #define __attribute(A)
23 #endif
24 #endif
25 
26 #ifdef __CYGWIN__
27 /* We use a Unix API, so pretend it's not Windows */
28 #undef WIN
29 #undef WIN32
30 #undef _WIN
31 #undef _WIN32
32 #undef _WIN64
33 #undef __WIN__
34 #undef __WIN32__
35 #endif /* __CYGWIN__ */
36 
37 #if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
38 #  include "ext/mysqlnd/config-win.h"
39 #endif /* _WIN32... */
40 
41 #if __STDC_VERSION__ < 199901L && !defined(atoll)
42   /* "inline" is a keyword */
43   #define atoll atol
44 #endif
45 
46 #include "php_stdint.h"
47 
48 #if SIZEOF_LONG_LONG > 4 && !defined(_LONG_LONG)
49 #define _LONG_LONG 1        /* For AIX string library */
50 #endif
51 
52 /* Go around some bugs in different OS and compilers */
53 
54 #if SIZEOF_LONG_LONG > 4
55 #define HAVE_LONG_LONG 1
56 #endif
57 
58 #ifdef PHP_WIN32
59 #define MYSQLND_SZ_T_SPEC "%Id"
60 #ifndef L64
61 #define L64(x) x##i64
62 #endif
63 #else
64 
65 #define MYSQLND_SZ_T_SPEC "%zd"
66 #ifndef L64
67 #define L64(x) x##LL
68 #endif
69 #endif
70 
71 
72 #define int1store(T,A)	do { *((int8_t*) (T)) = (A); } while(0)
73 #define uint1korr(A)	(*(((uint8_t*)(A))))
74 
75 /* Bit values are sent in reverted order of bytes, compared to normal !!! */
76 #define bit_uint2korr(A) ((uint16_t) (((uint16_t) (((unsigned char*) (A))[1])) +\
77                                    ((uint16_t) (((unsigned char*) (A))[0]) << 8)))
78 #define bit_uint3korr(A) ((uint32_t) (((uint32_t) (((unsigned char*) (A))[2])) +\
79                                    (((uint32_t) (((unsigned char*) (A))[1])) << 8) +\
80                                    (((uint32_t) (((unsigned char*) (A))[0])) << 16)))
81 #define bit_uint4korr(A) ((uint32_t) (((uint32_t) (((unsigned char*) (A))[3])) +\
82                                    (((uint32_t) (((unsigned char*) (A))[2])) << 8) +\
83                                    (((uint32_t) (((unsigned char*) (A))[1])) << 16) +\
84                                    (((uint32_t) (((unsigned char*) (A))[0])) << 24)))
85 #define bit_uint5korr(A) ((uint64_t)(((uint32_t) (((unsigned char*) (A))[4])) +\
86                                     (((uint32_t) (((unsigned char*) (A))[3])) << 8) +\
87                                     (((uint32_t) (((unsigned char*) (A))[2])) << 16) +\
88                                    (((uint32_t) (((unsigned char*) (A))[1])) << 24)) +\
89                                     (((uint64_t) (((unsigned char*) (A))[0])) << 32))
90 #define bit_uint6korr(A) ((uint64_t)(((uint32_t) (((unsigned char*) (A))[5])) +\
91                                     (((uint32_t) (((unsigned char*) (A))[4])) << 8) +\
92                                     (((uint32_t) (((unsigned char*) (A))[3])) << 16) +\
93                                     (((uint32_t) (((unsigned char*) (A))[2])) << 24)) +\
94                         (((uint64_t) (((uint32_t) (((unsigned char*) (A))[1])) +\
95                                     (((uint32_t) (((unsigned char*) (A))[0]) << 8)))) <<\
96                                      32))
97 #define bit_uint7korr(A) ((uint64_t)(((uint32_t) (((unsigned char*) (A))[6])) +\
98                                     (((uint32_t) (((unsigned char*) (A))[5])) << 8) +\
99                                     (((uint32_t) (((unsigned char*) (A))[4])) << 16) +\
100                                    (((uint32_t) (((unsigned char*) (A))[3])) << 24)) +\
101                         (((uint64_t) (((uint32_t) (((unsigned char*) (A))[2])) +\
102                                     (((uint32_t) (((unsigned char*) (A))[1])) << 8) +\
103                                     (((uint32_t) (((unsigned char*) (A))[0])) << 16))) <<\
104                                      32))
105 #define bit_uint8korr(A) ((uint64_t)(((uint32_t) (((unsigned char*) (A))[7])) +\
106                                     (((uint32_t) (((unsigned char*) (A))[6])) << 8) +\
107                                     (((uint32_t) (((unsigned char*) (A))[5])) << 16) +\
108                                     (((uint32_t) (((unsigned char*) (A))[4])) << 24)) +\
109                         (((uint64_t) (((uint32_t) (((unsigned char*) (A))[3])) +\
110                                     (((uint32_t) (((unsigned char*) (A))[2])) << 8) +\
111                                     (((uint32_t) (((unsigned char*) (A))[1])) << 16) +\
112                                     (((uint32_t) (((unsigned char*) (A))[0])) << 24))) <<\
113                                     32))
114 
115 
116 /*
117 ** Define-funktions for reading and storing in machine independent format
118 **  (low byte first)
119 */
120 
121 /* Optimized store functions for Intel x86, non-valid for WIN64. __i386__ is GCC */
122 #if defined(__i386__) && !defined(_WIN64)
123 #define sint2korr(A)    (*((int16_t *) (A)))
124 #define sint3korr(A)    ((int32_t) ((((zend_uchar) (A)[2]) & 128) ? \
125                    (((uint32_t) 255L << 24) | \
126                    (((uint32_t) (zend_uchar) (A)[2]) << 16) |\
127                    (((uint32_t) (zend_uchar) (A)[1]) << 8) | \
128                     ((uint32_t) (zend_uchar) (A)[0])) : \
129                    (((uint32_t) (zend_uchar) (A)[2]) << 16) |\
130                    (((uint32_t) (zend_uchar) (A)[1]) << 8) | \
131                     ((uint32_t) (zend_uchar) (A)[0])))
132 #define sint4korr(A)  (*((zend_long *) (A)))
133 
134 #define uint2korr(A)  (*((uint16_t *) (A)))
135 #define uint3korr(A)  (uint32_t) (((uint32_t) ((zend_uchar) (A)[0])) +\
136                                (((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
137                                (((uint32_t) ((zend_uchar) (A)[2])) << 16))
138 #define uint4korr(A)  (*((zend_ulong *) (A)))
139 
140 
141 
142 #define uint8korr(A)    (*((uint64_t *) (A)))
143 #define sint8korr(A)    (*((int64_t *) (A)))
144 #define int2store(T,A)    *((uint16_t*) (T))= (uint16_t) (A)
145 #define int3store(T,A)   { \
146                   *(T)=  (zend_uchar) ((A));\
147                   *(T+1)=(zend_uchar) (((uint32_t) (A) >> 8));\
148                   *(T+2)=(zend_uchar) (((A) >> 16)); }
149 #define int4store(T,A)    *((zend_long *) (T))= (zend_long) (A)
150 #define int5store(T,A)    { \
151               *((zend_uchar *)(T))= (zend_uchar)((A));\
152               *(((zend_uchar *)(T))+1)=(zend_uchar) (((A) >> 8));\
153               *(((zend_uchar *)(T))+2)=(zend_uchar) (((A) >> 16));\
154               *(((zend_uchar *)(T))+3)=(zend_uchar) (((A) >> 24)); \
155               *(((zend_uchar *)(T))+4)=(zend_uchar) (((A) >> 32)); }
156 
157 #define int8store(T,A)    *((uint64_t *) (T))= (uint64_t) (A)
158 
159 typedef union {
160   double v;
161   zend_long m[2];
162 } float8get_union;
163 #define float8get(V,M)    { ((float8get_union *)&(V))->m[0] = *((zend_long*) (M)); \
164                             ((float8get_union *)&(V))->m[1] = *(((zend_long*) (M))+1); }
165 #define float8store(T,V) { *((zend_long *) (T))     = ((float8get_union *)&(V))->m[0]; \
166                            *(((zend_long *) (T))+1) = ((float8get_union *)&(V))->m[1]; }
167 #define float4get(V,M)	{ *((float *) &(V)) = *((float*) (M)); }
168 /* From Andrey Hristov based on float8get */
169 #define floatget(V,M)    memcpy((char*) &(V),(char*) (M),sizeof(float))
170 #endif /* __i386__ */
171 
172 
173 /* If we haven't defined sint2korr, which is because the platform is not x86 or it's WIN64 */
174 #ifndef sint2korr
175 #define sint2korr(A)    (int16_t) (((int16_t) ((zend_uchar) (A)[0])) +\
176                                  ((int16_t) ((int16_t) (A)[1]) << 8))
177 #define sint3korr(A)    ((int32_t) ((((zend_uchar) (A)[2]) & 128) ? \
178                   (((uint32_t) 255L << 24) | \
179                   (((uint32_t) (zend_uchar) (A)[2]) << 16) |\
180                   (((uint32_t) (zend_uchar) (A)[1]) << 8) | \
181                    ((uint32_t) (zend_uchar) (A)[0])) : \
182                   (((uint32_t) (zend_uchar) (A)[2]) << 16) |\
183                   (((uint32_t) (zend_uchar) (A)[1]) << 8) | \
184                   ((uint32_t) (zend_uchar) (A)[0])))
185 #define sint4korr(A)  (int32_t) (((uint32_t) ((A)[0])) +\
186                               (((uint32_t) ((A)[1]) << 8)) +\
187                               (((uint32_t) ((A)[2]) << 16)) +\
188                               (((uint32_t) ((A)[3]) << 24)))
189 
190 #define sint8korr(A)  (int64_t) uint8korr(A)
191 #define uint2korr(A)  (uint16_t) (((uint16_t) ((zend_uchar) (A)[0])) +\
192                                ((uint16_t) ((zend_uchar) (A)[1]) << 8))
193 #define uint3korr(A)  (uint32_t) (((uint32_t) ((zend_uchar) (A)[0])) +\
194                                (((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
195                                (((uint32_t) ((zend_uchar) (A)[2])) << 16))
196 #define uint4korr(A)  (uint32_t) (((uint32_t) ((zend_uchar) (A)[0])) +\
197                                (((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
198                                (((uint32_t) ((zend_uchar) (A)[2])) << 16) +\
199                                (((uint32_t) ((zend_uchar) (A)[3])) << 24))
200 
201 #define uint8korr(A)	((uint64_t)(((uint32_t) ((zend_uchar) (A)[0])) +\
202 									(((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
203 									(((uint32_t) ((zend_uchar) (A)[2])) << 16) +\
204 									(((uint32_t) ((zend_uchar) (A)[3])) << 24)) +\
205 									(((uint64_t) (((uint32_t) ((zend_uchar) (A)[4])) +\
206 									(((uint32_t) ((zend_uchar) (A)[5])) << 8) +\
207 									(((uint32_t) ((zend_uchar) (A)[6])) << 16) +\
208 									(((uint32_t) ((zend_uchar) (A)[7])) << 24))) << 32))
209 
210 
211 #define int2store(T,A)  do { uint32_t def_temp= (uint32_t) (A) ;\
212                   *((zend_uchar*) (T))  =  (zend_uchar)(def_temp); \
213                   *((zend_uchar*) (T+1)) = (zend_uchar)((def_temp >> 8)); } while (0)
214 #define int3store(T,A)  do { /*lint -save -e734 */\
215                   *(((char *)(T)))   = (char) ((A));\
216                   *(((char *)(T))+1) = (char) (((A) >> 8));\
217                   *(((char *)(T))+2) = (char) (((A) >> 16)); \
218                   /*lint -restore */} while (0)
219 #define int4store(T,A)  do { \
220                   *(((char *)(T)))   = (char) ((A));\
221                   *(((char *)(T))+1) = (char) (((A) >> 8));\
222                   *(((char *)(T))+2) = (char) (((A) >> 16));\
223                   *(((char *)(T))+3) = (char) (((A) >> 24)); } while (0)
224 #define int5store(T,A)  do { \
225                   *(((char *)(T)))   = (char)((A));\
226                   *(((char *)(T))+1) = (char)(((A) >> 8));\
227                   *(((char *)(T))+2) = (char)(((A) >> 16));\
228                   *(((char *)(T))+3) = (char)(((A) >> 24)); \
229                   *(((char *)(T))+4) = (char)(((A) >> 32)); } while (0)
230 #define int8store(T,A)        { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
231                   int4store((T),def_temp); \
232                   int4store((T+4),def_temp2); \
233                 }
234 #ifdef WORDS_BIGENDIAN
235 #define float4get(V,M)   do { float def_temp;\
236                           ((char*) &def_temp)[0] = (M)[3];\
237                           ((char*) &def_temp)[1] = (M)[2];\
238                           ((char*) &def_temp)[2] = (M)[1];\
239                           ((char*) &def_temp)[3] = (M)[0];\
240                           (V)=def_temp; } while (0)
241 #define float8store(T,V)  do { \
242                            *(((char *)(T)))   = (char) ((char *) &(V))[7];\
243                            *(((char *)(T))+1) = (char) ((char *) &(V))[6];\
244                            *(((char *)(T))+2) = (char) ((char *) &(V))[5];\
245                            *(((char *)(T))+3) = (char) ((char *) &(V))[4];\
246                            *(((char *)(T))+4) = (char) ((char *) &(V))[3];\
247                            *(((char *)(T))+5) = (char) ((char *) &(V))[2];\
248                            *(((char *)(T))+6) = (char) ((char *) &(V))[1];\
249                            *(((char *)(T))+7) = (char) ((char *) &(V))[0]; } while (0)
250 
251 #define float8get(V,M)   do { double def_temp;\
252                           ((char*) &def_temp)[0] = (M)[7];\
253                           ((char*) &def_temp)[1] = (M)[6];\
254                           ((char*) &def_temp)[2] = (M)[5];\
255                           ((char*) &def_temp)[3] = (M)[4];\
256                           ((char*) &def_temp)[4] = (M)[3];\
257                           ((char*) &def_temp)[5] = (M)[2];\
258                           ((char*) &def_temp)[6] = (M)[1];\
259                           ((char*) &def_temp)[7] = (M)[0];\
260                           (V) = def_temp; \
261                          } while (0)
262 #else
263 #define float4get(V,M)   memcpy((char*) &(V),(char*) (M),sizeof(float))
264 
265 #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
266 #define float8store(T,V)  do { \
267                          *(((char *)(T)))= ((char *) &(V))[4];\
268                          *(((char *)(T))+1)=(char) ((char *) &(V))[5];\
269                          *(((char *)(T))+2)=(char) ((char *) &(V))[6];\
270                          *(((char *)(T))+3)=(char) ((char *) &(V))[7];\
271                          *(((char *)(T))+4)=(char) ((char *) &(V))[0];\
272                          *(((char *)(T))+5)=(char) ((char *) &(V))[1];\
273                          *(((char *)(T))+6)=(char) ((char *) &(V))[2];\
274                          *(((char *)(T))+7)=(char) ((char *) &(V))[3];} while (0)
275 #define float8get(V,M) do { double def_temp;\
276                          ((char*) &def_temp)[0]=(M)[4];\
277                          ((char*) &def_temp)[1]=(M)[5];\
278                          ((char*) &def_temp)[2]=(M)[6];\
279                          ((char*) &def_temp)[3]=(M)[7];\
280                          ((char*) &def_temp)[4]=(M)[0];\
281                          ((char*) &def_temp)[5]=(M)[1];\
282                          ((char*) &def_temp)[6]=(M)[2];\
283                          ((char*) &def_temp)[7]=(M)[3];\
284                          (V) = def_temp; } while (0)
285 #endif /* __FLOAT_WORD_ORDER */
286 
287 #endif /* WORDS_BIGENDIAN */
288 
289 #endif /* sint2korr */
290 /* To here if the platform is not x86 or it's WIN64 */
291 
292 
293 /* Define-funktions for reading and storing in machine format from/to
294    short/long to/from some place in memory V should be a (not
295    register) variable, M is a pointer to byte */
296 
297 #ifndef float8get
298 
299 #ifdef WORDS_BIGENDIAN
300 #define float8get(V,M)		memcpy((char*) &(V),(char*)  (M), sizeof(double))
301 #define float8store(T,V)	memcpy((char*)  (T),(char*) &(V), sizeof(double))
302 #else
303 #define float8get(V,M)    memcpy((char*) &(V),(char*) (M),sizeof(double))
304 #define float8store(T,V)  memcpy((char*) (T),(char*) &(V),sizeof(double))
305 #endif /* WORDS_BIGENDIAN */
306 
307 #endif /* float8get */
308 
309 #endif /* MYSQLND_PORTABILITY_H */
310