2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. 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) -1 / 10)
18 #define REMAINDER_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 % 10)
20 #define QUOTIENT_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 / 16)
21 #define REMAINDER_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 % 16)
23 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 / 10)
24 #define REMAINDER_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 % 10)
26 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 / 16)
27 #define REMAINDER_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 % 16)
30 Copies one Null-terminated Unicode string to another Null-terminated Unicode
31 string and returns the new Unicode string.
33 This function copies the contents of the Unicode string Source to the Unicode
34 string Destination, and returns Destination. If Source and Destination
35 overlap, then the results are undefined.
37 If Destination is NULL, then ASSERT().
38 If Destination is not aligned on a 16-bit boundary, then ASSERT().
39 If Source is NULL, then ASSERT().
40 If Source is not aligned on a 16-bit boundary, then ASSERT().
41 If Source and Destination overlap, then ASSERT().
42 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
43 PcdMaximumUnicodeStringLength Unicode characters not including the
44 Null-terminator, then ASSERT().
46 @param Destination Pointer to a Null-terminated Unicode string.
47 @param Source Pointer to a Null-terminated Unicode string.
55 OUT CHAR16
*Destination
,
56 IN CONST CHAR16
*Source
62 // Destination cannot be NULL
64 ASSERT (Destination
!= NULL
);
65 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
68 // Destination and source cannot overlap
70 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
71 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
73 ReturnValue
= Destination
;
74 while (*Source
!= 0) {
75 *(Destination
++) = *(Source
++);
82 Copies up to a specified length from one Null-terminated Unicode string to
83 another Null-terminated Unicode string and returns the new Unicode string.
85 This function copies the contents of the Unicode string Source to the Unicode
86 string Destination, and returns Destination. At most, Length Unicode
87 characters are copied from Source to Destination. If Length is 0, then
88 Destination is returned unmodified. If Length is greater that the number of
89 Unicode characters in Source, then Destination is padded with Null Unicode
90 characters. If Source and Destination overlap, then the results are
93 If Length > 0 and Destination is NULL, then ASSERT().
94 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
95 If Length > 0 and Source is NULL, then ASSERT().
96 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
97 If Source and Destination overlap, then ASSERT().
98 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
99 PcdMaximumUnicodeStringLength Unicode characters not including the
100 Null-terminator, then ASSERT().
102 @param Destination Pointer to a Null-terminated Unicode string.
103 @param Source Pointer to a Null-terminated Unicode string.
104 @param Length Maximum number of Unicode characters to copy.
112 OUT CHAR16
*Destination
,
113 IN CONST CHAR16
*Source
,
124 // Destination cannot be NULL if Length is not zero
126 ASSERT (Destination
!= NULL
);
127 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
130 // Destination and source cannot overlap
132 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
133 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
135 ReturnValue
= Destination
;
137 while ((*Source
!= L
'\0') && (Length
> 0)) {
138 *(Destination
++) = *(Source
++);
142 ZeroMem (Destination
, Length
* sizeof (*Destination
));
147 Returns the length of a Null-terminated Unicode string.
149 This function returns the number of Unicode characters in the Null-terminated
150 Unicode string specified by String.
152 If String is NULL, then ASSERT().
153 If String is not aligned on a 16-bit boundary, then ASSERT().
154 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
155 PcdMaximumUnicodeStringLength Unicode characters not including the
156 Null-terminator, then ASSERT().
158 @param String Pointer to a Null-terminated Unicode string.
160 @return The length of String.
166 IN CONST CHAR16
*String
171 ASSERT (String
!= NULL
);
172 ASSERT (((UINTN
) String
& BIT0
) == 0);
174 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
176 // If PcdMaximumUnicodeStringLength is not zero,
177 // length should not more than PcdMaximumUnicodeStringLength
179 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
180 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
187 Returns the size of a Null-terminated Unicode string in bytes, including the
190 This function returns the size, in bytes, of the Null-terminated Unicode string
193 If String is NULL, then ASSERT().
194 If String is not aligned on a 16-bit boundary, then ASSERT().
195 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
196 PcdMaximumUnicodeStringLength Unicode characters not including the
197 Null-terminator, then ASSERT().
199 @param String Pointer to a Null-terminated Unicode string.
201 @return The size of String.
207 IN CONST CHAR16
*String
210 return (StrLen (String
) + 1) * sizeof (*String
);
214 Compares two Null-terminated Unicode strings, and returns the difference
215 between the first mismatched Unicode characters.
217 This function compares the Null-terminated Unicode string FirstString to the
218 Null-terminated Unicode string SecondString. If FirstString is identical to
219 SecondString, then 0 is returned. Otherwise, the value returned is the first
220 mismatched Unicode character in SecondString subtracted from the first
221 mismatched Unicode character in FirstString.
223 If FirstString is NULL, then ASSERT().
224 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
225 If SecondString is NULL, then ASSERT().
226 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
227 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
228 than PcdMaximumUnicodeStringLength Unicode characters not including the
229 Null-terminator, then ASSERT().
230 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
231 than PcdMaximumUnicodeStringLength Unicode characters not including the
232 Null-terminator, then ASSERT().
234 @param FirstString Pointer to a Null-terminated Unicode string.
235 @param SecondString Pointer to a Null-terminated Unicode string.
237 @retval 0 FirstString is identical to SecondString.
238 @return others FirstString is not identical to SecondString.
244 IN CONST CHAR16
*FirstString
,
245 IN CONST CHAR16
*SecondString
249 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
251 ASSERT (StrSize (FirstString
) != 0);
252 ASSERT (StrSize (SecondString
) != 0);
254 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
258 return *FirstString
- *SecondString
;
262 Compares up to a specified length the contents of two Null-terminated Unicode strings,
263 and returns the difference between the first mismatched Unicode characters.
265 This function compares the Null-terminated Unicode string FirstString to the
266 Null-terminated Unicode string SecondString. At most, Length Unicode
267 characters will be compared. If Length is 0, then 0 is returned. If
268 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
269 value returned is the first mismatched Unicode character in SecondString
270 subtracted from the first mismatched Unicode character in FirstString.
272 If Length > 0 and FirstString is NULL, then ASSERT().
273 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
274 If Length > 0 and SecondString is NULL, then ASSERT().
275 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
276 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
277 than PcdMaximumUnicodeStringLength Unicode characters not including the
278 Null-terminator, then ASSERT().
279 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
280 than PcdMaximumUnicodeStringLength Unicode characters not including the
281 Null-terminator, then ASSERT().
283 @param FirstString Pointer to a Null-terminated Unicode string.
284 @param SecondString Pointer to a Null-terminated Unicode string.
285 @param Length Maximum number of Unicode characters to compare.
287 @retval 0 FirstString is identical to SecondString.
288 @return others FirstString is not identical to SecondString.
294 IN CONST CHAR16
*FirstString
,
295 IN CONST CHAR16
*SecondString
,
304 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
305 // Length tests are performed inside StrLen().
307 ASSERT (StrSize (FirstString
) != 0);
308 ASSERT (StrSize (SecondString
) != 0);
310 while ((*FirstString
!= L
'\0') &&
311 (*FirstString
== *SecondString
) &&
318 return *FirstString
- *SecondString
;
322 Concatenates one Null-terminated Unicode string to another Null-terminated
323 Unicode string, and returns the concatenated Unicode string.
325 This function concatenates two Null-terminated Unicode strings. The contents
326 of Null-terminated Unicode string Source are concatenated to the end of
327 Null-terminated Unicode string Destination. The Null-terminated concatenated
328 Unicode String is returned. If Source and Destination overlap, then the
329 results are undefined.
331 If Destination is NULL, then ASSERT().
332 If Destination is not aligned on a 16-bit boundary, then ASSERT().
333 If Source is NULL, then ASSERT().
334 If Source is not aligned on a 16-bit boundary, then ASSERT().
335 If Source and Destination overlap, then ASSERT().
336 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
337 than PcdMaximumUnicodeStringLength Unicode characters not including the
338 Null-terminator, then ASSERT().
339 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
340 PcdMaximumUnicodeStringLength Unicode characters not including the
341 Null-terminator, then ASSERT().
342 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
343 and Source results in a Unicode string with more than
344 PcdMaximumUnicodeStringLength Unicode characters not including the
345 Null-terminator, then ASSERT().
347 @param Destination Pointer to a Null-terminated Unicode string.
348 @param Source Pointer to a Null-terminated Unicode string.
356 IN OUT CHAR16
*Destination
,
357 IN CONST CHAR16
*Source
360 StrCpy (Destination
+ StrLen (Destination
), Source
);
363 // Size of the resulting string should never be zero.
364 // PcdMaximumUnicodeStringLength is tested inside StrLen().
366 ASSERT (StrSize (Destination
) != 0);
371 Concatenates up to a specified length one Null-terminated Unicode to the end
372 of another Null-terminated Unicode string, and returns the concatenated
375 This function concatenates two Null-terminated Unicode strings. The contents
376 of Null-terminated Unicode string Source are concatenated to the end of
377 Null-terminated Unicode string Destination, and Destination is returned. At
378 most, Length Unicode characters are concatenated from Source to the end of
379 Destination, and Destination is always Null-terminated. If Length is 0, then
380 Destination is returned unmodified. If Source and Destination overlap, then
381 the results are undefined.
383 If Destination is NULL, then ASSERT().
384 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
385 If Length > 0 and Source is NULL, then ASSERT().
386 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
387 If Source and Destination overlap, then ASSERT().
388 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
389 than PcdMaximumUnicodeStringLength Unicode characters not including the
390 Null-terminator, then ASSERT().
391 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
392 PcdMaximumUnicodeStringLength Unicode characters not including the
393 Null-terminator, then ASSERT().
394 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
395 and Source results in a Unicode string with more than
396 PcdMaximumUnicodeStringLength Unicode characters not including the
397 Null-terminator, then ASSERT().
399 @param Destination Pointer to a Null-terminated Unicode string.
400 @param Source Pointer to a Null-terminated Unicode string.
401 @param Length Maximum number of Unicode characters to concatenate from
410 IN OUT CHAR16
*Destination
,
411 IN CONST CHAR16
*Source
,
415 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
418 // Size of the resulting string should never be zero.
419 // PcdMaximumUnicodeStringLength is tested inside StrLen().
421 ASSERT (StrSize (Destination
) != 0);
426 Returns the first occurrence of a Null-terminated Unicode sub-string
427 in a Null-terminated Unicode string.
429 This function scans the contents of the Null-terminated Unicode string
430 specified by String and returns the first occurrence of SearchString.
431 If SearchString is not found in String, then NULL is returned. If
432 the length of SearchString is zero, then String is
435 If String is NULL, then ASSERT().
436 If String is not aligned on a 16-bit boundary, then ASSERT().
437 If SearchString is NULL, then ASSERT().
438 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
440 If PcdMaximumUnicodeStringLength is not zero, and SearchString
441 or String contains more than PcdMaximumUnicodeStringLength Unicode
442 characters not including the Null-terminator, then ASSERT().
444 @param String Pointer to a Null-terminated Unicode string.
445 @param SearchString Pointer to a Null-terminated Unicode string to search for.
447 @retval NULL If the SearchString does not appear in String.
448 @return others If there is a match.
454 IN CONST CHAR16
*String
,
455 IN CONST CHAR16
*SearchString
458 CONST CHAR16
*FirstMatch
;
459 CONST CHAR16
*SearchStringTmp
;
462 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
463 // Length tests are performed inside StrLen().
465 ASSERT (StrSize (String
) != 0);
466 ASSERT (StrSize (SearchString
) != 0);
468 while (*String
!= '\0') {
469 SearchStringTmp
= SearchString
;
472 while ((*String
== *SearchStringTmp
)
473 && (*SearchStringTmp
!= '\0')
474 && (*String
!= '\0')) {
479 if (*SearchStringTmp
== '\0') {
480 return (CHAR16
*) FirstMatch
;
483 if (SearchStringTmp
== SearchString
) {
485 // If no character from SearchString match,
486 // move the pointer to the String under search
497 Check if a Unicode character is a decimal character.
499 This internal function checks if a Unicode character is a
500 decimal character. The valid decimal character is from
503 @param Char The character to check against.
505 @retval TRUE If the Char is a decmial character.
506 @retval FALSE If the Char is not a decmial character.
511 InternalIsDecimalDigitCharacter (
515 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
519 Convert a Unicode character to upper case only if
520 it maps to a valid small-case ASCII character.
522 This internal function only deal with Unicode character
523 which maps to a valid small-case ASCII character, i.e.
524 L'a' to L'z'. For other Unicode character, the input character
525 is returned directly.
527 @param Char The character to convert.
529 @retval LowerCharacter If the Char is with range L'a' to L'z'.
530 @retval Unchanged Otherwise.
535 InternalCharToUpper (
539 if (Char
>= L
'a' && Char
<= L
'z') {
540 return (CHAR16
) (Char
- (L
'a' - L
'A'));
547 Convert a Unicode character to numerical value.
549 This internal function only deal with Unicode character
550 which maps to a valid hexadecimal ASII character, i.e.
551 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
552 Unicode character, the value returned does not make sense.
554 @param Char The character to convert.
556 @return The numerical value converted.
561 InternalHexCharToUintn (
565 if (InternalIsDecimalDigitCharacter (Char
)) {
569 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
573 Check if a Unicode character is a hexadecimal character.
575 This internal function checks if a Unicode character is a
576 decimal character. The valid hexadecimal character is
577 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
580 @param Char The character to check against.
582 @retval TRUE If the Char is a hexadecmial character.
583 @retval FALSE If the Char is not a hexadecmial character.
588 InternalIsHexaDecimalDigitCharacter (
593 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
594 (Char
>= L
'A' && Char
<= L
'F') ||
595 (Char
>= L
'a' && Char
<= L
'f'));
599 Convert a Null-terminated Unicode decimal string to a value of
602 This function returns a value of type UINTN by interpreting the contents
603 of the Unicode string specified by String as a decimal number. The format
604 of the input Unicode string String is:
606 [spaces] [decimal digits].
608 The valid decimal digit character is in the range [0-9]. The
609 function will ignore the pad space, which includes spaces or
610 tab characters, before [decimal digits]. The running zero in the
611 beginning of [decimal digits] will be ignored. Then, the function
612 stops at the first character that is a not a valid decimal character
613 or a Null-terminator, whichever one comes first.
615 If String is NULL, then ASSERT().
616 If String is not aligned in a 16-bit boundary, then ASSERT().
617 If String has only pad spaces, then 0 is returned.
618 If String has no pad spaces or valid decimal digits,
620 If the number represented by String overflows according
621 to the range defined by UINTN, then ASSERT().
623 If PcdMaximumUnicodeStringLength is not zero, and String contains
624 more than PcdMaximumUnicodeStringLength Unicode characters not including
625 the Null-terminator, then ASSERT().
627 @param String Pointer to a Null-terminated Unicode string.
629 @retval Value translated from String.
635 IN CONST CHAR16
*String
641 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
642 // Length tests are performed inside StrLen().
644 ASSERT (StrSize (String
) != 0);
647 // Ignore the pad spaces (space or tab)
649 while ((*String
== L
' ') || (*String
== L
'\t')) {
654 // Ignore leading Zeros after the spaces
656 while (*String
== L
'0') {
662 while (InternalIsDecimalDigitCharacter (*String
)) {
664 // If the number represented by String overflows according
665 // to the range defined by UINTN, then ASSERT().
667 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
668 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
669 (*String
- L
'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
672 Result
= Result
* 10 + (*String
- L
'0');
681 Convert a Null-terminated Unicode decimal string to a value of
684 This function returns a value of type UINT64 by interpreting the contents
685 of the Unicode string specified by String as a decimal number. The format
686 of the input Unicode string String is:
688 [spaces] [decimal digits].
690 The valid decimal digit character is in the range [0-9]. The
691 function will ignore the pad space, which includes spaces or
692 tab characters, before [decimal digits]. The running zero in the
693 beginning of [decimal digits] will be ignored. Then, the function
694 stops at the first character that is a not a valid decimal character
695 or a Null-terminator, whichever one comes first.
697 If String is NULL, then ASSERT().
698 If String is not aligned in a 16-bit boundary, then ASSERT().
699 If String has only pad spaces, then 0 is returned.
700 If String has no pad spaces or valid decimal digits,
702 If the number represented by String overflows according
703 to the range defined by UINT64, then ASSERT().
705 If PcdMaximumUnicodeStringLength is not zero, and String contains
706 more than PcdMaximumUnicodeStringLength Unicode characters not including
707 the Null-terminator, then ASSERT().
709 @param String Pointer to a Null-terminated Unicode string.
711 @retval Value translated from String.
717 IN CONST CHAR16
*String
723 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
724 // Length tests are performed inside StrLen().
726 ASSERT (StrSize (String
) != 0);
729 // Ignore the pad spaces (space or tab)
731 while ((*String
== L
' ') || (*String
== L
'\t')) {
736 // Ignore leading Zeros after the spaces
738 while (*String
== L
'0') {
744 while (InternalIsDecimalDigitCharacter (*String
)) {
746 // If the number represented by String overflows according
747 // to the range defined by UINTN, then ASSERT().
749 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
750 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
751 (*String
- L
'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
754 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
762 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
764 This function returns a value of type UINTN by interpreting the contents
765 of the Unicode string specified by String as a hexadecimal number.
766 The format of the input Unicode string String is:
768 [spaces][zeros][x][hexadecimal digits].
770 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
771 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
772 If "x" appears in the input string, it must be prefixed with at least one 0.
773 The function will ignore the pad space, which includes spaces or tab characters,
774 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
775 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
776 first valid hexadecimal digit. Then, the function stops at the first character that is
777 a not a valid hexadecimal character or NULL, whichever one comes first.
779 If String is NULL, then ASSERT().
780 If String is not aligned in a 16-bit boundary, then ASSERT().
781 If String has only pad spaces, then zero is returned.
782 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
783 then zero is returned.
784 If the number represented by String overflows according to the range defined by
785 UINTN, then ASSERT().
787 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
788 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
791 @param String Pointer to a Null-terminated Unicode string.
793 @retval Value translated from String.
799 IN CONST CHAR16
*String
805 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
806 // Length tests are performed inside StrLen().
808 ASSERT (StrSize (String
) != 0);
811 // Ignore the pad spaces (space or tab)
813 while ((*String
== L
' ') || (*String
== L
'\t')) {
818 // Ignore leading Zeros after the spaces
820 while (*String
== L
'0') {
824 if (InternalCharToUpper (*String
) == L
'X') {
825 if (*(String
- 1) != L
'0') {
836 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
838 // If the Hex Number represented by String overflows according
839 // to the range defined by UINTN, then ASSERT().
841 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
842 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
843 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
846 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
855 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
857 This function returns a value of type UINT64 by interpreting the contents
858 of the Unicode string specified by String as a hexadecimal number.
859 The format of the input Unicode string String is
861 [spaces][zeros][x][hexadecimal digits].
863 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
864 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
865 If "x" appears in the input string, it must be prefixed with at least one 0.
866 The function will ignore the pad space, which includes spaces or tab characters,
867 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
868 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
869 first valid hexadecimal digit. Then, the function stops at the first character that is
870 a not a valid hexadecimal character or NULL, whichever one comes first.
872 If String is NULL, then ASSERT().
873 If String is not aligned in a 16-bit boundary, then ASSERT().
874 If String has only pad spaces, then zero is returned.
875 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
876 then zero is returned.
877 If the number represented by String overflows according to the range defined by
878 UINT64, then ASSERT().
880 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
881 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
884 @param String Pointer to a Null-terminated Unicode string.
886 @retval Value translated from String.
892 IN CONST CHAR16
*String
898 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
899 // Length tests are performed inside StrLen().
901 ASSERT (StrSize (String
) != 0);
904 // Ignore the pad spaces (space or tab)
906 while ((*String
== L
' ') || (*String
== L
'\t')) {
911 // Ignore leading Zeros after the spaces
913 while (*String
== L
'0') {
917 if (InternalCharToUpper (*String
) == L
'X') {
918 ASSERT (*(String
- 1) == L
'0');
919 if (*(String
- 1) != L
'0') {
930 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
932 // If the Hex Number represented by String overflows according
933 // to the range defined by UINTN, then ASSERT().
935 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
)||
936 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
937 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
940 Result
= LShiftU64 (Result
, 4);
941 Result
= Result
+ InternalHexCharToUintn (*String
);
949 Check if a ASCII character is a decimal character.
951 This internal function checks if a Unicode character is a
952 decimal character. The valid decimal character is from
955 @param Char The character to check against.
957 @retval TRUE If the Char is a decmial character.
958 @retval FALSE If the Char is not a decmial character.
963 InternalAsciiIsDecimalDigitCharacter (
967 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
971 Check if a ASCII character is a hexadecimal character.
973 This internal function checks if a ASCII character is a
974 decimal character. The valid hexadecimal character is
975 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
978 @param Char The character to check against.
980 @retval TRUE If the Char is a hexadecmial character.
981 @retval FALSE If the Char is not a hexadecmial character.
986 InternalAsciiIsHexaDecimalDigitCharacter (
991 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
992 (Char
>= 'A' && Char
<= 'F') ||
993 (Char
>= 'a' && Char
<= 'f'));
997 Convert a Null-terminated Unicode string to a Null-terminated
998 ASCII string and returns the ASCII string.
1000 This function converts the content of the Unicode string Source
1001 to the ASCII string Destination by copying the lower 8 bits of
1002 each Unicode character. It returns Destination.
1004 If any Unicode characters in Source contain non-zero value in
1005 the upper 8 bits, then ASSERT().
1007 If Destination is NULL, then ASSERT().
1008 If Source is NULL, then ASSERT().
1009 If Source is not aligned on a 16-bit boundary, then ASSERT().
1010 If Source and Destination overlap, then ASSERT().
1012 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1013 more than PcdMaximumUnicodeStringLength Unicode characters not including
1014 the Null-terminator, then ASSERT().
1016 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1017 than PcdMaximumAsciiStringLength Unicode characters not including the
1018 Null-terminator, then ASSERT().
1020 @param Source Pointer to a Null-terminated Unicode string.
1021 @param Destination Pointer to a Null-terminated ASCII string.
1023 @return Destination.
1028 UnicodeStrToAsciiStr (
1029 IN CONST CHAR16
*Source
,
1030 OUT CHAR8
*Destination
1035 ASSERT (Destination
!= NULL
);
1038 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1039 // Length tests are performed inside StrLen().
1041 ASSERT (StrSize (Source
) != 0);
1044 // Source and Destination should not overlap
1046 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1047 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1050 ReturnValue
= Destination
;
1051 while (*Source
!= '\0') {
1053 // If any Unicode characters in Source contain
1054 // non-zero value in the upper 8 bits, then ASSERT().
1056 ASSERT (*Source
< 0x100);
1057 *(Destination
++) = (CHAR8
) *(Source
++);
1060 *Destination
= '\0';
1063 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1064 // Length tests are performed inside AsciiStrLen().
1066 ASSERT (AsciiStrSize (ReturnValue
) != 0);
1073 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1074 string and returns the new ASCII string.
1076 This function copies the contents of the ASCII string Source to the ASCII
1077 string Destination, and returns Destination. If Source and Destination
1078 overlap, then the results are undefined.
1080 If Destination is NULL, then ASSERT().
1081 If Source is NULL, then ASSERT().
1082 If Source and Destination overlap, then ASSERT().
1083 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1084 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1087 @param Destination Pointer to a Null-terminated ASCII string.
1088 @param Source Pointer to a Null-terminated ASCII string.
1096 OUT CHAR8
*Destination
,
1097 IN CONST CHAR8
*Source
1103 // Destination cannot be NULL
1105 ASSERT (Destination
!= NULL
);
1108 // Destination and source cannot overlap
1110 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1111 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1113 ReturnValue
= Destination
;
1114 while (*Source
!= 0) {
1115 *(Destination
++) = *(Source
++);
1122 Copies up to a specified length one Null-terminated ASCII string to another
1123 Null-terminated ASCII string and returns the new ASCII string.
1125 This function copies the contents of the ASCII string Source to the ASCII
1126 string Destination, and returns Destination. At most, Length ASCII characters
1127 are copied from Source to Destination. If Length is 0, then Destination is
1128 returned unmodified. If Length is greater that the number of ASCII characters
1129 in Source, then Destination is padded with Null ASCII characters. If Source
1130 and Destination overlap, then the results are undefined.
1132 If Destination is NULL, then ASSERT().
1133 If Source is NULL, then ASSERT().
1134 If Source and Destination overlap, then ASSERT().
1135 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1136 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1139 @param Destination Pointer to a Null-terminated ASCII string.
1140 @param Source Pointer to a Null-terminated ASCII string.
1141 @param Length Maximum number of ASCII characters to copy.
1149 OUT CHAR8
*Destination
,
1150 IN CONST CHAR8
*Source
,
1161 // Destination cannot be NULL
1163 ASSERT (Destination
!= NULL
);
1166 // Destination and source cannot overlap
1168 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1169 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1171 ReturnValue
= Destination
;
1173 while (*Source
!= 0 && Length
> 0) {
1174 *(Destination
++) = *(Source
++);
1178 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1183 Returns the length of a Null-terminated ASCII string.
1185 This function returns the number of ASCII characters in the Null-terminated
1186 ASCII string specified by String.
1188 If Length > 0 and Destination is NULL, then ASSERT().
1189 If Length > 0 and Source is NULL, then ASSERT().
1190 If PcdMaximumAsciiStringLength is not zero and String contains more than
1191 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1194 @param String Pointer to a Null-terminated ASCII string.
1196 @return The length of String.
1202 IN CONST CHAR8
*String
1207 ASSERT (String
!= NULL
);
1209 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1211 // If PcdMaximumUnicodeStringLength is not zero,
1212 // length should not more than PcdMaximumUnicodeStringLength
1214 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1215 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1222 Returns the size of a Null-terminated ASCII string in bytes, including the
1225 This function returns the size, in bytes, of the Null-terminated ASCII string
1226 specified by String.
1228 If String is NULL, then ASSERT().
1229 If PcdMaximumAsciiStringLength is not zero and String contains more than
1230 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1233 @param String Pointer to a Null-terminated ASCII string.
1235 @return The size of String.
1241 IN CONST CHAR8
*String
1244 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1248 Compares two Null-terminated ASCII strings, and returns the difference
1249 between the first mismatched ASCII characters.
1251 This function compares the Null-terminated ASCII string FirstString to the
1252 Null-terminated ASCII string SecondString. If FirstString is identical to
1253 SecondString, then 0 is returned. Otherwise, the value returned is the first
1254 mismatched ASCII character in SecondString subtracted from the first
1255 mismatched ASCII character in FirstString.
1257 If FirstString is NULL, then ASSERT().
1258 If SecondString is NULL, then ASSERT().
1259 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1260 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1262 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1263 than PcdMaximumAsciiStringLength ASCII characters not including the
1264 Null-terminator, then ASSERT().
1266 @param FirstString Pointer to a Null-terminated ASCII string.
1267 @param SecondString Pointer to a Null-terminated ASCII string.
1269 @retval ==0 FirstString is identical to SecondString.
1270 @retval !=0 FirstString is not identical to SecondString.
1276 IN CONST CHAR8
*FirstString
,
1277 IN CONST CHAR8
*SecondString
1281 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1283 ASSERT (AsciiStrSize (FirstString
));
1284 ASSERT (AsciiStrSize (SecondString
));
1286 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1291 return *FirstString
- *SecondString
;
1295 Converts a lowercase Ascii character to upper one.
1297 If Chr is lowercase Ascii character, then converts it to upper one.
1299 If Value >= 0xA0, then ASSERT().
1300 If (Value & 0x0F) >= 0x0A, then ASSERT().
1302 @param Chr one Ascii character
1304 @return The uppercase value of Ascii character
1313 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1317 Convert a ASCII character to numerical value.
1319 This internal function only deal with Unicode character
1320 which maps to a valid hexadecimal ASII character, i.e.
1321 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1322 ASCII character, the value returned does not make sense.
1324 @param Char The character to convert.
1326 @return The numerical value converted.
1331 InternalAsciiHexCharToUintn (
1335 if (InternalIsDecimalDigitCharacter (Char
)) {
1339 return (UINTN
) (10 + AsciiToUpper (Char
) - 'A');
1344 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1345 and returns the difference between the first mismatched ASCII characters.
1347 This function performs a case insensitive comparison of the Null-terminated
1348 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1349 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1350 value returned is the first mismatched lower case ASCII character in
1351 SecondString subtracted from the first mismatched lower case ASCII character
1354 If FirstString is NULL, then ASSERT().
1355 If SecondString is NULL, then ASSERT().
1356 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1357 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1359 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1360 than PcdMaximumAsciiStringLength ASCII characters not including the
1361 Null-terminator, then ASSERT().
1363 @param FirstString Pointer to a Null-terminated ASCII string.
1364 @param SecondString Pointer to a Null-terminated ASCII string.
1366 @retval ==0 FirstString is identical to SecondString using case insensitive
1368 @retval !=0 FirstString is not identical to SecondString using case
1369 insensitive comparisons.
1375 IN CONST CHAR8
*FirstString
,
1376 IN CONST CHAR8
*SecondString
1379 CHAR8 UpperFirstString
;
1380 CHAR8 UpperSecondString
;
1383 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1385 ASSERT (AsciiStrSize (FirstString
));
1386 ASSERT (AsciiStrSize (SecondString
));
1388 UpperFirstString
= AsciiToUpper (*FirstString
);
1389 UpperSecondString
= AsciiToUpper (*SecondString
);
1390 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1393 UpperFirstString
= AsciiToUpper (*FirstString
);
1394 UpperSecondString
= AsciiToUpper (*SecondString
);
1397 return UpperFirstString
- UpperSecondString
;
1401 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1402 the difference between the first mismatched ASCII characters.
1404 This function compares the Null-terminated ASCII string FirstString to the
1405 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1406 will be compared. If Length is 0, then 0 is returned. If FirstString is
1407 identical to SecondString, then 0 is returned. Otherwise, the value returned
1408 is the first mismatched ASCII character in SecondString subtracted from the
1409 first mismatched ASCII character in FirstString.
1411 If Length > 0 and FirstString is NULL, then ASSERT().
1412 If Length > 0 and SecondString is NULL, then ASSERT().
1413 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1414 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1416 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
1417 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1420 @param FirstString Pointer to a Null-terminated ASCII string.
1421 @param SecondString Pointer to a Null-terminated ASCII string.
1422 @param Length Maximum number of ASCII characters for compare.
1424 @retval ==0 FirstString is identical to SecondString.
1425 @retval !=0 FirstString is not identical to SecondString.
1431 IN CONST CHAR8
*FirstString
,
1432 IN CONST CHAR8
*SecondString
,
1441 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1443 ASSERT (AsciiStrSize (FirstString
));
1444 ASSERT (AsciiStrSize (SecondString
));
1446 while ((*FirstString
!= '\0') &&
1447 (*FirstString
== *SecondString
) &&
1453 return *FirstString
- *SecondString
;
1457 Concatenates one Null-terminated ASCII string to another Null-terminated
1458 ASCII string, and returns the concatenated ASCII string.
1460 This function concatenates two Null-terminated ASCII strings. The contents of
1461 Null-terminated ASCII string Source are concatenated to the end of Null-
1462 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1465 If Destination is NULL, then ASSERT().
1466 If Source is NULL, then ASSERT().
1467 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1468 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1470 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1471 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1473 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1474 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1475 ASCII characters, then ASSERT().
1477 @param Destination Pointer to a Null-terminated ASCII string.
1478 @param Source Pointer to a Null-terminated ASCII string.
1486 IN OUT CHAR8
*Destination
,
1487 IN CONST CHAR8
*Source
1490 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1493 // Size of the resulting string should never be zero.
1494 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1496 ASSERT (AsciiStrSize (Destination
) != 0);
1501 Concatenates up to a specified length one Null-terminated ASCII string to
1502 the end of another Null-terminated ASCII string, and returns the
1503 concatenated ASCII string.
1505 This function concatenates two Null-terminated ASCII strings. The contents
1506 of Null-terminated ASCII string Source are concatenated to the end of Null-
1507 terminated ASCII string Destination, and Destination is returned. At most,
1508 Length ASCII characters are concatenated from Source to the end of
1509 Destination, and Destination is always Null-terminated. If Length is 0, then
1510 Destination is returned unmodified. If Source and Destination overlap, then
1511 the results are undefined.
1513 If Length > 0 and Destination is NULL, then ASSERT().
1514 If Length > 0 and Source is NULL, then ASSERT().
1515 If Source and Destination overlap, then ASSERT().
1516 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1517 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1519 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1520 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1522 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1523 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1524 ASCII characters not including the Null-terminator, then ASSERT().
1526 @param Destination Pointer to a Null-terminated ASCII string.
1527 @param Source Pointer to a Null-terminated ASCII string.
1528 @param Length Maximum number of ASCII characters to concatenate from
1537 IN OUT CHAR8
*Destination
,
1538 IN CONST CHAR8
*Source
,
1542 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
1545 // Size of the resulting string should never be zero.
1546 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1548 ASSERT (AsciiStrSize (Destination
) != 0);
1553 Returns the first occurrence of a Null-terminated ASCII sub-string
1554 in a Null-terminated ASCII string.
1556 This function scans the contents of the ASCII string specified by String
1557 and returns the first occurrence of SearchString. If SearchString is not
1558 found in String, then NULL is returned. If the length of SearchString is zero,
1559 then String is returned.
1561 If String is NULL, then ASSERT().
1562 If SearchString is NULL, then ASSERT().
1564 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1565 String contains more than PcdMaximumAsciiStringLength Unicode characters
1566 not including the Null-terminator, then ASSERT().
1568 @param String Pointer to a Null-terminated ASCII string.
1569 @param SearchString Pointer to a Null-terminated ASCII string to search for.
1571 @retval NULL If the SearchString does not appear in String.
1572 @retval others If there is a match return the first occurrence of SearchingString.
1573 If the length of SearchString is zero,return String.
1579 IN CONST CHAR8
*String
,
1580 IN CONST CHAR8
*SearchString
1583 CONST CHAR8
*FirstMatch
;
1584 CONST CHAR8
*SearchStringTmp
;
1587 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1589 ASSERT (AsciiStrSize (String
) != 0);
1590 ASSERT (AsciiStrSize (SearchString
) != 0);
1592 while (*String
!= '\0') {
1593 SearchStringTmp
= SearchString
;
1594 FirstMatch
= String
;
1596 while ((*String
== *SearchStringTmp
)
1597 && (*SearchStringTmp
!= '\0')
1598 && (*String
!= '\0')) {
1603 if (*SearchStringTmp
== '\0') {
1604 return (CHAR8
*) FirstMatch
;
1607 if (SearchStringTmp
== SearchString
) {
1609 // If no character from SearchString match,
1610 // move the pointer to the String under search
1611 // by one character.
1622 Convert a Null-terminated ASCII decimal string to a value of type
1625 This function returns a value of type UINTN by interpreting the contents
1626 of the ASCII string String as a decimal number. The format of the input
1627 ASCII string String is:
1629 [spaces] [decimal digits].
1631 The valid decimal digit character is in the range [0-9]. The function will
1632 ignore the pad space, which includes spaces or tab characters, before the digits.
1633 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1634 function stops at the first character that is a not a valid decimal character or
1635 Null-terminator, whichever on comes first.
1637 If String has only pad spaces, then 0 is returned.
1638 If String has no pad spaces or valid decimal digits, then 0 is returned.
1639 If the number represented by String overflows according to the range defined by
1640 UINTN, then ASSERT().
1641 If String is NULL, then ASSERT().
1642 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1643 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1646 @param String Pointer to a Null-terminated ASCII string.
1648 @retval Value translated from String.
1653 AsciiStrDecimalToUintn (
1654 IN CONST CHAR8
*String
1660 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1662 ASSERT (AsciiStrSize (String
) != 0);
1665 // Ignore the pad spaces (space or tab)
1667 while ((*String
== ' ') || (*String
== '\t' )) {
1672 // Ignore leading Zeros after the spaces
1674 while (*String
== '0') {
1680 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1682 // If the number represented by String overflows according
1683 // to the range defined by UINTN, then ASSERT().
1685 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1686 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1687 (*String
- '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
1690 Result
= Result
* 10 + (*String
- '0');
1699 Convert a Null-terminated ASCII decimal string to a value of type
1702 This function returns a value of type UINT64 by interpreting the contents
1703 of the ASCII string String as a decimal number. The format of the input
1704 ASCII string String is:
1706 [spaces] [decimal digits].
1708 The valid decimal digit character is in the range [0-9]. The function will
1709 ignore the pad space, which includes spaces or tab characters, before the digits.
1710 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1711 function stops at the first character that is a not a valid decimal character or
1712 Null-terminator, whichever on comes first.
1714 If String has only pad spaces, then 0 is returned.
1715 If String has no pad spaces or valid decimal digits, then 0 is returned.
1716 If the number represented by String overflows according to the range defined by
1717 UINT64, then ASSERT().
1718 If String is NULL, then ASSERT().
1719 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1720 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1723 @param String Pointer to a Null-terminated ASCII string.
1725 @retval Value translated from String.
1730 AsciiStrDecimalToUint64 (
1731 IN CONST CHAR8
*String
1737 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1739 ASSERT (AsciiStrSize (String
) != 0);
1742 // Ignore the pad spaces (space or tab)
1744 while ((*String
== ' ') || (*String
== '\t' )) {
1749 // Ignore leading Zeros after the spaces
1751 while (*String
== '0') {
1757 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1759 // If the number represented by String overflows according
1760 // to the range defined by UINTN, then ASSERT().
1762 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1763 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1764 (*String
- '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
1767 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1775 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1777 This function returns a value of type UINTN by interpreting the contents of
1778 the ASCII string String as a hexadecimal number. The format of the input ASCII
1781 [spaces][zeros][x][hexadecimal digits].
1783 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1784 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1785 appears in the input string, it must be prefixed with at least one 0. The function
1786 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1787 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1788 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1789 digit. Then, the function stops at the first character that is a not a valid
1790 hexadecimal character or Null-terminator, whichever on comes first.
1792 If String has only pad spaces, then 0 is returned.
1793 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1796 If the number represented by String overflows according to the range defined by UINTN,
1798 If String is NULL, then ASSERT().
1799 If PcdMaximumAsciiStringLength is not zero,
1800 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1801 the Null-terminator, then ASSERT().
1803 @param String Pointer to a Null-terminated ASCII string.
1805 @retval Value translated from String.
1810 AsciiStrHexToUintn (
1811 IN CONST CHAR8
*String
1817 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1819 ASSERT (AsciiStrSize (String
) != 0);
1822 // Ignore the pad spaces (space or tab)
1824 while ((*String
== ' ') || (*String
== '\t' )) {
1829 // Ignore leading Zeros after the spaces
1831 while (*String
== '0') {
1835 if (AsciiToUpper (*String
) == 'X') {
1836 ASSERT (*(String
- 1) == '0');
1837 if (*(String
- 1) != '0') {
1848 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1850 // If the Hex Number represented by String overflows according
1851 // to the range defined by UINTN, then ASSERT().
1853 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1854 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1855 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
1858 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1867 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1869 This function returns a value of type UINT64 by interpreting the contents of
1870 the ASCII string String as a hexadecimal number. The format of the input ASCII
1873 [spaces][zeros][x][hexadecimal digits].
1875 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1876 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1877 appears in the input string, it must be prefixed with at least one 0. The function
1878 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1879 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1880 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1881 digit. Then, the function stops at the first character that is a not a valid
1882 hexadecimal character or Null-terminator, whichever on comes first.
1884 If String has only pad spaces, then 0 is returned.
1885 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1888 If the number represented by String overflows according to the range defined by UINT64,
1890 If String is NULL, then ASSERT().
1891 If PcdMaximumAsciiStringLength is not zero,
1892 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1893 the Null-terminator, then ASSERT().
1895 @param String Pointer to a Null-terminated ASCII string.
1897 @retval Value translated from String.
1902 AsciiStrHexToUint64 (
1903 IN CONST CHAR8
*String
1909 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1911 ASSERT (AsciiStrSize (String
) != 0);
1914 // Ignore the pad spaces (space or tab) and leading Zeros
1917 // Ignore the pad spaces (space or tab)
1919 while ((*String
== ' ') || (*String
== '\t' )) {
1924 // Ignore leading Zeros after the spaces
1926 while (*String
== '0') {
1930 if (AsciiToUpper (*String
) == 'X') {
1931 ASSERT (*(String
- 1) == '0');
1932 if (*(String
- 1) != '0') {
1943 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1945 // If the Hex Number represented by String overflows according
1946 // to the range defined by UINTN, then ASSERT().
1948 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1949 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1950 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
1953 Result
= LShiftU64 (Result
, 4);
1954 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1963 Convert one Null-terminated ASCII string to a Null-terminated
1964 Unicode string and returns the Unicode string.
1966 This function converts the contents of the ASCII string Source to the Unicode
1967 string Destination, and returns Destination. The function terminates the
1968 Unicode string Destination by appending a Null-terminator character at the end.
1969 The caller is responsible to make sure Destination points to a buffer with size
1970 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1972 If Destination is NULL, then ASSERT().
1973 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1974 If Source is NULL, then ASSERT().
1975 If Source and Destination overlap, then ASSERT().
1976 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1977 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1979 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1980 PcdMaximumUnicodeStringLength ASCII characters not including the
1981 Null-terminator, then ASSERT().
1983 @param Source Pointer to a Null-terminated ASCII string.
1984 @param Destination Pointer to a Null-terminated Unicode string.
1986 @return Destination.
1991 AsciiStrToUnicodeStr (
1992 IN CONST CHAR8
*Source
,
1993 OUT CHAR16
*Destination
1996 CHAR16
*ReturnValue
;
1998 ASSERT (Destination
!= NULL
);
2001 // ASSERT Source is less long than PcdMaximumAsciiStringLength
2003 ASSERT (AsciiStrSize (Source
) != 0);
2006 // Source and Destination should not overlap
2008 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
2009 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) > (AsciiStrLen (Source
) * sizeof (CHAR16
)));
2012 ReturnValue
= Destination
;
2013 while (*Source
!= '\0') {
2014 *(Destination
++) = (CHAR16
) *(Source
++);
2017 // End the Destination with a NULL.
2019 *Destination
= '\0';
2022 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
2024 ASSERT (StrSize (ReturnValue
) != 0);
2030 Converts an 8-bit value to an 8-bit BCD value.
2032 Converts the 8-bit value specified by Value to BCD. The BCD value is
2035 If Value >= 100, then ASSERT().
2037 @param Value The 8-bit value to convert to BCD. Range 0..99.
2039 @return The BCD value.
2048 ASSERT (Value
< 100);
2049 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2053 Converts an 8-bit BCD value to an 8-bit value.
2055 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2058 If Value >= 0xA0, then ASSERT().
2059 If (Value & 0x0F) >= 0x0A, then ASSERT().
2061 @param Value The 8-bit BCD value to convert to an 8-bit value.
2063 @return The 8-bit value is returned.
2072 ASSERT (Value
< 0xa0);
2073 ASSERT ((Value
& 0xf) < 0xa);
2074 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));