warnless.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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_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 (SIZEOF_LONG == 2)
  63. # define CURL_MASK_SLONG 0x7FFFL
  64. # define CURL_MASK_ULONG 0xFFFFUL
  65. #elif (SIZEOF_LONG == 4)
  66. # define CURL_MASK_SLONG 0x7FFFFFFFL
  67. # define CURL_MASK_ULONG 0xFFFFFFFFUL
  68. #elif (SIZEOF_LONG == 8)
  69. # define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFL
  70. # define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFUL
  71. #elif (SIZEOF_LONG == 16)
  72. # define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL
  73. # define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFUL
  74. #else
  75. # error "SIZEOF_LONG not defined"
  76. #endif
  77. #if (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 (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 (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 (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 "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 == 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 == 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 size_t to signed curl_off_t
  139. */
  140. curl_off_t curlx_uztoso(size_t uznum)
  141. {
  142. #ifdef __INTEL_COMPILER
  143. # pragma warning(push)
  144. # pragma warning(disable:810) /* conversion may lose significant bits */
  145. #elif defined(_MSC_VER)
  146. # pragma warning(push)
  147. # pragma warning(disable:4310) /* cast truncates constant value */
  148. #endif
  149. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
  150. return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
  151. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  152. # pragma warning(pop)
  153. #endif
  154. }
  155. /*
  156. ** unsigned size_t to signed int
  157. */
  158. int curlx_uztosi(size_t uznum)
  159. {
  160. #ifdef __INTEL_COMPILER
  161. # pragma warning(push)
  162. # pragma warning(disable:810) /* conversion may lose significant bits */
  163. #endif
  164. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  165. return (int)(uznum & (size_t) CURL_MASK_SINT);
  166. #ifdef __INTEL_COMPILER
  167. # pragma warning(pop)
  168. #endif
  169. }
  170. /*
  171. ** unsigned size_t to unsigned long
  172. */
  173. unsigned long curlx_uztoul(size_t uznum)
  174. {
  175. #ifdef __INTEL_COMPILER
  176. # pragma warning(push)
  177. # pragma warning(disable:810) /* conversion may lose significant bits */
  178. #endif
  179. #if (SIZEOF_LONG < SIZEOF_SIZE_T)
  180. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  181. #endif
  182. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  183. #ifdef __INTEL_COMPILER
  184. # pragma warning(pop)
  185. #endif
  186. }
  187. /*
  188. ** unsigned size_t to unsigned int
  189. */
  190. unsigned int curlx_uztoui(size_t uznum)
  191. {
  192. #ifdef __INTEL_COMPILER
  193. # pragma warning(push)
  194. # pragma warning(disable:810) /* conversion may lose significant bits */
  195. #endif
  196. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  197. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  198. #endif
  199. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  200. #ifdef __INTEL_COMPILER
  201. # pragma warning(pop)
  202. #endif
  203. }
  204. /*
  205. ** signed long to signed int
  206. */
  207. int curlx_sltosi(long slnum)
  208. {
  209. #ifdef __INTEL_COMPILER
  210. # pragma warning(push)
  211. # pragma warning(disable:810) /* conversion may lose significant bits */
  212. #endif
  213. DEBUGASSERT(slnum >= 0);
  214. #if (SIZEOF_INT < SIZEOF_LONG)
  215. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  216. #endif
  217. return (int)(slnum & (long) CURL_MASK_SINT);
  218. #ifdef __INTEL_COMPILER
  219. # pragma warning(pop)
  220. #endif
  221. }
  222. /*
  223. ** signed long to unsigned int
  224. */
  225. unsigned int curlx_sltoui(long slnum)
  226. {
  227. #ifdef __INTEL_COMPILER
  228. # pragma warning(push)
  229. # pragma warning(disable:810) /* conversion may lose significant bits */
  230. #endif
  231. DEBUGASSERT(slnum >= 0);
  232. #if (SIZEOF_INT < SIZEOF_LONG)
  233. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  234. #endif
  235. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  236. #ifdef __INTEL_COMPILER
  237. # pragma warning(pop)
  238. #endif
  239. }
  240. /*
  241. ** signed long to unsigned short
  242. */
  243. unsigned short curlx_sltous(long slnum)
  244. {
  245. #ifdef __INTEL_COMPILER
  246. # pragma warning(push)
  247. # pragma warning(disable:810) /* conversion may lose significant bits */
  248. #endif
  249. DEBUGASSERT(slnum >= 0);
  250. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  251. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  252. #ifdef __INTEL_COMPILER
  253. # pragma warning(pop)
  254. #endif
  255. }
  256. /*
  257. ** unsigned size_t to signed ssize_t
  258. */
  259. ssize_t curlx_uztosz(size_t uznum)
  260. {
  261. #ifdef __INTEL_COMPILER
  262. # pragma warning(push)
  263. # pragma warning(disable:810) /* conversion may lose significant bits */
  264. #endif
  265. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  266. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  267. #ifdef __INTEL_COMPILER
  268. # pragma warning(pop)
  269. #endif
  270. }
  271. /*
  272. ** signed curl_off_t to unsigned size_t
  273. */
  274. size_t curlx_sotouz(curl_off_t sonum)
  275. {
  276. #ifdef __INTEL_COMPILER
  277. # pragma warning(push)
  278. # pragma warning(disable:810) /* conversion may lose significant bits */
  279. #endif
  280. DEBUGASSERT(sonum >= 0);
  281. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  282. #ifdef __INTEL_COMPILER
  283. # pragma warning(pop)
  284. #endif
  285. }
  286. /*
  287. ** signed ssize_t to signed int
  288. */
  289. int curlx_sztosi(ssize_t sznum)
  290. {
  291. #ifdef __INTEL_COMPILER
  292. # pragma warning(push)
  293. # pragma warning(disable:810) /* conversion may lose significant bits */
  294. #endif
  295. DEBUGASSERT(sznum >= 0);
  296. #if (SIZEOF_INT < SIZEOF_SIZE_T)
  297. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  298. #endif
  299. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  300. #ifdef __INTEL_COMPILER
  301. # pragma warning(pop)
  302. #endif
  303. }
  304. /*
  305. ** unsigned int to unsigned short
  306. */
  307. unsigned short curlx_uitous(unsigned int uinum)
  308. {
  309. #ifdef __INTEL_COMPILER
  310. # pragma warning(push)
  311. # pragma warning(disable:810) /* conversion may lose significant bits */
  312. #endif
  313. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
  314. return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
  315. #ifdef __INTEL_COMPILER
  316. # pragma warning(pop)
  317. #endif
  318. }
  319. /*
  320. ** signed int to unsigned size_t
  321. */
  322. size_t curlx_sitouz(int sinum)
  323. {
  324. #ifdef __INTEL_COMPILER
  325. # pragma warning(push)
  326. # pragma warning(disable:810) /* conversion may lose significant bits */
  327. #endif
  328. DEBUGASSERT(sinum >= 0);
  329. return (size_t) sinum;
  330. #ifdef __INTEL_COMPILER
  331. # pragma warning(pop)
  332. #endif
  333. }
  334. #ifdef USE_WINSOCK
  335. /*
  336. ** curl_socket_t to signed int
  337. */
  338. int curlx_sktosi(curl_socket_t s)
  339. {
  340. return (int)((ssize_t) s);
  341. }
  342. /*
  343. ** signed int to curl_socket_t
  344. */
  345. curl_socket_t curlx_sitosk(int i)
  346. {
  347. return (curl_socket_t)((ssize_t) i);
  348. }
  349. #endif /* USE_WINSOCK */
  350. #if defined(WIN32) || defined(_WIN32)
  351. ssize_t curlx_read(int fd, void *buf, size_t count)
  352. {
  353. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  354. }
  355. ssize_t curlx_write(int fd, const void *buf, size_t count)
  356. {
  357. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  358. }
  359. #endif /* WIN32 || _WIN32 */
  360. #if defined(__INTEL_COMPILER) && defined(__unix__)
  361. int curlx_FD_ISSET(int fd, fd_set *fdset)
  362. {
  363. #pragma warning(push)
  364. #pragma warning(disable:1469) /* clobber ignored */
  365. return FD_ISSET(fd, fdset);
  366. #pragma warning(pop)
  367. }
  368. void curlx_FD_SET(int fd, fd_set *fdset)
  369. {
  370. #pragma warning(push)
  371. #pragma warning(disable:1469) /* clobber ignored */
  372. FD_SET(fd, fdset);
  373. #pragma warning(pop)
  374. }
  375. void curlx_FD_ZERO(fd_set *fdset)
  376. {
  377. #pragma warning(push)
  378. #pragma warning(disable:593) /* variable was set but never used */
  379. FD_ZERO(fdset);
  380. #pragma warning(pop)
  381. }
  382. unsigned short curlx_htons(unsigned short usnum)
  383. {
  384. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  385. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  386. #else
  387. #pragma warning(push)
  388. #pragma warning(disable:810) /* conversion may lose significant bits */
  389. return htons(usnum);
  390. #pragma warning(pop)
  391. #endif
  392. }
  393. unsigned short curlx_ntohs(unsigned short usnum)
  394. {
  395. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  396. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  397. #else
  398. #pragma warning(push)
  399. #pragma warning(disable:810) /* conversion may lose significant bits */
  400. return ntohs(usnum);
  401. #pragma warning(pop)
  402. #endif
  403. }
  404. #endif /* __INTEL_COMPILER && __unix__ */