2 The assistant function implementation for IpSecConfig application.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "IpSecConfig.h"
14 Helper function called to change an input parameter in the string format to a number.
16 @param[in] FlagStr The pointer to the flag string.
17 @param[in] Maximum Greatest value number.
18 @param[in, out] ValuePtr The pointer to the input parameter in string format.
19 @param[in] ByteCount The valid byte count
20 @param[in] Map The pointer to the STR2INT table.
21 @param[in] ParamPackage The pointer to the ParamPackage list.
22 @param[in] FormatMask The bit mask.
23 BIT 0 set indicates the value of a flag might be a number.
24 BIT 1 set indicates the value of a flag might be a string that needs to be looked up.
26 @retval EFI_SUCCESS The operation completed successfully.
27 @retval EFI_NOT_FOUND The input parameter can't be found.
28 @retval EFI_INVALID_PARAMETER The input parameter is an invalid input.
34 IN OUT VOID
*ValuePtr
,
37 IN LIST_ENTRY
*ParamPackage
,
45 CONST CHAR16
*ValueStr
;
47 ASSERT (FormatMask
& (FORMAT_NUMBER
| FORMAT_STRING
));
51 ValueStr
= ShellCommandLineGetValue (ParamPackage
, FlagStr
);
53 if (ValueStr
== NULL
) {
57 // Try to convert to integer directly if MaybeNumber is TRUE.
59 if ((FormatMask
& FORMAT_NUMBER
) != 0) {
60 Value64
= StrToUInteger (ValueStr
, &Status
);
61 if (!EFI_ERROR (Status
)) {
63 // Convert successfully.
65 if (Value64
> Maximum
) {
67 // But the result is invalid
73 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE
),
79 return EFI_INVALID_PARAMETER
;
86 if (!Converted
&& ((FormatMask
& FORMAT_STRING
) != 0)) {
88 // Convert falied, so use String->Integer map.
91 Value64
= MapStringToInteger (ValueStr
, Map
);
92 if (Value64
== (UINT32
) -1) {
94 // Cannot find the string in the map.
100 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE
),
106 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS
), mHiiHandle
);
107 for (Index
= 0; Map
[Index
].String
!= NULL
; Index
++) {
108 Print (L
" %s", Map
[Index
].String
);
112 return EFI_INVALID_PARAMETER
;
116 CopyMem (ValuePtr
, &Value64
, ByteCount
);
122 Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address
123 into a proper address for the EFI_IP_ADDRESS structure.
125 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
126 @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.
128 @retval EFI_SUCCESS The operation completed successfully.
129 @retval EFI_INVALID_PARAMETER Invalid parameter.
134 OUT EFI_IP_ADDRESS
*Ip
139 if ((Ptr
== NULL
) || (Ip
== NULL
)) {
140 return EFI_INVALID_PARAMETER
;
144 // Parse the input address as Ipv4 Address first.
146 Status
= NetLibStrToIp4 (Ptr
, &Ip
->v4
);
147 if (!EFI_ERROR (Status
)) {
151 Status
= NetLibStrToIp6 (Ptr
, &Ip
->v6
);
156 Helper function called to calculate the prefix length associated with the string
157 containing an Ipv4 or Ipv6 Internet Protocol address.
159 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
160 @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.
162 @retval EFI_SUCCESS The operation completed successfully.
163 @retval EFI_INVALID_PARAMETER Invalid parameter.
164 @retval Others Other mistake case.
169 OUT EFI_IP_ADDRESS_INFO
*Addr
174 if ((Ptr
== NULL
) || (Addr
== NULL
)) {
175 return EFI_INVALID_PARAMETER
;
178 Status
= NetLibStrToIp4 (Ptr
, &Addr
->Address
.v4
);
179 if (!EFI_ERROR (Status
)) {
180 if ((UINT32
)(*Addr
->Address
.v4
.Addr
) == 0) {
181 Addr
->PrefixLength
= 0;
183 Addr
->PrefixLength
= 32;
188 Status
= NetLibStrToIp6andPrefix (Ptr
, &Addr
->Address
.v6
, &Addr
->PrefixLength
);
189 if (!EFI_ERROR (Status
) && (Addr
->PrefixLength
== 0xFF)) {
190 Addr
->PrefixLength
= 128;
197 Helper function called to calculate the port range associated with the string.
199 @param[in] Ptr The pointer to the string containing a port and range.
200 @param[out] Port The pointer to the Port to contain the result.
201 @param[out] PortRange The pointer to the PortRange to contain the result.
203 @retval EFI_SUCCESS The operation completed successfully.
204 @retval EFI_INVALID_PARAMETER Invalid parameter.
205 @retval Others Other mistake case.
211 OUT UINT16
*PortRange
218 for (BreakPtr
= Ptr
; (*BreakPtr
!= L
'\0') && (*BreakPtr
!= L
':'); BreakPtr
++) {
224 *Port
= (UINT16
) StrToUInteger (Ptr
, &Status
);
226 if (EFI_ERROR (Status
)) {
231 if (*BreakPtr
== L
':') {
233 *PortRange
= (UINT16
) StrToUInteger (BreakPtr
, &Status
);
234 if (EFI_ERROR (Status
)) {
238 if (*PortRange
< *Port
) {
239 return EFI_INVALID_PARAMETER
;
242 *PortRange
= (UINT16
) (*PortRange
- *Port
);
249 Helper function called to transfer a string to an unsigned integer.
251 @param[in] Str The pointer to the string.
252 @param[out] Status The operation status.
254 @return The integer value of converted Str.
258 IN CONST CHAR16
*Str
,
259 OUT EFI_STATUS
*Status
271 *Status
= EFI_ABORTED
;
274 // Skip leading white space.
276 while ((*Str
!= 0) && (*Str
== ' ')) {
280 // For NULL Str, just return.
286 // Skip white space in tail.
289 StrTail
= (CHAR16
*) (Str
+ Len
- 1);
290 while (*StrTail
== ' ') {
295 Len
= StrTail
- Str
+ 1;
298 // Check hex prefix '0x'.
300 if ((Len
>= 2) && (*Str
== '0') && ((*(Str
+ 1) == 'x') || (*(Str
+ 1) == 'X'))) {
310 // Convert the string to value.
312 for (; Str
<= StrTail
; Str
++) {
317 if (RShiftU64 (Value
, 60) != 0) {
319 // Overflow here x16.
324 NewValue
= LShiftU64 (Value
, 4);
326 if (RShiftU64 (Value
, 61) != 0) {
333 NewValue
= LShiftU64 (Value
, 3);
334 Value
= LShiftU64 (Value
, 1);
336 if (NewValue
< Value
) {
346 if ((Base
== 16) && (Char
>= 'a') && (Char
<= 'f')) {
347 Char
= (CHAR16
) (Char
- 'a' + 'A');
350 if ((Base
== 16) && (Char
>= 'A') && (Char
<= 'F')) {
351 Value
+= (Char
- 'A') + 10;
352 } else if ((Char
>= '0') && (Char
<= '9')) {
353 Value
+= (Char
- '0');
356 // Unexpected Char encountered.
362 *Status
= EFI_SUCCESS
;
367 Helper function called to transfer a string to an unsigned integer according to the map table.
369 @param[in] Str The pointer to the string.
370 @param[in] Map The pointer to the map table.
372 @return The integer value of converted Str. If not found, then return -1.
376 IN CONST CHAR16
*Str
,
382 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
383 if (StrCmp (Item
->String
, Str
) == 0) {
384 return Item
->Integer
;
392 Helper function called to transfer an unsigned integer to a string according to the map table.
394 @param[in] Integer The pointer to the string.
395 @param[in] Map The pointer to the map table.
397 @return The converted Str. If not found, then return NULL.
407 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
408 if (Integer
== Item
->Integer
) {