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