2 Unicode and ASCII string primitives.
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BaseLibInternals.h"
17 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
20 [ATTENTION] This function will be deprecated for security reason.
22 Copies one Null-terminated Unicode string to another Null-terminated Unicode
23 string and returns the new Unicode string.
25 This function copies the contents of the Unicode string Source to the Unicode
26 string Destination, and returns Destination. If Source and Destination
27 overlap, then the results are undefined.
29 If Destination is NULL, then ASSERT().
30 If Destination is not aligned on a 16-bit boundary, then ASSERT().
31 If Source is NULL, then ASSERT().
32 If Source is not aligned on a 16-bit boundary, then ASSERT().
33 If Source and Destination overlap, then ASSERT().
34 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
35 PcdMaximumUnicodeStringLength Unicode characters, not including the
36 Null-terminator, then ASSERT().
38 @param Destination A pointer to a Null-terminated Unicode string.
39 @param Source A pointer to a Null-terminated Unicode string.
47 OUT CHAR16
*Destination
,
48 IN CONST CHAR16
*Source
54 // Destination cannot be NULL
56 ASSERT (Destination
!= NULL
);
57 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
60 // Destination and source cannot overlap
62 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
63 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
65 ReturnValue
= Destination
;
66 while (*Source
!= 0) {
67 *(Destination
++) = *(Source
++);
74 [ATTENTION] This function will be deprecated for security reason.
76 Copies up to a specified length from one Null-terminated Unicode string to
77 another Null-terminated Unicode string and returns the new Unicode string.
79 This function copies the contents of the Unicode string Source to the Unicode
80 string Destination, and returns Destination. At most, Length Unicode
81 characters are copied from Source to Destination. If Length is 0, then
82 Destination is returned unmodified. If Length is greater that the number of
83 Unicode characters in Source, then Destination is padded with Null Unicode
84 characters. If Source and Destination overlap, then the results are
87 If Length > 0 and Destination is NULL, then ASSERT().
88 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
89 If Length > 0 and Source is NULL, then ASSERT().
90 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
91 If Source and Destination overlap, then ASSERT().
92 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
93 PcdMaximumUnicodeStringLength, then ASSERT().
94 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
95 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
98 @param Destination A pointer to a Null-terminated Unicode string.
99 @param Source A pointer to a Null-terminated Unicode string.
100 @param Length The maximum number of Unicode characters to copy.
108 OUT CHAR16
*Destination
,
109 IN CONST CHAR16
*Source
,
120 // Destination cannot be NULL if Length is not zero
122 ASSERT (Destination
!= NULL
);
123 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
126 // Destination and source cannot overlap
128 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
129 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
131 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
132 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
135 ReturnValue
= Destination
;
137 while ((*Source
!= L
'\0') && (Length
> 0)) {
138 *(Destination
++) = *(Source
++);
142 ZeroMem (Destination
, Length
* sizeof (*Destination
));
148 Returns the length of a Null-terminated Unicode string.
150 This function returns the number of Unicode characters in the Null-terminated
151 Unicode string specified by String.
153 If String is NULL, then ASSERT().
154 If String is not aligned on a 16-bit boundary, then ASSERT().
155 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
156 PcdMaximumUnicodeStringLength Unicode characters, not including the
157 Null-terminator, then ASSERT().
159 @param String A pointer to a Null-terminated Unicode string.
161 @return The length of String.
167 IN CONST CHAR16
*String
172 ASSERT (String
!= NULL
);
173 ASSERT (((UINTN
) String
& BIT0
) == 0);
175 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
177 // If PcdMaximumUnicodeStringLength is not zero,
178 // length should not more than PcdMaximumUnicodeStringLength
180 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
181 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
188 Returns the size of a Null-terminated Unicode string in bytes, including the
191 This function returns the size, in bytes, of the Null-terminated Unicode string
194 If String is NULL, then ASSERT().
195 If String is not aligned on a 16-bit boundary, then ASSERT().
196 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
197 PcdMaximumUnicodeStringLength Unicode characters, not including the
198 Null-terminator, then ASSERT().
200 @param String A pointer to a Null-terminated Unicode string.
202 @return The size of String.
208 IN CONST CHAR16
*String
211 return (StrLen (String
) + 1) * sizeof (*String
);
215 Compares two Null-terminated Unicode strings, and returns the difference
216 between the first mismatched Unicode characters.
218 This function compares the Null-terminated Unicode string FirstString to the
219 Null-terminated Unicode string SecondString. If FirstString is identical to
220 SecondString, then 0 is returned. Otherwise, the value returned is the first
221 mismatched Unicode character in SecondString subtracted from the first
222 mismatched Unicode character in FirstString.
224 If FirstString is NULL, then ASSERT().
225 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
226 If SecondString is NULL, then ASSERT().
227 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
228 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
229 than PcdMaximumUnicodeStringLength Unicode characters, not including the
230 Null-terminator, then ASSERT().
231 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
232 than PcdMaximumUnicodeStringLength Unicode characters, not including the
233 Null-terminator, then ASSERT().
235 @param FirstString A pointer to a Null-terminated Unicode string.
236 @param SecondString A pointer to a Null-terminated Unicode string.
238 @retval 0 FirstString is identical to SecondString.
239 @return others FirstString is not identical to SecondString.
245 IN CONST CHAR16
*FirstString
,
246 IN CONST CHAR16
*SecondString
250 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
252 ASSERT (StrSize (FirstString
) != 0);
253 ASSERT (StrSize (SecondString
) != 0);
255 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
259 return *FirstString
- *SecondString
;
263 Compares up to a specified length the contents of two Null-terminated Unicode strings,
264 and returns the difference between the first mismatched Unicode characters.
266 This function compares the Null-terminated Unicode string FirstString to the
267 Null-terminated Unicode string SecondString. At most, Length Unicode
268 characters will be compared. If Length is 0, then 0 is returned. If
269 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
270 value returned is the first mismatched Unicode character in SecondString
271 subtracted from the first mismatched Unicode character in FirstString.
273 If Length > 0 and FirstString is NULL, then ASSERT().
274 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
275 If Length > 0 and SecondString is NULL, then ASSERT().
276 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
277 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
278 PcdMaximumUnicodeStringLength, then ASSERT().
279 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
280 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
282 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
283 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
286 @param FirstString A pointer to a Null-terminated Unicode string.
287 @param SecondString A pointer to a Null-terminated Unicode string.
288 @param Length The maximum number of Unicode characters to compare.
290 @retval 0 FirstString is identical to SecondString.
291 @return others FirstString is not identical to SecondString.
297 IN CONST CHAR16
*FirstString
,
298 IN CONST CHAR16
*SecondString
,
307 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
308 // Length tests are performed inside StrLen().
310 ASSERT (StrSize (FirstString
) != 0);
311 ASSERT (StrSize (SecondString
) != 0);
313 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
314 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
317 while ((*FirstString
!= L
'\0') &&
318 (*SecondString
!= L
'\0') &&
319 (*FirstString
== *SecondString
) &&
326 return *FirstString
- *SecondString
;
329 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
332 [ATTENTION] This function will be deprecated for security reason.
334 Concatenates one Null-terminated Unicode string to another Null-terminated
335 Unicode string, and returns the concatenated Unicode string.
337 This function concatenates two Null-terminated Unicode strings. The contents
338 of Null-terminated Unicode string Source are concatenated to the end of
339 Null-terminated Unicode string Destination. The Null-terminated concatenated
340 Unicode String is returned. If Source and Destination overlap, then the
341 results are undefined.
343 If Destination is NULL, then ASSERT().
344 If Destination is not aligned on a 16-bit boundary, then ASSERT().
345 If Source is NULL, then ASSERT().
346 If Source is not aligned on a 16-bit boundary, then ASSERT().
347 If Source and Destination overlap, then ASSERT().
348 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
349 than PcdMaximumUnicodeStringLength Unicode characters, not including the
350 Null-terminator, then ASSERT().
351 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
352 PcdMaximumUnicodeStringLength Unicode characters, not including the
353 Null-terminator, then ASSERT().
354 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
355 and Source results in a Unicode string with more than
356 PcdMaximumUnicodeStringLength Unicode characters, not including the
357 Null-terminator, then ASSERT().
359 @param Destination A pointer to a Null-terminated Unicode string.
360 @param Source A pointer to a Null-terminated Unicode string.
368 IN OUT CHAR16
*Destination
,
369 IN CONST CHAR16
*Source
372 StrCpy (Destination
+ StrLen (Destination
), Source
);
375 // Size of the resulting string should never be zero.
376 // PcdMaximumUnicodeStringLength is tested inside StrLen().
378 ASSERT (StrSize (Destination
) != 0);
383 [ATTENTION] This function will be deprecated for security reason.
385 Concatenates up to a specified length one Null-terminated Unicode to the end
386 of another Null-terminated Unicode string, and returns the concatenated
389 This function concatenates two Null-terminated Unicode strings. The contents
390 of Null-terminated Unicode string Source are concatenated to the end of
391 Null-terminated Unicode string Destination, and Destination is returned. At
392 most, Length Unicode characters are concatenated from Source to the end of
393 Destination, and Destination is always Null-terminated. If Length is 0, then
394 Destination is returned unmodified. If Source and Destination overlap, then
395 the results are undefined.
397 If Destination is NULL, then ASSERT().
398 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
399 If Length > 0 and Source is NULL, then ASSERT().
400 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
401 If Source and Destination overlap, then ASSERT().
402 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
403 PcdMaximumUnicodeStringLength, then ASSERT().
404 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
405 than PcdMaximumUnicodeStringLength Unicode characters, not including the
406 Null-terminator, then ASSERT().
407 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
408 PcdMaximumUnicodeStringLength Unicode characters, not including the
409 Null-terminator, then ASSERT().
410 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
411 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength
412 Unicode characters, not including the Null-terminator, then ASSERT().
414 @param Destination A pointer to a Null-terminated Unicode string.
415 @param Source A pointer to a Null-terminated Unicode string.
416 @param Length The maximum number of Unicode characters to concatenate from
425 IN OUT CHAR16
*Destination
,
426 IN CONST CHAR16
*Source
,
430 UINTN DestinationLen
;
432 DestinationLen
= StrLen (Destination
);
433 StrnCpy (Destination
+ DestinationLen
, Source
, Length
);
434 Destination
[DestinationLen
+ Length
] = L
'\0';
437 // Size of the resulting string should never be zero.
438 // PcdMaximumUnicodeStringLength is tested inside StrLen().
440 ASSERT (StrSize (Destination
) != 0);
446 Returns the first occurrence of a Null-terminated Unicode sub-string
447 in a Null-terminated Unicode string.
449 This function scans the contents of the Null-terminated Unicode string
450 specified by String and returns the first occurrence of SearchString.
451 If SearchString is not found in String, then NULL is returned. If
452 the length of SearchString is zero, then String is
455 If String is NULL, then ASSERT().
456 If String is not aligned on a 16-bit boundary, then ASSERT().
457 If SearchString is NULL, then ASSERT().
458 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
460 If PcdMaximumUnicodeStringLength is not zero, and SearchString
461 or String contains more than PcdMaximumUnicodeStringLength Unicode
462 characters, not including the Null-terminator, then ASSERT().
464 @param String A pointer to a Null-terminated Unicode string.
465 @param SearchString A pointer to a Null-terminated Unicode string to search for.
467 @retval NULL If the SearchString does not appear in String.
468 @return others If there is a match.
474 IN CONST CHAR16
*String
,
475 IN CONST CHAR16
*SearchString
478 CONST CHAR16
*FirstMatch
;
479 CONST CHAR16
*SearchStringTmp
;
482 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
483 // Length tests are performed inside StrLen().
485 ASSERT (StrSize (String
) != 0);
486 ASSERT (StrSize (SearchString
) != 0);
488 if (*SearchString
== L
'\0') {
489 return (CHAR16
*) String
;
492 while (*String
!= L
'\0') {
493 SearchStringTmp
= SearchString
;
496 while ((*String
== *SearchStringTmp
)
497 && (*String
!= L
'\0')) {
502 if (*SearchStringTmp
== L
'\0') {
503 return (CHAR16
*) FirstMatch
;
506 if (*String
== L
'\0') {
510 String
= FirstMatch
+ 1;
517 Check if a Unicode character is a decimal character.
519 This internal function checks if a Unicode character is a
520 decimal character. The valid decimal character is from
523 @param Char The character to check against.
525 @retval TRUE If the Char is a decmial character.
526 @retval FALSE If the Char is not a decmial character.
531 InternalIsDecimalDigitCharacter (
535 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
539 Convert a Unicode character to upper case only if
540 it maps to a valid small-case ASCII character.
542 This internal function only deal with Unicode character
543 which maps to a valid small-case ASCII character, i.e.
544 L'a' to L'z'. For other Unicode character, the input character
545 is returned directly.
547 @param Char The character to convert.
549 @retval LowerCharacter If the Char is with range L'a' to L'z'.
550 @retval Unchanged Otherwise.
555 InternalCharToUpper (
559 if (Char
>= L
'a' && Char
<= L
'z') {
560 return (CHAR16
) (Char
- (L
'a' - L
'A'));
567 Convert a Unicode character to numerical value.
569 This internal function only deal with Unicode character
570 which maps to a valid hexadecimal ASII character, i.e.
571 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
572 Unicode character, the value returned does not make sense.
574 @param Char The character to convert.
576 @return The numerical value converted.
581 InternalHexCharToUintn (
585 if (InternalIsDecimalDigitCharacter (Char
)) {
589 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
593 Check if a Unicode character is a hexadecimal character.
595 This internal function checks if a Unicode character is a
596 decimal character. The valid hexadecimal character is
597 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
600 @param Char The character to check against.
602 @retval TRUE If the Char is a hexadecmial character.
603 @retval FALSE If the Char is not a hexadecmial character.
608 InternalIsHexaDecimalDigitCharacter (
613 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
614 (Char
>= L
'A' && Char
<= L
'F') ||
615 (Char
>= L
'a' && Char
<= L
'f'));
619 Convert a Null-terminated Unicode decimal string to a value of
622 This function returns a value of type UINTN by interpreting the contents
623 of the Unicode string specified by String as a decimal number. The format
624 of the input Unicode string String is:
626 [spaces] [decimal digits].
628 The valid decimal digit character is in the range [0-9]. The
629 function will ignore the pad space, which includes spaces or
630 tab characters, before [decimal digits]. The running zero in the
631 beginning of [decimal digits] will be ignored. Then, the function
632 stops at the first character that is a not a valid decimal character
633 or a Null-terminator, whichever one comes first.
635 If String is NULL, then ASSERT().
636 If String is not aligned in a 16-bit boundary, then ASSERT().
637 If String has only pad spaces, then 0 is returned.
638 If String has no pad spaces or valid decimal digits,
640 If the number represented by String overflows according
641 to the range defined by UINTN, then ASSERT().
643 If PcdMaximumUnicodeStringLength is not zero, and String contains
644 more than PcdMaximumUnicodeStringLength Unicode characters, not including
645 the Null-terminator, then ASSERT().
647 @param String A pointer to a Null-terminated Unicode string.
649 @retval Value translated from String.
655 IN CONST CHAR16
*String
661 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
662 // Length tests are performed inside StrLen().
664 ASSERT (StrSize (String
) != 0);
667 // Ignore the pad spaces (space or tab)
669 while ((*String
== L
' ') || (*String
== L
'\t')) {
674 // Ignore leading Zeros after the spaces
676 while (*String
== L
'0') {
682 while (InternalIsDecimalDigitCharacter (*String
)) {
684 // If the number represented by String overflows according
685 // to the range defined by UINTN, then ASSERT().
687 ASSERT (Result
<= ((((UINTN
) ~0) - (*String
- L
'0')) / 10));
689 Result
= Result
* 10 + (*String
- L
'0');
698 Convert a Null-terminated Unicode decimal string to a value of
701 This function returns a value of type UINT64 by interpreting the contents
702 of the Unicode string specified by String as a decimal number. The format
703 of the input Unicode string String is:
705 [spaces] [decimal digits].
707 The valid decimal digit character is in the range [0-9]. The
708 function will ignore the pad space, which includes spaces or
709 tab characters, before [decimal digits]. The running zero in the
710 beginning of [decimal digits] will be ignored. Then, the function
711 stops at the first character that is a not a valid decimal character
712 or a Null-terminator, whichever one comes first.
714 If String is NULL, then ASSERT().
715 If String is not aligned in a 16-bit boundary, then ASSERT().
716 If String has only pad spaces, then 0 is returned.
717 If String has no pad spaces or valid decimal digits,
719 If the number represented by String overflows according
720 to the range defined by UINT64, then ASSERT().
722 If PcdMaximumUnicodeStringLength is not zero, and String contains
723 more than PcdMaximumUnicodeStringLength Unicode characters, not including
724 the Null-terminator, then ASSERT().
726 @param String A pointer to a Null-terminated Unicode string.
728 @retval Value translated from String.
734 IN CONST CHAR16
*String
740 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
741 // Length tests are performed inside StrLen().
743 ASSERT (StrSize (String
) != 0);
746 // Ignore the pad spaces (space or tab)
748 while ((*String
== L
' ') || (*String
== L
'\t')) {
753 // Ignore leading Zeros after the spaces
755 while (*String
== L
'0') {
761 while (InternalIsDecimalDigitCharacter (*String
)) {
763 // If the number represented by String overflows according
764 // to the range defined by UINTN, then ASSERT().
766 ASSERT (Result
<= DivU64x32 (((UINT64
) ~0) - (*String
- L
'0') , 10));
768 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
776 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
778 This function returns a value of type UINTN by interpreting the contents
779 of the Unicode string specified by String as a hexadecimal number.
780 The format of the input Unicode string String is:
782 [spaces][zeros][x][hexadecimal digits].
784 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
785 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
786 If "x" appears in the input string, it must be prefixed with at least one 0.
787 The function will ignore the pad space, which includes spaces or tab characters,
788 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
789 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
790 first valid hexadecimal digit. Then, the function stops at the first character that is
791 a not a valid hexadecimal character or NULL, whichever one comes first.
793 If String is NULL, then ASSERT().
794 If String is not aligned in a 16-bit boundary, then ASSERT().
795 If String has only pad spaces, then zero is returned.
796 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
797 then zero is returned.
798 If the number represented by String overflows according to the range defined by
799 UINTN, then ASSERT().
801 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
802 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
805 @param String A pointer to a Null-terminated Unicode string.
807 @retval Value translated from String.
813 IN CONST CHAR16
*String
819 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
820 // Length tests are performed inside StrLen().
822 ASSERT (StrSize (String
) != 0);
825 // Ignore the pad spaces (space or tab)
827 while ((*String
== L
' ') || (*String
== L
'\t')) {
832 // Ignore leading Zeros after the spaces
834 while (*String
== L
'0') {
838 if (InternalCharToUpper (*String
) == L
'X') {
839 if (*(String
- 1) != L
'0') {
850 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
852 // If the Hex Number represented by String overflows according
853 // to the range defined by UINTN, then ASSERT().
855 ASSERT (Result
<= ((((UINTN
) ~0) - InternalHexCharToUintn (*String
)) >> 4));
857 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
866 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
868 This function returns a value of type UINT64 by interpreting the contents
869 of the Unicode string specified by String as a hexadecimal number.
870 The format of the input Unicode string String is
872 [spaces][zeros][x][hexadecimal digits].
874 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
875 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
876 If "x" appears in the input string, it must be prefixed with at least one 0.
877 The function will ignore the pad space, which includes spaces or tab characters,
878 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
879 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
880 first valid hexadecimal digit. Then, the function stops at the first character that is
881 a not a valid hexadecimal character or NULL, whichever one comes first.
883 If String is NULL, then ASSERT().
884 If String is not aligned in a 16-bit boundary, then ASSERT().
885 If String has only pad spaces, then zero is returned.
886 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
887 then zero is returned.
888 If the number represented by String overflows according to the range defined by
889 UINT64, then ASSERT().
891 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
892 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
895 @param String A pointer to a Null-terminated Unicode string.
897 @retval Value translated from String.
903 IN CONST CHAR16
*String
909 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
910 // Length tests are performed inside StrLen().
912 ASSERT (StrSize (String
) != 0);
915 // Ignore the pad spaces (space or tab)
917 while ((*String
== L
' ') || (*String
== L
'\t')) {
922 // Ignore leading Zeros after the spaces
924 while (*String
== L
'0') {
928 if (InternalCharToUpper (*String
) == L
'X') {
929 ASSERT (*(String
- 1) == L
'0');
930 if (*(String
- 1) != L
'0') {
941 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
943 // If the Hex Number represented by String overflows according
944 // to the range defined by UINTN, then ASSERT().
946 ASSERT (Result
<= RShiftU64 (((UINT64
) ~0) - InternalHexCharToUintn (*String
) , 4));
948 Result
= LShiftU64 (Result
, 4);
949 Result
= Result
+ InternalHexCharToUintn (*String
);
957 Check if a ASCII character is a decimal character.
959 This internal function checks if a Unicode character is a
960 decimal character. The valid decimal character is from
963 @param Char The character to check against.
965 @retval TRUE If the Char is a decmial character.
966 @retval FALSE If the Char is not a decmial character.
971 InternalAsciiIsDecimalDigitCharacter (
975 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
979 Check if a ASCII character is a hexadecimal character.
981 This internal function checks if a ASCII character is a
982 decimal character. The valid hexadecimal character is
983 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
986 @param Char The character to check against.
988 @retval TRUE If the Char is a hexadecmial character.
989 @retval FALSE If the Char is not a hexadecmial character.
994 InternalAsciiIsHexaDecimalDigitCharacter (
999 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
1000 (Char
>= 'A' && Char
<= 'F') ||
1001 (Char
>= 'a' && Char
<= 'f'));
1004 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1007 [ATTENTION] This function is deprecated for security reason.
1009 Convert a Null-terminated Unicode string to a Null-terminated
1010 ASCII string and returns the ASCII string.
1012 This function converts the content of the Unicode string Source
1013 to the ASCII string Destination by copying the lower 8 bits of
1014 each Unicode character. It returns Destination.
1016 The caller is responsible to make sure Destination points to a buffer with size
1017 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
1019 If any Unicode characters in Source contain non-zero value in
1020 the upper 8 bits, then ASSERT().
1022 If Destination is NULL, then ASSERT().
1023 If Source is NULL, then ASSERT().
1024 If Source is not aligned on a 16-bit boundary, then ASSERT().
1025 If Source and Destination overlap, then ASSERT().
1027 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1028 more than PcdMaximumUnicodeStringLength Unicode characters, not including
1029 the Null-terminator, then ASSERT().
1031 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1032 than PcdMaximumAsciiStringLength Unicode characters, not including the
1033 Null-terminator, then ASSERT().
1035 @param Source A pointer to a Null-terminated Unicode string.
1036 @param Destination A pointer to a Null-terminated ASCII string.
1038 @return Destination.
1043 UnicodeStrToAsciiStr (
1044 IN CONST CHAR16
*Source
,
1045 OUT CHAR8
*Destination
1050 ASSERT (Destination
!= NULL
);
1053 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1054 // Length tests are performed inside StrLen().
1056 ASSERT (StrSize (Source
) != 0);
1059 // Source and Destination should not overlap
1061 ASSERT ((UINTN
) (Destination
- (CHAR8
*) Source
) >= StrSize (Source
));
1062 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1065 ReturnValue
= Destination
;
1066 while (*Source
!= '\0') {
1068 // If any Unicode characters in Source contain
1069 // non-zero value in the upper 8 bits, then ASSERT().
1071 ASSERT (*Source
< 0x100);
1072 *(Destination
++) = (CHAR8
) *(Source
++);
1075 *Destination
= '\0';
1078 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1079 // Length tests are performed inside AsciiStrLen().
1081 ASSERT (AsciiStrSize (ReturnValue
) != 0);
1087 [ATTENTION] This function will be deprecated for security reason.
1089 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1090 string and returns the new ASCII string.
1092 This function copies the contents of the ASCII string Source to the ASCII
1093 string Destination, and returns Destination. If Source and Destination
1094 overlap, then the results are undefined.
1096 If Destination is NULL, then ASSERT().
1097 If Source is NULL, then ASSERT().
1098 If Source and Destination overlap, then ASSERT().
1099 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1100 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1103 @param Destination A pointer to a Null-terminated ASCII string.
1104 @param Source A pointer to a Null-terminated ASCII string.
1112 OUT CHAR8
*Destination
,
1113 IN CONST CHAR8
*Source
1119 // Destination cannot be NULL
1121 ASSERT (Destination
!= NULL
);
1124 // Destination and source cannot overlap
1126 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1127 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1129 ReturnValue
= Destination
;
1130 while (*Source
!= 0) {
1131 *(Destination
++) = *(Source
++);
1138 [ATTENTION] This function will be deprecated for security reason.
1140 Copies up to a specified length one Null-terminated ASCII string to another
1141 Null-terminated ASCII string and returns the new ASCII string.
1143 This function copies the contents of the ASCII string Source to the ASCII
1144 string Destination, and returns Destination. At most, Length ASCII characters
1145 are copied from Source to Destination. If Length is 0, then Destination is
1146 returned unmodified. If Length is greater that the number of ASCII characters
1147 in Source, then Destination is padded with Null ASCII characters. If Source
1148 and Destination overlap, then the results are undefined.
1150 If Destination is NULL, then ASSERT().
1151 If Source is NULL, then ASSERT().
1152 If Source and Destination overlap, then ASSERT().
1153 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1154 PcdMaximumAsciiStringLength, then ASSERT().
1155 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1156 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1159 @param Destination A pointer to a Null-terminated ASCII string.
1160 @param Source A pointer to a Null-terminated ASCII string.
1161 @param Length The maximum number of ASCII characters to copy.
1169 OUT CHAR8
*Destination
,
1170 IN CONST CHAR8
*Source
,
1181 // Destination cannot be NULL
1183 ASSERT (Destination
!= NULL
);
1186 // Destination and source cannot overlap
1188 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1189 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1191 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1192 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1195 ReturnValue
= Destination
;
1197 while (*Source
!= 0 && Length
> 0) {
1198 *(Destination
++) = *(Source
++);
1202 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1208 Returns the length of a Null-terminated ASCII string.
1210 This function returns the number of ASCII characters in the Null-terminated
1211 ASCII string specified by String.
1213 If Length > 0 and Destination is NULL, then ASSERT().
1214 If Length > 0 and Source is NULL, then ASSERT().
1215 If PcdMaximumAsciiStringLength is not zero and String contains more than
1216 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1219 @param String A pointer to a Null-terminated ASCII string.
1221 @return The length of String.
1227 IN CONST CHAR8
*String
1232 ASSERT (String
!= NULL
);
1234 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1236 // If PcdMaximumUnicodeStringLength is not zero,
1237 // length should not more than PcdMaximumUnicodeStringLength
1239 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1240 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1247 Returns the size of a Null-terminated ASCII string in bytes, including the
1250 This function returns the size, in bytes, of the Null-terminated ASCII string
1251 specified by String.
1253 If String is NULL, then ASSERT().
1254 If PcdMaximumAsciiStringLength is not zero and String contains more than
1255 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1258 @param String A pointer to a Null-terminated ASCII string.
1260 @return The size of String.
1266 IN CONST CHAR8
*String
1269 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1273 Compares two Null-terminated ASCII strings, and returns the difference
1274 between the first mismatched ASCII characters.
1276 This function compares the Null-terminated ASCII string FirstString to the
1277 Null-terminated ASCII string SecondString. If FirstString is identical to
1278 SecondString, then 0 is returned. Otherwise, the value returned is the first
1279 mismatched ASCII character in SecondString subtracted from the first
1280 mismatched ASCII character in FirstString.
1282 If FirstString is NULL, then ASSERT().
1283 If SecondString is NULL, then ASSERT().
1284 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1285 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1287 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1288 than PcdMaximumAsciiStringLength ASCII characters, not including the
1289 Null-terminator, then ASSERT().
1291 @param FirstString A pointer to a Null-terminated ASCII string.
1292 @param SecondString A pointer to a Null-terminated ASCII string.
1294 @retval ==0 FirstString is identical to SecondString.
1295 @retval !=0 FirstString is not identical to SecondString.
1301 IN CONST CHAR8
*FirstString
,
1302 IN CONST CHAR8
*SecondString
1306 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1308 ASSERT (AsciiStrSize (FirstString
));
1309 ASSERT (AsciiStrSize (SecondString
));
1311 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1316 return *FirstString
- *SecondString
;
1320 Converts a lowercase Ascii character to upper one.
1322 If Chr is lowercase Ascii character, then converts it to upper one.
1324 If Value >= 0xA0, then ASSERT().
1325 If (Value & 0x0F) >= 0x0A, then ASSERT().
1327 @param Chr one Ascii character
1329 @return The uppercase value of Ascii character
1334 InternalBaseLibAsciiToUpper (
1338 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1342 Convert a ASCII character to numerical value.
1344 This internal function only deal with Unicode character
1345 which maps to a valid hexadecimal ASII character, i.e.
1346 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1347 ASCII character, the value returned does not make sense.
1349 @param Char The character to convert.
1351 @return The numerical value converted.
1356 InternalAsciiHexCharToUintn (
1360 if (InternalIsDecimalDigitCharacter (Char
)) {
1364 return (UINTN
) (10 + InternalBaseLibAsciiToUpper (Char
) - 'A');
1369 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1370 and returns the difference between the first mismatched ASCII characters.
1372 This function performs a case insensitive comparison of the Null-terminated
1373 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1374 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1375 value returned is the first mismatched lower case ASCII character in
1376 SecondString subtracted from the first mismatched lower case ASCII character
1379 If FirstString is NULL, then ASSERT().
1380 If SecondString is NULL, then ASSERT().
1381 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1382 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1384 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1385 than PcdMaximumAsciiStringLength ASCII characters, not including the
1386 Null-terminator, then ASSERT().
1388 @param FirstString A pointer to a Null-terminated ASCII string.
1389 @param SecondString A pointer to a Null-terminated ASCII string.
1391 @retval ==0 FirstString is identical to SecondString using case insensitive
1393 @retval !=0 FirstString is not identical to SecondString using case
1394 insensitive comparisons.
1400 IN CONST CHAR8
*FirstString
,
1401 IN CONST CHAR8
*SecondString
1404 CHAR8 UpperFirstString
;
1405 CHAR8 UpperSecondString
;
1408 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1410 ASSERT (AsciiStrSize (FirstString
));
1411 ASSERT (AsciiStrSize (SecondString
));
1413 UpperFirstString
= InternalBaseLibAsciiToUpper (*FirstString
);
1414 UpperSecondString
= InternalBaseLibAsciiToUpper (*SecondString
);
1415 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1418 UpperFirstString
= InternalBaseLibAsciiToUpper (*FirstString
);
1419 UpperSecondString
= InternalBaseLibAsciiToUpper (*SecondString
);
1422 return UpperFirstString
- UpperSecondString
;
1426 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1427 the difference between the first mismatched ASCII characters.
1429 This function compares the Null-terminated ASCII string FirstString to the
1430 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1431 will be compared. If Length is 0, then 0 is returned. If FirstString is
1432 identical to SecondString, then 0 is returned. Otherwise, the value returned
1433 is the first mismatched ASCII character in SecondString subtracted from the
1434 first mismatched ASCII character in FirstString.
1436 If Length > 0 and FirstString is NULL, then ASSERT().
1437 If Length > 0 and SecondString is NULL, then ASSERT().
1438 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1439 PcdMaximumAsciiStringLength, then ASSERT().
1440 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
1441 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1443 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
1444 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1447 @param FirstString A pointer to a Null-terminated ASCII string.
1448 @param SecondString A pointer to a Null-terminated ASCII string.
1449 @param Length The maximum number of ASCII characters for compare.
1451 @retval ==0 FirstString is identical to SecondString.
1452 @retval !=0 FirstString is not identical to SecondString.
1458 IN CONST CHAR8
*FirstString
,
1459 IN CONST CHAR8
*SecondString
,
1468 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1470 ASSERT (AsciiStrSize (FirstString
));
1471 ASSERT (AsciiStrSize (SecondString
));
1473 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1474 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1477 while ((*FirstString
!= '\0') &&
1478 (*SecondString
!= '\0') &&
1479 (*FirstString
== *SecondString
) &&
1485 return *FirstString
- *SecondString
;
1488 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1491 [ATTENTION] This function will be deprecated for security reason.
1493 Concatenates one Null-terminated ASCII string to another Null-terminated
1494 ASCII string, and returns the concatenated ASCII string.
1496 This function concatenates two Null-terminated ASCII strings. The contents of
1497 Null-terminated ASCII string Source are concatenated to the end of Null-
1498 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1501 If Destination is NULL, then ASSERT().
1502 If Source is NULL, then ASSERT().
1503 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1504 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1506 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1507 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1509 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1510 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1511 ASCII characters, then ASSERT().
1513 @param Destination A pointer to a Null-terminated ASCII string.
1514 @param Source A pointer to a Null-terminated ASCII string.
1522 IN OUT CHAR8
*Destination
,
1523 IN CONST CHAR8
*Source
1526 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1529 // Size of the resulting string should never be zero.
1530 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1532 ASSERT (AsciiStrSize (Destination
) != 0);
1537 [ATTENTION] This function will be deprecated for security reason.
1539 Concatenates up to a specified length one Null-terminated ASCII string to
1540 the end of another Null-terminated ASCII string, and returns the
1541 concatenated ASCII string.
1543 This function concatenates two Null-terminated ASCII strings. The contents
1544 of Null-terminated ASCII string Source are concatenated to the end of Null-
1545 terminated ASCII string Destination, and Destination is returned. At most,
1546 Length ASCII characters are concatenated from Source to the end of
1547 Destination, and Destination is always Null-terminated. If Length is 0, then
1548 Destination is returned unmodified. If Source and Destination overlap, then
1549 the results are undefined.
1551 If Length > 0 and Destination is NULL, then ASSERT().
1552 If Length > 0 and Source is NULL, then ASSERT().
1553 If Source and Destination overlap, then ASSERT().
1554 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1555 PcdMaximumAsciiStringLength, then ASSERT().
1556 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1557 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1559 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1560 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1562 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1563 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1564 ASCII characters, not including the Null-terminator, then ASSERT().
1566 @param Destination A pointer to a Null-terminated ASCII string.
1567 @param Source A pointer to a Null-terminated ASCII string.
1568 @param Length The maximum number of ASCII characters to concatenate from
1577 IN OUT CHAR8
*Destination
,
1578 IN CONST CHAR8
*Source
,
1582 UINTN DestinationLen
;
1584 DestinationLen
= AsciiStrLen (Destination
);
1585 AsciiStrnCpy (Destination
+ DestinationLen
, Source
, Length
);
1586 Destination
[DestinationLen
+ Length
] = '\0';
1589 // Size of the resulting string should never be zero.
1590 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1592 ASSERT (AsciiStrSize (Destination
) != 0);
1598 Returns the first occurrence of a Null-terminated ASCII sub-string
1599 in a Null-terminated ASCII string.
1601 This function scans the contents of the ASCII string specified by String
1602 and returns the first occurrence of SearchString. If SearchString is not
1603 found in String, then NULL is returned. If the length of SearchString is zero,
1604 then String is returned.
1606 If String is NULL, then ASSERT().
1607 If SearchString is NULL, then ASSERT().
1609 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1610 String contains more than PcdMaximumAsciiStringLength Unicode characters
1611 not including the Null-terminator, then ASSERT().
1613 @param String A pointer to a Null-terminated ASCII string.
1614 @param SearchString A pointer to a Null-terminated ASCII string to search for.
1616 @retval NULL If the SearchString does not appear in String.
1617 @retval others If there is a match return the first occurrence of SearchingString.
1618 If the length of SearchString is zero,return String.
1624 IN CONST CHAR8
*String
,
1625 IN CONST CHAR8
*SearchString
1628 CONST CHAR8
*FirstMatch
;
1629 CONST CHAR8
*SearchStringTmp
;
1632 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1634 ASSERT (AsciiStrSize (String
) != 0);
1635 ASSERT (AsciiStrSize (SearchString
) != 0);
1637 if (*SearchString
== '\0') {
1638 return (CHAR8
*) String
;
1641 while (*String
!= '\0') {
1642 SearchStringTmp
= SearchString
;
1643 FirstMatch
= String
;
1645 while ((*String
== *SearchStringTmp
)
1646 && (*String
!= '\0')) {
1651 if (*SearchStringTmp
== '\0') {
1652 return (CHAR8
*) FirstMatch
;
1655 if (*String
== '\0') {
1659 String
= FirstMatch
+ 1;
1666 Convert a Null-terminated ASCII decimal string to a value of type
1669 This function returns a value of type UINTN by interpreting the contents
1670 of the ASCII string String as a decimal number. The format of the input
1671 ASCII string String is:
1673 [spaces] [decimal digits].
1675 The valid decimal digit character is in the range [0-9]. The function will
1676 ignore the pad space, which includes spaces or tab characters, before the digits.
1677 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1678 function stops at the first character that is a not a valid decimal character or
1679 Null-terminator, whichever on comes first.
1681 If String has only pad spaces, then 0 is returned.
1682 If String has no pad spaces or valid decimal digits, then 0 is returned.
1683 If the number represented by String overflows according to the range defined by
1684 UINTN, then ASSERT().
1685 If String is NULL, then ASSERT().
1686 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1687 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1690 @param String A pointer to a Null-terminated ASCII string.
1692 @retval Value translated from String.
1697 AsciiStrDecimalToUintn (
1698 IN CONST CHAR8
*String
1704 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1706 ASSERT (AsciiStrSize (String
) != 0);
1709 // Ignore the pad spaces (space or tab)
1711 while ((*String
== ' ') || (*String
== '\t' )) {
1716 // Ignore leading Zeros after the spaces
1718 while (*String
== '0') {
1724 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1726 // If the number represented by String overflows according
1727 // to the range defined by UINTN, then ASSERT().
1729 ASSERT (Result
<= ((((UINTN
) ~0) - (*String
- L
'0')) / 10));
1731 Result
= Result
* 10 + (*String
- '0');
1740 Convert a Null-terminated ASCII decimal string to a value of type
1743 This function returns a value of type UINT64 by interpreting the contents
1744 of the ASCII string String as a decimal number. The format of the input
1745 ASCII string String is:
1747 [spaces] [decimal digits].
1749 The valid decimal digit character is in the range [0-9]. The function will
1750 ignore the pad space, which includes spaces or tab characters, before the digits.
1751 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1752 function stops at the first character that is a not a valid decimal character or
1753 Null-terminator, whichever on comes first.
1755 If String has only pad spaces, then 0 is returned.
1756 If String has no pad spaces or valid decimal digits, then 0 is returned.
1757 If the number represented by String overflows according to the range defined by
1758 UINT64, then ASSERT().
1759 If String is NULL, then ASSERT().
1760 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1761 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1764 @param String A pointer to a Null-terminated ASCII string.
1766 @retval Value translated from String.
1771 AsciiStrDecimalToUint64 (
1772 IN CONST CHAR8
*String
1778 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1780 ASSERT (AsciiStrSize (String
) != 0);
1783 // Ignore the pad spaces (space or tab)
1785 while ((*String
== ' ') || (*String
== '\t' )) {
1790 // Ignore leading Zeros after the spaces
1792 while (*String
== '0') {
1798 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1800 // If the number represented by String overflows according
1801 // to the range defined by UINTN, then ASSERT().
1803 ASSERT (Result
<= DivU64x32 (((UINT64
) ~0) - (*String
- L
'0') , 10));
1805 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1813 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1815 This function returns a value of type UINTN by interpreting the contents of
1816 the ASCII string String as a hexadecimal number. The format of the input ASCII
1819 [spaces][zeros][x][hexadecimal digits].
1821 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1822 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1823 appears in the input string, it must be prefixed with at least one 0. The function
1824 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1825 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1826 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1827 digit. Then, the function stops at the first character that is a not a valid
1828 hexadecimal character or Null-terminator, whichever on comes first.
1830 If String has only pad spaces, then 0 is returned.
1831 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1834 If the number represented by String overflows according to the range defined by UINTN,
1836 If String is NULL, then ASSERT().
1837 If PcdMaximumAsciiStringLength is not zero,
1838 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1839 the Null-terminator, then ASSERT().
1841 @param String A pointer to a Null-terminated ASCII string.
1843 @retval Value translated from String.
1848 AsciiStrHexToUintn (
1849 IN CONST CHAR8
*String
1855 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1857 ASSERT (AsciiStrSize (String
) != 0);
1860 // Ignore the pad spaces (space or tab)
1862 while ((*String
== ' ') || (*String
== '\t' )) {
1867 // Ignore leading Zeros after the spaces
1869 while (*String
== '0') {
1873 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1874 ASSERT (*(String
- 1) == '0');
1875 if (*(String
- 1) != '0') {
1886 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1888 // If the Hex Number represented by String overflows according
1889 // to the range defined by UINTN, then ASSERT().
1891 ASSERT (Result
<= ((((UINTN
) ~0) - InternalHexCharToUintn (*String
)) >> 4));
1893 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1902 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1904 This function returns a value of type UINT64 by interpreting the contents of
1905 the ASCII string String as a hexadecimal number. The format of the input ASCII
1908 [spaces][zeros][x][hexadecimal digits].
1910 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1911 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1912 appears in the input string, it must be prefixed with at least one 0. The function
1913 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1914 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1915 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1916 digit. Then, the function stops at the first character that is a not a valid
1917 hexadecimal character or Null-terminator, whichever on comes first.
1919 If String has only pad spaces, then 0 is returned.
1920 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1923 If the number represented by String overflows according to the range defined by UINT64,
1925 If String is NULL, then ASSERT().
1926 If PcdMaximumAsciiStringLength is not zero,
1927 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1928 the Null-terminator, then ASSERT().
1930 @param String A pointer to a Null-terminated ASCII string.
1932 @retval Value translated from String.
1937 AsciiStrHexToUint64 (
1938 IN CONST CHAR8
*String
1944 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1946 ASSERT (AsciiStrSize (String
) != 0);
1949 // Ignore the pad spaces (space or tab) and leading Zeros
1952 // Ignore the pad spaces (space or tab)
1954 while ((*String
== ' ') || (*String
== '\t' )) {
1959 // Ignore leading Zeros after the spaces
1961 while (*String
== '0') {
1965 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1966 ASSERT (*(String
- 1) == '0');
1967 if (*(String
- 1) != '0') {
1978 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1980 // If the Hex Number represented by String overflows according
1981 // to the range defined by UINTN, then ASSERT().
1983 ASSERT (Result
<= RShiftU64 (((UINT64
) ~0) - InternalHexCharToUintn (*String
) , 4));
1985 Result
= LShiftU64 (Result
, 4);
1986 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1993 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1996 [ATTENTION] This function is deprecated for security reason.
1998 Convert one Null-terminated ASCII string to a Null-terminated
1999 Unicode string and returns the Unicode string.
2001 This function converts the contents of the ASCII string Source to the Unicode
2002 string Destination, and returns Destination. The function terminates the
2003 Unicode string Destination by appending a Null-terminator character at the end.
2004 The caller is responsible to make sure Destination points to a buffer with size
2005 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
2007 If Destination is NULL, then ASSERT().
2008 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2009 If Source is NULL, then ASSERT().
2010 If Source and Destination overlap, then ASSERT().
2011 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
2012 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2014 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
2015 PcdMaximumUnicodeStringLength ASCII characters not including the
2016 Null-terminator, then ASSERT().
2018 @param Source A pointer to a Null-terminated ASCII string.
2019 @param Destination A pointer to a Null-terminated Unicode string.
2021 @return Destination.
2026 AsciiStrToUnicodeStr (
2027 IN CONST CHAR8
*Source
,
2028 OUT CHAR16
*Destination
2031 CHAR16
*ReturnValue
;
2033 ASSERT (Destination
!= NULL
);
2036 // ASSERT Source is less long than PcdMaximumAsciiStringLength
2038 ASSERT (AsciiStrSize (Source
) != 0);
2041 // Source and Destination should not overlap
2043 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
2044 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) >= (AsciiStrSize (Source
) * sizeof (CHAR16
)));
2047 ReturnValue
= Destination
;
2048 while (*Source
!= '\0') {
2049 *(Destination
++) = (CHAR16
) *(Source
++);
2052 // End the Destination with a NULL.
2054 *Destination
= '\0';
2057 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
2059 ASSERT (StrSize (ReturnValue
) != 0);
2067 Converts an 8-bit value to an 8-bit BCD value.
2069 Converts the 8-bit value specified by Value to BCD. The BCD value is
2072 If Value >= 100, then ASSERT().
2074 @param Value The 8-bit value to convert to BCD. Range 0..99.
2076 @return The BCD value.
2085 ASSERT (Value
< 100);
2086 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2090 Converts an 8-bit BCD value to an 8-bit value.
2092 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2095 If Value >= 0xA0, then ASSERT().
2096 If (Value & 0x0F) >= 0x0A, then ASSERT().
2098 @param Value The 8-bit BCD value to convert to an 8-bit value.
2100 @return The 8-bit value is returned.
2109 ASSERT (Value
< 0xa0);
2110 ASSERT ((Value
& 0xf) < 0xa);
2111 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));