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