Helper.c 10 KB

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