warnless.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 https://curl.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_UCHAR ((unsigned char)~0)
  34. #define CURL_MASK_SCHAR (CURL_MASK_UCHAR >> 1)
  35. #define CURL_MASK_USHORT ((unsigned short)~0)
  36. #define CURL_MASK_SSHORT (CURL_MASK_USHORT >> 1)
  37. #define CURL_MASK_UINT ((unsigned int)~0)
  38. #define CURL_MASK_SINT (CURL_MASK_UINT >> 1)
  39. #define CURL_MASK_ULONG ((unsigned long)~0)
  40. #define CURL_MASK_SLONG (CURL_MASK_ULONG >> 1)
  41. #define CURL_MASK_UCOFFT ((unsigned CURL_TYPEOF_CURL_OFF_T)~0)
  42. #define CURL_MASK_SCOFFT (CURL_MASK_UCOFFT >> 1)
  43. #define CURL_MASK_USIZE_T ((size_t)~0)
  44. #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1)
  45. /*
  46. ** unsigned long to unsigned short
  47. */
  48. unsigned short curlx_ultous(unsigned long ulnum)
  49. {
  50. #ifdef __INTEL_COMPILER
  51. # pragma warning(push)
  52. # pragma warning(disable:810) /* conversion may lose significant bits */
  53. #endif
  54. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
  55. return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
  56. #ifdef __INTEL_COMPILER
  57. # pragma warning(pop)
  58. #endif
  59. }
  60. /*
  61. ** unsigned long to unsigned char
  62. */
  63. unsigned char curlx_ultouc(unsigned long ulnum)
  64. {
  65. #ifdef __INTEL_COMPILER
  66. # pragma warning(push)
  67. # pragma warning(disable:810) /* conversion may lose significant bits */
  68. #endif
  69. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
  70. return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
  71. #ifdef __INTEL_COMPILER
  72. # pragma warning(pop)
  73. #endif
  74. }
  75. /*
  76. ** unsigned size_t to signed curl_off_t
  77. */
  78. curl_off_t curlx_uztoso(size_t uznum)
  79. {
  80. #ifdef __INTEL_COMPILER
  81. # pragma warning(push)
  82. # pragma warning(disable:810) /* conversion may lose significant bits */
  83. #elif defined(_MSC_VER)
  84. # pragma warning(push)
  85. # pragma warning(disable:4310) /* cast truncates constant value */
  86. #endif
  87. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
  88. return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
  89. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  90. # pragma warning(pop)
  91. #endif
  92. }
  93. /*
  94. ** unsigned size_t to signed int
  95. */
  96. int curlx_uztosi(size_t uznum)
  97. {
  98. #ifdef __INTEL_COMPILER
  99. # pragma warning(push)
  100. # pragma warning(disable:810) /* conversion may lose significant bits */
  101. #endif
  102. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  103. return (int)(uznum & (size_t) CURL_MASK_SINT);
  104. #ifdef __INTEL_COMPILER
  105. # pragma warning(pop)
  106. #endif
  107. }
  108. /*
  109. ** unsigned size_t to unsigned long
  110. */
  111. unsigned long curlx_uztoul(size_t uznum)
  112. {
  113. #ifdef __INTEL_COMPILER
  114. # pragma warning(push)
  115. # pragma warning(disable:810) /* conversion may lose significant bits */
  116. #endif
  117. #if ULONG_MAX < SIZE_T_MAX
  118. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  119. #endif
  120. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  121. #ifdef __INTEL_COMPILER
  122. # pragma warning(pop)
  123. #endif
  124. }
  125. /*
  126. ** unsigned size_t to unsigned int
  127. */
  128. unsigned int curlx_uztoui(size_t uznum)
  129. {
  130. #ifdef __INTEL_COMPILER
  131. # pragma warning(push)
  132. # pragma warning(disable:810) /* conversion may lose significant bits */
  133. #endif
  134. #if UINT_MAX < SIZE_T_MAX
  135. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  136. #endif
  137. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  138. #ifdef __INTEL_COMPILER
  139. # pragma warning(pop)
  140. #endif
  141. }
  142. /*
  143. ** signed long to signed int
  144. */
  145. int curlx_sltosi(long slnum)
  146. {
  147. #ifdef __INTEL_COMPILER
  148. # pragma warning(push)
  149. # pragma warning(disable:810) /* conversion may lose significant bits */
  150. #endif
  151. DEBUGASSERT(slnum >= 0);
  152. #if INT_MAX < LONG_MAX
  153. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  154. #endif
  155. return (int)(slnum & (long) CURL_MASK_SINT);
  156. #ifdef __INTEL_COMPILER
  157. # pragma warning(pop)
  158. #endif
  159. }
  160. /*
  161. ** signed long to unsigned int
  162. */
  163. unsigned int curlx_sltoui(long slnum)
  164. {
  165. #ifdef __INTEL_COMPILER
  166. # pragma warning(push)
  167. # pragma warning(disable:810) /* conversion may lose significant bits */
  168. #endif
  169. DEBUGASSERT(slnum >= 0);
  170. #if UINT_MAX < LONG_MAX
  171. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  172. #endif
  173. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  174. #ifdef __INTEL_COMPILER
  175. # pragma warning(pop)
  176. #endif
  177. }
  178. /*
  179. ** signed long to unsigned short
  180. */
  181. unsigned short curlx_sltous(long slnum)
  182. {
  183. #ifdef __INTEL_COMPILER
  184. # pragma warning(push)
  185. # pragma warning(disable:810) /* conversion may lose significant bits */
  186. #endif
  187. DEBUGASSERT(slnum >= 0);
  188. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  189. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  190. #ifdef __INTEL_COMPILER
  191. # pragma warning(pop)
  192. #endif
  193. }
  194. /*
  195. ** unsigned size_t to signed ssize_t
  196. */
  197. ssize_t curlx_uztosz(size_t uznum)
  198. {
  199. #ifdef __INTEL_COMPILER
  200. # pragma warning(push)
  201. # pragma warning(disable:810) /* conversion may lose significant bits */
  202. #endif
  203. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  204. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  205. #ifdef __INTEL_COMPILER
  206. # pragma warning(pop)
  207. #endif
  208. }
  209. /*
  210. ** signed curl_off_t to unsigned size_t
  211. */
  212. size_t curlx_sotouz(curl_off_t sonum)
  213. {
  214. #ifdef __INTEL_COMPILER
  215. # pragma warning(push)
  216. # pragma warning(disable:810) /* conversion may lose significant bits */
  217. #endif
  218. DEBUGASSERT(sonum >= 0);
  219. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  220. #ifdef __INTEL_COMPILER
  221. # pragma warning(pop)
  222. #endif
  223. }
  224. /*
  225. ** signed ssize_t to signed int
  226. */
  227. int curlx_sztosi(ssize_t sznum)
  228. {
  229. #ifdef __INTEL_COMPILER
  230. # pragma warning(push)
  231. # pragma warning(disable:810) /* conversion may lose significant bits */
  232. #endif
  233. DEBUGASSERT(sznum >= 0);
  234. #if INT_MAX < SSIZE_T_MAX
  235. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  236. #endif
  237. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  238. #ifdef __INTEL_COMPILER
  239. # pragma warning(pop)
  240. #endif
  241. }
  242. /*
  243. ** unsigned int to unsigned short
  244. */
  245. unsigned short curlx_uitous(unsigned int uinum)
  246. {
  247. #ifdef __INTEL_COMPILER
  248. # pragma warning(push)
  249. # pragma warning(disable:810) /* conversion may lose significant bits */
  250. #endif
  251. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
  252. return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
  253. #ifdef __INTEL_COMPILER
  254. # pragma warning(pop)
  255. #endif
  256. }
  257. /*
  258. ** signed int to unsigned size_t
  259. */
  260. size_t curlx_sitouz(int sinum)
  261. {
  262. #ifdef __INTEL_COMPILER
  263. # pragma warning(push)
  264. # pragma warning(disable:810) /* conversion may lose significant bits */
  265. #endif
  266. DEBUGASSERT(sinum >= 0);
  267. return (size_t) sinum;
  268. #ifdef __INTEL_COMPILER
  269. # pragma warning(pop)
  270. #endif
  271. }
  272. #ifdef USE_WINSOCK
  273. /*
  274. ** curl_socket_t to signed int
  275. */
  276. int curlx_sktosi(curl_socket_t s)
  277. {
  278. return (int)((ssize_t) s);
  279. }
  280. /*
  281. ** signed int to curl_socket_t
  282. */
  283. curl_socket_t curlx_sitosk(int i)
  284. {
  285. return (curl_socket_t)((ssize_t) i);
  286. }
  287. #endif /* USE_WINSOCK */
  288. #if defined(WIN32)
  289. ssize_t curlx_read(int fd, void *buf, size_t count)
  290. {
  291. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  292. }
  293. ssize_t curlx_write(int fd, const void *buf, size_t count)
  294. {
  295. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  296. }
  297. #endif /* WIN32 */
  298. #if defined(__INTEL_COMPILER) && defined(__unix__)
  299. int curlx_FD_ISSET(int fd, fd_set *fdset)
  300. {
  301. #pragma warning(push)
  302. #pragma warning(disable:1469) /* clobber ignored */
  303. return FD_ISSET(fd, fdset);
  304. #pragma warning(pop)
  305. }
  306. void curlx_FD_SET(int fd, fd_set *fdset)
  307. {
  308. #pragma warning(push)
  309. #pragma warning(disable:1469) /* clobber ignored */
  310. FD_SET(fd, fdset);
  311. #pragma warning(pop)
  312. }
  313. void curlx_FD_ZERO(fd_set *fdset)
  314. {
  315. #pragma warning(push)
  316. #pragma warning(disable:593) /* variable was set but never used */
  317. FD_ZERO(fdset);
  318. #pragma warning(pop)
  319. }
  320. unsigned short curlx_htons(unsigned short usnum)
  321. {
  322. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  323. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  324. #else
  325. #pragma warning(push)
  326. #pragma warning(disable:810) /* conversion may lose significant bits */
  327. return htons(usnum);
  328. #pragma warning(pop)
  329. #endif
  330. }
  331. unsigned short curlx_ntohs(unsigned short usnum)
  332. {
  333. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  334. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  335. #else
  336. #pragma warning(push)
  337. #pragma warning(disable:810) /* conversion may lose significant bits */
  338. return ntohs(usnum);
  339. #pragma warning(pop)
  340. #endif
  341. }
  342. #endif /* __INTEL_COMPILER && __unix__ */