+++ /dev/null
-/** @file\r
- The assistant function implementation for IpSecConfig application.\r
-\r
- Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
-\r
- SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#include "IpSecConfig.h"\r
-#include "Helper.h"\r
-\r
-/**\r
- Helper function called to change an input parameter in the string format to a number.\r
-\r
- @param[in] FlagStr The pointer to the flag string.\r
- @param[in] Maximum Greatest value number.\r
- @param[in, out] ValuePtr The pointer to the input parameter in string format.\r
- @param[in] ByteCount The valid byte count\r
- @param[in] Map The pointer to the STR2INT table.\r
- @param[in] ParamPackage The pointer to the ParamPackage list.\r
- @param[in] FormatMask The bit mask.\r
- BIT 0 set indicates the value of a flag might be a number.\r
- BIT 1 set indicates the value of a flag might be a string that needs to be looked up.\r
-\r
- @retval EFI_SUCCESS The operation completed successfully.\r
- @retval EFI_NOT_FOUND The input parameter can't be found.\r
- @retval EFI_INVALID_PARAMETER The input parameter is an invalid input.\r
-**/\r
-EFI_STATUS\r
-GetNumber (\r
- IN CHAR16 *FlagStr,\r
- IN UINT64 Maximum,\r
- IN OUT VOID *ValuePtr,\r
- IN UINTN ByteCount,\r
- IN STR2INT *Map,\r
- IN LIST_ENTRY *ParamPackage,\r
- IN UINT32 FormatMask\r
- )\r
-{\r
- EFI_STATUS Status;\r
- UINT64 Value64;\r
- BOOLEAN Converted;\r
- UINTN Index;\r
- CONST CHAR16 *ValueStr;\r
-\r
- ASSERT (FormatMask & (FORMAT_NUMBER | FORMAT_STRING));\r
-\r
- Converted = FALSE;\r
- Value64 = 0;\r
- ValueStr = ShellCommandLineGetValue (ParamPackage, FlagStr);\r
-\r
- if (ValueStr == NULL) {\r
- return EFI_NOT_FOUND;\r
- } else {\r
- //\r
- // Try to convert to integer directly if MaybeNumber is TRUE.\r
- //\r
- if ((FormatMask & FORMAT_NUMBER) != 0) {\r
- Value64 = StrToUInteger (ValueStr, &Status);\r
- if (!EFI_ERROR (Status)) {\r
- //\r
- // Convert successfully.\r
- //\r
- if (Value64 > Maximum) {\r
- //\r
- // But the result is invalid\r
- //\r
- ShellPrintHiiEx (\r
- -1,\r
- -1,\r
- NULL,\r
- STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),\r
- mHiiHandle,\r
- mAppName,\r
- FlagStr,\r
- ValueStr\r
- );\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Converted = TRUE;\r
- }\r
- }\r
-\r
- if (!Converted && ((FormatMask & FORMAT_STRING) != 0)) {\r
- //\r
- // Convert falied, so use String->Integer map.\r
- //\r
- ASSERT (Map != NULL);\r
- Value64 = MapStringToInteger (ValueStr, Map);\r
- if (Value64 == (UINT32) -1) {\r
- //\r
- // Cannot find the string in the map.\r
- //\r
- ShellPrintHiiEx (\r
- -1,\r
- -1,\r
- NULL,\r
- STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),\r
- mHiiHandle,\r
- mAppName,\r
- FlagStr,\r
- ValueStr\r
- );\r
- ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS), mHiiHandle);\r
- for (Index = 0; Map[Index].String != NULL; Index++) {\r
- Print (L" %s", Map[Index].String);\r
- }\r
-\r
- Print (L"\n");\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- }\r
-\r
- CopyMem (ValuePtr, &Value64, ByteCount);\r
- return EFI_SUCCESS;\r
- }\r
-}\r
-\r
-/**\r
- Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address\r
- into a proper address for the EFI_IP_ADDRESS structure.\r
-\r
- @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r
- @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.\r
-\r
- @retval EFI_SUCCESS The operation completed successfully.\r
- @retval EFI_INVALID_PARAMETER Invalid parameter.\r
-**/\r
-EFI_STATUS\r
-EfiInetAddr2 (\r
- IN CHAR16 *Ptr,\r
- OUT EFI_IP_ADDRESS *Ip\r
- )\r
-{\r
- EFI_STATUS Status;\r
-\r
- if ((Ptr == NULL) || (Ip == NULL)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- //\r
- // Parse the input address as Ipv4 Address first.\r
- //\r
- Status = NetLibStrToIp4 (Ptr, &Ip->v4);\r
- if (!EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- Status = NetLibStrToIp6 (Ptr, &Ip->v6);\r
- return Status;\r
-}\r
-\r
-/**\r
- Helper function called to calculate the prefix length associated with the string\r
- containing an Ipv4 or Ipv6 Internet Protocol address.\r
-\r
- @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r
- @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.\r
-\r
- @retval EFI_SUCCESS The operation completed successfully.\r
- @retval EFI_INVALID_PARAMETER Invalid parameter.\r
- @retval Others Other mistake case.\r
-**/\r
-EFI_STATUS\r
-EfiInetAddrRange (\r
- IN CHAR16 *Ptr,\r
- OUT EFI_IP_ADDRESS_INFO *Addr\r
- )\r
-{\r
- EFI_STATUS Status;\r
-\r
- if ((Ptr == NULL) || (Addr == NULL)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Status = NetLibStrToIp4 (Ptr, &Addr->Address.v4);\r
- if (!EFI_ERROR (Status)) {\r
- if ((UINT32)(*Addr->Address.v4.Addr) == 0) {\r
- Addr->PrefixLength = 0;\r
- } else {\r
- Addr->PrefixLength = 32;\r
- }\r
- return Status;\r
- }\r
-\r
- Status = NetLibStrToIp6andPrefix (Ptr, &Addr->Address.v6, &Addr->PrefixLength);\r
- if (!EFI_ERROR (Status) && (Addr->PrefixLength == 0xFF)) {\r
- Addr->PrefixLength = 128;\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- Helper function called to calculate the port range associated with the string.\r
-\r
- @param[in] Ptr The pointer to the string containing a port and range.\r
- @param[out] Port The pointer to the Port to contain the result.\r
- @param[out] PortRange The pointer to the PortRange to contain the result.\r
-\r
- @retval EFI_SUCCESS The operation completed successfully.\r
- @retval EFI_INVALID_PARAMETER Invalid parameter.\r
- @retval Others Other mistake case.\r
-**/\r
-EFI_STATUS\r
-EfiInetPortRange (\r
- IN CHAR16 *Ptr,\r
- OUT UINT16 *Port,\r
- OUT UINT16 *PortRange\r
- )\r
-{\r
- CHAR16 *BreakPtr;\r
- CHAR16 Ch;\r
- EFI_STATUS Status;\r
-\r
- for (BreakPtr = Ptr; (*BreakPtr != L'\0') && (*BreakPtr != L':'); BreakPtr++) {\r
- ;\r
- }\r
-\r
- Ch = *BreakPtr;\r
- *BreakPtr = L'\0';\r
- *Port = (UINT16) StrToUInteger (Ptr, &Status);\r
- *BreakPtr = Ch;\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- *PortRange = 0;\r
- if (*BreakPtr == L':') {\r
- BreakPtr++;\r
- *PortRange = (UINT16) StrToUInteger (BreakPtr, &Status);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- if (*PortRange < *Port) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- *PortRange = (UINT16) (*PortRange - *Port);\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Helper function called to transfer a string to an unsigned integer.\r
-\r
- @param[in] Str The pointer to the string.\r
- @param[out] Status The operation status.\r
-\r
- @return The integer value of converted Str.\r
-**/\r
-UINT64\r
-StrToUInteger (\r
- IN CONST CHAR16 *Str,\r
- OUT EFI_STATUS *Status\r
- )\r
-{\r
- UINT64 Value;\r
- UINT64 NewValue;\r
- CHAR16 *StrTail;\r
- CHAR16 Char;\r
- UINTN Base;\r
- UINTN Len;\r
-\r
- Base = 10;\r
- Value = 0;\r
- *Status = EFI_ABORTED;\r
-\r
- //\r
- // Skip leading white space.\r
- //\r
- while ((*Str != 0) && (*Str == ' ')) {\r
- Str++;\r
- }\r
- //\r
- // For NULL Str, just return.\r
- //\r
- if (*Str == 0) {\r
- return 0;\r
- }\r
- //\r
- // Skip white space in tail.\r
- //\r
- Len = StrLen (Str);\r
- StrTail = (CHAR16 *) (Str + Len - 1);\r
- while (*StrTail == ' ') {\r
- *StrTail = 0;\r
- StrTail--;\r
- }\r
-\r
- Len = StrTail - Str + 1;\r
-\r
- //\r
- // Check hex prefix '0x'.\r
- //\r
- if ((Len >= 2) && (*Str == '0') && ((*(Str + 1) == 'x') || (*(Str + 1) == 'X'))) {\r
- Str += 2;\r
- Len -= 2;\r
- Base = 16;\r
- }\r
-\r
- if (Len == 0) {\r
- return 0;\r
- }\r
- //\r
- // Convert the string to value.\r
- //\r
- for (; Str <= StrTail; Str++) {\r
-\r
- Char = *Str;\r
-\r
- if (Base == 16) {\r
- if (RShiftU64 (Value, 60) != 0) {\r
- //\r
- // Overflow here x16.\r
- //\r
- return 0;\r
- }\r
-\r
- NewValue = LShiftU64 (Value, 4);\r
- } else {\r
- if (RShiftU64 (Value, 61) != 0) {\r
- //\r
- // Overflow here x8.\r
- //\r
- return 0;\r
- }\r
-\r
- NewValue = LShiftU64 (Value, 3);\r
- Value = LShiftU64 (Value, 1);\r
- NewValue += Value;\r
- if (NewValue < Value) {\r
- //\r
- // Overflow here.\r
- //\r
- return 0;\r
- }\r
- }\r
-\r
- Value = NewValue;\r
-\r
- if ((Base == 16) && (Char >= 'a') && (Char <= 'f')) {\r
- Char = (CHAR16) (Char - 'a' + 'A');\r
- }\r
-\r
- if ((Base == 16) && (Char >= 'A') && (Char <= 'F')) {\r
- Value += (Char - 'A') + 10;\r
- } else if ((Char >= '0') && (Char <= '9')) {\r
- Value += (Char - '0');\r
- } else {\r
- //\r
- // Unexpected Char encountered.\r
- //\r
- return 0;\r
- }\r
- }\r
-\r
- *Status = EFI_SUCCESS;\r
- return Value;\r
-}\r
-\r
-/**\r
- Helper function called to transfer a string to an unsigned integer according to the map table.\r
-\r
- @param[in] Str The pointer to the string.\r
- @param[in] Map The pointer to the map table.\r
-\r
- @return The integer value of converted Str. If not found, then return -1.\r
-**/\r
-UINT32\r
-MapStringToInteger (\r
- IN CONST CHAR16 *Str,\r
- IN STR2INT *Map\r
- )\r
-{\r
- STR2INT *Item;\r
-\r
- for (Item = Map; Item->String != NULL; Item++) {\r
- if (StrCmp (Item->String, Str) == 0) {\r
- return Item->Integer;\r
- }\r
- }\r
-\r
- return (UINT32) -1;\r
-}\r
-\r
-/**\r
- Helper function called to transfer an unsigned integer to a string according to the map table.\r
-\r
- @param[in] Integer The pointer to the string.\r
- @param[in] Map The pointer to the map table.\r
-\r
- @return The converted Str. If not found, then return NULL.\r
-**/\r
-CHAR16 *\r
-MapIntegerToString (\r
- IN UINT32 Integer,\r
- IN STR2INT *Map\r
- )\r
-{\r
- STR2INT *Item;\r
-\r
- for (Item = Map; Item->String != NULL; Item++) {\r
- if (Integer == Item->Integer) {\r
- return Item->String;\r
- }\r
- }\r
-\r
- return NULL;\r
-}\r