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 #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 ** signed int to unsigned size_t
320 */
321 
curlx_sitouz(int sinum)322 size_t curlx_sitouz(int sinum)
323 {
324 #ifdef __INTEL_COMPILER
325 #  pragma warning(push)
326 #  pragma warning(disable:810) /* conversion may lose significant bits */
327 #endif
328 
329   DEBUGASSERT(sinum >= 0);
330   return (size_t) sinum;
331 
332 #ifdef __INTEL_COMPILER
333 #  pragma warning(pop)
334 #endif
335 }
336 
337 #ifdef USE_WINSOCK
338 
339 /*
340 ** curl_socket_t to signed int
341 */
342 
curlx_sktosi(curl_socket_t s)343 int curlx_sktosi(curl_socket_t s)
344 {
345   return (int)((ssize_t) s);
346 }
347 
348 /*
349 ** signed int to curl_socket_t
350 */
351 
curlx_sitosk(int i)352 curl_socket_t curlx_sitosk(int i)
353 {
354   return (curl_socket_t)((ssize_t) i);
355 }
356 
357 #endif /* USE_WINSOCK */
358 
359 #if defined(WIN32) || defined(_WIN32)
360 
curlx_read(int fd,void * buf,size_t count)361 ssize_t curlx_read(int fd, void *buf, size_t count)
362 {
363   return (ssize_t)read(fd, buf, curlx_uztoui(count));
364 }
365 
curlx_write(int fd,const void * buf,size_t count)366 ssize_t curlx_write(int fd, const void *buf, size_t count)
367 {
368   return (ssize_t)write(fd, buf, curlx_uztoui(count));
369 }
370 
371 #endif /* WIN32 || _WIN32 */
372 
373 #if defined(__INTEL_COMPILER) && defined(__unix__)
374 
curlx_FD_ISSET(int fd,fd_set * fdset)375 int curlx_FD_ISSET(int fd, fd_set *fdset)
376 {
377   #pragma warning(push)
378   #pragma warning(disable:1469) /* clobber ignored */
379   return FD_ISSET(fd, fdset);
380   #pragma warning(pop)
381 }
382 
curlx_FD_SET(int fd,fd_set * fdset)383 void curlx_FD_SET(int fd, fd_set *fdset)
384 {
385   #pragma warning(push)
386   #pragma warning(disable:1469) /* clobber ignored */
387   FD_SET(fd, fdset);
388   #pragma warning(pop)
389 }
390 
curlx_FD_ZERO(fd_set * fdset)391 void curlx_FD_ZERO(fd_set *fdset)
392 {
393   #pragma warning(push)
394   #pragma warning(disable:593) /* variable was set but never used */
395   FD_ZERO(fdset);
396   #pragma warning(pop)
397 }
398 
curlx_htons(unsigned short usnum)399 unsigned short curlx_htons(unsigned short usnum)
400 {
401 #if (__INTEL_COMPILER == 910) && defined(__i386__)
402   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
403 #else
404   #pragma warning(push)
405   #pragma warning(disable:810) /* conversion may lose significant bits */
406   return htons(usnum);
407   #pragma warning(pop)
408 #endif
409 }
410 
curlx_ntohs(unsigned short usnum)411 unsigned short curlx_ntohs(unsigned short usnum)
412 {
413 #if (__INTEL_COMPILER == 910) && defined(__i386__)
414   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
415 #else
416   #pragma warning(push)
417   #pragma warning(disable:810) /* conversion may lose significant bits */
418   return ntohs(usnum);
419   #pragma warning(pop)
420 #endif
421 }
422 
423 #endif /* __INTEL_COMPILER && __unix__ */
424