warnless.c 9.3 KB

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