mysqlnd_portability.h 17 KB

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