]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseSafeIntLib: Add SafeIntLib class and instance
authorSean Brogan <sean.brogan@microsoft.com>
Mon, 24 Apr 2017 23:37:20 +0000 (16:37 -0700)
committerKinney, Michael D <michael.d.kinney@intel.com>
Thu, 25 Jan 2018 17:42:20 +0000 (09:42 -0800)
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/MsCapsuleSupport
https://github.com/Microsoft/MS_UEFI/commit/21ef3a321c907b40fa93797619c9f6c686dd92e0
https://github.com/Microsoft/MS_UEFI/commit/ca516b1a61315c2d823f453e12d2135098f53d61
https://github.com/Microsoft/MS_UEFI/commit/33bab4031a417d7d5a7d356c15a14c2e60302b2d

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>
15 files changed:
MdePkg/Include/AArch64/ProcessorBind.h
MdePkg/Include/Arm/ProcessorBind.h
MdePkg/Include/Base.h
MdePkg/Include/Ebc/ProcessorBind.h
MdePkg/Include/Ia32/ProcessorBind.h
MdePkg/Include/Ipf/ProcessorBind.h
MdePkg/Include/Library/SafeIntLib.h [new file with mode: 0644]
MdePkg/Include/X64/ProcessorBind.h
MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf [new file with mode: 0644]
MdePkg/Library/BaseSafeIntLib/SafeIntLib.c [new file with mode: 0644]
MdePkg/Library/BaseSafeIntLib/SafeIntLib32.c [new file with mode: 0644]
MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c [new file with mode: 0644]
MdePkg/Library/BaseSafeIntLib/SafeIntLibEbc.c [new file with mode: 0644]
MdePkg/MdePkg.dec
MdePkg/MdePkg.dsc

index 7b0f0ff32f3baa47ba6369190ea77d14a8d1631a..bc473562f9e5b4b2d49331a98256c6676e55111b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Processor or Compiler specific defines and types for AArch64.\r
 \r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
   Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>\r
 \r
@@ -99,6 +99,11 @@ typedef INT64   INTN;
 #define MAX_INTN   ((INTN)0x7FFFFFFFFFFFFFFFULL)\r
 #define MAX_UINTN  ((UINTN)0xFFFFFFFFFFFFFFFFULL)\r
 \r
+///\r
+/// Minimum legal AArch64 INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-9223372036854775807LL) - 1)\r
+\r
 ///\r
 /// The stack alignment required for AARCH64\r
 ///\r
index 42ea2f3055f3df30b2d325acbfec4749ac6da86f..c30d353f40e449cb53148c3c98a3d531daace97c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Processor or Compiler specific defines and types for ARM.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
   This program and the accompanying materials                          \r
   are licensed and made available under the terms and conditions of the BSD License         \r
@@ -105,6 +105,11 @@ typedef INT32   INTN;
 #define MAX_INTN   ((INTN)0x7FFFFFFF)\r
 #define MAX_UINTN  ((UINTN)0xFFFFFFFF)\r
 \r
+///\r
+/// Minimum legal ARM INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-2147483647) - 1)\r
+\r
 ///\r
 /// The stack alignment required for ARM\r
 ///\r
