warnless.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. #include "warnless.h"
  34. #ifdef _WIN32
  35. #undef read
  36. #undef write
  37. #endif
  38. #include <limits.h>
  39. #define CURL_MASK_UCHAR ((unsigned char)~0)
  40. #define CURL_MASK_SCHAR (CURL_MASK_UCHAR >> 1)
  41. #define CURL_MASK_USHORT ((unsigned short)~0)
  42. #define CURL_MASK_SSHORT (CURL_MASK_USHORT >> 1)
  43. #define CURL_MASK_UINT ((unsigned int)~0)
  44. #define CURL_MASK_SINT (CURL_MASK_UINT >> 1)
  45. #define CURL_MASK_ULONG ((unsigned long)~0)
  46. #define CURL_MASK_SLONG (CURL_MASK_ULONG >> 1)
  47. #define CURL_MASK_UCOFFT ((unsigned CURL_TYPEOF_CURL_OFF_T)~0)
  48. #define CURL_MASK_SCOFFT (CURL_MASK_UCOFFT >> 1)
  49. #define CURL_MASK_USIZE_T ((size_t)~0)
  50. #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1)
  51. /*
  52. ** unsigned long to unsigned short
  53. */
  54. unsigned short curlx_ultous(unsigned long ulnum)
  55. {
  56. #ifdef __INTEL_COMPILER
  57. # pragma warning(push)
  58. # pragma warning(disable:810) /* conversion may lose significant bits */
  59. #endif
  60. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
  61. return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
  62. #ifdef __INTEL_COMPILER
  63. # pragma warning(pop)
  64. #endif
  65. }
  66. /*
  67. ** unsigned long to unsigned char
  68. */
  69. unsigned char curlx_ultouc(unsigned long ulnum)
  70. {
  71. #ifdef __INTEL_COMPILER
  72. # pragma warning(push)
  73. # pragma warning(disable:810) /* conversion may lose significant bits */
  74. #endif
  75. DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
  76. return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
  77. #ifdef __INTEL_COMPILER
  78. # pragma warning(pop)
  79. #endif
  80. }
  81. /*
  82. ** unsigned size_t to signed curl_off_t
  83. */
  84. curl_off_t curlx_uztoso(size_t uznum)
  85. {
  86. #ifdef __INTEL_COMPILER
  87. # pragma warning(push)
  88. # pragma warning(disable:810) /* conversion may lose significant bits */
  89. #elif defined(_MSC_VER)
  90. # pragma warning(push)
  91. # pragma warning(disable:4310) /* cast truncates constant value */
  92. #endif
  93. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
  94. return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
  95. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  96. # pragma warning(pop)
  97. #endif
  98. }
  99. /*
  100. ** unsigned size_t to signed int
  101. */
  102. int curlx_uztosi(size_t uznum)
  103. {
  104. #ifdef __INTEL_COMPILER
  105. # pragma warning(push)
  106. # pragma warning(disable:810) /* conversion may lose significant bits */
  107. #endif
  108. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  109. return (int)(uznum & (size_t) CURL_MASK_SINT);
  110. #ifdef __INTEL_COMPILER
  111. # pragma warning(pop)
  112. #endif
  113. }
  114. /*
  115. ** unsigned size_t to unsigned long
  116. */
  117. unsigned long curlx_uztoul(size_t uznum)
  118. {
  119. #ifdef __INTEL_COMPILER
  120. # pragma warning(push)
  121. # pragma warning(disable:810) /* conversion may lose significant bits */
  122. #endif
  123. #if ULONG_MAX < SIZE_T_MAX
  124. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  125. #endif
  126. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  127. #ifdef __INTEL_COMPILER
  128. # pragma warning(pop)
  129. #endif
  130. }
  131. /*
  132. ** unsigned size_t to unsigned int
  133. */
  134. unsigned int curlx_uztoui(size_t uznum)
  135. {
  136. #ifdef __INTEL_COMPILER
  137. # pragma warning(push)
  138. # pragma warning(disable:810) /* conversion may lose significant bits */
  139. #endif
  140. #if UINT_MAX < SIZE_T_MAX
  141. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  142. #endif
  143. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  144. #ifdef __INTEL_COMPILER
  145. # pragma warning(pop)
  146. #endif
  147. }
  148. /*
  149. ** signed long to signed int
  150. */
  151. int curlx_sltosi(long slnum)
  152. {
  153. #ifdef __INTEL_COMPILER
  154. # pragma warning(push)
  155. # pragma warning(disable:810) /* conversion may lose significant bits */
  156. #endif
  157. DEBUGASSERT(slnum >= 0);
  158. #if INT_MAX < LONG_MAX
  159. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  160. #endif
  161. return (int)(slnum & (long) CURL_MASK_SINT);
  162. #ifdef __INTEL_COMPILER
  163. # pragma warning(pop)
  164. #endif
  165. }
  166. /*
  167. ** signed long to unsigned int
  168. */
  169. unsigned int curlx_sltoui(long slnum)
  170. {
  171. #ifdef __INTEL_COMPILER
  172. # pragma warning(push)
  173. # pragma warning(disable:810) /* conversion may lose significant bits */
  174. #endif
  175. DEBUGASSERT(slnum >= 0);
  176. #if UINT_MAX < LONG_MAX
  177. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  178. #endif
  179. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  180. #ifdef __INTEL_COMPILER
  181. # pragma warning(pop)
  182. #endif
  183. }
  184. /*
  185. ** signed long to unsigned short
  186. */
  187. unsigned short curlx_sltous(long slnum)
  188. {
  189. #ifdef __INTEL_COMPILER
  190. # pragma warning(push)
  191. # pragma warning(disable:810) /* conversion may lose significant bits */
  192. #endif
  193. DEBUGASSERT(slnum >= 0);
  194. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  195. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  196. #ifdef __INTEL_COMPILER
  197. # pragma warning(pop)
  198. #endif
  199. }
  200. /*
  201. ** unsigned size_t to signed ssize_t
  202. */
  203. ssize_t curlx_uztosz(size_t uznum)
  204. {
  205. #ifdef __INTEL_COMPILER
  206. # pragma warning(push)
  207. # pragma warning(disable:810) /* conversion may lose significant bits */
  208. #endif
  209. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  210. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  211. #ifdef __INTEL_COMPILER
  212. # pragma warning(pop)
  213. #endif
  214. }
  215. /*
  216. ** signed curl_off_t to unsigned size_t
  217. */
  218. size_t curlx_sotouz(curl_off_t sonum)
  219. {
  220. #ifdef __INTEL_COMPILER
  221. # pragma warning(push)
  222. # pragma warning(disable:810) /* conversion may lose significant bits */
  223. #endif
  224. DEBUGASSERT(sonum >= 0);
  225. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  226. #ifdef __INTEL_COMPILER
  227. # pragma warning(pop)
  228. #endif
  229. }
  230. /*
  231. ** signed ssize_t to signed int
  232. */
  233. int curlx_sztosi(ssize_t sznum)
  234. {
  235. #ifdef __INTEL_COMPILER
  236. # pragma warning(push)
  237. # pragma warning(disable:810) /* conversion may lose significant bits */
  238. #endif
  239. DEBUGASSERT(sznum >= 0);
  240. #if INT_MAX < SSIZE_T_MAX
  241. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  242. #endif
  243. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  244. #ifdef __INTEL_COMPILER
  245. # pragma warning(pop)
  246. #endif
  247. }
  248. /*
  249. ** unsigned int to unsigned short
  250. */
  251. unsigned short curlx_uitous(unsigned int uinum)
  252. {
  253. #ifdef __INTEL_COMPILER
  254. # pragma warning(push)
  255. # pragma warning(disable:810) /* conversion may lose significant bits */
  256. #endif
  257. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
  258. return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
  259. #ifdef __INTEL_COMPILER
  260. # pragma warning(pop)
  261. #endif
  262. }
  263. /*
  264. ** signed int to unsigned size_t
  265. */
  266. size_t curlx_sitouz(int sinum)
  267. {
  268. #ifdef __INTEL_COMPILER
  269. # pragma warning(push)
  270. # pragma warning(disable:810) /* conversion may lose significant bits */
  271. #endif
  272. DEBUGASSERT(sinum >= 0);
  273. return (size_t) sinum;
  274. #ifdef __INTEL_COMPILER
  275. # pragma warning(pop)
  276. #endif
  277. }
  278. #ifdef USE_WINSOCK
  279. /*
  280. ** curl_socket_t to signed int
  281. */
  282. int curlx_sktosi(curl_socket_t s)
  283. {
  284. return (int)((ssize_t) s);
  285. }
  286. /*
  287. ** signed int to curl_socket_t
  288. */
  289. curl_socket_t curlx_sitosk(int i)
  290. {
  291. return (curl_socket_t)((ssize_t) i);
  292. }
  293. #endif /* USE_WINSOCK */
  294. #if defined(_WIN32)
  295. ssize_t curlx_read(int fd, void *buf, size_t count)
  296. {
  297. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  298. }
  299. ssize_t curlx_write(int fd, const void *buf, size_t count)
  300. {
  301. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  302. }
  303. #endif /* _WIN32 */
  304. /* Ensure that warnless.h redefinitions continue to have an effect
  305. in "unity" builds. */
  306. #undef HEADER_CURL_WARNLESS_H_REDEFS