warnless.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(__INTEL_COMPILER) && defined(__unix__)
  24. #ifdef HAVE_NETINET_IN_H
  25. # include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_ARPA_INET_H
  28. # include <arpa/inet.h>
  29. #endif
  30. #endif /* __INTEL_COMPILER && __unix__ */
  31. #define BUILDING_WARNLESS_C 1
  32. #include "warnless.h"
  33. #define CURL_MASK_SCHAR 0x7F
  34. #define CURL_MASK_UCHAR 0xFF
  35. #if (SIZEOF_SHORT == 2)
  36. # define CURL_MASK_SSHORT 0x7FFF
  37. # define CURL_MASK_USHORT 0xFFFF
  38. #elif (SIZEOF_SHORT == 4)
  39. # define CURL_MASK_SSHORT 0x7FFFFFFF
  40. # define CURL_MASK_USHORT 0xFFFFFFFF
  41. #elif (SIZEOF_SHORT == 8)
  42. # define CURL_MASK_SSHORT 0x7FFFFFFFFFFFFFFF
  43. # define CURL_MASK_USHORT 0xFFFFFFFFFFFFFFFF
  44. #else
  45. # error "SIZEOF_SHORT not defined"
  46. #endif
  47. #if (SIZEOF_INT == 2)
  48. # define CURL_MASK_SINT 0x7FFF
  49. # define CURL_MASK_UINT 0xFFFF
  50. #elif (SIZEOF_INT == 4)
  51. # define CURL_MASK_SINT 0x7FFFFFFF
  52. # define CURL_MASK_UINT 0xFFFFFFFF
  53. #elif (SIZEOF_INT == 8)
  54. # define CURL_MASK_SINT 0x7FFFFFFFFFFFFFFF
  55. # define CURL_MASK_UINT 0xFFFFFFFFFFFFFFFF
  56. #elif (SIZEOF_INT == 16)
  57. # define CURL_MASK_SINT 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  58. # define CURL_MASK_UINT 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  59. #else
  60. # error "SIZEOF_INT not defined"
  61. #endif
  62. #if (CURL_SIZEOF_LONG == 2)
  63. # define CURL_MASK_SLONG 0x7FFFL
  64. # define CURL_MASK_ULONG 0xFFFFUL
  65. #elif (CURL_SIZEOF_LONG == 4)
  66. # define CURL_MASK_SLONG 0x7FFFFFFFL
  67. # define CURL_MASK_ULONG 0xFFFFFFFFUL
  68. #elif (CURL_SIZEOF_LONG == 8)
  69. # define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFL
  70. # define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFUL
  71. #elif (CURL_SIZEOF_LONG == 16)
  72. # define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL
  73. # define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFUL
  74. #else
  75. # error "CURL_SIZEOF_LONG not defined"
  76. #endif
  77. #if (CURL_SIZEOF_CURL_OFF_T == 2)
  78. # define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFF)
  79. # define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFF)
  80. #elif (CURL_SIZEOF_CURL_OFF_T == 4)
  81. # define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFF)
  82. # define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFF)
  83. #elif (CURL_SIZEOF_CURL_OFF_T == 8)
  84. # define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF)
  85. # define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFFFFFFFFFF)
  86. #elif (CURL_SIZEOF_CURL_OFF_T == 16)
  87. # define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
  88. # define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
  89. #else
  90. # error "CURL_SIZEOF_CURL_OFF_T not defined"
  91. #endif
  92. #if (SIZEOF_SIZE_T == SIZEOF_SHORT)
  93. # define CURL_MASK_SSIZE_T CURL_MASK_SSHORT
  94. # define CURL_MASK_USIZE_T CURL_MASK_USHORT
  95. #elif (SIZEOF_SIZE_T == SIZEOF_INT)
  96. # define CURL_MASK_SSIZE_T CURL_MASK_SINT
  97. # define CURL_MASK_USIZE_T CURL_MASK_UINT
  98. #elif (SIZEOF_SIZE_T == CURL_SIZEOF_LONG)
  99. # define CURL_MASK_SSIZE_T CURL_MASK_SLONG
  100. # define CURL_MASK_USIZE_T CURL_MASK_ULONG
  101. #elif (SIZEOF_SIZE_T == CURL_SIZEOF_CURL_OFF_T)
  102. # define CURL_MASK_SSIZE_T CURL_MASK_SCOFFT
  103. # define CURL_MASK_USIZE_T CURL_MASK_UCOFFT
  104. #else
  105. # error "SIZEOF_SIZE_T not defined"
  106. #endif
  107. /*
  108. ** unsigned long to unsigned short
  109. */
  110. unsigned short curlx_ultous(unsigned long ulnum)
  111. {
  112. #ifdef __INTEL_COMPILER
  113. # pragma warning(push)
  114. # pragma warning(disable:810) /* conversion may lose significant bits */
  115. #endif
  116. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
  117. return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
  118. #ifdef __INTEL_COMPILER
  119. # pragma warning(pop)
  120. #endif
  121. }
  122. /*
  123. ** unsigned long to unsigned char
  124. */
  125. unsigned char curlx_ultouc(unsigned long ulnum)
  126. {
  127. #ifdef __INTEL_COMPILER
  128. # pragma warning(push)
  129. # pragma warning(disable:810) /* conversion may lose significant bits */
  130. #endif
  131. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
  132. return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
  133. #ifdef __INTEL_COMPILER
  134. # pragma warning(pop)
  135. #endif
  136. }
  137. /*
  138. ** unsigned long to signed int
  139. */
  140. int curlx_ultosi(unsigned long ulnum)
  141. {
  142. #ifdef __INTEL_COMPILER
  143. # pragma warning(push)
  144. # pragma warning(disable:810) /* conversion may lose significant bits */
  145. #endif
  146. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_SINT);
  147. return (int)(ulnum & (unsigned long) CURL_MASK_SINT);
  148. #ifdef __INTEL_COMPILER
  149. # pragma warning(pop)
  150. #endif
  151. }
  152. /*
  153. ** unsigned size_t to signed int
  154. */
  155. int curlx_uztosi(size_t uznum)
  156. {
  157. #ifdef __INTEL_COMPILER
  158. # pragma warning(push)
  159. # pragma warning(disable:810) /* conversion may lose significant bits */
  160. #endif
  161. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  162. return (int)(uznum & (size_t) CURL_MASK_SINT);
  163. #ifdef __INTEL_COMPILER
  164. # pragma warning(pop)
  165. #endif
  166. }
  167. /*
  168. ** unsigned size_t to unsigned long
  169. */
  170. unsigned long curlx_uztoul(size_t uznum)
  171. {
  172. #ifdef __INTEL_COMPILER
  173. # pragma warning(push)
  174. # pragma warning(disable:810) /* conversion may lose significant bits */
  175. #endif
  176. #if (CURL_SIZEOF_LONG < SIZEOF_SIZE_T)
  177. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  178. #endif
  179. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  180. #ifdef __INTEL_COMPILER
  181. # pragma warning(pop)
  182. #endif
  183. }
  184. /*
  185. ** unsigned size_t to unsigned int
  186. */
  187. unsigned int curlx_uztoui(size_t uznum)
  188. {
  189. #ifdef __INTEL_COMPILER
  190. # pragma warning(push)
  191. # pragma warning(disable:810) /* conversion may lose significant bits */
  192. #endif
  193. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  194. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  195. #endif
  196. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  197. #ifdef __INTEL_COMPILER
  198. # pragma warning(pop)
  199. #endif
  200. }
  201. /*
  202. ** signed long to signed int
  203. */
  204. int curlx_sltosi(long slnum)
  205. {
  206. #ifdef __INTEL_COMPILER
  207. # pragma warning(push)
  208. # pragma warning(disable:810) /* conversion may lose significant bits */
  209. #endif
  210. DEBUGASSERT(slnum >= 0);
  211. #if (SIZEOF_INT < CURL_SIZEOF_LONG)
  212. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  213. #endif
  214. return (int)(slnum & (long) CURL_MASK_SINT);
  215. #ifdef __INTEL_COMPILER
  216. # pragma warning(pop)
  217. #endif
  218. }
  219. /*
  220. ** signed long to unsigned int
  221. */
  222. unsigned int curlx_sltoui(long slnum)
  223. {
  224. #ifdef __INTEL_COMPILER
  225. # pragma warning(push)
  226. # pragma warning(disable:810) /* conversion may lose significant bits */
  227. #endif
  228. DEBUGASSERT(slnum >= 0);
  229. #if (SIZEOF_INT < CURL_SIZEOF_LONG)
  230. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  231. #endif
  232. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  233. #ifdef __INTEL_COMPILER
  234. # pragma warning(pop)
  235. #endif
  236. }
  237. /*
  238. ** signed long to unsigned short
  239. */
  240. unsigned short curlx_sltous(long slnum)
  241. {
  242. #ifdef __INTEL_COMPILER
  243. # pragma warning(push)
  244. # pragma warning(disable:810) /* conversion may lose significant bits */
  245. #endif
  246. DEBUGASSERT(slnum >= 0);
  247. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  248. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  249. #ifdef __INTEL_COMPILER
  250. # pragma warning(pop)
  251. #endif
  252. }
  253. /*
  254. ** unsigned size_t to signed ssize_t
  255. */
  256. ssize_t curlx_uztosz(size_t uznum)
  257. {
  258. #ifdef __INTEL_COMPILER
  259. # pragma warning(push)
  260. # pragma warning(disable:810) /* conversion may lose significant bits */
  261. #endif
  262. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  263. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  264. #ifdef __INTEL_COMPILER
  265. # pragma warning(pop)
  266. #endif
  267. }
  268. /*
  269. ** signed curl_off_t to unsigned size_t
  270. */
  271. size_t curlx_sotouz(curl_off_t sonum)
  272. {
  273. #ifdef __INTEL_COMPILER
  274. # pragma warning(push)
  275. # pragma warning(disable:810) /* conversion may lose significant bits */
  276. #endif
  277. DEBUGASSERT(sonum >= 0);
  278. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  279. #ifdef __INTEL_COMPILER
  280. # pragma warning(pop)
  281. #endif
  282. }
  283. /*
  284. ** signed ssize_t to signed int
  285. */
  286. int curlx_sztosi(ssize_t sznum)
  287. {
  288. #ifdef __INTEL_COMPILER
  289. # pragma warning(push)
  290. # pragma warning(disable:810) /* conversion may lose significant bits */
  291. #endif
  292. DEBUGASSERT(sznum >= 0);
  293. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  294. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  295. #endif
  296. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  297. #ifdef __INTEL_COMPILER
  298. # pragma warning(pop)
  299. #endif
  300. }
  301. /*
  302. ** signed int to unsigned size_t
  303. */
  304. size_t curlx_sitouz(int sinum)
  305. {
  306. #ifdef __INTEL_COMPILER
  307. # pragma warning(push)
  308. # pragma warning(disable:810) /* conversion may lose significant bits */
  309. #endif
  310. DEBUGASSERT(sinum >= 0);
  311. return (size_t) sinum;
  312. #ifdef __INTEL_COMPILER
  313. # pragma warning(pop)
  314. #endif
  315. }
  316. #ifdef USE_WINSOCK
  317. /*
  318. ** curl_socket_t to signed int
  319. */
  320. int curlx_sktosi(curl_socket_t s)
  321. {
  322. return (int)((ssize_t) s);
  323. }
  324. /*
  325. ** signed int to curl_socket_t
  326. */
  327. curl_socket_t curlx_sitosk(int i)
  328. {
  329. return (curl_socket_t)((ssize_t) i);
  330. }
  331. #endif /* USE_WINSOCK */
  332. #if defined(WIN32) || defined(_WIN32)
  333. ssize_t curlx_read(int fd, void *buf, size_t count)
  334. {
  335. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  336. }
  337. ssize_t curlx_write(int fd, const void *buf, size_t count)
  338. {
  339. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  340. }
  341. #endif /* WIN32 || _WIN32 */
  342. #if defined(__INTEL_COMPILER) && defined(__unix__)
  343. int curlx_FD_ISSET(int fd, fd_set *fdset)
  344. {
  345. #pragma warning(push)
  346. #pragma warning(disable:1469) /* clobber ignored */
  347. return FD_ISSET(fd, fdset);
  348. #pragma warning(pop)
  349. }
  350. void curlx_FD_SET(int fd, fd_set *fdset)
  351. {
  352. #pragma warning(push)
  353. #pragma warning(disable:1469) /* clobber ignored */
  354. FD_SET(fd, fdset);
  355. #pragma warning(pop)
  356. }
  357. void curlx_FD_ZERO(fd_set *fdset)
  358. {
  359. #pragma warning(push)
  360. #pragma warning(disable:593) /* variable was set but never used */
  361. FD_ZERO(fdset);
  362. #pragma warning(pop)
  363. }
  364. unsigned short curlx_htons(unsigned short usnum)
  365. {
  366. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  367. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  368. #else
  369. #pragma warning(push)
  370. #pragma warning(disable:810) /* conversion may lose significant bits */
  371. return htons(usnum);
  372. #pragma warning(pop)
  373. #endif
  374. }
  375. unsigned short curlx_ntohs(unsigned short usnum)
  376. {
  377. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  378. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  379. #else
  380. #pragma warning(push)
  381. #pragma warning(disable:810) /* conversion may lose significant bits */
  382. return ntohs(usnum);
  383. #pragma warning(pop)
  384. #endif
  385. }
  386. #endif /* __INTEL_COMPILER && __unix__ */