2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2014, 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 (*FirstString
== *SecondString
) &&
325 return *FirstString
- *SecondString
;
328 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
331 [ATTENTION] This function will be deprecated for security reason.
333 Concatenates one Null-terminated Unicode string to another Null-terminated
334 Unicode string, and returns the concatenated Unicode string.
336 This function concatenates two Null-terminated Unicode strings. The contents
337 of Null-terminated Unicode string Source are concatenated to the end of
338 Null-terminated Unicode string Destination. The Null-terminated concatenated
339 Unicode String is returned. If Source and Destination overlap, then the
340 results are undefined.
342 If Destination is NULL, then ASSERT().
343 If Destination is not aligned on a 16-bit boundary, then ASSERT().
344 If Source is NULL, then ASSERT().
345 If Source is not aligned on a 16-bit boundary, then ASSERT().
346 If Source and Destination overlap, then ASSERT().
347 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
348 than PcdMaximumUnicodeStringLength Unicode characters, not including the
349 Null-terminator, then ASSERT().
350 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
351 PcdMaximumUnicodeStringLength Unicode characters, not including the
352 Null-terminator, then ASSERT().
353 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
354 and Source results in a Unicode string with more than
355 PcdMaximumUnicodeStringLength Unicode characters, not including the
356 Null-terminator, then ASSERT().
358 @param Destination A pointer to a Null-terminated Unicode string.
359 @param Source A pointer to a Null-terminated Unicode string.
367 IN OUT CHAR16
*Destination
,
368 IN CONST CHAR16
*Source
371 StrCpy (Destination
+ StrLen (Destination
), Source
);
374 // Size of the resulting string should never be zero.
375 // PcdMaximumUnicodeStringLength is tested inside StrLen().
377 ASSERT (StrSize (Destination
) != 0);
382 [ATTENTION] This function will be deprecated for security reason.
384 Concatenates up to a specified length one Null-terminated Unicode to the end
385 of another Null-terminated Unicode string, and returns the concatenated
388 This function concatenates two Null-terminated Unicode strings. The contents
389 of Null-terminated Unicode string Source are concatenated to the end of
390 Null-terminated Unicode string Destination, and Destination is returned. At
391 most, Length Unicode characters are concatenated from Source to the end of
392 Destination, and Destination is always Null-terminated. If Length is 0, then
393 Destination is returned unmodified. If Source and Destination overlap, then
394 the results are undefined.
396 If Destination is NULL, then ASSERT().
397 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
398 If Length > 0 and Source is NULL, then ASSERT().
399 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
400 If Source and Destination overlap, then ASSERT().
401 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
402 PcdMaximumUnicodeStringLength, then ASSERT().
403 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
404 than PcdMaximumUnicodeStringLength Unicode characters, not including the
405 Null-terminator, then ASSERT().
406 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
407 PcdMaximumUnicodeStringLength Unicode characters, not including the
408 Null-terminator, then ASSERT().
409 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
410 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength
411 Unicode characters, not including the Null-terminator, then ASSERT().
413 @param Destination A pointer to a Null-terminated Unicode string.
414 @param Source A pointer to a Null-terminated Unicode string.
415 @param Length The maximum number of Unicode characters to concatenate from
424 IN OUT CHAR16
*Destination
,
425 IN CONST CHAR16
*Source
,
429 UINTN DestinationLen
;
431 DestinationLen
= StrLen (Destination
);
432 StrnCpy (Destination
+ DestinationLen
, Source
, Length
);
433 Destination
[DestinationLen
+ Length
] = L
'\0';
436 // Size of the resulting string should never be zero.
437 // PcdMaximumUnicodeStringLength is tested inside StrLen().
439 ASSERT (StrSize (Destination
) != 0);
445 Returns the first occurrence of a Null-terminated Unicode sub-string
446 in a Null-terminated Unicode string.
448 This function scans the contents of the Null-terminated Unicode string
449 specified by String and returns the first occurrence of SearchString.
450 If SearchString is not found in String, then NULL is returned. If
451 the length of SearchString is zero, then String is
454 If String is NULL, then ASSERT().
455 If String is not aligned on a 16-bit boundary, then ASSERT().
456 If SearchString is NULL, then ASSERT().
457 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
459 If PcdMaximumUnicodeStringLength is not zero, and SearchString
460 or String contains more than PcdMaximumUnicodeStringLength Unicode
461 characters, not including the Null-terminator, then ASSERT().
463 @param String A pointer to a Null-terminated Unicode string.
464 @param SearchString A pointer to a Null-terminated Unicode string to search for.
466 @retval NULL If the SearchString does not appear in String.
467 @return others If there is a match.
473 IN CONST CHAR16
*String
,
474 IN CONST CHAR16
*SearchString
477 CONST CHAR16
*FirstMatch
;
478 CONST CHAR16
*SearchStringTmp
;
481 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
482 // Length tests are performed inside StrLen().
484 ASSERT (StrSize (String
) != 0);
485 ASSERT (StrSize (SearchString
) != 0);
487 if (*SearchString
== L
'\0') {
488 return (CHAR16
*) String
;
491 while (*String
!= L
'\0') {
492 SearchStringTmp
= SearchString
;
495 while ((*String
== *SearchStringTmp
)
496 && (*String
!= L
'\0')) {
501 if (*SearchStringTmp
== L
'\0') {
502 return (CHAR16
*) FirstMatch
;
505 if (*String
== L
'\0') {
509 String
= FirstMatch
+ 1;
516 Check if a Unicode character is a decimal character.
518 This internal function checks if a Unicode character is a
519 decimal character. The valid decimal character is from
522 @param Char The character to check against.
524 @retval TRUE If the Char is a decmial character.
525 @retval FALSE If the Char is not a decmial character.
530 InternalIsDecimalDigitCharacter (
534 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
538 Convert a Unicode character to upper case only if
539 it maps to a valid small-case ASCII character.
541 This internal function only deal with Unicode character
542 which maps to a valid small-case ASCII character, i.e.
543 L'a' to L'z'. For other Unicode character, the input character
544 is returned directly.
546 @param Char The character to convert.
548 @retval LowerCharacter If the Char is with range L'a' to L'z'.
549 @retval Unchanged Otherwise.
554 InternalCharToUpper (
558 if (Char
>= L
'a' && Char
<= L
'z') {
559 return (CHAR16
) (Char
- (L
'a' - L
'A'));
566 Convert a Unicode character to numerical value.
568 This internal function only deal with Unicode character
569 which maps to a valid hexadecimal ASII character, i.e.
570 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
571 Unicode character, the value returned does not make sense.
573 @param Char The character to convert.
575 @return The numerical value converted.
580 InternalHexCharToUintn (
584 if (InternalIsDecimalDigitCharacter (Char
)) {
588 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
592 Check if a Unicode character is a hexadecimal character.
594 This internal function checks if a Unicode character is a
595 decimal character. The valid hexadecimal character is
596 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
599 @param Char The character to check against.
601 @retval TRUE If the Char is a hexadecmial character.
602 @retval FALSE If the Char is not a hexadecmial character.
607 InternalIsHexaDecimalDigitCharacter (
612 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
613 (Char
>= L
'A' && Char
<= L
'F') ||
614 (Char
>= L
'a' && Char
<= L
'f'));
618 Convert a Null-terminated Unicode decimal string to a value of
621 This function returns a value of type UINTN by interpreting the contents
622 of the Unicode string specified by String as a decimal number. The format
623 of the input Unicode string String is:
625 [spaces] [decimal digits].
627 The valid decimal digit character is in the range [0-9]. The
628 function will ignore the pad space, which includes spaces or
629 tab characters, before [decimal digits]. The running zero in the
630 beginning of [decimal digits] will be ignored. Then, the function
631 stops at the first character that is a not a valid decimal character
632 or a Null-terminator, whichever one comes first.
634 If String is NULL, then ASSERT().
635 If String is not aligned in a 16-bit boundary, then ASSERT().
636 If String has only pad spaces, then 0 is returned.
637 If String has no pad spaces or valid decimal digits,
639 If the number represented by String overflows according
640 to the range defined by UINTN, then ASSERT().
642 If PcdMaximumUnicodeStringLength is not zero, and String contains
643 more than PcdMaximumUnicodeStringLength Unicode characters, not including
644 the Null-terminator, then ASSERT().
646 @param String A pointer to a Null-terminated Unicode string.
648 @retval Value translated from String.
654 IN CONST CHAR16
*String
660 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
661 // Length tests are performed inside StrLen().
663 ASSERT (StrSize (String
) != 0);
666 // Ignore the pad spaces (space or tab)
668 while ((*String
== L
' ') || (*String
== L
'\t')) {
673 // Ignore leading Zeros after the spaces
675 while (*String
== L
'0') {
681 while (InternalIsDecimalDigitCharacter (*String
)) {
683 // If the number represented by String overflows according
684 // to the range defined by UINTN, then ASSERT().
686 ASSERT (Result
<= ((((UINTN
) ~0) - (*String
- L
'0')) / 10));
688 Result
= Result
* 10 + (*String
- L
'0');
697 Convert a Null-terminated Unicode decimal string to a value of
700 This function returns a value of type UINT64 by interpreting the contents
701 of the Unicode string specified by String as a decimal number. The format
702 of the input Unicode string String is:
704 [spaces] [decimal digits].
706 The valid decimal digit character is in the range [0-9]. The
707 function will ignore the pad space, which includes spaces or
708 tab characters, before [decimal digits]. The running zero in the
709 beginning of [decimal digits] will be ignored. Then, the function
710 stops at the first character that is a not a valid decimal character
711 or a Null-terminator, whichever one comes first.
713 If String is NULL, then ASSERT().
714 If String is not aligned in a 16-bit boundary, then ASSERT().
715 If String has only pad spaces, then 0 is returned.
716 If String has no pad spaces or valid decimal digits,
718 If the number represented by String overflows according
719 to the range defined by UINT64, then ASSERT().
721 If PcdMaximumUnicodeStringLength is not zero, and String contains
722 more than PcdMaximumUnicodeStringLength Unicode characters, not including
723 the Null-terminator, then ASSERT().
725 @param String A pointer to a Null-terminated Unicode string.
727 @retval Value translated from String.
733 IN CONST CHAR16
*String
739 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
740 // Length tests are performed inside StrLen().
742 ASSERT (StrSize (String
) != 0);
745 // Ignore the pad spaces (space or tab)
747 while ((*String
== L
' ') || (*String
== L
'\t')) {
752 // Ignore leading Zeros after the spaces
754 while (*String
== L
'0') {
760 while (InternalIsDecimalDigitCharacter (*String
)) {
762 // If the number represented by String overflows according
763 // to the range defined by UINTN, then ASSERT().
765 ASSERT (Result
<= DivU64x32 (((UINT64
) ~0) - (*String
- L
'0') , 10));
767 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
775 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
777 This function returns a value of type UINTN by interpreting the contents
778 of the Unicode string specified by String as a hexadecimal number.
779 The format of the input Unicode string String is:
781 [spaces][zeros][x][hexadecimal digits].
783 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
784 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
785 If "x" appears in the input string, it must be prefixed with at least one 0.
786 The function will ignore the pad space, which includes spaces or tab characters,
787 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
788 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
789 first valid hexadecimal digit. Then, the function stops at the first character that is
790 a not a valid hexadecimal character or NULL, whichever one comes first.
792 If String is NULL, then ASSERT().
793 If String is not aligned in a 16-bit boundary, then ASSERT().
794 If String has only pad spaces, then zero is returned.
795 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
796 then zero is returned.
797 If the number represented by String overflows according to the range defined by
798 UINTN, then ASSERT().
800 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
801 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
804 @param String A pointer to a Null-terminated Unicode string.
806 @retval Value translated from String.
812 IN CONST CHAR16
*String
818 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
819 // Length tests are performed inside StrLen().
821 ASSERT (StrSize (String
) != 0);
824 // Ignore the pad spaces (space or tab)
826 while ((*String
== L
' ') || (*String
== L
'\t')) {
831 // Ignore leading Zeros after the spaces
833 while (*String
== L
'0') {
837 if (InternalCharToUpper (*String
) == L
'X') {
838 if (*(String
- 1) != L
'0') {
849 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
851 // If the Hex Number represented by String overflows according
852 // to the range defined by UINTN, then ASSERT().
854 ASSERT (Result
<= ((((UINTN
) ~0) - InternalHexCharToUintn (*String
)) >> 4));
856 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
865 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
867 This function returns a value of type UINT64 by interpreting the contents
868 of the Unicode string specified by String as a hexadecimal number.
869 The format of the input Unicode string String is
871 [spaces][zeros][x][hexadecimal digits].
873 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
874 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
875 If "x" appears in the input string, it must be prefixed with at least one 0.
876 The function will ignore the pad space, which includes spaces or tab characters,
877 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
878 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
879 first valid hexadecimal digit. Then, the function stops at the first character that is
880 a not a valid hexadecimal character or NULL, whichever one comes first.
882 If String is NULL, then ASSERT().
883 If String is not aligned in a 16-bit boundary, then ASSERT().
884 If String has only pad spaces, then zero is returned.
885 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
886 then zero is returned.
887 If the number represented by String overflows according to the range defined by
888 UINT64, then ASSERT().
890 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
891 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
894 @param String A pointer to a Null-terminated Unicode string.
896 @retval Value translated from String.
902 IN CONST CHAR16
*String
908 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
909 // Length tests are performed inside StrLen().
911 ASSERT (StrSize (String
) != 0);
914 // Ignore the pad spaces (space or tab)
916 while ((*String
== L
' ') || (*String
== L
'\t')) {
921 // Ignore leading Zeros after the spaces
923 while (*String
== L
'0') {
927 if (InternalCharToUpper (*String
) == L
'X') {
928 ASSERT (*(String
- 1) == L
'0');
929 if (*(String
- 1) != L
'0') {
940 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
942 // If the Hex Number represented by String overflows according
943 // to the range defined by UINTN, then ASSERT().
945 ASSERT (Result
<= RShiftU64 (((UINT64
) ~0) - InternalHexCharToUintn (*String
) , 4));
947 Result
= LShiftU64 (Result
, 4);
948 Result
= Result
+ InternalHexCharToUintn (*String
);
956 Check if a ASCII character is a decimal character.
958 This internal function checks if a Unicode character is a
959 decimal character. The valid decimal character is from
962 @param Char The character to check against.
964 @retval TRUE If the Char is a decmial character.
965 @retval FALSE If the Char is not a decmial character.
970 InternalAsciiIsDecimalDigitCharacter (
974 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
978 Check if a ASCII character is a hexadecimal character.
980 This internal function checks if a ASCII character is a
981 decimal character. The valid hexadecimal character is
982 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
985 @param Char The character to check against.
987 @retval TRUE If the Char is a hexadecmial character.
988 @retval FALSE If the Char is not a hexadecmial character.
993 InternalAsciiIsHexaDecimalDigitCharacter (
998 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
999 (Char
>= 'A' && Char
<= 'F') ||
1000 (Char
>= 'a' && Char
<= 'f'));
1003 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1006 [ATTENTION] This function is deprecated for security reason.
1008 Convert a Null-terminated Unicode string to a Null-terminated
1009 ASCII string and returns the ASCII string.
1011 This function converts the content of the Unicode string Source
1012 to the ASCII string Destination by copying the lower 8 bits of
1013 each Unicode character. It returns Destination.
1015 The caller is responsible to make sure Destination points to a buffer with size
1016 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
1018 If any Unicode characters in Source contain non-zero value in
1019 the upper 8 bits, then ASSERT().
1021 If Destination is NULL, then ASSERT().
1022 If Source is NULL, then ASSERT().
1023 If Source is not aligned on a 16-bit boundary, then ASSERT().
1024 If Source and Destination overlap, then ASSERT().
1026 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1027 more than PcdMaximumUnicodeStringLength Unicode characters, not including
1028 the Null-terminator, then ASSERT().
1030 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1031 than PcdMaximumAsciiStringLength Unicode characters, not including the
1032 Null-terminator, then ASSERT().
1034 @param Source A pointer to a Null-terminated Unicode string.
1035 @param Destination A pointer to a Null-terminated ASCII string.
1037 @return Destination.
1042 UnicodeStrToAsciiStr (
1043 IN CONST CHAR16
*Source
,
1044 OUT CHAR8
*Destination
1049 ASSERT (Destination
!= NULL
);
1052 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1053 // Length tests are performed inside StrLen().
1055 ASSERT (StrSize (Source
) != 0);
1058 // Source and Destination should not overlap
1060 ASSERT ((UINTN
) (Destination
- (CHAR8
*) Source
) >= StrSize (Source
));
1061 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1064 ReturnValue
= Destination
;
1065 while (*Source
!= '\0') {
1067 // If any Unicode characters in Source contain
1068 // non-zero value in the upper 8 bits, then ASSERT().
1070 ASSERT (*Source
< 0x100);
1071 *(Destination
++) = (CHAR8
) *(Source
++);
1074 *Destination
= '\0';
1077 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1078 // Length tests are performed inside AsciiStrLen().
1080 ASSERT (AsciiStrSize (ReturnValue
) != 0);
1086 [ATTENTION] This function will be deprecated for security reason.
1088 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1089 string and returns the new ASCII string.
1091 This function copies the contents of the ASCII string Source to the ASCII
1092 string Destination, and returns Destination. If Source and Destination
1093 overlap, then the results are undefined.
1095 If Destination is NULL, then ASSERT().
1096 If Source is NULL, then ASSERT().
1097 If Source and Destination overlap, then ASSERT().
1098 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1099 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1102 @param Destination A pointer to a Null-terminated ASCII string.
1103 @param Source A pointer to a Null-terminated ASCII string.
1111 OUT CHAR8
*Destination
,
1112 IN CONST CHAR8
*Source
1118 // Destination cannot be NULL
1120 ASSERT (Destination
!= NULL
);
1123 // Destination and source cannot overlap
1125 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1126 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1128 ReturnValue
= Destination
;
1129 while (*Source
!= 0) {
1130 *(Destination
++) = *(Source
++);
1137 [ATTENTION] This function will be deprecated for security reason.
1139 Copies up to a specified length one Null-terminated ASCII string to another
1140 Null-terminated ASCII string and returns the new ASCII string.
1142 This function copies the contents of the ASCII string Source to the ASCII
1143 string Destination, and returns Destination. At most, Length ASCII characters
1144 are copied from Source to Destination. If Length is 0, then Destination is
1145 returned unmodified. If Length is greater that the number of ASCII characters
1146 in Source, then Destination is padded with Null ASCII characters. If Source
1147 and Destination overlap, then the results are undefined.
1149 If Destination is NULL, then ASSERT().
1150 If Source is NULL, then ASSERT().
1151 If Source and Destination overlap, then ASSERT().
1152 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1153 PcdMaximumAsciiStringLength, then ASSERT().
1154 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1155 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1158 @param Destination A pointer to a Null-terminated ASCII string.
1159 @param Source A pointer to a Null-terminated ASCII string.
1160 @param Length The maximum number of ASCII characters to copy.
1168 OUT CHAR8
*Destination
,
1169 IN CONST CHAR8
*Source
,
1180 // Destination cannot be NULL
1182 ASSERT (Destination
!= NULL
);
1185 // Destination and source cannot overlap
1187 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1188 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1190 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1191 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1194 ReturnValue
= Destination
;
1196 while (*Source
!= 0 && Length
> 0) {
1197 *(Destination
++) = *(Source
++);
1201 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1207 Returns the length of a Null-terminated ASCII string.
1209 This function returns the number of ASCII characters in the Null-terminated
1210 ASCII string specified by String.
1212 If Length > 0 and Destination is NULL, then ASSERT().
1213 If Length > 0 and Source is NULL, then ASSERT().
1214 If PcdMaximumAsciiStringLength is not zero and String contains more than
1215 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1218 @param String A pointer to a Null-terminated ASCII string.
1220 @return The length of String.
1226 IN CONST CHAR8
*String
1231 ASSERT (String
!= NULL
);
1233 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1235 // If PcdMaximumUnicodeStringLength is not zero,
1236 // length should not more than PcdMaximumUnicodeStringLength
1238 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1239 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1246 Returns the size of a Null-terminated ASCII string in bytes, including the
1249 This function returns the size, in bytes, of the Null-terminated ASCII string
1250 specified by String.
1252 If String is NULL, then ASSERT().
1253 If PcdMaximumAsciiStringLength is not zero and String contains more than
1254 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1257 @param String A pointer to a Null-terminated ASCII string.
1259 @return The size of String.
1265 IN CONST CHAR8
*String
1268 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1272 Compares two Null-terminated ASCII strings, and returns the difference
1273 between the first mismatched ASCII characters.
1275 This function compares the Null-terminated ASCII string FirstString to the
1276 Null-terminated ASCII string SecondString. If FirstString is identical to
1277 SecondString, then 0 is returned. Otherwise, the value returned is the first
1278 mismatched ASCII character in SecondString subtracted from the first
1279 mismatched ASCII character in FirstString.
1281 If FirstString is NULL, then ASSERT().
1282 If SecondString is NULL, then ASSERT().
1283 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1284 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1286 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1287 than PcdMaximumAsciiStringLength ASCII characters, not including the
1288 Null-terminator, then ASSERT().
1290 @param FirstString A pointer to a Null-terminated ASCII string.
1291 @param SecondString A pointer to a Null-terminated ASCII string.
1293 @retval ==0 FirstString is identical to SecondString.
1294 @retval !=0 FirstString is not identical to SecondString.
1300 IN CONST CHAR8
*FirstString
,
1301 IN CONST CHAR8
*SecondString
1305 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1307 ASSERT (AsciiStrSize (FirstString
));
1308 ASSERT (AsciiStrSize (SecondString
));
1310 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1315 return *FirstString
- *SecondString
;
1319 Converts a lowercase Ascii character to upper one.
1321 If Chr is lowercase Ascii character, then converts it to upper one.
1323 If Value >= 0xA0, then ASSERT().
1324 If (Value & 0x0F) >= 0x0A, then ASSERT().
1326 @param Chr one Ascii character
1328 @return The uppercase value of Ascii character
1333 InternalBaseLibAsciiToUpper (
1337 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1341 Convert a ASCII character to numerical value.
1343 This internal function only deal with Unicode character
1344 which maps to a valid hexadecimal ASII character, i.e.
1345 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1346 ASCII character, the value returned does not make sense.
1348 @param Char The character to convert.
1350 @return The numerical value converted.
1355 InternalAsciiHexCharToUintn (
1359 if (InternalIsDecimalDigitCharacter (Char
)) {
1363 return (UINTN
) (10 + InternalBaseLibAsciiToUpper (Char
) - 'A');
1368 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1369 and returns the difference between the first mismatched ASCII characters.
1371 This function performs a case insensitive comparison of the Null-terminated
1372 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1373 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1374 value returned is the first mismatched lower case ASCII character in
1375 SecondString subtracted from the first mismatched lower case ASCII character
1378 If FirstString is NULL, then ASSERT().
1379 If SecondString is NULL, then ASSERT().
1380 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1381 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1383 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1384 than PcdMaximumAsciiStringLength ASCII characters, not including the
1385 Null-terminator, then ASSERT().
1387 @param FirstString A pointer to a Null-terminated ASCII string.
1388 @param SecondString A pointer to a Null-terminated ASCII string.
1390 @retval ==0 FirstString is identical to SecondString using case insensitive
1392 @retval !=0 FirstString is not identical to SecondString using case
1393 insensitive comparisons.
1399 IN CONST CHAR8
*FirstString
,
1400 IN CONST CHAR8
*SecondString
1403 CHAR8 UpperFirstString
;
1404 CHAR8 UpperSecondString
;
1407 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1409 ASSERT (AsciiStrSize (FirstString
));
1410 ASSERT (AsciiStrSize (SecondString
));
1412 UpperFirstString
= InternalBaseLibAsciiToUpper (*FirstString
);
1413 UpperSecondString
= InternalBaseLibAsciiToUpper (*SecondString
);
1414 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1417 UpperFirstString
= InternalBaseLibAsciiToUpper (*FirstString
);
1418 UpperSecondString
= InternalBaseLibAsciiToUpper (*SecondString
);
1421 return UpperFirstString
- UpperSecondString
;
1425 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1426 the difference between the first mismatched ASCII characters.
1428 This function compares the Null-terminated ASCII string FirstString to the
1429 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1430 will be compared. If Length is 0, then 0 is returned. If FirstString is
1431 identical to SecondString, then 0 is returned. Otherwise, the value returned
1432 is the first mismatched ASCII character in SecondString subtracted from the
1433 first mismatched ASCII character in FirstString.
1435 If Length > 0 and FirstString is NULL, then ASSERT().
1436 If Length > 0 and SecondString is NULL, then ASSERT().
1437 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1438 PcdMaximumAsciiStringLength, then ASSERT().
1439 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
1440 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1442 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
1443 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1446 @param FirstString A pointer to a Null-terminated ASCII string.
1447 @param SecondString A pointer to a Null-terminated ASCII string.
1448 @param Length The maximum number of ASCII characters for compare.
1450 @retval ==0 FirstString is identical to SecondString.
1451 @retval !=0 FirstString is not identical to SecondString.
1457 IN CONST CHAR8
*FirstString
,
1458 IN CONST CHAR8
*SecondString
,
1467 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1469 ASSERT (AsciiStrSize (FirstString
));
1470 ASSERT (AsciiStrSize (SecondString
));
1472 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1473 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1476 while ((*FirstString
!= '\0') &&
1477 (*FirstString
== *SecondString
) &&
1483 return *FirstString
- *SecondString
;
1486 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1489 [ATTENTION] This function will be deprecated for security reason.
1491 Concatenates one Null-terminated ASCII string to another Null-terminated
1492 ASCII string, and returns the concatenated ASCII string.
1494 This function concatenates two Null-terminated ASCII strings. The contents of
1495 Null-terminated ASCII string Source are concatenated to the end of Null-
1496 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1499 If Destination is NULL, then ASSERT().
1500 If Source is NULL, then ASSERT().
1501 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1502 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1504 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1505 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1507 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1508 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1509 ASCII characters, then ASSERT().
1511 @param Destination A pointer to a Null-terminated ASCII string.
1512 @param Source A pointer to a Null-terminated ASCII string.
1520 IN OUT CHAR8
*Destination
,
1521 IN CONST CHAR8
*Source
1524 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1527 // Size of the resulting string should never be zero.
1528 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1530 ASSERT (AsciiStrSize (Destination
) != 0);
1535 [ATTENTION] This function will be deprecated for security reason.
1537 Concatenates up to a specified length one Null-terminated ASCII string to
1538 the end of another Null-terminated ASCII string, and returns the
1539 concatenated ASCII string.
1541 This function concatenates two Null-terminated ASCII strings. The contents
1542 of Null-terminated ASCII string Source are concatenated to the end of Null-
1543 terminated ASCII string Destination, and Destination is returned. At most,
1544 Length ASCII characters are concatenated from Source to the end of
1545 Destination, and Destination is always Null-terminated. If Length is 0, then
1546 Destination is returned unmodified. If Source and Destination overlap, then
1547 the results are undefined.
1549 If Length > 0 and Destination is NULL, then ASSERT().
1550 If Length > 0 and Source is NULL, then ASSERT().
1551 If Source and Destination overlap, then ASSERT().
1552 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1553 PcdMaximumAsciiStringLength, then ASSERT().
1554 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1555 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1557 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1558 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1560 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1561 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1562 ASCII characters, not including the Null-terminator, then ASSERT().
1564 @param Destination A pointer to a Null-terminated ASCII string.
1565 @param Source A pointer to a Null-terminated ASCII string.
1566 @param Length The maximum number of ASCII characters to concatenate from
1575 IN OUT CHAR8
*Destination
,
1576 IN CONST CHAR8
*Source
,
1580 UINTN DestinationLen
;
1582 DestinationLen
= AsciiStrLen (Destination
);
1583 AsciiStrnCpy (Destination
+ DestinationLen
, Source
, Length
);
1584 Destination
[DestinationLen
+ Length
] = '\0';
1587 // Size of the resulting string should never be zero.
1588 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1590 ASSERT (AsciiStrSize (Destination
) != 0);
1596 Returns the first occurrence of a Null-terminated ASCII sub-string
1597 in a Null-terminated ASCII string.
1599 This function scans the contents of the ASCII string specified by String
1600 and returns the first occurrence of SearchString. If SearchString is not
1601 found in String, then NULL is returned. If the length of SearchString is zero,
1602 then String is returned.
1604 If String is NULL, then ASSERT().
1605 If SearchString is NULL, then ASSERT().
1607 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1608 String contains more than PcdMaximumAsciiStringLength Unicode characters
1609 not including the Null-terminator, then ASSERT().
1611 @param String A pointer to a Null-terminated ASCII string.
1612 @param SearchString A pointer to a Null-terminated ASCII string to search for.
1614 @retval NULL If the SearchString does not appear in String.
1615 @retval others If there is a match return the first occurrence of SearchingString.
1616 If the length of SearchString is zero,return String.
1622 IN CONST CHAR8
*String
,
1623 IN CONST CHAR8
*SearchString
1626 CONST CHAR8
*FirstMatch
;
1627 CONST CHAR8
*SearchStringTmp
;
1630 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1632 ASSERT (AsciiStrSize (String
) != 0);
1633 ASSERT (AsciiStrSize (SearchString
) != 0);
1635 if (*SearchString
== '\0') {
1636 return (CHAR8
*) String
;
1639 while (*String
!= '\0') {
1640 SearchStringTmp
= SearchString
;
1641 FirstMatch
= String
;
1643 while ((*String
== *SearchStringTmp
)
1644 && (*String
!= '\0')) {
1649 if (*SearchStringTmp
== '\0') {
1650 return (CHAR8
*) FirstMatch
;
1653 if (*String
== '\0') {
1657 String
= FirstMatch
+ 1;
1664 Convert a Null-terminated ASCII decimal string to a value of type
1667 This function returns a value of type UINTN by interpreting the contents
1668 of the ASCII string String as a decimal number. The format of the input
1669 ASCII string String is:
1671 [spaces] [decimal digits].
1673 The valid decimal digit character is in the range [0-9]. The function will
1674 ignore the pad space, which includes spaces or tab characters, before the digits.
1675 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1676 function stops at the first character that is a not a valid decimal character or
1677 Null-terminator, whichever on comes first.
1679 If String has only pad spaces, then 0 is returned.
1680 If String has no pad spaces or valid decimal digits, then 0 is returned.
1681 If the number represented by String overflows according to the range defined by
1682 UINTN, then ASSERT().
1683 If String is NULL, then ASSERT().
1684 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1685 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1688 @param String A pointer to a Null-terminated ASCII string.
1690 @retval Value translated from String.
1695 AsciiStrDecimalToUintn (
1696 IN CONST CHAR8
*String
1702 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1704 ASSERT (AsciiStrSize (String
) != 0);
1707 // Ignore the pad spaces (space or tab)
1709 while ((*String
== ' ') || (*String
== '\t' )) {
1714 // Ignore leading Zeros after the spaces
1716 while (*String
== '0') {
1722 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1724 // If the number represented by String overflows according
1725 // to the range defined by UINTN, then ASSERT().
1727 ASSERT (Result
<= ((((UINTN
) ~0) - (*String
- L
'0')) / 10));
1729 Result
= Result
* 10 + (*String
- '0');
1738 Convert a Null-terminated ASCII decimal string to a value of type
1741 This function returns a value of type UINT64 by interpreting the contents
1742 of the ASCII string String as a decimal number. The format of the input
1743 ASCII string String is:
1745 [spaces] [decimal digits].
1747 The valid decimal digit character is in the range [0-9]. The function will
1748 ignore the pad space, which includes spaces or tab characters, before the digits.
1749 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1750 function stops at the first character that is a not a valid decimal character or
1751 Null-terminator, whichever on comes first.
1753 If String has only pad spaces, then 0 is returned.
1754 If String has no pad spaces or valid decimal digits, then 0 is returned.
1755 If the number represented by String overflows according to the range defined by
1756 UINT64, then ASSERT().
1757 If String is NULL, then ASSERT().
1758 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1759 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1762 @param String A pointer to a Null-terminated ASCII string.
1764 @retval Value translated from String.
1769 AsciiStrDecimalToUint64 (
1770 IN CONST CHAR8
*String
1776 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1778 ASSERT (AsciiStrSize (String
) != 0);
1781 // Ignore the pad spaces (space or tab)
1783 while ((*String
== ' ') || (*String
== '\t' )) {
1788 // Ignore leading Zeros after the spaces
1790 while (*String
== '0') {
1796 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1798 // If the number represented by String overflows according
1799 // to the range defined by UINTN, then ASSERT().
1801 ASSERT (Result
<= DivU64x32 (((UINT64
) ~0) - (*String
- L
'0') , 10));
1803 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1811 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1813 This function returns a value of type UINTN by interpreting the contents of
1814 the ASCII string String as a hexadecimal number. The format of the input ASCII
1817 [spaces][zeros][x][hexadecimal digits].
1819 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1820 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1821 appears in the input string, it must be prefixed with at least one 0. The function
1822 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1823 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1824 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1825 digit. Then, the function stops at the first character that is a not a valid
1826 hexadecimal character or Null-terminator, whichever on comes first.
1828 If String has only pad spaces, then 0 is returned.
1829 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1832 If the number represented by String overflows according to the range defined by UINTN,
1834 If String is NULL, then ASSERT().
1835 If PcdMaximumAsciiStringLength is not zero,
1836 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1837 the Null-terminator, then ASSERT().
1839 @param String A pointer to a Null-terminated ASCII string.
1841 @retval Value translated from String.
1846 AsciiStrHexToUintn (
1847 IN CONST CHAR8
*String
1853 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1855 ASSERT (AsciiStrSize (String
) != 0);
1858 // Ignore the pad spaces (space or tab)
1860 while ((*String
== ' ') || (*String
== '\t' )) {
1865 // Ignore leading Zeros after the spaces
1867 while (*String
== '0') {
1871 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1872 ASSERT (*(String
- 1) == '0');
1873 if (*(String
- 1) != '0') {
1884 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1886 // If the Hex Number represented by String overflows according
1887 // to the range defined by UINTN, then ASSERT().
1889 ASSERT (Result
<= ((((UINTN
) ~0) - InternalHexCharToUintn (*String
)) >> 4));
1891 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1900 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1902 This function returns a value of type UINT64 by interpreting the contents of
1903 the ASCII string String as a hexadecimal number. The format of the input ASCII
1906 [spaces][zeros][x][hexadecimal digits].
1908 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1909 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1910 appears in the input string, it must be prefixed with at least one 0. The function
1911 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1912 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1913 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1914 digit. Then, the function stops at the first character that is a not a valid
1915 hexadecimal character or Null-terminator, whichever on comes first.
1917 If String has only pad spaces, then 0 is returned.
1918 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1921 If the number represented by String overflows according to the range defined by UINT64,
1923 If String is NULL, then ASSERT().
1924 If PcdMaximumAsciiStringLength is not zero,
1925 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1926 the Null-terminator, then ASSERT().
1928 @param String A pointer to a Null-terminated ASCII string.
1930 @retval Value translated from String.
1935 AsciiStrHexToUint64 (
1936 IN CONST CHAR8
*String
1942 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1944 ASSERT (AsciiStrSize (String
) != 0);
1947 // Ignore the pad spaces (space or tab) and leading Zeros
1950 // Ignore the pad spaces (space or tab)
1952 while ((*String
== ' ') || (*String
== '\t' )) {
1957 // Ignore leading Zeros after the spaces
1959 while (*String
== '0') {
1963 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1964 ASSERT (*(String
- 1) == '0');
1965 if (*(String
- 1) != '0') {
1976 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1978 // If the Hex Number represented by String overflows according
1979 // to the range defined by UINTN, then ASSERT().
1981 ASSERT (Result
<= RShiftU64 (((UINT64
) ~0) - InternalHexCharToUintn (*String
) , 4));
1983 Result
= LShiftU64 (Result
, 4);
1984 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1991 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1994 [ATTENTION] This function is deprecated for security reason.
1996 Convert one Null-terminated ASCII string to a Null-terminated
1997 Unicode string and returns the Unicode string.
1999 This function converts the contents of the ASCII string Source to the Unicode
2000 string Destination, and returns Destination. The function terminates the
2001 Unicode string Destination by appending a Null-terminator character at the end.
2002 The caller is responsible to make sure Destination points to a buffer with size
2003 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
2005 If Destination is NULL, then ASSERT().
2006 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2007 If Source is NULL, then ASSERT().
2008 If Source and Destination overlap, then ASSERT().
2009 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
2010 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2012 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
2013 PcdMaximumUnicodeStringLength ASCII characters not including the
2014 Null-terminator, then ASSERT().
2016 @param Source A pointer to a Null-terminated ASCII string.
2017 @param Destination A pointer to a Null-terminated Unicode string.
2019 @return Destination.
2024 AsciiStrToUnicodeStr (
2025 IN CONST CHAR8
*Source
,
2026 OUT CHAR16
*Destination
2029 CHAR16
*ReturnValue
;
2031 ASSERT (Destination
!= NULL
);
2034 // ASSERT Source is less long than PcdMaximumAsciiStringLength
2036 ASSERT (AsciiStrSize (Source
) != 0);
2039 // Source and Destination should not overlap
2041 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
2042 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) >= (AsciiStrSize (Source
) * sizeof (CHAR16
)));
2045 ReturnValue
= Destination
;
2046 while (*Source
!= '\0') {
2047 *(Destination
++) = (CHAR16
) *(Source
++);
2050 // End the Destination with a NULL.
2052 *Destination
= '\0';
2055 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
2057 ASSERT (StrSize (ReturnValue
) != 0);
2065 Converts an 8-bit value to an 8-bit BCD value.
2067 Converts the 8-bit value specified by Value to BCD. The BCD value is
2070 If Value >= 100, then ASSERT().
2072 @param Value The 8-bit value to convert to BCD. Range 0..99.
2074 @return The BCD value.
2083 ASSERT (Value
< 100);
2084 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2088 Converts an 8-bit BCD value to an 8-bit value.
2090 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2093 If Value >= 0xA0, then ASSERT().
2094 If (Value & 0x0F) >= 0x0A, then ASSERT().
2096 @param Value The 8-bit BCD value to convert to an 8-bit value.
2098 @return The 8-bit value is returned.
2107 ASSERT (Value
< 0xa0);
2108 ASSERT ((Value
& 0xf) < 0xa);
2109 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));