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 int
105 */
106
curlx_uztosi(size_t uznum)107 int curlx_uztosi(size_t uznum)
108 {
109 #ifdef __INTEL_COMPILER
110 # pragma warning(push)
111 # pragma warning(disable:810) /* conversion may lose significant bits */
112 #endif
113
114 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT);
115 return (int)(uznum & (size_t) CURL_MASK_SINT);
116
117 #ifdef __INTEL_COMPILER
118 # pragma warning(pop)
119 #endif
120 }
121
122 /*
123 ** unsigned size_t to unsigned long
124 */
125
curlx_uztoul(size_t uznum)126 unsigned long curlx_uztoul(size_t uznum)
127 {
128 #ifdef __INTEL_COMPILER
129 # pragma warning(push)
130 # pragma warning(disable:810) /* conversion may lose significant bits */
131 #endif
132
133 #if ULONG_MAX < SIZE_T_MAX
134 DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG);
135 #endif
136 return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG);
137
138 #ifdef __INTEL_COMPILER
139 # pragma warning(pop)
140 #endif
141 }
142
143 /*
144 ** unsigned size_t to unsigned int
145 */
146
curlx_uztoui(size_t uznum)147 unsigned int curlx_uztoui(size_t uznum)
148 {
149 #ifdef __INTEL_COMPILER
150 # pragma warning(push)
151 # pragma warning(disable:810) /* conversion may lose significant bits */
152 #endif
153
154 #if UINT_MAX < SIZE_T_MAX
155 DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT);
156 #endif
157 return (unsigned int)(uznum & (size_t) CURL_MASK_UINT);
158
159 #ifdef __INTEL_COMPILER
160 # pragma warning(pop)
161 #endif
162 }
163
164 /*
165 ** signed long to signed int
166 */
167
curlx_sltosi(long slnum)168 int curlx_sltosi(long slnum)
169 {
170 #ifdef __INTEL_COMPILER
171 # pragma warning(push)
172 # pragma warning(disable:810) /* conversion may lose significant bits */
173 #endif
174
175 DEBUGASSERT(slnum >= 0);
176 #if INT_MAX < LONG_MAX
177 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT);
178 #endif
179 return (int)(slnum & (long) CURL_MASK_SINT);
180
181 #ifdef __INTEL_COMPILER
182 # pragma warning(pop)
183 #endif
184 }
185
186 /*
187 ** signed long to unsigned int
188 */
189
curlx_sltoui(long slnum)190 unsigned int curlx_sltoui(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 UINT_MAX < LONG_MAX
199 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT);
200 #endif
201 return (unsigned int)(slnum & (long) CURL_MASK_UINT);
202
203 #ifdef __INTEL_COMPILER
204 # pragma warning(pop)
205 #endif
206 }
207
208 /*
209 ** signed long to unsigned short
210 */
211
curlx_sltous(long slnum)212 unsigned short curlx_sltous(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 DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT);
221 return (unsigned short)(slnum & (long) CURL_MASK_USHORT);
222
223 #ifdef __INTEL_COMPILER
224 # pragma warning(pop)
225 #endif
226 }
227
228 /*
229 ** unsigned size_t to signed ssize_t
230 */
231
curlx_uztosz(size_t uznum)232 ssize_t curlx_uztosz(size_t uznum)
233 {
234 #ifdef __INTEL_COMPILER
235 # pragma warning(push)
236 # pragma warning(disable:810) /* conversion may lose significant bits */
237 #endif
238
239 DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T);
240 return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T);
241
242 #ifdef __INTEL_COMPILER
243 # pragma warning(pop)
244 #endif
245 }
246
247 /*
248 ** signed curl_off_t to unsigned size_t
249 */
250
curlx_sotouz(curl_off_t sonum)251 size_t curlx_sotouz(curl_off_t sonum)
252 {
253 #ifdef __INTEL_COMPILER
254 # pragma warning(push)
255 # pragma warning(disable:810) /* conversion may lose significant bits */
256 #endif
257
258 DEBUGASSERT(sonum >= 0);
259 return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T);
260
261 #ifdef __INTEL_COMPILER
262 # pragma warning(pop)
263 #endif
264 }
265
266 /*
267 ** signed ssize_t to signed int
268 */
269
curlx_sztosi(ssize_t sznum)270 int curlx_sztosi(ssize_t sznum)
271 {
272 #ifdef __INTEL_COMPILER
273 # pragma warning(push)
274 # pragma warning(disable:810) /* conversion may lose significant bits */
275 #endif
276
277 DEBUGASSERT(sznum >= 0);
278 #if INT_MAX < SSIZE_T_MAX
279 DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT);
280 #endif
281 return (int)(sznum & (ssize_t) CURL_MASK_SINT);
282
283 #ifdef __INTEL_COMPILER
284 # pragma warning(pop)
285 #endif
286 }
287
288 /*
289 ** unsigned int to unsigned short
290 */
291
curlx_uitous(unsigned int uinum)292 unsigned short curlx_uitous(unsigned int uinum)
293 {
294 #ifdef __INTEL_COMPILER
295 # pragma warning(push)
296 # pragma warning(disable:810) /* conversion may lose significant bits */
297 #endif
298
299 DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT);
300 return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT);
301
302 #ifdef __INTEL_COMPILER
303 # pragma warning(pop)
304 #endif
305 }
306
307 /*
308 ** signed int to unsigned size_t
309 */
310
curlx_sitouz(int sinum)311 size_t curlx_sitouz(int sinum)
312 {
313 #ifdef __INTEL_COMPILER
314 # pragma warning(push)
315 # pragma warning(disable:810) /* conversion may lose significant bits */
316 #endif
317
318 DEBUGASSERT(sinum >= 0);
319 return (size_t) sinum;
320
321 #ifdef __INTEL_COMPILER
322 # pragma warning(pop)
323 #endif
324 }
325
326 #if defined(_WIN32)
327
curlx_read(int fd,void * buf,size_t count)328 ssize_t curlx_read(int fd, void *buf, size_t count)
329 {
330 return (ssize_t)read(fd, buf, curlx_uztoui(count));
331 }
332
curlx_write(int fd,const void * buf,size_t count)333 ssize_t curlx_write(int fd, const void *buf, size_t count)
334 {
335 return (ssize_t)write(fd, buf, curlx_uztoui(count));
336 }
337
338 #endif /* _WIN32 */
339
340 /* Ensure that warnless.h redefinitions continue to have an effect
341 in "unity" builds. */
342 #undef HEADER_CURL_WARNLESS_H_REDEFS
343