mirror of
https://github.com/acidanthera/audk.git
synced 2025-07-31 01:24:12 +02:00
MdePkg/BaseSafeIntLib: Add SafeIntLib class and instance
https://bugzilla.tianocore.org/show_bug.cgi?id=798 SafeIntLib provides helper functions to prevent integer overflow during type conversion, addition, subtraction, and multiplication. Conversion Functions ==================== * Converting from a signed type to an unsigned type of the same size, or vice-versa. * Converting to a smaller type that could possibly overflow. * Converting from a signed type to a larger unsigned type. Unsigned Addition, Subtraction, Multiplication =============================================== * Unsigned integer math functions protect from overflow and underflow (in case of subtraction). Signed Addition, Subtraction, Multiplication ============================================ * Strongly consider using unsigned numbers. * Signed numbers are often used where unsigned numbers should be used. For example file sizes and array indices should always be unsigned. Subtracting a larger positive signed number from a smaller positive signed number with SafeInt32Sub() will succeed, producing a negative number, that then must not be used as an array index (but can occasionally be used as a pointer index.) Similarly for adding a larger magnitude negative number to a smaller magnitude positive number. * SafeIntLib does not protect you from such errors. It tells you if your integer operations overflowed, not if you are doing the right thing with your non-overflowed integers. * Likewise you can overflow a buffer with a non-overflowed unsigned index. Based on content from the following branch/commits: https://github.com/Microsoft/MS_UEFI/tree/share/MsCapsuleSupport21ef3a321c
ca516b1a61
33bab4031a
Cc: Sean Brogan <sean.brogan@microsoft.com> Cc: Jiewen Yao <jiewen.yao@intel.com> Cc: Liming Gao <liming.gao@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com> Reviewed-by: Sean Brogan <sean.brogan@microsoft.com> Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
parent
11cf02f6d0
commit
d7a09cb86a
@ -1,7 +1,7 @@
|
|||||||
/** @file
|
/** @file
|
||||||
Processor or Compiler specific defines and types for AArch64.
|
Processor or Compiler specific defines and types for AArch64.
|
||||||
|
|
||||||
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
|
||||||
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
||||||
Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
|
Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
|
||||||
|
|
||||||
@ -99,6 +99,11 @@ typedef INT64 INTN;
|
|||||||
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
||||||
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal AArch64 INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-9223372036854775807LL) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The stack alignment required for AARCH64
|
/// The stack alignment required for AARCH64
|
||||||
///
|
///
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/** @file
|
/** @file
|
||||||
Processor or Compiler specific defines and types for ARM.
|
Processor or Compiler specific defines and types for ARM.
|
||||||
|
|
||||||
Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
|
||||||
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
||||||
This program and the accompanying materials
|
This program and the accompanying materials
|
||||||
are licensed and made available under the terms and conditions of the BSD License
|
are licensed and made available under the terms and conditions of the BSD License
|
||||||
@ -105,6 +105,11 @@ typedef INT32 INTN;
|
|||||||
#define MAX_INTN ((INTN)0x7FFFFFFF)
|
#define MAX_INTN ((INTN)0x7FFFFFFF)
|
||||||
#define MAX_UINTN ((UINTN)0xFFFFFFFF)
|
#define MAX_UINTN ((UINTN)0xFFFFFFFF)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal ARM INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-2147483647) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The stack alignment required for ARM
|
/// The stack alignment required for ARM
|
||||||
///
|
///
|
||||||
|
@ -396,6 +396,14 @@ struct _LIST_ENTRY {
|
|||||||
#define MAX_INT64 ((INT64)0x7FFFFFFFFFFFFFFFULL)
|
#define MAX_INT64 ((INT64)0x7FFFFFFFFFFFFFFFULL)
|
||||||
#define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
|
#define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum values for the signed UEFI Data Types
|
||||||
|
///
|
||||||
|
#define MIN_INT8 (((INT8) -127) - 1)
|
||||||
|
#define MIN_INT16 (((INT16) -32767) - 1)
|
||||||
|
#define MIN_INT32 (((INT32) -2147483647) - 1)
|
||||||
|
#define MIN_INT64 (((INT64) -9223372036854775807LL) - 1)
|
||||||
|
|
||||||
#define BIT0 0x00000001
|
#define BIT0 0x00000001
|
||||||
#define BIT1 0x00000002
|
#define BIT1 0x00000002
|
||||||
#define BIT2 0x00000004
|
#define BIT2 0x00000004
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
We currently only have one EBC compiler so there may be some Intel compiler
|
We currently only have one EBC compiler so there may be some Intel compiler
|
||||||
specific functions in this file.
|
specific functions in this file.
|
||||||
|
|
||||||
Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
|
||||||
This program and the accompanying materials are licensed and made available under
|
This program and the accompanying materials are licensed and made available under
|
||||||
the terms and conditions of the BSD License that accompanies this distribution.
|
the terms and conditions of the BSD License that accompanies this distribution.
|
||||||
The full text of the license may be found at
|
The full text of the license may be found at
|
||||||
@ -91,23 +91,28 @@ typedef unsigned long UINTN;
|
|||||||
/// A value of native width with the highest bit set.
|
/// A value of native width with the highest bit set.
|
||||||
/// Scalable macro to set the most significant bit in a natural number.
|
/// Scalable macro to set the most significant bit in a natural number.
|
||||||
///
|
///
|
||||||
#define MAX_BIT (1ULL << (sizeof (INTN) * 8 - 1))
|
#define MAX_BIT ((UINTN)((1ULL << (sizeof (INTN) * 8 - 1))))
|
||||||
///
|
///
|
||||||
/// A value of native width with the two highest bits set.
|
/// A value of native width with the two highest bits set.
|
||||||
/// Scalable macro to set the most 2 significant bits in a natural number.
|
/// Scalable macro to set the most 2 significant bits in a natural number.
|
||||||
///
|
///
|
||||||
#define MAX_2_BITS (3ULL << (sizeof (INTN) * 8 - 2))
|
#define MAX_2_BITS ((UINTN)(3ULL << (sizeof (INTN) * 8 - 2)))
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Maximum legal EBC address
|
/// Maximum legal EBC address
|
||||||
///
|
///
|
||||||
#define MAX_ADDRESS ((UINTN) ~0)
|
#define MAX_ADDRESS ((UINTN)(~0ULL >> (64 - sizeof (INTN) * 8)))
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Maximum legal EBC INTN and UINTN values.
|
/// Maximum legal EBC INTN and UINTN values.
|
||||||
///
|
///
|
||||||
#define MAX_UINTN ((UINTN) ~0)
|
#define MAX_UINTN ((UINTN)(~0ULL >> (64 - sizeof (INTN) * 8)))
|
||||||
#define MAX_INTN ((INTN)~MAX_BIT)
|
#define MAX_INTN ((INTN)(~0ULL >> (65 - sizeof (INTN) * 8)))
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal EBC INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-MAX_INTN) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The stack alignment required for EBC
|
/// The stack alignment required for EBC
|
||||||
|
@ -252,6 +252,11 @@ typedef INT32 INTN;
|
|||||||
#define MAX_INTN ((INTN)0x7FFFFFFF)
|
#define MAX_INTN ((INTN)0x7FFFFFFF)
|
||||||
#define MAX_UINTN ((UINTN)0xFFFFFFFF)
|
#define MAX_UINTN ((UINTN)0xFFFFFFFF)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal IA-32 INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-2147483647) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The stack alignment required for IA-32.
|
/// The stack alignment required for IA-32.
|
||||||
///
|
///
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/** @file
|
/** @file
|
||||||
Processor or Compiler specific defines and types for Intel Itanium(TM) processors.
|
Processor or Compiler specific defines and types for Intel Itanium(TM) processors.
|
||||||
|
|
||||||
Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
|
||||||
This program and the accompanying materials are licensed and made available
|
This program and the accompanying materials are licensed and made available
|
||||||
under the terms and conditions of the BSD License which accompanies this
|
under the terms and conditions of the BSD License which accompanies this
|
||||||
distribution. The full text of the license may be found at
|
distribution. The full text of the license may be found at
|
||||||
@ -242,6 +242,11 @@ typedef INT64 INTN;
|
|||||||
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
||||||
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal Itanium-based INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-9223372036854775807LL) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Per the Itanium Software Conventions and Runtime Architecture Guide,
|
/// Per the Itanium Software Conventions and Runtime Architecture Guide,
|
||||||
/// section 3.3.4, IPF stack must always be 16-byte aligned.
|
/// section 3.3.4, IPF stack must always be 16-byte aligned.
|
||||||
|
3030
MdePkg/Include/Library/SafeIntLib.h
Normal file
3030
MdePkg/Include/Library/SafeIntLib.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -266,6 +266,11 @@ typedef INT64 INTN;
|
|||||||
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
#define MAX_INTN ((INTN)0x7FFFFFFFFFFFFFFFULL)
|
||||||
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
#define MAX_UINTN ((UINTN)0xFFFFFFFFFFFFFFFFULL)
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Minimum legal x64 INTN value.
|
||||||
|
///
|
||||||
|
#define MIN_INTN (((INTN)-9223372036854775807LL) - 1)
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The stack alignment required for x64
|
/// The stack alignment required for x64
|
||||||
///
|
///
|
||||||
|
58
MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
Normal file
58
MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
## @file
|
||||||
|
# Safe Integer Library
|
||||||
|
#
|
||||||
|
# This library provides helper functions to prevent integer overflow during
|
||||||
|
# type conversion, addition, subtraction, and multiplication.
|
||||||
|
#
|
||||||
|
# Copyright (c) 2017, Microsoft Corporation
|
||||||
|
#
|
||||||
|
# All rights reserved.
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
# 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer.
|
||||||
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
|
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
#
|
||||||
|
##
|
||||||
|
|
||||||
|
[Defines]
|
||||||
|
INF_VERSION = 0x00010005
|
||||||
|
BASE_NAME = BaseSafeIntLib
|
||||||
|
FILE_GUID = 4EA91BFA-3482-4930-B136-70679C6CE489
|
||||||
|
MODULE_TYPE = BASE
|
||||||
|
VERSION_STRING = 1.0
|
||||||
|
LIBRARY_CLASS = SafeIntLib
|
||||||
|
|
||||||
|
#
|
||||||
|
# The following information is for reference only and not required by the build tools.
|
||||||
|
#
|
||||||
|
# VALID_ARCHITECTURES = IA32 X64
|
||||||
|
#
|
||||||
|
|
||||||
|
[Sources]
|
||||||
|
SafeIntLib.c
|
||||||
|
|
||||||
|
[Sources.Ia32, Sources.ARM]
|
||||||
|
SafeIntLib32.c
|
||||||
|
|
||||||
|
[Sources.X64, Sources.IPF, Sources.AARCH64]
|
||||||
|
SafeIntLib64.c
|
||||||
|
|
||||||
|
[Sources.EBC]
|
||||||
|
SafeIntLibEbc.c
|
||||||
|
|
||||||
|
[Packages]
|
||||||
|
MdePkg/MdePkg.dec
|
4098
MdePkg/Library/BaseSafeIntLib/SafeIntLib.c
Normal file
4098
MdePkg/Library/BaseSafeIntLib/SafeIntLib.c
Normal file
File diff suppressed because it is too large
Load Diff
554
MdePkg/Library/BaseSafeIntLib/SafeIntLib32.c
Normal file
554
MdePkg/Library/BaseSafeIntLib/SafeIntLib32.c
Normal file
@ -0,0 +1,554 @@
|
|||||||
|
/** @file
|
||||||
|
This library provides helper functions to prevent integer overflow during
|
||||||
|
type conversion, addition, subtraction, and multiplication.
|
||||||
|
|
||||||
|
Copyright (c) 2017, Microsoft Corporation
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <Base.h>
|
||||||
|
#include <Library/SafeIntLib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT32 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt32ToUintn (
|
||||||
|
IN INT32 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt32ToUint32 (Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT32 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint32ToIntn (
|
||||||
|
IN UINT32 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint32ToInt32 (Operand, (INT32 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> INT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToInt32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT INT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = (INT32)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToUint32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Operand >= 0) {
|
||||||
|
*Result = (UINT32)Operand;
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINT32_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToUint32 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = (UINT32)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> INT64 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToInt64 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT INT64 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = (INT64)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToIntn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToInt32 (Operand, (INT32 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToUintn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToUint32 (Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint64ToUintn (
|
||||||
|
IN UINT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64ToUint32 ((UINT64) Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnAdd (
|
||||||
|
IN UINTN Augend,
|
||||||
|
IN UINTN Addend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((Augend + Addend) >= Augend) {
|
||||||
|
*Result = (Augend + Addend);
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINTN_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnSub (
|
||||||
|
IN UINTN Minuend,
|
||||||
|
IN UINTN Subtrahend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Minuend >= Subtrahend) {
|
||||||
|
*Result = (Minuend - Subtrahend);
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINTN_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnMult (
|
||||||
|
IN UINTN Multiplicand,
|
||||||
|
IN UINTN Multiplier,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT64 IntermediateResult;
|
||||||
|
|
||||||
|
IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);
|
||||||
|
|
||||||
|
return SafeUint64ToUintn (IntermediateResult, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnAdd (
|
||||||
|
IN INTN Augend,
|
||||||
|
IN INTN Addend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToIntn (((INT64)Augend) + ((INT64)Addend), Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnSub (
|
||||||
|
IN INTN Minuend,
|
||||||
|
IN INTN Subtrahend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToIntn (((INT64)Minuend) - ((INT64)Subtrahend), Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnMult (
|
||||||
|
IN INTN Multiplicand,
|
||||||
|
IN INTN Multiplier,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToIntn (((INT64)Multiplicand) *((INT64)Multiplier), Result);
|
||||||
|
}
|
||||||
|
|
508
MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c
Normal file
508
MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c
Normal file
@ -0,0 +1,508 @@
|
|||||||
|
/** @file
|
||||||
|
This library provides helper functions to prevent integer overflow during
|
||||||
|
type conversion, addition, subtraction, and multiplication.
|
||||||
|
|
||||||
|
Copyright (c) 2017, Microsoft Corporation
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <Base.h>
|
||||||
|
#include <Library/SafeIntLib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT32 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt32ToUintn (
|
||||||
|
IN INT32 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt32ToUint64 (Operand, (UINT64 *) Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT32 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint32ToIntn (
|
||||||
|
IN UINT32 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> INT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToInt32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT INT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToInt32 ((INT64) Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToUint32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToUint32 ((INT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToUint32 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64ToUint32 ((UINT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> INT64 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToInt64 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT INT64 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64ToInt64 ((UINT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToIntn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = (INTN)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToUintn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64ToUint64 (Operand, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint64ToUintn (
|
||||||
|
IN UINT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*Result = Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnAdd (
|
||||||
|
IN UINTN Augend,
|
||||||
|
IN UINTN Addend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64Add ((UINT64)Augend, (UINT64)Addend, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnSub (
|
||||||
|
IN UINTN Minuend,
|
||||||
|
IN UINTN Subtrahend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64Sub ((UINT64)Minuend, (UINT64)Subtrahend, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnMult (
|
||||||
|
IN UINTN Multiplicand,
|
||||||
|
IN UINTN Multiplier,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeUint64Mult ((UINT64)Multiplicand, (UINT64)Multiplier, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnAdd (
|
||||||
|
IN INTN Augend,
|
||||||
|
IN INTN Addend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64Add ((INT64)Augend, (INT64)Addend, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnSub (
|
||||||
|
IN INTN Minuend,
|
||||||
|
IN INTN Subtrahend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64Sub ((INT64)Minuend, (INT64)Subtrahend, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnMult (
|
||||||
|
IN INTN Multiplicand,
|
||||||
|
IN INTN Multiplier,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return SafeInt64Mult ((INT64)Multiplicand, (INT64)Multiplier, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
614
MdePkg/Library/BaseSafeIntLib/SafeIntLibEbc.c
Normal file
614
MdePkg/Library/BaseSafeIntLib/SafeIntLibEbc.c
Normal file
@ -0,0 +1,614 @@
|
|||||||
|
/** @file
|
||||||
|
This library provides helper functions to prevent integer overflow during
|
||||||
|
type conversion, addition, subtraction, and multiplication.
|
||||||
|
|
||||||
|
Copyright (c) 2017, Microsoft Corporation
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <Base.h>
|
||||||
|
#include <Library/SafeIntLib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT32 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt32ToUintn (
|
||||||
|
IN INT32 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt32ToUint32 (Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
return SafeInt32ToUint64 (Operand, (UINT64 *) Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT32 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint32ToIntn (
|
||||||
|
IN UINT32 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeUint32ToInt32 (Operand, (INT32 *)Result);
|
||||||
|
}
|
||||||
|
*Result = Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> INT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToInt32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT INT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
*Result = (INT32)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
return SafeInt64ToInt32 ((INT64) Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnToUint32 (
|
||||||
|
IN INTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
if (Operand >= 0) {
|
||||||
|
*Result = (UINT32)Operand;
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINT32_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
return SafeInt64ToUint32 ((INT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> UINT32 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToUint32 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT UINT32 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
*Result = (UINT32)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
return SafeUint64ToUint32 ((UINT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN -> INT64 conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnToInt64 (
|
||||||
|
IN UINTN Operand,
|
||||||
|
OUT INT64 *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
*Result = (INT64)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
return SafeUint64ToInt64 ((UINT64)Operand, Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> INTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToIntn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt64ToInt32 (Operand, (INT32 *)Result);
|
||||||
|
}
|
||||||
|
*Result = (INTN)Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeInt64ToUintn (
|
||||||
|
IN INT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt64ToUint32 (Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
return SafeInt64ToUint64 (Operand, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINT64 -> UINTN conversion
|
||||||
|
|
||||||
|
Converts the value specified by Operand to a value specified by Result type
|
||||||
|
and stores the converted value into the caller allocated output buffer
|
||||||
|
specified by Result. The caller must pass in a Result buffer that is at
|
||||||
|
least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the conversion results in an overflow or an underflow condition, then
|
||||||
|
Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Operand Operand to be converted to new type
|
||||||
|
@param[out] Result Pointer to the result of conversion
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful conversion
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUint64ToUintn (
|
||||||
|
IN UINT64 Operand,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeUint64ToUint32 ((UINT64) Operand, (UINT32 *)Result);
|
||||||
|
}
|
||||||
|
*Result = Operand;
|
||||||
|
return RETURN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnAdd (
|
||||||
|
IN UINTN Augend,
|
||||||
|
IN UINTN Addend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
if ((UINT32)(Augend + Addend) >= Augend) {
|
||||||
|
*Result = (Augend + Addend);
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINTN_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
return SafeUint64Add ((UINT64)Augend, (UINT64)Addend, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnSub (
|
||||||
|
IN UINTN Minuend,
|
||||||
|
IN UINTN Subtrahend,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
|
||||||
|
if (Result == NULL) {
|
||||||
|
return RETURN_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
if (Minuend >= Subtrahend) {
|
||||||
|
*Result = (Minuend - Subtrahend);
|
||||||
|
Status = RETURN_SUCCESS;
|
||||||
|
} else {
|
||||||
|
*Result = UINTN_ERROR;
|
||||||
|
Status = RETURN_BUFFER_TOO_SMALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
return SafeUint64Sub ((UINT64)Minuend, (UINT64)Subtrahend, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
UINTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeUintnMult (
|
||||||
|
IN UINTN Multiplicand,
|
||||||
|
IN UINTN Multiplier,
|
||||||
|
OUT UINTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT64 IntermediateResult;
|
||||||
|
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);
|
||||||
|
|
||||||
|
return SafeUint64ToUintn (IntermediateResult, Result);
|
||||||
|
}
|
||||||
|
return SafeUint64Mult ((UINT64)Multiplicand, (UINT64)Multiplier, (UINT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Addition
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Augend A number to which addend will be added
|
||||||
|
@param[in] Addend A number to be added to another
|
||||||
|
@param[out] Result Pointer to the result of addition
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful addition
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnAdd (
|
||||||
|
IN INTN Augend,
|
||||||
|
IN INTN Addend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt64ToIntn (((INT64)Augend) + ((INT64)Addend), Result);
|
||||||
|
}
|
||||||
|
return SafeInt64Add ((INT64)Augend, (INT64)Addend, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN Subtraction
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Minuend A number from which another is to be subtracted.
|
||||||
|
@param[in] Subtrahend A number to be subtracted from another
|
||||||
|
@param[out] Result Pointer to the result of subtraction
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful subtraction
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Underflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnSub (
|
||||||
|
IN INTN Minuend,
|
||||||
|
IN INTN Subtrahend,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt64ToIntn (((INT64)Minuend) - ((INT64)Subtrahend), Result);
|
||||||
|
}
|
||||||
|
return SafeInt64Sub ((INT64)Minuend, (INT64)Subtrahend, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
INTN multiplication
|
||||||
|
|
||||||
|
Performs the requested operation using the input parameters into a value
|
||||||
|
specified by Result type and stores the converted value into the caller
|
||||||
|
allocated output buffer specified by Result. The caller must pass in a
|
||||||
|
Result buffer that is at least as large as the Result type.
|
||||||
|
|
||||||
|
If Result is NULL, RETURN_INVALID_PARAMETER is returned.
|
||||||
|
|
||||||
|
If the requested operation results in an overflow or an underflow condition,
|
||||||
|
then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
|
||||||
|
|
||||||
|
@param[in] Multiplicand A number that is to be multiplied by another
|
||||||
|
@param[in] Multiplier A number by which the multiplicand is to be multiplied
|
||||||
|
@param[out] Result Pointer to the result of multiplication
|
||||||
|
|
||||||
|
@retval RETURN_SUCCESS Successful multiplication
|
||||||
|
@retval RETURN_BUFFER_TOO_SMALL Overflow
|
||||||
|
@retval RETURN_INVALID_PARAMETER Result is NULL
|
||||||
|
**/
|
||||||
|
RETURN_STATUS
|
||||||
|
EFIAPI
|
||||||
|
SafeIntnMult (
|
||||||
|
IN INTN Multiplicand,
|
||||||
|
IN INTN Multiplier,
|
||||||
|
OUT INTN *Result
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
||||||
|
return SafeInt64ToIntn (((INT64)Multiplicand) *((INT64)Multiplier), Result);
|
||||||
|
}
|
||||||
|
return SafeInt64Mult ((INT64)Multiplicand, (INT64)Multiplier, (INT64 *)Result);
|
||||||
|
}
|
||||||
|
|
@ -241,6 +241,11 @@
|
|||||||
## @libraryclass provides EFI_FILE_HANDLE services
|
## @libraryclass provides EFI_FILE_HANDLE services
|
||||||
FileHandleLib|Include/Library/FileHandleLib.h
|
FileHandleLib|Include/Library/FileHandleLib.h
|
||||||
|
|
||||||
|
## @libraryclass provides helper functions to prevent integer overflow during
|
||||||
|
# type conversion, addition, subtraction, and multiplication.
|
||||||
|
##
|
||||||
|
SafeIntLib|Include/Library/SafeIntLib.h
|
||||||
|
|
||||||
[LibraryClasses.IA32, LibraryClasses.X64]
|
[LibraryClasses.IA32, LibraryClasses.X64]
|
||||||
## @libraryclass Abstracts both S/W SMI generation and detection.
|
## @libraryclass Abstracts both S/W SMI generation and detection.
|
||||||
##
|
##
|
||||||
|
@ -86,6 +86,7 @@
|
|||||||
MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
|
MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
|
||||||
MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.inf
|
MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.inf
|
||||||
MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf
|
MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf
|
||||||
|
MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
|
||||||
|
|
||||||
MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf
|
MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf
|
||||||
MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf
|
MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user