]>
Commit | Line | Data |
---|---|---|
a3bcde70 HT |
1 | /** @file\r |
2 | The assistant function implementation for IpSecConfig application.\r | |
3 | \r | |
7a49cd08 | 4 | Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r |
a3bcde70 HT |
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 | |
36 | EFI_STATUS\r | |
37 | GetNumber (\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 | |
7a49cd08 | 96 | ASSERT (Map != NULL);\r |
a3bcde70 HT |
97 | Value64 = MapStringToInteger (ValueStr, Map);\r |
98 | if (Value64 == (UINT32) -1) {\r | |
99 | //\r | |
100 | // Cannot find the string in the map.\r | |
101 | //\r | |
102 | ShellPrintHiiEx (\r | |
103 | -1,\r | |
104 | -1,\r | |
105 | NULL,\r | |
106 | STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),\r | |
107 | mHiiHandle,\r | |
108 | mAppName,\r | |
109 | FlagStr,\r | |
110 | ValueStr\r | |
111 | );\r | |
112 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_ACCEPT_PARAMETERS), mHiiHandle);\r | |
113 | for (Index = 0; Map[Index].String != NULL; Index++) {\r | |
114 | Print (L" %s", Map[Index].String);\r | |
115 | }\r | |
116 | \r | |
117 | Print (L"\n");\r | |
118 | return EFI_INVALID_PARAMETER;\r | |
119 | }\r | |
120 | }\r | |
121 | \r | |
122 | CopyMem (ValuePtr, &Value64, ByteCount);\r | |
123 | return EFI_SUCCESS;\r | |
124 | }\r | |
125 | }\r | |
126 | \r | |
127 | /**\r | |
128 | Helper function called to convert a string containing an Ipv4 or Ipv6 Internet Protocol address\r | |
129 | into a proper address for the EFI_IP_ADDRESS structure.\r | |
130 | \r | |
131 | @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r | |
132 | @param[out] Ip The pointer to the EFI_IP_ADDRESS structure to contain the result.\r | |
133 | \r | |
134 | @retval EFI_SUCCESS The operation completed successfully.\r | |
135 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
136 | **/\r | |
137 | EFI_STATUS\r | |
138 | EfiInetAddr2 (\r | |
139 | IN CHAR16 *Ptr,\r | |
140 | OUT EFI_IP_ADDRESS *Ip\r | |
141 | )\r | |
142 | {\r | |
143 | EFI_STATUS Status;\r | |
144 | \r | |
145 | if ((Ptr == NULL) || (Ip == NULL)) {\r | |
146 | return EFI_INVALID_PARAMETER;\r | |
147 | }\r | |
148 | \r | |
149 | //\r | |
150 | // Parse the input address as Ipv4 Address first.\r | |
151 | //\r | |
152 | Status = NetLibStrToIp4 (Ptr, &Ip->v4);\r | |
153 | if (!EFI_ERROR (Status)) {\r | |
154 | return Status;\r | |
155 | }\r | |
156 | \r | |
157 | Status = NetLibStrToIp6 (Ptr, &Ip->v6);\r | |
158 | return Status;\r | |
159 | }\r | |
160 | \r | |
161 | /**\r | |
162 | Helper function called to calculate the prefix length associated with the string\r | |
163 | containing an Ipv4 or Ipv6 Internet Protocol address.\r | |
164 | \r | |
165 | @param[in] Ptr The pointer to the string containing an Ipv4 or Ipv6 Internet Protocol address.\r | |
166 | @param[out] Addr The pointer to the EFI_IP_ADDRESS_INFO structure to contain the result.\r | |
167 | \r | |
168 | @retval EFI_SUCCESS The operation completed successfully.\r | |
169 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
170 | @retval Others Other mistake case.\r | |
171 | **/\r | |
172 | EFI_STATUS\r | |
173 | EfiInetAddrRange (\r | |
174 | IN CHAR16 *Ptr,\r | |
175 | OUT EFI_IP_ADDRESS_INFO *Addr\r | |
176 | )\r | |
177 | {\r | |
178 | EFI_STATUS Status;\r | |
179 | \r | |
180 | if ((Ptr == NULL) || (Addr == NULL)) {\r | |
181 | return EFI_INVALID_PARAMETER;\r | |
182 | }\r | |
183 | \r | |
184 | Status = NetLibStrToIp4 (Ptr, &Addr->Address.v4);\r | |
185 | if (!EFI_ERROR (Status)) {\r | |
186 | if ((UINT32)(*Addr->Address.v4.Addr) == 0) {\r | |
187 | Addr->PrefixLength = 0;\r | |
188 | } else {\r | |
189 | Addr->PrefixLength = 32;\r | |
190 | }\r | |
191 | return Status;\r | |
192 | }\r | |
193 | \r | |
194 | Status = NetLibStrToIp6andPrefix (Ptr, &Addr->Address.v6, &Addr->PrefixLength);\r | |
195 | if (!EFI_ERROR (Status) && (Addr->PrefixLength == 0xFF)) {\r | |
196 | Addr->PrefixLength = 128;\r | |
197 | }\r | |
198 | \r | |
199 | return Status;\r | |
200 | }\r | |
201 | \r | |
202 | /**\r | |
203 | Helper function called to calculate the port range associated with the string.\r | |
204 | \r | |
205 | @param[in] Ptr The pointer to the string containing a port and range.\r | |
206 | @param[out] Port The pointer to the Port to contain the result.\r | |
207 | @param[out] PortRange The pointer to the PortRange to contain the result.\r | |
208 | \r | |
209 | @retval EFI_SUCCESS The operation completed successfully.\r | |
210 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
211 | @retval Others Other mistake case.\r | |
212 | **/\r | |
213 | EFI_STATUS\r | |
214 | EfiInetPortRange (\r | |
215 | IN CHAR16 *Ptr,\r | |
216 | OUT UINT16 *Port,\r | |
217 | OUT UINT16 *PortRange\r | |
218 | )\r | |
219 | {\r | |
220 | CHAR16 *BreakPtr;\r | |
221 | CHAR16 Ch;\r | |
222 | EFI_STATUS Status;\r | |
223 | \r | |
224 | for (BreakPtr = Ptr; (*BreakPtr != L'\0') && (*BreakPtr != L':'); BreakPtr++) {\r | |
225 | ;\r | |
226 | }\r | |
227 | \r | |
228 | Ch = *BreakPtr;\r | |
229 | *BreakPtr = L'\0';\r | |
230 | *Port = (UINT16) StrToUInteger (Ptr, &Status);\r | |
231 | *BreakPtr = Ch;\r | |
232 | if (EFI_ERROR (Status)) {\r | |
233 | return Status;\r | |
234 | }\r | |
235 | \r | |
236 | *PortRange = 0;\r | |
237 | if (*BreakPtr == L':') {\r | |
238 | BreakPtr++;\r | |
239 | *PortRange = (UINT16) StrToUInteger (BreakPtr, &Status);\r | |
240 | if (EFI_ERROR (Status)) {\r | |
241 | return Status;\r | |
242 | }\r | |
243 | \r | |
244 | if (*PortRange < *Port) {\r | |
245 | return EFI_INVALID_PARAMETER;\r | |
246 | }\r | |
247 | \r | |
248 | *PortRange = (UINT16) (*PortRange - *Port);\r | |
249 | }\r | |
250 | \r | |
251 | return EFI_SUCCESS;\r | |
252 | }\r | |
253 | \r | |
254 | /**\r | |
255 | Helper function called to transfer a string to an unsigned integer.\r | |
256 | \r | |
257 | @param[in] Str The pointer to the string.\r | |
258 | @param[out] Status The operation status.\r | |
259 | \r | |
260 | @return The integer value of converted Str.\r | |
261 | **/\r | |
262 | UINT64\r | |
263 | StrToUInteger (\r | |
264 | IN CONST CHAR16 *Str,\r | |
265 | OUT EFI_STATUS *Status\r | |
266 | )\r | |
267 | {\r | |
268 | UINT64 Value;\r | |
269 | UINT64 NewValue;\r | |
270 | CHAR16 *StrTail;\r | |
271 | CHAR16 Char;\r | |
272 | UINTN Base;\r | |
273 | UINTN Len;\r | |
274 | \r | |
275 | Base = 10;\r | |
276 | Value = 0;\r | |
277 | *Status = EFI_ABORTED;\r | |
278 | \r | |
279 | //\r | |
280 | // Skip leading white space.\r | |
281 | //\r | |
282 | while ((*Str != 0) && (*Str == ' ')) {\r | |
283 | Str++;\r | |
284 | }\r | |
285 | //\r | |
286 | // For NULL Str, just return.\r | |
287 | //\r | |
288 | if (*Str == 0) {\r | |
289 | return 0;\r | |
290 | }\r | |
291 | //\r | |
292 | // Skip white space in tail.\r | |
293 | //\r | |
294 | Len = StrLen (Str);\r | |
295 | StrTail = (CHAR16 *) (Str + Len - 1);\r | |
296 | while (*StrTail == ' ') {\r | |
297 | *StrTail = 0;\r | |
298 | StrTail--;\r | |
299 | }\r | |
300 | \r | |
301 | Len = StrTail - Str + 1;\r | |
302 | \r | |
303 | //\r | |
304 | // Check hex prefix '0x'.\r | |
305 | //\r | |
306 | if ((Len >= 2) && (*Str == '0') && ((*(Str + 1) == 'x') || (*(Str + 1) == 'X'))) {\r | |
307 | Str += 2;\r | |
308 | Len -= 2;\r | |
309 | Base = 16;\r | |
310 | }\r | |
311 | \r | |
312 | if (Len == 0) {\r | |
313 | return 0;\r | |
314 | }\r | |
315 | //\r | |
316 | // Convert the string to value.\r | |
317 | //\r | |
318 | for (; Str <= StrTail; Str++) {\r | |
319 | \r | |
320 | Char = *Str;\r | |
321 | \r | |
322 | if (Base == 16) {\r | |
323 | if (RShiftU64 (Value, 60) != 0) {\r | |
324 | //\r | |
325 | // Overflow here x16.\r | |
326 | //\r | |
327 | return 0;\r | |
328 | }\r | |
329 | \r | |
330 | NewValue = LShiftU64 (Value, 4);\r | |
331 | } else {\r | |
332 | if (RShiftU64 (Value, 61) != 0) {\r | |
333 | //\r | |
334 | // Overflow here x8.\r | |
335 | //\r | |
336 | return 0;\r | |
337 | }\r | |
338 | \r | |
339 | NewValue = LShiftU64 (Value, 3);\r | |
340 | Value = LShiftU64 (Value, 1);\r | |
341 | NewValue += Value;\r | |
342 | if (NewValue < Value) {\r | |
343 | //\r | |
344 | // Overflow here.\r | |
345 | //\r | |
346 | return 0;\r | |
347 | }\r | |
348 | }\r | |
349 | \r | |
350 | Value = NewValue;\r | |
351 | \r | |
352 | if ((Base == 16) && (Char >= 'a') && (Char <= 'f')) {\r | |
353 | Char = (CHAR16) (Char - 'a' + 'A');\r | |
354 | }\r | |
355 | \r | |
356 | if ((Base == 16) && (Char >= 'A') && (Char <= 'F')) {\r | |
357 | Value += (Char - 'A') + 10;\r | |
358 | } else if ((Char >= '0') && (Char <= '9')) {\r | |
359 | Value += (Char - '0');\r | |
360 | } else {\r | |
361 | //\r | |
362 | // Unexpected Char encountered.\r | |
363 | //\r | |
364 | return 0;\r | |
365 | }\r | |
366 | }\r | |
367 | \r | |
368 | *Status = EFI_SUCCESS;\r | |
369 | return Value;\r | |
370 | }\r | |
371 | \r | |
372 | /**\r | |
373 | Helper function called to transfer a string to an unsigned integer according to the map table.\r | |
374 | \r | |
375 | @param[in] Str The pointer to the string.\r | |
376 | @param[in] Map The pointer to the map table.\r | |
377 | \r | |
378 | @return The integer value of converted Str. If not found, then return -1.\r | |
379 | **/\r | |
380 | UINT32\r | |
381 | MapStringToInteger (\r | |
382 | IN CONST CHAR16 *Str,\r | |
383 | IN STR2INT *Map\r | |
384 | )\r | |
385 | {\r | |
386 | STR2INT *Item;\r | |
387 | \r | |
388 | for (Item = Map; Item->String != NULL; Item++) {\r | |
389 | if (StrCmp (Item->String, Str) == 0) {\r | |
390 | return Item->Integer;\r | |
391 | }\r | |
392 | }\r | |
393 | \r | |
394 | return (UINT32) -1;\r | |
395 | }\r | |
396 | \r | |
397 | /**\r | |
398 | Helper function called to transfer an unsigned integer to a string according to the map table.\r | |
399 | \r | |
400 | @param[in] Integer The pointer to the string.\r | |
401 | @param[in] Map The pointer to the map table.\r | |
402 | \r | |
403 | @return The converted Str. If not found, then return NULL.\r | |
404 | **/\r | |
405 | CHAR16 *\r | |
406 | MapIntegerToString (\r | |
407 | IN UINT32 Integer,\r | |
408 | IN STR2INT *Map\r | |
409 | )\r | |
410 | {\r | |
411 | STR2INT *Item;\r | |
412 | \r | |
413 | for (Item = Map; Item->String != NULL; Item++) {\r | |
414 | if (Integer == Item->Integer) {\r | |
415 | return Item->String;\r | |
416 | }\r | |
417 | }\r | |
418 | \r | |
419 | return NULL;\r | |
420 | }\r |