warnless.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. ** unsigned int to unsigned char
  336. */
  337. unsigned char curlx_uitouc(unsigned int uinum)
  338. {
  339. #ifdef __INTEL_COMPILER
  340. # pragma warning(push)
  341. # pragma warning(disable:810) /* conversion may lose significant bits */
  342. #endif
  343. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_UCHAR);
  344. return (unsigned char) (uinum & (unsigned int) CURL_MASK_UCHAR);
  345. #ifdef __INTEL_COMPILER
  346. # pragma warning(pop)
  347. #endif
  348. }
  349. /*
  350. ** unsigned int to signed int
  351. */
  352. int curlx_uitosi(unsigned int uinum)
  353. {
  354. #ifdef __INTEL_COMPILER
  355. # pragma warning(push)
  356. # pragma warning(disable:810) /* conversion may lose significant bits */
  357. #endif
  358. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_SINT);
  359. return (int) (uinum & (unsigned int) CURL_MASK_SINT);
  360. #ifdef __INTEL_COMPILER
  361. # pragma warning(pop)
  362. #endif
  363. }
  364. /*
  365. ** signed int to unsigned size_t
  366. */
  367. size_t curlx_sitouz(int sinum)
  368. {
  369. #ifdef __INTEL_COMPILER
  370. # pragma warning(push)
  371. # pragma warning(disable:810) /* conversion may lose significant bits */
  372. #endif
  373. DEBUGASSERT(sinum >= 0);
  374. return (size_t) sinum;
  375. #ifdef __INTEL_COMPILER
  376. # pragma warning(pop)
  377. #endif
  378. }
  379. #ifdef USE_WINSOCK
  380. /*
  381. ** curl_socket_t to signed int
  382. */
  383. int curlx_sktosi(curl_socket_t s)
  384. {
  385. return (int)((ssize_t) s);
  386. }
  387. /*
  388. ** signed int to curl_socket_t
  389. */
  390. curl_socket_t curlx_sitosk(int i)
  391. {
  392. return (curl_socket_t)((ssize_t) i);
  393. }
  394. #endif /* USE_WINSOCK */
  395. #if defined(WIN32) || defined(_WIN32)
  396. ssize_t curlx_read(int fd, void *buf, size_t count)
  397. {
  398. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  399. }
  400. ssize_t curlx_write(int fd, const void *buf, size_t count)
  401. {
  402. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  403. }
  404. #endif /* WIN32 || _WIN32 */
  405. #if defined(__INTEL_COMPILER) && defined(__unix__)
  406. int curlx_FD_ISSET(int fd, fd_set *fdset)
  407. {
  408. #pragma warning(push)
  409. #pragma warning(disable:1469) /* clobber ignored */
  410. return FD_ISSET(fd, fdset);
  411. #pragma warning(pop)
  412. }
  413. void curlx_FD_SET(int fd, fd_set *fdset)
  414. {
  415. #pragma warning(push)
  416. #pragma warning(disable:1469) /* clobber ignored */
  417. FD_SET(fd, fdset);
  418. #pragma warning(pop)
  419. }
  420. void curlx_FD_ZERO(fd_set *fdset)
  421. {
  422. #pragma warning(push)
  423. #pragma warning(disable:593) /* variable was set but never used */
  424. FD_ZERO(fdset);
  425. #pragma warning(pop)
  426. }
  427. unsigned short curlx_htons(unsigned short usnum)
  428. {
  429. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  430. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  431. #else
  432. #pragma warning(push)
  433. #pragma warning(disable:810) /* conversion may lose significant bits */
  434. return htons(usnum);
  435. #pragma warning(pop)
  436. #endif
  437. }
  438. unsigned short curlx_ntohs(unsigned short usnum)
  439. {
  440. #if (__INTEL_COMPILER == 910) && defined(__i386__)
  441. return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
  442. #else
  443. #pragma warning(push)
  444. #pragma warning(disable:810) /* conversion may lose significant bits */
  445. return ntohs(usnum);
  446. #pragma warning(pop)
  447. #endif
  448. }
  449. #endif /* __INTEL_COMPILER && __unix__ */