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