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