2 The assistant function implementation for IpSecConfig application.
4 Copyright (c) 2009 - 2010, 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.
96 Value64
= MapStringToInteger (ValueStr
, Map
);
97 if (Value64
== (UINT32
) -1) {
99 // Cannot find the string in the map.
105 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE
),
111 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS
), mHiiHandle
);
112 for (Index
= 0; Map
[Index
].String
!= NULL
; Index
++) {
113 Print (L
" %s", Map
[Index
].String
);
117 return EFI_INVALID_PARAMETER
;
121 CopyMem (ValuePtr
, &Value64
, ByteCount
);
127 Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address
128 into a proper address for the EFI_IP_ADDRESS structure.
130 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
131 @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.
133 @retval EFI_SUCCESS The operation completed successfully.
134 @retval EFI_INVALID_PARAMETER Invalid parameter.
139 OUT EFI_IP_ADDRESS
*Ip
144 if ((Ptr
== NULL
) || (Ip
== NULL
)) {
145 return EFI_INVALID_PARAMETER
;
149 // Parse the input address as Ipv4 Address first.
151 Status
= NetLibStrToIp4 (Ptr
, &Ip
->v4
);
152 if (!EFI_ERROR (Status
)) {
156 Status
= NetLibStrToIp6 (Ptr
, &Ip
->v6
);
161 Helper function called to calculate the prefix length associated with the string
162 containing an Ipv4 or Ipv6 Internet Protocol address.
164 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.
165 @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.
167 @retval EFI_SUCCESS The operation completed successfully.
168 @retval EFI_INVALID_PARAMETER Invalid parameter.
169 @retval Others Other mistake case.
174 OUT EFI_IP_ADDRESS_INFO
*Addr
179 if ((Ptr
== NULL
) || (Addr
== NULL
)) {
180 return EFI_INVALID_PARAMETER
;
183 Status
= NetLibStrToIp4 (Ptr
, &Addr
->Address
.v4
);
184 if (!EFI_ERROR (Status
)) {
185 if ((UINT32
)(*Addr
->Address
.v4
.Addr
) == 0) {
186 Addr
->PrefixLength
= 0;
188 Addr
->PrefixLength
= 32;
193 Status
= NetLibStrToIp6andPrefix (Ptr
, &Addr
->Address
.v6
, &Addr
->PrefixLength
);
194 if (!EFI_ERROR (Status
) && (Addr
->PrefixLength
== 0xFF)) {
195 Addr
->PrefixLength
= 128;
202 Helper function called to calculate the port range associated with the string.
204 @param[in] Ptr The pointer to the string containing a port and range.
205 @param[out] Port The pointer to the Port to contain the result.
206 @param[out] PortRange The pointer to the PortRange to contain the result.
208 @retval EFI_SUCCESS The operation completed successfully.
209 @retval EFI_INVALID_PARAMETER Invalid parameter.
210 @retval Others Other mistake case.
216 OUT UINT16
*PortRange
223 for (BreakPtr
= Ptr
; (*BreakPtr
!= L
'\0') && (*BreakPtr
!= L
':'); BreakPtr
++) {
229 *Port
= (UINT16
) StrToUInteger (Ptr
, &Status
);
231 if (EFI_ERROR (Status
)) {
236 if (*BreakPtr
== L
':') {
238 *PortRange
= (UINT16
) StrToUInteger (BreakPtr
, &Status
);
239 if (EFI_ERROR (Status
)) {
243 if (*PortRange
< *Port
) {
244 return EFI_INVALID_PARAMETER
;
247 *PortRange
= (UINT16
) (*PortRange
- *Port
);
254 Helper function called to transfer a string to an unsigned integer.
256 @param[in] Str The pointer to the string.
257 @param[out] Status The operation status.
259 @return The integer value of converted Str.
263 IN CONST CHAR16
*Str
,
264 OUT EFI_STATUS
*Status
276 *Status
= EFI_ABORTED
;
279 // Skip leading white space.
281 while ((*Str
!= 0) && (*Str
== ' ')) {
285 // For NULL Str, just return.
291 // Skip white space in tail.
294 StrTail
= (CHAR16
*) (Str
+ Len
- 1);
295 while (*StrTail
== ' ') {
300 Len
= StrTail
- Str
+ 1;
303 // Check hex prefix '0x'.
305 if ((Len
>= 2) && (*Str
== '0') && ((*(Str
+ 1) == 'x') || (*(Str
+ 1) == 'X'))) {
315 // Convert the string to value.
317 for (; Str
<= StrTail
; Str
++) {
322 if (RShiftU64 (Value
, 60) != 0) {
324 // Overflow here x16.
329 NewValue
= LShiftU64 (Value
, 4);
331 if (RShiftU64 (Value
, 61) != 0) {
338 NewValue
= LShiftU64 (Value
, 3);
339 Value
= LShiftU64 (Value
, 1);
341 if (NewValue
< Value
) {
351 if ((Base
== 16) && (Char
>= 'a') && (Char
<= 'f')) {
352 Char
= (CHAR16
) (Char
- 'a' + 'A');
355 if ((Base
== 16) && (Char
>= 'A') && (Char
<= 'F')) {
356 Value
+= (Char
- 'A') + 10;
357 } else if ((Char
>= '0') && (Char
<= '9')) {
358 Value
+= (Char
- '0');
361 // Unexpected Char encountered.
367 *Status
= EFI_SUCCESS
;
372 Helper function called to transfer a string to an unsigned integer according to the map table.
374 @param[in] Str The pointer to the string.
375 @param[in] Map The pointer to the map table.
377 @return The integer value of converted Str. If not found, then return -1.
381 IN CONST CHAR16
*Str
,
387 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
388 if (StrCmp (Item
->String
, Str
) == 0) {
389 return Item
->Integer
;
397 Helper function called to transfer an unsigned integer to a string according to the map table.
399 @param[in] Integer The pointer to the string.
400 @param[in] Map The pointer to the map table.
402 @return The converted Str. If not found, then return NULL.
412 for (Item
= Map
; Item
->String
!= NULL
; Item
++) {
413 if (Integer
== Item
->Integer
) {