2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2010, 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 #define QUOTIENT_MAX_UINTN_DIVIDED_BY_10 ((~((UINTN) 0)) / 10U)
18 #define REMAINDER_MAX_UINTN_DIVIDED_BY_10 ((~((UINTN) 0)) % 10U)
21 #define QUOTIENT_MAX_UINTN_DIVIDED_BY_16 ((~((UINTN) 0)) / 16U)
22 #define REMAINDER_MAX_UINTN_DIVIDED_BY_16 ((~((UINTN) 0)) % 16U)
24 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_10 ((~((UINT64) 0)) / 10U)
25 #define REMAINDER_MAX_UINT64_DIVIDED_BY_10 ((~((UINT64) 0)) % 10U)
27 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_16 ((~((UINT64) 0)) / 16U)
28 #define REMAINDER_MAX_UINT64_DIVIDED_BY_16 ((~((UINT64) 0)) % 16U)
31 Copies one Null-terminated Unicode string to another Null-terminated Unicode
32 string and returns the new Unicode string.
34 This function copies the contents of the Unicode string Source to the Unicode
35 string Destination, and returns Destination. If Source and Destination
36 overlap, then the results are undefined.
38 If Destination is NULL, then ASSERT().
39 If Destination is not aligned on a 16-bit boundary, then ASSERT().
40 If Source is NULL, then ASSERT().
41 If Source is not aligned on a 16-bit boundary, then ASSERT().
42 If Source and Destination overlap, then ASSERT().
43 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
44 PcdMaximumUnicodeStringLength Unicode characters, not including the
45 Null-terminator, then ASSERT().
47 @param Destination A pointer to a Null-terminated Unicode string.
48 @param Source A pointer to a Null-terminated Unicode string.
56 OUT CHAR16
*Destination
,
57 IN CONST CHAR16
*Source
63 // Destination cannot be NULL
65 ASSERT (Destination
!= NULL
);
66 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
69 // Destination and source cannot overlap
71 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
72 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
74 ReturnValue
= Destination
;
75 while (*Source
!= 0) {
76 *(Destination
++) = *(Source
++);
83 Copies up to a specified length from one Null-terminated Unicode string to
84 another Null-terminated Unicode string and returns the new Unicode string.
86 This function copies the contents of the Unicode string Source to the Unicode
87 string Destination, and returns Destination. At most, Length Unicode
88 characters are copied from Source to Destination. If Length is 0, then
89 Destination is returned unmodified. If Length is greater that the number of
90 Unicode characters in Source, then Destination is padded with Null Unicode
91 characters. If Source and Destination overlap, then the results are
94 If Length > 0 and Destination is NULL, then ASSERT().
95 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
96 If Length > 0 and Source is NULL, then ASSERT().
97 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
98 If Source and Destination overlap, then ASSERT().
99 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
100 PcdMaximumUnicodeStringLength, then ASSERT().
101 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
102 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
105 @param Destination A pointer to a Null-terminated Unicode string.
106 @param Source A pointer to a Null-terminated Unicode string.
107 @param Length The maximum number of Unicode characters to copy.
115 OUT CHAR16
*Destination
,
116 IN CONST CHAR16
*Source
,
127 // Destination cannot be NULL if Length is not zero
129 ASSERT (Destination
!= NULL
);
130 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
133 // Destination and source cannot overlap
135 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
136 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
138 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
139 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
142 ReturnValue
= Destination
;
144 while ((*Source
!= L
'\0') && (Length
> 0)) {
145 *(Destination
++) = *(Source
++);
149 ZeroMem (Destination
, Length
* sizeof (*Destination
));
154 Returns the length of a Null-terminated Unicode string.
156 This function returns the number of Unicode characters in the Null-terminated
157 Unicode string specified by String.
159 If String is NULL, then ASSERT().
160 If String is not aligned on a 16-bit boundary, then ASSERT().
161 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
162 PcdMaximumUnicodeStringLength Unicode characters, not including the
163 Null-terminator, then ASSERT().
165 @param String A pointer to a Null-terminated Unicode string.
167 @return The length of String.
173 IN CONST CHAR16
*String
178 ASSERT (String
!= NULL
);
179 ASSERT (((UINTN
) String
& BIT0
) == 0);
181 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
183 // If PcdMaximumUnicodeStringLength is not zero,
184 // length should not more than PcdMaximumUnicodeStringLength
186 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
187 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
194 Returns the size of a Null-terminated Unicode string in bytes, including the
197 This function returns the size, in bytes, of the Null-terminated Unicode string
200 If String is NULL, then ASSERT().
201 If String is not aligned on a 16-bit boundary, then ASSERT().
202 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
203 PcdMaximumUnicodeStringLength Unicode characters, not including the
204 Null-terminator, then ASSERT().
206 @param String A pointer to a Null-terminated Unicode string.
208 @return The size of String.
214 IN CONST CHAR16
*String
217 return (StrLen (String
) + 1) * sizeof (*String
);
221 Compares two Null-terminated Unicode strings, and returns the difference
222 between the first mismatched Unicode characters.
224 This function compares the Null-terminated Unicode string FirstString to the
225 Null-terminated Unicode string SecondString. If FirstString is identical to
226 SecondString, then 0 is returned. Otherwise, the value returned is the first
227 mismatched Unicode character in SecondString subtracted from the first
228 mismatched Unicode character in FirstString.
230 If FirstString is NULL, then ASSERT().
231 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
232 If SecondString is NULL, then ASSERT().
233 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
234 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
235 than PcdMaximumUnicodeStringLength Unicode characters, not including the
236 Null-terminator, then ASSERT().
237 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
238 than PcdMaximumUnicodeStringLength Unicode characters, not including the
239 Null-terminator, then ASSERT().
241 @param FirstString A pointer to a Null-terminated Unicode string.
242 @param SecondString A pointer to a Null-terminated Unicode string.
244 @retval 0 FirstString is identical to SecondString.
245 @return others FirstString is not identical to SecondString.
251 IN CONST CHAR16
*FirstString
,
252 IN CONST CHAR16
*SecondString
256 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
258 ASSERT (StrSize (FirstString
) != 0);
259 ASSERT (StrSize (SecondString
) != 0);
261 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
265 return *FirstString
- *SecondString
;
269 Compares up to a specified length the contents of two Null-terminated Unicode strings,
270 and returns the difference between the first mismatched Unicode characters.
272 This function compares the Null-terminated Unicode string FirstString to the
273 Null-terminated Unicode string SecondString. At most, Length Unicode
274 characters will be compared. If Length is 0, then 0 is returned. If
275 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
276 value returned is the first mismatched Unicode character in SecondString
277 subtracted from the first mismatched Unicode character in FirstString.
279 If Length > 0 and FirstString is NULL, then ASSERT().
280 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
281 If Length > 0 and SecondString is NULL, then ASSERT().
282 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
283 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
284 PcdMaximumUnicodeStringLength, then ASSERT().
285 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
286 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
288 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
289 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
292 @param FirstString A pointer to a Null-terminated Unicode string.
293 @param SecondString A pointer to a Null-terminated Unicode string.
294 @param Length The maximum number of Unicode characters to compare.
296 @retval 0 FirstString is identical to SecondString.
297 @return others FirstString is not identical to SecondString.
303 IN CONST CHAR16
*FirstString
,
304 IN CONST CHAR16
*SecondString
,
313 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
314 // Length tests are performed inside StrLen().
316 ASSERT (StrSize (FirstString
) != 0);
317 ASSERT (StrSize (SecondString
) != 0);
319 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
320 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
323 while ((*FirstString
!= L
'\0') &&
324 (*FirstString
== *SecondString
) &&
331 return *FirstString
- *SecondString
;
335 Concatenates one Null-terminated Unicode string to another Null-terminated
336 Unicode string, and returns the concatenated Unicode string.
338 This function concatenates two Null-terminated Unicode strings. The contents
339 of Null-terminated Unicode string Source are concatenated to the end of
340 Null-terminated Unicode string Destination. The Null-terminated concatenated
341 Unicode String is returned. If Source and Destination overlap, then the
342 results are undefined.
344 If Destination is NULL, then ASSERT().
345 If Destination is not aligned on a 16-bit boundary, then ASSERT().
346 If Source is NULL, then ASSERT().
347 If Source is not aligned on a 16-bit boundary, then ASSERT().
348 If Source and Destination overlap, then ASSERT().
349 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
350 than PcdMaximumUnicodeStringLength Unicode characters, not including the
351 Null-terminator, then ASSERT().
352 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
353 PcdMaximumUnicodeStringLength Unicode characters, not including the
354 Null-terminator, then ASSERT().
355 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
356 and Source results in a Unicode string with more than
357 PcdMaximumUnicodeStringLength Unicode characters, not including the
358 Null-terminator, then ASSERT().
360 @param Destination A pointer to a Null-terminated Unicode string.
361 @param Source A pointer to a Null-terminated Unicode string.
369 IN OUT CHAR16
*Destination
,
370 IN CONST CHAR16
*Source
373 StrCpy (Destination
+ StrLen (Destination
), Source
);
376 // Size of the resulting string should never be zero.
377 // PcdMaximumUnicodeStringLength is tested inside StrLen().
379 ASSERT (StrSize (Destination
) != 0);
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);
444 Returns the first occurrence of a Null-terminated Unicode sub-string
445 in a Null-terminated Unicode string.
447 This function scans the contents of the Null-terminated Unicode string
448 specified by String and returns the first occurrence of SearchString.
449 If SearchString is not found in String, then NULL is returned. If
450 the length of SearchString is zero, then String is
453 If String is NULL, then ASSERT().
454 If String is not aligned on a 16-bit boundary, then ASSERT().
455 If SearchString is NULL, then ASSERT().
456 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
458 If PcdMaximumUnicodeStringLength is not zero, and SearchString
459 or String contains more than PcdMaximumUnicodeStringLength Unicode
460 characters, not including the Null-terminator, then ASSERT().
462 @param String A pointer to a Null-terminated Unicode string.
463 @param SearchString A pointer to a Null-terminated Unicode string to search for.
465 @retval NULL If the SearchString does not appear in String.
466 @return others If there is a match.
472 IN CONST CHAR16
*String
,
473 IN CONST CHAR16
*SearchString
476 CONST CHAR16
*FirstMatch
;
477 CONST CHAR16
*SearchStringTmp
;
480 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
481 // Length tests are performed inside StrLen().
483 ASSERT (StrSize (String
) != 0);
484 ASSERT (StrSize (SearchString
) != 0);
486 if (*SearchString
== L
'\0') {
487 return (CHAR16
*) String
;
490 while (*String
!= L
'\0') {
491 SearchStringTmp
= SearchString
;
494 while ((*String
== *SearchStringTmp
)
495 && (*String
!= L
'\0')) {
500 if (*SearchStringTmp
== L
'\0') {
501 return (CHAR16
*) FirstMatch
;
504 if (*String
== L
'\0') {
508 String
= FirstMatch
+ 1;
515 Check if a Unicode character is a decimal character.
517 This internal function checks if a Unicode character is a
518 decimal character. The valid decimal character is from
521 @param Char The character to check against.
523 @retval TRUE If the Char is a decmial character.
524 @retval FALSE If the Char is not a decmial character.
529 InternalIsDecimalDigitCharacter (
533 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
537 Convert a Unicode character to upper case only if
538 it maps to a valid small-case ASCII character.
540 This internal function only deal with Unicode character
541 which maps to a valid small-case ASCII character, i.e.
542 L'a' to L'z'. For other Unicode character, the input character
543 is returned directly.
545 @param Char The character to convert.
547 @retval LowerCharacter If the Char is with range L'a' to L'z'.
548 @retval Unchanged Otherwise.
553 InternalCharToUpper (
557 if (Char
>= L
'a' && Char
<= L
'z') {
558 return (CHAR16
) (Char
- (L
'a' - L
'A'));
565 Convert a Unicode character to numerical value.
567 This internal function only deal with Unicode character
568 which maps to a valid hexadecimal ASII character, i.e.
569 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
570 Unicode character, the value returned does not make sense.
572 @param Char The character to convert.
574 @return The numerical value converted.
579 InternalHexCharToUintn (
583 if (InternalIsDecimalDigitCharacter (Char
)) {
587 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
591 Check if a Unicode character is a hexadecimal character.
593 This internal function checks if a Unicode character is a
594 decimal character. The valid hexadecimal character is
595 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
598 @param Char The character to check against.
600 @retval TRUE If the Char is a hexadecmial character.
601 @retval FALSE If the Char is not a hexadecmial character.
606 InternalIsHexaDecimalDigitCharacter (
611 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
612 (Char
>= L
'A' && Char
<= L
'F') ||
613 (Char
>= L
'a' && Char
<= L
'f'));
617 Convert a Null-terminated Unicode decimal string to a value of
620 This function returns a value of type UINTN by interpreting the contents
621 of the Unicode string specified by String as a decimal number. The format
622 of the input Unicode string String is:
624 [spaces] [decimal digits].
626 The valid decimal digit character is in the range [0-9]. The
627 function will ignore the pad space, which includes spaces or
628 tab characters, before [decimal digits]. The running zero in the
629 beginning of [decimal digits] will be ignored. Then, the function
630 stops at the first character that is a not a valid decimal character
631 or a Null-terminator, whichever one comes first.
633 If String is NULL, then ASSERT().
634 If String is not aligned in a 16-bit boundary, then ASSERT().
635 If String has only pad spaces, then 0 is returned.
636 If String has no pad spaces or valid decimal digits,
638 If the number represented by String overflows according
639 to the range defined by UINTN, then ASSERT().
641 If PcdMaximumUnicodeStringLength is not zero, and String contains
642 more than PcdMaximumUnicodeStringLength Unicode characters, not including
643 the Null-terminator, then ASSERT().
645 @param String A pointer to a Null-terminated Unicode string.
647 @retval Value translated from String.
653 IN CONST CHAR16
*String
659 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
660 // Length tests are performed inside StrLen().
662 ASSERT (StrSize (String
) != 0);
665 // Ignore the pad spaces (space or tab)
667 while ((*String
== L
' ') || (*String
== L
'\t')) {
672 // Ignore leading Zeros after the spaces
674 while (*String
== L
'0') {
680 while (InternalIsDecimalDigitCharacter (*String
)) {
682 // If the number represented by String overflows according
683 // to the range defined by UINTN, then ASSERT().
685 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
686 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
687 (*String
- L
'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
690 Result
= Result
* 10 + (*String
- L
'0');
699 Convert a Null-terminated Unicode decimal string to a value of
702 This function returns a value of type UINT64 by interpreting the contents
703 of the Unicode string specified by String as a decimal number. The format
704 of the input Unicode string String is:
706 [spaces] [decimal digits].
708 The valid decimal digit character is in the range [0-9]. The
709 function will ignore the pad space, which includes spaces or
710 tab characters, before [decimal digits]. The running zero in the
711 beginning of [decimal digits] will be ignored. Then, the function
712 stops at the first character that is a not a valid decimal character
713 or a Null-terminator, whichever one comes first.
715 If String is NULL, then ASSERT().
716 If String is not aligned in a 16-bit boundary, then ASSERT().
717 If String has only pad spaces, then 0 is returned.
718 If String has no pad spaces or valid decimal digits,
720 If the number represented by String overflows according
721 to the range defined by UINT64, then ASSERT().
723 If PcdMaximumUnicodeStringLength is not zero, and String contains
724 more than PcdMaximumUnicodeStringLength Unicode characters, not including
725 the Null-terminator, then ASSERT().
727 @param String A pointer to a Null-terminated Unicode string.
729 @retval Value translated from String.
735 IN CONST CHAR16
*String
741 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
742 // Length tests are performed inside StrLen().
744 ASSERT (StrSize (String
) != 0);
747 // Ignore the pad spaces (space or tab)
749 while ((*String
== L
' ') || (*String
== L
'\t')) {
754 // Ignore leading Zeros after the spaces
756 while (*String
== L
'0') {
762 while (InternalIsDecimalDigitCharacter (*String
)) {
764 // If the number represented by String overflows according
765 // to the range defined by UINTN, then ASSERT().
767 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
768 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
769 (*String
- L
'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
772 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
780 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
782 This function returns a value of type UINTN by interpreting the contents
783 of the Unicode string specified by String as a hexadecimal number.
784 The format of the input Unicode string String is:
786 [spaces][zeros][x][hexadecimal digits].
788 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
789 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
790 If "x" appears in the input string, it must be prefixed with at least one 0.
791 The function will ignore the pad space, which includes spaces or tab characters,
792 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
793 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
794 first valid hexadecimal digit. Then, the function stops at the first character that is
795 a not a valid hexadecimal character or NULL, whichever one comes first.
797 If String is NULL, then ASSERT().
798 If String is not aligned in a 16-bit boundary, then ASSERT().
799 If String has only pad spaces, then zero is returned.
800 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
801 then zero is returned.
802 If the number represented by String overflows according to the range defined by
803 UINTN, then ASSERT().
805 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
806 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
809 @param String A pointer to a Null-terminated Unicode string.
811 @retval Value translated from String.
817 IN CONST CHAR16
*String
823 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
824 // Length tests are performed inside StrLen().
826 ASSERT (StrSize (String
) != 0);
829 // Ignore the pad spaces (space or tab)
831 while ((*String
== L
' ') || (*String
== L
'\t')) {
836 // Ignore leading Zeros after the spaces
838 while (*String
== L
'0') {
842 if (InternalCharToUpper (*String
) == L
'X') {
843 if (*(String
- 1) != L
'0') {
854 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
856 // If the Hex Number represented by String overflows according
857 // to the range defined by UINTN, then ASSERT().
859 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
860 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
861 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
864 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
873 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
875 This function returns a value of type UINT64 by interpreting the contents
876 of the Unicode string specified by String as a hexadecimal number.
877 The format of the input Unicode string String is
879 [spaces][zeros][x][hexadecimal digits].
881 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
882 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
883 If "x" appears in the input string, it must be prefixed with at least one 0.
884 The function will ignore the pad space, which includes spaces or tab characters,
885 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
886 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
887 first valid hexadecimal digit. Then, the function stops at the first character that is
888 a not a valid hexadecimal character or NULL, whichever one comes first.
890 If String is NULL, then ASSERT().
891 If String is not aligned in a 16-bit boundary, then ASSERT().
892 If String has only pad spaces, then zero is returned.
893 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
894 then zero is returned.
895 If the number represented by String overflows according to the range defined by
896 UINT64, then ASSERT().
898 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
899 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
902 @param String A pointer to a Null-terminated Unicode string.
904 @retval Value translated from String.
910 IN CONST CHAR16
*String
916 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
917 // Length tests are performed inside StrLen().
919 ASSERT (StrSize (String
) != 0);
922 // Ignore the pad spaces (space or tab)
924 while ((*String
== L
' ') || (*String
== L
'\t')) {
929 // Ignore leading Zeros after the spaces
931 while (*String
== L
'0') {
935 if (InternalCharToUpper (*String
) == L
'X') {
936 ASSERT (*(String
- 1) == L
'0');
937 if (*(String
- 1) != L
'0') {
948 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
950 // If the Hex Number represented by String overflows according
951 // to the range defined by UINTN, then ASSERT().
953 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
)||
954 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
955 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
958 Result
= LShiftU64 (Result
, 4);
959 Result
= Result
+ InternalHexCharToUintn (*String
);
967 Check if a ASCII character is a decimal character.
969 This internal function checks if a Unicode character is a
970 decimal character. The valid decimal character is from
973 @param Char The character to check against.
975 @retval TRUE If the Char is a decmial character.
976 @retval FALSE If the Char is not a decmial character.
981 InternalAsciiIsDecimalDigitCharacter (
985 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
989 Check if a ASCII character is a hexadecimal character.
991 This internal function checks if a ASCII character is a
992 decimal character. The valid hexadecimal character is
993 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
996 @param Char The character to check against.
998 @retval TRUE If the Char is a hexadecmial character.
999 @retval FALSE If the Char is not a hexadecmial character.
1004 InternalAsciiIsHexaDecimalDigitCharacter (
1009 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
1010 (Char
>= 'A' && Char
<= 'F') ||
1011 (Char
>= 'a' && Char
<= 'f'));
1015 Convert a Null-terminated Unicode string to a Null-terminated
1016 ASCII string and returns the ASCII string.
1018 This function converts the content of the Unicode string Source
1019 to the ASCII string Destination by copying the lower 8 bits of
1020 each Unicode character. It returns Destination.
1022 If any Unicode characters in Source contain non-zero value in
1023 the upper 8 bits, then ASSERT().
1025 If Destination is NULL, then ASSERT().
1026 If Source is NULL, then ASSERT().
1027 If Source is not aligned on a 16-bit boundary, then ASSERT().
1028 If Source and Destination overlap, then ASSERT().
1030 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1031 more than PcdMaximumUnicodeStringLength Unicode characters, not including
1032 the Null-terminator, then ASSERT().
1034 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1035 than PcdMaximumAsciiStringLength Unicode characters, not including the
1036 Null-terminator, then ASSERT().
1038 @param Source A pointer to a Null-terminated Unicode string.
1039 @param Destination A pointer to a Null-terminated ASCII string.
1041 @return Destination.
1046 UnicodeStrToAsciiStr (
1047 IN CONST CHAR16
*Source
,
1048 OUT CHAR8
*Destination
1053 ASSERT (Destination
!= NULL
);
1056 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1057 // Length tests are performed inside StrLen().
1059 ASSERT (StrSize (Source
) != 0);
1062 // Source and Destination should not overlap
1064 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1065 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1068 ReturnValue
= Destination
;
1069 while (*Source
!= '\0') {
1071 // If any Unicode characters in Source contain
1072 // non-zero value in the upper 8 bits, then ASSERT().
1074 ASSERT (*Source
< 0x100);
1075 *(Destination
++) = (CHAR8
) *(Source
++);
1078 *Destination
= '\0';
1081 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1082 // Length tests are performed inside AsciiStrLen().
1084 ASSERT (AsciiStrSize (ReturnValue
) != 0);
1091 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1092 string and returns the new ASCII string.
1094 This function copies the contents of the ASCII string Source to the ASCII
1095 string Destination, and returns Destination. If Source and Destination
1096 overlap, then the results are undefined.
1098 If Destination is NULL, then ASSERT().
1099 If Source is NULL, then ASSERT().
1100 If Source and Destination overlap, then ASSERT().
1101 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1102 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1105 @param Destination A pointer to a Null-terminated ASCII string.
1106 @param Source A pointer to a Null-terminated ASCII string.
1114 OUT CHAR8
*Destination
,
1115 IN CONST CHAR8
*Source
1121 // Destination cannot be NULL
1123 ASSERT (Destination
!= NULL
);
1126 // Destination and source cannot overlap
1128 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1129 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1131 ReturnValue
= Destination
;
1132 while (*Source
!= 0) {
1133 *(Destination
++) = *(Source
++);
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
));
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
;
1487 Concatenates one Null-terminated ASCII string to another Null-terminated
1488 ASCII string, and returns the concatenated ASCII string.
1490 This function concatenates two Null-terminated ASCII strings. The contents of
1491 Null-terminated ASCII string Source are concatenated to the end of Null-
1492 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1495 If Destination is NULL, then ASSERT().
1496 If Source is NULL, then ASSERT().
1497 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1498 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1500 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1501 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1503 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1504 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1505 ASCII characters, then ASSERT().
1507 @param Destination A pointer to a Null-terminated ASCII string.
1508 @param Source A pointer to a Null-terminated ASCII string.
1516 IN OUT CHAR8
*Destination
,
1517 IN CONST CHAR8
*Source
1520 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1523 // Size of the resulting string should never be zero.
1524 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1526 ASSERT (AsciiStrSize (Destination
) != 0);
1531 Concatenates up to a specified length one Null-terminated ASCII string to
1532 the end of another Null-terminated ASCII string, and returns the
1533 concatenated ASCII string.
1535 This function concatenates two Null-terminated ASCII strings. The contents
1536 of Null-terminated ASCII string Source are concatenated to the end of Null-
1537 terminated ASCII string Destination, and Destination is returned. At most,
1538 Length ASCII characters are concatenated from Source to the end of
1539 Destination, and Destination is always Null-terminated. If Length is 0, then
1540 Destination is returned unmodified. If Source and Destination overlap, then
1541 the results are undefined.
1543 If Length > 0 and Destination is NULL, then ASSERT().
1544 If Length > 0 and Source is NULL, then ASSERT().
1545 If Source and Destination overlap, then ASSERT().
1546 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1547 PcdMaximumAsciiStringLength, then ASSERT().
1548 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1549 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1551 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1552 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1554 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1555 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1556 ASCII characters, not including the Null-terminator, then ASSERT().
1558 @param Destination A pointer to a Null-terminated ASCII string.
1559 @param Source A pointer to a Null-terminated ASCII string.
1560 @param Length The maximum number of ASCII characters to concatenate from
1569 IN OUT CHAR8
*Destination
,
1570 IN CONST CHAR8
*Source
,
1574 UINTN DestinationLen
;
1576 DestinationLen
= AsciiStrLen (Destination
);
1577 AsciiStrnCpy (Destination
+ DestinationLen
, Source
, Length
);
1578 Destination
[DestinationLen
+ Length
] = '\0';
1581 // Size of the resulting string should never be zero.
1582 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1584 ASSERT (AsciiStrSize (Destination
) != 0);
1589 Returns the first occurrence of a Null-terminated ASCII sub-string
1590 in a Null-terminated ASCII string.
1592 This function scans the contents of the ASCII string specified by String
1593 and returns the first occurrence of SearchString. If SearchString is not
1594 found in String, then NULL is returned. If the length of SearchString is zero,
1595 then String is returned.
1597 If String is NULL, then ASSERT().
1598 If SearchString is NULL, then ASSERT().
1600 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1601 String contains more than PcdMaximumAsciiStringLength Unicode characters
1602 not including the Null-terminator, then ASSERT().
1604 @param String A pointer to a Null-terminated ASCII string.
1605 @param SearchString A pointer to a Null-terminated ASCII string to search for.
1607 @retval NULL If the SearchString does not appear in String.
1608 @retval others If there is a match return the first occurrence of SearchingString.
1609 If the length of SearchString is zero,return String.
1615 IN CONST CHAR8
*String
,
1616 IN CONST CHAR8
*SearchString
1619 CONST CHAR8
*FirstMatch
;
1620 CONST CHAR8
*SearchStringTmp
;
1623 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1625 ASSERT (AsciiStrSize (String
) != 0);
1626 ASSERT (AsciiStrSize (SearchString
) != 0);
1628 if (*SearchString
== '\0') {
1629 return (CHAR8
*) String
;
1632 while (*String
!= '\0') {
1633 SearchStringTmp
= SearchString
;
1634 FirstMatch
= String
;
1636 while ((*String
== *SearchStringTmp
)
1637 && (*String
!= '\0')) {
1642 if (*SearchStringTmp
== '\0') {
1643 return (CHAR8
*) FirstMatch
;
1646 if (*String
== '\0') {
1650 String
= FirstMatch
+ 1;
1657 Convert a Null-terminated ASCII decimal string to a value of type
1660 This function returns a value of type UINTN by interpreting the contents
1661 of the ASCII string String as a decimal number. The format of the input
1662 ASCII string String is:
1664 [spaces] [decimal digits].
1666 The valid decimal digit character is in the range [0-9]. The function will
1667 ignore the pad space, which includes spaces or tab characters, before the digits.
1668 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1669 function stops at the first character that is a not a valid decimal character or
1670 Null-terminator, whichever on comes first.
1672 If String has only pad spaces, then 0 is returned.
1673 If String has no pad spaces or valid decimal digits, then 0 is returned.
1674 If the number represented by String overflows according to the range defined by
1675 UINTN, then ASSERT().
1676 If String is NULL, then ASSERT().
1677 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1678 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1681 @param String A pointer to a Null-terminated ASCII string.
1683 @retval Value translated from String.
1688 AsciiStrDecimalToUintn (
1689 IN CONST CHAR8
*String
1695 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1697 ASSERT (AsciiStrSize (String
) != 0);
1700 // Ignore the pad spaces (space or tab)
1702 while ((*String
== ' ') || (*String
== '\t' )) {
1707 // Ignore leading Zeros after the spaces
1709 while (*String
== '0') {
1715 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1717 // If the number represented by String overflows according
1718 // to the range defined by UINTN, then ASSERT().
1720 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1721 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1722 (*String
- '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
1725 Result
= Result
* 10 + (*String
- '0');
1734 Convert a Null-terminated ASCII decimal string to a value of type
1737 This function returns a value of type UINT64 by interpreting the contents
1738 of the ASCII string String as a decimal number. The format of the input
1739 ASCII string String is:
1741 [spaces] [decimal digits].
1743 The valid decimal digit character is in the range [0-9]. The function will
1744 ignore the pad space, which includes spaces or tab characters, before the digits.
1745 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1746 function stops at the first character that is a not a valid decimal character or
1747 Null-terminator, whichever on comes first.
1749 If String has only pad spaces, then 0 is returned.
1750 If String has no pad spaces or valid decimal digits, then 0 is returned.
1751 If the number represented by String overflows according to the range defined by
1752 UINT64, then ASSERT().
1753 If String is NULL, then ASSERT().
1754 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1755 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1758 @param String A pointer to a Null-terminated ASCII string.
1760 @retval Value translated from String.
1765 AsciiStrDecimalToUint64 (
1766 IN CONST CHAR8
*String
1772 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1774 ASSERT (AsciiStrSize (String
) != 0);
1777 // Ignore the pad spaces (space or tab)
1779 while ((*String
== ' ') || (*String
== '\t' )) {
1784 // Ignore leading Zeros after the spaces
1786 while (*String
== '0') {
1792 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1794 // If the number represented by String overflows according
1795 // to the range defined by UINTN, then ASSERT().
1797 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1798 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1799 (*String
- '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
1802 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1810 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1812 This function returns a value of type UINTN by interpreting the contents of
1813 the ASCII string String as a hexadecimal number. The format of the input ASCII
1816 [spaces][zeros][x][hexadecimal digits].
1818 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1819 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1820 appears in the input string, it must be prefixed with at least one 0. The function
1821 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1822 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1823 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1824 digit. Then, the function stops at the first character that is a not a valid
1825 hexadecimal character or Null-terminator, whichever on comes first.
1827 If String has only pad spaces, then 0 is returned.
1828 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1831 If the number represented by String overflows according to the range defined by UINTN,
1833 If String is NULL, then ASSERT().
1834 If PcdMaximumAsciiStringLength is not zero,
1835 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1836 the Null-terminator, then ASSERT().
1838 @param String A pointer to a Null-terminated ASCII string.
1840 @retval Value translated from String.
1845 AsciiStrHexToUintn (
1846 IN CONST CHAR8
*String
1852 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1854 ASSERT (AsciiStrSize (String
) != 0);
1857 // Ignore the pad spaces (space or tab)
1859 while ((*String
== ' ') || (*String
== '\t' )) {
1864 // Ignore leading Zeros after the spaces
1866 while (*String
== '0') {
1870 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1871 ASSERT (*(String
- 1) == '0');
1872 if (*(String
- 1) != '0') {
1883 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1885 // If the Hex Number represented by String overflows according
1886 // to the range defined by UINTN, then ASSERT().
1888 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1889 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1890 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
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
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1984 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1985 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
1988 Result
= LShiftU64 (Result
, 4);
1989 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
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
) > (AsciiStrLen (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);
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));