Imported Upstream version 7.50.2
[platform/upstream/curl.git] / lib / warnless.c
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
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
124 unsigned 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
143 unsigned 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
162 int 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
181 curl_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 #endif
187
188   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
189   return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
190
191 #ifdef __INTEL_COMPILER
192 #  pragma warning(pop)
193 #endif
194 }
195
196 /*
197 ** unsigned size_t to signed int
198 */
199
200 int curlx_uztosi(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   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
208   return (int)(uznum & (size_t) CURL_MASK_SINT);
209
210 #ifdef __INTEL_COMPILER
211 #  pragma warning(pop)
212 #endif
213 }
214
215 /*
216 ** unsigned size_t to unsigned long
217 */
218
219 unsigned long curlx_uztoul(size_t uznum)
220 {
221 #ifdef __INTEL_COMPILER
222 # pragma warning(push)
223 # pragma warning(disable:810) /* conversion may lose significant bits */
224 #endif
225
226 #if (CURL_SIZEOF_LONG < SIZEOF_SIZE_T)
227   DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
228 #endif
229   return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
230
231 #ifdef __INTEL_COMPILER
232 # pragma warning(pop)
233 #endif
234 }
235
236 /*
237 ** unsigned size_t to unsigned int
238 */
239
240 unsigned int curlx_uztoui(size_t uznum)
241 {
242 #ifdef __INTEL_COMPILER
243 # pragma warning(push)
244 # pragma warning(disable:810) /* conversion may lose significant bits */
245 #endif
246
247 #if (SIZEOF_INT < SIZEOF_SIZE_T)
248   DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
249 #endif
250   return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
251
252 #ifdef __INTEL_COMPILER
253 # pragma warning(pop)
254 #endif
255 }
256
257 /*
258 ** signed long to signed int
259 */
260
261 int curlx_sltosi(long slnum)
262 {
263 #ifdef __INTEL_COMPILER
264 #  pragma warning(push)
265 #  pragma warning(disable:810) /* conversion may lose significant bits */
266 #endif
267
268   DEBUGASSERT(slnum >= 0);
269 #if (SIZEOF_INT < CURL_SIZEOF_LONG)
270   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
271 #endif
272   return (int)(slnum & (long) CURL_MASK_SINT);
273
274 #ifdef __INTEL_COMPILER
275 #  pragma warning(pop)
276 #endif
277 }
278
279 /*
280 ** signed long to unsigned int
281 */
282
283 unsigned int curlx_sltoui(long slnum)
284 {
285 #ifdef __INTEL_COMPILER
286 #  pragma warning(push)
287 #  pragma warning(disable:810) /* conversion may lose significant bits */
288 #endif
289
290   DEBUGASSERT(slnum >= 0);
291 #if (SIZEOF_INT < CURL_SIZEOF_LONG)
292   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
293 #endif
294   return (unsigned int)(slnum & (long) CURL_MASK_UINT);
295
296 #ifdef __INTEL_COMPILER
297 #  pragma warning(pop)
298 #endif
299 }
300
301 /*
302 ** signed long to unsigned short
303 */
304
305 unsigned short curlx_sltous(long slnum)
306 {
307 #ifdef __INTEL_COMPILER
308 #  pragma warning(push)
309 #  pragma warning(disable:810) /* conversion may lose significant bits */
310 #endif
311
312   DEBUGASSERT(slnum >= 0);
313   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
314   return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
315
316 #ifdef __INTEL_COMPILER
317 #  pragma warning(pop)
318 #endif
319 }
320
321 /*
322 ** unsigned size_t to signed ssize_t
323 */
324
325 ssize_t curlx_uztosz(size_t uznum)
326 {
327 #ifdef __INTEL_COMPILER
328 #  pragma warning(push)
329 #  pragma warning(disable:810) /* conversion may lose significant bits */
330 #endif
331
332   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
333   return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
334
335 #ifdef __INTEL_COMPILER
336 #  pragma warning(pop)
337 #endif
338 }
339
340 /*
341 ** signed curl_off_t to unsigned size_t
342 */
343
344 size_t curlx_sotouz(curl_off_t sonum)
345 {
346 #ifdef __INTEL_COMPILER
347 #  pragma warning(push)
348 #  pragma warning(disable:810) /* conversion may lose significant bits */
349 #endif
350
351   DEBUGASSERT(sonum >= 0);
352   return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
353
354 #ifdef __INTEL_COMPILER
355 #  pragma warning(pop)
356 #endif
357 }
358
359 /*
360 ** signed ssize_t to signed int
361 */
362
363 int curlx_sztosi(ssize_t sznum)
364 {
365 #ifdef __INTEL_COMPILER
366 #  pragma warning(push)
367 #  pragma warning(disable:810) /* conversion may lose significant bits */
368 #endif
369
370   DEBUGASSERT(sznum >= 0);
371 #if (SIZEOF_INT < SIZEOF_SIZE_T)
372   DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
373 #endif
374   return (int)(sznum & (ssize_t) CURL_MASK_SINT);
375
376 #ifdef __INTEL_COMPILER
377 #  pragma warning(pop)
378 #endif
379 }
380
381 /*
382 ** unsigned int to unsigned short
383 */
384
385 unsigned short curlx_uitous(unsigned int uinum)
386 {
387 #ifdef __INTEL_COMPILER
388 #  pragma warning(push)
389 #  pragma warning(disable:810) /* conversion may lose significant bits */
390 #endif
391
392   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
393   return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
394
395 #ifdef __INTEL_COMPILER
396 #  pragma warning(pop)
397 #endif
398 }
399
400 /*
401 ** unsigned int to unsigned char
402 */
403
404 unsigned char curlx_uitouc(unsigned int uinum)
405 {
406 #ifdef __INTEL_COMPILER
407 #  pragma warning(push)
408 #  pragma warning(disable:810) /* conversion may lose significant bits */
409 #endif
410
411   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_UCHAR);
412   return (unsigned char) (uinum & (unsigned int) CURL_MASK_UCHAR);
413
414 #ifdef __INTEL_COMPILER
415 #  pragma warning(pop)
416 #endif
417 }
418
419 /*
420 ** unsigned int to signed int
421 */
422
423 int curlx_uitosi(unsigned int uinum)
424 {
425 #ifdef __INTEL_COMPILER
426 #  pragma warning(push)
427 #  pragma warning(disable:810) /* conversion may lose significant bits */
428 #endif
429
430   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_SINT);
431   return (int) (uinum & (unsigned int) CURL_MASK_SINT);
432
433 #ifdef __INTEL_COMPILER
434 #  pragma warning(pop)
435 #endif
436 }
437
438 /*
439 ** signed int to unsigned size_t
440 */
441
442 size_t curlx_sitouz(int sinum)
443 {
444 #ifdef __INTEL_COMPILER
445 #  pragma warning(push)
446 #  pragma warning(disable:810) /* conversion may lose significant bits */
447 #endif
448
449   DEBUGASSERT(sinum >= 0);
450   return (size_t) sinum;
451
452 #ifdef __INTEL_COMPILER
453 #  pragma warning(pop)
454 #endif
455 }
456
457 #ifdef USE_WINSOCK
458
459 /*
460 ** curl_socket_t to signed int
461 */
462
463 int curlx_sktosi(curl_socket_t s)
464 {
465   return (int)((ssize_t) s);
466 }
467
468 /*
469 ** signed int to curl_socket_t
470 */
471
472 curl_socket_t curlx_sitosk(int i)
473 {
474   return (curl_socket_t)((ssize_t) i);
475 }
476
477 #endif /* USE_WINSOCK */
478
479 #if defined(WIN32) || defined(_WIN32)
480
481 ssize_t curlx_read(int fd, void *buf, size_t count)
482 {
483   return (ssize_t)read(fd, buf, curlx_uztoui(count));
484 }
485
486 ssize_t curlx_write(int fd, const void *buf, size_t count)
487 {
488   return (ssize_t)write(fd, buf, curlx_uztoui(count));
489 }
490
491 #endif /* WIN32 || _WIN32 */
492
493 #if defined(__INTEL_COMPILER) && defined(__unix__)
494
495 int curlx_FD_ISSET(int fd, fd_set *fdset)
496 {
497   #pragma warning(push)
498   #pragma warning(disable:1469) /* clobber ignored */
499   return FD_ISSET(fd, fdset);
500   #pragma warning(pop)
501 }
502
503 void curlx_FD_SET(int fd, fd_set *fdset)
504 {
505   #pragma warning(push)
506   #pragma warning(disable:1469) /* clobber ignored */
507   FD_SET(fd, fdset);
508   #pragma warning(pop)
509 }
510
511 void curlx_FD_ZERO(fd_set *fdset)
512 {
513   #pragma warning(push)
514   #pragma warning(disable:593) /* variable was set but never used */
515   FD_ZERO(fdset);
516   #pragma warning(pop)
517 }
518
519 unsigned short curlx_htons(unsigned short usnum)
520 {
521 #if (__INTEL_COMPILER == 910) && defined(__i386__)
522   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
523 #else
524   #pragma warning(push)
525   #pragma warning(disable:810) /* conversion may lose significant bits */
526   return htons(usnum);
527   #pragma warning(pop)
528 #endif
529 }
530
531 unsigned short curlx_ntohs(unsigned short usnum)
532 {
533 #if (__INTEL_COMPILER == 910) && defined(__i386__)
534   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
535 #else
536   #pragma warning(push)
537   #pragma warning(disable:810) /* conversion may lose significant bits */
538   return ntohs(usnum);
539   #pragma warning(pop)
540 #endif
541 }
542
543 #endif /* __INTEL_COMPILER && __unix__ */