warnless.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 int
  83. */
  84. int curlx_uztosi(size_t uznum)
  85. {
  86. #ifdef __INTEL_COMPILER
  87. # pragma warning(push)
  88. # pragma warning(disable:810) /* conversion may lose significant bits */
  89. #endif
  90. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
  91. return (int)(uznum & (size_t) CURL_MASK_SINT);
  92. #ifdef __INTEL_COMPILER
  93. # pragma warning(pop)
  94. #endif
  95. }
  96. /*
  97. ** unsigned size_t to unsigned long
  98. */
  99. unsigned long curlx_uztoul(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. #if ULONG_MAX < SIZE_T_MAX
  106. DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
  107. #endif
  108. return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
  109. #ifdef __INTEL_COMPILER
  110. # pragma warning(pop)
  111. #endif
  112. }
  113. /*
  114. ** unsigned size_t to unsigned int
  115. */
  116. unsigned int curlx_uztoui(size_t uznum)
  117. {
  118. #ifdef __INTEL_COMPILER
  119. # pragma warning(push)
  120. # pragma warning(disable:810) /* conversion may lose significant bits */
  121. #endif
  122. #if UINT_MAX < SIZE_T_MAX
  123. DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
  124. #endif
  125. return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
  126. #ifdef __INTEL_COMPILER
  127. # pragma warning(pop)
  128. #endif
  129. }
  130. /*
  131. ** signed long to signed int
  132. */
  133. int curlx_sltosi(long slnum)
  134. {
  135. #ifdef __INTEL_COMPILER
  136. # pragma warning(push)
  137. # pragma warning(disable:810) /* conversion may lose significant bits */
  138. #endif
  139. DEBUGASSERT(slnum >= 0);
  140. #if INT_MAX < LONG_MAX
  141. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
  142. #endif
  143. return (int)(slnum & (long) CURL_MASK_SINT);
  144. #ifdef __INTEL_COMPILER
  145. # pragma warning(pop)
  146. #endif
  147. }
  148. /*
  149. ** signed long to unsigned int
  150. */
  151. unsigned int curlx_sltoui(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 UINT_MAX < LONG_MAX
  159. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
  160. #endif
  161. return (unsigned int)(slnum & (long) CURL_MASK_UINT);
  162. #ifdef __INTEL_COMPILER
  163. # pragma warning(pop)
  164. #endif
  165. }
  166. /*
  167. ** signed long to unsigned short
  168. */
  169. unsigned short curlx_sltous(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. DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
  177. return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
  178. #ifdef __INTEL_COMPILER
  179. # pragma warning(pop)
  180. #endif
  181. }
  182. /*
  183. ** unsigned size_t to signed ssize_t
  184. */
  185. ssize_t curlx_uztosz(size_t uznum)
  186. {
  187. #ifdef __INTEL_COMPILER
  188. # pragma warning(push)
  189. # pragma warning(disable:810) /* conversion may lose significant bits */
  190. #endif
  191. DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
  192. return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
  193. #ifdef __INTEL_COMPILER
  194. # pragma warning(pop)
  195. #endif
  196. }
  197. /*
  198. ** signed curl_off_t to unsigned size_t
  199. */
  200. size_t curlx_sotouz(curl_off_t sonum)
  201. {
  202. #ifdef __INTEL_COMPILER
  203. # pragma warning(push)
  204. # pragma warning(disable:810) /* conversion may lose significant bits */
  205. #endif
  206. DEBUGASSERT(sonum >= 0);
  207. return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
  208. #ifdef __INTEL_COMPILER
  209. # pragma warning(pop)
  210. #endif
  211. }
  212. /*
  213. ** signed ssize_t to signed int
  214. */
  215. int curlx_sztosi(ssize_t sznum)
  216. {
  217. #ifdef __INTEL_COMPILER
  218. # pragma warning(push)
  219. # pragma warning(disable:810) /* conversion may lose significant bits */
  220. #endif
  221. DEBUGASSERT(sznum >= 0);
  222. #if INT_MAX < SSIZE_T_MAX
  223. DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
  224. #endif
  225. return (int)(sznum & (ssize_t) CURL_MASK_SINT);
  226. #ifdef __INTEL_COMPILER
  227. # pragma warning(pop)
  228. #endif
  229. }
  230. /*
  231. ** unsigned int to unsigned short
  232. */
  233. unsigned short curlx_uitous(unsigned int uinum)
  234. {
  235. #ifdef __INTEL_COMPILER
  236. # pragma warning(push)
  237. # pragma warning(disable:810) /* conversion may lose significant bits */
  238. #endif
  239. DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
  240. return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
  241. #ifdef __INTEL_COMPILER
  242. # pragma warning(pop)
  243. #endif
  244. }
  245. /*
  246. ** signed int to unsigned size_t
  247. */
  248. size_t curlx_sitouz(int sinum)
  249. {
  250. #ifdef __INTEL_COMPILER
  251. # pragma warning(push)
  252. # pragma warning(disable:810) /* conversion may lose significant bits */
  253. #endif
  254. DEBUGASSERT(sinum >= 0);
  255. return (size_t) sinum;
  256. #ifdef __INTEL_COMPILER
  257. # pragma warning(pop)
  258. #endif
  259. }
  260. #if defined(_WIN32)
  261. ssize_t curlx_read(int fd, void *buf, size_t count)
  262. {
  263. return (ssize_t)read(fd, buf, curlx_uztoui(count));
  264. }
  265. ssize_t curlx_write(int fd, const void *buf, size_t count)
  266. {
  267. return (ssize_t)write(fd, buf, curlx_uztoui(count));
  268. }
  269. #endif /* _WIN32 */
  270. /* Ensure that warnless.h redefinitions continue to have an effect
  271. in "unity" builds. */
  272. #undef HEADER_CURL_WARNLESS_H_REDEFS