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