2 The assistant function implementation for IpSecConfig application.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "IpSecConfig.h"
20 Helper function called to change an input parameter in the string format to a number.
22 @param[in] FlagStr The pointer to the flag string.
23 @param[in] Maximum Greatest value number.
24 @param[in, out] ValuePtr The pointer to the input parameter in string format.
25 @param[in] ByteCount The valid byte count
26 @param[in] Map The pointer to the STR2INT table.
27 @param[in] ParamPackage The pointer to the ParamPackage list.
28 @param[in] FormatMask The bit mask.
29 BIT 0 set indicates the value of a flag might be a number.
30 BIT 1 set indicates the value of a flag might be a string that needs to be looked up.
32 @retval EFI_SUCCESS The operation completed successfully.
33 @retval EFI_NOT_FOUND The input parameter can't be found.
34 @retval EFI_INVALID_PARAMETER The input parameter is an invalid input.
40 IN OUT VOID
*ValuePtr
,
43 IN LIST_ENTRY
*ParamPackage
,
51 CONST CHAR16
*ValueStr
;
53 ASSERT (FormatMask
& (FORMAT_NUMBER
| FORMAT_STRING
));
57 ValueStr
= ShellCommandLineGetValue (ParamPackage
, FlagStr
);
59 if (ValueStr
== NULL
) {
63 // Try to convert to integer directly if MaybeNumber is TRUE.
65 if ((FormatMask
& FORMAT_NUMBER
) != 0) {
66 Value64
= StrToUInteger (ValueStr
, &Status
);
67 if (!EFI_ERROR (Status
)) {
69 // Convert successfully.
71 if (Value64
> Maximum
) {
73 // But the result is invalid
79 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE
),
85 return EFI_INVALID_PARAMETER
;
92 if (!Converted
&& ((FormatMask
& FORMAT_STRING
) != 0)) {
94 // Convert falied, so use String->Integer map.
97 Value64
= MapStringToInteger (ValueStr
, Map
);
98 if (Value64
== (UINT32
) -1) {
100 // Cannot find the string in the map.
106 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE
),
112 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS
), mHiiHandle
);
113 for (Index
= 0; Map
[Index
].String
!= NULL
; Index
++) {
114 Print (L
" %s", Map
[Index
].String
);
118 return EFI_INVALID_PARAMETER
;
122 CopyMem (ValuePtr
, &Value64
, ByteCount
);
128 Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address
129 into a proper address for the EFI_IP_ADDRESS structure.
131 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
132 @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.
134 @retval EFI_SUCCESS The operation completed successfully.
135 @retval EFI_INVALID_PARAMETER Invalid parameter.
140 OUT EFI_IP_ADDRESS
*Ip
145 if ((Ptr
== NULL
) || (Ip
== NULL
)) {
146 return EFI_INVALID_PARAMETER
;
150 // Parse the input address as Ipv4 Address first.
152 Status
= NetLibStrToIp4 (Ptr
, &Ip
->v4
);
153 if (!EFI_ERROR (Status
)) {
157 Status
= NetLibStrToIp6 (Ptr
, &Ip
->v6
);
162 Helper function called to calculate the prefix length associated with the string
163 containing an Ipv4 or Ipv6 Internet Protocol address.
165 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
166 @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.
168 @retval EFI_SUCCESS The operation completed successfully.
169 @retval EFI_INVALID_PARAMETER Invalid parameter.
170 @retval Others Other mistake case.
175 OUT EFI_IP_ADDRESS_INFO
*Addr
180 if ((Ptr
== NULL
) || (Addr
== NULL
)) {
181 return EFI_INVALID_PARAMETER
;
184 Status
= NetLibStrToIp4 (Ptr
, &Addr
->Address
.v4
);
185 if (!EFI_ERROR (Status
)) {
186 if ((UINT32
)(*Addr
->Address
.v4
.Addr
) == 0) {
187 Addr
->PrefixLength
= 0;
189 Addr
->PrefixLength
= 32;
194 Status
= NetLibStrToIp6andPrefix (Ptr
, &Addr
->Address
.v6
, &Addr
->PrefixLength
);
195 if (!EFI_ERROR (Status
) && (Addr
->PrefixLength
== 0xFF)) {
196 Addr
->PrefixLength
= 128;
203 Helper function called to calculate the port range associated with the string.
205 @param[in] Ptr The pointer to the string containing a port and range.
206 @param[out] Port The pointer to the Port to contain the result.
207 @param[out] PortRange The pointer to the PortRange to contain the result.
209 @retval EFI_SUCCESS The operation completed successfully.
210 @retval EFI_INVALID_PARAMETER Invalid parameter.
211 @retval Others Other mistake case.
217 OUT UINT16
*PortRange
224 for (BreakPtr
= Ptr
; (*BreakPtr
!= L
'\0') && (*BreakPtr
!= L
':'); BreakPtr
++) {
230 *Port
= (UINT16
) StrToUInteger (Ptr
, &Status
);
232 if (EFI_ERROR (Status
)) {
237 if (*BreakPtr
== L
':') {
239 *PortRange
= (UINT16
) StrToUInteger (BreakPtr
, &Status
);
240 if (EFI_ERROR (Status
)) {
244 if (*PortRange
< *Port
) {
245 return EFI_INVALID_PARAMETER
;
248 *PortRange
= (UINT16
) (*PortRange
- *Port
);
255 Helper function called to transfer a string to an unsigned integer.
257 @param[in] Str The pointer to the string.
258 @param[out] Status The operation status.
260 @return The integer value of converted Str.
264 IN CONST CHAR16
*Str
,
265 OUT EFI_STATUS
*Status
277 *Status
= EFI_ABORTED
;
280 // Skip leading white space.
282 while ((*Str
!= 0) && (*Str
== ' ')) {
286 // For NULL Str, just return.
292 // Skip white space in tail.
295 StrTail
= (CHAR16
*) (Str
+ Len
- 1);
296 while (*StrTail
== ' ') {
301 Len
= StrTail
- Str
+ 1;
304 // Check hex prefix '0x'.
306 if ((Len
>= 2) && (*Str
== '0') && ((*(Str
+ 1) == 'x') || (*(Str
+ 1) == 'X'))) {
316 // Convert the string to value.
318 for (; Str
<= StrTail
; Str
++) {
323 if (RShiftU64 (Value
, 60) != 0) {
325 // Overflow here x16.
330 NewValue
= LShiftU64 (Value
, 4);
332 if (RShiftU64 (Value
, 61) != 0) {
339 NewValue
= LShiftU64 (Value
, 3);
340 Value
= LShiftU64 (Value
, 1);
342 if (NewValue
< Value
) {
352 if ((Base
== 16) && (Char
>= 'a') && (Char
<= 'f')) {
353 Char
= (CHAR16
) (Char
- 'a' + 'A');
356 if ((Base
== 16) && (Char
>= 'A') && (Char
<= 'F')) {
357 Value
+= (Char
- 'A') + 10;
358 } else if ((Char
>= '0') && (Char
<= '9')) {
359 Value
+= (Char
- '0');
362 // Unexpected Char encountered.
368 *Status
= EFI_SUCCESS
;
373 Helper function called to transfer a string to an unsigned integer according to the map table.
375 @param[in] Str The pointer to the string.
376 @param[in] Map The pointer to the map table.
378 @return The integer value of converted Str. If not found, then return -1.
382 IN CONST CHAR16
*Str
,
388 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
389 if (StrCmp (Item
->String
, Str
) == 0) {
390 return Item
->Integer
;
398 Helper function called to transfer an unsigned integer to a string according to the map table.
400 @param[in] Integer The pointer to the string.
401 @param[in] Map The pointer to the map table.
403 @return The converted Str. If not found, then return NULL.
413 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
414 if (Integer
== Item
->Integer
) {