+/** @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