1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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
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_SCHAR 0x7F
41#define CURL_MASK_UCHAR 0xFF
42
43#if (SIZEOF_SHORT == 2)
44# define CURL_MASK_SSHORT 0x7FFF
45# define CURL_MASK_USHORT 0xFFFF
46#elif (SIZEOF_SHORT == 4)
47# define CURL_MASK_SSHORT 0x7FFFFFFF
48# define CURL_MASK_USHORT 0xFFFFFFFF
49#elif (SIZEOF_SHORT == 8)
50# define CURL_MASK_SSHORT 0x7FFFFFFFFFFFFFFF
51# define CURL_MASK_USHORT 0xFFFFFFFFFFFFFFFF
52#else
53# error "SIZEOF_SHORT not defined"
54#endif
55
56#if (SIZEOF_INT == 2)
57# define CURL_MASK_SINT 0x7FFF
58# define CURL_MASK_UINT 0xFFFF
59#elif (SIZEOF_INT == 4)
60# define CURL_MASK_SINT 0x7FFFFFFF
61# define CURL_MASK_UINT 0xFFFFFFFF
62#elif (SIZEOF_INT == 8)
63# define CURL_MASK_SINT 0x7FFFFFFFFFFFFFFF
64# define CURL_MASK_UINT 0xFFFFFFFFFFFFFFFF
65#elif (SIZEOF_INT == 16)
66# define CURL_MASK_SINT 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
67# define CURL_MASK_UINT 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
68#else
69# error "SIZEOF_INT not defined"
70#endif
71
72#if (SIZEOF_LONG == 2)
73# define CURL_MASK_SLONG 0x7FFFL
74# define CURL_MASK_ULONG 0xFFFFUL
75#elif (SIZEOF_LONG == 4)
76# define CURL_MASK_SLONG 0x7FFFFFFFL
77# define CURL_MASK_ULONG 0xFFFFFFFFUL
78#elif (SIZEOF_LONG == 8)
79# define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFL
80# define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFUL
81#elif (SIZEOF_LONG == 16)
82# define CURL_MASK_SLONG 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL
83# define CURL_MASK_ULONG 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFUL
84#else
85# error "SIZEOF_LONG not defined"
86#endif
87
88#if (SIZEOF_CURL_OFF_T == 2)
89# define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFF)
90# define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFF)
91#elif (SIZEOF_CURL_OFF_T == 4)
92# define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFF)
93# define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFF)
94#elif (SIZEOF_CURL_OFF_T == 8)
95# define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF)
96# define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFFFFFFFFFF)
97#elif (SIZEOF_CURL_OFF_T == 16)
98# define CURL_MASK_SCOFFT CURL_OFF_T_C(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
99# define CURL_MASK_UCOFFT CURL_OFF_TU_C(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
100#else
101# error "SIZEOF_CURL_OFF_T not defined"
102#endif
103
104#if (SIZEOF_SIZE_T == SIZEOF_SHORT)
105# define CURL_MASK_SSIZE_T CURL_MASK_SSHORT
106# define CURL_MASK_USIZE_T CURL_MASK_USHORT
107#elif (SIZEOF_SIZE_T == SIZEOF_INT)
108# define CURL_MASK_SSIZE_T CURL_MASK_SINT
109# define CURL_MASK_USIZE_T CURL_MASK_UINT
110#elif (SIZEOF_SIZE_T == SIZEOF_LONG)
111# define CURL_MASK_SSIZE_T CURL_MASK_SLONG
112# define CURL_MASK_USIZE_T CURL_MASK_ULONG
113#elif (SIZEOF_SIZE_T == SIZEOF_CURL_OFF_T)
114# define CURL_MASK_SSIZE_T CURL_MASK_SCOFFT
115# define CURL_MASK_USIZE_T CURL_MASK_UCOFFT
116#else
117# error "SIZEOF_SIZE_T not defined"
118#endif
119
120/*
121** unsigned long to unsigned short
122*/
123
124unsigned short curlx_ultous(unsigned long ulnum)
125{
126#ifdef __INTEL_COMPILER
127# pragma warning(push)
128# pragma warning(disable:810) /* conversion may lose significant bits */
129#endif
130
131 DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
132 return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
133
134#ifdef __INTEL_COMPILER
135# pragma warning(pop)
136#endif
137}
138
139/*
140** unsigned long to unsigned char
141*/
142
143unsigned char curlx_ultouc(unsigned long ulnum)
144{
145#ifdef __INTEL_COMPILER
146# pragma warning(push)
147# pragma warning(disable:810) /* conversion may lose significant bits */
148#endif
149
150 DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
151 return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
152
153#ifdef __INTEL_COMPILER
154# pragma warning(pop)
155#endif
156}
157
158/*
159** unsigned long to signed int
160*/
161
162int curlx_ultosi(unsigned long ulnum)
163{
164#ifdef __INTEL_COMPILER
165# pragma warning(push)
166# pragma warning(disable:810) /* conversion may lose significant bits */
167#endif
168
169 DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_SINT);
170 return (int)(ulnum & (unsigned long) CURL_MASK_SINT);
171
172#ifdef __INTEL_COMPILER
173# pragma warning(pop)
174#endif
175}
176
177/*
178** unsigned size_t to signed curl_off_t
179*/
180
181curl_off_t curlx_uztoso(size_t uznum)
182{
183#ifdef __INTEL_COMPILER
184# pragma warning(push)
185# pragma warning(disable:810) /* conversion may lose significant bits */
186#elif defined(_MSC_VER)
187# pragma warning(push)
188# pragma warning(disable:4310) /* cast truncates constant value */
189#endif
190
191 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
192 return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
193
194#if defined(__INTEL_COMPILER) || defined(_MSC_VER)
195# pragma warning(pop)
196#endif
197}
198
199/*
200** unsigned size_t to signed int
201*/
202
203int curlx_uztosi(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
210 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
211 return (int)(uznum & (size_t) CURL_MASK_SINT);
212
213#ifdef __INTEL_COMPILER
214# pragma warning(pop)
215#endif
216}
217
218/*
219** unsigned size_t to unsigned long
220*/
221
222unsigned long curlx_uztoul(size_t uznum)
223{
224#ifdef __INTEL_COMPILER
225# pragma warning(push)
226# pragma warning(disable:810) /* conversion may lose significant bits */
227#endif
228
229#if (SIZEOF_LONG < SIZEOF_SIZE_T)
230 DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
231#endif
232 return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
233
234#ifdef __INTEL_COMPILER
235# pragma warning(pop)
236#endif
237}
238
239/*
240** unsigned size_t to unsigned int
241*/
242
243unsigned int curlx_uztoui(size_t uznum)
244{
245#ifdef __INTEL_COMPILER
246# pragma warning(push)
247# pragma warning(disable:810) /* conversion may lose significant bits */
248#endif
249
250#if (SIZEOF_INT < SIZEOF_SIZE_T)
251 DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
252#endif
253 return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
254
255#ifdef __INTEL_COMPILER
256# pragma warning(pop)
257#endif
258}
259
260/*
261** signed long to signed int
262*/
263
264int curlx_sltosi(long slnum)
265{
266#ifdef __INTEL_COMPILER
267# pragma warning(push)
268# pragma warning(disable:810) /* conversion may lose significant bits */
269#endif
270
271 DEBUGASSERT(slnum >= 0);
272#if (SIZEOF_INT < SIZEOF_LONG)
273 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
274#endif
275 return (int)(slnum & (long) CURL_MASK_SINT);
276
277#ifdef __INTEL_COMPILER
278# pragma warning(pop)
279#endif
280}
281
282/*
283** signed long to unsigned int
284*/
285
286unsigned int curlx_sltoui(long slnum)
287{
288#ifdef __INTEL_COMPILER
289# pragma warning(push)
290# pragma warning(disable:810) /* conversion may lose significant bits */
291#endif
292
293 DEBUGASSERT(slnum >= 0);
294#if (SIZEOF_INT < SIZEOF_LONG)
295 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
296#endif
297 return (unsigned int)(slnum & (long) CURL_MASK_UINT);
298
299#ifdef __INTEL_COMPILER
300# pragma warning(pop)
301#endif
302}
303
304/*
305** signed long to unsigned short
306*/
307
308unsigned short curlx_sltous(long slnum)
309{
310#ifdef __INTEL_COMPILER
311# pragma warning(push)
312# pragma warning(disable:810) /* conversion may lose significant bits */
313#endif
314
315 DEBUGASSERT(slnum >= 0);
316 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
317 return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
318
319#ifdef __INTEL_COMPILER
320# pragma warning(pop)
321#endif
322}
323
324/*
325** unsigned size_t to signed ssize_t
326*/
327
328ssize_t curlx_uztosz(size_t uznum)
329{
330#ifdef __INTEL_COMPILER
331# pragma warning(push)
332# pragma warning(disable:810) /* conversion may lose significant bits */
333#endif
334
335 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
336 return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
337
338#ifdef __INTEL_COMPILER
339# pragma warning(pop)
340#endif
341}
342
343/*
344** signed curl_off_t to unsigned size_t
345*/
346
347size_t curlx_sotouz(curl_off_t sonum)
348{
349#ifdef __INTEL_COMPILER
350# pragma warning(push)
351# pragma warning(disable:810) /* conversion may lose significant bits */
352#endif
353
354 DEBUGASSERT(sonum >= 0);
355 return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
356
357#ifdef __INTEL_COMPILER
358# pragma warning(pop)
359#endif
360}
361
362/*
363** signed ssize_t to signed int
364*/
365
366int curlx_sztosi(ssize_t sznum)
367{
368#ifdef __INTEL_COMPILER
369# pragma warning(push)
370# pragma warning(disable:810) /* conversion may lose significant bits */
371#endif
372
373 DEBUGASSERT(sznum >= 0);
374#if (SIZEOF_INT < SIZEOF_SIZE_T)
375 DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
376#endif
377 return (int)(sznum & (ssize_t) CURL_MASK_SINT);
378
379#ifdef __INTEL_COMPILER
380# pragma warning(pop)
381#endif
382}
383
384/*
385** unsigned int to unsigned short
386*/
387
388unsigned short curlx_uitous(unsigned int uinum)
389{
390#ifdef __INTEL_COMPILER
391# pragma warning(push)
392# pragma warning(disable:810) /* conversion may lose significant bits */
393#endif
394
395 DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
396 return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
397
398#ifdef __INTEL_COMPILER
399# pragma warning(pop)
400#endif
401}
402
403/*
404** signed int to unsigned size_t
405*/
406
407size_t curlx_sitouz(int sinum)
408{
409#ifdef __INTEL_COMPILER
410# pragma warning(push)
411# pragma warning(disable:810) /* conversion may lose significant bits */
412#endif
413
414 DEBUGASSERT(sinum >= 0);
415 return (size_t) sinum;
416
417#ifdef __INTEL_COMPILER
418# pragma warning(pop)
419#endif
420}
421
422#ifdef USE_WINSOCK
423
424/*
425** curl_socket_t to signed int
426*/
427
428int curlx_sktosi(curl_socket_t s)
429{
430 return (int)((ssize_t) s);
431}
432
433/*
434** signed int to curl_socket_t
435*/
436
437curl_socket_t curlx_sitosk(int i)
438{
439 return (curl_socket_t)((ssize_t) i);
440}
441
442#endif /* USE_WINSOCK */
443
444#if defined(WIN32) || defined(_WIN32)
445
446ssize_t curlx_read(int fd, void *buf, size_t count)
447{
448 return (ssize_t)read(fd, buf, curlx_uztoui(count));
449}
450
451ssize_t curlx_write(int fd, const void *buf, size_t count)
452{
453 return (ssize_t)write(fd, buf, curlx_uztoui(count));
454}
455
456#endif /* WIN32 || _WIN32 */
457
458#if defined(__INTEL_COMPILER) && defined(__unix__)
459
460int curlx_FD_ISSET(int fd, fd_set *fdset)
461{
462 #pragma warning(push)
463 #pragma warning(disable:1469) /* clobber ignored */
464 return FD_ISSET(fd, fdset);
465 #pragma warning(pop)
466}
467
468void curlx_FD_SET(int fd, fd_set *fdset)
469{
470 #pragma warning(push)
471 #pragma warning(disable:1469) /* clobber ignored */
472 FD_SET(fd, fdset);
473 #pragma warning(pop)
474}
475
476void curlx_FD_ZERO(fd_set *fdset)
477{
478 #pragma warning(push)
479 #pragma warning(disable:593) /* variable was set but never used */
480 FD_ZERO(fdset);
481 #pragma warning(pop)
482}
483
484unsigned short curlx_htons(unsigned short usnum)
485{
486#if (__INTEL_COMPILER == 910) && defined(__i386__)
487 return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
488#else
489 #pragma warning(push)
490 #pragma warning(disable:810) /* conversion may lose significant bits */
491 return htons(usnum);
492 #pragma warning(pop)
493#endif
494}
495
496unsigned short curlx_ntohs(unsigned short usnum)
497{
498#if (__INTEL_COMPILER == 910) && defined(__i386__)
499 return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
500#else
501 #pragma warning(push)
502 #pragma warning(disable:810) /* conversion may lose significant bits */
503 return ntohs(usnum);
504 #pragma warning(pop)
505#endif
506}
507
508#endif /* __INTEL_COMPILER && __unix__ */
509