1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2014, 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 http://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 (CURL_SIZEOF_LONG == 2)
73#  define CURL_MASK_SLONG  0x7FFFL
74#  define CURL_MASK_ULONG  0xFFFFUL
75#elif (CURL_SIZEOF_LONG == 4)
76#  define CURL_MASK_SLONG  0x7FFFFFFFL
77#  define CURL_MASK_ULONG  0xFFFFFFFFUL
78#elif (CURL_SIZEOF_LONG == 8)
79#  define CURL_MASK_SLONG  0x7FFFFFFFFFFFFFFFL
80#  define CURL_MASK_ULONG  0xFFFFFFFFFFFFFFFFUL
81#elif (CURL_SIZEOF_LONG == 16)
82#  define CURL_MASK_SLONG  0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL
83#  define CURL_MASK_ULONG  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFUL
84#else
85#  error "CURL_SIZEOF_LONG not defined"
86#endif
87
88#if (CURL_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 (CURL_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 (CURL_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 (CURL_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 "CURL_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 == CURL_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 == CURL_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 int
179*/
180
181int curlx_uztosi(size_t uznum)
182{
183#ifdef __INTEL_COMPILER
184#  pragma warning(push)
185#  pragma warning(disable:810) /* conversion may lose significant bits */
186#endif
187
188  DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
189  return (int)(uznum & (size_t) CURL_MASK_SINT);
190
191#ifdef __INTEL_COMPILER
192#  pragma warning(pop)
193#endif
194}
195
196/*
197** unsigned size_t to unsigned long
198*/
199
200unsigned long curlx_uztoul(size_t uznum)
201{
202#ifdef __INTEL_COMPILER
203# pragma warning(push)
204# pragma warning(disable:810) /* conversion may lose significant bits */
205#endif
206
207#if (CURL_SIZEOF_LONG < SIZEOF_SIZE_T)
208  DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
209#endif
210  return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
211
212#ifdef __INTEL_COMPILER
213# pragma warning(pop)
214#endif
215}
216
217/*
218** unsigned size_t to unsigned int
219*/
220
221unsigned int curlx_uztoui(size_t uznum)
222{
223#ifdef __INTEL_COMPILER
224# pragma warning(push)
225# pragma warning(disable:810) /* conversion may lose significant bits */
226#endif
227
228#if (SIZEOF_INT < SIZEOF_SIZE_T)
229  DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
230#endif
231  return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
232
233#ifdef __INTEL_COMPILER
234# pragma warning(pop)
235#endif
236}
237
238/*
239** signed long to signed int
240*/
241
242int curlx_sltosi(long slnum)
243{
244#ifdef __INTEL_COMPILER
245#  pragma warning(push)
246#  pragma warning(disable:810) /* conversion may lose significant bits */
247#endif
248
249  DEBUGASSERT(slnum >= 0);
250#if (SIZEOF_INT < CURL_SIZEOF_LONG)
251  DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
252#endif
253  return (int)(slnum & (long) CURL_MASK_SINT);
254
255#ifdef __INTEL_COMPILER
256#  pragma warning(pop)
257#endif
258}
259
260/*
261** signed long to unsigned int
262*/
263
264unsigned int curlx_sltoui(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 < CURL_SIZEOF_LONG)
273  DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
274#endif
275  return (unsigned int)(slnum & (long) CURL_MASK_UINT);
276
277#ifdef __INTEL_COMPILER
278#  pragma warning(pop)
279#endif
280}
281
282/*
283** signed long to unsigned short
284*/
285
286unsigned short curlx_sltous(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  DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
295  return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
296
297#ifdef __INTEL_COMPILER
298#  pragma warning(pop)
299#endif
300}
301
302/*
303** unsigned size_t to signed ssize_t
304*/
305
306ssize_t curlx_uztosz(size_t uznum)
307{
308#ifdef __INTEL_COMPILER
309#  pragma warning(push)
310#  pragma warning(disable:810) /* conversion may lose significant bits */
311#endif
312
313  DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
314  return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
315
316#ifdef __INTEL_COMPILER
317#  pragma warning(pop)
318#endif
319}
320
321/*
322** signed curl_off_t to unsigned size_t
323*/
324
325size_t curlx_sotouz(curl_off_t sonum)
326{
327#ifdef __INTEL_COMPILER
328#  pragma warning(push)
329#  pragma warning(disable:810) /* conversion may lose significant bits */
330#endif
331
332  DEBUGASSERT(sonum >= 0);
333  return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
334
335#ifdef __INTEL_COMPILER
336#  pragma warning(pop)
337#endif
338}
339
340/*
341** signed ssize_t to signed int
342*/
343
344int curlx_sztosi(ssize_t sznum)
345{
346#ifdef __INTEL_COMPILER
347#  pragma warning(push)
348#  pragma warning(disable:810) /* conversion may lose significant bits */
349#endif
350
351  DEBUGASSERT(sznum >= 0);
352#if (SIZEOF_INT < SIZEOF_SIZE_T)
353  DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
354#endif
355  return (int)(sznum & (ssize_t) CURL_MASK_SINT);
356
357#ifdef __INTEL_COMPILER
358#  pragma warning(pop)
359#endif
360}
361
362/*
363** signed int to unsigned size_t
364*/
365
366size_t curlx_sitouz(int sinum)
367{
368#ifdef __INTEL_COMPILER
369#  pragma warning(push)
370#  pragma warning(disable:810) /* conversion may lose significant bits */
371#endif
372
373  DEBUGASSERT(sinum >= 0);
374  return (size_t) sinum;
375
376#ifdef __INTEL_COMPILER
377#  pragma warning(pop)
378#endif
379}
380
381#ifdef USE_WINSOCK
382
383/*
384** curl_socket_t to signed int
385*/
386
387int curlx_sktosi(curl_socket_t s)
388{
389  return (int)((ssize_t) s);
390}
391
392/*
393** signed int to curl_socket_t
394*/
395
396curl_socket_t curlx_sitosk(int i)
397{
398  return (curl_socket_t)((ssize_t) i);
399}
400
401#endif /* USE_WINSOCK */
402
403#if defined(WIN32) || defined(_WIN32)
404
405ssize_t curlx_read(int fd, void *buf, size_t count)
406{
407  return (ssize_t)read(fd, buf, curlx_uztoui(count));
408}
409
410ssize_t curlx_write(int fd, const void *buf, size_t count)
411{
412  return (ssize_t)write(fd, buf, curlx_uztoui(count));
413}
414
415#endif /* WIN32 || _WIN32 */
416
417#if defined(__INTEL_COMPILER) && defined(__unix__)
418
419int curlx_FD_ISSET(int fd, fd_set *fdset)
420{
421  #pragma warning(push)
422  #pragma warning(disable:1469) /* clobber ignored */
423  return FD_ISSET(fd, fdset);
424  #pragma warning(pop)
425}
426
427void curlx_FD_SET(int fd, fd_set *fdset)
428{
429  #pragma warning(push)
430  #pragma warning(disable:1469) /* clobber ignored */
431  FD_SET(fd, fdset);
432  #pragma warning(pop)
433}
434
435void curlx_FD_ZERO(fd_set *fdset)
436{
437  #pragma warning(push)
438  #pragma warning(disable:593) /* variable was set but never used */
439  FD_ZERO(fdset);
440  #pragma warning(pop)
441}
442
443unsigned short curlx_htons(unsigned short usnum)
444{
445#if (__INTEL_COMPILER == 910) && defined(__i386__)
446  return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
447#else
448  #pragma warning(push)
449  #pragma warning(disable:810) /* conversion may lose significant bits */
450  return htons(usnum);
451  #pragma warning(pop)
452#endif
453}
454
455unsigned short curlx_ntohs(unsigned short usnum)
456{
457#if (__INTEL_COMPILER == 910) && defined(__i386__)
458  return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
459#else
460  #pragma warning(push)
461  #pragma warning(disable:810) /* conversion may lose significant bits */
462  return ntohs(usnum);
463  #pragma warning(pop)
464#endif
465}
466
467#endif /* __INTEL_COMPILER && __unix__ */
468