]>
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 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/SafeIntLib.h>
16 INT32 -> UINTN conversion
18 Converts the value specified by Operand to a value specified by Result type
19 and stores the converted value into the caller allocated output buffer
20 specified by Result. The caller must pass in a Result buffer that is at
21 least as large as the Result type.
23 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
25 If the conversion results in an overflow or an underflow condition, then
26 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
28 @param[in] Operand Operand to be converted to new type
29 @param[out] Result Pointer to the result of conversion
31 @retval RETURN_SUCCESS Successful conversion
32 @retval RETURN_BUFFER_TOO_SMALL Overflow
33 @retval RETURN_INVALID_PARAMETER Result is NULL
42 return SafeInt32ToUint64 (Operand
, (UINT64
*) Result
);
46 UINT32 -> INTN conversion
48 Converts the value specified by Operand to a value specified by Result type
49 and stores the converted value into the caller allocated output buffer
50 specified by Result. The caller must pass in a Result buffer that is at
51 least as large as the Result type.
53 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
55 If the conversion results in an overflow or an underflow condition, then
56 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
58 @param[in] Operand Operand to be converted to new type
59 @param[out] Result Pointer to the result of conversion
61 @retval RETURN_SUCCESS Successful conversion
62 @retval RETURN_BUFFER_TOO_SMALL Overflow
63 @retval RETURN_INVALID_PARAMETER Result is NULL
73 return RETURN_INVALID_PARAMETER
;
77 return RETURN_SUCCESS
;
81 INTN -> INT32 conversion
83 Converts the value specified by Operand to a value specified by Result type
84 and stores the converted value into the caller allocated output buffer
85 specified by Result. The caller must pass in a Result buffer that is at
86 least as large as the Result type.
88 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
90 If the conversion results in an overflow or an underflow condition, then
91 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
93 @param[in] Operand Operand to be converted to new type
94 @param[out] Result Pointer to the result of conversion
96 @retval RETURN_SUCCESS Successful conversion
97 @retval RETURN_BUFFER_TOO_SMALL Overflow
98 @retval RETURN_INVALID_PARAMETER Result is NULL
107 return SafeInt64ToInt32 ((INT64
) Operand
, Result
);
111 INTN -> UINT32 conversion
113 Converts the value specified by Operand to a value specified by Result type
114 and stores the converted value into the caller allocated output buffer
115 specified by Result. The caller must pass in a Result buffer that is at
116 least as large as the Result type.
118 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
120 If the conversion results in an overflow or an underflow condition, then
121 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
123 @param[in] Operand Operand to be converted to new type
124 @param[out] Result Pointer to the result of conversion
126 @retval RETURN_SUCCESS Successful conversion
127 @retval RETURN_BUFFER_TOO_SMALL Overflow
128 @retval RETURN_INVALID_PARAMETER Result is NULL
137 return SafeInt64ToUint32 ((INT64
)Operand
, Result
);
141 UINTN -> UINT32 conversion
143 Converts the value specified by Operand to a value specified by Result type
144 and stores the converted value into the caller allocated output buffer
145 specified by Result. The caller must pass in a Result buffer that is at
146 least as large as the Result type.
148 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
150 If the conversion results in an overflow or an underflow condition, then
151 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
153 @param[in] Operand Operand to be converted to new type
154 @param[out] Result Pointer to the result of conversion
156 @retval RETURN_SUCCESS Successful conversion
157 @retval RETURN_BUFFER_TOO_SMALL Overflow
158 @retval RETURN_INVALID_PARAMETER Result is NULL
167 return SafeUint64ToUint32 ((UINT64
)Operand
, Result
);
171 UINTN -> INT64 conversion
173 Converts the value specified by Operand to a value specified by Result type
174 and stores the converted value into the caller allocated output buffer
175 specified by Result. The caller must pass in a Result buffer that is at
176 least as large as the Result type.
178 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
180 If the conversion results in an overflow or an underflow condition, then
181 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
183 @param[in] Operand Operand to be converted to new type
184 @param[out] Result Pointer to the result of conversion
186 @retval RETURN_SUCCESS Successful conversion
187 @retval RETURN_BUFFER_TOO_SMALL Overflow
188 @retval RETURN_INVALID_PARAMETER Result is NULL
197 return SafeUint64ToInt64 ((UINT64
)Operand
, Result
);
201 INT64 -> INTN conversion
203 Converts the value specified by Operand to a value specified by Result type
204 and stores the converted value into the caller allocated output buffer
205 specified by Result. The caller must pass in a Result buffer that is at
206 least as large as the Result type.
208 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
210 If the conversion results in an overflow or an underflow condition, then
211 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
213 @param[in] Operand Operand to be converted to new type
214 @param[out] Result Pointer to the result of conversion
216 @retval RETURN_SUCCESS Successful conversion
217 @retval RETURN_BUFFER_TOO_SMALL Overflow
218 @retval RETURN_INVALID_PARAMETER Result is NULL
227 if (Result
== NULL
) {
228 return RETURN_INVALID_PARAMETER
;
231 *Result
= (INTN
)Operand
;
232 return RETURN_SUCCESS
;
236 INT64 -> UINTN conversion
238 Converts the value specified by Operand to a value specified by Result type
239 and stores the converted value into the caller allocated output buffer
240 specified by Result. The caller must pass in a Result buffer that is at
241 least as large as the Result type.
243 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
245 If the conversion results in an overflow or an underflow condition, then
246 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
248 @param[in] Operand Operand to be converted to new type
249 @param[out] Result Pointer to the result of conversion
251 @retval RETURN_SUCCESS Successful conversion
252 @retval RETURN_BUFFER_TOO_SMALL Overflow
253 @retval RETURN_INVALID_PARAMETER Result is NULL
262 return SafeInt64ToUint64 (Operand
, (UINT64
*)Result
);
266 UINT64 -> UINTN conversion
268 Converts the value specified by Operand to a value specified by Result type
269 and stores the converted value into the caller allocated output buffer
270 specified by Result. The caller must pass in a Result buffer that is at
271 least as large as the Result type.
273 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
275 If the conversion results in an overflow or an underflow condition, then
276 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
278 @param[in] Operand Operand to be converted to new type
279 @param[out] Result Pointer to the result of conversion
281 @retval RETURN_SUCCESS Successful conversion
282 @retval RETURN_BUFFER_TOO_SMALL Overflow
283 @retval RETURN_INVALID_PARAMETER Result is NULL
292 if (Result
== NULL
) {
293 return RETURN_INVALID_PARAMETER
;
297 return RETURN_SUCCESS
;
303 Performs the requested operation using the input parameters into a value
304 specified by Result type and stores the converted value into the caller
305 allocated output buffer specified by Result. The caller must pass in a
306 Result buffer that is at least as large as the Result type.
308 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
310 If the requested operation results in an overflow or an underflow condition,
311 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
313 @param[in] Augend A number to which addend will be added
314 @param[in] Addend A number to be added to another
315 @param[out] Result Pointer to the result of addition
317 @retval RETURN_SUCCESS Successful addition
318 @retval RETURN_BUFFER_TOO_SMALL Overflow
319 @retval RETURN_INVALID_PARAMETER Result is NULL
329 return SafeUint64Add ((UINT64
)Augend
, (UINT64
)Addend
, (UINT64
*)Result
);
335 Performs the requested operation using the input parameters into a value
336 specified by Result type and stores the converted value into the caller
337 allocated output buffer specified by Result. The caller must pass in a
338 Result buffer that is at least as large as the Result type.
340 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
342 If the requested operation results in an overflow or an underflow condition,
343 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
345 @param[in] Minuend A number from which another is to be subtracted.
346 @param[in] Subtrahend A number to be subtracted from another
347 @param[out] Result Pointer to the result of subtraction
349 @retval RETURN_SUCCESS Successful subtraction
350 @retval RETURN_BUFFER_TOO_SMALL Underflow
351 @retval RETURN_INVALID_PARAMETER Result is NULL
361 return SafeUint64Sub ((UINT64
)Minuend
, (UINT64
)Subtrahend
, (UINT64
*)Result
);
367 Performs the requested operation using the input parameters into a value
368 specified by Result type and stores the converted value into the caller
369 allocated output buffer specified by Result. The caller must pass in a
370 Result buffer that is at least as large as the Result type.
372 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
374 If the requested operation results in an overflow or an underflow condition,
375 then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
377 @param[in] Multiplicand A number that is to be multiplied by another
378 @param[in] Multiplier A number by which the multiplicand is to be multiplied
379 @param[out] Result Pointer to the result of multiplication
381 @retval RETURN_SUCCESS Successful multiplication
382 @retval RETURN_BUFFER_TOO_SMALL Overflow
383 @retval RETURN_INVALID_PARAMETER Result is NULL
388 IN UINTN Multiplicand
,
393 return SafeUint64Mult ((UINT64
)Multiplicand
, (UINT64
)Multiplier
, (UINT64
*)Result
);
399 Performs the requested operation using the input parameters into a value
400 specified by Result type and stores the converted value into the caller
401 allocated output buffer specified by Result. The caller must pass in a
402 Result buffer that is at least as large as the Result type.
404 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
406 If the requested operation results in an overflow or an underflow condition,
407 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
409 @param[in] Augend A number to which addend will be added
410 @param[in] Addend A number to be added to another
411 @param[out] Result Pointer to the result of addition
413 @retval RETURN_SUCCESS Successful addition
414 @retval RETURN_BUFFER_TOO_SMALL Overflow
415 @retval RETURN_INVALID_PARAMETER Result is NULL
425 return SafeInt64Add ((INT64
)Augend
, (INT64
)Addend
, (INT64
*)Result
);
431 Performs the requested operation using the input parameters into a value
432 specified by Result type and stores the converted value into the caller
433 allocated output buffer specified by Result. The caller must pass in a
434 Result buffer that is at least as large as the Result type.
436 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
438 If the requested operation results in an overflow or an underflow condition,
439 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
441 @param[in] Minuend A number from which another is to be subtracted.
442 @param[in] Subtrahend A number to be subtracted from another
443 @param[out] Result Pointer to the result of subtraction
445 @retval RETURN_SUCCESS Successful subtraction
446 @retval RETURN_BUFFER_TOO_SMALL Underflow
447 @retval RETURN_INVALID_PARAMETER Result is NULL
457 return SafeInt64Sub ((INT64
)Minuend
, (INT64
)Subtrahend
, (INT64
*)Result
);
463 Performs the requested operation using the input parameters into a value
464 specified by Result type and stores the converted value into the caller
465 allocated output buffer specified by Result. The caller must pass in a
466 Result buffer that is at least as large as the Result type.
468 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
470 If the requested operation results in an overflow or an underflow condition,
471 then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
473 @param[in] Multiplicand A number that is to be multiplied by another
474 @param[in] Multiplier A number by which the multiplicand is to be multiplied
475 @param[out] Result Pointer to the result of multiplication
477 @retval RETURN_SUCCESS Successful multiplication
478 @retval RETURN_BUFFER_TOO_SMALL Overflow
479 @retval RETURN_INVALID_PARAMETER Result is NULL
484 IN INTN Multiplicand
,
489 return SafeInt64Mult ((INT64
)Multiplicand
, (INT64
)Multiplier
, (INT64
*)Result
);