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
25 #include "curl_setup.h"
26
27 #if defined(__INTEL_COMPILER) && defined(__unix__)
28
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef HAVE_ARPA_INET_H
33 # include <arpa/inet.h>
34 #endif
35
36 #endif /* __INTEL_COMPILER && __unix__ */
37
38 #define BUILDING_WARNLESS_C 1
39
40 #include "warnless.h"
41
42 #include <limits.h>
43
44 #define CURL_MASK_UCHAR ((unsigned char)~0)
45 #define CURL_MASK_SCHAR (CURL_MASK_UCHAR >> 1)
46
47 #define CURL_MASK_USHORT ((unsigned short)~0)
48 #define CURL_MASK_SSHORT (CURL_MASK_USHORT >> 1)
49
50 #define CURL_MASK_UINT ((unsigned int)~0)
51 #define CURL_MASK_SINT (CURL_MASK_UINT >> 1)
52
53 #define CURL_MASK_ULONG ((unsigned long)~0)
54 #define CURL_MASK_SLONG (CURL_MASK_ULONG >> 1)
55
56 #define CURL_MASK_UCOFFT ((unsigned CURL_TYPEOF_CURL_OFF_T)~0)
57 #define CURL_MASK_SCOFFT (CURL_MASK_UCOFFT >> 1)
58
59 #define CURL_MASK_USIZE_T ((size_t)~0)
60 #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1)
61
62 /*
63 ** unsigned long to unsigned short
64 */
65
curlx_ultous(unsigned long ulnum)66 unsigned short curlx_ultous(unsigned long ulnum)
67 {
68 #ifdef __INTEL_COMPILER
69 # pragma warning(push)
70 # pragma warning(disable:810) /* conversion may lose significant bits */
71 #endif
72
73 DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
74 return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
75
76 #ifdef __INTEL_COMPILER
77 # pragma warning(pop)
78 #endif
79 }
80
81 /*
82 ** unsigned long to unsigned char
83 */
84
curlx_ultouc(unsigned long ulnum)85 unsigned char curlx_ultouc(unsigned long ulnum)
86 {
87 #ifdef __INTEL_COMPILER
88 # pragma warning(push)
89 # pragma warning(disable:810) /* conversion may lose significant bits */
90 #endif
91
92 DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
93 return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
94
95 #ifdef __INTEL_COMPILER
96 # pragma warning(pop)
97 #endif
98 }
99
100 /*
101 ** unsigned size_t to signed curl_off_t
102 */
103
curlx_uztoso(size_t uznum)104 curl_off_t curlx_uztoso(size_t uznum)
105 {
106 #ifdef __INTEL_COMPILER
107 # pragma warning(push)
108 # pragma warning(disable:810) /* conversion may lose significant bits */
109 #elif defined(_MSC_VER)
110 # pragma warning(push)
111 # pragma warning(disable:4310) /* cast truncates constant value */
112 #endif
113
114 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
115 return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
116
117 #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
118 # pragma warning(pop)
119 #endif
120 }
121
122 /*
123 ** unsigned size_t to signed int
124 */
125
curlx_uztosi(size_t uznum)126 int curlx_uztosi(size_t uznum)
127 {
128 #ifdef __INTEL_COMPILER
129 # pragma warning(push)
130 # pragma warning(disable:810) /* conversion may lose significant bits */
131 #endif
132
133 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
134 return (int)(uznum & (size_t) CURL_MASK_SINT);
135
136 #ifdef __INTEL_COMPILER
137 # pragma warning(pop)
138 #endif
139 }
140
141 /*
142 ** unsigned size_t to unsigned long
143 */
144
curlx_uztoul(size_t uznum)145 unsigned long curlx_uztoul(size_t uznum)
146 {
147 #ifdef __INTEL_COMPILER
148 # pragma warning(push)
149 # pragma warning(disable:810) /* conversion may lose significant bits */
150 #endif
151
152 #if ULONG_MAX < SIZE_T_MAX
153 DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
154 #endif
155 return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
156
157 #ifdef __INTEL_COMPILER
158 # pragma warning(pop)
159 #endif
160 }
161
162 /*
163 ** unsigned size_t to unsigned int
164 */
165
curlx_uztoui(size_t uznum)166 unsigned int curlx_uztoui(size_t uznum)
167 {
168 #ifdef __INTEL_COMPILER
169 # pragma warning(push)
170 # pragma warning(disable:810) /* conversion may lose significant bits */
171 #endif
172
173 #if UINT_MAX < SIZE_T_MAX
174 DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
175 #endif
176 return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
177
178 #ifdef __INTEL_COMPILER
179 # pragma warning(pop)
180 #endif
181 }
182
183 /*
184 ** signed long to signed int
185 */
186
curlx_sltosi(long slnum)187 int curlx_sltosi(long slnum)
188 {
189 #ifdef __INTEL_COMPILER
190 # pragma warning(push)
191 # pragma warning(disable:810) /* conversion may lose significant bits */
192 #endif
193
194 DEBUGASSERT(slnum >= 0);
195 #if INT_MAX < LONG_MAX
196 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
197 #endif
198 return (int)(slnum & (long) CURL_MASK_SINT);
199
200 #ifdef __INTEL_COMPILER
201 # pragma warning(pop)
202 #endif
203 }
204
205 /*
206 ** signed long to unsigned int
207 */
208
curlx_sltoui(long slnum)209 unsigned int curlx_sltoui(long slnum)
210 {
211 #ifdef __INTEL_COMPILER
212 # pragma warning(push)
213 # pragma warning(disable:810) /* conversion may lose significant bits */
214 #endif
215
216 DEBUGASSERT(slnum >= 0);
217 #if UINT_MAX < LONG_MAX
218 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
219 #endif
220 return (unsigned int)(slnum & (long) CURL_MASK_UINT);
221
222 #ifdef __INTEL_COMPILER
223 # pragma warning(pop)
224 #endif
225 }
226
227 /*
228 ** signed long to unsigned short
229 */
230
curlx_sltous(long slnum)231 unsigned short curlx_sltous(long slnum)
232 {
233 #ifdef __INTEL_COMPILER
234 # pragma warning(push)
235 # pragma warning(disable:810) /* conversion may lose significant bits */
236 #endif
237
238 DEBUGASSERT(slnum >= 0);
239 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
240 return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
241
242 #ifdef __INTEL_COMPILER
243 # pragma warning(pop)
244 #endif
245 }
246
247 /*
248 ** unsigned size_t to signed ssize_t
249 */
250
curlx_uztosz(size_t uznum)251 ssize_t curlx_uztosz(size_t uznum)
252 {
253 #ifdef __INTEL_COMPILER
254 # pragma warning(push)
255 # pragma warning(disable:810) /* conversion may lose significant bits */
256 #endif
257
258 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
259 return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
260
261 #ifdef __INTEL_COMPILER
262 # pragma warning(pop)
263 #endif
264 }
265
266 /*
267 ** signed curl_off_t to unsigned size_t
268 */
269
curlx_sotouz(curl_off_t sonum)270 size_t curlx_sotouz(curl_off_t sonum)
271 {
272 #ifdef __INTEL_COMPILER
273 # pragma warning(push)
274 # pragma warning(disable:810) /* conversion may lose significant bits */
275 #endif
276
277 DEBUGASSERT(sonum >= 0);
278 return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
279
280 #ifdef __INTEL_COMPILER
281 # pragma warning(pop)
282 #endif
283 }
284
285 /*
286 ** signed ssize_t to signed int
287 */
288
curlx_sztosi(ssize_t sznum)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
296 DEBUGASSERT(sznum >= 0);
297 #if INT_MAX < SSIZE_T_MAX
298 DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
299 #endif
300 return (int)(sznum & (ssize_t) CURL_MASK_SINT);
301
302 #ifdef __INTEL_COMPILER
303 # pragma warning(pop)
304 #endif
305 }
306
307 /*
308 ** unsigned int to unsigned short
309 */
310
curlx_uitous(unsigned int uinum)311 unsigned short curlx_uitous(unsigned int uinum)
312 {
313 #ifdef __INTEL_COMPILER
314 # pragma warning(push)
315 # pragma warning(disable:810) /* conversion may lose significant bits */
316 #endif
317
318 DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
319 return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
320
321 #ifdef __INTEL_COMPILER
322 # pragma warning(pop)
323 #endif
324 }
325
326 /*
327 ** signed int to unsigned size_t
328 */
329
curlx_sitouz(int sinum)330 size_t curlx_sitouz(int sinum)
331 {
332 #ifdef __INTEL_COMPILER
333 # pragma warning(push)
334 # pragma warning(disable:810) /* conversion may lose significant bits */
335 #endif
336
337 DEBUGASSERT(sinum >= 0);
338 return (size_t) sinum;
339
340 #ifdef __INTEL_COMPILER
341 # pragma warning(pop)
342 #endif
343 }
344
345 #ifdef USE_WINSOCK
346
347 /*
348 ** curl_socket_t to signed int
349 */
350
curlx_sktosi(curl_socket_t s)351 int curlx_sktosi(curl_socket_t s)
352 {
353 return (int)((ssize_t) s);
354 }
355
356 /*
357 ** signed int to curl_socket_t
358 */
359
curlx_sitosk(int i)360 curl_socket_t curlx_sitosk(int i)
361 {
362 return (curl_socket_t)((ssize_t) i);
363 }
364
365 #endif /* USE_WINSOCK */
366
367 #if defined(WIN32)
368
curlx_read(int fd,void * buf,size_t count)369 ssize_t curlx_read(int fd, void *buf, size_t count)
370 {
371 return (ssize_t)read(fd, buf, curlx_uztoui(count));
372 }
373
curlx_write(int fd,const void * buf,size_t count)374 ssize_t curlx_write(int fd, const void *buf, size_t count)
375 {
376 return (ssize_t)write(fd, buf, curlx_uztoui(count));
377 }
378
379 #endif /* WIN32 */
380
381 #if defined(__INTEL_COMPILER) && defined(__unix__)
382
curlx_FD_ISSET(int fd,fd_set * fdset)383 int curlx_FD_ISSET(int fd, fd_set *fdset)
384 {
385 #pragma warning(push)
386 #pragma warning(disable:1469) /* clobber ignored */
387 return FD_ISSET(fd, fdset);
388 #pragma warning(pop)
389 }
390
curlx_FD_SET(int fd,fd_set * fdset)391 void curlx_FD_SET(int fd, fd_set *fdset)
392 {
393 #pragma warning(push)
394 #pragma warning(disable:1469) /* clobber ignored */
395 FD_SET(fd, fdset);
396 #pragma warning(pop)
397 }
398
curlx_FD_ZERO(fd_set * fdset)399 void curlx_FD_ZERO(fd_set *fdset)
400 {
401 #pragma warning(push)
402 #pragma warning(disable:593) /* variable was set but never used */
403 FD_ZERO(fdset);
404 #pragma warning(pop)
405 }
406
curlx_htons(unsigned short usnum)407 unsigned short curlx_htons(unsigned short usnum)
408 {
409 #if (__INTEL_COMPILER == 910) && defined(__i386__)
410 return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
411 #else
412 #pragma warning(push)
413 #pragma warning(disable:810) /* conversion may lose significant bits */
414 return htons(usnum);
415 #pragma warning(pop)
416 #endif
417 }
418
curlx_ntohs(unsigned short usnum)419 unsigned short curlx_ntohs(unsigned short usnum)
420 {
421 #if (__INTEL_COMPILER == 910) && defined(__i386__)
422 return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
423 #else
424 #pragma warning(push)
425 #pragma warning(disable:810) /* conversion may lose significant bits */
426 return ntohs(usnum);
427 #pragma warning(pop)
428 #endif
429 }
430
431 #endif /* __INTEL_COMPILER && __unix__ */
432