smtp: use the upload buffer size for scratch buffer malloc
[platform/upstream/curl.git] / lib / warnless.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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
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 #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
203 int 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
222 unsigned 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
243 unsigned 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
264 int 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
286 unsigned 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
308 unsigned 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
328 ssize_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
347 size_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
366 int 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
388 unsigned 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 ** unsigned int to unsigned char
405 */
406
407 unsigned char curlx_uitouc(unsigned int uinum)
408 {
409 #ifdef __INTEL_COMPILER
410 #  pragma warning(push)
411 #  pragma warning(disable:810) /* conversion may lose significant bits */
412 #endif
413
414   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_UCHAR);
415   return (unsigned char) (uinum & (unsigned int) CURL_MASK_UCHAR);
416
417 #ifdef __INTEL_COMPILER
418 #  pragma warning(pop)
419 #endif
420 }
421
422 /*
423 ** unsigned int to signed int
424 */
425
426 int curlx_uitosi(unsigned int uinum)
427 {
428 #ifdef __INTEL_COMPILER
429 #  pragma warning(push)
430 #  pragma warning(disable:810) /* conversion may lose significant bits */
431 #endif
432
433   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_SINT);
434   return (int) (uinum & (unsigned int) CURL_MASK_SINT);
435
436 #ifdef __INTEL_COMPILER
437 #  pragma warning(pop)
438 #endif
439 }
440
441 /*
442 ** signed int to unsigned size_t
443 */
444
445 size_t curlx_sitouz(int sinum)
446 {
447 #ifdef __INTEL_COMPILER
448 #  pragma warning(push)
449 #  pragma warning(disable:810) /* conversion may lose significant bits */
450 #endif
451
452   DEBUGASSERT(sinum >= 0);
453   return (size_t) sinum;
454
455 #ifdef __INTEL_COMPILER
456 #  pragma warning(pop)
457 #endif
458 }
459
460 #ifdef USE_WINSOCK
461
462 /*
463 ** curl_socket_t to signed int
464 */
465
466 int curlx_sktosi(curl_socket_t s)
467 {
468   return (int)((ssize_t) s);
469 }
470
471 /*
472 ** signed int to curl_socket_t
473 */
474
475 curl_socket_t curlx_sitosk(int i)
476 {
477   return (curl_socket_t)((ssize_t) i);
478 }
479
480 #endif /* USE_WINSOCK */
481
482 #if defined(WIN32) || defined(_WIN32)
483
484 ssize_t curlx_read(int fd, void *buf, size_t count)
485 {
486   return (ssize_t)read(fd, buf, curlx_uztoui(count));
487 }
488
489 ssize_t curlx_write(int fd, const void *buf, size_t count)
490 {
491   return (ssize_t)write(fd, buf, curlx_uztoui(count));
492 }
493
494 #endif /* WIN32 || _WIN32 */
495
496 #if defined(__INTEL_COMPILER) && defined(__unix__)
497
498 int curlx_FD_ISSET(int fd, fd_set *fdset)
499 {
500   #pragma warning(push)
501   #pragma warning(disable:1469) /* clobber ignored */
502   return FD_ISSET(fd, fdset);
503   #pragma warning(pop)
504 }
505
506 void curlx_FD_SET(int fd, fd_set *fdset)
507 {
508   #pragma warning(push)
509   #pragma warning(disable:1469) /* clobber ignored */
510   FD_SET(fd, fdset);
511   #pragma warning(pop)
512 }
513
514 void curlx_FD_ZERO(fd_set *fdset)
515 {
516   #pragma warning(push)
517   #pragma warning(disable:593) /* variable was set but never used */
518   FD_ZERO(fdset);
519   #pragma warning(pop)
520 }
521
522 unsigned short curlx_htons(unsigned short usnum)
523 {
524 #if (__INTEL_COMPILER == 910) && defined(__i386__)
525   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
526 #else
527   #pragma warning(push)
528   #pragma warning(disable:810) /* conversion may lose significant bits */
529   return htons(usnum);
530   #pragma warning(pop)
531 #endif
532 }
533
534 unsigned short curlx_ntohs(unsigned short usnum)
535 {
536 #if (__INTEL_COMPILER == 910) && defined(__i386__)
537   return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF));
538 #else
539   #pragma warning(push)
540   #pragma warning(disable:810) /* conversion may lose significant bits */
541   return ntohs(usnum);
542   #pragma warning(pop)
543 #endif
544 }
545
546 #endif /* __INTEL_COMPILER && __unix__ */