]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/String.c
MdePkg: Indicate UnicodeStrToAsciiStr/AsciiStrToUnicodeStr to be deprecated
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
CommitLineData
e1f414b6 1/** @file\r
2 Unicode and ASCII string primatives.\r
3\r
d52b9d86 4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
bb817c56 5 This program and the accompanying materials\r
e1f414b6 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
127010dd 8 http://opensource.org/licenses/bsd-license.php.\r
e1f414b6 9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
e1f414b6 15#include "BaseLibInternals.h"\r
16\r
1bb390f1 17#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
24dcb5e5 18\r
e1f414b6 19/**\r
1bb390f1
ED
20 [ATTENTION] This function will be deprecated for security reason.\r
21\r
e1f414b6 22 Copies one Null-terminated Unicode string to another Null-terminated Unicode\r
23 string and returns the new Unicode string.\r
24\r
25 This function copies the contents of the Unicode string Source to the Unicode\r
26 string Destination, and returns Destination. If Source and Destination\r
27 overlap, then the results are undefined.\r
28\r
29 If Destination is NULL, then ASSERT().\r
30 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
31 If Source is NULL, then ASSERT().\r
32 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
33 If Source and Destination overlap, then ASSERT().\r
34 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
dfbe9de9 35 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 36 Null-terminator, then ASSERT().\r
37\r
127010dd 38 @param Destination A pointer to a Null-terminated Unicode string.\r
39 @param Source A pointer to a Null-terminated Unicode string.\r
e1f414b6 40\r
9aa049d9 41 @return Destination.\r
e1f414b6 42\r
43**/\r
44CHAR16 *\r
45EFIAPI\r
46StrCpy (\r
47 OUT CHAR16 *Destination,\r
48 IN CONST CHAR16 *Source\r
49 )\r
50{\r
51 CHAR16 *ReturnValue;\r
52\r
53 //\r
54 // Destination cannot be NULL\r
55 //\r
56 ASSERT (Destination != NULL);\r
24dcb5e5 57 ASSERT (((UINTN) Destination & BIT0) == 0);\r
e1f414b6 58\r
59 //\r
60 // Destination and source cannot overlap\r
61 //\r
62 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
63 ASSERT ((UINTN)(Source - Destination) > StrLen (Source));\r
64\r
65 ReturnValue = Destination;\r
42eedea9 66 while (*Source != 0) {\r
e1f414b6 67 *(Destination++) = *(Source++);\r
68 }\r
69 *Destination = 0;\r
70 return ReturnValue;\r
71}\r
72\r
73/**\r
1bb390f1
ED
74 [ATTENTION] This function will be deprecated for security reason.\r
75\r
9aa049d9 76 Copies up to a specified length from one Null-terminated Unicode string to \r
77 another Null-terminated Unicode string and returns the new Unicode string.\r
e1f414b6 78\r
79 This function copies the contents of the Unicode string Source to the Unicode\r
80 string Destination, and returns Destination. At most, Length Unicode\r
81 characters are copied from Source to Destination. If Length is 0, then\r
82 Destination is returned unmodified. If Length is greater that the number of\r
83 Unicode characters in Source, then Destination is padded with Null Unicode\r
84 characters. If Source and Destination overlap, then the results are\r
85 undefined.\r
86\r
87 If Length > 0 and Destination is NULL, then ASSERT().\r
88 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
89 If Length > 0 and Source is NULL, then ASSERT().\r
77f863ee 90 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
e1f414b6 91 If Source and Destination overlap, then ASSERT().\r
53e96610 92 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
93 PcdMaximumUnicodeStringLength, then ASSERT().\r
e1f414b6 94 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
dfbe9de9 95 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
53e96610 96 then ASSERT().\r
e1f414b6 97\r
127010dd 98 @param Destination A pointer to a Null-terminated Unicode string.\r
99 @param Source A pointer to a Null-terminated Unicode string.\r
2fc59a00 100 @param Length The maximum number of Unicode characters to copy.\r
e1f414b6 101\r
9aa049d9 102 @return Destination.\r
e1f414b6 103\r
104**/\r
105CHAR16 *\r
106EFIAPI\r
107StrnCpy (\r
108 OUT CHAR16 *Destination,\r
109 IN CONST CHAR16 *Source,\r
110 IN UINTN Length\r
111 )\r
112{\r
113 CHAR16 *ReturnValue;\r
114\r
115 if (Length == 0) {\r
116 return Destination;\r
117 }\r
118\r
119 //\r
120 // Destination cannot be NULL if Length is not zero\r
121 //\r
122 ASSERT (Destination != NULL);\r
24dcb5e5 123 ASSERT (((UINTN) Destination & BIT0) == 0);\r
e1f414b6 124\r
125 //\r
126 // Destination and source cannot overlap\r
e1f414b6 127 //\r
128 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
129 ASSERT ((UINTN)(Source - Destination) >= Length);\r
130\r
dfbe9de9 131 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
132 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
133 }\r
134\r
e1f414b6 135 ReturnValue = Destination;\r
136\r
137 while ((*Source != L'\0') && (Length > 0)) {\r
138 *(Destination++) = *(Source++);\r
139 Length--;\r
140 }\r
141\r
142 ZeroMem (Destination, Length * sizeof (*Destination));\r
143 return ReturnValue;\r
144}\r
1bb390f1 145#endif\r
e1f414b6 146\r
147/**\r
148 Returns the length of a Null-terminated Unicode string.\r
149\r
150 This function returns the number of Unicode characters in the Null-terminated\r
151 Unicode string specified by String.\r
152\r
153 If String is NULL, then ASSERT().\r
154 If String is not aligned on a 16-bit boundary, then ASSERT().\r
155 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
dfbe9de9 156 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 157 Null-terminator, then ASSERT().\r
158\r
127010dd 159 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 160\r
161 @return The length of String.\r
162\r
163**/\r
164UINTN\r
165EFIAPI\r
166StrLen (\r
167 IN CONST CHAR16 *String\r
168 )\r
169{\r
170 UINTN Length;\r
171\r
172 ASSERT (String != NULL);\r
24dcb5e5 173 ASSERT (((UINTN) String & BIT0) == 0);\r
e1f414b6 174\r
175 for (Length = 0; *String != L'\0'; String++, Length++) {\r
176 //\r
177 // If PcdMaximumUnicodeStringLength is not zero,\r
178 // length should not more than PcdMaximumUnicodeStringLength\r
179 //\r
180 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
181 ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));\r
182 }\r
183 }\r
184 return Length;\r
185}\r
186\r
187/**\r
188 Returns the size of a Null-terminated Unicode string in bytes, including the\r
189 Null terminator.\r
190\r
9aa049d9 191 This function returns the size, in bytes, of the Null-terminated Unicode string \r
192 specified by String.\r
e1f414b6 193\r
194 If String is NULL, then ASSERT().\r
195 If String is not aligned on a 16-bit boundary, then ASSERT().\r
196 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
dfbe9de9 197 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 198 Null-terminator, then ASSERT().\r
199\r
127010dd 200 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 201\r
9aa049d9 202 @return The size of String.\r
e1f414b6 203\r
204**/\r
205UINTN\r
206EFIAPI\r
207StrSize (\r
208 IN CONST CHAR16 *String\r
209 )\r
210{\r
211 return (StrLen (String) + 1) * sizeof (*String);\r
212}\r
213\r
214/**\r
215 Compares two Null-terminated Unicode strings, and returns the difference\r
216 between the first mismatched Unicode characters.\r
217\r
218 This function compares the Null-terminated Unicode string FirstString to the\r
219 Null-terminated Unicode string SecondString. If FirstString is identical to\r
220 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
221 mismatched Unicode character in SecondString subtracted from the first\r
222 mismatched Unicode character in FirstString.\r
223\r
224 If FirstString is NULL, then ASSERT().\r
225 If FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
226 If SecondString is NULL, then ASSERT().\r
227 If SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
228 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
dfbe9de9 229 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 230 Null-terminator, then ASSERT().\r
231 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
dfbe9de9 232 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 233 Null-terminator, then ASSERT().\r
234\r
127010dd 235 @param FirstString A pointer to a Null-terminated Unicode string.\r
236 @param SecondString A pointer to a Null-terminated Unicode string.\r
e1f414b6 237\r
1106ffe1 238 @retval 0 FirstString is identical to SecondString.\r
9aa049d9 239 @return others FirstString is not identical to SecondString.\r
e1f414b6 240\r
241**/\r
242INTN\r
243EFIAPI\r
244StrCmp (\r
245 IN CONST CHAR16 *FirstString,\r
246 IN CONST CHAR16 *SecondString\r
247 )\r
248{\r
249 //\r
250 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength\r
251 //\r
252 ASSERT (StrSize (FirstString) != 0);\r
253 ASSERT (StrSize (SecondString) != 0);\r
254\r
255 while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {\r
256 FirstString++;\r
257 SecondString++;\r
258 }\r
259 return *FirstString - *SecondString;\r
260}\r
261\r
262/**\r
9aa049d9 263 Compares up to a specified length the contents of two Null-terminated Unicode strings,\r
264 and returns the difference between the first mismatched Unicode characters.\r
2fc60b70 265 \r
e1f414b6 266 This function compares the Null-terminated Unicode string FirstString to the\r
267 Null-terminated Unicode string SecondString. At most, Length Unicode\r
268 characters will be compared. If Length is 0, then 0 is returned. If\r
269 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
270 value returned is the first mismatched Unicode character in SecondString\r
271 subtracted from the first mismatched Unicode character in FirstString.\r
272\r
273 If Length > 0 and FirstString is NULL, then ASSERT().\r
77f863ee 274 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
e1f414b6 275 If Length > 0 and SecondString is NULL, then ASSERT().\r
77f863ee 276 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
53e96610 277 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than\r
278 PcdMaximumUnicodeStringLength, then ASSERT().\r
279 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than\r
280 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
281 then ASSERT().\r
282 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than\r
283 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
dfbe9de9 284 then ASSERT().\r
e1f414b6 285\r
127010dd 286 @param FirstString A pointer to a Null-terminated Unicode string.\r
287 @param SecondString A pointer to a Null-terminated Unicode string.\r
2fc59a00 288 @param Length The maximum number of Unicode characters to compare.\r
e1f414b6 289\r
1106ffe1 290 @retval 0 FirstString is identical to SecondString.\r
9aa049d9 291 @return others FirstString is not identical to SecondString.\r
e1f414b6 292\r
293**/\r
294INTN\r
295EFIAPI\r
296StrnCmp (\r
297 IN CONST CHAR16 *FirstString,\r
298 IN CONST CHAR16 *SecondString,\r
299 IN UINTN Length\r
300 )\r
301{\r
2bfb6009 302 if (Length == 0) {\r
e1f414b6 303 return 0;\r
304 }\r
305\r
306 //\r
307 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
308 // Length tests are performed inside StrLen().\r
309 //\r
310 ASSERT (StrSize (FirstString) != 0);\r
311 ASSERT (StrSize (SecondString) != 0);\r
312\r
dfbe9de9 313 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
314 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
315 }\r
316\r
e1f414b6 317 while ((*FirstString != L'\0') &&\r
318 (*FirstString == *SecondString) &&\r
319 (Length > 1)) {\r
320 FirstString++;\r
321 SecondString++;\r
322 Length--;\r
323 }\r
324\r
325 return *FirstString - *SecondString;\r
326}\r
327\r
1bb390f1
ED
328#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
329\r
e1f414b6 330/**\r
1bb390f1
ED
331 [ATTENTION] This function will be deprecated for security reason.\r
332\r
e1f414b6 333 Concatenates one Null-terminated Unicode string to another Null-terminated\r
334 Unicode string, and returns the concatenated Unicode string.\r
335\r
336 This function concatenates two Null-terminated Unicode strings. The contents\r
337 of Null-terminated Unicode string Source are concatenated to the end of\r
338 Null-terminated Unicode string Destination. The Null-terminated concatenated\r
339 Unicode String is returned. If Source and Destination overlap, then the\r
340 results are undefined.\r
341\r
342 If Destination is NULL, then ASSERT().\r
77f863ee 343 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
e1f414b6 344 If Source is NULL, then ASSERT().\r
77f863ee 345 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
e1f414b6 346 If Source and Destination overlap, then ASSERT().\r
347 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
dfbe9de9 348 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 349 Null-terminator, then ASSERT().\r
350 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
dfbe9de9 351 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 352 Null-terminator, then ASSERT().\r
353 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
354 and Source results in a Unicode string with more than\r
dfbe9de9 355 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 356 Null-terminator, then ASSERT().\r
357\r
127010dd 358 @param Destination A pointer to a Null-terminated Unicode string.\r
359 @param Source A pointer to a Null-terminated Unicode string.\r
e1f414b6 360\r
9aa049d9 361 @return Destination.\r
e1f414b6 362\r
363**/\r
364CHAR16 *\r
365EFIAPI\r
366StrCat (\r
367 IN OUT CHAR16 *Destination,\r
368 IN CONST CHAR16 *Source\r
369 )\r
370{\r
371 StrCpy (Destination + StrLen (Destination), Source);\r
372\r
373 //\r
374 // Size of the resulting string should never be zero.\r
375 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
376 //\r
377 ASSERT (StrSize (Destination) != 0);\r
378 return Destination;\r
379}\r
380\r
381/**\r
1bb390f1
ED
382 [ATTENTION] This function will be deprecated for security reason.\r
383\r
9aa049d9 384 Concatenates up to a specified length one Null-terminated Unicode to the end \r
385 of another Null-terminated Unicode string, and returns the concatenated \r
e1f414b6 386 Unicode string.\r
387\r
388 This function concatenates two Null-terminated Unicode strings. The contents\r
389 of Null-terminated Unicode string Source are concatenated to the end of\r
390 Null-terminated Unicode string Destination, and Destination is returned. At\r
391 most, Length Unicode characters are concatenated from Source to the end of\r
392 Destination, and Destination is always Null-terminated. If Length is 0, then\r
393 Destination is returned unmodified. If Source and Destination overlap, then\r
394 the results are undefined.\r
395\r
396 If Destination is NULL, then ASSERT().\r
397 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
398 If Length > 0 and Source is NULL, then ASSERT().\r
399 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
400 If Source and Destination overlap, then ASSERT().\r
53e96610 401 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
402 PcdMaximumUnicodeStringLength, then ASSERT().\r
e1f414b6 403 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
dfbe9de9 404 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 405 Null-terminator, then ASSERT().\r
406 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
dfbe9de9 407 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
e1f414b6 408 Null-terminator, then ASSERT().\r
409 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
53e96610 410 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength\r
411 Unicode characters, not including the Null-terminator, then ASSERT().\r
e1f414b6 412\r
127010dd 413 @param Destination A pointer to a Null-terminated Unicode string.\r
414 @param Source A pointer to a Null-terminated Unicode string.\r
2fc59a00 415 @param Length The maximum number of Unicode characters to concatenate from\r
e1f414b6 416 Source.\r
417\r
9aa049d9 418 @return Destination.\r
e1f414b6 419\r
420**/\r
421CHAR16 *\r
422EFIAPI\r
423StrnCat (\r
424 IN OUT CHAR16 *Destination,\r
425 IN CONST CHAR16 *Source,\r
426 IN UINTN Length\r
427 )\r
428{\r
8f635c36 429 UINTN DestinationLen;\r
430\r
431 DestinationLen = StrLen (Destination);\r
432 StrnCpy (Destination + DestinationLen, Source, Length);\r
433 Destination[DestinationLen + Length] = L'\0';\r
e1f414b6 434\r
435 //\r
436 // Size of the resulting string should never be zero.\r
437 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
438 //\r
439 ASSERT (StrSize (Destination) != 0);\r
440 return Destination;\r
441}\r
1bb390f1 442#endif\r
e1f414b6 443\r
444/**\r
9aa049d9 445 Returns the first occurrence of a Null-terminated Unicode sub-string\r
e1f414b6 446 in a Null-terminated Unicode string.\r
447\r
9aa049d9 448 This function scans the contents of the Null-terminated Unicode string\r
449 specified by String and returns the first occurrence of SearchString.\r
450 If SearchString is not found in String, then NULL is returned. If\r
451 the length of SearchString is zero, then String is\r
e1f414b6 452 returned.\r
9aa049d9 453\r
e1f414b6 454 If String is NULL, then ASSERT().\r
455 If String is not aligned on a 16-bit boundary, then ASSERT().\r
456 If SearchString is NULL, then ASSERT().\r
457 If SearchString is not aligned on a 16-bit boundary, then ASSERT().\r
458\r
9aa049d9 459 If PcdMaximumUnicodeStringLength is not zero, and SearchString\r
460 or String contains more than PcdMaximumUnicodeStringLength Unicode\r
dfbe9de9 461 characters, not including the Null-terminator, then ASSERT().\r
e1f414b6 462\r
127010dd 463 @param String A pointer to a Null-terminated Unicode string.\r
464 @param SearchString A pointer to a Null-terminated Unicode string to search for.\r
e1f414b6 465\r
9aa049d9 466 @retval NULL If the SearchString does not appear in String.\r
467 @return others If there is a match.\r
e1f414b6 468\r
469**/\r
470CHAR16 *\r
471EFIAPI\r
472StrStr (\r
2fc60b70 473 IN CONST CHAR16 *String,\r
474 IN CONST CHAR16 *SearchString\r
e1f414b6 475 )\r
476{\r
477 CONST CHAR16 *FirstMatch;\r
478 CONST CHAR16 *SearchStringTmp;\r
479\r
e1f414b6 480 //\r
4df26661 481 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
482 // Length tests are performed inside StrLen().\r
e1f414b6 483 //\r
4df26661 484 ASSERT (StrSize (String) != 0);\r
485 ASSERT (StrSize (SearchString) != 0);\r
e1f414b6 486\r
62e71e2f 487 if (*SearchString == L'\0') {\r
faeb3214 488 return (CHAR16 *) String;\r
62e71e2f 489 }\r
490\r
491 while (*String != L'\0') {\r
e1f414b6 492 SearchStringTmp = SearchString;\r
493 FirstMatch = String;\r
494 \r
495 while ((*String == *SearchStringTmp) \r
62e71e2f 496 && (*String != L'\0')) {\r
e1f414b6 497 String++;\r
498 SearchStringTmp++;\r
499 } \r
500 \r
62e71e2f 501 if (*SearchStringTmp == L'\0') {\r
e1f414b6 502 return (CHAR16 *) FirstMatch;\r
503 }\r
504\r
62e71e2f 505 if (*String == L'\0') {\r
506 return NULL;\r
e1f414b6 507 }\r
62e71e2f 508\r
509 String = FirstMatch + 1;\r
e1f414b6 510 }\r
511\r
512 return NULL;\r
513}\r
514\r
515/**\r
516 Check if a Unicode character is a decimal character.\r
517\r
518 This internal function checks if a Unicode character is a \r
519 decimal character. The valid decimal character is from\r
520 L'0' to L'9'.\r
521\r
e1f414b6 522 @param Char The character to check against.\r
523\r
524 @retval TRUE If the Char is a decmial character.\r
24dcb5e5 525 @retval FALSE If the Char is not a decmial character.\r
e1f414b6 526\r
527**/\r
e1f414b6 528BOOLEAN\r
42eedea9 529EFIAPI\r
e1f414b6 530InternalIsDecimalDigitCharacter (\r
531 IN CHAR16 Char\r
532 )\r
533{\r
534 return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r
535}\r
536\r
537/**\r
538 Convert a Unicode character to upper case only if \r
539 it maps to a valid small-case ASCII character.\r
540\r
541 This internal function only deal with Unicode character\r
24dcb5e5 542 which maps to a valid small-case ASCII character, i.e.\r
e1f414b6 543 L'a' to L'z'. For other Unicode character, the input character\r
544 is returned directly.\r
545\r
e1f414b6 546 @param Char The character to convert.\r
547\r
548 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
549 @retval Unchanged Otherwise.\r
550\r
551**/\r
e1f414b6 552CHAR16\r
42eedea9 553EFIAPI\r
e1f414b6 554InternalCharToUpper (\r
555 IN CHAR16 Char\r
556 )\r
557{\r
558 if (Char >= L'a' && Char <= L'z') {\r
559 return (CHAR16) (Char - (L'a' - L'A'));\r
560 }\r
561\r
562 return Char;\r
563}\r
564\r
565/**\r
566 Convert a Unicode character to numerical value.\r
567\r
568 This internal function only deal with Unicode character\r
569 which maps to a valid hexadecimal ASII character, i.e.\r
570 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
571 Unicode character, the value returned does not make sense.\r
572\r
573 @param Char The character to convert.\r
574\r
24dcb5e5 575 @return The numerical value converted.\r
e1f414b6 576\r
577**/\r
e1f414b6 578UINTN\r
42eedea9 579EFIAPI\r
e1f414b6 580InternalHexCharToUintn (\r
581 IN CHAR16 Char\r
582 )\r
583{\r
584 if (InternalIsDecimalDigitCharacter (Char)) {\r
585 return Char - L'0';\r
586 }\r
587\r
588 return (UINTN) (10 + InternalCharToUpper (Char) - L'A');\r
589}\r
590\r
591/**\r
592 Check if a Unicode character is a hexadecimal character.\r
593\r
594 This internal function checks if a Unicode character is a \r
595 decimal character. The valid hexadecimal character is \r
596 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
597\r
598\r
599 @param Char The character to check against.\r
600\r
601 @retval TRUE If the Char is a hexadecmial character.\r
24dcb5e5 602 @retval FALSE If the Char is not a hexadecmial character.\r
e1f414b6 603\r
604**/\r
e1f414b6 605BOOLEAN\r
42eedea9 606EFIAPI\r
e1f414b6 607InternalIsHexaDecimalDigitCharacter (\r
608 IN CHAR16 Char\r
609 )\r
610{\r
611\r
612 return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||\r
613 (Char >= L'A' && Char <= L'F') ||\r
614 (Char >= L'a' && Char <= L'f'));\r
615}\r
616\r
617/**\r
9aa049d9 618 Convert a Null-terminated Unicode decimal string to a value of\r
e1f414b6 619 type UINTN.\r
620\r
9aa049d9 621 This function returns a value of type UINTN by interpreting the contents\r
622 of the Unicode string specified by String as a decimal number. The format\r
e1f414b6 623 of the input Unicode string String is:\r
9aa049d9 624\r
2fe241a2 625 [spaces] [decimal digits].\r
9aa049d9 626\r
627 The valid decimal digit character is in the range [0-9]. The\r
628 function will ignore the pad space, which includes spaces or\r
629 tab characters, before [decimal digits]. The running zero in the\r
630 beginning of [decimal digits] will be ignored. Then, the function\r
631 stops at the first character that is a not a valid decimal character\r
632 or a Null-terminator, whichever one comes first.\r
633\r
e1f414b6 634 If String is NULL, then ASSERT().\r
9aa049d9 635 If String is not aligned in a 16-bit boundary, then ASSERT().\r
e1f414b6 636 If String has only pad spaces, then 0 is returned.\r
9aa049d9 637 If String has no pad spaces or valid decimal digits,\r
e1f414b6 638 then 0 is returned.\r
9aa049d9 639 If the number represented by String overflows according\r
e1f414b6 640 to the range defined by UINTN, then ASSERT().\r
9aa049d9 641\r
642 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
dfbe9de9 643 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
e1f414b6 644 the Null-terminator, then ASSERT().\r
645\r
127010dd 646 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 647\r
9aa049d9 648 @retval Value translated from String.\r
e1f414b6 649\r
650**/\r
651UINTN\r
652EFIAPI\r
653StrDecimalToUintn (\r
2fc60b70 654 IN CONST CHAR16 *String\r
e1f414b6 655 )\r
656{\r
657 UINTN Result;\r
658 \r
4df26661 659 //\r
660 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
661 // Length tests are performed inside StrLen().\r
662 //\r
663 ASSERT (StrSize (String) != 0);\r
e1f414b6 664\r
665 //\r
666 // Ignore the pad spaces (space or tab)\r
667 //\r
955c32f2 668 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 669 String++;\r
670 }\r
671\r
672 //\r
673 // Ignore leading Zeros after the spaces\r
674 //\r
955c32f2 675 while (*String == L'0') {\r
e1f414b6 676 String++;\r
677 }\r
678\r
679 Result = 0;\r
680\r
681 while (InternalIsDecimalDigitCharacter (*String)) {\r
682 //\r
683 // If the number represented by String overflows according \r
684 // to the range defined by UINTN, then ASSERT().\r
685 //\r
6efe9461 686 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
e1f414b6 687\r
688 Result = Result * 10 + (*String - L'0');\r
689 String++;\r
690 }\r
691 \r
692 return Result;\r
693}\r
694\r
695\r
696/**\r
9aa049d9 697 Convert a Null-terminated Unicode decimal string to a value of\r
e1f414b6 698 type UINT64.\r
699\r
9aa049d9 700 This function returns a value of type UINT64 by interpreting the contents\r
701 of the Unicode string specified by String as a decimal number. The format\r
e1f414b6 702 of the input Unicode string String is:\r
9aa049d9 703\r
2fe241a2 704 [spaces] [decimal digits].\r
9aa049d9 705\r
706 The valid decimal digit character is in the range [0-9]. The\r
707 function will ignore the pad space, which includes spaces or\r
708 tab characters, before [decimal digits]. The running zero in the\r
709 beginning of [decimal digits] will be ignored. Then, the function\r
710 stops at the first character that is a not a valid decimal character\r
711 or a Null-terminator, whichever one comes first.\r
712\r
e1f414b6 713 If String is NULL, then ASSERT().\r
9aa049d9 714 If String is not aligned in a 16-bit boundary, then ASSERT().\r
e1f414b6 715 If String has only pad spaces, then 0 is returned.\r
9aa049d9 716 If String has no pad spaces or valid decimal digits,\r
e1f414b6 717 then 0 is returned.\r
9aa049d9 718 If the number represented by String overflows according\r
e1f414b6 719 to the range defined by UINT64, then ASSERT().\r
9aa049d9 720\r
721 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
dfbe9de9 722 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
e1f414b6 723 the Null-terminator, then ASSERT().\r
724\r
127010dd 725 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 726\r
9aa049d9 727 @retval Value translated from String.\r
e1f414b6 728\r
729**/\r
730UINT64\r
731EFIAPI\r
732StrDecimalToUint64 (\r
2fc60b70 733 IN CONST CHAR16 *String\r
e1f414b6 734 )\r
735{\r
736 UINT64 Result;\r
737 \r
4df26661 738 //\r
739 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
740 // Length tests are performed inside StrLen().\r
741 //\r
742 ASSERT (StrSize (String) != 0);\r
e1f414b6 743\r
744 //\r
745 // Ignore the pad spaces (space or tab)\r
746 //\r
955c32f2 747 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 748 String++;\r
749 }\r
750\r
751 //\r
752 // Ignore leading Zeros after the spaces\r
753 //\r
955c32f2 754 while (*String == L'0') {\r
e1f414b6 755 String++;\r
756 }\r
757\r
758 Result = 0;\r
759\r
760 while (InternalIsDecimalDigitCharacter (*String)) {\r
761 //\r
762 // If the number represented by String overflows according \r
763 // to the range defined by UINTN, then ASSERT().\r
764 //\r
6efe9461 765 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
e1f414b6 766\r
767 Result = MultU64x32 (Result, 10) + (*String - L'0');\r
768 String++;\r
769 }\r
770 \r
771 return Result;\r
772}\r
773\r
774/**\r
775 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.\r
776\r
9aa049d9 777 This function returns a value of type UINTN by interpreting the contents\r
778 of the Unicode string specified by String as a hexadecimal number.\r
e1f414b6 779 The format of the input Unicode string String is:\r
9aa049d9 780\r
781 [spaces][zeros][x][hexadecimal digits].\r
782\r
783 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
784 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
785 If "x" appears in the input string, it must be prefixed with at least one 0.\r
786 The function will ignore the pad space, which includes spaces or tab characters,\r
787 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
788 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
789 first valid hexadecimal digit. Then, the function stops at the first character that is\r
e1f414b6 790 a not a valid hexadecimal character or NULL, whichever one comes first.\r
791\r
792 If String is NULL, then ASSERT().\r
793 If String is not aligned in a 16-bit boundary, then ASSERT().\r
794 If String has only pad spaces, then zero is returned.\r
9aa049d9 795 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
e1f414b6 796 then zero is returned.\r
9aa049d9 797 If the number represented by String overflows according to the range defined by\r
e1f414b6 798 UINTN, then ASSERT().\r
799\r
9aa049d9 800 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
dfbe9de9 801 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
e1f414b6 802 then ASSERT().\r
803\r
127010dd 804 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 805\r
9aa049d9 806 @retval Value translated from String.\r
e1f414b6 807\r
808**/\r
809UINTN\r
810EFIAPI\r
811StrHexToUintn (\r
2fc60b70 812 IN CONST CHAR16 *String\r
e1f414b6 813 )\r
814{\r
815 UINTN Result;\r
816\r
4df26661 817 //\r
818 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
819 // Length tests are performed inside StrLen().\r
820 //\r
821 ASSERT (StrSize (String) != 0);\r
e1f414b6 822 \r
823 //\r
824 // Ignore the pad spaces (space or tab) \r
825 //\r
955c32f2 826 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 827 String++;\r
828 }\r
829\r
830 //\r
831 // Ignore leading Zeros after the spaces\r
832 //\r
955c32f2 833 while (*String == L'0') {\r
e1f414b6 834 String++;\r
835 }\r
836\r
837 if (InternalCharToUpper (*String) == L'X') {\r
955c32f2 838 if (*(String - 1) != L'0') {\r
e1f414b6 839 return 0;\r
840 }\r
841 //\r
842 // Skip the 'X'\r
843 //\r
844 String++;\r
845 }\r
846\r
847 Result = 0;\r
848 \r
849 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
850 //\r
851 // If the Hex Number represented by String overflows according \r
852 // to the range defined by UINTN, then ASSERT().\r
853 //\r
6efe9461 854 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
e1f414b6 855\r
856 Result = (Result << 4) + InternalHexCharToUintn (*String);\r
857 String++;\r
858 }\r
859\r
860 return Result;\r
861}\r
862\r
863\r
864/**\r
865 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
866\r
9aa049d9 867 This function returns a value of type UINT64 by interpreting the contents\r
868 of the Unicode string specified by String as a hexadecimal number.\r
869 The format of the input Unicode string String is\r
870\r
871 [spaces][zeros][x][hexadecimal digits].\r
872\r
873 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
874 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
875 If "x" appears in the input string, it must be prefixed with at least one 0.\r
876 The function will ignore the pad space, which includes spaces or tab characters,\r
877 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
878 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
879 first valid hexadecimal digit. Then, the function stops at the first character that is\r
e1f414b6 880 a not a valid hexadecimal character or NULL, whichever one comes first.\r
881\r
882 If String is NULL, then ASSERT().\r
883 If String is not aligned in a 16-bit boundary, then ASSERT().\r
884 If String has only pad spaces, then zero is returned.\r
9aa049d9 885 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
e1f414b6 886 then zero is returned.\r
9aa049d9 887 If the number represented by String overflows according to the range defined by\r
e1f414b6 888 UINT64, then ASSERT().\r
889\r
9aa049d9 890 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
dfbe9de9 891 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
e1f414b6 892 then ASSERT().\r
893\r
127010dd 894 @param String A pointer to a Null-terminated Unicode string.\r
e1f414b6 895\r
9aa049d9 896 @retval Value translated from String.\r
e1f414b6 897\r
2fc60b70 898**/\r
e1f414b6 899UINT64\r
900EFIAPI\r
901StrHexToUint64 (\r
2fc60b70 902 IN CONST CHAR16 *String\r
e1f414b6 903 )\r
904{\r
905 UINT64 Result;\r
906\r
4df26661 907 //\r
908 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
909 // Length tests are performed inside StrLen().\r
910 //\r
911 ASSERT (StrSize (String) != 0);\r
e1f414b6 912 \r
913 //\r
914 // Ignore the pad spaces (space or tab) \r
915 //\r
955c32f2 916 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 917 String++;\r
918 }\r
919\r
920 //\r
921 // Ignore leading Zeros after the spaces\r
922 //\r
955c32f2 923 while (*String == L'0') {\r
e1f414b6 924 String++;\r
925 }\r
926\r
927 if (InternalCharToUpper (*String) == L'X') {\r
955c32f2 928 ASSERT (*(String - 1) == L'0');\r
929 if (*(String - 1) != L'0') {\r
e1f414b6 930 return 0;\r
931 }\r
932 //\r
933 // Skip the 'X'\r
934 //\r
935 String++;\r
936 }\r
937\r
938 Result = 0;\r
939 \r
940 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
941 //\r
942 // If the Hex Number represented by String overflows according \r
943 // to the range defined by UINTN, then ASSERT().\r
944 //\r
6efe9461 945 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
e1f414b6 946\r
947 Result = LShiftU64 (Result, 4);\r
948 Result = Result + InternalHexCharToUintn (*String);\r
949 String++;\r
950 }\r
951\r
952 return Result;\r
953}\r
954\r
955/**\r
956 Check if a ASCII character is a decimal character.\r
957\r
958 This internal function checks if a Unicode character is a \r
959 decimal character. The valid decimal character is from\r
960 '0' to '9'.\r
961\r
962 @param Char The character to check against.\r
963\r
964 @retval TRUE If the Char is a decmial character.\r
24dcb5e5 965 @retval FALSE If the Char is not a decmial character.\r
e1f414b6 966\r
967**/\r
e1f414b6 968BOOLEAN\r
42eedea9 969EFIAPI\r
e1f414b6 970InternalAsciiIsDecimalDigitCharacter (\r
971 IN CHAR8 Char\r
972 )\r
973{\r
974 return (BOOLEAN) (Char >= '0' && Char <= '9');\r
975}\r
976\r
977/**\r
978 Check if a ASCII character is a hexadecimal character.\r
979\r
980 This internal function checks if a ASCII character is a \r
981 decimal character. The valid hexadecimal character is \r
982 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
983\r
984\r
985 @param Char The character to check against.\r
986\r
987 @retval TRUE If the Char is a hexadecmial character.\r
24dcb5e5 988 @retval FALSE If the Char is not a hexadecmial character.\r
e1f414b6 989\r
990**/\r
e1f414b6 991BOOLEAN\r
42eedea9 992EFIAPI\r
e1f414b6 993InternalAsciiIsHexaDecimalDigitCharacter (\r
994 IN CHAR8 Char\r
995 )\r
996{\r
997\r
998 return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||\r
999 (Char >= 'A' && Char <= 'F') ||\r
1000 (Char >= 'a' && Char <= 'f'));\r
1001}\r
1002\r
415aa2f1
SZ
1003#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1004\r
e1f414b6 1005/**\r
415aa2f1
SZ
1006 [ATTENTION] This function is deprecated for security reason.\r
1007\r
9aa049d9 1008 Convert a Null-terminated Unicode string to a Null-terminated\r
e1f414b6 1009 ASCII string and returns the ASCII string.\r
9aa049d9 1010\r
1011 This function converts the content of the Unicode string Source\r
1012 to the ASCII string Destination by copying the lower 8 bits of\r
1013 each Unicode character. It returns Destination.\r
1014\r
d3e0289c 1015 The caller is responsible to make sure Destination points to a buffer with size\r
1016 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.\r
1017\r
9aa049d9 1018 If any Unicode characters in Source contain non-zero value in\r
1019 the upper 8 bits, then ASSERT().\r
e1f414b6 1020\r
1021 If Destination is NULL, then ASSERT().\r
1022 If Source is NULL, then ASSERT().\r
1023 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
1024 If Source and Destination overlap, then ASSERT().\r
1025\r
9aa049d9 1026 If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
dfbe9de9 1027 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
e1f414b6 1028 the Null-terminator, then ASSERT().\r
9aa049d9 1029\r
1030 If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
dfbe9de9 1031 than PcdMaximumAsciiStringLength Unicode characters, not including the\r
e1f414b6 1032 Null-terminator, then ASSERT().\r
1033\r
127010dd 1034 @param Source A pointer to a Null-terminated Unicode string.\r
1035 @param Destination A pointer to a Null-terminated ASCII string.\r
e1f414b6 1036\r
9aa049d9 1037 @return Destination.\r
e1f414b6 1038\r
1039**/\r
1040CHAR8 *\r
1041EFIAPI\r
1042UnicodeStrToAsciiStr (\r
2fc60b70 1043 IN CONST CHAR16 *Source,\r
1044 OUT CHAR8 *Destination\r
e1f414b6 1045 )\r
1046{\r
4df26661 1047 CHAR8 *ReturnValue;\r
1048\r
e1f414b6 1049 ASSERT (Destination != NULL);\r
4df26661 1050\r
1051 //\r
1052 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
1053 // Length tests are performed inside StrLen().\r
1054 //\r
1055 ASSERT (StrSize (Source) != 0);\r
e1f414b6 1056\r
1057 //\r
1058 // Source and Destination should not overlap\r
1059 //\r
d52b9d86 1060 ASSERT ((UINTN) (Destination - (CHAR8 *) Source) >= StrSize (Source));\r
e1f414b6 1061 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
1062\r
e1f414b6 1063\r
4df26661 1064 ReturnValue = Destination;\r
e1f414b6 1065 while (*Source != '\0') {\r
1066 //\r
1067 // If any Unicode characters in Source contain \r
1068 // non-zero value in the upper 8 bits, then ASSERT().\r
1069 //\r
1070 ASSERT (*Source < 0x100);\r
1071 *(Destination++) = (CHAR8) *(Source++);\r
1072 }\r
1073\r
1074 *Destination = '\0';\r
4df26661 1075\r
1076 //\r
1077 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
1078 // Length tests are performed inside AsciiStrLen().\r
1079 //\r
1080 ASSERT (AsciiStrSize (ReturnValue) != 0);\r
1081\r
1082 return ReturnValue;\r
e1f414b6 1083}\r
1084\r
e1f414b6 1085/**\r
1bb390f1
ED
1086 [ATTENTION] This function will be deprecated for security reason.\r
1087\r
e1f414b6 1088 Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
1089 string and returns the new ASCII string.\r
1090\r
1091 This function copies the contents of the ASCII string Source to the ASCII\r
1092 string Destination, and returns Destination. If Source and Destination\r
1093 overlap, then the results are undefined.\r
1094\r
1095 If Destination is NULL, then ASSERT().\r
1096 If Source is NULL, then ASSERT().\r
1097 If Source and Destination overlap, then ASSERT().\r
1098 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
dfbe9de9 1099 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1100 then ASSERT().\r
1101\r
127010dd 1102 @param Destination A pointer to a Null-terminated ASCII string.\r
1103 @param Source A pointer to a Null-terminated ASCII string.\r
e1f414b6 1104\r
9aa049d9 1105 @return Destination\r
e1f414b6 1106\r
1107**/\r
1108CHAR8 *\r
1109EFIAPI\r
1110AsciiStrCpy (\r
1111 OUT CHAR8 *Destination,\r
1112 IN CONST CHAR8 *Source\r
1113 )\r
1114{\r
1115 CHAR8 *ReturnValue;\r
1116\r
1117 //\r
1118 // Destination cannot be NULL\r
1119 //\r
1120 ASSERT (Destination != NULL);\r
1121\r
1122 //\r
1123 // Destination and source cannot overlap\r
1124 //\r
1125 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1126 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r
1127\r
1128 ReturnValue = Destination;\r
42eedea9 1129 while (*Source != 0) {\r
e1f414b6 1130 *(Destination++) = *(Source++);\r
1131 }\r
1132 *Destination = 0;\r
1133 return ReturnValue;\r
1134}\r
1135\r
1136/**\r
1bb390f1
ED
1137 [ATTENTION] This function will be deprecated for security reason.\r
1138\r
9aa049d9 1139 Copies up to a specified length one Null-terminated ASCII string to another \r
1140 Null-terminated ASCII string and returns the new ASCII string.\r
e1f414b6 1141\r
1142 This function copies the contents of the ASCII string Source to the ASCII\r
1143 string Destination, and returns Destination. At most, Length ASCII characters\r
1144 are copied from Source to Destination. If Length is 0, then Destination is\r
1145 returned unmodified. If Length is greater that the number of ASCII characters\r
1146 in Source, then Destination is padded with Null ASCII characters. If Source\r
1147 and Destination overlap, then the results are undefined.\r
1148\r
1149 If Destination is NULL, then ASSERT().\r
1150 If Source is NULL, then ASSERT().\r
1151 If Source and Destination overlap, then ASSERT().\r
53e96610 1152 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1153 PcdMaximumAsciiStringLength, then ASSERT().\r
e1f414b6 1154 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
dfbe9de9 1155 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
53e96610 1156 then ASSERT().\r
e1f414b6 1157\r
127010dd 1158 @param Destination A pointer to a Null-terminated ASCII string.\r
1159 @param Source A pointer to a Null-terminated ASCII string.\r
2fc59a00 1160 @param Length The maximum number of ASCII characters to copy.\r
e1f414b6 1161\r
9aa049d9 1162 @return Destination\r
e1f414b6 1163\r
1164**/\r
1165CHAR8 *\r
1166EFIAPI\r
1167AsciiStrnCpy (\r
1168 OUT CHAR8 *Destination,\r
1169 IN CONST CHAR8 *Source,\r
1170 IN UINTN Length\r
1171 )\r
1172{\r
1173 CHAR8 *ReturnValue;\r
1174\r
2bfb6009 1175 if (Length == 0) {\r
e1f414b6 1176 return Destination;\r
1177 }\r
1178\r
1179 //\r
1180 // Destination cannot be NULL\r
1181 //\r
1182 ASSERT (Destination != NULL);\r
1183\r
1184 //\r
1185 // Destination and source cannot overlap\r
1186 //\r
1187 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1188 ASSERT ((UINTN)(Source - Destination) >= Length);\r
1189\r
dfbe9de9 1190 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1191 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1192 }\r
1193\r
e1f414b6 1194 ReturnValue = Destination;\r
1195\r
42eedea9 1196 while (*Source != 0 && Length > 0) {\r
e1f414b6 1197 *(Destination++) = *(Source++);\r
1198 Length--;\r
1199 }\r
1200\r
1201 ZeroMem (Destination, Length * sizeof (*Destination));\r
1202 return ReturnValue;\r
1203}\r
1bb390f1 1204#endif\r
e1f414b6 1205\r
1206/**\r
1207 Returns the length of a Null-terminated ASCII string.\r
1208\r
1209 This function returns the number of ASCII characters in the Null-terminated\r
1210 ASCII string specified by String.\r
1211\r
9aa049d9 1212 If Length > 0 and Destination is NULL, then ASSERT().\r
1213 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1214 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
dfbe9de9 1215 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1216 then ASSERT().\r
1217\r
127010dd 1218 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1219\r
1220 @return The length of String.\r
1221\r
1222**/\r
1223UINTN\r
1224EFIAPI\r
1225AsciiStrLen (\r
1226 IN CONST CHAR8 *String\r
1227 )\r
1228{\r
1229 UINTN Length;\r
1230\r
1231 ASSERT (String != NULL);\r
1232\r
1233 for (Length = 0; *String != '\0'; String++, Length++) {\r
1234 //\r
1235 // If PcdMaximumUnicodeStringLength is not zero,\r
1236 // length should not more than PcdMaximumUnicodeStringLength\r
1237 //\r
1238 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1239 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r
1240 }\r
1241 }\r
1242 return Length;\r
1243}\r
1244\r
1245/**\r
1246 Returns the size of a Null-terminated ASCII string in bytes, including the\r
1247 Null terminator.\r
1248\r
1249 This function returns the size, in bytes, of the Null-terminated ASCII string\r
1250 specified by String.\r
1251\r
1252 If String is NULL, then ASSERT().\r
1253 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
dfbe9de9 1254 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1255 then ASSERT().\r
1256\r
127010dd 1257 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1258\r
1259 @return The size of String.\r
1260\r
1261**/\r
1262UINTN\r
1263EFIAPI\r
1264AsciiStrSize (\r
1265 IN CONST CHAR8 *String\r
1266 )\r
1267{\r
1268 return (AsciiStrLen (String) + 1) * sizeof (*String);\r
1269}\r
1270\r
1271/**\r
1272 Compares two Null-terminated ASCII strings, and returns the difference\r
1273 between the first mismatched ASCII characters.\r
1274\r
1275 This function compares the Null-terminated ASCII string FirstString to the\r
1276 Null-terminated ASCII string SecondString. If FirstString is identical to\r
1277 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
1278 mismatched ASCII character in SecondString subtracted from the first\r
1279 mismatched ASCII character in FirstString.\r
1280\r
1281 If FirstString is NULL, then ASSERT().\r
1282 If SecondString is NULL, then ASSERT().\r
1283 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
dfbe9de9 1284 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1285 then ASSERT().\r
1286 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
dfbe9de9 1287 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
e1f414b6 1288 Null-terminator, then ASSERT().\r
1289\r
127010dd 1290 @param FirstString A pointer to a Null-terminated ASCII string.\r
1291 @param SecondString A pointer to a Null-terminated ASCII string.\r
e1f414b6 1292\r
9aa049d9 1293 @retval ==0 FirstString is identical to SecondString.\r
1294 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1295\r
1296**/\r
1297INTN\r
1298EFIAPI\r
1299AsciiStrCmp (\r
1300 IN CONST CHAR8 *FirstString,\r
1301 IN CONST CHAR8 *SecondString\r
1302 )\r
1303{\r
1304 //\r
1305 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1306 //\r
1307 ASSERT (AsciiStrSize (FirstString));\r
1308 ASSERT (AsciiStrSize (SecondString));\r
1309\r
1310 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r
1311 FirstString++;\r
1312 SecondString++;\r
1313 }\r
1314\r
1315 return *FirstString - *SecondString;\r
1316}\r
1317\r
1318/**\r
24dcb5e5 1319 Converts a lowercase Ascii character to upper one.\r
e1f414b6 1320\r
1321 If Chr is lowercase Ascii character, then converts it to upper one.\r
1322\r
1323 If Value >= 0xA0, then ASSERT().\r
1324 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
1325\r
42eedea9 1326 @param Chr one Ascii character\r
e1f414b6 1327\r
1328 @return The uppercase value of Ascii character \r
1329\r
1330**/\r
e1f414b6 1331CHAR8\r
42eedea9 1332EFIAPI\r
80151e53 1333InternalBaseLibAsciiToUpper (\r
e1f414b6 1334 IN CHAR8 Chr\r
1335 )\r
1336{\r
1337 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);\r
1338}\r
1339\r
1340/**\r
1341 Convert a ASCII character to numerical value.\r
1342\r
1343 This internal function only deal with Unicode character\r
1344 which maps to a valid hexadecimal ASII character, i.e.\r
1345 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r
1346 ASCII character, the value returned does not make sense.\r
1347\r
1348 @param Char The character to convert.\r
1349\r
24dcb5e5 1350 @return The numerical value converted.\r
e1f414b6 1351\r
1352**/\r
e1f414b6 1353UINTN\r
42eedea9 1354EFIAPI\r
e1f414b6 1355InternalAsciiHexCharToUintn (\r
1356 IN CHAR8 Char\r
1357 )\r
1358{\r
1359 if (InternalIsDecimalDigitCharacter (Char)) {\r
1360 return Char - '0';\r
1361 }\r
1362\r
80151e53 1363 return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
e1f414b6 1364}\r
1365\r
1366\r
1367/**\r
1368 Performs a case insensitive comparison of two Null-terminated ASCII strings,\r
1369 and returns the difference between the first mismatched ASCII characters.\r
1370\r
1371 This function performs a case insensitive comparison of the Null-terminated\r
1372 ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r
1373 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
1374 value returned is the first mismatched lower case ASCII character in\r
1375 SecondString subtracted from the first mismatched lower case ASCII character\r
1376 in FirstString.\r
1377\r
1378 If FirstString is NULL, then ASSERT().\r
1379 If SecondString is NULL, then ASSERT().\r
1380 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
dfbe9de9 1381 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1382 then ASSERT().\r
1383 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
dfbe9de9 1384 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
e1f414b6 1385 Null-terminator, then ASSERT().\r
1386\r
127010dd 1387 @param FirstString A pointer to a Null-terminated ASCII string.\r
1388 @param SecondString A pointer to a Null-terminated ASCII string.\r
e1f414b6 1389\r
9aa049d9 1390 @retval ==0 FirstString is identical to SecondString using case insensitive\r
1106ffe1 1391 comparisons.\r
9aa049d9 1392 @retval !=0 FirstString is not identical to SecondString using case\r
1393 insensitive comparisons.\r
e1f414b6 1394\r
1395**/\r
1396INTN\r
1397EFIAPI\r
1398AsciiStriCmp (\r
1399 IN CONST CHAR8 *FirstString,\r
1400 IN CONST CHAR8 *SecondString\r
1401 )\r
1402{\r
1403 CHAR8 UpperFirstString;\r
1404 CHAR8 UpperSecondString;\r
1405\r
1406 //\r
1407 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1408 //\r
1409 ASSERT (AsciiStrSize (FirstString));\r
1410 ASSERT (AsciiStrSize (SecondString));\r
1411\r
80151e53
LG
1412 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1413 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
e1f414b6 1414 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
1415 FirstString++;\r
1416 SecondString++;\r
80151e53
LG
1417 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1418 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
e1f414b6 1419 }\r
1420\r
1421 return UpperFirstString - UpperSecondString;\r
1422}\r
1423\r
1424/**\r
1425 Compares two Null-terminated ASCII strings with maximum lengths, and returns\r
1426 the difference between the first mismatched ASCII characters.\r
1427\r
1428 This function compares the Null-terminated ASCII string FirstString to the\r
1429 Null-terminated ASCII string SecondString. At most, Length ASCII characters\r
1430 will be compared. If Length is 0, then 0 is returned. If FirstString is\r
1431 identical to SecondString, then 0 is returned. Otherwise, the value returned\r
1432 is the first mismatched ASCII character in SecondString subtracted from the\r
1433 first mismatched ASCII character in FirstString.\r
1434\r
9aa049d9 1435 If Length > 0 and FirstString is NULL, then ASSERT().\r
1436 If Length > 0 and SecondString is NULL, then ASSERT().\r
53e96610 1437 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1438 PcdMaximumAsciiStringLength, then ASSERT().\r
1439 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than\r
dfbe9de9 1440 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1441 then ASSERT().\r
53e96610 1442 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than\r
dfbe9de9 1443 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
53e96610 1444 then ASSERT().\r
e1f414b6 1445\r
127010dd 1446 @param FirstString A pointer to a Null-terminated ASCII string.\r
1447 @param SecondString A pointer to a Null-terminated ASCII string.\r
2fc59a00 1448 @param Length The maximum number of ASCII characters for compare.\r
2fc60b70 1449 \r
9aa049d9 1450 @retval ==0 FirstString is identical to SecondString.\r
1451 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1452\r
1453**/\r
1454INTN\r
1455EFIAPI\r
1456AsciiStrnCmp (\r
1457 IN CONST CHAR8 *FirstString,\r
1458 IN CONST CHAR8 *SecondString,\r
1459 IN UINTN Length\r
1460 )\r
1461{\r
2bfb6009 1462 if (Length == 0) {\r
e1f414b6 1463 return 0;\r
1464 }\r
1465\r
1466 //\r
1467 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1468 //\r
1469 ASSERT (AsciiStrSize (FirstString));\r
1470 ASSERT (AsciiStrSize (SecondString));\r
1471\r
dfbe9de9 1472 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1473 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1474 }\r
1475\r
e1f414b6 1476 while ((*FirstString != '\0') &&\r
1477 (*FirstString == *SecondString) &&\r
1478 (Length > 1)) {\r
1479 FirstString++;\r
1480 SecondString++;\r
1481 Length--;\r
1482 }\r
1483 return *FirstString - *SecondString;\r
1484}\r
1485\r
1bb390f1
ED
1486#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1487\r
e1f414b6 1488/**\r
1bb390f1
ED
1489 [ATTENTION] This function will be deprecated for security reason.\r
1490\r
e1f414b6 1491 Concatenates one Null-terminated ASCII string to another Null-terminated\r
1492 ASCII string, and returns the concatenated ASCII string.\r
1493\r
1494 This function concatenates two Null-terminated ASCII strings. The contents of\r
1495 Null-terminated ASCII string Source are concatenated to the end of Null-\r
1496 terminated ASCII string Destination. The Null-terminated concatenated ASCII\r
1497 String is returned.\r
1498\r
1499 If Destination is NULL, then ASSERT().\r
1500 If Source is NULL, then ASSERT().\r
1501 If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
dfbe9de9 1502 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1503 then ASSERT().\r
1504 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
dfbe9de9 1505 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1506 then ASSERT().\r
1507 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
1508 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1509 ASCII characters, then ASSERT().\r
1510\r
127010dd 1511 @param Destination A pointer to a Null-terminated ASCII string.\r
1512 @param Source A pointer to a Null-terminated ASCII string.\r
e1f414b6 1513\r
9aa049d9 1514 @return Destination\r
e1f414b6 1515\r
1516**/\r
1517CHAR8 *\r
1518EFIAPI\r
1519AsciiStrCat (\r
1520 IN OUT CHAR8 *Destination,\r
1521 IN CONST CHAR8 *Source\r
1522 )\r
1523{\r
1524 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r
1525\r
1526 //\r
1527 // Size of the resulting string should never be zero.\r
1528 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1529 //\r
1530 ASSERT (AsciiStrSize (Destination) != 0);\r
1531 return Destination;\r
1532}\r
1533\r
1534/**\r
1bb390f1
ED
1535 [ATTENTION] This function will be deprecated for security reason.\r
1536\r
9aa049d9 1537 Concatenates up to a specified length one Null-terminated ASCII string to \r
1538 the end of another Null-terminated ASCII string, and returns the \r
1539 concatenated ASCII string.\r
e1f414b6 1540\r
1541 This function concatenates two Null-terminated ASCII strings. The contents\r
1542 of Null-terminated ASCII string Source are concatenated to the end of Null-\r
1543 terminated ASCII string Destination, and Destination is returned. At most,\r
1544 Length ASCII characters are concatenated from Source to the end of\r
1545 Destination, and Destination is always Null-terminated. If Length is 0, then\r
1546 Destination is returned unmodified. If Source and Destination overlap, then\r
1547 the results are undefined.\r
1548\r
9aa049d9 1549 If Length > 0 and Destination is NULL, then ASSERT().\r
1550 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1551 If Source and Destination overlap, then ASSERT().\r
53e96610 1552 If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
1553 PcdMaximumAsciiStringLength, then ASSERT().\r
e1f414b6 1554 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
dfbe9de9 1555 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1556 then ASSERT().\r
1557 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
dfbe9de9 1558 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1559 then ASSERT().\r
1560 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
1561 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
53e96610 1562 ASCII characters, not including the Null-terminator, then ASSERT().\r
e1f414b6 1563\r
127010dd 1564 @param Destination A pointer to a Null-terminated ASCII string.\r
1565 @param Source A pointer to a Null-terminated ASCII string.\r
2fc59a00 1566 @param Length The maximum number of ASCII characters to concatenate from\r
e1f414b6 1567 Source.\r
1568\r
9aa049d9 1569 @return Destination\r
e1f414b6 1570\r
1571**/\r
1572CHAR8 *\r
1573EFIAPI\r
1574AsciiStrnCat (\r
1575 IN OUT CHAR8 *Destination,\r
1576 IN CONST CHAR8 *Source,\r
1577 IN UINTN Length\r
1578 )\r
1579{\r
8f635c36 1580 UINTN DestinationLen;\r
1581\r
1582 DestinationLen = AsciiStrLen (Destination);\r
1583 AsciiStrnCpy (Destination + DestinationLen, Source, Length);\r
1584 Destination[DestinationLen + Length] = '\0';\r
e1f414b6 1585\r
1586 //\r
1587 // Size of the resulting string should never be zero.\r
1588 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1589 //\r
1590 ASSERT (AsciiStrSize (Destination) != 0);\r
1591 return Destination;\r
1592}\r
1bb390f1 1593#endif\r
e1f414b6 1594\r
1595/**\r
9aa049d9 1596 Returns the first occurrence of a Null-terminated ASCII sub-string\r
e1f414b6 1597 in a Null-terminated ASCII string.\r
1598\r
9aa049d9 1599 This function scans the contents of the ASCII string specified by String\r
1600 and returns the first occurrence of SearchString. If SearchString is not\r
1601 found in String, then NULL is returned. If the length of SearchString is zero,\r
e1f414b6 1602 then String is returned.\r
9aa049d9 1603\r
e1f414b6 1604 If String is NULL, then ASSERT().\r
1605 If SearchString is NULL, then ASSERT().\r
1606\r
9aa049d9 1607 If PcdMaximumAsciiStringLength is not zero, and SearchString or\r
1608 String contains more than PcdMaximumAsciiStringLength Unicode characters\r
e1f414b6 1609 not including the Null-terminator, then ASSERT().\r
1610\r
127010dd 1611 @param String A pointer to a Null-terminated ASCII string.\r
1612 @param SearchString A pointer to a Null-terminated ASCII string to search for.\r
e1f414b6 1613\r
1614 @retval NULL If the SearchString does not appear in String.\r
9aa049d9 1615 @retval others If there is a match return the first occurrence of SearchingString.\r
1616 If the length of SearchString is zero,return String.\r
e1f414b6 1617\r
1618**/\r
1619CHAR8 *\r
1620EFIAPI\r
1621AsciiStrStr (\r
2fc60b70 1622 IN CONST CHAR8 *String,\r
1623 IN CONST CHAR8 *SearchString\r
e1f414b6 1624 )\r
1625{\r
1626 CONST CHAR8 *FirstMatch;\r
1627 CONST CHAR8 *SearchStringTmp;\r
1628\r
e1f414b6 1629 //\r
4df26661 1630 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
e1f414b6 1631 //\r
4df26661 1632 ASSERT (AsciiStrSize (String) != 0);\r
1633 ASSERT (AsciiStrSize (SearchString) != 0);\r
e1f414b6 1634\r
62e71e2f 1635 if (*SearchString == '\0') {\r
faeb3214 1636 return (CHAR8 *) String;\r
62e71e2f 1637 }\r
1638\r
e1f414b6 1639 while (*String != '\0') {\r
1640 SearchStringTmp = SearchString;\r
1641 FirstMatch = String;\r
1642 \r
1643 while ((*String == *SearchStringTmp) \r
e1f414b6 1644 && (*String != '\0')) {\r
1645 String++;\r
1646 SearchStringTmp++;\r
1647 } \r
1648 \r
1649 if (*SearchStringTmp == '\0') {\r
1650 return (CHAR8 *) FirstMatch;\r
1651 }\r
1652\r
62e71e2f 1653 if (*String == '\0') {\r
1654 return NULL;\r
e1f414b6 1655 }\r
1656\r
62e71e2f 1657 String = FirstMatch + 1;\r
e1f414b6 1658 }\r
1659\r
1660 return NULL;\r
1661}\r
1662\r
1663/**\r
9aa049d9 1664 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1665 UINTN.\r
1666\r
9aa049d9 1667 This function returns a value of type UINTN by interpreting the contents\r
1668 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1669 ASCII string String is:\r
9aa049d9 1670\r
e1f414b6 1671 [spaces] [decimal digits].\r
9aa049d9 1672\r
1673 The valid decimal digit character is in the range [0-9]. The function will\r
1674 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1675 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1676 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1677 Null-terminator, whichever on comes first.\r
9aa049d9 1678\r
e1f414b6 1679 If String has only pad spaces, then 0 is returned.\r
1680 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1681 If the number represented by String overflows according to the range defined by\r
e1f414b6 1682 UINTN, then ASSERT().\r
1683 If String is NULL, then ASSERT().\r
9aa049d9 1684 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1685 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1686 then ASSERT().\r
1687\r
127010dd 1688 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1689\r
9aa049d9 1690 @retval Value translated from String.\r
e1f414b6 1691\r
1692**/\r
1693UINTN\r
1694EFIAPI\r
1695AsciiStrDecimalToUintn (\r
4df26661 1696 IN CONST CHAR8 *String\r
e1f414b6 1697 )\r
1698{\r
1699 UINTN Result;\r
1700 \r
4df26661 1701 //\r
1702 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1703 //\r
1704 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1705\r
1706 //\r
1707 // Ignore the pad spaces (space or tab)\r
1708 //\r
955c32f2 1709 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1710 String++;\r
1711 }\r
1712\r
1713 //\r
1714 // Ignore leading Zeros after the spaces\r
1715 //\r
955c32f2 1716 while (*String == '0') {\r
e1f414b6 1717 String++;\r
1718 }\r
1719\r
1720 Result = 0;\r
1721\r
1722 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1723 //\r
1724 // If the number represented by String overflows according \r
1725 // to the range defined by UINTN, then ASSERT().\r
1726 //\r
6efe9461 1727 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
e1f414b6 1728\r
1729 Result = Result * 10 + (*String - '0');\r
1730 String++;\r
1731 }\r
1732 \r
1733 return Result;\r
1734}\r
1735\r
1736\r
1737/**\r
9aa049d9 1738 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1739 UINT64.\r
1740\r
9aa049d9 1741 This function returns a value of type UINT64 by interpreting the contents\r
1742 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1743 ASCII string String is:\r
9aa049d9 1744\r
e1f414b6 1745 [spaces] [decimal digits].\r
9aa049d9 1746\r
1747 The valid decimal digit character is in the range [0-9]. The function will\r
1748 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1749 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1750 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1751 Null-terminator, whichever on comes first.\r
9aa049d9 1752\r
e1f414b6 1753 If String has only pad spaces, then 0 is returned.\r
1754 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1755 If the number represented by String overflows according to the range defined by\r
e1f414b6 1756 UINT64, then ASSERT().\r
1757 If String is NULL, then ASSERT().\r
9aa049d9 1758 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1759 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1760 then ASSERT().\r
1761\r
127010dd 1762 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1763\r
9aa049d9 1764 @retval Value translated from String.\r
e1f414b6 1765\r
1766**/\r
1767UINT64\r
1768EFIAPI\r
1769AsciiStrDecimalToUint64 (\r
2fc60b70 1770 IN CONST CHAR8 *String\r
e1f414b6 1771 )\r
1772{\r
1773 UINT64 Result;\r
1774 \r
4df26661 1775 //\r
1776 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1777 //\r
1778 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1779\r
1780 //\r
1781 // Ignore the pad spaces (space or tab)\r
1782 //\r
955c32f2 1783 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1784 String++;\r
1785 }\r
1786\r
1787 //\r
1788 // Ignore leading Zeros after the spaces\r
1789 //\r
955c32f2 1790 while (*String == '0') {\r
e1f414b6 1791 String++;\r
1792 }\r
1793\r
1794 Result = 0;\r
1795\r
1796 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1797 //\r
1798 // If the number represented by String overflows according \r
1799 // to the range defined by UINTN, then ASSERT().\r
1800 //\r
6efe9461 1801 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
e1f414b6 1802\r
1803 Result = MultU64x32 (Result, 10) + (*String - '0');\r
1804 String++;\r
1805 }\r
1806 \r
1807 return Result;\r
1808}\r
1809\r
1810/**\r
1811 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1812\r
9aa049d9 1813 This function returns a value of type UINTN by interpreting the contents of\r
1814 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1815 string String is:\r
9aa049d9 1816\r
e1f414b6 1817 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1818\r
1819 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1820 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1821 appears in the input string, it must be prefixed with at least one 0. The function\r
1822 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1823 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1824 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1825 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1826 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1827\r
e1f414b6 1828 If String has only pad spaces, then 0 is returned.\r
1829 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1830 0 is returned.\r
1831\r
9aa049d9 1832 If the number represented by String overflows according to the range defined by UINTN,\r
e1f414b6 1833 then ASSERT().\r
1834 If String is NULL, then ASSERT().\r
9aa049d9 1835 If PcdMaximumAsciiStringLength is not zero,\r
1836 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1837 the Null-terminator, then ASSERT().\r
1838\r
127010dd 1839 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1840\r
9aa049d9 1841 @retval Value translated from String.\r
e1f414b6 1842\r
1843**/\r
1844UINTN\r
1845EFIAPI\r
1846AsciiStrHexToUintn (\r
2fc60b70 1847 IN CONST CHAR8 *String\r
e1f414b6 1848 )\r
1849{\r
1850 UINTN Result;\r
1851\r
4df26661 1852 //\r
1853 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1854 //\r
1855 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1856 \r
1857 //\r
1858 // Ignore the pad spaces (space or tab) \r
1859 //\r
955c32f2 1860 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1861 String++;\r
1862 }\r
1863\r
1864 //\r
1865 // Ignore leading Zeros after the spaces\r
1866 //\r
955c32f2 1867 while (*String == '0') {\r
e1f414b6 1868 String++;\r
1869 }\r
1870\r
80151e53 1871 if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
955c32f2 1872 ASSERT (*(String - 1) == '0');\r
1873 if (*(String - 1) != '0') {\r
e1f414b6 1874 return 0;\r
1875 }\r
1876 //\r
1877 // Skip the 'X'\r
1878 //\r
1879 String++;\r
1880 }\r
1881\r
1882 Result = 0;\r
1883 \r
1884 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1885 //\r
1886 // If the Hex Number represented by String overflows according \r
1887 // to the range defined by UINTN, then ASSERT().\r
1888 //\r
6efe9461 1889 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
e1f414b6 1890\r
1891 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
1892 String++;\r
1893 }\r
1894\r
1895 return Result;\r
1896}\r
1897\r
1898\r
1899/**\r
1900 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1901\r
9aa049d9 1902 This function returns a value of type UINT64 by interpreting the contents of\r
1903 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1904 string String is:\r
9aa049d9 1905\r
e1f414b6 1906 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1907\r
1908 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1909 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1910 appears in the input string, it must be prefixed with at least one 0. The function\r
1911 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1912 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1913 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1914 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1915 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1916\r
e1f414b6 1917 If String has only pad spaces, then 0 is returned.\r
1918 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1919 0 is returned.\r
1920\r
9aa049d9 1921 If the number represented by String overflows according to the range defined by UINT64,\r
e1f414b6 1922 then ASSERT().\r
1923 If String is NULL, then ASSERT().\r
9aa049d9 1924 If PcdMaximumAsciiStringLength is not zero,\r
1925 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1926 the Null-terminator, then ASSERT().\r
1927\r
127010dd 1928 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1929\r
9aa049d9 1930 @retval Value translated from String.\r
e1f414b6 1931\r
1932**/\r
1933UINT64\r
1934EFIAPI\r
1935AsciiStrHexToUint64 (\r
2fc60b70 1936 IN CONST CHAR8 *String\r
e1f414b6 1937 )\r
1938{\r
1939 UINT64 Result;\r
1940\r
4df26661 1941 //\r
1942 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1943 //\r
1944 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1945 \r
1946 //\r
1947 // Ignore the pad spaces (space or tab) and leading Zeros\r
1948 //\r
1949 //\r
1950 // Ignore the pad spaces (space or tab) \r
1951 //\r
955c32f2 1952 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1953 String++;\r
1954 }\r
1955\r
1956 //\r
1957 // Ignore leading Zeros after the spaces\r
1958 //\r
955c32f2 1959 while (*String == '0') {\r
e1f414b6 1960 String++;\r
1961 }\r
1962\r
80151e53 1963 if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
955c32f2 1964 ASSERT (*(String - 1) == '0');\r
1965 if (*(String - 1) != '0') {\r
e1f414b6 1966 return 0;\r
1967 }\r
1968 //\r
1969 // Skip the 'X'\r
1970 //\r
1971 String++;\r
1972 }\r
1973\r
1974 Result = 0;\r
1975 \r
1976 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1977 //\r
1978 // If the Hex Number represented by String overflows according \r
1979 // to the range defined by UINTN, then ASSERT().\r
1980 //\r
6efe9461 1981 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
e1f414b6 1982\r
1983 Result = LShiftU64 (Result, 4);\r
1984 Result = Result + InternalAsciiHexCharToUintn (*String);\r
1985 String++;\r
1986 }\r
1987\r
1988 return Result;\r
1989}\r
1990\r
415aa2f1 1991#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
e1f414b6 1992\r
1993/**\r
415aa2f1
SZ
1994 [ATTENTION] This function is deprecated for security reason.\r
1995\r
9aa049d9 1996 Convert one Null-terminated ASCII string to a Null-terminated\r
e1f414b6 1997 Unicode string and returns the Unicode string.\r
1998\r
9aa049d9 1999 This function converts the contents of the ASCII string Source to the Unicode\r
2000 string Destination, and returns Destination. The function terminates the\r
2001 Unicode string Destination by appending a Null-terminator character at the end.\r
2002 The caller is responsible to make sure Destination points to a buffer with size\r
e1f414b6 2003 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
9aa049d9 2004\r
e1f414b6 2005 If Destination is NULL, then ASSERT().\r
2006 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
2007 If Source is NULL, then ASSERT().\r
2008 If Source and Destination overlap, then ASSERT().\r
9aa049d9 2009 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
2010 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 2011 then ASSERT().\r
9aa049d9 2012 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
2013 PcdMaximumUnicodeStringLength ASCII characters not including the\r
e1f414b6 2014 Null-terminator, then ASSERT().\r
2015\r
127010dd 2016 @param Source A pointer to a Null-terminated ASCII string.\r
2017 @param Destination A pointer to a Null-terminated Unicode string.\r
e1f414b6 2018\r
9aa049d9 2019 @return Destination.\r
e1f414b6 2020\r
2021**/\r
2022CHAR16 *\r
2023EFIAPI\r
2024AsciiStrToUnicodeStr (\r
4df26661 2025 IN CONST CHAR8 *Source,\r
2026 OUT CHAR16 *Destination\r
e1f414b6 2027 )\r
2028{\r
4df26661 2029 CHAR16 *ReturnValue;\r
2030\r
e1f414b6 2031 ASSERT (Destination != NULL);\r
e1f414b6 2032\r
2033 //\r
4df26661 2034 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
e1f414b6 2035 //\r
4df26661 2036 ASSERT (AsciiStrSize (Source) != 0);\r
e1f414b6 2037\r
2038 //\r
4df26661 2039 // Source and Destination should not overlap\r
e1f414b6 2040 //\r
4df26661 2041 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
d52b9d86
SZ
2042 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) >= (AsciiStrSize (Source) * sizeof (CHAR16)));\r
2043\r
e1f414b6 2044\r
4df26661 2045 ReturnValue = Destination;\r
e1f414b6 2046 while (*Source != '\0') {\r
2047 *(Destination++) = (CHAR16) *(Source++);\r
2048 }\r
2049 //\r
2050 // End the Destination with a NULL.\r
2051 //\r
2052 *Destination = '\0';\r
2053\r
4df26661 2054 //\r
2055 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
2056 //\r
2057 ASSERT (StrSize (ReturnValue) != 0);\r
2058\r
2059 return ReturnValue;\r
e1f414b6 2060}\r
2061\r
415aa2f1
SZ
2062#endif\r
2063\r
e1f414b6 2064/**\r
2065 Converts an 8-bit value to an 8-bit BCD value.\r
2066\r
2067 Converts the 8-bit value specified by Value to BCD. The BCD value is\r
2068 returned.\r
2069\r
2070 If Value >= 100, then ASSERT().\r
2071\r
2072 @param Value The 8-bit value to convert to BCD. Range 0..99.\r
2073\r
9aa049d9 2074 @return The BCD value.\r
e1f414b6 2075\r
2076**/\r
2077UINT8\r
2078EFIAPI\r
2079DecimalToBcd8 (\r
2080 IN UINT8 Value\r
2081 )\r
2082{\r
2083 ASSERT (Value < 100);\r
2084 return (UINT8) (((Value / 10) << 4) | (Value % 10));\r
2085}\r
2086\r
2087/**\r
2088 Converts an 8-bit BCD value to an 8-bit value.\r
2089\r
2090 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
2091 value is returned.\r
2092\r
2093 If Value >= 0xA0, then ASSERT().\r
2094 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
2095\r
2096 @param Value The 8-bit BCD value to convert to an 8-bit value.\r
2097\r
9aa049d9 2098 @return The 8-bit value is returned.\r
e1f414b6 2099\r
2100**/\r
2101UINT8\r
2102EFIAPI\r
2103BcdToDecimal8 (\r
2104 IN UINT8 Value\r
2105 )\r
2106{\r
2107 ASSERT (Value < 0xa0);\r
2108 ASSERT ((Value & 0xf) < 0xa);\r
2109 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
2110}\r