2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2007, 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.
17 #include "BaseLibInternals.h"
20 Copies one Null-terminated Unicode string to another Null-terminated Unicode
21 string and returns the new Unicode string.
23 This function copies the contents of the Unicode string Source to the Unicode
24 string Destination, and returns Destination. If Source and Destination
25 overlap, then the results are undefined.
27 If Destination is NULL, then ASSERT().
28 If Source is NULL, then ASSERT().
29 If Source and Destination overlap, then ASSERT().
30 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
31 PcdMaximumUnicodeStringLength Unicode characters not including the
32 Null-terminator, then ASSERT().
34 @param Destination Pointer to a Null-terminated Unicode string.
35 @param Source Pointer to a Null-terminated Unicode string.
43 OUT CHAR16
*Destination
,
44 IN CONST CHAR16
*Source
50 // Destination cannot be NULL
52 ASSERT (Destination
!= NULL
);
55 // Destination and source cannot overlap
57 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
58 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
60 ReturnValue
= Destination
;
62 *(Destination
++) = *(Source
++);
69 Copies one Null-terminated Unicode string with a maximum length to another
70 Null-terminated Unicode string with a maximum length and returns the new
73 This function copies the contents of the Unicode string Source to the Unicode
74 string Destination, and returns Destination. At most, Length Unicode
75 characters are copied from Source to Destination. If Length is 0, then
76 Destination is returned unmodified. If Length is greater that the number of
77 Unicode characters in Source, then Destination is padded with Null Unicode
78 characters. If Source and Destination overlap, then the results are
81 If Destination is NULL, then ASSERT().
82 If Source is NULL, then ASSERT().
83 If Source and Destination overlap, then ASSERT().
84 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
85 PcdMaximumUnicodeStringLength Unicode characters not including the
86 Null-terminator, then ASSERT().
88 @param Destination Pointer to a Null-terminated Unicode string.
89 @param Source Pointer to a Null-terminated Unicode string.
90 @param Length Maximum number of Unicode characters to copy.
98 OUT CHAR16
*Destination
,
99 IN CONST CHAR16
*Source
,
110 // Destination cannot be NULL if Length is not zero
112 ASSERT (Destination
!= NULL
);
115 // Destination and source cannot overlap
116 // Q: Does Source have to be NULL-terminated?
118 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
119 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
121 ReturnValue
= Destination
;
123 while ((*Source
!= L
'\0') && (Length
> 0)) {
124 *(Destination
++) = *(Source
++);
128 ZeroMem (Destination
, Length
* sizeof (*Destination
));
133 Returns the length of a Null-terminated Unicode string.
135 This function returns the number of Unicode characters in the Null-terminated
136 Unicode string specified by String.
138 If String is NULL, then ASSERT().
139 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
140 PcdMaximumUnicodeStringLength Unicode characters not including the
141 Null-terminator, then ASSERT().
143 @param String Pointer to a Null-terminated Unicode string.
145 @return The length of String.
151 IN CONST CHAR16
*String
156 ASSERT (String
!= NULL
);
158 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
160 // If PcdMaximumUnicodeStringLength is not zero,
161 // length should not more than PcdMaximumUnicodeStringLength
163 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
164 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
171 Returns the size of a Null-terminated Unicode string in bytes, including the
174 This function returns the size, in bytes, of the Null-terminated Unicode
175 string specified by String.
177 If String is NULL, then ASSERT().
178 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
179 PcdMaximumUnicodeStringLength Unicode characters not including the
180 Null-terminator, then ASSERT().
182 @param String Pointer to a Null-terminated Unicode string.
184 @return The size of String.
190 IN CONST CHAR16
*String
193 return (StrLen (String
) + 1) * sizeof (*String
);
197 Compares two Null-terminated Unicode strings, and returns the difference
198 between the first mismatched Unicode characters.
200 This function compares the Null-terminated Unicode string FirstString to the
201 Null-terminated Unicode string SecondString. If FirstString is identical to
202 SecondString, then 0 is returned. Otherwise, the value returned is the first
203 mismatched Unicode character in SecondString subtracted from the first
204 mismatched Unicode character in FirstString.
206 If FirstString is NULL, then ASSERT().
207 If SecondString is NULL, then ASSERT().
208 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
209 than PcdMaximumUnicodeStringLength Unicode characters not including the
210 Null-terminator, then ASSERT().
211 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
212 than PcdMaximumUnicodeStringLength Unicode characters not including the
213 Null-terminator, then ASSERT().
215 @param FirstString Pointer to a Null-terminated Unicode string.
216 @param SecondString Pointer to a Null-terminated Unicode string.
218 @retval 0 FirstString is identical to SecondString.
219 @retval !=0 FirstString is not identical to SecondString.
225 IN CONST CHAR16
*FirstString
,
226 IN CONST CHAR16
*SecondString
230 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
232 ASSERT (StrSize (FirstString
) != 0);
233 ASSERT (StrSize (SecondString
) != 0);
235 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
239 return *FirstString
- *SecondString
;
243 Compares two Null-terminated Unicode strings with maximum lengths, and
244 returns the difference between the first mismatched Unicode characters.
246 This function compares the Null-terminated Unicode string FirstString to the
247 Null-terminated Unicode string SecondString. At most, Length Unicode
248 characters will be compared. If Length is 0, then 0 is returned. If
249 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
250 value returned is the first mismatched Unicode character in SecondString
251 subtracted from the first mismatched Unicode character in FirstString.
253 If FirstString is NULL, then ASSERT().
254 If SecondString is NULL, then ASSERT().
255 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
256 than PcdMaximumUnicodeStringLength Unicode characters not including the
257 Null-terminator, then ASSERT().
258 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
259 than PcdMaximumUnicodeStringLength Unicode characters not including the
260 Null-terminator, then ASSERT().
262 @param FirstString Pointer to a Null-terminated Unicode string.
263 @param SecondString Pointer to a Null-terminated Unicode string.
264 @param Length Maximum number of Unicode characters to compare.
266 @retval 0 FirstString is identical to SecondString.
267 @retval !=0 FirstString is not identical to SecondString.
273 IN CONST CHAR16
*FirstString
,
274 IN CONST CHAR16
*SecondString
,
283 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
284 // Length tests are performed inside StrLen().
286 ASSERT (StrSize (FirstString
) != 0);
287 ASSERT (StrSize (SecondString
) != 0);
289 while ((*FirstString
!= L
'\0') &&
290 (*FirstString
== *SecondString
) &&
297 return *FirstString
- *SecondString
;
301 Concatenates one Null-terminated Unicode string to another Null-terminated
302 Unicode string, and returns the concatenated Unicode string.
304 This function concatenates two Null-terminated Unicode strings. The contents
305 of Null-terminated Unicode string Source are concatenated to the end of
306 Null-terminated Unicode string Destination. The Null-terminated concatenated
307 Unicode String is returned. If Source and Destination overlap, then the
308 results are undefined.
310 If Destination is NULL, then ASSERT().
311 If Source is NULL, then ASSERT().
312 If Source and Destination overlap, then ASSERT().
313 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
314 than PcdMaximumUnicodeStringLength Unicode characters not including the
315 Null-terminator, then ASSERT().
316 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
317 PcdMaximumUnicodeStringLength Unicode characters not including the
318 Null-terminator, then ASSERT().
319 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
320 and Source results in a Unicode string with more than
321 PcdMaximumUnicodeStringLength Unicode characters not including the
322 Null-terminator, then ASSERT().
324 @param Destination Pointer to a Null-terminated Unicode string.
325 @param Source Pointer to a Null-terminated Unicode string.
333 IN OUT CHAR16
*Destination
,
334 IN CONST CHAR16
*Source
337 StrCpy (Destination
+ StrLen (Destination
), Source
);
340 // Size of the resulting string should never be zero.
341 // PcdMaximumUnicodeStringLength is tested inside StrLen().
343 ASSERT (StrSize (Destination
) != 0);
348 Concatenates one Null-terminated Unicode string with a maximum length to the
349 end of another Null-terminated Unicode string, and returns the concatenated
352 This function concatenates two Null-terminated Unicode strings. The contents
353 of Null-terminated Unicode string Source are concatenated to the end of
354 Null-terminated Unicode string Destination, and Destination is returned. At
355 most, Length Unicode characters are concatenated from Source to the end of
356 Destination, and Destination is always Null-terminated. If Length is 0, then
357 Destination is returned unmodified. If Source and Destination overlap, then
358 the results are undefined.
360 If Destination is NULL, then ASSERT().
361 If Source is NULL, then ASSERT().
362 If Source and Destination overlap, then ASSERT().
363 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
364 than PcdMaximumUnicodeStringLength Unicode characters not including the
365 Null-terminator, then ASSERT().
366 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
367 PcdMaximumUnicodeStringLength Unicode characters not including the
368 Null-terminator, then ASSERT().
369 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
370 and Source results in a Unicode string with more than
371 PcdMaximumUnicodeStringLength Unicode characters not including the
372 Null-terminator, then ASSERT().
374 @param Destination Pointer to a Null-terminated Unicode string.
375 @param Source Pointer to a Null-terminated Unicode string.
376 @param Length Maximum number of Unicode characters to concatenate from
385 IN OUT CHAR16
*Destination
,
386 IN CONST CHAR16
*Source
,
390 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
393 // Size of the resulting string should never be zero.
394 // PcdMaximumUnicodeStringLength is tested inside StrLen().
396 ASSERT (StrSize (Destination
) != 0);
401 Returns the first occurance of a Null-terminated Unicode sub-string
402 in a Null-terminated Unicode string.
404 This function scans the contents of the Null-terminated Unicode string
405 specified by String and returns the first occurrence of SearchString.
406 If SearchString is not found in String, then NULL is returned. If
407 the length of SearchString is zero, then String is
410 If String is NULL, then ASSERT().
411 If String is not aligned on a 16-bit boundary, then ASSERT().
412 If SearchString is NULL, then ASSERT().
413 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
415 If PcdMaximumUnicodeStringLength is not zero, and SearchString
416 or String contains more than PcdMaximumUnicodeStringLength Unicode
417 characters not including the Null-terminator, then ASSERT().
419 @param String Pointer to a Null-terminated Unicode string.
420 @param SearchString Pointer to a Null-terminated Unicode string to search for.
422 @retval NULL If the SearchString does not appear in String.
423 @retval !NULL If there is a match.
429 IN CONST CHAR16
*String
,
430 IN CONST CHAR16
*SearchString
433 CONST CHAR16
*FirstMatch
;
434 CONST CHAR16
*SearchStringTmp
;
436 ASSERT (String
!= NULL
);
437 ASSERT (((UINTN
) String
& 0x01) == 0);
438 ASSERT (SearchString
!= NULL
);
439 ASSERT (((UINTN
) SearchString
& 0x01) == 0);
442 // If PcdMaximumUnicodeStringLength is not zero,
443 // length of String should not more than PcdMaximumUnicodeStringLength
445 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
446 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
450 // If PcdMaximumUnicodeStringLength is not zero,
451 // length of SearchString should not more than PcdMaximumUnicodeStringLength
453 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
454 ASSERT (StrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
457 while (*String
!= '\0') {
458 SearchStringTmp
= SearchString
;
461 while ((*String
== *SearchStringTmp
)
462 && (*SearchStringTmp
!= '\0')
463 && (*String
!= '\0')) {
468 if (*SearchStringTmp
== '\0') {
469 return (CHAR16
*) FirstMatch
;
472 if (SearchStringTmp
== SearchString
) {
474 // If no character from SearchString match,
475 // move the pointer to the String under search
486 Check if a Unicode character is a decimal character.
488 This internal function checks if a Unicode character is a
489 decimal character. The valid decimal character is from
493 @param Char The character to check against.
495 @retval TRUE If the Char is a decmial character.
496 @retval FALSE Otherwise.
501 InternalIsDecimalDigitCharacter (
505 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
509 Convert a Unicode character to upper case only if
510 it maps to a valid small-case ASCII character.
512 This internal function only deal with Unicode character
513 which maps to a valid small-case ASII character, i.e.
514 L'a' to L'z'. For other Unicode character, the input character
515 is returned directly.
518 @param Char The character to convert.
520 @retval LowerCharacter If the Char is with range L'a' to L'z'.
521 @retval Unchanged Otherwise.
526 InternalCharToUpper (
530 if (Char
>= L
'a' && Char
<= L
'z') {
531 return (CHAR16
) (Char
- (L
'a' - L
'A'));
538 Convert a Unicode character to numerical value.
540 This internal function only deal with Unicode character
541 which maps to a valid hexadecimal ASII character, i.e.
542 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
543 Unicode character, the value returned does not make sense.
545 @param Char The character to convert.
547 @retval UINTN The numerical value converted.
552 InternalHexCharToUintn (
556 if (InternalIsDecimalDigitCharacter (Char
)) {
560 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
564 Check if a Unicode character is a hexadecimal character.
566 This internal function checks if a Unicode character is a
567 decimal character. The valid hexadecimal character is
568 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
571 @param Char The character to check against.
573 @retval TRUE If the Char is a hexadecmial character.
574 @retval FALSE Otherwise.
579 InternalIsHexaDecimalDigitCharacter (
584 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
585 (Char
>= L
'A' && Char
<= L
'F') ||
586 (Char
>= L
'a' && Char
<= L
'f'));
590 Convert a Null-terminated Unicode decimal string to a value of
593 This function returns a value of type UINTN by interpreting the contents
594 of the Unicode string specified by String as a decimal number. The format
595 of the input Unicode string String is:
597 [spaces] [decimal digits].
599 The valid decimal digit character is in the range [0-9]. The
600 function will ignore the pad space, which includes spaces or
601 tab characters, before [decimal digits]. The running zero in the
602 beginning of [decimal digits] will be ignored. Then, the function
603 stops at the first character that is a not a valid decimal character
604 or a Null-terminator, whichever one comes first.
606 If String is NULL, then ASSERT().
607 If String is not aligned in a 16-bit boundary, then ASSERT().
608 If String has only pad spaces, then 0 is returned.
609 If String has no pad spaces or valid decimal digits,
611 If the number represented by String overflows according
612 to the range defined by UINTN, then ASSERT().
614 If PcdMaximumUnicodeStringLength is not zero, and String contains
615 more than PcdMaximumUnicodeStringLength Unicode characters not including
616 the Null-terminator, then ASSERT().
618 @param String Pointer to a Null-terminated Unicode string.
626 IN CONST CHAR16
*String
631 ASSERT (String
!= NULL
);
632 ASSERT (((UINTN
) String
& 0x01) == 0);
633 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
636 // Ignore the pad spaces (space or tab)
638 while ((*String
== L
' ') || (*String
== L
'\t')) {
643 // Ignore leading Zeros after the spaces
645 while (*String
== L
'0') {
651 while (InternalIsDecimalDigitCharacter (*String
)) {
653 // If the number represented by String overflows according
654 // to the range defined by UINTN, then ASSERT().
656 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
657 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
658 (*String
- L
'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
661 Result
= Result
* 10 + (*String
- L
'0');
670 Convert a Null-terminated Unicode decimal string to a value of
673 This function returns a value of type UINT64 by interpreting the contents
674 of the Unicode string specified by String as a decimal number. The format
675 of the input Unicode string String is:
677 [spaces] [decimal digits].
679 The valid decimal digit character is in the range [0-9]. The
680 function will ignore the pad space, which includes spaces or
681 tab characters, before [decimal digits]. The running zero in the
682 beginning of [decimal digits] will be ignored. Then, the function
683 stops at the first character that is a not a valid decimal character
684 or a Null-terminator, whichever one comes first.
686 If String is NULL, then ASSERT().
687 If String is not aligned in a 16-bit boundary, then ASSERT().
688 If String has only pad spaces, then 0 is returned.
689 If String has no pad spaces or valid decimal digits,
691 If the number represented by String overflows according
692 to the range defined by UINT64, then ASSERT().
694 If PcdMaximumUnicodeStringLength is not zero, and String contains
695 more than PcdMaximumUnicodeStringLength Unicode characters not including
696 the Null-terminator, then ASSERT().
698 @param String Pointer to a Null-terminated Unicode string.
706 IN CONST CHAR16
*String
711 ASSERT (String
!= NULL
);
712 ASSERT (((UINTN
) String
& 0x01) == 0);
713 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
716 // Ignore the pad spaces (space or tab)
718 while ((*String
== L
' ') || (*String
== L
'\t')) {
723 // Ignore leading Zeros after the spaces
725 while (*String
== L
'0') {
731 while (InternalIsDecimalDigitCharacter (*String
)) {
733 // If the number represented by String overflows according
734 // to the range defined by UINTN, then ASSERT().
736 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
737 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
738 (*String
- L
'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
741 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
749 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
751 This function returns a value of type UINTN by interpreting the contents
752 of the Unicode string specified by String as a hexadecimal number.
753 The format of the input Unicode string String is:
755 [spaces][zeros][x][hexadecimal digits].
757 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
758 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
759 If "x" appears in the input string, it must be prefixed with at least one 0.
760 The function will ignore the pad space, which includes spaces or tab characters,
761 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
762 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
763 first valid hexadecimal digit. Then, the function stops at the first character that is
764 a not a valid hexadecimal character or NULL, whichever one comes first.
766 If String is NULL, then ASSERT().
767 If String is not aligned in a 16-bit boundary, then ASSERT().
768 If String has only pad spaces, then zero is returned.
769 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
770 then zero is returned.
771 If the number represented by String overflows according to the range defined by
772 UINTN, then ASSERT().
774 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
775 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
778 @param String Pointer to a Null-terminated Unicode string.
786 IN CONST CHAR16
*String
791 ASSERT (String
!= NULL
);
792 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
795 // Ignore the pad spaces (space or tab)
797 while ((*String
== L
' ') || (*String
== L
'\t')) {
802 // Ignore leading Zeros after the spaces
804 while (*String
== L
'0') {
808 if (InternalCharToUpper (*String
) == L
'X') {
809 ASSERT (*(String
- 1) == L
'0');
810 if (*(String
- 1) != L
'0') {
821 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
823 // If the Hex Number represented by String overflows according
824 // to the range defined by UINTN, then ASSERT().
826 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
827 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
828 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
831 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
840 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
842 This function returns a value of type UINT64 by interpreting the contents
843 of the Unicode string specified by String as a hexadecimal number.
844 The format of the input Unicode string String is
846 [spaces][zeros][x][hexadecimal digits].
848 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
849 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
850 If "x" appears in the input string, it must be prefixed with at least one 0.
851 The function will ignore the pad space, which includes spaces or tab characters,
852 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
853 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
854 first valid hexadecimal digit. Then, the function stops at the first character that is
855 a not a valid hexadecimal character or NULL, whichever one comes first.
857 If String is NULL, then ASSERT().
858 If String is not aligned in a 16-bit boundary, then ASSERT().
859 If String has only pad spaces, then zero is returned.
860 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
861 then zero is returned.
862 If the number represented by String overflows according to the range defined by
863 UINT64, then ASSERT().
865 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
866 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
869 @param String Pointer to a Null-terminated Unicode string.
877 IN CONST CHAR16
*String
882 ASSERT (String
!= NULL
);
883 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
886 // Ignore the pad spaces (space or tab)
888 while ((*String
== L
' ') || (*String
== L
'\t')) {
893 // Ignore leading Zeros after the spaces
895 while (*String
== L
'0') {
899 if (InternalCharToUpper (*String
) == L
'X') {
900 ASSERT (*(String
- 1) == L
'0');
901 if (*(String
- 1) != L
'0') {
912 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
914 // If the Hex Number represented by String overflows according
915 // to the range defined by UINTN, then ASSERT().
917 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
)||
918 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
919 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
922 Result
= LShiftU64 (Result
, 4);
923 Result
= Result
+ InternalHexCharToUintn (*String
);
931 Check if a ASCII character is a decimal character.
933 This internal function checks if a Unicode character is a
934 decimal character. The valid decimal character is from
937 @param Char The character to check against.
939 @retval TRUE If the Char is a decmial character.
940 @retval FALSE Otherwise.
945 InternalAsciiIsDecimalDigitCharacter (
949 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
953 Check if a ASCII character is a hexadecimal character.
955 This internal function checks if a ASCII character is a
956 decimal character. The valid hexadecimal character is
957 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
960 @param Char The character to check against.
962 @retval TRUE If the Char is a hexadecmial character.
963 @retval FALSE Otherwise.
968 InternalAsciiIsHexaDecimalDigitCharacter (
973 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
974 (Char
>= 'A' && Char
<= 'F') ||
975 (Char
>= 'a' && Char
<= 'f'));
979 Convert a Null-terminated Unicode string to a Null-terminated
980 ASCII string and returns the ASCII string.
982 This function converts the content of the Unicode string Source
983 to the ASCII string Destination by copying the lower 8 bits of
984 each Unicode character. It returns Destination. The function terminates
985 the ASCII string Destination by appending a Null-terminator character
986 at the end. The caller is responsible to make sure Destination points
987 to a buffer with size equal or greater than (StrLen (Source) + 1) in bytes.
989 If Destination is NULL, then ASSERT().
990 If Source is NULL, then ASSERT().
991 If Source is not aligned on a 16-bit boundary, then ASSERT().
992 If Source and Destination overlap, then ASSERT().
994 If any Unicode characters in Source contain non-zero value in
995 the upper 8 bits, then ASSERT().
997 If PcdMaximumUnicodeStringLength is not zero, and Source contains
998 more than PcdMaximumUnicodeStringLength Unicode characters not including
999 the Null-terminator, then ASSERT().
1001 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1002 than PcdMaximumAsciiStringLength Unicode characters not including the
1003 Null-terminator, then ASSERT().
1005 @param Source Pointer to a Null-terminated Unicode string.
1006 @param Destination Pointer to a Null-terminated ASCII string.
1013 UnicodeStrToAsciiStr (
1014 IN CONST CHAR16
*Source
,
1015 OUT CHAR8
*Destination
1018 ASSERT (Destination
!= NULL
);
1019 ASSERT (Source
!= NULL
);
1022 // Source and Destination should not overlap
1024 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1025 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1028 // If PcdMaximumUnicodeStringLength is not zero,
1029 // length of Source should not more than PcdMaximumUnicodeStringLength
1031 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
1032 ASSERT (StrLen (Source
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1035 while (*Source
!= '\0') {
1037 // If any Unicode characters in Source contain
1038 // non-zero value in the upper 8 bits, then ASSERT().
1040 ASSERT (*Source
< 0x100);
1041 *(Destination
++) = (CHAR8
) *(Source
++);
1044 *Destination
= '\0';
1051 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1052 string and returns the new ASCII string.
1054 This function copies the contents of the ASCII string Source to the ASCII
1055 string Destination, and returns Destination. If Source and Destination
1056 overlap, then the results are undefined.
1058 If Destination is NULL, then ASSERT().
1059 If Source is NULL, then ASSERT().
1060 If Source and Destination overlap, then ASSERT().
1061 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1062 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1065 @param Destination Pointer to a Null-terminated ASCII string.
1066 @param Source Pointer to a Null-terminated ASCII string.
1074 OUT CHAR8
*Destination
,
1075 IN CONST CHAR8
*Source
1081 // Destination cannot be NULL
1083 ASSERT (Destination
!= NULL
);
1086 // Destination and source cannot overlap
1088 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1089 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1091 ReturnValue
= Destination
;
1093 *(Destination
++) = *(Source
++);
1100 Copies one Null-terminated ASCII string with a maximum length to another
1101 Null-terminated ASCII string with a maximum length and returns the new ASCII
1104 This function copies the contents of the ASCII string Source to the ASCII
1105 string Destination, and returns Destination. At most, Length ASCII characters
1106 are copied from Source to Destination. If Length is 0, then Destination is
1107 returned unmodified. If Length is greater that the number of ASCII characters
1108 in Source, then Destination is padded with Null ASCII characters. If Source
1109 and Destination overlap, then the results are undefined.
1111 If Destination is NULL, then ASSERT().
1112 If Source is NULL, then ASSERT().
1113 If Source and Destination overlap, then ASSERT().
1114 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1115 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1118 @param Destination Pointer to a Null-terminated ASCII string.
1119 @param Source Pointer to a Null-terminated ASCII string.
1120 @param Length Maximum number of ASCII characters to copy.
1128 OUT CHAR8
*Destination
,
1129 IN CONST CHAR8
*Source
,
1140 // Destination cannot be NULL
1142 ASSERT (Destination
!= NULL
);
1145 // Destination and source cannot overlap
1147 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1148 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1150 ReturnValue
= Destination
;
1152 while (*Source
&& Length
> 0) {
1153 *(Destination
++) = *(Source
++);
1157 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1162 Returns the length of a Null-terminated ASCII string.
1164 This function returns the number of ASCII characters in the Null-terminated
1165 ASCII string specified by String.
1167 If String is NULL, then ASSERT().
1168 If PcdMaximumAsciiStringLength is not zero and String contains more than
1169 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1172 @param String Pointer to a Null-terminated ASCII string.
1174 @return The length of String.
1180 IN CONST CHAR8
*String
1185 ASSERT (String
!= NULL
);
1187 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1189 // If PcdMaximumUnicodeStringLength is not zero,
1190 // length should not more than PcdMaximumUnicodeStringLength
1192 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1193 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1200 Returns the size of a Null-terminated ASCII string in bytes, including the
1203 This function returns the size, in bytes, of the Null-terminated ASCII string
1204 specified by String.
1206 If String is NULL, then ASSERT().
1207 If PcdMaximumAsciiStringLength is not zero and String contains more than
1208 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1211 @param String Pointer to a Null-terminated ASCII string.
1213 @return The size of String.
1219 IN CONST CHAR8
*String
1222 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1226 Compares two Null-terminated ASCII strings, and returns the difference
1227 between the first mismatched ASCII characters.
1229 This function compares the Null-terminated ASCII string FirstString to the
1230 Null-terminated ASCII string SecondString. If FirstString is identical to
1231 SecondString, then 0 is returned. Otherwise, the value returned is the first
1232 mismatched ASCII character in SecondString subtracted from the first
1233 mismatched ASCII character in FirstString.
1235 If FirstString is NULL, then ASSERT().
1236 If SecondString is NULL, then ASSERT().
1237 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1238 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1240 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1241 than PcdMaximumAsciiStringLength ASCII characters not including the
1242 Null-terminator, then ASSERT().
1244 @param FirstString Pointer to a Null-terminated ASCII string.
1245 @param SecondString Pointer to a Null-terminated ASCII string.
1247 @retval 0 FirstString is identical to SecondString.
1248 @retval !=0 FirstString is not identical to SecondString.
1254 IN CONST CHAR8
*FirstString
,
1255 IN CONST CHAR8
*SecondString
1259 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1261 ASSERT (AsciiStrSize (FirstString
));
1262 ASSERT (AsciiStrSize (SecondString
));
1264 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1269 return *FirstString
- *SecondString
;
1273 Converts a lowercase Ascii character to upper one
1275 If Chr is lowercase Ascii character, then converts it to upper one.
1277 If Value >= 0xA0, then ASSERT().
1278 If (Value & 0x0F) >= 0x0A, then ASSERT().
1280 @param chr one Ascii character
1282 @return The uppercase value of Ascii character
1291 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1295 Convert a ASCII character to numerical value.
1297 This internal function only deal with Unicode character
1298 which maps to a valid hexadecimal ASII character, i.e.
1299 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1300 ASCII character, the value returned does not make sense.
1302 @param Char The character to convert.
1304 @retval UINTN The numerical value converted.
1309 InternalAsciiHexCharToUintn (
1313 if (InternalIsDecimalDigitCharacter (Char
)) {
1317 return (UINTN
) (10 + AsciiToUpper (Char
) - 'A');
1322 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1323 and returns the difference between the first mismatched ASCII characters.
1325 This function performs a case insensitive comparison of the Null-terminated
1326 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1327 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1328 value returned is the first mismatched lower case ASCII character in
1329 SecondString subtracted from the first mismatched lower case ASCII character
1332 If FirstString is NULL, then ASSERT().
1333 If SecondString is NULL, then ASSERT().
1334 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1335 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1337 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1338 than PcdMaximumAsciiStringLength ASCII characters not including the
1339 Null-terminator, then ASSERT().
1341 @param FirstString Pointer to a Null-terminated ASCII string.
1342 @param SecondString Pointer to a Null-terminated ASCII string.
1344 @retval 0 FirstString is identical to SecondString using case insensitive
1346 @retval !=0 FirstString is not identical to SecondString using case
1347 insensitive comparisons.
1353 IN CONST CHAR8
*FirstString
,
1354 IN CONST CHAR8
*SecondString
1357 CHAR8 UpperFirstString
;
1358 CHAR8 UpperSecondString
;
1361 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1363 ASSERT (AsciiStrSize (FirstString
));
1364 ASSERT (AsciiStrSize (SecondString
));
1366 UpperFirstString
= AsciiToUpper (*FirstString
);
1367 UpperSecondString
= AsciiToUpper (*SecondString
);
1368 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1371 UpperFirstString
= AsciiToUpper (*FirstString
);
1372 UpperSecondString
= AsciiToUpper (*SecondString
);
1375 return UpperFirstString
- UpperSecondString
;
1379 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1380 the difference between the first mismatched ASCII characters.
1382 This function compares the Null-terminated ASCII string FirstString to the
1383 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1384 will be compared. If Length is 0, then 0 is returned. If FirstString is
1385 identical to SecondString, then 0 is returned. Otherwise, the value returned
1386 is the first mismatched ASCII character in SecondString subtracted from the
1387 first mismatched ASCII character in FirstString.
1389 If FirstString is NULL, then ASSERT().
1390 If SecondString is NULL, then ASSERT().
1391 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1392 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1394 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
1395 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1398 @param FirstString Pointer to a Null-terminated ASCII string.
1399 @param SecondString Pointer to a Null-terminated ASCII string.
1401 @retval 0 FirstString is identical to SecondString.
1402 @retval !=0 FirstString is not identical to SecondString.
1408 IN CONST CHAR8
*FirstString
,
1409 IN CONST CHAR8
*SecondString
,
1418 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1420 ASSERT (AsciiStrSize (FirstString
));
1421 ASSERT (AsciiStrSize (SecondString
));
1423 while ((*FirstString
!= '\0') &&
1424 (*FirstString
== *SecondString
) &&
1430 return *FirstString
- *SecondString
;
1434 Concatenates one Null-terminated ASCII string to another Null-terminated
1435 ASCII string, and returns the concatenated ASCII string.
1437 This function concatenates two Null-terminated ASCII strings. The contents of
1438 Null-terminated ASCII string Source are concatenated to the end of Null-
1439 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1442 If Destination is NULL, then ASSERT().
1443 If Source is NULL, then ASSERT().
1444 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1445 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1447 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1448 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1450 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1451 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1452 ASCII characters, then ASSERT().
1454 @param Destination Pointer to a Null-terminated ASCII string.
1455 @param Source Pointer to a Null-terminated ASCII string.
1463 IN OUT CHAR8
*Destination
,
1464 IN CONST CHAR8
*Source
1467 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1470 // Size of the resulting string should never be zero.
1471 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1473 ASSERT (AsciiStrSize (Destination
) != 0);
1478 Concatenates one Null-terminated ASCII string with a maximum length to the
1479 end of another Null-terminated ASCII string, and returns the concatenated
1482 This function concatenates two Null-terminated ASCII strings. The contents
1483 of Null-terminated ASCII string Source are concatenated to the end of Null-
1484 terminated ASCII string Destination, and Destination is returned. At most,
1485 Length ASCII characters are concatenated from Source to the end of
1486 Destination, and Destination is always Null-terminated. If Length is 0, then
1487 Destination is returned unmodified. If Source and Destination overlap, then
1488 the results are undefined.
1490 If Destination is NULL, then ASSERT().
1491 If Source is NULL, then ASSERT().
1492 If Source and Destination overlap, then ASSERT().
1493 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1494 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1496 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1497 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1499 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1500 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1501 ASCII characters not including the Null-terminator, then ASSERT().
1503 @param Destination Pointer to a Null-terminated ASCII string.
1504 @param Source Pointer to a Null-terminated ASCII string.
1505 @param Length Maximum number of ASCII characters to concatenate from
1514 IN OUT CHAR8
*Destination
,
1515 IN CONST CHAR8
*Source
,
1519 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
1522 // Size of the resulting string should never be zero.
1523 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1525 ASSERT (AsciiStrSize (Destination
) != 0);
1530 Returns the first occurance of a Null-terminated ASCII sub-string
1531 in a Null-terminated ASCII string.
1533 This function scans the contents of the ASCII string specified by String
1534 and returns the first occurrence of SearchString. If SearchString is not
1535 found in String, then NULL is returned. If the length of SearchString is zero,
1536 then String is returned.
1538 If String is NULL, then ASSERT().
1539 If SearchString is NULL, then ASSERT().
1541 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1542 String contains more than PcdMaximumAsciiStringLength Unicode characters
1543 not including the Null-terminator, then ASSERT().
1545 @param String Pointer to a Null-terminated ASCII string.
1546 @param SearchString Pointer to a Null-terminated ASCII string to search for.
1548 @retval NULL If the SearchString does not appear in String.
1549 @retval !NULL If there is a match.
1555 IN CONST CHAR8
*String
,
1556 IN CONST CHAR8
*SearchString
1559 CONST CHAR8
*FirstMatch
;
1560 CONST CHAR8
*SearchStringTmp
;
1562 ASSERT (String
!= NULL
);
1563 ASSERT (SearchString
!= NULL
);
1566 // If PcdMaximumUnicodeStringLength is not zero,
1567 // length of String should not more than PcdMaximumUnicodeStringLength
1569 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1570 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1574 // If PcdMaximumUnicodeStringLength is not zero,
1575 // length of SearchString should not more than PcdMaximumUnicodeStringLength
1577 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1578 ASSERT (AsciiStrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1581 while (*String
!= '\0') {
1582 SearchStringTmp
= SearchString
;
1583 FirstMatch
= String
;
1585 while ((*String
== *SearchStringTmp
)
1586 && (*SearchStringTmp
!= '\0')
1587 && (*String
!= '\0')) {
1592 if (*SearchStringTmp
== '\0') {
1593 return (CHAR8
*) FirstMatch
;
1596 if (SearchStringTmp
== SearchString
) {
1598 // If no character from SearchString match,
1599 // move the pointer to the String under search
1600 // by one character.
1611 Convert a Null-terminated ASCII decimal string to a value of type
1614 This function returns a value of type UINTN by interpreting the contents
1615 of the ASCII string String as a decimal number. The format of the input
1616 ASCII string String is:
1618 [spaces] [decimal digits].
1620 The valid decimal digit character is in the range [0-9]. The function will
1621 ignore the pad space, which includes spaces or tab characters, before the digits.
1622 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1623 function stops at the first character that is a not a valid decimal character or
1624 Null-terminator, whichever on comes first.
1626 If String has only pad spaces, then 0 is returned.
1627 If String has no pad spaces or valid decimal digits, then 0 is returned.
1628 If the number represented by String overflows according to the range defined by
1629 UINTN, then ASSERT().
1630 If String is NULL, then ASSERT().
1631 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1632 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1635 @param String Pointer to a Null-terminated ASCII string.
1642 AsciiStrDecimalToUintn (
1643 IN CONST CHAR8
*String
1648 ASSERT (String
!= NULL
);
1649 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1652 // Ignore the pad spaces (space or tab)
1654 while ((*String
== ' ') || (*String
== '\t')) {
1659 // Ignore leading Zeros after the spaces
1661 while (*String
== '0') {
1667 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1669 // If the number represented by String overflows according
1670 // to the range defined by UINTN, then ASSERT().
1672 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1673 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1674 (*String
- '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
1677 Result
= Result
* 10 + (*String
- '0');
1686 Convert a Null-terminated ASCII decimal string to a value of type
1689 This function returns a value of type UINT64 by interpreting the contents
1690 of the ASCII string String as a decimal number. The format of the input
1691 ASCII string String is:
1693 [spaces] [decimal digits].
1695 The valid decimal digit character is in the range [0-9]. The function will
1696 ignore the pad space, which includes spaces or tab characters, before the digits.
1697 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1698 function stops at the first character that is a not a valid decimal character or
1699 Null-terminator, whichever on comes first.
1701 If String has only pad spaces, then 0 is returned.
1702 If String has no pad spaces or valid decimal digits, then 0 is returned.
1703 If the number represented by String overflows according to the range defined by
1704 UINT64, then ASSERT().
1705 If String is NULL, then ASSERT().
1706 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1707 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1710 @param String Pointer to a Null-terminated ASCII string.
1717 AsciiStrDecimalToUint64 (
1718 IN CONST CHAR8
*String
1723 ASSERT (String
!= NULL
);
1724 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1727 // Ignore the pad spaces (space or tab)
1729 while ((*String
== ' ') || (*String
== '\t')) {
1734 // Ignore leading Zeros after the spaces
1736 while (*String
== '0') {
1742 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1744 // If the number represented by String overflows according
1745 // to the range defined by UINTN, then ASSERT().
1747 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1748 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1749 (*String
- '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
1752 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1760 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1762 This function returns a value of type UINTN by interpreting the contents of
1763 the ASCII string String as a hexadecimal number. The format of the input ASCII
1766 [spaces][zeros][x][hexadecimal digits].
1768 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1769 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1770 appears in the input string, it must be prefixed with at least one 0. The function
1771 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1772 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1773 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1774 digit. Then, the function stops at the first character that is a not a valid
1775 hexadecimal character or Null-terminator, whichever on comes first.
1777 If String has only pad spaces, then 0 is returned.
1778 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1781 If the number represented by String overflows according to the range defined by UINTN,
1783 If String is NULL, then ASSERT().
1784 If PcdMaximumAsciiStringLength is not zero,
1785 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1786 the Null-terminator, then ASSERT().
1788 @param String Pointer to a Null-terminated ASCII string.
1795 AsciiStrHexToUintn (
1796 IN CONST CHAR8
*String
1801 ASSERT (String
!= NULL
);
1802 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1805 // Ignore the pad spaces (space or tab)
1807 while ((*String
== ' ') || (*String
== '\t')) {
1812 // Ignore leading Zeros after the spaces
1814 while (*String
== '0') {
1818 if (AsciiToUpper (*String
) == 'X') {
1819 ASSERT (*(String
- 1) == '0');
1820 if (*(String
- 1) != '0') {
1831 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1833 // If the Hex Number represented by String overflows according
1834 // to the range defined by UINTN, then ASSERT().
1836 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1837 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1838 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
1841 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1850 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1852 This function returns a value of type UINT64 by interpreting the contents of
1853 the ASCII string String as a hexadecimal number. The format of the input ASCII
1856 [spaces][zeros][x][hexadecimal digits].
1858 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1859 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1860 appears in the input string, it must be prefixed with at least one 0. The function
1861 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1862 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1863 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1864 digit. Then, the function stops at the first character that is a not a valid
1865 hexadecimal character or Null-terminator, whichever on comes first.
1867 If String has only pad spaces, then 0 is returned.
1868 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1871 If the number represented by String overflows according to the range defined by UINT64,
1873 If String is NULL, then ASSERT().
1874 If PcdMaximumAsciiStringLength is not zero,
1875 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1876 the Null-terminator, then ASSERT().
1878 @param String Pointer to a Null-terminated ASCII string.
1885 AsciiStrHexToUint64 (
1886 IN CONST CHAR8
*String
1891 ASSERT (String
!= NULL
);
1892 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1895 // Ignore the pad spaces (space or tab) and leading Zeros
1898 // Ignore the pad spaces (space or tab)
1900 while ((*String
== ' ') || (*String
== '\t')) {
1905 // Ignore leading Zeros after the spaces
1907 while (*String
== '0') {
1911 if (AsciiToUpper (*String
) == 'X') {
1912 ASSERT (*(String
- 1) == '0');
1913 if (*(String
- 1) != '0') {
1924 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1926 // If the Hex Number represented by String overflows according
1927 // to the range defined by UINTN, then ASSERT().
1929 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1930 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1931 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
1934 Result
= LShiftU64 (Result
, 4);
1935 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1944 Convert one Null-terminated ASCII string to a Null-terminated
1945 Unicode string and returns the Unicode string.
1947 This function converts the contents of the ASCII string Source to the Unicode
1948 string Destination, and returns Destination. The function terminates the
1949 Unicode string Destination by appending a Null-terminator character at the end.
1950 The caller is responsible to make sure Destination points to a buffer with size
1951 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1953 If Destination is NULL, then ASSERT().
1954 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1955 If Source is NULL, then ASSERT().
1956 If Source and Destination overlap, then ASSERT().
1957 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1958 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1960 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1961 PcdMaximumUnicodeStringLength ASCII characters not including the
1962 Null-terminator, then ASSERT().
1964 @param Source Pointer to a Null-terminated ASCII string.
1965 @param Destination Pointer to a Null-terminated Unicode string.
1972 AsciiStrToUnicodeStr (
1973 IN CONST CHAR8
*Source
,
1974 OUT CHAR16
*Destination
1977 ASSERT (Destination
!= NULL
);
1978 ASSERT (Source
!= NULL
);
1981 // Source and Destination should not overlap
1983 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
1984 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) > (AsciiStrLen (Source
) * sizeof (CHAR16
)));
1987 // If PcdMaximumAsciiStringLength is not zero,
1988 // length of Source should not more than PcdMaximumUnicodeStringLength
1990 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1991 ASSERT (AsciiStrLen (Source
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1994 while (*Source
!= '\0') {
1995 *(Destination
++) = (CHAR16
) *(Source
++);
1998 // End the Destination with a NULL.
2000 *Destination
= '\0';
2006 Converts an 8-bit value to an 8-bit BCD value.
2008 Converts the 8-bit value specified by Value to BCD. The BCD value is
2011 If Value >= 100, then ASSERT().
2013 @param Value The 8-bit value to convert to BCD. Range 0..99.
2015 @return The BCD value
2024 ASSERT (Value
< 100);
2025 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2029 Converts an 8-bit BCD value to an 8-bit value.
2031 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2034 If Value >= 0xA0, then ASSERT().
2035 If (Value & 0x0F) >= 0x0A, then ASSERT().
2037 @param Value The 8-bit BCD value to convert to an 8-bit value.
2039 @return The 8-bit value is returned.
2048 ASSERT (Value
< 0xa0);
2049 ASSERT ((Value
& 0xf) < 0xa);
2050 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));