• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #include "curl_setup.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 #include "warnless.h"
39 
40 #ifdef WIN32
41 #undef read
42 #undef write
43 #endif
44 
45 #include <limits.h>
46 
47 #define CURL_MASK_UCHAR   ((unsigned char)~0)
48 #define CURL_MASK_SCHAR   (CURL_MASK_UCHAR >> 1)
49 
50 #define CURL_MASK_USHORT  ((unsigned short)~0)
51 #define CURL_MASK_SSHORT  (CURL_MASK_USHORT >> 1)
52 
53 #define CURL_MASK_UINT    ((unsigned int)~0)
54 #define CURL_MASK_SINT    (CURL_MASK_UINT >> 1)
55 
56 #define CURL_MASK_ULONG   ((unsigned long)~0)
57 #define CURL_MASK_SLONG   (CURL_MASK_ULONG >> 1)
58 
59 #define CURL_MASK_UCOFFT  ((unsigned CURL_TYPEOF_CURL_OFF_T)~0)
60 #define CURL_MASK_SCOFFT  (CURL_MASK_UCOFFT >> 1)
61 
62 #define CURL_MASK_USIZE_T ((size_t)~0)
63 #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1)
64 
65 /*
66 ** unsigned long to unsigned short
67 */
68 
curlx_ultous(unsigned long ulnum)69 unsigned short curlx_ultous(unsigned long ulnum)
70 {
71 #ifdef __INTEL_COMPILER
72 #  pragma warning(push)
73 #  pragma warning(disable:810) /* conversion may lose significant bits */
74 #endif
75 
76   DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT);
77   return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT);
78 
79 #ifdef __INTEL_COMPILER
80 #  pragma warning(pop)
81 #endif
82 }
83 
84 /*
85 ** unsigned long to unsigned char
86 */
87 
curlx_ultouc(unsigned long ulnum)88 unsigned char curlx_ultouc(unsigned long ulnum)
89 {
90 #ifdef __INTEL_COMPILER
91 #  pragma warning(push)
92 #  pragma warning(disable:810) /* conversion may lose significant bits */
93 #endif
94 
95   DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR);
96   return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR);
97 
98 #ifdef __INTEL_COMPILER
99 #  pragma warning(pop)
100 #endif
101 }
102 
103 /*
104 ** unsigned size_t to signed curl_off_t
105 */
106 
curlx_uztoso(size_t uznum)107 curl_off_t curlx_uztoso(size_t uznum)
108 {
109 #ifdef __INTEL_COMPILER
110 #  pragma warning(push)
111 #  pragma warning(disable:810) /* conversion may lose significant bits */
112 #elif defined(_MSC_VER)
113 #  pragma warning(push)
114 #  pragma warning(disable:4310) /* cast truncates constant value */
115 #endif
116 
117   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT);
118   return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT);
119 
120 #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
121 #  pragma warning(pop)
122 #endif
123 }
124 
125 /*
126 ** unsigned size_t to signed int
127 */
128 
curlx_uztosi(size_t uznum)129 int curlx_uztosi(size_t uznum)
130 {
131 #ifdef __INTEL_COMPILER
132 #  pragma warning(push)
133 #  pragma warning(disable:810) /* conversion may lose significant bits */
134 #endif
135 
136   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
137   return (int)(uznum & (size_t) CURL_MASK_SINT);
138 
139 #ifdef __INTEL_COMPILER
140 #  pragma warning(pop)
141 #endif
142 }
143 
144 /*
145 ** unsigned size_t to unsigned long
146 */
147 
curlx_uztoul(size_t uznum)148 unsigned long curlx_uztoul(size_t uznum)
149 {
150 #ifdef __INTEL_COMPILER
151 # pragma warning(push)
152 # pragma warning(disable:810) /* conversion may lose significant bits */
153 #endif
154 
155 #if ULONG_MAX < SIZE_T_MAX
156   DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
157 #endif
158   return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
159 
160 #ifdef __INTEL_COMPILER
161 # pragma warning(pop)
162 #endif
163 }
164 
165 /*
166 ** unsigned size_t to unsigned int
167 */
168 
curlx_uztoui(size_t uznum)169 unsigned int curlx_uztoui(size_t uznum)
170 {
171 #ifdef __INTEL_COMPILER
172 # pragma warning(push)
173 # pragma warning(disable:810) /* conversion may lose significant bits */
174 #endif
175 
176 #if UINT_MAX < SIZE_T_MAX
177   DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
178 #endif
179   return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
180 
181 #ifdef __INTEL_COMPILER
182 # pragma warning(pop)
183 #endif
184 }
185 
186 /*
187 ** signed long to signed int
188 */
189 
curlx_sltosi(long slnum)190 int curlx_sltosi(long slnum)
191 {
192 #ifdef __INTEL_COMPILER
193 #  pragma warning(push)
194 #  pragma warning(disable:810) /* conversion may lose significant bits */
195 #endif
196 
197   DEBUGASSERT(slnum >= 0);
198 #if INT_MAX < LONG_MAX
199   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
200 #endif
201   return (int)(slnum & (long) CURL_MASK_SINT);
202 
203 #ifdef __INTEL_COMPILER
204 #  pragma warning(pop)
205 #endif
206 }
207 
208 /*
209 ** signed long to unsigned int
210 */
211 
curlx_sltoui(long slnum)212 unsigned int curlx_sltoui(long slnum)
213 {
214 #ifdef __INTEL_COMPILER
215 #  pragma warning(push)
216 #  pragma warning(disable:810) /* conversion may lose significant bits */
217 #endif
218 
219   DEBUGASSERT(slnum >= 0);
220 #if UINT_MAX < LONG_MAX
221   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
222 #endif
223   return (unsigned int)(slnum & (long) CURL_MASK_UINT);
224 
225 #ifdef __INTEL_COMPILER
226 #  pragma warning(pop)
227 #endif
228 }
229 
230 /*
231 ** signed long to unsigned short
232 */
233 
curlx_sltous(long slnum)234 unsigned short curlx_sltous(long slnum)
235 {
236 #ifdef __INTEL_COMPILER
237 #  pragma warning(push)
238 #  pragma warning(disable:810) /* conversion may lose significant bits */
239 #endif
240 
241   DEBUGASSERT(slnum >= 0);
242   DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
243   return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
244 
245 #ifdef __INTEL_COMPILER
246 #  pragma warning(pop)
247 #endif
248 }
249 
250 /*
251 ** unsigned size_t to signed ssize_t
252 */
253 
curlx_uztosz(size_t uznum)254 ssize_t curlx_uztosz(size_t uznum)
255 {
256 #ifdef __INTEL_COMPILER
257 #  pragma warning(push)
258 #  pragma warning(disable:810) /* conversion may lose significant bits */
259 #endif
260 
261   DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
262   return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
263 
264 #ifdef __INTEL_COMPILER
265 #  pragma warning(pop)
266 #endif
267 }
268 
269 /*
270 ** signed curl_off_t to unsigned size_t
271 */
272 
curlx_sotouz(curl_off_t sonum)273 size_t curlx_sotouz(curl_off_t sonum)
274 {
275 #ifdef __INTEL_COMPILER
276 #  pragma warning(push)
277 #  pragma warning(disable:810) /* conversion may lose significant bits */
278 #endif
279 
280   DEBUGASSERT(sonum >= 0);
281   return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
282 
283 #ifdef __INTEL_COMPILER
284 #  pragma warning(pop)
285 #endif
286 }
287 
288 /*
289 ** signed ssize_t to signed int
290 */
291 
curlx_sztosi(ssize_t sznum)292 int curlx_sztosi(ssize_t sznum)
293 {
294 #ifdef __INTEL_COMPILER
295 #  pragma warning(push)
296 #  pragma warning(disable:810) /* conversion may lose significant bits */
297 #endif
298 
299   DEBUGASSERT(sznum >= 0);
300 #if INT_MAX < SSIZE_T_MAX
301   DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
302 #endif
303   return (int)(sznum & (ssize_t) CURL_MASK_SINT);
304 
305 #ifdef __INTEL_COMPILER
306 #  pragma warning(pop)
307 #endif
308 }
309 
310 /*
311 ** unsigned int to unsigned short
312 */
313 
curlx_uitous(unsigned int uinum)314 unsigned short curlx_uitous(unsigned int uinum)
315 {
316 #ifdef __INTEL_COMPILER
317 #  pragma warning(push)
318 #  pragma warning(disable:810) /* conversion may lose significant bits */
319 #endif
320 
321   DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
322   return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
323 
324 #ifdef __INTEL_COMPILER
325 #  pragma warning(pop)
326 #endif
327 }
328 
329 /*
330 ** signed int to unsigned size_t
331 */
332 
curlx_sitouz(int sinum)333 size_t curlx_sitouz(int sinum)
334 {
335 #ifdef __INTEL_COMPILER
336 #  pragma warning(push)
337 #  pragma warning(disable:810) /* conversion may lose significant bits */
338 #endif
339 
340   DEBUGASSERT(sinum >= 0);
341   return (size_t) sinum;
342 
343 #ifdef __INTEL_COMPILER
344 #  pragma warning(pop)
345 #endif
346 }
347 
348 #ifdef USE_WINSOCK
349 
350 /*
351 ** curl_socket_t to signed int
352 */
353 
curlx_sktosi(curl_socket_t s)354 int curlx_sktosi(curl_socket_t s)
355 {
356   return (int)((ssize_t) s);
357 }
358 
359 /*
360 ** signed int to curl_socket_t
361 */
362 
curlx_sitosk(int i)363 curl_socket_t curlx_sitosk(int i)
364 {
365   return (curl_socket_t)((ssize_t) i);
366 }
367 
368 #endif /* USE_WINSOCK */
369 
370 #if defined(WIN32)
371 
curlx_read(int fd,void * buf,size_t count)372 ssize_t curlx_read(int fd, void *buf, size_t count)
373 {
374   return (ssize_t)read(fd, buf, curlx_uztoui(count));
375 }
376 
curlx_write(int fd,const void * buf,size_t count)377 ssize_t curlx_write(int fd, const void *buf, size_t count)
378 {
379   return (ssize_t)write(fd, buf, curlx_uztoui(count));
380 }
381 
382 /* Ensure that warnless.h continues to have an effect in "unity" builds. */
383 #undef HEADER_CURL_WARNLESS_H
384 
385 #endif /* WIN32 */
386 
387