warnless.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 curl_off_t
  154. */
  155. curl_off_t curlx_uztoso(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_SCOFFT);
  162. return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
  163. #ifdef __INTEL_COMPILER
  164. # pragma warning(pop)
  165. #endif
  166. }
  167. /*
  168. ** unsigned size_t to signed int
  169. */
  170. int curlx_uztosi(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. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  177. return (int)(uznum & (size_t) CURL_MASK_SINT);
  178. #ifdef __INTEL_COMPILER
  179. # pragma warning(pop)
  180. #endif
  181. }
  182. /*
  183. ** unsigned size_t to unsigned long
  184. */
  185. unsigned long curlx_uztoul(size_t uznum)
  186. {
  187. #ifdef __INTEL_COMPILER
  188. # pragma warning(push)
  189. # pragma warning(disable:810) /* conversion may lose significant bits */
  190. #endif
  191. #if (CURL_SIZEOF_LONG < SIZEOF_SIZE_T)
  192. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  193. #endif
  194. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  195. #ifdef __INTEL_COMPILER
  196. # pragma warning(pop)
  197. #endif
  198. }
  199. /*
  200. ** unsigned size_t to unsigned int
  201. */
  202. unsigned int curlx_uztoui(size_t uznum)
  203. {
  204. #ifdef __INTEL_COMPILER
  205. # pragma warning(push)
  206. # pragma warning(disable:810) /* conversion may lose significant bits */
  207. #endif
  208. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  209. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  210. #endif
  211. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  212. #ifdef __INTEL_COMPILER
  213. # pragma warning(pop)
  214. #endif
  215. }
  216. /*
  217. ** signed long to signed int
  218. */
  219. int curlx_sltosi(long slnum)
  220. {
  221. #ifdef __INTEL_COMPILER
  222. # pragma warning(push)
  223. # pragma warning(disable:810) /* conversion may lose significant bits */
  224. #endif
  225. DEBUGASSERT(slnum >= 0);
  226. #if (SIZEOF_INT < CURL_SIZEOF_LONG)
  227. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  228. #endif
  229. return (int)(slnum & (long) CURL_MASK_SINT);
  230. #ifdef __INTEL_COMPILER
  231. # pragma warning(pop)
  232. #endif
  233. }
  234. /*
  235. ** signed long to unsigned int
  236. */
  237. unsigned int curlx_sltoui(long slnum)
  238. {
  239. #ifdef __INTEL_COMPILER
  240. # pragma warning(push)
  241. # pragma warning(disable:810) /* conversion may lose significant bits */
  242. #endif
  243. DEBUGASSERT(slnum >= 0);
  244. #if (SIZEOF_INT < CURL_SIZEOF_LONG)
  245. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  246. #endif
  247. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  248. #ifdef __INTEL_COMPILER
  249. # pragma warning(pop)
  250. #endif
  251. }
  252. /*
  253. ** signed long to unsigned short
  254. */
  255. unsigned short curlx_sltous(long slnum)
  256. {
  257. #ifdef __INTEL_COMPILER
  258. # pragma warning(push)
  259. # pragma warning(disable:810) /* conversion may lose significant bits */
  260. #endif
  261. DEBUGASSERT(slnum >= 0);
  262. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  263. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  264. #ifdef __INTEL_COMPILER
  265. # pragma warning(pop)
  266. #endif
  267. }
  268. /*
  269. ** unsigned size_t to signed ssize_t
  270. */
  271. ssize_t curlx_uztosz(size_t uznum)
  272. {
  273. #ifdef __INTEL_COMPILER
  274. # pragma warning(push)
  275. # pragma warning(disable:810) /* conversion may lose significant bits */
  276. #endif
  277. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  278. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  279. #ifdef __INTEL_COMPILER
  280. # pragma warning(pop)
  281. #endif
  282. }
  283. /*
  284. ** signed curl_off_t to unsigned size_t
  285. */
  286. size_t curlx_sotouz(curl_off_t sonum)
  287. {
  288. #ifdef __INTEL_COMPILER
  289. # pragma warning(push)
  290. # pragma warning(disable:810) /* conversion may lose significant bits */
  291. #endif
  292. DEBUGASSERT(sonum >= 0);
  293. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  294. #ifdef __INTEL_COMPILER
  295. # pragma warning(pop)
  296. #endif
  297. }
  298. /*
  299. ** signed ssize_t to signed int
  300. */
  301. int curlx_sztosi(ssize_t sznum)
  302. {
  303. #ifdef __INTEL_COMPILER
  304. # pragma warning(push)
  305. # pragma warning(disable:810) /* conversion may lose significant bits */
  306. #endif
  307. DEBUGASSERT(sznum >= 0);
  308. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  309. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  310. #endif
  311. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  312. #ifdef __INTEL_COMPILER
  313. # pragma warning(pop)
  314. #endif
  315. }
  316. /*
  317. ** signed int to unsigned size_t
  318. */
  319. size_t curlx_sitouz(int sinum)
  320. {
  321. #ifdef __INTEL_COMPILER
  322. # pragma warning(push)
  323. # pragma warning(disable:810) /* conversion may lose significant bits */
  324. #endif
  325. DEBUGASSERT(sinum >= 0);
  326. return (size_t) sinum;
  327. #ifdef __INTEL_COMPILER
  328. # pragma warning(pop)
  329. #endif
  330. }
  331. #ifdef USE_WINSOCK
  332. /*
  333. ** curl_socket_t to signed int
  334. */
  335. int curlx_sktosi(curl_socket_t s)
  336. {
  337. return (int)((ssize_t) s);
  338. }
  339. /*
  340. ** signed int to curl_socket_t
  341. */
  342. curl_socket_t curlx_sitosk(int i)
  343. {
  344. return (curl_socket_t)((ssize_t) i);
  345. }
  346. #endif /* USE_WINSOCK */
  347. #if defined(WIN32) || defined(_WIN32)
  348. ssize_t curlx_read(int fd, void *buf, size_t count)
  349. {
  350. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  351. }
  352. ssize_t curlx_write(int fd, const void *buf, size_t count)
  353. {
  354. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  355. }
  356. #endif /* WIN32 || _WIN32 */
  357. #if defined(__INTEL_COMPILER) && defined(__unix__)
  358. int curlx_FD_ISSET(int fd, fd_set *fdset)
  359. {
  360. #pragma warning(push)
  361. #pragma warning(disable:1469) /* clobber ignored */
  362. return FD_ISSET(fd, fdset);
  363. #pragma warning(pop)
  364. }
  365. void curlx_FD_SET(int fd, fd_set *fdset)
  366. {
  367. #pragma warning(push)
  368. #pragma warning(disable:1469) /* clobber ignored */
  369. FD_SET(fd, fdset);
  370. #pragma warning(pop)
  371. }
  372. void curlx_FD_ZERO(fd_set *fdset)
  373. {
  374. #pragma warning(push)
  375. #pragma warning(disable:593) /* variable was set but never used */
  376. FD_ZERO(fdset);
  377. #pragma warning(pop)
  378. }
  379. unsigned short curlx_htons(unsigned short usnum)
  380. {
  381. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  382. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  383. #else
  384. #pragma warning(push)
  385. #pragma warning(disable:810) /* conversion may lose significant bits */
  386. return htons(usnum);
  387. #pragma warning(pop)
  388. #endif
  389. }
  390. unsigned short curlx_ntohs(unsigned short usnum)
  391. {
  392. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  393. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  394. #else
  395. #pragma warning(push)
  396. #pragma warning(disable:810) /* conversion may lose significant bits */
  397. return ntohs(usnum);
  398. #pragma warning(pop)
  399. #endif
  400. }
  401. #endif /* __INTEL_COMPILER && __unix__ */