]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/String.c
MdePkg/BaseCacheMaintenanceLib: Support IA32 processors without CLFLUSH
[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
1003/**\r
9aa049d9 1004 Convert a Null-terminated Unicode string to a Null-terminated\r
e1f414b6 1005 ASCII string and returns the ASCII string.\r
9aa049d9 1006\r
1007 This function converts the content of the Unicode string Source\r
1008 to the ASCII string Destination by copying the lower 8 bits of\r
1009 each Unicode character. It returns Destination.\r
1010\r
d3e0289c 1011 The caller is responsible to make sure Destination points to a buffer with size\r
1012 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.\r
1013\r
9aa049d9 1014 If any Unicode characters in Source contain non-zero value in\r
1015 the upper 8 bits, then ASSERT().\r
e1f414b6 1016\r
1017 If Destination is NULL, then ASSERT().\r
1018 If Source is NULL, then ASSERT().\r
1019 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
1020 If Source and Destination overlap, then ASSERT().\r
1021\r
9aa049d9 1022 If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
dfbe9de9 1023 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
e1f414b6 1024 the Null-terminator, then ASSERT().\r
9aa049d9 1025\r
1026 If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
dfbe9de9 1027 than PcdMaximumAsciiStringLength Unicode characters, not including the\r
e1f414b6 1028 Null-terminator, then ASSERT().\r
1029\r
127010dd 1030 @param Source A pointer to a Null-terminated Unicode string.\r
1031 @param Destination A pointer to a Null-terminated ASCII string.\r
e1f414b6 1032\r
9aa049d9 1033 @return Destination.\r
e1f414b6 1034\r
1035**/\r
1036CHAR8 *\r
1037EFIAPI\r
1038UnicodeStrToAsciiStr (\r
2fc60b70 1039 IN CONST CHAR16 *Source,\r
1040 OUT CHAR8 *Destination\r
e1f414b6 1041 )\r
1042{\r
4df26661 1043 CHAR8 *ReturnValue;\r
1044\r
e1f414b6 1045 ASSERT (Destination != NULL);\r
4df26661 1046\r
1047 //\r
1048 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
1049 // Length tests are performed inside StrLen().\r
1050 //\r
1051 ASSERT (StrSize (Source) != 0);\r
e1f414b6 1052\r
1053 //\r
1054 // Source and Destination should not overlap\r
1055 //\r
d52b9d86 1056 ASSERT ((UINTN) (Destination - (CHAR8 *) Source) >= StrSize (Source));\r
e1f414b6 1057 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
1058\r
e1f414b6 1059\r
4df26661 1060 ReturnValue = Destination;\r
e1f414b6 1061 while (*Source != '\0') {\r
1062 //\r
1063 // If any Unicode characters in Source contain \r
1064 // non-zero value in the upper 8 bits, then ASSERT().\r
1065 //\r
1066 ASSERT (*Source < 0x100);\r
1067 *(Destination++) = (CHAR8) *(Source++);\r
1068 }\r
1069\r
1070 *Destination = '\0';\r
4df26661 1071\r
1072 //\r
1073 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
1074 // Length tests are performed inside AsciiStrLen().\r
1075 //\r
1076 ASSERT (AsciiStrSize (ReturnValue) != 0);\r
1077\r
1078 return ReturnValue;\r
e1f414b6 1079}\r
1080\r
1bb390f1 1081#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
e1f414b6 1082\r
1083/**\r
1bb390f1
ED
1084 [ATTENTION] This function will be deprecated for security reason.\r
1085\r
e1f414b6 1086 Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
1087 string and returns the new ASCII string.\r
1088\r
1089 This function copies the contents of the ASCII string Source to the ASCII\r
1090 string Destination, and returns Destination. If Source and Destination\r
1091 overlap, then the results are undefined.\r
1092\r
1093 If Destination is NULL, then ASSERT().\r
1094 If Source is NULL, then ASSERT().\r
1095 If Source and Destination overlap, then ASSERT().\r
1096 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
dfbe9de9 1097 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1098 then ASSERT().\r
1099\r
127010dd 1100 @param Destination A pointer to a Null-terminated ASCII string.\r
1101 @param Source A pointer to a Null-terminated ASCII string.\r
e1f414b6 1102\r
9aa049d9 1103 @return Destination\r
e1f414b6 1104\r
1105**/\r
1106CHAR8 *\r
1107EFIAPI\r
1108AsciiStrCpy (\r
1109 OUT CHAR8 *Destination,\r
1110 IN CONST CHAR8 *Source\r
1111 )\r
1112{\r
1113 CHAR8 *ReturnValue;\r
1114\r
1115 //\r
1116 // Destination cannot be NULL\r
1117 //\r
1118 ASSERT (Destination != NULL);\r
1119\r
1120 //\r
1121 // Destination and source cannot overlap\r
1122 //\r
1123 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1124 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r
1125\r
1126 ReturnValue = Destination;\r
42eedea9 1127 while (*Source != 0) {\r
e1f414b6 1128 *(Destination++) = *(Source++);\r
1129 }\r
1130 *Destination = 0;\r
1131 return ReturnValue;\r
1132}\r
1133\r
1134/**\r
1bb390f1
ED
1135 [ATTENTION] This function will be deprecated for security reason.\r
1136\r
9aa049d9 1137 Copies up to a specified length one Null-terminated ASCII string to another \r
1138 Null-terminated ASCII string and returns the new ASCII string.\r
e1f414b6 1139\r
1140 This function copies the contents of the ASCII string Source to the ASCII\r
1141 string Destination, and returns Destination. At most, Length ASCII characters\r
1142 are copied from Source to Destination. If Length is 0, then Destination is\r
1143 returned unmodified. If Length is greater that the number of ASCII characters\r
1144 in Source, then Destination is padded with Null ASCII characters. If Source\r
1145 and Destination overlap, then the results are undefined.\r
1146\r
1147 If Destination is NULL, then ASSERT().\r
1148 If Source is NULL, then ASSERT().\r
1149 If Source and Destination overlap, then ASSERT().\r
53e96610 1150 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1151 PcdMaximumAsciiStringLength, then ASSERT().\r
e1f414b6 1152 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
dfbe9de9 1153 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
53e96610 1154 then ASSERT().\r
e1f414b6 1155\r
127010dd 1156 @param Destination A pointer to a Null-terminated ASCII string.\r
1157 @param Source A pointer to a Null-terminated ASCII string.\r
2fc59a00 1158 @param Length The maximum number of ASCII characters to copy.\r
e1f414b6 1159\r
9aa049d9 1160 @return Destination\r
e1f414b6 1161\r
1162**/\r
1163CHAR8 *\r
1164EFIAPI\r
1165AsciiStrnCpy (\r
1166 OUT CHAR8 *Destination,\r
1167 IN CONST CHAR8 *Source,\r
1168 IN UINTN Length\r
1169 )\r
1170{\r
1171 CHAR8 *ReturnValue;\r
1172\r
2bfb6009 1173 if (Length == 0) {\r
e1f414b6 1174 return Destination;\r
1175 }\r
1176\r
1177 //\r
1178 // Destination cannot be NULL\r
1179 //\r
1180 ASSERT (Destination != NULL);\r
1181\r
1182 //\r
1183 // Destination and source cannot overlap\r
1184 //\r
1185 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1186 ASSERT ((UINTN)(Source - Destination) >= Length);\r
1187\r
dfbe9de9 1188 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1189 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1190 }\r
1191\r
e1f414b6 1192 ReturnValue = Destination;\r
1193\r
42eedea9 1194 while (*Source != 0 && Length > 0) {\r
e1f414b6 1195 *(Destination++) = *(Source++);\r
1196 Length--;\r
1197 }\r
1198\r
1199 ZeroMem (Destination, Length * sizeof (*Destination));\r
1200 return ReturnValue;\r
1201}\r
1bb390f1 1202#endif\r
e1f414b6 1203\r
1204/**\r
1205 Returns the length of a Null-terminated ASCII string.\r
1206\r
1207 This function returns the number of ASCII characters in the Null-terminated\r
1208 ASCII string specified by String.\r
1209\r
9aa049d9 1210 If Length > 0 and Destination is NULL, then ASSERT().\r
1211 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1212 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
dfbe9de9 1213 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1214 then ASSERT().\r
1215\r
127010dd 1216 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1217\r
1218 @return The length of String.\r
1219\r
1220**/\r
1221UINTN\r
1222EFIAPI\r
1223AsciiStrLen (\r
1224 IN CONST CHAR8 *String\r
1225 )\r
1226{\r
1227 UINTN Length;\r
1228\r
1229 ASSERT (String != NULL);\r
1230\r
1231 for (Length = 0; *String != '\0'; String++, Length++) {\r
1232 //\r
1233 // If PcdMaximumUnicodeStringLength is not zero,\r
1234 // length should not more than PcdMaximumUnicodeStringLength\r
1235 //\r
1236 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1237 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r
1238 }\r
1239 }\r
1240 return Length;\r
1241}\r
1242\r
1243/**\r
1244 Returns the size of a Null-terminated ASCII string in bytes, including the\r
1245 Null terminator.\r
1246\r
1247 This function returns the size, in bytes, of the Null-terminated ASCII string\r
1248 specified by String.\r
1249\r
1250 If String is NULL, then ASSERT().\r
1251 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
dfbe9de9 1252 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1253 then ASSERT().\r
1254\r
127010dd 1255 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1256\r
1257 @return The size of String.\r
1258\r
1259**/\r
1260UINTN\r
1261EFIAPI\r
1262AsciiStrSize (\r
1263 IN CONST CHAR8 *String\r
1264 )\r
1265{\r
1266 return (AsciiStrLen (String) + 1) * sizeof (*String);\r
1267}\r
1268\r
1269/**\r
1270 Compares two Null-terminated ASCII strings, and returns the difference\r
1271 between the first mismatched ASCII characters.\r
1272\r
1273 This function compares the Null-terminated ASCII string FirstString to the\r
1274 Null-terminated ASCII string SecondString. If FirstString is identical to\r
1275 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
1276 mismatched ASCII character in SecondString subtracted from the first\r
1277 mismatched ASCII character in FirstString.\r
1278\r
1279 If FirstString is NULL, then ASSERT().\r
1280 If SecondString is NULL, then ASSERT().\r
1281 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
dfbe9de9 1282 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1283 then ASSERT().\r
1284 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
dfbe9de9 1285 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
e1f414b6 1286 Null-terminator, then ASSERT().\r
1287\r
127010dd 1288 @param FirstString A pointer to a Null-terminated ASCII string.\r
1289 @param SecondString A pointer to a Null-terminated ASCII string.\r
e1f414b6 1290\r
9aa049d9 1291 @retval ==0 FirstString is identical to SecondString.\r
1292 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1293\r
1294**/\r
1295INTN\r
1296EFIAPI\r
1297AsciiStrCmp (\r
1298 IN CONST CHAR8 *FirstString,\r
1299 IN CONST CHAR8 *SecondString\r
1300 )\r
1301{\r
1302 //\r
1303 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1304 //\r
1305 ASSERT (AsciiStrSize (FirstString));\r
1306 ASSERT (AsciiStrSize (SecondString));\r
1307\r
1308 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r
1309 FirstString++;\r
1310 SecondString++;\r
1311 }\r
1312\r
1313 return *FirstString - *SecondString;\r
1314}\r
1315\r
1316/**\r
24dcb5e5 1317 Converts a lowercase Ascii character to upper one.\r
e1f414b6 1318\r
1319 If Chr is lowercase Ascii character, then converts it to upper one.\r
1320\r
1321 If Value >= 0xA0, then ASSERT().\r
1322 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
1323\r
42eedea9 1324 @param Chr one Ascii character\r
e1f414b6 1325\r
1326 @return The uppercase value of Ascii character \r
1327\r
1328**/\r
e1f414b6 1329CHAR8\r
42eedea9 1330EFIAPI\r
80151e53 1331InternalBaseLibAsciiToUpper (\r
e1f414b6 1332 IN CHAR8 Chr\r
1333 )\r
1334{\r
1335 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);\r
1336}\r
1337\r
1338/**\r
1339 Convert a ASCII character to numerical value.\r
1340\r
1341 This internal function only deal with Unicode character\r
1342 which maps to a valid hexadecimal ASII character, i.e.\r
1343 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r
1344 ASCII character, the value returned does not make sense.\r
1345\r
1346 @param Char The character to convert.\r
1347\r
24dcb5e5 1348 @return The numerical value converted.\r
e1f414b6 1349\r
1350**/\r
e1f414b6 1351UINTN\r
42eedea9 1352EFIAPI\r
e1f414b6 1353InternalAsciiHexCharToUintn (\r
1354 IN CHAR8 Char\r
1355 )\r
1356{\r
1357 if (InternalIsDecimalDigitCharacter (Char)) {\r
1358 return Char - '0';\r
1359 }\r
1360\r
80151e53 1361 return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
e1f414b6 1362}\r
1363\r
1364\r
1365/**\r
1366 Performs a case insensitive comparison of two Null-terminated ASCII strings,\r
1367 and returns the difference between the first mismatched ASCII characters.\r
1368\r
1369 This function performs a case insensitive comparison of the Null-terminated\r
1370 ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r
1371 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
1372 value returned is the first mismatched lower case ASCII character in\r
1373 SecondString subtracted from the first mismatched lower case ASCII character\r
1374 in FirstString.\r
1375\r
1376 If FirstString is NULL, then ASSERT().\r
1377 If SecondString is NULL, then ASSERT().\r
1378 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
dfbe9de9 1379 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1380 then ASSERT().\r
1381 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
dfbe9de9 1382 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
e1f414b6 1383 Null-terminator, then ASSERT().\r
1384\r
127010dd 1385 @param FirstString A pointer to a Null-terminated ASCII string.\r
1386 @param SecondString A pointer to a Null-terminated ASCII string.\r
e1f414b6 1387\r
9aa049d9 1388 @retval ==0 FirstString is identical to SecondString using case insensitive\r
1106ffe1 1389 comparisons.\r
9aa049d9 1390 @retval !=0 FirstString is not identical to SecondString using case\r
1391 insensitive comparisons.\r
e1f414b6 1392\r
1393**/\r
1394INTN\r
1395EFIAPI\r
1396AsciiStriCmp (\r
1397 IN CONST CHAR8 *FirstString,\r
1398 IN CONST CHAR8 *SecondString\r
1399 )\r
1400{\r
1401 CHAR8 UpperFirstString;\r
1402 CHAR8 UpperSecondString;\r
1403\r
1404 //\r
1405 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1406 //\r
1407 ASSERT (AsciiStrSize (FirstString));\r
1408 ASSERT (AsciiStrSize (SecondString));\r
1409\r
80151e53
LG
1410 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1411 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
e1f414b6 1412 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
1413 FirstString++;\r
1414 SecondString++;\r
80151e53
LG
1415 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1416 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
e1f414b6 1417 }\r
1418\r
1419 return UpperFirstString - UpperSecondString;\r
1420}\r
1421\r
1422/**\r
1423 Compares two Null-terminated ASCII strings with maximum lengths, and returns\r
1424 the difference between the first mismatched ASCII characters.\r
1425\r
1426 This function compares the Null-terminated ASCII string FirstString to the\r
1427 Null-terminated ASCII string SecondString. At most, Length ASCII characters\r
1428 will be compared. If Length is 0, then 0 is returned. If FirstString is\r
1429 identical to SecondString, then 0 is returned. Otherwise, the value returned\r
1430 is the first mismatched ASCII character in SecondString subtracted from the\r
1431 first mismatched ASCII character in FirstString.\r
1432\r
9aa049d9 1433 If Length > 0 and FirstString is NULL, then ASSERT().\r
1434 If Length > 0 and SecondString is NULL, then ASSERT().\r
53e96610 1435 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1436 PcdMaximumAsciiStringLength, then ASSERT().\r
1437 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than\r
dfbe9de9 1438 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1439 then ASSERT().\r
53e96610 1440 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than\r
dfbe9de9 1441 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
53e96610 1442 then ASSERT().\r
e1f414b6 1443\r
127010dd 1444 @param FirstString A pointer to a Null-terminated ASCII string.\r
1445 @param SecondString A pointer to a Null-terminated ASCII string.\r
2fc59a00 1446 @param Length The maximum number of ASCII characters for compare.\r
2fc60b70 1447 \r
9aa049d9 1448 @retval ==0 FirstString is identical to SecondString.\r
1449 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1450\r
1451**/\r
1452INTN\r
1453EFIAPI\r
1454AsciiStrnCmp (\r
1455 IN CONST CHAR8 *FirstString,\r
1456 IN CONST CHAR8 *SecondString,\r
1457 IN UINTN Length\r
1458 )\r
1459{\r
2bfb6009 1460 if (Length == 0) {\r
e1f414b6 1461 return 0;\r
1462 }\r
1463\r
1464 //\r
1465 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1466 //\r
1467 ASSERT (AsciiStrSize (FirstString));\r
1468 ASSERT (AsciiStrSize (SecondString));\r
1469\r
dfbe9de9 1470 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1471 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1472 }\r
1473\r
e1f414b6 1474 while ((*FirstString != '\0') &&\r
1475 (*FirstString == *SecondString) &&\r
1476 (Length > 1)) {\r
1477 FirstString++;\r
1478 SecondString++;\r
1479 Length--;\r
1480 }\r
1481 return *FirstString - *SecondString;\r
1482}\r
1483\r
1bb390f1
ED
1484#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1485\r
e1f414b6 1486/**\r
1bb390f1
ED
1487 [ATTENTION] This function will be deprecated for security reason.\r
1488\r
e1f414b6 1489 Concatenates one Null-terminated ASCII string to another Null-terminated\r
1490 ASCII string, and returns the concatenated ASCII string.\r
1491\r
1492 This function concatenates two Null-terminated ASCII strings. The contents of\r
1493 Null-terminated ASCII string Source are concatenated to the end of Null-\r
1494 terminated ASCII string Destination. The Null-terminated concatenated ASCII\r
1495 String is returned.\r
1496\r
1497 If Destination is NULL, then ASSERT().\r
1498 If Source is NULL, then ASSERT().\r
1499 If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
dfbe9de9 1500 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1501 then ASSERT().\r
1502 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
dfbe9de9 1503 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1504 then ASSERT().\r
1505 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
1506 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1507 ASCII characters, then ASSERT().\r
1508\r
127010dd 1509 @param Destination A pointer to a Null-terminated ASCII string.\r
1510 @param Source A pointer to a Null-terminated ASCII string.\r
e1f414b6 1511\r
9aa049d9 1512 @return Destination\r
e1f414b6 1513\r
1514**/\r
1515CHAR8 *\r
1516EFIAPI\r
1517AsciiStrCat (\r
1518 IN OUT CHAR8 *Destination,\r
1519 IN CONST CHAR8 *Source\r
1520 )\r
1521{\r
1522 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r
1523\r
1524 //\r
1525 // Size of the resulting string should never be zero.\r
1526 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1527 //\r
1528 ASSERT (AsciiStrSize (Destination) != 0);\r
1529 return Destination;\r
1530}\r
1531\r
1532/**\r
1bb390f1
ED
1533 [ATTENTION] This function will be deprecated for security reason.\r
1534\r
9aa049d9 1535 Concatenates up to a specified length one Null-terminated ASCII string to \r
1536 the end of another Null-terminated ASCII string, and returns the \r
1537 concatenated ASCII string.\r
e1f414b6 1538\r
1539 This function concatenates two Null-terminated ASCII strings. The contents\r
1540 of Null-terminated ASCII string Source are concatenated to the end of Null-\r
1541 terminated ASCII string Destination, and Destination is returned. At most,\r
1542 Length ASCII characters are concatenated from Source to the end of\r
1543 Destination, and Destination is always Null-terminated. If Length is 0, then\r
1544 Destination is returned unmodified. If Source and Destination overlap, then\r
1545 the results are undefined.\r
1546\r
9aa049d9 1547 If Length > 0 and Destination is NULL, then ASSERT().\r
1548 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1549 If Source and Destination overlap, then ASSERT().\r
53e96610 1550 If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
1551 PcdMaximumAsciiStringLength, then ASSERT().\r
e1f414b6 1552 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
dfbe9de9 1553 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1554 then ASSERT().\r
1555 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
dfbe9de9 1556 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
e1f414b6 1557 then ASSERT().\r
1558 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
1559 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
53e96610 1560 ASCII characters, not including the Null-terminator, then ASSERT().\r
e1f414b6 1561\r
127010dd 1562 @param Destination A pointer to a Null-terminated ASCII string.\r
1563 @param Source A pointer to a Null-terminated ASCII string.\r
2fc59a00 1564 @param Length The maximum number of ASCII characters to concatenate from\r
e1f414b6 1565 Source.\r
1566\r
9aa049d9 1567 @return Destination\r
e1f414b6 1568\r
1569**/\r
1570CHAR8 *\r
1571EFIAPI\r
1572AsciiStrnCat (\r
1573 IN OUT CHAR8 *Destination,\r
1574 IN CONST CHAR8 *Source,\r
1575 IN UINTN Length\r
1576 )\r
1577{\r
8f635c36 1578 UINTN DestinationLen;\r
1579\r
1580 DestinationLen = AsciiStrLen (Destination);\r
1581 AsciiStrnCpy (Destination + DestinationLen, Source, Length);\r
1582 Destination[DestinationLen + Length] = '\0';\r
e1f414b6 1583\r
1584 //\r
1585 // Size of the resulting string should never be zero.\r
1586 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1587 //\r
1588 ASSERT (AsciiStrSize (Destination) != 0);\r
1589 return Destination;\r
1590}\r
1bb390f1 1591#endif\r
e1f414b6 1592\r
1593/**\r
9aa049d9 1594 Returns the first occurrence of a Null-terminated ASCII sub-string\r
e1f414b6 1595 in a Null-terminated ASCII string.\r
1596\r
9aa049d9 1597 This function scans the contents of the ASCII string specified by String\r
1598 and returns the first occurrence of SearchString. If SearchString is not\r
1599 found in String, then NULL is returned. If the length of SearchString is zero,\r
e1f414b6 1600 then String is returned.\r
9aa049d9 1601\r
e1f414b6 1602 If String is NULL, then ASSERT().\r
1603 If SearchString is NULL, then ASSERT().\r
1604\r
9aa049d9 1605 If PcdMaximumAsciiStringLength is not zero, and SearchString or\r
1606 String contains more than PcdMaximumAsciiStringLength Unicode characters\r
e1f414b6 1607 not including the Null-terminator, then ASSERT().\r
1608\r
127010dd 1609 @param String A pointer to a Null-terminated ASCII string.\r
1610 @param SearchString A pointer to a Null-terminated ASCII string to search for.\r
e1f414b6 1611\r
1612 @retval NULL If the SearchString does not appear in String.\r
9aa049d9 1613 @retval others If there is a match return the first occurrence of SearchingString.\r
1614 If the length of SearchString is zero,return String.\r
e1f414b6 1615\r
1616**/\r
1617CHAR8 *\r
1618EFIAPI\r
1619AsciiStrStr (\r
2fc60b70 1620 IN CONST CHAR8 *String,\r
1621 IN CONST CHAR8 *SearchString\r
e1f414b6 1622 )\r
1623{\r
1624 CONST CHAR8 *FirstMatch;\r
1625 CONST CHAR8 *SearchStringTmp;\r
1626\r
e1f414b6 1627 //\r
4df26661 1628 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
e1f414b6 1629 //\r
4df26661 1630 ASSERT (AsciiStrSize (String) != 0);\r
1631 ASSERT (AsciiStrSize (SearchString) != 0);\r
e1f414b6 1632\r
62e71e2f 1633 if (*SearchString == '\0') {\r
faeb3214 1634 return (CHAR8 *) String;\r
62e71e2f 1635 }\r
1636\r
e1f414b6 1637 while (*String != '\0') {\r
1638 SearchStringTmp = SearchString;\r
1639 FirstMatch = String;\r
1640 \r
1641 while ((*String == *SearchStringTmp) \r
e1f414b6 1642 && (*String != '\0')) {\r
1643 String++;\r
1644 SearchStringTmp++;\r
1645 } \r
1646 \r
1647 if (*SearchStringTmp == '\0') {\r
1648 return (CHAR8 *) FirstMatch;\r
1649 }\r
1650\r
62e71e2f 1651 if (*String == '\0') {\r
1652 return NULL;\r
e1f414b6 1653 }\r
1654\r
62e71e2f 1655 String = FirstMatch + 1;\r
e1f414b6 1656 }\r
1657\r
1658 return NULL;\r
1659}\r
1660\r
1661/**\r
9aa049d9 1662 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1663 UINTN.\r
1664\r
9aa049d9 1665 This function returns a value of type UINTN by interpreting the contents\r
1666 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1667 ASCII string String is:\r
9aa049d9 1668\r
e1f414b6 1669 [spaces] [decimal digits].\r
9aa049d9 1670\r
1671 The valid decimal digit character is in the range [0-9]. The function will\r
1672 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1673 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1674 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1675 Null-terminator, whichever on comes first.\r
9aa049d9 1676\r
e1f414b6 1677 If String has only pad spaces, then 0 is returned.\r
1678 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1679 If the number represented by String overflows according to the range defined by\r
e1f414b6 1680 UINTN, then ASSERT().\r
1681 If String is NULL, then ASSERT().\r
9aa049d9 1682 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1683 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1684 then ASSERT().\r
1685\r
127010dd 1686 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1687\r
9aa049d9 1688 @retval Value translated from String.\r
e1f414b6 1689\r
1690**/\r
1691UINTN\r
1692EFIAPI\r
1693AsciiStrDecimalToUintn (\r
4df26661 1694 IN CONST CHAR8 *String\r
e1f414b6 1695 )\r
1696{\r
1697 UINTN Result;\r
1698 \r
4df26661 1699 //\r
1700 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1701 //\r
1702 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1703\r
1704 //\r
1705 // Ignore the pad spaces (space or tab)\r
1706 //\r
955c32f2 1707 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1708 String++;\r
1709 }\r
1710\r
1711 //\r
1712 // Ignore leading Zeros after the spaces\r
1713 //\r
955c32f2 1714 while (*String == '0') {\r
e1f414b6 1715 String++;\r
1716 }\r
1717\r
1718 Result = 0;\r
1719\r
1720 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1721 //\r
1722 // If the number represented by String overflows according \r
1723 // to the range defined by UINTN, then ASSERT().\r
1724 //\r
6efe9461 1725 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
e1f414b6 1726\r
1727 Result = Result * 10 + (*String - '0');\r
1728 String++;\r
1729 }\r
1730 \r
1731 return Result;\r
1732}\r
1733\r
1734\r
1735/**\r
9aa049d9 1736 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1737 UINT64.\r
1738\r
9aa049d9 1739 This function returns a value of type UINT64 by interpreting the contents\r
1740 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1741 ASCII string String is:\r
9aa049d9 1742\r
e1f414b6 1743 [spaces] [decimal digits].\r
9aa049d9 1744\r
1745 The valid decimal digit character is in the range [0-9]. The function will\r
1746 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1747 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1748 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1749 Null-terminator, whichever on comes first.\r
9aa049d9 1750\r
e1f414b6 1751 If String has only pad spaces, then 0 is returned.\r
1752 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1753 If the number represented by String overflows according to the range defined by\r
e1f414b6 1754 UINT64, then ASSERT().\r
1755 If String is NULL, then ASSERT().\r
9aa049d9 1756 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1757 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1758 then ASSERT().\r
1759\r
127010dd 1760 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1761\r
9aa049d9 1762 @retval Value translated from String.\r
e1f414b6 1763\r
1764**/\r
1765UINT64\r
1766EFIAPI\r
1767AsciiStrDecimalToUint64 (\r
2fc60b70 1768 IN CONST CHAR8 *String\r
e1f414b6 1769 )\r
1770{\r
1771 UINT64 Result;\r
1772 \r
4df26661 1773 //\r
1774 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1775 //\r
1776 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1777\r
1778 //\r
1779 // Ignore the pad spaces (space or tab)\r
1780 //\r
955c32f2 1781 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1782 String++;\r
1783 }\r
1784\r
1785 //\r
1786 // Ignore leading Zeros after the spaces\r
1787 //\r
955c32f2 1788 while (*String == '0') {\r
e1f414b6 1789 String++;\r
1790 }\r
1791\r
1792 Result = 0;\r
1793\r
1794 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1795 //\r
1796 // If the number represented by String overflows according \r
1797 // to the range defined by UINTN, then ASSERT().\r
1798 //\r
6efe9461 1799 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
e1f414b6 1800\r
1801 Result = MultU64x32 (Result, 10) + (*String - '0');\r
1802 String++;\r
1803 }\r
1804 \r
1805 return Result;\r
1806}\r
1807\r
1808/**\r
1809 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1810\r
9aa049d9 1811 This function returns a value of type UINTN by interpreting the contents of\r
1812 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1813 string String is:\r
9aa049d9 1814\r
e1f414b6 1815 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1816\r
1817 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1818 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1819 appears in the input string, it must be prefixed with at least one 0. The function\r
1820 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1821 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1822 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1823 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1824 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1825\r
e1f414b6 1826 If String has only pad spaces, then 0 is returned.\r
1827 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1828 0 is returned.\r
1829\r
9aa049d9 1830 If the number represented by String overflows according to the range defined by UINTN,\r
e1f414b6 1831 then ASSERT().\r
1832 If String is NULL, then ASSERT().\r
9aa049d9 1833 If PcdMaximumAsciiStringLength is not zero,\r
1834 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1835 the Null-terminator, then ASSERT().\r
1836\r
127010dd 1837 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1838\r
9aa049d9 1839 @retval Value translated from String.\r
e1f414b6 1840\r
1841**/\r
1842UINTN\r
1843EFIAPI\r
1844AsciiStrHexToUintn (\r
2fc60b70 1845 IN CONST CHAR8 *String\r
e1f414b6 1846 )\r
1847{\r
1848 UINTN Result;\r
1849\r
4df26661 1850 //\r
1851 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1852 //\r
1853 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1854 \r
1855 //\r
1856 // Ignore the pad spaces (space or tab) \r
1857 //\r
955c32f2 1858 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1859 String++;\r
1860 }\r
1861\r
1862 //\r
1863 // Ignore leading Zeros after the spaces\r
1864 //\r
955c32f2 1865 while (*String == '0') {\r
e1f414b6 1866 String++;\r
1867 }\r
1868\r
80151e53 1869 if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
955c32f2 1870 ASSERT (*(String - 1) == '0');\r
1871 if (*(String - 1) != '0') {\r
e1f414b6 1872 return 0;\r
1873 }\r
1874 //\r
1875 // Skip the 'X'\r
1876 //\r
1877 String++;\r
1878 }\r
1879\r
1880 Result = 0;\r
1881 \r
1882 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1883 //\r
1884 // If the Hex Number represented by String overflows according \r
1885 // to the range defined by UINTN, then ASSERT().\r
1886 //\r
6efe9461 1887 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
e1f414b6 1888\r
1889 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
1890 String++;\r
1891 }\r
1892\r
1893 return Result;\r
1894}\r
1895\r
1896\r
1897/**\r
1898 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1899\r
9aa049d9 1900 This function returns a value of type UINT64 by interpreting the contents of\r
1901 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1902 string String is:\r
9aa049d9 1903\r
e1f414b6 1904 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1905\r
1906 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1907 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1908 appears in the input string, it must be prefixed with at least one 0. The function\r
1909 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1910 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1911 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1912 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1913 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1914\r
e1f414b6 1915 If String has only pad spaces, then 0 is returned.\r
1916 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1917 0 is returned.\r
1918\r
9aa049d9 1919 If the number represented by String overflows according to the range defined by UINT64,\r
e1f414b6 1920 then ASSERT().\r
1921 If String is NULL, then ASSERT().\r
9aa049d9 1922 If PcdMaximumAsciiStringLength is not zero,\r
1923 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1924 the Null-terminator, then ASSERT().\r
1925\r
127010dd 1926 @param String A pointer to a Null-terminated ASCII string.\r
e1f414b6 1927\r
9aa049d9 1928 @retval Value translated from String.\r
e1f414b6 1929\r
1930**/\r
1931UINT64\r
1932EFIAPI\r
1933AsciiStrHexToUint64 (\r
2fc60b70 1934 IN CONST CHAR8 *String\r
e1f414b6 1935 )\r
1936{\r
1937 UINT64 Result;\r
1938\r
4df26661 1939 //\r
1940 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1941 //\r
1942 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1943 \r
1944 //\r
1945 // Ignore the pad spaces (space or tab) and leading Zeros\r
1946 //\r
1947 //\r
1948 // Ignore the pad spaces (space or tab) \r
1949 //\r
955c32f2 1950 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1951 String++;\r
1952 }\r
1953\r
1954 //\r
1955 // Ignore leading Zeros after the spaces\r
1956 //\r
955c32f2 1957 while (*String == '0') {\r
e1f414b6 1958 String++;\r
1959 }\r
1960\r
80151e53 1961 if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
955c32f2 1962 ASSERT (*(String - 1) == '0');\r
1963 if (*(String - 1) != '0') {\r
e1f414b6 1964 return 0;\r
1965 }\r
1966 //\r
1967 // Skip the 'X'\r
1968 //\r
1969 String++;\r
1970 }\r
1971\r
1972 Result = 0;\r
1973 \r
1974 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1975 //\r
1976 // If the Hex Number represented by String overflows according \r
1977 // to the range defined by UINTN, then ASSERT().\r
1978 //\r
6efe9461 1979 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
e1f414b6 1980\r
1981 Result = LShiftU64 (Result, 4);\r
1982 Result = Result + InternalAsciiHexCharToUintn (*String);\r
1983 String++;\r
1984 }\r
1985\r
1986 return Result;\r
1987}\r
1988\r
1989\r
1990/**\r
9aa049d9 1991 Convert one Null-terminated ASCII string to a Null-terminated\r
e1f414b6 1992 Unicode string and returns the Unicode string.\r
1993\r
9aa049d9 1994 This function converts the contents of the ASCII string Source to the Unicode\r
1995 string Destination, and returns Destination. The function terminates the\r
1996 Unicode string Destination by appending a Null-terminator character at the end.\r
1997 The caller is responsible to make sure Destination points to a buffer with size\r
e1f414b6 1998 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
9aa049d9 1999\r
e1f414b6 2000 If Destination is NULL, then ASSERT().\r
2001 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
2002 If Source is NULL, then ASSERT().\r
2003 If Source and Destination overlap, then ASSERT().\r
9aa049d9 2004 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
2005 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 2006 then ASSERT().\r
9aa049d9 2007 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
2008 PcdMaximumUnicodeStringLength ASCII characters not including the\r
e1f414b6 2009 Null-terminator, then ASSERT().\r
2010\r
127010dd 2011 @param Source A pointer to a Null-terminated ASCII string.\r
2012 @param Destination A pointer to a Null-terminated Unicode string.\r
e1f414b6 2013\r
9aa049d9 2014 @return Destination.\r
e1f414b6 2015\r
2016**/\r
2017CHAR16 *\r
2018EFIAPI\r
2019AsciiStrToUnicodeStr (\r
4df26661 2020 IN CONST CHAR8 *Source,\r
2021 OUT CHAR16 *Destination\r
e1f414b6 2022 )\r
2023{\r
4df26661 2024 CHAR16 *ReturnValue;\r
2025\r
e1f414b6 2026 ASSERT (Destination != NULL);\r
e1f414b6 2027\r
2028 //\r
4df26661 2029 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
e1f414b6 2030 //\r
4df26661 2031 ASSERT (AsciiStrSize (Source) != 0);\r
e1f414b6 2032\r
2033 //\r
4df26661 2034 // Source and Destination should not overlap\r
e1f414b6 2035 //\r
4df26661 2036 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
d52b9d86
SZ
2037 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) >= (AsciiStrSize (Source) * sizeof (CHAR16)));\r
2038\r
e1f414b6 2039\r
4df26661 2040 ReturnValue = Destination;\r
e1f414b6 2041 while (*Source != '\0') {\r
2042 *(Destination++) = (CHAR16) *(Source++);\r
2043 }\r
2044 //\r
2045 // End the Destination with a NULL.\r
2046 //\r
2047 *Destination = '\0';\r
2048\r
4df26661 2049 //\r
2050 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
2051 //\r
2052 ASSERT (StrSize (ReturnValue) != 0);\r
2053\r
2054 return ReturnValue;\r
e1f414b6 2055}\r
2056\r
2057/**\r
2058 Converts an 8-bit value to an 8-bit BCD value.\r
2059\r
2060 Converts the 8-bit value specified by Value to BCD. The BCD value is\r
2061 returned.\r
2062\r
2063 If Value >= 100, then ASSERT().\r
2064\r
2065 @param Value The 8-bit value to convert to BCD. Range 0..99.\r
2066\r
9aa049d9 2067 @return The BCD value.\r
e1f414b6 2068\r
2069**/\r
2070UINT8\r
2071EFIAPI\r
2072DecimalToBcd8 (\r
2073 IN UINT8 Value\r
2074 )\r
2075{\r
2076 ASSERT (Value < 100);\r
2077 return (UINT8) (((Value / 10) << 4) | (Value % 10));\r
2078}\r
2079\r
2080/**\r
2081 Converts an 8-bit BCD value to an 8-bit value.\r
2082\r
2083 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
2084 value is returned.\r
2085\r
2086 If Value >= 0xA0, then ASSERT().\r
2087 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
2088\r
2089 @param Value The 8-bit BCD value to convert to an 8-bit value.\r
2090\r
9aa049d9 2091 @return The 8-bit value is returned.\r
e1f414b6 2092\r
2093**/\r
2094UINT8\r
2095EFIAPI\r
2096BcdToDecimal8 (\r
2097 IN UINT8 Value\r
2098 )\r
2099{\r
2100 ASSERT (Value < 0xa0);\r
2101 ASSERT ((Value & 0xf) < 0xa);\r
2102 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
2103}\r