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