warnless.c 12 KB

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