2
0

warnless.c 9.3 KB

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