index 29db8a253e2f67d701308c3e98b0c065696f6d66..b01e03a03b960831ed4b5686e31ffde68c87efbe 100644 (file)
@@ -396,6 +396,14 @@ struct _LIST_ENTRY {
 #define MAX_INT64   ((INT64)0x7FFFFFFFFFFFFFFFULL)\r
 #define MAX_UINT64  ((UINT64)0xFFFFFFFFFFFFFFFFULL)\r
 \r
+///\r
+/// Minimum values for the signed UEFI Data Types\r
+///\r
+#define MIN_INT8   (((INT8)  -127) - 1)\r
+#define MIN_INT16  (((INT16) -32767) - 1)\r
+#define MIN_INT32  (((INT32) -2147483647) - 1)\r
+#define MIN_INT64  (((INT64) -9223372036854775807LL) - 1)\r
+\r
 #define  BIT0     0x00000001\r
 #define  BIT1     0x00000002\r
 #define  BIT2     0x00000004\r
index da8b1a6d802a995420fd833e8cb69c1f0c1af59b..ed41648913284cb8b0befa18b268e82b48d056bb 100644 (file)
@@ -4,7 +4,7 @@
   We currently only have one EBC compiler so there may be some Intel compiler\r
   specific functions in this file.\r
 \r
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
@@ -91,23 +91,28 @@ typedef unsigned long         UINTN;
 /// A value of native width with the highest bit set.\r
 /// Scalable macro to set the most significant bit in a natural number.\r
 ///\r
-#define MAX_BIT     (1ULL << (sizeof (INTN) * 8 - 1)) \r
+#define MAX_BIT     ((UINTN)((1ULL << (sizeof (INTN) * 8 - 1))))\r
 ///\r
 /// A value of native width with the two highest bits set.\r
 /// Scalable macro to set the most 2 significant bits in a natural number.\r
 ///\r
-#define MAX_2_BITS  (3ULL << (sizeof (INTN) * 8 - 2))\r
+#define MAX_2_BITS  ((UINTN)(3ULL << (sizeof (INTN) * 8 - 2)))\r
 \r
 ///\r
 /// Maximum legal EBC address\r
 ///\r
-#define MAX_ADDRESS   ((UINTN) ~0)\r
+#define MAX_ADDRESS   ((UINTN)(~0ULL >> (64 - sizeof (INTN) * 8)))\r
 \r
 ///\r
 /// Maximum legal EBC INTN and UINTN values.\r
 ///\r
-#define MAX_UINTN  ((UINTN) ~0)\r
-#define MAX_INTN   ((INTN)~MAX_BIT)\r
+#define MAX_UINTN  ((UINTN)(~0ULL >> (64 - sizeof (INTN) * 8)))\r
+#define MAX_INTN   ((INTN)(~0ULL >> (65 - sizeof (INTN) * 8)))\r
+\r
+///\r
+/// Minimum legal EBC INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-MAX_INTN) - 1)\r
 \r
 ///\r
 /// The stack alignment required for EBC\r
index aeecf3fa9feba9a45ad78c351e9f26a411c9db4a..1f9b56a8cb30399fcc38eff0379a2bedcc4bff43 100644 (file)
@@ -252,6 +252,11 @@ typedef INT32   INTN;
 #define MAX_INTN   ((INTN)0x7FFFFFFF)\r
 #define MAX_UINTN  ((UINTN)0xFFFFFFFF)\r
 \r
+///\r
+/// Minimum legal IA-32 INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-2147483647) - 1)\r
+\r
 ///\r
 /// The stack alignment required for IA-32.\r
 ///\r
index 51885ca6135941c5e3d45183ed9126fe772ff840..bfbae01abb8479704f7e2135d0c2b93fbb7f1a81 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Processor or Compiler specific defines and types for Intel Itanium(TM) processors.\r
 \r
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available \r
 under the terms and conditions of the BSD License which accompanies this\r
 distribution.  The full text of the license may be found at\r
@@ -242,6 +242,11 @@ typedef INT64   INTN;
 #define MAX_INTN   ((INTN)0x7FFFFFFFFFFFFFFFULL)\r
 #define MAX_UINTN  ((UINTN)0xFFFFFFFFFFFFFFFFULL)\r
 \r
+///\r
+/// Minimum legal Itanium-based INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-9223372036854775807LL) - 1)\r
+\r
 ///\r
 /// Per the Itanium Software Conventions and Runtime Architecture Guide,\r
 /// section 3.3.4, IPF stack must always be 16-byte aligned.\r
diff --git a/MdePkg/Include/Library/SafeIntLib.h b/MdePkg/Include/Library/SafeIntLib.h
new file mode 100644 (file)
index 0000000..5839301
--- /dev/null
@@ -0,0 +1,3030 @@
+/** @file\r
+  This library provides helper functions to prevent integer overflow during\r
+  type conversion, addition, subtraction, and multiplication.\r
+\r
+  Copyright (c) 2017, Microsoft Corporation\r
+\r
+  All rights reserved.\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+#ifndef __INT_SAFE_LIB_H__\r
+#define __INT_SAFE_LIB_H__\r
+\r
+//\r
+// It is common for -1 to be used as an error value\r
+//\r
+#define INT8_ERROR    ((INT8) -1)\r
+#define UINT8_ERROR   MAX_UINT8\r
+#define CHAR8_ERROR   ((CHAR8)(MAX_INT8))\r
+#define INT16_ERROR   ((INT16) -1)\r
+#define UINT16_ERROR  MAX_UINT16\r
+#define CHAR16_ERROR  MAX_UINT16\r
+#define INT32_ERROR   ((INT32) -1)\r
+#define UINT32_ERROR  MAX_UINT32\r
+#define INT64_ERROR   ((INT64) -1)\r
+#define UINT64_ERROR  MAX_UINT64\r
+#define INTN_ERROR    ((INTN) -1)\r
+#define UINTN_ERROR   MAX_UINTN\r
+\r
+//\r
+// CHAR16 is defined to be the same as UINT16, so for CHAR16\r
+// operations redirect to the UINT16 ones:\r
+//\r
+#define SafeInt8ToChar16    SafeInt8ToUint16\r
+#define SafeInt16ToChar16   SafeInt16ToUint16\r
+#define SafeInt32ToChar16   SafeInt32ToUint16\r
+#define SafeUint32ToChar16  SafeUint32ToUint16\r
+#define SafeInt64ToChar16   SafeInt64ToUint16\r
+#define SafeUint64ToChar16  SafeUint64ToUint16\r
+#define SafeIntnToChar16    SafeIntnToUint16\r
+#define SafeUintnToChar16   SafeUintnToUint16\r
+\r
+#define SafeChar16ToInt8    SafeUint16ToInt8\r
+#define SafeChar16ToUint8   SafeUint16ToUint8\r
+#define SafeChar16ToChar8   SafeUint16ToChar8\r
+#define SafeChar16ToInt16   SafeUint16ToInt16\r
+\r
+#define SafeChar16Mult      SafeUint16Mult\r
+#define SafeChar16Sub       SafeUint16Sub\r
+#define SafeChar16Add       SafeUint16Add\r
+\r
+//\r
+// Conversion functions\r
+//\r
+// There are three reasons for having conversion functions:\r
+//\r
+// 1. We are converting from a signed type to an unsigned type of the same\r
+//    size, or vice-versa.\r
+//\r
+// 2. We are converting to a smaller type, and we could therefore possibly\r
+//    overflow.\r
+//\r
+// 3. We are converting to a bigger type, and we are signed and the type we are\r
+//    converting to is unsigned.\r
+//\r
+\r
+/**\r
+  INT8 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint8 (\r
+  IN  INT8   Operand,\r
+  OUT UINT8  *Result\r
+  );\r
+\r
+/**\r
+  INT8 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToChar8 (\r
+  IN  INT8   Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT8 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint16 (\r
+  IN  INT8    Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  INT8 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint32 (\r
+  IN  INT8    Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  INT8 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUintn (\r
+  IN  INT8   Operand,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  INT8 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint64 (\r
+  IN  INT8    Operand,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+/**\r
+  UINT8 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8ToInt8 (\r
+  IN  UINT8  Operand,\r
+  OUT INT8   *Result\r
+  );\r
+\r
+/**\r
+  UINT8 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8ToChar8 (\r
+  IN  UINT8  Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToInt8 (\r
+  IN  INT16  Operand,\r
+  OUT INT8   *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToChar8 (\r
+  IN  INT16  Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint8 (\r
+  IN INT16 Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  INT16 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint16 (\r
+  IN  INT16   Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint32 (\r
+  IN  INT16   Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUintn (\r
+  IN  INT16  Operand,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  INT16 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint64 (\r
+  IN  INT16   Operand,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+/**\r
+  UINT16 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToInt8 (\r
+  IN  UINT16  Operand,\r
+  OUT INT8    *Result\r
+  );\r
+\r
+/**\r
+  UINT16 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToChar8 (\r
+  IN  UINT16  Operand,\r
+  OUT CHAR8   *Result\r
+  );\r
+\r
+/**\r
+  UINT16 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToUint8 (\r
+  IN UINT16 Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  UINT16 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToInt16 (\r
+  IN  UINT16  Operand,\r
+  OUT INT16   *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToInt8 (\r
+  IN  INT32  Operand,\r
+  OUT INT8   *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToChar8 (\r
+  IN  INT32  Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint8 (\r
+  IN INT32 Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  INT32 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToInt16 (\r
+  IN  INT32  Operand,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint16 (\r
+  IN  INT32   Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+\r
+/**\r
+  INT32 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint32 (\r
+  IN  INT32   Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUintn (\r
+  IN  INT32  Operand,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  INT32 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint64 (\r
+  IN  INT32   Operand,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt8 (\r
+  IN  UINT32  Operand,\r
+  OUT INT8    *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToChar8 (\r
+  IN  UINT32  Operand,\r
+  OUT CHAR8   *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToUint8 (\r
+  IN UINT32 Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt16 (\r
+  IN  UINT32  Operand,\r
+  OUT INT16   *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToUint16 (\r
+  IN  UINT32  Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt32 (\r
+  IN  UINT32  Operand,\r
+  OUT INT32   *Result\r
+  );\r
+\r
+/**\r
+  UINT32 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToIntn (\r
+  IN  UINT32  Operand,\r
+  OUT INTN    *Result\r
+  );\r
+\r
+/**\r
+  INTN -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt8 (\r
+  IN  INTN  Operand,\r
+  OUT INT8  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToChar8 (\r
+  IN  INTN   Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint8 (\r
+  IN INTN Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  INTN -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt16 (\r
+  IN  INTN   Operand,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint16 (\r
+  IN  INTN    Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt32 (\r
+  IN  INTN   Operand,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint32 (\r
+  IN  INTN    Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUintn (\r
+  IN  INTN   Operand,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  INTN -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint64 (\r
+  IN  INTN    Operand,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt8 (\r
+  IN  UINTN  Operand,\r
+  OUT INT8   *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToChar8 (\r
+  IN  UINTN  Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint8 (\r
+  IN UINTN Operand,\r
+  OUT UINT8 *pui8Result\r
+  );\r
+\r
+/**\r
+  UINTN -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt16 (\r
+  IN  UINTN  Operand,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint16 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt32 (\r
+  IN  UINTN  Operand,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint32 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToIntn (\r
+  IN  UINTN  Operand,\r
+  OUT INTN   *Result\r
+  );\r
+\r
+/**\r
+  UINTN -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt64 (\r
+  IN  UINTN  Operand,\r
+  OUT INT64  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt8 (\r
+  IN  INT64  Operand,\r
+  OUT INT8   *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToChar8 (\r
+  IN  INT64  Operand,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint8 (\r
+  IN  INT64  Operand,\r
+  OUT UINT8  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt16 (\r
+  IN  INT64  Operand,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint16 (\r
+  IN  INT64   Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt32 (\r
+  IN  INT64  Operand,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint32 (\r
+  IN  INT64   Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToIntn (\r
+  IN  INT64  Operand,\r
+  OUT INTN   *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUintn (\r
+  IN  INT64  Operand,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  INT64 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint64 (\r
+  IN  INT64   Operand,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt8 (\r
+  IN  UINT64  Operand,\r
+  OUT INT8    *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToChar8 (\r
+  IN  UINT64  Operand,\r
+  OUT CHAR8   *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint8 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT8   *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt16 (\r
+  IN  UINT64  Operand,\r
+  OUT INT16   *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint16 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt32 (\r
+  IN  UINT64  Operand,\r
+  OUT INT32   *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint32 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToIntn (\r
+  IN  UINT64  Operand,\r
+  OUT INTN    *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUintn (\r
+  IN  UINT64  Operand,\r
+  OUT UINTN   *Result\r
+  );\r
+\r
+/**\r
+  UINT64 -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt64 (\r
+  IN  UINT64  Operand,\r
+  OUT INT64   *Result\r
+  );\r
+\r
+//\r
+// Addition functions\r
+//\r
+\r
+/**\r
+  UINT8 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Add (\r
+  IN  UINT8  Augend,\r
+  IN  UINT8  Addend,\r
+  OUT UINT8  *Result\r
+  );\r
+\r
+/**\r
+  UINT16 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Add (\r
+  IN  UINT16  Augend,\r
+  IN  UINT16  Addend,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINT32 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Add (\r
+  IN  UINT32  Augend,\r
+  IN  UINT32  Addend,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  UINTN addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnAdd (\r
+  IN  UINTN  Augend,\r
+  IN  UINTN  Addend,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Add (\r
+  IN  UINT64  Augend,\r
+  IN  UINT64  Addend,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+//\r
+// Subtraction functions\r
+//\r
+\r
+/**\r
+  UINT8 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Sub (\r
+  IN  UINT8  Minuend,\r
+  IN  UINT8  Subtrahend,\r
+  OUT UINT8  *Result\r
+  );\r
+\r
+/**\r
+  UINT16 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Sub (\r
+  IN  UINT16  Minuend,\r
+  IN  UINT16  Subtrahend,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINT32 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Sub (\r
+  IN  UINT32  Minuend,\r
+  IN  UINT32  Subtrahend,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  UINTN subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnSub (\r
+  IN  UINTN  Minuend,\r
+  IN  UINTN  Subtrahend,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Sub (\r
+  IN  UINT64  Minuend,\r
+  IN  UINT64  Subtrahend,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+//\r
+// Multiplication functions\r
+//\r
+\r
+/**\r
+  UINT8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Mult (\r
+  IN  UINT8  Multiplicand,\r
+  IN  UINT8  Multiplier,\r
+  OUT UINT8  *Result\r
+  );\r
+\r
+/**\r
+  UINT16 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Mult (\r
+  IN  UINT16  Multiplicand,\r
+  IN  UINT16  Multiplier,\r
+  OUT UINT16  *Result\r
+  );\r
+\r
+/**\r
+  UINT32 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Mult (\r
+  IN  UINT32  Multiplicand,\r
+  IN  UINT32  Multiplier,\r
+  OUT UINT32  *Result\r
+  );\r
+\r
+/**\r
+  UINTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnMult (\r
+  IN  UINTN  Multiplicand,\r
+  IN  UINTN  Multiplier,\r
+  OUT UINTN  *Result\r
+  );\r
+\r
+/**\r
+  UINT64 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Mult (\r
+  IN  UINT64  Multiplicand,\r
+  IN  UINT64  Multiplier,\r
+  OUT UINT64  *Result\r
+  );\r
+\r
+//\r
+// Signed operations\r
+//\r
+// Strongly consider using unsigned numbers.\r
+//\r
+// Signed numbers are often used where unsigned numbers should be used.\r
+// For example file sizes and array indices should always be unsigned.\r
+// Subtracting a larger positive signed number from a smaller positive\r
+// signed number with SafeInt32Sub will succeed, producing a negative number,\r
+// that then must not be used as an array index (but can occasionally be\r
+// used as a pointer index.) Similarly for adding a larger magnitude\r
+// negative number to a smaller magnitude positive number.\r
+//\r
+// This library does not protect you from such errors. It tells you if your\r
+// integer operations overflowed, not if you are doing the right thing\r
+// with your non-overflowed integers.\r
+//\r
+// Likewise you can overflow a buffer with a non-overflowed unsigned index.\r
+//\r
+\r
+//\r
+// Signed addition functions\r
+//\r
+\r
+/**\r
+  INT8 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Add (\r
+  IN  INT8  Augend,\r
+  IN  INT8  Addend,\r
+  OUT INT8  *Result\r
+  );\r
+\r
+/**\r
+  CHAR8 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Add (\r
+  IN  CHAR8  Augend,\r
+  IN  CHAR8  Addend,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT16 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Add (\r
+  IN  INT16  Augend,\r
+  IN  INT16  Addend,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INT32 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Add (\r
+  IN  INT32  Augend,\r
+  IN  INT32  Addend,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  INTN Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnAdd (\r
+  IN  INTN  Augend,\r
+  IN  INTN  Addend,\r
+  OUT INTN  *Result\r
+  );\r
+\r
+/**\r
+  INT64 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Add (\r
+  IN  INT64  Augend,\r
+  IN  INT64  Addend,\r
+  OUT INT64  *Result\r
+  );\r
+\r
+//\r
+// Signed subtraction functions\r
+//\r
+\r
+/**\r
+  INT8 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Sub (\r
+  IN  INT8  Minuend,\r
+  IN  INT8  Subtrahend,\r
+  OUT INT8  *Result\r
+  );\r
+\r
+/**\r
+  CHAR8 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Sub (\r
+  IN  CHAR8  Minuend,\r
+  IN  CHAR8  Subtrahend,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT16 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Sub (\r
+  IN  INT16  Minuend,\r
+  IN  INT16  Subtrahend,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INT32 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Sub (\r
+  IN  INT32  Minuend,\r
+  IN  INT32  Subtrahend,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  INTN Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnSub (\r
+  IN  INTN  Minuend,\r
+  IN  INTN  Subtrahend,\r
+  OUT INTN  *Result\r
+  );\r
+\r
+/**\r
+  INT64 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Sub (\r
+  IN  INT64  Minuend,\r
+  IN  INT64  Subtrahend,\r
+  OUT INT64  *Result\r
+  );\r
+\r
+//\r
+// Signed multiplication functions\r
+//\r
+\r
+/**\r
+  INT8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Mult (\r
+  IN  INT8  Multiplicand,\r
+  IN  INT8  Multiplier,\r
+  OUT INT8  *Result\r
+  );\r
+\r
+/**\r
+  CHAR8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Mult (\r
+  IN  CHAR8  Multiplicand,\r
+  IN  CHAR8  Multiplier,\r
+  OUT CHAR8  *Result\r
+  );\r
+\r
+/**\r
+  INT16 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Mult (\r
+  IN  INT16  Multiplicand,\r
+  IN  INT16  Multiplier,\r
+  OUT INT16  *Result\r
+  );\r
+\r
+/**\r
+  INT32 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Mult (\r
+  IN  INT32  Multiplicand,\r
+  IN  INT32  Multiplier,\r
+  OUT INT32  *Result\r
+  );\r
+\r
+/**\r
+  INTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnMult (\r
+  IN  INTN  Multiplicand,\r
+  IN  INTN  Multiplier,\r
+  OUT INTN  *Result\r
+  );\r
+\r
+/**\r
+  INT64 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Mult (\r
+  IN  INT64  Multiplicand,\r
+  IN  INT64  Multiplier,\r
+  OUT INT64  *Result\r
+  );\r
+\r
+#endif // __INT_SAFE_LIB_H__\r
index e637d8649f57a4d6987cd68cc43d5bb0377c54b3..38ef2665390fa2690d6f353bd4dc4a57c09e6e19 100644 (file)
@@ -266,6 +266,11 @@ typedef INT64   INTN;
 #define MAX_INTN   ((INTN)0x7FFFFFFFFFFFFFFFULL)\r
 #define MAX_UINTN  ((UINTN)0xFFFFFFFFFFFFFFFFULL)\r
 \r
+///\r
+/// Minimum legal x64 INTN value.\r
+///\r
+#define MIN_INTN   (((INTN)-9223372036854775807LL) - 1)\r
+\r
 ///\r
 /// The stack alignment required for x64\r
 ///\r
diff --git a/MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf b/MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
new file mode 100644 (file)
index 0000000..20a83ed
--- /dev/null
@@ -0,0 +1,58 @@
+## @file\r
+# Safe Integer Library\r
+#\r
+# This library provides helper functions to prevent integer overflow during\r
+# type conversion, addition, subtraction, and multiplication.\r
+#\r
+# Copyright (c) 2017, Microsoft Corporation\r
+#\r
+# All rights reserved.\r
+# Redistribution and use in source and binary forms, with or without\r
+# modification, are permitted provided that the following conditions are met:\r
+# 1. Redistributions of source code must retain the above copyright notice,\r
+# this list of conditions and the following disclaimer.\r
+# 2. Redistributions in binary form must reproduce the above copyright notice,\r
+# this list of conditions and the following disclaimer in the documentation\r
+#  and/or other materials provided with the distribution.\r
+#\r
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = BaseSafeIntLib\r
+  FILE_GUID                      = 4EA91BFA-3482-4930-B136-70679C6CE489\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = SafeIntLib\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  SafeIntLib.c\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLib32.c\r
+\r
+[Sources.X64, Sources.IPF, Sources.AARCH64]\r
+  SafeIntLib64.c\r
+\r
+[Sources.EBC]\r
+  SafeIntLibEbc.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
diff --git a/MdePkg/Library/BaseSafeIntLib/SafeIntLib.c b/MdePkg/Library/BaseSafeIntLib/SafeIntLib.c
new file mode 100644 (file)
index 0000000..d846160
--- /dev/null
@@ -0,0 +1,4098 @@
+/** @file\r
+  This library provides helper functions to prevent integer overflow during\r
+  type conversion, addition, subtraction, and multiplication.\r
+\r
+  Copyright (c) 2017, Microsoft Corporation\r
+\r
+  All rights reserved.\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/SafeIntLib.h>\r
+\r
+\r
+//\r
+// Magnitude of MIN_INT64 as expressed by a UINT64 number.\r
+//\r
+#define MIN_INT64_MAGNITUDE ((((UINT64) - (MIN_INT64 + 1))) + 1)\r
+\r
+//\r
+// Conversion functions\r
+//\r
+// There are three reasons for having conversion functions:\r
+//\r
+// 1. We are converting from a signed type to an unsigned type of the same\r
+//    size, or vice-versa.\r
+//\r
+// 2. We are converting to a smaller type, and we could therefore possibly\r
+//    overflow.\r
+//\r
+// 3. We are converting to a bigger type, and we are signed and the type we are\r
+//    converting to is unsigned.\r
+//\r
+\r
+/**\r
+  INT8 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint8 (\r
+  IN  INT8   Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT8 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToChar8 (\r
+  IN  INT8   Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT8 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint16 (\r
+  IN  INT8    Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT8 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint32 (\r
+  IN  INT8    Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT8 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUintn (\r
+  IN  INT8   Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINTN)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT8 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8ToUint64 (\r
+  IN  INT8    Operand,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT8 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8ToInt8 (\r
+  IN  UINT8  Operand,\r
+  OUT INT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT8 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8ToChar8 (\r
+  IN  UINT8  Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToInt8 (\r
+  IN  INT16  Operand,\r
+  OUT INT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToChar8 (\r
+  IN  INT16  Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint8 (\r
+  IN  INT16  Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint16 (\r
+  IN  INT16   Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint32 (\r
+  IN  INT16   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUintn (\r
+  IN  INT16  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINTN)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT16 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16ToUint64 (\r
+  IN  INT16   Operand,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToInt8 (\r
+  IN  UINT16  Operand,\r
+  OUT INT8    *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToChar8 (\r
+  IN  UINT16  Operand,\r
+  OUT CHAR8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToUint8 (\r
+  IN  UINT16  Operand,\r
+  OUT UINT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT8) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16ToInt16 (\r
+  IN  UINT16  Operand,\r
+  OUT INT16   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT16) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToInt8 (\r
+  IN  INT32  Operand,\r
+  OUT INT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToChar8 (\r
+  IN  INT32  Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint8 (\r
+  IN  INT32  Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToInt16 (\r
+  IN  INT32  Operand,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint16 (\r
+  IN  INT32   Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint32 (\r
+  IN  INT32   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT32 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUint64 (\r
+  IN  INT32   Operand,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt8 (\r
+  IN  UINT32  Operand,\r
+  OUT INT8    *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToChar8 (\r
+  IN  UINT32  Operand,\r
+  OUT CHAR8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToUint8 (\r
+  IN  UINT32  Operand,\r
+  OUT UINT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT8) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt16 (\r
+  IN  UINT32  Operand,\r
+  OUT INT16   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT16) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToUint16 (\r
+  IN  UINT32  Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT16) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToInt32 (\r
+  IN  UINT32  Operand,\r
+  OUT INT32   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT32) {\r
+    *Result = (INT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt8 (\r
+  IN  INTN  Operand,\r
+  OUT INT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToChar8 (\r
+  IN  INTN   Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint8 (\r
+  IN  INTN   Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt16 (\r
+  IN  INTN   Operand,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint16 (\r
+  IN  INTN    Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUintn (\r
+  IN  INTN   Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINTN)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INTN -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint64 (\r
+  IN  INTN    Operand,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt8 (\r
+  IN  UINTN  Operand,\r
+  OUT INT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToChar8 (\r
+  IN  UINTN  Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint8 (\r
+  IN  UINTN  Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT8) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt16 (\r
+  IN  UINTN  Operand,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT16) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint16 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT16) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt32 (\r
+  IN  UINTN  Operand,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT32) {\r
+    *Result = (INT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToIntn (\r
+  IN  UINTN  Operand,\r
+  OUT INTN   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INTN) {\r
+    *Result = (INTN)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt8 (\r
+  IN  INT64  Operand,\r
+  OUT INT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToChar8 (\r
+  IN  INT64  Operand,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
+    *Result = (CHAR8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint8 (\r
+  IN  INT64  Operand,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt16 (\r
+  IN  INT64  Operand,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint16 (\r
+  IN  INT64   Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToInt32 (\r
+  IN  INT64  Operand,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= MIN_INT32) && (Operand <= MAX_INT32)) {\r
+    *Result = (INT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint32 (\r
+  IN  INT64   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Operand >= 0) && (Operand <= MAX_UINT32)) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  INT64 -> UINT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUint64 (\r
+  IN  INT64   Operand,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> INT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt8 (\r
+  IN  UINT64  Operand,\r
+  OUT INT8    *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> CHAR8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToChar8 (\r
+  IN  UINT64  Operand,\r
+  OUT CHAR8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT8) {\r
+    *Result = (INT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = CHAR8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> UINT8 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint8 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT8   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT8) {\r
+    *Result = (UINT8)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> INT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt16 (\r
+  IN  UINT64  Operand,\r
+  OUT INT16   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT16) {\r
+    *Result = (INT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> UINT16 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint16 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT16) {\r
+    *Result = (UINT16)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt32 (\r
+  IN  UINT64  Operand,\r
+  OUT INT32   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT32) {\r
+    *Result = (INT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUint32 (\r
+  IN  UINT64  Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_UINT32) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToIntn (\r
+  IN  UINT64  Operand,\r
+  OUT INTN    *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INTN) {\r
+    *Result = (INTN)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToInt64 (\r
+  IN  UINT64  Operand,\r
+  OUT INT64   *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand <= MAX_INT64) {\r
+    *Result = (INT64)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = INT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+//\r
+// Addition functions\r
+//\r
+\r
+/**\r
+  UINT8 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Add (\r
+  IN  UINT8  Augend,\r
+  IN  UINT8  Addend,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (((UINT8)(Augend + Addend)) >= Augend) {\r
+    *Result = (UINT8)(Augend + Addend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Add (\r
+  IN  UINT16  Augend,\r
+  IN  UINT16  Addend,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (((UINT16)(Augend + Addend)) >= Augend) {\r
+    *Result = (UINT16)(Augend + Addend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Add (\r
+  IN  UINT32  Augend,\r
+  IN  UINT32  Addend,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Augend + Addend) >= Augend) {\r
+    *Result = (Augend + Addend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Add (\r
+  IN  UINT64  Augend,\r
+  IN  UINT64  Addend,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Augend + Addend) >= Augend) {\r
+    *Result = (Augend + Addend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+//\r
+// Subtraction functions\r
+//\r
+\r
+/**\r
+  UINT8 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Sub (\r
+  IN  UINT8  Minuend,\r
+  IN  UINT8  Subtrahend,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Minuend >= Subtrahend) {\r
+    *Result = (UINT8)(Minuend - Subtrahend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT8_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT16 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Sub (\r
+  IN  UINT16  Minuend,\r
+  IN  UINT16  Subtrahend,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Minuend >= Subtrahend) {\r
+    *Result = (UINT16)(Minuend - Subtrahend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT16_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT32 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Sub (\r
+  IN  UINT32  Minuend,\r
+  IN  UINT32  Subtrahend,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Minuend >= Subtrahend) {\r
+    *Result = (Minuend - Subtrahend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINT64 subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Sub (\r
+  IN  UINT64  Minuend,\r
+  IN  UINT64  Subtrahend,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Minuend >= Subtrahend) {\r
+    *Result = (Minuend - Subtrahend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+//\r
+// Multiplication functions\r
+//\r
+\r
+/**\r
+  UINT8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint8Mult (\r
+  IN  UINT8  Multiplicand,\r
+  IN  UINT8  Multiplier,\r
+  OUT UINT8  *Result\r
+  )\r
+{\r
+  UINT32  IntermediateResult;\r
+\r
+  IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
+\r
+  return SafeUint32ToUint8 (IntermediateResult, Result);\r
+}\r
+\r
+/**\r
+  UINT16 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint16Mult (\r
+  IN  UINT16  Multiplicand,\r
+  IN  UINT16  Multiplier,\r
+  OUT UINT16  *Result\r
+  )\r
+{\r
+  UINT32  IntermediateResult;\r
+\r
+  IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
+\r
+  return SafeUint32ToUint16 (IntermediateResult, Result);\r
+}\r
+\r
+/**\r
+  UINT32 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32Mult (\r
+  IN  UINT32  Multiplicand,\r
+  IN  UINT32  Multiplier,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  UINT64  IntermediateResult;\r
+\r
+  IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);\r
+\r
+  return SafeUint64ToUint32 (IntermediateResult, Result);\r
+}\r
+\r
+/**\r
+  UINT64 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64Mult (\r
+  IN  UINT64  Multiplicand,\r
+  IN  UINT64  Multiplier,\r
+  OUT UINT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINT32         DwordA;\r
+  UINT32         DwordB;\r
+  UINT32         DwordC;\r
+  UINT32         DwordD;\r
+  UINT64         ProductAD;\r
+  UINT64         ProductBC;\r
+  UINT64         ProductBD;\r
+  UINT64         UnsignedResult;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  ProductAD = 0;\r
+  ProductBC = 0;\r
+  ProductBD = 0;\r
+  UnsignedResult = 0;\r
+  Status = RETURN_BUFFER_TOO_SMALL;\r
+\r
+  //\r
+  // 64x64 into 128 is like 32.32 x 32.32.\r
+  //\r
+  // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d\r
+  // back in non-decimal notation where A=a*2^32 and C=c*2^32:\r
+  // A*C + A*d + b*C + b*d\r
+  // So there are four components to add together.\r
+  //   result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)\r
+  //\r
+  // a * c must be 0 or there would be bits in the high 64-bits\r
+  // a * d must be less than 2^32 or there would be bits in the high 64-bits\r
+  // b * c must be less than 2^32 or there would be bits in the high 64-bits\r
+  // then there must be no overflow of the resulting values summed up.\r
+  //\r
+  DwordA = (UINT32)(Multiplicand >> 32);\r
+  DwordC = (UINT32)(Multiplier >> 32);\r
+\r
+  //\r
+  // common case -- if high dwords are both zero, no chance for overflow\r
+  //\r
+  if ((DwordA == 0) && (DwordC == 0)) {\r
+    DwordB = (UINT32)Multiplicand;\r
+    DwordD = (UINT32)Multiplier;\r
+\r
+    *Result = (((UINT64)DwordB) *(UINT64)DwordD);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    //\r
+    // a * c must be 0 or there would be bits set in the high 64-bits\r
+    //\r
+    if ((DwordA == 0) ||\r
+        (DwordC == 0)) {\r
+      DwordD = (UINT32)Multiplier;\r
+\r
+      //\r
+      // a * d must be less than 2^32 or there would be bits set in the high 64-bits\r
+      //\r
+      ProductAD = (((UINT64)DwordA) *(UINT64)DwordD);\r
+      if ((ProductAD & 0xffffffff00000000) == 0) {\r
+        DwordB = (UINT32)Multiplicand;\r
+\r
+        //\r
+        // b * c must be less than 2^32 or there would be bits set in the high 64-bits\r
+        //\r
+        ProductBC = (((UINT64)DwordB) *(UINT64)DwordC);\r
+        if ((ProductBC & 0xffffffff00000000) == 0) {\r
+          //\r
+          // now sum them all up checking for overflow.\r
+          // shifting is safe because we already checked for overflow above\r
+          //\r
+          if (!RETURN_ERROR (SafeUint64Add (ProductBC << 32, ProductAD << 32, &UnsignedResult))) {\r
+            //\r
+            // b * d\r
+            //\r
+            ProductBD = (((UINT64)DwordB) *(UINT64)DwordD);\r
+\r
+            if (!RETURN_ERROR (SafeUint64Add (UnsignedResult, ProductBD, &UnsignedResult))) {\r
+              *Result = UnsignedResult;\r
+              Status = RETURN_SUCCESS;\r
+            }\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  if (RETURN_ERROR (Status)) {\r
+    *Result = UINT64_ERROR;\r
+  }\r
+  return Status;\r
+}\r
+\r
+//\r
+// Signed operations\r
+//\r
+// Strongly consider using unsigned numbers.\r
+//\r
+// Signed numbers are often used where unsigned numbers should be used.\r
+// For example file sizes and array indices should always be unsigned.\r
+// Subtracting a larger positive signed number from a smaller positive\r
+// signed number with SafeInt32Sub will succeed, producing a negative number,\r
+// that then must not be used as an array index (but can occasionally be\r
+// used as a pointer index.) Similarly for adding a larger magnitude\r
+// negative number to a smaller magnitude positive number.\r
+//\r
+// This library does not protect you from such errors. It tells you if your\r
+// integer operations overflowed, not if you are doing the right thing\r
+// with your non-overflowed integers.\r
+//\r
+// Likewise you can overflow a buffer with a non-overflowed unsigned index.\r
+//\r
+\r
+//\r
+// Signed addition functions\r
+//\r
+\r
+/**\r
+  INT8 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Add (\r
+  IN  INT8  Augend,\r
+  IN  INT8  Addend,\r
+  OUT INT8  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt8 (((INT32)Augend) + ((INT32)Addend), Result);\r
+}\r
+\r
+/**\r
+  CHAR8 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Add (\r
+  IN  CHAR8  Augend,\r
+  IN  CHAR8  Addend,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  INT32  Augend32;\r
+  INT32  Addend32;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  Augend32 = (INT32)Augend;\r
+  Addend32 = (INT32)Addend;\r
+  if (Augend32 < 0 || Augend32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+  if (Addend32 < 0 || Addend32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return SafeInt32ToChar8 (Augend32 + Addend32, Result);\r
+}\r
+\r
+/**\r
+  INT16 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Add (\r
+  IN  INT16  Augend,\r
+  IN  INT16  Addend,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt16 (((INT32)Augend) + ((INT32)Addend), Result);\r
+}\r
+\r
+/**\r
+  INT32 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Add (\r
+  IN  INT32  Augend,\r
+  IN  INT32  Addend,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  return SafeInt64ToInt32 (((INT64)Augend) + ((INT64)Addend), Result);\r
+}\r
+\r
+/**\r
+  INT64 Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Add (\r
+  IN  INT64  Augend,\r
+  IN  INT64  Addend,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  INT64          SignedResult;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  SignedResult = Augend + Addend;\r
+\r
+  //\r
+  // Adding positive to negative never overflows.\r
+  // If you add two positive numbers, you expect a positive result.\r
+  // If you add two negative numbers, you expect a negative result.\r
+  // Overflow if inputs are the same sign and output is not that sign.\r
+  //\r
+  if (((Augend < 0) == (Addend < 0))  &&\r
+      ((Augend < 0) != (SignedResult < 0))) {\r
+    *Result = INT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  } else {\r
+    *Result = SignedResult;\r
+    Status = RETURN_SUCCESS;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+//\r
+// Signed subtraction functions\r
+//\r
+\r
+/**\r
+  INT8 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Sub (\r
+  IN  INT8  Minuend,\r
+  IN  INT8  Subtrahend,\r
+  OUT INT8  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt8 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
+}\r
+\r
+/**\r
+  CHAR8 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Sub (\r
+  IN  CHAR8  Minuend,\r
+  IN  CHAR8  Subtrahend,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  INT32  Minuend32;\r
+  INT32  Subtrahend32;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  Minuend32    = (INT32)Minuend;\r
+  Subtrahend32 = (INT32)Subtrahend;\r
+  if (Minuend32 < 0 || Minuend32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+  if (Subtrahend32 < 0 || Subtrahend32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return SafeInt32ToChar8 (Minuend32 - Subtrahend32, Result);\r
+}\r
+\r
+/**\r
+  INT16 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Sub (\r
+  IN  INT16  Minuend,\r
+  IN  INT16  Subtrahend,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt16 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
+}\r
+\r
+/**\r
+  INT32 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Sub (\r
+  IN  INT32  Minuend,\r
+  IN  INT32  Subtrahend,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  return SafeInt64ToInt32 (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r
+}\r
+\r
+/**\r
+  INT64 Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Sub (\r
+  IN  INT64  Minuend,\r
+  IN  INT64  Subtrahend,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  INT64          SignedResult;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  SignedResult = Minuend - Subtrahend;\r
+\r
+  //\r
+  // Subtracting a positive number from a positive number never overflows.\r
+  // Subtracting a negative number from a negative number never overflows.\r
+  // If you subtract a negative number from a positive number, you expect a positive result.\r
+  // If you subtract a positive number from a negative number, you expect a negative result.\r
+  // Overflow if inputs vary in sign and the output does not have the same sign as the first input.\r
+  //\r
+  if (((Minuend < 0) != (Subtrahend < 0)) &&\r
+      ((Minuend < 0) != (SignedResult < 0))) {\r
+    *Result = INT64_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  } else {\r
+    *Result = SignedResult;\r
+    Status = RETURN_SUCCESS;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+//\r
+// Signed multiplication functions\r
+//\r
+\r
+/**\r
+  INT8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt8Mult (\r
+  IN  INT8  Multiplicand,\r
+  IN  INT8  Multiplier,\r
+  OUT INT8  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt8 (((INT32)Multiplier) *((INT32)Multiplicand), Result);\r
+}\r
+\r
+/**\r
+  CHAR8 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeChar8Mult (\r
+  IN  CHAR8  Multiplicand,\r
+  IN  CHAR8  Multiplier,\r
+  OUT CHAR8  *Result\r
+  )\r
+{\r
+  INT32  Multiplicand32;\r
+  INT32  Multiplier32;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  Multiplicand32 = (INT32)Multiplicand;\r
+  Multiplier32   = (INT32)Multiplier;\r
+  if (Multiplicand32 < 0 || Multiplicand32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+  if (Multiplier32 < 0 || Multiplier32 > MAX_INT8) {\r
+    *Result = CHAR8_ERROR;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return SafeInt32ToChar8 (Multiplicand32 * Multiplier32, Result);\r
+}\r
+\r
+/**\r
+  INT16 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt16Mult (\r
+  IN  INT16  Multiplicand,\r
+  IN  INT16  Multiplier,\r
+  OUT INT16  *Result\r
+  )\r
+{\r
+  return SafeInt32ToInt16 (((INT32)Multiplicand) *((INT32)Multiplier), Result);\r
+}\r
+\r
+/**\r
+  INT32 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32Mult (\r
+  IN  INT32  Multiplicand,\r
+  IN  INT32  Multiplier,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  return SafeInt64ToInt32 (((INT64)Multiplicand) *((INT64)Multiplier), Result);\r
+}\r
+\r
+/**\r
+  INT64 multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64Mult (\r
+  IN  INT64  Multiplicand,\r
+  IN  INT64  Multiplier,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINT64         UnsignedMultiplicand;\r
+  UINT64         UnsignedMultiplier;\r
+  UINT64         UnsignedResult;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Split into sign and magnitude, do unsigned operation, apply sign.\r
+  //\r
+  if (Multiplicand < 0) {\r
+    //\r
+    // Avoid negating the most negative number.\r
+    //\r
+    UnsignedMultiplicand = ((UINT64)(- (Multiplicand + 1))) + 1;\r
+  } else {\r
+    UnsignedMultiplicand = (UINT64)Multiplicand;\r
+  }\r
+\r
+  if (Multiplier < 0) {\r
+    //\r
+    // Avoid negating the most negative number.\r
+    //\r
+    UnsignedMultiplier = ((UINT64)(- (Multiplier + 1))) + 1;\r
+  } else {\r
+    UnsignedMultiplier = (UINT64)Multiplier;\r
+  }\r
+\r
+  Status = SafeUint64Mult (UnsignedMultiplicand, UnsignedMultiplier, &UnsignedResult);\r
+  if (!RETURN_ERROR (Status)) {\r
+    if ((Multiplicand < 0) != (Multiplier < 0)) {\r
+      if (UnsignedResult > MIN_INT64_MAGNITUDE) {\r
+        *Result = INT64_ERROR;\r
+        Status = RETURN_BUFFER_TOO_SMALL;\r
+      } else {\r
+        *Result = - ((INT64)UnsignedResult);\r
+      }\r
+    } else {\r
+      if (UnsignedResult > MAX_INT64) {\r
+        *Result = INT64_ERROR;\r
+        Status = RETURN_BUFFER_TOO_SMALL;\r
+      } else {\r
+        *Result = (INT64)UnsignedResult;\r
+      }\r
+    }\r
+  } else {\r
+    *Result = INT64_ERROR;\r
+  }\r
+  return Status;\r
+}\r
+\r
diff --git a/MdePkg/Library/BaseSafeIntLib/SafeIntLib32.c b/MdePkg/Library/BaseSafeIntLib/SafeIntLib32.c
new file mode 100644 (file)
index 0000000..18bfb9e
--- /dev/null
@@ -0,0 +1,554 @@
+/** @file\r
+  This library provides helper functions to prevent integer overflow during\r
+  type conversion, addition, subtraction, and multiplication.\r
+\r
+  Copyright (c) 2017, Microsoft Corporation\r
+\r
+  All rights reserved.\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/SafeIntLib.h>\r
+\r
+/**\r
+  INT32 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUintn (\r
+  IN  INT32  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeInt32ToUint32 (Operand, (UINT32 *)Result);\r
+}\r
+\r
+/**\r
+  UINT32 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToIntn (\r
+  IN  UINT32  Operand,\r
+  OUT INTN    *Result\r
+  )\r
+{\r
+  return SafeUint32ToInt32 (Operand, (INT32 *)Result);\r
+}\r
+\r
+/**\r
+  INTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt32 (\r
+  IN  INTN   Operand,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = (INT32)Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint32 (\r
+  IN  INTN    Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operand >= 0) {\r
+    *Result = (UINT32)Operand;\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINT32_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint32 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = (UINT32)Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  UINTN -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt64 (\r
+  IN  UINTN  Operand,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = (INT64)Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToIntn (\r
+  IN  INT64  Operand,\r
+  OUT INTN   *Result\r
+  )\r
+{\r
+  return SafeInt64ToInt32 (Operand, (INT32 *)Result);\r
+}\r
+\r
+/**\r
+  INT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUintn (\r
+  IN  INT64  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeInt64ToUint32 (Operand, (UINT32 *)Result);\r
+}\r
+\r
+/**\r
+  UINT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUintn (\r
+  IN  UINT64  Operand,\r
+  OUT UINTN   *Result\r
+  )\r
+{\r
+  return SafeUint64ToUint32 ((UINT64) Operand, (UINT32 *)Result);\r
+}\r
+\r
+/**\r
+  UINTN addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnAdd (\r
+  IN  UINTN  Augend,\r
+  IN  UINTN  Addend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((Augend + Addend) >= Augend) {\r
+    *Result = (Augend + Addend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnSub (\r
+  IN  UINTN  Minuend,\r
+  IN  UINTN  Subtrahend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Minuend >= Subtrahend) {\r
+    *Result = (Minuend - Subtrahend);\r
+    Status = RETURN_SUCCESS;\r
+  } else {\r
+    *Result = UINTN_ERROR;\r
+    Status = RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  UINTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnMult (\r
+  IN  UINTN  Multiplicand,\r
+  IN  UINTN  Multiplier,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  UINT64  IntermediateResult;\r
+\r
+  IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);\r
+\r
+  return SafeUint64ToUintn (IntermediateResult, Result);\r
+}\r
+\r
+/**\r
+  INTN Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnAdd (\r
+  IN  INTN  Augend,\r
+  IN  INTN  Addend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64ToIntn (((INT64)Augend) + ((INT64)Addend), Result);\r
+}\r
+\r
+/**\r
+  INTN Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnSub (\r
+  IN  INTN  Minuend,\r
+  IN  INTN  Subtrahend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64ToIntn (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r
+}\r
+\r
+/**\r
+  INTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnMult (\r
+  IN  INTN  Multiplicand,\r
+  IN  INTN  Multiplier,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64ToIntn (((INT64)Multiplicand) *((INT64)Multiplier), Result);\r
+}\r
+\r
diff --git a/MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c b/MdePkg/Library/BaseSafeIntLib/SafeIntLib64.c
new file mode 100644 (file)
index 0000000..b423c5c
--- /dev/null
@@ -0,0 +1,508 @@
+/** @file\r
+  This library provides helper functions to prevent integer overflow during\r
+  type conversion, addition, subtraction, and multiplication.\r
+\r
+  Copyright (c) 2017, Microsoft Corporation\r
+\r
+  All rights reserved.\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/SafeIntLib.h>\r
+\r
+/**\r
+  INT32 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUintn (\r
+  IN  INT32  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeInt32ToUint64 (Operand, (UINT64 *) Result);\r
+}\r
+\r
+/**\r
+  UINT32 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToIntn (\r
+  IN  UINT32  Operand,\r
+  OUT INTN    *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt32 (\r
+  IN  INTN   Operand,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  return SafeInt64ToInt32 ((INT64) Operand, Result);\r
+}\r
+\r
+/**\r
+  INTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint32 (\r
+  IN  INTN    Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  return SafeInt64ToUint32 ((INT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  UINTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint32 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  return SafeUint64ToUint32 ((UINT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  UINTN -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt64 (\r
+  IN  UINTN  Operand,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  return SafeUint64ToInt64 ((UINT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  INT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToIntn (\r
+  IN  INT64  Operand,\r
+  OUT INTN   *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = (INTN)Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUintn (\r
+  IN  INT64  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeInt64ToUint64 (Operand, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUintn (\r
+  IN  UINT64  Operand,\r
+  OUT UINTN   *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Result = Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  UINTN addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnAdd (\r
+  IN  UINTN  Augend,\r
+  IN  UINTN  Addend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeUint64Add ((UINT64)Augend, (UINT64)Addend, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINTN subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnSub (\r
+  IN  UINTN  Minuend,\r
+  IN  UINTN  Subtrahend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeUint64Sub ((UINT64)Minuend, (UINT64)Subtrahend, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnMult (\r
+  IN  UINTN  Multiplicand,\r
+  IN  UINTN  Multiplier,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  return SafeUint64Mult ((UINT64)Multiplicand, (UINT64)Multiplier, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnAdd (\r
+  IN  INTN  Augend,\r
+  IN  INTN  Addend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64Add ((INT64)Augend, (INT64)Addend, (INT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnSub (\r
+  IN  INTN  Minuend,\r
+  IN  INTN  Subtrahend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64Sub ((INT64)Minuend, (INT64)Subtrahend, (INT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnMult (\r
+  IN  INTN  Multiplicand,\r
+  IN  INTN  Multiplier,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  return SafeInt64Mult ((INT64)Multiplicand, (INT64)Multiplier, (INT64 *)Result);\r
+}\r
+\r
diff --git a/MdePkg/Library/BaseSafeIntLib/SafeIntLibEbc.c b/MdePkg/Library/BaseSafeIntLib/SafeIntLibEbc.c
new file mode 100644 (file)
index 0000000..4478957
--- /dev/null
@@ -0,0 +1,614 @@
+/** @file\r
+  This library provides helper functions to prevent integer overflow during\r
+  type conversion, addition, subtraction, and multiplication.\r
+\r
+  Copyright (c) 2017, Microsoft Corporation\r
+\r
+  All rights reserved.\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/SafeIntLib.h>\r
+\r
+/**\r
+  INT32 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt32ToUintn (\r
+  IN  INT32  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt32ToUint32 (Operand, (UINT32 *)Result);\r
+  }\r
+  return SafeInt32ToUint64 (Operand, (UINT64 *) Result);\r
+}\r
+\r
+/**\r
+  UINT32 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint32ToIntn (\r
+  IN  UINT32  Operand,\r
+  OUT INTN    *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeUint32ToInt32 (Operand, (INT32 *)Result);\r
+  }\r
+  *Result = Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INTN -> INT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToInt32 (\r
+  IN  INTN   Operand,\r
+  OUT INT32  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    *Result = (INT32)Operand;\r
+    return RETURN_SUCCESS;\r
+  }\r
+  return SafeInt64ToInt32 ((INT64) Operand, Result);\r
+}\r
+\r
+/**\r
+  INTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnToUint32 (\r
+  IN  INTN    Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    if (Operand >= 0) {\r
+      *Result = (UINT32)Operand;\r
+      Status = RETURN_SUCCESS;\r
+    } else {\r
+      *Result = UINT32_ERROR;\r
+      Status = RETURN_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    return Status;\r
+  }\r
+  return SafeInt64ToUint32 ((INT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  UINTN -> UINT32 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToUint32 (\r
+  IN  UINTN   Operand,\r
+  OUT UINT32  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    *Result = (UINT32)Operand;\r
+    return RETURN_SUCCESS;\r
+  }\r
+  return SafeUint64ToUint32 ((UINT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  UINTN -> INT64 conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnToInt64 (\r
+  IN  UINTN  Operand,\r
+  OUT INT64  *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    *Result = (INT64)Operand;\r
+    return RETURN_SUCCESS;\r
+  }\r
+  return SafeUint64ToInt64 ((UINT64)Operand, Result);\r
+}\r
+\r
+/**\r
+  INT64 -> INTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToIntn (\r
+  IN  INT64  Operand,\r
+  OUT INTN   *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt64ToInt32 (Operand, (INT32 *)Result);\r
+  }\r
+  *Result = (INTN)Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  INT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeInt64ToUintn (\r
+  IN  INT64  Operand,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt64ToUint32 (Operand, (UINT32 *)Result);\r
+  }\r
+  return SafeInt64ToUint64 (Operand, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINT64 -> UINTN conversion\r
+\r
+  Converts the value specified by Operand to a value specified by Result type\r
+  and stores the converted value into the caller allocated output buffer\r
+  specified by Result.  The caller must pass in a Result buffer that is at\r
+  least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the conversion results in an overflow or an underflow condition, then\r
+  Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Operand  Operand to be converted to new type\r
+  @param[out]  Result   Pointer to the result of conversion\r
+\r
+  @retval  RETURN_SUCCESS            Successful conversion\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUint64ToUintn (\r
+  IN  UINT64  Operand,\r
+  OUT UINTN   *Result\r
+  )\r
+{\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeUint64ToUint32 ((UINT64) Operand, (UINT32 *)Result);\r
+  }\r
+  *Result = Operand;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  UINTN addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnAdd (\r
+  IN  UINTN  Augend,\r
+  IN  UINTN  Addend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    if ((UINT32)(Augend + Addend) >= Augend) {\r
+      *Result = (Augend + Addend);\r
+      Status = RETURN_SUCCESS;\r
+    } else {\r
+      *Result = UINTN_ERROR;\r
+      Status = RETURN_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    return Status;\r
+  }\r
+  return SafeUint64Add ((UINT64)Augend, (UINT64)Addend, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINTN subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnSub (\r
+  IN  UINTN  Minuend,\r
+  IN  UINTN  Subtrahend,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+\r
+  if (Result == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    if (Minuend >= Subtrahend) {\r
+      *Result = (Minuend - Subtrahend);\r
+      Status = RETURN_SUCCESS;\r
+    } else {\r
+      *Result = UINTN_ERROR;\r
+      Status = RETURN_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    return Status;\r
+  }\r
+  return SafeUint64Sub ((UINT64)Minuend, (UINT64)Subtrahend, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  UINTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeUintnMult (\r
+  IN  UINTN  Multiplicand,\r
+  IN  UINTN  Multiplier,\r
+  OUT UINTN  *Result\r
+  )\r
+{\r
+  UINT64  IntermediateResult;\r
+\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);\r
+\r
+    return SafeUint64ToUintn (IntermediateResult, Result);\r
+  }\r
+  return SafeUint64Mult ((UINT64)Multiplicand, (UINT64)Multiplier, (UINT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN Addition\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Augend  A number to which addend will be added\r
+  @param[in]   Addend  A number to be added to another\r
+  @param[out]  Result  Pointer to the result of addition\r
+\r
+  @retval  RETURN_SUCCESS            Successful addition\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnAdd (\r
+  IN  INTN  Augend,\r
+  IN  INTN  Addend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt64ToIntn (((INT64)Augend) + ((INT64)Addend), Result);\r
+  }\r
+  return SafeInt64Add ((INT64)Augend, (INT64)Addend, (INT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN Subtraction\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Minuend     A number from which another is to be subtracted.\r
+  @param[in]   Subtrahend  A number to be subtracted from another\r
+  @param[out]  Result      Pointer to the result of subtraction\r
+\r
+  @retval  RETURN_SUCCESS            Successful subtraction\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Underflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnSub (\r
+  IN  INTN  Minuend,\r
+  IN  INTN  Subtrahend,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt64ToIntn (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r
+  }\r
+  return SafeInt64Sub ((INT64)Minuend, (INT64)Subtrahend, (INT64 *)Result);\r
+}\r
+\r
+/**\r
+  INTN multiplication\r
+\r
+  Performs the requested operation using the input parameters into a value\r
+  specified by Result type and stores the converted value into the caller\r
+  allocated output buffer specified by Result.  The caller must pass in a\r
+  Result buffer that is at least as large as the Result type.\r
+\r
+  If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
+\r
+  If the requested operation results in an overflow or an underflow condition,\r
+  then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
+\r
+  @param[in]   Multiplicand  A number that is to be multiplied by another\r
+  @param[in]   Multiplier    A number by which the multiplicand is to be multiplied\r
+  @param[out]  Result        Pointer to the result of multiplication\r
+\r
+  @retval  RETURN_SUCCESS            Successful multiplication\r
+  @retval  RETURN_BUFFER_TOO_SMALL   Overflow\r
+  @retval  RETURN_INVALID_PARAMETER  Result is NULL\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SafeIntnMult (\r
+  IN  INTN  Multiplicand,\r
+  IN  INTN  Multiplier,\r
+  OUT INTN  *Result\r
+  )\r
+{\r
+  if (sizeof (UINTN) == sizeof (UINT32)) {\r
+    return SafeInt64ToIntn (((INT64)Multiplicand) *((INT64)Multiplier), Result);\r
+  }\r
+  return SafeInt64Mult ((INT64)Multiplicand, (INT64)Multiplier, (INT64 *)Result);\r
+}\r
+\r
index 58992fa689f1fad9535a5c7d63d86115882f1229..0e64f22f4a747f3c1b529d33492b182852d4a143 100644 (file)
   ##  @libraryclass  provides EFI_FILE_HANDLE services\r
   FileHandleLib|Include/Library/FileHandleLib.h\r
 \r
+  ## @libraryclass provides helper functions to prevent integer overflow during\r
+  #                type conversion, addition, subtraction, and multiplication.\r
+  ##\r
+  SafeIntLib|Include/Library/SafeIntLib.h\r
+\r
 [LibraryClasses.IA32, LibraryClasses.X64]\r
   ##  @libraryclass  Abstracts both S/W SMI generation and detection.\r
   ##\r
index ea69f477c508c78fecc38499261a704734375b8e..60efd722e9d7ff7e2b2be6f556e0366e92b189fd 100644 (file)
@@ -86,6 +86,7 @@
   MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf\r
   MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.inf\r
   MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf\r
+  MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf\r
 \r
   MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf\r
   MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf\r