]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c
2 This library provides helper functions to prevent integer overflow during
3 type conversion, addition, subtraction, and multiplication.
5 Copyright (c) 2017, Microsoft Corporation
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <Library/SafeIntLib.h>
33 INT32 -> UINTN conversion
35 Converts the value specified by Operand to a value specified by Result type
36 and stores the converted value into the caller allocated output buffer
37 specified by Result. The caller must pass in a Result buffer that is at
38 least as large as the Result type.
40 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
42 If the conversion results in an overflow or an underflow condition, then
43 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
45 @param[in] Operand Operand to be converted to new type
46 @param[out] Result Pointer to the result of conversion
48 @retval RETURN_SUCCESS Successful conversion
49 @retval RETURN_BUFFER_TOO_SMALL Overflow
50 @retval RETURN_INVALID_PARAMETER Result is NULL
59 return SafeInt32ToUint64 (Operand
, (UINT64
*) Result
);
63 UINT32 -> INTN conversion
65 Converts the value specified by Operand to a value specified by Result type
66 and stores the converted value into the caller allocated output buffer
67 specified by Result. The caller must pass in a Result buffer that is at
68 least as large as the Result type.
70 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
72 If the conversion results in an overflow or an underflow condition, then
73 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
75 @param[in] Operand Operand to be converted to new type
76 @param[out] Result Pointer to the result of conversion
78 @retval RETURN_SUCCESS Successful conversion
79 @retval RETURN_BUFFER_TOO_SMALL Overflow
80 @retval RETURN_INVALID_PARAMETER Result is NULL
90 return RETURN_INVALID_PARAMETER
;
94 return RETURN_SUCCESS
;
98 INTN -> INT32 conversion
100 Converts the value specified by Operand to a value specified by Result type
101 and stores the converted value into the caller allocated output buffer
102 specified by Result. The caller must pass in a Result buffer that is at
103 least as large as the Result type.
105 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
107 If the conversion results in an overflow or an underflow condition, then
108 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
110 @param[in] Operand Operand to be converted to new type
111 @param[out] Result Pointer to the result of conversion
113 @retval RETURN_SUCCESS Successful conversion
114 @retval RETURN_BUFFER_TOO_SMALL Overflow
115 @retval RETURN_INVALID_PARAMETER Result is NULL
124 return SafeInt64ToInt32 ((INT64
) Operand
, Result
);
128 INTN -> UINT32 conversion
130 Converts the value specified by Operand to a value specified by Result type
131 and stores the converted value into the caller allocated output buffer
132 specified by Result. The caller must pass in a Result buffer that is at
133 least as large as the Result type.
135 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
137 If the conversion results in an overflow or an underflow condition, then
138 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
140 @param[in] Operand Operand to be converted to new type
141 @param[out] Result Pointer to the result of conversion
143 @retval RETURN_SUCCESS Successful conversion
144 @retval RETURN_BUFFER_TOO_SMALL Overflow
145 @retval RETURN_INVALID_PARAMETER Result is NULL
154 return SafeInt64ToUint32 ((INT64
)Operand
, Result
);
158 UINTN -> UINT32 conversion
160 Converts the value specified by Operand to a value specified by Result type
161 and stores the converted value into the caller allocated output buffer
162 specified by Result. The caller must pass in a Result buffer that is at
163 least as large as the Result type.
165 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
167 If the conversion results in an overflow or an underflow condition, then
168 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
170 @param[in] Operand Operand to be converted to new type
171 @param[out] Result Pointer to the result of conversion
173 @retval RETURN_SUCCESS Successful conversion
174 @retval RETURN_BUFFER_TOO_SMALL Overflow
175 @retval RETURN_INVALID_PARAMETER Result is NULL
184 return SafeUint64ToUint32 ((UINT64
)Operand
, Result
);
188 UINTN -> INT64 conversion
190 Converts the value specified by Operand to a value specified by Result type
191 and stores the converted value into the caller allocated output buffer
192 specified by Result. The caller must pass in a Result buffer that is at
193 least as large as the Result type.
195 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
197 If the conversion results in an overflow or an underflow condition, then
198 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
200 @param[in] Operand Operand to be converted to new type
201 @param[out] Result Pointer to the result of conversion
203 @retval RETURN_SUCCESS Successful conversion
204 @retval RETURN_BUFFER_TOO_SMALL Overflow
205 @retval RETURN_INVALID_PARAMETER Result is NULL
214 return SafeUint64ToInt64 ((UINT64
)Operand
, Result
);
218 INT64 -> INTN conversion
220 Converts the value specified by Operand to a value specified by Result type
221 and stores the converted value into the caller allocated output buffer
222 specified by Result. The caller must pass in a Result buffer that is at
223 least as large as the Result type.
225 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
227 If the conversion results in an overflow or an underflow condition, then
228 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
230 @param[in] Operand Operand to be converted to new type
231 @param[out] Result Pointer to the result of conversion
233 @retval RETURN_SUCCESS Successful conversion
234 @retval RETURN_BUFFER_TOO_SMALL Overflow
235 @retval RETURN_INVALID_PARAMETER Result is NULL
244 if (Result
== NULL
) {
245 return RETURN_INVALID_PARAMETER
;
248 *Result
= (INTN
)Operand
;
249 return RETURN_SUCCESS
;
253 INT64 -> UINTN conversion
255 Converts the value specified by Operand to a value specified by Result type
256 and stores the converted value into the caller allocated output buffer
257 specified by Result. The caller must pass in a Result buffer that is at
258 least as large as the Result type.
260 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
262 If the conversion results in an overflow or an underflow condition, then
263 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
265 @param[in] Operand Operand to be converted to new type
266 @param[out] Result Pointer to the result of conversion
268 @retval RETURN_SUCCESS Successful conversion
269 @retval RETURN_BUFFER_TOO_SMALL Overflow
270 @retval RETURN_INVALID_PARAMETER Result is NULL
279 return SafeInt64ToUint64 (Operand
, (UINT64
*)Result
);
283 UINT64 -> UINTN conversion
285 Converts the value specified by Operand to a value specified by Result type
286 and stores the converted value into the caller allocated output buffer
287 specified by Result. The caller must pass in a Result buffer that is at
288 least as large as the Result type.
290 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
292 If the conversion results in an overflow or an underflow condition, then
293 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
295 @param[in] Operand Operand to be converted to new type
296 @param[out] Result Pointer to the result of conversion
298 @retval RETURN_SUCCESS Successful conversion
299 @retval RETURN_BUFFER_TOO_SMALL Overflow
300 @retval RETURN_INVALID_PARAMETER Result is NULL
309 if (Result
== NULL
) {
310 return RETURN_INVALID_PARAMETER
;
314 return RETURN_SUCCESS
;
320 Performs the requested operation using the input parameters into a value
321 specified by Result type and stores the converted value into the caller
322 allocated output buffer specified by Result. The caller must pass in a
323 Result buffer that is at least as large as the Result type.
325 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
327 If the requested operation results in an overflow or an underflow condition,
328 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
330 @param[in] Augend A number to which addend will be added
331 @param[in] Addend A number to be added to another
332 @param[out] Result Pointer to the result of addition
334 @retval RETURN_SUCCESS Successful addition
335 @retval RETURN_BUFFER_TOO_SMALL Overflow
336 @retval RETURN_INVALID_PARAMETER Result is NULL
346 return SafeUint64Add ((UINT64
)Augend
, (UINT64
)Addend
, (UINT64
*)Result
);
352 Performs the requested operation using the input parameters into a value
353 specified by Result type and stores the converted value into the caller
354 allocated output buffer specified by Result. The caller must pass in a
355 Result buffer that is at least as large as the Result type.
357 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
359 If the requested operation results in an overflow or an underflow condition,
360 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
362 @param[in] Minuend A number from which another is to be subtracted.
363 @param[in] Subtrahend A number to be subtracted from another
364 @param[out] Result Pointer to the result of subtraction
366 @retval RETURN_SUCCESS Successful subtraction
367 @retval RETURN_BUFFER_TOO_SMALL Underflow
368 @retval RETURN_INVALID_PARAMETER Result is NULL
378 return SafeUint64Sub ((UINT64
)Minuend
, (UINT64
)Subtrahend
, (UINT64
*)Result
);
384 Performs the requested operation using the input parameters into a value
385 specified by Result type and stores the converted value into the caller
386 allocated output buffer specified by Result. The caller must pass in a
387 Result buffer that is at least as large as the Result type.
389 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
391 If the requested operation results in an overflow or an underflow condition,
392 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
394 @param[in] Multiplicand A number that is to be multiplied by another
395 @param[in] Multiplier A number by which the multiplicand is to be multiplied
396 @param[out] Result Pointer to the result of multiplication
398 @retval RETURN_SUCCESS Successful multiplication
399 @retval RETURN_BUFFER_TOO_SMALL Overflow
400 @retval RETURN_INVALID_PARAMETER Result is NULL
405 IN UINTN Multiplicand
,
410 return SafeUint64Mult ((UINT64
)Multiplicand
, (UINT64
)Multiplier
, (UINT64
*)Result
);
416 Performs the requested operation using the input parameters into a value
417 specified by Result type and stores the converted value into the caller
418 allocated output buffer specified by Result. The caller must pass in a
419 Result buffer that is at least as large as the Result type.
421 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
423 If the requested operation results in an overflow or an underflow condition,
424 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
426 @param[in] Augend A number to which addend will be added
427 @param[in] Addend A number to be added to another
428 @param[out] Result Pointer to the result of addition
430 @retval RETURN_SUCCESS Successful addition
431 @retval RETURN_BUFFER_TOO_SMALL Overflow
432 @retval RETURN_INVALID_PARAMETER Result is NULL
442 return SafeInt64Add ((INT64
)Augend
, (INT64
)Addend
, (INT64
*)Result
);
448 Performs the requested operation using the input parameters into a value
449 specified by Result type and stores the converted value into the caller
450 allocated output buffer specified by Result. The caller must pass in a
451 Result buffer that is at least as large as the Result type.
453 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
455 If the requested operation results in an overflow or an underflow condition,
456 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
458 @param[in] Minuend A number from which another is to be subtracted.
459 @param[in] Subtrahend A number to be subtracted from another
460 @param[out] Result Pointer to the result of subtraction
462 @retval RETURN_SUCCESS Successful subtraction
463 @retval RETURN_BUFFER_TOO_SMALL Underflow
464 @retval RETURN_INVALID_PARAMETER Result is NULL
474 return SafeInt64Sub ((INT64
)Minuend
, (INT64
)Subtrahend
, (INT64
*)Result
);
480 Performs the requested operation using the input parameters into a value
481 specified by Result type and stores the converted value into the caller
482 allocated output buffer specified by Result. The caller must pass in a
483 Result buffer that is at least as large as the Result type.
485 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
487 If the requested operation results in an overflow or an underflow condition,
488 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
490 @param[in] Multiplicand A number that is to be multiplied by another
491 @param[in] Multiplier A number by which the multiplicand is to be multiplied
492 @param[out] Result Pointer to the result of multiplication
494 @retval RETURN_SUCCESS Successful multiplication
495 @retval RETURN_BUFFER_TOO_SMALL Overflow
496 @retval RETURN_INVALID_PARAMETER Result is NULL
501 IN INTN Multiplicand
,
506 return SafeInt64Mult ((INT64
)Multiplicand
, (INT64
)Multiplier
, (INT64
*)Result
);