/src/curl/lib/curlx/warnless.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "curlx/warnless.h" |
27 | | |
28 | | #if defined(__INTEL_COMPILER) && defined(__unix__) |
29 | | |
30 | | #ifdef HAVE_NETINET_IN_H |
31 | | #include <netinet/in.h> |
32 | | #endif |
33 | | #ifdef HAVE_ARPA_INET_H |
34 | | #include <arpa/inet.h> |
35 | | #endif |
36 | | |
37 | | #endif /* __INTEL_COMPILER && __unix__ */ |
38 | | |
39 | 0 | #define CURL_MASK_UCHAR ((unsigned char)~0) |
40 | | |
41 | 56 | #define CURL_MASK_USHORT ((unsigned short)~0) |
42 | | |
43 | 5.56k | #define CURL_MASK_UINT ((unsigned int)~0) |
44 | 5.56k | #define CURL_MASK_SINT (CURL_MASK_UINT >> 1) |
45 | | |
46 | 0 | #define CURL_MASK_ULONG ((unsigned long)~0) |
47 | | |
48 | 0 | #define CURL_MASK_USIZE_T ((size_t)~0) |
49 | 0 | #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1) |
50 | | |
51 | | /* |
52 | | * unsigned long to unsigned char |
53 | | */ |
54 | | unsigned char curlx_ultouc(unsigned long ulnum) |
55 | 0 | { |
56 | | #ifdef __INTEL_COMPILER |
57 | | #pragma warning(push) |
58 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
59 | | #endif |
60 | |
|
61 | 0 | DEBUGASSERT(ulnum <= (unsigned long)CURL_MASK_UCHAR); |
62 | 0 | return (unsigned char)(ulnum & (unsigned long)CURL_MASK_UCHAR); |
63 | |
|
64 | | #ifdef __INTEL_COMPILER |
65 | | #pragma warning(pop) |
66 | | #endif |
67 | 0 | } |
68 | | |
69 | | /* |
70 | | * unsigned size_t to signed int |
71 | | */ |
72 | | int curlx_uztosi(size_t uznum) |
73 | 5.56k | { |
74 | | #ifdef __INTEL_COMPILER |
75 | | #pragma warning(push) |
76 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
77 | | #endif |
78 | | |
79 | 5.56k | DEBUGASSERT(uznum <= (size_t)CURL_MASK_SINT); |
80 | 5.56k | return (int)(uznum & (size_t)CURL_MASK_SINT); |
81 | | |
82 | | #ifdef __INTEL_COMPILER |
83 | | #pragma warning(pop) |
84 | | #endif |
85 | 5.56k | } |
86 | | |
87 | | /* |
88 | | * unsigned size_t to unsigned long |
89 | | */ |
90 | | unsigned long curlx_uztoul(size_t uznum) |
91 | 0 | { |
92 | | #ifdef __INTEL_COMPILER |
93 | | #pragma warning(push) |
94 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
95 | | #endif |
96 | |
|
97 | | #if ULONG_MAX < SIZE_MAX |
98 | | DEBUGASSERT(uznum <= (size_t)CURL_MASK_ULONG); |
99 | | #endif |
100 | 0 | return (unsigned long)(uznum & (size_t)CURL_MASK_ULONG); |
101 | |
|
102 | | #ifdef __INTEL_COMPILER |
103 | | #pragma warning(pop) |
104 | | #endif |
105 | 0 | } |
106 | | |
107 | | /* |
108 | | * unsigned size_t to unsigned int |
109 | | */ |
110 | | unsigned int curlx_uztoui(size_t uznum) |
111 | 0 | { |
112 | | #ifdef __INTEL_COMPILER |
113 | | #pragma warning(push) |
114 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
115 | | #endif |
116 | |
|
117 | 0 | #if UINT_MAX < SIZE_MAX |
118 | 0 | DEBUGASSERT(uznum <= (size_t)CURL_MASK_UINT); |
119 | 0 | #endif |
120 | 0 | return (unsigned int)(uznum & (size_t)CURL_MASK_UINT); |
121 | |
|
122 | | #ifdef __INTEL_COMPILER |
123 | | #pragma warning(pop) |
124 | | #endif |
125 | 0 | } |
126 | | |
127 | | /* |
128 | | * signed long to signed int |
129 | | */ |
130 | | int curlx_sltosi(long slnum) |
131 | 0 | { |
132 | | #ifdef __INTEL_COMPILER |
133 | | #pragma warning(push) |
134 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
135 | | #endif |
136 | |
|
137 | 0 | DEBUGASSERT(slnum >= 0); |
138 | 0 | #if INT_MAX < LONG_MAX |
139 | 0 | DEBUGASSERT((unsigned long)slnum <= (unsigned long)CURL_MASK_SINT); |
140 | 0 | #endif |
141 | 0 | return (int)(slnum & (long)CURL_MASK_SINT); |
142 | |
|
143 | | #ifdef __INTEL_COMPILER |
144 | | #pragma warning(pop) |
145 | | #endif |
146 | 0 | } |
147 | | |
148 | | /* |
149 | | * signed long to unsigned int |
150 | | */ |
151 | | unsigned int curlx_sltoui(long slnum) |
152 | 0 | { |
153 | | #ifdef __INTEL_COMPILER |
154 | | #pragma warning(push) |
155 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
156 | | #endif |
157 | |
|
158 | 0 | DEBUGASSERT(slnum >= 0); |
159 | 0 | #if UINT_MAX < LONG_MAX |
160 | 0 | DEBUGASSERT((unsigned long)slnum <= (unsigned long)CURL_MASK_UINT); |
161 | 0 | #endif |
162 | 0 | return (unsigned int)(slnum & (long)CURL_MASK_UINT); |
163 | |
|
164 | | #ifdef __INTEL_COMPILER |
165 | | #pragma warning(pop) |
166 | | #endif |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * signed long to unsigned short |
171 | | */ |
172 | | unsigned short curlx_sltous(long slnum) |
173 | 56 | { |
174 | | #ifdef __INTEL_COMPILER |
175 | | #pragma warning(push) |
176 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
177 | | #endif |
178 | | |
179 | 56 | DEBUGASSERT(slnum >= 0); |
180 | 56 | DEBUGASSERT((unsigned long)slnum <= (unsigned long)CURL_MASK_USHORT); |
181 | 56 | return (unsigned short)(slnum & (long)CURL_MASK_USHORT); |
182 | | |
183 | | #ifdef __INTEL_COMPILER |
184 | | #pragma warning(pop) |
185 | | #endif |
186 | 56 | } |
187 | | |
188 | | /* |
189 | | * unsigned size_t to signed ssize_t |
190 | | */ |
191 | | ssize_t curlx_uztosz(size_t uznum) |
192 | 0 | { |
193 | | #ifdef __INTEL_COMPILER |
194 | | #pragma warning(push) |
195 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
196 | | #endif |
197 | |
|
198 | 0 | DEBUGASSERT(uznum <= (size_t)CURL_MASK_SSIZE_T); |
199 | 0 | return (ssize_t)(uznum & (size_t)CURL_MASK_SSIZE_T); |
200 | |
|
201 | | #ifdef __INTEL_COMPILER |
202 | | #pragma warning(pop) |
203 | | #endif |
204 | 0 | } |
205 | | |
206 | | /* |
207 | | * signed curl_off_t to unsigned size_t |
208 | | */ |
209 | | size_t curlx_sotouz(curl_off_t sonum) |
210 | 0 | { |
211 | | #ifdef __INTEL_COMPILER |
212 | | #pragma warning(push) |
213 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
214 | | #endif |
215 | |
|
216 | 0 | DEBUGASSERT(sonum >= 0); |
217 | 0 | return (size_t)(sonum & (curl_off_t)CURL_MASK_USIZE_T); |
218 | |
|
219 | | #ifdef __INTEL_COMPILER |
220 | | #pragma warning(pop) |
221 | | #endif |
222 | 0 | } |
223 | | |
224 | | /* |
225 | | * signed ssize_t to signed int |
226 | | */ |
227 | | int curlx_sztosi(ssize_t sznum) |
228 | 0 | { |
229 | | #ifdef __INTEL_COMPILER |
230 | | #pragma warning(push) |
231 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
232 | | #endif |
233 | |
|
234 | 0 | DEBUGASSERT(sznum >= 0); |
235 | 0 | #if INT_MAX < SSIZE_MAX |
236 | 0 | DEBUGASSERT((size_t)sznum <= (size_t)CURL_MASK_SINT); |
237 | 0 | #endif |
238 | 0 | return (int)(sznum & (ssize_t)CURL_MASK_SINT); |
239 | |
|
240 | | #ifdef __INTEL_COMPILER |
241 | | #pragma warning(pop) |
242 | | #endif |
243 | 0 | } |
244 | | |
245 | | /* |
246 | | * unsigned int to unsigned short |
247 | | */ |
248 | | unsigned short curlx_uitous(unsigned int uinum) |
249 | 0 | { |
250 | | #ifdef __INTEL_COMPILER |
251 | | #pragma warning(push) |
252 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
253 | | #endif |
254 | |
|
255 | 0 | DEBUGASSERT(uinum <= (unsigned int)CURL_MASK_USHORT); |
256 | 0 | return (unsigned short)(uinum & (unsigned int)CURL_MASK_USHORT); |
257 | |
|
258 | | #ifdef __INTEL_COMPILER |
259 | | #pragma warning(pop) |
260 | | #endif |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * signed int to unsigned size_t |
265 | | */ |
266 | | size_t curlx_sitouz(int sinum) |
267 | 0 | { |
268 | | #ifdef __INTEL_COMPILER |
269 | | #pragma warning(push) |
270 | | #pragma warning(disable:810) /* conversion may lose significant bits */ |
271 | | #endif |
272 | |
|
273 | 0 | DEBUGASSERT(sinum >= 0); |
274 | 0 | return (size_t)sinum; |
275 | |
|
276 | | #ifdef __INTEL_COMPILER |
277 | | #pragma warning(pop) |
278 | | #endif |
279 | 0 | } |
280 | | |
281 | | size_t curlx_uitouz(unsigned int uinum) |
282 | 0 | { |
283 | 0 | return (size_t)uinum; |
284 | 0 | } |
285 | | |
286 | | size_t curlx_sotouz_range(curl_off_t sonum, size_t uzmin, size_t uzmax) |
287 | 0 | { |
288 | 0 | if(sonum < 0) |
289 | 0 | return uzmin; |
290 | | #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T |
291 | | if(sonum > SIZE_MAX) |
292 | | return uzmax; |
293 | | #endif |
294 | 0 | return CURLMIN(CURLMAX((size_t)sonum, uzmin), uzmax); |
295 | 0 | } |
296 | | |
297 | | bool curlx_sztouz(ssize_t sznum, size_t *puznum) |
298 | 0 | { |
299 | 0 | if(sznum < 0) { |
300 | 0 | *puznum = 0; |
301 | 0 | return FALSE; |
302 | 0 | } |
303 | 0 | *puznum = (size_t)sznum; |
304 | 0 | return TRUE; |
305 | 0 | } |
306 | | |
307 | | bool curlx_sotouz_fits(curl_off_t sonum, size_t *puznum) |
308 | 0 | { |
309 | 0 | if(sonum < 0) { |
310 | 0 | *puznum = 0; |
311 | 0 | return FALSE; |
312 | 0 | } |
313 | | #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T |
314 | | if(sonum > SIZE_MAX) { |
315 | | *puznum = 0; |
316 | | return FALSE; |
317 | | } |
318 | | #endif |
319 | 0 | *puznum = (size_t)sonum; |
320 | 0 | return TRUE; |
321 | 0 | } |
322 | | |
323 | | bool curlx_sltouz(long slnum, size_t *puznum) |
324 | 0 | { |
325 | 0 | if(slnum < 0) { |
326 | 0 | *puznum = 0; |
327 | 0 | return FALSE; |
328 | 0 | } |
329 | | /* We error in curl_setup.h if SIZEOF_LONG > SIZEOF_SIZE_T */ |
330 | 0 | *puznum = (size_t)slnum; |
331 | 0 | return TRUE; |
332 | 0 | } |
333 | | |
334 | | curl_off_t curlx_uztoso(size_t uznum) |
335 | 0 | { |
336 | 0 | #if SIZEOF_SIZE_T >= SIZEOF_CURL_OFF_T |
337 | 0 | if(uznum > (size_t)CURL_OFF_T_MAX) |
338 | 0 | return CURL_OFF_T_MAX; |
339 | 0 | #endif |
340 | 0 | return (curl_off_t)uznum; |
341 | 0 | } |