]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Application/IpsecConfig/Helper.c
NetworkPkg: Update Ipsecconfig Application to print the keys of SAD in concise way.
[mirror_edk2.git] / NetworkPkg / Application / IpsecConfig / Helper.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 The assistant function implementation for IpSecConfig application.\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "IpSecConfig.h"\r
17#include "Helper.h"\r
18\r
19/**\r
20 Helper function called to change an input parameter in the string format to a number.\r
21\r
22 @param[in] FlagStr The pointer to the flag string.\r
23 @param[in] Maximum Greatest value number.\r
24 @param[in, out] ValuePtr The pointer to the input parameter in string format.\r
25 @param[in] ByteCount The valid byte count\r
26 @param[in] Map The pointer to the STR2INT table.\r
27 @param[in] ParamPackage The pointer to the ParamPackage list.\r
28 @param[in] FormatMask The bit mask.\r
29 BIT 0 set indicates the value of a flag might be a number.\r
30 BIT 1 set indicates the value of a flag might be a string that needs to be looked up.\r
31\r
32 @retval EFI_SUCCESS The operation completed successfully.\r
33 @retval EFI_NOT_FOUND The input parameter can't be found.\r
34 @retval EFI_INVALID_PARAMETER The input parameter is an invalid input.\r
35**/\r
36EFI_STATUS\r
37GetNumber (\r
38 IN CHAR16 *FlagStr,\r
39 IN UINT64 Maximum,\r
40 IN OUT VOID *ValuePtr,\r
41 IN UINTN ByteCount,\r
42 IN STR2INT *Map,\r
43 IN LIST_ENTRY *ParamPackage,\r
44 IN UINT32 FormatMask\r
45 )\r
46{\r
47 EFI_STATUS Status;\r
48 UINT64 Value64;\r
49 BOOLEAN Converted;\r
50 UINTN Index;\r
51 CONST CHAR16 *ValueStr;\r
52\r
53 ASSERT (FormatMask & (FORMAT_NUMBER | FORMAT_STRING));\r
54\r
55 Converted = FALSE;\r
56 Value64 = 0;\r
57 ValueStr = ShellCommandLineGetValue (ParamPackage, FlagStr);\r
58\r
59 if (ValueStr == NULL) {\r
60 return EFI_NOT_FOUND;\r
61 } else {\r
62 //\r
63 // Try to convert to integer directly if MaybeNumber is TRUE.\r
64 //\r
65 if ((FormatMask & FORMAT_NUMBER) != 0) {\r
66 Value64 = StrToUInteger (ValueStr, &Status);\r
67 if (!EFI_ERROR (Status)) {\r
68 //\r
69 // Convert successfully.\r
70 //\r
71 if (Value64 > Maximum) {\r
72 //\r
73 // But the result is invalid\r
74 //\r
75 ShellPrintHiiEx (\r
76 -1,\r
77 -1,\r
78 NULL,\r
79 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),\r
80 mHiiHandle,\r
81 mAppName,\r
82 FlagStr,\r
83 ValueStr\r
84 );\r
85 return EFI_INVALID_PARAMETER;\r
86 }\r
87\r
88 Converted = TRUE;\r
89 }\r
90 }\r
91\r
92 if (!Converted && ((FormatMask & FORMAT_STRING) != 0)) {\r
93 //\r
94 // Convert falied, so use String->Integer map.\r
95 //\r
96 Value64 = MapStringToInteger (ValueStr, Map);\r
97 if (Value64 == (UINT32) -1) {\r
98 //\r
99 // Cannot find the string in the map.\r
100 //\r
101 ShellPrintHiiEx (\r
102 -1,\r
103 -1,\r
104 NULL,\r
105 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),\r
106 mHiiHandle,\r
107 mAppName,\r
108 FlagStr,\r
109 ValueStr\r
110 );\r
111 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS), mHiiHandle);\r
112 for (Index = 0; Map[Index].String != NULL; Index++) {\r
113 Print (L" %s", Map[Index].String);\r
114 }\r
115\r
116 Print (L"\n");\r
117 return EFI_INVALID_PARAMETER;\r
118 }\r
119 }\r
120\r
121 CopyMem (ValuePtr, &Value64, ByteCount);\r
122 return EFI_SUCCESS;\r
123 }\r
124}\r
125\r
126/**\r
127 Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address\r
128 into a proper address for the EFI_IP_ADDRESS structure.\r
129\r
130 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r
131 @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.\r
132\r
133 @retval EFI_SUCCESS The operation completed successfully.\r
134 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
135**/\r
136EFI_STATUS\r
137EfiInetAddr2 (\r
138 IN CHAR16 *Ptr,\r
139 OUT EFI_IP_ADDRESS *Ip\r
140 )\r
141{\r
142 EFI_STATUS Status;\r
143\r
144 if ((Ptr == NULL) || (Ip == NULL)) {\r
145 return EFI_INVALID_PARAMETER;\r
146 }\r
147\r
148 //\r
149 // Parse the input address as Ipv4 Address first.\r
150 //\r
151 Status = NetLibStrToIp4 (Ptr, &Ip->v4);\r
152 if (!EFI_ERROR (Status)) {\r
153 return Status;\r
154 }\r
155\r
156 Status = NetLibStrToIp6 (Ptr, &Ip->v6);\r
157 return Status;\r
158}\r
159\r
160/**\r
161 Helper function called to calculate the prefix length associated with the string\r
162 containing an Ipv4 or Ipv6 Internet Protocol address.\r
163\r
164 @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r
165 @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.\r
166\r
167 @retval EFI_SUCCESS The operation completed successfully.\r
168 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
169 @retval Others Other mistake case.\r
170**/\r
171EFI_STATUS\r
172EfiInetAddrRange (\r
173 IN CHAR16 *Ptr,\r
174 OUT EFI_IP_ADDRESS_INFO *Addr\r
175 )\r
176{\r
177 EFI_STATUS Status;\r
178\r
179 if ((Ptr == NULL) || (Addr == NULL)) {\r
180 return EFI_INVALID_PARAMETER;\r
181 }\r
182\r
183 Status = NetLibStrToIp4 (Ptr, &Addr->Address.v4);\r
184 if (!EFI_ERROR (Status)) {\r
185 if ((UINT32)(*Addr->Address.v4.Addr) == 0) {\r
186 Addr->PrefixLength = 0;\r
187 } else {\r
188 Addr->PrefixLength = 32;\r
189 }\r
190 return Status;\r
191 }\r
192\r
193 Status = NetLibStrToIp6andPrefix (Ptr, &Addr->Address.v6, &Addr->PrefixLength);\r
194 if (!EFI_ERROR (Status) && (Addr->PrefixLength == 0xFF)) {\r
195 Addr->PrefixLength = 128;\r
196 }\r
197\r
198 return Status;\r
199}\r
200\r
201/**\r
202 Helper function called to calculate the port range associated with the string.\r
203\r
204 @param[in] Ptr The pointer to the string containing a port and range.\r
205 @param[out] Port The pointer to the Port to contain the result.\r
206 @param[out] PortRange The pointer to the PortRange to contain the result.\r
207\r
208 @retval EFI_SUCCESS The operation completed successfully.\r
209 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
210 @retval Others Other mistake case.\r
211**/\r
212EFI_STATUS\r
213EfiInetPortRange (\r
214 IN CHAR16 *Ptr,\r
215 OUT UINT16 *Port,\r
216 OUT UINT16 *PortRange\r
217 )\r
218{\r
219 CHAR16 *BreakPtr;\r
220 CHAR16 Ch;\r
221 EFI_STATUS Status;\r
222\r
223 for (BreakPtr = Ptr; (*BreakPtr != L'\0') && (*BreakPtr != L':'); BreakPtr++) {\r
224 ;\r
225 }\r
226\r
227 Ch = *BreakPtr;\r
228 *BreakPtr = L'\0';\r
229 *Port = (UINT16) StrToUInteger (Ptr, &Status);\r
230 *BreakPtr = Ch;\r
231 if (EFI_ERROR (Status)) {\r
232 return Status;\r
233 }\r
234\r
235 *PortRange = 0;\r
236 if (*BreakPtr == L':') {\r
237 BreakPtr++;\r
238 *PortRange = (UINT16) StrToUInteger (BreakPtr, &Status);\r
239 if (EFI_ERROR (Status)) {\r
240 return Status;\r
241 }\r
242\r
243 if (*PortRange < *Port) {\r
244 return EFI_INVALID_PARAMETER;\r
245 }\r
246\r
247 *PortRange = (UINT16) (*PortRange - *Port);\r
248 }\r
249\r
250 return EFI_SUCCESS;\r
251}\r
252\r
253/**\r
254 Helper function called to transfer a string to an unsigned integer.\r
255\r
256 @param[in] Str The pointer to the string.\r
257 @param[out] Status The operation status.\r
258\r
259 @return The integer value of converted Str.\r
260**/\r
261UINT64\r
262StrToUInteger (\r
263 IN CONST CHAR16 *Str,\r
264 OUT EFI_STATUS *Status\r
265 )\r
266{\r
267 UINT64 Value;\r
268 UINT64 NewValue;\r
269 CHAR16 *StrTail;\r
270 CHAR16 Char;\r
271 UINTN Base;\r
272 UINTN Len;\r
273\r
274 Base = 10;\r
275 Value = 0;\r
276 *Status = EFI_ABORTED;\r
277\r
278 //\r
279 // Skip leading white space.\r
280 //\r
281 while ((*Str != 0) && (*Str == ' ')) {\r
282 Str++;\r
283 }\r
284 //\r
285 // For NULL Str, just return.\r
286 //\r
287 if (*Str == 0) {\r
288 return 0;\r
289 }\r
290 //\r
291 // Skip white space in tail.\r
292 //\r
293 Len = StrLen (Str);\r
294 StrTail = (CHAR16 *) (Str + Len - 1);\r
295 while (*StrTail == ' ') {\r
296 *StrTail = 0;\r
297 StrTail--;\r
298 }\r
299\r
300 Len = StrTail - Str + 1;\r
301\r
302 //\r
303 // Check hex prefix '0x'.\r
304 //\r
305 if ((Len >= 2) && (*Str == '0') && ((*(Str + 1) == 'x') || (*(Str + 1) == 'X'))) {\r
306 Str += 2;\r
307 Len -= 2;\r
308 Base = 16;\r
309 }\r
310\r
311 if (Len == 0) {\r
312 return 0;\r
313 }\r
314 //\r
315 // Convert the string to value.\r
316 //\r
317 for (; Str <= StrTail; Str++) {\r
318\r
319 Char = *Str;\r
320\r
321 if (Base == 16) {\r
322 if (RShiftU64 (Value, 60) != 0) {\r
323 //\r
324 // Overflow here x16.\r
325 //\r
326 return 0;\r
327 }\r
328\r
329 NewValue = LShiftU64 (Value, 4);\r
330 } else {\r
331 if (RShiftU64 (Value, 61) != 0) {\r
332 //\r
333 // Overflow here x8.\r
334 //\r
335 return 0;\r
336 }\r
337\r
338 NewValue = LShiftU64 (Value, 3);\r
339 Value = LShiftU64 (Value, 1);\r
340 NewValue += Value;\r
341 if (NewValue < Value) {\r
342 //\r
343 // Overflow here.\r
344 //\r
345 return 0;\r
346 }\r
347 }\r
348\r
349 Value = NewValue;\r
350\r
351 if ((Base == 16) && (Char >= 'a') && (Char <= 'f')) {\r
352 Char = (CHAR16) (Char - 'a' + 'A');\r
353 }\r
354\r
355 if ((Base == 16) && (Char >= 'A') && (Char <= 'F')) {\r
356 Value += (Char - 'A') + 10;\r
357 } else if ((Char >= '0') && (Char <= '9')) {\r
358 Value += (Char - '0');\r
359 } else {\r
360 //\r
361 // Unexpected Char encountered.\r
362 //\r
363 return 0;\r
364 }\r
365 }\r
366\r
367 *Status = EFI_SUCCESS;\r
368 return Value;\r
369}\r
370\r
371/**\r
372 Helper function called to transfer a string to an unsigned integer according to the map table.\r
373\r
374 @param[in] Str The pointer to the string.\r
375 @param[in] Map The pointer to the map table.\r
376\r
377 @return The integer value of converted Str. If not found, then return -1.\r
378**/\r
379UINT32\r
380MapStringToInteger (\r
381 IN CONST CHAR16 *Str,\r
382 IN STR2INT *Map\r
383 )\r
384{\r
385 STR2INT *Item;\r
386\r
387 for (Item = Map; Item->String != NULL; Item++) {\r
388 if (StrCmp (Item->String, Str) == 0) {\r
389 return Item->Integer;\r
390 }\r
391 }\r
392\r
393 return (UINT32) -1;\r
394}\r
395\r
396/**\r
397 Helper function called to transfer an unsigned integer to a string according to the map table.\r
398\r
399 @param[in] Integer The pointer to the string.\r
400 @param[in] Map The pointer to the map table.\r
401\r
402 @return The converted Str. If not found, then return NULL.\r
403**/\r
404CHAR16 *\r
405MapIntegerToString (\r
406 IN UINT32 Integer,\r
407 IN STR2INT *Map\r
408 )\r
409{\r
410 STR2INT *Item;\r
411\r
412 for (Item = Map; Item->String != NULL; Item++) {\r
413 if (Integer == Item->Integer) {\r
414 return Item->String;\r
415 }\r
416 }\r
417\r
418 return NULL;\r
419}\r